Extract Line By Line In *.Bam Files
2
4
Entering edit mode
13.7 years ago
User 4133 ▴ 150

Hello!

I'm working with a .bam file and I want extract line by line. I'm trying to use the following bash command:

for line in `samtools view filename.bam`
do
     something
done

but it doesn't works because the variable line will be a single field of .bam file. How can I assign at variable the entire line?

Thanks!

bam samtools • 7.8k views
ADD COMMENT
5
Entering edit mode
13.7 years ago

Try something like

samtools view file | while read LINE; do echo $LINE; done

(edit: a little warning here: this is untested, I don't have samtools)

ADD COMMENT
0
Entering edit mode

This would be my solution too, assuming that "samtools view" outputs a multi-line file.

ADD REPLY
0
Entering edit mode

Do you mean multi-line stdout? A file would need to be handled with cat file | ..

ADD REPLY
0
Entering edit mode

yeah, I meant stdout :-)

ADD REPLY
4
Entering edit mode
13.5 years ago
Stew ★ 1.4k

I agree with Michael that you can just pipe the output, depending what you want to do determines if you need the while loop or not.

You can just pipe the output into another tool, such as perl.

This example randomly samples the BAM file to give an output BAM file with half as many reads.

samtools view -h input.bam | perl -ne 'if (m/^@[HD|SQ|RG|PG|CO]/){print;next};
srand;print if rand() <=0.5' | samtools view -Sb - >output.bam

Here is another example that counts the number of reads on each chromosome

samtools view input.bam | cut -f 3 | sort |uniq -c
ADD COMMENT

Login before adding your answer.

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