Search in sources :

Example 1 with LogCloseableIterator

use of com.github.lindenb.jvarkit.tools.vcfviewgui.NgsStage.LogCloseableIterator in project jvarkit by lindenb.

the class BamStage method doMenuSaveAs.

@Override
protected void doMenuSaveAs() {
    final FileChooser fc = owner.newFileChooser();
    fc.getExtensionFilters().addAll(EXTENSION_FILTERS);
    final File saveAs = owner.updateLastDir(fc.showSaveDialog(this));
    if (saveAs == null)
        return;
    if (!saveAs.getName().endsWith(".bam")) {
        final Alert alert = new Alert(AlertType.ERROR, "Output should end with .bam", ButtonType.OK);
        alert.showAndWait();
        return;
    }
    Optional<BamJavascripFilter> bamjsfilter = Optional.empty();
    if (this.owner.javascriptCompiler.isPresent() && !this.javascriptArea.getText().trim().isEmpty()) {
        try {
            bamjsfilter = Optional.of(new BamJavascripFilter(this.getBamFile().getHeader(), Optional.of(this.owner.javascriptCompiler.get().compile(this.javascriptArea.getText()))));
        } catch (final Exception err) {
            JfxNgs.showExceptionDialog(this, err);
            bamjsfilter = Optional.empty();
            return;
        }
    }
    final Predicate<SAMRecord> flagfilter = makeFlagPredicate();
    final SAMFileWriterFactory swf = new SAMFileWriterFactory();
    swf.setCreateIndex(true);
    CloseableIterator<SAMRecord> iter = null;
    SAMFileWriter w = null;
    try {
        final SAMFileHeader h2 = this.getBamFile().getHeader().clone();
        h2.addComment("Generated with JfxNgs. javascript was: " + this.javascriptArea.getText().trim().replaceAll("[\n\t\r ]+", " "));
        w = swf.makeBAMWriter(h2, true, saveAs);
        iter = new LogCloseableIterator(this.getBamFile().iterator(), null);
        while (iter.hasNext()) {
            final SAMRecord rec = iter.next();
            if (!flagfilter.test(rec))
                continue;
            if (bamjsfilter.isPresent()) {
                if (bamjsfilter.get().eval(rec) == null)
                    continue;
            }
            w.addAlignment(rec);
        }
        w.close();
        w = null;
        iter.close();
        iter = null;
        final Alert alert = new Alert(AlertType.CONFIRMATION, "Done", ButtonType.OK);
        alert.showAndWait();
    } catch (Exception err) {
        JfxNgs.showExceptionDialog(this, err);
        return;
    } finally {
        CloserUtil.close(iter);
        CloserUtil.close(w);
    }
}
Also used : SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) SAMRecord(htsjdk.samtools.SAMRecord) FileChooser(javafx.stage.FileChooser) Alert(javafx.scene.control.Alert) SAMFileHeader(htsjdk.samtools.SAMFileHeader) LogCloseableIterator(com.github.lindenb.jvarkit.tools.vcfviewgui.NgsStage.LogCloseableIterator) File(java.io.File)

Aggregations

LogCloseableIterator (com.github.lindenb.jvarkit.tools.vcfviewgui.NgsStage.LogCloseableIterator)1 SAMFileHeader (htsjdk.samtools.SAMFileHeader)1 SAMFileWriter (htsjdk.samtools.SAMFileWriter)1 SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)1 SAMRecord (htsjdk.samtools.SAMRecord)1 File (java.io.File)1 IOException (java.io.IOException)1 Alert (javafx.scene.control.Alert)1 FileChooser (javafx.stage.FileChooser)1 ScriptException (javax.script.ScriptException)1