This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Run multiple times samtools and sed for a big number of bam files in folder

How can we execute the following commands with bash for a big amount of bam files in a folder

samtools view -H in.bam > header.sam
sed -i s/SN\:/SN\:chr/ header.sam
sed -i s/SN\:chrMT/SN\:chrM/ header.sam
samtools reheader header.sam in.bam > reheader_file.uns
samtools sort -o reheader_file.bam reheader_file.uns
samtools bash bam

random note: is the original bam already sorted? might not need to re-sort if so

1 answer

Avoiding to spoon-feed you the whole answer ;) :

bash for loop is the answer (for i in <list> ; do ... done ). Look up the exact syntax as well as how to pass the variable in the loop (and use it for your cmdlines)

I managed to do it like that, It is working but its a bit slow !

#!/bin/bash
for file in *_bwa_sortedCoord.bam ; do

F=$(echo $file | awk -F '_bwa_sortedCoord.bam' '{print $1}')
echo $F
F1=$F"_bwa_sortedCoord.bam"
echo $F1
F4=$F"_header.sam"
F6=$F"_reheader.uns"
F7=$F"_reheader.bam"

#transform bam to header sam 
samtools view -H $F1 > $F4
sed -i s/SN\:/SN\:chr/ $F4
sed -i s/SN\:chrMT/SN\:chrM/ $F4
samtools reheader $F4 $F1 > $F6
samtools sort -o $F7 $F6 

done < "/home/user/Desktop/"

exit

very well possible, but that has not much to do with the loop usage. Likely more due the command lines you actually execute.

If it is technically possible (==your computer or server is able) you can try to run parts of them multi-threaded?

Yes i am sure about that ! samtools takes to much time it needs threads definitely !!

it can I think, something weird-ish though, with an @ or such ? (can't recall the exact syntax now)

The sorting step is probably the slowest part, so multithreading it like you said with -@ would greatly speed it up.

Log in to answer this question.