1#!/usr/bin/env perl
2# -*-Perl-*-
3##
4## @file    printAnnotation.pl
5## @brief   Prints annotation strings for each element
6## @author  Akiya Jouraku
7##
8## <!--------------------------------------------------------------------------
9## This sample program is distributed under a different license than the rest
10## of libSBML.  This program uses the open-source MIT license, as follows:
11##
12## Copyright (c) 2013-2018 by the California Institute of Technology
13## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
14## and the University of Heidelberg (Germany), with support from the National
15## Institutes of Health (USA) under grant R01GM070923.  All rights reserved.
16##
17## Permission is hereby granted, free of charge, to any person obtaining a
18## copy of this software and associated documentation files (the "Software"),
19## to deal in the Software without restriction, including without limitation
20## the rights to use, copy, modify, merge, publish, distribute, sublicense,
21## and/or sell copies of the Software, and to permit persons to whom the
22## Software is furnished to do so, subject to the following conditions:
23##
24## The above copyright notice and this permission notice shall be included in
25## all copies or substantial portions of the Software.
26##
27## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33## DEALINGS IN THE SOFTWARE.
34##
35## Neither the name of the California Institute of Technology (Caltech), nor
36## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
37## of Heidelberg, nor the names of any contributors, may be used to endorse
38## or promote products derived from this software without specific prior
39## written permission.
40## ------------------------------------------------------------------------ -->
41##
42
43
44use LibSBML;
45no strict;
46sub printAnnotation {
47  $sb = $_[0];
48  $id = defined $_[1] ? $_[1] : '';
49
50  if (not $sb->isSetAnnotation()) {
51	return;
52  }
53  $pid = "";
54
55  if ($sb->isSetId()) {
56      $pid = $sb->getId();
57  }
58  print("----- ", $sb->getElementName(), " (", $pid, ") annotation -----", "\n");
59  print($sb->getAnnotationString(), "\n");
60  print("\n");
61}
62
63if ($#ARGV != 0) {
64  print "Usage: printAnnotation filename\n";
65  exit 1;
66}
67
68$filename = $ARGV[0];
69$document = LibSBML::readSBML($filename);
70
71$errors = $document->getNumErrors();
72
73print("\n");
74print("$filename: " , $filename , "\n");
75print("\n");
76
77if (errors > 0) {
78    $document->printErrors();
79    exit errors;
80}
81
82# Model
83
84$m = $document->getModel();
85printAnnotation($m);
86
87for ($i = 0; $i < $m->getNumReactions(); $i++) {
88    $re = $m->getReaction($i);
89    printAnnotation($re);
90
91    # SpeciesReference (Reacatant)
92    for ($j = 0; $j < $re->getNumReactants(); $j++) {
93        $rt = $re->getReactant($j);
94        if ($rt->isSetAnnotation()){
95		print("     ");
96        }
97        printAnnotation($rt, $rt->getSpecies());
98    }
99
100    # SpeciesReference (Product)
101    for ($j = 0; $j < $re->getNumProducts(); $j++) {
102        $rt = $re->getProduct($j);
103        if ($rt->isSetAnnotation()){
104		print("     ");
105        }
106        printAnnotation($rt, $rt->getSpecies());
107    }
108
109    # ModifierSpeciesReference (Modifiers)
110    for ($j = 0; $j < $re->getNumModifiers(); $j++) {
111        $md = $re->getModifier($j);
112        if ($md->isSetAnnotation()) {
113		print("     ");
114        }
115        printAnnotation($md, $md->getSpecies());
116    }
117
118    # KineticLaw
119    if ($re->isSetKineticLaw()) {
120        $kl = $re->getKineticLaw();
121        if ($kl->isSetAnnotation()) {
122		print("   ");
123        }
124        printAnnotation($kl);
125
126        # Parameter
127        for ($j = 0; $j < $kl->getNumParameters(); $j++) {
128            $pa = $kl->getParameter($j);
129            if ($pa->isSetAnnotation()) {
130			print("      ");
131            }
132            printAnnotation($pa);
133        }
134    }
135}
136# Species
137for ($i = 0; $i < $m->getNumSpecies(); $i++) {
138    $sp = $m->getSpecies($i);
139    printAnnotation($sp);
140}
141# Compartments
142for ($i = 0; $i < $m->getNumCompartments(); $i++) {
143    $sp = $m->getCompartment($i);
144    printAnnotation($sp);
145}
146# FunctionDefinition
147for ($i=0; $i<$m->getNumFunctionDefinitions(); $i++) {
148    $sp = $m->getFunctionDefinition($i);
149    printAnnotation($sp);
150}
151# UnitDefinition
152for ($i=0; $i<$m->getNumUnitDefinitions(); $i++) {
153    $sp = $m->getUnitDefinition($i);
154    printAnnotation($sp);
155}
156# Parameter
157for ($i = 0; $i <$m->getNumParameters(); $i++) {
158    $sp = $m->getParameter($i);
159    printAnnotation($sp);
160}
161# Rule
162for ($i = 0; $i <$m->getNumRules(); $i++) {
163    $sp = $m->getRule($i);
164    printAnnotation($sp);
165}
166# InitialAssignment
167for ($i = 0; $i <$m->getNumInitialAssignments(); $i++) {
168    $sp = $m->getInitialAssignment($i);
169    printAnnotation($sp);
170}
171# Event
172for ($i = 0; $i <$m->getNumEvents(); $i++) {
173    $sp = $m->getEvent($i);
174    printAnnotation($sp);
175
176    # Trigger
177    if ($sp->isSetTrigger()) {
178        $tg = $sp->getTrigger();
179        if ($tg->isSetAnnotation()) {
180		print("   ");
181        }
182        printAnnotation($tg);
183    }
184    # Delay
185    if ($sp->isSetDelay()) {
186        $dl = $sp->getDelay();
187        if ($dl->isSetAnnotation()) {
188		print("   ");
189        }
190        printAnnotation($dl);
191    }
192    # EventAssignment
193    for ($j = 0; $j <$sp->getNumEventAssignments(); $j++) {
194        $ea = $sp->getEventAssignment($j);
195        if ($ea->isSetAnnotation()) {
196		print("   ");
197        }
198        printAnnotation($ea);
199    }
200}
201# SpeciesType
202for ($i = 0; $i < $m->getNumSpeciesTypes(); $i++) {
203    $sp = $m->getSpeciesType($i);
204    printAnnotation($sp);
205}
206# Constraints
207for ($i = 0; $i <$m->getNumConstraints(); $i++) {
208    $sp = $m->getConstraint($i);
209    printAnnotation($sp);
210}
211exit $errors;
212
213