Search in sources :

Example 11 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project gridss by PapenfussLab.

the class StubFastqAligner method align.

public StubFastqAligner align(SAMRecord r, BreakendDirection direction, int referenceIndex, int pos, boolean isNegativeStrand, String cigar) {
    List<FastqRecord> srs = SplitReadIdentificationHelper.getSplitReadRealignments(r, false, eidgen);
    FastqRecord fqr = srs.get(0);
    if (srs.size() == 2) {
        if (direction == BreakendDirection.Forward ^ r.getReadNegativeStrandFlag()) {
            fqr = srs.get(1);
        }
    }
    map.put(fqr.getReadName(), new ChimericAlignment(r.getHeader().getSequenceDictionary().getSequence(referenceIndex).getSequenceName(), pos, isNegativeStrand, TextCigarCodec.decode(cigar), 40, 0));
    nameLookup.put(fqr.getReadName(), r);
    return this;
}
Also used : ChimericAlignment(au.edu.wehi.idsv.sam.ChimericAlignment) FastqRecord(htsjdk.samtools.fastq.FastqRecord)

Example 12 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project gridss by PapenfussLab.

the class StubFastqAligner method align.

@Override
public void align(File fastq, File output, File reference, int threads) throws IOException {
    SAMFileHeader header = context.getBasicSamHeader();
    try (FastqReader reader = new FastqReader(fastq)) {
        try (SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, true, output)) {
            for (FastqRecord fqr : reader) {
                SAMRecord source = nameLookup.get(fqr.getReadName());
                SAMRecord r = new SAMRecord(header);
                r.setReadName(fqr.getReadName());
                r.setReadBases(fqr.getReadString().getBytes());
                r.setBaseQualities(SAMUtils.fastqToPhred(fqr.getBaseQualityString()));
                if (source == null) {
                    r.setReadUnmappedFlag(true);
                } else {
                    ChimericAlignment aln = map.get(fqr.getReadName());
                    r.setReferenceName(aln.rname);
                    r.setAlignmentStart(aln.pos);
                    r.setCigar(aln.cigar);
                    r.setMappingQuality(aln.mapq);
                    r.setAttribute("NM", aln.nm);
                    if (aln.isNegativeStrand) {
                        SequenceUtil.reverseComplement(r.getReadBases());
                        ArrayUtils.reverse(r.getBaseQualities());
                    }
                }
                writer.addAlignment(r);
            }
        }
    }
}
Also used : ChimericAlignment(au.edu.wehi.idsv.sam.ChimericAlignment) FastqReader(htsjdk.samtools.fastq.FastqReader) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) FastqRecord(htsjdk.samtools.fastq.FastqRecord) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader)

Example 13 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project gridss by PapenfussLab.

the class SplitReadRealigner method processInputRecord.

private void processInputRecord(StreamingAligner aligner, SplitReadFastqExtractor rootExtractor, Map<String, SplitReadRealignmentInfo> realignments, SAMFileWriter writer, SAMRecord r) throws IOException {
    List<FastqRecord> softclipRealignments = rootExtractor.extract(r);
    if (softclipRealignments.size() == 0) {
        // nothing to do - just output the record
        writer.addAlignment(r);
    } else {
        // perform split read realignment
        SplitReadRealignmentInfo info = new SplitReadRealignmentInfo(r);
        realignments.put(info.lookupKey, info);
        for (FastqRecord fq : softclipRealignments) {
            aligner.asyncAlign(fq);
            info.outstandingRealignments++;
        }
    }
}
Also used : FastqRecord(htsjdk.samtools.fastq.FastqRecord)

Example 14 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project gridss by PapenfussLab.

the class SmithWatermanFastqAligner method align.

@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);
                }
            }
        }
    }
}
Also used : FastqReader(htsjdk.samtools.fastq.FastqReader) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) FastqRecord(htsjdk.samtools.fastq.FastqRecord) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) ReferenceSequenceFile(htsjdk.samtools.reference.ReferenceSequenceFile) SAMFileHeader(htsjdk.samtools.SAMFileHeader) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile)

Example 15 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project jvarkit by lindenb.

the class FourLinesFastqReader method readNextRecord.

@Override
protected FastqRecord readNextRecord() {
    try {
        // Read sequence header
        this.seqHeader = this.readLine();
        if (this.seqHeader == null)
            return null;
        ++nLines;
        if (StringUtil.isBlank(this.seqHeader)) {
            throw new RuntimeException(error("Missing sequence header"));
        }
        if (!this.seqHeader.startsWith(FastqConstants.SEQUENCE_HEADER)) {
            throw new RuntimeException(error("Sequence header must start with " + FastqConstants.SEQUENCE_HEADER));
        }
        // Read sequence line
        final String seqLine = this.readLine();
        ++nLines;
        checkLine(seqLine, "sequence line");
        // Read quality header
        final String qualHeader = this.readLine();
        ++nLines;
        checkLine(qualHeader, "quality header");
        if (!qualHeader.startsWith(FastqConstants.QUALITY_HEADER)) {
            throw new RuntimeException(error("Quality header must start with " + FastqConstants.QUALITY_HEADER + ": " + qualHeader));
        }
        // Read quality line
        final String qualLine = this.readLine();
        ++nLines;
        checkLine(qualLine, "quality line");
        // Check sequence and quality lines are same length
        if (seqLine.length() != qualLine.length()) {
            throw new RuntimeException(error("Sequence and quality line must be the same length"));
        }
        final FastqRecord frec = new FastqRecord(seqHeader.substring(1, seqHeader.length()), seqLine, qualHeader.substring(1, qualHeader.length()), qualLine);
        this.seqHeader = null;
        return frec;
    } catch (IOException e) {
        throw new RuntimeException(String.format("Error reading fastq '%s'", getAbsolutePath()), e);
    }
}
Also used : FastqRecord(htsjdk.samtools.fastq.FastqRecord) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException)

Aggregations

FastqRecord (htsjdk.samtools.fastq.FastqRecord)40 SAMRecord (htsjdk.samtools.SAMRecord)16 Test (org.junit.Test)12 FastqReader (htsjdk.samtools.fastq.FastqReader)8 File (java.io.File)6 SAMFileHeader (htsjdk.samtools.SAMFileHeader)4 FastqReader (com.github.lindenb.jvarkit.util.picard.FastqReader)3 FourLinesFastqReader (com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader)3 SAMFileWriter (htsjdk.samtools.SAMFileWriter)3 IOException (java.io.IOException)3 Test (org.testng.annotations.Test)3 BufferedReferenceSequenceFile (au.edu.wehi.idsv.picard.BufferedReferenceSequenceFile)2 ChimericAlignment (au.edu.wehi.idsv.sam.ChimericAlignment)2 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)2 Cigar (htsjdk.samtools.Cigar)2 CigarElement (htsjdk.samtools.CigarElement)2 SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)2 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)2 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)2 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)2