Answers.Body: <p>Assuming both the MD and CIGAR are present <strong>and correct</strong>, then yes, you can parse both to get the edit distance (<code>NM</code> auxiliary tag). One big caveat to this is that there's a reason that the <code>samtools calmd</code> command exists, since it's historically been the case that not all aligners have output correct MD strings. It's rare for the CIGAR string to be wrong and that'd be more of a catastrophic error on the part of an aligner. For what it's worth, if the <code>NM</code> auxiliary is absent on a given alignment but present on others produced by the same aligner then it's fair to assume <code>NM:i:0</code> for a given alignment by default (many aligners only produce <code>NM:i:XXX</code> if the edit distance is at least 1).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-14T06:43:46.623Z
Answers.LastActivityDate: 2017-06-14T06:43:46.623Z
Answers.UserDisplayName: Devon Ryan
Body: <p>From <a href="https://samtools.github.io/hts-specs/SAMtags.pdf" rel="noreferrer">SAM Optional Fields Specification</a> the NM field is </p>
  
  <blockquote>
    <p>Edit distance to the reference, including ambiguous bases but excluding clipping</p>
  </blockquote>
  
  <p>Assuming both the MD and CIGAR are present, is the edit distance simply the number of characters <code>[A-Z]</code> appearing in the MD field plus the number of bases inserted (<code>xI</code>, if any) from the CIGAR string? Are there any other complications? </p>
  
Tags: <file-formats><sam><cigar>
Title: Is the optional SAM NM field strictly computable from the MD and CIGAR?
ThreadText: # Is the optional SAM NM field strictly computable from the MD and CIGAR?
  
  (Asked by: username_0 on 2017-06-13)
  
  From [SAM Optional Fields Specification](https://samtools.github.io/hts-specs/SAMtags.pdf) the NM field is
  
  > Edit distance to the reference, including ambiguous bases but excluding clipping
  
  Assuming both the MD and CIGAR are present, is the edit distance simply the number of characters `[A-Z]` appearing in the MD field plus the number of bases inserted (`xI`, if any) from the CIGAR string? Are there any other complications?
  
  **Tags:** file-formats, sam, cigar
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-06-14. Score: 4 | Accepted answer
  
  Assuming both the MD and CIGAR are present **and correct**, then yes, you can parse both to get the edit distance (`NM` auxiliary tag). One big caveat to this is that there's a reason that the `samtools calmd` command exists, since it's historically been the case that not all aligners have output correct MD strings. It's rare for the CIGAR string to be wrong and that'd be more of a catastrophic error on the part of an aligner. For what it's worth, if the `NM` auxiliary is absent on a given alignment but present on others produced by the same aligner then it's fair to assume `NM:i:0` for a given alignment by default (many aligners only produce `NM:i:XXX` if the edit distance is at least 1).
  
  
  ---
  
  