This file is indexed.

/usr/share/cain/fio/ContentHandler.py is in cain 1.9-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
"""XML content handler."""
# CONTINUE: store attributes.keys() in keys.

if __name__ == '__main__':
    import sys
    sys.path.insert(1, '..')

from state.Model import Model, writeModelXml
from state.Method import Method
from state.TimeSeriesFrames import TimeSeriesFrames
from state.TimeSeriesAllReactions import TimeSeriesAllReactions
from state.HistogramFrames import HistogramFrames
from state.HistogramAverage import HistogramAverage
from state.StatisticsFrames import StatisticsFrames
from state.StatisticsAverage import StatisticsAverage
from state.Value import Value
from state.Species import Species
from state.Reaction import Reaction
from state.TimeEvent import TimeEvent
from state.TriggerEvent import TriggerEvent
from state.SpeciesReference import SpeciesReference

import xml.sax.handler

class ContentHandler(xml.sax.ContentHandler):
    """An XML content handler.

    Inherit from the XML SAX content handler.

    Error messages are collected in the string self.errors. After recording an
    error there is a return statement if the element cannot be further 
    processed."""

    def __init__(self):
        xml.sax.ContentHandler.__init__(self)
        self.models = {}
        self.methods = {}
        self.output = {}
        self.seed = None
        self.listOfMt19937States = []
        # The stack of the enclosing elements.
        self.elements = []
        self.errors = ''
        self.content = ''

    def startElement(self, name, attributes):
        self.elements.append(name)
        #
        # The top level element.
        if name == 'cain':
            if 'version' in attributes.keys():
                self.version = float(attributes['version'])
            else:
                self.version = 1.9
        elif name == 'sbml':
            # Fatal error.
            raise Exception('This is an SBML file. One may only directly open Cain files. Use File->Import SBML instead.')
        #
        # Elements for the model.
        #
        elif name == 'listOfModels':
            pass
        elif name == 'model':
            # Start a new model.
            self.model = Model()
            if 'id' in attributes.keys():
                self.model.id = attributes['id']
            if 'name' in attributes.keys():
                self.model.name = attributes['name']
        elif name == 'listOfParameters':
            # Start the dictionary of parameters.
            self.model.parameters = {}
        elif name == 'listOfCompartments':
            # Start the dictionary of compartments.
            self.model.compartments = {}
        elif name == 'listOfSpecies':
            # Start the dictionary of species and list of species identifiers.
            self.model.species = {}
            self.model.speciesIdentifiers = []
        elif name == 'listOfReactions':
            # Start the list of reactions.
            self.model.reactions = []
        elif name == 'listOfTimeEvents':
            # Start the list of time events.
            self.model.timeEvents = []
        elif name == 'listOfTriggerEvents':
            # Start the list of trigger events.
            self.model.triggerEvents = []
        elif name == 'parameter':
            # Append a parameter.
            if not 'id' in attributes.keys():
                self.errors += 'Missing id attribute in parameter.\n'
                return
            x = Value('', '')
            error = x.readXml(attributes)
            if error:
                self.errors += error
                return
            self.model.parameters[attributes['id']] = x
        elif name == 'compartment':
            # Append a compartment.
            if not 'id' in attributes.keys():
                self.errors += 'Missing id attribute in compartment.\n'
                return
            if not attributes['id']:
                self.errors += 'Compartment identifier is empty.\n'
                return
            compartment = Value('', '')
            compartment.readXml(attributes)
            self.model.compartments[attributes['id']] = compartment
        elif name == 'species':
            # Append a species.
            if not 'id' in attributes.keys():
                self.errors += 'Missing id attribute in species.\n'
                return
            x = Species(None, None, None)
            x.readXml(attributes)
            self.model.species[attributes['id']] = x
            self.model.speciesIdentifiers.append(attributes['id'])
        elif name == 'reaction':
            # Append a reaction.
            x = Reaction('', '', [], [], True, '')
            error = x.readXml(attributes)
            if error:
                self.errors += error
                return
            self.model.reactions.append(x)
        elif name == 'timeEvent':
            # Append a time event.
            x = TimeEvent('', '', '', '')
            error = x.readXml(attributes)
            if error:
                self.errors += error
                return
            self.model.timeEvents.append(x)
        elif name == 'triggerEvent':
            # Append a trigger event.
            x = TriggerEvent('', '', '', '', 0., False)
            error = x.readXml(attributes)
            if error:
                self.errors += error
                return
            self.model.triggerEvents.append(x)
        elif name == 'listOfReactants':
            if not self.model.reactions:
                self.errors += 'Badly placed listOfReactants tag.\n'
                return
        elif name == 'listOfProducts':
            if not self.model.reactions:
                self.errors += 'Badly placed listOfProducts tag.\n'
                return
        elif name == 'listOfModifiers':
            if not self.model.reactions:
                self.errors += 'Badly placed listOfModifiers tag.\n'
                return
        elif name == 'speciesReference':
            # Add the reactant or product to the current reaction.
            if not self.model.reactions:
                self.errors += 'Badly placed speciesReference tag.\n'
                return
            if not 'species' in attributes.keys():
                self.errors +=\
                    'Missing species attribute in speciesReference.\n'
                return
            if 'stoichiometry' in attributes.keys():
                stoichiometry = int(attributes['stoichiometry'])
            else:
                stoichiometry = 1
            # No need to record if the stoichiometry is zero.
            if stoichiometry != 0:
                sr = SpeciesReference(attributes['species'], stoichiometry)
                if self.elements[-2] == 'listOfReactants':
                    self.model.reactions[-1].reactants.append(sr)
                elif self.elements[-2] == 'listOfProducts':
                    self.model.reactions[-1].products.append(sr)
                else:
                    self.errors += 'Badly placed speciesReference tag.\n'
                    return
        elif name == 'modifierSpeciesReference':
            # Add to the reactants and products of the current reaction.
            if not self.model.reactions:
                self.errors += 'Badly placed modifierSpeciesReference tag.\n'
                return
            if not 'species' in attributes.keys():
                self.errors +=\
                    'Missing species attribute in modifierSpeciesReference.\n'
                return
            if self.elements[-2] != 'listOfModifiers':
                self.errors += 'Badly placed modifierSpeciesReference tag.\n'
                return
            sr = SpeciesReference(attributes['species'])
            self.model.reactions[-1].reactants.append(sr)
            self.model.reactions[-1].products.append(sr)
        #
        # Elements for the simulation parameters.
        #
        elif name == 'listOfMethods':
            pass
        elif name == 'method':
            m = Method()
            error = m.readXml(attributes)
            if error:
                self.errors += error
                return
            self.methods[m.id] = m
        #
        # Elements for the simulation output.
        #
        elif name == 'listOfOutput':
            pass
        # CONTINUE: trajectoryFrames is deprecated.
        elif name in ('timeSeriesFrames', 'trajectoryFrames'):
            if not 'model' in attributes.keys():
                self.errors +=\
                    'Missing model attribute in timeSeriesFrames.\n'
                return
            if not 'method' in attributes.keys():
                self.errors += 'Missing method attribute in timeSeriesFrames.\n'
                return
            key = (attributes['model'], attributes['method'])
            # An ensemble of trajectories should not be listed twice.
            if key in self.output:
                self.errors += 'Simulation output (' + key[0] + ', ' +\
                    key[1] + ') listed twice.\n'
            else:
                # Start a new ensemble.
                self.output[key] = TimeSeriesFrames()
                self.currentOutput = self.output[key]
        # CONTINUE: trajectoryAllReactions is deprecated.
        elif name in ('timeSeriesAllReactions', 'trajectoryAllReactions'):
            if not 'model' in attributes.keys():
                self.errors +=\
                    'Missing model attribute in timeSeriesAllReactions.\n'
                return
            if not 'method' in attributes.keys():
                self.errors += 'Missing method attribute in timeSeriesAllReactions.\n'
                return
            key = (attributes['model'], attributes['method'])
            if self.version >= 1:
                if not 'initialTime' in attributes.keys():
                    self.errors +=\
                        'Missing initialTime attribute in timeSeriesAllReactions.\n'
                    return
                if not 'finalTime' in attributes.keys():
                    self.errors +=\
                        'Missing finalTime attribute in timeSeriesAllReactions.\n'
                    return
            else:
                if not 'endTime' in attributes.keys():
                    self.errors +=\
                        'Missing endTime attribute in timeSeriesAllReactions.\n'
                    return
            # An ensemble of trajectories should not be listed twice.
            if key in self.output:
                self.errors += 'Simulation output (' + key[0] + ', ' +\
                    key[1] + ') listed twice.\n'
                return
            # Check that the model has been defined.
            if not attributes['model'] in self.models:
                self.errors += 'timeSeriesAllReactions uses an undefined model.\n'
                return
            # Start a new ensemble. By definition all of the species and
            # reactions are recorded.
            model = self.models[attributes['model']]
            if self.version >= 1:
                self.output[key] =\
                    TimeSeriesAllReactions(range(len(model.speciesIdentifiers)),
                                           range(len(model.reactions)),
                                           float(attributes['initialTime']),
                                           float(attributes['finalTime']))
            else:
                # CONTINUE: Deprecated.
                self.output[key] =\
                    TimeSeriesAllReactions(range(len(model.speciesIdentifiers)),
                                           range(len(model.reactions)),
                                           0.,
                                           float(attributes['endTime']))
            self.currentOutput = self.output[key]
        elif name == 'histogramFrames':
            for key in ('model', 'method', 'numberOfTrajectories'):
                if not key in attributes.keys():
                    self.errors +=\
                        'Missing ' + key + ' attribute in histogramFrames.\n'
                    return
            # CONTINUE: Make multiplicity mandatory.
            if 'multiplicity' in attributes.keys():
                multiplicity = int(attributes['multiplicity'])
            else:
                multiplicity = 2
            key = (attributes['model'], attributes['method'])
            # An ensemble of trajectories should not be listed twice.
            if key in self.output:
                self.errors += 'Simulation output (' + key[0] + ', ' +\
                    key[1] + ') listed twice.\n'
            else:
                # Check that the method has been defined.
                if not attributes['method'] in self.methods:
                    self.errors += 'HistogramFrames uses an undefined method.\n'
                    return
                # Start a new ensemble.
                method = self.methods[attributes['method']]
                self.output[key] = HistogramFrames(method.numberOfBins,
                                                   multiplicity)
                self.currentOutput = self.output[key]
                self.currentOutput.numberOfTrajectories =\
                    float(attributes['numberOfTrajectories'])
        elif name == 'histogramAverage':
            for key in ('model', 'method', 'numberOfTrajectories'):
                if not key in attributes.keys():
                    self.errors +=\
                        'Missing ' + key + ' attribute in histogramAverage.\n'
                    return
            # CONTINUE: Make multiplicity mandatory.
            if 'multiplicity' in attributes.keys():
                multiplicity = int(attributes['multiplicity'])
            else:
                multiplicity = 2
            key = (attributes['model'], attributes['method'])
            # An ensemble of trajectories should not be listed twice.
            if key in self.output:
                self.errors += 'Simulation output (' + key[0] + ', ' +\
                    key[1] + ') listed twice.\n'
            else:
                # Check that the method has been defined.
                if not attributes['method'] in self.methods:
                    self.errors += 'HistogramAverage uses an undefined method.\n'
                    return
                # Start a new ensemble.
                method = self.methods[attributes['method']]
                self.output[key] =\
                    HistogramAverage(method.numberOfBins, multiplicity)
                self.currentOutput = self.output[key]
                self.currentOutput.numberOfTrajectories =\
                    float(attributes['numberOfTrajectories'])
        elif name == 'statisticsFrames':
            for key in ('model', 'method'):
                if not key in attributes.keys():
                    self.errors +=\
                        'Missing ' + key + ' attribute in statisticsFrames.\n'
                    return
            key = (attributes['model'], attributes['method'])
            # An ensemble of trajectories should not be listed twice.
            if key in self.output:
                self.errors += 'Simulation output (' + key[0] + ', ' +\
                    key[1] + ') listed twice.\n'
            else:
                # Check that the method has been defined.
                if not attributes['method'] in self.methods:
                    self.errors += 'StatisticsFrames uses an undefined method.\n'
                    return
                # Start a new ensemble.
                method = self.methods[attributes['method']]
                self.output[key] = StatisticsFrames()
                self.currentOutput = self.output[key]
        elif name == 'statisticsAverage':
            for key in ('model', 'method'):
                if not key in attributes.keys():
                    self.errors +=\
                        'Missing ' + key + ' attribute in statisticsAverage.\n'
                    return
            key = (attributes['model'], attributes['method'])
            # An ensemble of trajectories should not be listed twice.
            if key in self.output:
                self.errors += 'Simulation output (' + key[0] + ', ' +\
                    key[1] + ') listed twice.\n'
            else:
                # Check that the method has been defined.
                if not attributes['method'] in self.methods:
                    self.errors += 'StatisticsAverage uses an undefined method.\n'
                    return
                # Start a new ensemble.
                method = self.methods[attributes['method']]
                self.output[key] = StatisticsAverage()
                self.currentOutput = self.output[key]
        elif name == 'frameTimes':
            self.frameTimes = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in frameTimes tag.\n'
        elif name == 'recordedSpecies':
            self.recordedSpecies = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in recordedSpecies tag.\n'
        elif name == 'recordedReactions':
            self.recordedReactions = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in recordedReactions tag.\n'
        elif name == 'populations':
            self.populations = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in population tag.\n'
        elif name == 'reactionCounts':
            self.reactionCounts = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in reactionCounts tag.\n'
        elif name == 'initialPopulations':
            self.initialPopulations = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in initialPopulations tag.\n'
        elif name == 'indices':
            self.indices = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in indices tag.\n'
        elif name == 'times':
            self.times = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in times tag.\n'
        elif name == 'histogram':
            # CONTINUE: Add 'cardinality', 'mean', 'summedSecondCenteredMoment',
            # and 'sumOfWeights' to the required attributes.
            for key in ('lowerBound', 'width', 'species'):
                if not key in attributes.keys():
                    self.errors +=\
                        'Missing ' + key + ' attribute in histogram.\n'
                    return
            species = int(attributes['species'])
            if self.currentOutput.__class__.__name__ == 'HistogramFrames':
                if not 'frame' in attributes.keys():
                    self.errors +=\
                        'Missing frame attribute in histogram.\n'
                    return
                frame = int(attributes['frame'])
                self.currentHistogram =\
                    self.currentOutput.histograms[frame][species]
            elif self.currentOutput.__class__.__name__ == 'HistogramAverage':
                self.currentHistogram =\
                    self.currentOutput.histograms[species]
            else:
                self.errors += 'Unkown histogram type.\n'
                return
            self.currentHistogramIndex = 0
            # CONTINUE Make these attributes required.
            if 'cardinality' in attributes:
                self.currentHistogram.cardinality =\
                    float(attributes['cardinality'])
            if 'mean' in attributes:
                self.currentHistogram.mean = float(attributes['mean'])
            if 'summedSecondCenteredMoment' in attributes:
                self.currentHistogram.summedSecondCenteredMoment =\
                    float(attributes['summedSecondCenteredMoment'])
            if 'sumOfWeights' in attributes:
                self.currentHistogram.sumOfWeights =\
                    float(attributes['sumOfWeights'])
            self.currentHistogram.lowerBound = float(attributes['lowerBound'])
            self.currentHistogram.setWidth(float(attributes['width']))
        elif name == 'statistics':
            self.statistics = []
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in statistics tag.\n'
        # CONTINUE: These are deprecated.
        elif name == 'firstHistogram':
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in firstHistogram tag.\n'
        elif name == 'secondHistogram':
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in secondHistogram tag.\n'
        elif name == 'histogramElement':
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in histogramElement tag.\n'
        #
        # Elements for the random number generator state.
        #
        elif name == 'random':
            if 'seed' in attributes.keys():
                self.seed = int(attributes['seed'])
        elif name == 'stateMT19937':
            self.listOfMt19937States.append([])
            # The content should be empty.
            if self.content:
                self.errors += 'Mishandled content in stateMT19937 tag.\n'
        #
        # Unknown tag.
        #
        else:
            self.errors += 'Unknown tag: ' + name + '\n'
            
    def endElement(self, name):
        del self.elements[-1]
        if name == 'model':
            # Add the current model.
            self.models[self.model.id] = self.model
        elif name == 'listOfParameters':
            # The list of parameters may be empty.
            pass
        elif name == 'listOfCompartments':
            # The list of compartments may be empty.
            pass
        elif name == 'listOfSpecies':
            if not (self.model.species and self.model.speciesIdentifiers):
                self.errors += 'No species were defined.\n'
        elif name == 'frameTimes':
            self.currentOutput.setFrameTimes\
                (map(float, self.content.split()))
            self.content = ''
        elif name == 'recordedSpecies':
            self.currentOutput.setRecordedSpecies\
                (map(int, self.content.split()))
            self.content = ''
        elif name == 'recordedReactions':
            self.currentOutput.recordedReactions =\
                map(int, self.content.split())
            self.content = ''
        elif name == 'histogram':
            self.currentHistogram = None
            self.currentHistogramIndex = None
        # CONTINUE: These are deprecated.
        elif name == 'firstHistogram':
            self.currentHistogram.set(0, map(float, self.content.split()))
            self.content = ''
        elif name == 'secondHistogram':
            self.currentHistogram.set(1, map(float, self.content.split()))
            self.content = ''
        elif name == 'histogramElement':
            self.currentHistogram.set(self.currentHistogramIndex,
                                      map(float, self.content.split()))
            self.currentHistogramIndex += 1
            self.content = ''
        elif name == 'populations':
            # Add the populations to the current set of trajectories.
            self.currentOutput.appendPopulations\
                (map(float, self.content.split()))
            self.content = ''
        elif name == 'reactionCounts':
            # Add the reaction counts the current set of trajectories.
            self.currentOutput.appendReactionCounts\
                (map(float, self.content.split()))
            self.content = ''
        elif name == 'initialPopulations':
            # Add the initial populations to the current set of trajectories.
            self.currentOutput.appendInitialPopulations\
                (map(float, self.content.split()))
            self.content = ''
        elif name == 'indices':
            # Add the reaction indices to the current set of trajectories.
            self.currentOutput.appendIndices(map(int, self.content.split()))
            self.content = ''
        elif name == 'times':
            # Add the reaction times to the current set of trajectories.
            self.currentOutput.appendTimes\
                (map(float, self.content.split()))
            self.content = ''
        elif name == 'statistics':
            self.currentOutput.setStatistics\
                (map(float, self.content.split()))
            self.content = ''
        elif name == 'stateMT19937':
            # Add the elements to the state.
            self.listOfMt19937States[-1].extend(map(int, self.content.split()))
            self.content = ''
            # 624 for the array, 1 for the position.
            if len(self.listOfMt19937States[-1]) != 625:
                self.errors += 'Bad Mersenne Twister state.\n'
        # CONTINUE: Should I check the populations and reaction counts?
                
    def characters(self, content):
        # CONTINUE: firstHistogram and secondHistogram are deprecated.
        if self.elements[-1] in\
                ('firstHistogram', 'secondHistogram', 'histogramElement',
                 'frameTimes', 'recordedSpecies', 'recordedReactions',
                 'populations', 'reactionCounts', 'initialPopulations',
                 'indices', 'times', 'statistics', 'stateMT19937'):
            self.content += content

def main():
    from xml.sax import parse
    from glob import glob

    for name in glob('../examples/cain/*.xml'):
        handler = ContentHandler()
        parse(open(name, 'r'), handler)
        assert not handler.errors

if __name__ == '__main__':
    main()