Search in sources :

Example 1 with BcfToolsPredictionParser

use of com.github.lindenb.jvarkit.util.vcf.predictions.BcfToolsPredictionParser in project jvarkit by lindenb.

the class VcfBurdenFilterGenes method doVcfToVcf.

@Override
protected int doVcfToVcf(final String inputName, final VCFIterator in, final VariantContextWriter out) {
    final VCFHeader header = in.getHeader();
    try {
        final VCFHeader h2 = addMetaData(new VCFHeader(header));
        final VCFFilterHeaderLine filterControlsHeader;
        if (!StringUtil.isBlank(this.filterTag)) {
            filterControlsHeader = new VCFFilterHeaderLine(this.filterTag.trim(), "Genes not in list " + this.geneFile);
            h2.addMetaDataLine(filterControlsHeader);
        } else {
            filterControlsHeader = null;
        }
        final List<String> lookColumns = Arrays.asList("CCDS", "Feature", "ENSP", "Gene", "HGNC", "HGNC_ID", "SYMBOL", "RefSeq");
        final VepPredictionParser vepParser = new VepPredictionParserFactory(header).get();
        final AnnPredictionParser annParser = new AnnPredictionParserFactory(header).get();
        final BcfToolsPredictionParser bcftoolsParser = new BcfToolsPredictionParserFactory(header).get();
        JVarkitVersion.getInstance().addMetaData(this, h2);
        out.writeHeader(h2);
        while (in.hasNext()) {
            final VariantContext ctx = in.next();
            boolean keep = false;
            final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
            // not just set FILTER ?
            if (filterControlsHeader == null) {
                vcb.rmAttribute(vepParser.getTag());
                vcb.rmAttribute(annParser.getTag());
            }
            final List<String> newVepList = new ArrayList<>();
            for (final String predStr : ctx.getAttributeAsStringList(vepParser.getTag(), "")) {
                final VepPredictionParser.VepPrediction pred = vepParser.parseOnePrediction(ctx, predStr);
                for (final String col : lookColumns) {
                    final String token = pred.getByCol(col);
                    if (!StringUtil.isBlank(token) && this.geneNames.contains(token)) {
                        newVepList.add(predStr);
                        keep = true;
                        // break lookColumns
                        break;
                    }
                }
            }
            final List<String> newEffList = new ArrayList<>();
            for (final String predStr : ctx.getAttributeAsStringList(annParser.getTag(), "")) {
                final AnnPredictionParser.AnnPrediction pred = annParser.parseOnePrediction(predStr);
                String token = pred.getGeneName();
                if (!StringUtil.isBlank(token) && this.geneNames.contains(token)) {
                    newEffList.add(predStr);
                    keep = true;
                    continue;
                }
                token = pred.getGeneId();
                if (!StringUtil.isBlank(token) && this.geneNames.contains(token)) {
                    newEffList.add(predStr);
                    keep = true;
                    continue;
                }
                token = pred.getFeatureId();
                if (!StringUtil.isBlank(token) && this.geneNames.contains(token)) {
                    newEffList.add(predStr);
                    keep = true;
                    continue;
                }
            }
            final List<String> newBcfList = new ArrayList<>();
            for (final String predStr : ctx.getAttributeAsStringList(bcftoolsParser.getTag(), "")) {
                final BcfToolsPredictionParser.BcfToolsPrediction pred = bcftoolsParser.parseOnePrediction(ctx, predStr);
                String token = pred.getGeneName();
                if (!StringUtil.isBlank(token) && this.geneNames.contains(token)) {
                    newBcfList.add(predStr);
                    keep = true;
                    continue;
                }
                token = pred.getTranscript();
                if (!StringUtil.isBlank(token) && this.geneNames.contains(token)) {
                    newBcfList.add(predStr);
                    keep = true;
                    continue;
                }
            }
            // not just set FILTER ?
            if (filterControlsHeader == null) {
                if (!newVepList.isEmpty())
                    vcb.attribute(vepParser.getTag(), newVepList);
                if (!newEffList.isEmpty())
                    vcb.attribute(annParser.getTag(), newEffList);
                if (!newBcfList.isEmpty())
                    vcb.attribute(bcftoolsParser.getTag(), newBcfList);
            }
            if (filterControlsHeader != null) {
                if (!keep) {
                    vcb.filter(filterControlsHeader.getID());
                } else if (!ctx.isFiltered()) {
                    vcb.passFilters();
                }
                out.add(vcb.make());
            } else {
                if (keep)
                    out.add(vcb.make());
            }
        }
        return 0;
    } catch (final Throwable err) {
        LOG.error(err);
        return -1;
    }
}
Also used : AnnPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParser) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) BcfToolsPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.BcfToolsPredictionParser) VepPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParser) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) BcfToolsPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.BcfToolsPredictionParserFactory) AnnPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParserFactory) VCFFilterHeaderLine(htsjdk.variant.vcf.VCFFilterHeaderLine) VCFHeader(htsjdk.variant.vcf.VCFHeader) VepPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParserFactory)

Aggregations

AnnPredictionParser (com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParser)1 AnnPredictionParserFactory (com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParserFactory)1 BcfToolsPredictionParser (com.github.lindenb.jvarkit.util.vcf.predictions.BcfToolsPredictionParser)1 BcfToolsPredictionParserFactory (com.github.lindenb.jvarkit.util.vcf.predictions.BcfToolsPredictionParserFactory)1 VepPredictionParser (com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParser)1 VepPredictionParserFactory (com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParserFactory)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)1 VCFFilterHeaderLine (htsjdk.variant.vcf.VCFFilterHeaderLine)1 VCFHeader (htsjdk.variant.vcf.VCFHeader)1 ArrayList (java.util.ArrayList)1