def test_set_context(): cxa = CxAssembler() cxa.add_statements([st_phos, st_dephos]) cxa.make_model() cxa.set_context('BT20_BREAST') print(cxa.cx['nodeAttributes']) assert len(cxa.cx['nodeAttributes']) == 11
def assemble_cx(stmts, name): ca = CxAssembler() ca.network_name = name ca.add_statements(stmts) ca.make_model() cx_str = ca.print_cx() return cx_str
def assemble_cx(stmts_with_counts): for channel, (stmts, ev_counts, source_counts) in stmts_with_counts.items(): cxa = CxAssembler(stmts, network_name='%s INDRA interactome' % channel) cxa.make_model() add_semantic_hub_layout(cxa.cx, channel) model_id = cxa.upload_model()
def upload_to_ndex(self): """Upload the assembled model as CX to NDEx""" assembled_stmts = self.run_assembly() cxa = CxAssembler(assembled_stmts, network_name=self.name) cxa.make_model() cx_str = cxa.print_cx() ndex_client.update_network(cx_str, self.ndex_network)
def update_to_ndex(self): """Update assembled model as CX on NDEx, updates existing network.""" if not self.assembled_stmts: self.run_assembly() cxa = CxAssembler(self.assembled_stmts, network_name=self.name) cxa.make_model() cx_str = cxa.print_cx() ndex_client.update_network(cx_str, self.ndex_network)
def upload_to_ndex(self): """Upload the assembled model as CX to NDEx, creates new network.""" if not self.assembled_stmts: self.run_assembly() cxa = CxAssembler(self.assembled_stmts, network_name=self.name) cxa.make_model() model_uuid = cxa.upload_model() self.ndex_network = model_uuid return model_uuid
def upload_ndex_network(kinase, stmts): network_set_id = '9d9d4f66-e3da-11eb-b666-0ac135e8bacf' name = '%s INDRA network' % kinase cxa = CxAssembler(stmts, name) cxa.make_model() add_semantic_hub_layout(cxa.cx, kinase) network_id = cxa.upload_model(private=False) # Style setting already done as part of upload # ndex_client.set_style(network_id) ndex_client.add_to_network_set(network_id, network_set_id) return network_id
def test_cited(): cxa = CxAssembler() cxa.add_statements([st_cited]) cxa.make_model() assert len(cxa.cx['citations']) == 1 assert len(cxa.cx['edgeCitations']) == 1 citation = cxa.cx['citations'][0] assert citation.get('dc:identifier') == 'pmid:12345' cid = citation.get('@id') assert cxa.cx['edgeCitations'][0]['citations'][0] == cid print(cxa.print_cx())
def test_eidos_to_cx(): stmts = __get_stmts_from_remote_jsonld() cx = CxAssembler() # Make sure these don't error cx.add_statements(stmts) cx.make_model() test_fname = 'test_cag_to_cx.cx' try: cx.save_model(test_fname) assert os.path.exists(test_fname), "Failed to create cx file." finally: if os.path.exists(test_fname): os.remove(test_fname) return
def share_model_ndex(): """Upload the model to NDEX""" if request.method == 'OPTIONS': return {} response = request.body.read().decode('utf-8') body = json.loads(response) stmts_str = body.get('stmts') stmts_json = json.loads(stmts_str) stmts = stmts_from_json(stmts_json["statements"]) ca = CxAssembler(stmts) for n, v in body.items(): ca.cx['networkAttributes'].append({'n': n, 'v': v, 'd': 'string'}) ca.make_model() network_id = ca.upload_model(private=False) return {'network_id': network_id}
def assemble_cx(): """Assemble INDRA Statements and return CX network json.""" if request.method == 'OPTIONS': return {} response = request.body.read().decode('utf-8') body = json.loads(response) stmts_json = body.get('statements') stmts = stmts_from_json(stmts_json) ca = CxAssembler(stmts) model_str = ca.make_model() res = {'model': model_str} return res
def test_hub_layout(): stmts = [st_phos, st_dephos, st_act] cxa = CxAssembler(stmts) cxa.make_model() graph = hub_layout.cx_to_networkx(cxa.cx) erk = hub_layout.get_node_by_name(graph, 'MAPK1') hub_layout.add_semantic_hub_layout(cxa.cx, 'MAPK1') assert cxa.cx['cartesianLayout'] for node in cxa.cx['cartesianLayout']: if node['node'] == erk: assert node['x'] == 0.0 assert node['y'] == 0.0 else: assert node['x'] != 0 assert node['y'] != 0 node_classes = hub_layout.classify_nodes(graph, erk) assert node_classes[hub_layout.get_node_by_name(graph, 'DUSP4')] == \ (1, 'modification', 'other') assert node_classes[hub_layout.get_node_by_name(graph, 'MAP2K1')] in \ {(1, 'activity', 'protein'), (1, 'modification', 'protein')}
def post(self): """Upload the model to NDEX. Parameters ---------- statements : list[indra.statements.Statement.to_json()] A list of INDRA Statements to assemble. Returns ------- network_id : str ID of uploaded NDEx network. """ args = request.json stmts_json = args.get('statements') stmts = stmts_from_json(stmts_json) ca = CxAssembler(stmts) for n, v in args.items(): ca.cx['networkAttributes'].append({'n': n, 'v': v, 'd': 'string'}) ca.make_model() network_id = ca.upload_model(private=False) return {'network_id': network_id}
def assemble_cx(stmts, out_file_prefix, network_type): """Return a CX assembler.""" stmts = ac.filter_belief(stmts, 0.95) stmts = ac.filter_top_level(stmts) if network_type == 'direct': stmts = ac.filter_direct(stmts) out_file = '%s_%s.cx' % (out_file_prefix, network_type) ca = CxAssembler() ca.add_statements(stmts) model = ca.make_model() ca.save_model(out_file) return ca
def create_upload_model(model_name, full_name, indra_stmts, ndex_id=None): """Make and upload an EMMAA model from a list of INDRA Statements. Parameters ---------- short_name : str Short name of the model to use on S3. full_name : str Human-readable model name to use in EMMAA dashboard. indra_stmts : list of indra.statement INDRA Statements to be used to populate the EMMAA model. ndex_id : str UUID of the network corresponding to the model on NDex. If provided, the NDex network will be updated with the latest model content. If None (default), a new network will be created and the UUID stored in the model config files on S3. """ emmaa_stmts = to_emmaa_stmts(indra_stmts, datetime.datetime.now(), []) # Get updated CX content for the INDRA Statements cxa = CxAssembler(indra_stmts) cx_str = cxa.make_model() # If we don't have an NDex ID, create network and upload to Ndex if ndex_id is None: ndex_id = cxa.upload_model(private=False) print(f'NDex ID for {model_name} is {ndex_id}.') # If the NDEx ID is provided, update the existing network else: ndex_client.update_network(cx_str, ndex_id) # Create the config dictionary config_dict = {'ndex': {'network': ndex_id}, 'search_terms': []} # Create EMMAA model emmaa_model = EmmaaModel(model_name, config_dict) emmaa_model.add_statements(emmaa_stmts) # Upload model to S3 with config as YAML and JSON emmaa_model.save_to_s3() s3_client = boto3.client('s3') config_json = json.dumps(config_dict) s3_client.put_object(Body=config_json.encode('utf8'), Key='models/%s/config.json' % model_name, Bucket='emmaa') config_json = json.dumps(config_dict) s3_client.put_object(Body=config_json.encode('utf8'), Key='models/%s/config.json' % model_name, Bucket='emmaa')
def post(self): """Assemble INDRA Statements and return CX network json. Parameters ---------- statements : list[indra.statements.Statement.to_json()] A list of INDRA Statements to assemble. Returns ------- model Assembled model string. """ args = request.json stmts_json = args.get('statements') stmts = stmts_from_json(stmts_json) ca = CxAssembler(stmts) model_str = ca.make_model() res = {'model': model_str} return res
def create_upload_model(model_name, indra_stmts, config_file): """Make and upload an EMMAA model from a list of INDRA Statements. Parameters ---------- model_name : str Name of the model to use on S3. indra_stmts : list of indra.statement INDRA Statements to be used to populate the EMMAA model. config_file : str Path to the local config.json file. """ emmaa_stmts = to_emmaa_stmts(indra_stmts, datetime.datetime.now(), [], {'internal': True}) # Load config information with open(config_file, 'rt') as f: config_json = json.load(f) # If there is no ndex entry in the config, create a new network and update # the config file with the NDex network ID if 'ndex' not in config_json: cxa = CxAssembler(indra_stmts) cx_str = cxa.make_model() ndex_id = cxa.upload_model(private=False) print(f'NDex ID for {model_name} is {ndex_id}.') config_json['ndex'] = {'network': ndex_id} updated_config_file = f'{config_file}.updated' with open(updated_config_file, 'wt') as f: json.dump(config_json, f, indent=2) # If the NDEx ID is provided we don't need to update the existing network # because this will occur as part of the model assembly/update procedure # on EMMAA itself. # Create the config dictionary # Create EMMAA model emmaa_model = EmmaaModel(model_name, config_json) emmaa_model.add_statements(emmaa_stmts) # Upload model to S3 emmaa_model.save_to_s3() # Upload config JSON s3_client = boto3.client('s3') save_config_to_s3(model_name, config_json)
def test_act(): cxa = CxAssembler() cxa.add_statements([st_act, st_act2]) cxa.make_model() assert len(cxa.cx['nodes']) == 3 assert len(cxa.cx['edges']) == 2
def test_complex2(): cxa = CxAssembler() cxa.add_statements([st_complex2]) cxa.make_model() assert len(cxa.cx['nodes']) == 3 assert len(cxa.cx['edges']) == 5
def test_dephos(): cxa = CxAssembler() cxa.add_statements([st_phos, st_dephos]) cxa.make_model() assert len(cxa.cx['nodes']) == 3 assert len(cxa.cx['edges']) == 2
def test_no_pmid(): cxa = CxAssembler([st_not_cited]) cxa.make_model() assert not cxa.cx['edgeCitations']
def test_make_print_model(): cxa = CxAssembler() cxa.add_statements([st_phos]) cx_str = cxa.make_model() assert cx_str
def assemble_cx(statements): cxa = CxAssembler(statements) model = cxa.make_model() cxa.save_model('model.cx')
import sys import pickle from indra.databases import ndex_client from indra.assemblers.cx import CxAssembler from indra.assemblers.cx.hub_layout import add_semantic_hub_layout if __name__ == '__main__': kinase = sys.argv[1] with open('dark_kinase_statements_assembled.pkl', 'rb') as fh: stmts = pickle.load(fh) cxa = CxAssembler(stmts[kinase]) cxa.make_model() add_semantic_hub_layout(cxa.cx, kinase) model_id = cxa.upload_model() ndex_client.set_style(model_id)
def test_gene_network(): # Chunk 1: this is tested in _get_gene_network_stmts # from indra.tools.gene_network import GeneNetwork # gn = GeneNetwork(['H2AX']) # biopax_stmts = gn.get_biopax_stmts() # bel_stmts = gn.get_bel_stmts() # Chunk 2 from indra import literature pmids = literature.pubmed_client.get_ids_for_gene('H2AX') # Chunk 3 from indra import literature paper_contents = {} for pmid in pmids: content, content_type = literature.get_full_text(pmid, 'pmid') if content_type == 'abstract': paper_contents[pmid] = content if len(paper_contents) == 5: # Is 10 in actual code break # Chunk 4 from indra.sources import reach literature_stmts = [] for pmid, content in paper_contents.items(): rp = reach.process_text(content, url=reach.local_text_url) literature_stmts += rp.statements print('Got %d statements' % len(literature_stmts)) assert literature_stmts # replaces a print statements # Chunk 6 from indra.tools import assemble_corpus as ac # stmts = biopax_stmts + bel_stmts + literature_stmts # tested elsewhere stmts = gn_stmts + literature_stmts # Added instead of above line stmts = ac.map_grounding(stmts) stmts = ac.map_sequence(stmts) stmts = ac.run_preassembly(stmts) assert stmts # Chunk 7 from indra.assemblers.cx import CxAssembler from indra.databases import ndex_client cxa = CxAssembler(stmts) cx_str = cxa.make_model() assert cx_str # Chunk 8 # ndex_cred = {'user': '******', 'password': '******'} # network_id = ndex_client.create_network(cx_str, ndex_cred) # print(network_id) # Chunk 9 from indra.assemblers.indranet import IndraNetAssembler indranet_assembler = IndraNetAssembler(statements=stmts) indranet = indranet_assembler.make_model() assert len(indranet.nodes) > 0, 'indranet conatins no nodes' assert len(indranet.edges) > 0, 'indranet conatins no edges' # Chunk 10 import networkx as nx paths = nx.single_source_shortest_path(G=indranet, source='H2AX', cutoff=1) assert paths # Chunk 11 from indra.assemblers.pysb import PysbAssembler pysb = PysbAssembler(statements=stmts) pysb_model = pysb.make_model() assert pysb_model
def assemble_sif(stmts, data, out_file): """Return an assembled SIF.""" # Filter for high-belief statements stmts = ac.filter_belief(stmts, 0.99) stmts = ac.filter_top_level(stmts) # Filter for Activation / Inhibition stmts_act = ac.filter_by_type(stmts, Activation) stmts_inact = ac.filter_by_type(stmts, Inhibition) stmts = stmts_act + stmts_inact # Get Ras227 and filter statments ras_genes = process_data.get_ras227_genes() #ras_genes = [x for x in ras_genes if x not in ['YAP1']] stmts = ac.filter_gene_list(stmts, ras_genes, 'all') # Get the drugs inhibiting their targets as INDRA # statements def get_drug_statements(): drug_targets = process_data.get_drug_targets() drug_stmts = [] for dn, tns in drug_targets.items(): da = Agent(dn + ':Drugs') for tn in tns: ta = Agent(tn) drug_stmt = Inhibition(da, ta) drug_stmts.append(drug_stmt) return drug_stmts drug_stmts = get_drug_statements() stmts = stmts + drug_stmts # Rewrite statements to replace genes with their corresponding # antibodies when possible stmts = rewrite_ab_stmts(stmts, data) def filter_ab_edges(st, policy='all'): st_out = [] for s in st: if policy == 'all': all_ab = True for a in s.agent_list(): if a is not None: if a.name.find('_p') == -1 and \ a.name.find('Drugs') == -1: all_ab = False break if all_ab: st_out.append(s) elif policy == 'one': any_ab = False for a in s.agent_list(): if a is not None and a.name.find('_p') != -1: any_ab = True break if any_ab: st_out.append(s) return st_out stmts = filter_ab_edges(stmts, 'all') # Get a list of the AB names that end up being covered in the prior network # This is important because other ABs will need to be taken out of the # MIDAS file to work. def get_ab_names(st): prior_abs = set() for s in st: for a in s.agent_list(): if a is not None: if a.name.find('_p') != -1: prior_abs.add(a.name) return sorted(list(prior_abs)) pkn_abs = get_ab_names(stmts) def get_drug_names(st): prior_drugs = set() for s in st: for a in s.agent_list(): if a is not None: if a.name.find('Drugs') != -1: prior_drugs.add(a.name.split(':')[0]) return sorted(list(prior_drugs)) pkn_drugs = get_drug_names(stmts) print('Boolean PKN contains these antibodies: %s' % ', '.join(pkn_abs)) # Because of a bug in CNO, # node names containing AND need to be replaced # node names containing - need to be replaced # node names starting in a digit need to be replaced # must happen before SIF assembly, but not sooner as that will drop # names from the MIDAS file def rename_nodes(st): for s in st: for a in s.agent_list(): if a is not None: if a.name.find('AND') != -1: a.name = a.name.replace('AND', 'A_ND') if a.name.find('-') != -1: a.name = a.name.replace('-', '_') if a.name[0].isdigit(): a.name = 'abc_' + a.name rename_nodes(stmts) # Make the SIF model sa = SifAssembler(stmts) sa.make_model(use_name_as_key=True) sif_str = sa.print_model() # assemble and dump a cx of the sif ca = CxAssembler() ca.add_statements(stmts) model = ca.make_model() ca.save_model('sif.cx') with open(out_file, 'wb') as fh: fh.write(sif_str.encode('utf-8')) # Make the MIDAS data file used for training the model midas_data = process_data.get_midas_data(data, pkn_abs, pkn_drugs) return sif_str
def test_invalid_cited(): cxa = CxAssembler() cxa.add_statements([st_invalid_cited]) cxa.make_model() assert not cxa.cx['citations'] assert not cxa.cx['edgeCitations']
def test_gap(): cxa = CxAssembler() cxa.add_statements([st_gap]) cxa.make_model() assert len(cxa.cx['nodes']) == 2 assert len(cxa.cx['edges']) == 1
def test_supports(): cxa = CxAssembler() cxa.add_statements([st_cited]) cxa.make_model() assert len(cxa.cx['supports']) == 1 assert len(cxa.cx['edgeSupports']) == 1
def test_edge_attributes(): cxa = CxAssembler() cxa.add_statements([st_phos, st_dephos]) cxa.make_model() assert len(cxa.cx['edgeAttributes']) == 14
def assemble_cx(stmts, save_file): cxa = CxAssembler(stmts) cxa.make_model(add_indra_json=False) cxa.save_model(save_file) return cxa