1#
2# @file    convertSBML.R
3# @brief   Converts SBML documents between levels
4# @author  Frank Bergmann
5#
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#
42# Usage: R --slave -f convertSBML.R --args <input-filename> <output-filename>
43#
44#
45
46library(libSBML)
47
48args <- commandArgs(trailingOnly = TRUE)
49
50latestLevel   = SBMLDocument_getDefaultLevel();
51latestVersion = SBMLDocument_getDefaultVersion();
52
53if (length(args) != 2) {
54  stop(
55		paste(
56         "Usage: convertSBML <input-filename> <output-filename>\n",
57		 "This program will attempt to convert a model either to\n",
58         "SBML Level ",latestLevel," Version ",latestVersion," (if the model is not already) or, if",
59	     "the model is already expressed in Level ",latestLevel," Version ",latestVersion,", this\n",
60	     "program will attempt to convert the model to Level 1 Version 2.\n",
61		 sep = "")
62      );
63}
64
65d      = readSBML(args[1]);
66errors = SBMLErrorLog_getNumFailsWithSeverity(
67			SBMLDocument_getErrorLog(d),
68			enumToInteger("LIBSBML_SEV_ERROR", "_XMLErrorSeverity_t")
69		 );
70
71if (errors > 0) {
72  cat("Encountered the following SBML error(s):\n");
73  SBMLDocument_printErrors(d);
74  stop("Conversion skipped.  Please correct the problems above first.\n");
75} else {
76  olevel   = SBase_getLevel(d);
77  oversion = SBase_getVersion(d);
78
79  if (olevel < latestLevel || oversion < latestVersion) {
80    cat("Attempting to convert model to SBML Level ",latestLevel," Version ",latestVersion,".\n");
81    success = SBMLDocument_setLevelAndVersion(d, latestLevel, latestVersion);
82  } else {
83    cat("Attempting to convert model to SBML Level 1 Version 2.\n");
84    success = SBMLDocument_setLevelAndVersion(d, 1, 2);
85  }
86
87  errors = SBMLErrorLog_getNumFailsWithSeverity(
88			SBMLDocument_getErrorLog(d),
89			enumToInteger("LIBSBML_SEV_ERROR", "_XMLErrorSeverity_t")
90		 );
91
92  if (!success) {
93    cat("Unable to perform conversion due to the following:\n");
94    SBMLDocument_printErrors(d);
95
96    cat("Conversion skipped.  Either libSBML does not (yet) have\n");
97    cat("ability to convert this model, or (automatic) conversion\n");
98    cat("is not possible in this case.\n");
99  } else if (errors > 0) {
100    cat("Information may have been lost in conversion; but a valid model ");
101    cat("was produced by the conversion.\nThe following information ");
102    cat("was provided:\n");
103    SBMLDocument_printErrors(d);
104    writeSBML(d, args[2]);
105  } else {
106    cat("Conversion completed.\n");
107    writeSBML(d, args[2]);
108  }
109}
110
111q(status=errors);
112