Ejemplo n.º 1
0
def get_initial_system(model, res_dir, r_id2rev, tree_efm_path, max_efm_number, threshold, rewrite=True,
                       pw2rs=None):
    m_id2i, r_id2i = get_element2id_mapping(model)
    boundary_m_ids = [m.getId() for m in model.getListOfSpecies() if m.getBoundaryCondition()]
    N = model2stoichiometric_matrix(model, m_id2i, r_id2i)
    logging.info('Calculated N of shape %s' % 'x'.join((str(it) for it in N.shape)))
    r_id2coefficient_list = compute_efms(model, res_dir, max_efm_number, r_id2rev=r_id2rev,
                                         tree_efm_path=tree_efm_path, threshold=threshold, rewrite=rewrite)

    efm_id2pws = {'efm_%d' % i: r_ids2pws(set(r_id2coeff.keys()), pw2rs)
                  for i, r_id2coeff in enumerate(r_id2coefficient_list)}
    V = get_efm_matrix(r_id2coefficient_list, r_id2i)
    # inner_m_ids = [m.getId() for m in model.getListOfSpecies() if not m.getBoundaryCondition()]
    # rev = [model.getReaction(r_id).getReversible() for (r_id, i) in
    #        sorted(r_id2i.items(), key=lambda (_, i): i)]
    # N_in = N[[m_id2i[m_id] for m_id in inner_m_ids], :]
    # V = sampler(N_in, rev, K=None, debug=True)
    # V = V[:, [i for i in range(0, V.shape[1])
    #           if V[r_id2i[out_r_id], i] * (-1 if out_rev else 1) > 0 and
    #           next((False for (in_r_id, in_rev) in in_r_id2rev.items() if
    #                 V[r_id2i[in_r_id], i] * (-1 if in_rev else 1) <= 0), True)]]
    # V = remove_invalid_efms(N[tuple(m_id2i[m_id] for m_id in inner_m_ids), :], V)

    logging.info('Calculated V of shape %s' % 'x'.join((str(it) for it in V.shape)))
    efm_id2i = {'efm_%d' % i: i for i in range(0, V.shape[1])}
    S = System(N=N, V=V, m_id2i=m_id2i, r_id2i=r_id2i, efm_id2i=efm_id2i, boundary_m_ids=boundary_m_ids)
    return S.get_used_system(), efm_id2pws
Ejemplo n.º 2
0
 def test_boundary_metabolites(self):
     N = model2stoichiometric_matrix(self.model, self.s_id2i, self.r_id2i)
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 10, 'r3': 10, 'r4': 10, 'r6': 10}], self.r_id2i)
     m = get_boundary_metabolites(N, V[:, 0])
     result = {(m[self.s_id2i[m_id]], m_id) for m_id in self.s_id2i.keys() if m[self.s_id2i[m_id]]}
     self.assertSetEqual({(-1, 'm1_b'), (1, 'm2_b')},
                         result,
                         'Was supposed to get -1 m1_b 1 m2_b as boundary metabolites, got %s' % result)
Ejemplo n.º 3
0
 def test_lump_coupled_reactions_NV(self):
     N = model2stoichiometric_matrix(self.model, self.s_id2i, self.r_id2i)
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 1, 'r3': 1, 'r4': 1, 'r6': 1}], self.r_id2i)
     NV = np.dot(N, V)
     coupled_r_id_groups = get_coupled_reactions(V, self.r_id2i)
     N_new, V_new, new_r_id2i, r_id2lr_id, _ = lump_coupled_reactions(N, V, coupled_r_id_groups, self.r_id2i)
     NV_new = np.dot(N_new, V_new)
     self.assertListEqual(list(NV.flatten()), list(NV_new.flatten()), 'NV should not have changed')
Ejemplo n.º 4
0
 def test_lump_coupled_reactions_N_2(self):
     N = model2stoichiometric_matrix(self.model, self.s_id2i, self.r_id2i)
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 1, 'r3': 1, 'r4': 1, 'r6': 1}], self.r_id2i)
     coupled_r_id_groups = get_coupled_reactions(V, self.r_id2i)
     N_new, V_new, new_r_id2i, r_id2lr_id, _ = lump_coupled_reactions(N, V, coupled_r_id_groups, self.r_id2i)
     lr_id = next(r_id2lr_id.values())
     st = N_new[self.s_id2i['m2_b'], new_r_id2i[lr_id]]
     self.assertEqual(1, st, 'The lumped reaction was supposed to produce 1 m2_b, consumes %g instead' % -st)
Ejemplo n.º 5
0
 def test_lump_coupled_reactions_V(self):
     N = model2stoichiometric_matrix(self.model, self.s_id2i, self.r_id2i)
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 1, 'r3': 1, 'r4': 1, 'r6': 1}], self.r_id2i)
     coupled_r_id_groups = get_coupled_reactions(V, self.r_id2i)
     N_new, V_new, new_r_id2i, r_id2lr_id, _ = lump_coupled_reactions(N, V, coupled_r_id_groups, self.r_id2i)
     lr_id = next(r_id2lr_id.values())
     r_i = V_new[new_r_id2i[lr_id], :]
     self.assertListEqual([10, 1], list(r_i),
                          'The lumped reaction was supposed to participate in EFMs with coefficients [10, 1] not %s'
                          % r_i)
Ejemplo n.º 6
0
 def test_coupled_reactions_content(self):
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 10, 'r3': 10, 'r4': 10, 'r6': 10}], self.r_id2i)
     coupled_r_id_groups = get_coupled_reactions(V, self.r_id2i)
     self.assertIn(['r1', 'r3'], coupled_r_id_groups, 'Was supposed to find [r1, r3] coupled reaction group.')
Ejemplo n.º 7
0
 def test_coupled_reactions_len(self):
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 10, 'r3': 10, 'r4': 10, 'r6': 10}], self.r_id2i)
     c_r_id_lists = get_coupled_reactions(V, self.r_id2i)
     self.assertEqual(1, len(c_r_id_lists),
                      'Was supposed to find one coupled reaction group, got %g instead.' % len(c_r_id_lists))
Ejemplo n.º 8
0
 def test_yield_no_out_metabolite(self):
     N = model2stoichiometric_matrix(self.model, self.s_id2i, self.r_id2i)
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 10, 'r3': 10, 'r4': 10, 'r6': 10}], self.r_id2i)
     y = get_yield(N, V[:, 0], out_m_index=self.s_id2i['m3_b'], in_m_index=self.s_id2i['m1_b'])
     self.assertEqual(0, y,
                      'Yield of m3_b with respect to m1_b in EFM 0 was supposed to be 0, got %g instead.' % y)
Ejemplo n.º 9
0
 def test_control_efficiency(self):
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 10, 'r3': 10, 'r4': 10, 'r6': 10}], self.r_id2i)
     ce = get_control_efficiency(V[:, 0], r_index=self.r_id2i['r3'])
     self.assertEqual(1/3.0, ce,
                      'Control efficiency of r3 in EFM 0 was supposed to be 1/3, got %g instead.' % ce)
Ejemplo n.º 10
0
 def test_efm_conversion_absent_r_coefficient(self):
     V = get_efm_matrix([{'r1': 10, 'r2': 10, 'r3': 10}, {'r1': 10, 'r3': 10, 'r4': 10, 'r6': 10}], self.r_id2i)
     self.assertEqual(0, V[self.r_id2i['r4'], 0],
                      'V[r4, efm0] was supposed to be 0, got %g instead.' % V[self.r_id2i['r1'], 0])