1 /**
2  * @file    printAnnotation.java
3  * @brief   Prints annotation strings for each element
4  * @author  Akira Funahashi (translated from libSBML C++ examples (using other Java examples provided by Nicolas Rodriguez))
5  * @author  Akiya Jouraku
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 
43 import org.sbml.libsbml.Compartment;
44 import org.sbml.libsbml.Constraint;
45 import org.sbml.libsbml.Event;
46 import org.sbml.libsbml.Delay;
47 import org.sbml.libsbml.InitialAssignment;
48 import org.sbml.libsbml.ModifierSpeciesReference;
49 import org.sbml.libsbml.Parameter;
50 import org.sbml.libsbml.SBase;
51 import org.sbml.libsbml.Species;
52 import org.sbml.libsbml.SpeciesReference;
53 import org.sbml.libsbml.SpeciesType;
54 import org.sbml.libsbml.Trigger;
55 import org.sbml.libsbml.EventAssignment;
56 import org.sbml.libsbml.FunctionDefinition;
57 import org.sbml.libsbml.KineticLaw;
58 import org.sbml.libsbml.Model;
59 import org.sbml.libsbml.Reaction;
60 import org.sbml.libsbml.Rule;
61 import org.sbml.libsbml.SBMLDocument;
62 import org.sbml.libsbml.SBMLReader;
63 import org.sbml.libsbml.UnitDefinition;
64 
65 
66 public class printAnnotation
67 {
main(String[] args)68   public static void main (String[] args)
69   {
70     if (args.length != 1)
71     {
72       println("Usage: java printAnnotation filename");
73       System.exit(1);
74     }
75 
76     String filename       = args[0];
77     SBMLDocument document;
78     SBMLReader reader     = new SBMLReader();
79 
80     document = reader.readSBML(filename);
81     long errors = document.getNumErrors();
82     println("\nfilename: " + filename + "\n");
83     if (errors > 0)
84     {
85       document.printErrors();
86       System.exit((int)errors);
87     }
88 
89     /* Model */
90     Model m = document.getModel();
91     printAnnotation(m);
92 
93     for(int i=0; i < m.getNumReactions(); i++)
94     {
95       Reaction re = m.getReaction(i);
96       printAnnotation(re);
97 
98       /* SpeciesReference (Reactant) */
99       for(int j=0; j < re.getNumReactants(); j++)
100       {
101         SpeciesReference rt = re.getReactant(j);
102         if (rt.isSetAnnotation()) print("   ");
103         printAnnotation(rt, (rt.isSetSpecies() ? rt.getSpecies() : ""));
104       }
105 
106       /* SpeciesReference (Product) */
107       for(int j=0; j < re.getNumProducts(); j++)
108       {
109         SpeciesReference rt = re.getProduct(j);
110         if (rt.isSetAnnotation()) print("   ");
111         printAnnotation(rt, (rt.isSetSpecies() ? rt.getSpecies() : "") );
112       }
113 
114       /* ModifierSpeciesReference (Modifier) */
115       for(int j=0; j < re.getNumModifiers(); j++)
116       {
117         ModifierSpeciesReference md = re.getModifier(j);
118         if (md.isSetAnnotation()) print("   ");
119         printAnnotation(md, (md.isSetSpecies() ? md.getSpecies() : "") );
120       }
121 
122       /* KineticLaw */
123       if(re.isSetKineticLaw())
124       {
125         KineticLaw kl = re.getKineticLaw();
126         if (kl.isSetAnnotation()) print("   ");
127         printAnnotation(kl);
128 
129         /* Parameter */
130         for(int j=0; j < kl.getNumParameters(); j++)
131         {
132           Parameter pa = kl.getParameter(j);
133           if (pa.isSetAnnotation()) print("      ");
134           printAnnotation(pa);
135         }
136       }
137     }
138 
139     /* Species */
140     for(int i=0; i < m.getNumSpecies(); i++)
141     {
142       Species sp = m.getSpecies(i);
143       printAnnotation(sp);
144     }
145 
146     /* Compartment */
147     for(int i=0; i < m.getNumCompartments(); i++)
148     {
149       Compartment sp = m.getCompartment(i);
150       printAnnotation(sp);
151     }
152 
153     /* FunctionDefinition */
154     for(int i=0; i < m.getNumFunctionDefinitions(); i++)
155     {
156       FunctionDefinition sp = m.getFunctionDefinition(i);
157       printAnnotation(sp);
158     }
159 
160     /* UnitDefinition */
161     for(int i=0; i < m.getNumUnitDefinitions(); i++)
162     {
163       UnitDefinition sp = m.getUnitDefinition(i);
164       printAnnotation(sp);
165     }
166 
167     /* Parameter */
168     for(int i=0; i < m.getNumParameters(); i++)
169     {
170       Parameter sp = m.getParameter(i);
171       printAnnotation(sp);
172     }
173 
174     /* Rule */
175     for(int i=0; i < m.getNumRules(); i++)
176     {
177       Rule sp = m.getRule(i);
178       printAnnotation(sp);
179     }
180 
181     /* InitialAssignment */
182     for(int i=0; i < m.getNumInitialAssignments(); i++)
183     {
184       InitialAssignment sp = m.getInitialAssignment(i);
185       printAnnotation(sp);
186     }
187 
188     /* Event */
189     for(int i=0; i < m.getNumEvents(); i++)
190     {
191       Event sp = m.getEvent(i);
192       printAnnotation(sp);
193 
194       /* Trigger */
195       if(sp.isSetTrigger())
196       {
197         Trigger tg = sp.getTrigger();
198         if (tg.isSetAnnotation()) print("   ");
199         printAnnotation(tg);
200       }
201 
202       /* Delay */
203       if(sp.isSetDelay())
204       {
205         Delay dl = sp.getDelay();
206         if (dl.isSetAnnotation()) print("   ");
207         printAnnotation(dl);
208       }
209 
210       /* EventAssignment */
211       for(int j=0; j < sp.getNumEventAssignments(); j++)
212       {
213         EventAssignment ea = sp.getEventAssignment(j);
214         if (ea.isSetAnnotation()) print("   ");
215         printAnnotation(ea);
216       }
217     }
218 
219     /* SpeciesType */
220     for(int i=0; i < m.getNumSpeciesTypes(); i++)
221     {
222       SpeciesType sp = m.getSpeciesType(i);
223       printAnnotation(sp);
224     }
225 
226     /* Constraint */
227     for(int i=0; i < m.getNumConstraints(); i++)
228     {
229       Constraint sp = m.getConstraint(i);
230       printAnnotation(sp);
231     }
232 
233     System.exit((int)errors);
234   }
235 
printAnnotation(SBase sb, String id)236   static void printAnnotation(SBase sb, String id)
237   {
238     if (!sb.isSetAnnotation()) return;
239 
240     String pid = id;
241 
242     if (pid == "" && sb.isSetId())
243     {
244       pid = sb.getId();
245     }
246     println("----- " + sb.getElementName() + " (" + pid
247          + ") annotation -----");
248     println(sb.getAnnotationString());
249     println("");
250   }
251 
printAnnotation(SBase sb)252   static void printAnnotation(SBase sb)
253   {
254     printAnnotation(sb, "");
255   }
256 
print(String msg)257   static void print (String msg)
258   {
259     System.out.print(msg);
260   }
261 
262 
println(String msg)263   static void println (String msg)
264   {
265     System.out.println(msg);
266   }
267 
268 
269   /**
270    * Loads the SWIG-generated libSBML Java module when this class is
271    * loaded, or reports a sensible diagnostic message about why it failed.
272    */
273   static
274   {
275     try
276     {
277       System.loadLibrary("sbmlj");
278       // For extra safety, check that the jar file is in the classpath.
279       Class.forName("org.sbml.libsbml.libsbml");
280     }
281     catch (UnsatisfiedLinkError e)
282     {
283       System.err.println("Error encountered while attempting to load libSBML:");
284       System.err.println("Please check the value of your "
285                          + (System.getProperty("os.name").startsWith("Mac OS")
286                             ? "DYLD_LIBRARY_PATH" : "LD_LIBRARY_PATH") +
287                          " environment variable and/or your" +
288                          " 'java.library.path' system property (depending on" +
289                          " which one you are using) to make sure it list the" +
290                          " directories needed to find the " +
291                          System.mapLibraryName("sbmlj") + " library file and" +
292                          " libraries it depends upon (e.g., the XML parser).");
293       System.exit(1);
294     }
295     catch (ClassNotFoundException e)
296     {
297       System.err.println("Error: unable to load the file 'libsbmlj.jar'." +
298                          " It is likely that your -classpath command line " +
299                          " setting or your CLASSPATH environment variable " +
300                          " do not include the file 'libsbmlj.jar'.");
301       e.printStackTrace();
302 
303       System.exit(1);
304     }
305     catch (SecurityException e)
306     {
307       System.err.println("Error encountered while attempting to load libSBML:");
308       e.printStackTrace();
309       System.err.println("Could not load the libSBML library files due to a"+
310                          " security exception.\n");
311       System.exit(1);
312     }
313   }
314 }
315 
316