Using seq command in terminal with Samtools view
1
1
Entering edit mode
7.1 years ago
NGS-Newbie ▴ 10

I am trying to create three separate bam files using Samtools View for three different regions of interest. I am using the following, but I don't get any output. Could somebody point out what is wrong? Regions_1 to 3 were used to generate bowtie indexes and the bam files.

$ for number in 'seq 1 3' ; do samtools view -bh ./Sample1_bowtie2.bam Region_${number} > ./Sample1_Amplicon${number}.bam

Any alternate suggestions?

Thank you all!

Samtools shell • 2.3k views
ADD COMMENT
1
Entering edit mode
7.1 years ago
for R in "chr1:10-20" "chr10:12-13" "chr20:14-15" 
do
   samtools view -b -o "${R//[\:\-]/_}.bam" input.bam "${R}" 
done
ADD COMMENT
1
Entering edit mode

Variation: If you have a lot of regions which you have in a file listed line by line e.g.: myregions.txt

chr1:10-20
chr10:12-13
chr20:14-15

you could use the following:

for R in `cat myregions.txt`
do
   samtools view -b -o "${R//[\:\-]/_}.bam" input.bam "${R}" 
done
ADD REPLY
0
Entering edit mode

Thanks so much guys!

I suppose I still am a novice in this...

I tried both -

for R in "Region_1" "Region_2" "Region_3" ; do samtools view -b -o "${R//[\:\-]/_}.bam Sample1_bowtie2.bam "${R}" ; done

for R in 'cat regions.txt' ; do samtools view -b -o "${R//[\:\-]/_}.bam Sample1_bowtie2.bam "${R}" ; done

However, both seems to not work. I am guessing I'm doing something wrong here - "${R//[\:-]/_}.bam ?

Your suggestions are greatly appreciated.

ADD REPLY
0
Entering edit mode

both seems to not work.

Try to be more specific when reporting something isn't working, that might help us to spot the issue.

At least one error is that you used incorrect quotes in my command, around cat myregions.txt there are backtick quotes " ` ".

You can check how your command would look like using a simple echo statement, e.g. for Pierre's command:

for R in "chr1:10-20" "chr10:12-13" "chr20:14-15" 
do
   echo samtools view -b -o "${R//[\:\-]/_}.bam" input.bam "${R}" 
done

And then check if the outputted command is as you would expect it. Easier for troubleshooting.

ADD REPLY
0
Entering edit mode

Thanks, WouterDeCoster!

For echo I get - -bash: syntax error near unexpected token `echo'

ADD REPLY
0
Entering edit mode

Also, with the commands Pierre and Wouter suggested, I see > with a blinking cursor next to it.

ADD REPLY
0
Entering edit mode

Are you properly adding newlines as in our code examples? They play a role here.
Alternatively, try the following:

for R in "chr1:10-20" "chr10:12-13" "chr20:14-15" ;
do
   echo samtools view -b -o "${R//[\:\-]/_}.bam" input.bam "${R}" ;
done

(Added semicolons)

ADD REPLY
0
Entering edit mode

Thank you, WouterDeCoster! I think it's a silly question. But, do I run these commands directly in terminal or I have to make a script? If it is a script, how do I invoke it in terminal?

ADD REPLY
0
Entering edit mode

You could do both, but there is no need to make a script. You can just run these commands in the terminal.

ADD REPLY

Login before adding your answer.

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