2020 */
2121package org .biojava3 .sequencing .io .fastq ;
2222
23+ import java .io .*;
2324import java .net .URL ;
24-
25- import java .io .BufferedReader ;
26- import java .io .File ;
27- import java .io .FileInputStream ;
28- import java .io .InputStream ;
29- import java .io .InputStreamReader ;
30- import java .io .IOException ;
31-
3225import java .util .ArrayList ;
3326import java .util .List ;
3427
3730 *
3831 * @since 3.0.3
3932 */
40- abstract class AbstractFastqReader
41- implements FastqReader
42- {
33+ abstract class AbstractFastqReader implements FastqReader {
4334
44- /** Parser state. */
45- private static enum State
46- {
47- /** Description parser state. */
48- DESCRIPTION ,
35+ /**
36+ * Parser state.
37+ */
38+ private static enum State {
4939
50- /** Sequence parser state. */
40+ /**
41+ * Description parser state.
42+ */
43+ DESCRIPTION ,
44+ /**
45+ * Sequence parser state.
46+ */
5147 SEQUENCE ,
52-
53- /** Repeat description parser state. */
48+ /**
49+ * Repeat description parser state.
50+ */
5451 REPEAT_DESCRIPTION ,
55-
56- /** Quality score parser state. */
52+ /**
53+ * Quality score parser state.
54+ */
5755 QUALITY ,
58-
59- /** Complete parser state. */
56+ /**
57+ * Complete parser state.
58+ */
6059 COMPLETE ;
6160 };
6261
@@ -75,8 +74,7 @@ private static enum State
7574 * @param lineNumber current line number in input stream
7675 * @throws IOException if the specified description is not valid
7776 */
78- protected abstract void validateDescription (FastqBuilder builder , String description , int lineNumber )
79- throws IOException ;
77+ protected abstract void validateDescription (FastqBuilder builder , String description , int lineNumber ) throws IOException ;
8078
8179 /**
8280 * Validate the specified sequence.
@@ -86,8 +84,7 @@ protected abstract void validateDescription(FastqBuilder builder, String descrip
8684 * @param lineNumber current line number in input stream
8785 * @throws IOException if the specified sequence is not valid
8886 */
89- protected abstract void validateSequence (FastqBuilder builder , String sequence , int lineNumber )
90- throws IOException ;
87+ protected abstract void validateSequence (FastqBuilder builder , String sequence , int lineNumber ) throws IOException ;
9188
9289 /**
9390 * Validate the specified repeat description.
@@ -97,8 +94,7 @@ protected abstract void validateSequence(FastqBuilder builder, String sequence,
9794 * @param lineNumber current line number in input stream
9895 * @throws IOException if the specified repeat description is not valid
9996 */
100- protected abstract void validateRepeatDescription (FastqBuilder builder , String repeatDescription , int lineNumber )
101- throws IOException ;
97+ protected abstract void validateRepeatDescription (FastqBuilder builder , String repeatDescription , int lineNumber ) throws IOException ;
10298
10399 /**
104100 * Validate the specified quality scores.
@@ -108,160 +104,127 @@ protected abstract void validateRepeatDescription(FastqBuilder builder, String r
108104 * @param lineNumber current line number in input stream
109105 * @throws IOException if the specified quality scores are not valid
110106 */
111- protected abstract void validateQuality (FastqBuilder builder , String quality , int lineNumber )
112- throws IOException ;
113-
107+ protected abstract void validateQuality (FastqBuilder builder , String quality , int lineNumber ) throws IOException ;
114108
115- /** {@inheritDoc} */
116- public final Iterable <Fastq > read (final File file ) throws IOException
117- {
118- if (file == null )
119- {
109+ /**
110+ * {@inheritDoc}
111+ */
112+ @ Override
113+ public final Iterable <Fastq > read (final File file ) throws IOException {
114+ if (file == null ) {
120115 throw new IllegalArgumentException ("file must not be null" );
121116 }
122117 InputStream inputStream = null ;
123- try
124- {
118+ try {
125119 inputStream = new FileInputStream (file );
126120 return read (inputStream );
127- }
128- catch (IOException e )
129- {
121+ } catch (IOException e ) {
130122 throw e ;
131- }
132- finally
133- {
134- if (inputStream != null )
135- {
136- try
137- {
123+ } finally {
124+ if (inputStream != null ) {
125+ try {
138126 inputStream .close ();
139- }
140- catch (IOException e )
141- {
127+ } catch (IOException e ) {
142128 // ignore
143129 }
144130 }
145131 }
146132 }
147133
148- /** {@inheritDoc} */
149- public final Iterable <Fastq > read (final URL url ) throws IOException
150- {
151- if (url == null )
152- {
134+ /**
135+ * {@inheritDoc}
136+ */
137+ @ Override
138+ public final Iterable <Fastq > read (final URL url ) throws IOException {
139+ if (url == null ) {
153140 throw new IllegalArgumentException ("url must not be null" );
154141 }
155142 InputStream inputStream = null ;
156- try
157- {
143+ try {
158144 inputStream = url .openStream ();
159145 return read (inputStream );
160- }
161- catch (IOException e )
162- {
146+ } catch (IOException e ) {
163147 throw e ;
164- }
165- finally
166- {
167- if (inputStream != null )
168- {
169- try
170- {
148+ } finally {
149+ if (inputStream != null ) {
150+ try {
171151 inputStream .close ();
172- }
173- catch (IOException e )
174- {
152+ } catch (IOException e ) {
175153 // ignore
176154 }
177155 }
178156 }
179157 }
180158
181- /** {@inheritDoc} */
182- public final Iterable <Fastq > read (final InputStream inputStream ) throws IOException
183- {
184- if (inputStream == null )
185- {
186- throw new IllegalArgumentException ("inputStream must not be null" );
159+ /**
160+ * {@inheritDoc}
161+ */
162+ @ Override
163+ public final Iterable <Fastq > read (final InputStream inputStream ) throws IOException {
164+ if (inputStream == null ) {
165+ throw new IllegalArgumentException ("inputStream must not be null" );
187166 }
188167 int lineNumber = 0 ;
189168 State state = State .DESCRIPTION ;
190169 List <Fastq > result = new ArrayList <Fastq >();
191170 FastqBuilder builder = new FastqBuilder ().withVariant (getVariant ());
192171 BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream ));
193- while (reader .ready ())
194- {
172+ while (reader .ready ()) {
195173 String line = reader .readLine ();
196- switch (state )
197- {
198- case DESCRIPTION :
199- validateDescription (builder , line , lineNumber );
200- builder .withDescription (line .substring (1 ).trim ());
201- state = State .SEQUENCE ;
202- break ;
203- case SEQUENCE :
204- validateSequence (builder , line , lineNumber );
205- builder .withSequence (line .trim ());
206- state = State .REPEAT_DESCRIPTION ;
207- break ;
208- case REPEAT_DESCRIPTION :
209- if (!line .startsWith ("+" ))
210- {
211- builder .appendSequence (line .trim ());
212- }
213- else
214- {
215- validateRepeatDescription (builder , line , lineNumber );
216- state = State .QUALITY ;
217- }
218- break ;
219- case QUALITY :
220- validateQuality (builder , line , lineNumber );
221- builder .withQuality (line .trim ());
222- state = State .COMPLETE ;
223- break ;
224- case COMPLETE :
225- if (!builder .sequenceAndQualityLengthsMatch ())
226- {
227- builder .appendQuality (line .trim ());
228- }
229- else
230- {
231- try
232- {
233- result .add (builder .build ());
234- }
235- catch (IllegalStateException e )
236- {
237- throw new IOException ("caught an IllegalStateException at line " + lineNumber + " " + e .getMessage ());
238- //throw new IOException("caught an IllegalStateException at line " + lineNumber, e); jdk 1.6+
239- }
174+ switch (state ) {
175+ case DESCRIPTION :
240176 validateDescription (builder , line , lineNumber );
241177 builder .withDescription (line .substring (1 ).trim ());
242178 state = State .SEQUENCE ;
243- }
244- break ;
245- default :
246- break ;
179+ break ;
180+ case SEQUENCE :
181+ validateSequence (builder , line , lineNumber );
182+ builder .withSequence (line .trim ());
183+ state = State .REPEAT_DESCRIPTION ;
184+ break ;
185+ case REPEAT_DESCRIPTION :
186+ if (!line .startsWith ("+" )) {
187+ builder .appendSequence (line .trim ());
188+ } else {
189+ validateRepeatDescription (builder , line , lineNumber );
190+ state = State .QUALITY ;
191+ }
192+ break ;
193+ case QUALITY :
194+ validateQuality (builder , line , lineNumber );
195+ builder .withQuality (line .trim ());
196+ state = State .COMPLETE ;
197+ break ;
198+ case COMPLETE :
199+ if (!builder .sequenceAndQualityLengthsMatch ()) {
200+ builder .appendQuality (line .trim ());
201+ } else {
202+ try {
203+ result .add (builder .build ());
204+ } catch (IllegalStateException e ) {
205+ throw new IOException ("caught an IllegalStateException at line " + lineNumber + " " + e .getMessage ());
206+ //throw new IOException("caught an IllegalStateException at line " + lineNumber, e); jdk 1.6+
207+ }
208+ validateDescription (builder , line , lineNumber );
209+ builder .withDescription (line .substring (1 ).trim ());
210+ state = State .SEQUENCE ;
211+ }
212+ break ;
213+ default :
214+ break ;
247215 }
248216 lineNumber ++;
249217 }
250- if (state == State .COMPLETE )
251- {
252- try
253- {
218+ if (state == State .COMPLETE ) {
219+ try {
254220 result .add (builder .build ());
255221 state = State .DESCRIPTION ;
256- }
257- catch (IllegalStateException e )
258- {
259- throw new IOException ("caught an IllegalStateException at line " + lineNumber + " " + e .getMessage ());
260- //throw new IOException("caught an IllegalStateException at line " + lineNumber, e); jdk 1.6+
222+ } catch (IllegalStateException e ) {
223+ //throw new IOException("caught an IllegalStateException at line " + lineNumber + " " + e.getMessage());
224+ throw new IOException ("caught an IllegalStateException at line " + lineNumber , e );
261225 }
262226 }
263- if (state != State .DESCRIPTION )
264- {
227+ if (state != State .DESCRIPTION ) {
265228 throw new IOException ("truncated sequence at line " + lineNumber );
266229 }
267230 return result ;
0 commit comments