use of htsjdk.samtools.util.CloseableIterator 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.util.CloseableIterator in project gridss by PapenfussLab.
the class AssemblyEvidenceSource method getAllAssemblies_single_threaded.
@SuppressWarnings("unused")
private Iterator<SAMRecord> getAllAssemblies_single_threaded() {
ProgressLogger progressLog = new ProgressLogger(log);
List<Iterator<SAMRecord>> list = new ArrayList<>();
for (BreakendDirection direction : BreakendDirection.values()) {
CloseableIterator<DirectedEvidence> it = mergedIterator(source, false);
Iterator<DirectedEvidence> throttledIt = throttled(it);
ProgressLoggingDirectedEvidenceIterator<DirectedEvidence> loggedIt = new ProgressLoggingDirectedEvidenceIterator<>(getContext(), throttledIt, progressLog);
Iterator<SAMRecord> evidenceIt = new PositionalAssembler(getContext(), this, new SequentialIdGenerator("asm"), loggedIt, direction);
list.add(evidenceIt);
}
return Iterators.concat(list.iterator());
}
use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.
the class FindAVariation method scan.
private void scan(final BufferedReader in) throws IOException {
String line;
while ((line = in.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#"))
continue;
final File f = new File(line);
if (!f.isFile())
continue;
if (!f.canRead())
continue;
if (!VCFUtils.isVcfFile(f))
continue;
VcfIterator iter = null;
if (VCFUtils.isTribbleVcfFile(f)) {
VCFFileReader r = null;
try {
r = new VCFFileReader(f, true);
final VCFHeader header = r.getFileHeader();
for (final Mutation m : convertFromVcfHeader(f, header)) {
final CloseableIterator<VariantContext> iter2 = r.query(m.chrom, m.pos, m.pos);
while (iter2.hasNext()) {
final VariantContext ctx = iter2.next();
if (this.onlySnp) {
if (ctx.getStart() != m.pos || ctx.getEnd() != m.pos)
continue;
}
report(f, header, ctx, m);
}
CloserUtil.close(iter2);
}
} catch (final htsjdk.tribble.TribbleException.InvalidHeader err) {
LOG.warn(f + "\t" + err.getMessage());
} catch (final Exception err) {
LOG.severe("cannot read " + f, err);
} finally {
CloserUtil.close(r);
}
} else if (VCFUtils.isTabixVcfFile(f)) {
TabixVcfFileReader r = null;
try {
r = new TabixVcfFileReader(f.getPath());
final VCFHeader header = r.getHeader();
for (final Mutation m : convertFromVcfHeader(f, header)) {
final Iterator<VariantContext> iter2 = r.iterator(m.chrom, m.pos, m.pos);
while (iter2.hasNext()) {
final VariantContext ctx = iter2.next();
if (this.onlySnp) {
if (ctx.getStart() != m.pos || ctx.getEnd() != m.pos)
continue;
}
report(f, header, ctx, m);
}
CloserUtil.close(iter2);
}
} catch (final htsjdk.tribble.TribbleException.InvalidHeader err) {
LOG.warn(f + "\t" + err.getMessage());
} catch (final Exception err) {
LOG.severe("cannot read " + f, err);
} finally {
CloserUtil.close(r);
}
} else if (!this.indexedOnly) {
try {
iter = VCFUtils.createVcfIteratorFromFile(f);
final VCFHeader header = iter.getHeader();
final Set<Mutation> mutlist = convertFromVcfHeader(f, iter.getHeader());
while (iter.hasNext()) {
final VariantContext ctx = iter.next();
final Mutation m = new Mutation(ctx.getContig(), ctx.getStart());
for (final Mutation m2 : mutlist) {
if (m.equals(m2)) {
if (this.onlySnp) {
if (ctx.getStart() != m2.pos || ctx.getEnd() != m2.pos)
continue;
}
report(f, header, ctx, m2);
break;
}
}
}
} catch (final htsjdk.tribble.TribbleException.InvalidHeader err) {
LOG.warn(f + "\t" + err.getMessage());
} catch (final Exception err) {
LOG.severe("Error in " + f, err);
} finally {
CloserUtil.close(iter);
}
}
}
}
use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.
the class VCFComposite method doWork.
@Override
public int doWork(final List<String> args) {
PrintWriter out = null;
try {
out = super.openFileOrStdoutAsPrintWriter(this.outputFile);
if (listModels) {
for (final Type t : Type.values()) {
out.println(t.name());
out.println("\t" + t.getDescription());
}
out.flush();
return 0;
}
this.pedigree = Pedigree.newParser().parse(pedigreeFile);
if (this.pedigree.getAffected().isEmpty()) {
LOG.error("No Affected sample in " + this.pedigreeFile);
return -1;
}
if (this.pedigree.getUnaffected().isEmpty()) {
LOG.error("No Unaffected sample in " + this.pedigreeFile);
return -1;
}
final DiseaseModel model = this.createModel();
final String inputName = super.oneFileOrNull(args);
final LineIterator r = (inputName == null ? IOUtils.openStreamForLineIterator(stdin()) : IOUtils.openURIForLineIterator(inputName));
final VCFCodec codec = new VCFCodec();
final VCFHeader header = (VCFHeader) codec.readActualHeader(r);
final AnnPredictionParser annParser = new AnnPredictionParserFactory(header).get();
final VepPredictionParser vepParser = new VepPredictionParserFactory(header).get();
// final VCFHeader h2=new VCFHeader(header.getMetaDataInInputOrder(),header.getSampleNamesInOrder());
// h2.addMetaDataLine(new VCFInfoHeaderLine(this.TAG,1,VCFHeaderLineType.String,"Values from bigwig file: "+BIGWIG));
SortingCollection<GeneAndVariant> sorting = null;
String prevContig = null;
for (; ; ) {
String line;
final VariantContext ctx;
if (r.hasNext()) {
line = r.next();
ctx = codec.decode(line);
} else {
line = null;
ctx = null;
}
if (ctx == null || !ctx.getContig().equals(prevContig)) {
if (sorting != null) {
LOG.debug("Dump contig " + prevContig);
sorting.doneAdding();
CloseableIterator<GeneAndVariant> iter2 = sorting.iterator();
EqualRangeIterator<GeneAndVariant> eqiter = new EqualRangeIterator<>(iter2, (A, B) -> A.gene.compareTo(B.gene));
while (eqiter.hasNext()) {
final List<GeneAndVariant> variants = eqiter.next();
model.scan(variants.get(0).gene, variants.stream().map(L -> codec.decode(L.ctxLine)).collect(Collectors.toList()), out);
}
eqiter.close();
iter2.close();
sorting.cleanup();
}
sorting = null;
if (ctx == null)
break;
prevContig = ctx.getContig();
}
if (!ctx.isVariant())
continue;
if (!acceptFiltered && ctx.isFiltered())
continue;
if (!acceptID && ctx.hasID())
continue;
if (!model.accept(ctx))
continue;
final Set<String> geneKeys = new HashSet<>();
for (final AnnPredictionParser.AnnPrediction pred : annParser.getPredictions(ctx)) {
geneKeys.addAll(pred.getGeneKeys().stream().map(S -> ctx.getContig() + "_" + S).collect(Collectors.toSet()));
}
for (final VepPredictionParser.VepPrediction pred : vepParser.getPredictions(ctx)) {
geneKeys.addAll(pred.getGeneKeys().stream().map(S -> ctx.getContig() + "_" + S).collect(Collectors.toSet()));
}
if (sorting == null) {
sorting = SortingCollection.newInstance(GeneAndVariant.class, new GeneAndVariantCodec(), (A, B) -> {
int i = A.gene.compareTo(B.gene);
if (i != 0)
return i;
return A.ctxLine.compareTo(B.ctxLine);
}, this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
sorting.setDestructiveIteration(true);
}
for (final String gk : geneKeys) {
final GeneAndVariant gav = new GeneAndVariant();
gav.gene = gk;
gav.ctxLine = line;
sorting.add(gav);
}
}
out.flush();
out.close();
out = null;
return 0;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(out);
}
}
use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.
the class SortVcfOnInfo method doWork.
@Override
public int doWork(final List<String> args) {
CloseableIterator<VcfLine> iter = null;
VariantContextWriter w = null;
SortingCollection<VcfLine> sorted = null;
LineIterator r = null;
try {
if (args.isEmpty()) {
LOG.info("reading from stdin");
r = IOUtils.openStreamForLineIterator(stdin());
} else if (args.size() == 1) {
String filename = args.get(0);
LOG.info("Reading " + filename);
r = IOUtils.openURIForLineIterator(filename);
} else {
LOG.error("Illegal number of arguments.");
return -1;
}
final VCFUtils.CodecAndHeader ch = VCFUtils.parseHeader(r);
VCFHeader header = ch.header;
this.codec = ch.codec;
this.infoDecl = header.getInfoHeaderLine(this.infoField);
if (this.infoDecl == null) {
final StringBuilder msg = new StringBuilder("VCF doesn't contain the INFO field :" + infoField + ". Available:");
for (VCFInfoHeaderLine vil : header.getInfoHeaderLines()) msg.append(" ").append(vil.getID());
LOG.error(msg.toString());
return -1;
}
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkVersion", HtsjdkVersion.getVersion()));
header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkHome", HtsjdkVersion.getHome()));
w = super.openVariantContextWriter(this.outputFile);
w.writeHeader(header);
sorted = SortingCollection.newInstance(VcfLine.class, new VariantCodec(), (V1, V2) -> V1.compareTo(V2), this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
sorted.setDestructiveIteration(true);
while (r.hasNext()) {
final VcfLine vc = new VcfLine(r.next());
progress.watch(vc.getContext());
sorted.add(vc);
}
CloserUtil.close(r);
r = null;
sorted.doneAdding();
progress.finish();
LOG.info("now writing...");
iter = sorted.iterator();
while (iter.hasNext()) {
w.add(iter.next().getContext());
}
iter.close();
iter = null;
w.close();
w = null;
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(r);
CloserUtil.close(iter);
try {
if (sorted != null)
sorted.cleanup();
} catch (Exception err) {
}
CloserUtil.close(w);
}
}
Aggregations