SEQanswers

Go Back   SEQanswers > Bioinformatics > Bioinformatics



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract reads from multiple regions from bam file meher Bioinformatics 7 12-14-2012 04:18 AM
Filter paired & mapped reads from SAM/BAM file pirates.genome Bioinformatics 2 06-19-2012 02:21 AM
how to filter a bam file paolo.kunder Bioinformatics 2 01-20-2012 02:07 AM
Extracting specific regions from binary .map file mhayes Bioinformatics 0 08-15-2011 01:14 PM
Filter paired end BAM file based on iSize Leif Bergsagel Bioinformatics 2 12-16-2010 12:50 PM

Reply
 
Thread Tools
Old 02-13-2013, 01:42 AM   #1
fcr
Member
 
Location: Seville

Join Date: Jan 2012
Posts: 19
Default How to filter out chromosomes/specific regions from a BAM file?

Is there any way of filtering out specific regions from a BAM file. Let's say I want to exclude the sex chromosomes from some analyses, is there any way to do this from a processed BAM file?
Seems possible to do it by excluding those chromosomes from the reference fasta during the alignment. But does implies, ehem, starting the pipeline all over again!

Cheers,
Nando
fcr is offline   Reply With Quote
Old 02-13-2013, 02:16 AM   #2
dariober
Senior Member
 
Location: Cambridge, UK

Join Date: May 2010
Posts: 311
Default

Quote:
Originally Posted by fcr View Post
Is there any way of filtering out specific regions from a BAM file. Let's say I want to exclude the sex chromosomes from some analyses, is there any way to do this from a processed BAM file?
Seems possible to do it by excluding those chromosomes from the reference fasta during the alignment. But does implies, ehem, starting the pipeline all over again!

Cheers,
Nando
I would make a file of regions you want to retain (e.g. a tab separated file with <chromosome> <start> <end> for all chromosomes but excluding, say, chrY and chrX). Then pass this file to samtools view.

Dario

Last edited by dariober; 02-13-2013 at 02:23 AM. Reason: Do not use -R
dariober is offline   Reply With Quote
Old 02-13-2013, 02:29 AM   #3
fcr
Member
 
Location: Seville

Join Date: Jan 2012
Posts: 19
Default

Thanks Dario,

Ah, so it will work like this:

samtools view aln.bam regions_to_keep.tab

without the -R. Right??

Cheers,
Nando

Last edited by fcr; 02-13-2013 at 02:40 AM.
fcr is offline   Reply With Quote
Old 02-13-2013, 02:42 AM   #4
dariober
Senior Member
 
Location: Cambridge, UK

Join Date: May 2010
Posts: 311
Default

Quote:
Originally Posted by fcr View Post
Ahá damn simple, I guess this will work:

samtools view -R aln.bam regions_to_keep.tab

Thanks Dario!
...Hold on, I don't think -R is correct (I haven't tested it) I actually think you have to pass the file after the bam, without flag... (Sorry about the confusion)
dariober is offline   Reply With Quote
Old 02-13-2013, 02:48 AM   #5
fcr
Member
 
Location: Seville

Join Date: Jan 2012
Posts: 19
Default

Ok, no worries. thanks again for your answer.
fcr is offline   Reply With Quote
Old 02-13-2013, 06:50 AM   #6
volks
Member
 
Location: hd.de

Join Date: Jun 2010
Posts: 81
Default

i dont think realigning without certain parts of the reference is a good idea, because some reads will instead be mapping elsewhere in the genome.

you could also use awk to filter:
samtools view -h aln.bam | awk '{if($3 != "chrX" && $3 != "chrY"){print $0}}' | samtools view -Sb - > aln.filter.bam
volks is offline   Reply With Quote
Old 10-29-2018, 06:24 AM   #7
lgmSeq
Junior Member
 
Location: France

Join Date: Jun 2014
Posts: 5
Default

To filter out specific regions from a BAM file, you could use the -U option of samtools view:

samtools view -b -L specificRegions.bed -U myFileWithoutSpecificRegions.bam myFile.bam > overlappingSpecificRegions.bam

-b Output in the BAM format
-L FILE Only output alignments overlapping the input BED FILE
-U FILE Write alignments that are not selected by the various filter options to FILE. When this option is used, all alignments intersecting the regions specified are written to either the output file or this file, but never both
lgmSeq 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 01:15 PM.


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