SEQanswers

Go Back   SEQanswers > Bioinformatics > Bioinformatics



Similar Threads
Thread Thread Starter Forum Replies Last Post
samtools mpileup multiple bam files lizzi Bioinformatics 9 03-09-2016 05:40 AM
How does Pindel handle multiple bam files nasreddin Bioinformatics 1 08-05-2014 06:08 AM
Viewing multiple BAM files in Artemis tospo Bioinformatics 6 12-17-2013 11:04 PM
Merging multiple BAM files unibegenomics Bioinformatics 1 08-25-2011 02:03 AM
Calling multiple BAM files for SNPs and vcf newbietonextgen Bioinformatics 3 04-19-2011 11:29 AM

Reply
 
Thread Tools
Old 09-15-2015, 08:08 AM   #1
cmccabe
Senior Member
 
Location: chicago

Join Date: Jul 2012
Posts: 351
Default multiple bam files one command

I have multiple .bam files in a directory that I would like to run the following command on:

Code:
samtools view -H Input.bam | sed '/^@PG/d' | samtools reheader - Input.bam > Input_newheader.bam
The command works great for one file, but I am trying to use that command on all .bam file in a directory (/home/cmccabe/Desktop/NGS).

to do multiple? or is there a better way? Thank you .

Code:
find *bam | parallel 'samtools view -H Input.bam | sed '/^@PG/d' | samtools reheader - Input.bam > Input_newheader.bam'
cmccabe is offline   Reply With Quote
Old 09-15-2015, 10:25 AM   #2
dpryan
Devon Ryan
 
Location: Freiburg, Germany

Join Date: Jul 2011
Posts: 3,480
Default

Code:
for f in *.bam
do
prefix=${f%%.bam}
samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > ${prefix}_newheader.bam
done
This is going to quickly become IO bound, so you're unlikely to see much benefit from parallel. BTW, to do this with parallel, the simplest method is to just write a shell script that takes a single file as input and use that with parallel.
dpryan is offline   Reply With Quote
Old 09-15-2015, 10:46 AM   #3
cmccabe
Senior Member
 
Location: chicago

Join Date: Jul 2012
Posts: 351
Default

If all the bam files are stored on a separate drive (/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215), and the output gets re-directed to (/home/cmccabe/Desktop/NGS/pool_I_090215)will the below work?

Code:
cd "/home/cmccabe/Desktop/NGS" -- path to samtools

for f in *.bam
do
prefix=${f%%.bam}
samtools view -H /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/$f | sed '/^@PG/d' | samtools reheader - /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/$f > /home/cmccabe/Desktop/NGS/pool_I_090215/${prefix}_newheader.bam
done
Thank you
cmccabe is offline   Reply With Quote
Old 09-15-2015, 10:55 AM   #4
dpryan
Devon Ryan
 
Location: Freiburg, Germany

Join Date: Jul 2011
Posts: 3,480
Default

"*.bam" is looking in the current working directory, so no, that won't work. If you said "/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam" then note that you would need to do something like:
Code:
bname=`basename $f`
pref=${bname%%.bam}
That would strip the path to the file appropriately. You would also then just use $f instead of /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/$f. For why that's the case, run:

Code:
for f in /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam
do
echo $f
done
dpryan is offline   Reply With Quote
Old 09-15-2015, 11:08 AM   #5
cmccabe
Senior Member
 
Location: chicago

Join Date: Jul 2012
Posts: 351
Default

Makes sense, so I tried: the bold is the output so why is it looking for those files? Thank you for your help.

Code:
cmccabe@HPZ640:~/Desktop/NGS$ for f in /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam; do prefix=${f%%.bam}; samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/pool_I_090215${prefix}_newheader.bam; done
bash: /home/cmccabe/Desktop/NGS/pool_I_090215/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_008_150902_newheader.bam: No such file or directory
bash: /home/cmccabe/Desktop/NGS/pool_I_090215/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_015_rawlib_newheader.bam: No such file or directory
bash: /home/cmccabe/Desktop/NGS/pool_I_090215/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_016_150902_newheader.bam: No such file or directory
cmccabe is offline   Reply With Quote
Old 09-15-2015, 11:21 AM   #6
dpryan
Devon Ryan
 
Location: Freiburg, Germany

Join Date: Jul 2011
Posts: 3,480
Default

You forgot the basename line.
dpryan is offline   Reply With Quote
Old 09-15-2015, 11:36 AM   #7
cmccabe
Senior Member
 
Location: chicago

Join Date: Jul 2012
Posts: 351
Default

I thought I got it but I am a bit confused:

Code:
bname=`basename $f`
pref=${bname%%.bam}
for f in /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam
do
prefix=${f%%.bam}
samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/pool_I_090215/${prefix}_newheader.bam
done
gives the below error:

Code:
cmccabe@HPZ640:~$ cd "/home/cmccabe/Desktop/NGS"
cmccabe@HPZ640:~/Desktop/NGS$ bname=`basename $f`
basename: missing operand
Try 'basename --help' for more information.
cmccabe@HPZ640:~/Desktop/NGS$ pref=${bname%%.bam}
cmccabe@HPZ640:~/Desktop/NGS$ for f in /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam
> do
> prefix=${f%%.bam}
> samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/pool_I_090215/${prefix}_newheader.bam
> done
bash: /home/cmccabe/Desktop/NGS/pool_I_090215//media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_008_150902_newheader.bam: No such file or directory
bash: /home/cmccabe/Desktop/NGS/pool_I_090215//media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_015_rawlib_newheader.bam: No such file or directory
bash: /home/cmccabe/Desktop/NGS/pool_I_090215//media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_016_150902_newheader.bam: No such file or directory
Thank you .
cmccabe is offline   Reply With Quote
Old 09-15-2015, 11:44 AM   #8
dpryan
Devon Ryan
 
Location: Freiburg, Germany

Join Date: Jul 2011
Posts: 3,480
Default

When you run basename, $f hasn't yet been defined...

I'm actually not going to explicitly tell you the solution to this, you should be able to figure it out given a bit of playing around and noting the error message.
dpryan is offline   Reply With Quote
Old 09-16-2015, 08:42 AM   #9
cmccabe
Senior Member
 
Location: chicago

Join Date: Jul 2012
Posts: 351
Default

So here is the command:

Code:
/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam ; do     bname=`basename $f`;     pref=${bname%%.bam};     samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/pool_I_090215/${pref}_newheader.bam; done
This seems to work great, thank you for your help .
cmccabe is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off




All times are GMT -8. The time now is 02:16 AM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Single Sign On provided by vBSSO