Skip to content

Commit 32060a8

Browse files
committed
adding implicit conversion in AbstractFastqWriter
1 parent 8930d61 commit 32060a8

File tree

8 files changed

+42
-79
lines changed

8 files changed

+42
-79
lines changed

biojava-sequencing/src/main/java/org/biojava/nbio/sequencing/io/fastq/AbstractFastqWriter.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
*/
2121
package org.biojava.nbio.sequencing.io.fastq;
2222

23-
import java.io.*;
23+
import java.io.BufferedWriter;
24+
import java.io.File;
25+
import java.io.FileWriter;
26+
import java.io.IOException;
27+
import java.io.OutputStream;
28+
import java.io.OutputStreamWriter;
29+
import java.io.Writer;
30+
2431
import java.util.Arrays;
2532

2633
/**
@@ -33,12 +40,14 @@ abstract class AbstractFastqWriter
3340
{
3441

3542
/**
36-
* Validate the specified FASTQ formatted sequence for writing.
43+
* Convert the specified FASTQ formatted sequence if necessary.
3744
*
38-
* @param fastq FASTQ formatted sequence to validate, will not be null
39-
* @throws IOException if the specified FASTQ formatted sequence is not valid for writing
45+
* @since 4.2
46+
* @param fastq FASTQ formatted sequence to convert, must not be null
47+
* @return the specified FASTQ formatted sequence or a new FASTA formatted
48+
* sequence if conversion is necessary
4049
*/
41-
protected abstract void validate(final Fastq fastq) throws IOException;
50+
protected abstract Fastq convert(final Fastq fastq);
4251

4352
@Override
4453
public final <T extends Appendable> T append(final T appendable, final Fastq... fastq) throws IOException
@@ -59,16 +68,15 @@ public final <T extends Appendable> T append(final T appendable, final Iterable<
5968
}
6069
for (Fastq f : fastq)
6170
{
62-
validate(f);
6371
if (f != null)
6472
{
73+
Fastq converted = convert(f);
6574
appendable.append("@");
66-
appendable.append(f.getDescription());
67-
appendable.append("\n");
68-
appendable.append(f.getSequence());
75+
appendable.append(converted.getDescription());
6976
appendable.append("\n");
70-
appendable.append("+\n");
71-
appendable.append(f.getQuality());
77+
appendable.append(converted.getSequence());
78+
appendable.append("\n+\n");
79+
appendable.append(converted.getQuality());
7280
appendable.append("\n");
7381
}
7482
}
@@ -152,4 +160,4 @@ public final void write(final OutputStream outputStream, final Iterable<Fastq> f
152160
}
153161
}
154162
}
155-
}
163+
}

biojava-sequencing/src/main/java/org/biojava/nbio/sequencing/io/fastq/IlluminaFastqWriter.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,8 @@ public final class IlluminaFastqWriter
3232
{
3333

3434
@Override
35-
protected void validate(final Fastq fastq) throws IOException
35+
protected Fastq convert(final Fastq fastq)
3636
{
37-
if (fastq == null)
38-
{
39-
return;
40-
}
41-
if (!fastq.getVariant().isIllumina())
42-
{
43-
throw new IOException("sequence " + fastq.getDescription()
44-
+ " not fastq-illumina format, was " + fastq.getVariant().lowercaseName());
45-
}
37+
return fastq.convertTo(FastqVariant.FASTQ_ILLUMINA);
4638
}
47-
}
39+
}

biojava-sequencing/src/main/java/org/biojava/nbio/sequencing/io/fastq/SangerFastqWriter.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,8 @@ public final class SangerFastqWriter
3232
{
3333

3434
@Override
35-
protected void validate(final Fastq fastq) throws IOException
35+
protected Fastq convert(final Fastq fastq)
3636
{
37-
if (fastq == null)
38-
{
39-
return;
40-
}
41-
if (!fastq.getVariant().isSanger())
42-
{
43-
throw new IOException("sequence " + fastq.getDescription()
44-
+ " not fastq-sanger format, was " + fastq.getVariant().lowercaseName());
45-
}
37+
return fastq.convertTo(FastqVariant.FASTQ_SANGER);
4638
}
47-
}
39+
}

biojava-sequencing/src/main/java/org/biojava/nbio/sequencing/io/fastq/SolexaFastqWriter.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,8 @@ public final class SolexaFastqWriter
3232
{
3333

3434
@Override
35-
protected void validate(final Fastq fastq) throws IOException
35+
protected Fastq convert(final Fastq fastq)
3636
{
37-
if (fastq == null)
38-
{
39-
return;
40-
}
41-
if (!fastq.getVariant().isSolexa())
42-
{
43-
throw new IOException("sequence " + fastq.getDescription()
44-
+ " not fastq-solexa format, was " + fastq.getVariant().lowercaseName());
45-
}
37+
return fastq.convertTo(FastqVariant.FASTQ_SOLEXA);
4638
}
47-
}
39+
}

biojava-sequencing/src/test/java/org/biojava/nbio/sequencing/io/fastq/ConvertTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void testConvert() throws Exception
8080
FileWriter fileWriter = new FileWriter(tmp);
8181

8282
for (Fastq fastq : reader.read(getClass().getResource(inputFileName))) {
83-
writer.append(fileWriter, fastq.convertTo(variant2));
83+
writer.append(fileWriter, fastq);
8484
}
8585

8686
try

biojava-sequencing/src/test/java/org/biojava/nbio/sequencing/io/fastq/IlluminaFastqWriterTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Fastq createFastq()
4646
.build();
4747
}
4848

49-
public void testValidateNotIlluminaVariant()
49+
public void testConvertNotIlluminaVariant() throws Exception
5050
{
5151
IlluminaFastqWriter writer = new IlluminaFastqWriter();
5252
Appendable appendable = new StringBuilder();
@@ -56,14 +56,7 @@ public void testValidateNotIlluminaVariant()
5656
.withQuality("quality_")
5757
.withVariant(FastqVariant.FASTQ_SANGER)
5858
.build();
59-
try
60-
{
61-
writer.append(appendable, invalid);
62-
fail("validate not fastq-illumina variant expected IOException");
63-
}
64-
catch (IOException e)
65-
{
66-
// expected
67-
}
59+
60+
writer.append(appendable, invalid);
6861
}
69-
}
62+
}

biojava-sequencing/src/test/java/org/biojava/nbio/sequencing/io/fastq/SangerFastqWriterTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Fastq createFastq()
4646
.build();
4747
}
4848

49-
public void testValidateNotSangerVariant()
49+
public void testConvertNotSangerVariant() throws Exception
5050
{
5151
SangerFastqWriter writer = new SangerFastqWriter();
5252
Appendable appendable = new StringBuilder();
@@ -56,14 +56,7 @@ public void testValidateNotSangerVariant()
5656
.withQuality("quality_")
5757
.withVariant(FastqVariant.FASTQ_SOLEXA)
5858
.build();
59-
try
60-
{
61-
writer.append(appendable, invalid);
62-
fail("validate not fastq-sanger variant expected IOException");
63-
}
64-
catch (IOException e)
65-
{
66-
// expected
67-
}
59+
60+
writer.append(appendable, invalid);
6861
}
69-
}
62+
}

biojava-sequencing/src/test/java/org/biojava/nbio/sequencing/io/fastq/SolexaFastqWriterTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Fastq createFastq()
4646
.build();
4747
}
4848

49-
public void testValidateNotSolexaVariant()
49+
public void testConvertNotSolexaVariant() throws Exception
5050
{
5151
SolexaFastqWriter writer = new SolexaFastqWriter();
5252
Appendable appendable = new StringBuilder();
@@ -56,14 +56,7 @@ public void testValidateNotSolexaVariant()
5656
.withQuality("quality_")
5757
.withVariant(FastqVariant.FASTQ_ILLUMINA)
5858
.build();
59-
try
60-
{
61-
writer.append(appendable, invalid);
62-
fail("validate not fastq-solexa variant expected IOException");
63-
}
64-
catch (IOException e)
65-
{
66-
// expected
67-
}
59+
60+
writer.append(appendable, invalid);
6861
}
69-
}
62+
}

0 commit comments

Comments
 (0)