reaction_graphs.pl 5.54 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3 4
:- module(
  reaction_graphs,
  [
    reaction_graph/0,
Thierry Martinez's avatar
Thierry Martinez committed
5
    import_reactions_from_graph/0,
Thierry Martinez's avatar
Thierry Martinez committed
6
    draw_reactions/0
Thierry Martinez's avatar
Thierry Martinez committed
7 8 9
  ]
).

Thierry Martinez's avatar
Thierry Martinez committed
10

Thierry Martinez's avatar
Thierry Martinez committed
11 12
:- devdoc('\\section{Commands}').

Thierry Martinez's avatar
Thierry Martinez committed
13

Thierry Martinez's avatar
Thierry Martinez committed
14 15
reaction_graph :-
  biocham_command,
Thierry Martinez's avatar
Thierry Martinez committed
16
  doc('Builds the reaction graph of the current model.'),
Thierry Martinez's avatar
SBML  
Thierry Martinez committed
17
  delete_items([kind: graph, key: reaction_graph]),
Thierry Martinez's avatar
Thierry Martinez committed
18 19 20 21 22 23 24 25 26
  new_graph,
  set_graph_name(reaction_graph),
  set_counter(reaction, 0),
  \+ (
    item([kind: reaction, item: Item]),
    reaction(Item, Kinetics, Reactants, Products, Reversible),
    \+ (
      count(reaction, ReactionCount),
      format(atom(ReactionCounter), 'reaction~d', [ReactionCount]),
Thierry Martinez's avatar
Thierry Martinez committed
27
      transition([ReactionCounter]),
Thierry Martinez's avatar
Thierry Martinez committed
28 29 30
      (
        Reversible = yes
      ->
Thierry Martinez's avatar
Thierry Martinez committed
31
        set_attribute([ReactionCounter], reversible: Reversible)
Thierry Martinez's avatar
Thierry Martinez committed
32 33 34 35 36 37 38 39
      ;
        true
      ),
      (
        Kinetics = 'MA'(1)
      ->
        true
      ;
Thierry Martinez's avatar
Thierry Martinez committed
40
        set_attribute([ReactionCounter], kinetics: Kinetics)
Thierry Martinez's avatar
Thierry Martinez committed
41 42 43 44 45 46 47 48 49 50 51 52
      ),
      \+ (
        (
          member(Stochiometry * Object, Reactants),
          From = Object,
          To = ReactionCounter
        ;
          member(Stochiometry * Object, Products),
          From = ReactionCounter,
          To = Object
        ),
        \+ (
Thierry Martinez's avatar
Thierry Martinez committed
53 54
          place([Object]),
          add_edge([From -> To]),
Thierry Martinez's avatar
Thierry Martinez committed
55 56 57 58 59 60 61 62 63 64 65
          (
            get_attribute((From -> To), stochiometry: OldStochiometry)
          ->
            NewStochiometry is OldStochiometry + Stochiometry
          ;
            NewStochiometry is Stochiometry
          ),
          (
            NewStochiometry = 1
          ->
            catch(
Thierry Martinez's avatar
Thierry Martinez committed
66
              delete_attribute([From -> To], stochiometry),
Thierry Martinez's avatar
Thierry Martinez committed
67 68 69 70
              error(unknown_item),
              true
            )
          ;
Thierry Martinez's avatar
Thierry Martinez committed
71
            set_attribute([From -> To], stochiometry: NewStochiometry)
Thierry Martinez's avatar
Thierry Martinez committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
          )
        )
      )
    )
  ).


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.

Thierry Martinez's avatar
Thierry Martinez committed
87 88 89

draw_reactions :-
  biocham_command,
Thierry Martinez's avatar
SBML  
Thierry Martinez committed
90 91 92 93 94 95 96 97 98 99 100 101 102
  doc('
    Draws the reaction graph of the current model.
    Equivalent to \\texttt{reaction_graph. draw_graph.}
    \\begin{example}
  '),
  biocham_silent(clear_model),
  biocham(load('../examples/mapk/mapk')),
  biocham(draw_reactions),
  doc('
    \\end{example}
  '),
  reaction_graph,
  draw_graph.
Thierry Martinez's avatar
Thierry Martinez committed
103

Thierry Martinez's avatar
Thierry Martinez committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

:- 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]),
    get_attribute(VertexId, kind: Kind),
    \+ (
      (
        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),
    \+ (
      get_kinetics(Reaction, Kinetics),
      get_reversible(Reaction, Reversible),
      findall(
        Stochiometry * Reactant,
        (
          vertex_place(Reactant),
          item([
            parent: GraphId,
            kind: edge,
            item: (Reactant -> Reaction),
            id: EdgeId
          ]),
          get_stochiometry(EdgeId, Stochiometry)
        ),
        Left
      ),
      findall(
        Stochiometry * Product,
        (
          vertex_place(Product),
          item([
            parent: GraphId,
            kind: edge,
            item: (Reaction -> Product),
            id: EdgeId
          ]),
          get_stochiometry(EdgeId, Stochiometry)
        ),
        Right
      ),
      add_reaction(Kinetics, Left, Right, Reversible)
    )
  ).


get_kinetics(Vertex, Kinetics) :-
  (
    get_attribute(Vertex, kinetics: Kinetics)
  ->
    true
  ;
    Kinetics = 'MA'(1)
  ).


get_reversible(Vertex, Reversible) :-
  (
    get_attribute(Vertex, reversible: Reversible)
  ->
    (
      (
        Reversible = yes
      ;
        Reversible = no
      )
    ->
      true
    ;
      throw(error(reversible_attribute_yes_no))
    )
  ;
    Reversible = no
  ).


get_stochiometry(Vertex, Stochiometry) :-
  (
    get_attribute(Vertex, stochiometry: Stochiometry)
  ->
    true
  ;
    Stochiometry = 1
  ).