Search in sources :

Example 1 with RExporter

use of com.github.lindenb.jvarkit.chart.RExporter in project jvarkit by lindenb.

the class BamStatsJfx method save.

private void save() throws IOException {
    try (final PrintWriter pw = super.openFileOrStdoutAsPrintWriter(this.outputFile)) {
        final RExporter exporter = new RExporter();
        for (final ChartGenerator cg : this.chartGenerators) {
            LOG.info("saving " + cg.getChartTitle());
            if (!cg.isEnabled())
                continue;
            final Chart chart = cg.makeChart();
            if (chart == null)
                continue;
            exporter.exportToR(pw, chart);
        }
        pw.flush();
    }
}
Also used : RExporter(com.github.lindenb.jvarkit.chart.RExporter) LineChart(com.github.lindenb.jvarkit.chart.LineChart) XYChart(com.github.lindenb.jvarkit.chart.XYChart) BarChart(com.github.lindenb.jvarkit.chart.BarChart) Chart(com.github.lindenb.jvarkit.chart.Chart) StackedBarChart(com.github.lindenb.jvarkit.chart.StackedBarChart) PrintWriter(java.io.PrintWriter)

Example 2 with RExporter

use of com.github.lindenb.jvarkit.chart.RExporter in project jvarkit by lindenb.

the class VcfStatsJfx method save.

private void save() throws IOException {
    try (final PrintWriter pw = super.openFileOrStdoutAsPrintWriter(this.outputFile)) {
        final RExporter exporter = new RExporter();
        for (final ChartGenerator cg : this.chartGenerators) {
            LOG.info("saving " + cg.getChartTitle());
            if (!cg.isEnabled())
                continue;
            final Chart chart = cg.makeChart();
            if (chart == null)
                continue;
            exporter.exportToR(pw, chart);
        }
        pw.flush();
    }
}
Also used : RExporter(com.github.lindenb.jvarkit.chart.RExporter) XYChart(com.github.lindenb.jvarkit.chart.XYChart) BarChart(com.github.lindenb.jvarkit.chart.BarChart) Chart(com.github.lindenb.jvarkit.chart.Chart) StackedBarChart(com.github.lindenb.jvarkit.chart.StackedBarChart) PrintWriter(java.io.PrintWriter)

Example 3 with RExporter

use of com.github.lindenb.jvarkit.chart.RExporter in project jvarkit by lindenb.

the class CaseControlJfx method doWork.

@Override
public int doWork(final List<String> args) {
    final VariantPartition partition;
    Pedigree pedigree = null;
    VCFIterator in = null;
    try {
        switch(this.partitionType) {
            case variantType:
                partition = new VariantTypePartition();
                break;
            case chromosome:
                partition = new ChromosomePartition();
                break;
            case autosomes:
                partition = new SexualContigPartition();
                break;
            case qual:
                partition = new QualPartition();
                break;
            case vqslod:
                partition = new VQSLODPartition();
                break;
            case typeFilter:
                partition = new TypeAndFilterPartiton();
                break;
            case distance:
                partition = new DisanceToDiagonalPartiton();
                break;
            case n_alts:
                partition = new NAltsPartition();
                break;
            default:
                throw new IllegalStateException(this.partitionType.name());
        }
        in = openVCFIterator(oneFileOrNull(args));
        if (this.pedigreeFile != null) {
            pedigree = Pedigree.newParser().parse(this.pedigreeFile);
        } else {
            pedigree = Pedigree.newParser().parse(in.getHeader());
        }
        final AFExtractor controlAFExtractor;
        if (!StringUtil.isBlank(this.controlFields)) {
            final List<AFExtractor> extractors = new AFExtractorFactory().parseFieldExtractors(this.controlFields);
            if (extractors.size() != 1) {
                LOG.error("extractor list should have size==1 . got " + extractors);
                return -1;
            }
            controlAFExtractor = extractors.get(0);
            if (!controlAFExtractor.validateHeader(in.getHeader())) {
                LOG.error("Invalid : " + controlAFExtractor);
                return -1;
            }
        } else {
            controlAFExtractor = null;
        }
        int count = 0;
        final ProgressFactory.Watcher<VariantContext> progress = ProgressFactory.newInstance().dictionary(in.getHeader()).logger(LOG).build();
        while (in.hasNext() && (this.limit_to_N_variants < 0 || count < this.limit_to_N_variants)) {
            final VariantContext ctx = progress.apply(in.next());
            if (this.ignore_ctx_filtered && ctx.isFiltered())
                continue;
            ++count;
            final List<Allele> alternates = ctx.getAlternateAlleles();
            for (int alt_idx = 0; alt_idx < alternates.size(); ++alt_idx) {
                final Allele alt = alternates.get(alt_idx);
                final Double[] mafs = { null, null };
                for (int i = 0; i < 2; ++i) {
                    if (i == 1 && controlAFExtractor != null) {
                        final List<Double> dvals = controlAFExtractor.parse(ctx);
                        if (alt_idx < dvals.size() && dvals.get(alt_idx) != null) {
                            final double d = dvals.get(alt_idx);
                            if (!Double.isNaN(d) && d >= 0 && d <= 1.0)
                                mafs[1] = d;
                        }
                    } else {
                        final MafCalculator mafCalculator = new MafCalculator(alt, ctx.getContig());
                        mafCalculator.setNoCallIsHomRef(no_call_is_homref);
                        for (Pedigree.Person person : (i == 0 ? pedigree.getAffected() : pedigree.getUnaffected())) {
                            if (selectSamples.equals(SelectSamples.males) && !person.isMale())
                                continue;
                            if (selectSamples.equals(SelectSamples.females) && !person.isFemale())
                                continue;
                            final Genotype genotype = ctx.getGenotype(person.getId());
                            if (genotype == null)
                                continue;
                            if (ignore_gt_filtered && genotype.isFiltered())
                                continue;
                            mafCalculator.add(genotype, person.isMale());
                        }
                        if (!mafCalculator.isEmpty()) {
                            mafs[i] = mafCalculator.getMaf();
                        }
                    }
                }
                if (mafs[0] == null || mafs[1] == null)
                    continue;
                final XYChart.Data<Number, Number> data = new XYChart.Data<Number, Number>(mafs[0], mafs[1]);
                partition.add(ctx, pedigree, data);
            }
        }
        progress.close();
        in.close();
        in = null;
        final NumberAxis xAxis = new NumberAxis(0.0, 1.0, 0.1);
        xAxis.setLabel("Cases");
        final NumberAxis yAxis = new NumberAxis(0.0, 1.0, 0.1);
        yAxis.setLabel("Controls" + (StringUtil.isBlank(this.controlFields) ? "" : "[" + this.controlFields + "]"));
        final ScatterChart<Number, Number> chart = new ScatterChart<>(xAxis, yAxis);
        for (final XYChart.Series<Number, Number> series : partition.getSeries()) {
            chart.getData().add(series);
        }
        String title = "Case/Control";
        if (!args.isEmpty()) {
            title = args.get(0);
            int slash = title.lastIndexOf("/");
            if (slash != -1)
                title = title.substring(slash + 1);
            if (title.endsWith(".vcf.gz"))
                title = title.substring(0, title.length() - 7);
            if (title.endsWith(".vcf"))
                title = title.substring(0, title.length() - 4);
        }
        if (userTitle != null)
            title = userTitle;
        chart.setTitle(title);
        chart.setAnimated(false);
        // chart.setLegendSide(this.legendSide);
        final RExporter rExporter = new RExporter();
        final PrintWriter pw = super.openFileOrStdoutAsPrintWriter(this.outputFile);
        rExporter.exportToR(pw, chart);
        pw.flush();
        pw.close();
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(in);
    }
    return 0;
}
Also used : NumberAxis(com.github.lindenb.jvarkit.chart.NumberAxis) ProgressFactory(com.github.lindenb.jvarkit.util.log.ProgressFactory) ScatterChart(com.github.lindenb.jvarkit.chart.ScatterChart) VariantContext(htsjdk.variant.variantcontext.VariantContext) AFExtractorFactory(com.github.lindenb.jvarkit.util.vcf.AFExtractorFactory) VCFIterator(htsjdk.variant.vcf.VCFIterator) PrintWriter(java.io.PrintWriter) Genotype(htsjdk.variant.variantcontext.Genotype) RExporter(com.github.lindenb.jvarkit.chart.RExporter) Allele(htsjdk.variant.variantcontext.Allele) Pedigree(com.github.lindenb.jvarkit.util.Pedigree) AFExtractor(com.github.lindenb.jvarkit.util.vcf.AFExtractorFactory.AFExtractor) XYChart(com.github.lindenb.jvarkit.chart.XYChart)

Example 4 with RExporter

use of com.github.lindenb.jvarkit.chart.RExporter in project jvarkit by lindenb.

the class SimplePlot method doWork.

@Override
public int doWork(final List<String> args) {
    Chart chartNode = null;
    try {
        final BufferedReader br = super.openBufferedReader(oneFileOrNull(args));
        this.lineSupplier = () -> {
            try {
                for (; ; ) {
                    final String L = br.readLine();
                    if (L == null)
                        return null;
                    return L;
                }
            } catch (final IOException err) {
                throw new RuntimeIOException(err);
            }
        };
        switch(this.chartType) {
            case BEDGRAPH:
                {
                    chartNode = new BedGraphSupplier().get();
                    break;
                }
            case PIE:
                chartNode = new PieChartSupplier().get();
                break;
            case SIMPLE_HISTOGRAM:
                chartNode = new SimpleHistogramSupplier().get();
                break;
            case HISTOGRAM:
                chartNode = new HistogramSupplier().get();
                break;
            case STACKED_HISTOGRAM:
                chartNode = new StackedHistogramSupplier().get();
                break;
            case STACKED_HISTOGRAM_PIVOTED:
                chartNode = new StackedHistogramPivoted().get();
                break;
            case XYV:
            case STACKED_XYV:
                chartNode = new XYVHistogramSupplier().setStacked(this.chartType == PlotType.STACKED_XYV).get();
                break;
            case HEATMAP:
                chartNode = new HeatmapSupplier().get();
                break;
            default:
                {
                    LOG.error("Bad chart type : " + this.chartType);
                    return -1;
                }
        }
        CloserUtil.close(br);
        if (chartNode == null) {
            LOG.error("No chart was generated");
            return -1;
        }
        if (StringUtil.isBlank(this.chartTitle)) {
            chartNode.setLegendVisible(false);
        } else {
            // chart.setTitleSide(this.titleSide);
            chartNode.setTitle(this.chartTitle);
        }
        // chart.setLegendSide(this.legendSide);
        chartNode.setLegendVisible(!this.hide_legend);
        try (PrintWriter pw = super.openFileOrStdoutAsPrintWriter(this.outputFile)) {
            final RExporter exporter = new RExporter();
            exporter.exportToR(pw, chartNode);
            pw.flush();
        }
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
    }
    return 0;
}
Also used : RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException) RExporter(com.github.lindenb.jvarkit.chart.RExporter) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) BufferedReader(java.io.BufferedReader) HeatMapChart(com.github.lindenb.jvarkit.chart.HeatMapChart) ScatterChart(com.github.lindenb.jvarkit.chart.ScatterChart) BubbleChart(com.github.lindenb.jvarkit.chart.BubbleChart) XYChart(com.github.lindenb.jvarkit.chart.XYChart) BarChart(com.github.lindenb.jvarkit.chart.BarChart) PieChart(com.github.lindenb.jvarkit.chart.PieChart) Chart(com.github.lindenb.jvarkit.chart.Chart) StackedBarChart(com.github.lindenb.jvarkit.chart.StackedBarChart) PrintWriter(java.io.PrintWriter)

Aggregations

RExporter (com.github.lindenb.jvarkit.chart.RExporter)4 XYChart (com.github.lindenb.jvarkit.chart.XYChart)4 PrintWriter (java.io.PrintWriter)4 BarChart (com.github.lindenb.jvarkit.chart.BarChart)3 Chart (com.github.lindenb.jvarkit.chart.Chart)3 StackedBarChart (com.github.lindenb.jvarkit.chart.StackedBarChart)3 ScatterChart (com.github.lindenb.jvarkit.chart.ScatterChart)2 BubbleChart (com.github.lindenb.jvarkit.chart.BubbleChart)1 HeatMapChart (com.github.lindenb.jvarkit.chart.HeatMapChart)1 LineChart (com.github.lindenb.jvarkit.chart.LineChart)1 NumberAxis (com.github.lindenb.jvarkit.chart.NumberAxis)1 PieChart (com.github.lindenb.jvarkit.chart.PieChart)1 JvarkitException (com.github.lindenb.jvarkit.lang.JvarkitException)1 Pedigree (com.github.lindenb.jvarkit.util.Pedigree)1 ProgressFactory (com.github.lindenb.jvarkit.util.log.ProgressFactory)1 AFExtractorFactory (com.github.lindenb.jvarkit.util.vcf.AFExtractorFactory)1 AFExtractor (com.github.lindenb.jvarkit.util.vcf.AFExtractorFactory.AFExtractor)1 RuntimeIOException (htsjdk.samtools.util.RuntimeIOException)1 Allele (htsjdk.variant.variantcontext.Allele)1 Genotype (htsjdk.variant.variantcontext.Genotype)1