Search in sources :

Example 1 with NmTagIterator

use of au.edu.wehi.idsv.sam.NmTagIterator 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 2 with NmTagIterator

use of au.edu.wehi.idsv.sam.NmTagIterator in project gridss by PapenfussLab.

the class SplitReadRealigner method mergeSupplementaryAlignment.

private void mergeSupplementaryAlignment(File input, List<File> aligned, File output) throws IOException {
    log.info("Merging split read alignments for ", output);
    File suppMerged = FileSystemContext.getWorkingFileFor(output, "gridss.tmp.SplitReadAligner.sa.");
    File tmpoutput = FileSystemContext.getWorkingFileFor(output);
    tmpFiles.add(suppMerged);
    tmpFiles.add(tmpoutput);
    List<SamReader> suppReaders = new ArrayList<>();
    List<PeekingIterator<SAMRecord>> suppIt = new ArrayList<>();
    SAMFileHeader header;
    try (SamReader reader = readerFactory.open(input)) {
        header = reader.getFileHeader();
        for (File sf : aligned) {
            SamReader suppReader = readerFactory.open(sf);
            suppReaders.add(suppReader);
            suppIt.add(new AsyncBufferedIterator<>(new NmTagIterator(suppReader.iterator(), pc.getReference()), sf.getName()));
        }
        try (SAMFileWriter inputWriter = writerFactory.makeSAMOrBAMWriter(header, true, tmpoutput)) {
            try (SAMFileWriter suppWriter = writerFactory.makeSAMOrBAMWriter(header, false, suppMerged)) {
                try (AsyncBufferedIterator<SAMRecord> bufferedIt = new AsyncBufferedIterator<>(new NmTagIterator(reader.iterator(), pc.getReference()), input.getName())) {
                    mergeSupplementaryAlignment(bufferedIt, suppIt, inputWriter, suppWriter);
                }
            }
        }
    } finally {
        for (Iterator<SAMRecord> it : suppIt) {
            CloserUtil.close(it);
        }
        for (SamReader sr : suppReaders) {
            sr.close();
        }
    }
    if (header.getSortOrder() != null && header.getSortOrder() != SortOrder.unsorted) {
        File suppMergedsorted = FileSystemContext.getWorkingFileFor(output, "gridss.tmp.SplitReadAligner.sorted.sa.");
        tmpFiles.add(suppMergedsorted);
        SAMFileUtil.sort(pc.getFileSystemContext(), suppMerged, suppMergedsorted, header.getSortOrder());
        FileHelper.move(suppMergedsorted, suppMerged, true);
    }
    SAMFileUtil.merge(ImmutableList.of(tmpoutput, suppMerged), output);
}
Also used : NmTagIterator(au.edu.wehi.idsv.sam.NmTagIterator) SAMFileWriter(htsjdk.samtools.SAMFileWriter) ArrayList(java.util.ArrayList) AsyncBufferedIterator(au.edu.wehi.idsv.util.AsyncBufferedIterator) PeekingIterator(com.google.common.collect.PeekingIterator) SamReader(htsjdk.samtools.SamReader) SAMRecord(htsjdk.samtools.SAMRecord) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

Aggregations

NmTagIterator (au.edu.wehi.idsv.sam.NmTagIterator)2 AsyncBufferedIterator (au.edu.wehi.idsv.util.AsyncBufferedIterator)2 SAMRecord (htsjdk.samtools.SAMRecord)2 TemplateTagsIterator (au.edu.wehi.idsv.sam.TemplateTagsIterator)1 PeekingIterator (com.google.common.collect.PeekingIterator)1 SAMFileHeader (htsjdk.samtools.SAMFileHeader)1 SAMFileWriter (htsjdk.samtools.SAMFileWriter)1 SamReader (htsjdk.samtools.SamReader)1 ProgressLogger (htsjdk.samtools.util.ProgressLogger)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1