Run multiple times samtools and sed for a big number of bam files in folder
1
0
Entering edit mode
6 weeks ago
dzisis1986 ▴ 30

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

samtools bash bam • 533 views
1
Entering edit mode

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

2
Entering edit mode
6 weeks ago

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)

0
Entering edit mode

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

0
Entering edit mode

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?

0
Entering edit mode

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

0
Entering edit mode

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

0
Entering edit mode

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