<p>Hi I am trying to convert <code>bam</code> files generated from Ion Torrent Proton sequencing to <code>fastq</code> format so that I can upload them to KBase for analysis.</p>
<p>The files are named as follows:
<code>01_thu159_IX_RNA_001.bam 01_thu163_IX_RNA_001.bam 01_thu164_IX_RNA_001.bam 01_thu190_IX_RNA_001.bam</code></p>
<p>So I tried:</p>
<p><code>samtools fastq *.bam &gt; *.fastq</code></p>
<p>However, that did not work. But, I know it is possible to use a placeholder for the name in code by using something like <code>$f</code> or I have also seen <code>%N%</code>. I am just not sure how to basically use those placeholders to be able to quickly apply a <code>for</code> loop, which will convert the files to fastq format without having to do each one individually. I have 28 samples each containing 4 <code>bam</code> files. Any advice on how I can do this would be greatly appreciated.</p>

CC BY-SA 4.0
2021-08-18T10:23:15.483Z
I am not familiar with `samtools` however your example, which I believe is run through the terminal, is printing all the output into a single file and not into many, as you may think. I guess that by using `> *.fastq` you meant to create many fastq files, which is not what you're doing.
also I found this link that may help you https://onestopdataanalysis.com/bam-to-fastq/ and here it looks like they process one file at a time. if this is the case you just need a `for loop` to run the command for each file
gbt
CC BY-SA 4.0
2021-08-18T10:32:05.737Z
Hi there, yes I was running that command in the terminal. So the idea basically was I don't want to have to type the name of the individual files in each case. For example I would like to know how to write the ```for loop``` so that it converts the ```bam``` file to ```fastq``` format and maintains the same file name just the format is changed. Otherwise I will have to type out each name individually as such: ```samtools fastq 01_thu159_IX_RNA_001.bam > 01_thu159_IX_RNA_001.fastq``` and this is tedious considering how many files I have.
Justin1609
CC BY-SA 4.0
2021-08-18T10:39:39.737Z
ok, so your question actually was about how to run a command on multiple files at once, am I right? If yes, then here you can find some answers to that specific question:
https://unix.stackexchange.com/questions/80851/how-do-i-run-a-command-on-multiple-files
https://unix.stackexchange.com/questions/80851/how-do-i-run-a-command-on-multiple-files
gbt
CC BY-SA 4.0
2021-08-18T10:55:06.360Z
Yes, that is correct. Thank you, I truly appreciate the assistance.
Justin1609
CC BY-SA 4.0
2021-08-18T10:59:55.297Z
of course, if you are not able to write the necessary code, you can still ask a question regarding that specific point. However, I believe there should be enough information in the two answers I linked to help you.
gbt
<sam><samtools><fastq><format-conversion>
How to convert multiple single-end bam files to fastq using samtools
# How to convert multiple single-end bam files to fastq using samtools

(Asked by: username_0 on 2021-08-18)

Hi I am trying to convert `bam` files generated from Ion Torrent Proton sequencing to `fastq` format so that I can upload them to KBase for analysis.

The files are named as follows:
`01_thu159_IX_RNA_001.bam 01_thu163_IX_RNA_001.bam 01_thu164_IX_RNA_001.bam 01_thu190_IX_RNA_001.bam`

So I tried:

`samtools fastq *.bam > *.fastq`

However, that did not work. But, I know it is possible to use a placeholder for the name in code by using something like `$f` or I have also seen `%N%`. I am just not sure how to basically use those placeholders to be able to quickly apply a `for` loop, which will convert the files to fastq format without having to do each one individually. I have 28 samples each containing 4 `bam` files. Any advice on how I can do this would be greatly appreciated.

**Tags:** sam, samtools, fastq, format-conversion
> **Comments:**
> **username_1**: I am not familiar with `samtools` however your example, which I believe is run through the terminal, is printing all the output into a single file and not into many, as you may think. I guess that by using `> *.fastq` you meant to create many fastq files, which is not what you're doing. also I found this link that may help you https://onestopdataanalysis.com/bam-to-fastq/ and here it looks like they process one file at a time. if this is the case you just need a `for loop` to run the command for each file  
> **username_0**: Hi there, yes I was running that command in the terminal. So the idea basically was I don't want to have to type the name of the individual files in each case. For example I would like to know how to write the ```for loop``` so that it converts the ```bam``` file to ```fastq``` format and maintains the same file name just the format is changed. Otherwise I will have to type out each name individually as such: ```samtools fastq 01_thu159_IX_RNA_001.bam > 01_thu159_IX_RNA_001.fastq``` and this is tedious considering how many files I have.  
> **username_1**: ok, so your question actually was about how to run a command on multiple files at once, am I right? If yes, then here you can find some answers to that specific question: https://unix.stackexchange.com/questions/80851/how-do-i-run-a-command-on-multiple-files https://unix.stackexchange.com/questions/80851/how-do-i-run-a-command-on-multiple-files  
> **username_0**: Yes, that is correct. Thank you, I truly appreciate the assistance.  
> **username_1**: of course, if you are not able to write the necessary code, you can still ask a question regarding that specific point. However, I believe there should be enough information in the two answers I linked to help you.  

---

## 0 answers

