| 1 | package GO::OntologyProvider::OntologyParser; | ||
| 2 | |||
| 3 | # File : OntologyParser.pm | ||
| 4 | # Authors : Elizabeth Boyle; Gavin Sherlock | ||
| 5 | # Date Begun : Summer 2001 | ||
| 6 | # Rewritten : September 29th 2002 | ||
| 7 | |||
| 8 | # $Id: OntologyParser.pm,v 1.19 2007/03/18 03:11:19 sherlock Exp $ | ||
| 9 | |||
| 10 | # License information (the MIT license) | ||
| 11 | |||
| 12 | # Copyright (c) 2003 Gavin Sherlock; Stanford University | ||
| 13 | |||
| 14 | # Permission is hereby granted, free of charge, to any person | ||
| 15 | # obtaining a copy of this software and associated documentation files | ||
| 16 | # (the "Software"), to deal in the Software without restriction, | ||
| 17 | # including without limitation the rights to use, copy, modify, merge, | ||
| 18 | # publish, distribute, sublicense, and/or sell copies of the Software, | ||
| 19 | # and to permit persons to whom the Software is furnished to do so, | ||
| 20 | # subject to the following conditions: | ||
| 21 | |||
| 22 | # The above copyright notice and this permission notice shall be | ||
| 23 | # included in all copies or substantial portions of the Software. | ||
| 24 | |||
| 25 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 26 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 27 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 28 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
| 29 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
| 30 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 31 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 32 | # SOFTWARE. | ||
| 33 | |||
| 34 | =pod | ||
| 35 | |||
| 36 | =head1 NAME | ||
| 37 | |||
| 38 | GO::OntologyProvider::OntologyParser - Provides API for retrieving data from Gene Ontology files | ||
| 39 | |||
| 40 | =head1 SYNOPSIS | ||
| 41 | |||
| 42 | use GO::OntologyProvider::OntologyParser; | ||
| 43 | |||
| 44 | my $ontology = GO::OntologyProvider::OntologyParser->new(ontologyFile => "process.ontology"); | ||
| 45 | |||
| 46 | print "The ancestors of GO:0006177 are:\n"; | ||
| 47 | |||
| 48 | my $node = $ontology->nodeFromId("GO:0006177"); | ||
| 49 | |||
| 50 | foreach my $ancestor ($node->ancestors){ | ||
| 51 | |||
| 52 | print $ancestor->goid, " ", $ancestor->term, "\n"; | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | $ontology->printOntology(); | ||
| 57 | |||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | GO::OntologyProvider::OntologyParser implements the interface | ||
| 62 | defined by GO::OntologyProvider, and parses a gene ontology file | ||
| 63 | (GO) in plain text (not XML) format. These files can be obtained from | ||
| 64 | the Gene Ontology Consortium web site, http://www.geneontology.org/. | ||
| 65 | From the information in the file, it creates a directed acyclic graph | ||
| 66 | (DAG) structure in memory. This means that GO terms are arranged into | ||
| 67 | tree-like structures where each GO node can have multiple parent nodes | ||
| 68 | and multiple child nodes. | ||
| 69 | |||
| 70 | This data structure can be used in conjunction with files in which | ||
| 71 | certain genes are annotated to corresponding GO nodes. | ||
| 72 | |||
| 73 | Each GO ID (e.g. "GO:1234567") has associated with it a GO node. That | ||
| 74 | GO node contains the name of the GO term, a list of the nodes directly | ||
| 75 | above the node ("parent nodes"), and a list of the nodes directly | ||
| 76 | below the current node ("child nodes"). The "ancestor nodes" of a | ||
| 77 | certain node are all of the nodes that are in a path from the current | ||
| 78 | node to the root of the ontology, with all repetitions removed. | ||
| 79 | |||
| 80 | The format of the GO file is as follows (taken from | ||
| 81 | http://www.geneontology.org/doc/GO.doc.html) | ||
| 82 | |||
| 83 | Comment lines: | ||
| 84 | |||
| 85 | Lines that begin ! are comment lines. | ||
| 86 | |||
| 87 | $ lines: | ||
| 88 | |||
| 89 | Line in which the first non-space character is a $ either reflect the | ||
| 90 | domain and aspect of the ontology (i.e. $text) or the end of file | ||
| 91 | (i.e. the $ character on a line by itself). | ||
| 92 | |||
| 93 | Versioning: | ||
| 94 | |||
| 95 | The first lines of each file after any html header information (in | ||
| 96 | *.html files) always carry information about the version, the date of | ||
| 97 | last update, (optionally) the source of the file, the name of the | ||
| 98 | database, the domain of the file and the editors of the file, e.g.: | ||
| 99 | |||
| 100 | ! | ||
| 101 | !Gene Ontology | ||
| 102 | ![domain of file] | ||
| 103 | ! | ||
| 104 | !editors: Michael Ashburner (FlyBase), Midori Harris (GO), Judith Blake (MGD) | ||
| 105 | !Leonore Reiser (TAIR), Karen Christie (SGD) and colleagues | ||
| 106 | !with software by Suzanna Lewis (FlyBase Berkeley). | ||
| 107 | |||
| 108 | Syntax: | ||
| 109 | |||
| 110 | Parent-child relationships between terms are represented by | ||
| 111 | indentation: | ||
| 112 | |||
| 113 | parent_term | ||
| 114 | child_term | ||
| 115 | |||
| 116 | Instance relationship: | ||
| 117 | |||
| 118 | |||
| 119 | %term0 | ||
| 120 | %term1 % term2 | ||
| 121 | |||
| 122 | |||
| 123 | To be read as term1 being an instance of term0 and also an instance of | ||
| 124 | term2. Part of relationship: | ||
| 125 | |||
| 126 | %term0 | ||
| 127 | %term1 < term2 < term3 | ||
| 128 | |||
| 129 | |||
| 130 | To be read as term1 being an instance of term0 and also a part-of of | ||
| 131 | term2 and term3. | ||
| 132 | |||
| 133 | Line syntax (showing the order in which items appear on a line; * | ||
| 134 | indicates optional item): | ||
| 135 | |||
| 136 | |||
| 137 | < | % term [; db cross ref]* [; synonym:text]* [ < | % term]* | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | ################################################################## | ||
| 142 | ################################################################## | ||
| 143 | |||
| 144 | use strict; | ||
| 145 | use warnings; | ||
| 146 | use diagnostics; | ||
| 147 | |||
| 148 | use IO::File; | ||
| 149 | |||
| 150 | use vars qw (@ISA $PACKAGE $VERSION); | ||
| 151 | |||
| 152 | use GO::OntologyProvider; | ||
| 153 | @ISA = qw (GO::OntologyProvider); | ||
| 154 | |||
| 155 | use GO::Node; | ||
| 156 | |||
| 157 | use Storable qw (nstore); | ||
| 158 | |||
| 159 | $VERSION = 0.15; | ||
| 160 | $PACKAGE = "GO::OntologyProvider::OntologyParser"; | ||
| 161 | |||
| 162 | ################################################################## | ||
| 163 | # | ||
| 164 | # CLASS ATTRIBUTES | ||
| 165 | # | ||
| 166 | ################################################################## | ||
| 167 | |||
| 168 | # All the following class attributes are constants, that should be | ||
| 169 | # initialized here at compile time. | ||
| 170 | |||
| 171 | my $DEBUG = 0; | ||
| 172 | |||
| 173 | my $kFile = $PACKAGE.'::__file'; | ||
| 174 | my $kRootNode = $PACKAGE.'::__rootNode'; | ||
| 175 | my $kNodes = $PACKAGE.'::__nodes'; | ||
| 176 | my $kCurrentLineage = $PACKAGE.'::__currentLineage'; | ||
| 177 | my $kSecondaryIds = $PACKAGE.'::__secondaryIds'; | ||
| 178 | |||
| 179 | ################################################################## | ||
| 180 | |||
| 181 | # The constructor, and associated initialization methods | ||
| 182 | |||
| 183 | ################################################################## | ||
| 184 | sub new{ | ||
| 185 | ################################################################## | ||
| 186 | # This is the constructor for an OntologyParser object. | ||
| 187 | # | ||
| 188 | # The constructor expects one of two arguments, either an | ||
| 189 | # 'ontologyFile' argument, or an 'objectFile' argument. When | ||
| 190 | # instantiated with an ontologyFile argument, it expects it to | ||
| 191 | # correspond to an ontology file created by the GO consortium, | ||
| 192 | # according to their file format. When instantiated with an | ||
| 193 | # objectFile argument, it expects to open a previously created | ||
| 194 | # ontologyParser object that has been serialized to disk. | ||
| 195 | |||
| 196 | # | ||
| 197 | # | ||
| 198 | # Usage : | ||
| 199 | # | ||
| 200 | # my $ontology = GO::OntologyProvider::OntologyParser->new(ontologyFile=>$file); | ||
| 201 | # | ||
| 202 | # my $ontology = GO::OntologyProvider::OntologyParser->new(objectFile=>$file); | ||
| 203 | |||
| 204 | my ($class, %args) = @_; | ||
| 205 | |||
| 206 | my $self; | ||
| 207 | |||
| 208 | if (exists($args{'objectFile'})){ | ||
| 209 | |||
| 210 | $self = Storable::retrieve($args{'objectFile'}) | ||
| 211 | |||
| 212 | }elsif (exists($args{'ontologyFile'})){ | ||
| 213 | |||
| 214 | $self = {}; | ||
| 215 | |||
| 216 | bless $self, $class; | ||
| 217 | |||
| 218 | $self->__setFile($args{'ontologyFile'}); | ||
| 219 | |||
| 220 | $self->__init; | ||
| 221 | |||
| 222 | } | ||
| 223 | |||
| 224 | return ($self); | ||
| 225 | |||
| 226 | } | ||
| 227 | |||
| 228 | ############################################################################ | ||
| 229 | sub __setFile{ | ||
| 230 | ############################################################################ | ||
| 231 | # This private method simply stores the name of the file used for | ||
| 232 | # construction inside the object's hash | ||
| 233 | |||
| 234 | my ($self, $file) = @_; | ||
| 235 | |||
| 236 | if (!-e $file){ | ||
| 237 | |||
| 238 | die "$file does not exist"; | ||
| 239 | |||
| 240 | }elsif (-d $file){ | ||
| 241 | |||
| 242 | die "$file is a directory"; | ||
| 243 | |||
| 244 | }elsif (!-r $file){ | ||
| 245 | |||
| 246 | die "$file is not readable"; | ||
| 247 | |||
| 248 | } | ||
| 249 | |||
| 250 | $self->{$kFile} = $file; | ||
| 251 | |||
| 252 | } | ||
| 253 | |||
| 254 | ############################################################################ | ||
| 255 | sub __file{ | ||
| 256 | ############################################################################ | ||
| 257 | # This private method returns the name of the file used to construct the object | ||
| 258 | |||
| 259 | return $_[0]->{$kFile}; | ||
| 260 | |||
| 261 | } | ||
| 262 | |||
| 263 | ############################################################################ | ||
| 264 | sub __init{ | ||
| 265 | ############################################################################ | ||
| 266 | # This method initializes the ontologyParser object, by parsing an ontology | ||
| 267 | # file, and storing the structures represented therein, in memory. | ||
| 268 | |||
| 269 | my $self = shift; | ||
| 270 | |||
| 271 | my $ontologyFh = IO::File->new($self->__file, q{<} )|| die "$PACKAGE can't open file ". $self->__file ." : $!"; | ||
| 272 | |||
| 273 | # go through the ontology one line at a time | ||
| 274 | |||
| 275 | while (<$ontologyFh>){ | ||
| 276 | |||
| 277 | chomp; | ||
| 278 | |||
| 279 | next if (/^\!/); # skip "!" comment lines | ||
| 280 | |||
| 281 | $self->__processNode($_); | ||
| 282 | |||
| 283 | } | ||
| 284 | |||
| 285 | $ontologyFh->close || die "Can't close ". $self->__file ." : $!"; | ||
| 286 | |||
| 287 | } | ||
| 288 | |||
| 289 | ############################################################################ | ||
| 290 | sub __processNode{ | ||
| 291 | ############################################################################ | ||
| 292 | # This private method processes any line identified as a node (ie a | ||
| 293 | # non-comment line). The general idea is that it needs three pieces of | ||
| 294 | # information about the line to deal with it: | ||
| 295 | # | ||
| 296 | # 1. The number of spaces at the beginning of the line, which indicate | ||
| 297 | # its relationship to previous lines (nodes) in the file. | ||
| 298 | # 2. The name of the node. | ||
| 299 | # 3. Any GOIDs associated with the node. | ||
| 300 | # | ||
| 301 | # If it hasn't seen the node before (a node may appear many times in | ||
| 302 | # the file, if it has multiple parents) it will create a new node | ||
| 303 | # object, otherwise it will use the corresponding previously created | ||
| 304 | # node object. It will then indicate in that node object the current | ||
| 305 | # parent, and lineage to the root. It will also add the current node | ||
| 306 | # as a child of the present immediate parent in the lineage to the | ||
| 307 | # root. | ||
| 308 | |||
| 309 | |||
| 310 | my ($self, $line) = @_; | ||
| 311 | |||
| 312 | my $numSpaces = $self->__getNumSpaces($line) || 0; | ||
| 313 | |||
| 314 | my ($nodeName, @goids) = $self->__getNodeInfoFromLine($line); | ||
| 315 | |||
| 316 | my $node; | ||
| 317 | |||
| 318 | if ($self->__nodeIsAlreadyCreated($goids[0])){ | ||
| 319 | |||
| 320 | $node = $self->nodeFromId($goids[0]); | ||
| 321 | |||
| 322 | } else { # node has not already been created | ||
| 323 | |||
| 324 | # create node | ||
| 325 | |||
| 326 | $node = GO::Node->new(goid => $goids[0], | ||
| 327 | term => $nodeName); | ||
| 328 | |||
| 329 | # store it | ||
| 330 | |||
| 331 | $self->{$kNodes}{$goids[0]} = $node; | ||
| 332 | |||
| 333 | # # now hash any secondaries to the primary | ||
| 334 | |||
| 335 | for (my $i=1; $i < @goids; $i++){ | ||
| 336 | |||
| 337 | $self->{$kSecondaryIds}{$goids[$i]} = $goids[0]; | ||
| 338 | |||
| 339 | } | ||
| 340 | |||
| 341 | } | ||
| 342 | |||
| 343 | # before we update the lineage, can we work out whether the last | ||
| 344 | # node was a leaf, and thus for each parent, work out a path to a | ||
| 345 | # leaf? This will satisfy Shuai's needs to be able to look at | ||
| 346 | # descendent paths, and avoid recursion. It will however balloon | ||
| 347 | # the required memory. | ||
| 348 | |||
| 349 | # now add the current node to the lineage | ||
| 350 | |||
| 351 | $self->__updateCurrentLineage($numSpaces, $node); | ||
| 352 | |||
| 353 | if ($numSpaces == 0){ # must be the root node | ||
| 354 | |||
| 355 | $self->{$kRootNode} = $node; | ||
| 356 | |||
| 357 | }else{ | ||
| 358 | |||
| 359 | # and add current path to root to the node | ||
| 360 | |||
| 361 | my @path = @{$self->{$kCurrentLineage}}; | ||
| 362 | |||
| 363 | pop (@path); # remove current node from path; | ||
| 364 | |||
| 365 | $node->addPathToRoot(@path); | ||
| 366 | |||
| 367 | # create connections between current node and it parent | ||
| 368 | |||
| 369 | my $currentParent = $self->__getCurrentParent(); | ||
| 370 | |||
| 371 | $node->addParentNodes($currentParent); | ||
| 372 | |||
| 373 | $currentParent->addChildNodes($node); | ||
| 374 | |||
| 375 | } | ||
| 376 | |||
| 377 | } | ||
| 378 | |||
| 379 | ############################################################################ | ||
| 380 | sub __getNumSpaces{ | ||
| 381 | ############################################################################ | ||
| 382 | # This returns the number of spaces that occur at the beginning of a line. | ||
| 383 | ############################################################################ | ||
| 384 | |||
| 385 | my ($self, $line) = @_; | ||
| 386 | |||
| 387 | if ($line =~ /^( +)/){ # capture leading spaces | ||
| 388 | |||
| 389 | return (length($1)); | ||
| 390 | |||
| 391 | }else{ # it must have been the top level node | ||
| 392 | |||
| 393 | return 0; | ||
| 394 | |||
| 395 | } | ||
| 396 | |||
| 397 | } | ||
| 398 | |||
| 399 | ############################################################################ | ||
| 400 | sub __getNodeInfoFromLine{ | ||
| 401 | ############################################################################ | ||
| 402 | # This private method takes a line from an ontology file as an | ||
| 403 | # argument, and returns the term name, and any goids associated with | ||
| 404 | # that term name. The primary goid will be the first goid returned in | ||
| 405 | # the list. Because all multiple parent relationships should be in | ||
| 406 | # the file redundantly, such that relationships to additional parents | ||
| 407 | # are indicated through indentation, as well as additional information | ||
| 408 | # at the end of a line, this method simply ignores the additional | ||
| 409 | # relationships coded at the end of the line. It only picks them up | ||
| 410 | # thruogh the repeated appearance of nodes under each of their | ||
| 411 | # parents. | ||
| 412 | # | ||
| 413 | # An example line would be: | ||
| 414 | # | ||
| 415 | # %deoxyribodipyrimidine photolyase ; GO:0003904 ; EC:4.1.99.3 % DNA photolyase ; GO:0003913 | ||
| 416 | # | ||
| 417 | # Note that in addition to a database XRef for GO (the GOID) there may | ||
| 418 | # be database cross references to other databases (eg the EC number) | ||
| 419 | # | ||
| 420 | # An example line with more than one GOID for a term is: | ||
| 421 | # | ||
| 422 | # %phospholipid transporter ; GO:0005548, GO:0008497 % lipid transporter ; GO:0005319 | ||
| 423 | # | ||
| 424 | # Note that the GOIDs are separated by a comma. | ||
| 425 | # | ||
| 426 | # Usage: | ||
| 427 | # | ||
| 428 | # my ($termName, @goids) = $self->__getNodeInfoFromLine($line); | ||
| 429 | |||
| 430 | my ($self, $line) = @_; | ||
| 431 | |||
| 432 | # strip off beginning of line, up to beginning of term name | ||
| 433 | |||
| 434 | $line =~ s/^ +//; # eliminate leading spaces | ||
| 435 | $line =~ s/^(\$|\%|\<)//; # eliminate $, % and < characters | ||
| 436 | |||
| 437 | # grab first two fields separated by " ; " | ||
| 438 | |||
| 439 | # Note: $goids may contain extra junk at the end, if there are | ||
| 440 | # additional parents, but no other DBXRefs | ||
| 441 | |||
| 442 | my ($nodeName, $goids) = split(/ ; /, $line); | ||
| 443 | |||
| 444 | # check that we can actually get some goids. Added this in to | ||
| 445 | # deal with when a broken file that appeared on the GO site, it | ||
| 446 | # caused me to get email saying my code was broken... | ||
| 447 | |||
| 448 | if (!defined $goids || $goids eq ""){ | ||
| 449 | |||
| 450 | die "There appears to be a problem with the ontology file.\n". | ||
| 451 | "No GOIDs could be extracted from line $. in ". $self->__file ."\n". | ||
| 452 | "The contents of that line in the file are :\n\n\'".$_[1]."\'\n\n". | ||
| 453 | "Please correct the ontology file and try again.\n\n"; | ||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | # get rid of anything following the goids, that pertains to secondary parents | ||
| 458 | |||
| 459 | $goids =~ s/ (\<|\%).*//; | ||
| 460 | |||
| 461 | # extract goids | ||
| 462 | |||
| 463 | my @goids = split(/\, /, $goids); | ||
| 464 | |||
| 465 | # remove \'s from nodeName | ||
| 466 | |||
| 467 | $nodeName =~ s/\\//g; | ||
| 468 | |||
| 469 | return ($nodeName, @goids); | ||
| 470 | |||
| 471 | } | ||
| 472 | |||
| 473 | ############################################################################ | ||
| 474 | sub __nodeIsAlreadyCreated{ | ||
| 475 | ############################################################################ | ||
| 476 | # This private method returns a boolean to indicate whether a node has | ||
| 477 | # already been created for a given GO ID. | ||
| 478 | |||
| 479 | |||
| 480 | return (exists($_[0]->{$kNodes}{$_[1]})); | ||
| 481 | |||
| 482 | } | ||
| 483 | |||
| 484 | ############################################################################ | ||
| 485 | sub __updateCurrentLineage{ | ||
| 486 | ############################################################################ | ||
| 487 | # Based on the number of spaces before the node, the previous ancestors | ||
| 488 | # that apply to this node also are determined. Cousins of the current node | ||
| 489 | # are removed, and the current node is added to the lineage. | ||
| 490 | # | ||
| 491 | # So how does this work? | ||
| 492 | # | ||
| 493 | # Well, the number of spaces in front of an entry indicate how many | ||
| 494 | # ancestors it must have in a path to the root of the Ontology. Thus, | ||
| 495 | # as we read through the file, the number of spaces will increase and | ||
| 496 | # decrease in front of entries. The number of spaces may only ever | ||
| 497 | # increase by one, from one line to the next, but it may decrease by | ||
| 498 | # several, as we may jump up the ontology, as we finish deep in one | ||
| 499 | # branch, then go high up in another. Because the number of spaces | ||
| 500 | # indicates the number of ancestors a term must have, we simply use an | ||
| 501 | # array, that holds those ancestors for the currently considered line. | ||
| 502 | # When we consider the node, we see how many ancestors it must have, | ||
| 503 | # by the number of spaces in front, then make the array have only that | ||
| 504 | # number of entries, by splicing any extra out. We then push our node | ||
| 505 | # on the array. In this fashion, the array will always hold the | ||
| 506 | # current path to the currently considered node. | ||
| 507 | |||
| 508 | my ($self, $numSpaces, $newNode) = @_; | ||
| 509 | |||
| 510 | # get rid of extra lineage elements that exist from the previously | ||
| 511 | # considered line | ||
| 512 | |||
| 513 | splice (@{$self->{$kCurrentLineage}}, $numSpaces); | ||
| 514 | |||
| 515 | # and put new node onto end of lineage | ||
| 516 | |||
| 517 | push (@{$self->{$kCurrentLineage}}, $newNode); | ||
| 518 | |||
| 519 | } | ||
| 520 | |||
| 521 | ############################################################################ | ||
| 522 | sub __getCurrentParent{ | ||
| 523 | ############################################################################ | ||
| 524 | # This returns the current parent, which is the node at the | ||
| 525 | # second-to-last position of the currentLineage array, after the | ||
| 526 | # currentLineage array has been updated to consider the current node | ||
| 527 | |||
| 528 | return ($_[0]->{$kCurrentLineage}[-2]); | ||
| 529 | |||
| 530 | } | ||
| 531 | |||
| 532 | ############################################################################ | ||
| 533 | sub printOntology{ | ||
| 534 | ############################################################################ | ||
| 535 | # This prints out the ontology, with redundancies. | ||
| 536 | |||
| 537 | my $self = shift; | ||
| 538 | |||
| 539 | $self->__printNode($self->rootNode, 0); | ||
| 540 | |||
| 541 | } | ||
| 542 | |||
| 543 | ############################################################################ | ||
| 544 | sub __printNode{ | ||
| 545 | ############################################################################ | ||
| 546 | # This recursive function prints the name of the specified node and the | ||
| 547 | # names of all of its descendants. | ||
| 548 | # | ||
| 549 | |||
| 550 | my ($self, $node, $indentationLevel) = @_; | ||
| 551 | |||
| 552 | print " " x $indentationLevel, $node->term, " ; ", $node->goid, "\n"; | ||
| 553 | |||
| 554 | foreach my $childNode (sort {$a->term cmp $b->term} $node->childNodes) { | ||
| 555 | |||
| 556 | $self->__printNode($childNode, $indentationLevel+1); | ||
| 557 | |||
| 558 | } | ||
| 559 | |||
| 560 | } | ||
| 561 | |||
| 562 | ############################################################################ | ||
| 563 | sub allNodes{ | ||
| 564 | ############################################################################ | ||
| 565 | # This method returns an array of all the nodes that have been created. | ||
| 566 | # | ||
| 567 | # Usage: | ||
| 568 | # | ||
| 569 | # my @nodes = $ontologyParser->allNodes; | ||
| 570 | |||
| 571 | return (values %{$_[0]->{$kNodes}}); | ||
| 572 | |||
| 573 | } | ||
| 574 | |||
| 575 | ############################################################################ | ||
| 576 | sub rootNode{ | ||
| 577 | ############################################################################ | ||
| 578 | # This returns the root node in the ontology. | ||
| 579 | # | ||
| 580 | # Usage: | ||
| 581 | # | ||
| 582 | # my $rootNode = $ontologyParser->rootNode; | ||
| 583 | |||
| 584 | return ($_[0]->{$kRootNode}); | ||
| 585 | |||
| 586 | } | ||
| 587 | |||
| 588 | ############################################################################ | ||
| 589 | sub nodeFromId{ | ||
| 590 | ############################################################################ | ||
| 591 | # This public method takes a GOID and returns the GO::Node that | ||
| 592 | # it corresponds to. It should also work with secondary id's | ||
| 593 | # | ||
| 594 | # Usage : | ||
| 595 | # | ||
| 596 | # my $node = $ontologyParser->nodeFromId($goid); | ||
| 597 | |||
| 598 | my ($self, $goid) = @_; | ||
| 599 | |||
| 600 | if (exists ($self->{$kNodes}{$goid})){ # it's a primary | ||
| 601 | |||
| 602 | return ($self->{$kNodes}{$goid}); | ||
| 603 | |||
| 604 | }elsif (exists ($self->{$kSecondaryIds}{$goid})){ # it's a secondary | ||
| 605 | |||
| 606 | return $self->{$kNodes}{$self->{$kSecondaryIds}{$goid}}; | ||
| 607 | |||
| 608 | }else{ | ||
| 609 | |||
| 610 | return undef; | ||
| 611 | |||
| 612 | } | ||
| 613 | |||
| 614 | } | ||
| 615 | |||
| 616 | ############################################################################ | ||
| 617 | sub numNodes{ | ||
| 618 | ############################################################################ | ||
| 619 | # This public method returns the number of nodes that exist with the | ||
| 620 | # ontology | ||
| 621 | # | ||
| 622 | # Usage : | ||
| 623 | # | ||
| 624 | # my $numNodes = $ontologyParser->numNodes; | ||
| 625 | |||
| 626 | return scalar (keys %{$_[0]->{$kNodes}}); | ||
| 627 | |||
| 628 | } | ||
| 629 | |||
| 630 | ############################################################################ | ||
| 631 | sub serializeToDisk { | ||
| 632 | ############################################################################ | ||
| 633 | # Saves the current state of the Ontology Parser Object to a file, | ||
| 634 | # using the Storable package. Saves in network order for portability, | ||
| 635 | # just in case. Returns the name of the file. If no filename is | ||
| 636 | # provided, then the name of the file (and it's directory, if one was | ||
| 637 | # provided) used for object construction, will be used, with .obj | ||
| 638 | # appended. If the object was instantiated from a file with a .obj | ||
| 639 | # suffix, then the same filename would be used, if none were provided. | ||
| 640 | # | ||
| 641 | # This method currently causes a segfault on MacOSX (at least 10.1.5 | ||
| 642 | # -> 10.2.3), with perl 5.6, and Storable 1.0.14, when trying to store | ||
| 643 | # the process ontology. This failure occurs using either store, or | ||
| 644 | # nstore, and is manifested by a segmentation fault. It has not been | ||
| 645 | # investigated whether this is a perl problem, or a Storable problem | ||
| 646 | # (which has large amounts of C-code). This does not cause a | ||
| 647 | # segmentation on Solaris, using perl 5.6.1 and Storable 1.0.13. This | ||
| 648 | # doesn't make it clear whether it's a MacOSX problem or a perl | ||
| 649 | # problem or not. It should be noted that newer versions of both perl | ||
| 650 | # and Storable exist, and the code should be tested with those as | ||
| 651 | # well. | ||
| 652 | # | ||
| 653 | # Usage: | ||
| 654 | # | ||
| 655 | # my $objectFile = $ontologyParser->serializeToDisk(filename=>$filename); | ||
| 656 | |||
| 657 | my ($self, %args) = @_; | ||
| 658 | |||
| 659 | my $fileName; | ||
| 660 | |||
| 661 | if (exists ($args{'filename'})){ # they supply their own filename | ||
| 662 | |||
| 663 | $fileName = $args{'filename'}; | ||
| 664 | |||
| 665 | }else{ # we build a name from the file used to instantiate ourselves | ||
| 666 | |||
| 667 | $fileName = $self->__file; | ||
| 668 | |||
| 669 | if ($fileName !~ /\.obj$/){ # if we weren't instantiated from an object | ||
| 670 | |||
| 671 | $fileName .= ".obj"; # add a .obj suffix to the name | ||
| 672 | |||
| 673 | } | ||
| 674 | |||
| 675 | } | ||
| 676 | |||
| 677 | nstore ($self, $fileName) || die "$PACKAGE could not serialize itself to $fileName : $!"; | ||
| 678 | |||
| 679 | return ($fileName); | ||
| 680 | |||
| 681 | } | ||
| 682 | |||
| 683 | 1; # to keep perl happy | ||
| 684 | |||
| 685 | |||
| 686 | # P O D D O C U M E N T A T I O N # | ||
| 687 | |||
| 688 | =pod | ||
| 689 | |||
| 690 | =head1 Instance Constructor | ||
| 691 | |||
| 692 | =head2 new | ||
| 693 | |||
| 694 | This is the constructor for an OntologyParser object. The constructor | ||
| 695 | expects one of two arguments, either an 'ontologyFile' argument, or an | ||
| 696 | 'objectFile' argument. When instantiated with an ontologyFile | ||
| 697 | argument, it expects it to correspond to an ontology file created by | ||
| 698 | the GO consortium, according to their file format. When instantiated | ||
| 699 | with an objectFile argument, it expects to open a previously created | ||
| 700 | ontologyParser object that has been serialized to disk (see serializeToDisk). | ||
| 701 | |||
| 702 | Usage: | ||
| 703 | |||
| 704 | my $ontology = GO::OntologyProvider::OntologyParser->new(ontologyFile => $ontologyFile); | ||
| 705 | |||
| 706 | my $ontology = GO::OntologyProvider::OntologyParser->new(objectFile => $objectFile); | ||
| 707 | |||
| 708 | =head1 Instance Methods | ||
| 709 | |||
| 710 | =head2 printOntology | ||
| 711 | |||
| 712 | This prints out the ontology, with redundancies, to STDOUT. It does | ||
| 713 | not yet print out all of the ontology information (like relationship | ||
| 714 | type etc). This method will be likely be removed in a future version, | ||
| 715 | so should not be relied upon. | ||
| 716 | |||
| 717 | Usage: | ||
| 718 | |||
| 719 | $ontologyParser->printOntology; | ||
| 720 | |||
| 721 | =head2 allNodes | ||
| 722 | |||
| 723 | This method returns an array of all the GO:Nodes that have been | ||
| 724 | created. | ||
| 725 | |||
| 726 | Usage: | ||
| 727 | |||
| 728 | my @nodes = $ontologyParser->allNodes; | ||
| 729 | |||
| 730 | =head2 rootNode | ||
| 731 | |||
| 732 | This returns the root node in the ontology. | ||
| 733 | |||
| 734 | my $rootNode = $ontologyParser->rootNode; | ||
| 735 | |||
| 736 | =head2 nodeFromId | ||
| 737 | |||
| 738 | This public method takes a GOID and returns the GO::Node that | ||
| 739 | it corresponds to. | ||
| 740 | |||
| 741 | Usage : | ||
| 742 | |||
| 743 | my $node = $ontologyParser->nodeFromId($goid); | ||
| 744 | |||
| 745 | If the GOID does not correspond to a GO node, then undef will be | ||
| 746 | returned. Note if you try to call any methods on an undef, you will | ||
| 747 | get a fatal runtime error, so if you can't guarantee all GOIDs that | ||
| 748 | you supply are good, you should check that the return value from this | ||
| 749 | method is defined. | ||
| 750 | |||
| 751 | =head2 numNodes | ||
| 752 | |||
| 753 | This public method returns the number of nodes that exist with the | ||
| 754 | ontology | ||
| 755 | |||
| 756 | Usage : | ||
| 757 | |||
| 758 | my $numNodes = $ontologyParser->numNodes; | ||
| 759 | |||
| 760 | =head2 serializeToDisk | ||
| 761 | |||
| 762 | Saves the current state of the Ontology Parser Object to a file, using | ||
| 763 | the Storable package. Saves in network order for portability, just in | ||
| 764 | case. Returns the name of the file. If no filename is provided, then | ||
| 765 | the name of the file (and its directory, if one was provided) used for | ||
| 766 | object construction, will be used, with .obj appended. If the object | ||
| 767 | was instantiated from a file with a .obj suffix, then the same | ||
| 768 | filename would be used, if none were provided. | ||
| 769 | |||
| 770 | This method currently causes a segfault on MacOSX (at least 10.1.5 -> | ||
| 771 | 10.2.3), with perl 5.6, and Storable 1.0.14, when trying to store the | ||
| 772 | process ontology. This failure occurs using either store, or nstore, | ||
| 773 | and is manifested by a segmentation fault. It has not been | ||
| 774 | investigated whether this is a perl problem, or a Storable problem | ||
| 775 | (which has large amounts of C-code). This does not cause a | ||
| 776 | segmentation on Solaris, using perl 5.6.1 and Storable 1.0.13. This | ||
| 777 | does not make it clear whether it is a MacOSX problem or a perl | ||
| 778 | problem or not. It should be noted that newer versions of both perl | ||
| 779 | and Storable exist, and the code should be tested with those as well. | ||
| 780 | |||
| 781 | Usage: | ||
| 782 | |||
| 783 | my $objectFile = $ontologyParser->serializeToDisk(filename=>$filename); | ||
| 784 | |||
| 785 | =head1 Authors | ||
| 786 | |||
| 787 | Gavin Sherlock; sherlock@genome.stanford.edu | ||
| 788 | Elizabeth Boyle; ell@mit.edu | ||
| 789 | |||
| 790 | =cut |