Search in sources :

Example 1 with ProgressLogger

use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.

the class SequentialCoverageAnnotator method createLookup.

private List<ReferenceCoverageLookup> createLookup(ProcessingContext context, List<SAMEvidenceSource> sources, int windowSize) {
    List<ReferenceCoverageLookup> result = new ArrayList<>();
    for (SAMEvidenceSource ses : sources) {
        assert (ses.getSourceCategory() >= 0);
        assert (ses.getSourceCategory() < context.getCategoryCount());
        // one read-ahead thread per input file
        SamReader reader = SamReaderFactory.makeDefault().open(ses.getFile());
        SAMRecordIterator rawIterator = reader.iterator();
        rawIterator.assertSorted(SortOrder.coordinate);
        CloseableIterator<SAMRecord> sit = new AsyncBufferedIterator<SAMRecord>(rawIterator, ses.getFile().getName() + "-Coverage");
        // close the async iterator first to prevent aysnc reading from a closed stream
        toclose.add(sit);
        toclose.add(rawIterator);
        toclose.add(reader);
        sit = new ProgressLoggingSAMRecordIterator(sit, new ProgressLogger(log, 10000000));
        SequentialReferenceCoverageLookup sourceLookup = new SequentialReferenceCoverageLookup(sit, ses.getMetrics().getIdsvMetrics(), ses.getReadPairConcordanceCalculator(), windowSize, ses.getSourceCategory(), context.isFilterDuplicates());
        context.registerBuffer(ses.getFile().getName(), sourceLookup);
        result.add(sourceLookup);
    }
    return result;
}
Also used : SamReader(htsjdk.samtools.SamReader) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMRecord(htsjdk.samtools.SAMRecord) ArrayList(java.util.ArrayList) AsyncBufferedIterator(au.edu.wehi.idsv.util.AsyncBufferedIterator) ProgressLogger(htsjdk.samtools.util.ProgressLogger)

Example 2 with ProgressLogger

use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.

the class ComputeSamTags method compute.

public static void compute(Iterator<SAMRecord> rawit, SAMFileWriter writer, ReferenceLookup reference, Set<String> tags, boolean softenHardClips, boolean fixMates, boolean recalculateSupplementary, String threadprefix) throws IOException {
    ProgressLogger progress = new ProgressLogger(log);
    try (CloseableIterator<SAMRecord> aysncit = new AsyncBufferedIterator<SAMRecord>(rawit, threadprefix + "raw")) {
        Iterator<SAMRecord> it = aysncit;
        if (tags.contains(SAMTag.NM.name()) || tags.contains(SAMTag.SA.name())) {
            it = new AsyncBufferedIterator<SAMRecord>(it, threadprefix + "nm");
            it = new NmTagIterator(it, reference);
        }
        if (!Sets.intersection(tags, SAMRecordUtil.TEMPLATE_TAGS).isEmpty() || softenHardClips) {
            it = new TemplateTagsIterator(it, softenHardClips, fixMates, recalculateSupplementary, tags);
            it = new AsyncBufferedIterator<SAMRecord>(it, threadprefix + "tags");
        }
        while (it.hasNext()) {
            SAMRecord r = it.next();
            writer.addAlignment(r);
            progress.record(r);
        }
    }
}
Also used : TemplateTagsIterator(au.edu.wehi.idsv.sam.TemplateTagsIterator) NmTagIterator(au.edu.wehi.idsv.sam.NmTagIterator) SAMRecord(htsjdk.samtools.SAMRecord) AsyncBufferedIterator(au.edu.wehi.idsv.util.AsyncBufferedIterator) ProgressLogger(htsjdk.samtools.util.ProgressLogger)

Example 3 with ProgressLogger

use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.

the class ByReadNameSinglePassSamProgram method makeItSo.

public static void makeItSo(final File input, final File referenceSequence, final boolean assumeSorted, final long stopAfter, final Collection<ByReadNameSinglePassSamProgram> programs) throws FileNotFoundException {
    // Setup the standard inputs
    IOUtil.assertFileIsReadable(input);
    SamReader in = SamReaderFactory.makeDefault().referenceSequence(referenceSequence).open(input);
    // Optionally load up the reference sequence and double check sequence dictionaries
    final ReferenceLookup lookup;
    if (referenceSequence == null) {
        lookup = null;
    } else {
        IOUtil.assertFileIsReadable(referenceSequence);
        lookup = new TwoBitBufferedReferenceSequenceFile(new IndexedFastaSequenceFile(referenceSequence));
        if (!in.getFileHeader().getSequenceDictionary().isEmpty()) {
            SequenceUtil.assertSequenceDictionariesEqual(in.getFileHeader().getSequenceDictionary(), lookup.getSequenceDictionary());
        }
    }
    // Check on the sort order of the BAM file
    final SortOrder sort = in.getFileHeader().getSortOrder();
    if (sort != SortOrder.queryname) {
        if (assumeSorted) {
            log.warn("File reports sort order '" + sort + "', assuming it's queryname sorted anyway.");
        } else {
            throw new PicardException("File " + input.getAbsolutePath() + " should be queryname sorted but " + "the header says the sort order is " + sort + ". If you believe the file " + "to be queryname sorted you may pass ASSUME_SORTED=true");
        }
    }
    for (final ByReadNameSinglePassSamProgram program : programs) {
        program.setReference(lookup);
        program.setup(in.getFileHeader(), input);
    }
    final ProgressLogger progress = new ProgressLogger(log);
    final SAMRecordIterator rawit = in.iterator();
    final CloseableIterator<SAMRecord> it = new AsyncBufferedIterator<SAMRecord>(rawit, "ByReadNameSinglePassSamProgram " + input.getName());
    try {
        List<SAMRecord> currentRecords = new ArrayList<>();
        String currentReadName = null;
        while (it.hasNext()) {
            SAMRecord r = it.next();
            String readname = r.getReadName();
            // if read name we have to just treat it as a single read
            if (readname == null || !readname.equals(currentReadName)) {
                if (currentRecords.size() > 0) {
                    for (final ByReadNameSinglePassSamProgram program : programs) {
                        program.acceptFragment(currentRecords, lookup);
                    }
                }
                currentRecords.clear();
                currentReadName = readname;
                if (stopAfter > 0 && progress.getCount() >= stopAfter) {
                    break;
                }
            }
            currentRecords.add(r);
            progress.record(r);
        }
        if (currentRecords.size() > 0) {
            for (final ByReadNameSinglePassSamProgram program : programs) {
                program.acceptFragment(currentRecords, lookup);
            }
        }
    } finally {
        CloserUtil.close(it);
        CloserUtil.close(rawit);
        CloserUtil.close(in);
    }
    for (final ByReadNameSinglePassSamProgram program : programs) {
        program.finish();
    }
}
Also used : TwoBitBufferedReferenceSequenceFile(au.edu.wehi.idsv.picard.TwoBitBufferedReferenceSequenceFile) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) ArrayList(java.util.ArrayList) SortOrder(htsjdk.samtools.SAMFileHeader.SortOrder) AsyncBufferedIterator(au.edu.wehi.idsv.util.AsyncBufferedIterator) ProgressLogger(htsjdk.samtools.util.ProgressLogger) PicardException(picard.PicardException) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) SamReader(htsjdk.samtools.SamReader) ReferenceLookup(au.edu.wehi.idsv.picard.ReferenceLookup) SAMRecord(htsjdk.samtools.SAMRecord)

Example 4 with ProgressLogger

use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.

the class VcfTransformCommandLineProgram method saveVcf.

protected void saveVcf(File file, Iterator<IdsvVariantContext> calls) throws IOException {
    File tmp = gridss.Defaults.OUTPUT_TO_TEMP_FILE ? FileSystemContext.getWorkingFileFor(file) : file;
    final ProgressLogger writeProgress = new ProgressLogger(log);
    try (VariantContextWriter vcfWriter = getContext().getVariantContextWriter(tmp, true)) {
        while (calls.hasNext()) {
            IdsvVariantContext record = calls.next();
            vcfWriter.add(record);
            writeProgress.record(record.getContig(), record.getStart());
        }
    }
    if (tmp != file) {
        FileHelper.move(tmp, file, true);
    }
}
Also used : IdsvVariantContext(au.edu.wehi.idsv.IdsvVariantContext) ProgressLogger(htsjdk.samtools.util.ProgressLogger) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) File(java.io.File)

Example 5 with ProgressLogger

use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.

the class ReadsToBedpe method doWork.

@Override
protected int doWork() {
    log.debug("Setting language-neutral locale");
    java.util.Locale.setDefault(Locale.ROOT);
    validateParameters();
    SamReaderFactory readerFactory = SamReaderFactory.make();
    try {
        try (SamReader reader = readerFactory.open(INPUT)) {
            SAMFileHeader header = reader.getFileHeader();
            SAMSequenceDictionary dict = header.getSequenceDictionary();
            // ExecutorService threadpool = Executors.newFixedThreadPool(WORKER_THREADS, new ThreadFactoryBuilder().setDaemon(false).setNameFormat("Worker-%d").build());
            try (CloseableIterator<SAMRecord> rawit = new AsyncBufferedIterator<SAMRecord>(reader.iterator(), 3, 64)) {
                ProgressLoggingSAMRecordIterator logit = new ProgressLoggingSAMRecordIterator(rawit, new ProgressLogger(log));
                // ParallelTransformIterator<SAMRecord, List<String>> it = new ParallelTransformIterator<>(logit, r -> asBedPe(dict, r), 16 + 2 * WORKER_THREADS, threadpool);
                Iterator<List<String>> it = Iterators.transform(logit, r -> asBedPe(dict, r));
                int i = 0;
                try (BufferedWriter writer = new BufferedWriter(new FileWriter(OUTPUT))) {
                    while (it.hasNext()) {
                        for (String line : it.next()) {
                            if (line != null) {
                                writer.write(line);
                                writer.write('\n');
                            }
                        }
                        i++;
                    }
                    if (i % 1000 == 0) {
                        writer.flush();
                    }
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
        return -1;
    }
    return 0;
}
Also used : SamReaderFactory(htsjdk.samtools.SamReaderFactory) FileWriter(java.io.FileWriter) AsyncBufferedIterator(au.edu.wehi.idsv.util.AsyncBufferedIterator) ProgressLogger(htsjdk.samtools.util.ProgressLogger) IOException(java.io.IOException) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) DirectedBreakpoint(au.edu.wehi.idsv.DirectedBreakpoint) BufferedWriter(java.io.BufferedWriter) SamReader(htsjdk.samtools.SamReader) SAMRecord(htsjdk.samtools.SAMRecord) ArrayList(java.util.ArrayList) List(java.util.List) ProgressLoggingSAMRecordIterator(au.edu.wehi.idsv.ProgressLoggingSAMRecordIterator) SAMFileHeader(htsjdk.samtools.SAMFileHeader)

Aggregations

ProgressLogger (htsjdk.samtools.util.ProgressLogger)7 SAMRecord (htsjdk.samtools.SAMRecord)6 AsyncBufferedIterator (au.edu.wehi.idsv.util.AsyncBufferedIterator)4 SamReader (htsjdk.samtools.SamReader)4 ArrayList (java.util.ArrayList)4 ProgressLoggingSAMRecordIterator (au.edu.wehi.idsv.ProgressLoggingSAMRecordIterator)2 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)2 SamReaderFactory (htsjdk.samtools.SamReaderFactory)2 File (java.io.File)2 IOException (java.io.IOException)2 DirectedBreakpoint (au.edu.wehi.idsv.DirectedBreakpoint)1 IdsvVariantContext (au.edu.wehi.idsv.IdsvVariantContext)1 PositionalAssembler (au.edu.wehi.idsv.debruijn.positional.PositionalAssembler)1 ReferenceLookup (au.edu.wehi.idsv.picard.ReferenceLookup)1 TwoBitBufferedReferenceSequenceFile (au.edu.wehi.idsv.picard.TwoBitBufferedReferenceSequenceFile)1 NmTagIterator (au.edu.wehi.idsv.sam.NmTagIterator)1 TemplateTagsIterator (au.edu.wehi.idsv.sam.TemplateTagsIterator)1 SAMFileHeader (htsjdk.samtools.SAMFileHeader)1 SortOrder (htsjdk.samtools.SAMFileHeader.SortOrder)1 SAMFileWriter (htsjdk.samtools.SAMFileWriter)1