libSBML Python API  5.18.0
validateSBML.py

Validates one or more SBML files.

1 #!/usr/bin/env python
2 ##
3 ## @file validateSBML.py
4 ## @brief Validates one or more SBML files
5 ## @author Akiya Jouraku (translated from libSBML C++ examples)
6 ## @author Ben Bornstein
7 ## @author Michael Hucka
8 ##
9 ## <!--------------------------------------------------------------------------
10 ## This sample program is distributed under a different license than the rest
11 ## of libSBML. This program uses the open-source MIT license, as follows:
12 ##
13 ## Copyright (c) 2013-2018 by the California Institute of Technology
14 ## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
15 ## and the University of Heidelberg (Germany), with support from the National
16 ## Institutes of Health (USA) under grant R01GM070923. All rights reserved.
17 ##
18 ## Permission is hereby granted, free of charge, to any person obtaining a
19 ## copy of this software and associated documentation files (the "Software"),
20 ## to deal in the Software without restriction, including without limitation
21 ## the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 ## and/or sell copies of the Software, and to permit persons to whom the
23 ## Software is furnished to do so, subject to the following conditions:
24 ##
25 ## The above copyright notice and this permission notice shall be included in
26 ## all copies or substantial portions of the Software.
27 ##
28 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 ## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34 ## DEALINGS IN THE SOFTWARE.
35 ##
36 ## Neither the name of the California Institute of Technology (Caltech), nor
37 ## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
38 ## of Heidelberg, nor the names of any contributors, may be used to endorse
39 ## or promote products derived from this software without specific prior
40 ## written permission.
41 ## ------------------------------------------------------------------------ -->
42 
43 import sys
44 import os.path
45 import time
46 import libsbml
47 
48 class validateSBML:
49  def __init__(self, ucheck):
50  self.reader = libsbml.SBMLReader()
51  self.ucheck = ucheck
52  self.numinvalid = 0
53 
54  def validate(self, file):
55  if not os.path.exists(file):
56  print("[Error] %s : No such file." % infile)
57  self.numinvalid += 1
58  return
59 
60  start = time.time()
61  sbmlDoc = libsbml.readSBML(file)
62  stop = time.time()
63  timeRead = (stop - start)*1000
64  errors = sbmlDoc.getNumErrors()
65 
66  seriousErrors = False
67 
68  numReadErr = 0
69  numReadWarn = 0
70  errMsgRead = ""
71 
72  if errors > 0:
73 
74  for i in range(errors):
75  severity = sbmlDoc.getError(i).getSeverity()
76  if (severity == libsbml.LIBSBML_SEV_ERROR) or (severity == libsbml.LIBSBML_SEV_FATAL):
77  seriousErrors = True
78  numReadErr += 1
79  else:
80  numReadWarn += 1
81 
82  errMsgRead = sbmlDoc.getErrorLog().toString()
83 
84  # If serious errors are encountered while reading an SBML document, it
85  # does not make sense to go on and do full consistency checking because
86  # the model may be nonsense in the first place.
87 
88  numCCErr = 0
89  numCCWarn = 0
90  errMsgCC = ""
91  skipCC = False
92  timeCC = 0.0
93 
94  if seriousErrors:
95  skipCC = True
96  errMsgRead += "Further consistency checking and validation aborted."
97  self.numinvalid += 1
98  else:
99  sbmlDoc.setConsistencyChecks(libsbml.LIBSBML_CAT_UNITS_CONSISTENCY, self.ucheck)
100  start = time.time()
101  failures = sbmlDoc.checkConsistency()
102  stop = time.time()
103  timeCC = (stop - start)*1000
104 
105 
106  if failures > 0:
107 
108  isinvalid = False
109  for i in range(failures):
110  severity = sbmlDoc.getError(i).getSeverity()
111  if (severity == libsbml.LIBSBML_SEV_ERROR) or (severity == libsbml.LIBSBML_SEV_FATAL):
112  numCCErr += 1
113  isinvalid = True
114  else:
115  numCCWarn += 1
116 
117  if isinvalid:
118  self.numinvalid += 1
119 
120  errMsgCC = sbmlDoc.getErrorLog().toString()
121 
122  #
123  # print results
124  #
125 
126  print(" filename : %s" % file)
127  print(" file size (byte) : %d" % (os.path.getsize(file)))
128  print(" read time (ms) : %f" % timeRead)
129 
130  if not skipCC :
131  print( " c-check time (ms) : %f" % timeCC)
132  else:
133  print( " c-check time (ms) : skipped")
134 
135  print( " validation error(s) : %d" % (numReadErr + numCCErr))
136  if not skipCC :
137  print( " (consistency error(s)): %d" % numCCErr)
138  else:
139  print( " (consistency error(s)): skipped")
140 
141  print( " validation warning(s) : %d" % (numReadWarn + numCCWarn))
142  if not skipCC :
143  print( " (consistency warning(s)): %d" % numCCWarn)
144  else:
145  print( " (consistency warning(s)): skipped")
146 
147  if errMsgRead or errMsgCC:
148  print()
149  print( "===== validation error/warning messages =====\n")
150  if errMsgRead :
151  print( errMsgRead)
152  if errMsgCC :
153  print( "*** consistency check ***\n")
154  print( errMsgCC)
155 
156 
157 def main (args):
158  """usage: validateSBML.py [-u] inputfile1 [inputfile2 ...]
159  -u skips unit consistency check
160  """
161  if len(args) < 2:
162  print( main.__doc__)
163  sys.exit(1)
164  elif (len(args) == 1) and (args[1] == "-u"):
165  print( main.__doc__)
166  sys.exit(1)
167 
168  enableUnitCCheck = True
169 
170  if args[1] == "-u":
171  enableUnitCCheck = False
172 
173  validator = validateSBML(enableUnitCCheck)
174 
175  fnum = 0
176 
177  for i in range(1,len(args)):
178  if args[i] == "-u":
179  continue
180  print( "---------------------------------------------------------------------------")
181  validator.validate(args[i])
182  fnum += 1
183 
184  numinvalid = validator.numinvalid
185 
186  print( "---------------------------------------------------------------------------")
187  print( "Validated %d files, %d valid files, %d invalid files" % (fnum, fnum - numinvalid, numinvalid))
188  if not enableUnitCCheck:
189  print( "(Unit consistency checks skipped)")
190 
191  if numinvalid > 0:
192  sys.exit(1)
193 
194 if __name__ == '__main__':
195  main(sys.argv)
196