1#!/usr/bin/env ruby
2#
3# @file    convertSBML.rb
4# @brief   Converts SBML documents between levels
5# @author  Alex Gutteridge (Ruby conversion of examples/c/convertSBML.c)
6# @author  Ben Bornstein
7# @author  Michael Hucka
8#
9#
10## <!--------------------------------------------------------------------------
11## This sample program is distributed under a different license than the rest
12## of libSBML.  This program uses the open-source MIT license, as follows:
13##
14## Copyright (c) 2013-2018 by the California Institute of Technology
15## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
16## and the University of Heidelberg (Germany), with support from the National
17## Institutes of Health (USA) under grant R01GM070923.  All rights reserved.
18##
19## Permission is hereby granted, free of charge, to any person obtaining a
20## copy of this software and associated documentation files (the "Software"),
21## to deal in the Software without restriction, including without limitation
22## the rights to use, copy, modify, merge, publish, distribute, sublicense,
23## and/or sell copies of the Software, and to permit persons to whom the
24## Software is furnished to do so, subject to the following conditions:
25##
26## The above copyright notice and this permission notice shall be included in
27## all copies or substantial portions of the Software.
28##
29## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35## DEALINGS IN THE SOFTWARE.
36##
37## Neither the name of the California Institute of Technology (Caltech), nor
38## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
39## of Heidelberg, nor the names of any contributors, may be used to endorse
40## or promote products derived from this software without specific prior
41## written permission.
42## ------------------------------------------------------------------------ -->
43
44require 'libSBML'
45
46latest_level   = LibSBML::SBMLDocument::getDefaultLevel
47latest_version = LibSBML::SBMLDocument::getDefaultVersion
48
49if ARGV.size != 2
50  puts "Usage: ruby convertSBML.rb input-filename output-filename"
51  puts "This program will attempt to convert a model either to"
52  puts "SBML Level #{latest_level} Version #{latest_version} (if the model is not already) or, if"
53  puts "the model is already expressed in Level #{latest_level} Version #{latest_version}, this"
54  puts "program will attempt to convert the model to Level 1 Version 2."
55  exit(1)
56end
57
58d = LibSBML::readSBML(ARGV[0])
59
60if d.getNumErrors > 0
61  puts "Encountered the following SBML error(s)"
62  d.printErrors
63  puts "Conversion skipped. Please correct the problems above first"
64  exit d.getNumErrors
65end
66
67success = false
68
69if d.getLevel < latest_level || d.getVersion < latest_version
70  puts "Attempting to convert model to SBML Level #{latest_level} Version #{latest_version}"
71  success = d.setLevelAndVersion(latest_level,latest_version)
72else
73  puts "Attempting to convert model to SBML Level 1 Version 2"
74  success = d.setLevelAndVersion(1,2)
75end
76
77if not success
78  puts "Unable to perform conversion due to the following:"
79  d.printErrors
80  puts "Conversion skipped.  Either libSBML does not (yet) have"
81  puts "ability to convert this model, or (automatic) conversion"
82  puts "is not possible in this case."
83elsif d.getNumErrors > 0
84  puts "Information may have been lost in conversion; but a valid model"
85  puts "was produced by the conversion.\nThe following information "
86  puts "was provided:"
87  d.printErrors
88  LibSBML::writeSBML(d, ARGV[1]);
89else
90  puts "Conversion completed."
91  LibSBML::writeSBML(d, ARGV[1])
92end
93
94