Run multiple times samtools and sed for a big number of bam files in folder
1
0
Entering edit mode
3.0 years ago
dzisis1986 ▴ 70

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 • 1.7k views
ADD COMMENT
1
Entering edit mode

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

ADD REPLY
2
Entering edit mode
3.0 years 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)

ADD COMMENT
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
ADD REPLY
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?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1571 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6