Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curations using INDRA DB REST API #200

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion emmaa/analyze_tests_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from indra.statements.statements import Statement
from indra.assemblers.english.assembler import EnglishAssembler
from indra.literature import pubmed_client, crossref_client, pmc_client
from indra.sources.indra_db_rest import get_curations
from indra_db import get_db
from indra_db.client.principal.curation import get_curations
from indra_db.util import unpack


Expand Down
2 changes: 1 addition & 1 deletion emmaa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from indra.pipeline import AssemblyPipeline, register_pipeline
from indra.tools.assemble_corpus import filter_grounded_only
from indra.sources.minerva import process_from_web
from indra_db.client.principal.curation import get_curations
from indra.sources.indra_db_rest import get_curations
from indra_db.util import get_db, _get_trids
from emmaa.priors import SearchTerm
from emmaa.readers.aws_reader import read_pmid_search_terms
Expand Down
64 changes: 47 additions & 17 deletions emmaa_service/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
RemoveModification, get_statement_by_name, stmts_to_json
from indra.assemblers.html.assembler import _format_evidence_text, \
_format_stmt_text
from indra_db.client.principal.curation import get_curations, submit_curation
from indra.sources.indra_db_rest import submit_curation, get_curations

from emmaa.util import find_latest_s3_file, does_exist, \
EMMAA_BUCKET_NAME, list_s3_files, find_index_of_s3_file, \
Expand Down Expand Up @@ -471,7 +471,7 @@ def _set_curation(stmt_hash, correct, incorrect):

def _label_curations(include_partial=False, **kwargs):
logger.info('Getting curations')
curations = get_curations(**kwargs)
curations = get_curations()
logger.info('Labeling curations')
if include_partial:
correct = {str(c['pa_hash']) for c in curations if
Expand Down Expand Up @@ -525,8 +525,8 @@ def _get_stmt_row(stmt, source, model, cur_counts, date, test_corpus=None,
evid_count = len(stmt.evidence)
evid = []
if with_evid and cur_dict is not None:
evid = _format_evidence_text(
stmt, cur_dict, ['correct', 'act_vs_amt', 'hypothesis'])[:10]
evid = json.loads(json.dumps(_format_evidence_text(
stmt, cur_dict, ['correct', 'act_vs_amt', 'hypothesis'])))[:10]
params = {'stmt_hash': stmt_hash, 'source': source, 'model': model,
'format': 'json', 'date': date}
if test_corpus:
Expand Down Expand Up @@ -954,7 +954,9 @@ def get_paper_statements(model):
stmts_by_hash = {}
for stmt in updated_stmts:
stmts_by_hash[str(stmt.get_hash())] = stmt
curations = get_curations(pa_hash=paper_hashes)
all_curations = get_curations()
curations = [
cur for cur in all_curations if cur['pa_hash'] in paper_hashes]
cur_dict = defaultdict(list)
for cur in curations:
cur_dict[(cur['pa_hash'], cur['source_hash'])].append(
Expand Down Expand Up @@ -1280,7 +1282,9 @@ def get_statement_evidence_page():
stmts_by_hash = {}
for stmt in stmts:
stmts_by_hash[str(stmt.get_hash())] = stmt
curations = get_curations(pa_hash=stmt_hashes)
all_curations = get_curations()
curations = [
cur for cur in all_curations if cur['pa_hash'] in stmt_hashes]
cur_dict = defaultdict(list)
for cur in curations:
cur_dict[(cur['pa_hash'], cur['source_hash'])].append(
Expand Down Expand Up @@ -1651,7 +1655,7 @@ def get_statement_by_hash_model(model, date, hash_val):
"""Get model statement JSON by hash."""
stmts = _load_stmts_from_cache(model, date)
st_json = {}
curations = get_curations(pa_hash=hash_val)
curations = get_curations(hash_val=hash_val)
cur_dict = defaultdict(list)
for cur in curations:
cur_dict[(cur['pa_hash'], cur['source_hash'])].append(
Expand All @@ -1669,7 +1673,7 @@ def get_statement_by_hash_model(model, date, hash_val):
def get_tests_by_hash(test_corpus, hash_val):
"""Get test statement JSON by hash."""
tests = _load_tests_from_cache(test_corpus)
curations = get_curations(pa_hash=hash_val)
curations = get_curations(hash_val=hash_val)
cur_dict = defaultdict(list)
for cur in curations:
cur_dict[(cur['pa_hash'], cur['source_hash'])].append(
Expand All @@ -1690,7 +1694,7 @@ def get_statement_by_paper(model, paper_id, paper_id_type, date, hash_val):
"""Get model statement by hash and paper ID."""
stmts = _load_stmts_from_cache(model, date)
st_json = {}
curations = get_curations(pa_hash=hash_val)
curations = get_curations(hash_val=hash_val)
cur_dict = defaultdict(list)
for cur in curations:
cur_dict[(cur['pa_hash'], cur['source_hash'])].append(
Expand Down Expand Up @@ -1721,31 +1725,57 @@ def submit_curation_endpoint(hash_val, **kwargs):
res_dict = {"result": "failure",
"reason": "POST with API key requires a user email."}
return jsonify(res_dict), 400

api_key = roles[0].api_key
if not api_key:
res_dict = {"result": "failure",
"reason": "API key is required to submit curations."}
return jsonify(res_dict), 401
logger.info("Adding curation for statement %s." % hash_val)
ev_hash = request.json.get('ev_hash')
source_api = request.json.pop('source', 'EMMAA')
tag = request.json.get('tag')
ip = request.remote_addr
text = request.json.get('text')
ev_json = request.json.get('ev_json')
is_test = 'test' in request.args
if not is_test:
assert tag != 'test'
try:
dbid = submit_curation(hash_val, tag, email, ip, text, ev_hash,
source_api)
res = submit_curation(hash_val, tag, email, text, source='EMMAA',
ev_hash=ev_hash, ev_json=ev_json,
api_key=api_key)
except BadHashError as e:
abort(Response("Invalid hash: %s." % e.mk_hash, 400))
res = {'result': 'success', 'ref': {'id': dbid}}
else:
res = {'result': 'test passed', 'ref': None}
res = {'result': 'test passed', 'ref': None}
logger.info("Got result: %s" % str(res))
return jsonify(res)


@app.route('/curation/list/<stmt_hash>/<src_hash>', methods=['GET'])
@jwt_optional
def list_curations(stmt_hash, src_hash):
curations = get_curations(pa_hash=stmt_hash, source_hash=src_hash)
user, roles = resolve_auth(dict(request.args))
if not roles and not user:
res_dict = {"result": "failure", "reason": "Invalid Credentials"}
logger.debug(res_dict)
return jsonify(res_dict), 401

if user:
email = user.email
else:
email = request.json.get('email')
if not email:
res_dict = {"result": "failure",
"reason": "POST with API key requires a user email."}
logger.debug(res_dict)
return jsonify(res_dict), 400
api_key = roles[0].api_key
if not api_key:
res_dict = {"result": "failure",
"reason": "API key is required to view curations."}
logger.debug(res_dict)
return jsonify(res_dict), 401
curations = get_curations(hash_val=stmt_hash, source_hash=src_hash,
api_key=api_key)
return jsonify(curations)


Expand Down