use of htsjdk.samtools.SAMFileWriterFactory in project gridss by PapenfussLab.
the class AssemblyEvidenceSource method assembleChunk.
private void assembleChunk(File output, int chunkNumber, QueryInterval[] qi) throws IOException {
AssemblyIdGenerator assemblyNameGenerator = new SequentialIdGenerator(String.format("asm%d-", chunkNumber));
String chuckName = String.format("chunk %d (%s:%d-%s:%d)", chunkNumber, getContext().getDictionary().getSequence(qi[0].referenceIndex).getSequenceName(), qi[0].start, getContext().getDictionary().getSequence(qi[qi.length - 1].referenceIndex).getSequenceName(), qi[qi.length - 1].end);
log.info(String.format("Starting assembly on %s", chuckName));
Stopwatch timer = Stopwatch.createStarted();
SAMFileHeader header = getContext().getBasicSamHeader();
// TODO: add assembly @PG header
File filteredout = FileSystemContext.getWorkingFileFor(output, "filtered.");
File tmpout = FileSystemContext.getWorkingFileFor(output, "gridss.tmp.");
try (SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, false, tmpout)) {
if (getContext().getAssemblyParameters().writeFiltered) {
try (SAMFileWriter filteredWriter = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, false, filteredout)) {
for (BreakendDirection direction : BreakendDirection.values()) {
assembleChunk(writer, filteredWriter, chunkNumber, qi, direction, assemblyNameGenerator);
}
}
} else {
for (BreakendDirection direction : BreakendDirection.values()) {
assembleChunk(writer, null, chunkNumber, qi, direction, assemblyNameGenerator);
}
}
} catch (Exception e) {
log.error(e, "Error assembling ", chuckName);
if (getContext().getConfig().terminateOnFirstError) {
System.exit(1);
}
throw e;
} finally {
timer.stop();
log.info(String.format("Completed assembly on %s in %ds (%s)", chuckName, timer.elapsed(TimeUnit.SECONDS), timer.toString()));
}
SAMFileUtil.sort(getContext().getFileSystemContext(), tmpout, output, SortOrder.coordinate);
if (gridss.Defaults.DELETE_TEMPORARY_FILES) {
tmpout.delete();
filteredout.delete();
}
if (gridss.Defaults.DEFENSIVE_GC) {
log.debug("Requesting defensive GC to ensure OS file handles are closed");
System.gc();
System.runFinalization();
}
}
use of htsjdk.samtools.SAMFileWriterFactory in project jvarkit by lindenb.
the class TestUtils method sortBamOnQueryName.
protected File sortBamOnQueryName(final Path bamFile, final Predicate<SAMRecord> pred) throws IOException {
File sortedBam = this.createTmpFile(".bam");
SamReader sr = SamReaderFactory.makeDefault().open(bamFile);
SAMFileHeader outHeader = sr.getFileHeader().clone();
outHeader.setSortOrder(SortOrder.queryname);
SAMFileWriter w = new SAMFileWriterFactory().makeBAMWriter(outHeader, false, sortedBam);
sr.iterator().stream().filter(R -> pred == null ? true : pred.test(R)).forEach(R -> w.addAlignment(R));
w.close();
sr.close();
return sortedBam;
}
use of htsjdk.samtools.SAMFileWriterFactory in project jvarkit by lindenb.
the class TestUtils method addClippingToBam.
protected File addClippingToBam(final File bamFile) throws IOException {
final String bases = "ATGC";
File clippedBam = this.createTmpFile(".bam");
SamReader sr = SamReaderFactory.makeDefault().open(bamFile);
SAMFileHeader inHeader = sr.getFileHeader();
boolean createIndex = sr.hasIndex() && inHeader.getSortOrder().equals(SortOrder.coordinate);
if (createIndex) {
this.deleteFilesAtExit.add(new File(bamFile.getParentFile(), IOUtil.basename(clippedBam) + BAMIndex.BAMIndexSuffix));
}
final SAMFileWriter w = new SAMFileWriterFactory().setCreateIndex(createIndex).makeBAMWriter(inHeader, true, clippedBam);
sr.iterator().stream().map(R -> {
if (R.getReadUnmappedFlag() || R.getCigar() == null)
return R;
if (R.getCigar().isClipped())
return R;
if (R.getBaseQualities().equals(SAMRecord.NULL_QUALS))
return R;
if (R.getBaseQualityString().equals(SAMRecord.NULL_QUALS_STRING))
return R;
for (int side = 0; side < 2; side++) {
final String cigar;
boolean hard = this.random.nextBoolean();
final int clipLen = 1 + this.random.nextInt(100);
final StringBuilder seq = new StringBuilder();
final StringBuilder qual = new StringBuilder();
if (hard) {
cigar = String.valueOf(clipLen) + "H";
} else {
cigar = String.valueOf(clipLen) + "S";
for (int x = 0; x < clipLen; ++x) {
seq.append(bases.charAt(this.random.nextInt(bases.length())));
qual.append("#");
}
}
if (side == 0) {
R.setReadString(seq.toString() + R.getReadString());
R.setBaseQualityString(qual.toString() + R.getBaseQualityString());
R.setCigarString(cigar + R.getCigarString());
} else {
R.setCigarString(R.getCigarString() + cigar);
R.setReadString(R.getReadString() + seq.toString());
R.setBaseQualityString(R.getBaseQualityString() + qual.toString());
}
}
return R;
}).forEach(R -> w.addAlignment(R));
w.close();
sr.close();
assertIsValidBam(bamFile);
return bamFile;
}
use of htsjdk.samtools.SAMFileWriterFactory in project jvarkit by lindenb.
the class BlastToSam method doWork.
@Override
public int doWork(List<String> args) {
if (this.faidx == null || !this.faidx.exists() || !this.faidx.isFile()) {
LOG.error("Option -R was not defined or dictionary missing");
return -1;
}
final boolean interleaved_input = this.EXPECTED_SIZE > 0;
final int maxRecordsInRam = 5000;
SAMFileWriter sfw = null;
XMLEventReader rx = null;
final SAMFileWriterFactory sfwf = new SAMFileWriterFactory();
sfwf.setCreateIndex(false);
sfwf.setMaxRecordsInRam(maxRecordsInRam);
sfwf.setCreateMd5File(false);
sfwf.setUseAsyncIo(false);
final SAMFileHeader header = new SAMFileHeader();
try {
LOG.info("opening " + faidx);
this.dictionary = SAMSequenceDictionaryExtractor.extractDictionary(faidx);
header.setSortOrder(SortOrder.unsorted);
header.setSequenceDictionary(this.dictionary);
final JAXBContext jc = JAXBContext.newInstance("gov.nih.nlm.ncbi.blast");
this.unmarshaller = jc.createUnmarshaller();
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
xmlInputFactory.setXMLResolver(new XMLResolver() {
@Override
public Object resolveEntity(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException {
LOG.info("resolveEntity:" + arg0 + "/" + arg1 + "/" + arg2);
return null;
}
});
final String inputName = oneFileOrNull(args);
if (inputName == null) {
LOG.info("Reading from stdin");
rx = xmlInputFactory.createXMLEventReader(stdin());
} else if (args.size() == 1) {
LOG.info("Reading from " + inputName);
rx = xmlInputFactory.createXMLEventReader(IOUtils.openURIForBufferedReading(inputName));
} else {
LOG.error("Illegal number of args");
return -1;
}
final SAMProgramRecord prg2 = header.createProgramRecord();
fillHeader(rx, prg2);
final SAMProgramRecord prg1 = header.createProgramRecord();
prg1.setCommandLine(getProgramCommandLine());
prg1.setProgramVersion(getVersion());
prg1.setProgramName(getProgramName());
prg1.setPreviousProgramGroupId(prg2.getId());
final SAMReadGroupRecord rg1 = new SAMReadGroupRecord("g1");
rg1.setLibrary("blast");
rg1.setSample("blast");
rg1.setDescription("blast");
header.addReadGroup(rg1);
sfw = this.writingBamArgs.openSAMFileWriter(outputFile, header, true);
if (interleaved_input) {
run_paired(sfw, rx, header);
} else {
run_single(sfw, rx, header);
}
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(sfw);
CloserUtil.close(rx);
}
}
use of htsjdk.samtools.SAMFileWriterFactory 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