Model flattening example.
42 import sys, getopt, os.path
49 def check(value, message):
50 """If 'value' is None, prints an error message constructed using 51 'message' and then exits with status code 1. If 'value' is an integer, 52 it assumes it is a libSBML return status code. If the code value is 53 LIBSBML_OPERATION_SUCCESS, returns without further action; if it is not, 54 prints an error message constructed using 'message' along with text from 55 libSBML explaining the meaning of the code, and exits with status code 1. 58 raise SystemExit(
'LibSBML returned a null value trying to ' + message +
'.')
59 elif type(value)
is int:
60 if value == LIBSBML_OPERATION_SUCCESS:
63 err_msg =
'Error encountered trying to ' + message +
'.' \
64 +
'LibSBML returned error code ' + str(value) +
': "' \
66 raise SystemExit(err_msg)
74 """Usage: flattenModel.py [-p] MODEL_FILE OUTPUT_FILE 76 -p (Optional) list unused ports 82 if not SBMLExtensionRegistry.isPackageEnabled(
"comp"):
83 err_msg =
'This copy of libSBML does not contain the "comp" extension.' \
84 +
'Unable to proceed with flattening the model.' 85 raise SystemExit(err_msg)
90 opts, args = getopt.getopt(argv[1:],
"p")
92 raise SystemExit(main.__doc__)
95 raise SystemExit(main.__doc__)
97 leave_ports =
'-p' in opts
101 if not os.path.exists(input_file):
102 raise SystemExit(
'%s : No such file.' % input_file)
107 check(reader,
'create an SBMLReader object.')
108 sbmldoc = reader.readSBML(input_file)
109 check(sbmldoc,
'create an SBMLDocument object from file input')
111 if sbmldoc.getNumErrors() > 0:
112 if sbmldoc.getError(0).getErrorId() == XMLFileUnreadable:
114 sbmldoc.printErrors()
115 elif sbmldoc.getError(0).getErrorId() == XMLFileOperationError:
117 sbmldoc.printErrors()
120 sbmldoc.printErrors()
126 props = ConversionProperties()
127 props.addOption(
"flatten comp",
True)
128 props.addOption(
"leave_ports", leave_ports)
132 result = sbmldoc.convert(props)
133 if result != LIBSBML_OPERATION_SUCCESS:
134 sbmldoc.printErrors()
135 raise SystemExit(
"Conversion failed... ("+ str(result) +
")")
140 check(writer,
'create an SBMLWriter object.')
141 writer.writeSBML(sbmldoc, output_file)
142 print(
"Flattened model written to %s" % output_file)
144 if __name__ ==
'__main__':