<p>There are multiple ways. Here are two:</p>

<ol>
<li><code>samtools idxstats ‹bamfile›</code></li>
<li><code>samtools view -H ‹bamfile› | grep '^@SQ'</code></li>
</ol>

<p>Both commands give you a list of the references in the BAM file. To get their number, simply append <code>| wc -l</code>. But note that <code>idxstats</code> also outputs a final entry, <code>*</code>, which doesn’t correspond to any reference but to all unmapped reads. So subtract that from the number.</p>

<p>However, beware that while this lists all references associated with a BAM file, not every reference necessarily has records associated with it. So there might be no reads mapping to a given reference. This is the third column in the <code>idxstats</code> output. So, to get only those references with at least one mapped read, we can filter for those:</p>

<pre><code>samtools idxstats ‹bamfile› | awk '$4 != 0' | head -n-1 | wc -l
</code></pre>

<p>Oh, and these (and other) commands work on <strong>BAM</strong> files, not SAM. The <code>idxstats</code> command additionally needs the BAM index, since it’s actually reading information from that index file, not the BAM (this also means that it only works on sorted BAM files). Working on SAM would require looping through the whole file, which is quite inefficient. You should generally be working with BAM rather than SAM, so this is hopefully not a restriction.</p>

<p>To do the same on a SAM file, you need to iterate over the file:</p>

<pre><code>awk '{print $3}' ‹samfile› | sort -u | wc -l
</code></pre>

<p>Or:</p>

<pre><code>awk '{refs[$3] = 1} END {for (ref in refs) print ref}' ‹samfile› | wc -l
</code></pre>

CC BY-SA 4.0
2018-05-14T15:45:47.297Z
2018-05-15T08:38:57.963Z
2018-05-15T08:38:57.963Z
Konrad Rudolph
<p>A SAM file stores alignments to a set of reference sequences.  However, those reference sequences are not usually stored in the file. Instead, they will be in a separate sequence file that is usually in FASTA format. You can get the number of reference sequences stored in that file using the command</p>

<pre><code>grep -c '^&gt;' reference.fasta
</code></pre>

CC BY-SA 4.0
2018-11-21T15:48:14.813Z
2018-11-21T15:48:14.813Z
winni2k
<p>From a single record, I can get the reference sequence ID from the field rID, but how can I get the total number of different reference sequences stored in the whole SAM file?
It can be simple as just looping through all records, but I need some other solution.</p>

CC BY-SA 4.0
2018-05-14T14:42:41.210Z
By `rID` are you talking about `RNAME` (reference sequence name) field? That's in the header.
Devon Ryan
CC BY-SA 4.0
2018-05-14T14:50:43.593Z
rId is the current reference Id of the record and rNextId is the mate pair reference id.
Omair Iqbal
<reference-genome><sam>
Number of reference sequences in a SAM file
# Number of reference sequences in a SAM file

(Asked by: username_0 on 2018-05-14)

From a single record, I can get the reference sequence ID from the field rID, but how can I get the total number of different reference sequences stored in the whole SAM file?
It can be simple as just looping through all records, but I need some other solution.

**Tags:** reference-genome, sam
> **Comments:**
> **username_1**: By `rID` are you talking about `RNAME` (reference sequence name) field? That's in the header.  
> **username_0**: rId is the current reference Id of the record and rNextId is the mate pair reference id.  

---

## 2 answers

### Answer by username_2 on 2018-05-14. Score: 3

There are multiple ways. Here are two:

samtools idxstats ‹bamfile›
samtools view -H ‹bamfile› | grep '^@SQ'

Both commands give you a list of the references in the BAM file. To get their number, simply append | wc -l. But note that idxstats also outputs a final entry, *, which doesn’t correspond to any reference but to all unmapped reads. So subtract that from the number.
However, beware that while this lists all references associated with a BAM file, not every reference necessarily has records associated with it. So there might be no reads mapping to a given reference. This is the third column in the idxstats output. So, to get only those references with at least one mapped read, we can filter for those:
samtools idxstats ‹bamfile› | awk '$4 != 0' | head -n-1 | wc -l

Oh, and these (and other) commands work on BAM files, not SAM. The idxstats command additionally needs the BAM index, since it’s actually reading information from that index file, not the BAM (this also means that it only works on sorted BAM files). Working on SAM would require looping through the whole file, which is quite inefficient. You should generally be working with BAM rather than SAM, so this is hopefully not a restriction.
To do the same on a SAM file, you need to iterate over the file:
awk '{print $3}' ‹samfile› | sort -u | wc -l

Or:
awk '{refs[$3] = 1} END {for (ref in refs) print ref}' ‹samfile› | wc -l


---

### Answer by username_3 on 2018-11-21. Score: 1

A SAM file stores alignments to a set of reference sequences.  However, those reference sequences are not usually stored in the file. Instead, they will be in a separate sequence file that is usually in FASTA format. You can get the number of reference sequences stored in that file using the command
grep -c '^>' reference.fasta


---

