<p>Assuming that you're looking for variants, this is likely to be an <a href="https://xyproblem.info/" rel="nofollow noreferrer">XY problem</a>. Large-scale variant detection is best done with BAM files and VCF files, not fastq files.</p>
<p>I have an answer about converting multiple BAM files into an aggregated VCF format here (as part of a workflow from the initial fastq files). Here's the meat of it:</p>
<pre><code>mkdir -p variants;
(for x in mapped/*.bam;
   do echo samtools mpileup -v -f /data/all/genomes/hsap/CHM13/chm13v2.0.fa &quot;<span class="math-container">${x}" \| \
   bcftools call --ploidy GRCh38 -v -m -O z -o variants/$</span>(basename &quot;${x}&quot; .bam).vcf.gz; done) &gt; call_jobs.txt
cat call_jobs.txt | parallel -u;
</code></pre>
<p>As a slow, but more accurate alternative, variants can be called for all samples at the same time:</p>
<pre><code>samtools mpileup -v -f /data/all/genomes/hsap/CHM13/chm13v2.0.fa mapped/*.bam | \
  bcftools call --ploidy GRCh38 -v -m -O z -o variants/hisat2_allCalled_vs_chm13.vcf.gz
</code></pre>
<p>If a faster processing is desired, then the variant calling can be parallelised across chromosomes using <code>parallel</code> and merged afterwards using <code>bcftools norm</code>. The implementation of this is left as an exercise to the reader.</p>

CC BY-SA 4.0
2024-07-12T21:50:13.423Z
2024-07-12T21:56:50.987Z
2024-07-12T21:56:50.987Z
gringer
<p><em>This question was also asked on <a href="https://www.biostars.org/p/9598851/" rel="nofollow noreferrer">Biostars</a></em></p>
<p>I want to find variants in the exome data. How can I convert all the bam files in a directory into fastq files using  samtools bam2fq?</p>
<p>I couldn't find simple code anywhere online for this example.</p>

CC BY-SA 4.0
2024-07-11T20:56:59.793Z
What have you tried already? Please add additional context around your question, and examples of what you have already tried. Any error messages you have encountered are really helpful in understanding your problem.
gringer
CC BY-SA 4.0
2024-07-12T03:22:13.627Z
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Community
CC BY-SA 4.0
2024-07-12T08:17:01.360Z
Are these bam files all from the same sample? Different ones? Same species? Same reference genome?
terdon
<sam><fastq><tools>
How can I convert all the bam files in a directory into fastq files using samtools bam2fq?
# How can I convert all the bam files in a directory into fastq files using samtools bam2fq?

(Asked by: username_0 on 2024-07-11)

*This question was also asked on [Biostars](https://www.biostars.org/p/9598851/)*

I want to find variants in the exome data. How can I convert all the bam files in a directory into fastq files using samtools bam2fq?

I couldn't find simple code anywhere online for this example.

**Tags:** sam, fastq, tools
> **Comments:**
> **username_1**: What have you tried already? Please add additional context around your question, and examples of what you have already tried. Any error messages you have encountered are really helpful in understanding your problem.  
> **username_2**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  
> **username_3**: Are these bam files all from the same sample? Different ones? Same species? Same reference genome?  

---

## 1 answers

### Answer by username_1 on 2024-07-12. Score: 0

Assuming that you're looking for variants, this is likely to be an XY problem. Large-scale variant detection is best done with BAM files and VCF files, not fastq files.
I have an answer about converting multiple BAM files into an aggregated VCF format here (as part of a workflow from the initial fastq files). Here's the meat of it:
mkdir -p variants;
(for x in mapped/*.bam;
   do echo samtools mpileup -v -f /data/all/genomes/hsap/CHM13/chm13v2.0.fa "${x}" \| \
   bcftools call --ploidy GRCh38 -v -m -O z -o variants/$(basename "${x}" .bam).vcf.gz; done) > call_jobs.txt
cat call_jobs.txt | parallel -u;

As a slow, but more accurate alternative, variants can be called for all samples at the same time:
samtools mpileup -v -f /data/all/genomes/hsap/CHM13/chm13v2.0.fa mapped/*.bam | \
  bcftools call --ploidy GRCh38 -v -m -O z -o variants/hisat2_allCalled_vs_chm13.vcf.gz

If a faster processing is desired, then the variant calling can be parallelised across chromosomes using parallel and merged afterwards using bcftools norm. The implementation of this is left as an exercise to the reader.


---

