|
| 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.bio.program.fastq; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.io.IOException; |
| 26 | + |
| 27 | +import java.net.URL; |
| 28 | + |
| 29 | +import junit.framework.TestCase; |
| 30 | + |
| 31 | +/** |
| 32 | + * Abstract unit test for implementations of FastqReader. |
| 33 | + */ |
| 34 | +public abstract class AbstractFastqReaderTest |
| 35 | + extends TestCase |
| 36 | +{ |
| 37 | + /** Array of example files that should throw IOExceptions. */ |
| 38 | + static final String[] ERROR_EXAMPLES = new String[] |
| 39 | + { |
| 40 | + "error_diff_ids.fastq", |
| 41 | + "error_double_qual.fastq", |
| 42 | + "error_double_seq.fastq", |
| 43 | + "error_long_qual.fastq", |
| 44 | + "error_no_qual.fastq", |
| 45 | + "error_qual_del.fastq", |
| 46 | + "error_qual_escape.fastq", |
| 47 | + "error_qual_null.fastq", |
| 48 | + "error_qual_space.fastq", |
| 49 | + "error_qual_tab.fastq", |
| 50 | + "error_qual_unit_sep.fastq", |
| 51 | + "error_qual_vtab.fastq", |
| 52 | + "error_short_qual.fastq", |
| 53 | + "error_spaces.fastq", |
| 54 | + "error_tabs.fastq", |
| 55 | + "error_trunc_at_plus.fastq", |
| 56 | + "error_trunc_at_qual.fastq", |
| 57 | + "error_trunc_at_seq.fastq", |
| 58 | + "error_trunc_in_plus.fastq", |
| 59 | + "error_trunc_in_qual.fastq", |
| 60 | + "error_trunc_in_seq.fastq", |
| 61 | + "error_trunc_in_title.fastq" |
| 62 | + }; |
| 63 | + |
| 64 | + /** |
| 65 | + * Create and return a new FASTQ formatted sequence suitable for testing. |
| 66 | + * |
| 67 | + * @return a new FASTQ formatted sequence suitable for testing. |
| 68 | + */ |
| 69 | + public abstract Fastq createFastq(); |
| 70 | + |
| 71 | + /** |
| 72 | + * Create and return a new instance of an implementation of FastqReader to test. |
| 73 | + * |
| 74 | + * @return a new instance of an implementation of FastqReader to test |
| 75 | + */ |
| 76 | + public abstract FastqReader createFastqReader(); |
| 77 | + |
| 78 | + /** |
| 79 | + * Create and return a new instance of an implementation of FastqWriter to test round-tripping. |
| 80 | + * |
| 81 | + * @return a new instance of an implementation of FastqWriter to test round-tripping. |
| 82 | + */ |
| 83 | + public abstract FastqWriter createFastqWriter(); |
| 84 | + |
| 85 | + public void testCreateFastq() |
| 86 | + { |
| 87 | + Fastq fastq = createFastq(); |
| 88 | + assertNotNull(fastq); |
| 89 | + } |
| 90 | + |
| 91 | + public void testCreateFastqReader() |
| 92 | + { |
| 93 | + FastqReader reader = createFastqReader(); |
| 94 | + assertNotNull(reader); |
| 95 | + } |
| 96 | + |
| 97 | + public void testCreateFastqWriter() |
| 98 | + { |
| 99 | + FastqWriter writer = createFastqWriter(); |
| 100 | + assertNotNull(writer); |
| 101 | + } |
| 102 | + |
| 103 | + public void testReadFile() throws Exception |
| 104 | + { |
| 105 | + FastqReader reader = createFastqReader(); |
| 106 | + try |
| 107 | + { |
| 108 | + reader.read((File) null); |
| 109 | + fail("read((File) null) expected IllegalArgumentException"); |
| 110 | + } |
| 111 | + catch (IllegalArgumentException e) |
| 112 | + { |
| 113 | + // expected |
| 114 | + } |
| 115 | + try |
| 116 | + { |
| 117 | + File noSuchFile = new File("no such file"); |
| 118 | + reader.read(noSuchFile); |
| 119 | + fail("read(no such file) expected IOException"); |
| 120 | + } |
| 121 | + catch (IOException e) |
| 122 | + { |
| 123 | + // expected |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public void testReadEmptyFile() throws Exception |
| 128 | + { |
| 129 | + FastqReader reader = createFastqReader(); |
| 130 | + File empty = File.createTempFile("abstractFastqReaderTest", null); |
| 131 | + Iterable<Fastq> iterable = reader.read(empty); |
| 132 | + assertNotNull(iterable); |
| 133 | + int count = 0; |
| 134 | + for (Fastq f : iterable) |
| 135 | + { |
| 136 | + assertNotNull(f); |
| 137 | + count++; |
| 138 | + } |
| 139 | + assertEquals(0, count); |
| 140 | + } |
| 141 | + |
| 142 | + public void testReadRoundTripSingleFile() throws Exception |
| 143 | + { |
| 144 | + FastqReader reader = createFastqReader(); |
| 145 | + File single = File.createTempFile("abstractFastqReaderTest", null); |
| 146 | + Fastq fastq = createFastq(); |
| 147 | + FastqWriter writer = createFastqWriter(); |
| 148 | + writer.write(single, fastq); |
| 149 | + Iterable<Fastq> iterable = reader.read(single); |
| 150 | + assertNotNull(iterable); |
| 151 | + int count = 0; |
| 152 | + for (Fastq f : iterable) |
| 153 | + { |
| 154 | + assertNotNull(f); |
| 155 | + count++; |
| 156 | + } |
| 157 | + assertEquals(1, count); |
| 158 | + } |
| 159 | + |
| 160 | + public void testReadRoundTripMultipleFile() throws Exception |
| 161 | + { |
| 162 | + FastqReader reader = createFastqReader(); |
| 163 | + File multiple = File.createTempFile("abstractFastqReaderTest", null); |
| 164 | + Fastq fastq0 = createFastq(); |
| 165 | + Fastq fastq1 = createFastq(); |
| 166 | + Fastq fastq2 = createFastq(); |
| 167 | + FastqWriter writer = createFastqWriter(); |
| 168 | + writer.write(multiple, fastq0, fastq1, fastq2); |
| 169 | + Iterable<Fastq> iterable = reader.read(multiple); |
| 170 | + assertNotNull(iterable); |
| 171 | + int count = 0; |
| 172 | + for (Fastq f : iterable) |
| 173 | + { |
| 174 | + assertNotNull(f); |
| 175 | + count++; |
| 176 | + } |
| 177 | + assertEquals(3, count); |
| 178 | + } |
| 179 | + |
| 180 | + public void testReadURL() throws Exception |
| 181 | + { |
| 182 | + FastqReader reader = createFastqReader(); |
| 183 | + try |
| 184 | + { |
| 185 | + reader.read((URL) null); |
| 186 | + fail("read((URL) null) expected IllegalArgumentException"); |
| 187 | + } |
| 188 | + catch (IllegalArgumentException e) |
| 189 | + { |
| 190 | + // expected |
| 191 | + } |
| 192 | + try |
| 193 | + { |
| 194 | + URL noSuchURL = new URL("file:///no such url"); |
| 195 | + reader.read(noSuchURL); |
| 196 | + fail("read(no such URL) expected IOException"); |
| 197 | + } |
| 198 | + catch (IOException e) |
| 199 | + { |
| 200 | + // expected |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + public void testReadEmptyURL() throws Exception |
| 205 | + { |
| 206 | + FastqReader reader = createFastqReader(); |
| 207 | + URL empty = getClass().getResource("empty.fastq"); |
| 208 | + Iterable<Fastq> iterable = reader.read(empty); |
| 209 | + assertNotNull(iterable); |
| 210 | + int count = 0; |
| 211 | + for (Fastq f : iterable) |
| 212 | + { |
| 213 | + assertNotNull(f); |
| 214 | + count++; |
| 215 | + } |
| 216 | + assertEquals(0, count); |
| 217 | + } |
| 218 | + |
| 219 | + public void testReadInputStream() throws Exception |
| 220 | + { |
| 221 | + FastqReader reader = createFastqReader(); |
| 222 | + try |
| 223 | + { |
| 224 | + reader.read((InputStream) null); |
| 225 | + fail("read((InputStream) null) expected IllegalArgumentException"); |
| 226 | + } |
| 227 | + catch (IllegalArgumentException e) |
| 228 | + { |
| 229 | + // expected |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + public void testReadEmptyInputStream() throws Exception |
| 234 | + { |
| 235 | + FastqReader reader = createFastqReader(); |
| 236 | + InputStream empty = getClass().getResourceAsStream("empty.fastq"); |
| 237 | + Iterable<Fastq> iterable = reader.read(empty); |
| 238 | + assertNotNull(iterable); |
| 239 | + int count = 0; |
| 240 | + for (Fastq f : iterable) |
| 241 | + { |
| 242 | + assertNotNull(f); |
| 243 | + count++; |
| 244 | + } |
| 245 | + assertEquals(0, count); |
| 246 | + empty.close(); |
| 247 | + } |
| 248 | + |
| 249 | + public void testWrappedSequence() throws Exception |
| 250 | + { |
| 251 | + FastqReader reader = createFastqReader(); |
| 252 | + InputStream wrappedSequence = getClass().getResourceAsStream("wrapped-sequence.fastq"); |
| 253 | + Iterable<Fastq> iterable = reader.read(wrappedSequence); |
| 254 | + assertNotNull(iterable); |
| 255 | + int count = 0; |
| 256 | + for (Fastq f : iterable) |
| 257 | + { |
| 258 | + assertNotNull(f); |
| 259 | + assertEquals("ACTG", f.getSequence()); |
| 260 | + count++; |
| 261 | + } |
| 262 | + assertEquals(1, count); |
| 263 | + wrappedSequence.close(); |
| 264 | + } |
| 265 | + |
| 266 | + public void testWrappedQuality() throws Exception |
| 267 | + { |
| 268 | + FastqReader reader = createFastqReader(); |
| 269 | + InputStream wrappedQuality = getClass().getResourceAsStream("wrapped-quality.fastq"); |
| 270 | + Iterable<Fastq> iterable = reader.read(wrappedQuality); |
| 271 | + assertNotNull(iterable); |
| 272 | + int count = 0; |
| 273 | + for (Fastq f : iterable) |
| 274 | + { |
| 275 | + assertNotNull(f); |
| 276 | + assertEquals("ZZZZ", f.getQuality()); |
| 277 | + count++; |
| 278 | + } |
| 279 | + assertEquals(1, count); |
| 280 | + wrappedQuality.close(); |
| 281 | + } |
| 282 | + |
| 283 | + public void testMultipleWrappedQuality() throws Exception |
| 284 | + { |
| 285 | + FastqReader reader = createFastqReader(); |
| 286 | + InputStream wrappedQuality = getClass().getResourceAsStream("multiple-wrapped-quality.fastq"); |
| 287 | + Iterable<Fastq> iterable = reader.read(wrappedQuality); |
| 288 | + assertNotNull(iterable); |
| 289 | + int count = 0; |
| 290 | + for (Fastq f : iterable) |
| 291 | + { |
| 292 | + assertNotNull(f); |
| 293 | + assertEquals("ZZZZ", f.getQuality()); |
| 294 | + count++; |
| 295 | + } |
| 296 | + assertEquals(4, count); |
| 297 | + wrappedQuality.close(); |
| 298 | + } |
| 299 | + |
| 300 | + public void testErrorExamples() throws Exception |
| 301 | + { |
| 302 | + FastqReader reader = createFastqReader(); |
| 303 | + for (String errorExample : ERROR_EXAMPLES) |
| 304 | + { |
| 305 | + InputStream inputStream = getClass().getResourceAsStream(errorExample); |
| 306 | + try |
| 307 | + { |
| 308 | + reader.read(inputStream); |
| 309 | + fail("error example " + errorExample + " expected IOException"); |
| 310 | + } |
| 311 | + catch (IOException e) |
| 312 | + { |
| 313 | + // expected |
| 314 | + } |
| 315 | + finally |
| 316 | + { |
| 317 | + if (inputStream != null) |
| 318 | + { |
| 319 | + try |
| 320 | + { |
| 321 | + inputStream.close(); |
| 322 | + } |
| 323 | + catch (IOException e) |
| 324 | + { |
| 325 | + // ignore |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + } |
| 330 | + } |
| 331 | +} |
0 commit comments