text: user3669: 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?
  
  user15257: 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
  
  
  user964: 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
  
original_text: 3669: <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>
   15257: <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>
   964: <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>
  