001/** 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021 022package org.biojava.bio.seq.io; 023 024import java.io.NotSerializableException; 025import java.io.ObjectStreamException; 026import java.io.Serializable; 027 028import org.biojava.bio.BioException; 029import org.biojava.bio.seq.Sequence; 030import org.biojava.bio.seq.impl.SimpleSequence; 031import org.biojava.bio.symbol.Alphabet; 032import org.biojava.bio.symbol.IllegalAlphabetException; 033import org.biojava.bio.symbol.PackedSymbolListFactory; 034import org.biojava.bio.symbol.Symbol; 035import org.biojava.bio.symbol.SymbolList; 036import org.biojava.utils.StaticMemberPlaceHolder; 037 038/** 039 * Basic SequenceBuilder implementation which accumulates all 040 * notified information and chooses a sequence implementation 041 * suited to the size of the sequence. This may or may not bit-encode 042 * the symbols (using PackedSymbolList), and may or may not store the 043 * symbols in multiple fixed-length chunks (using ChunkedSymbolList). 044 * 045 * <p>More functionality is offered by {@link org.biojavax.bio.seq.io.SimpleRichSequenceBuilder SimpleRichSequenceBuilder}, 046 * Use of this class is prefered.</p> 047 * 048 * @author David Huen 049 * @author Matthew Pocock 050 */ 051 052public class SmartSequenceBuilder extends SequenceBuilderBase { 053 public final static SequenceBuilderFactory FACTORY = new SSBFactory(-1); 054 public final static SequenceBuilderFactory BIT_PACKED = new SSBFactory(0); 055 056 private static class SSBFactory implements SequenceBuilderFactory, Serializable { 057 private final int threshold; 058 059 private SSBFactory(int threshold) { 060 this.threshold = threshold; 061 } 062 063 public SequenceBuilder makeSequenceBuilder() { 064 return new SmartSequenceBuilder(threshold); 065 } 066 067 private Object writeReplace() throws ObjectStreamException { 068 try { 069 return new StaticMemberPlaceHolder(SimpleSequenceBuilder.class.getField("FACTORY")); 070 } catch (NoSuchFieldException nsfe) { 071 throw new NotSerializableException(nsfe.getMessage()); 072 } 073 } 074 } 075 076 private ChunkedSymbolListFactory slFactory; 077 078 private SmartSequenceBuilder(int threshold) { 079 slFactory = new ChunkedSymbolListFactory( 080 new PackedSymbolListFactory(), threshold); 081 } 082 083 // 084 // SeqIOListener 085 // 086 087 public void addSymbols(Alphabet alpha, Symbol[] syms, int pos, int len) 088 throws IllegalAlphabetException 089 { 090 slFactory.addSymbols(alpha, syms, pos, len); 091 } 092 093 094 public Sequence makeSequence() 095 throws BioException 096 { 097 SymbolList symbols; 098 try { 099 symbols = slFactory.makeSymbolList(); 100 seq = new SimpleSequence(symbols, uri, name, annotation); 101 } 102 catch (IllegalAlphabetException iae) { 103 // this shouldn't happen!!! 104 } 105 106 return super.makeSequence(); 107 } 108}