def test_influence(): c2 = Concept('food insecurity', db_refs={'WM': [('wm/food_insecurity', 1.0)]}) c1 = Concept('floods', db_refs={'WM': [('wm/floods', 1.0)]}) c3 = Concept('x', db_refs={}) stmt = Influence(Event(c1), Event(c2)) stmt2 = Influence(Event(c1), Event(c3)) ha = HtmlAssembler([stmt, stmt2]) ha.make_model()
def test_sort_group_by_agent_pair_custom_function(): stmts = _get_sort_corpus() ha = HtmlAssembler(stmts, sort_by=lambda d: 4 * d['trips'] + 2 * d['reach'] + 2 * d['medscan'] + d['sparser'] - d['isi']) json_model = ha.make_json_model(grouping_level='agent-pair') assert list(json_model.keys()) == ['Fez-Baz', 'Bar-Baz', 'Fez-Bar'] ha.make_model(grouping_level='agent-pair')
def test_active_form(): stmt = ActiveForm(Agent('MAPK1', mods=[ModCondition('phosphorylation')]), 'kinase', True) ha = HtmlAssembler([stmt]) ha.make_model() # Case when it's not active stmt = ActiveForm(Agent('MAPK1', mods=[ModCondition('phosphorylation')]), 'activity', False) ha = HtmlAssembler([stmt]) ha.make_model()
def test_assembler(): stmt = make_stmt() ha = HtmlAssembler([stmt]) result = ha.make_model() assert isinstance(result, str) # Read from the template file and make sure the beginning and end of the # content matches template, _, _ = loader.get_source(None, 'indra/template.html') assert result.startswith(template[0:100]) # Make sure assembler works with other parameters provided stmt2 = make_bad_stmt() ha = HtmlAssembler(source_counts={ stmt.get_hash(): { 'test': 1 }, stmt2.get_hash(): { 'test': 1 } }, ev_counts={ stmt.get_hash(): 1, stmt2.get_hash(): 1 }, db_rest_url='test.db.url') ha.add_statements([stmt, stmt2]) result = ha.make_model(grouping_level='agent-pair') assert isinstance(result, str) result = ha.make_model(grouping_level='statement') assert isinstance(result, str) # Test belief badges result = ha.make_model(grouping_level='statement', show_belief=True) assert isinstance(result, str) assert '<small\n' \ ' class="badge badge-pill badge-belief"\n' \ ' title="Belief score for this statement">1.0</small>' in result result = ha.make_model(grouping_level='statement', show_belief=False) assert isinstance(result, str) assert '<small\n' \ ' class="badge badge-pill badge-belief"\n' \ ' title="Belief score for this statement">1</small>' \ not in result # Test if source URL exists assert 'http://www.causalbionet.com/' in result # Make sure warning can be appended ha.append_warning('warning') assert ('\t<span style="color:red;">(CAUTION: warning occurred when ' 'creating this page.)</span>' in ha.model) # Make sure model is created before saving ha = HtmlAssembler([stmt]) assert not ha.model ha.save_model('tempfile.html') assert ha.model
def test_sort_group_by_statement(): ha = HtmlAssembler(_get_sort_corpus()) # Test ordering and grouping by statement. json_model = ha.make_json_model(grouping_level='statement') assert list(json_model.keys()) == ['all-statements'] assert len(json_model['all-statements']['stmts_formatted']) == 1 statements = \ json_model['all-statements']['stmts_formatted'][0]['stmt_info_list'] assert len(statements) == 6 assert [len(s['evidence']) for s in statements] == [4, 3, 2, 2, 1, 1] # Make sure the html assembly works. ha.make_model(grouping_level='statement')
def test_sort_group_by_statement_custom_function(): stmts = _get_sort_corpus() ha = HtmlAssembler(stmts, sort_by=lambda d: 4 * d['trips'] + 2 * d['reach'] + 2 * d['medscan'] + d['sparser'] - d['isi']) json_model = ha.make_json_model(grouping_level='statement') statements = \ json_model['all-statements']['stmts_formatted'][0]['stmt_info_list'] assert len(statements) == len(stmts) exp_order = [ '6106301533612997', '-17995265549545446', '34182032179844940', '32266861591785935', '-30059881887512900', '-5998595995539618' ] assert [s['hash'] for s in statements] == exp_order ha.make_model(grouping_level='statement')
def test_assembler(): stmt = make_stmt() ha = HtmlAssembler([stmt]) result = ha.make_model() assert isinstance(result, str) # Read from the template file and make sure the beginning and end of the # content matches loader = IndraHTMLLoader() template, _, _ = loader.get_source(None, 'indra/indra_template.html') assert result.startswith(template[0:100])
def test_assembler(): stmt = make_stmt() ha = HtmlAssembler([stmt]) result = ha.make_model() assert isinstance(result, str) # Read from the template file and make sure the beginning and end of the # content matches with open(template_path, 'rt') as f: template = f.read().strip() assert result.startswith(template[0:100]) assert result.strip().endswith(template[-10:])
def test_sort_group_by_agent_custom_ordering(): stmts = _get_sort_corpus() custom_values = [0.1, 0.2, 0.15, 0.6, 0.3, 0.8] val_dict = {s.get_hash(): v for v, s in zip(custom_values, stmts)} custom_stat = StmtStat('value', val_dict, float, AveAggregator) ha = HtmlAssembler(stmts, sort_by='value', custom_stats=[custom_stat]) json_model = ha.make_json_model(grouping_level='agent-pair') assert len(json_model.keys()) == 4 # This result was slightly counter-intuitive, but recall that averages will # mean a grouping with the conversion will always have a lower value than # the conversion itself, so it makes sense for it to come out on top. assert list(json_model.keys()) == [ 'Fez-Far-Faz-Bar-Baz', 'Fez-Bar', 'Bar-Baz', 'Fez-Baz' ] ha.make_model(grouping_level='agent-pair')
def test_sort_group_by_statement_custom_ordering(): stmts = _get_sort_corpus() custom_values = [0.1, 0.2, 0.15, 0.6, 0.3, 0.8] val_dict = {s.get_hash(): v for v, s in zip(custom_values, stmts)} custom_stat = StmtStat('value', val_dict, float, AveAggregator) ha = HtmlAssembler(stmts, sort_by='value', custom_stats=[custom_stat]) json_model = ha.make_json_model(grouping_level='statement') statements = \ json_model['all-statements']['stmts_formatted'][0]['stmt_info_list'] got_h_list = [int(s['hash']) for s in statements] exp_h_list = sorted((h for h in val_dict.keys()), key=lambda h: val_dict[h], reverse=True) assert got_h_list == exp_h_list ha.make_model(grouping_level='statement')
def test_sort_group_by_relation_custom_function(): stmts = _get_sort_corpus() ha = HtmlAssembler(stmts, sort_by=lambda d: 4 * d['trips'] + 2 * d['reach'] + 2 * d['medscan'] + d['sparser'] - d['isi']) json_model = ha.make_json_model(grouping_level='relation') relations = json_model['all-relations']['stmts_formatted'] assert len(relations) == 5, len(relations) relation_names = [rel['short_name'] for rel in relations] exp_rel_names = [ '<b>Bar</b> phosphorylates <b>Baz</b>.', '<b>Fez</b> inhibits <b>Baz</b>.', '<b>Bar</b> binds <b>Baz</b> and <b>Fez</b>.', '<b>Fez</b> decreases the amount of <b>Baz</b>.', '<b>Fez</b> catalyzes the conversion of <b>Far</b> and <b>Faz</b> into ' '<b>Bar</b> and <b>Baz</b>.' ] assert relation_names == exp_rel_names, relation_names ha.make_model(grouping_level='relation')
def test_sort_group_by_relation(): ha = HtmlAssembler(_get_sort_corpus()) # Test ordering, grouping by relation. json_model = ha.make_json_model(grouping_level='relation') assert list(json_model.keys()) == ['all-relations'] relations = json_model['all-relations']['stmts_formatted'] assert len(relations) == 5, len(relations) # Make sure the HTML assembles. model = ha.make_model(grouping_level='relation') with open('test_relation.html', 'w') as f: f.write(model)
def test_assembler(): stmt = make_stmt() ha = HtmlAssembler([stmt]) result = ha.make_model() assert isinstance(result, str) # Read from the template file and make sure the beginning and end of the # content matches template, _, _ = loader.get_source(None, 'indra/template.html') assert result.startswith(template[0:100]) # Make sure assembler works with other parameters provided stmt2 = make_bad_stmt() ha = HtmlAssembler(source_counts={ stmt.get_hash(): { 'test': 1 }, stmt2.get_hash(): { 'test': 1 } }, ev_totals={ stmt.get_hash(): 1, stmt2.get_hash(): 1 }, db_rest_url='test.db.url') ha.add_statements([stmt, stmt2]) result = ha.make_model(with_grouping=True) assert isinstance(result, str) result = ha.make_model(with_grouping=False) assert isinstance(result, str) # Make sure warning can be appended ha.append_warning('warning') assert ('\t<span style="color:red;">(CAUTION: warning occurred when ' 'creating this page.)</span>' in ha.model) # Make sure model is created before saving ha = HtmlAssembler([stmt]) assert not ha.model ha.save_model('tempfile.html') assert ha.model
def test_sort_group_by_relation_custom_ordering(): stmts = _get_sort_corpus() custom_values = [0.1, 0.2, 0.15, 0.6, 0.3, 0.8] val_dict = {s.get_hash(): v for v, s in zip(custom_values, stmts)} custom_stat = StmtStat('value', val_dict, float, AveAggregator) ha = HtmlAssembler(stmts, sort_by='value', custom_stats=[custom_stat]) json_model = ha.make_json_model(grouping_level='relation') assert list(json_model.keys()) == ['all-relations'] relations = json_model['all-relations']['stmts_formatted'] assert len(relations) == 5, len(relations) relation_names = [rel['short_name'] for rel in relations] exp_relation_names = [ '<b>Fez</b> catalyzes the conversion of <b>Far</b> and <b>Faz</b> into ' '<b>Bar</b> and <b>Baz</b>.', '<b>Bar</b> phosphorylates <b>Baz</b>.', '<b>Fez</b> decreases the amount of <b>Baz</b>.', '<b>Bar</b> binds <b>Baz</b> and <b>Fez</b>.', '<b>Fez</b> inhibits <b>Baz</b>.' ] assert relation_names == exp_relation_names ha.make_model(grouping_level='relation')
def test_sort_default(): ha = HtmlAssembler(_get_sort_corpus()) # Test the ordering of the statements in the default mode of make_json_model json_model = ha.make_json_model() assert list(json_model.keys()) == ['Fez-Baz', 'Bar-Baz', 'Fez-Bar'] exp_stmt_counts = {'Fez-Baz': 4, 'Bar-Baz': 2, 'Fez-Bar': 2} assert all( len(json_model[k]['stmts_formatted']) == n for k, n in exp_stmt_counts.items()) ev_counts = { k: sum( len(s['evidence']) for r in m['stmts_formatted'] for s in r['stmt_info_list']) for k, m in json_model.items() } assert ev_counts == {'Fez-Baz': 8, 'Bar-Baz': 7, 'Fez-Bar': 3}, ev_counts # Check to make sure the HTML assembler runs. model = ha.make_model() with open('test_agent_pair.html', 'w') as f: f.write(model)
def test_autophosphorylation(): stmt = Autophosphorylation( Agent('P38', bound_conditions=[BoundCondition(Agent('TAB1'))])) ha = HtmlAssembler([stmt]) ha.make_model()
def test_inhibition(): stmt = Inhibition(Agent('DUSP4'), Agent('MAPK1')) ha = HtmlAssembler([stmt]) ha.make_model()
def produce_response(self, result): if result.result_type == 'statements': res_json = result.json() # Add derived values to the res_json. if self.w_cur_counts: res_json['num_curations'] = self.get_curation_counts(result) res_json['statement_limit'] = MAX_STMTS res_json['statements_returned'] = len(result.results) res_json['end_of_statements'] = \ (len(result.results) < MAX_STMTS) res_json['statements_removed'] = 0 res_json['evidence_returned'] = result.returned_evidence # Build the HTML if HTML, else just tweak the JSON. stmts_json = result.results if self.fmt == 'html': title = TITLE ev_counts = res_json.pop('evidence_counts') beliefs = res_json.pop('belief_scores') stmts = stmts_from_json(stmts_json.values()) db_rest_url = request.url_root[:-1] \ + self._env.globals['url_for']('root')[:-1] html_assembler = \ HtmlAssembler(stmts, summary_metadata=res_json, ev_counts=ev_counts, beliefs=beliefs, sort_by=self.sort_by, title=title, source_counts=result.source_counts, db_rest_url=db_rest_url) idbr_template = \ self._env.get_template('idbr_statements_view.html') if not TESTING['status']: identity = self.user.identity() if self.user else None else: identity = None source_info, source_colors = get_html_source_info() resp_content = html_assembler.make_model( idbr_template, identity=identity, source_info=source_info, source_colors=source_colors, simple=False ) if self.tracker.get_messages(): level_stats = ['%d %ss' % (n, lvl.lower()) for lvl, n in self.tracker.get_level_stats().items()] msg = ' '.join(level_stats) resp_content = html_assembler.append_warning(msg) mimetype = 'text/html' else: # Return JSON for all other values of the format argument res_json.update(self.tracker.get_level_stats()) res_json['statements'] = stmts_json resp_content = json.dumps(res_json) mimetype = 'application/json' resp = Response(resp_content, mimetype=mimetype) logger.info("Exiting with %d statements with %d/%d evidence of " "size %f MB after %s seconds." % (res_json['statements_returned'], res_json['evidence_returned'], res_json['total_evidence'], sys.getsizeof(resp.data) / 1e6, sec_since(self.start_time))) elif result.result_type != 'hashes': # Look up curations, if result with_curations was set. if self.w_cur_counts: rel_hash_lookup = defaultdict(list) if result.result_type == 'interactions': for h, rel in result.results.items(): rel['cur_count'] = 0 rel_hash_lookup[int(h)].append(rel) else: for rel in result.results.values(): for h in rel['hashes']: rel['cur_count'] = 0 rel_hash_lookup[int(h)].append(rel) if not self.special['with_hashes']: rel['hashes'] = None curations = get_curations(pa_hash=set(rel_hash_lookup.keys())) for cur in curations: for rel in rel_hash_lookup[cur['pa_hash']]: rel['cur_count'] += 1 logger.info("Returning with %s results after %.2f seconds." % (len(result.results), sec_since(self.start_time))) res_json = result.json() res_json['relations'] = list(res_json['results'].values()) if result.result_type == 'agents' and self.fmt == 'json-js': res_json['complexes_covered'] = \ [str(h) for h in res_json['complexes_covered']] res_json.pop('results') res_json['query_str'] = str(self.db_query) resp = Response(json.dumps(res_json), mimetype='application/json') logger.info("Result prepared after %.2f seconds." % sec_since(self.start_time)) else: return super(StatementApiCall, self).produce_response(result) return resp
def test_activation(): stmt = Activation(Agent('MAP2K1'), Agent('MAPK1'), 'kinase') ha = HtmlAssembler([stmt]) ha.make_model()
def test_event(): stmt = Event(Concept('a')) ha = HtmlAssembler([stmt]) ha.make_model()
def test_migration(): stmt = Migration(Concept('migration')) ha = HtmlAssembler([stmt]) ha.make_model()
def test_dephosphorylation(): stmt = Dephosphorylation(Agent('DUSP6'), Agent('MAPK1'), 'T', '185') ha = HtmlAssembler([stmt]) ha.make_model()
def test_conversion(): stmt = Conversion(Agent('RAS'), [Agent('GTP'), Agent('B')], [Agent('GDP'), Agent('C')]) ha = HtmlAssembler([stmt]) ha.make_model()
def test_has_activity(): stmt = HasActivity(Agent('MAPK1'), 'activity', True) ha = HtmlAssembler([stmt]) ha.make_model()
def test_gap(): stmt = Gap(Agent('RASA1'), Agent('KRAS')) ha = HtmlAssembler([stmt]) ha.make_model()
def test_gef(): stmt = Gef(Agent('SOS1'), Agent('KRAS')) ha = HtmlAssembler([stmt]) ha.make_model()
def test_decrease_amount(): stmt = DecreaseAmount(Agent('TP53'), Agent('MDM2')) ha = HtmlAssembler([stmt]) ha.make_model()
def test_association(): stmt = Association([Event(Concept('a')), Event(Concept('b'))]) ha = HtmlAssembler([stmt]) ha.make_model()
def test_translocation(): stmt = Translocation(Agent('FOXO3A'), None, 'nucleus') ha = HtmlAssembler([stmt]) ha.make_model()
def decorator(*args, **kwargs): tracker = LogTracker() start_time = datetime.now() logger.info("Got query for %s at %s!" % (get_db_query.__name__, start_time)) web_query = request.args.copy() offs = _pop(web_query, 'offset', type_cast=int) ev_lim = _pop(web_query, 'ev_limit', type_cast=int) best_first = _pop(web_query, 'best_first', True, bool) max_stmts = min(_pop(web_query, 'max_stmts', MAX_STATEMENTS, int), MAX_STATEMENTS) fmt = _pop(web_query, 'format', 'json') w_english = _pop(web_query, 'with_english', False, bool) w_cur_counts = _pop(web_query, 'with_cur_counts', False, bool) # Figure out authorization. has = dict.fromkeys(['elsevier', 'medscan'], False) if not TESTING: user, roles = resolve_auth(web_query) for role in roles: for resource in has.keys(): has[resource] |= role.permissions.get(resource, False) logger.info('Auths: %s' % str(has)) else: web_query.pop('api_key', None) has['elsevier'] = False has['medscan'] = False # Actually run the function. logger.info("Running function %s after %s seconds." % (get_db_query.__name__, sec_since(start_time))) db_query = get_db_query(web_query, *args, **kwargs) if isinstance(db_query, Response): return db_query elif not isinstance(db_query, QueryCore): raise RuntimeError("Result should be a child of QueryCore.") if ev_lim is None: if get_db_query is get_statement_by_hash: ev_lim = 10000 else: ev_lim = 10 if not has['medscan']: minus_q = ~HasOnlySource('medscan') db_query &= minus_q ev_filter = minus_q.ev_filter() else: ev_filter = None result = db_query.get_statements(offset=offs, limit=max_stmts, ev_limit=ev_lim, best_first=best_first, evidence_filter=ev_filter) logger.info("Finished function %s after %s seconds." % (get_db_query.__name__, sec_since(start_time))) # Handle any necessary redactions res_json = result.json() stmts_json = res_json.pop('results') elsevier_redactions = 0 source_counts = result.source_counts if not all(has.values()) or fmt == 'json-js' or w_english: for h, stmt_json in stmts_json.copy().items(): if w_english: stmt = stmts_from_json([stmt_json])[0] stmt_json['english'] = _format_stmt_text(stmt) stmt_json['evidence'] = _format_evidence_text(stmt) if has['elsevier'] and fmt != 'json-js' and not w_english: continue if not has['medscan']: source_counts[h].pop('medscan', 0) for ev_json in stmt_json['evidence'][:]: if fmt == 'json-js': ev_json['source_hash'] = str(ev_json['source_hash']) # Check for elsevier and redact if necessary if not has['elsevier'] and \ get_source(ev_json) == 'elsevier': text = ev_json['text'] if len(text) > 200: ev_json['text'] = text[:200] + REDACT_MESSAGE elsevier_redactions += 1 logger.info(f"Redacted {elsevier_redactions} pieces of elsevier " f"evidence.") logger.info("Finished redacting evidence for %s after %s seconds." % (get_db_query.__name__, sec_since(start_time))) # Get counts of the curations for the resulting statements. if w_cur_counts: curations = get_curations(pa_hash=set(stmts_json.keys())) logger.info("Found %d curations" % len(curations)) cur_counts = {} for curation in curations: # Update the overall counts. if curation.pa_hash not in cur_counts: cur_counts[curation.pa_hash] = 0 cur_counts[curation.pa_hash] += 1 # Work these counts into the evidence dict structure. for ev_json in stmts_json[curation.pa_hash]['evidence']: if str(ev_json['source_hash']) == str( curation.source_hash): ev_json['num_curations'] = \ ev_json.get('num_curations', 0) + 1 break res_json['num_curations'] = cur_counts # Add derived values to the res_json. res_json['offset'] = offs res_json['evidence_limit'] = ev_lim res_json['statement_limit'] = MAX_STATEMENTS res_json['statements_returned'] = len(stmts_json) res_json['end_of_statements'] = (len(stmts_json) < MAX_STATEMENTS) res_json['statements_removed'] = 0 res_json['evidence_returned'] = result.returned_evidence if fmt == 'html': title = TITLE + ': ' + 'Results' ev_totals = res_json.pop('evidence_totals') stmts = stmts_from_json(stmts_json.values()) html_assembler = HtmlAssembler(stmts, res_json, ev_totals, source_counts, title=title, db_rest_url=request.url_root[:-1]) idbr_template = env.get_template('idbr_statements_view.html') identity = user.identity() if user else None content = html_assembler.make_model(idbr_template, identity=identity) if tracker.get_messages(): level_stats = [ '%d %ss' % (n, lvl.lower()) for lvl, n in tracker.get_level_stats().items() ] msg = ' '.join(level_stats) content = html_assembler.append_warning(msg) mimetype = 'text/html' else: # Return JSON for all other values of the format argument res_json.update(tracker.get_level_stats()) res_json['statements'] = stmts_json res_json['source_counts'] = source_counts content = json.dumps(res_json) mimetype = 'application/json' resp = Response(content, mimetype=mimetype) logger.info("Exiting with %d statements with %d/%d evidence of size " "%f MB after %s seconds." % (res_json['statements_returned'], res_json['evidence_returned'], res_json['total_evidence'], sys.getsizeof(resp.data) / 1e6, sec_since(start_time))) return resp