-
Notifications
You must be signed in to change notification settings - Fork 1
/
reference_harvester_ws.py
72 lines (59 loc) · 1.9 KB
/
reference_harvester_ws.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
from flask import Flask, jsonify, request
from utils import europe_pmc_api, mongo_access, config, nlp
from itertools import chain
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/harvest/<int:pmid>", methods=["GET", "OPTIONS"])
def submit(pmid):
use_alleles = request.args.get("useAlleles")
alleles = None
if use_alleles:
with open(config.get("DEFAULT", "TARGET_ALLELE_FILE")) as f:
alleles = f.read().splitlines()
if mongo_access.exist(pmid):
reference = mongo_access.get_by_pmid(pmid)
scrub(reference)
return jsonify(reference)
reference = europe_pmc_api.get_paper_by_pmid(pmid)
if reference is None:
return jsonify({"error": "Error while trying to get data from EuropePMC"})
reference = dict(
chain(
{
"status": "pending",
"alleles": [],
"datasource": "manual",
"consortiumPaper": False,
"citations": [],
"cites": [],
"citedBy": [],
"alleleCandidates": [],
"comment": "",
"tags": [],
}.items(),
reference.items(),
)
)
reference["firstPublicationDate"] = str(
reference["firstPublicationDate"].isoformat()
)
reference = nlp.get_fragments(reference, alleles)
return reference
def scrub(obj, bad_key="_id"):
if isinstance(obj, dict):
for key in list(obj.keys()):
if key == bad_key:
del obj[key]
else:
scrub(obj[key], bad_key)
elif isinstance(obj, list):
for i in reversed(range(len(obj))):
if obj[i] == bad_key:
del obj[i]
else:
scrub(obj[i], bad_key)
else:
pass
if __name__ == "__main__":
app.run(debug=True, port=8000, host="0.0.0.0")