RevertSamWriter( final boolean outputByReadGroup, final Map<String, SAMFileHeader> headerMap, final Map<String, File> outputMap, final SAMFileHeader singleOutHeader, final File singleOutput, final boolean presorted, final SAMFileWriterFactory factory, final File referenceFasta) { this.outputByReadGroup = outputByReadGroup; if (outputByReadGroup) { singleWriter = null; for (final Map.Entry<String, File> outputMapEntry : outputMap.entrySet()) { final String readGroupId = outputMapEntry.getKey(); final File output = outputMapEntry.getValue(); final SAMFileHeader header = headerMap.get(readGroupId); final SAMFileWriter writer = factory.makeWriter(header, presorted, output, referenceFasta); writerMap.put(readGroupId, writer); } } else { singleWriter = factory.makeWriter(singleOutHeader, presorted, singleOutput, referenceFasta); } }
RevertSamWriter( final boolean outputByReadGroup, final Map<String, SAMFileHeader> headerMap, final Map<String, File> outputMap, final SAMFileHeader singleOutHeader, final File singleOutput, final boolean presorted, final SAMFileWriterFactory factory, final File referenceFasta) { this.outputByReadGroup = outputByReadGroup; if (outputByReadGroup) { singleWriter = null; for (final Map.Entry<String, File> outputMapEntry : outputMap.entrySet()) { final String readGroupId = outputMapEntry.getKey(); final File output = outputMapEntry.getValue(); final SAMFileHeader header = headerMap.get(readGroupId); final SAMFileWriter writer = factory.makeWriter(header, presorted, output, referenceFasta); writerMap.put(readGroupId, writer); } } else { singleWriter = factory.makeWriter(singleOutHeader, presorted, singleOutput, referenceFasta); } }
/** * * Create a SAM, BAM or CRAM writer based on examination of the outputPath extension. * * @param header header. Sort order is determined by the sortOrder property of this arg. * @param presorted if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder. * @param outputPath where to write the output. Must end with .sam, .bam or .cram. * @param referenceFasta reference sequence file * @return SAMFileWriter appropriate for the file type specified in outputPath * @deprecated since 6/18, use {@link #makeWriter(SAMFileHeader, boolean, Path, Path)} instead */ @Deprecated public SAMFileWriter makeWriter(final SAMFileHeader header, final boolean presorted, final Path outputPath, final File referenceFasta) { return makeWriter(header, presorted, outputPath, IOUtil.toPath( referenceFasta )); }
/** * * Create a SAM, BAM or CRAM writer based on examination of the outputPath extension. * * @param header header. Sort order is determined by the sortOrder property of this arg. * @param presorted if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder. * @param outputPath where to write the output. Must end with .sam, .bam or .cram. * @param referenceFasta reference sequence file * @return SAMFileWriter appropriate for the file type specified in outputPath * @deprecated since 6/18, use {@link #makeWriter(SAMFileHeader, boolean, Path, Path)} instead */ @Deprecated public SAMFileWriter makeWriter(final SAMFileHeader header, final boolean presorted, final Path outputPath, final File referenceFasta) { return makeWriter(header, presorted, outputPath, IOUtil.toPath( referenceFasta )); }
/** * * Create a SAM, BAM or CRAM writer based on examination of the outputFile extension. * * @param header header. Sort order is determined by the sortOrder property of this arg. * @param presorted if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder. * @param outputFile where to write the output. Must end with .sam, .bam or .cram. * @param referenceFasta reference sequence file * @return SAMFileWriter appropriate for the file type specified in outputFile * */ public SAMFileWriter makeWriter(final SAMFileHeader header, final boolean presorted, final File outputFile, final File referenceFasta) { return makeWriter(header, presorted, IOUtil.toPath( outputFile ), IOUtil.toPath(referenceFasta)); }
/** * * Create a SAM, BAM or CRAM writer based on examination of the outputFile extension. * * @param header header. Sort order is determined by the sortOrder property of this arg. * @param presorted if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder. * @param outputFile where to write the output. Must end with .sam, .bam or .cram. * @param referenceFasta reference sequence file * @return SAMFileWriter appropriate for the file type specified in outputFile * */ public SAMFileWriter makeWriter(final SAMFileHeader header, final boolean presorted, final File outputFile, final File referenceFasta) { return makeWriter(header, presorted, IOUtil.toPath( outputFile ), IOUtil.toPath(referenceFasta)); }
private void writeRecordsToPath ( final List<SAMRecord> recs, final Path targetPath, final File referenceFile, final SAMFileHeader samHeader) { // NOTE: even when the input is coord-sorted, using assumePresorted=false will cause some // tests to fail since it can change the order of some unmapped reads - this is allowed // by the spec since the order is arbitrary for unmapped. try (final SAMFileWriter writer = new SAMFileWriterFactory() .makeWriter(samHeader, true, targetPath, referenceFile.toPath())) { for (SAMRecord rec : recs) { writer.addAlignment(rec); } } }
@Test public void testAsync() throws IOException { final SAMFileWriterFactory builder = new SAMFileWriterFactory(); final File outputFile = prepareOutputFile(BamFileIoUtils.BAM_FILE_EXTENSION); final SAMFileHeader header = new SAMFileHeader(); final SAMFileWriterFactory factory = createWriterFactoryWithOptions(header); final File referenceFile = new File(TEST_DATA_DIR, "hg19mini.fasta"); SAMFileWriter writer = builder.makeWriter(header, false, outputFile, referenceFile); Assert.assertEquals(writer instanceof AsyncSAMFileWriter, Defaults.USE_ASYNC_IO_WRITE_FOR_SAMTOOLS, "testAsync default"); writer = builder.setUseAsyncIo(true).makeWriter(header, false, outputFile, referenceFile); Assert.assertTrue(writer instanceof AsyncSAMFileWriter, "testAsync option=set"); writer = builder.setUseAsyncIo(false).makeWriter(header, false, outputFile, referenceFile); Assert.assertFalse(writer instanceof AsyncSAMFileWriter, "testAsync option=unset"); } }
private void writeRecordsToFile ( final List<SAMRecord> recs, final File targetFile, final File referenceFile, final SAMFileHeader samHeader) { // NOTE: even when the input is coord-sorted, using assumePresorted=false will cause some // tests to fail since it can change the order of some unmapped reads - AFAICT this is allowed // by the spec since the order is arbitrary for unmapped try (final SAMFileWriter writer = new SAMFileWriterFactory() .makeWriter(samHeader, true, targetFile, referenceFile)) { for (SAMRecord rec : recs) { writer.addAlignment(rec); } } }
private void tryToWriteToSamAndBam(final SamReader reader) throws IOException { final File bamOutput = File.createTempFile("test", ".bam"); final File samOutput = File.createTempFile("test", ".sam"); final SAMFileWriter samWriter = new SAMFileWriterFactory().makeWriter(reader.getFileHeader(), true, samOutput, null); final SAMFileWriter bamWriter = new SAMFileWriterFactory().makeWriter(reader.getFileHeader(), true, bamOutput, null); final List<String> errorMessages = new ArrayList<>(); for (SAMRecord rec : reader) { try { samWriter.addAlignment(rec); bamWriter.addAlignment(rec); } catch (final Throwable e) { System.out.println(e.getMessage()); errorMessages.add(e.getMessage()); } } CloserUtil.close(reader); samWriter.close(); bamWriter.close(); Assert.assertEquals(errorMessages.size(), 0); bamOutput.deleteOnExit(); samOutput.deleteOnExit(); }
@Test(dataProvider="bamOrCramWriter") public void testMakeWriterPathAndReferencePath(String extension) throws Exception { final String referenceName = "hg19mini.fasta"; try (FileSystem jimfs = Jimfs.newFileSystem(Configuration.unix())) { Path outputPath = jimfs.getPath("testMakeWriterPath" + extension); Files.deleteIfExists(outputPath); final SAMFileHeader header = new SAMFileHeader(); final SAMFileWriterFactory factory = createWriterFactoryWithOptions(header); final Path referencePath = jimfs.getPath(referenceName); Files.copy(new File(TEST_DATA_DIR, referenceName).toPath(), referencePath); int nRecs; try (final SAMFileWriter samWriter = factory.makeWriter(header, false, outputPath, referencePath)) { nRecs = fillSmallBam(samWriter); } verifyWriterOutput(outputPath, new ReferenceSource(referencePath), nRecs, true); } }
@Test(dataProvider="bamOrCramWriter") public void testMakeWriterPath(String extension) throws Exception { try (FileSystem jimfs = Jimfs.newFileSystem(Configuration.unix())) { Path outputPath = jimfs.getPath("testMakeWriterPath" + extension); Files.deleteIfExists(outputPath); final SAMFileHeader header = new SAMFileHeader(); final SAMFileWriterFactory factory = createWriterFactoryWithOptions(header); final Path referenceFile = new File(TEST_DATA_DIR, "hg19mini.fasta").toPath(); int nRecs; try (final SAMFileWriter samWriter = factory.makeWriter(header, false, outputPath, referenceFile)) { nRecs = fillSmallBam(samWriter); } verifyWriterOutput(outputPath, new ReferenceSource(referenceFile), nRecs, true); } }
@Test(dataProvider="bamOrCramWriter") public void testMakeWriter(String extension) throws Exception { final File outputFile = prepareOutputFile(extension); final SAMFileHeader header = new SAMFileHeader(); final SAMFileWriterFactory factory = createWriterFactoryWithOptions(header); final File referenceFile = new File(TEST_DATA_DIR, "hg19mini.fasta"); int nRecs; try (final SAMFileWriter samWriter = factory.makeWriter(header, false, outputFile, referenceFile)) { nRecs = fillSmallBam(samWriter); } verifyWriterOutput(outputFile, new ReferenceSource(referenceFile), nRecs, true); }
private static CachingBAMFileIndex getIndexWith200Contigs() throws IOException { List<SAMSequenceRecord> contigs = IntStream.range(1, 200) .mapToObj(i -> new SAMSequenceRecord(String.valueOf(i), 1000)) .collect(Collectors.toList()); final SAMSequenceDictionary dict = new SAMSequenceDictionary(contigs); final SAMFileHeader header = new SAMFileHeader(dict); header.setSortOrder(SAMFileHeader.SortOrder.coordinate); final SAMFileWriterFactory writerFactory = new SAMFileWriterFactory().setCreateIndex(true); final File outBam = File.createTempFile("tmp", ".bam"); try(final SAMFileWriter writer = writerFactory.makeWriter(header, true, outBam, null)){ IntStream.range(1,200).mapToObj(i -> { final SAMRecord record = new SAMRecord(header); record.setReadName("name" + i); record.setReferenceName(String.valueOf(i)); record.setReadUnmappedFlag(false); record.setAlignmentStart(1); record.setCigarString("20M"); return record; }).forEach(writer::addAlignment); } final File indexFile = new File(outBam.getParent(), IOUtil.basename(outBam) + BAMIndex.BAMIndexSuffix); indexFile.deleteOnExit(); outBam.deleteOnExit(); return new CachingBAMFileIndex(indexFile, dict); }
@Test public void testCRAMQuerySort() throws IOException { final File input = new File("src/test/resources/htsjdk/samtools/cram_query_sorted.cram"); final File reference = new File("src/test/resources/htsjdk/samtools/cram_query_sorted.fasta"); final File outputFile = File.createTempFile("tmp.", ".cram"); try (final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(reference).open(input); final SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(reader.getFileHeader().clone(), false, outputFile, reference)) { for (SAMRecord rec : reader) { writer.addAlignment(rec); } } try (final SamReader outReader = SamReaderFactory.makeDefault().referenceSequence(reference).open(outputFile)) { String prevName = null; for (final SAMRecord rec : outReader) { if (prevName == null) { prevName = rec.getReadName(); continue; } // test if the read names are sorted alphabetically: Assert.assertTrue(rec.getReadName().compareTo(prevName) >= 0); } } }
@Test(dataProvider = "getEmptyArraysAndExtensions") public void testWriteSamWithEmptyArray(Object emptyArray, Class<?> arrayClass, String fileExtension) throws IOException { Assert.assertEquals(emptyArray.getClass(), arrayClass); Assert.assertEquals(Array.getLength(emptyArray), 0); final SAMRecordSetBuilder samRecords = new SAMRecordSetBuilder(); samRecords.addFrag("Read", 0, 100, false); final SAMRecord record = samRecords.getRecords().iterator().next(); record.setAttribute(ARRAY_TAG, emptyArray); checkArrayIsEmpty(ARRAY_TAG, record, arrayClass); final Path tmp = Files.createTempFile("tmp", fileExtension); IOUtil.deleteOnExit(tmp); final SAMFileWriterFactory writerFactory = new SAMFileWriterFactory() .setCreateMd5File(false) .setCreateIndex(false); final Path reference = IOUtil.getPath("src/test/resources/htsjdk/samtools/one-contig.fasta"); try (final SAMFileWriter samFileWriter = writerFactory.makeWriter(samRecords.getHeader(), false, tmp, reference)) { samFileWriter.addAlignment(record); } try (final SamReader reader = SamReaderFactory.makeDefault() .referenceSequence(reference) .open(tmp)) { final SAMRecordIterator iterator = reader.iterator(); Assert.assertTrue(iterator.hasNext()); final SAMRecord recordFromDisk = iterator.next(); checkArrayIsEmpty(ARRAY_TAG, recordFromDisk, arrayClass); } }
@Override protected int doWork() { try (final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(IOUtil.getPath(INPUT)); final SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(reader.getFileHeader(), true, IOUtil.getPath(OUTPUT), REFERENCE_SEQUENCE)) { writer.setProgressLogger( new ProgressLogger(log, (int) 1e7, "Wrote", "records")); final OverlapDetector overlapDetector = getOverlapDetectorFromIntervalListFile(INTERVAL_LIST, 0, 0); for (final SAMRecord rec : reader) { if (overlapDetector == null || overlapDetector.overlapsAny(rec)) { setOATag(rec); } writer.addAlignment(rec); } } catch (IOException e) { log.error(e); return 1; } return 0; }
@Override protected int doWork() { try (final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(IOUtil.getPath(INPUT)); final SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(reader.getFileHeader(), true, IOUtil.getPath(OUTPUT), REFERENCE_SEQUENCE)) { writer.setProgressLogger( new ProgressLogger(log, (int) 1e7, "Wrote", "records")); final OverlapDetector overlapDetector = getOverlapDetectorFromIntervalListFile(INTERVAL_LIST, 0, 0); for (final SAMRecord rec : reader) { if (overlapDetector == null || overlapDetector.overlapsAny(rec)) { setOATag(rec); } writer.addAlignment(rec); } } catch (IOException e) { log.error(e); return 1; } return 0; }
protected int doWork() { IOUtil.assertFileIsReadable(INPUT); IOUtil.assertFileIsWritable(OUTPUT); final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT); final SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(reader.getFileHeader(), true, OUTPUT, REFERENCE_SEQUENCE); if (CREATE_INDEX && writer.getFileHeader().getSortOrder() != SAMFileHeader.SortOrder.coordinate) { throw new PicardException("Can't CREATE_INDEX unless sort order is coordinate"); } final ProgressLogger progress = new ProgressLogger(Log.getInstance(SamFormatConverter.class)); for (final SAMRecord rec : reader) { writer.addAlignment(rec); progress.record(rec); } CloserUtil.close(reader); writer.close(); return 0; } }
protected int doWork() { IOUtil.assertFileIsReadable(INPUT); IOUtil.assertFileIsWritable(OUTPUT); final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT); final SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(reader.getFileHeader(), true, OUTPUT, REFERENCE_SEQUENCE); if (CREATE_INDEX && writer.getFileHeader().getSortOrder() != SAMFileHeader.SortOrder.coordinate) { throw new PicardException("Can't CREATE_INDEX unless sort order is coordinate"); } final ProgressLogger progress = new ProgressLogger(Log.getInstance(SamFormatConverter.class)); for (final SAMRecord rec : reader) { writer.addAlignment(rec); progress.record(rec); } CloserUtil.close(reader); writer.close(); return 0; } }