use of com.github.lindenb.jvarkit.util.vcf.VcfIterator in project jvarkit by lindenb.
the class VcfSpringFilter method doWork.
@Override
public int doWork(final List<String> args) {
VcfIterator vcfIn = null;
VariantContextWriter vcfOut = null;
try {
if (this.springCongigFiles.isEmpty()) {
LOG.error("no spring config file was provided");
return -1;
}
this.springApplicationContext = new FileSystemXmlApplicationContext(springCongigFiles.toArray(new String[springCongigFiles.size()]));
if (!this.springApplicationContext.containsBean(this.mainBeanName)) {
LOG.error("cannot get bean " + mainBeanName + " in " + this.springCongigFiles);
return -1;
}
final Object o = this.springApplicationContext.getBean(mainBeanName);
if (o == null || !(o instanceof VariantContextWriter)) {
LOG.error("bean " + mainBeanName + " is not a VcfChain but " + (o == null ? "null" : o.getClass().getName()));
return -1;
}
final VariantContextWriter writer = VariantContextWriter.class.cast(o);
final String inputFile = oneFileOrNull(args);
vcfIn = super.openVcfIterator(inputFile);
return ret;
} catch (final Throwable err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(vcfIn);
CloserUtil.close(vcfOut);
this.springApplicationContext = null;
}
}
use of com.github.lindenb.jvarkit.util.vcf.VcfIterator in project jvarkit by lindenb.
the class VcfToPostscript method doWork.
@Override
public int doWork(List<String> args) {
VcfIterator iter = null;
BufferedReader r = null;
try {
iter = super.openVcfIterator(oneFileOrNull(args));
this.outw = super.openFileOrStdoutAsPrintStream(this.outputFile);
final SAMSequenceDictionary dict = iter.getHeader().getSequenceDictionary();
LOG.info("Reading " + this.ucscKnownGene);
r = IOUtils.openURIForBufferedReading(this.ucscKnownGene);
int nKG = 0;
Pattern tab = Pattern.compile("[\t]");
String line;
while ((line = r.readLine()) != null) {
String[] tokens = tab.split(line);
KnownGene g = new KnownGene(tokens);
if (dict != null && dict.getSequence(g.getContig()) == null)
continue;
List<KnownGene> kg = this.chrom2knownGenes.get(g.getContig());
if (kg == null) {
kg = new ArrayList<KnownGene>();
this.chrom2knownGenes.put(g.getContig(), kg);
}
kg.add(g);
++nKG;
}
r.close();
LOG.info("Done Reading knownGenes. N=" + nKG);
final double ticksH = (fHeight / 2.0f) * 0.6f;
final double ticksx = 20;
this.outw.print("%!PS\n" + "%%Creator: Pierre Lindenbaum PhD plindenbaum@yahoo.fr http://plindenbaum.blogspot.com\n" + "%%Title: " + getClass().getSimpleName() + "\n" + "%%CreationDate: " + new Date() + "\n" + "%%EndComments\n" + "%%BoundingBox: 0 0 " + (this.pageDef.width + margin.left + margin.right) + " " + (this.pageDef.height + margin.top + margin.bottom) + "\n" + "/Courier findfont 9 scalefont setfont\n" + "/circle { 10 0 360 arc} bind def\n" + "/ticksF {\n" + (-ticksH) + " " + (ticksH) + " rmoveto\n" + ticksH + " " + (-ticksH) + " rlineto\n" + (-ticksH) + " " + (-ticksH) + " rlineto\n" + ticksH + " " + ticksH + " rmoveto\n" + "} bind def\n" + "/ticksR {\n" + (ticksH) + " " + (ticksH) + " rmoveto\n" + (-ticksH) + " " + (-ticksH) + " rlineto\n" + (ticksH) + " " + (-ticksH) + " rlineto\n" + (-ticksH) + " " + (ticksH) + " rmoveto\n" + "} bind def\n" + "/forticksF {2 dict begin\n" + "/x2 exch def\n" + "/x1 exch def\n" + "0 1 x2 x1 sub " + ticksx + " div {\n" + "ticksF " + ticksx + " 0 rmoveto\n" + "}for\n" + "} bind def\n" + "/forticksR {2 dict begin\n" + "/x2 exch def\n" + "/x1 exch def\n" + "0 1 x2 x1 sub " + ticksx + " div {\n" + " ticksR " + ticksx + " 0 rmoveto\n" + "}for\n" + "} bind def\n" + "/box\n" + "{\n" + "4 dict begin\n" + "/height exch def\n" + "/width exch def\n" + "/y exch def\n" + "/x exch def\n" + "x y moveto\n" + "width 0 rlineto\n" + "0 height rlineto\n" + "width -1 mul 0 rlineto\n" + "0 height -1 mul rlineto\n" + "end\n" + "} bind def\n" + "/gradient\n" + "{\n" + "4 dict begin\n" + "/height exch def\n" + "/width exch def\n" + "/y exch def\n" + "/x exch def\n" + "/i 0 def\n" + "height 2 div /i exch def\n" + "\n" + "0 1 height 2 div {\n" + " 1 i height 2.0 div div sub setgray\n" + " newpath\n" + " x \n" + " y height 2 div i sub add\n" + " width\n" + " i 2 mul\n" + " box\n" + " closepath\n" + " fill\n" + " i 1 sub /i exch def\n" + " }for\n" + "newpath\n" + "0 setgray\n" + "0.4 setlinewidth\n" + "x y width height box\n" + "closepath\n" + "stroke\n" + "end\n" + "} bind def\n");
run(iter);
iter.close();
this.outw.print("\n%%Trailer\n%%EOF\n");
this.outw.flush();
this.outw.close();
this.outw = null;
return 0;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(r);
CloserUtil.close(iter);
CloserUtil.close(this.outw);
}
}
use of com.github.lindenb.jvarkit.util.vcf.VcfIterator in project jvarkit by lindenb.
the class VcfToTable method doWork.
@Override
public int doWork(final List<String> args) {
VcfIterator in = null;
try {
in = super.openVcfIterator(oneFileOrNull(args));
viewer.writeHeader(in.getHeader());
while (!this.viewer.checkError() && in.hasNext()) {
viewer.add(in.next());
}
viewer.close();
viewer = null;
in.close();
in = null;
return 0;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(in);
CloserUtil.close(viewer);
}
}
use of com.github.lindenb.jvarkit.util.vcf.VcfIterator in project jvarkit by lindenb.
the class VcfIndexTabix method doWork.
@Override
public int doWork(List<String> args) {
if (outputFile == null) {
LOG.error(" output file undefined.");
return -1;
}
if (!outputFile.getName().endsWith(".vcf.gz")) {
LOG.error("output file should en with .vcf.gz but got " + outputFile);
return 1;
}
VcfIterator iter = null;
try {
final String inputName = oneFileOrNull(args);
iter = super.openVcfIterator(inputName);
return doVcfToVcf(inputName == null ? "STDIN" : inputName, iter, outputFile);
} catch (Exception e) {
LOG.error(e);
return -1;
} finally {
CloserUtil.close(iter);
}
}
use of com.github.lindenb.jvarkit.util.vcf.VcfIterator in project jvarkit by lindenb.
the class VcfIn method scanFileSorted.
private int scanFileSorted(final VariantContextWriter vcw, final String databaseVcfUri, final VcfIterator userVcfIn) {
EqualRangeVcfIterator equalRangeDbIter = null;
EqualRangeIterator<VariantContext> equalRangeUserVcf = null;
try {
final VCFHeader header = new VCFHeader(userVcfIn.getHeader());
final SAMSequenceDictionary userVcfDict = header.getSequenceDictionary();
// / NO need if(dict1==null)
if (userVcfDict == null) {
LOG.error(JvarkitException.VcfDictionaryMissing.getMessage("user file"));
return -1;
}
final Comparator<VariantContext> userVcfComparator = VCFUtils.createTidPosComparator(userVcfDict);
equalRangeDbIter = new EqualRangeVcfIterator(VCFUtils.createVcfIterator(databaseVcfUri), userVcfComparator);
this.addMetaData(header);
vcw.writeHeader(header);
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(userVcfDict).logger(LOG);
equalRangeUserVcf = new EqualRangeIterator<>(userVcfIn, userVcfComparator);
while (equalRangeUserVcf.hasNext()) {
final List<VariantContext> ctxList = equalRangeUserVcf.next();
progress.watch(ctxList.get(0));
// fill both contextes
final List<VariantContext> dbContexes = new ArrayList<VariantContext>(equalRangeDbIter.next(ctxList.get(0)));
for (final VariantContext userCtx : ctxList) {
boolean keep = dbContexes.stream().filter(V -> sameContext(userCtx, V)).anyMatch(V -> allUserAltFoundInDatabase(userCtx, V));
addVariant(vcw, userCtx, keep);
}
if (vcw.checkError())
break;
}
equalRangeUserVcf.close();
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(equalRangeDbIter);
CloserUtil.close(userVcfIn);
CloserUtil.close(vcw);
}
}
Aggregations