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

Demonstrates how to use the element filter class to search the model for elements with specific attributes .

1 #!/usr/bin/env python
2 ##
3 ## @file getAllElementsWithNotes.py
4 ## @brief Utility program, demontrating how to use the element filter
5 ## class to search the model for elements with specific attributes
6 ## in this example, we look for elements with notes
7 ##
8 ## @author Frank T. Bergmann
9 ##
10 ##
11 ## <!--------------------------------------------------------------------------
12 ## This sample program is distributed under a different license than the rest
13 ## of libSBML. This program uses the open-source MIT license, as follows:
14 ##
15 ## Copyright (c) 2013-2014 by the California Institute of Technology
16 ## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
17 ## and the University of Heidelberg (Germany), with support from the National
18 ## Institutes of Health (USA) under grant R01GM070923. All rights reserved.
19 ##
20 ## Permission is hereby granted, free of charge, to any person obtaining a
21 ## copy of this software and associated documentation files (the "Software"),
22 ## to deal in the Software without restriction, including without limitation
23 ## the rights to use, copy, modify, merge, publish, distribute, sublicense,
24 ## and/or sell copies of the Software, and to permit persons to whom the
25 ## Software is furnished to do so, subject to the following conditions:
26 ##
27 ## The above copyright notice and this permission notice shall be included in
28 ## all copies or substantial portions of the Software.
29 ##
30 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33 ## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
36 ## DEALINGS IN THE SOFTWARE.
37 ##
38 ## Neither the name of the California Institute of Technology (Caltech), nor
39 ## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
40 ## of Heidelberg, nor the names of any contributors, may be used to endorse
41 ## or promote products derived from this software without specific prior
42 ## written permission.
43 ## ------------------------------------------------------------------------ -->
44 ##
45 ##
46 
47 import sys
48 import os.path
49 import time
50 import libsbml
51 
52 # This class implements an element filter, that can be used to find elements
53 # with notes
54 class NotesFilter(libsbml.ElementFilter):
55  def __init__(self):
56  # call the constructor of the base class
57  libsbml.ElementFilter.__init__(self)
58 
59  # The function performing the filtering, here we just check
60  # that we have a valid element, and that it has notes.
61  def filter(self, element):
62  # return in case we don't have a valid element
63  if (element == None or element.isSetNotes() == False):
64  return False;
65  # otherwise we have notes set and want to keep the element
66  if (element.isSetId()):
67  print " found : {0}".format(element.getId())
68  else:
69  print " found element without id"
70  return True
71 
72 def main (args):
73  """Usage: getAllElementsWithNotes filename
74  """
75  if len(args) != 2:
76  print(main.__doc__)
77  sys.exit(1)
78 
79  filename = args[1];
80 
81  # read the document
82  start = time.time() * 1000;
83  document = libsbml.readSBMLFromFile(filename);
84  stop = time.time() * 1000;
85 
86 
87  print ""
88  print " filename: {0}".format( filename);
89  print " read time (ms): {0}".format( stop - start);
90 
91  # stop in case of serious errors
92  errors = document.getNumErrors(libsbml.LIBSBML_SEV_ERROR);
93  if (errors > 0):
94  print " error(s): {0}".format(errors);
95  document.printErrors();
96  sys.exit (errors);
97 
98 
99  # create the filter we want to use
100  filter = NotesFilter()
101 
102  # get a list of all elements with notes
103  start = time.time() * 1000;
104  print " searching ......:"
105  allElements = document.getListOfAllElements(filter);
106  stop = time.time() * 1000;
107  print " search time (ms): {0}".format(stop - start);
108 
109  print " elements with notes: {0}".format(allElements.getSize())
110 
111  # if we got here all went well ...
112 
113 if __name__ == '__main__':
114  main(sys.argv)