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);
}
}
Aggregations