Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-03T14:22:47.180Z
Answers.LastActivityDate: 2023-03-03T14:22:47.180Z
Answers.UserDisplayName: bricoletc
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-06T20:18:29.770Z
Answers.Text: 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!
Answers.UserDisplayName: d_kennetz
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-06T20:24:26.003Z
Answers.Text: just as a warning this can take much longer if the read name file is big.
Answers.UserDisplayName: Bioathlete
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-06T20:26:07.260Z
Answers.Text: 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).
Answers.UserDisplayName: d_kennetz
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-06T20:04:28.987Z
Answers.LastActivityDate: 2018-12-06T20:04:28.987Z
Answers.UserDisplayName: Bioathlete
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-06T21:45:44.083Z
Comments.Text: 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).
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-06T21:51:44.943Z
Comments.Text: 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!
Comments.UserDisplayName: d_kennetz
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-06T22:03:24.583Z
Comments.Text: Yeah, most of us use it very often until it bites us! :)
Comments.UserDisplayName: terdon
Tags: <sam><samtools><reads>
Title: Extracting all reads from bam file which match read IDs in another file
ThreadText: # 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 username_1, 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:
  
  ```sh
  samtools view -N read_names.txt in.bam > read_locs.sam
  
  ```
  
  ```sh
  ❯ samtools --version
  samtools 1.16.1
  Using htslib 1.16
  
  ```
  
  
  ---
  
  