4040import org .biojava .nbio .core .sequence .io .template .SequenceHeaderParserInterface ;
4141import org .biojava .nbio .core .sequence .template .AbstractSequence ;
4242import org .biojava .nbio .core .sequence .template .Compound ;
43+ import org .slf4j .Logger ;
44+ import org .slf4j .LoggerFactory ;
4345
4446import java .io .*;
4547import java .util .ArrayList ;
@@ -55,20 +57,28 @@ public class GenbankReader<S extends AbstractSequence<C>, C extends Compound> {
5557
5658 private SequenceCreatorInterface <C > sequenceCreator ;
5759 private GenbankSequenceParser <S ,C > genbankParser ;
58- private InputStream inputStream ;
60+ private BufferedReader bufferedReader ;
61+ private boolean closed ;
62+ private final Logger logger = LoggerFactory .getLogger (this .getClass ());
63+
64+ public boolean isClosed () {
65+ return closed ;
66+ }
5967
6068 /**
6169 * If you are going to use FileProxyProteinSequenceCreator then do not use this constructor because we need details about
6270 * local file offsets for quick reads. InputStreams does not give you the name of the stream to access quickly via file seek. A seek in
6371 * an inputstream is forced to read all the data so you don't gain anything.
64- * @param br
72+ * @param is
6573 * @param headerParser
6674 * @param sequenceCreator
6775 */
68- public GenbankReader (InputStream is , SequenceHeaderParserInterface <S ,C > headerParser , SequenceCreatorInterface <C > sequenceCreator ) {
76+ public GenbankReader (final InputStream is , final SequenceHeaderParserInterface <S ,C > headerParser ,
77+ final SequenceCreatorInterface <C > sequenceCreator ) {
6978 this .sequenceCreator = sequenceCreator ;
70- this .inputStream = is ;
71- genbankParser = new GenbankSequenceParser <S ,C >();
79+ bufferedReader = new BufferedReader (new InputStreamReader (is ));
80+ genbankParser = new GenbankSequenceParser <>();
81+ closed = false ;
7282 }
7383
7484 /**
@@ -85,14 +95,14 @@ public GenbankReader(InputStream is, SequenceHeaderParserInterface<S,C> headerPa
8595 * method denies read access to the file.
8696 */
8797 public GenbankReader (
88- File file ,
89- SequenceHeaderParserInterface <S ,C > headerParser ,
90- SequenceCreatorInterface <C > sequenceCreator
98+ final File file ,
99+ final SequenceHeaderParserInterface <S ,C > headerParser ,
100+ final SequenceCreatorInterface <C > sequenceCreator
91101 ) throws FileNotFoundException {
92102
93- inputStream = new FileInputStream ( file );
103+ this . bufferedReader = new BufferedReader ( new FileReader ( file ) );
94104 this .sequenceCreator = sequenceCreator ;
95- genbankParser = new GenbankSequenceParser <S , C >();
105+ genbankParser = new GenbankSequenceParser <>();
96106 }
97107
98108 /**
@@ -108,8 +118,7 @@ public GenbankReader(
108118 * @throws CompoundNotFoundException
109119 */
110120 public LinkedHashMap <String ,S > process () throws IOException , CompoundNotFoundException {
111- LinkedHashMap <String ,S > sequences = process (-1 );
112- return sequences ;
121+ return process (-1 );
113122 }
114123
115124 /**
@@ -122,7 +131,7 @@ public LinkedHashMap<String,S> process() throws IOException, CompoundNotFoundExc
122131 * time before the first result is available.<br>
123132 * <b>N.B.</b>
124133 * <ul>
125- * <li>This method ca 't be called after calling its NO-ARGUMENT twin.</li>
134+ * <li>This method can 't be called after calling its NO-ARGUMENT twin.</li>
126135 * <li>remember to close the underlying resource when you are done.</li>
127136 * </ul>
128137 * @see #process()
@@ -134,17 +143,17 @@ public LinkedHashMap<String,S> process() throws IOException, CompoundNotFoundExc
134143 * @throws IOException
135144 * @throws CompoundNotFoundException
136145 */
137- public LinkedHashMap <String ,S > process (int max ) throws IOException , CompoundNotFoundException {
138- LinkedHashMap <String ,S > sequences = new LinkedHashMap <String , S >();
146+ public LinkedHashMap <String ,S > process (final int max ) throws IOException , CompoundNotFoundException {
147+ LinkedHashMap <String ,S > sequences = new LinkedHashMap <>();
139148 @ SuppressWarnings ("unchecked" )
140149 int i =0 ;
141- BufferedReader br = new BufferedReader (new InputStreamReader (inputStream ));
142150 while (true ) {
143151 if (max >0 && i >=max ) break ;
144152 i ++;
145- String seqString = genbankParser .getSequence (br , 0 );
153+ String seqString = genbankParser .getSequence (bufferedReader , 0 );
146154 //reached end of file?
147155 if (seqString ==null ) break ;
156+ @ SuppressWarnings ("unchecked" )
148157 S sequence = (S ) sequenceCreator .getSequence (seqString , 0 );
149158 genbankParser .getSequenceHeaderParser ().parseHeader (genbankParser .getHeader (), sequence );
150159
@@ -165,32 +174,41 @@ public LinkedHashMap<String,S> process(int max) throws IOException, CompoundNotF
165174
166175 sequences .put (sequence .getAccession ().getID (), sequence );
167176 }
168- br .close ();
169- close ();
177+
178+ if (max < 0 ) {
179+ close ();
180+ }
181+
170182 return sequences ;
171183 }
172184
173- public void close () throws IOException {
174- inputStream .close ();
185+ public void close () {
186+ try {
187+ bufferedReader .close ();
188+ this .closed = true ;
189+ } catch (IOException e ) {
190+ logger .error ("Couldn't close the reader. {}" , e .getMessage ());
191+ this .closed = false ;
192+ }
175193 }
176194
177195 public static void main (String [] args ) throws Exception {
178196 String proteinFile = "src/test/resources/BondFeature.gb" ;
179197 FileInputStream is = new FileInputStream (proteinFile );
180198
181- GenbankReader <ProteinSequence , AminoAcidCompound > proteinReader = new GenbankReader <ProteinSequence , AminoAcidCompound >(is , new GenericGenbankHeaderParser <ProteinSequence , AminoAcidCompound >(), new ProteinSequenceCreator (AminoAcidCompoundSet .getAminoAcidCompoundSet ()));
199+ GenbankReader <ProteinSequence , AminoAcidCompound > proteinReader = new GenbankReader <>(is , new GenericGenbankHeaderParser <>(), new ProteinSequenceCreator (AminoAcidCompoundSet .getAminoAcidCompoundSet ()));
182200 LinkedHashMap <String ,ProteinSequence > proteinSequences = proteinReader .process ();
183201 System .out .println (proteinSequences );
184202
185203 String inputFile = "src/test/resources/NM_000266.gb" ;
186204 is = new FileInputStream (inputFile );
187- GenbankReader <DNASequence , NucleotideCompound > dnaReader = new GenbankReader <DNASequence , NucleotideCompound >(is , new GenericGenbankHeaderParser <DNASequence , NucleotideCompound >(), new DNASequenceCreator (DNACompoundSet .getDNACompoundSet ()));
205+ GenbankReader <DNASequence , NucleotideCompound > dnaReader = new GenbankReader <>(is , new GenericGenbankHeaderParser <>(), new DNASequenceCreator (DNACompoundSet .getDNACompoundSet ()));
188206 LinkedHashMap <String ,DNASequence > dnaSequences = dnaReader .process ();
189207 System .out .println (dnaSequences );
190208
191209 String crazyFile = "src/test/resources/CraftedFeature.gb" ;
192210 is = new FileInputStream (crazyFile );
193- GenbankReader <DNASequence , NucleotideCompound > crazyReader = new GenbankReader <DNASequence , NucleotideCompound >(is , new GenericGenbankHeaderParser <DNASequence , NucleotideCompound >(), new DNASequenceCreator (DNACompoundSet .getDNACompoundSet ()));
211+ GenbankReader <DNASequence , NucleotideCompound > crazyReader = new GenbankReader <>(is , new GenericGenbankHeaderParser <>(), new DNASequenceCreator (DNACompoundSet .getDNACompoundSet ()));
194212 LinkedHashMap <String ,DNASequence > crazyAnnotatedSequences = crazyReader .process ();
195213
196214 is .close ();
0 commit comments