Search in sources :

Example 16 with RuntimeIOException

use of htsjdk.samtools.util.RuntimeIOException in project jvarkit by lindenb.

the class SimplePlot method doWork.

@Override
public int doWork(final Stage primaryStage, final List<String> args) {
    Chart chart = null;
    try {
        final BufferedReader br;
        if (args.isEmpty()) {
            // open stdin
            br = IOUtils.openStdinForBufferedReader();
        } else if (args.size() == 1) {
            br = IOUtils.openURIForBufferedReading(args.get(0));
        } else {
            LOG.error("Illegal Number of arguments: " + args);
            return -1;
        }
        this.lineSupplier = () -> {
            try {
                for (; ; ) {
                    String L = br.readLine();
                    if (L == null)
                        return null;
                    return L;
                }
            } catch (final IOException err) {
                throw new RuntimeIOException(err);
            }
        };
        switch(this.chartType) {
            case BEDGRAPH:
                {
                    chart = new BedGraphSupplier().get();
                    break;
                }
            case PIE:
                chart = new PieChartSupplier().get();
                break;
            case SIMPLE_HISTOGRAM:
                chart = new SimpleHistogramSupplier().get();
                break;
            case HISTOGRAM:
                chart = new HistogramSupplier().get();
                break;
            case STACKED_HISTOGRAM:
                chart = new StackedHistogramSupplier().get();
                break;
            case XYV:
            case STACKED_XYV:
                chart = new XYVHistogramSupplier().setStacked(this.chartType == PlotType.STACKED_XYV).get();
                break;
            default:
                {
                    LOG.error("Bad chart type : " + this.chartType);
                    return -1;
                }
        }
        CloserUtil.close(br);
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
    }
    if (chart == null) {
        LOG.error("No chart was generated");
        return -1;
    }
    if (StringUtil.isBlank(this.chartTitle)) {
        chart.setLegendVisible(false);
    } else {
        chart.setTitleSide(this.titleSide);
        chart.setTitle(this.chartTitle);
    }
    chart.setLegendSide(this.legendSide);
    chart.setLegendVisible(!this.hide_legend);
    if (this.outputFile != null) {
        chart.setAnimated(false);
        LOG.info("saving as " + this.outputFile + " and exiting.");
        final Chart theChart = chart;
        primaryStage.setOnShown(WE -> {
            try {
                saveImageAs(theChart, this.outputFile);
            } catch (final IOException err) {
                LOG.error(err);
                setExitStatus(-1);
            }
            Platform.exit();
        });
    }
    final Screen scr = Screen.getPrimary();
    Scene scene = new Scene(chart, scr.getBounds().getWidth() - 100, scr.getBounds().getHeight() - 100);
    primaryStage.setScene(scene);
    primaryStage.show();
    return 0;
}
Also used : RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) Screen(javafx.stage.Screen) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException) Scene(javafx.scene.Scene) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) XYChart(javafx.scene.chart.XYChart) BarChart(javafx.scene.chart.BarChart) Chart(javafx.scene.chart.Chart) PieChart(javafx.scene.chart.PieChart) BubbleChart(javafx.scene.chart.BubbleChart) StackedBarChart(javafx.scene.chart.StackedBarChart) ScatterChart(javafx.scene.chart.ScatterChart)

Example 17 with RuntimeIOException

use of htsjdk.samtools.util.RuntimeIOException in project jvarkit by lindenb.

the class TrapIndexer method decode.

public static TrapRecord decode(final String contig, byte[] array) {
    if (array.length != RECORD_SIZOF)
        throw new IllegalStateException("byte.length " + array.length + "!=" + RECORD_SIZOF);
    try {
        final DataInputStream dis = new DataInputStream(new ByteArrayInputStream(array));
        final int pos = dis.readInt();
        if (pos < 0)
            throw new IOException("pos<0 : " + pos);
        final byte ref = dis.readByte();
        final byte alt = dis.readByte();
        int ensgId = dis.readInt();
        final String ensg = String.format("ENSG%0" + (ENSG_STRLEN - 4) + "d", ensgId);
        byte[] score_bytes = new byte[SCORE_SIZEOF];
        dis.readFully(score_bytes);
        final float score;
        if (score_bytes[0] == (byte) 1) {
            score = 1.0f;
        } else {
            score = Float.parseFloat("0." + new String(score_bytes));
        }
        return new TrapRecord() {

            @Override
            public int getStart() {
                return pos;
            }

            @Override
            public int getEnd() {
                return pos;
            }

            @Override
            public String getContig() {
                return contig;
            }

            @Override
            public String getChr() {
                return getContig();
            }

            @Override
            public float getScore() {
                return score;
            }

            @Override
            public char getRef() {
                return (char) ref;
            }

            @Override
            public String getGene() {
                return ensg;
            }

            @Override
            public char getAlt() {
                return (char) alt;
            }

            @Override
            public String toString() {
                return contig + ":" + pos + ":" + (char) ref + "/" + (char) alt + " " + ensg + " " + score;
            }
        };
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
Also used : RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) DataInputStream(java.io.DataInputStream)

Example 18 with RuntimeIOException

use of htsjdk.samtools.util.RuntimeIOException in project jvarkit by lindenb.

the class ContigNameConverter method fromFile.

public static ContigNameConverter fromFile(final File mappingFile) {
    IOUtil.assertFileIsReadable(mappingFile);
    final MapBasedContigNameConverter mapper = new MapBasedContigNameConverter();
    mapper.name = mappingFile.getName();
    BufferedReader in = null;
    try {
        in = IOUtils.openFileForBufferedReading(mappingFile);
        String line;
        while ((line = in.readLine()) != null) {
            if (line.isEmpty() || line.startsWith("#"))
                continue;
            final String[] tokens = line.split("[\t]");
            if (tokens.length != 2 || tokens[0].trim().isEmpty() || tokens[1].trim().isEmpty()) {
                in.close();
                in = null;
                throw new IOException("Bad mapping line: \"" + line + "\"");
            }
            tokens[0] = tokens[0].trim();
            tokens[1] = tokens[1].trim();
            if (mapper.map.containsKey(tokens[0])) {
                in.close();
                throw new IOException("Mapping defined twice for: \"" + tokens[0] + "\"");
            }
            mapper.map.put(tokens[0], tokens[1]);
        }
        return mapper;
    } catch (final IOException err) {
        throw new RuntimeIOException(err);
    } finally {
        CloserUtil.close(in);
    }
}
Also used : RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException)

Example 19 with RuntimeIOException

use of htsjdk.samtools.util.RuntimeIOException in project jvarkit by lindenb.

the class FastaSequenceReader method read.

protected FastaSequence read(final PushbackReader reader) throws IOException {
    boolean at_begin = true;
    StringBuilder name = null;
    ByteArrayOutputStream sequence = null;
    try {
        int c;
        while ((c = reader.read()) != -1) {
            if (at_begin && c == '>') {
                if (name != null) {
                    reader.unread(c);
                    return this.fastaSequenceCreator.apply(name.toString(), sequence.toByteArray());
                }
                name = new StringBuilder();
                sequence = new ByteArrayOutputStream(this.sequenceCapacity);
                /* consume header */
                while ((c = reader.read()) != -1 && c != '\n') {
                    name.append((char) c);
                }
                at_begin = true;
            } else if (Character.isWhitespace(c)) {
                at_begin = (c == '\n');
            } else if (sequence == null) {
                throw new IOException("Illegal character " + (char) c);
            } else {
                sequence.write(c);
                while ((c = reader.read()) != -1 && c != '\n') {
                    sequence.write(c);
                }
                at_begin = true;
            }
        }
        /* eof met */
        if (name != null) {
            return this.fastaSequenceCreator.apply(name.toString(), sequence.toByteArray());
        }
        return null;
    } catch (final IOException e) {
        throw new RuntimeIOException(e);
    }
}
Also used : RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException)

Example 20 with RuntimeIOException

use of htsjdk.samtools.util.RuntimeIOException in project jvarkit by lindenb.

the class IOUtils method cat.

/**
 * write something in a file
 */
public static void cat(final Object o, final File out, boolean append) {
    PrintWriter w = null;
    try {
        w = new PrintWriter(new FileWriter(out, append));
        w.print(o);
        w.flush();
        w.close();
        w = null;
    } catch (final IOException err) {
        throw new RuntimeIOException(err);
    } finally {
        if (w != null)
            w.close();
    }
}
Also used : RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) FileWriter(java.io.FileWriter) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

RuntimeIOException (htsjdk.samtools.util.RuntimeIOException)21 IOException (java.io.IOException)11 File (java.io.File)6 List (java.util.List)5 VariantContext (htsjdk.variant.variantcontext.VariantContext)4 JvarkitException (com.github.lindenb.jvarkit.lang.JvarkitException)3 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)3 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)3 VCFHeader (htsjdk.variant.vcf.VCFHeader)3 VCFInfoHeaderLine (htsjdk.variant.vcf.VCFInfoHeaderLine)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Parameter (com.beust.jcommander.Parameter)2 TeeInputStream (com.github.lindenb.jvarkit.io.TeeInputStream)2 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)2 Program (com.github.lindenb.jvarkit.util.jcommander.Program)2 Logger (com.github.lindenb.jvarkit.util.log.Logger)2 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)2 VCFUtils (com.github.lindenb.jvarkit.util.vcf.VCFUtils)2 VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)2