libSBML C++ API  5.20.4
SBMLReader Class Reference

Detailed Description

File and text-string SBML reader.

This class of objects is defined by libSBML only and has no direct equivalent in terms of SBML components. This class is not prescribed by the SBML specifications, although it is used to implement features defined in SBML.

The SBMLReader class provides the main interface for reading SBML content from files and strings. The methods for reading SBML all return an SBMLDocument object representing the results. In the case of failures (such as if the SBML contains errors or a file cannot be read), the errors will be recorded with the SBMLErrorLog object kept in the SBMLDocument returned by SBMLReader. Consequently, immediately after calling a method on SBMLReader, callers should always check for errors and warnings using the methods for this purpose provided by SBMLDocument.

For convenience as well as easy access from other languages besides C++, this file also defines two global functions, @sbmlglobalfunction{readSBML, String} and @sbmlglobalfunction{readSBMLFromString, String}. They are equivalent to creating an SBMLReader object and then calling the SBMLReader::readSBML() and SBMLReader::readSBMLFromString() methods, respectively.

Support for reading compressed files

LibSBML provides support for reading (as well as writing) compressed SBML files. The process is transparent to the calling application—the application does not need to do anything deliberate to invoke the functionality. If a given SBML filename ends with an extension for the gzip, zip or bzip2 compression formats (respectively, .gz, .zip, or .bz2), then the methods SBMLReader::readSBML() and SBMLWriter::writeSBML() will automatically decompress and compress the file while reading and writing it. If the filename has no such extension, it will be read and written uncompressed as normal.

The compression feature requires that the zlib (for gzip and zip formats) and/or bzip2 (for bzip2 format) be available on the system running libSBML, and that libSBML was configured with their support compiled-in. Please see the libSBML installation instructions for more information about this. The methods hasZlib() and hasBzip2() can be used by an application to query at run-time whether support for the compression libraries is available in the present copy of libSBML.

Support for compression is not mandated by the SBML standard, but applications may find it helpful, particularly when large SBML models are being communicated across data links of limited bandwidth.

Examples
getAllElementsWithNotes.cpp, printAnnotation.cpp, printNotes.cpp, readSBML.cpp, renameSId.cpp, setIdFromNames.cpp, setNamesFromIds.cpp, unsetAnnotation.cpp, unsetNotes.cpp, and validateSBML.cpp.

Public Member Functions

SBMLDocumentreadSBML (const std::string &filename)
 Reads an SBML document from the given file. More...
 
SBMLDocumentreadSBMLFromFile (const std::string &filename)
 Reads an SBML document from the given file. More...
 
SBMLDocumentreadSBMLFromString (const std::string &xml)
 Reads an SBML document from a text string. More...
 
 SBMLReader ()
 Creates a new SBMLReader object and returns it. More...
 
virtual ~SBMLReader ()
 Destroys this SBMLReader. More...
 

Static Public Member Functions

static bool hasBzip2 ()
 Static method; returns true if this copy of libSBML supports bzip2 format compression. More...
 
static bool hasZlib ()
 Static method; returns true if this copy of libSBML supports gzip and zip format compression. More...
 

Constructor & Destructor Documentation

◆ SBMLReader()

SBMLReader::SBMLReader ( )

Creates a new SBMLReader object and returns it.

The libSBML SBMLReader object offers methods for reading SBML in XML form from files and text strings.

◆ ~SBMLReader()

SBMLReader::~SBMLReader ( )
virtual

Destroys this SBMLReader.

Member Function Documentation

◆ hasBzip2()

bool SBMLReader::hasBzip2 ( )
static

Static method; returns true if this copy of libSBML supports bzip2 format compression.

Returns
true if libSBML is linked with the bzip2 libraries, false otherwise.
See also
hasZlib()

◆ hasZlib()

bool SBMLReader::hasZlib ( )
static

Static method; returns true if this copy of libSBML supports gzip and zip format compression.

Returns
true if libSBML has been linked with the zlib library, false otherwise.
See also
hasBzip2()

◆ readSBML()

SBMLDocument * SBMLReader::readSBML ( const std::string &  filename)

Reads an SBML document from the given file.

If the file named filename does not exist or its content is not valid SBML, one or more errors will be logged with the SBMLDocument object returned by this method. Callers can use the methods on SBMLDocument such as , SBMLDocument::getNumErrors() and SBMLDocument::getError() to get the errors. The object returned by SBMLDocument::getError() is an SBMLError object, and it has methods to get the error code, category, and severity level of the problem, as well as a textual description of the problem. The possible severity levels range from informational messages to fatal errors; see the documentation for SBMLError for more information.

If the file filename could not be read, the file-reading error will appear first. The error code (a value drawn from the enumeration XMLErrorCode_t) can provide a clue about what happened. For example, a file might be unreadable (either because it does not actually exist or because the user does not have the necessary access privileges to read it) or some sort of file operation error may have been reported by the underlying operating system. Callers can check for these situations using a program fragment such as the following:

SBMLReader reader;
SBMLDocument* doc = reader.readSBMLFromFile(filename);
if (doc->getNumErrors() > 0)
{
{
// Handle case of unreadable file here.
}
{
// Handle case of other file operation error here.
}
else
{
// Handle other cases -- see error codes defined in XMLErrorCode_t
// for other possible cases to check.
}
}
@ XMLFileOperationError
Definition: XMLError.h:379
@ XMLFileUnreadable
Definition: XMLError.h:375
Definition: SBMLDocument.h:351
unsigned int getNumErrors() const
Returns the number of errors or warnings encountered during parsing, consistency checking,...
Definition: SBMLDocument.cpp:1169
const SBMLError * getError(unsigned int n) const
Returns the nth error or warning encountered during parsing, consistency checking,...
Definition: SBMLDocument.cpp:1152
Definition: SBMLReader.h:343
SBMLDocument * readSBMLFromFile(const std::string &filename)
Reads an SBML document from the given file.
Definition: SBMLReader.cpp:122
unsigned int getErrorId() const
Returns the identifier of this error.
Definition: XMLError.cpp:504
If the given filename ends with the suffix ".gz" (for example, "myfile.xml.gz"), the file is assumed to be compressed in gzip format and will be automatically decompressed upon reading. Similarly, if the given filename ends with ".zip" or ".bz2", the file is assumed to be compressed in zip or bzip2 format (respectively). Files whose names lack these suffixes will be read uncompressed. Note that if the file is in zip format but the archive contains more than one file, only the first file in the archive will be read and the rest ignored.
To read a gzip/zip file, libSBML needs to be configured and linked with the zlib library at compile time. It also needs to be linked with the bzip2 library to read files in bzip2 format. (Both of these are the default configurations for libSBML.) Errors about unreadable files will be logged if a compressed filename is given and libSBML was not linked with the corresponding required library.

This method is identical to SBMLReader::readSBMLFromFile().

Parameters
filenamethe name or full pathname of the file to be read.
Returns
a pointer to the SBMLDocument object created from the SBML content in filename.
Note
LibSBML versions 2.x and later versions behave differently in error handling in several respects. One difference is how early some errors are caught and whether libSBML continues processing a file in the face of some early errors. In general, libSBML versions after 2.x stop parsing SBML inputs sooner than libSBML version 2.x in the face of XML errors, because the errors may invalidate any further SBML content. For example, a missing XML declaration at the beginning of the file was ignored by libSBML 2.x but in version 3.x and later, it will cause libSBML to stop parsing the rest of the input altogether. While this behavior may seem more severe and intolerant, it was necessary in order to provide uniform behavior regardless of which underlying XML parser (Expat, Xerces, libxml2) is being used by libSBML. The XML parsers themselves behave differently in their error reporting, and sometimes libSBML has to resort to the lowest common denominator.
See also
readSBMLFromString()
SBMLError
SBMLDocument
Examples
getAllElementsWithNotes.cpp, printAnnotation.cpp, printNotes.cpp, readSBML.cpp, renameSId.cpp, setIdFromNames.cpp, setNamesFromIds.cpp, unsetAnnotation.cpp, unsetNotes.cpp, and validateSBML.cpp.

◆ readSBMLFromFile()

SBMLDocument * SBMLReader::readSBMLFromFile ( const std::string &  filename)

Reads an SBML document from the given file.

If the file named filename does not exist or its content is not valid SBML, one or more errors will be logged with the SBMLDocument object returned by this method. Callers can use the methods on SBMLDocument such as , SBMLDocument::getNumErrors() and SBMLDocument::getError() to get the errors. The object returned by SBMLDocument::getError() is an SBMLError object, and it has methods to get the error code, category, and severity level of the problem, as well as a textual description of the problem. The possible severity levels range from informational messages to fatal errors; see the documentation for SBMLError for more information.

If the file filename could not be read, the file-reading error will appear first. The error code (a value drawn from the enumeration XMLErrorCode_t) can provide a clue about what happened. For example, a file might be unreadable (either because it does not actually exist or because the user does not have the necessary access privileges to read it) or some sort of file operation error may have been reported by the underlying operating system. Callers can check for these situations using a program fragment such as the following:

SBMLReader reader;
SBMLDocument* doc = reader.readSBMLFromFile(filename);
if (doc->getNumErrors() > 0)
{
{
// Handle case of unreadable file here.
}
{
// Handle case of other file operation error here.
}
else
{
// Handle other cases -- see error codes defined in XMLErrorCode_t
// for other possible cases to check.
}
}
If the given filename ends with the suffix ".gz" (for example, "myfile.xml.gz"), the file is assumed to be compressed in gzip format and will be automatically decompressed upon reading. Similarly, if the given filename ends with ".zip" or ".bz2", the file is assumed to be compressed in zip or bzip2 format (respectively). Files whose names lack these suffixes will be read uncompressed. Note that if the file is in zip format but the archive contains more than one file, only the first file in the archive will be read and the rest ignored.
To read a gzip/zip file, libSBML needs to be configured and linked with the zlib library at compile time. It also needs to be linked with the bzip2 library to read files in bzip2 format. (Both of these are the default configurations for libSBML.) Errors about unreadable files will be logged if a compressed filename is given and libSBML was not linked with the corresponding required library.

This method is identical to SBMLReader::readSBML().

Parameters
filenamethe name or full pathname of the file to be read.
Returns
a pointer to the SBMLDocument object created from the SBML content in filename.
Note
LibSBML versions 2.x and later versions behave differently in error handling in several respects. One difference is how early some errors are caught and whether libSBML continues processing a file in the face of some early errors. In general, libSBML versions after 2.x stop parsing SBML inputs sooner than libSBML version 2.x in the face of XML errors, because the errors may invalidate any further SBML content. For example, a missing XML declaration at the beginning of the file was ignored by libSBML 2.x but in version 3.x and later, it will cause libSBML to stop parsing the rest of the input altogether. While this behavior may seem more severe and intolerant, it was necessary in order to provide uniform behavior regardless of which underlying XML parser (Expat, Xerces, libxml2) is being used by libSBML. The XML parsers themselves behave differently in their error reporting, and sometimes libSBML has to resort to the lowest common denominator.
See also
readSBMLFromString()
SBMLError
SBMLDocument

◆ readSBMLFromString()

SBMLDocument * SBMLReader::readSBMLFromString ( const std::string &  xml)

Reads an SBML document from a text string.

This method is flexible with respect to the presence of an XML declaration at the beginning of the string. In particular, if the string in xml does not begin with the XML declaration

<?xml version='1.0' encoding='UTF-8'?>

then this method will automatically prepend the declaration to xml.

This method will log a fatal error if the content given in the parameter xml is not in SBML format. See the method documentation for SBMLReader::readSBML() for an example of code for testing the returned error code.

Parameters
xmla string containing a full SBML model.
Returns
a pointer to the SBMLDocument created from the SBML content, or a null pointer if xml is NULL.
Note
When using this method to read an SBMLDocument that uses the SBML Level 3 Hierarchical Model Composition package (comp) the document location cannot be set automatically. Thus, if the model contains references to ExternalModelDefinition objects, it will be necessary to manually set the document URI location (SBMLDocument::setLocationURI() ) in order to facilitate resolving these models.
See also
SBMLReader::readSBML()