Don't like ads? PRO users don't see any ads ;-)

ensembl search module

By: stephanheijl on Apr 29th, 2012  |  syntax: Python  |  size: 1.86 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #################################
  2. #     ensembl search module             #
  3. #         Stephan Heijl                 #
  4. #        Afvinkopdracht 3               #
  5. #            23/4/2012                  #
  6. #################################
  7.  
  8. import MySQLdb,time
  9. class ensembleSearch():
  10.         def __init__(self, term, conffile="ensembl.conf"):
  11.                 conf = self.parseConfigurationFile(conffile)
  12.                 self.term = term
  13.                 con = MySQLdb.connect(host=conf["HOST"],
  14.                               user=conf["USER"],
  15.                               passwd=conf["PASSWORD"],
  16.                               db=conf["DATABASE"])
  17.                              
  18.                 limit = conf['LIMIT'] if 'LIMIT' in conf.keys() else 10
  19.                 self.results = self.find(con.cursor(),term,limit)
  20.                
  21.         def find(self,cursor,term,limit):
  22.                 query = "SELECT * FROM gene WHERE description LIKE '%{0}%' LIMIT {1}".format(term,limit)
  23.                 cursor.execute(query)
  24.                 return cursor.fetchall()
  25.                
  26.         def saveResults(self,saveType):
  27.                 try:
  28.                         __import__(saveType).dump(self.results,open(self.term.replace(" ",".")+"."+str(time.time())[4:-3]+"."+saveType,"w"))
  29.                 except ImportError:
  30.                         print "Unsupported savetype, file was not saved."
  31.                         exit()
  32.                 except IOError:
  33.                         print "Write error, file was not saved."
  34.                         exit()
  35.                 except:
  36.                         print "Unknown save error."
  37.                
  38.         def returnResults(self):
  39.                 return self.results
  40.                        
  41.         def parseConfigurationFile(self,name):
  42.                 try:
  43.                         conf = open(name)
  44.                 except IOError:
  45.                         print "Could not find {0} file.".format(name)
  46.                         exit()
  47.                
  48.                 configuration = {}
  49.                 for line in conf:
  50.                         if line[0] != "#":
  51.                                 kv = line.split("=")
  52.                                 configuration[kv[0]] = kv[1][:-1]
  53.                
  54.                 if all([k in configuration.keys() for k in ['USER','HOST','PASSWORD','DATABASE']]):
  55.                         return configuration
  56.                 else:
  57.                         print "Incomplete configuration file."
  58.        
  59. if __name__ == "__main__":
  60.         term = "zinc finger"
  61.         eS = ensembleSearch(term)
  62.         print eS.returnResults()
  63.         eS.saveResults("json")