@Override public FastqRecord next() { if (!hasNext()) { throw new NoSuchElementException("next() called when !hasNext()"); } final FastqRecord rec = nextRecord; nextRecord = readNextRecord(); return rec; }
private FastqReader fileToFastqReader(final File file) { return new FastqReader(file, ALLOW_AND_IGNORE_EMPTY_LINES); }
/** Little utility to give error messages corresponding to line numbers in the input files. */ private String error(final FastqReader freader, final String str) { return str +" at line "+freader.getLineNumber() +" in file "+freader.getFile().getAbsolutePath(); }
protected static Set<String> createFastqReadHeaderSet(final File file) { final Set<String> set = new HashSet<String>(); final FastqReader freader = new FastqReader(file); while (freader.hasNext()) { final FastqRecord frec = freader.next(); set.add(frec.getReadName()); } return set ; }
/** Creates a simple SAM file from a single fastq file. */ protected int doUnpaired(final FastqReader freader, final SAMFileWriter writer) { int readCount = 0; final ProgressLogger progress = new ProgressLogger(LOG); for ( ; freader.hasNext() ; readCount++) { final FastqRecord frec = freader.next(); final SAMRecord srec = createSamRecord(writer.getFileHeader(), SequenceUtil.getSamReadNameFromFastqHeader(frec.getReadHeader()) , frec, false) ; srec.setReadPairedFlag(false); writer.addAlignment(srec); progress.record(srec); } return readCount; }
final String seqHeader = readLineConditionallySkippingBlanks(); if (seqHeader == null) return null ; if (StringUtil.isBlank(seqHeader)) { throw new SAMException(error("Missing sequence header")); throw new SAMException(error("Sequence header must start with "+ FastqConstants.SEQUENCE_HEADER+": "+seqHeader)); final String seqLine = readLineConditionallySkippingBlanks(); checkLine(seqLine,"sequence line"); final String qualHeader = readLineConditionallySkippingBlanks(); checkLine(qualHeader,"quality header"); if (!qualHeader.startsWith(FastqConstants.QUALITY_HEADER)) { throw new SAMException(error("Quality header must start with "+ FastqConstants.QUALITY_HEADER+": "+qualHeader)); final String qualLine = readLineConditionallySkippingBlanks(); checkLine(qualLine,"quality line"); throw new SAMException(error("Sequence and quality line must be the same length")); throw new SAMException(String.format("Error reading fastq '%s'", getAbsolutePath()), e);
Iterator<FastqRecord> it = new FastqReader(f1).iterator(); FastqRecord first = it.next(); Assert.assertEquals(first.getReadString(), bases1_1, testName); Assert.assertEquals(second.getReadString(), bases1_2, testName); Assert.assertEquals(second.getBaseQualityString(), quals1_2, testName); it = new FastqReader(f2).iterator(); first = it.next(); Assert.assertEquals(first.getReadString(), bases2_1, testName);
private FastqRecord readNextRecord() { try { final String seqHeader = readLineConditionallySkippingBlanks(); if (seqHeader == null) return null ; if (StringUtil.isBlank(seqHeader)) { throw new SAMException(error("Missing sequence header")); throw new SAMException(error("Sequence header must start with " + FastqConstants.SEQUENCE_HEADER + ": " + seqHeader)); final String seqLine = readLineConditionallySkippingBlanks(); checkLine(seqLine, LineType.SequenceLine); final String qualHeader = readLineConditionallySkippingBlanks(); checkLine(qualHeader, LineType.QualityHeader); if (!qualHeader.startsWith(FastqConstants.QUALITY_HEADER)) { throw new SAMException(error("Quality header must start with " + FastqConstants.QUALITY_HEADER + ": "+ qualHeader)); final String qualLine = readLineConditionallySkippingBlanks(); checkLine(qualLine, LineType.QualityLine); throw new SAMException(error("Sequence and quality line must be the same length")); throw new SAMException(error(e.getMessage()), e);
@Test(dataProvider = "FASTQ_TESTCASES", groups = {"unix"}) public void testFastqQualityInference(final File input, final FastqQualityFormat expectedQualityFormat) { final FastqReader reader = new FastqReader(input); Assert.assertEquals(QualityEncodingDetector.detect(reader), expectedQualityFormat); reader.close(); }
@Override public String toString() { return "FastqReader[" + (this.fastqFile == null ? "" : this.fastqFile) + " Line:" + getLineNumber() + "]"; } }
@Override public FastqRecordsForCluster decode() { if (!reader.hasNext()) return null; final FastqRecordsForCluster ret = new FastqRecordsForCluster(numTemplates, numSampleBarcodes, numMolecularBarcodes); decodeArray(ret.templateRecords); decodeArray(ret.sampleBarcodeRecords); decodeArray(ret.molecularBarcodeRecords); return ret; }
queue.add(reader.iterator());
@Override public void close() { try { reader.close(); } catch (IOException e) { throw new SAMException(error(e.getMessage()), e); } }
/** Generates an error message with line number information. */ protected String error(final String msg) { return msg + " at line " + line + " in fastq " + getAbsolutePath(); }
/** * Looks at fastq input(s) and attempts to determine the proper quality format * * Closes the reader(s) by side effect * * @param reader1 The first fastq input * @param reader2 The second fastq input, if necessary. To not use this input, set it to null * @param expectedQuality If provided, will be used for sanity checking. If left null, autodetection will occur */ public static FastqQualityFormat determineQualityFormat(final FastqReader reader1, final FastqReader reader2, final FastqQualityFormat expectedQuality) { final QualityEncodingDetector detector = new QualityEncodingDetector(); if (reader2 == null) { detector.add(QualityEncodingDetector.DEFAULT_MAX_RECORDS_TO_ITERATE, reader1); } else { detector.add(QualityEncodingDetector.DEFAULT_MAX_RECORDS_TO_ITERATE, reader1, reader2); reader2.close(); } reader1.close(); final FastqQualityFormat qualityFormat = detector.generateBestGuess(QualityEncodingDetector.FileContext.FASTQ, expectedQuality); if (detector.isDeterminationAmbiguous()) { LOG.warn("Making ambiguous determination about fastq's quality encoding; more than one format possible based on observed qualities."); } LOG.info(String.format("Auto-detected quality format as: %s.", qualityFormat)); return qualityFormat; }
private void decodeArray(final FastqRecord[] recs) { for (int i = 0; i < recs.length; ++i) { recs[i] = reader.next(); } }
/** Creates a simple SAM file from a single fastq file. */ protected int doUnpaired(final FastqReader freader, final SAMFileWriter writer) { int readCount = 0; final ProgressLogger progress = new ProgressLogger(LOG); for ( ; freader.hasNext() ; readCount++) { final FastqRecord frec = freader.next(); final SAMRecord srec = createSamRecord(writer.getFileHeader(), SequenceUtil.getSamReadNameFromFastqHeader(frec.getReadHeader()) , frec, false) ; srec.setReadPairedFlag(false); writer.addAlignment(srec); progress.record(srec); } return readCount; }
Iterator<FastqRecord> it = new FastqReader(f1).iterator(); FastqRecord first = it.next(); Assert.assertEquals(first.getReadString(), bases_1_1, testName); Assert.assertEquals(second.getBaseQualityString(), quals_1_2, testName); if (bases_2_1 != null) { it = new FastqReader(f2).iterator(); first = it.next(); Assert.assertEquals(first.getReadString(), bases_2_1, testName);
private FastqRecord readNextRecord() { try { final String seqHeader = readLineConditionallySkippingBlanks(); if (seqHeader == null) return null ; if (StringUtil.isBlank(seqHeader)) { throw new SAMException(error("Missing sequence header")); throw new SAMException(error("Sequence header must start with " + FastqConstants.SEQUENCE_HEADER + ": " + seqHeader)); final String seqLine = readLineConditionallySkippingBlanks(); checkLine(seqLine, LineType.SequenceLine); final String qualHeader = readLineConditionallySkippingBlanks(); checkLine(qualHeader, LineType.QualityHeader); if (!qualHeader.startsWith(FastqConstants.QUALITY_HEADER)) { throw new SAMException(error("Quality header must start with " + FastqConstants.QUALITY_HEADER + ": "+ qualHeader)); final String qualLine = readLineConditionallySkippingBlanks(); checkLine(qualLine, LineType.QualityLine); throw new SAMException(error("Sequence and quality line must be the same length")); throw new SAMException(error(e.getMessage()), e);
@Override public String toString() { return "FastqReader["+(this.fastqFile == null?"":this.fastqFile)+ " Line:"+getLineNumber()+"]"; } }