libSBML Python API  5.10.0
 All Classes Namespaces Files Functions Variables Modules Pages
addingEvidenceCodes_2.py

Adds evidence codes to a species in a model.

1 #!/usr/bin/env python
2 ##
3 ## \file addingEvidenceCodes_2.py
4 ## \brief adds evidence codes to a species in a model
5 ## \author Sarah Keating
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-2014 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 import sys
42 import os.path
43 from libsbml import *
44 
45 def main (args):
46  """usage: addingEvidenceCodes_2 <input-filename> <output-filename>
47  Adds controlled vocabulary term to a species
48  """
49  if len(args) != 3:
50  print(main.__doc__)
51  sys.exit(2)
52 
53  d = readSBML(args[1]);
54  errors = d.getNumErrors();
55 
56  if (errors > 0):
57  print("Read Error(s):\n");
58  d.printErrors();
59 
60  print("Correct the above and re-run.\n");
61  else:
62  n = d.getModel().getNumSpecies();
63  if (n <= 0):
64  print("Model has no species.\n Cannot add CV terms\n");
65  else:
66  s = d.getModel().getSpecies(0);
67 
68  # check that the species has a metaid
69  # no CVTerms will be added if there is no metaid to reference
70  #
71  if (not s.isSetMetaId()):
72  s.setMetaId("metaid_0000052");
73 
74  cv1 = CVTerm(BIOLOGICAL_QUALIFIER);
75  cv1.setBiologicalQualifierType(BQB_OCCURS_IN);
76  cv1.addResource("urn:miriam:obo.go:GO%3A0005764");
77 
78  s.addCVTerm(cv1);
79 
80  # now create the additional annotation
81 
82  # <rdf:Statement>
83  # <rdf:subject rdf:resource="#metaid_0000052"/>
84  # <rdf:predicate rdf:resource="http://biomodels.net/biology-qualifiers/occursIn"/>
85  # <rdf:object rdf:resource="urn:miriam:obo.go:GO%3A0005764"/>
86  # <bqbiol:isDescribedBy>
87  # <rdf:Bag>
88  # <rdf:li rdf:resource="urn:miriam:obo.eco:ECO%3A0000004"/>
89  # <rdf:li rdf:resource="urn:miriam:pubmed:7017716"/>
90  # </rdf:Bag>
91  # </bqbiol:isDescribedBy>
92  # </rdf:Statement>
93 
94  # attributes
95  blank_att = XMLAttributes();
96 
97  resource_att = XMLAttributes();
98 
99  # create the outer statement node
100  statement_triple = XMLTriple("Statement",
101  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
102  "rdf");
103 
104  statement_token = XMLToken(statement_triple, blank_att);
105 
106  statement = XMLNode(statement_token);
107 
108  # create the subject node
109  subject_triple = XMLTriple("subject",
110  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
111  "rdf");
112 
113  resource_att.clear();
114  resource_att.add("rdf:resource", "#" + s.getMetaId());
115 
116  subject_token = XMLToken(subject_triple, resource_att);
117 
118  subject = XMLNode(subject_token);
119 
120 
121  #create the predicate node
122  predicate_triple = XMLTriple("predicate",
123  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
124  "rdf");
125 
126  resource_att.clear();
127  resource_att.add("rdf:resource",
128  "http://biomodels.net/biology-qualifiers/occursIn");
129 
130  predicate_token = XMLToken(predicate_triple, resource_att);
131 
132  predicate = XMLNode(predicate_token);
133 
134  #create the object node
135  object_triple = XMLTriple("object",
136  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
137  "rdf");
138 
139  resource_att.clear();
140  resource_att.add("rdf:resource", "urn:miriam:obo.go:GO%3A0005764");
141 
142  object_token = XMLToken(object_triple, resource_att);
143 
144  object_ = XMLNode(object_token);
145 
146  # create the bqbiol node
147  bqbiol_triple = XMLTriple("isDescribedBy",
148  "http://biomodels.net/biology-qualifiers/",
149  "bqbiol");
150 
151  bqbiol_token = XMLToken(bqbiol_triple, blank_att);
152 
153  bqbiol = XMLNode(bqbiol_token);
154 
155  # create the bag node
156  bag_triple = XMLTriple("Bag",
157  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
158  "rdf");
159 
160  bag_token = XMLToken(bag_triple, blank_att);
161 
162  bag = XMLNode(bag_token);
163 
164  # create each li node and add to the bag
165  li_triple = XMLTriple("li",
166  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
167  "rdf");
168 
169  resource_att.clear();
170  resource_att.add("rdf:resource", "urn:miriam:obo.eco:ECO%3A0000004");
171 
172  li_token = XMLToken(li_triple, resource_att);
173  li_token.setEnd();
174 
175  li = XMLNode(li_token);
176 
177  bag.addChild(li);
178 
179  resource_att.clear();
180  resource_att.add("rdf:resource", "urn:miriam:pubmed:7017716");
181  li_token = XMLToken(li_triple, resource_att);
182  li_token.setEnd();
183  li = XMLNode(li_token);
184 
185  bag.addChild(li);
186 
187  # add the bag to bqbiol
188  bqbiol.addChild(bag);
189 
190  # add subject, predicate, object and bqbiol to statement
191  statement.addChild(subject);
192  statement.addChild(predicate);
193  statement.addChild(object_);
194  statement.addChild(bqbiol);
195 
196 
197  # create a top-level RDF element
198  # this will ensure correct merging
199  #
200 
201  xmlns = XMLNamespaces();
202  xmlns.add("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf");
203  xmlns.add("http://purl.org/dc/elements/1.1/", "dc");
204  xmlns.add("http://purl.org/dc/terms/", "dcterms");
205  xmlns.add("http://www.w3.org/2001/vcard-rdf/3.0#", "vCard");
206  xmlns.add("http://biomodels.net/biology-qualifiers/", "bqbiol");
207  xmlns.add("http://biomodels.net/model-qualifiers/", "bqmodel");
208 
209  RDF_triple = XMLTriple("RDF",
210  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
211  "rdf");
212 
213  RDF_token = XMLToken(RDF_triple, blank_att, xmlns);
214 
215  annotation = XMLNode(RDF_token);
216 
217  # add the staement node to the RDF node
218  annotation.addChild(statement);
219 
220  s.appendAnnotation(annotation);
221 
222  writeSBML(d, args[2]);
223 
224  return errors;
225 
226 if __name__ == '__main__':
227  main(sys.argv)