<jupyter_start><jupyter_text>This code takes clean data exported from the DID parser and writes it to Wikidata<jupyter_code>from wikidataintegrator import wdi_core, wdi_login
from wikidataintegrator.ref_handlers import update_retrieved_if_new_multiple_refs
import pandas as pd
from pandas import read_csv
import requests
from tqdm.notebook import trange, tqdm
import ipywidgets 
import widgetsnbextension
from datetime import datetime
import copy
def create_reference():
    refStatedIn = wdi_core.WDItemID(value="Q70116865", prop_nr="P248", is_reference=True)
    timeStringNow = datetime.now().strftime("+%Y-%m-%dT00:00:00Z")
    refRetrieved = wdi_core.WDTime(timeStringNow, prop_nr="P813", is_reference=True)
    refStatedIn2 = wdi_core.WDItemID(value="Q21008030", prop_nr="P248", is_reference=True)
    refRetrieved2 = wdi_core.WDTime(timeStringNow, prop_nr="P813", is_reference=True)
    
    return [refStatedIn, refRetrieved, refStatedIn2, refRetrieved2]
## Login for Scheduled bot
print("Logging in...")
try:
    from scheduled_bots.local import WDUSER, WDPASS
except ImportError:
    if "WDUSER" in os.environ and "WDPASS" in os.environ:
        WDUSER = os.environ['WDUSER']
        WDPASS = os.environ['WDPASS']
    else:
        raise ValueError("WDUSER and WDPASS must be specified in local.py or as environment variables")
"""
print("Logging in...")
import wdi_user_config ## Credentials stored in a wdi_user_config file
login_dict = wdi_user_config.get_credentials()
login = wdi_login.WDLogin(login_dict['WDUSER'], login_dict['WDPASS'])
"""<jupyter_output>Logging in...
https://www.wikidata.org/w/api.php
Successfully logged in as Gtsulab<jupyter_text>Update WD with statements derived from WDID<jupyter_code>## Unit test-- write a statement

drug_qid = 'Q407125'
phen_qid = 'Q18968110'
reference = create_reference()
statement = [wdi_core.WDItemID(value=phen_qid, prop_nr="P2175", references=[copy.deepcopy(reference)])]
item = wdi_core.WDItemEngine(wd_item_id=drug_qid, data=statement, append_value="P2175",
                       global_ref_mode='CUSTOM', ref_handler=update_retrieved_if_new_multiple_refs)
item.write(login)

predicate_map = {'may_prevent':'P4954',
                 'may_treat':'P2175',
                 'causes':'P1542'}

wd_properties = {'P4954':'may prevent', ## applied to drug entities, referencing disease entities
                 'P2175':'medical condition treated', ## applied to drug entities, referencing disease entities
                 'P1696':'drug used for treatment', ##applied to disease entities, referencing drug entities
                 'P1542':'has effect', ## applied to drug entities, referencing disease entities
                 'P828':'has cause', ## applied to disease entities, referencing drug entities
                }<jupyter_output><empty_output><jupyter_text>Write treatment statements<jupyter_code>may_treats = read_csv('results/may_treat.tsv',delimiter='\t',header=0, index_col=0)
treatment_statement_info = may_treats[['drug_cas_wdid','phen_cui_wdid']].copy()
treatment_statement_info.drop_duplicates(keep='first',inplace=True)
treatment_statement_info.reset_index(inplace=True)
i=0
for i in tqdm(range(len(treatment_statement_info))):
    drug_qid = treatment_statement_info.iloc[i]['drug_cas_wdid']
    phen_qid = treatment_statement_info.iloc[i]['phen_cui_wdid']
    reference = create_reference()
    statement = [wdi_core.WDItemID(value=phen_qid, prop_nr="P2175", references=[copy.deepcopy(reference)])]
    item = wdi_core.WDItemEngine(wd_item_id=drug_qid, data=statement, append_value=predicate_map['may_treat'],
                       global_ref_mode='CUSTOM', ref_handler=update_retrieved_if_new_multiple_refs)
    item.write(login)
    #print(i, drug_qid, phen_qid)
    i=i+1<jupyter_output>0 Q407125 Q18968110
1 Q407125 Q653971
2 Q407125 Q223591
3 Q407125 Q1495657
4 Q407125 Q4003020
5 Q1087888 Q41861
6 Q179731 Q653971
7 Q179731 Q223591
8 Q179731 Q202837
9 Q179731 Q605259
10 Q179731 Q3505252
11 Q409251 Q936382
12 Q409251 Q736715
13 Q23767 Q18968110
14 Q23767 Q653971
15 Q23767 Q223591
16 Q23767 Q1495657
17 Q23767 Q936382
18 Q23767 Q736715
19 Q23767 Q4003020
20 Q1052672 Q18975220
21 Q1052672 Q101896
22 Q1052672 Q223254
23 Q1052672 Q3776920
24 Q1052672 Q11564537
25 Q1052672 Q186470
26 Q1052672 Q55779861
27 Q2329715 Q12206
28 Q27132403 Q2480013
29 Q417174 Q180762
30 Q415563 Q938107
31 Q415563 Q1494682
32 Q415563 Q647630
33 Q5405160 Q205764
34 Q5405160 Q938107
35 Q5515257 Q916280
36 Q27122277 Q506652
37 Q27122277 Q18553923
38 Q27114666 Q5526839
39 Q426386 Q422225
40 Q121874 Q35805
41 Q121874 Q114953
42 Q121874 Q767327
43 Q121874 Q34879
44 Q121874 Q3991731
45 Q390305 Q1890194
46 Q390305 Q16499
47 Q27107089 Q471521
48 Q27107089 Q101991
49 Q27107089 Q81938
50 Q27107089 Q281289
51 [...]<jupyter_text>Write prevents statements<jupyter_code>may_prevent = read_csv('results/may_prevent.tsv',delimiter='\t',header=0)
prevent_statement_info = may_prevent[['drug_cas_wdid','phen_cui_wdid']].copy()
prevent_statement_info.drop_duplicates(keep='first',inplace=True)
prevent_statement_info.reset_index(inplace=True)
j=0
for j in tqdm(range(len(prevent_statement_info))):
    drug_qid = prevent_statement_info.iloc[j]['drug_cas_wdid']
    phen_qid = prevent_statement_info.iloc[j]['phen_cui_wdid']
    reference = create_reference()
    statement = [wdi_core.WDItemID(value=phen_qid, prop_nr="P4954", references=[copy.deepcopy(reference)])]
    item = wdi_core.WDItemEngine(wd_item_id=drug_qid, data=statement, append_value=predicate_map['may_prevent'],
                       global_ref_mode='CUSTOM', ref_handler=update_retrieved_if_new_multiple_refs)
    item.write(login)
    #print(j, drug_qid, phen_qid)
    j=j+1<jupyter_output><empty_output><jupyter_text>Write cause statements<jupyter_code>causes = read_csv('results/cause.tsv',delimiter='\t',header=0)
cause_statement_info = causes[['drug_cas_wdid','phen_cui_wdid']].copy()
cause_statement_info.drop_duplicates(keep='first',inplace=True)
cause_statement_info.reset_index(inplace=True)
k=0
for k in tqdm(range(len(cause_statement_info))):
    drug_qid = cause_statement_info.iloc[k]['drug_cas_wdid']
    phen_qid = cause_statement_info.iloc[k]['phen_cui_wdid']
    reference = create_reference()
    statement = [wdi_core.WDItemID(value=phen_qid, prop_nr="P1542", references=[copy.deepcopy(reference)])]
    item = wdi_core.WDItemEngine(wd_item_id=drug_qid, data=statement, append_value=predicate_map['causes'],
                       global_ref_mode='CUSTOM', ref_handler=update_retrieved_if_new_multiple_refs)
    item.write(login)
    #print(k,drug_qid, phen_qid)
    k=k+1<jupyter_output><empty_output><jupyter_text>abstract task to a bot<jupyter_code>statement_types = ['cause','may_prevent','may_treat']
property_dict = {'cause':'P1542','may_prevent':'P4954','may_treat':'P2175'}
filelocation = 'results/'

drug_qid_column_to_use = 'drug_cas_wdid'
phen_qid_column_to_use = 'phen_cui_wdid'

for each_did_type in statement_types:
    statement_filename = filelocation+each_did_type+'.tsv'
    triples = read_csv(statement_filename,delimiter='\t',header=0)
    triples_clean = triples[[drug_qid_column_to_use,phen_qid_column_to_use]].copy()
    triples_clean.drop_duplicates(keep='first',inplace=True)
    triples_clean.reset_index(inplace=True)
    i=0
    for i in tqdm(range(len(triples_clean))):
        drug_qid = triples_clean.iloc[i][drug_qid_column_to_use]
        phen_qid = triples_clean.iloc[i][phen_qid_column_to_use]
        reference = create_reference()
        statement = [wdi_core.WDItemID(value=phen_qid, prop_nr=property_dict[each_did_type], references=[copy.deepcopy(reference)])]
        item = wdi_core.WDItemEngine(wd_item_id=drug_qid, data=statement, append_value=property_dict[each_did_type],
                           global_ref_mode='CUSTOM', ref_handler=update_retrieved_if_new_multiple_refs)
        i=i+1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10185770251484898, 'fraction_numerical': 0.1101984076835587, 'mean_word_length': 5.9117903930131, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '386468', 'license': 'MIT', 'n_tokens_mistral': 3463, 'n_tokens_neox': 2892, 'n_words': 518, 'path': 'scheduled_bots/DID/DID WD writer.ipynb', 'repo_name': 'egonw/scheduled-bots', 'size': '7913'}