That seems to do exactly what I want, thank you! So I guess the best way to do this in Python is to not use Python...
What is the preferred way to, given locations in a genome, find the first genes that are upstream or downstream of these locations?
I have been looking into processing a GFF file using BioPython, but it is taking an insanely long time (2h+) to parse a file other tools can parse in seconds. I also considered using Ensembl, but there also doesn't seem to be a good API for Python there.
So what would be my best course of action here? Use MySqlDb to hook straight into the Ensembl database? Parse the GFF file myself?
3 answers
there is a implementation of bedtools incase you need to use a a part of a script. http://packages.python.org/pybedtools/
Since noone has mentioned it, you can use BEDTools and python (your cake and eat it too) with pybedtools
With that, the code would be something like:
import sys
from bedtools import BedTool
locs_file = sys.argv[1]
genes_file = sys.argv[2]
for nearest in BedTool(locs_file).closest(genes_file):
print nearest
Thank you for the suggestion. Do you know how well this performs compared to the command-line bedtools?
BedTool(locs_file).closest(genes_file) will be virtually identical in performance to the command-line. Looping over the result will incur some overhead because it is converting each line into a python object. But generally, speed is very good, most of the performance-critical parts of pybedtools are written in C.
Pierre's bedtools approach is probably the best. If you are set on developing you're own in-house application with Python I would recommend using bx.python and IntervalTrees. You can parse a feature list (such as a gene list with coordinates and chromosome) in to IntervalTrees (one per chromosome) and look for the closest intervals to your position(s) of interest. However bedtools will work much quicker and be easier "out-of-the-box".
But learning IntervalTrees can be quite handy for dealing with genomic data in general, especially with Python, so it may be something worth looking in to as a learning exercise.
Yeah intervals trees are a good way to go. I posted some code on a python implementation recently: http://blog.nextgenetics.net/?e=45
Looks good. There is the implementation in bx.python, which includes a cython implementation already, I highly recommend checking it out as it is a pretty robust library
I think I will go with the easy solution above for now, but IntervalTrees are definitely something I will have to play around with in the future, so thank you for the suggestion!
I agree, I think the bedtools solution is optimal for your current needs. I am using IntervalTrees pretty heavily though for downstream data analysis and additional annotations that I am doing as part of my pipeline.
Log in to answer this question.