def associated_events(self, drugname, sortby="COUNT"): """ This method will return a sorted list of drug-event frequencies. """ drugname = drugs.standardize([drugname])[0] sql = "SELECT DRUGNAME, PT, COUNT FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME = '%s' ORDER BY %s DESC" % (drugname, sortby) results = self.conn.execute(sql).fetchall() return [[str(k),str(v),c] for k,v,c in results]
def associated_events(self, drugname, sortby="COUNT"): """ This method will return a sorted list of drug-event frequencies. """ drugname = drugs.standardize([drugname])[0] sql = "SELECT DRUGNAME, PT, COUNT FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME = '%s' ORDER BY %s DESC" % ( drugname, sortby) results = self.conn.execute(sql).fetchall() return [[str(k), str(v), c] for k, v, c in results]
def mine_prr_by_drug(self, drug, n=3): """ Given a drug, compute PRR for all events assocated with drug for drug-event with frequency >= n. """ drug = drugs.standardize([drug])[0] sql = "SELECT PT FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME = '%s' AND COUNT >= %s" % (drug, str(n)) result = self.conn.execute(sql).fetchall() events = [str(i[0]) for i in result] prr_list = [self.prr(drug, e) for e in events] prr_list = [i for i in prr_list if i['PRR'] != None] idx = sorted(range(len(prr_list)), key = lambda k: -prr_list[k]['PRR']) prr_list = [prr_list[i] for i in idx] return prr_list
def mine_prr_by_drug(self, drug, n=3): """ Given a drug, compute PRR for all events assocated with drug for drug-event with frequency >= n. """ drug = drugs.standardize([drug])[0] sql = "SELECT PT FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME = '%s' AND COUNT >= %s" % ( drug, str(n)) result = self.conn.execute(sql).fetchall() events = [str(i[0]) for i in result] prr_list = [self.prr(drug, e) for e in events] prr_list = [i for i in prr_list if i['PRR'] != None] idx = sorted(range(len(prr_list)), key=lambda k: -prr_list[k]['PRR']) prr_list = [prr_list[i] for i in idx] return prr_list
def drug_event_stats(self, drug, event): """ This method computes frequencies used in calculating the PRR: ----------------------------------+--------------------------------------------+ | Drug of interest | All other drugs | Total | ----------------------------------+------------------+-----------------+-------| Adverse drug reaction of interest | A | B | A + B | ----------------------------------+------------------+-----------------+-------| All other adverse drug reactions | C | D | C + D | ----------------------------------+------------------+-----------------+-------| Total | A+C | B+D |A+B+C+D| -------------------------------------------------------------------------------+ """ # print """ #+-----------------------------------+------------------+-------------------------+ #| | Drug of interest | All other drugs | Total | #+-----------------------------------+------------------+-----------------+-------| #| Adverse drug reaction of interest | A | B | A + B | #+-----------------------------------+------------------+-----------------+-------| #| All other adverse drug reactions | C | D | C + D | #+-----------------------------------+------------------+-----------------+-------| #| Total | A+C | B+D |A+B+C+D| #+-----------------------------------+------------------+-----------------+-------+ #""" drug = drugs.standardize([drug])[0] event = event.upper() A = "SELECT SUM(COUNT) FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME = \"%s\" AND PT = \"%s\"" % ( drug, event) B = "SELECT SUM(COUNT) FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME <> \"%s\" AND PT = \"%s\"" % ( drug, event) C = "SELECT SUM(COUNT) FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME = \"%s\" AND PT <> \"%s\"" % ( drug, event) D = "SELECT SUM(COUNT) FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME <> \"%s\" AND PT <> \"%s\"" % ( drug, event) A = self.conn.execute(A).fetchone()[0] B = self.conn.execute(B).fetchone()[0] C = self.conn.execute(C).fetchone()[0] D = self.conn.execute(D).fetchone()[0] return {"A": A, "B": B, "C": C, "D": D, "drug": drug, "event": event}
def drug_event_stats(self, drug, event): """ This method computes frequencies used in calculating the PRR: ----------------------------------+--------------------------------------------+ | Drug of interest | All other drugs | Total | ----------------------------------+------------------+-----------------+-------| Adverse drug reaction of interest | A | B | A + B | ----------------------------------+------------------+-----------------+-------| All other adverse drug reactions | C | D | C + D | ----------------------------------+------------------+-----------------+-------| Total | A+C | B+D |A+B+C+D| -------------------------------------------------------------------------------+ """ # print """ #+-----------------------------------+------------------+-------------------------+ #| | Drug of interest | All other drugs | Total | #+-----------------------------------+------------------+-----------------+-------| #| Adverse drug reaction of interest | A | B | A + B | #+-----------------------------------+------------------+-----------------+-------| #| All other adverse drug reactions | C | D | C + D | #+-----------------------------------+------------------+-----------------+-------| #| Total | A+C | B+D |A+B+C+D| #+-----------------------------------+------------------+-----------------+-------+ #""" drug = drugs.standardize([drug])[0] event = event.upper() A = "SELECT SUM(COUNT) FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME = \"%s\" AND PT = \"%s\"" % (drug, event) B = "SELECT SUM(COUNT) FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME <> \"%s\" AND PT = \"%s\"" % (drug, event) C = "SELECT SUM(COUNT) FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME = \"%s\" AND PT <> \"%s\"" % (drug, event) D = "SELECT SUM(COUNT) FROM temp.DRUG_EVENT_COUNT WHERE DRUGNAME <> \"%s\" AND PT <> \"%s\"" % (drug, event) A = self.conn.execute(A).fetchone()[0] B = self.conn.execute(B).fetchone()[0] C = self.conn.execute(C).fetchone()[0] D = self.conn.execute(D).fetchone()[0] return {"A":A, "B":B, "C":C, "D":D, "drug":drug, "event":event}
#!/usr/bin/python import drugstandards as drugs import sqlite3 import csv # Set the minmal Jario-Winkler similarity required for two strings to be considered a match. THRESH = 0.9 # Establish connection to faers database which was created by running ./import_faers_data.sh conn = sqlite3.connect("faers.db") # Get unique drug names from faers.db to standardize. faers = [i[0] for i in conn.execute("SELECT DISTINCT(drugname) FROM drug").fetchall() if i[0] != None] # Standardize drug names form above. stand = drugs.standardize(faers, thresh=THRESH) pairs = [ (faers[i], stand[i]) for i in range(len(stand)) if stand[i] != None and faers[i] != stand[i]] print "Adding %d records to drugmap" % len(pairs) # Add DRUG_MAP table. conn.execute('CREATE TABLE drugmap (original TEXT, replacement TEXT)') conn.executemany('INSERT INTO drugmap (original, replacement) VALUES (?, ?)', (pairs)) conn.execute('CREATE INDEX drugmap_idx ON drugmap (original, replacement)') conn.commit()