Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-14T15:45:47.297Z
Answers.LastActivityDate: 2018-05-15T08:38:57.963Z
Answers.LastEditDate: 2018-05-15T08:38:57.963Z
Answers.UserDisplayName: Konrad Rudolph
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-21T15:48:14.813Z
Answers.LastActivityDate: 2018-11-21T15:48:14.813Z
Answers.UserDisplayName: winni2k
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-14T14:42:41.210Z
Comments.Text: By `rID` are you talking about `RNAME` (reference sequence name) field? That's in the header.
Comments.UserDisplayName: Devon Ryan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-14T14:50:43.593Z
Comments.Text: rId is the current reference Id of the record and rNextId is the mate pair reference id.
Comments.UserDisplayName: Omair Iqbal
Tags: <reference-genome><sam>
Title: Number of reference sequences in a SAM file
ThreadText: # 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:
  
  1. `samtools idxstats ‹bamfile›`
  2. `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
  
  ```
  
  
  ---
  
  