htsjdk.samtools.SAMFileWriterFactory Java Examples

The following examples show how to use htsjdk.samtools.SAMFileWriterFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PreprocessingTools.java    From halvade with GNU General Public License v3.0 6 votes vote down vote up
public int callElPrep(String input, String output, String rg, int threads, 
        SAMRecordIterator SAMit,
        SAMFileHeader header, String dictFile, boolean updateRG, boolean keepDups, String RGID) throws InterruptedException, QualityException {
    
    SAMRecord sam;
    SAMFileWriterFactory factory = new SAMFileWriterFactory();
    SAMFileWriter Swriter = factory.makeSAMWriter(header, true, new File(input));
    
    int reads = 0;
    while(SAMit.hasNext()) {
        sam = SAMit.next();
        if(updateRG)
            sam.setAttribute(SAMTag.RG.name(), RGID);
        Swriter.addAlignment(sam);
        reads++;
    }
    Swriter.close();
    
    String customArgs = HalvadeConf.getCustomArgs(context.getConfiguration(), "elprep", "");  
    String[] command = CommandGenerator.elPrep(bin, input, output, threads, true, rg, null, !keepDups, customArgs);
    long estimatedTime = runProcessAndWait("elPrep", command);
    if(context != null)
        context.getCounter(HalvadeCounters.TIME_ELPREP).increment(estimatedTime);
    
    return reads;
}
 
Example #2
Source File: SortSam.java    From picard with MIT License 6 votes vote down vote up
protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsWritable(OUTPUT);
    final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
    ;
    reader.getFileHeader().setSortOrder(SORT_ORDER.getSortOrder());
    final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(reader.getFileHeader(), false, OUTPUT);
    writer.setProgressLogger(
            new ProgressLogger(log, (int) 1e7, "Wrote", "records from a sorting collection"));

    final ProgressLogger progress = new ProgressLogger(log, (int) 1e7, "Read");
    for (final SAMRecord rec : reader) {
        writer.addAlignment(rec);
        progress.record(rec);
    }

    log.info("Finished reading inputs, merging and writing to output now.");

    CloserUtil.close(reader);
    writer.close();
    return 0;
}
 
Example #3
Source File: RevertSam.java    From picard with MIT License 6 votes vote down vote up
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);
    }
}
 
Example #4
Source File: ReplaceSamHeader.java    From picard with MIT License 6 votes vote down vote up
private void standardReheader(final SAMFileHeader replacementHeader) {
    final SamReader recordReader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).validationStringency(ValidationStringency.SILENT).open(INPUT);
    if (replacementHeader.getSortOrder() != recordReader.getFileHeader().getSortOrder()) {
        throw new PicardException("Sort orders of INPUT (" + recordReader.getFileHeader().getSortOrder().name() +
                ") and HEADER (" + replacementHeader.getSortOrder().name() + ") do not agree.");
    }
    final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(replacementHeader, true, OUTPUT);

    final ProgressLogger progress = new ProgressLogger(Log.getInstance(ReplaceSamHeader.class));
    for (final SAMRecord rec : recordReader) {
        rec.setHeader(replacementHeader);
        writer.addAlignment(rec);
        progress.record(rec);
    }
    writer.close();
    CloserUtil.close(recordReader);
}
 
Example #5
Source File: SamFormatConverter.java    From picard with MIT License 6 votes vote down vote up
/**
 * Convert a file from one of sam/bam/cram format to another based on the extension of output.
 *
 * @param input             input file in one of sam/bam/cram format
 * @param output            output to write converted file to, the conversion is based on the extension of this filename
 * @param referenceSequence the reference sequence to use, necessary when reading/writing cram
 * @param createIndex       whether or not an index should be written alongside the output file
 */
public static void convert(final File input, final File output, final File referenceSequence, final Boolean createIndex) {
    IOUtil.assertFileIsReadable(input);
    IOUtil.assertFileIsWritable(output);
    final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(referenceSequence).open(input);
    final SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(reader.getFileHeader(), true, output, referenceSequence);
    if (createIndex && 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();
}
 
Example #6
Source File: GatherBamFiles.java    From picard with MIT License 6 votes vote down vote up
/**
 * Simple implementation of a gather operations that uses SAMFileReaders and Writers in order to concatenate
 * multiple BAM files.
 */
private static void gatherNormally(final List<File> inputs, final File output, final boolean createIndex, final boolean createMd5,
                                   final File referenceFasta) {
    final SAMFileHeader header;
    {
        header = SamReaderFactory.makeDefault().referenceSequence(referenceFasta).getFileHeader(inputs.get(0));
    }

    final SAMFileWriter out = new SAMFileWriterFactory().setCreateIndex(createIndex).setCreateMd5File(createMd5).makeSAMOrBAMWriter(header, true, output);

    for (final File f : inputs) {
        log.info("Gathering " + f.getAbsolutePath());
        final SamReader in = SamReaderFactory.makeDefault().referenceSequence(referenceFasta).open(f);
        for (final SAMRecord rec : in) out.addAlignment(rec);
        CloserUtil.close(in);
    }

    out.close();
}
 
Example #7
Source File: CollectDuplicateMetricsTester.java    From picard with MIT License 6 votes vote down vote up
@Override
public File getOutput() {
    final File output;
    try {
        output = File.createTempFile("CollectDuplicateMetrics", ".sam");
    } catch (IOException e) {
        throw new PicardException("problems creating output file", e);
    }
    output.deleteOnExit();

    try(SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(samRecordSetBuilder.getHeader(), true, output.toPath(), this.fastaFiles.get(samRecordSetBuilder.getHeader()))) {
        samRecordSetBuilder.getRecords().forEach(a -> {
            final SAMRecord b = a.deepCopy();
            b.setDuplicateReadFlag(duplicateFlags.get(samRecordToDuplicatesFlagsKey(a)));
            writer.addAlignment(b);
        });
    }

    return output;
}
 
Example #8
Source File: SamUtilsTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void convertSamToBam(File outFile, File indexFile, File samFile) throws IOException {
  try (RecordIterator<SAMRecord> sam = new SkipInvalidRecordsIterator(samFile.getPath(), new SamClosedFileReader(samFile, null, null, SamUtils.getSingleHeader(samFile)))) {
    final SAMFileHeader header = sam.header().clone();
    header.setSortOrder(SAMFileHeader.SortOrder.coordinate);
    SamUtils.addProgramRecord(header);
    SamUtils.updateRunId(header);

    final SAMFileWriterFactory fact = new SAMFileWriterFactory();
    try (SAMFileWriter writer = fact.makeBAMWriter(header, true, outFile)) {
      while (sam.hasNext()) {
        final SAMRecord record = sam.next();
        try {
          writer.addAlignment(record);
        } catch (final IllegalArgumentException iae) {
          throw new NoTalkbackSlimException(iae.getMessage().replaceAll(Pattern.quote("SAMFileWriterImpl.addAlignment for "), ""));
        }
      }
    }
  }
  try {
    BamIndexer.saveBamIndex(outFile, indexFile);
  } catch (final UnindexableDataException e) {
    Diagnostic.warning("Cannot create BAM index: " + e.getMessage());
  }
}
 
Example #9
Source File: CollectGcBiasMetricsTest.java    From picard with MIT License 6 votes vote down vote up
public File build (final List<SAMRecordSetBuilder> setBuilder, final File unsortedSam, final SAMFileHeader header) throws IOException {
    final File sortedSam = VcfTestUtils.createTemporaryIndexedFile("CollectGcBias", ".bam");

    final SAMFileWriter writer = new SAMFileWriterFactory()
            .setCreateIndex(true).makeBAMWriter(header, false, unsortedSam);

    for (final SAMRecordSetBuilder subSetBuilder : setBuilder) {
        for (final SAMRecord record : subSetBuilder) {
            writer.addAlignment(record);
        }
    }
    writer.close();

    final SortSam sorter = new SortSam();
    final String[] args = new String[] {
            "INPUT=" + unsortedSam.getAbsolutePath(),
            "OUTPUT=" + sortedSam.getAbsolutePath(),
            "SORT_ORDER=coordinate"
    };

    sorter.instanceMain(args);

    return sortedSam;
}
 
Example #10
Source File: SdfStatistics.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
static void printSAMHeader(SequencesReader reader, final Appendable out, boolean specified) throws IOException {
  final ReferenceGenome rg = new ReferenceGenome(reader, ReferenceGenome.SEX_ALL, ReferenceGenome.ReferencePloidy.AUTO);
  final SAMFileHeader header = new SAMFileHeader();
  header.setSortOrder(SAMFileHeader.SortOrder.coordinate);
  SamUtils.addProgramRecord(header);
  final int[] lengths = reader.sequenceLengths(0, reader.numberSequences());
  for (int i = 0; i < lengths.length; ++i) {
    final String name = reader.hasNames() ? reader.name(i) : ("sequence_" + i);
    final ReferenceSequence s = specified ? rg.sequence(name) : null;
    if (s == null || s.isSpecified()) {
      final SAMSequenceRecord record = new SAMSequenceRecord(name, lengths[i]);
      header.addSequence(record);
    }
  }
  if (reader.getSdfId().available()) {
    header.addComment(SamUtils.TEMPLATE_SDF_ATTRIBUTE + reader.getSdfId());
  }
  final ByteArrayOutputStream bo = new ByteArrayOutputStream();
  new SAMFileWriterFactory().makeSAMWriter(header, true, bo).close();
  out.append(bo.toString());
}
 
Example #11
Source File: SamBamUtils.java    From chipster with MIT License 6 votes vote down vote up
public static void sortSamBam(File samBamFile, File sortedBamFile) {
	
	SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT);
	SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(samBamFile));
	SAMFileWriter writer = null;
	try {
		
		reader.getFileHeader().setSortOrder(SAMFileHeader.SortOrder.coordinate);
		writer = new SAMFileWriterFactory().makeBAMWriter(reader.getFileHeader(), false, sortedBamFile);
		Iterator<SAMRecord> iterator = reader.iterator();
		while (iterator.hasNext()) {
			writer.addAlignment(iterator.next());
		}
		
	} finally {
		closeIfPossible(reader);
		closeIfPossible(writer);
	}
}
 
Example #12
Source File: BamSlicerApplication.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
private static void sliceFromVCF(@NotNull CommandLine cmd) throws IOException {
    String inputPath = cmd.getOptionValue(INPUT);
    String vcfPath = cmd.getOptionValue(VCF);
    int proximity = Integer.parseInt(cmd.getOptionValue(PROXIMITY, "500"));

    SamReaderFactory readerFactory = createFromCommandLine(cmd);
    SamReader reader = readerFactory.open(new File(inputPath));

    QueryInterval[] intervals = getIntervalsFromVCF(vcfPath, reader.getFileHeader(), proximity);
    CloseableIterator<SAMRecord> iterator = reader.queryOverlapping(intervals);
    SAMFileWriter writer = new SAMFileWriterFactory().setCreateIndex(true)
            .makeBAMWriter(reader.getFileHeader(), true, new File(cmd.getOptionValue(OUTPUT)));

    writeToSlice(writer, iterator);

    writer.close();
    reader.close();
}
 
Example #13
Source File: DetectBeadSynthesisErrors.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
 * For each problematic cell, replace cell barcodes positions with N.
 * Take the replaced bases and prepend them to the UMI, and trim the last <X> bases off the end of the UMI.
 */
private void cleanBAM (final Map<String, BeadSynthesisErrorData> errorBarcodesWithPositions, final Map<String, String> intendedSequenceMap) {
	log.info("Cleaning BAM");
       final SamHeaderAndIterator headerAndIterator = SamFileMergeUtil.mergeInputs(INPUT, true);
	SamHeaderUtil.addPgRecord(headerAndIterator.header, this);

	SAMFileWriter writer= new SAMFileWriterFactory().setCreateIndex(CREATE_INDEX).makeSAMOrBAMWriter(headerAndIterator.header, true, OUTPUT);
	ProgressLogger pl = new ProgressLogger(log);
	for (SAMRecord r: new IterableAdapter<>(headerAndIterator.iterator)) {
		pl.record(r);
		r=padCellBarcodeFix(r, errorBarcodesWithPositions, intendedSequenceMap, this.CELL_BARCODE_TAG, this.MOLECULAR_BARCODE_TAG, this.EXTREME_BASE_RATIO);
		if (r!=null)
			writer.addAlignment(r);
	}
	CloserUtil.close(headerAndIterator.iterator);
	writer.close();
}
 
Example #14
Source File: DetectBeadSubstitutionErrors.java    From Drop-seq with MIT License 6 votes vote down vote up
private void repairBAM (final BottomUpCollapseResult result) {

		final SamHeaderAndIterator headerAndIterator = SamFileMergeUtil.mergeInputs(this.INPUT, true);
		SamHeaderUtil.addPgRecord(headerAndIterator.header, this);
		headerAndIterator.header.addComment("Bottom-up edit distance collapse tag " + this.CELL_BARCODE_TAG +" with edit distance " + this.EDIT_DISTANCE+ " filtering ambiguous neighbors=" + this.FILTER_AMBIGUOUS);
		SAMFileWriter writer= new SAMFileWriterFactory().setCreateIndex(CREATE_INDEX).makeSAMOrBAMWriter(headerAndIterator.header, true, OUTPUT);

		ProgressLogger pl = new ProgressLogger(log);
		log.info("Repairing BAM");
		for (SAMRecord r: new IterableAdapter<>(headerAndIterator.iterator)) {
			pl.record(r);
			r=repairBarcode(r, result);
			if (r!=null)
				writer.addAlignment(r);
		}
		CloserUtil.close(headerAndIterator.iterator);
		writer.close();
		log.info("Repair Complete");
	}
 
Example #15
Source File: CollapseBarcodesInPlace.java    From Drop-seq with MIT License 6 votes vote down vote up
public void processOnlyPrimary () {
       final SamHeaderAndIterator inputs = openInputs();
	CloseableIterator<SAMRecord> inputSam = inputs.iterator;
	SAMFileHeader header = inputs.header;
	header.addComment("Edit distance collapsed tag " +  this.PRIMARY_BARCODE + " to new tag " + this.OUT_BARCODE+ " with edit distance "+ this.EDIT_DISTANCE);
       SAMFileWriter writer= new SAMFileWriterFactory().makeSAMOrBAMWriter(header, true, this.OUTPUT);

	// gather up the barcodes that exist in the BAM
       final SamHeaderAndIterator inputs2 = openInputs();
	ObjectCounter<String> barcodes = new BamTagHistogram().getBamTagCounts(inputs2.iterator, this.PRIMARY_BARCODE,this.MINIMUM_MAPPING_QUALITY, this.FILTER_PCR_DUPLICATES);
       CloserUtil.close(inputs2.iterator);

	// filter barcodes by #reds in each barcode.
	barcodes=filterBarcodesByNumReads(barcodes, this.MIN_NUM_READS_NONCORE);

	// collapse them
	Map<String, String> childParentBarcodes=collapseBarcodes(this.MIN_NUM_READS_CORE, this.NUM_CORE_BARCODES, barcodes, this.FIND_INDELS, this.EDIT_DISTANCE);
	// iterate through the reads and retag with the proper reads.
	// log.info("STUFF");
	retagReads(inputSam, writer, childParentBarcodes, this.PRIMARY_BARCODE, this.OUT_BARCODE);
	// collapsed.size();

	CloserUtil.close(inputSam);
	writer.close();
}
 
Example #16
Source File: SamBamUtils.java    From chipster with MIT License 6 votes vote down vote up
public String printSamBam(InputStream samBamStream, int maxRecords) throws IOException {
	SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT);
	SAMFileReader in = new SAMFileReader(samBamStream);
	SAMFileHeader header = in.getFileHeader();
	ByteArrayOutputStream buffer = new ByteArrayOutputStream();
	SAMFileWriter out = new SAMFileWriterFactory().makeSAMWriter(header, true, buffer);
	int i = 0;
	try {
		for (final SAMRecord rec : in) {
			if (i > maxRecords) {
				break;
			}
			out.addAlignment(rec);
			i++;
		}
	} finally {
		closeIfPossible(out);
	}

	if (i > maxRecords) {
		buffer.write("SAM/BAM too long for viewing, truncated here!\n".getBytes());
	}
	
	return buffer.toString();
}
 
Example #17
Source File: SplitBamByCell.java    From Drop-seq with MIT License 6 votes vote down vote up
@Override
protected int doWork() {
    if (!OUTPUT.getPath().contains(OUTPUT_SLUG)) {
        throw new IllegalArgumentException(OUTPUT + " does not contain the replacement token " + OUTPUT_SLUG);
    }

    samWriterFactory = new SAMFileWriterFactory().setCreateIndex(CREATE_INDEX);

    Map<String, Integer> cellBarcodeWriterIdxMap = new HashMap<>();
    List<SAMFileInfo> writerInfoList = new ArrayList<>();

    splitBAMs(cellBarcodeWriterIdxMap, writerInfoList);

    if (OUTPUT_LIST != null) {
        writeOutputList(writerInfoList);
    }
    if (REPORT != null) {
        writeReport(writerInfoList);
    }

    return 0;
}
 
Example #18
Source File: TagReadWithGeneExonFunction.java    From Drop-seq with MIT License 5 votes vote down vote up
@Override
protected int doWork() {
    IOUtil.assertFileIsReadable(this.INPUT);
    IOUtil.assertFileIsReadable(this.ANNOTATIONS_FILE);
    if (this.SUMMARY!=null) IOUtil.assertFileIsWritable(this.SUMMARY);
    IOUtil.assertFileIsWritable(this.OUTPUT);

    SamReader inputSam = SamReaderFactory.makeDefault().open(INPUT);

    SAMFileHeader header = inputSam.getFileHeader();
    SamHeaderUtil.addPgRecord(header, this);
    SAMSequenceDictionary bamDict = header.getSequenceDictionary();

    final OverlapDetector<Gene> geneOverlapDetector = GeneAnnotationReader.loadAnnotationsFile(ANNOTATIONS_FILE, bamDict);
    SAMFileWriter writer= new SAMFileWriterFactory().makeSAMOrBAMWriter(header, true, OUTPUT);

    for (SAMRecord r: inputSam) {
        pl.record(r);

        if (!r.getReadUnmappedFlag())
            r=	setAnnotations(r, geneOverlapDetector);
        writer.addAlignment(r);
    }

    CloserUtil.close(inputSam);
    writer.close();
    if (this.USE_STRAND_INFO) log.info(this.metrics.toString());
    if (SUMMARY==null) return 0;

    //process summary
    MetricsFile<ReadTaggingMetric, Integer> outFile = new MetricsFile<>();
    outFile.addMetric(this.metrics);
    outFile.write(this.SUMMARY);
    return 0;

}
 
Example #19
Source File: BAMTestUtil.java    From Hadoop-BAM with MIT License 5 votes vote down vote up
public static File writeBamFileWithLargeHeader() throws IOException {
  SAMRecordSetBuilder samRecordSetBuilder =
      new SAMRecordSetBuilder(true, SAMFileHeader.SortOrder.queryname);
  for (int i = 0; i < 1000; i++) {
    int chr = 20;
    int start1 = (i + 1) * 1000;
    int start2 = start1 + 100;
    samRecordSetBuilder.addPair(String.format("test-read-%03d", i), chr, start1,
        start2);
  }

  final File bamFile = File.createTempFile("test", ".bam");
  bamFile.deleteOnExit();
  SAMFileHeader samHeader = samRecordSetBuilder.getHeader();
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < 1000000; i++) {
    sb.append("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
  }
  samHeader.addComment(sb.toString());
  final SAMFileWriter bamWriter = new SAMFileWriterFactory()
      .makeSAMOrBAMWriter(samHeader, true, bamFile);
  for (final SAMRecord rec : samRecordSetBuilder.getRecords()) {
    bamWriter.addAlignment(rec);
  }
  bamWriter.close();

  return bamFile;
}
 
Example #20
Source File: CollectHsMetricsTest.java    From picard with MIT License 5 votes vote down vote up
/** Writes the contents of a SAMRecordSetBuilder out to a file. */
private File writeBam(final SAMRecordSetBuilder builder, final File f) {
    try (final SAMFileWriter out = new SAMFileWriterFactory().makeSAMOrBAMWriter(builder.getHeader(), false, f)) {
        builder.forEach(out::addAlignment);
    }
    return f;
}
 
Example #21
Source File: SamBamUtils.java    From chipster with MIT License 5 votes vote down vote up
public void normaliseBam(File bamFile, File normalisedBamFile) {

		// Read in a BAM file and its header
		SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT);
		SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(bamFile));
		SAMFileWriter writer = null;
		try {
			SAMFileHeader normalisedHeader = reader.getFileHeader();

			// Alter the chromosome names in header's SAMSequenceDictionary
			SAMSequenceDictionary normalisedDictionary = new SAMSequenceDictionary();
			for (SAMSequenceRecord sequenceRecord : normalisedHeader.getSequenceDictionary().getSequences()) {

				// Normalise chromosome
				String sequenceName = chromosomeNormaliser.normaliseChromosome(sequenceRecord.getSequenceName());
				normalisedDictionary.addSequence(new SAMSequenceRecord(sequenceName, sequenceRecord.getSequenceLength()));
			}
			normalisedHeader.setSequenceDictionary(normalisedDictionary);

			// Write new BAM file with normalised chromosome names
			writer = new SAMFileWriterFactory().makeBAMWriter(normalisedHeader, true, normalisedBamFile);
			for (final SAMRecord rec : reader) {
				rec.setHeader(normalisedHeader);
				writer.addAlignment(rec);
			}
			
		} finally {
			closeIfPossible(reader);
			closeIfPossible(writer);
		}
	}
 
Example #22
Source File: MultiFastqOutputter.java    From cramtools with Apache License 2.0 5 votes vote down vote up
protected void kickedFromCache(FastqRead read) {
	if (writer == null) {
		log.info("Creating overflow BAM file.");
		headerForOverflowWriter = header.clone();
		headerForOverflowWriter.setSortOrder(SAMFileHeader.SortOrder.queryname);

		writer = new SAMFileWriterFactory().makeBAMWriter(headerForOverflowWriter, false, cacheOverFlowStream);
	}
	SAMRecord r = read.toSAMRecord(writer.getFileHeader());
	writer.addAlignment(r);
}
 
Example #23
Source File: Cram2Bam.java    From cramtools with Apache License 2.0 5 votes vote down vote up
private static SAMFileWriter createSAMFileWriter(Params params, CramHeader cramHeader,
		SAMFileWriterFactory samFileWriterFactory) throws IOException {
	/*
	 * building sam writer, sometimes we have to go deeper to get to the
	 * required functionality:
	 */
	SAMFileWriter writer = null;
	if (params.outputFastq) {
		if (params.cramURL == null) {
			writer = new FastqSAMFileWriter(System.out, null, cramHeader.getSamFileHeader());
		} else {
			writer = new FastqSAMFileWriter(Utils.getFileName(params.cramURL), false, cramHeader.getSamFileHeader());

		}
	} else if (params.outputFastqGz) {
		if (params.cramURL == null) {
			GZIPOutputStream gos = new GZIPOutputStream(System.out);
			PrintStream ps = new PrintStream(gos);
			writer = new FastqSAMFileWriter(ps, null, cramHeader.getSamFileHeader());
		} else {
			writer = new FastqSAMFileWriter(Utils.getFileName(params.cramURL), true, cramHeader.getSamFileHeader());

		}
	} else if (params.outputFile == null) {
		OutputStream os = new BufferedOutputStream(System.out);
		if (params.outputBAM) {
			writer = new SAMFileWriterFactory().makeBAMWriter(cramHeader.getSamFileHeader(), true, os);
		} else {
			writer = Utils.createSAMTextWriter(samFileWriterFactory, os, cramHeader.getSamFileHeader(),
					params.printSAMHeader);
		}
	} else {
		writer = samFileWriterFactory.makeSAMOrBAMWriter(cramHeader.getSamFileHeader(), true, params.outputFile);
	}
	return writer;
}
 
Example #24
Source File: TagReadWithInterval.java    From Drop-seq with MIT License 5 votes vote down vote up
@Override
protected int doWork() {
	IOUtil.assertFileIsReadable(INPUT);
	IOUtil.assertFileIsWritable(OUTPUT);
	SamReader inputSam = SamReaderFactory.makeDefault().open(INPUT);
	SAMFileHeader header = inputSam.getFileHeader();
       header.setSortOrder(SAMFileHeader.SortOrder.coordinate);
	SamHeaderUtil.addPgRecord(header, this);

	SAMFileWriter writer= new SAMFileWriterFactory().makeSAMOrBAMWriter(header, true, OUTPUT);

	IntervalList loci = IntervalList.fromFile(this.INTERVALS);
	OverlapDetector<Interval> od =getOverlapDetector (loci);
	ProgressLogger processLogger = new ProgressLogger(log);

	for (SAMRecord record: inputSam) {
		processLogger.record(record);

		if (record.getReadUnmappedFlag()==false)
			// use alignment blocks instead of start/end to properly deal with split reads mapped over exon/exon boundaries.
			record=tagRead(record, od);
		else
			record.setAttribute(this.TAG, null);
		writer.addAlignment(record);

	}

	CloserUtil.close(inputSam);
	writer.close();

	return(0);


}
 
Example #25
Source File: FilterBamByTag.java    From Drop-seq with MIT License 5 votes vote down vote up
@Override
protected int doWork() {
	if (TAG_VALUES_FILE == null && TAG_VALUE==null) {
		log.error("You must set either a file of tag values, or a single tag value.");
		return(1);
	}

	IOUtil.assertFileIsReadable(INPUT);
	IOUtil.assertFileIsWritable(OUTPUT);
	if (this.SUMMARY!=null) IOUtil.assertFileIsWritable(this.SUMMARY);
	Set<String> values;

	if (this.TAG_VALUES_FILE != null) {
		IOUtil.assertFileIsReadable(TAG_VALUES_FILE);
		values = readValues(this.TAG_VALUES_FILE);
	} else {
		values = new HashSet<>();
		if (this.TAG_VALUE!=null)
			values.addAll(this.TAG_VALUE);
	}

	SamReader in = SamReaderFactory.makeDefault().enable(SamReaderFactory.Option.EAGERLY_DECODE).open(INPUT);
	SAMFileWriter out = new SAMFileWriterFactory().makeSAMOrBAMWriter(
			in.getFileHeader(), true, OUTPUT);

	if (!this.PAIRED_MODE)
		processUnpairedMode(in, out, values, this.SUMMARY, MINIMUM_MAPPING_QUALITY, ALLOW_PARTIAL_MATCH);
	else
		processPairedMode(in, out, values, this.SUMMARY, MINIMUM_MAPPING_QUALITY, ALLOW_PARTIAL_MATCH);

	return 0;
}
 
Example #26
Source File: CollapseTagWithContext.java    From Drop-seq with MIT License 5 votes vote down vote up
private SAMFileWriter getWriter (final SamReader reader) {
	SAMFileHeader header = reader.getFileHeader();
	SamHeaderUtil.addPgRecord(header, this);
	String context = StringUtil.join(" ", this.CONTEXT_TAGS);
	header.addComment("Edit distance collapsed tag " +  this.COLLAPSE_TAG + " to new tag " + this.OUT_TAG+ " with edit distance "+ this.EDIT_DISTANCE + "using indels=" + this.FIND_INDELS + " in the context of tags [" + context + "]");
       SAMFileWriter writer= new SAMFileWriterFactory().makeSAMOrBAMWriter(header, false, this.OUTPUT);
       return writer;
}
 
Example #27
Source File: BamSlicerApplication.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
private static void sliceFromURLs(@NotNull URL indexUrl, @NotNull URL bamUrl, @NotNull CommandLine cmd) throws IOException {
    File indexFile = downloadIndex(indexUrl);
    indexFile.deleteOnExit();

    SamReader reader = createFromCommandLine(cmd).open(SamInputResource.of(bamUrl).index(indexFile));

    BAMIndex bamIndex;
    if (indexFile.getPath().contains(".crai")) {
        SeekableStream craiIndex = CRAIIndex.openCraiFileAsBaiStream(indexFile, reader.getFileHeader().getSequenceDictionary());
        bamIndex = new DiskBasedBAMFileIndex(craiIndex, reader.getFileHeader().getSequenceDictionary());
    } else {
        bamIndex = new DiskBasedBAMFileIndex(indexFile, reader.getFileHeader().getSequenceDictionary(), false);
    }

    Optional<Pair<QueryInterval[], BAMFileSpan>> queryIntervalsAndSpan = queryIntervalsAndSpan(reader, bamIndex, cmd);
    Optional<Chunk> unmappedChunk = getUnmappedChunk(bamIndex, HttpUtils.getHeaderField(bamUrl, "Content-Length"), cmd);
    List<Chunk> sliceChunks = sliceChunks(queryIntervalsAndSpan, unmappedChunk);
    SamReader cachingReader = createCachingReader(indexFile, bamUrl, cmd, sliceChunks);

    SAMFileWriter writer = new SAMFileWriterFactory().setCreateIndex(true)
            .makeBAMWriter(reader.getFileHeader(), true, new File(cmd.getOptionValue(OUTPUT)));

    queryIntervalsAndSpan.ifPresent(pair -> {
        LOGGER.info("Slicing bam on bed regions...");
        CloseableIterator<SAMRecord> bedIterator = getIterator(cachingReader, pair.getKey(), pair.getValue().toCoordinateArray());
        writeToSlice(writer, bedIterator);
        LOGGER.info("Done writing bed slices.");
    });

    unmappedChunk.ifPresent(chunk -> {
        LOGGER.info("Slicing unmapped reads...");
        CloseableIterator<SAMRecord> unmappedIterator = cachingReader.queryUnmapped();
        writeToSlice(writer, unmappedIterator);
        LOGGER.info("Done writing unmapped reads.");
    });

    reader.close();
    writer.close();
    cachingReader.close();
}
 
Example #28
Source File: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Converts a BAM file to string, useful for tests
 * @param bamFile the BAM file
 * @return string representation of BAM file
 * @throws IOException if an IO error occurs
 */
public static String bamToString(File bamFile) throws IOException {
  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (final SamReader reader = makeSamReader(bamFile)) {
    try (SAMFileWriter writer = new SAMFileWriterFactory().makeSAMWriter(reader.getFileHeader(), true, baos)) {
      for (SAMRecord r : reader) {
        writer.addAlignment(r);
      }
    }
  }
  return baos.toString();
}
 
Example #29
Source File: ExtractCli.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static void extractSamBam(File f, ReferenceRanges<String> regions, OutputStream out, boolean printHeader, boolean headerOnly) throws IOException {
  final SAMFileHeader header = SamUtils.getSingleHeader(f);
  try (final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMWriter(header, true, out, printHeader)) {
    if (!headerOnly) {
      try (RecordIterator<SAMRecord> samfr = new SkipInvalidRecordsIterator(f.getPath(), new SamClosedFileReader(f, regions, null, header))) {
        while (samfr.hasNext()) {
          writer.addAlignment(samfr.next());
        }
      }
    }
  }
}
 
Example #30
Source File: SmartSamWriterTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void test() throws IOException {
  final SAMRecord[] records = {
    createRecord(1300),
    createRecord(1400),
    createRecord(1500),
    createRecord(1600),
    createRecord(2000),
    createRecord(70, "B"),
    createRecord(71, "B"),
    createRecord(0, SAMRecord.NO_ALIGNMENT_REFERENCE_NAME),
    createRecord(0, SAMRecord.NO_ALIGNMENT_REFERENCE_NAME),
    createRecord(0, SAMRecord.NO_ALIGNMENT_REFERENCE_NAME)
  };
  final MemoryPrintStream mps = new MemoryPrintStream();
  final SmartSamWriter smartSamWriter = new SmartSamWriter(new SAMFileWriterFactory().makeSAMWriter(records[0].getHeader(), true, mps.outputStream()));

  smartSamWriter.addRecord(records[2]);
  smartSamWriter.addRecord(records[0]);
  smartSamWriter.addRecord(records[4]);
  smartSamWriter.addRecord(records[3]);
  smartSamWriter.addRecord(records[1]);
  smartSamWriter.addRecord(records[5]);
  smartSamWriter.addRecord(records[6]);
  smartSamWriter.addRecord(records[7]);
  smartSamWriter.addRecord(records[8]);
  smartSamWriter.addRecord(records[9]);
  smartSamWriter.close();
  final StringBuilder sb = new StringBuilder();
  sb.append(SamUtils.getHeaderAsString(records[0].getHeader()));
  for (SAMRecord r : records) {
    sb.append(r.getSAMString());
  }
  assertEquals(sb.toString(), mps.toString());
}