# How to convert multiple single-end bam files to fastq using samtools

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.

• 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 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
– gabt
Aug 18 at 10:23
• 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. Aug 18 at 10:32
• 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: unix.stackexchange.com/questions/80851/… unix.stackexchange.com/questions/80851/…
– gabt
Aug 18 at 10:39
• Yes, that is correct. Thank you, I truly appreciate the assistance. Aug 18 at 10:55
• 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.
– gabt
Aug 18 at 10:59

ls *.bam | parallel -j 2 "samtools fastq {} > {.}.fastq"

Use -j to control the number of parallel jobs. That only works for single-end data though as paired-end data must first be sorted or collated by name.