[Bio] / FigWebServices / sigs.cgi Repository:
ViewVC logotype

Annotation of /FigWebServices/sigs.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.46 - (view) (download)

1 : overbeek 1.15 ##################################################################
2 : olson 1.14 # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 : overbeek 1.15 ##################################################################
18 : olson 1.14
19 : efrank 1.1 use FIG;
20 : olson 1.13 require SproutFIG;
21 : efrank 1.1
22 :     use HTML;
23 :     use CGI;
24 : parrello 1.20 use Tracer;
25 : parrello 1.36 use PageBuilder;
26 : efrank 1.1 my $cgi = new CGI;
27 :    
28 : overbeek 1.8 if (0) {
29 :     my $VAR1;
30 :     eval(join("",`cat /tmp/sig_parms`));
31 :     $cgi = $VAR1;
32 :     # print STDERR &Dumper($cgi);
33 :     }
34 :    
35 :     if (0) {
36 : efrank 1.1 print $cgi->header;
37 :     my @params = $cgi->param;
38 :     print "<pre>\n";
39 : overbeek 1.8 foreach $_ (@params) {
40 : parrello 1.36 print "$_\t:",join(",",$cgi->param($_)),":\n";
41 : overbeek 1.8 }
42 :    
43 :     if (0) {
44 :     if (open(TMP,">/tmp/sig_parms")) {
45 : parrello 1.36 print TMP &Dumper($cgi);
46 :     close(TMP);
47 :     }
48 : efrank 1.1 }
49 :     exit;
50 :     }
51 :    
52 :     my $html = [];
53 :    
54 : overbeek 1.9 my($fig_or_sprout);
55 :     if ($cgi->param('SPROUT')) {
56 :     $is_sprout = 1;
57 :     $fig_or_sprout = new SproutFIG($FIG_Config::sproutDB, $FIG_Config::sproutData);
58 :     } else {
59 :     $is_sprout = 0;
60 :     $fig_or_sprout = new FIG;
61 :     unshift @$html, "<TITLE>The SEED Signature Genes Page</TITLE>\n";
62 :     }
63 : parrello 1.45
64 :     # Initialize tracing.
65 : parrello 1.46 ETracing($cgi);
66 : parrello 1.45
67 : overbeek 1.42 my $bbhs = $cgi->param('bbhs');
68 :    
69 : efrank 1.1 my @tmp = grep { $_ =~ /^\d+\.\d+$/ } $cgi->param;
70 :     my @set1 = grep { $cgi->param($_) eq "set1" } @tmp;
71 :     my @set2 = grep { $cgi->param($_) eq "set2" } @tmp;
72 :    
73 :     my $given = $cgi->param('given');
74 :    
75 : parrello 1.41 # Determine the operating mode.
76 :     my $submit1 = $cgi->param("Find Common Proteins");
77 :     my $submit2 = $cgi->param("Find Discriminating Proteins");
78 :     # This will be set to 1 for common proteins and 2 for discriminating proteins.
79 :     my $mode = 0;
80 :    
81 :     if (! $given && ($submit1 || $submit2)) {
82 :     push @$html, $cgi->h3("You must select a given organism.");
83 :     } else {
84 :     # Here the user wantsd to process data. Insure the given organism is in
85 :     # set 1.
86 :     my @found = grep { $_ eq $given } @set1;
87 :     if (! @found) {
88 :     push @set1, $given;
89 :     }
90 :     if ($submit1) {
91 :     $mode = 1;
92 :     if (@set2 > 0) {
93 :     push @$html, $cgi->h3("Organisms from set 2 were ignored.");
94 :     }
95 :     } elsif ($submit2) {
96 :     if (@set2 == 0) {
97 :     push @$html, $cgi->h3("Could not process: set 2 was empty.");
98 :     } else {
99 :     $mode = 2;
100 :     }
101 :     }
102 :     }
103 :    
104 :     if ($mode == 0) {
105 : efrank 1.1
106 :     $col_hdrs = ["Given","Set 1","","Set 2","genome","Genus/Species"];
107 :     $tab = [];
108 : parrello 1.45 Trace("Retrieving genomes.") if T(2);
109 : overbeek 1.9 @orgs = $fig_or_sprout->genomes("complete");
110 : parrello 1.45 Trace("Processing genomes.") if T(2);
111 : efrank 1.1 foreach $org (@orgs)
112 :     {
113 : parrello 1.36 $full{$org} = $fig_or_sprout->genus_species($org);
114 : efrank 1.1 }
115 :     @orgs = sort { $full{$a} cmp $full{$b} } @orgs;
116 :    
117 :     @given = $cgi->radio_group(-name => 'given',
118 : parrello 1.36 -values => [@orgs],
119 :     -nolabels => 1);
120 : parrello 1.41 # We're going to create a navigation bar here that gets you automatically
121 :     # to the first organism beginning with a specified letter.
122 : parrello 1.45 Trace("Creating navigation bar.") if T(2);
123 : parrello 1.41 my $bar = $cgi->a({ name => "bar" },"");
124 :     my $currLetter = "";
125 : efrank 1.1 foreach $org (@orgs)
126 :     {
127 : parrello 1.41 my $name = $full{$org};
128 :     my $backToTop = "";
129 :     if (substr($name, 0, 1) ne $currLetter) {
130 :     # Here we have a navigation target: the first organism whose name begins
131 :     # with a new letter. We put in the name label
132 :     $currLetter = substr($name, 0, 1);
133 :     $bar .= $cgi->a({ href => "#$currLetter" }, $currLetter) . " ";
134 :     $name = $cgi->a({ name => $currLetter },"") . $name;
135 :     $backToTop = $cgi->a({ href => "#bar"}, "TOP");
136 :     }
137 : parrello 1.36 push(@$tab,[shift @given,
138 : efrank 1.1 $cgi->radio_group(-name => $org,
139 : parrello 1.36 -default => 'neither',
140 :     -values => ['set1','neither','set2'],
141 :     -nolabels => 1
142 :     ),
143 :     $org,
144 : parrello 1.41 $name, $backToTop]);
145 : efrank 1.1 }
146 : overbeek 1.10 my $sprout = $cgi->param('SPROUT') ? 1 : 0;
147 : parrello 1.41 # Create the navigation bar.
148 : efrank 1.1 push(@$html,$cgi->start_form(-action => 'sigs.cgi'),
149 : overbeek 1.42 $cgi->hidden(-name => 'SPROUT', -value => $sprout));
150 :     if (! $sprout)
151 :     {
152 :     push(@$html,"<br>",$cgi->checkbox( -name => 'bbhs', -value => 1, -override => 1, -label => 'Use BBHs' ),"<br>");
153 :     }
154 : parrello 1.45 Trace("Creating main form.") if T(2);
155 : overbeek 1.42 push(@$html,$cgi->h3("Find Proteins from Given Organism that Discriminate Set 1 from Set 2"),
156 : parrello 1.36 $cgi->br, "Similarity Cutoff: ",$cgi->textfield(-name => "cutoff", -size => 10, -value => 1.0e-10),
157 :     $cgi->br,
158 :     $cgi->checkbox( -name => 'sort_by_func', -value => 1, -override => 1, -checked => 0, -label => 'Sort by Function'),
159 :     $cgi->br,
160 :     $cgi->checkbox( -name => 'write_tab', -value => 1, -override => 1, -checked => 0, -label => 'Export Tab Delimited Table'),
161 :     $cgi->br,
162 : overbeek 1.42 $cgi->br,
163 : parrello 1.41 $cgi->submit("Find Discriminating Proteins"),$cgi->reset,
164 :     $cgi->br,
165 : parrello 1.36 $cgi->br,
166 : parrello 1.41 $cgi->h3("Find Proteins from Given Organism that are Common to Set 1"),
167 : parrello 1.36 $cgi->br,
168 : parrello 1.41 "Minimum matches from Set 1: ", $cgi->textfield(-name => minN, -size => 5),
169 : parrello 1.36 $cgi->br,
170 : parrello 1.41 $cgi->submit("Find Common Proteins"),$cgi->reset,
171 :     $cgi->br, $cgi->br,
172 :     $cgi->h3("Select Given, Set 1, and Set 2"),
173 :     $cgi->p($bar),
174 :     &HTML::make_table($col_hdrs,$tab,"Pick organisms for Set 1 and Set 2"),
175 :     $cgi->br);
176 : parrello 1.28 if ($cgi->param('TTYPE')) {
177 :     push @$html,
178 :     $cgi->br,
179 :     $cgi->br, "Tracing: ",$cgi->textfield(-name => "TRACE", -size => 30, -value => ""),
180 :     $cgi->hidden(-name => 'TTYPE', -value => $cgi->param('TTYPE'));
181 :     }
182 :     push @$html,
183 : parrello 1.36 $cgi->end_form,
184 :     "\n";
185 : parrello 1.41 } else {
186 :     # Mode 1 or 2
187 : efrank 1.1 my $sim_cutoff = $cgi->param('sim_cutoff');
188 :     if (! $sim_cutoff) { $sim_cutoff = 1.0e-10 }
189 : parrello 1.41 my @hits;
190 :     if ($mode == 2) {
191 : parrello 1.45 Trace("Computing discriminants.") if T(2);
192 : overbeek 1.42 @hits = &differentiating_genes(\@set1,\@set2,$given,$sim_cutoff,$is_sprout,$bbhs);
193 : parrello 1.41 Trace(scalar(@hits) . " hits found by differentiating_genes.") if T(3);
194 :     if ($cgi->param('sort_by_func')) {
195 :     @hits = sort { ($a->[2] cmp $b->[2]) or ($b->[1] <=> $a->[1]) or (&FIG::by_fig_id($a->[0],$b->[0])) } @hits;
196 :     } else {
197 :     @hits = sort { ($b->[1] <=> $a->[1]) || (&FIG::by_fig_id($a->[0],$b->[0])) } @hits;
198 :     }
199 : efrank 1.1
200 : parrello 1.41 if ($cgi->param('write_tab')) {
201 :     push(@$html,"<pre>\n");
202 :     foreach $_ (@hits) {
203 :     push(@$html,join("\t",@$_) . "\n");
204 : parrello 1.36 }
205 : parrello 1.41 push(@$html,"</pre>\n");
206 :     } else {
207 : overbeek 1.42 $col_hdrs = ["","Gene","Score","Function","Subsystems"];
208 : parrello 1.41 $tab = [];
209 :     my $gs = $fig_or_sprout->genus_species($given);
210 :     $title = "Genes in $gs that Discriminate";
211 :     my $subscript = 1;
212 :     foreach $_ (@hits) {
213 :     my($peg,$score,$function) = @$_;
214 : overbeek 1.42 my @sub = grep { $fig_or_sprout->usable_subsystem($_) } $fig_or_sprout->peg_to_subsystems($peg);
215 :    
216 :     push(@$tab,[$subscript,&HTML::fid_link($cgi,$peg,"local"),$score,$function,join("<br>",@sub)]);
217 : parrello 1.41 $subscript++;
218 : parrello 1.36 }
219 : parrello 1.41 push(@$html,&HTML::make_table($col_hdrs,$tab,$title));
220 :     }
221 :     } else {
222 :     # Mode 1
223 :     my($i,$j,%which_col,$peg1,$func1,$link,$genome1,$hit);
224 :     my $minN = $cgi->param('minN');
225 :     $minN = $minN ? $minN : @set1;
226 : overbeek 1.42 @hits = &common_genes(\@set1,$given,$sim_cutoff,$minN,$is_sprout,$bbhs);
227 : parrello 1.41 $col_hdrs = ["",&FIG::abbrev($fig_or_sprout->genus_species($given)),"Score","Function"];
228 :     for ($i=0; ($i < @set1); $i++) {
229 :     $which_col{$set1[$i]} = $i+4;
230 :     push(@$col_hdrs,&FIG::abbrev($fig_or_sprout->genus_species($set1[$i])));
231 :     }
232 :    
233 :     $tab = [];
234 :     $title = "Genes in Common";
235 : parrello 1.36
236 : parrello 1.41 for ($j=0; ($j < @hits); $j++) {
237 :     my($peg,$score,$hits) = @{$hits[$j]};
238 :     my $func = scalar $fig_or_sprout->function_of($peg,$cgi->param('user'));
239 :     my $row = [$j+1,&HTML::fid_link($cgi,$peg,"local"),$score,$func];
240 :     for ($i=0; ($i < @set1); $i++) {
241 :     push(@$row,"");
242 : parrello 1.36 }
243 : parrello 1.41 foreach $hit (@$hits) {
244 :     ($peg1,$func1) = @$hit;
245 :     $genome1 = &FIG::genome_of($peg1);
246 :     $col = $which_col{$genome1};
247 :     $link = &HTML::fid_link($cgi,$peg1,"local");
248 :     $row->[$col] = ($func eq $func1) ? $link : "*$link";
249 : parrello 1.36 }
250 : parrello 1.41 push(@$tab,$row);
251 : parrello 1.36 }
252 : parrello 1.41 push(@$html,&HTML::make_table($col_hdrs,$tab,$title));
253 : efrank 1.1 }
254 :     }
255 : parrello 1.35 if ($is_sprout) {
256 :     # For sprout, we use a template.
257 : parrello 1.39 my $template = "$FIG_Config::template_url/Sigs_tmpl.php";
258 : parrello 1.45 my $traceList = QTrace("html");
259 : parrello 1.40 my $result = PageBuilder::Build($template, { data => join("\n", @$html),
260 :     tracing => $traceList }, "Html");
261 : parrello 1.38 print "CONTENT-TYPE: text/html\n\n";
262 : parrello 1.39 print $result;
263 : parrello 1.35 } else {
264 :     # For SEED, a normal SEED page.
265 : parrello 1.45 push @$html, QTrace("html");
266 : parrello 1.35 &HTML::show_page($cgi,$html);
267 :     }
268 : efrank 1.1
269 : overbeek 1.6 sub common_genes {
270 : overbeek 1.42 my($set1,$given,$sim_cutoff,$minN,$is_sprout,$bbhs) = @_;
271 : overbeek 1.6 my %set1 = map { $_ => 1 } @$set1;
272 :     if ($set1{$given})
273 :     {
274 : parrello 1.36 $minN--;
275 :     $set1{$given} = 0;
276 : overbeek 1.6 }
277 :    
278 : overbeek 1.9 foreach $peg ($fig_or_sprout->all_features($given,"peg"))
279 : overbeek 1.6 {
280 : parrello 1.36 undef %hits_set1;
281 : overbeek 1.42 foreach $sim (($is_sprout || $bbhs) ? $fig_or_sprout->bbhs($peg, $sim_cutoff) : $fig_or_sprout->sims($peg, 1000, $sim_cutoff, "fig"))
282 : parrello 1.36 {
283 : overbeek 1.42 $id2 = ($is_sprout || $bbhs) ? $sim->[0] : $sim->[1]; # id2
284 : parrello 1.36 if ($id2 =~ /^fig\|(\d+\.\d+)/)
285 :     {
286 :     my $org1 = $1;
287 :     if ($set1{$org1})
288 :     {
289 :     if (! $hits_set1{$org1})
290 :     {
291 :     $hits_set1{$org1} = $id2;
292 :     }
293 :     }
294 :     }
295 :     }
296 :     $sc = keys(%hits_set1);
297 :     if ($sc >= $minN)
298 :     {
299 :     push(@hits,[$peg,$sc,[map { [$hits_set1{$_}, scalar $fig_or_sprout->function_of($hits_set1{$_})] } keys(%hits_set1)]]);
300 :     }
301 : overbeek 1.6 }
302 :     return @hits;
303 :     }
304 : parrello 1.36
305 : efrank 1.1 sub differentiating_genes {
306 : overbeek 1.42 my($set1,$set2,$given,$sim_cutoff,$is_sprout,$bbhs) = @_;
307 : efrank 1.1 my %set1 = map { $_ => 1 } @$set1;
308 :     my %set2 = map { $_ => 1 } @$set2;
309 :    
310 :     my(%hits_set1,%hits_set2,@hits,$sim,$id2,$peg);
311 : parrello 1.40 my $pegCount = 0;
312 :     my $simCount = 0;
313 : overbeek 1.9 foreach $peg ($fig_or_sprout->all_features($given,"peg"))
314 : efrank 1.1 {
315 : parrello 1.40 $pegCount++;
316 : parrello 1.36 undef %hits_set1; undef %hits_set2;
317 :     $hits_set1{&FIG::genome_of($peg)} = 1;
318 : parrello 1.40 Trace("Processing $peg.") if T(4);
319 : overbeek 1.42 foreach $sim (($is_sprout || $bbhs) ? $fig_or_sprout->bbhs($peg, $sim_cutoff) : $fig_or_sprout->sims($peg, 1000, $sim_cutoff, "fig"))
320 : parrello 1.36 {
321 : parrello 1.40 $simCount++;
322 : overbeek 1.42 $id2 = ($is_sprout || $bbhs) ? $sim->[0] : $sim->[1];
323 : parrello 1.23 Trace("SIG tool sim check for $peg vs. $id2.") if T(4);
324 : parrello 1.36 if ($id2 =~ /^fig\|(\d+\.\d+)/)
325 :     {
326 :     my $org1 = $1;
327 :     if ($set1{$org1})
328 :     {
329 :     $hits_set1{$org1} = 1;
330 :     }
331 :     elsif ($set2{$org1})
332 :     {
333 :     $hits_set2{$org1} = 1;
334 :     }
335 :     }
336 :     }
337 :     my $n_set1 = keys(%hits_set1);
338 :     my $n_set2 = keys(%hits_set2);
339 :    
340 :     # my $sc = sprintf "%.3f", ($n_set1 / @set1) - (($n_set2 / @set2) * $fudge);
341 :     my $sc = sprintf "%.3f", &sig($n_set1,(@set1 - $n_set1),$n_set2,(@set2 - $n_set2));
342 :     if ($sc >= 1)
343 :     {
344 :     push(@hits,[$peg,$sc,scalar $fig_or_sprout->function_of($peg)]);
345 :     }
346 : efrank 1.1 }
347 : parrello 1.40 Trace(scalar(@hits) . " hits found by differentiator off of $simCount sims for $pegCount pegs with cutoff $sim_cutoff.") if T(3);
348 : efrank 1.1 return @hits;
349 :     }
350 :    
351 :     sub sig {
352 :     my($in_has,$in_has_not,$out_has,$out_has_not) = @_;
353 :     my($sx,$sy,$xx,$xy,$yy,$din,$dout);
354 :     $sx = $in_has + $in_has_not;
355 :     $sy = $out_has + $out_has_not;
356 :     $xx = ($in_has * $in_has) + ($in_has_not * $in_has_not);
357 :     $xy = ($in_has * $out_has) + ($in_has_not * $out_has_not);
358 :     $yy = ($out_has * $out_has) + ($out_has_not * $out_has_not);
359 :     (($sx > 0) && ($yy > 0) && ($sy > 0) && ($xx > 0)) || return 0;
360 :     $din = 1 - (($sy * $xy) / ($sx * $yy));
361 :     $dout = 1 - (($sx * $xy) / ($sy * $xx));
362 :     return $din+$dout;
363 :     }
364 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3