<p>Those numbers are not arbitrarily picked (well... maybe 255/60/40 is arbitrarily picked).</p>

<p>To convert from log10 Q values like these (also used for error rates in FASTQ files) to probabilities, divide the number by 10, negate it, then raise 10 to the power of the result.</p>

<p>Another way of looking at it is to consider the decade to mean the number of 9s in the [mapping] accuracy, e.g.:</p>

<p>Q10: 90%<br>
Q20: 99%<br>
Q30: 99.9%<br>
Q40: 99.99%</p>

<p>With the formula adjusted to make all the numbers in between a smooth transition.</p>

<p>With this transformation, a MAPQ score of 3 corresponds to a probability of an incorrect mapping of almost exactly 50%, which is as confident as you can be about a specific mapping if you know that it maps equally well to two locations:</p>

<pre><code>&gt; 10^-0.3
[1] 0.5011872
</code></pre>

<p>A MAPQ score of 2 is an incorrect mapping probability of 63% (i.e. close to 3 different locations), and a MAPQ of 1 is an incorrect mapping probability of 79% (close to 5 different locations). These fit with the numbers that you have described.</p>

<p>However, as you point out, BWA doesn't do this; it seems to ignore the number of mappable locations when considering the MAPQ score. I explore the reason why this is a problem in <a href="https://bioinformatics.stackexchange.com/questions/508/obtaining-uniquely-mapped-reads-from-bwa-mem-alignment/519#519">my question about multi-mapped reads</a>.</p>

<p>I notice that there's a bit of an attempt to answer this in a <a href="https://bioinformatics.stackexchange.com/questions/289/how-does-the-bwa-mem-algorithm-assign-its-mapping-qualities">similar previous question</a>:</p>

<blockquote>
  <p><span class="math-container">$C * (s_1 - s_2) / s_1,$</span></p>
  
  <p>where <span class="math-container">$s_1$</span> and <span class="math-container">$s_2$</span> denoted the alignment scores of two best alignments and <code>C</code> was some constant.</p>
</blockquote>

<p>See the answer by user172818 for more insight into the details of BWA-MEM MAPQ scores.</p>

CC BY-SA 3.0
2017-08-31T21:09:04.043Z
"Similar" question is generous, that's basically a duplicate of my question that I didn't do due diligence to find!
ijoseph
CC BY-SA 3.0
2017-08-31T21:09:53.377Z
So it basically comes down to understanding this uncommented `C` code. Maybe I'll try contacting Heng Li directly. https://github.com/lh3/bwa/blob/master/bwamem.c#L952-L976
ijoseph
CC BY-SA 4.0
2017-08-31T05:45:46.403Z
2020-01-04T04:21:25.790Z
2020-01-04T04:21:25.790Z
gringer
<p>First of all, if you want to understand mapping quality (mapQ), ignore RNA-seq mappers. They often produce misleading mapQ because mapQ is not important to RNA-seq anyway.</p>

<p>Strictly speaking, you have two questions, one in the title: the meaning of mapQ; and the other in a comment: how mapQ is computed. On the meaning, mapQ is nearly the same as baseQ – the phred scaled probability of the alignment/base being wrong. It often amuses me that we question mapQ but take baseQ for granted. BaseQ is also scaled and discretized differently; even fewer people know how Illumina/pacbio/nanopore/historical sequencers estimates baseQ.</p>

<p>On the second question, Section 2 of the <a href="http://genome.cshlp.org/content/suppl/2008/09/26/gr.078212.108.DC1/maq-supp.pdf" rel="noreferrer">MAQ supplementary</a> explains the theoretical aspects of mapQ, which is still correct today. Briefly, mapping quality consists of three components: 1) the probability of contamination; 2) the effect of mapping heuristics and 3) the error due to the repetitiveness of the reference. Only 3) can be modeled theoretically.</p>

<p>In case of bwa-mem, if we assume the matching score is 1, type-3 error is estimated with:</p>

<p>$$10/\log10\cdot[\log4\cdot(S_1-S_2)-\log n_{\rm sub}]$$</p>

<p>where $S_1$ is the best alignment score, $S_2$ is the second best and $n_{\rm sub}$ is the number of suboptimal alignments. Factor $\log 4$ comes from the scoring matrix. Factor $10/\log10$ is the Phred scale. This equation assumes gap-free alignment and is very close to Section 2.5.2 in the MAQ supplementary. It is ok-ish for short reads, but often overestimates for long reads. I am not aware of a practical approach in general cases. In addition to this method, you can estimate mapQ by read simulation: just try to find a function such that it fits empirical mapQ. Some have tried machine learning, too.</p>

CC BY-SA 3.0
2017-09-06T14:20:36.260Z
This has an ambiguous definition, at least because it doesn't define what happens when there are two equally similar and optimal matches in different parts of the reference index. If "second best" is allowed to have the same alignment score as "best", then this score reduces to $$10/\log10\cdot[-\log n_{\rm sub}]$$ in such a case.
gringer
CC BY-SA 3.0
2017-09-06T17:14:33.907Z
Beautiful. Do you have a reference for the type-3 error estimation expression for `bwa-mem`?
ijoseph
CC BY-SA 3.0
2017-09-07T12:02:22.253Z
@gringer Read the MAQ supplementary. It is an approximate on the condition that $S_1\gg S_2$. Furthermore, you do need to consider type-2 errors and pairing. The real estimate is full of ad hoc. ijoseph, that would the MAQ paper.
user172818
CC BY-SA 3.0
2017-09-06T03:13:46.477Z
2017-09-06T03:13:46.477Z
user172818
<p>Does anyone know what the <code>MAPQ</code> values produced by <a href="http://bio-bwa.sourceforge.net/" rel="noreferrer"><code>BWA-MEM</code></a> mean? </p>

<p>I'm looking for something similar to what <a href="http://www.acgt.me/blog/2015/3/17/more-madness-with-mapq-scores-aka-why-bioinformaticians-hate-poor-and-incomplete-software-documentation" rel="noreferrer">Keith Bradnam discovered for Tophat v 1.4.1</a>, where he realized that:</p>

<blockquote>
  <p>0 = maps to 5 or more locations </p>
  
  <p>1 = maps to 3-4 locations </p>
  
  <p>3 = maps to
  2 locations </p>
  
  <p>255 = unique mapping</p>
</blockquote>

<p>I'm familiar with the notion that MAPQ should be theoretically be related to the probability of an "incorrect" alignment (10^(-<code>MAPQ</code>/10)), although this is vaguely-specified enough that aligners in practice tend to actually just use something like the above. </p>

<p>Note that this is different than the <code>BWA</code> <code>MAPQ</code> scoring interpretation, because <code>BWA-MEM</code> gives <code>MAPQ</code> scores in the range $[0,60]$, rather than $[0,37]$ as has <a href="http://www.acgt.me/blog/2014/12/16/understanding-mapq-scores-in-sam-files-does-37-42" rel="noreferrer">been established for <code>BWA</code></a>.</p>

<phylogenetics><read-mapping><bwa>
Meaning of BWA-MEM MAPQ scores
# Meaning of BWA-MEM MAPQ scores

(Asked by: username_0 on 2017-08-30)

Does anyone know what the `MAPQ` values produced by [`BWA-MEM`](http://bio-bwa.sourceforge.net/) mean?

I'm looking for something similar to what [Keith Bradnam discovered for Tophat v 1.4.1](http://www.acgt.me/blog/2015/3/17/more-madness-with-mapq-scores-aka-why-bioinformaticians-hate-poor-and-incomplete-software-documentation), where he realized that:

> 0 = maps to 5 or more locations
>
> 1 = maps to 3-4 locations
>
> 3 = maps to
> 2 locations
>
> 255 = unique mapping

I'm familiar with the notion that MAPQ should be theoretically be related to the probability of an "incorrect" alignment (10^(-`MAPQ`/10)), although this is vaguely-specified enough that aligners in practice tend to actually just use something like the above.

Note that this is different than the `BWA` `MAPQ` scoring interpretation, because `BWA-MEM` gives `MAPQ` scores in the range $[0,60]$, rather than $[0,37]$ as has [been established for `BWA`](http://www.acgt.me/blog/2014/12/16/understanding-mapq-scores-in-sam-files-does-37-42).

**Tags:** phylogenetics, read-mapping, bwa

---

## 2 answers

### Answer by username_2 on 2017-09-06. Score: 16 | Accepted answer

First of all, if you want to understand mapping quality (mapQ), ignore RNA-seq mappers. They often produce misleading mapQ because mapQ is not important to RNA-seq anyway.

Strictly speaking, you have two questions, one in the title: the meaning of mapQ; and the other in a comment: how mapQ is computed. On the meaning, mapQ is nearly the same as baseQ – the phred scaled probability of the alignment/base being wrong. It often amuses me that we question mapQ but take baseQ for granted. BaseQ is also scaled and discretized differently; even fewer people know how Illumina/pacbio/nanopore/historical sequencers estimates baseQ.

On the second question, Section 2 of the [MAQ supplementary](http://genome.cshlp.org/content/suppl/2008/09/26/gr.078212.108.DC1/maq-supp.pdf) explains the theoretical aspects of mapQ, which is still correct today. Briefly, mapping quality consists of three components: 1) the probability of contamination; 2) the effect of mapping heuristics and 3) the error due to the repetitiveness of the reference. Only 3) can be modeled theoretically.

In case of bwa-mem, if we assume the matching score is 1, type-3 error is estimated with:

$$10/\log10\cdot[\log4\cdot(S\_1-S\_2)-\log n\_{\rm sub}]$$

where $S\_1$ is the best alignment score, $S\_2$ is the second best and $n\_{\rm sub}$ is the number of suboptimal alignments. Factor $\log 4$ comes from the scoring matrix. Factor $10/\log10$ is the Phred scale. This equation assumes gap-free alignment and is very close to Section 2.5.2 in the MAQ supplementary. It is ok-ish for short reads, but often overestimates for long reads. I am not aware of a practical approach in general cases. In addition to this method, you can estimate mapQ by read simulation: just try to find a function such that it fits empirical mapQ. Some have tried machine learning, too.

> **Comments:**
> **username_1**: This has an ambiguous definition, at least because it doesn't define what happens when there are two equally similar and optimal matches in different parts of the reference index. If "second best" is allowed to have the same alignment score as "best", then this score reduces to $$10/\log10\cdot[-\log n_{\rm sub}]$$ in such a case.  
> **username_0**: Beautiful. Do you have a reference for the type-3 error estimation expression for `bwa-mem`?  
> **username_2**: @gringer Read the MAQ supplementary. It is an approximate on the condition that $S_1\gg S_2$. Furthermore, you do need to consider type-2 errors and pairing. The real estimate is full of ad hoc. ijoseph, that would the MAQ paper.  

---

### Answer by username_1 on 2017-08-31. Score: 7

Those numbers are not arbitrarily picked (well... maybe 255/60/40 is arbitrarily picked).
To convert from log10 Q values like these (also used for error rates in FASTQ files) to probabilities, divide the number by 10, negate it, then raise 10 to the power of the result.
Another way of looking at it is to consider the decade to mean the number of 9s in the [mapping] accuracy, e.g.:
Q10: 90%
Q20: 99%
Q30: 99.9%
Q40: 99.99%
With the formula adjusted to make all the numbers in between a smooth transition.
With this transformation, a MAPQ score of 3 corresponds to a probability of an incorrect mapping of almost exactly 50%, which is as confident as you can be about a specific mapping if you know that it maps equally well to two locations:
> 10^-0.3
[1] 0.5011872

A MAPQ score of 2 is an incorrect mapping probability of 63% (i.e. close to 3 different locations), and a MAPQ of 1 is an incorrect mapping probability of 79% (close to 5 different locations). These fit with the numbers that you have described.
However, as you point out, BWA doesn't do this; it seems to ignore the number of mappable locations when considering the MAPQ score. I explore the reason why this is a problem in my question about multi-mapped reads.
I notice that there's a bit of an attempt to answer this in a similar previous question:

$C * (s_1 - s_2) / s_1,$
where $s_1$ and $s_2$ denoted the alignment scores of two best alignments and C was some constant.

See the answer by user172818 for more insight into the details of BWA-MEM MAPQ scores.

> **Comments:**
> **username_0**: "Similar" question is generous, that's basically a duplicate of my question that I didn't do due diligence to find!  
> **username_0**: So it basically comes down to understanding this uncommented `C` code. Maybe I'll try contacting Heng Li directly. https://github.com/lh3/bwa/blob/master/bwamem.c#L952-L976  

---

