-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsePoaXml.py
445 lines (333 loc) · 12.7 KB
/
parsePoaXml.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
import os
from generatePoaXml import *
import settings
from elifetools import parseJATS as parser
import re
"""
Parse PoA XML file and can instantiate a eLifePOA object from the data
"""
def text_from_affiliation_elements(department, institution, city, country):
"""
Given an author affiliation from
"""
text = ""
for element in (department, institution, city, country):
if text != "":
text += ", "
if element:
text += element
return text
def build_contributors(authors, contrib_type):
"""
Given a list of authors from the parser, instantiate contributors
objects and build them
"""
contributors = []
for author in authors:
contributor = None
author_contrib_type = contrib_type
surname = author.get("surname")
given_name = author.get("given-names")
collab = author.get("collab")
# Small hack for on-behalf-of type when building authors
# use on-behalf-of as the contrib_type
if author.get("type") and author.get("type") == "on-behalf-of":
collab = author.get("on-behalf-of")
author_contrib_type = "on-behalf-of"
if surname or collab:
contributor = eLifePOSContributor(author_contrib_type, surname, given_name, collab)
else:
continue
contributor.group_author_key = author.get("group-author-key")
contributor.orcid = author.get("orcid")
if author.get("corresp"):
contributor.corresp = True
else:
contributor.corresp = False
# Affiliations, compile text for each
department = []
institution = []
city = []
country = []
if author.get("affiliations"):
for aff in author.get("affiliations"):
department.append(aff.get("dept"))
institution.append(aff.get("institution"))
city.append(aff.get("city"))
country.append(aff.get("country"))
# Turn the set of lists into ContributorAffiliation
for index in range(0, len(institution)):
affiliation = ContributorAffiliation()
affiliation.department = department[index]
affiliation.institution = institution[index]
affiliation.city = city[index]
affiliation.country = country[index]
affiliation.text = text_from_affiliation_elements(
affiliation.department,
affiliation.institution,
affiliation.city,
affiliation.country)
contributor.set_affiliation(affiliation)
# Finally add the contributor to the list
if contributor:
contributors.append(contributor)
return contributors
def build_funding(award_groups):
"""
Given a funding data, format it
"""
if not award_groups:
return []
funding_awards = []
for award_group in award_groups:
for id, award_group in award_group.iteritems():
award = eLifeFundingAward()
if award_group.get('id-type') == "FundRef":
award.institution_id = award_group.get('id')
award.institution_name = award_group.get('institution')
# TODO !!! Check for multiple award_id, if exists
if award_group.get('award-id'):
award.add_award_id(award_group.get('award-id'))
funding_awards.append(award)
return funding_awards
def build_ref_list(refs):
"""
Given parsed references build a list of ref objects
"""
ref_list = []
for reference in refs:
ref = eLifeRef()
# Publcation Type
if reference.get('publication-type'):
ref.publication_type = reference.get('publication-type')
# Article title
if reference.get('article_title'):
ref.article_title = reference.get('article_title')
# Article title
if reference.get('source'):
ref.source = reference.get('source')
# Volume
if reference.get('volume'):
ref.volume = reference.get('volume')
# First page
if reference.get('fpage'):
ref.fpage = reference.get('fpage')
# Last page
if reference.get('lpage'):
ref.lpage = reference.get('lpage')
# DOI
if reference.get('reference_id'):
ref.doi = reference.get('reference_id')
# Year
if reference.get('year'):
ref.year = reference.get('year')
# elocation-id
if reference.get('elocation-id'):
ref.elocation_id = reference.get('elocation-id')
# Authors
if reference.get('authors'):
for author in reference.get('authors'):
# TODO - Need to parse surname and given-names
ref_author = {}
if 'surname' in author:
ref_author['surname'] = author['surname']
ref.add_author(ref_author)
elif 'collab' in author:
ref_author['collab'] = author['collab']
ref.add_author(ref_author)
ref_list.append(ref)
return ref_list
def component_title(component):
"""
Label, title and caption
Title is the label text plus the title text
Title may contain italic tag, etc.
"""
title = u''
label_text = u''
title_text = u''
if component.get('label'):
label_text = component.get('label')
if component.get('title'):
title_text = component.get('title')
title = unicode(label_text)
if label_text != '' and title_text != '':
title += ' '
title += unicode(title_text)
if component.get('type') == 'abstract' and title == '':
title = 'Abstract'
return title
def build_components(components):
"""
Given parsed components build a list of component objects
"""
component_list = []
for comp in components:
component = eLifeComponent()
# DOI and Resource URL
if comp.get('doi'):
component.doi = comp.get('doi')
# Based on lookup URL path logic
doi_resource = "http://elifesciences.org/lookup/doi/" + comp.get('doi')
component.doi_resource = doi_resource
if component_title(comp) != '':
component.title = component_title(comp)
# Subtitle
if comp.get('type') in ['supplementary-material', 'fig']:
if comp.get('full_caption'):
subtitle = comp.get('full_caption')
subtitle = clean_abstract(subtitle)
component.subtitle = subtitle
# Mime type
if comp.get('type') in ['abstract', 'table-wrap', 'sub-article',
'chem-struct-wrap', 'boxed-text']:
component.mime_type = 'text/plain'
if comp.get('type') in ['fig']:
component.mime_type = 'image/tiff'
elif comp.get('type') in ['media', 'supplementary-material']:
if comp.get('mimetype') and comp.get('mime-subtype'):
component.mime_type = (comp.get('mimetype') + '/'
+ comp.get('mime-subtype'))
# Permissions
component.permissions = comp.get('permissions')
# Append it to our list of components
component_list.append(component)
return component_list
def build_related_articles(related_articles):
"""
Given parsed data build a list of related article objects
"""
article_list = []
for related_article in related_articles:
article = eLifeRelatedArticle()
if related_article.get('xlink_href'):
article.xlink_href = related_article.get('xlink_href')
if related_article.get('related_article_type'):
article.related_article_type = related_article.get('related_article_type')
if related_article.get('ext_link_type'):
article.ext_link_type = related_article.get('ext_link_type')
# Append it to our list
article_list.append(article)
return article_list
def remove_tag(tag_name, string):
"""
Remove open and close tags - the tags themselves only - using
a non-greedy angle bracket pattern match
"""
if not string:
return string
p = re.compile('</?' + tag_name + '.*?>')
string = p.sub('', string)
return string
def clean_abstract(abstract):
"""
Remove unwanted tags from abstract string,
parsing it as HTML, then only keep the body paragraph contents
"""
remove_tags = ['xref', 'ext-link', 'inline-formula', 'mml:*']
for tag_name in remove_tags:
abstract = remove_tag(tag_name, abstract)
return abstract
def build_article_from_xml(article_xml_filename, detail="brief"):
"""
Parse JATS XML with elifetools parser, and populate an
eLifePOA article object
Basic data crossref needs: article_id, doi, title, contributors with names set
detail="brief" is normally enough,
detail="full" will populate all the contributor affiliations that are linked by xref tags
"""
error_count = 0
soup = parser.parse_document(article_xml_filename)
# Get DOI
doi = parser.doi(soup)
# Create the article object
article = eLifePOA(doi, title=None)
# Related articles
article.related_articles = build_related_articles(parser.related_article(soup))
# Get publisher_id and set object manuscript value
publisher_id = parser.publisher_id(soup)
article.manuscript = publisher_id
# Set the articleType
article_type = parser.article_type(soup)
if article_type:
article.articleType = article_type
# title
article.title = parser.full_title(soup)
#print article.title
# abstract
article.abstract = clean_abstract(parser.full_abstract(soup))
# digest
article.digest = clean_abstract(parser.full_digest(soup))
# elocation-id
article.elocation_id = parser.elocation_id(soup)
# contributors
all_contributors = parser.contributors(soup, detail)
author_contributors = filter(lambda con: con.get('type')
in ['author', 'on-behalf-of'], all_contributors)
contrib_type = "author"
contributors = build_contributors(author_contributors, contrib_type)
contrib_type = "author non-byline"
authors = parser.authors_non_byline(soup, detail)
contributors_non_byline = build_contributors(authors, contrib_type)
article.contributors = contributors + contributors_non_byline
# license href
license = eLifeLicense()
license.href = parser.license_url(soup)
article.license = license
# article_category
article.article_categories = parser.category(soup)
# keywords
article.author_keywords = parser.keywords(soup)
# research organisms
article.research_organisms = parser.research_organism(soup)
# funding awards
article.funding_awards = build_funding(parser.full_award_groups(soup))
# references or citations
article.ref_list = build_ref_list(parser.refs(soup))
# components with component DOI
article.component_list = build_components(parser.components(soup))
# History dates
date_types = ["received", "accepted"]
for date_type in date_types:
history_date = parser.history_date(soup, date_type)
if history_date:
date_instance = eLifeDate(date_type, history_date)
article.add_date(date_instance)
# Pub date
pub_date = parser.pub_date(soup)
if pub_date:
date_instance = eLifeDate("pub", pub_date)
article.add_date(date_instance)
# Set the volume if present
volume = parser.volume(soup)
if volume:
article.volume = volume
article.is_poa = parser.is_poa(soup)
return article, error_count
def build_articles_from_article_xmls(article_xmls):
"""
Given a list of article XML filenames, convert to article objects
"""
poa_articles = []
detail = "full"
for article_xml in article_xmls:
print "working on ", article_xml
article, error_count = build_article_from_xml(article_xml, detail)
if error_count == 0:
poa_articles.append(article)
return poa_articles
if __name__ == '__main__':
article_xlms = [#"elife_poa_e02935.xml"
#,"Feature.xml"
"elife_poa_e02923.xml"
, "elife00003.xml"
, "elife02676.xml"
]
poa_articles = []
for article_xml in article_xlms:
print "working on ", article_xml
article, error_count = build_article_from_xml("generated_xml_output" +
os.sep + article_xml, detail="full")
if error_count == 0:
poa_articles.append(article)
print article.doi