def add_context(self, other, atomic_signature: dict, structure_signature: dict) -> set: """ Fills missing context for given agent. Note: it is assumed this method is used only for well formed rules, which means given structure agents have specified the same atomic agents and miss the same atomic agents. Moreover, agents which are not specified are completely omitted. TBD: this could be somehow hacked. :param other: possibly a structure agent, -1 if context is empty on left, 1 for right :param atomic_signature: given mapping of atomic name to possible states :param structure_signature: given mapping of structure name to possible atomics :return: set of pairs """ present_atomics = set(map(lambda a: a.name, self.composition)) if type(self) == type(other): if structure_signature[self.name] - present_atomics != set(): result = [] for atomic_name in structure_signature[self.name] - present_atomics: possibilities = AtomicAgent(atomic_name, "_").add_context(AtomicAgent(atomic_name, "_"), atomic_signature, structure_signature) result.append(possibilities) agents = set() for options in itertools.product(*result): new_agent_self = StructureAgent(self.name, set(self.composition)) new_agent_other = StructureAgent(other.name, set(other.composition)) for (left, right) in options: new_agent_self.composition.add(left) new_agent_other.composition.add(right) agents.add((new_agent_self, new_agent_other)) return agents else: return {(self, other)} if structure_signature[self.name] - present_atomics != set(): result = [] for atomic_name in structure_signature[self.name] - present_atomics: possibilities = AtomicAgent(atomic_name, "_").add_context(1, atomic_signature, structure_signature) result.append(possibilities) agents = set() for options in itertools.product(*result): new_agent = StructureAgent(self.name, set(self.composition)) for (left, right) in options: new_agent.composition.add(left) if other == -1: agents.add((None, new_agent)) else: agents.add((new_agent, None)) return agents else: return {(None, self)} if other == -1 else {(self, None)}
def setUp(self): self.a1 = AtomicAgent("T", "s") self.a2 = AtomicAgent("S", "i") self.a3 = AtomicAgent("U", "a") self.a4 = AtomicAgent("T", "_") self.a5 = AtomicAgent("U", "_") self.a6 = AtomicAgent("S", "_") self.s1 = StructureAgent("X", {self.a1}) self.s2 = StructureAgent("A", {self.a2, self.a3}) self.s3 = StructureAgent("X", {self.a4}) self.s4 = StructureAgent("A", {self.a2, self.a5}) self.s5 = StructureAgent("A", {self.a6, self.a3}) self.s6 = StructureAgent("A", set()) self.c1 = Complex([self.s1, self.s2, self.s2], "cyt") self.c2 = Complex([self.s3, self.s4, self.s5], "cyt") self.c3 = Complex([self.s2, self.s2, self.s1], "cyt") self.c4 = Complex([self.s2, self.s2, self.s1], "cell") self.c5 = Complex([self.s2, self.s4, self.a1], "cell") self.c6 = Complex([self.s6, self.s6, self.a4], "cell") self.large_c1 = Complex([self.s4] * 6 + [self.s3] * 5, "cell") self.large_c2 = Complex([self.s5] * 7 + [self.s3] * 6, "cell")
def setUp(self): # agents self.a1 = AtomicAgent("T", "i") self.a2 = AtomicAgent("S", "i") self.a3 = AtomicAgent("T", "a") self.a4 = AtomicAgent("S", "a") self.s1 = StructureAgent("X", {self.a4}) self.s2 = StructureAgent("X", {self.a2}) self.s3 = StructureAgent("K", {self.a3}) self.s4 = StructureAgent("K", {self.a1}) self.s5 = StructureAgent("X", set()) self.s6 = StructureAgent("K", set()) self.c1 = Complex([self.s6], "cyt") # K()::cyt self.c2 = Complex([self.s3], "cyt") # K(T{a})::cyt self.c3 = Complex([self.s4], "cyt") # K(T{i})::cyt self.c4 = Complex([self.s4, self.s1], "cyt") # K(T{i}).X(S{a})::cyt self.c5 = Complex([self.s4, self.s2], "cyt") # K(T{i}).X(S{i})::cyt self.c6 = Complex([self.s3, self.s1], "cyt") # K(T{a}).X(S{a})::cyt self.c7 = Complex([self.s3, self.s2], "cyt") # K(T{a}).X(S{i})::cyt # rates self.parser = Parser("rate") rate_expr = "3.0*[K()::cyt]/2.0*v_1" self.rate_1 = Core.Rate.Rate(self.parser.parse(rate_expr).data) rate_expr = "3.0*[K(T{i}).X()::cyt] + [K()::cyt]" self.rate_2 = Core.Rate.Rate(self.parser.parse(rate_expr).data) # states self.state_1 = State(np.array([2, 3])) self.state_2 = State(np.array([2, 0, 3, 1, 6, 2]))
class TestAtomic(unittest.TestCase): def setUp(self): self.a1 = AtomicAgent("T", "s") self.a2 = AtomicAgent("S", "a") self.a3 = AtomicAgent("T", "s") self.a4 = AtomicAgent("T", "_") self.a5 = AtomicAgent("T", "_") self.a6 = AtomicAgent("T", "p") self.a7 = AtomicAgent("T", "u") def test_eq(self): self.assertEqual(self.a1, self.a3) self.assertNotEqual(self.a1, self.a2) self.assertNotEqual(self.a1, self.a4) def test_print(self): self.assertEqual(str(self.a1), "T{s}") self.assertEqual(str(self.a4), "T{_}") def test_compatibility(self): self.assertTrue(self.a4.compatible(self.a1)) self.assertFalse(self.a2.compatible(self.a1)) self.assertFalse(self.a1.compatible(self.a4)) def test_add_context(self): atomic_signature = {"T": {"u", "p"}} structure_signature = dict() self.assertEqual(self.a4.add_context(self.a5, atomic_signature, structure_signature), {(self.a6, self.a6), (self.a7, self.a7)}) self.assertEqual(self.a6.add_context(self.a6, atomic_signature, structure_signature), {(self.a6, self.a6)}) self.assertEqual(self.a4.add_context(-1, atomic_signature, structure_signature), {(None, self.a6), (None, self.a7)}) def test_reduce_context(self): self.assertEqual(self.a3.reduce_context(), self.a4)
def setUp(self): self.a1 = AtomicAgent("T", "s") self.a2 = AtomicAgent("S", "a") self.a3 = AtomicAgent("T", "s") self.a4 = AtomicAgent("T", "_") self.a5 = AtomicAgent("T", "p") self.a6 = AtomicAgent("S", "i") self.s1 = StructureAgent("strA", {self.a1, self.a2}) self.s2 = StructureAgent("strA", {self.a2, self.a4}) self.s3 = StructureAgent("strA", {self.a4}) self.s4 = StructureAgent("strD", set()) self.s5 = StructureAgent("strA", {self.a2, self.a1}) # context self.s6 = StructureAgent("strA", {self.a3}) self.s7 = StructureAgent("strA", {self.a5}) self.s8 = StructureAgent("strA", {self.a3, self.a2}) self.s9 = StructureAgent("strA", {self.a5, self.a2}) self.s10 = StructureAgent("strA", {self.a3, self.a6}) self.s11 = StructureAgent("strA", {self.a5, self.a6}) self.s12 = StructureAgent("strA", set())
def setUp(self): self.s1 = StructureAgent("X", set()) self.s2 = StructureAgent("Y", set()) self.s3 = StructureAgent("Z", set()) self.c1 = Complex([self.s1], "rep") self.c2 = Complex([self.s2], "rep") self.c3 = Complex([self.s3], "rep") ordering = (self.c1, self.c2, self.c3) params = {"k1": 0.05, "k2": 0.1} self.rate_parser = Parser("rate") rate_expr = "1/(1+([X()::rep])**2)" rate_1 = Rate(self.rate_parser.parse(rate_expr).data) rate_1.vectorize(ordering, params) rate_expr = "k1*[X()::rep]" rate_2 = Rate(self.rate_parser.parse(rate_expr).data) rate_2.vectorize(ordering, params) rate_expr = "k2*[Z()::rep]" rate_3 = Rate(self.rate_parser.parse(rate_expr).data) rate_3.vectorize(ordering, params) init = State(np.array([2.0, 1.0, 1.0])) vector_reactions = { VectorReaction(State(np.array([0.0, 0.0, 0.0])), State(np.array([0.0, 1.0, 0.0])), rate_1), VectorReaction(State(np.array([1.0, 0.0, 0.0])), State(np.array([0.0, 0.0, 0.0])), rate_2), VectorReaction(State(np.array([0.0, 0.0, 1.0])), State(np.array([1.0, 0.0, 0.0])), None) } self.vm_1 = VectorModel(vector_reactions, init, ordering, None) vector_reactions = { VectorReaction(State(np.array([0.0, 0.0, 0.0])), State(np.array([0.0, 1.0, 0.0])), rate_1), VectorReaction(State(np.array([1.0, 0.0, 0.0])), State(np.array([0.0, 0.0, 0.0])), rate_2), VectorReaction(State(np.array([0.0, 0.0, 1.0])), State(np.array([1.0, 0.0, 0.0])), rate_3) } self.vm_2 = VectorModel(vector_reactions, init, ordering, None) # test abstract model self.model_parser = Parser("model") self.model_abstract = \ """#! rules => X()::rep @ k2*[T{_}::rep] T{a}::rep => T{i}::rep @ k1*[T{_}::rep] #! inits 10 T{a}::rep #! definitions k1 = 0.05 k2 = 0.12 """ # test transition system generating a1 = AtomicAgent("B", "a") a2 = AtomicAgent("S", "u") a3 = AtomicAgent("S", "p") a4 = AtomicAgent("T", "i") s1 = StructureAgent("K", {a3, a4}) s2 = StructureAgent("K", {a2, a4}) cx1 = Complex([a1], "cyt") cx2 = Complex([s1], "cyt") cx3 = Complex([s2], "cyt") cx4 = Complex([s1, a1], "cyt") cx5 = Complex([s2, a1], "cyt") ordering = (cx5, cx4, cx3, cx2, cx1) # (K(S{u},T{i}).B{a}::cyt, K(S{p},T{i}).B{a}::cyt, K(S{u},T{i})::cyt, K(S{p},T{i})::cyt, B{a}::cyt) self.model_TS = \ """#! rules => K(S{u},T{i})::cyt @ omega K(S{u})::cyt => K(S{p})::cyt @ alpha*[K(S{u})::cyt] K(S{p})::cyt + B{a}::cyt => K(S{p}).B{a}::cyt @ beta*[K(S{p})::cyt]*[B{a}::cyt] B{_}::cyt => @ gamma*[B{_}::cyt] K(S{u},T{i}).B{a}::cyt => @ 5 #! inits 1 B{a}::cyt #! definitions alpha = 10 beta = 5 gamma = 2 omega = 3 """ alpha = 10 beta = 5 gamma = 2 omega = 3 self.test_ts = TransitionSystem(ordering) states = [ State(np.array((0.0, 0.0, 0.0, 0.0, 1.0))), State(np.array((0.0, 0.0, 0.0, 0.0, 0.0))), State(np.array((0.0, 0.0, 1.0, 0.0, 0.0))), State(np.array((0.0, 0.0, 1.0, 0.0, 1.0))), State(np.array((np.inf, np.inf, np.inf, np.inf, np.inf))), State(np.array((0.0, 0.0, 0.0, 1.0, 1.0))), State(np.array((0.0, 0.0, 1.0, 1.0, 1.0))), State(np.array((0.0, 1.0, 0.0, 0.0, 0.0))), State(np.array((0.0, 0.0, 0.0, 1.0, 0.0))), State(np.array((0.0, 0.0, 1.0, 1.0, 0.0))), State(np.array((0.0, 1.0, 1.0, 0.0, 0.0))), State(np.array((0.0, 1.0, 0.0, 1.0, 0.0))), State(np.array((0.0, 1.0, 1.0, 1.0, 0.0))) ] # in edges we have probabilities, not rates, so we must normalise go = gamma + omega # 5 goa = gamma + omega + alpha # 15 goab = gamma + omega + alpha + beta # 20 gob = gamma + omega + beta # 10 oa = omega + alpha # 13 self.test_ts.processed = set(states) self.test_ts.edges = { Edge(states[0], states[1], gamma / go), Edge(states[0], states[3], omega / go), Edge(states[1], states[2], omega / omega), Edge(states[2], states[4], omega / oa), Edge(states[2], states[8], alpha / oa), Edge(states[3], states[2], gamma / goa), Edge(states[3], states[4], omega / goa), Edge(states[3], states[5], alpha / goa), Edge(states[4], states[4], 1), Edge(states[5], states[6], omega / gob), Edge(states[5], states[7], beta / gob), Edge(states[5], states[8], gamma / gob), Edge(states[6], states[4], oa / goab), Edge(states[6], states[9], gamma / goab), Edge(states[6], states[10], beta / goab), Edge(states[7], states[10], omega / omega), Edge(states[8], states[9], gamma / gamma), Edge(states[9], states[4], 1), Edge(states[10], states[4], omega / oa), Edge(states[10], states[11], alpha / oa), Edge(states[11], states[12], omega / omega), Edge(states[12], states[4], 1) } self.test_ts.encode(states[0]) # bigger TS self.model_bigger_TS = \ """#! rules => 2 K(S{u},T{i})::cyt @ omega K(S{u})::cyt => K(S{p})::cyt @ alpha*[K(S{u})::cyt] K(S{p})::cyt + B{a}::cyt => K(S{p}).B{a}::cyt @ beta*[K(S{p})::cyt]*[B{a}::cyt] B{_}::cyt => @ gamma*[B{_}::cyt] K(S{u},T{i}).B{a}::cyt => @ 5 #! inits 6 B{a}::cyt #! definitions alpha = 10 beta = 5 gamma = 2 omega = 3 """ # even bigger TS self.model_even_bigger_TS = \ """#! rules => K(S{u},T{i})::cyt @ omega K(S{u})::cyt => K(S{p})::cyt @ alpha*[K(S{u})::cyt] K(S{p})::cyt + B{a}::cyt => K(S{p}).B{a}::cyt @ beta*[K(S{p})::cyt]*[B{a}::cyt] B{_}::cyt => @ gamma*[B{_}::cyt] K(S{u},T{i}).B{a}::cyt => @ 5 #! inits 10 B{a}::cyt #! definitions alpha = 10 beta = 5 gamma = 2 omega = 3 """ self.model_parametrised = \ """#! rules => K(S{u},T{i})::cyt @ omega K(S{u})::cyt => K(S{p})::cyt @ alpha*[K(S{u})::cyt] K(S{p})::cyt + B{a}::cyt => K(S{p}).B{a}::cyt @ beta*[K(S{p})::cyt]*[B{a}::cyt] B{_}::cyt => @ gamma*[B{_}::cyt] K(S{u},T{i}).B{a}::cyt => @ 5 #! inits 1 B{a}::cyt #! definitions alpha = 10 beta = 5 //gamma = 2 omega = 3 """ self.model_with_sinks = \ """#! rules
def atomic(self, matches): name, state = str(matches[0].children[0]), matches[1] return AtomicAgent(name, state)
def setUp(self): self.a1 = AtomicAgent("T", "s") self.a2 = AtomicAgent("S", "a") self.a3 = AtomicAgent("T", "s") self.a4 = AtomicAgent("T", "_") self.a5 = AtomicAgent("T", "_") self.a6 = AtomicAgent("T", "p") self.a7 = AtomicAgent("T", "u")
def setUp(self): self.a1 = AtomicAgent("S", "u") self.a2 = AtomicAgent("S", "p") self.a3 = AtomicAgent("B", "_") self.a4 = AtomicAgent("B", "-") self.a5 = AtomicAgent("B", "+") self.s1 = StructureAgent("K", {self.a1}) self.s2 = StructureAgent("B", set()) self.s3 = StructureAgent("K", {self.a2}) self.s4 = StructureAgent("B", set()) self.s5 = StructureAgent("D", {self.a3}) self.s6 = StructureAgent("K", {self.a4}) self.s7 = StructureAgent("K", {self.a5}) self.c1 = Complex([self.s1, self.s2], "cyt") self.c2 = Complex([self.s3], "cyt") self.c3 = Complex([self.s2], "cyt") self.c4 = Complex([self.s5], "cell") # rules sequence_1 = (self.s1, self.s2, self.s3, self.s4) mid_1 = 2 compartments_1 = ["cyt"] * 4 complexes_1 = [(0, 1), (2, 2), (3, 3)] pairs_1 = [(0, 2), (1, 3)] rate_1 = Rate("3.0*[K()::cyt]/2.0*v_1") self.r1 = Rule(sequence_1, mid_1, compartments_1, complexes_1, pairs_1, rate_1) sequence_2 = (self.s1, self.s2, self.s3, self.s4, self.s5) mid_2 = 2 compartments_2 = ["cyt"] * 4 + ["cell"] complexes_2 = [(0, 1), (2, 2), (3, 3), (4, 4)] pairs_2 = [(0, 2), (1, 3), (None, 4)] rate_2 = Rate("3.0*[K()::cyt]/2.0*v_1") self.r2 = Rule(sequence_2, mid_2, compartments_2, complexes_2, pairs_2, rate_2) sequence_3 = (self.s6, self.s2, self.s5, self.s7, self.s4) mid_3 = 3 compartments_3 = ["cyt"] * 2 + ["cell"] + ["cyt"] * 2 complexes_3 = [(0, 1), (2, 2), (3, 3), (4, 4)] pairs_3 = [(0, 3), (1, 4), (2, None)] rate_3 = Rate("3.0*[K(T{3+})::cyt]/2.0*v_1") self.r3 = Rule(sequence_3, mid_3, compartments_3, complexes_3, pairs_3, rate_3) # special cases self.s1_s = StructureAgent("X", set()) self.s2_s = StructureAgent("Y", set()) self.s3_s = StructureAgent("Z", set()) sequence_4 = (self.s1_s, ) mid_4 = 1 compartments_4 = ["rep"] complexes_4 = [(0, 0)] pairs_4 = [(0, None)] rate_4 = Rate("k1*[X()::rep]") self.r4 = Rule(sequence_4, mid_4, compartments_4, complexes_4, pairs_4, rate_4) sequence_5 = (self.s2_s, ) mid_5 = 0 compartments_5 = ["rep"] complexes_5 = [(0, 0)] pairs_5 = [(None, 0)] rate_5 = Rate("1.0/(1.0+([X()::rep])**4.0)") self.r5 = Rule(sequence_5, mid_5, compartments_5, complexes_5, pairs_5, rate_5) # reactions lhs = Side([self.c1]) rhs = Side([self.c2, self.c3, self.c4]) self.reaction1 = Reaction(lhs, rhs, rate_2) # create self.t_i = AtomicAgent("T", "i") self.t_a = AtomicAgent("T", "a") self.a4_p = AtomicAgent("C", "p") self.a4_u = AtomicAgent("C", "u") self.u2_c1_p = AtomicAgent("U", "p") self.u2_c1_u = AtomicAgent("U", "u") self.s6 = StructureAgent("D", set()) self.s6_c1_p = StructureAgent("D", {self.a4_p}) self.s6_c1_u = StructureAgent("D", {self.a4_u}) self.s2_c1_p = StructureAgent("B", {self.u2_c1_p}) self.s2_c1_u = StructureAgent("B", {self.u2_c1_u}) self.s1_c1_a = StructureAgent("K", {self.a1, self.t_a}) self.s1_c1_i = StructureAgent("K", {self.a1, self.t_i}) self.s3_c1_a = StructureAgent("K", {self.a2, self.t_a}) self.s3_c1_i = StructureAgent("K", {self.a2, self.t_i}) sequence_c1 = (self.s1, self.s2, self.s3, self.s4, self.s6) mid_c1 = 2 compartments_c1 = ["cyt"] * 5 complexes_c1 = [(0, 0), (1, 1), (2, 3), (4, 4)] pairs_c1 = [(0, 2), (1, 3), (None, 4)] rate_c1 = Rate("3*[K()::cyt]/2*v_1") self.c1_c1 = Complex([self.s2_c1_u], "cyt") # B(U{u})::cyt self.c1_c2 = Complex([self.s2_c1_p], "cyt") # B(U{p})::cyt self.c1_c3 = Complex([self.s1_c1_a], "cyt") # K(S{u},T{a})::cyt self.c1_c4 = Complex([self.s1_c1_i], "cyt") # K(S{u},T{i})::cyt self.c1_c5 = Complex([self.s3_c1_a, self.s2_c1_u], "cyt") # K(S{p},T{a}).B(U{u})::c self.c1_c6 = Complex([self.s3_c1_i, self.s2_c1_u], "cyt") # K(S{p},T{i}).B(U{u})::c self.c1_c7 = Complex([self.s3_c1_i, self.s2_c1_p], "cyt") # K(S{p},T{i}).B(U{p})::c self.c1_c8 = Complex([self.s3_c1_a, self.s2_c1_p], "cyt") # K(S{p},T{a}).B(U{p})::c self.c1_c9 = Complex([self.s6_c1_p], "cyt") # D(C{p})::cyt self.c1_c10 = Complex([self.s6_c1_u], "cyt") # D(C{u})::cyt self.rule_c1 = Rule(sequence_c1, mid_c1, compartments_c1, complexes_c1, pairs_c1, rate_c1) self.reaction_c1_1 = Reaction(Side([self.c1_c1, self.c1_c3]), Side([self.c1_c5, self.c1_c9]), rate_c1) self.reaction_c1_2 = Reaction(Side([self.c1_c1, self.c1_c3]), Side([self.c1_c5, self.c1_c10]), rate_c1) self.reaction_c1_3 = Reaction(Side([self.c1_c2, self.c1_c4]), Side([self.c1_c7, self.c1_c10]), rate_c1) self.reaction_c1_4 = Reaction(Side([self.c1_c1, self.c1_c4]), Side([self.c1_c6, self.c1_c9]), rate_c1) self.reaction_c1_5 = Reaction(Side([self.c1_c2, self.c1_c3]), Side([self.c1_c8, self.c1_c9]), rate_c1) self.reaction_c1_6 = Reaction(Side([self.c1_c2, self.c1_c3]), Side([self.c1_c8, self.c1_c10]), rate_c1) self.reaction_c1_7 = Reaction(Side([self.c1_c1, self.c1_c4]), Side([self.c1_c6, self.c1_c10]), rate_c1) self.reaction_c1_8 = Reaction(Side([self.c1_c2, self.c1_c4]), Side([self.c1_c7, self.c1_c9]), rate_c1) self.reactions_c1 = { self.reaction_c1_1, self.reaction_c1_2, self.reaction_c1_3, self.reaction_c1_4, self.reaction_c1_5, self.reaction_c1_6, self.reaction_c1_7, self.reaction_c1_8 } # context no change sequence_no_change = (self.s1_c1_a, self.s2_c1_u, self.s3_c1_a, self.s2_c1_u, self.s6_c1_p) self.rule_no_change = Rule(sequence_no_change, mid_c1, compartments_c1, complexes_c1, pairs_c1, rate_c1) # parsing self.parser = Parser("rule") self.rule_no_rate = Rule(sequence_1, mid_1, compartments_1, complexes_1, pairs_1, None)