<p><code>samtools</code> can do this natively too using <code>-N, --qname-file</code> CLI option:</p>
<pre class="lang-sh prettyprint-override"><code>samtools view -N read_names.txt in.bam &gt; read_locs.sam
</code></pre>
<pre class="lang-sh prettyprint-override"><code>❯ samtools --version
samtools 1.16.1
Using htslib 1.16
</code></pre>

CC BY-SA 4.0
2023-03-03T14:22:47.180Z
2023-03-03T14:22:47.180Z
bricoletc
<p>It is still slow but grep has a <code>-f</code> option to take in a file</p>

<p><code>samtools view inbam.bam | grep -f read_names.txt &gt; read_locs.txt</code></p>

CC BY-SA 4.0
2018-12-06T20:18:29.770Z
This took 30 seconds as opposed to several hours, so I'd say the overhead was definitely in samtools view. I feel silly because of how simple this was. Thank you!
d_kennetz
CC BY-SA 4.0
2018-12-06T20:24:26.003Z
just as a warning this can take much longer if the read name file is big.
Bioathlete
CC BY-SA 4.0
2018-12-06T20:26:07.260Z
I appreciate the heads up! Comparatively, I do not think it will ever take longer than the method I was using so it is still a great answer. (This is serving as a caveat to your warning).
d_kennetz
CC BY-SA 4.0
2018-12-06T20:04:28.987Z
2018-12-06T20:04:28.987Z
Bioathlete
<p>I have a long list of read IDs of interest to me in a file called read_names.txt. it is simply in the format:</p>

<pre><code>m54197_180831_211346/4981510/ccs
m54197_180831_211346/6226723/ccs
m54197_180831_211346/6619607/ccs
...
</code></pre>

<p>etc where these are the actual read names from a fastq file. I am then trying to find those in a bam file for which the fastq has been mapped, and I can accomplish this like:</p>

<pre><code>for ID in `cat read_names.txt`
do
samtools view inbam.bam | grep $ID &gt;&gt; read_locs.sam
done
</code></pre>

<p>However, this method is obscenely slow because it is rerunning samtools view for every ID iteration (several hours now for 600 read IDs), and I was hoping to do this for several <code>read_names.txt</code> files. </p>

<p>I tried sort of flipping the script a bit and running samtools view first but it only returned the first read ID present in the file and stopped:</p>

<pre><code>samtools view inbam.bam | for ID in `cat read_names.txt`; do grep $ID &gt;&gt; read_locs.txt; done
</code></pre>

<p>Am I missing something or is this the only way to accomplish this task?</p>

CC BY-SA 4.0
2018-12-06T21:45:44.083Z
Just as a general rule _never_ do `for var in $(cat file)`. Also known as [bash pitfall number 1](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29).
terdon
CC BY-SA 4.0
2018-12-06T21:51:44.943Z
thanks terdon, I will keep this in mind and find the better ways to handle this in the future. I use this method quite often so that is great for me to know!
d_kennetz
CC BY-SA 4.0
2018-12-06T22:03:24.583Z
Yeah, most of us use it very often until it bites us! :)
terdon
<sam><samtools><reads>
Extracting all reads from bam file which match read IDs in another file
# Extracting all reads from bam file which match read IDs in another file

(Asked by: username_0 on 2018-12-06)

I have a long list of read IDs of interest to me in a file called read_names.txt. it is simply in the format:
m54197_180831_211346/4981510/ccs
m54197_180831_211346/6226723/ccs
m54197_180831_211346/6619607/ccs
...

etc where these are the actual read names from a fastq file. I am then trying to find those in a bam file for which the fastq has been mapped, and I can accomplish this like:
for ID in `cat read_names.txt`
do
samtools view inbam.bam | grep $ID >> read_locs.sam
done

However, this method is obscenely slow because it is rerunning samtools view for every ID iteration (several hours now for 600 read IDs), and I was hoping to do this for several read_names.txt files. 
I tried sort of flipping the script a bit and running samtools view first but it only returned the first read ID present in the file and stopped:
samtools view inbam.bam | for ID in `cat read_names.txt`; do grep $ID >> read_locs.txt; done

Am I missing something or is this the only way to accomplish this task?

**Tags:** sam, samtools, reads
> **Comments:**
> **username_1**: Just as a general rule _never_ do `for var in $(cat file)`. Also known as [bash pitfall number 1](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29).  
> **username_0**: thanks terdon, I will keep this in mind and find the better ways to handle this in the future. I use this method quite often so that is great for me to know!  
> **username_1**: Yeah, most of us use it very often until it bites us! :)  

---

## 2 answers

### Answer by username_3 on 2018-12-06. Score: 9 | Accepted answer

It is still slow but grep has a `-f` option to take in a file

`samtools view inbam.bam | grep -f read_names.txt > read_locs.txt`

> **Comments:**
> **username_0**: This took 30 seconds as opposed to several hours, so I'd say the overhead was definitely in samtools view. I feel silly because of how simple this was. Thank you!  
> **username_3**: just as a warning this can take much longer if the read name file is big.  
> **username_0**: I appreciate the heads up! Comparatively, I do not think it will ever take longer than the method I was using so it is still a great answer. (This is serving as a caveat to your warning).  

---

### Answer by username_2 on 2023-03-03. Score: 2

samtools can do this natively too using -N, --qname-file CLI option:
samtools view -N read_names.txt in.bam > read_locs.sam

❯ samtools --version
samtools 1.16.1
Using htslib 1.16


---

