-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindexapi_v2.py
502 lines (425 loc) · 17.2 KB
/
indexapi_v2.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2023, Silvio Peroni <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright notice
# and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
__author__ = 'Arcangelo Massari & Ivan Heibi'
from urllib.parse import quote, unquote
from requests import get,post
from rdflib import Graph, URIRef
from re import sub,findall
from json import loads
from datetime import datetime
from dateutil.relativedelta import relativedelta
from dateutil.parser import parse
from collections import defaultdict
def lower(s):
return s.lower(),
def encode(s):
return quote(s),
def id2omid(s):
if "omid" in s:
return s.replace("omid:br/",""),
return __get_omid_of(s),
def id2omids(s):
if "omid" in s:
return s.replace("omid:br/","<https://w3id.org/oc/meta/br/") +">",
return __get_omid_of(s, multi = True),
def __get_omid_of(s, multi = False):
MULTI_VAL_MAX = 9000
sparql_endpoint = "https://test.opencitations.net/meta/sparql"
# SPARQL query
is_journal = False
br_pre_l = ["doi","issn","isbn","pmid","pmcid","url","wikidata","wikipedia","jid","arxiv"]
for br_pre in br_pre_l:
if s.startswith(br_pre+":"):
is_journal = br_pre in ["issn"]
s = s.replace(br_pre+":","")
break
sparql_query = """
PREFIX datacite: <http://purl.org/spar/datacite/>
PREFIX literal: <http://www.essepuntato.it/2010/06/literalreification/>
SELECT ?br {
?identifier literal:hasLiteralValue '"""+s+"""'.
?br datacite:hasIdentifier ?identifier
}
"""
# in case is a journal the SAPRQL query retrieves all associated BRs
if is_journal:
sparql_query = """
PREFIX datacite: <http://purl.org/spar/datacite/>
PREFIX literal: <http://www.essepuntato.it/2010/06/literalreification/>
PREFIX ns1: <http://purl.org/vocab/frbr/core#>
PREFIX fabio: <http://purl.org/spar/fabio/>
SELECT ?br {
?identifier literal:hasLiteralValue '"""+s+"""' .
?venue datacite:hasIdentifier ?identifier .
{?br ns1:partOf ?venue .}
UNION { ?br ns1:partOf/ns1:partOf ?venue . }
UNION { ?br ns1:partOf/ns1:partOf/ns1:partOf ?venue . }
UNION { ?br ns1:partOf/ns1:partOf/ns1:partOf/ns1:partOf ?venue .}
?br a fabio:JournalArticle .
}
"""
headers={"Accept": "application/sparql-results+json", "Content-Type": "application/sparql-query"}
data = {"query": sparql_query}
omid_l = []
try:
response = post(sparql_endpoint, headers=headers, data=sparql_query, timeout=45)
if response.status_code == 200:
r = loads(response.text)
results = r["results"]["bindings"]
if len(results) > 0:
for elem in results:
omid_val = elem["br"]["value"].split("meta/br/")[1]
omid_l.append(omid_val)
except:
return ""
if multi:
if len(omid_l) == 0:
return ""
else:
sparql_values = []
for i in range(0, len(omid_l), MULTI_VAL_MAX):
sparql_values.append( " ".join(["<https://w3id.org/oc/meta/br/"+e+">" for e in omid_l[i:i + MULTI_VAL_MAX]]) )
return sparql_values
if len(omid_l) == 0:
return ""
elif len(omid_l) == 1:
return omid_l[0]
else:
# return the citation/reference count of all OMIDs
return __call_tp_for_citations(omid_l)
def __call_tp_for_citations(omid_l):
MAX_VALUES = 9900
part_omid_l = omid_l[:MAX_VALUES]
rest_omid_l = omid_l[MAX_VALUES:]
# Check the OMID which has more citations/references
sparql_values = " ".join(["<https://w3id.org/oc/meta/br/"+e+">" for e in part_omid_l])
sparql_endpoint = "http://127.0.0.1/index/sparql"
sparql_query = """
PREFIX cito:<http://purl.org/spar/cito/>
SELECT ?cited (COUNT(?citation) as ?citation_count) WHERE {
VALUES ?cited {"""+sparql_values+"""} .
?citation cito:hasCitedEntity ?cited .
} GROUP BY ?cited
"""
try:
headers={"Accept": "application/sparql-results+json", "Content-Type": "application/sparql-query"}
response = post(sparql_endpoint, headers=headers, data=sparql_query, timeout=45)
if response.status_code == 200:
r = loads(response.text)
results = r["results"]["bindings"]
max_cits = -1
res_omid = part_omid_l[0]
if len(results) > 0:
for elem in results:
cits_num = elem["citation_count"]["value"]
if int(cits_num) > max_cits:
res_omid = elem["cited"]["value"].split("meta/br/")[1]
max_cits = int(cits_num)
if len(rest_omid_l) == 0:
return res_omid
else:
return res_omid + __call_tp_for_citations(rest_omid_l)
except:
return omid_l[0]
# args must contain the <count>
def sum_all(res, *args):
header = res[0]
try:
count_idx = header.index(args[0])
tot_count = 0
for idx, row in enumerate(res[1:]):
tot_count += int(row[count_idx][1])
# delete the item + citing + cited columns
res = [header,[str(tot_count)]]
return res, True
except:
return [], True
def count_unique_brs(res, *args):
header = res[0]
idx_omid_br_uri = header.index(args[0])
count_brs = 0
if len(res) > 1:
l_brs = []
for idx, row in enumerate(res[1:]):
l_brs.append(row[idx_omid_br_uri][1])
l_brs = ["<"+_c+">" for _c in l_brs]
l_brs_anyids = __get_ids_from_meta(l_brs)
unique_brs_anyid = []
for _l in l_brs_anyids:
s = set(_l)
# check the unique br anyids
_c_intersection = 0
for __unique in unique_brs_anyid:
_c_intersection += len(__unique.intersection(s))
# if there is no common anyids with the other br entities
if _c_intersection == 0:
unique_brs_anyid.append(s)
count_brs = len(unique_brs_anyid)
res = [["count"],[count_brs]]
return res, True
# args must contain the <citing> and <cited>
def citations_info(res, *args):
header = res[0]
oci_idx = header.index(args[0])
citing_idx = header.index(args[1])
cited_idx = header.index(args[2])
# ids managed – ordered by relevance
all_ids = ["doi","pmid"]
if len(args) > 3:
all_ids = args[3].split("__")
index_meta = {}
#all_entities = ["omid:br/06101068294","omid:br/0610123167","omid:br/06101494166"]
res_entities = {}
all_entities = []
if len(res) > 1:
for idx, row in enumerate(res[1:]):
res_entities[idx] = []
res_entities[idx] += [__get_omid_str(row[citing_idx][1]), __get_omid_str(row[cited_idx][1])]
all_entities += [__get_omid_str(row[citing_idx][1]), __get_omid_str(row[cited_idx][1])]
# delete the item + citing + cited columns
res = [[elem for idx, elem in enumerate(row) if idx != oci_idx and idx != citing_idx and idx != cited_idx] for row in res]
additional_fields = ["oci", "citing", "cited", "creation", "timespan", "journal_sc","author_sc"]
header = res[0]
header.extend(additional_fields)
# call __ocmeta_parser for each STEP entities each time
r = {}
STEP = 8
all_entities = list(set(all_entities))
all_entities = ["<"+e+">" for e in all_entities]
r = __br_meta_metadata(all_entities)
# process and elaborate additional fields
#creation = entities_data["citing"][1]
if len(res) > 1:
for idx, row in enumerate(res[1:]):
citing_entity = res_entities[idx][0]
cited_entity = res_entities[idx][1]
oci_val = __get_omid_str(citing_entity,True)+"-"+__get_omid_str(cited_entity,True)
citing_id = ""
citing_pubdate = ""
if citing_entity in r:
citing_id = __get_all_pids(r[citing_entity], citing_entity)
citing_pubdate = __get_pub_date(r[citing_entity])
cited_id = ""
duration = ""
journal_sc = ""
author_sc = ""
if citing_entity in r and cited_entity in r:
cited_id = __get_all_pids(r[cited_entity], cited_entity)
duration = __cit_duration(__get_pub_date(r[citing_entity]),__get_pub_date(r[cited_entity]))
journal_sc = __cit_journal_sc(__get_source(r[citing_entity]),__get_source(r[cited_entity]))
author_sc = __cit_author_sc(__get_author(r[citing_entity]),__get_author(r[cited_entity]))
# in case its the API of POCI add the prefix
if len(all_ids) == 1 and "pmid" in all_ids:
if oci_val != "":
oci_val = "oci:"+oci_val
if citing_id != "":
citing_id = "pmid:"+citing_id
if cited_id != "":
cited_id = "pmid:"+cited_id
# pre_source = ""
# if len(all_ids) > 1:
# if citing_id.startswith("10."):
# pre_source = "coci => "
# else:
# pre_source = "poci => "
row.extend([
# oci value
oci_val,
# citing
citing_id,
# cited
cited_id,
# creation = citing[pub_date]
citing_pubdate,
# timespan = citing[pub_date] - cited[pub_date]
duration,
# journal_sc = compare citing[source_id] and cited[source_id]
journal_sc,
# author_sc = compare citing[source_id] and cited[source_id]
author_sc
])
return res, True
def __get_omid_str(val, reverse = False):
if not reverse:
return "https://w3id.org/oc/meta/"+val.split("oc/meta/")[1]
return val.replace("https://w3id.org/oc/meta/br/","")
def __get_all_pids(elem, uri_omid):
str_omid = "omid:br/"+__get_omid_str(uri_omid,True)
str_ids = [str_omid]
if "ids" in elem:
for id in elem["ids"]["value"].split(" __ "):
str_ids.append(id)
return " ".join(str_ids)
def __get_pub_date(elem):
if "pubDate" in elem:
return elem["pubDate"]["value"]
return ""
def __get_source(elem):
if "source" in elem:
return elem["source"]["value"].split("; ")
return ""
def __get_author(elem):
if "author" in elem:
return elem["author"]["value"].split("; ")
return ""
def __cit_journal_sc(citing_source_ids, cited_source_ids):
if len(set(citing_source_ids).intersection(set(cited_source_ids))) > 0:
return "yes"
return "no"
def __cit_author_sc(citing_authors, cited_authors):
if len(set(citing_authors).intersection(set(cited_authors))) > 0:
return "yes"
return "no"
def __cit_duration(citing_complete_pub_date, cited_complete_pub_date):
def ___contains_years(date):
return date is not None and len(date) >= 4
def ___contains_months(date):
return date is not None and len(date) >= 7
def ___contains_days(date):
return date is not None and len(date) >= 10
DEFAULT_DATE = datetime(1970, 1, 1, 0, 0)
consider_months = ___contains_months(citing_complete_pub_date) and ___contains_months(cited_complete_pub_date)
consider_days = ___contains_days(citing_complete_pub_date) and ___contains_days(cited_complete_pub_date)
try:
if citing_complete_pub_date == "" or citing_complete_pub_date == None:
return ""
citing_pub_datetime = parse(
citing_complete_pub_date, default=DEFAULT_DATE
)
except ValueError: # It is not a leap year
citing_pub_datetime = parse(
citing_complete_pub_date[:7] + "-28", default=DEFAULT_DATE
)
try:
if cited_complete_pub_date == "" or cited_complete_pub_date == None:
return ""
cited_pub_datetime = parse(
cited_complete_pub_date, default=DEFAULT_DATE
)
except ValueError: # It is not a leap year
cited_pub_datetime = parse(
cited_complete_pub_date[:7] + "-28", default=DEFAULT_DATE
)
delta = relativedelta(citing_pub_datetime, cited_pub_datetime)
result = ""
if (
delta.years < 0
or (delta.years == 0 and delta.months < 0 and consider_months)
or (
delta.years == 0
and delta.months == 0
and delta.days < 0
and consider_days
)
):
result += "-"
result += "P%sY" % abs(delta.years)
if consider_months:
result += "%sM" % abs(delta.months)
if consider_days:
result += "%sD" % abs(delta.days)
return result
def __get_ids_from_meta(values, finalres = None):
if finalres == None:
finalres = []
MAX_VALUES = 2000
values_part = values[:MAX_VALUES]
values_rest = values[MAX_VALUES:]
sparql_endpoint = "https://test.opencitations.net/meta/sparql"
sparql_query = """
PREFIX datacite: <http://purl.org/spar/datacite/>
PREFIX literal: <http://www.essepuntato.it/2010/06/literalreification/>
SELECT ?br_omid ?identifier_val ?scheme {
VALUES ?br_omid { """+" ".join(values_part)+""" }
?br_omid datacite:hasIdentifier ?identifier .
?identifier literal:hasLiteralValue ?identifier_val .
?identifier datacite:usesIdentifierScheme ?scheme .
}
"""
headers={"Accept": "application/sparql-results+json", "Content-Type": "application/sparql-query"}
data = {"query": sparql_query}
res = dict()
try:
response = post(sparql_endpoint, headers=headers, data=sparql_query)
if response.status_code == 200:
r = loads(response.text)
results = r["results"]["bindings"]
if len(results) > 0:
for elem in results:
omid_br = elem["br_omid"]["value"]
anyid_pref = elem["scheme"]["value"].split("datacite/")[1]
anyid_val = elem["identifier_val"]["value"]
if omid_br not in res:
res[omid_br] = []
res[omid_br].append(anyid_pref+":"+anyid_val)
except:
pass
finalres += [res[k] for k in res]
res = None
if len(values_rest) > 0:
return __get_ids_from_meta(values_rest, finalres)
else:
return finalres
def __br_meta_metadata(values):
sparql_endpoint = "https://test.opencitations.net/meta/sparql"
# SPARQL query
sparql_query = """
PREFIX pro: <http://purl.org/spar/pro/>
PREFIX frbr: <http://purl.org/vocab/frbr/core#>
PREFIX fabio: <http://purl.org/spar/fabio/>
PREFIX datacite: <http://purl.org/spar/datacite/>
PREFIX literal: <http://www.essepuntato.it/2010/06/literalreification/>
PREFIX prism: <http://prismstandard.org/namespaces/basic/2.0/>
SELECT DISTINCT ?val ?pubDate (GROUP_CONCAT(DISTINCT ?id; SEPARATOR=' __ ') AS ?ids) (GROUP_CONCAT(?venue; separator="; ") as ?source) (GROUP_CONCAT(?raAuthor; separator="; ") as ?author)
WHERE {
VALUES ?val { """+" ".join(values)+""" }
OPTIONAL { ?val prism:publicationDate ?pubDate. }
OPTIONAL {
?val datacite:hasIdentifier ?identifier.
?identifier datacite:usesIdentifierScheme ?scheme;
literal:hasLiteralValue ?literalValue.
BIND(CONCAT(STRAFTER(STR(?scheme), "http://purl.org/spar/datacite/"), ":", ?literalValue) AS ?id)
}
OPTIONAL {
{
?val a fabio:JournalArticle;
frbr:partOf+ ?venue.
?venue a fabio:Journal.
} UNION {
?val frbr:partOf ?venue.
}
}
OPTIONAL {
?val pro:isDocumentContextFor ?arAuthor.
?arAuthor pro:withRole pro:author;
pro:isHeldBy ?raAuthor.
}
} GROUP BY ?val ?pubDate
"""
headers={"Accept": "application/sparql-results+json", "Content-Type": "application/sparql-query"}
data = {"query": sparql_query}
try:
response = post(sparql_endpoint, headers=headers, data=sparql_query)
if response.status_code == 200:
r = loads(response.text)
results = r["results"]["bindings"]
res_json = {}
if len(results) > 0:
for elem in results:
res_json[elem["val"]["value"]] = elem
return res_json
except:
return None