def _make_queries_in_results(result_iter): # Each element of result_iter: (model_id, query_json, result_json, date) # Replace query_json with Query object results = [] for res in result_iter: query = QueryObject._from_json(res[1]) results.append((res[0], query, res[2], res[3], res[4])) return results
def test_intervention_query_from_json(): query_file = join(dirname(abspath(__file__)), 'intervention_query.json') with open(query_file, 'r') as f: json_dict = json.load(f) query = Query._from_json(json_dict) assert query assert isinstance(query, SimpleInterventionProperty) 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.direction == 'up'
def test_open_query_to_json(): ag = Agent('EGFR', db_refs={'HGNC': '3236'}) query = OpenSearchQuery(ag, 'Inhibition', 'object', ['chebi']) assert query json = query.to_json() assert json.get('type') == 'open_search_query' assert json.get('stmt_type') == 'Inhibition' assert json.get('entity_role') == 'object' assert json.get('terminal_ns') == ['chebi'] deserialize_query = Query._from_json(json) json2 = deserialize_query.to_json() assert json == json2, {'json': json, 'json2': json2}
def test_path_property_to_json(): stmt = Phosphorylation(enz=Agent('EGFR', db_refs={'HGNC': '3236'}), sub=Agent('ERK', db_refs={'FPLX': 'ERK'})) entity_constraints = {'exclude': [Agent('PI3K', db_refs={'FPLX': 'PI3K'})]} relationship_contraints = {'exclude': ['IncreaseAmount', 'DecreaseAmount']} query = PathProperty(stmt, entity_constraints, relationship_contraints) assert query json = query.to_json() assert json.get('type') == 'path_property' path = json.get('path') assert path.get('type') == 'Phosphorylation' deserialize_query = Query._from_json(json) json2 = deserialize_query.to_json() assert json == json2, {'json': json, 'json2': json2}
def test_open_query_from_json(): query_file = join(dirname(abspath(__file__)), 'open_query.json') with open(query_file, 'r') as f: json_dict = json.load(f) query = Query._from_json(json_dict) assert query assert isinstance(query, OpenSearchQuery) assert isinstance(query.entity, Agent) assert query.entity.name == 'EGFR' assert query.entity_role == 'object' assert query.stmt_type == 'Inhibition' assert isinstance(query.path_stmt, Inhibition) assert query.path_stmt.subj is None assert query.terminal_ns == ['chebi']
def test_path_property_from_json(): query_file = join(dirname(abspath(__file__)), 'path_property_query.json') with open(query_file, 'r') as f: json_dict = json.load(f) query = Query._from_json(json_dict) assert query assert isinstance(query, PathProperty) assert isinstance(query.path_stmt, Phosphorylation), query.path_stmt assert query.path_stmt.enz.name == 'EGFR', query.path_stmt assert query.path_stmt.sub.name == 'ERK', query.path_stmt assert isinstance(query.exclude_entities[0], Agent) assert query.exclude_entities[0].name == 'PI3K' assert isinstance(query.include_entities[0], Agent) assert query.include_entities[0].name == 'MAPK1' assert set(query.exclude_rels) == set(['IncreaseAmount', 'DecreaseAmount']) assert query.include_rels[0] == 'Inhibition'
def test_dynamic_property_from_json(): query_file = join(dirname(abspath(__file__)), 'dynamic_property_query.json') with open(query_file, 'r') as f: json_dict = json.load(f) query = Query._from_json(json_dict) assert query assert isinstance(query, DynamicProperty) assert isinstance(query.entity, Agent) assert query.entity.name == 'EGFR' assert isinstance(query.pattern_type, str) assert query.pattern_type == 'always_value' assert isinstance(query.quant_value, str) assert query.quant_value == 'low' assert isinstance(query.quant_type, str) assert query.quant_type == 'qualitative'
def get_queries(self, model_id): """Get queries that refer to the given model_id. Parameters ---------- model_id : str The short, standard model ID. Returns ------- queries : list[emmaa.queries.Query] A list of queries retrieved from the database. """ # TODO: check whether a query is registered or not. with self.get_session() as sess: q = sess.query(Query.json).filter(Query.model_id == model_id) queries = [QueryObject._from_json(q) for q, in q.all()] return queries
def get_queries(self, model_id): """Get queries that refer to the given model_id. Parameters ---------- model_id : str The short, standard model ID. Returns ------- queries : list[emmaa.queries.Query] A list of queries retrieved from the database. """ with self.get_session() as sess: q = sess.query(Query.json).filter( Query.model_id == model_id, Query.hash == UserQuery.query_hash, UserQuery.subscription).distinct() queries = [QueryObject._from_json(q) for q, in q.all()] return queries
def get_subscribed_queries(self, email): """Get a list of (query object, model id, query hash) for a user Parameters ---------- email : str The email address to check subscribed queries for Returns ------- list(tuple(emmaa.queries.Query, str, query_hash)) """ logger.info(f"Got request to list user queries for {email}") # Get the query json for which email is subscribed with self.get_session() as sess: q = sess.query(Query.json, Query.model_id, Query.hash).filter( Query.hash == UserQuery.query_hash, UserQuery.user_id == User.id, User.email == email, UserQuery.subscription) # Returns list of (query json, query hash) tuples return [(QueryObject._from_json(qj), mid, qh) for qj, mid, qh in q.all()]
'db_refs': { 'HGNC': '1097' } }, 'obj': { 'type': 'Agent', 'name': 'MAPK1', 'db_refs': { 'HGNC': '6871' } }, 'obj_activity': 'activity' } } simple_query = 'BRAF activates MAPK1.' query_object = Query._from_json(test_query) dyn_ag = get_agent_from_trips('active MAP2K1') dyn_query = DynamicProperty(dyn_ag, 'eventual_value', 'high') open_qj = { 'type': 'open_search_query', 'entity': { 'type': 'Agent', 'name': 'BRAF', 'db_refs': { 'HGNC': '1097' } }, 'entity_role': 'subject', 'stmt_type': 'Activation' } open_query = Query._from_json(open_qj)
}, 'obj': { 'type': 'Agent', 'name': 'MAPK1', 'db_refs': { 'HGNC': '6871' } } } } simple_query = { 'typeSelection': 'Activation', 'subjectSelection': 'BRAF', 'objectSelection': 'MAPK1' } query_object = Query._from_json(test_query) test_response = { 3801854542: [('BRAF activates MAP2K1.', 'https://db.indra.bio/statements/from_agents?subject=1097@HGNC&object=' '6840@HGNC&type=Activation&format=html'), ('Active MAP2K1 activates MAPK1.', 'https://db.indra.bio/statements/from_agents?subject=6840@HGNC&object=' '6871@HGNC&type=Activation&format=html')] } processed_link = '<a href="https://db.indra.bio/statements/from_agents?'\ 'subject=1097@HGNC&object=6840@HGNC&type=Activation&format=html" '\ 'target="_blank" class="status-link">'\ 'BRAF activates MAP2K1.</a>' query_not_appl = {
}, { 'type': 'path_property', 'path': { 'type': 'Phosphorylation', 'enz': { 'type': 'Agent', 'name': 'ERK' }, 'sub': { 'type': 'Agent', 'name': 'MEK' } } }] test_queries = [QueryObject._from_json(qj) for qj in test_query_jsons] @with_setup(setup_query_db, teardown_query_db) @attr('nonpublic') def test_instantiation(): db = _get_test_db() assert db return @with_setup(setup_query_db, teardown_query_db) @attr('nonpublic') def test_put_queries(): db = _get_test_db() db.put_queries('joshua', 1, test_queries[0], ['aml', 'luad'])