libSBML Python API  5.18.0
callExternalValidator.py

Example that shows how to call an external program for validation.

1 #!/usr/bin/env python
2 ##
3 ## @file callExternalValidator.py
4 ## @brief Example that shows how to call an external program for 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 from libsbml import *
44 
45 def main (args):
46  """Usage: callExternalValidator filename externalValidator [ tempSBMLFile outputFile [ ADDITIONAL-ARGS] ]
47  calls an external validator
48  """
49  if len(args) < 3:
50  print(main.__doc__)
51  sys.exit(1)
52 
53  filename = args[1]
54 
55  # read additional args
56  externalValidator = args[2]
57 
58  tempSBMLFileName = filename + "_temp.xml"
59  if len(args) > 3:
60  tempSBMLFileName = args[3]
61 
62  outputFile = filename + "_out.xml"
63  if len(args) > 4:
64  outputFile = args[4]
65 
66  additionalArgs = []
67  for i in range (5, len(args)):
68  additionalArgs += [args[i]]
69 
70  # add the output file as additional arg
71  additionalArgs += [outputFile]
72 
73  # read the file name
74  document = readSBML(filename)
75 
76  # create a external validator that will write the model to
77  # tempFile, then call teh externalValidator with the given number of arguments
78  # to produce the output file. This output file will then be parsed and its
79  # errors will be added to the error log.
80  validator = SBMLExternalValidator()
81 
82  validator.setProgram(externalValidator)
83  validator.setSBMLFileName(tempSBMLFileName)
84  validator.setOutputFileName(outputFile)
85  for item in additionalArgs:
86  validator.addArgument(item)
87 
88  # this means that the external program will be called with the following arguments
89  #
90  # externalValidator tempSBMLFileName additionalArgs
91  #
92  # (where additionalArgs contains the output file as last argument)
93  #
94  # The output file that is generated should be an XML document following the
95  # Validator XML format as described here: http://sbml.org/validator/api/#xml
96  #
97 
98  # disable all regular checks
99  document.setApplicableValidators(0)
100 
101  # add a custom validator
102  document.addValidator(validator)
103 
104  # check consistency like before
105  numErrors = document.checkConsistency()
106 
107  # print errors and warnings
108  document.printErrors()
109 
110  # return number of errors
111  return numErrors
112 
113 
114 if __name__ == '__main__':
115  main(sys.argv)