Skip to content

Instantly share code, notes, and snippets.

@danielecook
Last active March 24, 2017 10:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielecook/7088662 to your computer and use it in GitHub Desktop.
Save danielecook/7088662 to your computer and use it in GitHub Desktop.
Quick function for fetching the gene coordinates when given the gene name. Can sepcify build.
# Note: Requires mysqldb; install using:
# pip install MySQL-python
from MySQLdb.constants import FIELD_TYPE
import _mysql
db = None
def fetch_gene_coordinates(gene_name,build):
global db # db is global to prevent reconnecting.
if db is None:
print 'connect'
conv= { FIELD_TYPE.LONG: int }
db = _mysql.connect(host='genome-mysql.cse.ucsc.edu',user='genome',passwd='',db=build,conv=conv)
db.query("""SELECT * FROM kgXref INNER JOIN knownGene ON kgXref.kgID=knownGene.name WHERE kgXref.geneSymbol = '%s'""" % gene_name)
r = db.use_result().fetch_row(how=1,maxrows=0)
print r
if len(r)>1:
pass
else:
return r[0]['txStart'], r[0]['txEnd'], r[0]['chrom'],r[0]['strand']
print fetch_gene_coordinates('klf1','mm9')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment