public static void calculateMdAndNmTags(final SAMRecord record, final byte[] ref, final boolean calcMD, final boolean calcNM) { SequenceUtil.calculateMdAndNmTags(record, ref, calcMD, calcNM); }
/** Calculates and sets the NM, MD, and and UQ tags from the record and the reference * * @param record the record to be fixed * @param refSeqWalker a ReferenceSequenceWalker that will be used to traverse the reference * @param isBisulfiteSequence a flag indicating whether the sequence came from bisulfite-sequencing which would imply a different * calculation of the NM tag. * * No return value, modifies the provided record. */ public static void fixNmMdAndUq(final SAMRecord record, final ReferenceSequenceFileWalker refSeqWalker, final boolean isBisulfiteSequence) { final byte[] referenceBases = refSeqWalker.get(record.getReferenceIndex()).getBases(); // only recalculate NM if it isn't bisulfite, since it needs to be treated specially below SequenceUtil.calculateMdAndNmTags(record, referenceBases, true, !isBisulfiteSequence); if (isBisulfiteSequence) { // recalculate the NM tag for bisulfite data record.setAttribute(SAMTag.NM.name(), SequenceUtil.calculateSamNmTag(record, referenceBases, 0, isBisulfiteSequence)); } fixUq(record, refSeqWalker, isBisulfiteSequence); }
/** Calculates and sets the NM, MD, and and UQ tags from the record and the reference * * @param record the record to be fixed * @param refSeqWalker a ReferenceSequenceWalker that will be used to traverse the reference * @param isBisulfiteSequence a flag indicating whether the sequence came from bisulfite-sequencing which would imply a different * calculation of the NM tag. * * No return value, modifies the provided record. */ public static void fixNmMdAndUq(final SAMRecord record, final ReferenceSequenceFileWalker refSeqWalker, final boolean isBisulfiteSequence) { final byte[] referenceBases = refSeqWalker.get(record.getReferenceIndex()).getBases(); // only recalculate NM if it isn't bisulfite, since it needs to be treated specially below SequenceUtil.calculateMdAndNmTags(record, referenceBases, true, !isBisulfiteSequence); if (isBisulfiteSequence) { // recalculate the NM tag for bisulfite data record.setAttribute(SAMTag.NM.name(), SequenceUtil.calculateSamNmTag(record, referenceBases, 0, isBisulfiteSequence)); } fixUq(record, refSeqWalker, isBisulfiteSequence); }
@Test public void testCalculateNmTag() { final File TEST_DIR = new File("src/test/resources/htsjdk/samtools/SequenceUtil"); final File referenceFile = new File(TEST_DIR, "reference_with_lower_and_uppercase.fasta"); final File samFile = new File(TEST_DIR, "upper_and_lowercase_read.sam"); SamReader reader = SamReaderFactory.makeDefault().open(samFile); ReferenceSequenceFile ref = ReferenceSequenceFileFactory.getReferenceSequenceFile(referenceFile); reader.iterator().stream().forEach(r -> { Integer nm = SequenceUtil.calculateSamNmTag(r, ref.getSequence(r.getContig()).getBases()); String md = r.getStringAttribute(SAMTag.MD.name()); Assert.assertEquals(r.getIntegerAttribute(SAMTag.NM.name()), nm, "problem with NM in read \'" + r.getReadName() + "\':"); SequenceUtil.calculateMdAndNmTags(r, ref.getSequence(r.getContig()).getBases(), true, true); Assert.assertEquals(r.getIntegerAttribute(SAMTag.NM.name()), nm, "problem with NM in read \'" + r.getReadName() + "\':"); if (md != null) { Assert.assertEquals(r.getStringAttribute(SAMTag.MD.name()), md, "problem with MD in read \'" + r.getReadName() + "\':"); } }); }
SequenceUtil.calculateMdAndNmTags(checkRecord, ref, true, true);