[Bio] / Sprout / SHSigGenes.pm Repository:
ViewVC logotype

Annotation of /Sprout/SHSigGenes.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     package SHSigGenes;
4 :    
5 :     use strict;
6 :     use Tracer;
7 :     use SearchHelper;
8 :     use CGI;
9 :     use HTML;
10 :     use Sprout;
11 :    
12 :     our @ISA = qw(SearchHelper);
13 :    
14 :     =head1 Gene Discrimination Feature Search Helper
15 :    
16 :     =head2 Introduction
17 :    
18 :     This search performs a signature genes comparison. The user selects two genome sets,
19 :     and the search returns genes from a given genome which are common only in the first set
20 :     and not in the second. If the second set is empty, the search will return genes from
21 :     the given genome that are common in the first set.
22 :    
23 :     Gene identity will be computed in this case using bidirectional best hits. If gene X
24 :     from the given genome has a BBH in a specified genome Y, then it is said to occur
25 :     in whatever set includes genome Y. A gene is considered I<common> if it occurs in a
26 :     certain percentage of the genomes of the set.
27 :    
28 :     This search has the following extra parameters.
29 :    
30 :     =over 4
31 :    
32 :     =item given
33 :    
34 :     The ID of the given genome.
35 :    
36 :     =item target[]
37 :    
38 :     The IDs of the genomes in the first (target) set. The given genome is
39 :     automatically considered a part of this set, so it can never be empty.
40 :    
41 :     =item exclusion[]
42 :    
43 :     The IDs of the genomes in the second (exclusion) set. If this set is empty, then
44 :     no genes will be considered common in set 2, causing all genes common in set 1
45 :     to be selected.
46 :    
47 :     =item commonality
48 :    
49 :     Minimum score for a gene to be considered common. The score is equal to the number
50 :     of genomes containing a bidirectional best hit of the gene divided by the total
51 :     number of genomes. The default is C<0.8>. A value of C<1> means a gene must have
52 :     BBHs in all of the genomes to be considered common; a value of C<0> is invalid.
53 :    
54 :     =item cutoff
55 :    
56 :     Maximum match difference for a BBH hit to be considered valid. The default is C<1e-10>.
57 :    
58 :     =back
59 :    
60 :     =head2 Virtual Methods
61 :    
62 :     =head3 Form
63 :    
64 : parrello 1.7 C<< my $html = $shelp->Form(); >>
65 : parrello 1.1
66 :     Generate the HTML for a form to request a new search.
67 :    
68 :     =cut
69 :    
70 :     sub Form {
71 :     # Get the parameters.
72 :     my ($self) = @_;
73 :     # Get the CGI and sprout objects.
74 :     my $cgi = $self->Q();
75 :     my $sprout = $self->DB();
76 :     # Start the form.
77 :     my $retVal = $self->FormStart("Signature Genes");
78 :     # The bulk of this form will be two genome selection menus, one for the first
79 :     # (target) set and one for the second (exclusion) set. Above these two controls
80 :     # there is the selector for the given genome, the commonality and cutoff values,
81 :     # and the submit button. Our first task, then, is to get the genome selection
82 :     # menus.
83 : parrello 1.5 my $givenMenu = $self->NmpdrGenomeMenu('given', 0, [$cgi->param('given')]);
84 : parrello 1.4 my $targetMenu = $self->NmpdrGenomeMenu('target', 'multiple', [$cgi->param('target')], 10, 'exclusion');
85 :     my $excludeMenu = $self->NmpdrGenomeMenu('exclusion', 'multiple', [$cgi->param('exclusion')], 10, 'target');
86 : parrello 1.1 # Get the default values to use for the commonality and cutoff controls.
87 :     my $commonality = $cgi->param('commonality') || "0.8";
88 :     my $cutoff = $cgi->param('cutoff') || "1e-10";
89 : parrello 1.6 my $statistical = $cgi->param('statistical') || 1;
90 : parrello 1.1 # Now we build the table rows. The top contains the two numeric parameters and
91 :     # the submit button.
92 :     my @rows = ();
93 :     push @rows, $cgi->Tr($cgi->td("Commonality"),
94 :     $cgi->td($cgi->textfield(-name => 'commonality',
95 :     -value => $commonality,
96 : parrello 1.5 -size => 5) . "&nbsp;" .
97 :     $cgi->checkbox(-name => 'statistical',
98 :     -checked => $statistical,
99 :     -value => 1,
100 :     -label => 'Use Statistical Algorithm')));
101 : parrello 1.1 push @rows, $cgi->Tr($cgi->td("Cutoff"),
102 :     $cgi->td($cgi->textfield(-name => 'cutoff',
103 :     -value => $cutoff,
104 :     -size => 5)));
105 :     push @rows, $self->SubmitRow();
106 : parrello 1.2 # The next rows have the given genome and a feature filter.
107 : parrello 1.1 push @rows, $cgi->Tr($cgi->td({valign => "top"}, "Given Genome"),
108 :     $cgi->td({colspan => 2}, $givenMenu));
109 : parrello 1.2 push @rows, $self->FeatureFilterRows();
110 :     # Now show the target and exclusion menus.
111 : parrello 1.1 push @rows, $cgi->Tr($cgi->td({valign => "top"}, "Target Genomes (Set 1)"),
112 :     $cgi->td({colspan => 2}, $targetMenu));
113 :     push @rows, $cgi->Tr($cgi->td({valign => "top"}, "Exclusion Genomes (Set 2)"),
114 :     $cgi->td({colspan => 2}, $excludeMenu));
115 :     # Create the table.
116 :     $retVal .= $self->MakeTable(\@rows);
117 :     # Close the form.
118 :     $retVal .= $self->FormEnd();
119 :     # Return the result.
120 :     return $retVal;
121 :     }
122 :    
123 :     =head3 Find
124 :    
125 :     C<< my $resultCount = $shelp->Find(); >>
126 :    
127 :     Conduct a search based on the current CGI query parameters. The search results will
128 :     be written to the session cache file and the number of results will be
129 :     returned. If the search parameters are invalid, a result count of C<undef> will be
130 :     returned and a result message will be stored in this object describing the problem.
131 :    
132 :     =cut
133 :    
134 :     sub Find {
135 :     # Get the parameters.
136 :     my ($self) = @_;
137 :     # Get the sprout and CGI query objects.
138 :     my $cgi = $self->Q();
139 :     my $sprout = $self->DB();
140 : parrello 1.2 # Declare the return variable. If it remains undefined, the caller will
141 :     # assume there was an error.
142 : parrello 1.1 my $retVal;
143 : parrello 1.2 # Validate the numeric parameters.
144 :     my $commonality = $cgi->param('commonality');
145 :     my $cutoff = $cgi->param('cutoff');
146 :     if ($commonality !~ /^\s*\d(\.\d+)?\s*$/) {
147 :     $self->SetMessage("Commonality value appears invalid, too big, negative, or not a number.");
148 :     } elsif ($commonality <= 0 || $commonality > 1) {
149 :     $self->SetMessage("Commonality cannot be 0 and cannot be greater than 1.");
150 :     } elsif ($cutoff !~ /^\s*\d(.\d+)?(e\-\d+)?\s*$/) {
151 :     $self->SetMessage("Cutoff must be an exponential number (e.g. \"1e-20\" or \"2.5e-11\".");
152 :     } elsif ($cutoff > 1) {
153 :     $self->SetMessage("Cutoff cannot be greater than 1.");
154 :     } else {
155 :     # Now we need to gather and validate the genome sets.
156 : parrello 1.3 my ($givenGenomeID) = $self->GetGenomes('given');
157 :     my %targetGenomes = map { $_ => 1 } $self->GetGenomes('target');
158 :     my %exclusionGenomes = map { $_ => 1 } $self->GetGenomes('exclusion');
159 : parrello 1.2 # Insure the given genome is not in the exclusion set.
160 :     if ($exclusionGenomes{$givenGenomeID}) {
161 :     $self->SetMessage("The given genome ($givenGenomeID) cannot be in the exclusion set.");
162 :     } else {
163 :     # Insure the given genome is in the target set.
164 :     $targetGenomes{$givenGenomeID} = 1
165 :     }
166 : parrello 1.5 # Find out if we want to use a statistical analysis.
167 :     my $statistical = $cgi->param('statistical') || 0;
168 : parrello 1.2 # Denote we have not yet found any genomes.
169 :     $retVal = 0;
170 : parrello 1.4 # Create a feature query object to loop through the chosen features of the given
171 :     # genome.
172 :     my $fquery = FeatureQuery->new($self, $givenGenomeID);
173 :     # Get the sizes of the two sets. This information is useful in computing commonality.
174 :     my $targetSetSize = scalar keys %targetGenomes;
175 :     my $exclusionSetSize = scalar keys %exclusionGenomes;
176 :     # Loop through the features.
177 :     while (my $record = $fquery->Fetch()) {
178 :     # Get the feature's ID.
179 :     my $fid = $fquery->FID();
180 :     # Request its list of BBHs. The list is actually a hash mapping each BBH to its
181 :     # score. All we care about, however, are the BBHs themselves.
182 :     my %bbhList = $sprout->LowBBHs($fid, $cutoff);
183 :     # We next wish to loop through the BBH IDs, counting how many are in each of the
184 :     # sets. If a genome occurs twice, we only want to count the first occurrence, so
185 :     # we have a hash of genomes we've already seen.
186 :     my %alreadySeen = ();
187 :     # Clear the counts.
188 :     my ($targetCount, $exclusionCount) = (0, 0);
189 :     # Loop through the BBHs.
190 :     for my $bbhPeg (keys %bbhList) {
191 :     # Get the genome ID. We want to find out if this genome is new.
192 :     my ($genomeID) = FIGRules::ParseFeatureID($bbhPeg);
193 :     if (! $alreadySeen{$genomeID}) {
194 :     # It's new, so we check to see which set it's in.
195 :     if ($targetGenomes{$genomeID}) {
196 :     $targetCount++;
197 :     } elsif ($exclusionGenomes{$genomeID}) {
198 :     $exclusionCount++;
199 :     }
200 :     # Make sure we don't look at it again.
201 :     $alreadySeen{$genomeID} = 1;
202 :     }
203 :     }
204 : parrello 1.5 # Create a variable to indicate whether or not we want to keep this feature.
205 :     my $okFlag;
206 :     # We need to see if we're using statistics or not. This only matters
207 :     # for a two-set situation.
208 :     if ($statistical && $exclusionSetSize > 0) {
209 :     # This looks like it has something to do with variance computations,
210 :     # but I'm not sure.
211 :     my $targetNotCount = $targetSetSize - $targetCount;
212 :     my $targetSquare = $targetCount * $targetCount + $targetNotCount * $targetNotCount;
213 :     my $exclusionNotCount = $exclusionSetSize - $exclusionCount;
214 :     my $exclusionSquare = $exclusionCount * $exclusionCount + $exclusionNotCount * $exclusionNotCount;
215 :     my $mixed = $targetCount * $exclusionCount + $targetNotCount * $exclusionNotCount;
216 :     my $inD = 1 - (($exclusionSetSize * $mixed) / ($targetSetSize * $exclusionSquare));
217 :     my $outD = 1 - (($targetSetSize * $mixed) / ($exclusionSetSize * $targetSquare));
218 :     # If the two differentials are greater than one, we keep this feature.
219 :     $okFlag = ($inD + $outD > 1);
220 :     } else {
221 :     # Check to see if we're common in set 1 and not in set 2.
222 :     if (IsCommon($targetCount, $targetSetSize, $commonality) &&
223 :     ! IsCommon($exclusionCount, $exclusionSetSize, $commonality)) {
224 :     # We satisfy the criterion, so we put this feature to the output.
225 :     $okFlag = 1;
226 :     }
227 :     }
228 :     if ($okFlag) {
229 :     # Put this feature to the output.
230 : parrello 1.4 $self->PutFeature($fquery);
231 :     # Increase the result count.
232 :     $retVal++;
233 :     }
234 :     }
235 : parrello 1.5 # Close the session file.
236 :     $self->CloseSession();
237 : parrello 1.2 }
238 : parrello 1.1 # Return the result count.
239 :     return $retVal;
240 :     }
241 :    
242 :     =head3 Description
243 :    
244 :     C<< my $htmlText = $shelp->Description(); >>
245 :    
246 :     Return a description of this search. The description is used for the table of contents
247 :     on the main search tools page. It may contain HTML, but it should be character-level,
248 :     not block-level, since the description is going to appear in a list.
249 :    
250 :     =cut
251 :    
252 :     sub Description {
253 :     # Get the parameters.
254 :     my ($self) = @_;
255 :     # Return the result.
256 : parrello 1.3 return "Search for features that are common to a group of organisms or that discriminate between two groups of organisms.";
257 : parrello 1.1 }
258 :    
259 : parrello 1.4 =head2 Internal Utilities
260 :    
261 :     =head3 IsCommon
262 :    
263 :     C<< my $flag = SHSigGenes::IsCommon($count, $size, $commonality); >>
264 :    
265 :     Return TRUE if a specified count indicates a gene is common in a specified set.
266 :     Commonality is computed by dividing the count by the size of the set and
267 :     comparing the result to the minimum commonality ratio. The one exception is
268 :     if the set size is 0. In that case, this method always returns FALSE.
269 :    
270 :     =over 4
271 :    
272 :     =item count
273 :    
274 :     Number of elements of the set that have the relevant characteristic.
275 :    
276 :     =item size
277 :    
278 :     Total number of elements in the set.
279 :    
280 :     =item commonality
281 :    
282 :     Minimum count/size ratio for the characteristic to be considered common.
283 :    
284 :     =item RETURN
285 :    
286 :     Returns TRUE if the characteristic is common, else FALSE.
287 :    
288 :     =back
289 :    
290 :     =cut
291 :    
292 :     sub IsCommon {
293 :     # Get the parameters.
294 :     my ($count, $size, $commonality) = @_;
295 :     # Declare the return variable.
296 :     my $retVal = 0;
297 :     # Only procced if the size is positive.
298 :     if ($size > 0) {
299 :     $retVal = ($count/$size >= $commonality);
300 :     }
301 :     # Return the result.
302 :     return $retVal;
303 :     }
304 :    
305 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3