Use samtools index on multiple bam files at the same time from the command line?
1
6
Entering edit mode
8.9 years ago
nash.claire ▴ 510

Hi all,

Simple question, is it possible to run samtools index (or any of the samtools in fact) on multiple bam files at the same time from the command line? I tried samtools index *.bam from within the directory with all more bam files stored and got the following error

Samtools-htslib-API: bam_index_build2() not yet implemented
Abort trap: 6

I also had a go at using ls *.bam | samtools index which also didn't work. To note, my bam files are all co-ordinate sorted. I went through the pain of doing that one by one.

It would be especially useful to be able to use samtools on multiple files when it comes to filtering on MAPQ scores etc etc.

Any ideas?

RNA-Seq next-gen sequence • 16k views
ADD COMMENT
0
Entering edit mode

Thanks Pierre I'll try it out. I already have GNU Parallel installed so I'll use that. And this should work for other samtools too?

And can I ask a naive newbie question? What is the '{}' doing specifically? What does it mean? I saw this in other posts and wasn't sure.

ADD REPLY
0
Entering edit mode

'{}' will be replaced by the filename.

ADD REPLY
0
Entering edit mode

And sorry to bug you again but I just tried the following command :-

$ ls *.bam | parallel samtools view -b -q 23 '{}'

in an effort to try out Parallel with a different samtool (I want to filter multiple bam files based on mapq score) and this happened and scared me....

I got reams and reams of stuff like this before terminal eventually stopped responding (I think it crashed)

2??E??4,%e???
?v????C?d?8.??e??d/?f?j???e??ʫ?W?/j    ?????Cy??%??)Ǫi%zp??H???"??x???Q??FڌTb?b?q??Ф???q#Ӳ?I?Ua?,%?
                                                                                                    gF?s??#6??6???<8T????u?Gđ?@?/?i?X??3??lS?&
                                                                                                                                              ??'y??1ȱ?w?G?j?XM?"*,t?GQ3ZT??h??#)
       ??;?kG?f??v:???N6@5?f???`?
?.Lم?5?6_??VWYQk?^???%????5
                           ???Z?.?f??XV??F??{jr(?c??=/!G?D??-HV??\??(?v??b?_?go??????<?,I?e?|?]p?9V?qN?h??O?{[???|x?ˬ<e?l;l󞄭?R??Bz94l???:?>???*?]*?٪?F
ub????o?A?M~?<E?????H:H?0?3?)?"y??s89מ3O^A2?>=?w?4?>)?5S}?ѓ???+.?QP.????k?Q??dgV?+?O]??F???@?K]?L?B̓fs?h?,M"??T8PTZ???

I'm guessing that ain't normal??

ADD REPLY
0
Entering edit mode

because samtools view (not index as you asked) ouput bam to stdout. See the parallel manual for '{.}' and '{/}' etc...

ADD REPLY
19
Entering edit mode
8.9 years ago

use XARGS

ls *.bam | xargs -n1 -P5 samtools index

or GNU parallel:

ls *.bam | parallel samtools index '{}'

UPDATE 2023:

http://www.htslib.org/doc/samtools-index.html

I think samtools index now supports the following syntax:

samtools index -M *.bam
ADD COMMENT
1
Entering edit mode

When doing samtools index *.bam an error occurs:

samtools index *.bam

samtools index: use -M to enable indexing more than one alignment file

Hence the working command working for me is:

samtools index *.bam -M

ADD REPLY
0
Entering edit mode

ls *.bam | xargs -n1 -P5 samtools index works in my hands, but what are the meanings of -n1 and -P5? Thank you very much!

ADD REPLY

Login before adding your answer.

Traffic: 1913 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