Search in sources :

Example 16 with SAMRecordIterator

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

the class GenomicProcessingContext method getSamReaderIterator.

private CloseableIterator<SAMRecord> getSamReaderIterator(SamReader reader, SortOrder expectedOrder, File file) {
    SAMRecordIterator rawIterator = reader.iterator();
    if (expectedOrder != null && expectedOrder != SortOrder.unsorted) {
        rawIterator.assertSorted(expectedOrder);
    }
    // wrap so we're happy to close as many times as we want
    CloseableIterator<SAMRecord> safeIterator = new AutoClosingIterator<SAMRecord>(rawIterator, reader);
    return applyCommonSAMRecordFilters(safeIterator);
}
Also used : SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMRecord(htsjdk.samtools.SAMRecord) AutoClosingIterator(au.edu.wehi.idsv.util.AutoClosingIterator)

Example 17 with SAMRecordIterator

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

the class LowResBam2Raster method scan.

private void scan(final SamReader r) {
    final SAMRecordIterator iter = r.query(interval.getContig(), interval.getStart(), interval.getEnd(), false);
    while (iter.hasNext()) {
        final SAMRecord rec = iter.next();
        if (rec.getReadUnmappedFlag())
            continue;
        if (this.samRecordFilter.filterOut(rec)) {
            // don't dicard now, we need to build pairs of reads
            if (!rec.getReadPairedFlag())
                continue;
            if (rec.getMateUnmappedFlag())
                continue;
            if (!this.interval.getContig().equals(rec.getMateReferenceName()))
                continue;
        }
        if (!this.interval.getContig().equals(rec.getReferenceName()))
            continue;
        final SamRecordPair srp = new SamRecordPair(rec);
        if (srp.getEnd() < this.interval.getStart()) {
            continue;
        }
        if (srp.getStart() > this.interval.getEnd()) {
            break;
        }
        final String group = this.groupBy.apply(rec.getReadGroup());
        if (group == null)
            continue;
        PartitionImage partition = this.key2partition.get(group);
        if (partition == null) {
            partition = new PartitionImage(group);
            this.key2partition.put(group, partition);
        }
        partition.visit(rec);
    }
    iter.close();
}
Also used : SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMRecord(htsjdk.samtools.SAMRecord)

Example 18 with SAMRecordIterator

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

the class BamToSVG method doWork.

@Override
public int doWork(List<String> args) {
    /* parse interval */
    if (this.intervalStr == null) {
        LOG.error("bed.interval0.undefined");
        return -1;
    }
    int colon = this.intervalStr.indexOf(':');
    int hyphen = this.intervalStr.indexOf('-', colon + 1);
    if (colon < 1 || hyphen <= colon || hyphen + 1 == intervalStr.length()) {
        LOG.error("Bad interval " + this.intervalStr);
        return -1;
    }
    this.interval = new Interval();
    this.interval.chrom = this.intervalStr.substring(0, colon);
    this.interval.start = Integer.parseInt(this.intervalStr.substring(colon + 1, hyphen)) + 1;
    this.interval.end = Integer.parseInt(this.intervalStr.substring(hyphen + 1));
    this.drawinAreaWidth = Math.max(100, this.drawinAreaWidth);
    SamReader in = null;
    SAMRecordIterator iter = null;
    SamReaderFactory sfrf = SamReaderFactory.makeDefault();
    sfrf.validationStringency(ValidationStringency.SILENT);
    XMLStreamWriter w = null;
    FileOutputStream fout = null;
    try {
        /* get genomic sequence */
        if (this.referenceFile != null) {
            LOG.info("opening " + this.referenceFile);
            this.indexedFastaSequenceFile = new IndexedFastaSequenceFile(this.referenceFile);
            this.genomicSequence = new GenomicSequence(this.indexedFastaSequenceFile, this.interval.chrom);
        }
        for (String vcf : this.vcfFileSet) {
            readVariantFile(vcf);
        }
        /* read SAM data */
        if (args.isEmpty()) {
            LOG.info("Reading from stdin");
            in = sfrf.open(SamInputResource.of(stdin()));
            iter = in.iterator();
            readBamStream(iter);
            iter.close();
            in.close();
        } else {
            for (String arg : args) {
                File filename = new File(arg);
                LOG.info("Reading from " + filename);
                in = sfrf.open(SamInputResource.of(filename));
                if (in.hasIndex()) {
                    iter = in.query(this.interval.getChrom(), this.interval.getStart(), this.interval.getEnd(), false);
                } else {
                    LOG.info("Bam file not indexed !! " + filename);
                    iter = in.iterator();
                }
                readBamStream(iter);
                iter.close();
                in.close();
            }
        }
        this.featureWidth = this.drawinAreaWidth / (double) ((this.interval.end - this.interval.start) + 1);
        this.featureHeight = Math.min(Math.max(5.0, this.featureWidth), 30);
        this.HEIGHT_RULER = (int) (this.niceIntFormat.format(this.interval.end).length() * this.featureHeight + 5);
        LOG.info("Feature height:" + this.featureHeight);
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        if (this.outputFile == null) {
            w = xof.createXMLStreamWriter(stdout(), "UTF-8");
        } else {
            fout = new FileOutputStream(this.outputFile);
            w = xof.createXMLStreamWriter(fout, "UTF-8");
        }
        w.writeStartDocument("UTF-8", "1.0");
        printDocument(w, intervalStr);
        w.writeEndDocument();
        w.flush();
        w.close();
        return RETURN_OK;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(iter);
        CloserUtil.close(fout);
        CloserUtil.close(in);
        CloserUtil.close(this.indexedFastaSequenceFile);
        this.indexedFastaSequenceFile = null;
        this.interval = null;
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SamReaderFactory(htsjdk.samtools.SamReaderFactory) GenomicSequence(com.github.lindenb.jvarkit.util.picard.GenomicSequence) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) SamReader(htsjdk.samtools.SamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile)

Example 19 with SAMRecordIterator

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

the class Bam2Xml method run.

private int run(SamReader samReader) {
    OutputStream fout = null;
    SAMRecordIterator iter = null;
    XMLStreamWriter w = null;
    try {
        XMLOutputFactory xmlfactory = XMLOutputFactory.newInstance();
        if (this.outputFile != null) {
            fout = IOUtils.openFileForWriting(this.outputFile);
            w = xmlfactory.createXMLStreamWriter(fout, "UTF-8");
        } else {
            w = xmlfactory.createXMLStreamWriter(stdout(), "UTF-8");
        }
        w.writeStartDocument("UTF-8", "1.0");
        final SAMFileHeader header = samReader.getFileHeader();
        final SAMXMLWriter xw = new SAMXMLWriter(w, header);
        final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
        iter = samReader.iterator();
        while (iter.hasNext()) {
            xw.addAlignment(progress.watch(iter.next()));
        }
        xw.close();
        w.writeEndDocument();
        if (fout != null)
            fout.flush();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        return -1;
    } finally {
        CloserUtil.close(w);
        CloserUtil.close(iter);
        CloserUtil.close(fout);
    }
    return 0;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) SAMFileHeader(htsjdk.samtools.SAMFileHeader) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 20 with SAMRecordIterator

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

the class BamIndexReadNames method indexBamFile.

private void indexBamFile(File bamFile) throws IOException {
    NameIndexDef indexDef = new NameIndexDef();
    SortingCollection<NameAndPos> sorting = null;
    LOG.info("Opening " + bamFile);
    SamReader sfr = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(bamFile);
    sorting = SortingCollection.newInstance(NameAndPos.class, new NameAndPosCodec(), new NameAndPosComparator(), maxRecordsInRAM, bamFile.getParentFile().toPath());
    sorting.setDestructiveIteration(true);
    if (sfr.getFileHeader().getSortOrder() != SortOrder.coordinate) {
        throw new IOException("not SortOrder.coordinate " + sfr.getFileHeader().getSortOrder());
    }
    SAMRecordIterator iter = sfr.iterator();
    SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(sfr.getFileHeader().getSequenceDictionary());
    while (iter.hasNext()) {
        SAMRecord rec = iter.next();
        progress.watch(rec);
        NameAndPos nap = new NameAndPos();
        nap.name = rec.getReadName();
        indexDef.maxNameLengt = Math.max(nap.name.length() + 1, indexDef.maxNameLengt);
        nap.tid = rec.getReferenceIndex();
        nap.pos = rec.getAlignmentStart();
        indexDef.countReads++;
        sorting.add(nap);
    }
    progress.finish();
    iter.close();
    sfr.close();
    sorting.doneAdding();
    LOG.info("Done Adding. N=" + indexDef.countReads);
    File indexFile = new File(bamFile.getParentFile(), bamFile.getName() + NAME_IDX_EXTENSION);
    LOG.info("Writing index " + indexFile);
    FileOutputStream raf = new FileOutputStream(indexFile);
    ByteBuffer byteBuff = ByteBuffer.allocate(8 + 4);
    byteBuff.putLong(indexDef.countReads);
    byteBuff.putInt(indexDef.maxNameLengt);
    raf.write(byteBuff.array());
    byteBuff = ByteBuffer.allocate(indexDef.maxNameLengt + 4 + 4);
    CloseableIterator<NameAndPos> iter2 = sorting.iterator();
    while (iter2.hasNext()) {
        byteBuff.rewind();
        NameAndPos nap = iter2.next();
        for (int i = 0; i < nap.name.length(); ++i) {
            byteBuff.put((byte) nap.name.charAt(i));
        }
        for (int i = nap.name.length(); i < indexDef.maxNameLengt; ++i) {
            byteBuff.put((byte) '\0');
        }
        byteBuff.putInt(nap.tid);
        byteBuff.putInt(nap.pos);
        raf.write(byteBuff.array());
    }
    raf.flush();
    raf.close();
    sorting.cleanup();
}
Also used : SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SamReader(htsjdk.samtools.SamReader) SAMRecord(htsjdk.samtools.SAMRecord) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

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