1#
2# @file    unsetNotes.R
3# @brief   unset notes for each element
4# @author  Frank Bergmann
5#
6# <!--------------------------------------------------------------------------
7# This sample program is distributed under a different license than the rest
8# of libSBML.  This program uses the open-source MIT license, as follows:
9#
10# Copyright (c) 2013-2018 by the California Institute of Technology
11# (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
12# and the University of Heidelberg (Germany), with support from the National
13# Institutes of Health (USA) under grant R01GM070923.  All rights reserved.
14#
15# Permission is hereby granted, free of charge, to any person obtaining a
16# copy of this software and associated documentation files (the "Software"),
17# to deal in the Software without restriction, including without limitation
18# the rights to use, copy, modify, merge, publish, distribute, sublicense,
19# and/or sell copies of the Software, and to permit persons to whom the
20# Software is furnished to do so, subject to the following conditions:
21#
22# The above copyright notice and this permission notice shall be included in
23# all copies or substantial portions of the Software.
24#
25# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31# DEALINGS IN THE SOFTWARE.
32#
33# Neither the name of the California Institute of Technology (Caltech), nor
34# of the European Bioinformatics Institute (EMBL-EBI), nor of the University
35# of Heidelberg, nor the names of any contributors, may be used to endorse
36# or promote products derived from this software without specific prior
37# written permission.
38# ------------------------------------------------------------------------ -->
39#
40# Usage: R --slave -f unsetNotes.R --args <full path to input file> <full path to output file>
41#
42library(libSBML)
43
44
45unsetNotes <- function(sb, id ) {
46
47  if (!is.null(sb) && SBase_isSetNotes(sb)) {
48    SBase_unsetNotes(sb)
49  }
50}
51
52args <- commandArgs(trailingOnly = TRUE)
53
54
55if (length(args) != 2)
56{
57  stop("Usage: unsetNotes input-filename output-filename\n");
58}
59
60filename  = args[1];
61document  = readSBML(filename);
62
63errors = SBMLErrorLog_getNumFailsWithSeverity(
64			SBMLDocument_getErrorLog(document),
65			enumToInteger("LIBSBML_SEV_ERROR", "_XMLErrorSeverity_t")
66		 );
67
68cat("\n",filename,"\n\n");
69
70if(errors > 0) {
71  SBMLDocument_printErrors(document);
72  q(status=errors);
73}
74
75
76# Model
77
78m = SBMLDocument_getModel(document);
79unsetNotes(m, Model_getId(m));
80
81for(i in seq_len(Model_getNumReactions(m))) {
82
83  re = Model_getReaction( m, i-1);
84  unsetNotes(re, Reaction_getId(re));
85
86  # SpeciesReference (Reactant)
87
88  for(j in seq_len(Reaction_getNumReactants( re))) {
89    rt =  Reaction_getReactant(re, j-1);
90
91    if (SBase_isSetNotes( rt)) cat("   ");
92    unsetNotes(rt, SimpleSpeciesReference_getSpecies( rt ) );
93  }
94
95  # SpeciesReference (Product)
96
97  for(j in seq_len(Reaction_getNumProducts( re ))) {
98    rt = Reaction_getProduct( re, j-1);
99    if (SBase_isSetNotes( rt)) cat("   ");
100    unsetNotes(rt, SimpleSpeciesReference_getSpecies( rt ) );
101  }
102
103  # ModifierSpeciesReference (Modifiers)
104
105  for(j in seq_len(Reaction_getNumModifiers( re )))  {
106    md = Reaction_getModifier(re, j-1);
107    if (SBase_isSetNotes( md)) cat("   ");
108    unsetNotes(md, SimpleSpeciesReference_getSpecies( md ) );
109  }
110
111  # KineticLaw
112
113  if(Reaction_isSetKineticLaw( re )) {
114    kl = Reaction_getKineticLaw( re );
115    if (SBase_isSetNotes( kl)) cat("   ");
116    unsetNotes(kl, "");
117
118    # Parameter
119
120    for(j in seq_len(KineticLaw_getNumParameters( kl ))) {
121      pa = KineticLaw_getParameter( kl, j-1);
122      if (SBase_isSetNotes( pa)) cat("   ");
123      unsetNotes(pa, Parameter_getId(pa));
124    }
125  }
126
127}
128
129# Species
130
131for(i in seq_len(Model_getNumSpecies(m))) {
132  sp = Model_getSpecies(m, i-1);
133  unsetNotes(sp, Species_getId(sp));
134}
135
136# Compartments
137
138for(i in seq_len(Model_getNumCompartments( m ))) {
139  sp = Model_getCompartment(m, i-1);
140  unsetNotes(sp, Compartment_getId(sp));
141}
142
143# FunctionDefinition
144
145for(i in seq_len(Model_getNumFunctionDefinitions(m))) {
146  sp = Model_getFunctionDefinition(m, i-1);
147  unsetNotes(sp, FunctionDefinition_getId(sp));
148}
149
150# UnitDefinition
151
152for(i in seq_len(Model_getNumUnitDefinitions(m))) {
153  sp = Model_getUnitDefinition( m, i-1);
154  unsetNotes(sp, UnitDefinition_getId(sp));
155}
156
157# Parameter
158for(i in seq_len(Model_getNumParameters( m ))) {
159  sp = Model_getParameter( m, i-1);
160  unsetNotes(sp, Parameter_getId(sp));
161}
162
163# Rule
164
165for(i in seq_len(Model_getNumRules( m ))) {
166  sp = Model_getRule(m, i-1);
167  unsetNotes(sp, "");
168}
169
170# InitialAssignment
171
172for(i in seq_len(Model_getNumInitialAssignments(m))) {
173  sp = Model_getInitialAssignment(m, i-1);
174  unsetNotes(sp, "");
175}
176
177# Event
178
179for(i in seq_len(Model_getNumEvents(m))) {
180  sp = Model_getEvent(m, i-1);
181  unsetNotes(sp, Event_getId(sp));
182
183  # Trigger
184  if(Event_isSetTrigger( sp )) {
185    tg = Event_getTrigger(sp);
186    if (SBase_isSetNotes(  tg)) cat( "   " );
187    unsetNotes(tg, "");
188  }
189
190  # Delay
191
192  if(Event_isSetDelay(sp))  {
193    dl = Event_getDelay(sp);
194    if (SBase_isSetNotes(  dl)) cat( "   " );
195    unsetNotes( dl, "");
196  }
197
198  # EventAssignment
199
200  for(j in seq_len(Event_getNumEventAssignments(sp))) {
201    ea = Event_getEventAssignment(sp, j-1);
202    if (SBase_isSetNotes(  ea)) cat( "   " );
203    unsetNotes(ea, "");
204  }
205}
206
207# SpeciesType
208
209for(i in seq_len(Model_getNumSpeciesTypes(m))) {
210  sp = Model_getSpeciesType(m, i-1);
211  unsetNotes(sp, SpeciesType_getId(sp));
212}
213
214# Constraints
215
216for(i in seq_len(Model_getNumConstraints(m))) {
217  sp = Model_getConstraint(m, i-1);
218  unsetNotes(sp, "");
219}
220
221writeSBML(document, args[2])
222
223q(status=errors);
224