libSBML Python API  5.18.0
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-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 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  #create the predicate node
121  predicate_triple = XMLTriple("predicate",
122  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
123  "rdf")
124 
125  resource_att.clear()
126  resource_att.add("rdf:resource",
127  "http://biomodels.net/biology-qualifiers/occursIn")
128 
129  predicate_token = XMLToken(predicate_triple, resource_att)
130 
131  predicate = XMLNode(predicate_token)
132 
133  #create the object node
134  object_triple = XMLTriple("object",
135  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
136  "rdf")
137 
138  resource_att.clear()
139  resource_att.add("rdf:resource", "urn:miriam:obo.go:GO%3A0005764")
140 
141  object_token = XMLToken(object_triple, resource_att)
142 
143  object_ = XMLNode(object_token)
144 
145  # create the bqbiol node
146  bqbiol_triple = XMLTriple("isDescribedBy",
147  "http://biomodels.net/biology-qualifiers/",
148  "bqbiol")
149 
150  bqbiol_token = XMLToken(bqbiol_triple, blank_att)
151 
152  bqbiol = XMLNode(bqbiol_token)
153 
154  # create the bag node
155  bag_triple = XMLTriple("Bag",
156  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
157  "rdf")
158 
159  bag_token = XMLToken(bag_triple, blank_att)
160 
161  bag = XMLNode(bag_token)
162 
163  # create each li node and add to the bag
164  li_triple = XMLTriple("li",
165  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
166  "rdf")
167 
168  resource_att.clear()
169  resource_att.add("rdf:resource", "urn:miriam:obo.eco:ECO%3A0000004")
170 
171  li_token = XMLToken(li_triple, resource_att)
172  li_token.setEnd()
173 
174  li = XMLNode(li_token)
175 
176  bag.addChild(li)
177 
178  resource_att.clear()
179  resource_att.add("rdf:resource", "urn:miriam:pubmed:7017716")
180  li_token = XMLToken(li_triple, resource_att)
181  li_token.setEnd()
182  li = XMLNode(li_token)
183 
184  bag.addChild(li)
185 
186  # add the bag to bqbiol
187  bqbiol.addChild(bag)
188 
189  # add subject, predicate, object and bqbiol to statement
190  statement.addChild(subject)
191  statement.addChild(predicate)
192  statement.addChild(object_)
193  statement.addChild(bqbiol)
194 
195  # create a top-level RDF element
196  # this will ensure correct merging
197  #
198 
199  xmlns = XMLNamespaces()
200  xmlns.add("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf")
201  xmlns.add("http://purl.org/dc/elements/1.1/", "dc")
202  xmlns.add("http://purl.org/dc/terms/", "dcterms")
203  xmlns.add("http://www.w3.org/2001/vcard-rdf/3.0#", "vCard")
204  xmlns.add("http://biomodels.net/biology-qualifiers/", "bqbiol")
205  xmlns.add("http://biomodels.net/model-qualifiers/", "bqmodel")
206 
207  RDF_triple = XMLTriple("RDF",
208  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
209  "rdf")
210 
211  RDF_token = XMLToken(RDF_triple, blank_att, xmlns)
212 
213  annotation = XMLNode(RDF_token)
214 
215  # add the staement node to the RDF node
216  annotation.addChild(statement)
217 
218  s.appendAnnotation(annotation)
219 
220  writeSBML(d, args[2])
221 
222  return errors
223 
224 
225 if __name__ == '__main__':
226  main(sys.argv)