use of htsjdk.samtools.SAMRecordIterator in project gridss by PapenfussLab.
the class SAMEvidenceSource method iterator.
public CloseableIterator<DirectedEvidence> iterator(final QueryInterval[] intervals) {
SamReader reader = getReader();
// expand query bounds as the alignment for a discordant read pair could fall before or after the breakend interval we are extracting
SAMRecordIterator it = tryOpenReader(reader, QueryIntervalUtil.padIntervals(getContext().getDictionary(), intervals, getMaxConcordantFragmentSize() + 1));
Iterator<DirectedEvidence> eit = asEvidence(it);
eit = Iterators.filter(eit, e -> QueryIntervalUtil.overlaps(intervals, e.getBreakendSummary()));
return new AutoClosingIterator<>(eit, reader, it);
}
use of htsjdk.samtools.SAMRecordIterator in project gridss by PapenfussLab.
the class SAMEvidenceSource method iterator.
public CloseableIterator<DirectedEvidence> iterator() {
SamReader reader = getReader();
SAMRecordIterator it = reader.iterator();
it.assertSorted(SortOrder.coordinate);
Iterator<DirectedEvidence> eit = asEvidence(it);
return new AutoClosingIterator<>(eit, reader, it);
}
use of htsjdk.samtools.SAMRecordIterator in project gridss by PapenfussLab.
the class ExternalProcessFastqAligner method align.
@Override
public void align(final File fastq, final File output, final File reference, final int threads) throws IOException {
List<String> commandline = template.stream().map(s -> String.format(s, fastq.getAbsolutePath(), reference.getAbsolutePath(), threads)).collect(Collectors.toList());
String commandlinestr = commandline.stream().collect(Collectors.joining(" "));
log.info("Invoking external aligner");
log.info(commandlinestr);
Process aligner = new ProcessBuilder(commandline).redirectError(Redirect.INHERIT).directory(output.getParentFile()).start();
final SamReader reader = readerFactory.open(SamInputResource.of(aligner.getInputStream()));
final SAMFileHeader header = reader.getFileHeader();
try (final SAMFileWriter writer = writerFactory.makeWriter(header, false, output, reference)) {
final SAMRecordIterator it = reader.iterator();
while (it.hasNext()) {
writer.addAlignment(it.next());
}
}
ExternalProcessHelper.shutdownAligner(aligner, commandlinestr, reference);
}
use of htsjdk.samtools.SAMRecordIterator in project gridss by PapenfussLab.
the class ExternalProcessStreamingAligner method readAllAlignments.
private void readAllAlignments(final SamReaderFactory readerFactory) {
SamReader fromExternalProgram = readerFactory.open(SamInputResource.of(aligner.getInputStream()));
SAMRecordIterator it = fromExternalProgram.iterator();
while (it.hasNext()) {
SAMRecord r = it.next();
buffer.add(r);
outstandingReads.decrementAndGet();
}
log.info("Reader thread complete.");
}
use of htsjdk.samtools.SAMRecordIterator in project jvarkit by lindenb.
the class PcrClipReads method run.
private int run(final SamReader reader) {
final SAMFileHeader header1 = reader.getFileHeader();
final SAMFileHeader header2 = header1.clone();
Optional<SAMProgramRecord> samProgramRecord = Optional.empty();
if (this.programId) {
final SAMProgramRecord spr = header2.createProgramRecord();
samProgramRecord = Optional.of(spr);
spr.setProgramName(PcrClipReads.class.getSimpleName());
spr.setProgramVersion(this.getGitHash());
spr.setCommandLine(getProgramCommandLine().replace('\t', ' '));
}
header2.addComment(getProgramName() + " " + getVersion() + ": Processed with " + getProgramCommandLine());
header2.setSortOrder(SortOrder.unsorted);
SAMFileWriter sw = null;
SAMRecordIterator iter = null;
try {
sw = this.writingBamArgs.openSAMFileWriter(outputFile, header2, false);
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header1).logger(LOG);
iter = reader.iterator();
while (iter.hasNext()) {
SAMRecord rec = progress.watch(iter.next());
if (this.onlyFlag != -1 && (rec.getFlags() & this.onlyFlag) != 0) {
sw.addAlignment(rec);
continue;
}
if (rec.getReadUnmappedFlag()) {
sw.addAlignment(rec);
continue;
}
final Interval fragment = findInterval(rec);
if (fragment == null) {
rec.setMappingQuality(0);
sw.addAlignment(rec);
continue;
}
// strand is '-' and overap in 5' of PCR fragment
if (rec.getReadNegativeStrandFlag() && fragment.getStart() < rec.getAlignmentStart() && rec.getAlignmentStart() < fragment.getEnd()) {
rec.setMappingQuality(0);
sw.addAlignment(rec);
continue;
}
// strand is '+' and overap in 3' of PCR fragment
if (!rec.getReadNegativeStrandFlag() && fragment.getStart() < rec.getAlignmentEnd() && rec.getAlignmentEnd() < fragment.getEnd()) {
rec.setMappingQuality(0);
sw.addAlignment(rec);
continue;
}
// contained int PCR fragment
if (rec.getAlignmentStart() >= fragment.getStart() && rec.getAlignmentEnd() <= fragment.getEnd()) {
sw.addAlignment(rec);
continue;
}
final ReadClipper readClipper = new ReadClipper();
if (samProgramRecord.isPresent()) {
readClipper.setProgramGroup(samProgramRecord.get().getId());
}
rec = readClipper.clip(rec, fragment);
sw.addAlignment(rec);
}
progress.finish();
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(iter);
CloserUtil.close(sw);
}
}
Aggregations