|
| 1 | +/* |
| 2 | + * BioJava development code |
| 3 | + * |
| 4 | + * This code may be freely distributed and modified under the |
| 5 | + * terms of the GNU Lesser General Public Licence. This should |
| 6 | + * be distributed with the code. If you do not have a copy, |
| 7 | + * see: |
| 8 | + * |
| 9 | + * http://www.gnu.org/copyleft/lesser.html |
| 10 | + * |
| 11 | + * Copyright for this code is held jointly by the individual |
| 12 | + * authors. These should be listed in @author doc comments. |
| 13 | + * |
| 14 | + * For more information on the BioJava project and its aims, |
| 15 | + * or to join the biojava-l mailing list, visit the home page |
| 16 | + * at: |
| 17 | + * |
| 18 | + * http://www.biojava.org/ |
| 19 | + * |
| 20 | + */ |
| 21 | +package org.biojava.nbio.sequencing.io.fastq; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.FileWriter; |
| 25 | +import java.io.IOException; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import junit.framework.TestCase; |
| 31 | + |
| 32 | +import com.google.common.collect.Lists; |
| 33 | +import com.google.common.collect.Maps; |
| 34 | + |
| 35 | +/** |
| 36 | + * Round trip conversion functional tests. |
| 37 | + */ |
| 38 | +public final class ConvertTest |
| 39 | + extends TestCase |
| 40 | +{ |
| 41 | + |
| 42 | + public void testConvert() throws Exception |
| 43 | + { |
| 44 | + Map<FastqVariant, FastqReader> readers = Maps.newHashMap(); |
| 45 | + readers.put(FastqVariant.FASTQ_SANGER, new SangerFastqReader()); |
| 46 | + readers.put(FastqVariant.FASTQ_SOLEXA, new SolexaFastqReader()); |
| 47 | + readers.put(FastqVariant.FASTQ_ILLUMINA, new IlluminaFastqReader()); |
| 48 | + |
| 49 | + Map<FastqVariant, FastqWriter> writers = Maps.newHashMap(); |
| 50 | + writers.put(FastqVariant.FASTQ_SANGER, new SangerFastqWriter()); |
| 51 | + writers.put(FastqVariant.FASTQ_SOLEXA, new SolexaFastqWriter()); |
| 52 | + writers.put(FastqVariant.FASTQ_ILLUMINA, new IlluminaFastqWriter()); |
| 53 | + |
| 54 | + Map<FastqVariant, String> inputFileNames = Maps.newHashMap(); |
| 55 | + inputFileNames.put(FastqVariant.FASTQ_SANGER, "sanger_full_range_as_sanger.fastq"); |
| 56 | + inputFileNames.put(FastqVariant.FASTQ_SOLEXA, "solexa_full_range_as_solexa.fastq"); |
| 57 | + inputFileNames.put(FastqVariant.FASTQ_ILLUMINA, "illumina_full_range_as_illumina.fastq"); |
| 58 | + |
| 59 | + Map<FastqVariantPair, String> expectedFileNames = Maps.newHashMap(); |
| 60 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_SANGER, FastqVariant.FASTQ_SANGER), "sanger_full_range_as_sanger.fastq"); |
| 61 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_SANGER, FastqVariant.FASTQ_SOLEXA), "sanger_full_range_as_solexa.fastq"); |
| 62 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_SANGER, FastqVariant.FASTQ_ILLUMINA), "sanger_full_range_as_illumina.fastq"); |
| 63 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_SOLEXA, FastqVariant.FASTQ_SANGER), "solexa_full_range_as_sanger.fastq"); |
| 64 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_SOLEXA, FastqVariant.FASTQ_SOLEXA), "solexa_full_range_as_solexa.fastq"); |
| 65 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_SOLEXA, FastqVariant.FASTQ_ILLUMINA), "solexa_full_range_as_illumina.fastq"); |
| 66 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_ILLUMINA, FastqVariant.FASTQ_SANGER), "illumina_full_range_as_sanger.fastq"); |
| 67 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_ILLUMINA, FastqVariant.FASTQ_SOLEXA), "illumina_full_range_as_solexa.fastq"); |
| 68 | + expectedFileNames.put(new FastqVariantPair(FastqVariant.FASTQ_ILLUMINA, FastqVariant.FASTQ_ILLUMINA), "illumina_full_range_as_illumina.fastq"); |
| 69 | + |
| 70 | + for (FastqVariant variant1 : FastqVariant.values()) |
| 71 | + { |
| 72 | + FastqReader reader = readers.get(variant1); |
| 73 | + String inputFileName = inputFileNames.get(variant1); |
| 74 | + for (FastqVariant variant2 : FastqVariant.values()) |
| 75 | + { |
| 76 | + FastqWriter writer = writers.get(variant2); |
| 77 | + String expectedFileName = expectedFileNames.get(new FastqVariantPair(variant1, variant2)); |
| 78 | + |
| 79 | + File tmp = File.createTempFile("convertTest", "fastq"); |
| 80 | + FileWriter fileWriter = new FileWriter(tmp); |
| 81 | + |
| 82 | + for (Fastq fastq : reader.read(getClass().getResource(inputFileName))) { |
| 83 | + writer.append(fileWriter, fastq.convertTo(variant2)); |
| 84 | + } |
| 85 | + |
| 86 | + try |
| 87 | + { |
| 88 | + fileWriter.close(); |
| 89 | + } |
| 90 | + catch (Exception e) |
| 91 | + { |
| 92 | + // ignore |
| 93 | + } |
| 94 | + |
| 95 | + FastqReader resultReader = readers.get(variant2); |
| 96 | + List<Fastq> observed = Lists.newArrayList(resultReader.read(tmp)); |
| 97 | + List<Fastq> expected = Lists.newArrayList(resultReader.read(getClass().getResource(expectedFileName))); |
| 98 | + |
| 99 | + assertEquals(expected.size(), observed.size()); |
| 100 | + for (int i = 0; i < expected.size(); i++) |
| 101 | + { |
| 102 | + assertEquals(expected.get(i).getDescription(), observed.get(i).getDescription()); |
| 103 | + assertEquals(expected.get(i).getSequence(), observed.get(i).getSequence()); |
| 104 | + assertEquals(expected.get(i).getQuality(), observed.get(i).getQuality()); |
| 105 | + assertEquals(expected.get(i).getVariant(), observed.get(i).getVariant()); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static final class FastqVariantPair |
| 112 | + { |
| 113 | + final FastqVariant variant1; |
| 114 | + final FastqVariant variant2; |
| 115 | + |
| 116 | + FastqVariantPair(final FastqVariant variant1, final FastqVariant variant2) |
| 117 | + { |
| 118 | + this.variant1 = variant1; |
| 119 | + this.variant2 = variant2; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public int hashCode() |
| 124 | + { |
| 125 | + int result = 47; |
| 126 | + result = 31 * result + variant1.hashCode(); |
| 127 | + result = 31 * result + variant2.hashCode(); |
| 128 | + return result; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean equals(final Object o) |
| 133 | + { |
| 134 | + if (o == this) |
| 135 | + { |
| 136 | + return true; |
| 137 | + } |
| 138 | + if (!(o instanceof FastqVariantPair)) |
| 139 | + { |
| 140 | + return false; |
| 141 | + } |
| 142 | + FastqVariantPair pair = (FastqVariantPair) o; |
| 143 | + return variant1.equals(pair.variant1) && variant2.equals(pair.variant2); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments