-
Notifications
You must be signed in to change notification settings - Fork 5
/
search.py
341 lines (284 loc) · 11.4 KB
/
search.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import argparse
import os
import pickle
import xml.etree.ElementTree as ET
from rdkit import Chem
def directory_creation(dir_path):
try:
# Create target Directory
os.makedirs(dir_path)
print("Directory ", dir_path, " Created ")
except FileExistsError:
print("Directory ", dir_path, " already exists")
def file_open(patent_year, data_dir):
# To open the file
with open(os.path.join(data_dir, "chemistry_patent_list_" + patent_year), "rb") as fp:
chemical_patent_list = pickle.load(fp)
with open(os.path.join(data_dir, "smile_normal_list_" + patent_year), "rb") as fp:
smile_normal_list = pickle.load(fp)
with open(os.path.join(data_dir, "path_mol_normal_list_" + patent_year), "rb") as fp:
path_mol_normal_list = pickle.load(fp)
with open(os.path.join(data_dir, "path_mol_special_list_" + patent_year), "rb") as fp:
path_mol_special_list = pickle.load(fp)
with open(os.path.join(data_dir, "mol_special_list_" + patent_year), "rb") as fp:
mol_special_list = pickle.load(fp)
return (
chemical_patent_list,
smile_normal_list,
path_mol_normal_list,
path_mol_special_list,
mol_special_list,
)
def parsingXML(
subject_list,
data_dir,
chemical_patent_list,
):
print("Subject list:", subject_list)
# Parsing the XML file containing the claim of the patent
counter = 0
index_list = []
code_list = []
for patent in chemical_patent_list:
patent_ = os.path.join(data_dir, patent)
year = int(patent[:4])
# 2001-2004 have different XML structure with weird characters that are hard to escape, so treat as text
if year < 2005:
early_year = True
else:
early_year = False
# 2001 uses SGM instead of XML
if year == 2001:
xml_file = patent_ + patent_[-20:-1] + ".SGM"
else:
xml_file = patent_ + patent_[-20:-1] + ".XML"
try:
claim = ET.ElementTree(file=xml_file)
except FileNotFoundError as e:
print(e)
continue
except ET.ParseError:
if early_year:
pass
else:
print(f"Failed with ET.ParseError on {patent_}")
continue
if early_year:
with open(xml_file, "r") as f:
patent_text = f.read()
# search through whole XML as one piece of text for years 2002-2004
match = 0
for subject in subject_list:
if subject in patent_text:
match += 1
if match > 0:
counter += 1
# patent_code = patent.split("/")[-2].split("-")[0]
# find the text between PDAT and /PDAT in patent_text
country_start = patent_text.find("<B190><PDAT>")
country_end = patent_text.find("</PDAT></B190>")
country = patent_text[country_start + 12 : country_end]
docnumber_start = patent_text.find("<B110><DNUM><PDAT>")
docnumber_end = patent_text.find("</PDAT></DNUM></B110>")
docnumber = patent_text[docnumber_start + 18 : docnumber_end]
kind_start = patent_text.find("<B130><PDAT>")
kind_end = patent_text.find("</PDAT></B130>")
kind = patent_text[kind_start + 12 : kind_end]
# docnumber = 09862000 => patent code = US9862000B2
if docnumber.startswith("0"):
patent_code = country + docnumber[1:] + kind
elif docnumber.startswith("RE0"):
patent_code = country + "RE" + docnumber[3:] + kind
else:
patent_code = country + docnumber + kind
else:
# For looking into only the abstract
abstract_xml = claim.find("abstract")
abstract_text = ET.tostring(abstract_xml, method="text").decode("utf-8")
match = 0
for subject in subject_list:
if subject in abstract_text:
match += 1
# Build a list (xml_text) with the text contained in the title and bulk text of the xml file.
xml_text = []
for tag_list in ["invention-title", "p"]:
for elem in claim.iter(tag=tag_list):
xml_text.append(elem.text)
# Check for words matching the target (subject_list)
for text in xml_text:
if text is not None:
for subject in subject_list:
if subject in text:
match += 1
if match > 0:
counter += 1
# Get the patent code
for elem in claim.iter(tag="country"):
country = elem.text
break # The right one is on the first iteration
for elem in claim.iter(tag="doc-number"):
docnumber = elem.text
break
for elem in claim.iter(tag="kind"):
kind = elem.text
break
if docnumber.startswith(
"0"
): # Again, consistency! docnumber = 09862000 => patent code = US9862000B2
patent_code = country + docnumber[1:] + kind
else:
patent_code = country + docnumber + kind
if match > 0: # If match, add the index of the patent to the list (index_list)
# Index list
try:
index_list.append(chemical_patent_list.index(patent))
except ValueError as e:
print("ValueError:", e)
pass
# Code list
code_list.append(patent_code)
return index_list, code_list
def dictionary(
chemical_patent_list,
smile_normal_list,
path_mol_normal_list,
path_mol_special_list,
mol_special_list,
index_list,
code_list,
):
# Extract all molecules (path) of the patent from the patent path
index_mol_list = []
code_mol_list = (
[]
) # When we extract the molecules, this is a cheap trick to keep the code patent
for item in range(len(index_list)):
extracted_mol = [
i
for i, x in enumerate(path_mol_normal_list)
if chemical_patent_list[index_list[item]] in x
]
# Done with intermediary var To keep the length
index_mol_list.append(extracted_mol)
for j in range(len(extracted_mol)):
code_mol_list.append(code_list[item])
index_mol_flat_list = [item for sublist in index_mol_list for item in sublist]
# Building of the smile list from the correlated index of path_mol_normal_list and smile_normal_list
subject_smiles_list = []
for index in index_mol_flat_list:
subject_smiles_list.append(smile_normal_list[index])
"""BUILDING THE DICTIONARY - key: the smiles, property: the patent code
1) Currently, the patent code and the smiles are correlated by the index of their respective lists
"subject_smiles_list" and "code_list"
2) We need to split the Smiles that contains '.'. Within the loop, the smile has to be reprocessed
with RDKit and be added to the dictionary with its corresponding code (that iterates with the for loop)
"""
subject_smiles_dictionary = {}
for index in range(len(subject_smiles_list)):
# split clusters (SMILES separated by '.')
subject_smiles_splitted = subject_smiles_list[index].split(".")
for smile in subject_smiles_splitted:
# Reprocess with RDKit
resmile = Chem.MolFromSmiles(smile)
if resmile is None:
continue
else:
resmile_processed = Chem.MolToSmiles(resmile)
subject_smiles_dictionary.setdefault(resmile_processed, []).append(code_mol_list[index])
# if resmile_processed in subject_smiles_dictionary:
# subject_smiles_dictionary.setdefault(resmile_processed, []).append(code_mol_list[index])
# else:
# subject_smiles_dictionary[resmile_processed] = code_mol_list[index]
return subject_smiles_dictionary
def get_parser():
parser = argparse.ArgumentParser(description="Downloads patent files from USPTO website")
parser.add_argument(
"--years",
type=str,
nargs="+",
required=True,
help="Year(s) of patent files to download (or 'all')",
)
parser.add_argument(
"--data_dir",
type=str,
default=".",
help="Path where all data is be stored (e.g. /data/patents_data/)",
)
parser.add_argument(
"--naming",
type=str,
required=True,
help="Name of query (location where results will be stored)",
)
parser.add_argument(
"--subject_list",
type=str,
nargs="+",
required=True,
help="Terms to query (include irregular plurals and variations on words)",
)
parser.add_argument(
"--output_dir",
type=str,
default=".",
help="Parent directory of 'args.naming'",
)
return parser
def main(args):
directory_creation(os.path.join(args.output_dir, args.naming))
if args.years == ["all"]:
print("Preparing to search chemistry patents from 2001 to 2023...")
list(map(str, range(2001, 2024)))
else:
print("Preparing to search chemistry patents from", ", ".join(args.years), "...")
years = args.years
for patent_year in years:
if not os.path.exists(os.path.join(args.data_dir, patent_year)):
print(f"Directory for year {patent_year} does not exist in this data_dir")
continue
if os.path.isfile(
args.naming + "_".join(["/subject_smiles_dictionary", args.naming, patent_year])
):
print("Year " + patent_year + " already processed.")
continue
else:
print("Starting the search for " + patent_year)
# File opening
print("File opening")
(
chemical_patent_list,
smile_normal_list,
path_mol_normal_list,
path_mol_special_list,
mol_special_list,
) = file_open(patent_year, args.data_dir)
# Parsing
print("XML parsing")
index_list, code_list = parsingXML(
args.subject_list,
args.data_dir,
chemical_patent_list,
)
print("index_list:", index_list)
# Writing of the dictionary
print("Writing of the dictionary")
subject_smiles_dictionary = dictionary(
chemical_patent_list,
smile_normal_list,
path_mol_normal_list,
path_mol_special_list,
mol_special_list,
index_list,
code_list,
)
with open(
args.naming + "_".join(["/subject_smiles_dictionary", args.naming, patent_year]),
"wb",
) as file_subject_smiles_dictionary:
pickle.dump(subject_smiles_dictionary, file_subject_smiles_dictionary)
print("len(subject_smiles_dictionary):", len(subject_smiles_dictionary))
return
if __name__ == "__main__":
args = get_parser().parse_args()
main(args)