-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapPubChem2Wiki.py
172 lines (127 loc) · 3.58 KB
/
mapPubChem2Wiki.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
# mapPubChem2Wiki.py
# Author: Peter MacHarrie ([email protected])
# Date: Jan. 10, 2017
# Inputs
# PubChem attribute to wikidata attribute map (file: ./pc-wikidata-property-mapping.txt)
# PubChem cid List (stdin)
# Outputs
# Wikidata formatted tsv output (stdout)
#
#
import httplib
import time
import json
import sys
#
# PubTerm to Wiki Id Map
#
# Example:
# pub2Wiki_dict['pKa']=P117
#
# To be filled in by file below
pub2Wiki_dict = {}
#
# Request PubChemDoc Section for CID and Term
#
def getPubChemDoc (x, y):
# Input x - CID
# y - term
# Output - data1 (json in text, or "404" if invalid URL)
conn = httplib.HTTPSConnection("pubchem.ncbi.nlm.nih.gov")
#
# ExampleURL: /rest/pug_view/data/compound/1140/JSON/?response_type=display&heading=Boiling%20Point
# x y
request_txt = "/rest/pug_view/data/compound/" + x + "/JSON/?response_type=display&heading="+y
# print "rt=", request_txt
conn.request("GET", request_txt)
r1 = conn.getresponse()
data1 = ""
if r1.status == 404:
# Some cids throw an exception, return in data, improve handling later
data1="404"
else:
# otherwise, have a valid json object return
data1 = r1.read()
# print "data=", data1
return data1
#
# print the information content of the term
#
def printTerm(d, term):
#
# Input d - json structure:
# term - the term being extracted
# Output
# print term in wikidata format
#
# Recurse the json object
for key, item in d.items():
# print "key=", key, "type=", type(item), "item=", item
if type(item) is dict:
# Keep Traversing if item is a dicitionary
printTerm(item, term)
else:
if type(item) is list:
if key == 'Information': # This is what we've been looking for
# Found the information section for the term
# Print out the needed data, proof of concept, some term have more attributes that StringValue or NumValue, need to complete
for i, val in enumerate(item):
#print "i=", i, "val=", val
outString="LAST\t" + pub2Wiki_dict[term] + "\t"
if 'StringValue' in val:
outString += '"' + val['StringValue'] + '"'
if 'NumValue' in val:
outString += '"' + str(val['NumValue']) + '"'
print outString
for i, val in enumerate(item):
# For this json, Dictionary are wrapped inside list so if the list contains a dictionary
# Keep Traversing
# Otherwise done.
# print "i=", i, "val=", val, "type=", type(val)
if type(val) is dict:
# Remember, dictionaries wrapped in list so keep going
printTerm(val, term)
# else:
# print "key=", key, "item=", item
#
# Main
#
#
# Get mapping data, populate pub2Wiki_dict
#
f = open('pc-wikidata-property-mapping.txt')
for line in f:
# stripe of the linefeed
line = line.rstrip()
[pub,wiki] = line.split('\t')
pub=pub.replace(' ', '%20')
# print pub, wiki
pub2Wiki_dict[pub]=wiki
f.close()
#
# Process cids
#
#cid_list = [702, 2244, 5793, 122172997]
for cid in sys.stdin:
cid=cid.rstrip()
#
# print the wikidata header info for each cid
#
print "CREATE"
print "LAST\tP31\t\"Q11173\""
print 'LAST\tP662\t"' + str(cid) + '"'
#
# for each term passed in get the json, parse and print
#
for key, value in sorted(pub2Wiki_dict.iteritems()):
# get json
pubData=getPubChemDoc(str(cid), key)
#print pubData
#print "**********", key, "**********"
if pubData <> "404":
# get json successful
# parse
d=json.loads(pubData)
# print
printTerm(d, key)
# Done