reaction_graphs.pl 6.33 KB
 MARTINEZ Thierry committed Oct 19, 2015 1 2 3 :- module( reaction_graphs, [  MARTINEZ Thierry committed Mar 18, 2016 4  % Commands  MARTINEZ Thierry committed Oct 19, 2015 5  reaction_graph/0,  MARTINEZ Thierry committed Oct 20, 2015 6  import_reactions_from_graph/0,  MARTINEZ Thierry committed Mar 18, 2016 7 8 9  draw_reactions/0, % Public API reaction_graph/1  MARTINEZ Thierry committed Oct 19, 2015 10 11 12  ] ).  MARTINEZ Thierry committed Oct 20, 2015 13   MARTINEZ Thierry committed Oct 19, 2015 14 15 :- devdoc('\\section{Commands}').  FAGES Francois committed Aug 08, 2016 16 17 18 19 20 :- devcom('FF: It would be nice to have a notion of input/output and draw graphs accordingly, with inputs on top and outputs bottom in all (reaction or influence) graphs. The input species could be those present at initial_state. The output species could be those not reactant (nor catalyst) of any reaction, nor source of any nfluence. Alternatively we could have input/output annotations on molecular species. It could be useful also for some analyses.').  MARTINEZ Thierry committed Oct 20, 2015 21   MARTINEZ Thierry committed Oct 19, 2015 22 23 reaction_graph :- biocham_command,  MARTINEZ Thierry committed Oct 20, 2015 24  doc('Builds the reaction graph of the current model.'),  MARTINEZ Thierry committed Oct 23, 2015 25  delete_items([kind: graph, key: reaction_graph]),  MARTINEZ Thierry committed Oct 20, 2015 26 27  new_graph, set_graph_name(reaction_graph),  MARTINEZ Thierry committed Mar 18, 2016 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61  get_current_graph(GraphId), reaction_graph(GraphId). import_reactions_from_graph :- biocham_command, doc(' Updates the set of reactions of the current model with the current graph. '), make_bipartite_graph, make_reactions. draw_reactions :- biocham_command, doc(' Draws the reaction graph of the current model. Equivalent to \\texttt{reaction_graph. draw_graph.} \\begin{example} '), biocham_silent(clear_model), biocham(load(library:examples/mapk/mapk)), biocham(draw_reactions), doc(' \\end{example} '), reaction_graph, draw_graph. :- devdoc('\\section{Public API}'). reaction_graph(GraphId) :-  MARTINEZ Thierry committed Oct 20, 2015 62 63 64 65 66 67 68  set_counter(reaction, 0), \+ ( item([kind: reaction, item: Item]), reaction(Item, Kinetics, Reactants, Products, Reversible), \+ ( count(reaction, ReactionCount), format(atom(ReactionCounter), 'reaction~d', [ReactionCount]),  MARTINEZ Thierry committed Mar 18, 2016 69  transition(GraphId, [ReactionCounter]),  MARTINEZ Thierry committed Oct 20, 2015 70  (  MARTINEZ Thierry committed Oct 24, 2015 71  Reversible = true  MARTINEZ Thierry committed Oct 20, 2015 72  ->  MARTINEZ Thierry committed Mar 24, 2016 73  set_attribute(GraphId, [ReactionCounter], reversible = Reversible)  MARTINEZ Thierry committed Oct 20, 2015 74 75 76 77 78 79 80 81  ; true ), ( Kinetics = 'MA'(1) -> true ;  MARTINEZ Thierry committed Mar 24, 2016 82  set_attribute(GraphId, [ReactionCounter], kinetics = Kinetics)  MARTINEZ Thierry committed Oct 20, 2015 83 84 85  ), \+ ( (  MARTINEZ Thierry committed Oct 24, 2015 86  member(Stoichiometry * Object, Reactants),  MARTINEZ Thierry committed Oct 20, 2015 87 88 89  From = Object, To = ReactionCounter ;  MARTINEZ Thierry committed Oct 24, 2015 90  member(Stoichiometry * Object, Products),  MARTINEZ Thierry committed Oct 20, 2015 91 92 93 94  From = ReactionCounter, To = Object ), \+ (  MARTINEZ Thierry committed Mar 18, 2016 95 96  place(GraphId, Object), add_edge(GraphId, From -> To, EdgeId),  MARTINEZ Thierry committed Oct 20, 2015 97  (  MARTINEZ Thierry committed Mar 24, 2016 98  get_attribute(EdgeId, stoichiometry = OldStoichiometry)  MARTINEZ Thierry committed Oct 20, 2015 99  ->  MARTINEZ Thierry committed Oct 24, 2015 100  NewStoichiometry is OldStoichiometry + Stoichiometry  MARTINEZ Thierry committed Oct 20, 2015 101  ;  MARTINEZ Thierry committed Oct 24, 2015 102  NewStoichiometry is Stoichiometry  MARTINEZ Thierry committed Oct 20, 2015 103 104  ), (  MARTINEZ Thierry committed Oct 24, 2015 105  NewStoichiometry = 1  MARTINEZ Thierry committed Oct 20, 2015 106 107  -> catch(  MARTINEZ Thierry committed Mar 18, 2016 108  delete_attribute(EdgeId, stoichiometry),  MARTINEZ Thierry committed Oct 20, 2015 109 110 111 112  error(unknown_item), true ) ;  MARTINEZ Thierry committed Mar 24, 2016 113  set_attribute(EdgeId, stoichiometry = NewStoichiometry)  MARTINEZ Thierry committed Oct 20, 2015 114 115 116 117 118 119 120  ) ) ) ) ).  MARTINEZ Thierry committed Feb 16, 2016 121 122 123 :- devdoc('\\section{Private predicates}').  MARTINEZ Thierry committed Oct 20, 2015 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 :- dynamic(vertex_transition/1). :- dynamic(vertex_place/1). :- dynamic(fixpoint/0). make_bipartite_graph :- get_current_graph(GraphId), retractall(vertex_transition(_)), retractall(vertex_place(_)), retractall(vertex_unknown(_)), \+ ( item([parent: GraphId, kind: vertex, id: VertexId, item: Vertex]),  MARTINEZ Thierry committed Mar 24, 2016 140  get_attribute(VertexId, kind = Kind),  MARTINEZ Thierry committed Oct 20, 2015 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237  \+ ( ( Kind = place -> assertz(vertex_place(Vertex)) ; Kind = transition -> assertz(vertex_transition(Vertex)) ; true ) ) ), \+ \+ ( repeat, assertz(fixpoint), \+ ( item([parent: GraphId, kind: edge, item: (A -> B)]), \+ ( reaction_object(A, B), object_reaction(A, B), reaction_object(B, A), object_reaction(B, A) ) ), fixpoint ), check_bipartite_graph. check_bipartite_graph :- get_current_graph(GraphId), findall( AmbiguousVertex, ( vertex_transition(AmbiguousVertex), vertex_place(AmbiguousVertex) ), AmbiguousVertices ), ( AmbiguousVertices = [] -> true ; throw(error(ambiguous_vertices(AmbiguousVertices))) ), findall( AmbiguousEdge, ( item([parent: GraphId, kind: edge, item: AmbiguousEdge]), AmbiguousEdge = (From -> _To), \+ vertex_transition(From), \+ vertex_place(From) ), AmbiguousEdges ), ( AmbiguousEdges = [] -> true ; throw(error(ambiguous_edges(AmbiguousEdges))) ). reaction_object(A, B) :- ( vertex_transition(A), \+ vertex_place(B) -> assertz(vertex_place(B)), retractall(fixpoint) ; true ). object_reaction(A, B) :- ( vertex_place(A), \+ vertex_transition(B) -> assertz(vertex_transition(B)), retractall(fixpoint) ; true ). make_reactions :- delete_items([kind: reaction]), get_current_graph(GraphId), \+ ( vertex_transition(Reaction), \+ (  MARTINEZ Thierry committed Mar 18, 2016 238 239  get_kinetics(GraphId, Reaction, Kinetics), get_reversible(GraphId, Reaction, Reversible),  MARTINEZ Thierry committed Oct 20, 2015 240  findall(  MARTINEZ Thierry committed Oct 24, 2015 241  Stoichiometry * Reactant,  MARTINEZ Thierry committed Oct 20, 2015 242 243 244 245 246 247 248 249  ( vertex_place(Reactant), item([ parent: GraphId, kind: edge, item: (Reactant -> Reaction), id: EdgeId ]),  MARTINEZ Thierry committed Oct 24, 2015 250  get_stoichiometry(EdgeId, Stoichiometry)  MARTINEZ Thierry committed Oct 20, 2015 251 252 253 254  ), Left ), findall(  MARTINEZ Thierry committed Oct 24, 2015 255  Stoichiometry * Product,  MARTINEZ Thierry committed Oct 20, 2015 256 257 258 259 260 261 262 263  ( vertex_place(Product), item([ parent: GraphId, kind: edge, item: (Reaction -> Product), id: EdgeId ]),  MARTINEZ Thierry committed Oct 24, 2015 264  get_stoichiometry(EdgeId, Stoichiometry)  MARTINEZ Thierry committed Oct 20, 2015 265 266 267 268 269 270 271 272  ), Right ), add_reaction(Kinetics, Left, Right, Reversible) ) ).  MARTINEZ Thierry committed Mar 18, 2016 273 get_kinetics(GraphId, Vertex, Kinetics) :-  MARTINEZ Thierry committed Oct 20, 2015 274  (  MARTINEZ Thierry committed Mar 24, 2016 275  get_attribute(GraphId, Vertex, kinetics = Kinetics)  MARTINEZ Thierry committed Oct 20, 2015 276 277 278 279 280 281 282  -> true ; Kinetics = 'MA'(1) ).  MARTINEZ Thierry committed Mar 18, 2016 283 get_reversible(GraphId, Vertex, Reversible) :-  MARTINEZ Thierry committed Oct 20, 2015 284  (  MARTINEZ Thierry committed Mar 24, 2016 285  get_attribute(GraphId, Vertex, reversible = Reversible)  MARTINEZ Thierry committed Oct 20, 2015 286 287 288  -> ( (  MARTINEZ Thierry committed Oct 24, 2015 289  Reversible = true  MARTINEZ Thierry committed Oct 20, 2015 290  ;  MARTINEZ Thierry committed Oct 24, 2015 291  Reversible = false  MARTINEZ Thierry committed Oct 20, 2015 292 293 294 295  ) -> true ;  MARTINEZ Thierry committed Oct 24, 2015 296  throw(error(reversible_attribute_boolean))  MARTINEZ Thierry committed Oct 20, 2015 297 298  ) ;  MARTINEZ Thierry committed Oct 24, 2015 299  Reversible = false  MARTINEZ Thierry committed Oct 20, 2015 300 301 302  ).  MARTINEZ Thierry committed Mar 18, 2016 303 get_stoichiometry(EdgeId, Stoichiometry) :-  MARTINEZ Thierry committed Oct 20, 2015 304  (  MARTINEZ Thierry committed Mar 24, 2016 305  get_attribute(EdgeId, stoichiometry = Stoichiometry)  MARTINEZ Thierry committed Oct 20, 2015 306 307 308  -> true ;  MARTINEZ Thierry committed Oct 24, 2015 309  Stoichiometry = 1  MARTINEZ Thierry committed Oct 20, 2015 310  ).