Search in sources :

Example 66 with SAMRecordIterator

use of htsjdk.samtools.SAMRecordIterator in project gridss by PapenfussLab.

the class SAMEvidenceSource method iterator.

public CloseableIterator<DirectedEvidence> iterator(final QueryInterval[] intervals) {
    SamReader reader = getReader();
    // expand query bounds as the alignment for a discordant read pair could fall before or after the breakend interval we are extracting
    SAMRecordIterator it = tryOpenReader(reader, QueryIntervalUtil.padIntervals(getContext().getDictionary(), intervals, getMaxConcordantFragmentSize() + 1));
    Iterator<DirectedEvidence> eit = asEvidence(it);
    eit = Iterators.filter(eit, e -> QueryIntervalUtil.overlaps(intervals, e.getBreakendSummary()));
    return new AutoClosingIterator<>(eit, reader, it);
}
Also used : TextCigarCodec(htsjdk.samtools.TextCigarCodec) Cigar(htsjdk.samtools.Cigar) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Iterables(com.google.common.collect.Iterables) SAMRecordUtil(au.edu.wehi.idsv.sam.SAMRecordUtil) BufferedIterator(au.edu.wehi.idsv.util.BufferedIterator) AsyncBufferedIterator(au.edu.wehi.idsv.util.AsyncBufferedIterator) Iterators(com.google.common.collect.Iterators) ComputeSamTags(gridss.ComputeSamTags) SoftClipsToSplitReads(gridss.SoftClipsToSplitReads) SortOrder(htsjdk.samtools.SAMFileHeader.SortOrder) GridssConfiguration(au.edu.wehi.idsv.configuration.GridssConfiguration) SAMTag(htsjdk.samtools.SAMTag) CollectGridssMetrics(gridss.analysis.CollectGridssMetrics) Lists(com.google.common.collect.Lists) AutoClosingIterator(au.edu.wehi.idsv.util.AutoClosingIterator) ImmutableList(com.google.common.collect.ImmutableList) FileHelper(au.edu.wehi.idsv.util.FileHelper) StructuralVariantReadMetrics(gridss.analysis.StructuralVariantReadMetrics) ChimericAlignment(au.edu.wehi.idsv.sam.ChimericAlignment) CommandLineProgramHelper(gridss.cmdline.CommandLineProgramHelper) PairOrientation(htsjdk.samtools.SamPairUtil.PairOrientation) Iterator(java.util.Iterator) IdsvSamFileMetrics(au.edu.wehi.idsv.metrics.IdsvSamFileMetrics) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) IOException(java.io.IOException) SoftClipConfiguration(au.edu.wehi.idsv.configuration.SoftClipConfiguration) OrderAssertingIterator(au.edu.wehi.idsv.validation.OrderAssertingIterator) SamReader(htsjdk.samtools.SamReader) MetricsFile(htsjdk.samtools.metrics.MetricsFile) Collectors(java.util.stream.Collectors) File(java.io.File) IntervalBed(au.edu.wehi.idsv.bed.IntervalBed) AutoClosingMergedIterator(au.edu.wehi.idsv.util.AutoClosingMergedIterator) SAMRecord(htsjdk.samtools.SAMRecord) CommandLineProgram(picard.cmdline.CommandLineProgram) List(java.util.List) ExtractSVReads(gridss.ExtractSVReads) Log(htsjdk.samtools.util.Log) QueryInterval(htsjdk.samtools.QueryInterval) ReferenceCommandLineProgram(gridss.cmdline.ReferenceCommandLineProgram) SAMFileUtil(au.edu.wehi.idsv.sam.SAMFileUtil) CigarUtil(au.edu.wehi.idsv.sam.CigarUtil) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SamReader(htsjdk.samtools.SamReader) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) AutoClosingIterator(au.edu.wehi.idsv.util.AutoClosingIterator)

Example 67 with SAMRecordIterator

use of htsjdk.samtools.SAMRecordIterator in project gridss by PapenfussLab.

the class SAMEvidenceSource method iterator.

public CloseableIterator<DirectedEvidence> iterator() {
    SamReader reader = getReader();
    SAMRecordIterator it = reader.iterator();
    it.assertSorted(SortOrder.coordinate);
    Iterator<DirectedEvidence> eit = asEvidence(it);
    return new AutoClosingIterator<>(eit, reader, it);
}
Also used : SamReader(htsjdk.samtools.SamReader) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) AutoClosingIterator(au.edu.wehi.idsv.util.AutoClosingIterator)

Example 68 with SAMRecordIterator

use of htsjdk.samtools.SAMRecordIterator in project gridss by PapenfussLab.

the class ExternalProcessFastqAligner method align.

@Override
public void align(final File fastq, final File output, final File reference, final int threads) throws IOException {
    List<String> commandline = template.stream().map(s -> String.format(s, fastq.getAbsolutePath(), reference.getAbsolutePath(), threads)).collect(Collectors.toList());
    String commandlinestr = commandline.stream().collect(Collectors.joining(" "));
    log.info("Invoking external aligner");
    log.info(commandlinestr);
    Process aligner = new ProcessBuilder(commandline).redirectError(Redirect.INHERIT).directory(output.getParentFile()).start();
    final SamReader reader = readerFactory.open(SamInputResource.of(aligner.getInputStream()));
    final SAMFileHeader header = reader.getFileHeader();
    try (final SAMFileWriter writer = writerFactory.makeWriter(header, false, output, reference)) {
        final SAMRecordIterator it = reader.iterator();
        while (it.hasNext()) {
            writer.addAlignment(it.next());
        }
    }
    ExternalProcessHelper.shutdownAligner(aligner, commandlinestr, reference);
}
Also used : SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) Redirect(java.lang.ProcessBuilder.Redirect) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) IOException(java.io.IOException) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SamReader(htsjdk.samtools.SamReader) Collectors(java.util.stream.Collectors) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File) List(java.util.List) SamInputResource(htsjdk.samtools.SamInputResource) Log(htsjdk.samtools.util.Log) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SamReader(htsjdk.samtools.SamReader) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileHeader(htsjdk.samtools.SAMFileHeader)

Example 69 with SAMRecordIterator

use of htsjdk.samtools.SAMRecordIterator in project gridss by PapenfussLab.

the class ExternalProcessStreamingAligner method readAllAlignments.

private void readAllAlignments(final SamReaderFactory readerFactory) {
    SamReader fromExternalProgram = readerFactory.open(SamInputResource.of(aligner.getInputStream()));
    SAMRecordIterator it = fromExternalProgram.iterator();
    while (it.hasNext()) {
        SAMRecord r = it.next();
        buffer.add(r);
        outstandingReads.decrementAndGet();
    }
    log.info("Reader thread complete.");
}
Also used : SamReader(htsjdk.samtools.SamReader) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMRecord(htsjdk.samtools.SAMRecord)

Example 70 with SAMRecordIterator

use of htsjdk.samtools.SAMRecordIterator in project jvarkit by lindenb.

the class PcrClipReads method run.

private int run(final SamReader reader) {
    final SAMFileHeader header1 = reader.getFileHeader();
    final SAMFileHeader header2 = header1.clone();
    Optional<SAMProgramRecord> samProgramRecord = Optional.empty();
    if (this.programId) {
        final SAMProgramRecord spr = header2.createProgramRecord();
        samProgramRecord = Optional.of(spr);
        spr.setProgramName(PcrClipReads.class.getSimpleName());
        spr.setProgramVersion(this.getGitHash());
        spr.setCommandLine(getProgramCommandLine().replace('\t', ' '));
    }
    header2.addComment(getProgramName() + " " + getVersion() + ": Processed with " + getProgramCommandLine());
    header2.setSortOrder(SortOrder.unsorted);
    SAMFileWriter sw = null;
    SAMRecordIterator iter = null;
    try {
        sw = this.writingBamArgs.openSAMFileWriter(outputFile, header2, false);
        final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header1).logger(LOG);
        iter = reader.iterator();
        while (iter.hasNext()) {
            SAMRecord rec = progress.watch(iter.next());
            if (this.onlyFlag != -1 && (rec.getFlags() & this.onlyFlag) != 0) {
                sw.addAlignment(rec);
                continue;
            }
            if (rec.getReadUnmappedFlag()) {
                sw.addAlignment(rec);
                continue;
            }
            final Interval fragment = findInterval(rec);
            if (fragment == null) {
                rec.setMappingQuality(0);
                sw.addAlignment(rec);
                continue;
            }
            // strand is '-' and overap in 5' of PCR fragment
            if (rec.getReadNegativeStrandFlag() && fragment.getStart() < rec.getAlignmentStart() && rec.getAlignmentStart() < fragment.getEnd()) {
                rec.setMappingQuality(0);
                sw.addAlignment(rec);
                continue;
            }
            // strand is '+' and overap in 3' of PCR fragment
            if (!rec.getReadNegativeStrandFlag() && fragment.getStart() < rec.getAlignmentEnd() && rec.getAlignmentEnd() < fragment.getEnd()) {
                rec.setMappingQuality(0);
                sw.addAlignment(rec);
                continue;
            }
            // contained int PCR fragment
            if (rec.getAlignmentStart() >= fragment.getStart() && rec.getAlignmentEnd() <= fragment.getEnd()) {
                sw.addAlignment(rec);
                continue;
            }
            final ReadClipper readClipper = new ReadClipper();
            if (samProgramRecord.isPresent()) {
                readClipper.setProgramGroup(samProgramRecord.get().getId());
            }
            rec = readClipper.clip(rec, fragment);
            sw.addAlignment(rec);
        }
        progress.finish();
        return 0;
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(iter);
        CloserUtil.close(sw);
    }
}
Also used : SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) SAMFileHeader(htsjdk.samtools.SAMFileHeader) SAMProgramRecord(htsjdk.samtools.SAMProgramRecord) Interval(htsjdk.samtools.util.Interval)

Aggregations

SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)107 SAMRecord (htsjdk.samtools.SAMRecord)92 SamReader (htsjdk.samtools.SamReader)83 SAMFileHeader (htsjdk.samtools.SAMFileHeader)49 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)47 File (java.io.File)47 SAMFileWriter (htsjdk.samtools.SAMFileWriter)45 IOException (java.io.IOException)41 ArrayList (java.util.ArrayList)34 CigarElement (htsjdk.samtools.CigarElement)30 Cigar (htsjdk.samtools.Cigar)26 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)24 SamReaderFactory (htsjdk.samtools.SamReaderFactory)21 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)18 CigarOperator (htsjdk.samtools.CigarOperator)16 Interval (htsjdk.samtools.util.Interval)16 PrintWriter (java.io.PrintWriter)15 HashMap (java.util.HashMap)15 SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)14 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)14