/** * @return the read name * @deprecated since 02/2017. Use {@link #getReadName()} instead */ @Deprecated public String getReadHeader() { return getReadName(); }
/** * @return the read name * @deprecated since 02/2017. Use {@link #getReadName()} instead */ @Deprecated public String getReadHeader() { return getReadName(); }
/** * Encodes a FastqRecord in the String FASTQ format. */ public static String encode(final FastqRecord record) { // reserve some memory based on the read length int capacity = record.getReadLength() * 2 + 5; // reserve some memory based on the read name if (record.getReadName() != null) { capacity += record.getReadName().length(); } return write(new StringBuilder(capacity), record).toString(); }
/** * Encodes a FastqRecord in the String FASTQ format. */ public static String encode(final FastqRecord record) { // reserve some memory based on the read length int capacity = record.getReadLength() * 2 + 5; // reserve some memory based on the read name if (record.getReadName() != null) { capacity += record.getReadName().length(); } return write(new StringBuilder(capacity), record).toString(); }
private static void writeStream(final OutputStream stream, final FastqRecord record) throws IOException { final String readName = record.getReadName(); final String readString = record.getReadString(); final String qualHeader = record.getBaseQualityHeader(); final String qualityString = record.getBaseQualityString(); stream.write(SEQUENCE_HEADER); if (readName != null) { stream.write(readName.getBytes(FASTQ_ENCODING)); } stream.write(FASTQ_EOL); if (readString != null) { stream.write(readString.getBytes(FASTQ_ENCODING)); } stream.write(FASTQ_EOL); stream.write(QUALITY_HEADER); if (qualHeader != null) { stream.write(qualHeader.getBytes(FASTQ_ENCODING)); } stream.write(FASTQ_EOL); if (qualityString != null) { stream.write(qualityString.getBytes(FASTQ_ENCODING)); } stream.write(FASTQ_EOL); } @Override
/** * Writes a FastqRecord into the Appendable output. * @throws SAMException if any I/O error occurs. */ public static Appendable write(final Appendable out,final FastqRecord record) { final String readName = record.getReadName(); final String readString = record.getReadString(); final String qualHeader = record.getBaseQualityHeader(); final String qualityString = record.getBaseQualityString(); try { return out.append(FastqConstants.SEQUENCE_HEADER) .append(readName == null ? "" : readName).append('\n') .append(readString == null ? "" : readString).append('\n') .append(FastqConstants.QUALITY_HEADER) .append(qualHeader == null ? "" : qualHeader).append('\n') .append(qualityString == null ? "" : qualityString); } catch (IOException e) { throw new SAMException(e); } }
/** * Writes a FastqRecord into the Appendable output. * @throws SAMException if any I/O error occurs. */ public static Appendable write(final Appendable out,final FastqRecord record) { final String readName = record.getReadName(); final String readString = record.getReadString(); final String qualHeader = record.getBaseQualityHeader(); final String qualityString = record.getBaseQualityString(); try { return out.append(FastqConstants.SEQUENCE_HEADER) .append(readName == null ? "" : readName).append('\n') .append(readString == null ? "" : readString).append('\n') .append(FastqConstants.QUALITY_HEADER) .append(qualHeader == null ? "" : qualHeader).append('\n') .append(qualityString == null ? "" : qualityString); } catch (IOException e) { throw new SAMException(e); } }
@Test public void testBasicEmptyHeaderPrefix() { final String seqHeaderPrefix = ""; final String seqLine = "ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT"; final String qualHeaderPrefix = ""; final String qualLine = ";<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; final FastqRecord fastqRecord = new FastqRecord(seqHeaderPrefix, seqLine, qualHeaderPrefix, qualLine); Assert.assertNull(fastqRecord.getReadName()); Assert.assertNull(fastqRecord.getBaseQualityHeader()); }
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 ; }
@Test public void testEmptyRecord() { //Note: this does not blow up now but it will once we enforce non empty fields final FastqRecord record = new FastqRecord(null, (String) null, null, null); // assert how null is handled Assert.assertNull(record.getReadName()); Assert.assertNull(record.getReadString()); Assert.assertNull(record.getBaseQualityString()); Assert.assertEquals(record.getReadBases(), SAMRecord.NULL_SEQUENCE); Assert.assertEquals(record.getBaseQualities(), SAMRecord.NULL_QUALS); // copy the FastqRecord to check that equals and hashCode is working for the null read without blow up final FastqRecord copy = new FastqRecord(record); Assert.assertEquals(record, copy); Assert.assertEquals(record.hashCode(), copy.hashCode()); }
@Test public void testBasic() { final String seqHeaderPrefix = "FAKE0003 Original version has Solexa scores from 62 to -5 inclusive (in that order)"; final String seqLine = "ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT"; final String qualHeaderPrefix = ""; final String qualLine = ";<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; final FastqRecord fastqRecord = new FastqRecord(seqHeaderPrefix, seqLine, qualHeaderPrefix, qualLine); Assert.assertNull(fastqRecord.getBaseQualityHeader()); Assert.assertEquals(fastqRecord.getReadName(), seqHeaderPrefix); Assert.assertEquals(fastqRecord.getBaseQualityString(), qualLine); Assert.assertEquals(fastqRecord.getReadString(), seqLine); Assert.assertNotNull(fastqRecord.toString());//just check not nullness Assert.assertNotEquals(fastqRecord, null); Assert.assertFalse(fastqRecord.equals(null)); Assert.assertNotEquals(null, fastqRecord); Assert.assertEquals(fastqRecord, fastqRecord); Assert.assertNotEquals(fastqRecord, "fred"); Assert.assertNotEquals("fred", fastqRecord); Assert.assertEquals(fastqRecord.getReadLength(), seqLine.length()); Assert.assertEquals(fastqRecord.getBaseQualityString().length(), fastqRecord.getReadString().length()); Assert.assertEquals(fastqRecord.getReadString().length(), fastqRecord.getReadLength()); }
private void testRecord(final String expectedReadName, final FastqRecord fastqRecord, final SAMRecord samRecord) { Assert.assertEquals(fastqRecord.getReadName(), expectedReadName); Assert.assertEquals(fastqRecord.getBaseQualities(), samRecord.getBaseQualities()); Assert.assertEquals(fastqRecord.getReadBases(), samRecord.getReadBases()); Assert.assertEquals(fastqRecord.getBaseQualityHeader(), samRecord.getStringAttribute(SAMTag.CO.name())); }
/** * Converts a {@link FastqRecord} into a simple unmapped {@link SAMRecord}. * * <p>This method allows to pass a {@link BiConsumer} to add the information from the record in * a customizable manner. * * @param record object to encode. * @param header header for the returned object. * @param custom function to customize encoding. Note that default information might be overriden. */ public static SAMRecord asSAMRecord(final FastqRecord record, final SAMFileHeader header, final BiConsumer<FastqRecord, SAMRecord> custom) { // construct the SAMRecord and set the unmapped flag final SAMRecord samRecord = new SAMRecord(header); samRecord.setReadUnmappedFlag(true); // get the read name from the FastqRecord correctly formatted final String readName = SequenceUtil.getSamReadNameFromFastqHeader(record.getReadName()); // set the basic information from the FastqRecord samRecord.setReadName(readName); samRecord.setReadBases(record.getReadBases()); samRecord.setBaseQualities(record.getBaseQualities()); custom.accept(record, samRecord); return samRecord; }
/** * Converts a {@link FastqRecord} into a simple unmapped {@link SAMRecord}. * * <p>This method allows to pass a {@link BiConsumer} to add the information from the record in * a customizable manner. * * @param record object to encode. * @param header header for the returned object. * @param custom function to customize encoding. Note that default information might be overriden. */ public static SAMRecord asSAMRecord(final FastqRecord record, final SAMFileHeader header, final BiConsumer<FastqRecord, SAMRecord> custom) { // construct the SAMRecord and set the unmapped flag final SAMRecord samRecord = new SAMRecord(header); samRecord.setReadUnmappedFlag(true); // get the read name from the FastqRecord correctly formatted final String readName = SequenceUtil.getSamReadNameFromFastqHeader(record.getReadName()); // set the basic information from the FastqRecord samRecord.setReadName(readName); samRecord.setReadBases(record.getReadBases()); samRecord.setBaseQualities(record.getBaseQualities()); custom.accept(record, samRecord); return samRecord; }
@Override public void align(File fastq, File output, File reference, int threads) throws IOException { try (ReferenceSequenceFile ref = new IndexedFastaSequenceFile(reference)) { SAMFileHeader header = new SAMFileHeader(); header.setSequenceDictionary(ref.getSequenceDictionary()); byte[] bases = ref.getSequence(ref.getSequenceDictionary().getSequence(referenceIndex).getSequenceName()).getBases(); try (FastqReader reader = new FastqReader(fastq)) { try (SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, true, output)) { for (FastqRecord fqr : reader) { Alignment aln = aligner.align_smith_waterman(fqr.getReadString().getBytes(), bases); SAMRecord r = new SAMRecord(header); r.setReadName(fqr.getReadName()); r.setReferenceIndex(referenceIndex); r.setAlignmentStart(aln.getStartPosition() + 1); r.setCigarString(aln.getCigar()); r.setReadBases(fqr.getReadString().getBytes()); r.setBaseQualities(SAMUtils.fastqToPhred(fqr.getBaseQualityString())); writer.addAlignment(r); } } } } } }