File: CompExample1.cs

package info (click to toggle)
libsbml 5.17.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 156,336 kB
  • sloc: cpp: 1,012,766; xml: 314,374; cs: 58,129; ansic: 54,053; python: 27,299; java: 27,152; makefile: 9,721; perl: 9,039; sh: 8,777; ruby: 4,760; php: 202; csh: 3
file content (213 lines) | stat: -rw-r--r-- 8,220 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 * @file    FlattenModel.cs
 * @brief   Flattens the comp code from the given SBML file.
 * @author  Sarah Keating
 * @author  Frank T. Bergmann
 *
 * <!--------------------------------------------------------------------------
 * This sample program is distributed under a different license than the rest
 * of libSBML.  This program uses the open-source MIT license, as follows:
 *
 * Copyright (c) 2013-2018 by the California Institute of Technology
 * (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
 * and the University of Heidelberg (Germany), with support from the National
 * Institutes of Health (USA) under grant R01GM070923.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Neither the name of the California Institute of Technology (Caltech), nor
 * of the European Bioinformatics Institute (EMBL-EBI), nor of the University
 * of Heidelberg, nor the names of any contributors, may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * ------------------------------------------------------------------------ -->
 */

using System;
using libsbmlcs;

namespace CompExamples
{
  public class CompExample1
  {
    public static int Main(string[] args)
    {
      int retval = 0;
      SBMLNamespaces sbmlns = new SBMLNamespaces(3, 1, "comp", 1);

      // create the document
      SBMLDocument document = new SBMLDocument(sbmlns);

      // create the Model
      Model model = document.createModel();

      // create a replacement parameter
      Parameter parameter = model.createParameter();
      parameter.setId("x");
      parameter.setConstant(true);

      // create a parameter to be a conversion factor
      Parameter param2 = model.createParameter();
      param2.setId("x_conv");
      param2.setMetaId("_110013");
      param2.setConstant(true);

      // create a parameter to be a conversion factor
      Parameter param3 = model.createParameter();
      param3.setId("lcf");
      param3.setConstant(true);

      // Convert parameter to the plugin version so we can add the new attributes and replacements to it.
      CompSBasePlugin splugin = (CompSBasePlugin) (parameter.getPlugin("comp"));

      // Add a replaced element.
      ReplacedElement rep1 = splugin.createReplacedElement();
      int rv = rep1.setSubmodelRef("submod1");
      rv = rep1.setConversionFactor("x_conv");
      rv = rep1.setIdRef("param1");

      // Add a second replaced element in a different way.
      ReplacedElement rep2 = new ReplacedElement();
      rv = rep2.setSubmodelRef("submod2");
      rv = rep2.setDeletion("del1");
      rv = splugin.addReplacedElement(rep2);

      //Now create a replaced element that points into a submodel.
      rep2.unsetDeletion();
      rep2.setIdRef("submod2");
      SBaseRef sbr5 = rep2.createSBaseRef();
      sbr5.setIdRef("submodelG");
      SBaseRef sbr6 = sbr5.createSBaseRef();
      sbr6.setIdRef("buriedElement");
      splugin.addReplacedElement(rep2);


      // Create a submodel
      CompModelPlugin mplugin = (CompModelPlugin) (model.getPlugin("comp"));
      CompSBMLDocumentPlugin compdoc = (CompSBMLDocumentPlugin) (document.getPlugin("comp"));
      compdoc.setRequired(true);

      ModelDefinition moddef1 = compdoc.createModelDefinition();
      moddef1.setId("Mod1");
      Parameter m1param1 = moddef1.createParameter();
      m1param1.setId("param1");
      m1param1.setConstant(true);
      Parameter m1param2 = moddef1.createParameter();
      m1param2.setId("param2");
      m1param2.setConstant(false);
      m1param2.setValue(3.2);

      ModelDefinition moddef2 = new ModelDefinition();
      moddef2.setId("Mod2");
      Parameter subparam2 = moddef2.createParameter();
      subparam2.setId("subparam2");
      subparam2.setConstant(false);
      compdoc.addModelDefinition(moddef2);


      ExternalModelDefinition extmod1 = compdoc.createExternalModelDefinition();
      extmod1.setId("ExtMod1");
      extmod1.setSource("urn:miriam:biomodels.db:BIOMD0000000127");

      ExternalModelDefinition extmod2 = new ExternalModelDefinition();
      extmod2.setId("ExtMod2");
      extmod2.setSource("otherfile.xml");
      extmod2.setModelRef("modelnamethere");
      extmod2.setMd5("406022s908ge74sklj");
      compdoc.addExternalModelDefinition(extmod2);

      Submodel submod1 = mplugin.createSubmodel();
      submod1.setId("submod1");
      submod1.setModelRef("Mod1");
      Deletion del1 = submod1.createDeletion();
      del1.setId("deletionA");
      del1.setIdRef("param2");

      Submodel submod2 = new Submodel();
      submod2.setId("submod2");
      submod2.setModelRef("ExtMod1");
      submod2.setSubstanceConversionFactor("subcf");
      submod2.setTimeConversionFactor("tcf");
      submod2.setExtentConversionFactor("xtf");
      Deletion del2 = new Deletion();
      del2.setId("deletionB");
      del2.setMetaIdRef("_0010110");
      rv = submod2.addDeletion(del2);
      del2.setId("deletionC");
      del2.unsetMetaIdRef();
      del2.setPortRef("port2");
      rv = submod2.addDeletion(del2);
      del2.unsetId();
      del2.unsetPortRef();
      del2.setUnitRef("mph");
      rv = submod2.addDeletion(del2);
      del2.unsetUnitRef();
      del2.setIdRef("submodG");
      SBaseRef sbr = del2.createSBaseRef();
      sbr.setIdRef("element5");
      rv = submod2.addDeletion(del2);
      Deletion del3 = new Deletion();
      del3.setIdRef("submodG");
      SBaseRef sbr2 = new SBaseRef();
      sbr2.setIdRef("subsubmodQ");
      SBaseRef subsbr = sbr2.createSBaseRef();
      subsbr.setPortRef("toBdel");
      del3.setSBaseRef(sbr2);
      submod2.addDeletion(del3);
      mplugin.addSubmodel(submod2);

      Port port1 = mplugin.createPort();
      port1.setId("port1");
      port1.setMetaIdRef("_110013");
      Port port2 = new Port();
      port2.setId("port2");
      port2.setIdRef("x");
      mplugin.addPort(port2);
      port2.setId("port3");
      port2.setIdRef("submod2");
      port2.setSBaseRef(sbr2);
      mplugin.addPort(port2);

      libsbml.writeSBMLToFile(document, "comp_example1.xml");
      document = libsbml.readSBMLFromFile("comp_example1.xml");
      if (document == null)
      {
        Console.WriteLine("Error reading back in file.");
        retval = -1;
      }
      else
      {
        document.setConsistencyChecks(libsbml.LIBSBML_CAT_UNITS_CONSISTENCY, false);
        document.checkConsistency();
        if (document.getErrorLog().getNumFailsWithSeverity(2) > 0 ||
            document.getErrorLog().getNumFailsWithSeverity(3) > 0)
        {
          OStringStream stream = new OStringStream();
          document.printErrors(stream);
          Console.WriteLine("Errors encoutered when round-tripping  SBML file: \n" +
                            stream.str());
          retval = -1;
        }
        libsbml.writeSBMLToFile(document, "comp_example1_rt.xml");
      }

      return retval;
    }
  }
}