def test(self): """ Test that algorithmically-like submodels are correctly merged """ """ Construct model with 3 submodels: two SSA and one FBA """ mdl = Model() cmp = mdl.compartments.create(id='comp_0', name='compartment 0') species = [] for i in range(5): st = mdl.species_types.create(id='spec_type_{}'.format(i), type=onto['WC:metabolite']) s = mdl.species.create(species_type=st, compartment=cmp) s.id = s.gen_id() species.append(s) submdl_0 = mdl.submodels.create( id='submdl_0', framework=onto['WC:stochastic_simulation_algorithm']) submdl_1 = mdl.submodels.create( id='submdl_1', framework=onto['WC:stochastic_simulation_algorithm']) submdl_2 = mdl.submodels.create( id='submdl_2', framework=onto['WC:dynamic_flux_balance_analysis']) submdl_3 = mdl.submodels.create( id='submdl_3', framework=onto['WC:dynamic_flux_balance_analysis']) rxn_0_0 = mdl.reactions.create(id='rxn_0_0', submodel=submdl_0) rxn_0_0.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_0_0.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_0_0.participants.add( SpeciesCoefficient(species=species[2], coefficient=1)) rxn_0_1 = mdl.reactions.create(id='rxn_0_1', submodel=submdl_0) rxn_0_1.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_0_1.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_0_1.participants.add( SpeciesCoefficient(species=species[2], coefficient=1)) rxn_1_0 = mdl.reactions.create(id='rxn_1_0', submodel=submdl_1) rxn_1_0.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_1_0.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_1_0.participants.add( SpeciesCoefficient(species=species[3], coefficient=1)) rxn_1_1 = mdl.reactions.create(id='rxn_1_1', submodel=submdl_1) rxn_1_1.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_1_1.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_1_1.participants.add( SpeciesCoefficient(species=species[3], coefficient=1)) rxn_2_0 = mdl.reactions.create(id='rxn_2_0', submodel=submdl_2) rxn_2_0.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_2_0.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_2_0.participants.add( SpeciesCoefficient(species=species[4], coefficient=1)) rxn_2_1 = mdl.reactions.create(id='rxn_2_1', submodel=submdl_2) rxn_2_1.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_2_1.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_2_1.participants.add( SpeciesCoefficient(species=species[4], coefficient=1)) dfba_obj_rxn_2_0 = mdl.dfba_obj_reactions.create(id='dfba_obj_rxn_2_0', submodel=submdl_2) dfba_obj_rxn_2_1 = mdl.dfba_obj_reactions.create(id='dfba_obj_rxn_2_1', submodel=submdl_2) dfba_obj_rxn_3_0 = mdl.dfba_obj_reactions.create(id='dfba_obj_rxn_3_0', submodel=submdl_3) dfba_obj_rxn_3_1 = mdl.dfba_obj_reactions.create(id='dfba_obj_rxn_3_1', submodel=submdl_3) dfba_obj_2 = mdl.dfba_objs.create( id='dfba_obj_2', submodel=submdl_2, expression=DfbaObjectiveExpression.deserialize( 'rxn_2_0 + 2 * rxn_2_1 + 3 * dfba_obj_rxn_2_0 + dfba_obj_rxn_2_1', { Reaction: { 'rxn_2_0': rxn_2_0, 'rxn_2_1': rxn_2_1, }, DfbaObjReaction: { 'dfba_obj_rxn_2_0': dfba_obj_rxn_2_0, 'dfba_obj_rxn_2_1': dfba_obj_rxn_2_1, }, })[0]) dfba_obj_3 = mdl.dfba_objs.create( id='dfba_obj_3', submodel=submdl_3, expression=DfbaObjectiveExpression.deserialize( 'dfba_obj_rxn_3_0 + dfba_obj_rxn_3_1', { Reaction: {}, DfbaObjReaction: { 'dfba_obj_rxn_3_0': dfba_obj_rxn_3_0, 'dfba_obj_rxn_3_1': dfba_obj_rxn_3_1, } })[0]) mdl.parameters.create(id='param_0') mdl.parameters.create(id='param_1') mdl.parameters.create(id='param_2') mdl.observations.create(id='obs_0') mdl.observations.create(id='obs_1') mdl.observations.create(id='obs_2') mdl.observations.create(id='obs_3') mdl.observations.create(id='obs_4') mdl.observations.create(id='obs_5') mdl.observations.create(id='obs_6') mdl.observations.create(id='obs_7') ev_0 = Evidence(observation=mdl.observations[0], type=onto['WC:supporting_evidence']) ev_1 = Evidence(observation=mdl.observations[1], type=onto['WC:supporting_evidence']) ev_2 = Evidence(observation=mdl.observations[2], type=onto['WC:supporting_evidence']) ev_3 = Evidence(observation=mdl.observations[3], type=onto['WC:supporting_evidence']) submdl_0.evidence.extend([ev_0, ev_1]) submdl_1.evidence.extend([ev_0, ev_2, ev_3]) submdl_2.evidence.extend([ev_1, ev_2]) submdl_3.evidence.extend([ev_3]) dfba_obj_2.evidence.append(ev_2) dfba_obj_3.evidence.append(ev_3) mdl.conclusions.create(id='conclusion_0', submodels=[submdl_0, submdl_1], evidence=[ Evidence( observation=mdl.observations[4], type=onto['WC:supporting_evidence']) ]) mdl.conclusions.create(id='conclusion_1', submodels=[submdl_0, submdl_2], evidence=[ Evidence( observation=mdl.observations[5], type=onto['WC:supporting_evidence']) ]) mdl.conclusions.create(id='conclusion_2', submodels=[submdl_1, submdl_2], dfba_objs=[dfba_obj_2], evidence=[ Evidence( observation=mdl.observations[6], type=onto['WC:supporting_evidence']) ]) mdl.conclusions.create(id='conclusion_2', submodels=[submdl_1, submdl_3], dfba_objs=[dfba_obj_3], evidence=[ Evidence( observation=mdl.observations[7], type=onto['WC:supporting_evidence']) ]) mdl.references.create(id='ref_0', submodels=[submdl_0]) mdl.references.create(id='ref_1', submodels=[submdl_1]) mdl.references.create(id='ref_2', submodels=[submdl_2], dfba_objs=[dfba_obj_2]) mdl.references.create(id='ref_2', submodels=[submdl_3], dfba_objs=[dfba_obj_3]) submdl_0.identifiers.create(id='xref_0') submdl_1.identifiers.create(id='xref_1') submdl_2.identifiers.create(id='xref_2', dfba_objs=[dfba_obj_2]) submdl_3.identifiers.create(id='xref_3', dfba_objs=[dfba_obj_3]) """ Merge algorithmically-like submodels """ merged_mdl = mdl.copy() MergeAlgorithmicallyLikeSubmodelsTransform().run(merged_mdl) merged_submdl_ssa = merged_mdl.submodels.get_one( framework=onto['WC:stochastic_simulation_algorithm']) merged_submdl_fba = merged_mdl.submodels.get_one( framework=onto['WC:dynamic_flux_balance_analysis']) """ Test submodels merged corrected """ self.assertEqual(len(merged_mdl.compartments), len(mdl.compartments)) self.assertEqual(len(merged_mdl.species_types), len(mdl.species_types)) self.assertEqual(len(merged_mdl.submodels), 2) self.assertEqual(len(merged_mdl.parameters), len(mdl.parameters)) self.assertEqual(len(merged_mdl.reactions), len(mdl.reactions)) self.assertEqual(len(merged_mdl.dfba_objs), 1) self.assertEqual(len(merged_mdl.dfba_obj_reactions), len(mdl.dfba_obj_reactions)) self.assertEqual(len(merged_mdl.observations), len(mdl.observations)) self.assertEqual(len(merged_mdl.conclusions), len(mdl.conclusions)) self.assertEqual(len(merged_mdl.references), len(mdl.references)) self.assertIn(merged_submdl_ssa.id, [ '{0}_{1}'.format(submdl_0.id, submdl_1.id), '{1}_{0}'.format(submdl_0.id, submdl_1.id), ]) self.assertIn(merged_submdl_fba.id, [ '{0}_{1}'.format(submdl_2.id, submdl_3.id), '{1}_{0}'.format(submdl_2.id, submdl_3.id), ]) self.assertEqual(merged_submdl_ssa.framework, onto['WC:stochastic_simulation_algorithm']) self.assertEqual(merged_submdl_fba.framework, onto['WC:dynamic_flux_balance_analysis']) self.assertEqual( len(merged_submdl_ssa.get_children(kind='submodel', __type=Species)), len( set( submdl_0.get_children(kind='submodel', __type=Species) + submdl_1.get_children(kind='submodel', __type=Species)))) self.assertEqual( len(merged_submdl_fba.get_children(kind='submodel', __type=Species)), len( set( submdl_2.get_children(kind='submodel', __type=Species) + submdl_3.get_children(kind='submodel', __type=Species)))) self.assertEqual( len(merged_submdl_ssa.reactions), len(set(submdl_0.reactions) | set(submdl_1.reactions))) self.assertEqual( len(merged_submdl_fba.reactions), len(set(submdl_2.reactions) | set(submdl_3.reactions))) self.assertEqual(merged_submdl_ssa.dfba_obj, None) self.assertEqual(merged_submdl_fba.dfba_obj.id, merged_submdl_fba.dfba_obj.gen_id()) self.assertEqual(len(merged_submdl_fba.dfba_obj.expression.reactions), len(submdl_2.reactions) + len(submdl_3.reactions)) self.assertEqual( len(merged_submdl_fba.dfba_obj.expression.dfba_obj_reactions), len(submdl_2.dfba_obj_reactions) + len(submdl_3.dfba_obj_reactions)) self.assertEqual(merged_submdl_ssa.dfba_obj_reactions, []) self.assertEqual( len(merged_submdl_fba.dfba_obj_reactions), len(submdl_2.dfba_obj_reactions) + len(submdl_3.dfba_obj_reactions)) self.assertEqual(len(set(submdl_0.evidence) | set(submdl_1.evidence)), len(merged_submdl_ssa.evidence)) self.assertEqual(len(merged_submdl_fba.evidence), len(set(submdl_2.evidence) | set(submdl_3.evidence))) self.assertEqual( len(set(submdl_0.conclusions) | set(submdl_1.conclusions)), len(merged_submdl_ssa.conclusions)) self.assertEqual( len(merged_submdl_fba.conclusions), len(set(submdl_2.conclusions) | set(submdl_3.conclusions))) self.assertEqual( len(set(submdl_0.references) | set(submdl_1.references)), len(merged_submdl_ssa.references)) self.assertEqual( len(merged_submdl_fba.references), len(set(submdl_2.references) | set(submdl_3.references))) self.assertEqual( len(set(submdl_0.identifiers) | set(submdl_1.identifiers)), len(merged_submdl_ssa.identifiers)) self.assertEqual( len(merged_submdl_fba.identifiers), len(set(submdl_2.identifiers) | set(submdl_3.identifiers)))
def test(self): model = Model() c = model.compartments.create(id='c') t0 = model.species_types.create(id='s0', type=onto['WC:metabolite']) t1 = model.species_types.create(id='s1', type=onto['WC:metabolite']) t2 = model.species_types.create(id='s2', type=onto['WC:metabolite']) s0 = model.species.create(id='s0[c]', species_type=t0, compartment=c) s1 = model.species.create(id='s1[c]', species_type=t1, compartment=c) s2 = model.species.create(id='s2[c]', species_type=t2, compartment=c) submodel = model.submodels.create(id='submodel', framework='SSA') r0 = model.reactions.create(id='r0', reversible=True, submodel=submodel) r0.participants.create(species=s0, coefficient=-2) r0.participants.create(species=s1, coefficient=3) r0_f = r0.rate_laws.create(direction=RateLawDirection.forward, model=model) a = model.parameters.create(id='a', value=1., units=unit_registry.parse_units('s^-1')) r0_f.expression, error = RateLawExpression.deserialize( 'a', {Parameter: { 'a': a }}) assert error is None, str(error) r0_b = r0.rate_laws.create( direction=RateLawDirection.backward, expression=RateLawExpression(expression='b'), model=model) b = model.parameters.create(id='b', value=1., units=unit_registry.parse_units('s^-1')) r0_b.expression, error = RateLawExpression.deserialize( 'b', {Parameter: { 'b': b }}) assert error is None, str(error) r0.references.create(id='ref_0', model=model) r0.identifiers.create(namespace='x', id='y') r1 = model.reactions.create(id='r1', reversible=False, submodel=submodel) r1.participants.create(species=s1, coefficient=-3) r1.participants.create(species=s2, coefficient=4) r1_f = r1.rate_laws.create(direction=RateLawDirection.forward, model=model) c = model.parameters.create(id='c', value=1., units=unit_registry.parse_units('s^-1')) r1_f.expression, error = RateLawExpression.deserialize( 'c', {Parameter: { 'c': c }}) assert error is None, str(error) r1_b = r1.rate_laws.create(direction=RateLawDirection.backward, model=model) d = model.parameters.create(id='d', value=1., units=unit_registry.parse_units('s^-1')) r1_b.expression, error = RateLawExpression.deserialize( 'd', {Parameter: { 'd': d }}) assert error is None, str(error) r1.references.create(id='ref_1', model=model) r1.identifiers.create(namespace='xx', id='yy') model2 = model.copy() submodel2 = model2.submodels.get_one(id='submodel') r0 = submodel2.reactions.get_one(id='r0') SplitReversibleReactionsTransform().run(model2) self.assertEqual(set([x.id for x in submodel2.reactions]), set(['r0_forward', 'r0_backward', 'r1'])) r0_f = submodel2.reactions.get_one(id='r0_forward') r0_b = submodel2.reactions.get_one(id='r0_backward') r1_2 = submodel2.reactions.get_one(id='r1') attr = Reaction.Meta.attributes['participants'] self.assertEqual(attr.serialize(r0_f.participants), '[c]: (2) s0 ==> (3) s1') self.assertEqual(attr.serialize(r0_b.participants), '[c]: (3) s1 ==> (2) s0') self.assertEqual(attr.serialize(r1_2.participants), '[c]: (3) s1 ==> (4) s2') self.assertEqual(len(r0_f.rate_laws), 1) self.assertEqual(len(r0_b.rate_laws), 1) self.assertEqual(r0_f.rate_laws[0].direction, RateLawDirection.forward) self.assertEqual(r0_b.rate_laws[0].direction, RateLawDirection.forward) self.assertEqual(r0_f.rate_laws[0].expression.expression, 'a') self.assertEqual(r0_b.rate_laws[0].expression.expression, 'b') self.assertEqual(r0_f.rate_laws[0].id, r0_f.rate_laws[0].gen_id()) self.assertEqual(r0_b.rate_laws[0].id, r0_b.rate_laws[0].gen_id()) self.assertEqual(r0_f.rate_laws[0].id, 'r0_forward-forward') self.assertEqual(r0_b.rate_laws[0].id, 'r0_backward-forward') self.assertEqual( r1_2.rate_laws.get_one( direction=RateLawDirection.forward).expression.expression, 'c') self.assertEqual( r1_2.rate_laws.get_one( direction=RateLawDirection.backward).expression.expression, 'd') self.assertEqual(set([x.id for x in r0_f.references]), set(['ref_0'])) self.assertEqual(set([x.id for x in r0_b.references]), set(['ref_0'])) self.assertEqual(set([x.id for x in r1_2.references]), set(['ref_1'])) self.assertEqual(set([x.id for x in r0_f.identifiers]), set(['y'])) self.assertEqual(set([x.id for x in r0_b.identifiers]), set(['y'])) self.assertEqual(set([x.id for x in r1_2.identifiers]), set(['yy'])) self.assertEqual(r0.submodel, None) self.assertEqual(r0.participants, []) self.assertEqual(r0.rate_laws, []) self.assertEqual(r0.references, []) self.assertEqual(r0.identifiers, []) for attr_name, attr in chain(Reaction.Meta.attributes.items(), Reaction.Meta.related_attributes.items()): if isinstance(attr, RelatedAttribute): val = getattr(r0, attr_name) self.assertTrue(val is None or (isinstance(val, list) and len(val) == 0))