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);
}
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();
}
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;
}
}
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;
}
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();
}
Aggregations