1 /**
2  * @file    appendAnnotation.java
3  * @brief   Adds annotation to a model and a species
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.libsbmlConstants;
44 import org.sbml.libsbml.CVTerm;
45 import org.sbml.libsbml.Date;
46 import org.sbml.libsbml.ModelCreator;
47 import org.sbml.libsbml.ModelHistory;
48 import org.sbml.libsbml.SBMLWriter;
49 import org.sbml.libsbml.Species;
50 import org.sbml.libsbml.SBMLDocument;
51 import org.sbml.libsbml.SBMLReader;
52 
53 
54 public class appendAnnotation
55 {
main(String[] args)56   public static void main (String[] args)
57   {
58     if (args.length != 2)
59     {
60       println("\n  usage: java appendAnnotation <input-filename> <output-filename>\n");
61       System.exit(2);
62     }
63 
64     SBMLDocument d;
65     SBMLReader reader     = new SBMLReader();
66     SBMLWriter writer     = new SBMLWriter();
67 
68     d = reader.readSBML(args[0]);
69 
70     long errors = d.getNumErrors();
71 
72     if (errors > 0)
73     {
74       println("Read Error(s):");
75       d.printErrors();
76       println("Correct the above and re-run.");
77     }
78     else
79     {
80         String model_history_annotation =
81            "<annotation>\n" +
82            "  <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:vCard=\"http://www.w3.org/2001/vcard-rdf/3.0#\" xmlns:bqbiol=\"http://biomodels.net/biology-qualifiers/\" xmlns:bqmodel=\"http://biomodels.net/model-qualifiers/\">\n" +
83            "    <rdf:Description rdf:about=\"#\">\n" +
84            "      <dc:creator rdf:parseType=\"Resource\">\n" +
85            "        <rdf:Bag>\n" +
86            "          <rdf:li rdf:parseType=\"Resource\">\n" +
87            "            <vCard:N rdf:parseType=\"Resource\">\n" +
88            "              <vCard:Family>Keating</vCard:Family>\n" +
89            "              <vCard:Given>Sarah</vCard:Given>\n" +
90            "            </vCard:N>\n" +
91            "            <vCard:EMAIL>sbml-team@caltech.edu</vCard:EMAIL>\n" +
92            "            <vCard:ORG>\n" +
93            "              <vCard:Orgname>University of Hertfordshire</vCard:Orgname>\n" +
94            "            </vCard:ORG>\n" +
95            "          </rdf:li>\n" +
96            "        </rdf:Bag>\n" +
97            "      </dc:creator>\n" +
98            "      <dcterms:created rdf:parseType=\"Resource\">\n" +
99            "        <dcterms:W3CDTF>1999-11-13T06:54:32Z</dcterms:W3CDTF>\n" +
100            "      </dcterms:created>\n" +
101            "      <dcterms:modified rdf:parseType=\"Resource\">\n" +
102            "        <dcterms:W3CDTF>2007-11-31T06:54:00-02:00</dcterms:W3CDTF>\n" +
103            "      </dcterms:modified>\n" +
104            "    </rdf:Description>\n" +
105            "  </rdf:RDF>\n" +
106            "</annotation>\n";
107 
108         d.getModel().appendAnnotation(model_history_annotation);
109         /*
110          * The above code can be replaced by the following code.
111          *
112          ModelHistory h = new ModelHistory();
113 
114          ModelCreator c = new ModelCreator();
115          c.setFamilyName("Keating");
116          c.setGivenName("Sarah");
117          c.setEmail("sbml-team@caltech.edu");
118          c.setOrganisation("University of Hertfordshire");
119 
120          h.addCreator(c);
121 
122          Date date = new Date("1999-11-13T06:54:32");
123          Date date2 = new Date("2007-11-31T06:54:00-02:00");
124 
125          h.setCreatedDate(date);
126          h.setModifiedDate(date2);
127 
128          d.getModel().setModelHistory(h);
129          *
130          */
131 
132         long n = d.getModel().getNumSpecies();
133 
134         if (n > 0)
135         {
136           Species s = d.getModel().getSpecies(0);
137 
138           String cvterms_annotation =
139             "<annotation>\n" +
140             "  <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:vCard=\"http://www.w3.org/2001/vcard-rdf/3.0#\" xmlns:bqbiol=\"http://biomodels.net/biology-qualifiers/\" xmlns:bqmodel=\"http://biomodels.net/model-qualifiers/\">\n" +
141             "    <rdf:Description rdf:about=\"#\">\n" +
142             "      <bqbiol:isVersionOf>\n" +
143             "        <rdf:Bag>\n" +
144             "          <rdf:li rdf:resource=\"http://www.geneontology.org/#GO:0005892\"/>\n" +
145             "          <rdf:li rdf:resource=\"http://www.ebi.ac.uk/interpro/#IPR002394\"/>\n" +
146             "        </rdf:Bag>\n" +
147             "      </bqbiol:isVersionOf>\n" +
148             "      <bqbiol:is>\n" +
149             "        <rdf:Bag>\n" +
150             "          <rdf:li rdf:resource=\"http://www.geneontology.org/#GO:0005895\"/>\n" +
151             "        </rdf:Bag>\n" +
152             "      </bqbiol:is>\n" +
153             "    </rdf:Description>\n" +
154             "  </rdf:RDF>\n" +
155             "</annotation>\n";
156 
157           s.appendAnnotation(cvterms_annotation);
158 
159           /*
160            * The above code can be replaced by the following code.
161            *
162           CVTerm cv = new CVTerm();
163           cv.setQualifierType(libsbmlConstants.BIOLOGICAL_QUALIFIER);
164           cv.setBiologicalQualifierType(libsbmlConstants.BQB_IS_VERSION_OF);
165           cv.addResource("http://www.geneontology.org/#GO:0005892");
166 
167           CVTerm cv2 = new CVTerm();
168           cv2.setQualifierType(libsbmlConstants.BIOLOGICAL_QUALIFIER);
169           cv2.setBiologicalQualifierType(libsbmlConstants.BQB_IS);
170           cv2.addResource("http://www.geneontology.org/#GO:0005895");
171 
172           CVTerm cv1 = new CVTerm();
173           cv1.setQualifierType(libsbmlConstants.BIOLOGICAL_QUALIFIER);
174           cv1.setBiologicalQualifierType(libsbmlConstants.BQB_IS_VERSION_OF);
175           cv1.addResource("http://www.ebi.ac.uk/interpro/#IPR002394");
176 
177           s.addCVTerm(cv);
178           s.addCVTerm(cv2);
179           s.addCVTerm(cv1);
180            *
181            */
182         }
183     }
184     writer.writeSBML(d, args[1]);
185   }
186 
println(String msg)187   static void println (String msg)
188   {
189     System.out.println(msg);
190   }
191 
192 
193   /**
194    * Loads the SWIG-generated libSBML Java module when this class is
195    * loaded, or reports a sensible diagnostic message about why it failed.
196    */
197   static
198   {
199     try
200     {
201       System.loadLibrary("sbmlj");
202       // For extra safety, check that the jar file is in the classpath.
203       Class.forName("org.sbml.libsbml.libsbml");
204     }
205     catch (UnsatisfiedLinkError e)
206     {
207       System.err.println("Error encountered while attempting to load libSBML:");
208       System.err.println("Please check the value of your "
209                          + (System.getProperty("os.name").startsWith("Mac OS")
210                             ? "DYLD_LIBRARY_PATH" : "LD_LIBRARY_PATH") +
211                          " environment variable and/or your" +
212                          " 'java.library.path' system property (depending on" +
213                          " which one you are using) to make sure it list the" +
214                          " directories needed to find the " +
215                          System.mapLibraryName("sbmlj") + " library file and" +
216                          " libraries it depends upon (e.g., the XML parser).");
217       System.exit(1);
218     }
219     catch (ClassNotFoundException e)
220     {
221       System.err.println("Error: unable to load the file 'libsbmlj.jar'." +
222                          " It is likely that your -classpath command line " +
223                          " setting or your CLASSPATH environment variable " +
224                          " do not include the file 'libsbmlj.jar'.");
225       e.printStackTrace();
226 
227       System.exit(1);
228     }
229     catch (SecurityException e)
230     {
231       System.err.println("Error encountered while attempting to load libSBML:");
232       e.printStackTrace();
233       System.err.println("Could not load the libSBML library files due to a"+
234                          " security exception.\n");
235       System.exit(1);
236     }
237   }
238 }
239 
240