Java Code Examples for htsjdk.samtools.SAMRecord#setHeader()
The following examples show how to use
htsjdk.samtools.SAMRecord#setHeader() .
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: ReplaceSamHeader.java From picard with MIT License | 6 votes |
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 2
Source File: SamBamUtils.java From chipster with MIT License | 5 votes |
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 3
Source File: ReorderSam.java From picard with MIT License | 4 votes |
/** * Helper function that writes reads from iterator it into writer out, updating each SAMRecord along the way * according to the newOrder mapping from dictionary index -> index. Name is used for printing only. */ private void writeReads(final SAMFileWriter out, final SAMRecordIterator it, final Map<Integer, Integer> newOrder, final String name) { long counter = 0; log.info(" Processing " + name); while (it.hasNext()) { counter++; final SAMRecord read = it.next(); final int oldRefIndex = read.getReferenceIndex(); final int oldMateIndex = read.getMateReferenceIndex(); final int newRefIndex = newOrderIndex(read, oldRefIndex, newOrder); read.setHeader(out.getFileHeader()); read.setReferenceIndex(newRefIndex); // read becoming unmapped if (oldRefIndex != NO_ALIGNMENT_REFERENCE_INDEX && newRefIndex == NO_ALIGNMENT_REFERENCE_INDEX) { read.setAlignmentStart(NO_ALIGNMENT_START); read.setReadUnmappedFlag(true); read.setCigarString(SAMRecord.NO_ALIGNMENT_CIGAR); read.setMappingQuality(SAMRecord.NO_MAPPING_QUALITY); } final int newMateIndex = newOrderIndex(read, oldMateIndex, newOrder); if (oldMateIndex != NO_ALIGNMENT_REFERENCE_INDEX && newMateIndex == NO_ALIGNMENT_REFERENCE_INDEX) { // mate becoming unmapped read.setMateAlignmentStart(NO_ALIGNMENT_START); read.setMateUnmappedFlag(true); read.setAttribute(SAMTag.MC.name(), null); // Set the Mate Cigar String to null } read.setMateReferenceIndex(newMateIndex); out.addAlignment(read); } it.close(); log.info("Wrote " + counter + " reads"); }
Example 4
Source File: SAMRecordWriter.java From Hadoop-BAM with MIT License | 4 votes |
protected void writeAlignment(final SAMRecord rec) { rec.setHeader(header); writer.writeAlignment(rec); }