<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Genome-scale metabolic reconstruction
Step1:  The stoichiometric matrix S
Step2:  Steady-state mass balances
Step3:  The stoichiometry matrix S has the following dimensions.
Step4:  Gene-Protein-Reaction Rules (GPRs)
Step5:  Exercises
This is how the the stoichiometry matrix S looks like when visualized as a matrix plot.
Step6:  What do you think the first diagonal blocks means?
What do you think the long horizontal lines mean?
Solution
Solution 1
The diagonal pattern from column 0 to 330 corresponds to exchange reactions.
Step7:  Solution 2
The horizontal lines correspond to metabolites (mostly co-factors) that appear in many reactions.
Step8:  These metabolites are the reason metabolic network look like giant hairballs.
<ASSISTANT_TASK:>
Python Code:
from sympy import Matrix, Eq, var
from cameo import models
model = models.bigg.iJO1366
len(model.reactions)
len(model.metabolites)
len(model.genes)
model.medium
print(model.objective)
Explanation: Genome-scale metabolic reconstruction
End of explanation
S = model.to_array_based_model().S.todense()
S
Explanation: The stoichiometric matrix S
End of explanation
S = Matrix()
print(S)
v = Matrix([var(reaction.id) for reaction in model.reactions])
print(v)
Explanation: Steady-state mass balances: $\mathbf{S} \mathbf{v} = \mathbf{0}$
End of explanation
S.shape
v.shape
S.dot(v)
Explanation: The stoichiometry matrix S has the following dimensions.
End of explanation
import pandas
pandas.DataFrame([[reaction.id, reaction.gene_name_reaction_rule] for reaction in model.reactions], columns=['reaction', 'GPR'])
Explanation: Gene-Protein-Reaction Rules (GPRs)
End of explanation
import matplotlib.pyplot as plt
plt.spy(S, precision=0.01, markersize=.1)
Explanation: Exercises
This is how the the stoichiometry matrix S looks like when visualized as a matrix plot.
End of explanation
len(model.exchanges)
model.reactions[0]
Explanation: What do you think the first diagonal blocks means?
What do you think the long horizontal lines mean?
Solution
Solution 1
The diagonal pattern from column 0 to 330 corresponds to exchange reactions.
End of explanation
import networkx as nx
def reactions_to_network(reactions):
    edges = list()
    for reaction in reactions:
        substrates = list(reaction.reactants)
        for substrate in substrates:
            products = list(reaction.products)
            for product in products:
                if reaction.reversibility:
                    edges.append((product, substrate, dict(reaction=reaction)))
                    edges.append((substrate, product, dict(reaction=reaction)))
                elif reaction.lower_bound > 0:
                    edges.append((substrate, product, dict(reaction=reaction)))
                else:
                    edges.append((product, substrate, dict(reaction=reaction)))
    multi_graph = nx.MultiDiGraph(edges)
    return multi_graph
network = reactions_to_network(model.reactions)
Explanation: Solution 2
The horizontal lines correspond to metabolites (mostly co-factors) that appear in many reactions.
End of explanation
nx.draw_spring(network)
Explanation: These metabolites are the reason metabolic network look like giant hairballs.
End of explanation
<END_TASK>