def test_model_json(): """Test the json structure and content of EmmaaModel.to_json() output""" indra_stmts = \ [Activation(Agent('BRAF', db_refs={'HGNC': '20974'}), Agent('MAP2K1'), evidence=[Evidence(text='BRAF activates MAP2K1.')]), Activation(Agent('MAP2K1', activity=ActivityCondition('activity', True)), Agent('MAPK1'), evidence=[Evidence(text='Active MAP2K1 activates MAPK1.')]) ] st = SearchTerm('gene', 'MAP2K1', db_refs={}, search_term='MAP2K1') emmaa_stmts = [ EmmaaStatement(stmt, datetime.datetime.now(), [st]) for stmt in indra_stmts ] config_dict = { 'ndex': { 'network': 'a08479d1-24ce-11e9-bb6a-0ac135e8bacf' }, 'search_terms': [{ 'db_refs': { 'HGNC': '20974' }, 'name': 'MAPK1', 'search_term': 'MAPK1', 'type': 'gene' }] } emmaa_model = EmmaaModel('test', config_dict) emmaa_model.add_statements(emmaa_stmts) emmaa_model_json = emmaa_model.to_json() # Test json structure assert emmaa_model_json['name'] == 'test' assert isinstance(emmaa_model_json['stmts'], list) assert emmaa_model_json['ndex_network'] == \ 'a08479d1-24ce-11e9-bb6a-0ac135e8bacf' # Test config assert emmaa_model_json['search_terms'][0]['type'] == 'gene' assert emmaa_model_json['search_terms'][0]['db_refs'] == {'HGNC': '20974'} # Test json statements assert 'BRAF activates MAP2K1.' == \ emmaa_model_json['stmts'][0]['stmt']['evidence'][0]['text'] assert 'BRAF activates MAP2K1.' == \ emmaa_model_json['stmts'][0]['stmt']['evidence'][0]['text'] assert 'Active MAP2K1 activates MAPK1.' == \ emmaa_model_json['stmts'][1]['stmt']['evidence'][0]['text'] assert emmaa_model_json['stmts'][0]['stmt']['subj']['name'] == 'BRAF' assert emmaa_model_json['stmts'][1]['stmt']['subj']['name'] == 'MAP2K1' assert emmaa_model_json['stmts'][1]['stmt']['obj']['name'] == 'MAPK1' # Need hashes to be strings so that javascript can read them assert isinstance( emmaa_model_json['stmts'][0]['stmt']['evidence'][0]['source_hash'], str)
def test_filter_relevance(): config_dict = { 'ndex': { 'network': 'a08479d1-24ce-11e9-bb6a-0ac135e8bacf' }, 'search_terms': [{ 'db_refs': { 'HGNC': '20974' }, 'name': 'MAPK1', 'search_term': 'MAPK1', 'type': 'gene' }] } indra_stmts = \ [Activation(Agent('BRAF', db_refs={'HGNC': '20974'}), Agent('MAP2K1'), evidence=[Evidence(text='BRAF activates MAP2K1.', source_api='assertion')]), Activation(Agent('MAP2K1', activity=ActivityCondition('activity', True)), Agent('MAPK1'), evidence=[Evidence(text='Active MAP2K1 activates ' 'MAPK1.', source_api='assertion')]) ] st = SearchTerm('gene', 'MAP2K1', db_refs={}, search_term='MAP2K1') emmaa_stmts = [ EmmaaStatement(stmt, datetime.datetime.now(), [st]) for stmt in indra_stmts ] # Try no filter first emmaa_model = EmmaaModel('test', config_dict) emmaa_model.extend_unique(emmaa_stmts) emmaa_model.run_assembly() assert len(emmaa_model.assembled_stmts) == 2, emmaa_model.assembled_stmts # Next do a prior_one filter config_dict['assembly'] = {'filter_relevance': 'prior_one'} emmaa_model = EmmaaModel('test', config_dict) emmaa_model.extend_unique(emmaa_stmts) emmaa_model.run_assembly() assert len(emmaa_model.assembled_stmts) == 1, emmaa_model.assembled_stmts assert emmaa_model.assembled_stmts[0].obj.name == 'MAPK1' # Next do a prior_all filter config_dict['assembly'] = {'filter_relevance': 'prior_all'} emmaa_model = EmmaaModel('test', config_dict) emmaa_model.extend_unique(emmaa_stmts) emmaa_model.run_assembly() assert len(emmaa_model.assembled_stmts) == 0
def test_db_ref_keys(): # test that expanded families get TEXT, UP, HGNC keys in their db_refs # Get the Expander exp = ef.Expander(hierarchies) # Declare some agents grb2 = Agent('GRB2', db_refs={'TEXT': 'Grb2', 'UP': 'P62993', 'HGNC': '4566'}) shc = Agent('SHC', db_refs={'FPLX':'SHC'}) # Test case where one agent is a family and the other is a gene st = Activation(grb2, shc) expanded_stmts = exp.expand_families([st]) for st in expanded_stmts: for agent in st.agent_list(): if agent is not None: assert set(list(agent.db_refs)) == \ set(['TEXT','UP','HGNC']) # Test for case involving None for one of the agents st = Phosphorylation(None, shc) expanded_stmts = exp.expand_families([st]) for st in expanded_stmts: for agent in st.agent_list(): if agent is not None: assert set(list(agent.db_refs)) == \ set(['TEXT','UP','HGNC']) # Statement with two families: 4x4 SHC st = Activation(shc, shc) expanded_stmts = exp.expand_families([st]) for st in expanded_stmts: for agent in st.agent_list(): if agent is not None: assert set(list(agent.db_refs)) == \ set(['TEXT','UP','HGNC'])
def test_load_extra_evidence(): stmt = Activation(Agent('BRAF', db_refs={'HGNC': '1097'}), Agent('MAP2K1', db_refs={'HGNC': '6840'}), evidence=[ Evidence(text='BRAF activates MAP2K1.', source_api='assertion', text_refs={'TRID': '1234'}) ]) assert len(stmt.evidence) == 1 stmt_hash = stmt.get_hash() updated = load_extra_evidence([stmt]) # Get back the same statement with extra evidence assert len(updated) == 1 assert updated[0].get_hash() == stmt_hash assert len(updated[0].evidence) > 10
def test_get_chemical_agents(): stmts = [ Activation(Agent('BRAF'), Agent('KRAS')), Inhibition(Agent('DRUG', db_refs={'CHEBI': '123'}), Agent('BRAF')) ] chemical_agents = tra_module.get_chemical_agents(stmts) assert chemical_agents == ['DRUG']
def test_msa_custom_corpus_stmt_type(): # Create a pickle with a test statement test_corpus = 'test_corpus2.pkl' kras = Agent('KRAS', db_refs={'HGNC': '6407'}) st1 = Phosphorylation(Agent('x'), kras) st2 = Inhibition(Agent('y'), kras) st3 = Activation(Agent('z'), kras) with open(test_corpus, 'wb') as fh: pickle.dump([st1, st2, st3], fh) # Instantiate MSA with that pickle as the corpus msa = MSA(corpus_config='pickle:%s' % test_corpus) # Query the MSA finder = msa.find_mechanisms('to_target', target=kras, verb='activate') # Make sure we got the original statement back res_stmts = finder.get_statements() assert res_stmts[0].__class__.__name__ == 'Activation' assert res_stmts[0].subj.name == 'z' finder = msa.find_mechanisms('to_target', target=kras, verb='phosphorylate') # Make sure we got the original statement back res_stmts = finder.get_statements() assert res_stmts[0].__class__.__name__ == "Phosphorylation" assert res_stmts[0].enz.name == 'x'
def test_missing_monomer_condition(): stmts = [Activation(Agent('BRAF'), Agent('KRAS'))] model = tra_module.assemble_model(stmts) entity = Agent('HRAS') quantity = tra.MolecularQuantityReference('total', entity) condition = tra.MolecularCondition('multiple', quantity, 10) tra.apply_condition(model, condition)
def test_assemble_model_chemical_agents(): stmts = [ Activation(Agent('BRAF'), Agent('KRAS')), Inhibition(Agent('DRUG', db_refs={'CHEBI': '123'}), Agent('BRAF')) ] model = tra_module.assemble_model(stmts) assert model.parameters['DRUG_0'].value == 10000.0
def create_model(relevance=None, paper_ids=None): indra_stmts = [ Activation(Agent('BRAF', db_refs={'HGNC': '1097'}), Agent('MAP2K1', db_refs={'HGNC': '6840'}), evidence=[Evidence(text='BRAF activates MAP2K1.', source_api='assertion', text_refs={'TRID': '1234'})]), Activation(Agent('MAP2K1', db_refs={'HGNC': '6840'}, activity=ActivityCondition('activity', True)), Agent('MAPK1', db_refs={'HGNC': '6871'}), evidence=[Evidence(text='Active MAP2K1 activates MAPK1.', source_api='assertion', text_refs={'TRID': '2345'})]) ] st = SearchTerm('gene', 'MAP2K1', db_refs={}, search_term='MAP2K1') emmaa_stmts = [ EmmaaStatement( indra_stmts[0], datetime.datetime.now(), [st], {'internal': True, 'curated': False}), EmmaaStatement( indra_stmts[1], datetime.datetime.now(), [st], {'internal': True, 'curated': True}) ] config_dict = { 'ndex': {'network': 'a08479d1-24ce-11e9-bb6a-0ac135e8bacf'}, 'search_terms': [{'db_refs': {'HGNC': '20974'}, 'name': 'MAPK1', 'search_term': 'MAPK1', 'type': 'gene'}], 'human_readable_name': 'Test Model', 'test': { 'statement_checking': {'max_path_length': 5, 'max_paths': 1}, 'test_corpus': 'simple_tests', 'mc_types': ['pysb', 'pybel', 'signed_graph', 'unsigned_graph']}, 'assembly': [ {'function': 'filter_no_hypothesis'}, {'function': 'map_grounding'}, {'function': 'filter_grounded_only'}, {'function': 'filter_human_only'}, {'function': 'map_sequence'}, {'function': 'run_preassembly', 'kwargs': { 'return_toplevel': False}}]} if relevance: config_dict['assembly'].append( {'function': 'filter_relevance', 'kwargs': {'policy': relevance}}) emmaa_model = EmmaaModel('test', config_dict, paper_ids) emmaa_model.add_statements(emmaa_stmts) return emmaa_model
def test_assemble_model_targeted_agents(): stmts = [ Activation(Agent('BRAF'), Agent('KRAS')), Inhibition(Agent('DRUG'), Agent('BRAF')) ] model = tra_module.assemble_model(stmts) assert model.parameters['BRAF_0'].value == 50.0 assert model.parameters['BRAF_0_mod'].value == 50.0
def _get_db_no_pa_stmts(): db = get_temp_db(clear=True) db_builder = DbBuilder(db) db_builder.add_text_refs([('12345', 'PMC54321'), ('24680', 'PMC08642'), ('97531', )]) db_builder.add_text_content([['pubmed-ttl', 'pubmed-abs', 'pmc_oa'], ['pubmed-abs', 'manuscripts'], ['pubmed-ttl', 'pubmed-abs']]) db_builder.add_readings([['REACH', 'TRIPS'], ['REACH', 'SPARSER'], ['REACH', 'ISI'], ['SPARSER'], ['REACH', 'SPARSER'], ['SPARSER', 'TRIPS', 'REACH'], ['REACH', 'EIDOS']]) db_builder.add_raw_reading_statements([ [Phosphorylation(mek, erk)], # reach pubmed title [Phosphorylation(mek, erk, 'T', '124')], # trips pubmed title [ Phosphorylation(mek, erk), Inhibition(erk, ras), (Phosphorylation(mek, erk), 'in the body') ], # reach pubmed-abs [ Complex([mek, erk]), Complex([erk, ras]), (Phosphorylation(None, erk), 'In the body') ], # sparser pubmed-abs [], # reach pmc_oa [], # ISI pmc_oa [Phosphorylation(map2k1, mapk1)], # sparser pubmed-abs [], # reach manuscripts [], # sparser manuscripts [Inhibition(simvastatin_ng, raf), Activation(map2k1_mg, erk)], # sparser pubmed title [], # TRIPS pubmed title [], # reach pubmed title [], # reach pubmed abs [], # eidos pubmed abs ]) db_builder.add_databases(['biopax', 'tas', 'bel']) db_builder.add_raw_database_statements([[ Activation(mek, raf), Inhibition(erk, ras), Phosphorylation(mek, erk) ], [Inhibition(simvastatin, raf)], [Phosphorylation(mek, erk, 'T', '124')]]) return db
def test_filter_eidos_ungrounded(): stmts = [ # Grounded Eidos Activation(Agent('A', db_refs={'HGNC': '1097'}), Agent('B', db_refs={'HGNC': '6840'}), evidence=[ Evidence(text='A activates B.', source_api='eidos', text_refs={'TRID': '1234'}) ]), # Ungrounded Eidos Activation(Agent('B', db_refs={'TEXT': 'B'}), Agent('C', db_refs={'TEXT': 'C'}), evidence=[ Evidence(text='B activates C.', source_api='eidos', text_refs={'TRID': '2345'}) ]), # Grounded not Eidos Activation(Agent('C', db_refs={'HGNC': '1097'}), Agent('D', db_refs={'HGNC': '6840'}), evidence=[ Evidence(text='C activates D.', source_api='trips', text_refs={'TRID': '1234'}) ]), # Ungrounded not Eidos Activation(Agent('D', db_refs={'TEXT': 'D'}, activity=ActivityCondition('activity', True)), Agent('E', db_refs={'TEXT': 'E'}), evidence=[ Evidence(text='D activates E.', source_api='trips', text_refs={'TRID': '2345'}) ]) ] hashes = [stmt.get_hash() for stmt in stmts] filtered = filter_eidos_ungrounded(stmts) assert len(filtered) == 3 filtered_hashes = [stmt.get_hash() for stmt in filtered] assert set(hashes) - set(filtered_hashes) == {hashes[1]}
def test_filter_source(): ev1 = Evidence(source_api='bel') ev2 = Evidence(source_api='biopax') ev3 = Evidence(source_api='reach') st1 = Activation(Agent('a'), Agent('b'), evidence=[ev3]) st2 = Activation(Agent('a'), Agent('b'), evidence=[ev1, ev2]) st3 = Activation(Agent('a'), Agent('b'), evidence=[ev1, ev3]) st_out = ac.filter_evidence_source([st1, st2], ['reach'], 'one') assert (len(st_out) == 1) st_out = ac.filter_evidence_source([st1, st2, st3], ['reach'], 'all') assert (len(st_out) == 2) st_out = ac.filter_evidence_source([st1, st2, st3], ['bel', 'biopax'], 'one') assert (len(st_out) == 2) st_out = ac.filter_evidence_source([st1, st2, st3], ['bel', 'biopax'], 'all') assert (len(st_out) == 1) st_out = ac.filter_evidence_source([st1, st2, st3], ['bel', 'biopax'], 'none') assert (len(st_out) == 1)
def test_find_contradicts(): st1 = Inhibition(Agent('a'), Agent('b')) st2 = Activation(Agent('a'), Agent('b')) st3 = IncreaseAmount(Agent('a'), Agent('b')) st4 = DecreaseAmount(Agent('a'), Agent('b')) pa = Preassembler(hierarchies, [st1, st2, st3, st4]) contradicts = pa.find_contradicts() assert len(contradicts) == 2 for s1, s2 in contradicts: assert {s1.uuid, s2.uuid} in ({st1.uuid, st2.uuid}, {st3.uuid, st4.uuid})
def test_intervention_query_from_stmt(): stmt = Activation(Agent('EGF', db_refs={'HGNC': '3229'}), Agent('ERK', db_refs={'FPLX': 'ERK'})) query = SimpleInterventionProperty.from_stmt(stmt) assert isinstance(query.condition_entity, Agent) assert query.condition_entity.name == 'EGF' assert isinstance(query.target_entity, Agent) assert query.target_entity.name == 'ERK' assert query.target_entity.activity stmt = Phosphorylation(Agent('EGF', db_refs={'HGNC': '3229'}), Agent('ERK', db_refs={'FPLX': 'ERK'})) query = SimpleInterventionProperty.from_stmt(stmt) assert query.target_entity.mods
def test_map_grounding(): a = Agent('MEK', db_refs={'TEXT': 'MEK'}) b = Agent('X', db_refs={'TEXT': 'ERK'}) st = Activation(a, b) st_out = ac.map_grounding([st], do_rename=False) assert (len(st_out) == 1) assert (st_out[0].subj.db_refs.get('BE')) assert (st_out[0].obj.db_refs.get('BE')) assert (st_out[0].obj.name == 'X') st_out = ac.map_grounding([st], do_rename=True) assert (len(st_out) == 1) assert (st_out[0].subj.db_refs.get('BE')) assert (st_out[0].obj.db_refs.get('BE')) assert (st_out[0].obj.name == 'ERK')
def _construct_database(): db = get_temp_db(clear=True) db_builder = DbBuilder(db) db_builder.add_text_refs([('12345', 'PMC54321'), ('24680', 'PMC08642')]) db_builder.add_text_content([['pubmed-abs', 'pmc_oa'], ['pubmed-abs']]) db_builder.add_readings([['REACH'], ['REACH'], ['REACH', 'SPARSER']]) mek = Agent('MEK', db_refs={'FPLX': 'MEK'}) erk = Agent('ERK', db_refs={'FPLX': 'ERK'}) raf = Agent('RAF', db_refs={'FPLX': 'RAF'}) db_builder.add_raw_reading_statements( [[Phosphorylation(mek, erk), Complex([mek, erk])], [Phosphorylation(mek, erk)], [Activation(mek, erk)], [Complex([mek, erk]), Complex([raf, erk])]]) db_builder.add_databases(['signor']) db_builder.add_raw_database_statements([[Complex([raf, erk])]]) db_builder.add_pa_statements([(Phosphorylation(mek, erk), [0, 2]), (Complex([mek, erk]), [1, 4]), (Activation(mek, erk), [3]), (Complex([raf, erk]), [5, 6])]) return db
def test_run_tests(): model = create_model() tests = [StatementCheckingTest( Activation(Agent('BRAF', db_refs={'HGNC': '1097'}), Agent('MAPK1', db_refs={'UP': 'P28482'})))] mm = ModelManager(model) tm = TestManager([mm], tests) tm.make_tests(ScopeTestConnector()) tm.run_tests() assert len(mm.applicable_tests) == 1 assert isinstance(mm.applicable_tests[0], StatementCheckingTest) assert len(mm.mc_types['pysb']['test_results']) == 1 assert len(mm.mc_types['pybel']['test_results']) == 1 assert len(mm.mc_types['signed_graph']['test_results']) == 1 assert len(mm.mc_types['unsigned_graph']['test_results']) == 1 assert isinstance(mm.mc_types['pysb']['test_results'][0], PathResult)
def test_activation_refinement(): subj = Agent('alcohol', db_refs={ 'CHEBI': 'CHEBI:16236', 'HMDB': 'HMDB00108', 'PUBCHEM': '702', 'TEXT': 'alcohol' }) obj = Agent('endotoxin', db_refs={'TEXT': 'endotoxin'}) st1 = Inhibition(subj, obj) st2 = Activation(subj, obj) pa = Preassembler(hierarchies, stmts=[st1, st2]) pa.combine_duplicates() assert len(pa.unique_stmts) == 2 pa.combine_related() assert len(pa.related_stmts) == 2
def test_path_counts(): db = _get_test_db('stmt') # Put statements in the database model_id = 'test' date = '2021-01-01' stmts = [ Activation(Agent('A', db_refs={'HGNC': '1234'}), Agent('B', db_refs={'HGNC': '2345'}), evidence=[ Evidence(text='A activates B.', source_api='assertion', text_refs={'TRID': '1234'}), Evidence(text='A activates B.', source_api='assertion', text_refs={'TRID': '1235'}) ]), Phosphorylation(Agent('B', db_refs={'HGNC': '2345'}), Agent('C', db_refs={'HGNC': '3456'}), evidence=[ Evidence(text='B phosphorylates C.', source_api='assertion', text_refs={'TRID': '2345'}) ]) ] hash0 = str(stmts[0].get_hash()) hash1 = str(stmts[1].get_hash()) stmt_jsons = stmts_to_json(stmts) db.add_statements(model_id, date, stmt_jsons) # All path counts should be 0 path_counts = db.get_path_counts(model_id, date) assert len(path_counts) == 0 # Can update path counts multiple times, can be a subset of hashes db.update_statements_path_counts(model_id, date, {hash0: 7}) path_counts = db.get_path_counts(model_id, date) assert len(path_counts) == 1, path_counts assert path_counts[hash0] == 7 db.update_statements_path_counts(model_id, date, {hash0: 1, hash1: 5}) path_counts = db.get_path_counts(model_id, date) assert len(path_counts) == 2 assert path_counts[hash0] == 8 # 7 + 1 assert path_counts[hash1] == 5 db.update_statements_path_counts(model_id, date, {hash0: 3}) path_counts = db.get_path_counts(model_id, date) assert len(path_counts) == 2 assert path_counts[hash0] == 11 # 7 + 1 + 3 assert path_counts[hash1] == 5 # Only added 5
def test_get_dates_and_delete(): db = _get_test_db('stmt') model_id = 'test' # At first there are no statements in the database assert db.get_number_of_dates(model_id) == 0 assert db.get_oldest_date(model_id) is None # Put statements in the database date = '2021-01-01' stmts = [ Activation(Agent('A', db_refs={'HGNC': '1234'}), Agent('B', db_refs={'HGNC': '2345'}), evidence=[ Evidence(text='A activates B.', source_api='assertion', text_refs={'TRID': '1234'}), Evidence(text='A activates B.', source_api='assertion', text_refs={'TRID': '1235'}) ]), Phosphorylation(Agent('B', db_refs={'HGNC': '2345'}), Agent('C', db_refs={'HGNC': '3456'}), evidence=[ Evidence(text='B phosphorylates C.', source_api='assertion', text_refs={'TRID': '2345'}) ]) ] stmt_jsons = stmts_to_json(stmts) db.add_statements(model_id, date, stmt_jsons) # There should be one date assert db.get_number_of_dates(model_id) == 1 assert db.get_oldest_date(model_id) == date # Add another date date2 = '2022-01-01' db.add_statements(model_id, date2, stmt_jsons) assert db.get_number_of_dates(model_id) == 2 # Oldest date is still the first one assert db.get_oldest_date(model_id) == date # Delete statements from the first date db.delete_statements(model_id, date) # There should be one date left assert db.get_number_of_dates(model_id) == 1 assert db.get_oldest_date(model_id) == date2
def test_find_contradicts(): st1 = Inhibition(Agent('a'), Agent('b')) st2 = Activation(Agent('a'), Agent('b')) st3 = IncreaseAmount(Agent('a'), Agent('b')) st4 = DecreaseAmount(Agent('a'), Agent('b')) st5 = ActiveForm( Agent('a', mods=[ModCondition('phosphorylation', None, None, True)]), 'kinase', True) st6 = ActiveForm( Agent('a', mods=[ModCondition('phosphorylation', None, None, True)]), 'kinase', False) pa = Preassembler(hierarchies, [st1, st2, st3, st4, st5, st6]) contradicts = pa.find_contradicts() assert len(contradicts) == 3 for s1, s2 in contradicts: assert {s1.uuid, s2.uuid} in ({st1.uuid, st2.uuid}, {st3.uuid, st4.uuid}, {st5.uuid, st6.uuid})
def test_get_statements_by_hash(): db = _get_test_db('stmt') # Put statements in the database model_id = 'test' date = '2021-01-01' stmts = [ Activation(Agent('A', db_refs={'HGNC': '1234'}), Agent('B', db_refs={'HGNC': '2345'}), evidence=[ Evidence(text='A activates B.', source_api='assertion', text_refs={'TRID': '1234'}), Evidence(text='A activates B.', source_api='assertion', text_refs={'TRID': '1235'}) ]), Phosphorylation(Agent('B', db_refs={'HGNC': '2345'}), Agent('C', db_refs={'HGNC': '3456'}), evidence=[ Evidence(text='B phosphorylates C.', source_api='assertion', text_refs={'TRID': '2345'}) ]) ] hash0 = stmts[0].get_hash() hash1 = stmts[1].get_hash() stmt_jsons = stmts_to_json(stmts) db.add_statements(model_id, date, stmt_jsons) # Load statements by hash stmts_loaded = db.get_statements_by_hash(model_id, date, [hash0, hash1]) assert len(stmts_loaded) == 2 assert stmts_loaded[0].get_hash() == hash0 assert stmts_loaded[1].get_hash() == hash1 stmts_loaded = db.get_statements_by_hash(model_id, date, [hash0]) assert len(stmts_loaded) == 1 assert stmts_loaded[0].get_hash() == hash0
class TestSimpleStatement(_NlgTestBase): statements = [Activation(kras, braf)] sentences = ["KRAS activates BRAF"]
class TestActiveFlag(_NlgTestBase): statements = [Activation(active_braf, map2k1)] sentences = ["Active BRAF activates MAP2K1"]
def setup_bucket(add_model=False, add_mm=False, add_tests=False, add_results=False, add_model_stats=False, add_test_stats=False): """ This function creates a new (local) bucket mocking S3 bucket at each call. Then all calls to S3 are calling this bucket instead of real S3 bucket. Depending on the test we might or might not need the bucket to contain different files. For faster computation, only required files for the test are generated and stored in the bucket. Files can be added by setting corresponding arguments to True when calling this function. """ # Local imports are recommended when using moto from emmaa.util import get_s3_client from emmaa.model import save_config_to_s3 from emmaa.model_tests import ModelManager, save_model_manager_to_s3, \ StatementCheckingTest # Create a mock s3 bucket client = get_s3_client() bucket = client.create_bucket(Bucket=TEST_BUCKET_NAME, ACL='public-read') date_str = make_date_str() emmaa_model = None if add_model: # Put config and model files into empty bucket config_dict = { 'ndex': { 'network': 'a08479d1-24ce-11e9-bb6a-0ac135e8bacf' }, 'search_terms': [{ 'db_refs': { 'HGNC': '20974' }, 'name': 'MAPK1', 'search_term': 'MAPK1', 'type': 'gene' }], 'test': { 'test_corpus': 'simple_tests', 'default_test_corpus': 'simple_tests' }, 'human_readable_name': 'Test Model', 'assembly': [{ 'function': 'filter_no_hypothesis' }, { 'function': 'map_grounding' }, { 'function': 'filter_grounded_only' }, { 'function': 'filter_human_only' }, { 'function': 'map_sequence' }, { 'function': 'run_preassembly', 'kwargs': { 'return_toplevel': False } }, { 'function': 'filter_top_level' }] } save_config_to_s3('test', config_dict, bucket=TEST_BUCKET_NAME) emmaa_model = create_model() emmaa_model.save_to_s3(bucket=TEST_BUCKET_NAME) if add_mm: # Add a ModelManager to bucket if not emmaa_model: emmaa_model = create_model() mm = ModelManager(emmaa_model) mm.date_str = date_str mm.save_assembled_statements(upload_to_db=False, bucket=TEST_BUCKET_NAME) save_model_manager_to_s3('test', mm, bucket=TEST_BUCKET_NAME) if add_tests: tests = [ StatementCheckingTest(Activation(Agent('BRAF'), Agent('MAPK1'))) ] test_dict = { 'test_data': { 'description': 'Tests for functionality testing' }, 'tests': tests } client.put_object(Body=pickle.dumps(test_dict), Bucket=TEST_BUCKET_NAME, Key=f'tests/simple_tests.pkl') if add_results: client.put_object( Body=json.dumps(previous_results, indent=1), Bucket=TEST_BUCKET_NAME, Key=f'results/test/results_simple_tests_{date_str}.json') if add_model_stats: client.put_object(Body=json.dumps(previous_model_stats, indent=1), Bucket=TEST_BUCKET_NAME, Key=f'model_stats/test/model_stats_{date_str}.json') if add_test_stats: client.put_object( Body=json.dumps(previous_test_stats, indent=1), Bucket=TEST_BUCKET_NAME, Key=f'stats/test/test_stats_simple_tests_{date_str}.json') return client
def test_get_statements(): db = _get_test_db('stmt') # Put statements and path counts in the database model_id = 'test' date = '2021-01-01' stmts = [ Activation(Agent('A', db_refs={'HGNC': '1234'}), Agent('B', db_refs={'HGNC': '2345'}), evidence=[ Evidence(text='A activates B.', source_api='assertion', text_refs={'TRID': '1234'}), Evidence(text='A activates B.', source_api='assertion', text_refs={'TRID': '1235'}) ]), Phosphorylation(Agent('B', db_refs={'HGNC': '2345'}), Agent('C', db_refs={'HGNC': '3456'}), evidence=[ Evidence(text='B phosphorylates C.', source_api='assertion', text_refs={'TRID': '2345'}) ]), IncreaseAmount(Agent('A', db_refs={'HGNC': '1234'}), Agent('C', db_refs={'HGNC': '3456'})) ] stmts[0].belief = 0.8 stmts[1].belief = 0.9 stmts[2].belief = 0.5 hash0 = stmts[0].get_hash() hash1 = stmts[1].get_hash() hash2 = stmts[2].get_hash() stmt_jsons = stmts_to_json(stmts) db.add_statements(model_id, date, stmt_jsons) db.update_statements_path_counts(model_id, date, { str(hash0): 1, str(hash1): 5 }) # Load statements with different sort/filter options # Sort by evidence count stmts_loaded = db.get_statements(model_id, date, sort_by='evidence') assert len(stmts_loaded) == 3 assert stmts_loaded[0].get_hash() == hash0, len(stmts_loaded[0].evidence) assert stmts_loaded[1].get_hash() == hash1, len(stmts_loaded[1].evidence) assert stmts_loaded[2].get_hash() == hash2 # stmt with no evidence is last # Sort by belief stmts_loaded = db.get_statements(model_id, date, sort_by='belief') assert len(stmts_loaded) == 3 assert stmts_loaded[0].get_hash() == hash1 assert stmts_loaded[1].get_hash() == hash0 # Sort by path count stmts_loaded = db.get_statements(model_id, date, sort_by='paths') assert len(stmts_loaded) == 3 assert stmts_loaded[0].get_hash() == hash1, stmts_loaded assert stmts_loaded[1].get_hash() == hash0 # Filter by statement type stmts_loaded = db.get_statements(model_id, date, stmt_types=['Activation']) assert len(stmts_loaded) == 1 assert stmts_loaded[0].get_hash() == hash0 stmts_loaded = db.get_statements(model_id, date, stmt_types=['Phosphorylation']) assert len(stmts_loaded) == 1 assert stmts_loaded[0].get_hash() == hash1 # Filter by belief stmts_loaded = db.get_statements(model_id, date, min_belief=0.85) assert len(stmts_loaded) == 1 assert stmts_loaded[0].get_hash() == hash1 stmts_loaded = db.get_statements(model_id, date, max_belief=0.85) assert len(stmts_loaded) == 2 assert set([stmt.get_hash() for stmt in stmts_loaded]) == {hash0, hash2} stmts_loaded = db.get_statements(model_id, date, min_belief=0.85, max_belief=0.85) assert len(stmts_loaded) == 0 # Use offset and limit stmts_loaded = db.get_statements(model_id, date) assert len(stmts_loaded) == 3 stmts_loaded = db.get_statements(model_id, date, offset=1) assert len(stmts_loaded) == 2, stmts_loaded stmts_loaded = db.get_statements(model_id, date, limit=1) assert len(stmts_loaded) == 1 # Returns only remaining statements after upset even if limit is larger stmts_loaded = db.get_statements(model_id, date, offset=1, limit=5) assert len(stmts_loaded) == 2
location='nucleus', bound_conditions=['x', 'y', 'z']) st1 = Phosphorylation(a, b) st2 = Phosphorylation(a, d) st3 = Phosphorylation(c, d) st4 = Phosphorylation(b, e) st5 = Phosphorylation(None, b) st6 = Phosphorylation(None, d) st7 = Phosphorylation(None, e) st8 = Phosphorylation(b, f) st9 = Phosphorylation(None, f) st10 = Phosphorylation(None, g) st11 = Phosphorylation(None, h) st12 = Phosphorylation(a, b, evidence=[Evidence(epistemics={'direct': True})]) st13 = Phosphorylation(a, b, evidence=[Evidence(epistemics={'direct': False})]) st14 = Activation(a, b, 'activity') st15 = Activation(a, b, 'kinase') st14.supports = [st15] st15.supported_by = [st14] st1.belief = 0.9 st2.belief = 0.8 st3.belief = 0.7 def test_load_stmts(): with open('_test.pkl', 'wb') as fh: pickle.dump([st1], fh, protocol=2) st_loaded = ac.load_statements('_test.pkl') assert (len(st_loaded) == 1) assert (st_loaded[0].equals(st1))
import datetime from copy import deepcopy from emmaa.statements import EmmaaStatement, to_emmaa_stmts, \ filter_emmaa_stmts_by_metadata, filter_indra_stmts_by_metadata from emmaa.priors import SearchTerm from indra.statements import Activation, Agent, Evidence braf = Agent('BRAF', db_refs={'HGNC': '1097'}) map2k1 = Agent('MAP2K1', db_refs={'HGNC': '6840'}) stmt = Activation(braf, map2k1, evidence=[ Evidence(text='BRAF activates MAP2K1.', source_api='assertion', text_refs={'TRID': '1234'}) ]) date = datetime.datetime.now() search_terms = [ SearchTerm('gene', braf.name, braf.db_refs, '"BRAF"'), SearchTerm('gene', map2k1.name, map2k1.db_refs, '"MAP2K1"') ] def test_to_emmaa_stmts(): estmts = to_emmaa_stmts([stmt], date=date, search_terms=search_terms, metadata={'internal': True}) assert estmts estmt = estmts[0]
previous_test_stats_file = os.path.join(path_here, 'previous_stats.json') previous_model_stats_file = os.path.join(path_here, 'previous_model_stats.json') with open(previous_results_file, 'r') as f: previous_results = json.load(f) with open(new_results_file, 'r') as f: new_results = json.load(f) with open(previous_test_stats_file, 'r') as f: previous_test_stats = json.load(f) with open(previous_model_stats_file, 'r') as f: previous_model_stats = json.load(f) previous_stmts = [ Activation(Agent('BRAF', db_refs={'HGNC': '20974'}), Agent('MAP2K1'), evidence=[ Evidence(text='BRAF activates MAP2K1.', source_api='assertion') ]), Activation(Agent('MAP2K1', activity=ActivityCondition('activity', True)), Agent('MAPK1'), evidence=[ Evidence(text='Active MAP2K1 activates MAPK1.', source_api='assertion') ]) ] new_stmts = previous_stmts + [ Activation(Agent('BRAF', db_refs={'HGNC': '1097'}), Agent('AKT', db_refs={'FPLX': 'AKT'}), evidence=[ Evidence(text='BRAF activates AKT',