I want to figure out how to use Python to get all genes associated with a GO Term. I was trying to Biomart Python API but it's really weird and Retrieve All Genes Associated With A Go Term is for R. I'm trying to use mygene but there's no GO for the scopes (the input term) only in the fields (output terms) Gene Id Conversion Tool I need to do it for ~6000 GO pathways. I got it to work using bioservices db2db but I get timed out. Set a pause for 5 seconds between each search and I'm still getting the timeout. (http://pythonhosted.org/bioservices/references.html#module-bioservices.biodbnet if anyone wants to know how to use this...really useful)
Does anyone know a tool in Python I can use to do this that won't time me out?
1 answer
You can use mygene Python module to query GO terms for matching genes:
import mygene
mg = mygene.MyGeneInfo()
to query just one GO term:
mg.query('GO:0023026', size=1000)
By default, it returns the first ten matched genes, to get all genes, set a higher size like 1000. Note that there are some GO terms having a large list of genes associated, and you probably don't want to retrieve them all (not that useful anyway), so cap the returned gene list up to 1000 should be a reasonable setting (also avoid timeout).
With mygene, you can also query multiple GO terms in a batch:
mg.querymany(["GO:0023026", "GO:0002503"], scopes='go', size=1000)
In returned result, each gene hit contains a "query" attribute with the value of the corresponding GO term.
And you might want to restrict the number of GO terms in one batch, so that you don't overload the server.
Log in to answer this question.
https://github.com/endrebak/biomartian