Content deleted Content added
updated code |
m →top: Replaced deprecated <source> tags with <syntaxhighlight> |
||
(2 intermediate revisions by one other user not shown) | |||
Line 1:
<
#!/usr/bin/python
# -*- coding: UTF-8 -*-
Line 150:
exp = r'\{\{' # the opening brackets for the infobox
exp = exp + r'\s*' # any amount of whitespace
exp = exp + r'[Dd]rugbox' # the word "
exp = exp + r'.*\}\}' # any amount of anything, followed by the end of the
regexp_drug_infobox = re.compile(exp, re.DOTALL)
Line 192:
regexp_ref_tag_end = re.compile(r"</ref>")
regexp_citation_template = re.compile(r"\{\{[C|c]ite\s*?(?P<TEMPLATE>.*?)\}\}")
# href='/monograph/maprotiline-hydrochloride.html'
regexp_monograph_url = re.compile("href='/monograph/(?P<STEM>.*?)\.html'", re.DOTALL)
# http://www.nlm.nih.gov/medlineplus/druginfo/meds/a604021.html
regexp_medlineplus_url = re.compile("www.nlm.nih.gov/medlineplus/druginfo/meds/(?P<ACNO>.*?)\.html", re.DOTALL)
def Allowbots(text):
Line 213 ⟶ 219:
if search_result:
result_text = search_result.group(0) # returns the entire matching sequence
begin, end
else:
return None
Line 228 ⟶ 234:
break
offset = result_text.find('|')
___location = (begin+offset, begin+last_ind-1, begin, begin+last_ind+1)
return ___location
Line 239 ⟶ 245:
# Name Trade_Names Drug_Type MedlinePlus Drugs.com_link KEGG_Drug_ID KEGG_Compound_ID ChemSpider_ID PubChem_Compound_ID DrugBank_ID
drug_data = csv.reader(open('/Users/
for row in drug_data:
Line 441 ⟶ 447:
value = pad_parameters(value)
# print "parameter, value: ", parameter, " ", value
if not value:
value = " "
current_parameters[parameter] = value
else:
Line 451 ⟶ 459:
value = (result_drug_param.group('VALUE').encode("utf-8")).strip()
value = pad_parameters(value)
current_parameters[parameter] = value
Line 509 ⟶ 519:
current_parameters['type'] = "vaccine"
# if not previously assigned, add the following "empty" parameters
new_drugbox = unicode( "", "utf-8" )▼
if not current_parameters.has_key("
current_parameters["tradename"] = " "
▲ new_drugbox = unicode( "{{Drugbox\n", "utf-8" )
if current_parameters.has_key("Verifiedfields"): new_drugbox += "| Verifiedfields = " + current_parameters['Verifiedfields'] + "\n"
if current_parameters.has_key("Watchedfields"): new_drugbox += "| Watchedfields = " + current_parameters['Watchedfields'] + "\n"
if current_parameters.has_key("verifiedrevid"): new_drugbox += "| verifiedrevid = " + current_parameters['verifiedrevid'] + "\n"
if current_parameters.has_key("IUPAC_name"): new_drugbox += "| IUPAC_name = " + current_parameters['IUPAC_name'] + "\n"
Line 588 ⟶ 602:
"KEGG" in current_parameters or "ChEBI" in current_parameters or "ChEMBL" in current_parameters):
new_drugbox += "\n<!--Identifiers-->\n"
if current_parameters.has_key("
if current_parameters.has_key("CASNo_Ref"): new_drugbox += "| CASNo_Ref = " + current_parameters['CASNo_Ref'] + "\n"
if current_parameters.has_key("CAS_number"): new_drugbox += "| CAS_number = " + current_parameters['CAS_number'] + "\n"
if current_parameters.has_key("CAS_supplemental"): new_drugbox += "| CAS_supplemental = " + current_parameters['CAS_supplemental'] + "\n"
Line 598 ⟶ 613:
if current_parameters.has_key("PubChemSubstance"): new_drugbox += "| PubChemSubstance = " + current_parameters['PubChemSubstance'] + "\n"
if current_parameters.has_key("IUPHAR_ligand"): new_drugbox += "| IUPHAR_ligand = " + current_parameters['IUPHAR_ligand'] + "\n"
if current_parameters.has_key("DrugBank_Ref"): new_drugbox += "| DrugBank_Ref = " + current_parameters['DrugBank_Ref'] + "\n"
if current_parameters.has_key("DrugBank"): new_drugbox += "| DrugBank = " + current_parameters['DrugBank'] + "\n"
if current_parameters.has_key("ChemSpiderID_Ref"): new_drugbox += "| ChemSpiderID_Ref = " + current_parameters['ChemSpiderID_Ref'] + "\n"
Line 617 ⟶ 633:
"Sb" in current_parameters or "Se" in current_parameters or "Sr" in current_parameters or "Tc" in current_parameters or "charge" in current_parameters):
if current_parameters.has_key("chemical_formula"): new_drugbox += "| chemical_formula = " + current_parameters['chemical_formula'] + "\n"
# new_drugbox += " "
if current_parameters.has_key("C"): new_drugbox += "| C=" + current_parameters['C'] + " "
if current_parameters.has_key("H"): new_drugbox += "| H=" + current_parameters['H'] + " "
Line 650 ⟶ 667:
if current_parameters.has_key("InChI_Ref"): new_drugbox += "| InChI_Ref = " + current_parameters['InChI_Ref'] + "\n"
if current_parameters.has_key("InChI"): new_drugbox += "| InChI = " + current_parameters['InChI'] + "\n"
if current_parameters.has_key("InChIKey"): new_drugbox += "| InChIKey = " + current_parameters['InChIKey'] + "\n"
if current_parameters.has_key("StdInChI_Ref"): new_drugbox += "| StdInChI_Ref = " + current_parameters['StdInChI_Ref'] + "\n"
if current_parameters.has_key("StdInChI"): new_drugbox += "| StdInChI = " + current_parameters['StdInChI'] + "\n"
Line 665 ⟶ 683:
if current_parameters.has_key("specific_rotation"): new_drugbox += "| specific_rotation = " + current_parameters['specific_rotation'] + "\n"
if current_parameters.has_key("sec_combustion"): new_drugbox += "| sec_combustion = " + current_parameters['sec_combustion'] + "\n"
new_drugbox += "}}"
# print new_drugbox
Line 735 ⟶ 755:
# drugs.com root links:
roots = [("monograph","http://www.drugs.com/monograph/"), ("CDI","http://www.drugs.com/cdi/"), ("CONS","http://www.drugs.com/cons/"), ("MTM","http://www.drugs.com/mtm/"), ("pro","http://www.drugs.com/pro/"), ("international","http://www.drugs.com/international/"), ("parent","http://www.drugs.com/")]
stems = []
Line 748 ⟶ 768:
drugname = string.lower(drugname)
# print "drugnames: ", drugnames
if (drugname != " " and string.find(drugname, " ") > -1):
stems.append(string.replace(drugname, " ", "_"))
stems.append(string.replace(drugname, " ", "-"))
stems.append(drugname)
# also try common salts
stems.append(drugname + "-hydrochloride")
stems.append(drugname + "-sulfate")
# stems.append(drugname + "-chloride")
# stems.append(drugname + "-sodium")
# stems.append(drugname + "-bromide")
# stems.append(drugname + "-maleate")
# stems.append(drugname + "-citrate")
if drugbank_drugs_com:
if (string.find(drugbank_drugs_com, "http://www.drugs.com/") > -1):
Line 768 ⟶ 796:
if urllib.urlopen(link).getcode() == 200: # test link status to make sure it is good before assigning parameter
# print "passed link: ", link
raise StopIteration()
else:
opener = urllib.FancyURLopener({})
f = opener.open(link)
text = f.read()
result = regexp_monograph_url.search(text)
if result:
stem = result.group('STEM')
link = "{{drugs.com|" + roots[0][0] + "|" + stem + "}}"
raise StopIteration()
else:
link = "{{drugs.com|" + root[0] + "|" + stem + "}}"
# print "link: ", link
raise StopIteration()
else:
link = ""
Line 799 ⟶ 841:
return False
def savepage(page, text, summary = '', minor = False, log_string = ""):
"""Save text to a page and log exceptions."""
if summary != '':
Line 805 ⟶ 847:
try:
page.put(text, minorEdit = minor)
wikipedia.output('%s \03{green}saving %s' % (log_string, page.title()) )
return ''
except wikipedia.LockedPage:
wikipedia.output('%s \03{red}cannot save %s because it is locked\03{default}' % (log_string, page.title()) )
return '# %s: page was locked\n' % page.aslink()
except wikipedia.EditConflict:
wikipedia.output('%s \03{red}cannot save %s because of edit conflict\03{default}' % (log_string, page.title()) )
return '# %s: edit conflict occurred\n' % page.aslink()
except wikipedia.SpamfilterError, error:
wikipedia.output('%s \03{red}cannot save %s because of spam blacklist entry %s\03{default}' % ((log_string, page.title(), error.url)) )
return '# %s: spam blacklist entry\n' % page.aslink()
except:
wikipedia.output('%s \03{red}unknown error on saving %s\03{default}' % (log_string, page.title()) )
return '# %s: unknown error occurred\n' % page.aslink()
Line 831 ⟶ 873:
# list of articles to work on is generated by: "python pagegenerators.py -namespace:0 -transcludes:Drugbox > drugbox_titles.txt"
articles = []
articles = codecs.open('/Users/
# articles = ['Progesterone']
Line 846 ⟶ 888:
new_drugbox = ""
log_string = "* [[" + article + "]], "
print log_string,▼
site = wikipedia.getSite()
Line 856 ⟶ 897:
continue
begin, end, begin2, end2 = find_drugbox_from_text(text)
if
parameters = text[begin:end]
▲ print log_string,
else:
log_string =
print log_string
continue
Line 871 ⟶ 910:
if unbalanced(parameters):
log_string =
print log_string
continue
Line 896 ⟶ 935:
db_data = drugbank_data[INN]
elif "DrugBank" in current_parameters and current_parameters['DrugBank'] in DrugBank_ID_INN:
INN = DrugBank_ID_INN[current_parameters['DrugBank']]
db_data = drugbank_data[INN]
if not "drug_name" in current_parameters:
Line 907 ⟶ 946:
if "DrugBank" in current_parameters and current_parameters['DrugBank'] in DrugBank_ID_INN:
if DrugBank_ID_INN[current_parameters['DrugBank']] == INN:
else:
else:
if db_data:
if db_data[8]:
current_parameters['DrugBank'] = db_data[8]
Line 920 ⟶ 960:
if db_data:
if db_data[4] and not
current_parameters['KEGG'] = db_data[4]
if db_data[6] and not
current_parameters['ChemSpiderID'] = db_data[6]
if db_data[7] and not
current_parameters['PubChem'] = db_data[7]
Line 938 ⟶ 978:
else:
merck_tradename = ""
if
current_tradename = current_parameters['tradename']
else:
Line 953 ⟶ 993:
# add Drugs.com link
if
tradename = current_parameters['tradename']
else:
Line 968 ⟶ 1,008:
if result: current_parameters['Drugs.com'] = result
# add MedlinePlus parameter
new_text = text[:begin] + build_new_drugbox(current_parameters) + text[end:]▼
if db_data[2]:
if test_MedlinePlus(db_data[2]):
current_parameters['MedlinePlus'] = db_data[2]
if not 'MedlinePlus' in current_parameters:
opener = urllib.FancyURLopener({})
stem = string.replace(article, " ", "+")
link = "http://vsearch.nlm.nih.gov/vivisimo/cgi-bin/query-meta?&v:project=medlineplus&query=" + stem
# print "MedlinePlus link:", link
f = opener.open(link)
text2 = f.read()
result = regexp_medlineplus_url.search(text2)
if result:
current_parameters['MedlinePlus'] = result.group('ACNO')
# print build_new_drugbox(current_parameters)
# print current_parameters
# print new_text
if current_parameters:
comment='populated new fields in {{drugbox}} and reordered per [[Wikipedia:Bots/Requests_for_approval/BogBot_2|bot approval]]. Report errors and suggestions to [[User_talk:BogBot]]'
status = savepage(page, new_text, comment,
▲ print ", ", status
else:
print ", page not updated"
Line 984 ⟶ 1,046:
run()
</syntaxhighlight>
|