-
Notifications
You must be signed in to change notification settings - Fork 16
/
models.py
453 lines (361 loc) · 12.9 KB
/
models.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from portality.lib import dataobj
from portality.lib import xmlutil as xutil
from lxml import etree
class JATSException(Exception):
def __init__(self, message, rawstring, *args, **kwargs):
super(JATSException, self).__init__(message, *args, **kwargs)
self.raw = rawstring
class EPMCFullTextException(JATSException):
"""
Here for backwards compatibility
"""
pass
class EPMCMetadataException(Exception):
def __init__(self, message, rawstring, *args, **kwargs):
super(EPMCMetadataException, self).__init__(message, *args, **kwargs)
self.raw = rawstring
class EPMCMetadataXML(object):
def __init__(self, raw=None, xml=None):
self.raw = None
self.xml = None
if raw is not None:
self.raw = raw
try:
self.xml = etree.fromstring(self.raw)
except:
raise JATSException("Unable to parse XML", self.raw)
elif xml is not None:
self.xml = xml
def tostring(self):
if self.raw is not None:
return self.raw
elif self.xml is not None:
return etree.tostring(self.xml)
@property
def title(self):
return xutil.xp_first_text(self.xml, "title")
@property
def publication_type(self):
return xutil.xp_first_text(self.xml, "//pubTypeList/pubType")
@property
def language(self):
return xutil.xp_first_text(self.xml, "language")
@property
def publication_date(self):
pd = xutil.xp_first_text(self.xml, "firstPublicationDate")
if pd is not None:
return pd
pd = xutil.xp_first_text(self.xml, "electronicPublicationDate")
if pd is not None:
return pd
pd = xutil.xp_first_text(self.xml, "//journalInfo/printPublicationDate")
return pd
@property
def pmid(self):
return xutil.xp_first_text(self.xml, "pmid")
@property
def pmcid(self):
return xutil.xp_first_text(self.xml, "pmcid")
@property
def doi(self):
return xutil.xp_first_text(self.xml, "DOI")
@property
def issns(self):
issn = xutil.xp_first_text(self.xml, "//journalInfo/journal/ISSN")
essn = xutil.xp_first_text(self.xml, "//journalInfo/journal/ESSN")
issns = []
if issn is not None:
issns.append(issn)
if essn is not None:
issns.append(essn)
return issns
@property
def keywords(self):
return xutil.xp_texts(self.xml, "//keywordList/keyword")
@property
def author_string(self):
return xutil.xp_first_text(self.xml, "//authorString")
@property
def authors(self):
"""
<fullName>Cerasoli E</fullName>
<firstName>Eleonora</firstName>
<lastName>Cerasoli</lastName>
<initials>E</initials>
<affiliation>Biotechnology Department, National Physical Laboratory Teddington, UK.</affiliation>
"""
author_elements = self.xml.xpath("//authorList/author")
obs = []
for ael in author_elements:
ao = {}
fn = ael.find("fullName")
if fn is not None:
ao["fullName"] = fn.text
first = ael.find("firstName")
if first is not None:
ao["firstName"] = first.text
last = ael.find("lastName")
if last is not None:
ao["lastName"] = last.text
inits = ael.find("initials")
if inits is not None:
ao["initials"] = inits.text
aff = ael.find("affiliation")
if aff is not None:
ao["affiliation"] = aff.text
if len(list(ao.keys())) > 0:
obs.append(ao)
return obs
@property
def grants(self):
grant_elements = self.xml.xpath("//grantsList/grant")
obs = []
for ael in grant_elements:
go = {}
gid = ael.find("grantId")
if gid is not None:
go["grantId"] = gid.text
ag = ael.find("agency")
if ag is not None:
go["agency"] = ag.text
if len(list(go.keys())) > 0:
obs.append(go)
return obs
@property
def mesh_descriptors(self):
return xutil.xp_texts(self.xml, "//meshHeadingList/meshHeading/descriptorName")
class EPMCMetadata(dataobj.DataObj):
def __init__(self, raw):
super(EPMCMetadata, self).__init__(raw)
@property
def pmcid(self):
return self._get_single("pmcid", self._utf8_unicode(), allow_coerce_failure=False)
@property
def pmid(self):
return self._get_single("pmid", self._utf8_unicode(), allow_coerce_failure=False)
@property
def doi(self):
return self._get_single("doi", self._utf8_unicode(), allow_coerce_failure=False)
@property
def in_epmc(self):
return self._get_single("inEPMC", self._utf8_unicode(), allow_coerce_failure=False)
@property
def is_oa(self):
return self._get_single("isOpenAccess", self._utf8_unicode(), allow_coerce_failure=False)
@property
def issn(self):
return self._get_single("journalInfo.journal.issn", self._utf8_unicode(), allow_coerce_failure=False)
@property
def journal(self):
return self._get_single("journalInfo.journal.title", self._utf8_unicode(), allow_coerce_failure=False)
@property
def essn(self):
return self._get_single("journalInfo.journal.essn", self._utf8_unicode(), allow_coerce_failure=False)
@property
def title(self):
return self._get_single("title", self._utf8_unicode(), allow_coerce_failure=False)
@property
def journal_volume(self):
return self._get_single("journalInfo.volume", self._utf8_unicode())
@property
def journal_issue(self):
return self._get_single("journalInfo.issue", self._utf8_unicode())
@property
def language(self):
return self._get_single("language", self._utf8_unicode())
@property
def month_of_publication(self):
return self._get_single("journalInfo.monthOfPublication", dataobj.to_int())
@property
def year_of_publication(self):
return self._get_single("journalInfo.yearOfPublication", dataobj.to_int())
@property
def page_info(self):
return self._get_single("pageInfo", self._utf8_unicode())
@property
def start_page(self):
pi = self.page_info
if pi is None:
return None
bits = pi.split("-")
if len(bits) > 0:
return bits[0]
return None
@property
def end_page(self):
pi = self.page_info
if pi is None:
return None
bits = pi.split("-")
if len(bits) > 1:
return bits[1]
return None
@property
def fulltext_urls(self):
return self._get_list("fullTextUrlList.fullTextUrl")
def get_first_fulltext_url(self, availability=None, document_style=None, site=None):
for obj in self.fulltext_urls:
if availability is not None and availability != obj.get("availability"):
continue
if document_style is not None and document_style != obj.get("documentStyle"):
continue
if site is not None and site != obj.get("site"):
continue
return obj.get("url")
return None
@property
def abstract(self):
return self._get_single("abstractText", self._utf8_unicode())
@property
def authors(self):
return self._get_list("authorList.author")
@property
def author_string(self):
return self._get_single("authorString")
class JATS(object):
def __init__(self, raw=None, xml=None):
self.raw = None
self.xml = None
if raw is not None:
self.raw = raw
try:
self.xml = etree.fromstring(self.raw)
except:
raise JATSException("Unable to parse XML", self.raw)
elif xml is not None:
self.xml = xml
@property
def title(self):
return xutil.xp_first_text(self.xml, "//title-group/article-title")
@property
def is_aam(self):
manuscripts = self.xml.xpath("//article-id[@pub-id-type='manuscript']")
return len(manuscripts) > 0
def get_licence_details(self):
# get the licence type
l = self.xml.xpath("//license")
if len(l) > 0:
l = l[0]
else:
return None, None, None
type = l.get("license-type")
url = l.get("{http://www.w3.org/1999/xlink}href")
# get the paragraph(s) describing the licence
para = self.xml.xpath("//license/license-p")
out = ""
for p in para:
out += etree.tostring(p)
return type, url, out
@property
def copyright_statement(self):
return xutil.xp_first_text(self.xml, "//copyright-statement")
@property
def categories(self):
return xutil.xp_texts(self.xml, "//article-categories/subj-group/subject")
@property
def authors(self):
aels = self.xml.xpath("//contrib-group/contrib[@contrib-type='author']")
return self._make_contribs(aels)
@property
def contribs(self):
cs = self.xml.xpath("//contrib-group/contrib")
return self._make_contribs(cs)
@property
def emails(self):
return xutil.xp_texts(self.xml, "//email")
@property
def keywords(self):
return xutil.xp_texts(self.xml, "//kwd-group/kwd")
@property
def publisher(self):
return xutil.xp_first_text(self.xml, "//publisher/publisher-name")
@property
def publication_date(self):
# first look for an explicit publication date
pds = self.xml.xpath("//article-meta/pub-date[@date-type='pub']")
if len(pds) > 0:
return self._make_date(pds[0])
# if not, look for exactly one pub-date and use that
pds = self.xml.xpath("//article-meta/pub-date")
if len(pds) == 1:
return self._make_date(pds[0])
# otherwise, insufficient information
return None
@property
def date_accepted(self):
das = self.xml.xpath("//history/date[@date-type='accepted']")
if len(das) > 0:
return self._make_date(das[0])
@property
def date_submitted(self):
rcs = self.xml.xpath("//history/date[@date-type='received']")
if len(rcs) > 0:
return self._make_date(rcs[0])
@property
def issn(self):
return xutil.xp_texts(self.xml, "//journal-meta/issn")
@property
def pmcid(self):
id = xutil.xp_first_text(self.xml, "//article-meta/article-id[@pub-id-type='pmcid']")
if id is not None and not id.startswith("PMC"):
id = "PMC" + id
return id
@property
def doi(self):
return xutil.xp_first_text(self.xml, "//article-meta/article-id[@pub-id-type='doi']")
def _make_date(self, element):
ob = xutil.objectify(element)
year = ob.get("year")
month = ob.get("month", "01")
day = ob.get("day", "01")
if len(month) < 2:
month = "0" + month
if len(day) < 2:
day = "0" + day
if year is None or len(year) != 4:
return None
return year + "-" + month + "-" + day
def _make_contribs(self, elements):
obs = []
for c in elements:
con = {}
# first see if there is a name we can pull out
name = c.find("name")
if name is not None:
sn = name.find("surname")
if sn is not None:
con["surname"] = sn.text
gn = name.find("given-names")
if gn is not None:
con["given-names"] = gn.text
# see if there's an email address
email = c.find("email")
if email is not None:
con["email"] = email.text
# now do the affiliations (by value and by (x)reference)
affs = []
aff = c.find("aff")
if aff is not None:
contents = aff.xpath("string()")
norm = " ".join(contents.split())
affs.append(norm)
xrefs = c.findall("xref")
for x in xrefs:
if x.get("ref-type") == "aff":
affid = x.get("rid")
xp = "//aff[@id='" + affid + "']"
aff_elements = self.xml.xpath(xp)
for ae in aff_elements:
contents = ae.xpath("string()")
norm = " ".join(contents.split())
affs.append(norm)
if len(affs) > 0:
con["affiliations"] = affs
if len(list(con.keys())) > 0:
obs.append(con)
return obs
def tostring(self):
if self.raw is not None:
return self.raw
elif self.xml is not None:
return etree.tostring(self.xml)