-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalepfubot.py
215 lines (175 loc) · 6.93 KB
/
alepfubot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
AlepfuBot adds the claim "significant drug interaction (P769)" to drug items.
Claims are imported via a headless CSV file of the following format:
"object-drug","DrugbankID","precipitant-drug","DrugbankID".
If a drug has no item in Wikidata, the claim will be skipped.
If DrugbankIDs don't match, the claim will be skipped.
Example usage:
python alepfubot.py -file=http://www.example.com/data/file.csv -delim=$ -ref=Q17505343
Mandatory parameters:
-file=<url> Full HTTP url to file
-ref=<item> Wikidata item of the source you want to reference
Optional parameters:
-delim=<char> CSV delimiter character, default is ','
-dry If set, nothing will be changed
Other parameters:
¶ms;
"""
#
# (C) Alexander Pfundner (Alepfu), 2014
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
import pywikibot
from pywikibot import pagegenerators
import urllib
import csv
# This is required for the command line help
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp
}
class AlepfuBot:
"""
Adds "significant drug interaction (P769)" claims to drug items.
"""
def __init__(self, isDry, csvFile, delim, ref):
"""
Constructor
Parameters:
@param isDry: If set, nothing will be changed.
@type isDry: boolean.
@param csvFile: Http-Link to remote file.
@type csvFile: unicode.
@param delim: CSV delimiter character
@type delim: unicode.
@param ref: Wikidata item of the source you want to reference.
@type ref: unicode.
"""
self.isDry = isDry
self.csvFile = csvFile
self.delim = delim
self.ref = ref
def run(self):
"""
Steps:
1) Download CSV file
2) Parse CSV file
3) Some checks regarding correctness
4) Add claims to wikidata
"""
# Download CSV file
print "Downloading file", self.csvFile
fileName = self.csvFile[self.csvFile.rfind("/")+1:]
urllib.urlretrieve(self.csvFile, fileName)
print "File", fileName, "saved"
# Init site, repository, claim and source
site = pywikibot.Site("en", "wikipedia")
repo = site.data_repository()
claim = pywikibot.Claim(repo, u'P769')
statedIn = pywikibot.Claim(repo, u'P248')
source = pywikibot.ItemPage(repo, self.ref)
statedIn.setTarget(source)
# Open file
csvReader = csv.reader(open(fileName, "rb"), delimiter=self.delim.encode('utf-8')) # delimiter needs to be str
# Loop over all entries from the file
for row in csvReader:
objDrug = row[0].decode("utf-8") # because reader delivers str
objDrugId = row[1].decode("utf-8")
preDrug = row[2].decode("utf-8")
preDrugId = row[3].decode("utf-8")
print "Adding claim:", objDrug, objDrugId, preDrug, preDrugId
# Remove prefix from DrugBank IDs
if objDrugId.startswith("DB"):
objDrugId = objDrugId[2:]
if preDrugId.startswith("DB"):
preDrugId = preDrugId[2:]
# Load the object and precipitant items, if an item is missing the entry will be skipped
objDrugPage = pywikibot.Page(site, objDrug)
objDrugItem = pywikibot.ItemPage.fromPage(objDrugPage)
if objDrugItem.exists():
objDrugItem.get()
else:
print "No item found for object drug"
print "Skipping entry"
continue
preDrugPage = pywikibot.Page(site, preDrug)
preDrugItem = pywikibot.ItemPage.fromPage(preDrugPage)
if preDrugItem.exists():
preDrugItem.get()
else:
print "No item found for precipitant drug"
print "Skipping entry"
continue
# Check for correct DrugBank IDs
print "Checking DrugBank IDs"
if objDrugItem.claims:
if "P715" in objDrugItem.claims:
if not objDrugItem.claims["P715"][0].getTarget() == objDrugId:
print "Object ID is incorrect"
print "Skipping entry"
continue
if preDrugItem.claims:
if "P715" in preDrugItem.claims:
if not preDrugItem.claims["P715"][0].getTarget() == preDrugId:
print "Precipitant ID is incorrect"
print "Skipping entry"
continue
# Check for existing claims
print "Checking for existing claims"
isExist = False
preLabel = preDrugItem.labels.get("en")
if objDrugItem.claims:
if "P769" in objDrugItem.claims:
precipitants = objDrugItem.claims["P769"]
for p in precipitants:
pitem = p.getTarget();
pitem.get();
label = pitem.labels.get("en")
if label == preLabel:
isExist = True
break
if isExist:
print "Found entry for precipitant"
print "Skipping entry"
continue
# Dry check
if self.isDry:
print "Bot is set dry"
else:
print "Applying changes"
claim.setTarget(preDrugItem)
objDrugItem.addClaim(claim)
claim.addSources([statedIn])
def main():
"""
Process command line arguments and invoke AlepfuBot.
"""
# Init args variables
isDry = False
csvFile = None
delim = ","
ref = None
# Loop over the passed local args
for arg in pywikibot.handleArgs():
if arg.startswith("-dry"):
isDry = True
if arg.startswith("-file"):
csvFile = arg[arg.find("=")+1:]
if arg.startswith("-delim"):
delim = arg[arg.find("=")+1:]
if arg.startswith("-ref"):
ref = arg[arg.find("=")+1:]
# Run the bot, if mandatory args are provided
if csvFile and ref:
bot = AlepfuBot(isDry, csvFile, delim, ref)
bot.run()
else:
if not csvFile:
print "Mandatory parameter -file is missing"
if not ref:
print "Mandatory parameter -ref is missing"
if __name__ == "__main__":
main()