use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.
the class VCFComparePredictions method doWork.
@Override
public int doWork(List<String> args) {
PrintWriter out = null;
SortingCollection<LineAndFile> variants = null;
try {
if (args.isEmpty()) {
LOG.error("Illegal number of arguments");
return -1;
}
out = super.openFileOrStdoutAsPrintWriter(super.outputFile);
variants = SortingCollection.newInstance(LineAndFile.class, new AbstractVCFCompareBase.LineAndFileCodec(), new AbstractVCFCompareBase.LineAndFileComparator(), super.sortingCollectionArgs.getMaxRecordsInRam(), super.sortingCollectionArgs.getTmpPaths());
variants.setDestructiveIteration(true);
for (final String filename : args) {
LOG.info("Reading from " + filename);
Input input = super.put(variants, filename);
LOG.info("end reading " + input.filename);
}
List<PredictionTuple> predictionTuples = new ArrayList<PredictionTuple>(super.inputs.size());
for (AbstractVCFCompareBase.Input input : this.inputs) {
PredictionTuple predictionTuple = new PredictionTuple();
predictionTuple.snpEffPredictionParser = new SnpEffPredictionParserFactory(input.codecAndHeader.header).get();
predictionTuple.vepPredictionParser = new VepPredictionParserFactory(input.codecAndHeader.header).get();
predictionTuples.add(predictionTuple);
}
List<AbstractVCFCompareBase.LineAndFile> row = new ArrayList<LineAndFile>(super.inputs.size());
CloseableIterator<LineAndFile> iter = variants.iterator();
final Comparator<LineAndFile> posCompare = (A, B) -> A.getContigPosRef().compareTo(B.getContigPosRef());
for (; ; ) {
LineAndFile rec = null;
if (iter.hasNext()) {
rec = iter.next();
}
if (rec == null || (!row.isEmpty() && posCompare.compare(row.get(0), rec) != 0)) {
if (!row.isEmpty()) {
boolean printed = false;
VariantContext ctx = row.get(0).getContext();
if (row.size() != this.inputs.size()) {
startLine(out, ctx);
out.println("\tDiscordant number of variants");
printed = true;
}
for (int i = 0; i + 1 < row.size(); ++i) {
Input input1 = this.inputs.get(row.get(i).fileIdx);
VariantContext ctx1 = row.get(i).getContext();
PredictionTuple predtuple1 = predictionTuples.get(row.get(i).fileIdx);
List<VepPrediction> vepPredictions1 = predtuple1.vepPredictionParser.getPredictions(ctx1);
List<SnpEffPrediction> snpEffPredictions1 = predtuple1.snpEffPredictionParser.getPredictions(ctx1);
Set<SequenceOntologyTree.Term> so_vep_1 = getVepSoTerms(predtuple1.vepPredictionParser, ctx1);
Set<SequenceOntologyTree.Term> so_snpeff_1 = getSnpEffSoTerms(predtuple1.snpEffPredictionParser, ctx1);
for (int j = i + 1; j < row.size(); ++j) {
Input input2 = this.inputs.get(row.get(j).fileIdx);
VariantContext ctx2 = row.get(j).getContext();
PredictionTuple predtuple2 = predictionTuples.get(row.get(j).fileIdx);
List<VepPrediction> vepPredictions2 = predtuple2.vepPredictionParser.getPredictions(ctx2);
List<SnpEffPrediction> snpEffPredictions2 = predtuple2.snpEffPredictionParser.getPredictions(ctx2);
Set<SequenceOntologyTree.Term> so_vep_2 = getVepSoTerms(predtuple2.vepPredictionParser, ctx2);
Set<SequenceOntologyTree.Term> so_snpeff_2 = getSnpEffSoTerms(predtuple2.snpEffPredictionParser, ctx2);
if (vepPredictions1.size() != vepPredictions2.size()) {
startLine(out, ctx);
out.print("\tVEP discordant transcripts count");
out.print("\t" + input1.filename + ":" + vepPredictions1.size());
out.print("\t" + input2.filename + ":" + vepPredictions2.size());
out.println();
printed = true;
}
if (snpEffPredictions1.size() != snpEffPredictions2.size()) {
startLine(out, ctx);
out.print("\tSNPEFF discordant transcripts count");
out.print("\t" + input1.filename + ":" + snpEffPredictions1.size());
out.print("\t" + input2.filename + ":" + snpEffPredictions2.size());
out.println();
printed = true;
}
if (!unshared(so_vep_1, so_vep_2).isEmpty()) {
startLine(out, ctx);
out.print("\tVEP discordant SO:terms");
printDiscordantSO(out, input1, so_vep_1, input2, so_vep_2);
printed = true;
}
if (!unshared(so_snpeff_1, so_snpeff_2).isEmpty()) {
startLine(out, ctx);
out.print("\tSNPEFF discordant SO:terms");
printDiscordantSO(out, input1, so_snpeff_1, input2, so_snpeff_2);
printed = true;
}
}
}
if (!printed) {
startLine(out, ctx);
out.println("\tPASS");
}
row.clear();
}
if (rec == null)
break;
}
row.add(rec);
}
iter.close();
out.flush();
out.close();
out = null;
return 0;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(out);
try {
if (variants != null)
variants.cleanup();
} catch (Exception err) {
}
}
}
use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.
the class VcfBurdenFisherHTest method test01.
@Test(dataProvider = "src1", enabled = false)
public void test01(final String inputFile) throws IOException {
try {
final VCFReader r0 = VCFReaderFactory.makeDefault().open(new File(inputFile), false);
final VCFHeader vcfheader = r0.getHeader();
if (vcfheader.getNGenotypeSamples() < 2) {
r0.close();
return;
}
final CloseableIterator<VariantContext> iter = r0.iterator();
final Path inputVcf = support.createTmpPath(".vcf");
final VariantContextWriter w = VCFUtils.createVariantContextWriter(inputVcf.toFile());
w.writeHeader(vcfheader);
iter.stream().filter(V -> V.getAlleles().size() == 2).forEach(V -> w.add(V));
iter.close();
w.close();
r0.close();
final Path ped = support.createRandomPedigreeFromFile(inputFile);
if (ped == null) {
Reporter.getCurrentTestResult().setAttribute("warn", "No Pedigree for " + inputFile);
return;
}
final Path output = support.createTmpPath(".vcf");
Assert.assertEquals(new VcfBurdenFisherH().instanceMain(new String[] { "-o", output.toString(), "--pedigree", ped.toString() }), 0);
support.assertIsVcf(output);
} finally {
support.removeTmpFiles();
}
}
use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.
the class VcfInTest method makeVcfIn.
private Path makeVcfIn(final String vcfpath, String other_args) throws Exception {
Path vcfDbIn = Paths.get(vcfpath);
Path tmpIn = support.createTmpPath(".vcf");
Path outVcf = support.createTmpPath(".vcf");
final VariantContextWriter w = VCFUtils.createVariantContextWriterToPath(tmpIn);
final VCFReader r = VCFReaderFactory.makeDefault().open(vcfDbIn, true);
w.writeHeader(r.getHeader());
int n = 0;
final CloseableIterator<VariantContext> iter = r.iterator();
while (iter.hasNext()) {
VariantContext ctx = iter.next();
// ignore some variants
if (n++ % 2 == 0)
continue;
w.add(ctx);
// add it twice
w.add(ctx);
VariantContextBuilder vcb = new VariantContextBuilder(vcfpath, ctx.getContig(), ctx.getStart(), ctx.getEnd(), Arrays.asList(ctx.getReference(), ZORG_ALLELE));
vcb.genotypes(ctx.getGenotypes().stream().map(G -> new GenotypeBuilder(G.getSampleName(), Arrays.asList(ZORG_ALLELE, ZORG_ALLELE)).make()).collect(Collectors.toList()));
w.add(vcb.make());
}
w.close();
iter.close();
r.close();
final List<String> args = new ArrayList<>();
args.add("-o");
args.add(outVcf.toString());
args.add("--database");
args.add(vcfDbIn.toString());
Arrays.stream(other_args.split("[ ]")).filter(S -> !S.isEmpty()).forEach(S -> args.add(S));
args.add(vcfpath.toString());
Assert.assertEquals(new VcfIn().instanceMain(args), 0);
support.assertIsVcf(outVcf);
return outVcf;
}
use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.
the class MultiBamLauncher method doWork.
@Override
public int doWork(final List<String> args0) {
final Map<SamReader, CloseableIterator<SAMRecord>> sam2iterator = new HashMap<>();
final CloseableIterator<SAMRecord> mainIterator;
final SAMFileHeader mainHeader;
final List<String> inputs = new ArrayList<>(args0.size());
/* parse input */
try {
// unroll list
if (args0.size() == 1 && args0.get(0).endsWith(".list")) {
final Path p1 = Paths.get(args0.get(0));
IOUtil.assertFileIsReadable(p1);
inputs.addAll(Files.lines(p1).filter(S -> !S.startsWith("#")).filter(S -> !StringUtils.isBlank(S)).collect(Collectors.toList()));
if (inputs.isEmpty()) {
LOG.error("List is empty " + p1);
return -1;
}
} else {
inputs.addAll(args0);
}
} catch (final IOException err) {
LOG.error(err);
return -1;
}
/* check input is not output */
if (validateInputsPath(inputs) != 0) {
LOG.info("input validation failed");
return -1;
}
/* before SAM */
try {
if (beforeSam() != 0) {
LOG.error("initialization failed");
return -1;
}
} catch (final Throwable err) {
LOG.error(err);
return -1;
}
try {
final SamReaderFactory srf = createSamReaderFactory();
if (inputs.isEmpty() || inputs.size() == 1) {
final SamReader in;
if (inputs.isEmpty()) {
in = srf.open(SamInputResource.of(stdin()));
} else if (IOUtil.isUrl(inputs.get(0))) {
in = srf.open(SamInputResource.of(new URL(inputs.get(0))));
} else {
in = srf.open(Paths.get(inputs.get(0)));
}
final CloseableIterator<SAMRecord> iter = openSamIterator(in);
sam2iterator.put(in, iter);
mainHeader = in.getFileHeader();
mainIterator = iter;
} else {
for (final String bamPath : inputs) {
final SamReader in = srf.open(Paths.get(bamPath));
final CloseableIterator<SAMRecord> iter = openSamIterator(in);
sam2iterator.put(in, iter);
}
final Set<SAMFileHeader.SortOrder> all_sort_orders = sam2iterator.keySet().stream().map(SR -> SR.getFileHeader()).map(H -> H.getSortOrder()).collect(Collectors.toSet());
if (all_sort_orders.size() != 1) {
LOG.error("Heterogenous sort order in input bams : " + all_sort_orders);
return -1;
}
final SAMFileHeader.SortOrder sortOrder = all_sort_orders.iterator().next();
final SamFileHeaderMerger headerMerger = new SamFileHeaderMerger(sortOrder, sam2iterator.keySet().stream().map(SR -> SR.getFileHeader()).collect(Collectors.toList()), false);
mainHeader = headerMerger.getMergedHeader();
@SuppressWarnings("resource") final MergingSamRecordIterator iter = new MergingSamRecordIterator(headerMerger, sam2iterator, false);
mainIterator = iter;
}
if (this.faidxPath != null) {
final SAMSequenceDictionary dict = SequenceDictionaryUtils.extractRequired(this.faidxPath);
SequenceUtil.assertSequenceDictionariesEqual(dict, SequenceDictionaryUtils.extractRequired(mainHeader));
}
final int err = processInput(mainHeader, mainIterator);
mainIterator.close();
for (final SamReader sr : sam2iterator.keySet()) {
sam2iterator.get(sr).close();
sr.close();
}
sam2iterator.clear();
if (err != 0)
deleteOutputOnError();
return err;
} catch (final Throwable err) {
LOG.error(err);
deleteOutputOnError();
return -1;
} finally {
try {
afterSam();
} catch (final Throwable err) {
LOG.error(err);
}
for (final SamReader sr : sam2iterator.keySet()) {
CloserUtil.close(sam2iterator.get(sr));
CloserUtil.close(sr);
}
}
}
use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.
the class MultiBamLauncher method openSamIterator.
/**
* create input SAMRecord iterator for one sample
*/
protected CloseableIterator<SAMRecord> openSamIterator(final SamReader sr) {
if (this.regionFiles != null) {
this.regionFiles.dictionary(SequenceDictionaryUtils.extractRequired(sr.getFileHeader()));
if (!sr.hasIndex()) {
final List<Interval> L = this.regionFiles.stream().map(x -> new Interval(x)).collect(Collectors.toList());
final SAMRecordIterator st0 = sr.iterator();
return new FilteringSamIterator(st0, new IntervalFilter(L, sr.getFileHeader()));
} else {
final QueryInterval[] optimized = this.regionFiles.optimizedQueryIntervals();
return sr.query(optimized, false);
}
}
return sr.iterator();
}
Aggregations