1
2
3
4
5
6 """Martel based parser to read ECell formatted files.
7
8 This is a huge regular regular expression for Ecell, built using
9 the 'regular expressiona on steroids' capabilities of Martel.
10
11 #http://www.bioinformatics.org/ecell2/
12
13
14
15 Notes:
16 Just so I remember -- the new end of line syntax is:
17 New regexp syntax - \R
18 \R means "\n|\r\n?"
19 [\R] means "[\n\r]"
20
21 This helps us have endlines be consistent across platforms.
22
23 """
24
25 import string
26
27
28
29
30
31 """Hold ECell data in a straightforward format.
32
33 classes:
34 o Record - All of the information in an ECell record.
35 """
36
38 """Hold ECell information in a format similar to the original record.
39
40
41 """
43 self.cell_dict = {}
44 self._max_dict = {}
45 self._duplicates = []
46 self._ecell_version = '0.9.4.6'
47 self._name = 'Ecell module'
48 self._version = ''
49 self.include_buf = ''
50 self.num_systems = 0
51 self.num_substances = 0
52 self.num_reactors = 0
53 self.include_block = ''
54 self.contains_concentration = 0
55
56
58 output = '# this is ecell rule file for E-CELL' + self._ecell_version + '\n'
59 output = output + '# converted by %s %s\n\n' % ( self._name, self._version )
60 if( self.contains_concentration ):
61 output = output + 'include(qty.er)\n'
62 output = output + self.include_buf
63 system_output = self._print_systems()
64 substance_output = self._print_substances()
65 reactor_output = self._print_reactors( substance_output )
66 output = output + system_output + reactor_output
67 return output
68
70 output = ''
71 for system in range( 1, self.num_systems + 1 ):
72 composite_key = 'system' + str( system ) + 'class0'
73 output = output + '\nsystem %s' % self.cell_dict[ composite_key ]
74 composite_key = 'system' + str( system ) + 'path0'
75 output = output + '(%s:' % self.cell_dict[ composite_key ]
76 composite_key = 'system' + str( system ) + 'id0'
77 output = output + '%s,' % self.cell_dict[ composite_key ]
78 composite_key = 'system' + str( system ) + 'name0'
79 output = output + '"%s")\n' % self.cell_dict[ composite_key ]
80 output = output + '{\n'
81
82 output = output + '\tStepper SlaveStepper;\n'
83 composite_key = 'system' + str( system ) + 'inside0'
84 if( self.cell_dict.has_key( composite_key ) ):
85 output = output + '\tInside %s;\n' % self.cell_dict[ composite_key ]
86 composite_key = 'system' + str( system ) + 'outside0'
87 if( self.cell_dict.has_key( composite_key ) ):
88 output = output + '\tOutside %s;\n' % self.cell_dict[ composite_key ]
89 composite_key = 'system' + str( system ) + 'volumeindex0'
90 if( self.cell_dict.has_key( composite_key ) ):
91 output = output + '\tVolumeIndex %s;\n' % self.cell_dict[ composite_key ]
92 output = output + '}\n'
93 return output
94
96 output = ''
97 for substance in range( 1, self.num_substances + 1 ):
98 composite_key = 'substance' + str( substance ) + 'class0'
99 if( self.cell_dict.has_key( composite_key ) ):
100 output = output + 'substance %s' % self.cell_dict[ composite_key ]
101 composite_key = 'substance' + str( substance ) + 'path0'
102 output = output + '(%s:' % get_entry( self.cell_dict, composite_key )
103 composite_key = 'substance' + str( substance ) + 'id0'
104 output = output + '%s,' % get_entry( self.cell_dict, composite_key )
105 composite_key = 'substance' + str( substance ) + 'name0'
106 output = output + '"%s")\n' % get_entry( self.cell_dict, composite_key )
107 composite_key = 'substance' + str( substance ) + 'qty0'
108 output = output + '{\n\tQuantity %s;\n}\n' % get_entry( self.cell_dict, composite_key )
109 else:
110 composite_key = 'substance' + str( substance ) + 'path0'
111 output = output + 'substance %s:' % get_entry( self.cell_dict, composite_key )
112 composite_key = 'substance' + str( substance ) + 'id0'
113 output = output + '%s ' % get_entry( self.cell_dict, composite_key )
114 composite_key = 'substance' + str( substance ) + 'name0'
115 output = output + '"%s" ' % get_entry( self.cell_dict, composite_key )
116 composite_key = 'substance' + str( substance ) + 'qty0'
117 output = output + '%s;\n' % get_entry( self.cell_dict, composite_key )
118 qty_key = 'substance' + str( substance ) + 'qty1'
119 conc_key = 'substance' + str( substance ) + 'conc1'
120 if( get_entry( self.cell_dict, qty_key ).lower() == 'fix' ):
121 composite_key = 'substance' + str( substance ) + 'path0'
122 output = output + 'fix %s:' % get_entry( self.cell_dict, composite_key )
123 composite_key = 'substance' + str( substance ) + 'id0'
124 output = output + '%s;\n' % get_entry( self.cell_dict, composite_key )
125 elif( get_entry( self.cell_dict, conc_key ).lower() == 'fix' ):
126 composite_key = 'substance' + str( substance ) + 'path0'
127 output = output + 'fix %s:' % get_entry( self.cell_dict, composite_key )
128 composite_key = 'substance' + str( substance ) + 'id0'
129 output = output + '%s;\n' % get_entry( self.cell_dict, composite_key )
130 return output
131
133 volume_buf = '\n'
134 self._check_duplicates()
135 for reactor in range( 1, self.num_reactors + 1 ):
136 output = output + '\nreactor '
137 prefix = 'reactor' + str( reactor )
138 composite_key = prefix + 'class0'
139 output = output + self.cell_dict[ composite_key ]
140 composite_key = prefix + 'path0'
141 output = output + '(%s:' % self.cell_dict[ composite_key ]
142 composite_key = prefix + 'id0'
143 output = output + '%s,' % self.cell_dict[ composite_key ]
144 composite_key = prefix + 'name0'
145 output = output + '"%s")\n' % self.cell_dict[ composite_key ]
146
147 composite_key = 's_' + str( reactor )
148 num_substrates = get_entry( self._max_dict, composite_key )
149 output = output + '{\n'
150 for substrate in range( 1, num_substrates + 1 ):
151 output = output + '\tSubstrate '
152 composite_key = prefix + 's_path' + str( substrate )
153 output = output + get_entry( self.cell_dict, composite_key )
154 composite_key = prefix + 's_id' + str( substrate )
155 output = output + ':%s ' % get_entry( self.cell_dict, composite_key )
156 composite_key = prefix + 's_coeff' + str( substrate )
157 output = output + '%s' % get_entry( self.cell_dict, composite_key )
158 output = output + ';\n'
159
160 composite_key = 'p_' + str( reactor )
161 num_products = get_entry( self._max_dict, composite_key )
162 for product in range( 1, num_products + 1 ):
163 output = output + '\tProduct '
164 composite_key = prefix + 'p_path' + str( product )
165 output = output + get_entry( self.cell_dict, composite_key )
166 composite_key = prefix + 'p_id' + str( product )
167 output = output + ':%s ' % get_entry( self.cell_dict, composite_key )
168 composite_key = prefix + 'p_coeff' + str( product )
169 output = output + '%s' % get_entry( self.cell_dict, composite_key )
170 output = output + ';\n'
171
172 composite_key = 'c_' + str( reactor )
173 num_catalysts = get_entry( self._max_dict, composite_key )
174 for catalyst in range( 1, num_catalysts + 1 ):
175 output = output + '\tCatalyst '
176 composite_key = prefix + 'c_path' + str( catalyst )
177 output = output + get_entry( self.cell_dict, composite_key )
178 composite_key = prefix + 'c_id' + str( catalyst )
179 output = output + ':%s' % get_entry( self.cell_dict, composite_key )
180 output = output + ';\n'
181
182 composite_key = 'e_' + str( reactor )
183 num_effectors = get_entry( self._max_dict, composite_key )
184 for effector in range( 1, num_effectors + 1 ):
185 output = output + '\tEffector '
186 composite_key = prefix + 'e_path' + str( effector )
187 output = output + get_entry( self.cell_dict, composite_key )
188 composite_key = prefix + 'e_id' + str( effector )
189 output = output + ':%s ' % get_entry( self.cell_dict, composite_key )
190 composite_key = prefix + 'e_coeff' + str( effector )
191 output = output + '%s;\n' % get_entry( self.cell_dict, composite_key )
192 output = output + ';\n'
193
194 composite_key = 'o_' + str( reactor )
195 num_options = get_entry( self._max_dict, composite_key )
196 for option in range( 1, num_options + 1 ):
197 composite_key = prefix + 'o_type' + str( option )
198 output = output + '\t%s ' % get_entry( self.cell_dict, composite_key )
199 composite_key = prefix + 'o_path' + str( option )
200 output = output + get_entry( self.cell_dict, composite_key )
201 composite_key = prefix + 'o_id' + str( option )
202 output = output + ':%s ' % get_entry( self.cell_dict, composite_key )
203 composite_key = prefix + 'o_coeff' + str( option )
204 output = output + '%s;\n' % get_entry( self.cell_dict, composite_key )
205 output = output + ';\n'
206
207 composite_key = 'arg_tag' + str( reactor )
208 num_args = get_entry( self._max_dict, composite_key )
209 for arg in range( 1, num_args + 1 ):
210 composite_key = prefix + 'arg_tag' + str( arg )
211 output = output + '\t%s ' % get_entry( self.cell_dict, composite_key )
212 composite_key = prefix + 'arg_coeff' + str( arg )
213 output = output + '%s;\n' % get_entry( self.cell_dict, composite_key )
214
215 for system in range( 1, self.num_systems + 1 ):
216 path_key = prefix + 'path0'
217 id_key = prefix + 'id0'
218 reactor_path = get_entry( self.cell_dict, path_key )
219 reactor_id = get_entry( self.cell_dict, id_key )
220 path_id = '%s:%s' % ( reactor_path, reactor_id )
221 volume_key = 'system' + str( system ) + 'volumeindex0'
222 volume_index = get_entry( self.cell_dict, volume_key )
223 if( path_id == volume_index ):
224 output = output + '\tInitialActivity '
225 init_act_key = prefix + 'init_act0'
226 init_act0 = get_entry( self.cell_dict, init_act_key )
227 if( init_act0 == '' ):
228 init_act0 = get_entry( self.cell_dict, prefix + 'init_act1' )
229 output = output + '%s;\n' % init_act0
230 if( not ( system in self._duplicates ) ):
231 volume_buf = volume_buf + R'_SETVOLUME('
232 volume_buf = volume_buf + '%s,%s)\n' % ( reactor_path, init_act0 )
233 volume_buf = volume_buf.replace( r'//', '\/' )
234
235 output = output + '}\n'
236
237 if( self.contains_concentration ):
238 output = volume_buf + output
239 return output
240
242 self._duplicates = []
243 for system in range( 1, self.num_systems + 1 ):
244 target_path_key = 'system' + str( system ) + 'path0'
245 target_id_key = 'system' + str( system ) + 'id0'
246 for other in range( 1, system ):
247 match_path_key = 'system' + str( other ) + 'path0'
248 match_id_key = 'system' + str( other ) + 'id0'
249 if( self.cell_dict.has_key( target_path_key ) and \
250 self.cell_dict.has_key( target_id_key ) ):
251 if self._match_cell_dict_entry( target_path_key, match_path_key ):
252 if self._match_cell_dict_entry( target_id_key, match_id_key ):
253 self._duplicates.append( system )
254
255 - def _match_cell_dict_entry( self, key, other_key ):
256 if( get_entry( self.cell_dict, key ) == get_entry( self.cell_dict, other_key ) ):
257 return 1
258 return 0
259
260
261 -def get_entry( dict, key ):
262 try:
263 entry = dict[ key ]
264 except KeyError:
265 entry = ''
266 return entry
267