def test_structure_to_negative_interaction_states() -> None: rxncon_system = Quick("""Cdc28_P+_Bem2_[(cdc28)]; ! <Cdc28Cln13> <Cdc28Cln13>; OR <Cdc28Cln1>; OR <Cdc28Cln3> <Cdc28Cln1>; AND Cdc28_[cyclin]--Cln1_[cdc28] <Cdc28Cln3>; AND Cdc28_[cyclin]--Cln3_[cdc28]; AND Cdc28_[whi3]--0 Cdc28_[cyclin]_ppi_Cln1_[cdc28] Cdc28_[far1]_ppi_Far1_[cdc28] Cdc28_[cks1]_ppi_Cks1_[cdc28] Cak1_P+_Cdc28_[Tloop(T169)] Cdc28_[cyclin]_ppi_Cln2_[cdc28] Cdc28_[cyclin]_ppi_Cln3_[cdc28] Cdc28_[whi3]_ppi_Whi3_[cdc28]""").rxncon_system rbm = rule_based_model_from_rxncon(rxncon_system) rules_of_interest = [ rule_from_str( "Cdc28(whi3D) + Whi3(cdc28D) -> Cdc28(whi3D!1).Whi3(cdc28D!1) k_13 Cdc28_[whi3]_ppi+_Whi3_[cdc28]"), rule_from_str( "Cdc28(whi3D!1).Whi3(cdc28D!1) -> Cdc28(whi3D) + Whi3(cdc28D) k_14 Cdc28_[whi3]_ppi-_Whi3_[cdc28]") ] for rule_of_interest in rules_of_interest: identities = [rule.is_equivalent_to(rule_of_interest) for rule in rbm.rules] assert identities.count(True) == 1 # exactly once
def test_trslprocat() -> None: procatdef = reaction.ReactionDef( 'pro-cat-translation', '$x_trslprocat_$y', { '$x': (Spec, LocusResolution.component), '$y': (MRNASpec, LocusResolution.component) }, '$x%# + $y%# -> $x%# + $y%# + $y.to_protein_component_spec().with_name_suffix(\'PRO\')%!$y.to_protein_component_spec().with_name_suffix(\'CAT\')%#' # pylint: disable=line-too-long '$y.to_protein_component_spec().with_name_suffix(\'PRO\').with_domain(\'PROCAT\')%--$y.to_protein_component_spec().with_name_suffix(\'CAT\').with_domain(\'CATPRO\')%!0' ) reaction.REACTION_DEFS = reaction.DEFAULT_REACTION_DEFS + [procatdef] rxn_system = Quick("""Ribo_trslprocat_Ssy5mRNA A_p+_Ssy5CAT B_deg_Ssy5PRO""").rxncon_system rbm = rule_based_model_from_rxncon(rxn_system) expected_rules = [ 'Ribo() + Ssy5mRNA() -> Ribo() + Ssy5CAT(AR~0,CATPROD!1).Ssy5PRO(PROCATD!1) + Ssy5mRNA() k', 'A() + Ssy5CAT(AR~0) -> A() + Ssy5CAT(AR~p) k', 'B() + Ssy5PRO() -> B() k' ] assert len(rbm.rules) == len(expected_rules) for actual_rule in rbm.rules: assert any(rule_from_str(rule).is_equivalent_to(actual_rule) for rule in expected_rules) reaction.REACTION_DEFS = reaction.DEFAULT_REACTION_DEFS
def test_boolean_inhibition_interaction() -> None: rbm = rule_based_model_from_rxncon(Quick('''A_ppi+_C C_ppi+_D B_ppi+_E B_ppi+_F A_ppi+_B; x <comp1> <comp1>; OR <comp1C1> <comp1>; OR <comp2C1> <comp1C1>; AND A--C <comp1C1>; AND C--D <comp2C1>; AND B--F <comp2C1>; AND B--E''').rxncon_system) expected_rules = [ 'A(CD) + C(AD) -> A(CD!1).C(AD!1) k', 'C(DD) + D(CD) -> C(DD!1).D(CD!1) k', 'B(ED) + E(BD) -> B(ED!1).E(BD!1) k', 'B(FD) + F(BD) -> B(FD!1).F(BD!1) k', 'A(BD,CD) + B(AD,FD) -> A(BD!1,CD).B(AD!1,FD) k', 'A(BD,CD) + B(AD,ED,FD!1).F(BD!1) -> A(BD!2,CD).B(AD!2,ED,FD!1).F(BD!1) k', 'A(BD,CD!1).C(AD!1,DD) + B(AD,FD) -> A(BD!2,CD!1).B(AD!2,FD).C(AD!1,DD) k', 'A(BD,CD!1).C(AD!1,DD) + B(AD,ED,FD!2).F(BD!2) -> A(BD!3,CD!1).B(AD!3,ED,FD!2).C(AD!1,DD).F(BD!2) k', ] assert len(rbm.rules) == len(expected_rules) for actual_rule in rbm.rules: assert any(rule_from_str(rule).is_equivalent_to(actual_rule) for rule in expected_rules)
def test_single_requirement_modification() -> None: rbm = rule_based_model_from_rxncon(Quick("""A_[b]_ppi+_B_[a]; ! A_[(r)]-{p} A_[b]_ppi-_B_[a] C_p+_A_[(r)] D_p-_A_[(r)]""").rxncon_system) expected_rules = [ 'A(rR~p,bD) + B(aD) -> A(rR~p,bD!1).B(aD!1) k', 'A(bD!1).B(aD!1) -> A(bD) + B(aD) k', 'C() + A(rR~0) -> C() + A(rR~p) k', 'A(rR~p) + D() -> A(rR~0) + D() k' ] assert len(rbm.rules) == len(expected_rules) for actual_rule in rbm.rules: print(actual_rule) # assert any(rule_from_str(rule).is_equivalent_to(actual_rule) for rule in expected_rules) expected_ics = [ 'A(bD,rR~0) NumA', 'B(aD) NumB', 'D() NumD', 'C() NumC' ] for actual_ic in rbm.initial_conditions: assert any(initial_condition_from_str(ic).is_equivalent_to(actual_ic) for ic in expected_ics)
def test_single_inhibition_interaction() -> None: rbm = rule_based_model_from_rxncon(Quick("""A_[b]_ppi+_B_[a]; x A_[(r)]-{p} E_[x]_ppi+_B_[a] C_p+_A_[(r)] D_ub+_A_[(r)]""").rxncon_system) expected_rules = [ 'A(rR~ub,bD) + B(aD) -> A(rR~ub,bD!1).B(aD!1) k', 'A(rR~0,bD) + B(aD) -> A(rR~0,bD!1).B(aD!1) k', 'B(aD) + E(xD) -> B(aD!1).E(xD!1) k', 'A(rR~0) + C() -> A(rR~p) + C() k', 'A(rR~0) + D() -> A(rR~ub) + D() k' ] assert len(rbm.rules) == len(expected_rules) for actual_rule in rbm.rules: assert any(rule_from_str(rule).is_equivalent_to(actual_rule) for rule in expected_rules) expected_ics = [ 'A(bD,rR~0) NumA', 'B(aD) NumB', 'D() NumD', 'C() NumC', 'E(xD) NumE' ] for actual_ic in rbm.initial_conditions: assert any(initial_condition_from_str(ic).is_equivalent_to(actual_ic) for ic in expected_ics)
def write_bngl(excel_filename: str, base_name=None): if not base_name: base_name = os.path.splitext(os.path.basename(excel_filename))[0] base_path = os.path.dirname(excel_filename) bngl_model_filename = os.path.join(base_path, '{0}.bngl'.format(base_name)) print('Reading in Excel file [{}] ...'.format(excel_filename)) excel_book = ExcelBook(excel_filename) rxncon_system = excel_book.rxncon_system print( 'Constructed rxncon system: [{} reactions], [{} contingencies]'.format( len(rxncon_system.reactions), len(rxncon_system.contingencies))) print('Generating BNGL output ...') rbm = rule_based_model_from_rxncon(rxncon_system) print( 'Constructed rule-based model: [{} molecule types], [{} rules], [{} observables]' .format(len(rbm.mol_defs), len(rbm.rules), len(rbm.observables))) model_str = bngl_from_rule_based_model(rbm) print('Writing BNGL model file [{}] ...'.format(bngl_model_filename)) with open(bngl_model_filename, mode='w') as f: f.write(model_str)
def test_kplus_overlaps_with_reaction() -> None: rxncon_system = Quick("""A_p+_B_[(s)]; ! <Aphos> ; k+ A_[(r1)]-{p} ; k+ A_[(r2)]-{p} <Aphos>; OR A_[(r1)]-{p} <Aphos>; OR A_[(r2)]-{p} C_p+_A_[(r1)] D_p+_A_[(r2)]""").rxncon_system rbm = rule_based_model_from_rxncon(rxncon_system) for rule in rbm.rules: print(rule)
def post(self, request, system_id=None): self.system_id = system_id self.request = request self.form = RuleForm(self.request.POST or None) if self.form.is_valid(): media_url = settings.MEDIA_URL media_root = settings.MEDIA_ROOT loaded = File.objects.filter(loaded=True) if not loaded: system_type = "Quick" system = Quick.objects.filter(id=system_id)[0] project_name = system.name book = RxnconQuick(system.quick_input) else: system_type = "File" system = File.objects.filter(id=system_id)[0] project_name = system.project_name book = ExcelBook(system.get_absolute_path()) bngl_model_filename = system.slug + "_model.bngl" model_path = os.path.join(media_root, system.slug, "rule_based", bngl_model_filename) if not check_filepath(request, model_path, system, media_root): if system_type == "Quick": return quick_detail(request, system_id) else: return file_detail(request, system_id) # pickled_rxncon_system = Rxncon_system.objects.get(project_id=system_id, project_type=system_type) # rxncon_system = pickle.loads(pickled_rxncon_system.pickled_system) rxncon_system = book.rxncon_system rbm = rule_based_model_from_rxncon(rxncon_system) model_str = bngl_from_rule_based_model(rbm) if not os.path.exists(os.path.join(media_root, system.slug, "rule_based")): os.mkdir(os.path.join(media_root, system.slug, "rule_based")) with open(model_path, mode='w') as f: f.write(model_str) r = Rule_based_from_rxnconsys(project_name=project_name, model_path=model_path, comment=request.POST.get('comment')) r.save() messages.info(request, "BoolNet files for project '" + r.project_name + "' successfully created.") if system_type == "Quick": Quick.objects.filter(id=system_id).update(rule_based_model=r) return quick_detail(request, system_id) else: File.objects.filter(id=system_id).update(rule_based_model=r) return file_detail(request, system_id)
def test_simple_system() -> None: rbm = rule_based_model_from_rxncon(Quick("""A_[b]_ppi+_B_[a]; ! A_[(r)]-{p} A_[b]_ppi-_B_[a] C_p+_A_[(r)] D_p-_A_[(r)]""").rxncon_system) expected_bngl = '''begin model begin parameters NumA 100 NumB 100 NumC 100 NumD 100 k 1.0 end parameters begin molecule types A(bD,rR~0~p) B(aD) C() D() end molecule types begin seed species A(bD,rR~0) NumA B(aD) NumB C() NumC D() NumD end seed species begin observables end observables begin reaction rules # A_[b]_ppi+_B_[a] A(bD,rR~p) + B(aD) -> A(bD!1,rR~p).B(aD!1) k # A_[b]_ppi-_B_[a] A(bD!1).B(aD!1) -> A(bD) + B(aD) k # C_p+_A_[(r)] A(rR~0) + C() -> A(rR~p) + C() k # D_p-_A_[(r)] A(rR~p) + D() -> A(rR~0) + D() k end reaction rules end model simulate_nf({t_end=>100,n_steps=>10}); ''' assert bngl_from_rule_based_model(rbm) == expected_bngl
def test_mutually_exclusive_bindings() -> None: rbm = rule_based_model_from_rxncon(Quick('''C_[A]_ppi+_A_[x] D_[A]_ppi+_A_[x] B_p+_A; x C_[A]--A_[x]''').rxncon_system) expected_rules = [ 'A(xD) + C(AD) -> A(xD!1).C(AD!1) k', 'A(xD) + D(AD) -> A(xD!1).D(AD!1) k', 'A(BR~0,xD) + B() -> A(BR~p,xD) + B() k', 'A(BR~0,xD!1).D(AD!1) + B() -> A(BR~p,xD!1).D(AD!1) + B() k' ] assert len(rbm.rules) == len(expected_rules) for actual_rule in rbm.rules: assert any(rule_from_str(rule).is_equivalent_to(actual_rule) for rule in expected_rules)
def test_self_regulation() -> None: rxn_system = Quick("""Rlm1_[MADS]_bind+_Rlm1Gene PolII_trsc_Rlm1Gene Ribo_trsl_Rlm1mRNA""").rxncon_system rbm = rule_based_model_from_rxncon(rxn_system) expected_rules = [ 'Rlm1(MADSD) + Rlm1Gene(Rlm1D) -> Rlm1(MADSD!1).Rlm1Gene(Rlm1D!1) k', 'PolII() + Rlm1Gene() -> PolII() + Rlm1Gene() + Rlm1mRNA() k', 'Ribo() + Rlm1mRNA() -> Ribo() + Rlm1(MADSD) + Rlm1mRNA() k' ] assert len(rbm.rules) == len(expected_rules) for actual_rule in rbm.rules: assert any(rule_from_str(rule).is_equivalent_to(actual_rule) for rule in expected_rules)
def test_kplus_kminus() -> None: rbm = rule_based_model_from_rxncon(Quick("""A_[b]_ppi+_B_[a]; k+ A_[(r1)]-{p} A_[b]_ppi+_B_[a]; k- A_[(r2)]-{p} C_p+_A_[(r1)] D_p+_A_[(r2)]""").rxncon_system) expected_rules = [ 'A(r1R~p,r2R~p,bD) + B(aD) -> A(r1R~p,r2R~p,bD!1).B(aD!1) k', 'A(r1R~p,r2R~0,bD) + B(aD) -> A(r1R~p,r2R~0,bD!1).B(aD!1) k', 'A(r1R~0,r2R~p,bD) + B(aD) -> A(r1R~0,r2R~p,bD!1).B(aD!1) k', 'A(r1R~0,r2R~0,bD) + B(aD) -> A(r1R~0,r2R~0,bD!1).B(aD!1) k', 'A(r1R~0) + C() -> A(r1R~p) + C() k', 'A(r2R~0) + D() -> A(r2R~p) + D() k' ] assert len(rbm.rules) == len(expected_rules) for actual_rule in rbm.rules: assert any(rule_from_str(rule).is_equivalent_to(actual_rule) for rule in expected_rules)
def test_trslprocat() -> None: rxn_system = Quick("""Ribo_trslprocat_Ssy5mRNA A_p+_Ssy5CAT B_deg_Ssy5PRO""").rxncon_system rbm = rule_based_model_from_rxncon(rxn_system) expected_rules = [ 'Ribo() + Ssy5mRNA() -> Ribo() + Ssy5CAT(AR~0,CATPROD!1).Ssy5PRO(PROCATD!1) + Ssy5mRNA() k', 'A() + Ssy5CAT(AR~0) -> A() + Ssy5CAT(AR~p) k', 'B() + Ssy5PRO() -> B() k' ] assert len(rbm.rules) == len(expected_rules) for actual_rule in rbm.rules: assert any( rule_from_str(rule).is_equivalent_to(actual_rule) for rule in expected_rules)
def test_simple_system() -> None: rbm = rule_based_model_from_rxncon( Quick("""A_[b]_ppi+_B_[a]; ! A_[(r)]-{p} A_[b]_ppi-_B_[a] C_p+_A_[(r)]; k+ A_[b]--B_[a] D_p-_A_[(r)]""").rxncon_system) expected_bngl = '''begin model begin parameters NumA 1000 NumB 1000 NumC 1000 NumD 1000 k_1 1.0 # A_[b]_ppi+_B_[a] k_2 1.0 # A_[b]_ppi-_B_[a] k_3_1 1.0 # C_p+_A_[(r)] k_3_2 1.0 # C_p+_A_[(r)] k_4 1.0 # D_p-_A_[(r)] end parameters begin molecule types A(bD,rR~0~p) B(aD) C() D() end molecule types begin seed species A(bD,rR~0) NumA B(aD) NumB C() NumC D() NumD end seed species begin observables end observables begin reaction rules # Rule 1. rxn: A_[b]_ppi+_B_[a], quant_cont: UniversalSet A(bD,rR~p) + B(aD) -> A(bD!1,rR~p).B(aD!1) k_1 # Rule 2. rxn: A_[b]_ppi-_B_[a], quant_cont: UniversalSet A(bD!1).B(aD!1) -> A(bD) + B(aD) k_2 # Rule 3. rxn: C_p+_A_[(r)], quant_cont: A@1_[b]--B@2_[a] A(bD!1,rR~0).B(aD!1) + C() -> A(bD!1,rR~p).B(aD!1) + C() k_3_1 # Rule 4. rxn: C_p+_A_[(r)], quant_cont: !(A@1_[b]--B@2_[a]) A(bD,rR~0) + C() -> A(bD,rR~p) + C() k_3_2 # Rule 5. rxn: D_p-_A_[(r)], quant_cont: UniversalSet A(rR~p) + D() -> A(rR~0) + D() k_4 end reaction rules end model simulate_nf({t_end=>100,n_steps=>10}); ''' assert bngl_from_rule_based_model(rbm) == expected_bngl