Bedtools - converting multiple bam files to bed
2
0
Entering edit mode
3.2 years ago
rbravos87 • 0

Hi all,

I have previous experience in R, but since some months ago I am trying new things in Python (JupyterLab).

I have a a directory with different files. Some of them are '.bam' files. My objective is to obtain '.bed' files from them. Therefore, I start as follows:

from pybedtools import BedTool  
import os, fnmatch  
bam_files = fnmatch.filter(os.listdir('.'), '*.BAM') #To filter only '.bam' files in working directory file

After this, I try to work with the expression

bedtools bamtobed -i reads.bam > reads.bed

But I think, as I have multiple files, I am doing something wrong. Any help about how to proceed to convert my multiple files??

Thank you very much!!

BED Bedtools Python BAM to • 4.5k views
ADD COMMENT
3
Entering edit mode
3.2 years ago
seidel 11k

Umm...convert them one at a time? Do you have multiple files listed in bam_files? If it is indeed a populated list, you can loop over it, get the prefix and make an output file name, then use your expression with the input and output names. There may be a fancier way, but something of the sort:

# loop through BAM files
for b in bam_files:
  # get the base of the file name
  base = b.split('.')[0]
  # create an output file name
  bed = base + ".bed"
  # create a BedTool reference to the BAM file
  a = BedTool(b)
  # convert to BED
  a.bam_to_bed().saveas(bed)

This all assumes you have indexed BAM files and are running the script from the same directory as the BAM files.

ADD COMMENT
0
Entering edit mode

Thank you!! it worked!!!

ADD REPLY
0
Entering edit mode
3.2 years ago

Via BEDOPS bam2bed:

$ for fn in `ls *.bam`; do bam2bed $fn > $fn.bed; done
ADD COMMENT
0
Entering edit mode

I also tried your way, but maybe I am a little bit unexperienced in Python. The point is that this loops which have 'do' don't run... I think I am doing something wrong

ADD REPLY
1
Entering edit mode

You can do this on the command line, and it will be faster and easier to convert than Python.

ADD REPLY
1
Entering edit mode

Just to be clear, @Alex's solution avoids Python altogether. The command he references assumes you have a suite of tools called BEDOPS installed on your computer, and that you have "command line" access to the computer you are using. One of those tools is called bam2bed, and what he's typed above is command line language (typically bash) to read all the bam files and create a for loop in bash to use bam2bed to do what you're trying to do in Python. If you have command line access, and BEDOPS installed, this is a more straightforward way of processing your files. But given that you mentioned you're trying to learn how to do things in a new way using Python, the method you're trying through a jupyter notebook is fine. There are always many ways of doing something, and it's very useful to know how things can be accomplished with different methods in different environments.

ADD REPLY
0
Entering edit mode

Thank you!! I understood what you mean!!

ADD REPLY

Login before adding your answer.

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