1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Event handling in go-perl</title>
<link rel="stylesheet" type="text/css" href="../../doc/stylesheet.css">
</head>
<body>
<h1>Event handling in go-perl</h1>
<h2>
Background
</h2>
<p>
<a href="go-perl-doc.html">go-perl</a> general documentation
<h2>Parser/Handler Framework</h2>
<h3>Event-based parsing</h3>
<p>
go-perl has an event-streaming framework for parsing
files. Parsers or Generators read input files and fire XML
events. These events are intercepted by Handlers, which do
something with the events, such as convert them into another
format, or spit out a report.
</p>
<p>
This kind of framework has many advantages. It is more
efficient, as it is not necessary to parse a whole file into
memory before doing something with it.
</p>
<p>
Although it is possible to turn the events into objects
conforming to the GO object model (below), it is not necessary
to learn an object model in order to write your own handlers.
</p>
<p>
The framework can easily be used in a mixed programming language
environment - for example, the events can be passed as an XML
stream to an XSLT processor, or to a java program.
</p>
<h3>Nested Event Structure</h3>
<p>
The structure of the nested events fired by the parsers
correspond to XML. The structure of this XML can be shown as
either DTD or stag-schema.
See the <a href="../../xml/dtd/">go-dev/xml/dtd
directory</a> for descriptions of the events fired by the
various parsers.
</p>
<p>
Note that all events generated by any file format corresponding
to ontologies are subsets of obo parser events; see <a
href="../../xml/dtd/obo-parser-events.dtd">obo-parser-events.dtd</a> dtd
</p>
<h3>Architecture</h3>
<p>
A GO::Parser class reads in a file; it can be in various
formats. The parser fires nested events. These are consumed by
the handler; the handler may write out these events in an
alternate format, or it may transform the events into some
different xml.
<pre>
[ reads file ] [ nested events]
FILE ----------------> GO::Parser -------------> GO::Handler
==== ========== ===========
.obo file obo_text_parser
.ontology file go_ont_parser
.defs file go_defs_parser
xref2go file go_xrefs_parser
gene_association file go_assoc_parser
</pre>
</p>
<h3>Stag XML Events</h3>
<p>
The parsers fire off XML events using a limited simplified
subset of XML which has no attributes and no mixed
elements. I believe this has a number of advantages - the parsing
and handling code is more concise, and the events can also be
seen as Lisp-style S-Expressions or simple indented text (don't
worry if you don't know what an S-Expression is)
</p>
<p>
go-perl uses the Data::Stag library for generating and consuming
events; you can read more at <a
href="http://stag.sourceforge.net">stag.sourceforge.net</a>
</p>
<h3>GO::Parsers</h3>
<p>
go-perl comes with the following parsers, all in the <a
href="../GO/Parsers">GO/Parsers</a> directory.
</p>
<dl>
<dt>
GO::Parsers::go_ont_parser
</dt>
<dd>
parses .ontology files into an obo-xml event stream.
See <a
href="../../xml/dtd/go_ont-parser-events.dtd">go_ont-parser-events.dtd</a>
</dd>
<dt>
GO::Parsers::go_def_parser
</dt>
<dd>
parses .definition files into an obo-xml event stream.
See <a
href="../../xml/dtd/go_def-parser-events.dtd">go_def-parser-events.dtd</a>
</dd>
<dt>
GO::Parsers::go_xref_parser
</dt>
<dd>
parses xref2go files into an obo-xml event stream.
See <a
href="../../xml/dtd/go_xref-parser-events.dtd">go_xref-parser-events.dtd</a>
</dd>
<dt>
GO::Parsers::go_assoc_parser
</dt>
<dd>
parses gene_association files into an obo-xml event stream.
See <a
href="../../xml/dtd/go_assoc-parser-events.dtd">go_assoc-parser-events.dtd</a>
</dd>
<dt>
GO::Parsers::obo_text_parser
</dt>
<dd>
parses .obo files into an obo-xml event stream.
See <a
href="../../xml/dtd/obo-parser-events.dtd">obo-parser-events.dtd</a>
</dd>
<dt>
GO::Parsers::obo_xml_parser
</dt>
<dd>
parses .obo-xml files into an obo-xml event stream.
See <a
href="../../xml/dtd/obo-parser-events.dtd">obo-parser-events.dtd</a>
</dd>
<dt>
GO::Parsers::owl_parser
</dt>
<dd>
<b>EXPERIMENTAL</b> - parses OWL-XML into an obo-xml event stream
</dd>
</dl>
<h3>GO::Handlers</h3>
<p>
Handlers intercept the stream of events generated by a
parser. Typically they will either transform one XML event
stream into another (transformation) or they will generate
formatted text.
</p>
<p>
go-perl comes with the following handlers, all in the <a
href="../GO/Handlers">GO/Handlers</a> directory
</p>
<dl>
<dt>
GO::Handlers::obj
</dt>
<dd>
Intercepts obo-xml events and creates GO::Model:: objects -
see the section on the object model, below
</dd>
<dt>
GO::Handlers::obo_text
</dt>
<dd>
Intercepts obo-xml events and generates obo text format output
</dd>
<dt>
GO::Handlers::obo_xml
</dt>
<dd>
Intercepts obo-xml events and generates obo xml output
</dd>
<dt>
GO::Handlers::rdf
</dt>
<dd>
Intercepts obo-xml events and generates GO RDF XML
</dd>
<dt>
GO::Handlers::summary
</dt>
<dd>
Intercepts obo-xml events and generates summary report output
</dd>
<dt>
GO::Handlers::error_report
</dt>
<dd>
Intercepts obo-xml events and generates a report of parse
errors or semantic errors in the source input file
</dd>
<dt>
GO::Handlers::prolog
</dt>
<dd>
Intercepts obo-xml events and generates prolog output
(mainly for use with the obol project)
</dd>
<dt>
GO::Handlers::sxpr
</dt>
<dd>
Intercepts obo-xml events and generates lisp S-Expressions
(aka SXML) using the obo-xml schema. For use with lisp programs.
</dd>
<dt>
GO::Handlers::owl
</dt>
<dd>
Intercepts obo-xml events and generates W3C OWL XML
</dd>
<dt>
GO::Handlers::tbl
</dt>
<dd>
Intercepts obo-xml events and generates simple tabular
output summarising the source ontology, one line per term
</dd>
<dt>
GO::Handlers::pathlist
</dt>
<dd>
Intercepts obo-xml events and generates pathlists for every
term in the source ontology. A pathlist shows the list of
valid paths from a term to the ontology top/root term(s).
</dd>
<dt>
GO::Handlers::obo_html
</dt>
<dd>
Intercepts obo-xml events and generates html-ized obo format
output
</dd>
<dt>
GO::Handlers::text_html
</dt>
<dd>
Intercepts obo-xml events and generates html-ized go_ont
format output
</dd>
<dt>
GO::Handlers::go_ont
</dt>
<dd>
Intercepts obo-xml events and generates (deprecated) go_ont
file format output (ie .ontology files), with parentage
indicated by indentation
</dd>
<dt>
GO::Handlers::go_def
</dt>
<dd>
Intercepts obo-xml events and generates (deprecated) go_def
style output (ie .definition files)
</dd>
<dt>
GO::Handlers::go_xref
</dt>
<dd>
Intercepts obo-xml events and generates xref2go output,
showing mapping between ontology terms and xref_analogs in
different dbs
</dd>
<dt>
GO::Handlers::godb_prestore
</dt>
<dd>
Intercepts obo-xml events and transforms the input xml
stream into an xml output stream conforming to the schema of
the go-database. See <a
href="../../go-db-perl/doc/go-db-perl-doc.html">go-db-perl</a>
for details. This is how the GO database is loaded. The
structure of the transformed XML events is described by
<a
href="../../xml/dtd/godb_prestore-events.dtd">godb_prestore-events.dtd</a>
</dd>
<dt>
GO::Handlers::chadodb_prestore [EXPERIMENTAL]
</dt>
<dd>
As GO::Handlers::godb_prestore, but maps xml to a chado db schema
</dd>
<dt>
GO::Handlers::owl_to_obo [EXPERIMENTAL]
</dt>
<dd>
Takes an input OWL XML stream and transforms it to obo-xml
parse streams
</dd>
</dl>
<p>
Most of these handlers come with a corresponding script; for
example, you can test the GO::Handlers::pathlist parser by
running go2pathlist on any GO or OBO formatted file.
</p>
<h3>Writing your own Handler</h3>
<p>
Let's say you wish to parse a gene_association file, and produce
a formatted report that looks something like this:
</p>
<p>
You're going to be intercepting a go-assoc event stream; you can
get a rough idea of what this looks like by running the script
go2xml on any of the gene_association files in the main GO
repository.
</p>
<div class="codeblock">
<pre>
<assocs>
<dbset>
<proddb>RGD</proddb>
<prod>
<prodacc>RGD:621326</prodacc>
<prodsymbol>Arl1</prodsymbol>
<prodname>ADP-ribosylationfactor-like1</prodname>
<prodtype>gene</prodtype>
<prodtaxa>10116</prodtaxa>
<assoc>
<assocdate>20040317</assocdate>
<source_db>RGD</source_db>
<termacc>GO:0005802</termacc>
<is_not>0</is_not>
<aspect>C</aspect>
<evidence>
<evcode>IDA</evcode>
<ref>RGD:724590</ref>
</evidence>
</assoc>
</prod>
<prod>
<prodacc>RGD:69327</prodacc>
<prodsymbol>Arl3</prodsymbol>
<prodname>ADP-ribosylation-like3</prodname>
<prodtype>gene</prodtype>
<prodtaxa>10116</prodtaxa>
<assoc>
<assocdate>20040317</assocdate>
<source_db>RGD</source_db>
<termacc>GO:0019003</termacc>
<is_not>0</is_not>
<aspect>F</aspect>
<evidence>
<evcode>IDA</evcode>
<ref>RGD:68742</ref>
</evidence>
</assoc>
<assoc>
...etc
</pre>
</div>
<p>
Or you can look at the structure of the go-assoc event stream;
see <a
href="../../xml/dtd/go_assoc-parser-events.dtd">go_assoc-parser-events.dtd</a>
in the <a href="../../xml/dtd/">go-dev/xml/dtd
directory</a>. The same structure can be shown with the
following stag-schema, below:
</p>
<div class="codeblock">
<pre>
(assocs
(dbset+
(proddb "s")
(prod+
(prodacc "s")
(prodsymbol "s")
(prodtype "s")
(prodtaxa "i")
(assoc+
(assocdate "i")
(source_db "s")
(termacc "s")
(is_not "i")
(aspect "s")
(evidence+
(evcode "s")
(ref "s"))))))
</pre>
</div>
<p>
We wish to catch <prod> events and write them out
</p>
<div class="codeblock">
<pre>
package My::Handler;
use base (GO::Handlers::DefHandler);
sub e_prod {
my ($self, $prod) = @_;
printf "Gene Product: %s %s\n",
$prod->get_acc, $prod->get_symbol;
my @assocs = $prod->get_assoc;
foreach my $assoc (@assocs) {
printf " To Term: %s\n", $assoc->get_termacc;
}
}
1;
</pre>
</div>
<p>
The get_* methods are automatically provided by the Data::Stag
modules. See <a
href="http://search.cpan.org/perldoc?Data::Stag">Data::Stag</a>
documentation, on CPAN.
</p>
<p>
You can then see the output of your handler using the go2fmt.pl script:
</p>
<div class="codeblock">
<pre>
go2fmt.pl -w My::Handler gene_associations.sgd
</pre>
</div>
<p>
You can also use your module in code like this:
</p>
<div class="codeblock">
<pre>
use GO::Parser;
# create a parser object that passes things to an object handler
my $parser = GO::Parser->new({handler=>"My::Handler"});
# the parser will go through the file firing events
# which will be intercepted by My::Handler
$parser->parse($file);
exit 0;
</pre>
</div>
<h2>Object Model</h2>
<p>
Sometimes we find it necessary to do things such as graph
traversal on the ontology; this is where the object model comes
in handy.
</p>
<p>
Before reading on, you should make sure you are reasonably
familiar with the slightly abstruse perl object system. You
should also be familiar with commands like "perldoc" so you can
get more information on any class in the object model.
</p>
<p>
Here's a diagram showing the overall
structure of the go-perl object model:
<img src="gomodel.png"></img>
</p>
<p>
Note that unless you're using the <a href="../../go-db-perl/doc/go-db-perl-doc.html">go-db-perl</a>, then you probably
don't have any need for many of the classes, such as
Association, GeneProduct and Seq
</p>
<p>
The 3 core classes are Term, Relationship and Graph. A Term is
used to represent a GO or OBO Term/Class, and holds data such as
the identifier, the name, definition, synonyms and so forth.
</p>
<p>
Perhaps the most useful class in the object model is the
GO::Model::Graph class - this holds a collection of Terms and
knows the Relationships between them. Note that the Term objects
themselves are quite dumb - they have no idea what their context
is in the overall DAG/Ontology. You have to use the Graph object
to discover this sort of thing.
</p>
<p>
The Graph class has various methods for doing graph traversal
and querying - see the pod docs for full details
</p>
<h3>Using the object model</h3>
<p>
</p>
<div class="codeblock">
<pre>
use GO::Parser;
# create a parser object that passes things to an object handler
my $parser = GO::Parser->new({handler=>'obj'});
# the parser will go through the file firing events - the
# object handler will catch those events and store them
# in a GO::Model::Graph object
$parser->parse($file);
my $graph = $parser->handler->graph;
</pre>
</div>
<h2>Mail List</h2>
<p>
<address><a href="mailto:go-database@fruitfly.org">go-database@fruitfly.org</a></address>
</p>
<hr>
<address><a href="mailto:cjm@fruitfly.org">Chris Mungall</a></address>
<!-- Created: Fri Jan 23 14:30:13 PST 2004 -->
<!-- hhmts start -->
Last modified: Thu Nov 4 20:03:10 PST 2004
<!-- hhmts end -->
</body>
</html>
|