| 1 | package GO::TermFinderReport::Text; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | GO::TermFinderReport::Text - prints results of GO::TermFinder as a text report | ||
| 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 number of genes that were used to generate the | ||
| 14 | terms, and the number of genes that were said to be in the genome. It | ||
| 15 | will then generate a text report that summarizes those results. | ||
| 16 | Optionally, filehandle and p-value cutoff arguments may also be passed | ||
| 17 | in. It will return the | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use GO::TermFinder; | ||
| 22 | use GO::TermFinderReport::Text; | ||
| 23 | |||
| 24 | . | ||
| 25 | . | ||
| 26 | . | ||
| 27 | |||
| 28 | my @pvalues = $termFinder->findTerms(genes=>\@genes); | ||
| 29 | |||
| 30 | my $report = GO::TermFinderReport::Text->new(); | ||
| 31 | |||
| 32 | open (OUT, ">report.text"); | ||
| 33 | |||
| 34 | my $numHypotheses = $report->print(pvalues => \@pvalues, | ||
| 35 | aspect => $aspect, | ||
| 36 | numGenes => scalar(@genes), | ||
| 37 | totalNum => $totalNum, | ||
| 38 | cutoff => 0.01, | ||
| 39 | fh => \*OUT); | ||
| 40 | |||
| 41 | close OUT; | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | use strict; | ||
| 46 | use warnings; | ||
| 47 | use diagnostics; | ||
| 48 | |||
| 49 | use vars qw ($VERSION); | ||
| 50 | |||
| 51 | $VERSION = 0.10; | ||
| 52 | |||
| 53 | ###################################################################################### | ||
| 54 | sub new{ | ||
| 55 | ###################################################################################### | ||
| 56 | |||
| 57 | =head2 new | ||
| 58 | |||
| 59 | This is the constructor. | ||
| 60 | |||
| 61 | Usage: | ||
| 62 | |||
| 63 | my $report = GO::TermFinderReport::Text->new(); | ||
| 64 | |||
| 65 | A GO::TermFinderReport::Text object is returned. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | ###################################################################################### | ||
| 70 | |||
| 71 | my $self = {}; | ||
| 72 | |||
| 73 | bless $self, shift; | ||
| 74 | |||
| 75 | return $self; | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | ###################################################################################### | ||
| 80 | sub print{ | ||
| 81 | ###################################################################################### | ||
| 82 | |||
| 83 | =head2 print | ||
| 84 | |||
| 85 | This method prints out the text report of the passed in hypotheses. | ||
| 86 | The report is ordered in ascending order of p-value (i.e. most | ||
| 87 | significant first). If the FDR was calculated, the FDR will also be | ||
| 88 | printed. It returns the number of hypotheses that had corrected | ||
| 89 | p-values as good or better than the passed in cutoff. | ||
| 90 | |||
| 91 | Usage: | ||
| 92 | |||
| 93 | my $numHypotheses = $report->print(pvalues => \@pvalues, | ||
| 94 | numGenes => scalar(@genes), | ||
| 95 | totalNum => $totalNum, | ||
| 96 | cutoff => 0.01, | ||
| 97 | fh => \*OUT, | ||
| 98 | table => 0 ); | ||
| 99 | |||
| 100 | Required arguments: | ||
| 101 | |||
| 102 | pvalues : A reference to the array returned by the findTerms() method | ||
| 103 | of GO::TermFinder | ||
| 104 | |||
| 105 | numGenes : The number of genes that were in the list passed to the | ||
| 106 | findTerms method | ||
| 107 | |||
| 108 | totalNum : The total number of genes that were indicated to be in the | ||
| 109 | genome for finding terms. | ||
| 110 | |||
| 111 | Optional arguments: | ||
| 112 | |||
| 113 | fh : A reference to a file handle to which the table should be | ||
| 114 | printed. Defaults to standard out. | ||
| 115 | |||
| 116 | cutoff : The p-value cutoff, above which p-values and associated | ||
| 117 | information will not be printed. Default is no cutoff. | ||
| 118 | |||
| 119 | table : 0 for standard output, 1 for tab delimited table. Default is 0 | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | ###################################################################################### | ||
| 124 | |||
| 125 | my ($self, %args) = @_; | ||
| 126 | |||
| 127 | if (!exists($args{'pvalues'})){ | ||
| 128 | |||
| 129 | die "You must supply a pvalues argument to the print method."; | ||
| 130 | |||
| 131 | } | ||
| 132 | |||
| 133 | if (!exists $args{'numGenes'}){ | ||
| 134 | |||
| 135 | die "You must supply a numGene argument to the print method."; | ||
| 136 | |||
| 137 | } | ||
| 138 | |||
| 139 | if (!exists $args{'totalNum'}){ | ||
| 140 | |||
| 141 | die "You must supply a totalNum argument to the print method."; | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | my $pvalues = $args{'pvalues'}; | ||
| 146 | my $numGenes = $args{'numGenes'}; | ||
| 147 | my $totalNum = $args{'totalNum'}; | ||
| 148 | my $fh = $args{'fh'} || \*STDOUT; | ||
| 149 | my $cutoff = $args{'cutoff'} || 1; | ||
| 150 | my $table = $args{'table'} || 0; | ||
| 151 | |||
| 152 | my $rows; | ||
| 153 | my $numRows = 0; | ||
| 154 | |||
| 155 | my $hasFdr = 0; | ||
| 156 | |||
| 157 | my $hypothesis = 1; | ||
| 158 | |||
| 159 | my @header = ("GOID", "TERM", "CORRECTED_PVALUE", | ||
| 160 | "UNCORRECTED_PVALUE", "NUM_LIST_ANNOTATIONS", | ||
| 161 | "LIST_SIZE", "TOTAL_NUM_ANNOTATIONS", | ||
| 162 | "POPULATION_SIZE", "FDR_RATE", | ||
| 163 | "EXPECTED_FALSE_POSITIVES", "ANNOTATED_GENES"); | ||
| 164 | |||
| 165 | print $fh join("\t", @header), "\n" if ($table); | ||
| 166 | |||
| 167 | foreach my $pvalue (@{$pvalues}){ | ||
| 168 | |||
| 169 | # skip if above cutoff | ||
| 170 | |||
| 171 | next if ($pvalue->{CORRECTED_PVALUE} > $cutoff); | ||
| 172 | |||
| 173 | # now format the p-value | ||
| 174 | |||
| 175 | my $value = $pvalue->{CORRECTED_PVALUE}; | ||
| 176 | |||
| 177 | # if it's in scientific notation, we want up to two of the decimal places | ||
| 178 | |||
| 179 | $value =~ s/^(.*\.[0-9]{2}).*(e.+)$/$1$2/; | ||
| 180 | |||
| 181 | # otherwise, we'll take up to five decimal places | ||
| 182 | |||
| 183 | $value =~ s/^(0\.[0-9]{5})[0-9]*$/$1/; | ||
| 184 | |||
| 185 | if (defined ($pvalue->{NUM_OBSERVATIONS}) && $pvalue->{NUM_OBSERVATIONS} == 0){ | ||
| 186 | |||
| 187 | # simulations were used to generate the corrected p-value. | ||
| 188 | # If we never saw anything better than this p-value in the | ||
| 189 | # simulations, then prepend a less than sign to the | ||
| 190 | # corrected p-value | ||
| 191 | |||
| 192 | $value = "<".$value; | ||
| 193 | |||
| 194 | } | ||
| 195 | |||
| 196 | if (!$table){ | ||
| 197 | |||
| 198 | print $fh | ||
| 199 | |||
| 200 | "-- $hypothesis of ", scalar @{$pvalues}, " --\n", | ||
| 201 | "GOID\t", $pvalue->{NODE}->goid, "\n", | ||
| 202 | "TERM\t", $pvalue->{NODE}->term, "\n", | ||
| 203 | "CORRECTED P-VALUE\t", $pvalue->{CORRECTED_PVALUE}, "\n", | ||
| 204 | "UNCORRECTED P-VALUE\t", $pvalue->{PVALUE}, "\n"; | ||
| 205 | |||
| 206 | }else{ | ||
| 207 | |||
| 208 | print $fh join("\t", ($pvalue->{NODE}->goid, | ||
| 209 | $pvalue->{NODE}->term, | ||
| 210 | $pvalue->{CORRECTED_PVALUE}, | ||
| 211 | $pvalue->{PVALUE}, | ||
| 212 | $pvalue->{NUM_ANNOTATIONS}, | ||
| 213 | $numGenes, | ||
| 214 | $pvalue->{TOTAL_NUM_ANNOTATIONS}, | ||
| 215 | $totalNum)), "\t"; | ||
| 216 | |||
| 217 | } | ||
| 218 | |||
| 219 | # deal with FDR | ||
| 220 | |||
| 221 | my ($fdr, $falsePositives); | ||
| 222 | |||
| 223 | if (exists ($pvalue->{FDR_RATE})){ | ||
| 224 | |||
| 225 | $fdr = sprintf ("%.2f%%", $pvalue->{FDR_RATE} * 100); | ||
| 226 | |||
| 227 | $falsePositives = sprintf ("%.2f", $pvalue->{EXPECTED_FALSE_POSITIVES}); | ||
| 228 | |||
| 229 | if(!$table){ | ||
| 230 | |||
| 231 | print $fh | ||
| 232 | |||
| 233 | "FDR_RATE\t", $fdr, "\n", | ||
| 234 | "EXPECTED_FALSE_POSITIVES\t", $falsePositives, "\n"; | ||
| 235 | |||
| 236 | }else{ | ||
| 237 | |||
| 238 | print $fh $fdr, "\t", $falsePositives, "\t"; | ||
| 239 | |||
| 240 | } | ||
| 241 | |||
| 242 | }else{ | ||
| 243 | |||
| 244 | print $fh "\t\t" if ($table); # Gotta fill in the blanks | ||
| 245 | |||
| 246 | } | ||
| 247 | |||
| 248 | if (!$table){ | ||
| 249 | |||
| 250 | print $fh "NUM_ANNOTATIONS\t"; | ||
| 251 | print $fh $pvalue->{NUM_ANNOTATIONS}; | ||
| 252 | print $fh " of $numGenes in the list, vs "; | ||
| 253 | print $fh $pvalue->{TOTAL_NUM_ANNOTATIONS}; | ||
| 254 | print $fh " of $totalNum in the genome\n"; | ||
| 255 | print $fh "The genes annotated to this node are:\n";; | ||
| 256 | |||
| 257 | } | ||
| 258 | |||
| 259 | print $fh join(", ", values(%{$pvalue->{ANNOTATED_GENES}})), "\n"; | ||
| 260 | print $fh "\n" if (!$table); | ||
| 261 | |||
| 262 | $hypothesis++; | ||
| 263 | |||
| 264 | } | ||
| 265 | |||
| 266 | return ($hypothesis - 1); | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
| 270 | 1; # to keep Perl happy | ||
| 271 | |||
| 272 | =pod | ||
| 273 | |||
| 274 | =head1 AUTHOR | ||
| 275 | |||
| 276 | Gavin Sherlock | ||
| 277 | |||
| 278 | sherlock@genome.stanford.edu | ||
| 279 | |||
| 280 | =cut |