Search in sources :

Example 31 with VcfIterator

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;
    }
}
Also used : FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Example 32 with VcfIterator

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);
    }
}
Also used : Pattern(java.util.regex.Pattern) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) Date(java.util.Date) VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) BufferedReader(java.io.BufferedReader) KnownGene(com.github.lindenb.jvarkit.util.ucsc.KnownGene)

Example 33 with VcfIterator

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);
    }
}
Also used : VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 34 with VcfIterator

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);
    }
}
Also used : VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) IOException(java.io.IOException)

Example 35 with VcfIterator

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);
    }
}
Also used : VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) Allele(htsjdk.variant.variantcontext.Allele) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Program(com.github.lindenb.jvarkit.util.jcommander.Program) Parameter(com.beust.jcommander.Parameter) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) TabixVcfFileReader(com.github.lindenb.jvarkit.util.vcf.TabixVcfFileReader) CloserUtil(htsjdk.samtools.util.CloserUtil) VCFFilterHeaderLine(htsjdk.variant.vcf.VCFFilterHeaderLine) Iterator(java.util.Iterator) Logger(com.github.lindenb.jvarkit.util.log.Logger) VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) Set(java.util.Set) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) File(java.io.File) List(java.util.List) EqualRangeIterator(com.github.lindenb.jvarkit.util.iterator.EqualRangeIterator) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VariantContext(htsjdk.variant.variantcontext.VariantContext) Comparator(java.util.Comparator) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) VCFHeader(htsjdk.variant.vcf.VCFHeader)

Aggregations

VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)55 VariantContext (htsjdk.variant.variantcontext.VariantContext)39 VCFHeader (htsjdk.variant.vcf.VCFHeader)35 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)30 ArrayList (java.util.ArrayList)28 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)26 IOException (java.io.IOException)24 File (java.io.File)22 HashSet (java.util.HashSet)19 List (java.util.List)19 Genotype (htsjdk.variant.variantcontext.Genotype)18 Parameter (com.beust.jcommander.Parameter)17 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)17 Program (com.github.lindenb.jvarkit.util.jcommander.Program)17 Logger (com.github.lindenb.jvarkit.util.log.Logger)17 Set (java.util.Set)17 Allele (htsjdk.variant.variantcontext.Allele)16 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)15 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)15 JvarkitException (com.github.lindenb.jvarkit.lang.JvarkitException)14