1 package org.textensor.stochdiff.model;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5
6 import java.util.StringTokenizer;
7
8 import org.textensor.report.E;
9 import org.textensor.stochdiff.inter.AddableTo;
10 import org.textensor.stochdiff.numeric.chem.ReactionTable;
11
12
13 public class Reaction implements AddableTo {
14
15 public String name;
16 public String id;
17
18 public String reactants;
19 public String products;
20
21 private ArrayList<Reactant> p_reactants;
22 private ArrayList<Product> p_products;
23
24 public Catalyst catalyst;
25
26 public double forwardRate;
27 public double reverseRate;
28
29 public double michaelisConstant;
30
31 public double Q10;
32
33 private ArrayList<Specie> r_reactants;
34 private ArrayList<Specie> r_products;
35 private Specie r_catalyst;
36
37
38 public void add(Object obj) {
39 if (obj instanceof Reactant) {
40 if (p_reactants == null) {
41 p_reactants = new ArrayList<Reactant>();
42 }
43 p_reactants.add((Reactant)obj);
44 } else if (obj instanceof Product) {
45 if (p_products == null) {
46 p_products = new ArrayList<Product>();
47 }
48 p_products.add((Product)obj);
49
50 } else if (obj instanceof Catalyst) {
51 catalyst = (Catalyst)obj;
52
53 } else {
54 E.error("cant add " + obj);
55 }
56
57 }
58
59
60 public void resolve(HashMap<String, Specie> sphm) {
61 if (reactants != null) {
62 r_reactants = parseList(reactants, sphm);
63
64 } else if (p_reactants != null) {
65 r_reactants = parseRefs(p_reactants, sphm);
66 } else {
67 E.error("no reactants? ");
68 }
69
70 if (products != null) {
71 r_products = parseList(reactants, sphm);
72 } else if (p_products != null) {
73 r_products = parseRefs(p_products, sphm);
74 } else {
75 E.error("no reactants? ");
76 }
77 if (catalyst != null) {
78 r_catalyst = sphm.get(catalyst.getSpecieID());
79 }
80 }
81
82
83 private ArrayList<Specie> parseList(String lst, HashMap<String, Specie> sphm) {
84
85 ArrayList<Specie> ret = new ArrayList<Specie>();
86 StringTokenizer st = new StringTokenizer(lst, " ,");
87 while (st.hasMoreTokens()) {
88 String tok = st.nextToken();
89 if (sphm.containsKey(tok)) {
90 ret.add(sphm.get(tok));
91 } else {
92 E.error("reaction " + name + " mentions unknown specie " + tok);
93 }
94
95 }
96 return ret;
97 }
98
99
100
101 private ArrayList<Specie> parseRefs(ArrayList<? extends SpecieRef> asr,
102 HashMap<String, Specie> sphm) {
103
104 ArrayList<Specie> ret = new ArrayList<Specie>();
105 for (SpecieRef sr : asr) {
106
107 if (sphm.containsKey(sr.getSpecieID())) {
108 ret.add(sphm.get(sr.getSpecieID()));
109 } else {
110 E.error("reaction " + name + " mentions unknown specie " + sr);
111 }
112
113 }
114 return ret;
115 }
116
117
118
119
120 private int[][] getIndices(ArrayList<Specie> spa, ArrayList<? extends SpecieRef> refs) {
121 int n = spa.size();
122 int[][] ret = new int[2][n];
123 for (int i = 0; i < n; i++) {
124 ret[0][i] = spa.get(i).getIndex();
125 ret[1][i] = 1;
126 }
127 if (refs != null) {
128 for (int i = 0; i < n; i++) {
129 ret[1][i] = refs.get(i).getN();
130 }
131 }
132 return ret;
133 }
134
135
136 public void writeCatalyzedToTable(ReactionTable rtab, int ir) {
137 int nr = r_reactants.size();
138 int np= r_products.size();
139 int icat = r_catalyst.getIndex();
140 rtab.setCatalyzedReactionData(ir, nr, np, icat,
141 getIndices(r_reactants, p_reactants), getIndices(r_products, p_products), michaelisConstant);
142 }
143
144
145
146 public void writeForwardToTable(ReactionTable rtab, int ir) {
147 rtab.setReactionData(ir, getIndices(r_reactants, p_reactants),
148 getIndices(r_products, p_products), forwardRate);
149 }
150
151
152 public void writeReverseToTable(ReactionTable rtab, int ir) {
153 rtab.setReactionData(ir, getIndices(r_products, p_products),
154 getIndices(r_reactants, p_reactants), reverseRate);
155
156 }
157
158
159 }