use of htsjdk.samtools.util.SamRecordIntervalIteratorFactory in project ASCIIGenome by dariober.
the class SamLocusIterator method iterator.
public Iterator<LocusInfo> iterator() {
if (samIterator != null) {
throw new IllegalStateException("Cannot call iterator() more than once on SamLocusIterator");
}
CloseableIterator<SAMRecord> tempIterator;
if (intervals != null) {
tempIterator = new SamRecordIntervalIteratorFactory().makeSamRecordIntervalIterator(samReader, intervals, useIndex);
} else {
tempIterator = samReader.iterator();
}
if (samFilters != null) {
tempIterator = new FilteringIterator(tempIterator, new AggregateFilter(samFilters));
}
samIterator = new PeekableIterator<SAMRecord>(tempIterator);
return this;
}
use of htsjdk.samtools.util.SamRecordIntervalIteratorFactory in project jvarkit by lindenb.
the class ImpactOfDuplicates method doWork.
@Override
public int doWork(final List<String> args) {
CloseableIterator<Duplicate> dupIter = null;
final List<File> INPUT = args.stream().map(S -> new File(S)).collect(Collectors.toList());
try {
this.duplicates = SortingCollection.newInstance(Duplicate.class, new DuplicateCodec(), new Comparator<Duplicate>() {
@Override
public int compare(Duplicate o1, Duplicate o2) {
return o1.compareTo(o2);
}
}, this.sortingCollectionArgs.getMaxRecordsInRam(), this.sortingCollectionArgs.getTmpPaths());
for (this.bamIndex = 0; this.bamIndex < INPUT.size(); this.bamIndex++) {
int prev_tid = -1;
int prev_pos = -1;
long nLines = 0L;
File inFile = INPUT.get(this.bamIndex);
LOG.info("Processing " + inFile);
IOUtil.assertFileIsReadable(inFile);
SamReader samReader = null;
CloseableIterator<SAMRecord> iter = null;
try {
samReader = SamReaderFactory.make().validationStringency(ValidationStringency.LENIENT).open(inFile);
final SAMFileHeader header = samReader.getFileHeader();
this.samFileDicts.add(header.getSequenceDictionary());
if (BEDFILE == null) {
iter = samReader.iterator();
} else {
IntervalList intervalList = new IntervalList(header);
BufferedReader in = new BufferedReader(new FileReader(BEDFILE));
String line = null;
while ((line = in.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#"))
continue;
String[] tokens = line.split("[\t]");
Interval interval = new Interval(tokens[0], 1 + Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]));
intervalList.add(interval);
}
in.close();
intervalList = intervalList.sorted();
List<Interval> uniqueIntervals = IntervalList.getUniqueIntervals(intervalList, false);
SamRecordIntervalIteratorFactory sriif = new SamRecordIntervalIteratorFactory();
iter = sriif.makeSamRecordIntervalIterator(samReader, uniqueIntervals, false);
}
while (iter.hasNext()) {
SAMRecord rec = iter.next();
if (rec.getReadUnmappedFlag())
continue;
if (!rec.getReadPairedFlag())
continue;
if (rec.getReferenceIndex() != rec.getMateReferenceIndex())
continue;
if (!rec.getProperPairFlag())
continue;
if (!rec.getFirstOfPairFlag())
continue;
if (prev_tid != -1) {
if (prev_tid > rec.getReferenceIndex()) {
throw new IOException("Bad sort order from " + rec);
} else if (prev_tid == rec.getReferenceIndex() && prev_pos > rec.getAlignmentStart()) {
throw new IOException("Bad sort order from " + rec);
} else {
prev_pos = rec.getAlignmentStart();
}
} else {
prev_tid = rec.getReferenceIndex();
prev_pos = -1;
}
if ((++nLines) % 1000000 == 0) {
LOG.info("In " + inFile + " N=" + nLines);
}
Duplicate dup = new Duplicate();
dup.bamIndex = this.bamIndex;
dup.pos = Math.min(rec.getAlignmentStart(), rec.getMateAlignmentStart());
dup.tid = rec.getReferenceIndex();
dup.size = Math.abs(rec.getInferredInsertSize());
this.duplicates.add(dup);
}
} finally {
if (iter != null)
iter.close();
if (samReader != null)
samReader.close();
}
LOG.info("done " + inFile);
}
/**
* loop done, now scan the duplicates
*/
LOG.info("doneAdding");
this.duplicates.doneAdding();
this.out = super.openFileOrStdoutAsPrintStream(outputFile);
out.print("#INTERVAL\tMAX\tMEAN");
for (int i = 0; i < INPUT.size(); ++i) {
out.print('\t');
out.print(INPUT.get(i));
}
out.println();
dupIter = this.duplicates.iterator();
while (dupIter.hasNext()) {
Duplicate dup = dupIter.next();
if (this.duplicatesBuffer.isEmpty() || dup.compareChromPosSize(this.duplicatesBuffer.get(0)) == 0) {
this.duplicatesBuffer.add(dup);
} else {
dumpDuplicatesBuffer(INPUT);
this.duplicatesBuffer.add(dup);
}
}
dumpDuplicatesBuffer(INPUT);
LOG.info("end iterator");
out.flush();
out.close();
} catch (Exception e) {
LOG.error(e);
return -1;
} finally {
if (dupIter != null)
dupIter.close();
LOG.info("cleaning duplicates");
this.duplicates.cleanup();
}
return 0;
}
Aggregations