-
Notifications
You must be signed in to change notification settings - Fork 1
/
article-subj-from-chebi.py
120 lines (109 loc) · 3.37 KB
/
article-subj-from-chebi.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
import os, json, argparse, sys, datetime, time
import pronto, six
"""
bzcat latest-all.json.bz2 |wikibase-dump-filter --simplify --claim 'P698&P921' |jq '[.id,.claims.P698,.claims.P921]' -c >PMID.ndjson
"""
# Initiate the parser
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--output_qs", help="output to QS",
action="store_true")
parser.add_argument("-q", "--query", help="perform SPARQL query",
action="store_true")
# Read arguments from the command line
args = parser.parse_args()
# Check for --version or -V
QS = args.output_qs
dontquery = not args.query
script = os.path.basename(sys.argv[0])[:-3]
print('Reading ChEBI')
ont = pronto.Ontology('chebi.obo')
if dontquery is False:
print('performing query...')
ret = os.popen('wd sparql {}.rq >{}.json'.format(script, script))
if ret.close() is not None:
raise
file = open('{}.json'.format(script))
s = file.read()
jol = json.loads(s)
dups_with_pmid = False
for d in jol:
chebid = 'CHEBI:' + d.get('value').get('value')
items = d.get('items')
lab = d.get('itemLabels')
term = ont.get(chebid)
if any(xref.id.startswith('PMID') for xref in term.xrefs):
dups_with_pmid = True
print('{} items:{} |{}|'.format(chebid, items, lab))
if dups_with_pmid:
print('!!!')
if dontquery is False:
print('performing query...')
ret = os.popen('wd sparql {}.rq1 >{}1.json'.format(script, script))
if ret.close() is not None:
raise
file = open('{}1.json'.format(script))
s = file.read()
jol = json.loads(s)
chebits = {}
for d in jol:
item = d.get('item')
chebid = 'CHEBI:' + d.get('chebi')
chebits[chebid] = item
pmids = {}
print('reading dump data...')
file = open('PMID.ndjson')
for line in file.readlines():
arr = json.loads(line.strip())
qit = arr[0]
pma = arr[1]
if len(pma) == 0:
continue
pmid = pma[0]
subj = arr[2]
if subj is None:
subj = []
p = pmids.get(pmid)
if p is None:
pmids[pmid] = ([qit], subj)
else:
p[0].append(qit)
p[1].extend(subj)
blacklist = []
for chebid in chebits.keys():
if chebid in blacklist:
continue
term = ont.get(chebid)
if term is None or term.obsolete:
print("CAN'T HAPPEN: {}".format(chebid))
continue
chebit = chebits.get(chebid)
pms = []
if term.definition is not None and term.xrefs is not None:
for xref in term.xrefs:
if xref.id.startswith('PMID'):
pms.append(xref.id[5:])
for pmid in pms:
p = pmids.get(pmid)
if p is None:
print('PMID {} is missing'.format(pmid))
continue
pmits,pmsbj = p
if chebit in pmsbj:
continue
if QS:
print('{}|P921|{}|S248|Q95689128|S683|"{}"'.format(min(pmits), chebit, chebid[6:]))
else:
j = {"id": min(pmits),
"claims": {
"P921": { "value": chebit,
"references": { "P248": "Q95689128", "P683": chebid[6:]} },
}
}
f = open('t.json', 'w')
f.write(json.dumps(j))
f.close()
print(json.dumps(j), flush=True)
ret = os.popen('wd ee t.json --summary article-subj-from-chebi')
print(ret.read())
if ret.close() is not None:
print('ERROR')