3434import org .biojava .nbio .core .sequence .compound .AminoAcidCompoundSet ;
3535import org .biojava .nbio .core .sequence .compound .DNACompoundSet ;
3636import org .biojava .nbio .core .sequence .compound .NucleotideCompound ;
37- import org .biojava .nbio .core .sequence .features .AbstractFeature ;
3837import org .biojava .nbio .core .sequence .features .DBReferenceInfo ;
3938import org .biojava .nbio .core .sequence .io .template .SequenceCreatorInterface ;
4039import org .biojava .nbio .core .sequence .io .template .SequenceHeaderParserInterface ;
4746import java .util .ArrayList ;
4847import java .util .HashMap ;
4948import java .util .LinkedHashMap ;
49+ import java .util .List ;
5050
5151/**
52- * Use GenbankReaderHelper as an example of how to use this class where GenbankReaderHelper should be the
52+ * Use {@link GenbankReaderHelper} as an example of how to use this class where {@link GenbankReaderHelper} should be the
5353 * primary class used to read Genbank files
5454 *
5555 */
@@ -66,9 +66,9 @@ public boolean isClosed() {
6666 }
6767
6868 /**
69- * If you are going to use FileProxyProteinSequenceCreator then do not use this constructor because we need details about
70- * 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
71- * an inputstream is forced to read all the data so you don't gain anything.
69+ * If you are going to use {@link FileProxyProteinSequenceCreator} then do not use this constructor because we need details about
70+ * local file offsets for quick reads. {@link InputStream} does not give you the name of the stream to access quickly via file seek. A seek in
71+ * an {@link InputStream} is forced to read all the data so you don't gain anything.
7272 * @param is
7373 * @param headerParser
7474 * @param sequenceCreator
@@ -107,18 +107,21 @@ public GenbankReader(
107107
108108 /**
109109 * The parsing is done in this method.<br>
110- * This method tries to process all the available Genbank records
110+ * This method will return all the available Genbank records
111111 * in the File or InputStream, closes the underlying resource,
112112 * and return the results in {@link LinkedHashMap}.<br>
113- * You don't need to call {@link #close()} after calling this method.
113+ * You don't need to call {@link GenbankReader #close()} after calling this method.
114114 * @see #process(int)
115115 * @return {@link HashMap} containing all the parsed Genbank records
116116 * present, starting current fileIndex onwards.
117117 * @throws IOException
118118 * @throws CompoundNotFoundException
119+ * @throws OutOfMemoryError if the input resource is larger than the allocated heap.
119120 */
120121 public LinkedHashMap <String ,S > process () throws IOException , CompoundNotFoundException {
121- return process (-1 );
122+ LinkedHashMap <String ,S > result = process (-1 );
123+ close ();
124+ return result ;
122125 }
123126
124127 /**
@@ -137,13 +140,16 @@ public LinkedHashMap<String,S> process() throws IOException, CompoundNotFoundExc
137140 * @see #process()
138141 * @author Amr AL-Hossary
139142 * @since 3.0.6
140- * @param max maximum number of records to return, <code>-1</code> for infinity .
143+ * @param max maximum number of records to return.
141144 * @return {@link HashMap} containing maximum <code>max</code> parsed Genbank records
142145 * present, starting current fileIndex onwards.
143146 * @throws IOException
144147 * @throws CompoundNotFoundException
145148 */
146149 public LinkedHashMap <String ,S > process (final int max ) throws IOException , CompoundNotFoundException {
150+
151+ if (closed ) throw new IOException ("Cannot perform action: resource has been closed." );
152+
147153 LinkedHashMap <String ,S > sequences = new LinkedHashMap <>();
148154 @ SuppressWarnings ("unchecked" )
149155 int i =0 ;
@@ -158,12 +164,9 @@ public LinkedHashMap<String,S> process(final int max) throws IOException, Compou
158164 genbankParser .getSequenceHeaderParser ().parseHeader (genbankParser .getHeader (), sequence );
159165
160166 // add features to new sequence
161- for (String k : genbankParser .getFeatures ().keySet ()){
162- for (AbstractFeature f : genbankParser .getFeatures (k )){
163- //f.getLocations().setSequence(sequence); // can't set proper sequence source to features. It is actually needed? Don't think so...
164- sequence .addFeature (f );
165- }
166- }
167+ genbankParser .getFeatures ().values ().stream ()
168+ .flatMap (List ::stream )
169+ .forEach (sequence ::addFeature );
167170
168171 // add taxonomy ID to new sequence
169172 ArrayList <DBReferenceInfo > dbQualifier = genbankParser .getDatabaseReferences ().get ("db_xref" );
@@ -175,10 +178,6 @@ public LinkedHashMap<String,S> process(final int max) throws IOException, Compou
175178 sequences .put (sequence .getAccession ().getID (), sequence );
176179 }
177180
178- if (max < 0 ) {
179- close ();
180- }
181-
182181 return sequences ;
183182 }
184183
@@ -187,11 +186,12 @@ public void close() {
187186 bufferedReader .close ();
188187 this .closed = true ;
189188 } catch (IOException e ) {
190- logger .error ("Couldn't close the reader. {} " , e . getMessage () );
189+ logger .error ("Couldn't close the reader." , e );
191190 this .closed = false ;
192191 }
193192 }
194193
194+ //TODO turn this into a test case?
195195 public static void main (String [] args ) throws Exception {
196196 String proteinFile = "src/test/resources/BondFeature.gb" ;
197197 FileInputStream is = new FileInputStream (proteinFile );
@@ -206,6 +206,7 @@ public static void main(String[] args) throws Exception {
206206 LinkedHashMap <String ,DNASequence > dnaSequences = dnaReader .process ();
207207 System .out .println (dnaSequences );
208208
209+ //TODO restore CraftedFeature.gb or delete this code
209210 String crazyFile = "src/test/resources/CraftedFeature.gb" ;
210211 is = new FileInputStream (crazyFile );
211212 GenbankReader <DNASequence , NucleotideCompound > crazyReader = new GenbankReader <>(is , new GenericGenbankHeaderParser <>(), new DNASequenceCreator (DNACompoundSet .getDNACompoundSet ()));
0 commit comments