How to run bedtools from inside a python program
1
0
Entering edit mode
3.5 years ago
Gene_MMP8 ▴ 240

I have been using the bedtools to extract fasta sequences based on genomic positions mentioned in a bed file. The command that I usually use os the following:

bedtools getfasta -fi input.fa -bed input.bed -fo out.fa

Now I want to implement the same command line from a python program. This is mainly because I will be doing some downstream analysis using the "bedtools getfasta" output and that part has been entirely written in python. Both the input and the output can be pandas dataframes. I am aware of the "pybedtools" module but my question also extends to integration of other command line tools as well.

bedtools python commandline • 2.9k views
ADD COMMENT
0
Entering edit mode
cmd = "bedtools getfasta -fi "+ input.fa + "-bed" +  input.bed + "-fo" + out.fa  
subprocess.call(cmd, shell=True)

or

bedtools="/home/Apps/bedtools"
cmd = bedtools + "getfasta -fi "+ input.fa + "-bed" +  input.bed + "-fo" + out.fa  
subprocess.call(cmd, shell=True)

You may need to pre-define input.fa, input.bed and out.fa before calling them.

Search for cmd in the link below, and see some examples:

https://sourceforge.net/p/fun4me/code/ci/master/tree/fun4me.py

ADD REPLY
1
Entering edit mode
3.5 years ago
Joe 21k

Use pybedtools if at all possible as its designed for this purpose.

Or run the commands via the subprocess module which is part of the standard library (or some combination of the two if pybedtools doesn't give you what you want).

Subprocess is the general solution to running commandlines in python though.

ADD COMMENT

Login before adding your answer.

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