<!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>