1010 *
1111 * Copyright for this code is held jointly by the individual
1212 * authors. These should be listed in @author doc comments.
13- *
13+ *
14+ * @author Scooter Willis ;lt;willishf at gmail dot com>
15+ * @author Karl Nicholas <github:karlnicholas>
16+ *
1417 * For more information on the BioJava project and its aims,
1518 * or to join the biojava-l mailing list, visit the home page
1619 * at:
2124 */
2225package org .biojava3 .core .sequence .io ;
2326
27+ import java .io .BufferedReader ;
2428import java .io .File ;
2529import java .io .FileInputStream ;
2630import java .io .FileNotFoundException ;
2731import java .io .IOException ;
2832import java .io .InputStream ;
33+ import java .io .InputStreamReader ;
2934import java .util .HashMap ;
3035import java .util .LinkedHashMap ;
3136
3540import org .biojava3 .core .sequence .compound .AminoAcidCompoundSet ;
3641import org .biojava3 .core .sequence .compound .DNACompoundSet ;
3742import org .biojava3 .core .sequence .compound .NucleotideCompound ;
38- import org .biojava3 .core .sequence .io .template .GenbankHeaderParserInterface ;
3943import org .biojava3 .core .sequence .io .template .SequenceCreatorInterface ;
44+ import org .biojava3 .core .sequence .io .template .SequenceHeaderParserInterface ;
45+ import org .biojava3 .core .sequence .template .AbstractSequence ;
4046import org .biojava3 .core .sequence .template .Compound ;
41- import org .biojava3 .core .sequence .template .Sequence ;
4247
4348/**
4449 * Use GenbankReaderHelper as an example of how to use this class where GenbankReaderHelper should be the
4550 * primary class used to read Genbank files
46- * -- copied from original FastReader by Scooter Willis ;lt;willishf at gmail dot com>
47- * @author Karl Nicholas
48-
51+ *
4952 */
50- public class GenbankReader <S extends Sequence <? >, C extends Compound > {
53+ public class GenbankReader <S extends AbstractSequence < C >, C extends Compound > {
5154
52- SequenceCreatorInterface <C > sequenceCreator ;
53- GenbankHeaderParserInterface <S ,C > headerParser ;
54- FileInputStream fi = null ;
55- long fileIndex = 0 ;
56- long sequenceIndex = 0 ;
57- GenbankParser <S ,C > genbankParser ;
55+ private SequenceCreatorInterface <C > sequenceCreator ;
56+ private GenbankSequenceParser <S ,C > genbankParser ;
57+ private InputStream inputStream ;
5858
5959 /**
6060 * If you are going to use FileProxyProteinSequenceCreator then do not use this constructor because we need details about
@@ -64,11 +64,10 @@ public class GenbankReader<S extends Sequence<?>, C extends Compound> {
6464 * @param headerParser
6565 * @param sequenceCreator
6666 */
67- public GenbankReader (InputStream is , GenbankHeaderParserInterface <S ,C > headerParser ,
68- SequenceCreatorInterface <C > sequenceCreator ) {
69- this .headerParser = headerParser ;
67+ public GenbankReader (InputStream is , SequenceHeaderParserInterface <S ,C > headerParser , SequenceCreatorInterface <C > sequenceCreator ) {
7068 this .sequenceCreator = sequenceCreator ;
71- genbankParser = new GenbankParser <S ,C >(is , headerParser );
69+ this .inputStream = is ;
70+ genbankParser = new GenbankSequenceParser <S ,C >();
7271 }
7372
7473 /**
@@ -84,12 +83,15 @@ public GenbankReader(InputStream is, GenbankHeaderParserInterface<S,C> headerPar
8483 * @throws SecurityException if a security manager exists and its checkRead
8584 * method denies read access to the file.
8685 */
87- public GenbankReader (File file , GenbankHeaderParserInterface <S ,C > headerParser ,
88- SequenceCreatorInterface <C > sequenceCreator ) throws FileNotFoundException {
89- this .headerParser = headerParser ;
90- fi = new FileInputStream (file );
86+ public GenbankReader (
87+ File file ,
88+ SequenceHeaderParserInterface <S ,C > headerParser ,
89+ SequenceCreatorInterface <C > sequenceCreator
90+ ) throws FileNotFoundException {
91+
92+ inputStream = new FileInputStream (file );
9193 this .sequenceCreator = sequenceCreator ;
92- genbankParser = new GenbankParser <S ,C >(fi , headerParser );
94+ genbankParser = new GenbankSequenceParser <S ,C >();
9395 }
9496
9597 /**
@@ -101,11 +103,10 @@ public GenbankReader(File file, GenbankHeaderParserInterface<S,C> headerParser,
101103 * @see #process(int)
102104 * @return {@link HashMap} containing all the parsed Genbank records
103105 * present, starting current fileIndex onwards.
104- * @throws IOException if an error occurs reading the input file
106+ * @throws Exception
105107 */
106- public LinkedHashMap <String ,S > process () throws IOException {
108+ public LinkedHashMap <String ,S > process () throws Exception {
107109 LinkedHashMap <String ,S > sequences = process (-1 );
108- close ();
109110 return sequences ;
110111 }
111112
@@ -127,22 +128,20 @@ public LinkedHashMap<String,S> process() throws IOException {
127128 * @param max maximum number of records to return, <code>-1</code> for infinity.
128129 * @return {@link HashMap} containing maximum <code>max</code> parsed Genbank records
129130 * present, starting current fileIndex onwards.
130- * @throws IOException if an error occurs reading the input file
131+ * @throws Exception
131132 */
132- public LinkedHashMap <String ,S > process (int max ) throws IOException {
133+ public LinkedHashMap <String ,S > process (int max ) throws Exception {
133134 LinkedHashMap <String ,S > sequences = new LinkedHashMap <String ,S >();
134- genbankParser .parse ();
135- S sequence = genbankParser .getSequence (sequenceCreator );
135+ @ SuppressWarnings ("unchecked" )
136+ S sequence = (S ) sequenceCreator .getSequence (genbankParser .getSequence (new BufferedReader (new InputStreamReader (inputStream )), 0 ), 0 );
137+ genbankParser .getSequenceHeaderParser ().parseHeader (genbankParser .getHeader (), sequence );
136138 sequences .put (sequence .getAccession ().getID (), sequence );
139+ close ();
137140 return sequences ;
138-
139141 }
140142
141143 public void close () throws IOException {
142- //If stream was created from File object then we need to close it
143- if (fi != null ) {
144- fi .close ();
145- }
144+ inputStream .close ();
146145 }
147146
148147 public static void main (String [] args ) throws Exception {
@@ -151,7 +150,6 @@ public static void main(String[] args) throws Exception {
151150
152151 GenbankReader <ProteinSequence , AminoAcidCompound > proteinReader = new GenbankReader <ProteinSequence , AminoAcidCompound >(is , new GenericGenbankHeaderParser <ProteinSequence ,AminoAcidCompound >(), new ProteinSequenceCreator (AminoAcidCompoundSet .getAminoAcidCompoundSet ()));
153152 LinkedHashMap <String ,ProteinSequence > proteinSequences = proteinReader .process ();
154- is .close ();
155153 System .out .println (proteinSequences );
156154
157155 String inputFile = "src/test/resources/NM_000266.gb" ;
@@ -160,7 +158,7 @@ public static void main(String[] args) throws Exception {
160158 LinkedHashMap <String ,DNASequence > dnaSequences = dnaReader .process ();
161159 is .close ();
162160 System .out .println (dnaSequences );
163-
164161 }
162+
165163}
166164
0 commit comments