| 1 | package GO::View; | ||
| 2 | |||
| 3 | ######################################################################### | ||
| 4 | # Module Name : View.pm | ||
| 5 | # | ||
| 6 | # Date created : Oct. 2003 | ||
| 7 | # | ||
| 8 | # Cared for by Shuai Weng <shuai@genome.stanford.edu> | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | ######################################################################### | ||
| 12 | |||
| 13 | # POD documentation - main docs before the code | ||
| 14 | |||
| 15 | # TODO | ||
| 16 | |||
| 17 | # have a much better options handling set of code, probably based on Getopt::Long | ||
| 18 | # see http://www.perl.com/pub/a/2007/07/12/options-and-configuration.html | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | GO::View - Creates a gif or png image for visualizing the GO DAG | ||
| 25 | |||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | This perl module generates a graphic that displays the parent and child | ||
| 30 | relationships of a selected GO term. It also provides the visualization | ||
| 31 | for the GO::TermFinder perl module created by the Stanford Microarray | ||
| 32 | Database (SMD). This module is useful when analyzing experimental or | ||
| 33 | computational results that produce a set of gene products that may have | ||
| 34 | a common function or process. | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use GO::View; | ||
| 39 | |||
| 40 | my $goView = | ||
| 41 | |||
| 42 | GO::View->new(-goid => $goid, | ||
| 43 | -ontologyProvider => $ontology, | ||
| 44 | -annotationProvider => $annotation, | ||
| 45 | -termFinder => \@pvalues, | ||
| 46 | -aspect => 'P', | ||
| 47 | -configFile => $confFile, | ||
| 48 | -imageDir => "/tmp", | ||
| 49 | -imageUrlRoot => "http://www.ABC.com/tmp", | ||
| 50 | -imageName => "GOview.88.png", | ||
| 51 | -tree => 'up', | ||
| 52 | -nodeUrl => $goUrl, | ||
| 53 | -geneUrl => $geneUrl, | ||
| 54 | -pvalueCutOff => '0.01', | ||
| 55 | -imageLabel => "SGD"); | ||
| 56 | |||
| 57 | |||
| 58 | argument required expect data and type | ||
| 59 | ------------------------------------------------------------------------- | ||
| 60 | -goid No A gene ontology ID (GOID). | ||
| 61 | If nothing is passed in, the module | ||
| 62 | will use the top goid of each ontology | ||
| 63 | branch (i.e, goid for | ||
| 64 | molecular_function, biological_process, | ||
| 65 | or cellular_component) | ||
| 66 | |||
| 67 | -ontologyProvider Yes An ontology provider instance. | ||
| 68 | |||
| 69 | -annotationProvider No An annotation provider instance. It is | ||
| 70 | required for creating tree for GO Term | ||
| 71 | Finder result. | ||
| 72 | |||
| 73 | -termFinder No An array of hash references returned | ||
| 74 | from 'findTerms' method of | ||
| 75 | GO::TermFinder module. It is required | ||
| 76 | for creating tree for GO Term Finder | ||
| 77 | result. | ||
| 78 | |||
| 79 | -aspect No <P|C|F>. The aspect of the ontology | ||
| 80 | provider. It is required for creating | ||
| 81 | tree for GO Term Finder result. | ||
| 82 | |||
| 83 | -configFile Yes The configuration file for setting the | ||
| 84 | general variables for the graphic | ||
| 85 | display. | ||
| 86 | |||
| 87 | -imageDir Yes The directory for storing the newly | ||
| 88 | created image file. It must be | ||
| 89 | world (nobody) readable and writable | ||
| 90 | if you want to display the image to | ||
| 91 | the web. | ||
| 92 | |||
| 93 | -imageUrlRoot No The url root for the -imageDir. It is | ||
| 94 | required if you want to display the | ||
| 95 | image to the web. | ||
| 96 | |||
| 97 | -imageName No The image file name. By default, the | ||
| 98 | name will be something like | ||
| 99 | 'GOview.xxxx.png'. The 'xxxx' will be | ||
| 100 | the process id. A suffix for the image (.png | ||
| 101 | or .gif) should not be provided, as that will | ||
| 102 | be determined at run time, depending on the | ||
| 103 | capabilities of the GD library. | ||
| 104 | |||
| 105 | -treeType No <up|down>. The tree type. | ||
| 106 | |||
| 107 | 1. up => display the ancestor tree | ||
| 108 | for the given goid. | ||
| 109 | 2. down => display the descendant tree | ||
| 110 | for the given goid. | ||
| 111 | By default, it will display the | ||
| 112 | descendant tree. | ||
| 113 | |||
| 114 | -geneUrl No The URL for each Gene to link to. | ||
| 115 | It needs to have the text <REPLACE_THIS> in | ||
| 116 | the url which will be substituted | ||
| 117 | by the real goid for a node. | ||
| 118 | |||
| 119 | -nodeUrl No The url for each GO node to link to. | ||
| 120 | It needs to have the text <REPLACE_THIS> in | ||
| 121 | the url which will be substituted | ||
| 122 | by the real goid for a node. | ||
| 123 | |||
| 124 | -pvalueCutOff No The p-value cutoff for displaying | ||
| 125 | the graphic for GO Term Finder. | ||
| 126 | The default is 0.01 | ||
| 127 | |||
| 128 | -imageLabel No The image label which will appear at | ||
| 129 | the left bottom corner of the map. | ||
| 130 | |||
| 131 | -maxTopNodeToShow No This argument is used to limit the | ||
| 132 | amount of the graph that might be | ||
| 133 | shown, for the sake of reducing run- | ||
| 134 | time. The default is 6. | ||
| 135 | |||
| 136 | ------------------------------------------------------------------------ | ||
| 137 | |||
| 138 | To display the image on the web: | ||
| 139 | |||
| 140 | $goView->showGraph; | ||
| 141 | |||
| 142 | To create and return image file name with full path: | ||
| 143 | |||
| 144 | my $imageFile = $goView->createImage; | ||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | =head1 FEEDBACK | ||
| 149 | |||
| 150 | =head2 Reporting Bugs | ||
| 151 | |||
| 152 | Bug reports can be submitted via email | ||
| 153 | |||
| 154 | shuai@genome.stanford.edu | ||
| 155 | |||
| 156 | =head1 AUTHOR | ||
| 157 | |||
| 158 | Shuai Weng, shuai@genome.stanford.edu | ||
| 159 | |||
| 160 | =head1 COPYRIGHT | ||
| 161 | |||
| 162 | Copyright (c) 2003 Stanford University. All Rights Reserved. | ||
| 163 | This module is free software; you can redistribute it and/or | ||
| 164 | modify it under the same terms as Perl itself. | ||
| 165 | |||
| 166 | =head1 APPENDIX | ||
| 167 | |||
| 168 | The rest of the documentation details each of the public methods. | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | use strict; | ||
| 173 | use warnings; | ||
| 174 | use GD; | ||
| 175 | use GraphViz; | ||
| 176 | use IO::File; | ||
| 177 | |||
| 178 | use GO::View::GD; | ||
| 179 | |||
| 180 | use vars qw ($PACKAGE $VERSION); | ||
| 181 | |||
| 182 | $PACKAGE = 'GO::View'; | ||
| 183 | $VERSION = 0.15; | ||
| 184 | |||
| 185 | my $kReplacementText = "<REPLACE_THIS>"; | ||
| 186 | |||
| 187 | ######################################################################### | ||
| 188 | |||
| 189 | =head1 METHODS | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | ######################################################################### | ||
| 194 | sub new { | ||
| 195 | ######################################################################### | ||
| 196 | |||
| 197 | =head2 new | ||
| 198 | |||
| 199 | Title : new | ||
| 200 | Function: Initializes the GO::View object. | ||
| 201 | : Recognized named parameters are -goid, -ontologyProvider, | ||
| 202 | -annotationProvider, -termFinder, -aspect, -configFile, | ||
| 203 | -imageDir, -imageUrlRoot, -imageName, -treeType, -nodeUrl, | ||
| 204 | -imageLabel | ||
| 205 | Returns : a new object | ||
| 206 | Args : named parameters | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | ######################################################################### | ||
| 211 | my ($class, %args) = @_; | ||
| 212 | |||
| 213 | my $self = bless {}, $class; | ||
| 214 | |||
| 215 | $self->_init(%args); | ||
| 216 | |||
| 217 | return $self; | ||
| 218 | |||
| 219 | } | ||
| 220 | |||
| 221 | ################################################################# | ||
| 222 | sub graph { | ||
| 223 | ################################################################# | ||
| 224 | |||
| 225 | =head2 graph | ||
| 226 | |||
| 227 | Title : graph | ||
| 228 | Usage : my $graph = $goView->graph; | ||
| 229 | Function: Gets the newly created Graphviz instance. | ||
| 230 | Returns : a new Graphviz instance. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | ################################################################ | ||
| 235 | |||
| 236 | return $_[0]->{GRAPH}; | ||
| 237 | |||
| 238 | } | ||
| 239 | |||
| 240 | ################################################################ | ||
| 241 | sub showGraph { | ||
| 242 | ################################################################ | ||
| 243 | |||
| 244 | =head2 showGraph | ||
| 245 | |||
| 246 | Title : showGraph | ||
| 247 | Usage : $goView->showGraph; | ||
| 248 | Function: Creates the image and print the map image to a file. | ||
| 249 | Returns : the name of the file to which the image was written | ||
| 250 | Throws : Exception if the imageUrlRoot is not passed to the object. | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | ######################################################################### | ||
| 255 | my ($self) = @_; | ||
| 256 | |||
| 257 | if ($self->graph) { | ||
| 258 | |||
| 259 | $self->_createAndShowImage; | ||
| 260 | |||
| 261 | } | ||
| 262 | |||
| 263 | return $self->{IMAGE_FILE}; | ||
| 264 | |||
| 265 | } | ||
| 266 | |||
| 267 | ######################################################################### | ||
| 268 | sub imageFile { | ||
| 269 | ######################################################################### | ||
| 270 | |||
| 271 | =head2 imageFile | ||
| 272 | |||
| 273 | Title : imageFile | ||
| 274 | Usage : my $imageFile = $goView->imageFile; | ||
| 275 | Function: Gets the newly created image file name (with full path). | ||
| 276 | Returns : image file name. | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | ######################################################################### | ||
| 281 | |||
| 282 | my ($self) = @_; | ||
| 283 | |||
| 284 | return $self->{IMAGE_FILE}; | ||
| 285 | |||
| 286 | |||
| 287 | } | ||
| 288 | |||
| 289 | ################################################################ | ||
| 290 | sub createImage { | ||
| 291 | ################################################################ | ||
| 292 | |||
| 293 | =head2 createImage | ||
| 294 | |||
| 295 | Title : createImage | ||
| 296 | Usage : $goView->createImage; | ||
| 297 | Function: Creates the GO tree image file. Calls it only if you | ||
| 298 | want to create the image file only and do not want to | ||
| 299 | display the image. | ||
| 300 | Returns : The newly created image file name with full path. | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | ######################################################################### | ||
| 305 | |||
| 306 | my ($self) = @_; | ||
| 307 | |||
| 308 | if ($self->graph) { | ||
| 309 | |||
| 310 | $self->{CREATE_IMAGE_ONLY} = 1; | ||
| 311 | |||
| 312 | return $self->_createAndShowImage; | ||
| 313 | |||
| 314 | } | ||
| 315 | |||
| 316 | } | ||
| 317 | |||
| 318 | ################################################################ | ||
| 319 | sub imageMap{ | ||
| 320 | ################################################################ | ||
| 321 | |||
| 322 | =head2 imageMap | ||
| 323 | |||
| 324 | Title : imageMap | ||
| 325 | Usage : my $map = $goView->imageMap; | ||
| 326 | Function : returns the text that constitutes an image map for the | ||
| 327 | created image. | ||
| 328 | Returns : a string | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | ######################################################################### | ||
| 333 | |||
| 334 | return $_[0]->{IMAGE_MAP}; | ||
| 335 | |||
| 336 | } | ||
| 337 | |||
| 338 | ################################################################ | ||
| 339 | sub _goid { | ||
| 340 | ################################################################ | ||
| 341 | # | ||
| 342 | # =head2 _goid | ||
| 343 | # | ||
| 344 | # Title : _goid | ||
| 345 | # Usage : my $goid = $self->_goid; | ||
| 346 | # Function: Gets the goid that client interface passed in. | ||
| 347 | # Returns : GOID | ||
| 348 | # Args : none | ||
| 349 | # | ||
| 350 | # =cut | ||
| 351 | # | ||
| 352 | ######################################################################### | ||
| 353 | |||
| 354 | my ($self) = @_; | ||
| 355 | |||
| 356 | return $self->{GOID}; | ||
| 357 | |||
| 358 | } | ||
| 359 | |||
| 360 | ######################################################################### | ||
| 361 | sub _ontologyProvider { | ||
| 362 | ######################################################################### | ||
| 363 | # | ||
| 364 | # =head2 _ontologyProvider | ||
| 365 | # | ||
| 366 | # Title : _ontologyProvider | ||
| 367 | # Usage : my $ontology = $self->_ontologyProvider; | ||
| 368 | # Function: Gets the ontology provider instance which is passed in by | ||
| 369 | # client interface. | ||
| 370 | # Returns : ontology provider instance | ||
| 371 | # Args : none | ||
| 372 | # | ||
| 373 | # =cut | ||
| 374 | # | ||
| 375 | ######################################################################### | ||
| 376 | |||
| 377 | my ($self) = @_; | ||
| 378 | |||
| 379 | return $self->{ONTOLOGY}; | ||
| 380 | |||
| 381 | } | ||
| 382 | |||
| 383 | ######################################################################### | ||
| 384 | sub _annotationProvider { | ||
| 385 | ######################################################################### | ||
| 386 | # | ||
| 387 | # =head2 _annotationProvider | ||
| 388 | # | ||
| 389 | # Title : _annotationProvider | ||
| 390 | # Usage : my $annotation = $self->_annotationProvider; | ||
| 391 | # Function: Gets the annotation provider instance which is passed in by | ||
| 392 | # client interface. | ||
| 393 | # Returns : annotation provider instance | ||
| 394 | # Args : none | ||
| 395 | # | ||
| 396 | # =cut | ||
| 397 | # | ||
| 398 | ######################################################################### | ||
| 399 | |||
| 400 | my ($self) = @_; | ||
| 401 | |||
| 402 | return $self->{ANNOTATION}; | ||
| 403 | |||
| 404 | } | ||
| 405 | |||
| 406 | ######################################################################### | ||
| 407 | sub _termFinder { | ||
| 408 | ######################################################################### | ||
| 409 | # | ||
| 410 | # =head2 _termFinder | ||
| 411 | # | ||
| 412 | # Title : _termFinder | ||
| 413 | # Usage : my $termFinder = $self->_termFinderProvider; | ||
| 414 | # Function: Gets the term finder result arrayref which is passed in by | ||
| 415 | # client interface. | ||
| 416 | # Returns : term finder result arrayref | ||
| 417 | # Args : none | ||
| 418 | # | ||
| 419 | # =cut | ||
| 420 | # | ||
| 421 | ######################################################################### | ||
| 422 | |||
| 423 | my ($self) = @_; | ||
| 424 | |||
| 425 | return $self->{TERM_FINDER}; | ||
| 426 | |||
| 427 | } | ||
| 428 | |||
| 429 | ######################################################################### | ||
| 430 | sub _init { | ||
| 431 | ######################################################################### | ||
| 432 | # | ||
| 433 | # =head2 _init | ||
| 434 | # | ||
| 435 | # Title : _init | ||
| 436 | # Usage : n/a; automatically called by new() | ||
| 437 | # Function: Initializes the variables required for creating the map. | ||
| 438 | # Returns : void | ||
| 439 | # Args : n/a | ||
| 440 | # Throws : Exception if ontology provider instance or tmp image | ||
| 441 | # directory for storing the image file are not passed | ||
| 442 | # to this object. | ||
| 443 | # | ||
| 444 | # =cut | ||
| 445 | # | ||
| 446 | ######################################################################### | ||
| 447 | |||
| 448 | my ($self, %args) = @_; | ||
| 449 | |||
| 450 | # first do some sanity checks | ||
| 451 | |||
| 452 | if (!$args{-ontologyProvider} || !$args{-configFile} || | ||
| 453 | !$args{-imageDir}) { | ||
| 454 | |||
| 455 | die "Can't build a $PACKAGE object without the ontologyProvider, configuration file, and tmp image directory name for storing the image file."; | ||
| 456 | |||
| 457 | } | ||
| 458 | |||
| 459 | $self->{ONTOLOGY} = $args{-ontologyProvider}; | ||
| 460 | |||
| 461 | if ($args{-goid} && $args{-goid} !~ /^[0-9]+$/ && | ||
| 462 | $args{-goid} !~ /^GO:[0-9]+$/) { | ||
| 463 | |||
| 464 | die "The goid ($args{-goid}) passed to $PACKAGE is is not in a recognized format, such as GO:0000166."; | ||
| 465 | |||
| 466 | } | ||
| 467 | |||
| 468 | my $goid = $args{-goid}; | ||
| 469 | |||
| 470 | # fix format of the GOID, so it is padded preceded with GO: and properly padded | ||
| 471 | |||
| 472 | if ($goid && $goid !~ /^GO:[0-9]+$/) { | ||
| 473 | |||
| 474 | $goid = $self->_formatGoid($goid); | ||
| 475 | |||
| 476 | } | ||
| 477 | |||
| 478 | # if we have no goid passed in, then we're likely creating an | ||
| 479 | # image based upon the output from GO::TermFinder. In this case | ||
| 480 | # we just set the goid to the node corresponding to the aspect | ||
| 481 | # that is being dealt with | ||
| 482 | |||
| 483 | if (!$goid) { | ||
| 484 | |||
| 485 | # set top goid for the given ontology (molecular_function, | ||
| 486 | # biological_process, or cellular_component). | ||
| 487 | |||
| 488 | $goid = $self->_initGoidFromOntology; | ||
| 489 | |||
| 490 | } | ||
| 491 | |||
| 492 | # now store the goid | ||
| 493 | |||
| 494 | $self->{GOID} = $goid; | ||
| 495 | |||
| 496 | # work out the image name and url - note that they will both | ||
| 497 | # receive a suffix later on that indicates the type of image that | ||
| 498 | # has been output (png or gif) | ||
| 499 | |||
| 500 | $self->{IMAGE_DIR} = $args{-imageDir}; | ||
| 501 | |||
| 502 | if ($self->{IMAGE_DIR} !~ /\/$/) { | ||
| 503 | |||
| 504 | $self->{IMAGE_DIR} .= "/"; | ||
| 505 | |||
| 506 | } | ||
| 507 | |||
| 508 | my $suffix; | ||
| 509 | |||
| 510 | if (GD::Image->can('png')){ | ||
| 511 | |||
| 512 | $suffix = 'png'; | ||
| 513 | |||
| 514 | }else{ | ||
| 515 | |||
| 516 | $suffix = 'gif'; | ||
| 517 | |||
| 518 | } | ||
| 519 | |||
| 520 | my $imageName; | ||
| 521 | |||
| 522 | if (exists $args{-imageName} && defined $args{-imageName} && $args{-imageName} ne ""){ | ||
| 523 | |||
| 524 | $imageName = $args{-imageName}; | ||
| 525 | |||
| 526 | }else{ | ||
| 527 | |||
| 528 | my $id = $$; | ||
| 529 | |||
| 530 | # now keep incrementing $id, until the name | ||
| 531 | # doesn't clash with an existing file | ||
| 532 | |||
| 533 | while (-e $self->{IMAGE_DIR}."GOview.$id.$suffix"){ | ||
| 534 | |||
| 535 | $id++; | ||
| 536 | |||
| 537 | } | ||
| 538 | |||
| 539 | $imageName = "GOview.$id"; | ||
| 540 | |||
| 541 | } | ||
| 542 | |||
| 543 | $imageName .= ".$suffix"; | ||
| 544 | |||
| 545 | $self->{IMAGE_FILE} = $self->{IMAGE_DIR}.$imageName; | ||
| 546 | |||
| 547 | if ($args{-imageUrlRoot}) { | ||
| 548 | |||
| 549 | $self->{IMAGE_URL} = $args{-imageUrlRoot}; | ||
| 550 | |||
| 551 | if ($self->{IMAGE_URL} !~ /\/$/) { $self->{IMAGE_URL} .= "/"; } | ||
| 552 | |||
| 553 | $self->{IMAGE_URL} .= $imageName; | ||
| 554 | |||
| 555 | }else{ | ||
| 556 | |||
| 557 | # if we didn't get a root url, we just assume that the image will | ||
| 558 | # be in the same directory as the image | ||
| 559 | |||
| 560 | $self->{IMAGE_URL} = "./".$imageName; | ||
| 561 | |||
| 562 | } | ||
| 563 | |||
| 564 | # now set the TREE_TYPE, which can be up or down. | ||
| 565 | |||
| 566 | $self->{TREE_TYPE} = $args{-treeType} || 'down'; | ||
| 567 | |||
| 568 | my $count; | ||
| 569 | |||
| 570 | $self->{MAX_TOP_NODE_TO_SHOW} = $args{-maxTopNodeToShow} || 6; | ||
| 571 | |||
| 572 | if ($args{-annotationProvider} && $args{-termFinder} && | ||
| 573 | $args{-aspect}) { | ||
| 574 | |||
| 575 | # we're dealing with GO::TermFinder output, so we'll | ||
| 576 | # store and initialize all the information we need | ||
| 577 | |||
| 578 | $self->{PVALUE_CUTOFF} = $args{-pvalueCutOff} || 0.01; | ||
| 579 | |||
| 580 | $self->{ANNOTATION} = $args{-annotationProvider}; | ||
| 581 | |||
| 582 | $self->{TERM_FINDER} = $args{-termFinder}; | ||
| 583 | |||
| 584 | $self->{ASPECT} = $args{-aspect}; | ||
| 585 | |||
| 586 | $count = $self->_initPvaluesGeneNamesDescendantGoids; | ||
| 587 | |||
| 588 | }elsif ($args{-annotationProvider} || $args{-termFinder} || | ||
| 589 | $args{-aspect}) { | ||
| 590 | |||
| 591 | die "You have to pass annotation provider and term finder instances and GO aspect ([P|F|C]) to $PACKAGE if you want to display graphic for term finder result."; | ||
| 592 | |||
| 593 | } | ||
| 594 | |||
| 595 | # store some more variables | ||
| 596 | |||
| 597 | $self->{IMAGE_LABEL} = $args{-imageLabel}; | ||
| 598 | |||
| 599 | $self->{GENE_URL} = $args{-geneUrl}; | ||
| 600 | |||
| 601 | $self->{GO_URL} = $args{-nodeUrl}; | ||
| 602 | |||
| 603 | $self->_initVariablesFromConfigFile($args{-configFile}); | ||
| 604 | |||
| 605 | # only make the graph if we had at least one node passing our p value cutoff | ||
| 606 | |||
| 607 | $self->_createGraph if ($count > 0); | ||
| 608 | |||
| 609 | } | ||
| 610 | |||
| 611 | ################################################################ | ||
| 612 | sub _createGraph { | ||
| 613 | ################################################################ | ||
| 614 | # | ||
| 615 | # =head2 _createGraph | ||
| 616 | # | ||
| 617 | # Title : _createGraph | ||
| 618 | # Usage : $self->_createGraph; | ||
| 619 | # automatically called by _init() | ||
| 620 | # Function: To create the GraphViz instance and add each descendant/ | ||
| 621 | # ancestor goid into the Graph tree. | ||
| 622 | # Returns : newly created GraphViz instance. | ||
| 623 | # | ||
| 624 | # =cut | ||
| 625 | # | ||
| 626 | ######################################################################### | ||
| 627 | |||
| 628 | my ($self) = @_; | ||
| 629 | |||
| 630 | # If client does not ask for up (ancestor) tree and this is not | ||
| 631 | # for the special tree paths client asked for the given goid to | ||
| 632 | # its specified descendants (i.e. for GO Term Finder), then we | ||
| 633 | # need to determine how many generations of the descendants we can | ||
| 634 | # display. | ||
| 635 | |||
| 636 | if ($self->{TREE_TYPE} !~ /up/i && | ||
| 637 | !$self->{DESCENDANT_GOID_ARRAY_REF}) { | ||
| 638 | |||
| 639 | $self->_setGeneration($self->_goid); | ||
| 640 | |||
| 641 | } | ||
| 642 | |||
| 643 | # If this is for the ancestor tree, we need to determine up to | ||
| 644 | # which ancestor we want to display the tree paths. We will | ||
| 645 | # display tree path up to $self->{TOP_GOID} | ||
| 646 | |||
| 647 | if ($self->{TREE_TYPE} =~ /up/i) { | ||
| 648 | |||
| 649 | $self->_setTopGoid; | ||
| 650 | |||
| 651 | if (!$self->{TOP_GOID}) { return; } | ||
| 652 | |||
| 653 | $self->{NODE_NUM} = $self->_descendantCount($self->{TOP_GOID}, | ||
| 654 | $self->{GENERATION}); | ||
| 655 | |||
| 656 | } | ||
| 657 | |||
| 658 | my $goid; | ||
| 659 | |||
| 660 | if ($self->{TOP_GOID}) { | ||
| 661 | |||
| 662 | $goid = $self->{TOP_GOID}; | ||
| 663 | |||
| 664 | }else { | ||
| 665 | |||
| 666 | $goid = $self->_goid; | ||
| 667 | |||
| 668 | } | ||
| 669 | |||
| 670 | $self->_createGraphObject; | ||
| 671 | |||
| 672 | my %foundNode; | ||
| 673 | my %foundEdge; | ||
| 674 | |||
| 675 | # add the top node to the graph | ||
| 676 | |||
| 677 | $self->_addNode($goid, | ||
| 678 | fillcolor => $self->_colorForNode($goid), | ||
| 679 | color => $self->_colorForNode($goid)); | ||
| 680 | |||
| 681 | # and record that we've seen it | ||
| 682 | |||
| 683 | $foundNode{$goid}++; | ||
| 684 | |||
| 685 | # draw go_path for ancestor goid ($self->{GOID}) to each | ||
| 686 | # descendant goid in @{$self->{DESCENDANT_GOID_ARRAY_REF}}. The | ||
| 687 | # DESCENDANT_GOID_ARRAY_REF is only created if we were dealing | ||
| 688 | # with output from GO::TermFinder | ||
| 689 | |||
| 690 | if ($self->{DESCENDANT_GOID_ARRAY_REF}) { | ||
| 691 | |||
| 692 | # get the top node | ||
| 693 | |||
| 694 | my $topAncestorNode = $self->_ontologyProvider->nodeFromId($self->_goid); | ||
| 695 | |||
| 696 | # and record its term | ||
| 697 | |||
| 698 | $self->{TERM_HASH_REF_FOR_GOID}{$self->_goid} = $topAncestorNode->term; | ||
| 699 | |||
| 700 | my $i; | ||
| 701 | |||
| 702 | # now go through every GO Node to which our list of genes is | ||
| 703 | # directly annotated that contibuted to the gene count of a | ||
| 704 | # node that passed the cutoff | ||
| 705 | |||
| 706 | foreach my $goid (@{$self->{DESCENDANT_GOID_ARRAY_REF}}) { | ||
| 707 | |||
| 708 | my $childNode = $self->_ontologyProvider->nodeFromId($goid); | ||
| 709 | |||
| 710 | # get the list of paths that link from this node up to the | ||
| 711 | # top - the first node in each path is the root, and the | ||
| 712 | # final node is the immediate parent of $childNode | ||
| 713 | |||
| 714 | my @path = $childNode->pathsToAncestor($topAncestorNode); | ||
| 715 | |||
| 716 | # now add that path to the graph | ||
| 717 | |||
| 718 | my $found = $self->_addAncestorPathToGraph($childNode, | ||
| 719 | \@path, | ||
| 720 | \%foundNode, | ||
| 721 | \%foundEdge); | ||
| 722 | |||
| 723 | # we can skip to the next goid if no new nodes were added | ||
| 724 | # to the graph | ||
| 725 | |||
| 726 | next if (!$found); | ||
| 727 | |||
| 728 | $i++; | ||
| 729 | |||
| 730 | # now add the genes that are annotated to this node | ||
| 731 | |||
| 732 | if ($self->{GENE_NAME_HASH_REF_FOR_GOID} && | ||
| 733 | $self->{GENE_NAME_HASH_REF_FOR_GOID}->{$goid}) { | ||
| 734 | |||
| 735 | my $loci = $self->{GENE_NAME_HASH_REF_FOR_GOID}->{$goid}; | ||
| 736 | |||
| 737 | $loci =~ s/:/ /g; | ||
| 738 | |||
| 739 | $self->_addNode($loci, | ||
| 740 | fillcolor => 'grey65', | ||
| 741 | color => 'grey65', | ||
| 742 | fontcolor => 'blue'); | ||
| 743 | |||
| 744 | $self->_addEdge($goid, $loci); | ||
| 745 | |||
| 746 | } | ||
| 747 | |||
| 748 | } | ||
| 749 | |||
| 750 | return; | ||
| 751 | |||
| 752 | } | ||
| 753 | |||
| 754 | # draw part of the tree, and it can go up and down the tree. | ||
| 755 | |||
| 756 | # draw up tree and only show ancestors in the paths from the given | ||
| 757 | # goid to the ancestor goid $self->{TOP_GOID} since there are too | ||
| 758 | # many nodes... | ||
| 759 | |||
| 760 | if ($self->{TREE_TYPE} =~ /up/i && | ||
| 761 | $self->{NODE_NUM} > $self->{MAX_NODE}) { | ||
| 762 | |||
| 763 | my $childNode = $self->_ontologyProvider->nodeFromId($self->_goid); | ||
| 764 | |||
| 765 | my $topAncestorNode = $self->_ontologyProvider->nodeFromId($goid); | ||
| 766 | |||
| 767 | my @path = $childNode->pathsToAncestor($topAncestorNode); | ||
| 768 | |||
| 769 | $self->_addAncestorPathToGraph($childNode, | ||
| 770 | \@path, | ||
| 771 | \%foundNode, | ||
| 772 | \%foundEdge); | ||
| 773 | |||
| 774 | return; | ||
| 775 | |||
| 776 | } | ||
| 777 | |||
| 778 | ##### draw down tree | ||
| 779 | |||
| 780 | my $node = $self->_ontologyProvider->nodeFromId($goid); | ||
| 781 | |||
| 782 | $self->_addChildOfTheNodeToGraph($node, | ||
| 783 | \%foundNode, | ||
| 784 | \%foundEdge); | ||
| 785 | |||
| 786 | } | ||
| 787 | |||
| 788 | ################################################################ | ||
| 789 | sub _addChildOfTheNodeToGraph { | ||
| 790 | ################################################################ | ||
| 791 | # | ||
| 792 | # =head2 _addChildOfTheNodeToGraph | ||
| 793 | # | ||
| 794 | # Title : _addChildOfTheNodeToGraph | ||
| 795 | # Usage : $self->_addChildOfTheNodeToGraph($node, | ||
| 796 | # $foundNodeHashRef, | ||
| 797 | # $foundEdgeHashRef); | ||
| 798 | # | ||
| 799 | # Function: To add each unique descendant of the given node to the | ||
| 800 | # graph tree. | ||
| 801 | # Returns : void | ||
| 802 | # | ||
| 803 | # =cut | ||
| 804 | # | ||
| 805 | ######################################################################### | ||
| 806 | |||
| 807 | # This is only called when we are dealing with a 'down' tree. It is | ||
| 808 | # not called when dealing with GO::TermFinder output | ||
| 809 | |||
| 810 | my ($self, $node, $foundNodeHashRef, $foundEdgeHashRef, | ||
| 811 | $generation) = @_; | ||
| 812 | |||
| 813 | if (!$generation) { $generation = 1; } | ||
| 814 | |||
| 815 | my @childNodes = $node->childNodes; | ||
| 816 | |||
| 817 | foreach my $childNode ($node->childNodes) { | ||
| 818 | |||
| 819 | my $parentGoid = $node->goid; | ||
| 820 | |||
| 821 | my $childGoid = $childNode->goid; | ||
| 822 | |||
| 823 | if (!$foundNodeHashRef->{$parentGoid}) { | ||
| 824 | |||
| 825 | $self->_addNode($parentGoid); | ||
| 826 | |||
| 827 | $foundNodeHashRef->{$parentGoid}++; | ||
| 828 | |||
| 829 | } | ||
| 830 | |||
| 831 | if (!$foundNodeHashRef->{$childGoid}) { | ||
| 832 | |||
| 833 | $self->_addNode($childGoid); | ||
| 834 | |||
| 835 | $foundNodeHashRef->{$childGoid}++; | ||
| 836 | |||
| 837 | } | ||
| 838 | if (!$foundEdgeHashRef->{$parentGoid."::".$childGoid}) { | ||
| 839 | |||
| 840 | $self->_addEdge($parentGoid, $childGoid); | ||
| 841 | |||
| 842 | $foundEdgeHashRef->{$parentGoid."::".$childGoid}++; | ||
| 843 | |||
| 844 | } | ||
| 845 | |||
| 846 | if ($generation < $self->{GENERATION}) { | ||
| 847 | |||
| 848 | $self->_addChildOfTheNodeToGraph($childNode, | ||
| 849 | $foundNodeHashRef, | ||
| 850 | $foundEdgeHashRef, | ||
| 851 | $generation++); | ||
| 852 | } | ||
| 853 | |||
| 854 | } | ||
| 855 | |||
| 856 | } | ||
| 857 | |||
| 858 | ################################################################ | ||
| 859 | sub _addAncestorPathToGraph { | ||
| 860 | ################################################################ | ||
| 861 | # | ||
| 862 | # =head2 _addAncestorPathToGraph | ||
| 863 | # | ||
| 864 | # Title : _addAncestorPathToGraph | ||
| 865 | # Usage : $self->_addAncestorPathToGraph($node, | ||
| 866 | # $ancestorPathArrayRef, | ||
| 867 | # $foundNodeHashRef, | ||
| 868 | # $foundEdgeHashRef); | ||
| 869 | # | ||
| 870 | # Function: To add each unique ancestor of the given node to the | ||
| 871 | # graph tree. | ||
| 872 | # Returns : void | ||
| 873 | # | ||
| 874 | # =cut | ||
| 875 | # | ||
| 876 | ######################################################################### | ||
| 877 | |||
| 878 | # This is only called when we are dealing with an 'up' tree, or when | ||
| 879 | # dealing with GO::TermFinder output | ||
| 880 | |||
| 881 | my ($self, $childNode, $ancestorPathArrayRef, | ||
| 882 | $foundNodeHashRef, $foundEdgeHashRef) = @_; | ||
| 883 | |||
| 884 | my $found; | ||
| 885 | |||
| 886 | # go through each path back to the root | ||
| 887 | |||
| 888 | foreach my $ancestorNodeArrayRef (@{$ancestorPathArrayRef}) { | ||
| 889 | |||
| 890 | # add the child node to the path, so it gets included too | ||
| 891 | |||
| 892 | push(@{$ancestorNodeArrayRef}, $childNode); | ||
| 893 | |||
| 894 | # reverse the order, so that we have the child node first, and | ||
| 895 | # the root last | ||
| 896 | |||
| 897 | @{$ancestorNodeArrayRef} = reverse(@{$ancestorNodeArrayRef}); | ||
| 898 | |||
| 899 | # now go through the path | ||
| 900 | |||
| 901 | for (my $i = 0; $i < @{$ancestorNodeArrayRef}; $i++) { | ||
| 902 | |||
| 903 | my ($goid1, $goid2); | ||
| 904 | |||
| 905 | # get the goid for the current node, and store it's term | ||
| 906 | |||
| 907 | if (defined $ancestorNodeArrayRef->[$i]) { | ||
| 908 | |||
| 909 | $goid1 = $ancestorNodeArrayRef->[$i]->goid; | ||
| 910 | |||
| 911 | $self->{TERM_HASH_REF_FOR_GOID}{$goid1} | ||
| 912 | = $ancestorNodeArrayRef->[$i]->term; | ||
| 913 | |||
| 914 | } | ||
| 915 | |||
| 916 | # get the goid for the next node (the current node's | ||
| 917 | # parent), and store it's term too | ||
| 918 | |||
| 919 | if (defined $ancestorNodeArrayRef->[$i+1]) { | ||
| 920 | |||
| 921 | $goid2 = $ancestorNodeArrayRef->[$i+1]->goid; | ||
| 922 | |||
| 923 | $self->{TERM_HASH_REF_FOR_GOID}{$goid2} | ||
| 924 | = $ancestorNodeArrayRef->[$i+1]->term; | ||
| 925 | |||
| 926 | } | ||
| 927 | |||
| 928 | # if the current node isn't already on the graph, add it | ||
| 929 | |||
| 930 | if ($goid1 && !$foundNodeHashRef->{$goid1}) { | ||
| 931 | |||
| 932 | $self->_addNode($goid1, | ||
| 933 | fillcolor => $self->_colorForNode($goid1), | ||
| 934 | color => $self->_colorForNode($goid1)); | ||
| 935 | |||
| 936 | # record that we've seen it | ||
| 937 | |||
| 938 | $foundNodeHashRef->{$goid1}++; | ||
| 939 | |||
| 940 | } | ||
| 941 | |||
| 942 | # if we have a parent, and we haven't yet recorded an edge | ||
| 943 | # between the child and parent, let's add that | ||
| 944 | |||
| 945 | if ($goid1 && $goid2 && | ||
| 946 | !$foundEdgeHashRef->{$goid2."::".$goid1}) { | ||
| 947 | |||
| 948 | $self->_addEdge($goid2, $goid1); | ||
| 949 | |||
| 950 | # record that we've added the edge | ||
| 951 | |||
| 952 | $foundEdgeHashRef->{$goid2."::".$goid1}++; | ||
| 953 | |||
| 954 | } | ||
| 955 | |||
| 956 | } | ||
| 957 | |||
| 958 | # record that something has been added to the graph (as it's | ||
| 959 | # possible that we may end up finding we've added everything | ||
| 960 | |||
| 961 | $found++; | ||
| 962 | |||
| 963 | } | ||
| 964 | |||
| 965 | # return whether something was added to the graph | ||
| 966 | |||
| 967 | return $found; | ||
| 968 | |||
| 969 | } | ||
| 970 | |||
| 971 | ################################################################ | ||
| 972 | sub _createAndShowImage { | ||
| 973 | ################################################################ | ||
| 974 | # | ||
| 975 | # =head2 _createAndShowImage | ||
| 976 | # | ||
| 977 | # Title : _createAndShowImage | ||
| 978 | # Usage : $self->_createAndShowImage(); | ||
| 979 | # automatically called by showGraph() and createImage(). | ||
| 980 | # Function: To create the graph tree image. It will print the image to | ||
| 981 | # stdout if it is called by showGraph(). | ||
| 982 | # Returns : returns graphText file if the text format is changed. | ||
| 983 | # returns image file name if called by createImage(). | ||
| 984 | # | ||
| 985 | # =cut | ||
| 986 | # | ||
| 987 | ######################################################################### | ||
| 988 | |||
| 989 | my ($self) = @_; | ||
| 990 | |||
| 991 | my ($width, $height); | ||
| 992 | |||
| 993 | # first thing we do is get the contents of the graph in text form. | ||
| 994 | # We will then use this text to create a gif or png image, with | ||
| 995 | # various boxes etc that have the coordinates as indicated by the | ||
| 996 | # text from the graph image. | ||
| 997 | |||
| 998 | # the actual contents of the text string will be something like: | ||
| 999 | |||
| 1000 | # digraph test { | ||
| 1001 | # node [label="\N", shape=box]; | ||
| 1002 | # graph [bb="0,0,662,1012"]; | ||
| 1003 | # node1 [label=" biological_process\nGO:GO:0008150", pos="342,986", width="1.97", height="0.50"]; | ||
| 1004 | # node2 [label="pre-replicative\ncomplex\nformation and\n maintenance\nGO:GO:0006267", pos="162,122", width="1.69", height="1.17"]; | ||
| 1005 | # node3 [label="DNA-dependent\n DNA replication\nGO:GO:0006261", pos="353,226", width="1.86", height="0.72"]; | ||
| 1006 | # node4 [label=" DNA replication\nGO:GO:0006260", pos="353,306", width="1.86", height="0.50"]; | ||
| 1007 | # node5 [label="DNA replication\nand chromosome\n cycle\nGO:GO:0000067", pos="299,394", width="1.83", height="0.94"]; | ||
| 1008 | # node6 [label=" cell cycle\nGO:GO:0007049", pos="271,482", width="1.72", height="0.50"]; | ||
| 1009 | # node7 [label="cell\n proliferation\nGO:GO:0008283", pos="271,586", width="1.67", height="0.72"]; | ||
| 1010 | # node8 [label="cell growth\nand/or\n maintenance\nGO:GO:0008151", pos="271,706", width="1.61", height="0.94"]; | ||
| 1011 | # node9 [label="cellular\nphysiological\n process\nGO:GO:0050875", pos="272,810", width="1.67", height="0.94"]; | ||
| 1012 | # node10 [label="cellular\n process\nGO:GO:0009987", pos="272,906", width="1.69", height="0.72"]; | ||
| 1013 | # node11 [label="physiological\n process\nGO:GO:0007582", pos="412,906", width="1.69", height="0.72"]; | ||
| 1014 | # node12 [label=" DNA metabolism\nGO:GO:0006259", pos="422,482", width="1.97", height="0.50"]; | ||
| 1015 | # node13 [label="nucleobase,\nnucleoside,\nnucleotide and\nnucleic acid\n metabolism\nGO:GO:0006139", pos="421,586", width="1.64", height="1.39"]; | ||
| 1016 | # node14 [label=" metabolism\nGO:GO:0008152", pos="420,706", width="1.64", height="0.50"]; | ||
| 1017 | # node15 [label=" 1: MCM4 MCM3 CDC6 MCM2", pos="119,26", width="3.31", height="0.50"]; | ||
| 1018 | # node16 [label=" DNA unwinding\nGO:GO:0006268", pos="353,122", width="1.92", height="0.50"]; | ||
| 1019 | # node17 [label=" 2: MCM4 MCM3 MCM2", pos="353,26", width="2.69", height="0.50"]; | ||
| 1020 | # node18 [label="DNA replication\n initiation\nGO:GO:0006270", pos="534,122", width="1.78", height="0.72"]; | ||
| 1021 | # node19 [label=" 3: MCM4 MCM3 MCM2", pos="565,26", width="2.69", height="0.50"]; | ||
| 1022 | # node5 -> node4 [pos="e,342,324 320,360 325,351 331,341 337,332"]; | ||
| 1023 | # node13 -> node12 [pos="e,422,500 421,536 421,527 422,517 422,509"]; | ||
| 1024 | # node12 -> node4 [pos="e,360,324 415,464 402,433 377,369 363,333"]; | ||
| 1025 | # node4 -> node3 [pos="e,353,252 353,288 353,280 353,271 353,262"]; | ||
| 1026 | # node3 -> node2 [pos="e,223,155 305,200 283,187 256,173 232,160"]; | ||
| 1027 | # node3 -> node16 [pos="e,353,140 353,200 353,184 353,165 353,149"]; | ||
| 1028 | # node3 -> node18 [pos="e,489,148 399,200 423,186 454,168 480,153"]; | ||
| 1029 | # node2 -> node15 [pos="e,127,44 143,80 139,70 135,61 131,52"]; | ||
| 1030 | # node16 -> node17 [pos="e,353,44 353,104 353,90 353,69 353,53"]; | ||
| 1031 | # node18 -> node19 [pos="e,559,44 542,96 547,82 552,66 556,53"]; | ||
| 1032 | # node6 -> node5 [pos="e,288,428 277,464 279,457 282,447 285,438"]; | ||
| 1033 | # node11 -> node14 [pos="e,419,724 413,880 415,842 418,773 419,734"]; | ||
| 1034 | # node11 -> node9 [pos="e,322,844 374,880 361,871 345,860 330,850"]; | ||
| 1035 | # node1 -> node11 [pos="e,389,932 358,968 365,959 374,949 382,940"]; | ||
| 1036 | # node1 -> node10 [pos="e,295,932 326,968 319,959 310,949 302,940"]; | ||
| 1037 | # node8 -> node7 [pos="e,271,612 271,672 271,656 271,638 271,621"]; | ||
| 1038 | # node14 -> node13 [pos="e,420,636 420,688 420,677 420,661 420,647"]; | ||
| 1039 | # node7 -> node6 [pos="e,271,500 271,560 271,544 271,525 271,509"]; | ||
| 1040 | # node10 -> node9 [pos="e,272,844 272,880 272,872 272,863 272,854"]; | ||
| 1041 | # node9 -> node8 [pos="e,271,740 272,776 272,768 271,758 271,749"]; | ||
| 1042 | # } | ||
| 1043 | |||
| 1044 | # a description of the dot language can be found at: | ||
| 1045 | # | ||
| 1046 | # http://www.research.att.com/~erg/graphviz/info/lang.html | ||
| 1047 | |||
| 1048 | if (defined $self->{MAKE_PS} && $self->{MAKE_PS}){ | ||
| 1049 | |||
| 1050 | my $file = $self->{IMAGE_FILE}; | ||
| 1051 | |||
| 1052 | $file =~ s/\.\w+$/\.ps/; | ||
| 1053 | |||
| 1054 | my $fh = IO::File->new($file, q{>} )|| die "Cannot create $file : $!"; | ||
| 1055 | |||
| 1056 | print $fh $self->graph->as_ps; | ||
| 1057 | |||
| 1058 | $fh->close; | ||
| 1059 | |||
| 1060 | } | ||
| 1061 | |||
| 1062 | # hence we can determine the size of the image, the positions and sizes | ||
| 1063 | # of every box, and how to draw the edges between the boxes | ||
| 1064 | |||
| 1065 | my $graphText = $self->graph->as_text; | ||
| 1066 | |||
| 1067 | # the following line *may* fix reported problems when running on | ||
| 1068 | # Windows, that I think are a result of dot.exe using CRLF line | ||
| 1069 | # endings. | ||
| 1070 | |||
| 1071 | $graphText =~ s/\015?\012/\n/g; | ||
| 1072 | |||
| 1073 | # if we can get the height and width, we'll get them | ||
| 1074 | |||
| 1075 | if ($graphText =~ /graph \[bb=\"0,0,([0-9]+),([0-9]+)\" *\]\;/) { | ||
| 1076 | |||
| 1077 | $width = $1 * $self->{WIDTH_DISPLAY_RATIO}; | ||
| 1078 | |||
| 1079 | $height = $2 * $self->{HEIGHT_DISPLAY_RATIO}; | ||
| 1080 | |||
| 1081 | }else { | ||
| 1082 | |||
| 1083 | # otherwise, we simply create a png image and we're done | ||
| 1084 | |||
| 1085 | # this seems to be undocumented - I'm not sure under what | ||
| 1086 | # circumstances we can't actually get the height and width | ||
| 1087 | |||
| 1088 | $self->graph->as_png($self->{IMAGE_DIR}."goPath.$$.png"); | ||
| 1089 | |||
| 1090 | return $self->{IMAGE_DIR}."goPath.$$.png"; | ||
| 1091 | |||
| 1092 | } | ||
| 1093 | |||
| 1094 | my @graphLine = split(/\n/, $graphText); | ||
| 1095 | |||
| 1096 | # add borders sizes to the height and width | ||
| 1097 | |||
| 1098 | my $border = 25; | ||
| 1099 | |||
| 1100 | my $mapWidth = $width + 2 * $border; | ||
| 1101 | |||
| 1102 | my $mapHeight = $height + 2 * $border; | ||
| 1103 | |||
| 1104 | my $keyY; | ||
| 1105 | |||
| 1106 | # now modify the height and width according to unclear rules.... | ||
| 1107 | |||
| 1108 | if ($self->{PVALUE_HASH_REF_FOR_GOID} || !$self->{GENE_NAME_HASH_REF_FOR_GOID}) { | ||
| 1109 | |||
| 1110 | $keyY = $mapHeight; | ||
| 1111 | |||
| 1112 | # make the width to be at least the minimum acceptable width | ||
| 1113 | |||
| 1114 | if ($mapWidth < $self->{MIN_MAP_WIDTH}) { | ||
| 1115 | |||
| 1116 | $mapWidth = $self->{MIN_MAP_WIDTH}; | ||
| 1117 | |||
| 1118 | } | ||
| 1119 | |||
| 1120 | # modify the height | ||
| 1121 | |||
| 1122 | if (!$self->{GENE_NAME_HASH_REF_FOR_GOID}) { | ||
| 1123 | |||
| 1124 | # if there are no genes annotated to nodes, use some | ||
| 1125 | # complex, opaque and unclear rule to change the height | ||
| 1126 | |||
| 1127 | $mapHeight += int((length($self->{MAP_NOTE})*6/($mapWidth-100))*15) + 65; | ||
| 1128 | |||
| 1129 | }else { | ||
| 1130 | |||
| 1131 | # otherwise just add 50, the 'magic number'... | ||
| 1132 | |||
| 1133 | $mapHeight += 50; | ||
| 1134 | |||
| 1135 | } | ||
| 1136 | |||
| 1137 | } | ||
| 1138 | |||
| 1139 | # now create a GD image of the appropriate height and width | ||
| 1140 | |||
| 1141 | my $gd = GO::View::GD->new(width => $mapWidth, | ||
| 1142 | height => $mapHeight); | ||
| 1143 | |||
| 1144 | # add a border, with a label and a date | ||
| 1145 | |||
| 1146 | $self->_drawFrame($gd, $mapWidth, $mapHeight); | ||
| 1147 | |||
| 1148 | my (@nodeLine, @edgeLine); | ||
| 1149 | |||
| 1150 | my $preLine; | ||
| 1151 | |||
| 1152 | # now go through each line of the output from the graph->as_text method | ||
| 1153 | |||
| 1154 | foreach my $line (@graphLine) { | ||
| 1155 | |||
| 1156 | if ($line =~ /\\$/) { | ||
| 1157 | |||
| 1158 | # if it ends with a backslash (i.e. is a wrapped line), we | ||
| 1159 | # simply remove the trailing slash, and any leading | ||
| 1160 | # spaces, and remember it. | ||
| 1161 | |||
| 1162 | $line =~ s/\\$//; | ||
| 1163 | |||
| 1164 | $line =~ s/^ *//; | ||
| 1165 | |||
| 1166 | $preLine .= $line; | ||
| 1167 | |||
| 1168 | next; | ||
| 1169 | |||
| 1170 | }elsif ($preLine && $line =~ /\;$/ && $line !~ / *node[0-9]/) { | ||
| 1171 | |||
| 1172 | # if we have some remembered previous line, and this line | ||
| 1173 | # ends in a semi-colon (which terminates the entity), and | ||
| 1174 | # this line does not begin the definition of a node, then | ||
| 1175 | # we add the previous line information to this line, and | ||
| 1176 | # undef the $preLine variable | ||
| 1177 | |||
| 1178 | $line = $preLine.$line; | ||
| 1179 | |||
| 1180 | undef $preLine; | ||
| 1181 | |||
| 1182 | } | ||
| 1183 | |||
| 1184 | # now store type of entity (nodes vs edges) in different | ||
| 1185 | # arrays | ||
| 1186 | |||
| 1187 | if ($line =~ / *node[0-9]+ *\[(label=.+)\]\;$/i) { | ||
| 1188 | |||
| 1189 | # it's a node | ||
| 1190 | |||
| 1191 | push(@nodeLine, $1); | ||
| 1192 | |||
| 1193 | }elsif ($line =~ / *node[0-9]+ *-> *node[0-9]+ \[pos=\"(.+)\"\]\;$/i) { | ||
| 1194 | |||
| 1195 | # it's an edge | ||
| 1196 | |||
| 1197 | push(@edgeLine, $1); | ||
| 1198 | |||
| 1199 | } | ||
| 1200 | |||
| 1201 | } | ||
| 1202 | |||
| 1203 | # add the keys, which are either keys for the p-value colors, or a | ||
| 1204 | # general description about GO terms and their annotations | ||
| 1205 | |||
| 1206 | if (exists $self->{PVALUE_HASH_REF_FOR_GOID} && | ||
| 1207 | $height > $self->{MIN_MAP_WIDTH_FOR_ONE_LINE_KEY}) { | ||
| 1208 | |||
| 1209 | ### draw keys on the top of the map | ||
| 1210 | if ($width < $self->{MIN_MAP_WIDTH_FOR_ONE_LINE_KEY}) { | ||
| 1211 | |||
| 1212 | $self->{MOVE_Y} = 15; | ||
| 1213 | |||
| 1214 | } | ||
| 1215 | |||
| 1216 | $self->_drawKeys($gd, $mapWidth, 5, 'isTop'); | ||
| 1217 | |||
| 1218 | } | ||
| 1219 | |||
| 1220 | if ($self->{PVALUE_HASH_REF_FOR_GOID} || | ||
| 1221 | !$self->{GENE_NAME_HASH_REF_FOR_GOID}) { | ||
| 1222 | |||
| 1223 | $self->_drawKeys($gd, $mapWidth, $keyY); | ||
| 1224 | |||
| 1225 | } | ||
| 1226 | |||
| 1227 | # now draw the actual edges and nodes | ||
| 1228 | |||
| 1229 | # do the edges first | ||
| 1230 | |||
| 1231 | foreach my $line (@edgeLine) { | ||
| 1232 | |||
| 1233 | $self->_drawEdge($gd, $height, $border, $line); | ||
| 1234 | |||
| 1235 | } | ||
| 1236 | |||
| 1237 | # and now the nodes | ||
| 1238 | |||
| 1239 | foreach my $line (@nodeLine) { | ||
| 1240 | |||
| 1241 | $self->_drawNode($gd, $height, $border, $line); | ||
| 1242 | |||
| 1243 | } | ||
| 1244 | |||
| 1245 | # now output the image to a file | ||
| 1246 | |||
| 1247 | my $imageFile = $self->{IMAGE_FILE}; | ||
| 1248 | my $imageUrl = $self->{IMAGE_URL}; | ||
| 1249 | |||
| 1250 | my $fh = IO::File->new($imageFile, q{>} )|| die "Cannot create $imageFile : $!"; | ||
| 1251 | |||
| 1252 | binmode $fh; | ||
| 1253 | |||
| 1254 | if ($gd->im->can('png')) { | ||
| 1255 | |||
| 1256 | print $fh $gd->im->png; | ||
| 1257 | |||
| 1258 | }else { | ||
| 1259 | |||
| 1260 | print $fh $gd->im->gif; | ||
| 1261 | |||
| 1262 | } | ||
| 1263 | |||
| 1264 | $fh->close; | ||
| 1265 | |||
| 1266 | if ($self->{CREATE_IMAGE_ONLY}) { | ||
| 1267 | |||
| 1268 | return $imageFile; | ||
| 1269 | |||
| 1270 | }else{ | ||
| 1271 | |||
| 1272 | my $map = $gd->imageMap; | ||
| 1273 | |||
| 1274 | if (defined ($map)){ | ||
| 1275 | |||
| 1276 | $self->{IMAGE_MAP} = | ||
| 1277 | |||
| 1278 | "<MAP NAME='goPathImage'>\n". | ||
| 1279 | $gd->imageMap. | ||
| 1280 | "</MAP>". | ||
| 1281 | "<center><img src='$imageUrl' usemap='#goPathImage'></center><p>\n"; | ||
| 1282 | |||
| 1283 | } | ||
| 1284 | |||
| 1285 | } | ||
| 1286 | |||
| 1287 | } | ||
| 1288 | |||
| 1289 | ###################################################################### | ||
| 1290 | sub _drawNode { | ||
| 1291 | ###################################################################### | ||
| 1292 | # | ||
| 1293 | # =head2 _drawNode | ||
| 1294 | # | ||
| 1295 | # Title : _drawNode | ||
| 1296 | # Usage : $self->_drawNode($gd, $height, $border, $line); | ||
| 1297 | # automatically called by _createAndShowImage(). | ||
| 1298 | # Function: To draw each node. | ||
| 1299 | # Returns : void | ||
| 1300 | # | ||
| 1301 | # =cut | ||
| 1302 | # | ||
| 1303 | ######################################################################### | ||
| 1304 | |||
| 1305 | my ($self, $gd, $height, $border, $line) = @_; | ||
| 1306 | |||
| 1307 | # let's let's extract the information from the line, e.g. which may look like: | ||
| 1308 | # | ||
| 1309 | # label="MCM4 MCM3 MCM2", pos="565,26", width="2.69", height="0.50" | ||
| 1310 | # | ||
| 1311 | # or: | ||
| 1312 | # | ||
| 1313 | # label=MET28, color=grey65, fillcolor=grey65, fontcolor=blue, pos="735,561", width="0.92", height="0.50" | ||
| 1314 | # | ||
| 1315 | # or: | ||
| 1316 | # | ||
| 1317 | # label=" DNA unwinding\nGO:GO:0006268", pos="353,122", width="1.92", height="0.50" | ||
| 1318 | # | ||
| 1319 | # depending on whether it's genes annotating a GO node, or a box | ||
| 1320 | # representing a GO Node itself | ||
| 1321 | |||
| 1322 | # get the label - may or may not be surrounded in quotes | ||
| 1323 | |||
| 1324 | my $label; | ||
| 1325 | |||
| 1326 | if ($line =~ /label=\"([^\"]*)\"/){ | ||
| 1327 | |||
| 1328 | $label = $1; | ||
| 1329 | |||
| 1330 | }elsif ($line =~ /label=(.+?), /){ | ||
| 1331 | |||
| 1332 | $label = $1; | ||
| 1333 | |||
| 1334 | } | ||
| 1335 | |||
| 1336 | my @label = split(/\\n/, $label); | ||
| 1337 | |||
| 1338 | # get the width and height, and convert to number of pixels - I | ||
| 1339 | # don't know where the use of the value '60' comes from. The documentation for graphviz says that | ||
| 1340 | # the default scale is actually 72 pixels per inch... | ||
| 1341 | |||
| 1342 | $line =~ /width=\"([^\"]+)\"/; | ||
| 1343 | |||
| 1344 | my $boxW = $1 * 60; | ||
| 1345 | |||
| 1346 | $line =~ /height=\"([^\"]+)\"/; | ||
| 1347 | |||
| 1348 | my $boxH = $1 * 60; | ||
| 1349 | |||
| 1350 | # remove some arbitrary amount based on the label - presumably | ||
| 1351 | # this is because we're removing the GO id from the label? | ||
| 1352 | |||
| 1353 | $boxH -= 4*(@label-1); | ||
| 1354 | |||
| 1355 | # and for some reason subtract an extra 10 if we have a | ||
| 1356 | # p-value for it? | ||
| 1357 | |||
| 1358 | if ($self->{PVALUE_HASH_REF_FOR_GOID}) { | ||
| 1359 | |||
| 1360 | $boxH -= 10; | ||
| 1361 | |||
| 1362 | } | ||
| 1363 | |||
| 1364 | if (!$self->{MOVE_Y}) { | ||
| 1365 | |||
| 1366 | $self->{MOVE_Y} = 0; | ||
| 1367 | |||
| 1368 | } | ||
| 1369 | |||
| 1370 | # now get the coordinates of the center of box for the node, and | ||
| 1371 | # use that to work out the coordinate of the bounding box for the | ||
| 1372 | # node. | ||
| 1373 | |||
| 1374 | $line =~ /pos=\"([0-9]+),([0-9]+)\"/; | ||
| 1375 | |||
| 1376 | my $x1 = $1 * $self->{WIDTH_DISPLAY_RATIO}-$boxW/2 + $border; | ||
| 1377 | |||
| 1378 | my $y1 = $height - $2 * $self->{HEIGHT_DISPLAY_RATIO} - $boxH/2 + $border + $self->{MOVE_Y}; | ||
| 1379 | |||
| 1380 | my $x2 = $x1 + $boxW; | ||
| 1381 | |||
| 1382 | my $y2 = $y1 + $boxH; | ||
| 1383 | |||
| 1384 | my $goid; | ||
| 1385 | |||
| 1386 | if ($label =~ /(GO:[0-9]+)$/) { | ||
| 1387 | |||
| 1388 | $goid = $1; | ||
| 1389 | |||
| 1390 | } | ||
| 1391 | |||
| 1392 | if (!$goid) { | ||
| 1393 | |||
| 1394 | $boxH = 9*(@label) + 4; | ||
| 1395 | |||
| 1396 | } | ||
| 1397 | |||
| 1398 | my $geneNum; | ||
| 1399 | my $totalGeneNum; | ||
| 1400 | my $barColor; | ||
| 1401 | my $outline; | ||
| 1402 | my $linkUrl; | ||
| 1403 | |||
| 1404 | # now work out the color for the box, and work out URL links | ||
| 1405 | # for the nodes | ||
| 1406 | |||
| 1407 | if ($self->{PVALUE_HASH_REF_FOR_GOID} && $goid) { | ||
| 1408 | |||
| 1409 | # we must be dealing with GO::TermFinder output for a goid | ||
| 1410 | |||
| 1411 | $barColor = $self->_getBoxColor($gd, $goid); | ||
| 1412 | |||
| 1413 | if (!$self->{CREATE_IMAGE_ONLY}) { | ||
| 1414 | |||
| 1415 | $linkUrl = $self->{GO_URL}; | ||
| 1416 | |||
| 1417 | $linkUrl =~ s/$kReplacementText/$goid/ if $linkUrl; | ||
| 1418 | |||
| 1419 | } | ||
| 1420 | |||
| 1421 | }elsif ($goid) { | ||
| 1422 | |||
| 1423 | # non GO::TermFinder output for a GOID | ||
| 1424 | |||
| 1425 | my $node = $self->_ontologyProvider->nodeFromId($goid); | ||
| 1426 | |||
| 1427 | if ($node && $node->childNodes) { | ||
| 1428 | |||
| 1429 | $barColor = $gd->lightBlue; | ||
| 1430 | |||
| 1431 | if (!$self->{CREATE_IMAGE_ONLY}) { | ||
| 1432 | |||
| 1433 | $linkUrl = $self->{GO_URL}; | ||
| 1434 | |||
| 1435 | $linkUrl =~ s/$kReplacementText/$goid/ if $linkUrl; | ||
| 1436 | |||
| 1437 | } | ||
| 1438 | |||
| 1439 | }else { | ||
| 1440 | |||
| 1441 | # the box isn't representing a GOID, e.g. annotating genes | ||
| 1442 | |||
| 1443 | $barColor = $gd->grey; | ||
| 1444 | |||
| 1445 | } | ||
| 1446 | |||
| 1447 | }else { | ||
| 1448 | |||
| 1449 | $barColor = $gd->grey; | ||
| 1450 | |||
| 1451 | } | ||
| 1452 | |||
| 1453 | |||
| 1454 | my $onInfoText; | ||
| 1455 | |||
| 1456 | if (( $self->{TOP_GOID} && $goid && $goid eq $self->{TOP_GOID}) || | ||
| 1457 | (!$self->{TOP_GOID} && $goid && $goid eq $self->_goid)) { | ||
| 1458 | |||
| 1459 | $self->_drawUpArrow($gd, $goid, ($x1+$x2)/2-7, | ||
| 1460 | ($x1+$x2)/2+7, $y1-15, 10, | ||
| 1461 | $linkUrl); | ||
| 1462 | |||
| 1463 | } | ||
| 1464 | |||
| 1465 | # now draw the box itself | ||
| 1466 | |||
| 1467 | $gd->drawBar(barColor => $barColor, | ||
| 1468 | numX1 => $x1, | ||
| 1469 | numX2 => $x2, | ||
| 1470 | numY => $y1, | ||
| 1471 | linkUrl => $linkUrl, | ||
| 1472 | barHeight => $boxH, | ||
| 1473 | outline => $outline, | ||
| 1474 | onInfoText => $onInfoText); | ||
| 1475 | |||
| 1476 | |||
| 1477 | # and now add the label to the box, one line at a time | ||
| 1478 | |||
| 1479 | my $i = 0; | ||
| 1480 | |||
| 1481 | foreach my $label (@label) { | ||
| 1482 | |||
| 1483 | # skip if it's a GOID or is blank | ||
| 1484 | |||
| 1485 | next if (!$label || $label =~ /^GO:/i); | ||
| 1486 | |||
| 1487 | if (!$goid) { | ||
| 1488 | |||
| 1489 | $label =~ s/[0-9]+://i; | ||
| 1490 | |||
| 1491 | } | ||
| 1492 | |||
| 1493 | my $nameColor = $gd->black; | ||
| 1494 | |||
| 1495 | if (!$goid) { | ||
| 1496 | |||
| 1497 | $nameColor = $gd->blue; | ||
| 1498 | |||
| 1499 | }elsif ($goid eq $self->_goid) { | ||
| 1500 | |||
| 1501 | $nameColor = $gd->red; | ||
| 1502 | |||
| 1503 | } | ||
| 1504 | |||
| 1505 | my $startPixel = int(($boxW - length($label)*6)/2); | ||
| 1506 | my $numX1 = $x1 + $startPixel; | ||
| 1507 | my $numY1 = $y1 + $i*10; | ||
| 1508 | |||
| 1509 | if ($goid) { | ||
| 1510 | |||
| 1511 | # the box we're labeling is for a goid | ||
| 1512 | |||
| 1513 | $gd->drawName(name => $label, | ||
| 1514 | nameColor => $nameColor, | ||
| 1515 | numX1 => $numX1, | ||
| 1516 | numY => $numY1); | ||
| 1517 | |||
| 1518 | }else { | ||
| 1519 | |||
| 1520 | # $numX1 -= 10; | ||
| 1521 | |||
| 1522 | # we're adding in a list of genes | ||
| 1523 | |||
| 1524 | my @gene = split(' ', $label); | ||
| 1525 | |||
| 1526 | # add in each one - the image map being generated by | ||
| 1527 | # the GO::View::GD object will have the relevant | ||
| 1528 | # information added to support linking genes to it. | ||
| 1529 | |||
| 1530 | # go through each gene | ||
| 1531 | |||
| 1532 | foreach my $gene(@gene) { | ||
| 1533 | |||
| 1534 | my $linkUrl; | ||
| 1535 | |||
| 1536 | if (!$self->{CREATE_IMAGE_ONLY} && $self->{GENE_URL}) { | ||
| 1537 | |||
| 1538 | $linkUrl = $self->{GENE_URL}; | ||
| 1539 | |||
| 1540 | $linkUrl =~ s/$kReplacementText/$gene/; | ||
| 1541 | |||
| 1542 | } | ||
| 1543 | |||
| 1544 | # add the gene name | ||
| 1545 | |||
| 1546 | $gd->drawName(name => $gene, | ||
| 1547 | nameColor => $nameColor, | ||
| 1548 | linkUrl => $linkUrl, | ||
| 1549 | numX1 => $numX1, | ||
| 1550 | numY => $numY1); | ||
| 1551 | |||
| 1552 | $numX1 += (length($gene)+1)*6; | ||
| 1553 | |||
| 1554 | } | ||
| 1555 | |||
| 1556 | } | ||
| 1557 | |||
| 1558 | $i++; | ||
| 1559 | |||
| 1560 | } | ||
| 1561 | |||
| 1562 | # I think this is supposed to say something about how many | ||
| 1563 | # genes are annotated to a given node, but am not sure that | ||
| 1564 | # $geneNum ever gets defined... | ||
| 1565 | |||
| 1566 | if ($geneNum) { | ||
| 1567 | |||
| 1568 | my $label = $geneNum." gene"; | ||
| 1569 | |||
| 1570 | if ($totalGeneNum != 1) { | ||
| 1571 | |||
| 1572 | $label .= "s"; | ||
| 1573 | |||
| 1574 | } | ||
| 1575 | |||
| 1576 | my $startPixel = int(($boxW - length($label)*6)/2); | ||
| 1577 | |||
| 1578 | my $numX1 = $x1 + $startPixel+2; | ||
| 1579 | |||
| 1580 | my $numY1 = $y1 + $i*10+2; | ||
| 1581 | |||
| 1582 | $gd->drawName(name => $label, | ||
| 1583 | nameColor => $gd->maroon, | ||
| 1584 | numX1 => $numX1, | ||
| 1585 | numY => $numY1); | ||
| 1586 | |||
| 1587 | } | ||
| 1588 | |||
| 1589 | } | ||
| 1590 | |||
| 1591 | ###################################################################### | ||
| 1592 | sub _drawEdge { | ||
| 1593 | ###################################################################### | ||
| 1594 | # | ||
| 1595 | # =head2 _drawEdge | ||
| 1596 | # | ||
| 1597 | # Title : _drawEdge | ||
| 1598 | # Usage : $self->_drawEdge($gd, $height, $border, $line); | ||
| 1599 | # automatically called by _createAndShowImage(). | ||
| 1600 | # Function: To draw each edge. | ||
| 1601 | # Returns : void | ||
| 1602 | # | ||
| 1603 | # =cut | ||
| 1604 | # | ||
| 1605 | ######################################################################### | ||
| 1606 | |||
| 1607 | my ($self, $gd, $height, $border, $line) = @_; | ||
| 1608 | |||
| 1609 | # $line will contain something like: | ||
| 1610 | # | ||
| 1611 | # 342,324 320,360 325,351 331,341 337,332 | ||
| 1612 | # | ||
| 1613 | # where each pair of coordinates defines a point on the line. | ||
| 1614 | # Thus to draw the line, we simply connect the points. | ||
| 1615 | |||
| 1616 | my @point = split(/ /, $line); | ||
| 1617 | |||
| 1618 | # get rid of everything prior to the first point | ||
| 1619 | |||
| 1620 | my ($preX, $preY); | ||
| 1621 | |||
| 1622 | # now go through each point | ||
| 1623 | |||
| 1624 | foreach my $point (@point) { | ||
| 1625 | |||
| 1626 | my ($x, $y) = split(/\,/, $point); | ||
| 1627 | |||
| 1628 | # modify the x coorfinate to take account of the border and | ||
| 1629 | # scaling factor | ||
| 1630 | |||
| 1631 | $x *= $self->{WIDTH_DISPLAY_RATIO}; | ||
| 1632 | |||
| 1633 | $x += $border; | ||
| 1634 | |||
| 1635 | if (!defined $self->{MOVE_Y}) { $self->{MOVE_Y} = 0; } | ||
| 1636 | |||
| 1637 | # modify the y coordinate based on the scaling factor, the border | ||
| 1638 | # the 'MOVE_Y' (whatever that is) and an arbitrary 5 | ||
| 1639 | |||
| 1640 | $y = $height - $y*$self->{HEIGHT_DISPLAY_RATIO} + $border + | ||
| 1641 | $self->{MOVE_Y} + 5; | ||
| 1642 | |||
| 1643 | # now if we have a prior x and y coordinate, we can draw a | ||
| 1644 | # line from it to the current point | ||
| 1645 | |||
| 1646 | if ($preX && $preY) { | ||
| 1647 | |||
| 1648 | $gd->im->line($x, $y, $preX, $preY, $gd->black); | ||
| 1649 | |||
| 1650 | } | ||
| 1651 | |||
| 1652 | # remember these coordinates for the next time through the loop | ||
| 1653 | |||
| 1654 | $preX = $x; | ||
| 1655 | |||
| 1656 | $preY = $y; | ||
| 1657 | |||
| 1658 | } | ||
| 1659 | |||
| 1660 | } | ||
| 1661 | |||
| 1662 | ################################################################# | ||
| 1663 | sub _drawUpArrow { | ||
| 1664 | ################################################################# | ||
| 1665 | # | ||
| 1666 | # =head2 _drawUpArrow | ||
| 1667 | # | ||
| 1668 | # Title : _drawUpArrow | ||
| 1669 | # Usage : $self->_drawUpArrow($gd, $goid, $X1, $X2, $Y, $barHeight, | ||
| 1670 | # $linkUrl); | ||
| 1671 | # automatically called by _drawNode(). | ||
| 1672 | # Function: To draw an up arrow on the tree map. | ||
| 1673 | # Returns : void | ||
| 1674 | # | ||
| 1675 | # =cut | ||
| 1676 | # | ||
| 1677 | ######################################################################### | ||
| 1678 | |||
| 1679 | my ($self, $gd, $goid, $X1, $X2, $Y, $barHeight, | ||
| 1680 | $linkUrl) = @_; | ||
| 1681 | |||
| 1682 | my $node = $self->_ontologyProvider->nodeFromId($goid); | ||
| 1683 | |||
| 1684 | my $maxGenerationUp = $node->lengthOfLongestPathToRoot; | ||
| 1685 | |||
| 1686 | if ($maxGenerationUp <= 1) { return; } | ||
| 1687 | |||
| 1688 | if (!$self->{CREATE_IMAGE_ONLY} && $linkUrl) { | ||
| 1689 | |||
| 1690 | $linkUrl .= "&tree=up"; | ||
| 1691 | |||
| 1692 | } | ||
| 1693 | |||
| 1694 | $gd->drawBar(barColor => $gd->blue, | ||
| 1695 | numX1 => $X1, | ||
| 1696 | numX2 => $X2, | ||
| 1697 | numY => $Y, | ||
| 1698 | linkUrl => $linkUrl, | ||
| 1699 | barHeight => $barHeight, | ||
| 1700 | outline => 1, | ||
| 1701 | arrow => 'up'); | ||
| 1702 | |||
| 1703 | } | ||
| 1704 | |||
| 1705 | ###################################################################### | ||
| 1706 | sub _drawKeys { | ||
| 1707 | ###################################################################### | ||
| 1708 | # | ||
| 1709 | # =head2 _drawKeys | ||
| 1710 | # | ||
| 1711 | # Title : _drawKeys | ||
| 1712 | # Usage : $self->_drawKeys($gd, $mapWidth, $keyY, $isTop); | ||
| 1713 | # automatically called by _createAndShowImage(). | ||
| 1714 | # Function: To draw the display keys on the top or bottom of the tree map. | ||
| 1715 | # Returns : void | ||
| 1716 | # | ||
| 1717 | # =cut | ||
| 1718 | # | ||
| 1719 | ######################################################################### | ||
| 1720 | |||
| 1721 | my ($self, $gd, $mapWidth, $keyY, $isTop) = @_; | ||
| 1722 | |||
| 1723 | if (!$self->{GENE_NAME_HASH_REF_FOR_GOID}) { | ||
| 1724 | |||
| 1725 | my $y = $keyY; | ||
| 1726 | |||
| 1727 | my $boxH = 10; | ||
| 1728 | |||
| 1729 | my $startX = 50; | ||
| 1730 | |||
| 1731 | $gd->drawBar(barColor => $gd->lightBlue, | ||
| 1732 | numX1 => $startX, | ||
| 1733 | numX2 => $startX + 20, | ||
| 1734 | numY => $y, | ||
| 1735 | barHeight => $boxH); | ||
| 1736 | |||
| 1737 | my $numX1 = $startX + 20; | ||
| 1738 | |||
| 1739 | $gd->drawName(name => " = GO term with child(ren)", | ||
| 1740 | nameColor => $gd->black, | ||
| 1741 | numX1 => $numX1, | ||
| 1742 | numY => $y-2); | ||
| 1743 | |||
| 1744 | $y += 15; | ||
| 1745 | |||
| 1746 | $gd->drawBar(barColor => $gd->grey, | ||
| 1747 | numX1 => $startX, | ||
| 1748 | numX2 => $startX + 20, | ||
| 1749 | numY => $y, | ||
| 1750 | barHeight => $boxH); | ||
| 1751 | |||
| 1752 | $gd->drawName(name => " = GO term with no child(ren)", | ||
| 1753 | nameColor => $gd->black, | ||
| 1754 | numX1 => $numX1, | ||
| 1755 | numY => $y-2); | ||
| 1756 | |||
| 1757 | my $maxTextLen = int(($mapWidth-2*$startX)/6); | ||
| 1758 | |||
| 1759 | my $text = $self->_processLabel($self->{MAP_NOTE}, $maxTextLen); | ||
| 1760 | |||
| 1761 | my @geneNumExample = split(/\12/, $text); | ||
| 1762 | |||
| 1763 | $y += 15; | ||
| 1764 | |||
| 1765 | foreach my $text (@geneNumExample){ | ||
| 1766 | |||
| 1767 | $text =~ s/^ *//; | ||
| 1768 | |||
| 1769 | $gd->drawName(name => $text, | ||
| 1770 | nameColor => $gd->black, | ||
| 1771 | numX1 => $startX, | ||
| 1772 | numY => $y-2); | ||
| 1773 | |||
| 1774 | $y += 15; | ||
| 1775 | |||
| 1776 | } | ||
| 1777 | |||
| 1778 | return; | ||
| 1779 | |||
| 1780 | } | ||
| 1781 | |||
| 1782 | my $y1 = $keyY; | ||
| 1783 | |||
| 1784 | my $boxH = 15; | ||
| 1785 | |||
| 1786 | my $boxW = 88; | ||
| 1787 | |||
| 1788 | my $startX = 48 + ($mapWidth - $boxW*6 - 35 - 48)/2; | ||
| 1789 | |||
| 1790 | my $twoLine; | ||
| 1791 | |||
| 1792 | if ($startX < 48) { | ||
| 1793 | |||
| 1794 | $startX = 48 + ($mapWidth - $boxW*3 - 15 - 48)/2; | ||
| 1795 | |||
| 1796 | $twoLine = 1; | ||
| 1797 | |||
| 1798 | if (!$isTop) { $y1 -= 10; } | ||
| 1799 | |||
| 1800 | } | ||
| 1801 | |||
| 1802 | ####### new code | ||
| 1803 | |||
| 1804 | if ($isTop) { | ||
| 1805 | |||
| 1806 | #$startX = 48; | ||
| 1807 | |||
| 1808 | } | ||
| 1809 | |||
| 1810 | $gd->drawName(name => 'pvalue:', | ||
| 1811 | nameColor => $gd->black, | ||
| 1812 | numX1 => 10, | ||
| 1813 | numY => $y1+1); | ||
| 1814 | |||
| 1815 | my $i; | ||
| 1816 | my $preX2 = $startX; | ||
| 1817 | |||
| 1818 | foreach my $name ('<=1e-10', '1e-10 to 1e-8', '1e-8 to 1e-6', | ||
| 1819 | '1e-6 to 1e-4', '1e-4 to 1e-2', '>0.01') { | ||
| 1820 | |||
| 1821 | $i++; | ||
| 1822 | |||
| 1823 | my $pvalue = $name; | ||
| 1824 | |||
| 1825 | $pvalue =~ s/^<=//; | ||
| 1826 | |||
| 1827 | $pvalue =~ s/^>0.01/1/; | ||
| 1828 | |||
| 1829 | $pvalue =~ s/^.+ to (.+)$/$1/; | ||
| 1830 | |||
| 1831 | my $barColor = $self->_color4pvalue($gd, $pvalue); | ||
| 1832 | |||
| 1833 | if ($i == 4 && $twoLine) { | ||
| 1834 | $preX2 = $startX; | ||
| 1835 | $y1 += 20; | ||
| 1836 | } | ||
| 1837 | |||
| 1838 | my $x1 = $preX2 + 5; | ||
| 1839 | |||
| 1840 | my $x2 = $x1 + $boxW; | ||
| 1841 | |||
| 1842 | $gd->drawBar(barColor => $barColor, | ||
| 1843 | numX1 => $x1, | ||
| 1844 | numX2 => $x2, | ||
| 1845 | numY => $y1, | ||
| 1846 | barHeight => $boxH); | ||
| 1847 | |||
| 1848 | my $numX1 = $x1 + ($boxW-length($name)*6)/2; | ||
| 1849 | |||
| 1850 | my $numY1 = $y1 + 2; | ||
| 1851 | |||
| 1852 | $gd->drawName(name => $name, | ||
| 1853 | nameColor => $gd->black, | ||
| 1854 | numX1 => $numX1, | ||
| 1855 | numY => $numY1); | ||
| 1856 | |||
| 1857 | $preX2 = $x2; | ||
| 1858 | |||
| 1859 | } | ||
| 1860 | |||
| 1861 | } | ||
| 1862 | |||
| 1863 | ###################################################################### | ||
| 1864 | sub _drawFrame { | ||
| 1865 | ###################################################################### | ||
| 1866 | # | ||
| 1867 | # =head2 _drawFrame | ||
| 1868 | # | ||
| 1869 | # Title : _drawFrame | ||
| 1870 | # Usage : $self->_drawFrame($gd, $width, $height); | ||
| 1871 | # automatically called by _createAndShowImage(). | ||
| 1872 | # Function: To draw a frame around the image map with date and label | ||
| 1873 | # on the bottom corner. | ||
| 1874 | # Returns : void | ||
| 1875 | # | ||
| 1876 | # =cut | ||
| 1877 | # | ||
| 1878 | ######################################################################### | ||
| 1879 | |||
| 1880 | my ($self, $gd, $width, $height) = @_; | ||
| 1881 | |||
| 1882 | $gd->drawFrameWithLabelAndDate(width => $width, | ||
| 1883 | height => $height, | ||
| 1884 | text => $self->{IMAGE_LABEL}); | ||
| 1885 | |||
| 1886 | } | ||
| 1887 | |||
| 1888 | ################################################################ | ||
| 1889 | sub _createGraphObject { | ||
| 1890 | ################################################################ | ||
| 1891 | # | ||
| 1892 | # =head2 _createGraphObject | ||
| 1893 | # | ||
| 1894 | # Title : _createGraphObject | ||
| 1895 | # Usage : my $self->_createGraphObject(); | ||
| 1896 | # automatically called by _createGraph(). | ||
| 1897 | # Function: Gets newly created empty GraphViz instance. | ||
| 1898 | # Returns : newly created empty GraphViz instance. | ||
| 1899 | # | ||
| 1900 | # =cut | ||
| 1901 | # | ||
| 1902 | ######################################################################### | ||
| 1903 | |||
| 1904 | my ($self) = @_; | ||
| 1905 | |||
| 1906 | my %args; | ||
| 1907 | |||
| 1908 | if (defined $self->{MAKE_PS} && $self->{MAKE_PS}){ | ||
| 1909 | |||
| 1910 | %args = (width => 7.5, | ||
| 1911 | height => 10, | ||
| 1912 | pagewidth => 8.5, | ||
| 1913 | pageheight => 11.5); | ||
| 1914 | |||
| 1915 | } | ||
| 1916 | |||
| 1917 | $self->{GRAPH} = GraphViz->new(node => { shape => 'box', | ||
| 1918 | style => 'filled' }, | ||
| 1919 | edge => { arrowhead => 'none' }, | ||
| 1920 | |||
| 1921 | overlap => 'false', | ||
| 1922 | |||
| 1923 | %args); | ||
| 1924 | |||
| 1925 | } | ||
| 1926 | |||
| 1927 | ################################################################ | ||
| 1928 | sub _addNode { | ||
| 1929 | ################################################################ | ||
| 1930 | # | ||
| 1931 | # =head2 _addNode | ||
| 1932 | # | ||
| 1933 | # Title : _addNode | ||
| 1934 | # Usage : $self->_addNode($goid); | ||
| 1935 | # automatically called by _createGraph(). | ||
| 1936 | # Function: Adds node to the GraphViz instance. | ||
| 1937 | # Returns : void | ||
| 1938 | # | ||
| 1939 | # =cut | ||
| 1940 | # | ||
| 1941 | ######################################################################### | ||
| 1942 | |||
| 1943 | my ($self, $goid, %args) = @_; | ||
| 1944 | |||
| 1945 | my $label; | ||
| 1946 | |||
| 1947 | if ($goid !~ /^GO:[0-9]+$/) { | ||
| 1948 | |||
| 1949 | # if the label is not a goid (i.e. it's a list of genes that | ||
| 1950 | # annotate a term), we'll process it, so that there are no | ||
| 1951 | # lines longer than 30 characters | ||
| 1952 | |||
| 1953 | $label = $self->_processLabel($goid, 30); | ||
| 1954 | |||
| 1955 | }else{ | ||
| 1956 | |||
| 1957 | # otherwise, we'll get the term for the goid | ||
| 1958 | |||
| 1959 | $label = $self->{TERM_HASH_REF_FOR_GOID}->{$goid}; | ||
| 1960 | |||
| 1961 | if (!$label) { | ||
| 1962 | |||
| 1963 | # if we didn't get a term, we'll go back to the | ||
| 1964 | # ontologyProvider to get one | ||
| 1965 | |||
| 1966 | my $node = $self->_ontologyProvider->nodeFromId($goid); | ||
| 1967 | |||
| 1968 | $label = $node->term; | ||
| 1969 | |||
| 1970 | } | ||
| 1971 | |||
| 1972 | # reformat the goid | ||
| 1973 | |||
| 1974 | my $stdGoid; | ||
| 1975 | |||
| 1976 | if (!$self->{PVALUE_HASH_REF_FOR_GOID}) { | ||
| 1977 | |||
| 1978 | $stdGoid = $self->_formatGoid($goid); | ||
| 1979 | |||
| 1980 | }else { | ||
| 1981 | |||
| 1982 | $stdGoid = $goid; | ||
| 1983 | |||
| 1984 | } | ||
| 1985 | |||
| 1986 | # append the goid to the processed label | ||
| 1987 | |||
| 1988 | if ($label) { | ||
| 1989 | |||
| 1990 | $label = $self->_processLabel($label)."\n".$stdGoid; | ||
| 1991 | |||
| 1992 | }else { | ||
| 1993 | |||
| 1994 | $label = $stdGoid; | ||
| 1995 | |||
| 1996 | } | ||
| 1997 | |||
| 1998 | } | ||
| 1999 | |||
| 2000 | # now add the node to the graph, with the appropriate label | ||
| 2001 | |||
| 2002 | $self->graph->add_node($goid, | ||
| 2003 | label => $label, | ||
| 2004 | %args); | ||
| 2005 | |||
| 2006 | |||
| 2007 | } | ||
| 2008 | |||
| 2009 | ################################################################ | ||
| 2010 | sub _addEdge { | ||
| 2011 | ################################################################ | ||
| 2012 | # | ||
| 2013 | # =head2 _addEdge | ||
| 2014 | # | ||
| 2015 | # Title : _addEdge | ||
| 2016 | # Usage : $self->_addEdge($parentGoid, $childGoid); | ||
| 2017 | # automatically called by _createGraph(). | ||
| 2018 | # Function: Adds edge to the GraphViz instance. | ||
| 2019 | # Returns : void | ||
| 2020 | # | ||
| 2021 | # =cut | ||
| 2022 | # | ||
| 2023 | ######################################################################### | ||
| 2024 | |||
| 2025 | my ($self, $parentGoid, $childGoid) = @_; | ||
| 2026 | |||
| 2027 | $self->graph->add_edge($parentGoid => $childGoid); | ||
| 2028 | |||
| 2029 | } | ||
| 2030 | |||
| 2031 | ######################################################################## | ||
| 2032 | sub _descendantCount { | ||
| 2033 | ######################################################################## | ||
| 2034 | # | ||
| 2035 | # =head2 _descendantCount | ||
| 2036 | # | ||
| 2037 | # Title : _descendantCount | ||
| 2038 | # Usage : my $nodeCount = | ||
| 2039 | # $self->_descendantCount($goid, $generationDown); | ||
| 2040 | # automatically called by _createGraph(). | ||
| 2041 | # Function: Gets total descendant node number down to a given generation. | ||
| 2042 | # Returns : The total descendant node number down to a given generation. | ||
| 2043 | # | ||
| 2044 | # =cut | ||
| 2045 | # | ||
| 2046 | ######################################################################### | ||
| 2047 | |||
| 2048 | my ($self, $goid, $generationDown) = @_; | ||
| 2049 | |||
| 2050 | my $node = $self->_ontologyProvider->nodeFromId($goid); | ||
| 2051 | |||
| 2052 | my %descendantCount4generation; | ||
| 2053 | |||
| 2054 | $self->_descendantCount4generation($node, \%descendantCount4generation); | ||
| 2055 | |||
| 2056 | my $nodeNum = 0; | ||
| 2057 | |||
| 2058 | foreach my $generation (1..$generationDown) { | ||
| 2059 | |||
| 2060 | $nodeNum += $descendantCount4generation{$generation}; | ||
| 2061 | |||
| 2062 | } | ||
| 2063 | |||
| 2064 | return $nodeNum; | ||
| 2065 | |||
| 2066 | } | ||
| 2067 | |||
| 2068 | ######################################################################## | ||
| 2069 | sub _setGeneration { | ||
| 2070 | ####################################################################### | ||
| 2071 | # | ||
| 2072 | # =head2 _setGeneration | ||
| 2073 | # | ||
| 2074 | # Title : _setGeneration | ||
| 2075 | # Usage : $self->_setGeneration($goid); | ||
| 2076 | # automatically called by _createGraph(). | ||
| 2077 | # Function: Sets the maximum generation number it will display. | ||
| 2078 | # Returns : void | ||
| 2079 | # | ||
| 2080 | # =cut | ||
| 2081 | # | ||
| 2082 | ######################################################################### | ||
| 2083 | |||
| 2084 | my ($self, $goid) = @_; | ||
| 2085 | |||
| 2086 | my $node = $self->_ontologyProvider->nodeFromId($goid); | ||
| 2087 | |||
| 2088 | my %descendantCount4generation; | ||
| 2089 | |||
| 2090 | $self->_descendantCount4generation($node, \%descendantCount4generation); | ||
| 2091 | |||
| 2092 | my $nodeNum = 0; | ||
| 2093 | my $preNodeNum = 0; | ||
| 2094 | |||
| 2095 | foreach my $generation (sort {$a<=>$b} (keys %descendantCount4generation)) { | ||
| 2096 | |||
| 2097 | $nodeNum += $descendantCount4generation{$generation}; | ||
| 2098 | |||
| 2099 | if ($nodeNum == $self->{MAX_NODE}) { | ||
| 2100 | |||
| 2101 | $self->{GENERATION} = $generation; | ||
| 2102 | |||
| 2103 | $self->{NODE_NUM} = $nodeNum; | ||
| 2104 | |||
| 2105 | last; | ||
| 2106 | |||
| 2107 | } | ||
| 2108 | |||
| 2109 | if ($nodeNum > $self->{MAX_NODE}) { | ||
| 2110 | |||
| 2111 | $self->{GENERATION} = $generation-1; | ||
| 2112 | |||
| 2113 | $self->{NODE_NUM} = $preNodeNum; | ||
| 2114 | |||
| 2115 | last; | ||
| 2116 | |||
| 2117 | } | ||
| 2118 | |||
| 2119 | $preNodeNum = $nodeNum; | ||
| 2120 | |||
| 2121 | } | ||
| 2122 | |||
| 2123 | if (!$self->{GENERATION} || $self->{GENERATION} < 1) { | ||
| 2124 | |||
| 2125 | $self->{GENERATION} = 1; | ||
| 2126 | |||
| 2127 | if (!$node->childNodes) { | ||
| 2128 | |||
| 2129 | $self->{TREE_TYPE} = 'up'; | ||
| 2130 | |||
| 2131 | } | ||
| 2132 | |||
| 2133 | } | ||
| 2134 | |||
| 2135 | } | ||
| 2136 | |||
| 2137 | ######################################################################## | ||
| 2138 | sub _descendantCount4generation { | ||
| 2139 | ######################################################################## | ||
| 2140 | # | ||
| 2141 | # =head2 _descendantCount4generation | ||
| 2142 | # | ||
| 2143 | # Title : _descendantCount4generation | ||
| 2144 | # Usage : $self->_descendantCount4generation($node, | ||
| 2145 | # $nodeCountHashRef, | ||
| 2146 | # $generation); | ||
| 2147 | # automatically called by _descendantCount(), | ||
| 2148 | # _setGeneration(), and itself. | ||
| 2149 | # Function: Gets the descebdant count for each generation. | ||
| 2150 | # Returns : void | ||
| 2151 | # | ||
| 2152 | # =cut | ||
| 2153 | # | ||
| 2154 | ######################################################################### | ||
| 2155 | |||
| 2156 | my ($self, $node, $nodeCountHashRef, $generation) = @_; | ||
| 2157 | |||
| 2158 | if (!$generation) { $generation = 1; } | ||
| 2159 | |||
| 2160 | my @childNodes = $node->childNodes; | ||
| 2161 | |||
| 2162 | $nodeCountHashRef->{$generation} += scalar(@childNodes); | ||
| 2163 | |||
| 2164 | foreach my $childNode ($node->childNodes) { | ||
| 2165 | |||
| 2166 | $self->_descendantCount4generation($childNode, | ||
| 2167 | $nodeCountHashRef, | ||
| 2168 | $generation++); | ||
| 2169 | |||
| 2170 | } | ||
| 2171 | |||
| 2172 | } | ||
| 2173 | |||
| 2174 | ################################################################ | ||
| 2175 | sub _setTopGoid { | ||
| 2176 | ################################################################ | ||
| 2177 | # | ||
| 2178 | # =head2 _setTopGoid | ||
| 2179 | # | ||
| 2180 | # Title : _setTopGoid | ||
| 2181 | # Usage : $self->_setTopGoid(); | ||
| 2182 | # automatically called by _createGraph(). | ||
| 2183 | # Function: Sets the top ancestor goid. We want to display the | ||
| 2184 | # tree up to this goid. | ||
| 2185 | # Returns : void | ||
| 2186 | # | ||
| 2187 | # =cut | ||
| 2188 | # | ||
| 2189 | ######################################################################### | ||
| 2190 | |||
| 2191 | my ($self) = @_; | ||
| 2192 | |||
| 2193 | my $node = $self->_ontologyProvider->nodeFromId($self->_goid); | ||
| 2194 | |||
| 2195 | my $maxGenerationUp = $node->lengthOfLongestPathToRoot - 1; | ||
| 2196 | |||
| 2197 | my @pathsToRoot = $node->pathsToRoot; | ||
| 2198 | |||
| 2199 | my %count4goid; | ||
| 2200 | my %generation4goid; | ||
| 2201 | |||
| 2202 | my $pathNum = scalar(@pathsToRoot); | ||
| 2203 | |||
| 2204 | foreach my $path (@pathsToRoot) { | ||
| 2205 | |||
| 2206 | my @nodeInPath = reverse(@{$path}); | ||
| 2207 | |||
| 2208 | my $generation = 0; | ||
| 2209 | |||
| 2210 | foreach my $node (@nodeInPath) { | ||
| 2211 | |||
| 2212 | $count4goid{$node->goid}++; | ||
| 2213 | |||
| 2214 | $generation++; | ||
| 2215 | |||
| 2216 | if (!$generation4goid{$node->goid} || | ||
| 2217 | $generation4goid{$node->goid} < $generation) { | ||
| 2218 | |||
| 2219 | $generation4goid{$node->goid} = $generation; | ||
| 2220 | |||
| 2221 | } | ||
| 2222 | |||
| 2223 | if ( $pathNum == $count4goid{$node->goid} ) { | ||
| 2224 | ### the same goid appears on all the paths. | ||
| 2225 | |||
| 2226 | $self->{TOP_GOID} = $node->goid; | ||
| 2227 | |||
| 2228 | $self->{GENERATION} = $generation4goid{$node->goid}; | ||
| 2229 | |||
| 2230 | last; | ||
| 2231 | |||
| 2232 | } | ||
| 2233 | |||
| 2234 | } | ||
| 2235 | |||
| 2236 | } | ||
| 2237 | |||
| 2238 | } | ||
| 2239 | |||
| 2240 | ################################################################ | ||
| 2241 | sub _initGoidFromOntology { | ||
| 2242 | ################################################################ | ||
| 2243 | # | ||
| 2244 | # =head2 _initGoidFromOntology | ||
| 2245 | # | ||
| 2246 | # Title : _initGoidFromOntology | ||
| 2247 | # Usage : my $goid = $self->_initGoidFromOntology; | ||
| 2248 | # automatically called by _init(). | ||
| 2249 | # Function: Gets the top goid for the given ontology | ||
| 2250 | # (biological_process, molecular_function, or | ||
| 2251 | # cellular_component) | ||
| 2252 | # Returns : goid | ||
| 2253 | # | ||
| 2254 | # =cut | ||
| 2255 | # | ||
| 2256 | ######################################################################### | ||
| 2257 | |||
| 2258 | my ($self) = @_; | ||
| 2259 | |||
| 2260 | # gene_ontology super node | ||
| 2261 | |||
| 2262 | my $rootNode = $self->_ontologyProvider->rootNode; | ||
| 2263 | |||
| 2264 | # node for molecular_function, biological_process, or | ||
| 2265 | # cellular_component | ||
| 2266 | |||
| 2267 | my ($topNode) = $rootNode->childNodes; | ||
| 2268 | |||
| 2269 | return $topNode->goid; | ||
| 2270 | |||
| 2271 | } | ||
| 2272 | |||
| 2273 | ####################################################################### | ||
| 2274 | sub _initPvaluesGeneNamesDescendantGoids { | ||
| 2275 | ####################################################################### | ||
| 2276 | # | ||
| 2277 | # =head2 _initPvaluesGeneNamesDescendantGoids | ||
| 2278 | # | ||
| 2279 | # Title : _initPvaluesGeneNamesDescendantGoids | ||
| 2280 | # Usage : $self->_initPvaluesGeneNamesDescendantGoids; | ||
| 2281 | # automatically called by _init(). | ||
| 2282 | # Function: Sets $self->{PVALUE_HASH_REF_FOR_GOID}, | ||
| 2283 | # $self->{GENE_NAME_HASH_REF_FOR_GOID}, | ||
| 2284 | # and $self->{DESCENDANT_GOID_ARRAY_REF} | ||
| 2285 | # Returns : void | ||
| 2286 | # | ||
| 2287 | # =cut | ||
| 2288 | # | ||
| 2289 | ######################################################################### | ||
| 2290 | |||
| 2291 | # This method reads through all of the hypotheses that came from the | ||
| 2292 | # TermFinder analysis, and records various information that is | ||
| 2293 | # subsequently used to construct the image. | ||
| 2294 | |||
| 2295 | # TODO - naming of the $pvalue variable and _termFinder methods is | ||
| 2296 | # very poor, and misleading. This should be changed. | ||
| 2297 | # | ||
| 2298 | # TODO - currently, when we compare the maxTopNodeToShow, we shouldn't | ||
| 2299 | # increment the count when a node that is being added is the parent or | ||
| 2300 | # child of a node already on the graph. | ||
| 2301 | |||
| 2302 | my ($self) = @_; | ||
| 2303 | |||
| 2304 | my %foundGoid; | ||
| 2305 | my %foundGoidGene; | ||
| 2306 | |||
| 2307 | my @directAnnotatedGoid; | ||
| 2308 | my %loci4goid; | ||
| 2309 | my %pvalue4goid; | ||
| 2310 | |||
| 2311 | # $maxTopNodeToShow is used to limit the size of the output graph. | ||
| 2312 | # The default value (set during initialization is 6, but can be | ||
| 2313 | # modified via a user argument | ||
| 2314 | |||
| 2315 | my $maxTopNodeToShow = $self->{MAX_TOP_NODE_TO_SHOW}; | ||
| 2316 | |||
| 2317 | my $count = 0; | ||
| 2318 | |||
| 2319 | # go through each hypothesis | ||
| 2320 | |||
| 2321 | foreach my $pvalue (@{$self->_termFinder}){ | ||
| 2322 | |||
| 2323 | # map the goid to the corrected p-value for later retrieval | ||
| 2324 | # when deciding on what color a node should have | ||
| 2325 | |||
| 2326 | $pvalue4goid{$pvalue->{NODE}->goid} = $pvalue->{CORRECTED_PVALUE}; | ||
| 2327 | |||
| 2328 | # skip if it has a p-value worse than our threshold note | ||
| 2329 | |||
| 2330 | next if ($pvalue->{CORRECTED_PVALUE} > $self->{PVALUE_CUTOFF}); | ||
| 2331 | |||
| 2332 | # skip if we've exceeded the maxTopNodeToShow value | ||
| 2333 | # | ||
| 2334 | # NB, we do 'next', rather than 'last', so that the mapping of the | ||
| 2335 | # goid to p-value still gets recorded for any subsequent nodes, which | ||
| 2336 | # ensures that their colors will be correct | ||
| 2337 | |||
| 2338 | next if ($count >= $maxTopNodeToShow); | ||
| 2339 | |||
| 2340 | # grab the GO::Node object for this hypothesis | ||
| 2341 | |||
| 2342 | my $ancestorNode = $pvalue->{NODE}; | ||
| 2343 | |||
| 2344 | # now we go through the list of genes directly annotated to | ||
| 2345 | # this node and get every goid to which any of those genes are | ||
| 2346 | # annotated - thus we'll build up a list of all nodes to which | ||
| 2347 | # the genes are annotated. We'll also record which nodes are | ||
| 2348 | # directly annotated by the genes | ||
| 2349 | |||
| 2350 | foreach my $databaseId (keys %{$pvalue->{ANNOTATED_GENES}}) { | ||
| 2351 | |||
| 2352 | # get every goid that the gene maps to. | ||
| 2353 | |||
| 2354 | my $goidArrayRef = $self->_annotationProvider->goIdsByDatabaseId(databaseId => $databaseId, | ||
| 2355 | aspect => $self->{ASPECT}); | ||
| 2356 | |||
| 2357 | # get the name of the gene, as it was passed to TermFinder | ||
| 2358 | |||
| 2359 | my $gene = $pvalue->{ANNOTATED_GENES}->{$databaseId}; | ||
| 2360 | |||
| 2361 | # now go through each goid that the gene was annotated to | ||
| 2362 | |||
| 2363 | foreach my $goid (@{$goidArrayRef}) { | ||
| 2364 | |||
| 2365 | # get a GO::Node object for it | ||
| 2366 | |||
| 2367 | my $node = $self->_ontologyProvider->nodeFromId($goid); | ||
| 2368 | |||
| 2369 | # the following check is probably superfluous, but | ||
| 2370 | # there may be cases where a node in the annotation | ||
| 2371 | # provider is not in the ontology provider, so we just | ||
| 2372 | # ignore them | ||
| 2373 | |||
| 2374 | next if (!$node); | ||
| 2375 | |||
| 2376 | # We only want to keep information about genes | ||
| 2377 | # directly annotated to this node if it is a | ||
| 2378 | # descendant of the '$ancestorNode', which we know | ||
| 2379 | # passes the p-value cut-off, or if it is the node | ||
| 2380 | # itself. In this way, we only record nodes to which | ||
| 2381 | # our genes of interest are annotated if they have | ||
| 2382 | # contributed to the count associated with the | ||
| 2383 | # significant '$ancestorNode', and thus prune the | ||
| 2384 | # tree, as we don't show all nodes to which any of the | ||
| 2385 | # genes are annotated. | ||
| 2386 | |||
| 2387 | next unless ($node->isADescendantOf($ancestorNode) || $goid eq $ancestorNode->goid); | ||
| 2388 | |||
| 2389 | # now record some information about the this goid and the gene | ||
| 2390 | |||
| 2391 | if (!exists $foundGoidGene{$goid."::".$gene}) { | ||
| 2392 | |||
| 2393 | # record the genes in the list that are annotated | ||
| 2394 | # to this node | ||
| 2395 | |||
| 2396 | $loci4goid{$goid} .= ":".$gene; | ||
| 2397 | |||
| 2398 | # and remember that we've recorded this annotation | ||
| 2399 | |||
| 2400 | $foundGoidGene{$goid."::".$gene}++; | ||
| 2401 | |||
| 2402 | } | ||
| 2403 | |||
| 2404 | # skip if we've already seen the goid | ||
| 2405 | |||
| 2406 | next if ($foundGoid{$goid}); | ||
| 2407 | |||
| 2408 | # record the goid as directly annotated by a gene of interest | ||
| 2409 | |||
| 2410 | push(@directAnnotatedGoid, $goid); | ||
| 2411 | |||
| 2412 | # and remember that we've seen it | ||
| 2413 | |||
| 2414 | $foundGoid{$goid}++; | ||
| 2415 | |||
| 2416 | } | ||
| 2417 | |||
| 2418 | } | ||
| 2419 | |||
| 2420 | $count++; # keep a count of the number of nodes that exceed the cutoff | ||
| 2421 | |||
| 2422 | } | ||
| 2423 | |||
| 2424 | # now record all of our information within ourselves | ||
| 2425 | |||
| 2426 | $self->{DESCENDANT_GOID_ARRAY_REF} = \@directAnnotatedGoid; | ||
| 2427 | $self->{PVALUE_HASH_REF_FOR_GOID} = \%pvalue4goid; | ||
| 2428 | $self->{GENE_NAME_HASH_REF_FOR_GOID} = \%loci4goid; | ||
| 2429 | |||
| 2430 | # and return the number of top nodes recorded, which is used to | ||
| 2431 | # decide whether to print the graph or not. | ||
| 2432 | |||
| 2433 | return $count; | ||
| 2434 | |||
| 2435 | } | ||
| 2436 | |||
| 2437 | ########################################################################## | ||
| 2438 | sub _initVariablesFromConfigFile { | ||
| 2439 | ########################################################################## | ||
| 2440 | my ($self, $configFile) = @_; | ||
| 2441 | |||
| 2442 | my $fh = IO::File->new($configFile, q{<}) || die "Can't open '$configFile' for reading : $!"; | ||
| 2443 | |||
| 2444 | while(<$fh>) { | ||
| 2445 | |||
| 2446 | chomp; | ||
| 2447 | |||
| 2448 | # skip comments, blank, and whitespace only lines | ||
| 2449 | |||
| 2450 | if (/^\#/ || /^\s*$/) { next; } | ||
| 2451 | |||
| 2452 | my ($name, $value) = split(/=/); | ||
| 2453 | |||
| 2454 | $value =~ s/^ *(.+) *$/$1/; | ||
| 2455 | |||
| 2456 | if ($name =~ /^maxNode/i) { | ||
| 2457 | |||
| 2458 | $self->{MAX_NODE} = $value; | ||
| 2459 | |||
| 2460 | }elsif ($name =~ /^maxNodeNameWidth/i) { | ||
| 2461 | |||
| 2462 | $self->{MAX_NODE_NAME_WIDTH} = $value; | ||
| 2463 | |||
| 2464 | }elsif ($name =~ /^widthDisplayRatio/i) { | ||
| 2465 | |||
| 2466 | $self->{WIDTH_DISPLAY_RATIO} = $value; | ||
| 2467 | |||
| 2468 | }elsif ($name =~ /^heightDisplayRatio/i) { | ||
| 2469 | |||
| 2470 | $self->{HEIGHT_DISPLAY_RATIO} = $value; | ||
| 2471 | |||
| 2472 | }elsif ($name =~ /^minMapWidth\b/i) { # need the \b, as it is a substring of minMapWidth4OneLineKey | ||
| 2473 | |||
| 2474 | $self->{MIN_MAP_WIDTH} = $value; | ||
| 2475 | |||
| 2476 | }elsif ($name =~ /^minMapHeight4TopKey/i) { | ||
| 2477 | |||
| 2478 | $self->{MIN_MAP_HEIGHT_FOR_TOP_KEY} = $value; | ||
| 2479 | |||
| 2480 | }elsif ($name =~ /^minMapWidth4OneLineKey/i) { | ||
| 2481 | |||
| 2482 | $self->{MIN_MAP_WIDTH_FOR_ONE_LINE_KEY} = $value; | ||
| 2483 | |||
| 2484 | }elsif ($name =~ /^mapNote/i) { | ||
| 2485 | |||
| 2486 | $self->{MAP_NOTE} = $value; | ||
| 2487 | |||
| 2488 | }elsif ($name =~ /^binDir/i) { | ||
| 2489 | |||
| 2490 | $ENV{PATH} .= ":".$value; | ||
| 2491 | |||
| 2492 | }elsif ($name =~ /^libDir/i) { | ||
| 2493 | |||
| 2494 | $ENV{LD_LIBRARY_PATH} .= ":".$value; | ||
| 2495 | |||
| 2496 | }elsif ($name =~ /^makePs/i){ | ||
| 2497 | |||
| 2498 | $self->{MAKE_PS} = $value; | ||
| 2499 | |||
| 2500 | } | ||
| 2501 | |||
| 2502 | } | ||
| 2503 | |||
| 2504 | $fh->close; | ||
| 2505 | |||
| 2506 | } | ||
| 2507 | |||
| 2508 | ################################################################ | ||
| 2509 | sub _getBoxColor { | ||
| 2510 | ################################################################ | ||
| 2511 | # | ||
| 2512 | # =head2 _getBoxColor | ||
| 2513 | # | ||
| 2514 | # Title : _getBoxColor | ||
| 2515 | # Usage : my $boxColor = $self->_getBoxColor($gd, $goid); | ||
| 2516 | # automatically called by _drawNode(). | ||
| 2517 | # Function: Gets the color for the node box in the display. | ||
| 2518 | # Returns : gd color | ||
| 2519 | # | ||
| 2520 | # =cut | ||
| 2521 | # | ||
| 2522 | ######################################################################### | ||
| 2523 | |||
| 2524 | my ($self, $gd, $goid) = @_; | ||
| 2525 | |||
| 2526 | if ($self->{PVALUE_HASH_REF_FOR_GOID} && | ||
| 2527 | $self->{PVALUE_HASH_REF_FOR_GOID}->{$goid}) { | ||
| 2528 | |||
| 2529 | return $self->_color4pvalue($gd, | ||
| 2530 | $self->{PVALUE_HASH_REF_FOR_GOID}->{$goid}); | ||
| 2531 | |||
| 2532 | } | ||
| 2533 | |||
| 2534 | return $gd->tan; | ||
| 2535 | |||
| 2536 | } | ||
| 2537 | |||
| 2538 | ################################################################ | ||
| 2539 | sub _color4pvalue { | ||
| 2540 | ################################################################ | ||
| 2541 | # | ||
| 2542 | # =head2 _color4pvalue | ||
| 2543 | # | ||
| 2544 | # Title : _color4pvalue | ||
| 2545 | # Usage : my $boxColor = $self->_color4pvalue($gd, $pvalue); | ||
| 2546 | # automatically called by _drawKeys() and _getBoxColor(). | ||
| 2547 | # Function: Gets the color for the node box in the display. | ||
| 2548 | # Returns : gd color | ||
| 2549 | # | ||
| 2550 | # =cut | ||
| 2551 | # | ||
| 2552 | ######################################################################### | ||
| 2553 | |||
| 2554 | my ($self, $gd, $pvalue) = @_; | ||
| 2555 | |||
| 2556 | if ($pvalue <= 1e-10) { | ||
| 2557 | |||
| 2558 | return $gd->orange; | ||
| 2559 | |||
| 2560 | }elsif ($pvalue <= 1e-8) { | ||
| 2561 | |||
| 2562 | return $gd->yellow; | ||
| 2563 | |||
| 2564 | }elsif ($pvalue <= 1e-6) { | ||
| 2565 | |||
| 2566 | return $gd->green4; | ||
| 2567 | |||
| 2568 | }elsif ($pvalue <= 1e-4) { | ||
| 2569 | |||
| 2570 | return $gd->lightBlue; | ||
| 2571 | |||
| 2572 | }elsif ($pvalue <= 1e-2) { | ||
| 2573 | |||
| 2574 | return $gd->blue4; | ||
| 2575 | |||
| 2576 | }else { | ||
| 2577 | |||
| 2578 | return $gd->tan; | ||
| 2579 | |||
| 2580 | } | ||
| 2581 | |||
| 2582 | } | ||
| 2583 | |||
| 2584 | ################################################################ | ||
| 2585 | sub _colorForNode{ | ||
| 2586 | ################################################################ | ||
| 2587 | |||
| 2588 | my ($self, $goid) = @_; | ||
| 2589 | |||
| 2590 | if ($self->{PVALUE_HASH_REF_FOR_GOID} && | ||
| 2591 | $self->{PVALUE_HASH_REF_FOR_GOID}->{$goid}) { | ||
| 2592 | |||
| 2593 | my $pvalue = $self->{PVALUE_HASH_REF_FOR_GOID}->{$goid}; | ||
| 2594 | |||
| 2595 | if ($pvalue <= 1e-10) { | ||
| 2596 | |||
| 2597 | return 'orange'; | ||
| 2598 | |||
| 2599 | }elsif ($pvalue <= 1e-8) { | ||
| 2600 | |||
| 2601 | return 'yellow'; | ||
| 2602 | |||
| 2603 | }elsif ($pvalue <= 1e-6) { | ||
| 2604 | |||
| 2605 | return 'green'; | ||
| 2606 | |||
| 2607 | }elsif ($pvalue <= 1e-4) { | ||
| 2608 | |||
| 2609 | return 'cyan'; | ||
| 2610 | |||
| 2611 | }elsif ($pvalue <= 1e-2) { | ||
| 2612 | |||
| 2613 | return 'royalblue1'; | ||
| 2614 | |||
| 2615 | }else { | ||
| 2616 | |||
| 2617 | return 'burlywood2'; | ||
| 2618 | |||
| 2619 | } | ||
| 2620 | |||
| 2621 | |||
| 2622 | } | ||
| 2623 | |||
| 2624 | return 'burlywood2'; | ||
| 2625 | |||
| 2626 | } | ||
| 2627 | |||
| 2628 | ################################################################ | ||
| 2629 | sub _processLabel { | ||
| 2630 | ################################################################ | ||
| 2631 | # | ||
| 2632 | # =head2 _processLabel | ||
| 2633 | # | ||
| 2634 | # Title : _processLabel | ||
| 2635 | # Usage : my $newLabel = $self->_processLabel($label, | ||
| 2636 | # $maxLabelLen); | ||
| 2637 | # automatically called by _drawKeys() and _addNode(). | ||
| 2638 | # Function: Splits the label into multiple lines if the label is too | ||
| 2639 | # long. | ||
| 2640 | # Returns : new label string | ||
| 2641 | # | ||
| 2642 | # =cut | ||
| 2643 | # | ||
| 2644 | ######################################################################### | ||
| 2645 | |||
| 2646 | my ($self, $label, $maxLabelLen) = @_; | ||
| 2647 | |||
| 2648 | if (!$maxLabelLen) { | ||
| 2649 | |||
| 2650 | $maxLabelLen = $self->{MAX_NODE_NAME_WIDTH} || 15; | ||
| 2651 | |||
| 2652 | } | ||
| 2653 | |||
| 2654 | # separate the label into its constituent words | ||
| 2655 | |||
| 2656 | my @words = split(/ /, $label); | ||
| 2657 | |||
| 2658 | my (@lines, $line); | ||
| 2659 | |||
| 2660 | # go through each word | ||
| 2661 | |||
| 2662 | foreach my $word (@words) { | ||
| 2663 | |||
| 2664 | # if the line we're building up is too long already, or it'll | ||
| 2665 | # be too long when we add the next word, add it to the array | ||
| 2666 | # of lines, and start a new one | ||
| 2667 | |||
| 2668 | if ((defined $line && length($line) >= $maxLabelLen) || | ||
| 2669 | (defined $line && (length($line)+length($word) > $maxLabelLen)) ) { | ||
| 2670 | |||
| 2671 | # get rid of leading space | ||
| 2672 | |||
| 2673 | $line =~ s/^ +//; | ||
| 2674 | |||
| 2675 | push (@lines, $line); | ||
| 2676 | |||
| 2677 | undef $line; | ||
| 2678 | |||
| 2679 | } | ||
| 2680 | |||
| 2681 | # add the current word onto the line | ||
| 2682 | |||
| 2683 | $line .= " ".$word; | ||
| 2684 | |||
| 2685 | } | ||
| 2686 | |||
| 2687 | # add in a final line if there is one | ||
| 2688 | |||
| 2689 | if (defined $line){ | ||
| 2690 | |||
| 2691 | $line =~ s/^ +//; | ||
| 2692 | |||
| 2693 | push (@lines, $line); | ||
| 2694 | |||
| 2695 | } | ||
| 2696 | |||
| 2697 | return join("\n", @lines); | ||
| 2698 | |||
| 2699 | } | ||
| 2700 | |||
| 2701 | ################################################################ | ||
| 2702 | sub _formatGoid { | ||
| 2703 | ################################################################ | ||
| 2704 | # | ||
| 2705 | # =head2 _formatGoid | ||
| 2706 | # | ||
| 2707 | # Title : _formatGoid | ||
| 2708 | # Usage : my $goid = $self->_formatGoid($goid); | ||
| 2709 | # automatically called by _init() and _addNode(). | ||
| 2710 | # Function: Reformats the goid (plain number) to STD GOID format | ||
| 2711 | # (GO:0000388) | ||
| 2712 | # Returns : std GOID | ||
| 2713 | # | ||
| 2714 | # =cut | ||
| 2715 | # | ||
| 2716 | ######################################################################### | ||
| 2717 | |||
| 2718 | my ($self, $goid) = @_; | ||
| 2719 | |||
| 2720 | my $len = length($goid); | ||
| 2721 | |||
| 2722 | for (my $i = 1; $i <= 7 - $len; $i++) { | ||
| 2723 | |||
| 2724 | $goid = "0".$goid; | ||
| 2725 | |||
| 2726 | } | ||
| 2727 | |||
| 2728 | $goid = "GO:".$goid; | ||
| 2729 | |||
| 2730 | return $goid; | ||
| 2731 | |||
| 2732 | } | ||
| 2733 | |||
| 2734 | ####################################################################### | ||
| 2735 | sub DESTROY { | ||
| 2736 | ####################################################################### | ||
| 2737 | |||
| 2738 | # nothing needs to be done | ||
| 2739 | |||
| 2740 | } | ||
| 2741 | |||
| 2742 | ####################################################################### | ||
| 2743 | 1; | ||
| 2744 | ####################################################################### | ||
| 2745 |