libSBML Python API  5.18.0
addCustomValidator.py

Example of creating a custom validator to be called during validation.

1 #!/usr/bin/env python
2 ##
3 ## @file addCustomValidator.py
4 ## @brief Example creating a custom validator to be called during validation
5 ## @author Frank T. Bergmann
6 ##
7 ## <!--------------------------------------------------------------------------
8 ## This sample program is distributed under a different license than the rest
9 ## of libSBML. This program uses the open-source MIT license, as follows:
10 ##
11 ## Copyright (c) 2013-2018 by the California Institute of Technology
12 ## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
13 ## and the University of Heidelberg (Germany), with support from the National
14 ## Institutes of Health (USA) under grant R01GM070923. All rights reserved.
15 ##
16 ## Permission is hereby granted, free of charge, to any person obtaining a
17 ## copy of this software and associated documentation files (the "Software"),
18 ## to deal in the Software without restriction, including without limitation
19 ## the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 ## and/or sell copies of the Software, and to permit persons to whom the
21 ## Software is furnished to do so, subject to the following conditions:
22 ##
23 ## The above copyright notice and this permission notice shall be included in
24 ## all copies or substantial portions of the Software.
25 ##
26 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 ## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 ## DEALINGS IN THE SOFTWARE.
33 ##
34 ## Neither the name of the California Institute of Technology (Caltech), nor
35 ## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
36 ## of Heidelberg, nor the names of any contributors, may be used to endorse
37 ## or promote products derived from this software without specific prior
38 ## written permission.
39 ## ------------------------------------------------------------------------ -->
40 
41 import sys
42 import os.path
43 import libsbml
44 
45 
46 ##
47 ## Declares a custom validator to be called. This allows you to validate
48 ## any aspect of an SBML Model that you want to be notified about. You could
49 ## use this to notify your application that a model contains an unsupported
50 ## feature of SBML (either as warning).
51 ##
52 ## In this example the validator will go through the model and test for the
53 ## presence of 'fast' reactions and algebraic rules. If either is used a
54 ## warning will be added to the error log.
55 ##
56 
57 class MyCustomValidator(libsbml.SBMLValidator):
58  def __init__(self, orig=None):
59  if orig is None:
60  super(MyCustomValidator, self).__init__()
61  else:
62  super(MyCustomValidator, self).__init__(orig)
63 
64  def clone(self):
65  return MyCustomValidator(self)
66 
67  def validate(self):
68  # if we don't have a model we don't apply this validator.
69  if self.getDocument() is None or self.getModel() is None:
70  return 0
71 
72  # if we have no rules and reactions we don't apply this validator either
73  if self.getModel().getNumReactions() == 0 and self.getModel().getNumRules() == 0:
74  return 0
75 
76  numErrors = 0
77  # test for algebraic rules
78  for i in range(0, self.getModel().getNumRules()):
79  if self.getModel().getRule(i).getTypeCode() == libsbml.SBML_ALGEBRAIC_RULE:
80  self.getErrorLog().add(SBMLError(99999, 3, 1,
81  "This model uses algebraic rules, however this application does not support them.",
82  0, 0,
83  libsbml.LIBSBML_SEV_WARNING,
84  # or LIBSBML_SEV_ERROR if you want to stop
85  libsbml.LIBSBML_CAT_SBML # or whatever category you prefer
86  ))
87  numErrors += 1
88 
89  # test for fast reactions
90  for i in range(0, self.getModel().getNumReactions()):
91  # test whether value is set, and true
92  if (self.getModel().getReaction(i).isSetFast() and
93  self.getModel().getReaction(i).getFast()):
94  self.getErrorLog().add(SBMLError(99999, 3, 1,
95  "This model uses fast reactions, however this application does not support them.",
96  0, 0,
97  libsbml.LIBSBML_SEV_WARNING,
98  # or LIBSBML_SEV_ERROR if you want to stop
99  libsbml.LIBSBML_CAT_SBML # or whatever category you prefer
100  ))
101  numErrors += 1
102  return numErrors
103 
104 
105 def main(args):
106  """Usage: addCustomValidator filename
107  """
108  if len(args) != 2:
109  print(main.__doc__)
110  sys.exit(1)
111 
112  # read the file name
113  document = libsbml.readSBML(args[1])
114 
115  # add a custom validator
116  document.addValidator(MyCustomValidator())
117 
118  # check consistency like before
119  numErrors = document.checkConsistency()
120 
121  # print errors and warnings
122  document.printErrors()
123 
124  # return number of errors
125  return numErrors
126 
127 
128 if __name__ == '__main__':
129  main(sys.argv)