This is a test version of Biostars. For the public version, visit https://www.biostars.org.
convert multiple files simultaneously using samtools

Hello everyone, I am new to bioinformatics. I have several files in BAM format and I want to convert them to fastq using samtools.

Is there any way to convert them all at once?

I tried : samtools fastq * .bam > * .fastq but it was failure.

rna-seq samtools

1 answer

To process one after another:

for file in *.bam; do samtools fastq $file > ${file//.bam/.fastq}; done

If you want to run them simultaneously (let's say 8 at a time) you need to install the program parallel, then:

for file in *.bam; do echo samtools fastq $file ">" ${file//.bam/.fastq} >> tasks.txt; done
parallel -j 8 -a tasks.txt

try this with parallel: parallel --dry-run 'samtools fastq {} > {.}.fastq' ::: *.bam

Hi e.ortiz.v

Your answer is so helpful. I have a question that

if I have abc.bam and I would like to remove .bam and add nothing and get abs. after that, I will add abs to the string of is it ok to write it in this way

if I just remove and keep the file name

A= test_1.fq.gz -R "@RG\tID:1\tSM:${A[i]//_1.fq.gz}"

result is

-R "@RG\tID:1\tSM: test"

is it correct?

Hi E. Ortiz,

I would like to ask you, please, if would it be possible a similar code for simultaneously to convert many fastq files to respective bam files?. Could you write that code ?. I was trying the above code changing fastq by bam and vice versa, but it doesn't work for me.

Thank you very much.

Log in to answer this question.