Search in sources :

Example 16 with SAMFileWriterFactory

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();
    }
}
Also used : SAMFileWriter(htsjdk.samtools.SAMFileWriter) Stopwatch(com.google.common.base.Stopwatch) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File) IOException(java.io.IOException)

Example 17 with SAMFileWriterFactory

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;
}
Also used : Arrays(java.util.Arrays) AfterGroups(org.testng.annotations.AfterGroups) IOUtil(htsjdk.samtools.util.IOUtil) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) SamFiles(htsjdk.samtools.SamFiles) Random(java.util.Random) Test(org.testng.annotations.Test) JFXPanel(javafx.embed.swing.JFXPanel) SAMFileHeader(htsjdk.samtools.SAMFileHeader) SortOrder(htsjdk.samtools.SAMFileHeader.SortOrder) Application(javafx.application.Application) DefaultSAMRecordFactory(htsjdk.samtools.DefaultSAMRecordFactory) Vector(java.util.Vector) BeforeGroups(org.testng.annotations.BeforeGroups) ImageIO(javax.imageio.ImageIO) SAXParser(javax.xml.parsers.SAXParser) FastaSequenceIndexCreator(htsjdk.samtools.reference.FastaSequenceIndexCreator) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) Predicate(java.util.function.Predicate) JfxLauncher(com.github.lindenb.jvarkit.util.jcommander.JfxLauncher) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) BeforeClass(org.testng.annotations.BeforeClass) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) AfterTest(org.testng.annotations.AfterTest) SAMSequenceDictionaryCodec(htsjdk.samtools.SAMSequenceDictionaryCodec) Platform(javafx.application.Platform) CountDownLatch(java.util.concurrent.CountDownLatch) ReferenceSequenceFileFactory(htsjdk.samtools.reference.ReferenceSequenceFileFactory) List(java.util.List) Stream(java.util.stream.Stream) VariantContext(htsjdk.variant.variantcontext.VariantContext) Pattern(java.util.regex.Pattern) SamReaderFactory(htsjdk.samtools.SamReaderFactory) BeforeSuite(org.testng.annotations.BeforeSuite) FilenameFilter(java.io.FilenameFilter) CloseableIterator(htsjdk.samtools.util.CloseableIterator) ZipInputStream(java.util.zip.ZipInputStream) DataProvider(org.testng.annotations.DataProvider) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Function(java.util.function.Function) ValidationStringency(htsjdk.samtools.ValidationStringency) ArrayList(java.util.ArrayList) NcbiApiKey(com.github.lindenb.jvarkit.util.ncbi.NcbiApiKey) Interval(htsjdk.samtools.util.Interval) BeforeTest(org.testng.annotations.BeforeTest) Assert(org.testng.Assert) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) SAMRecordFactory(htsjdk.samtools.SAMRecordFactory) SAMSequenceDictionaryExtractor(htsjdk.variant.utils.SAMSequenceDictionaryExtractor) AfterClass(org.testng.annotations.AfterClass) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SamReader(htsjdk.samtools.SamReader) File(java.io.File) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Stage(javafx.stage.Stage) Paths(java.nio.file.Paths) BAMIndex(htsjdk.samtools.BAMIndex) BufferedReader(java.io.BufferedReader) AfterSuite(org.testng.annotations.AfterSuite) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) SamReader(htsjdk.samtools.SamReader) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

Example 18 with SAMFileWriterFactory

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;
}
Also used : Arrays(java.util.Arrays) AfterGroups(org.testng.annotations.AfterGroups) IOUtil(htsjdk.samtools.util.IOUtil) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) SamFiles(htsjdk.samtools.SamFiles) Random(java.util.Random) Test(org.testng.annotations.Test) JFXPanel(javafx.embed.swing.JFXPanel) SAMFileHeader(htsjdk.samtools.SAMFileHeader) SortOrder(htsjdk.samtools.SAMFileHeader.SortOrder) Application(javafx.application.Application) DefaultSAMRecordFactory(htsjdk.samtools.DefaultSAMRecordFactory) Vector(java.util.Vector) BeforeGroups(org.testng.annotations.BeforeGroups) ImageIO(javax.imageio.ImageIO) SAXParser(javax.xml.parsers.SAXParser) FastaSequenceIndexCreator(htsjdk.samtools.reference.FastaSequenceIndexCreator) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) Predicate(java.util.function.Predicate) JfxLauncher(com.github.lindenb.jvarkit.util.jcommander.JfxLauncher) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) BeforeClass(org.testng.annotations.BeforeClass) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) AfterTest(org.testng.annotations.AfterTest) SAMSequenceDictionaryCodec(htsjdk.samtools.SAMSequenceDictionaryCodec) Platform(javafx.application.Platform) CountDownLatch(java.util.concurrent.CountDownLatch) ReferenceSequenceFileFactory(htsjdk.samtools.reference.ReferenceSequenceFileFactory) List(java.util.List) Stream(java.util.stream.Stream) VariantContext(htsjdk.variant.variantcontext.VariantContext) Pattern(java.util.regex.Pattern) SamReaderFactory(htsjdk.samtools.SamReaderFactory) BeforeSuite(org.testng.annotations.BeforeSuite) FilenameFilter(java.io.FilenameFilter) CloseableIterator(htsjdk.samtools.util.CloseableIterator) ZipInputStream(java.util.zip.ZipInputStream) DataProvider(org.testng.annotations.DataProvider) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Function(java.util.function.Function) ValidationStringency(htsjdk.samtools.ValidationStringency) ArrayList(java.util.ArrayList) NcbiApiKey(com.github.lindenb.jvarkit.util.ncbi.NcbiApiKey) Interval(htsjdk.samtools.util.Interval) BeforeTest(org.testng.annotations.BeforeTest) Assert(org.testng.Assert) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) SAMRecordFactory(htsjdk.samtools.SAMRecordFactory) SAMSequenceDictionaryExtractor(htsjdk.variant.utils.SAMSequenceDictionaryExtractor) AfterClass(org.testng.annotations.AfterClass) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SamReader(htsjdk.samtools.SamReader) File(java.io.File) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Stage(javafx.stage.Stage) Paths(java.nio.file.Paths) BAMIndex(htsjdk.samtools.BAMIndex) BufferedReader(java.io.BufferedReader) AfterSuite(org.testng.annotations.AfterSuite) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) SamReader(htsjdk.samtools.SamReader) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

Example 19 with SAMFileWriterFactory

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);
    }
}
Also used : SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) JAXBContext(javax.xml.bind.JAXBContext) SAMProgramRecord(htsjdk.samtools.SAMProgramRecord) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventReader(javax.xml.stream.XMLEventReader) XMLResolver(javax.xml.stream.XMLResolver) SAMFileHeader(htsjdk.samtools.SAMFileHeader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 20 with SAMFileWriterFactory

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

SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)33 SAMFileWriter (htsjdk.samtools.SAMFileWriter)26 File (java.io.File)26 SAMRecord (htsjdk.samtools.SAMRecord)22 SAMFileHeader (htsjdk.samtools.SAMFileHeader)20 SamReader (htsjdk.samtools.SamReader)17 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)14 IOException (java.io.IOException)14 SamReaderFactory (htsjdk.samtools.SamReaderFactory)12 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)8 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)7 ArrayList (java.util.ArrayList)7 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)5 Test (org.testng.annotations.Test)5 Interval (htsjdk.samtools.util.Interval)4 BufferedReader (java.io.BufferedReader)4 List (java.util.List)4 BAMIndex (htsjdk.samtools.BAMIndex)3 DefaultSAMRecordFactory (htsjdk.samtools.DefaultSAMRecordFactory)3 SAMProgramRecord (htsjdk.samtools.SAMProgramRecord)3