def test_simple_assembly(): st1 = Activation(Agent('a'), Agent('b')) st2 = Inhibition(Agent('a'), Agent('c')) sa = SifAssembler([st1, st2]) sa.make_model() assert(len(sa.graph.nodes()) == 3) assert(len(sa.graph.edges()) == 2)
def test_simple_assembly(): st1 = Activation(Agent('a'), Agent('b')) st2 = Inhibition(Agent('a'), Agent('c')) sa = SifAssembler([st1, st2]) sa.make_model() assert (len(sa.graph.nodes()) == 3) assert (len(sa.graph.edges()) == 2)
def assemble_loopy(): """Assemble INDRA Statements into a Loopy model using SIF Assembler.""" response = request.body.read().decode('utf-8') body = json.loads(response) stmts_json = body.get('statements') stmts = stmts_from_json(stmts_json) sa = SifAssembler(stmts) sa.make_model(use_name_as_key=True) model_str = sa.print_loopy(as_url=True) res = {'loopy_url': model_str} return res
def test_evidence_assembly(): ev1 = Evidence(pmid='1') ev2 = Evidence(pmid='2') ev3 = Evidence(pmid='3') ev4 = Evidence(pmid='4') st1 = Activation(Agent('a'), Agent('b'), evidence=[ev1]) st2 = Inhibition(Agent('a'), Agent('c'), evidence=[ev1, ev2, ev3]) sa = SifAssembler([st1, st2]) sa.make_model() assert(len(sa.graph.nodes()) == 3) assert(len(sa.graph.edges()) == 2) sa.set_edge_weights('support_pmid')
def assemble_pysb(stmts, data_genes, out_file): """Return an assembled PySB model.""" base_file, _ = os.path.splitext(out_file) #stmts = ac.load_statements('%s.pkl' % base_file) stmts = preprocess_stmts(stmts, data_genes) # Make a SIF model equivalent to the PySB model # Useful for making direct comparisons in pathfinding sa = SifAssembler(stmts) sa.make_model(use_name_as_key=True, include_mods=True, include_complexes=True) sif_str = sa.print_model(include_unsigned_edges=True) with open('%s_pysb.sif' % base_file, 'wt') as f: f.write(sif_str) # This is the "final" set of statements going into the assembler so it # makes sense to cache these. # This is also the point where index cards can be generated ac.dump_statements(stmts, '%s_before_pa.pkl' % base_file) assemble_index_cards(stmts, 'output/index_cards') # Save a version of statements with no evidence for faster loading for s in stmts: s.evidence = [] for ss in s.supports + s.supported_by: ss.evidence = [] ac.dump_statements(stmts, '%s_no_evidence.pkl' % base_file) # Assemble model pa = PysbAssembler() pa.add_statements(stmts) pa.make_model(reverse_effects=False) #ac.dump_statements(pa.statements, '%s_after_pa.pkl' % base_file) # Set context set_context(pa) # Add observables add_observables(pa.model) pa.save_model(out_file) with open('korkut_pysb.pkl', 'wb') as fh: pickle.dump(pa.model, fh) #pa.export_model('kappa', '%s.ka' % base_file) return pa.model
def test_modification(): st1 = Phosphorylation(Agent('BRAF'), Agent('MAP2K1'), 'S', '222') sa = SifAssembler([st1]) sa.make_model(True, True, True) assert (len(sa.graph.nodes()) == 2) assert (len(sa.graph.edges()) == 1) sa.save_model('test_sif.sif', True) with open('test_sif.sif', 'rb') as fh: txt = fh.read().decode('utf-8') assert txt == 'BRAF 0 MAP2K1\n', txt
def test_evidence_assembly(): ev1 = Evidence(pmid='1') ev2 = Evidence(pmid='2') ev3 = Evidence(pmid='3') Evidence(pmid='4') st1 = Activation(Agent('a'), Agent('b'), evidence=[ev1]) st2 = Inhibition(Agent('a'), Agent('c'), evidence=[ev1, ev2, ev3]) sa = SifAssembler([st1, st2]) sa.make_model() assert (len(sa.graph.nodes()) == 3) assert (len(sa.graph.edges()) == 2) sa.set_edge_weights('support_pmid')
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 # Because of a bug in CNO, node names containing AND # need to be replaced def rename_and_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') rename_and_nodes(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) print('Boolean PKN contains these antibodies: %s' % ', '.join(pkn_abs)) # Make the SIF model sa = SifAssembler(stmts) sa.make_model(use_name_as_key=True) sif_str = sa.print_model() 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) return sif_str
on_nodes = [] else: on_nodes = on coll = boolean2.util.Collector() bn_str = boolean2.modify_states(bn_str, turnon=on, turnoff=off) model = boolean2.Model(text=bn_str, mode='async') for i in range(nsim): model.initialize() model.iterate(steps=nsteps) coll.collect(states=model.states, nodes=model.nodes) avgs = coll.get_averages(normalize=True) return avgs st = pickle.load(open('statements.pkl', 'rb')) sa = SifAssembler(st) sa.make_model() bn_str = sa.print_boolean_net('ras_pathway_bn.txt') # Condition 1 off = [] on = ['Growth_factor_proteins'] avgs = get_sim_avgs(bn_str, off=off, on=on) jun_basic_noinh = avgs['JUN'] # Condition 2 off = ['MAP2K1', 'MAP2K2'] on = ['Growth_factor_proteins'] avgs = get_sim_avgs(bn_str, off=off, on=on) jun_basic_inh = avgs['JUN'] st_ext = pickle.load(open('extension.pkl', 'rb')) sa = SifAssembler(st + st_ext)
on_nodes = on coll = boolean2.util.Collector() bn_str = boolean2.modify_states(bn_str, turnon=on, turnoff=off) model = boolean2.Model(text=bn_str, mode='async') for i in range(nsim): model.initialize() model.iterate(steps=nsteps) coll.collect(states=model.states, nodes=model.nodes) avgs = coll.get_averages(normalize=True) return avgs if __name__ == '__main__': # Build Boolean net for basic pathway st = ac.load_statements('ras_pathway.pkl') sa = SifAssembler(st) sa.make_model(use_name_as_key=True) sa.save_model('ras_pathway.sif') bn_str = sa.print_boolean_net('ras_pathway_bn.txt') # Build Boolean net for extended pathway st_ext = ac.load_statements('ras_pathway_extension.pkl') sa = SifAssembler(st + st_ext) sa.make_model(use_name_as_key=True) sa.save_model('ras_pathway_extension.sif') bn_str = sa.print_boolean_net('ras_pathway_extension_bn.txt') # Condition 1 off = [] on = ['GROWTH-FACTOR'] avgs = get_sim_avgs(bn_str, off=off, on=on)