| 1 | package GO::TermFinderReport::Html; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | GO::TermFinderReport::Html - prints an html table of the results of GO::TermFinder | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This print() method of this Perl module receives a reference to an the | ||
| 12 | array that is the return value from the findTerms method of | ||
| 13 | GO::TermFinder, the aspect for which terms were found, the number of | ||
| 14 | genes that were used to generate the terms, and the number of genes | ||
| 15 | that were said to be in the genome. It will then generate an html | ||
| 16 | table that summarizes those results. Optionally, filehandle, p-value | ||
| 17 | cutoff, gene URL, and GOID URL arguments may also be passed in. Url | ||
| 18 | links should have the string <REPLACE_THIS> to indicate where the gene | ||
| 19 | name, or GOID should be put. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use GO::TermFinder; | ||
| 24 | use GO::TermFinderReport::Html; | ||
| 25 | |||
| 26 | . | ||
| 27 | . | ||
| 28 | . | ||
| 29 | |||
| 30 | my @pvalues = $termFinder->findTerms(genes=>\@genes); | ||
| 31 | |||
| 32 | my $report = GO::TermFinderReport::Html->new(); | ||
| 33 | |||
| 34 | open (HTML, ">blah.html"); | ||
| 35 | |||
| 36 | print HTML "<html><body>"; | ||
| 37 | |||
| 38 | my $numRows = $report->print(pvalues => \@pvalues, | ||
| 39 | aspect => $aspect, | ||
| 40 | numGenes => scalar(@genes), | ||
| 41 | totalNum => $totalNum, | ||
| 42 | fh => \*HTML, | ||
| 43 | cutoff => 0.01, | ||
| 44 | geneUrl => 'http://db.yeastgenome.org/cgi-bin/SGD/locus.pl?locus=<REPLACE_THIS>', | ||
| 45 | goidUrl => 'http://amigo.geneontology.org/cgi-bin/amigo/go.cgi?view=details&search_constraint=terms&depth=0&query=<REPLACE_THIS>'); | ||
| 46 | |||
| 47 | print HTML "</body></html>"; | ||
| 48 | |||
| 49 | close HTML; | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | use strict; | ||
| 54 | use warnings; | ||
| 55 | use diagnostics; | ||
| 56 | |||
| 57 | use vars qw ($VERSION); | ||
| 58 | |||
| 59 | $VERSION = 0.12; | ||
| 60 | |||
| 61 | use CGI qw/:all :html3/; | ||
| 62 | |||
| 63 | ###################################################################################### | ||
| 64 | sub new{ | ||
| 65 | ###################################################################################### | ||
| 66 | |||
| 67 | =head2 new | ||
| 68 | |||
| 69 | This is the constructor. | ||
| 70 | |||
| 71 | Usage: | ||
| 72 | |||
| 73 | my $report = GO::TermFinderReport::Html->new(); | ||
| 74 | |||
| 75 | A GO::TermFinderReport::Html object is returned. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | ###################################################################################### | ||
| 80 | |||
| 81 | my $self = {}; | ||
| 82 | |||
| 83 | bless $self, shift; | ||
| 84 | |||
| 85 | return $self; | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | ###################################################################################### | ||
| 90 | sub print{ | ||
| 91 | ###################################################################################### | ||
| 92 | |||
| 93 | =head2 print | ||
| 94 | |||
| 95 | This method prints out the report, in the form of an html table. The | ||
| 96 | table is ordered in ascending order of p-value (i.e. most significant | ||
| 97 | first), and will print out the GO node, the frequency of use of that | ||
| 98 | node within the selected group of genes, and the population as a | ||
| 99 | whole, the corrected p-value of that, and a list of the genes | ||
| 100 | annotated to that node. If the FDR was calculated, the FDR will also | ||
| 101 | be printed. It returns the number of annotation rows in the table | ||
| 102 | that exceed the provided p-value cutoff (which may even be zero, in | ||
| 103 | which case nothing is printed). | ||
| 104 | |||
| 105 | Usage: | ||
| 106 | |||
| 107 | my $numRows = $report->print(pvalues => \@pvalues, | ||
| 108 | aspect => $aspect, # P, C, or F | ||
| 109 | numGenes => scalar(@genes), | ||
| 110 | totalNum => $totalNum, | ||
| 111 | fh => \*HTML, | ||
| 112 | pvalueCutOff => 0.01, | ||
| 113 | geneUrl => 'http://db.yeastgenome.org/cgi-bin/SGD/locus.pl?locus=<REPLACE_THIS>', | ||
| 114 | goidUrl => 'http://amigo.geneontology.org/cgi-bin/amigo/go.cgi?view=details&search_constraint=terms&depth=0&query=<REPLACE_THIS>'); | ||
| 115 | |||
| 116 | Required arguments: | ||
| 117 | |||
| 118 | pvalues : A reference to the array returned by the findTerms() method of GO::TermFinder | ||
| 119 | |||
| 120 | aspect : The aspect of the Gene Ontology for which terms were found (C, F or P) | ||
| 121 | |||
| 122 | numGenes : The number of genes that were in the list passed to the findTerms method | ||
| 123 | |||
| 124 | totalNum : The total number of genes that were indicated to be in the genome for finding terms. | ||
| 125 | |||
| 126 | Optional arguments: | ||
| 127 | |||
| 128 | |||
| 129 | fh : A reference to a file handle to which the table should be | ||
| 130 | printed. Defaults to standard out. | ||
| 131 | |||
| 132 | pvalueCutOff : The p-value cutoff, above which p-values and associated | ||
| 133 | information will not be printed. Default is no cutoff. | ||
| 134 | |||
| 135 | geneUrl : A url to which you want genes linked. Must contain the | ||
| 136 | text '<REPLACE_THIS>', which will be replaced with the | ||
| 137 | gene name. | ||
| 138 | |||
| 139 | goidUrl : A url to which you want the GOIDs linked. Must contain the | ||
| 140 | text '<REPLACE_THIS>', which will be replaced with the | ||
| 141 | goid. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | ###################################################################################### | ||
| 146 | |||
| 147 | my ($self, %args) = @_; | ||
| 148 | |||
| 149 | if (!exists($args{'pvalues'})){ | ||
| 150 | |||
| 151 | die "You must supply a pvalues argument to the print method."; | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | if (!exists($args{'aspect'})){ | ||
| 156 | |||
| 157 | die "You must supply a aspect argument to the print method."; | ||
| 158 | |||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | if (!exists $args{'numGenes'}){ | ||
| 163 | |||
| 164 | die "You must supply a numGene argument to the print method."; | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | if (!exists $args{'totalNum'}){ | ||
| 169 | |||
| 170 | die "You must supply a totalNum argument to the print method."; | ||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | my $pvalues = $args{'pvalues'}; | ||
| 175 | my $aspect = $args{'aspect'}; | ||
| 176 | my $numGenes = $args{'numGenes'}; | ||
| 177 | my $totalNum = $args{'totalNum'}; | ||
| 178 | my $fh = $args{'fh'} || \*STDOUT; | ||
| 179 | my $cutoff = $args{'pvalueCutOff'} || 1; | ||
| 180 | my $geneUrl = $args{'geneUrl'}; | ||
| 181 | my $goidUrl = $args{'goidUrl'}; | ||
| 182 | |||
| 183 | my $replacementText = "<REPLACE_THIS>"; | ||
| 184 | |||
| 185 | my $rows; | ||
| 186 | my $numRows = 0; | ||
| 187 | |||
| 188 | my $hasFdr = 0; | ||
| 189 | |||
| 190 | foreach my $pvalue (@{$pvalues}){ | ||
| 191 | |||
| 192 | # skip if above cutoff | ||
| 193 | |||
| 194 | next if ($pvalue->{CORRECTED_PVALUE} > $cutoff); | ||
| 195 | |||
| 196 | # now generate a list of loci annotated to this node, with | ||
| 197 | # links if requested | ||
| 198 | |||
| 199 | my @loci; | ||
| 200 | |||
| 201 | foreach my $databaseId (keys %{$pvalue->{ANNOTATED_GENES}}){ | ||
| 202 | |||
| 203 | my $gene = $pvalue->{ANNOTATED_GENES}->{$databaseId}; | ||
| 204 | |||
| 205 | if (defined $geneUrl) { | ||
| 206 | |||
| 207 | my $url = $geneUrl; | ||
| 208 | |||
| 209 | $url =~ s/$replacementText/$gene/; | ||
| 210 | |||
| 211 | $gene = a({-href=>$url, | ||
| 212 | -target=>'infowin'}, $gene); | ||
| 213 | |||
| 214 | } | ||
| 215 | |||
| 216 | push (@loci, $gene); | ||
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | my $loci = join(", ", @loci); | ||
| 221 | |||
| 222 | # now calculate the frequency for annotation for the list and the genome | ||
| 223 | |||
| 224 | my $frequencyPercent = sprintf("%.1f", ($pvalue->{NUM_ANNOTATIONS}/$numGenes) * 100); | ||
| 225 | |||
| 226 | my $frequency = $pvalue->{NUM_ANNOTATIONS}." out of $numGenes genes, $frequencyPercent\%"; | ||
| 227 | |||
| 228 | my $geneFrequencyPercent = sprintf("%.1f", ($pvalue->{TOTAL_NUM_ANNOTATIONS}/$totalNum) * 100); | ||
| 229 | |||
| 230 | my $genomeFrequency = $pvalue->{TOTAL_NUM_ANNOTATIONS}." out of $totalNum genes, $geneFrequencyPercent\%"; | ||
| 231 | |||
| 232 | # now format the p-value | ||
| 233 | |||
| 234 | my $value = $pvalue->{CORRECTED_PVALUE}; | ||
| 235 | |||
| 236 | # if it's in scientific notation, we want up to two of the decimal places | ||
| 237 | |||
| 238 | $value =~ s/^(.*\.[0-9]{2}).*(e.+)$/$1$2/; | ||
| 239 | |||
| 240 | # otherwise, we'll take up to five decimal places | ||
| 241 | |||
| 242 | $value =~ s/^(0\.[0-9]{5})[0-9]*$/$1/; | ||
| 243 | |||
| 244 | if (defined ($pvalue->{NUM_OBSERVATIONS}) && $pvalue->{NUM_OBSERVATIONS} == 0){ | ||
| 245 | |||
| 246 | # simulations were used to generate the corrected p-value. | ||
| 247 | # If we never saw anything better than this p-value in the | ||
| 248 | # simulations, then prepend a less than sign to the | ||
| 249 | # corrected p-value | ||
| 250 | |||
| 251 | $value = "<".$value; | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | # now deal with the GOID column | ||
| 256 | |||
| 257 | my $goColumn; | ||
| 258 | |||
| 259 | if (defined $goidUrl){ | ||
| 260 | |||
| 261 | my $url = $goidUrl; | ||
| 262 | my $goid = $pvalue->{NODE}->goid; | ||
| 263 | |||
| 264 | $url =~ s/$replacementText/$goid/; | ||
| 265 | |||
| 266 | # make link with name of term as the text | ||
| 267 | |||
| 268 | $goColumn = a({-href=>$url, | ||
| 269 | -target=>'infowin'}, $pvalue->{NODE}->term); | ||
| 270 | |||
| 271 | }else{ | ||
| 272 | |||
| 273 | # if no link, just use term, and parenthetical GOID | ||
| 274 | |||
| 275 | $goColumn = $pvalue->{NODE}->term." (".$pvalue->{NODE}->goid.")"; | ||
| 276 | |||
| 277 | } | ||
| 278 | |||
| 279 | # deal with FDR | ||
| 280 | |||
| 281 | my ($fdr, $falsePositives); | ||
| 282 | |||
| 283 | if (exists ($pvalue->{FDR_RATE})){ | ||
| 284 | |||
| 285 | $hasFdr = 1; | ||
| 286 | |||
| 287 | $fdr = sprintf ("%.2f%%", $pvalue->{FDR_RATE} * 100); | ||
| 288 | |||
| 289 | $falsePositives = sprintf ("%.2f", $pvalue->{EXPECTED_FALSE_POSITIVES}); | ||
| 290 | |||
| 291 | } | ||
| 292 | |||
| 293 | $rows .= $self->_oneRow($goColumn, $frequency, | ||
| 294 | $genomeFrequency, $value, $loci, $fdr, | ||
| 295 | $falsePositives); | ||
| 296 | |||
| 297 | $numRows++; | ||
| 298 | |||
| 299 | } | ||
| 300 | |||
| 301 | # print the table out, if there were any rows | ||
| 302 | |||
| 303 | $self->_printTable($fh, $rows, $aspect, $cutoff, $hasFdr) if ($numRows > 0); | ||
| 304 | |||
| 305 | return $numRows; | ||
| 306 | |||
| 307 | } | ||
| 308 | |||
| 309 | ################################################################### | ||
| 310 | sub _oneRow{ | ||
| 311 | ################################################################### | ||
| 312 | # This protected method simply returns a row from the html table, | ||
| 313 | # based on what was passed in. | ||
| 314 | |||
| 315 | my ($self, $goColumn, $frequency, $genomeFrequency, $pvalue, | ||
| 316 | $loci, $fdr, $falsePositives) = @_; | ||
| 317 | |||
| 318 | my $row = td($goColumn). | ||
| 319 | td($frequency). | ||
| 320 | td($genomeFrequency). | ||
| 321 | td($pvalue); | ||
| 322 | |||
| 323 | if (defined($fdr)){ | ||
| 324 | |||
| 325 | $row .= td($fdr).td($falsePositives); | ||
| 326 | |||
| 327 | } | ||
| 328 | td($loci); | ||
| 329 | |||
| 330 | $row .= td($loci); | ||
| 331 | |||
| 332 | return Tr($row); | ||
| 333 | |||
| 334 | } | ||
| 335 | |||
| 336 | ################################################################### | ||
| 337 | sub _printTable{ | ||
| 338 | ################################################################### | ||
| 339 | # This method prints out the actual html table | ||
| 340 | |||
| 341 | my ($self, $fh, $rows, $aspect, $cutoff, $hasFdr) = @_; | ||
| 342 | |||
| 343 | $aspect =~ s/^F/Function/i; | ||
| 344 | $aspect =~ s/^P/Process/i; | ||
| 345 | $aspect =~ s/^C/Component/i; | ||
| 346 | |||
| 347 | print $fh a({-name=>'table'}); | ||
| 348 | print $fh center(h3("Result Table")), p; | ||
| 349 | |||
| 350 | print $fh table({-align => 'center', | ||
| 351 | -border => 1, | ||
| 352 | -cellpadding => 2, | ||
| 353 | -width => 400}, | ||
| 354 | Tr(td({-bgcolor => '#FFCC99', | ||
| 355 | -align => 'center', | ||
| 356 | -width => '100%', | ||
| 357 | -nowrap => undef}, | ||
| 358 | b("Terms from the $aspect Ontology with p-value as good or better than $cutoff")))); | ||
| 359 | |||
| 360 | my $headings = th({-align => 'center'}, "Gene Ontology term"). | ||
| 361 | th({-align => 'center'}, "Cluster frequency"). | ||
| 362 | th({-align => 'center'}, "Genome frequency of use"). | ||
| 363 | th({-align => 'center'}, "Corrected P-value"); | ||
| 364 | |||
| 365 | if ($hasFdr){ | ||
| 366 | |||
| 367 | $headings .= th({-align => 'center'}, "FDR"). | ||
| 368 | th({-align => 'center'}, "False Positives"); | ||
| 369 | |||
| 370 | } | ||
| 371 | |||
| 372 | $headings .= th({-align => 'center'}, "Genes annotated to the term"); | ||
| 373 | |||
| 374 | print $fh table({-align => 'center', | ||
| 375 | -border => 2}, | ||
| 376 | Tr({-bgcolor => '#CCCCFF'}, | ||
| 377 | $headings). | ||
| 378 | $rows), p; | ||
| 379 | |||
| 380 | } | ||
| 381 | |||
| 382 | 1; # to keep Perl happy | ||
| 383 | |||
| 384 | =pod | ||
| 385 | |||
| 386 | =head1 AUTHOR | ||
| 387 | |||
| 388 | Gavin Sherlock | ||
| 389 | |||
| 390 | sherlock@genome.stanford.edu | ||
| 391 | |||
| 392 | =cut |