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

Annotation of /FigWebServices/genome_statistics.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (view) (download)

1 : redwards 1.10 # -*- perl -*-
2 : overbeek 1.1 #### start ####
3 : overbeek 1.5
4 : overbeek 1.1 use FIG;
5 :     my $fig = new FIG;
6 :    
7 :     use HTML;
8 :     use strict;
9 :     use CGI;
10 :     my $cgi = new CGI;
11 :    
12 : overbeek 1.2 if (0)
13 : overbeek 1.1 {
14 :     my $VAR1;
15 :     eval(join("",`cat /tmp/statistics_parms`));
16 :     $cgi = $VAR1;
17 :     # print STDERR &Dumper($cgi);
18 :     }
19 :    
20 :     if (0)
21 :     {
22 :     print $cgi->header;
23 :     my @params = $cgi->param;
24 :     print "<pre>\n";
25 :     foreach $_ (@params)
26 :     {
27 :     print "$_\t:",join(",",$cgi->param($_)),":\n";
28 :     }
29 :    
30 :     if (0)
31 :     {
32 :     if (open(TMP,">/tmp/statistics_parms"))
33 :     {
34 :     print TMP &Dumper($cgi);
35 :     close(TMP);
36 :     }
37 :     }
38 :     exit;
39 :     }
40 :    
41 :     my $html = [];
42 : olson 1.7 unshift @$html, "<TITLE>The SEED Statistics Page</TITLE>\n";
43 : overbeek 1.1
44 : overbeek 1.8 my @genomes = $cgi->param('genome');
45 :     my $request = $cgi->param('request');
46 : overbeek 1.9
47 :     if ($request eq "subsystems_summary")
48 :     {
49 :     &subsys_summary($fig,$cgi,$html);
50 :     }
51 : redwards 1.10 elsif ($request eq "edit_kv_stats") {
52 :     &edit_kv_stats($fig,$cgi,$html, $cgi->param('genome'));
53 :     }
54 : overbeek 1.9 elsif ((@genomes == 0) && (! $request))
55 : overbeek 1.8 {
56 :     &table_of_genomes($fig,$cgi,$html);
57 :     }
58 :     elsif (! $request)
59 :     {
60 :     my $genome;
61 :     foreach $genome (@genomes)
62 :     {
63 :     &basic_stats($fig,$cgi,$html,$genome);
64 :     push(@$html,$cgi->hr);
65 :     &assignment_stats($fig,$cgi,$html,$genome);
66 :     push(@$html,$cgi->hr);
67 : redwards 1.14 &kv_peg_stats($fig, $cgi, $html, $genome);
68 :     push(@$html,$cgi->hr);
69 : redwards 1.10 &kv_stats($fig, $cgi, $html, $genome);
70 : overbeek 1.19 my $user = $cgi->param('user');
71 : overbeek 1.21 push(@$html,"<a href=./genome_statistics.cgi?genome=$genome&request=show_subsystems&user=$user>Show Subsystems</a><br>\n");
72 :     push(@$html,"<a href=./genome_statistics.cgi?genome=$genome&request=show_reactions&user=$user>Show Reactions</a><br>\n");
73 : overbeek 1.8 push(@$html,$cgi->br);
74 :     }
75 :     }
76 :     elsif (@genomes == 0)
77 : overbeek 1.1 {
78 : overbeek 1.8 push(@$html,"<h1>Sorry, you need to specify at least one valid genome</h1>\n");
79 : overbeek 1.1 }
80 : overbeek 1.3 else
81 :     {
82 : golsen 1.16 if ($request eq "hypo_sub") { &handle_hypo_sub($fig,$cgi,$html,$genomes[0]) }
83 : overbeek 1.8 elsif ($request eq "hypo_nosub") { &handle_hypo_nosub($fig,$cgi,$html,$genomes[0]) }
84 :     elsif ($request eq "nothypo_sub") { &handle_nothypo_sub($fig,$cgi,$html,$genomes[0]) }
85 :     elsif ($request eq "nothypo_nosub") { &handle_nothypo_nosub($fig,$cgi,$html,$genomes[0]) }
86 :     elsif ($request eq "show_subsystems") { &handle_show_subsystems($fig,$cgi,$html,$genomes[0]) }
87 : overbeek 1.21 elsif ($request eq "show_reactions") { &handle_show_reactions($fig,$cgi,$html,$genomes[0]) }
88 : overbeek 1.8 else
89 :     {
90 :     push(@$html,$cgi->h1("Invalid request: $request"));
91 :     }
92 : overbeek 1.3 }
93 : overbeek 1.1 &HTML::show_page($cgi,$html);
94 :     exit;
95 : overbeek 1.3
96 : golsen 1.16
97 :     # Only subroutines below
98 :    
99 :    
100 : overbeek 1.3 sub basic_stats {
101 :     my($fig,$cgi,$html,$genome) = @_;
102 :    
103 : overbeek 1.8 my($gname,$szdna,$num_contigs,$pegs,$rnas,$taxonomy) = &get_basic_stats($fig,$genome);
104 : golsen 1.16 push @$html, $cgi->h1('Basic Statistics'), "\n",
105 :     "<b>Genome ID:</b> $genome", $cgi->br, "\n",
106 :     "<b>Name:</b> $gname", $cgi->br, "\n",
107 :     "<b>Size (bp):</b> $szdna", $cgi->br, "\n",
108 :     "<b>Number contigs:</b> $num_contigs", $cgi->br, "\n",
109 :     "<b>Number CDSs:</b> $pegs", $cgi->br, "\n",
110 :     "<b>Number rnas:</b> $rnas", $cgi->br, "\n",
111 :     "<b>Taxonomy:</b> $taxonomy", $cgi->br, "\n";
112 :     push @$html, project_description( $genome );
113 : overbeek 1.3 return
114 :     }
115 : overbeek 1.4
116 : golsen 1.16
117 :     sub project_description {
118 :     ( my $genome = shift @_ ) or return ();
119 :     -d $FIG_Config::organisms && -d "$FIG_Config::organisms/$genome"
120 :     && -f "$FIG_Config::organisms/$genome/PROJECT"
121 :     || return ();
122 :     open( PROJECT, "<$FIG_Config::organisms/$genome/PROJECT" ) || return ();
123 :     my @project = <PROJECT>;
124 :     close PROJECT;
125 :     return ( "<b>Project description:</b>\n<pre>",
126 :     ( map { " " . $_ } @project ),
127 :     "</pre>\n"
128 :     );
129 :     }
130 :    
131 :    
132 : overbeek 1.4 sub commify {
133 :     my($n) = @_;
134 :     my(@n) = ();
135 :     my($i);
136 :    
137 :     for ($i = (length($n) - 3); ($i > 0); $i -= 3)
138 :     {
139 :     unshift(@n,",",substr($n,$i,3));
140 :     }
141 :     unshift(@n,substr($n,0,$i+3));
142 :     return join("",@n);
143 :     }
144 :    
145 :     sub assignment_stats {
146 :     my($fig,$cgi,$html,$genome) = @_;
147 :    
148 : overbeek 1.5 my $rdbH = $fig->db_handle;
149 :    
150 :     my $hypo_sub = 0;
151 :     my $hypo_nosub = 0;
152 :     my $nothypo_sub = 0;
153 :     my $nothypo_nosub = 0;
154 :    
155 : overbeek 1.6 my($subsystem_data,$assignments_data) = &get_data($fig,$cgi,$genome);
156 :     my %in = map { $_->[2] => 1 } @$subsystem_data;
157 :     my $in = keys(%in);
158 : overbeek 1.5
159 : overbeek 1.6 foreach $_ (@$assignments_data)
160 : overbeek 1.5 {
161 :     my($peg,$func) = @$_;
162 :     my $is_hypo = &FIG::hypo($func);
163 :    
164 :     if ($is_hypo && $in{$peg}) { $hypo_sub++ }
165 :     elsif ($is_hypo && ! $in{$peg}) { $hypo_nosub++ }
166 :     elsif ((! $is_hypo) && (! $in{$peg})) { $nothypo_nosub++ }
167 :     elsif ((! $is_hypo) && $in{$peg}) { $nothypo_sub++ }
168 :     }
169 :     my $tot = $hypo_sub + $nothypo_sub + $hypo_nosub + $nothypo_nosub;
170 :     my $fracHS = sprintf "%4.2f", $hypo_sub / $tot;
171 :     my $fracNHS = sprintf "%4.2f", $nothypo_sub / $tot;
172 :     my $fracHNS = sprintf "%4.2f", $hypo_nosub / $tot;
173 :     my $fracNHNS = sprintf "%4.2f", $nothypo_nosub / $tot;
174 : overbeek 1.6
175 :     my $user = $cgi->param('user');
176 : overbeek 1.5
177 : golsen 1.16 push @$html, "<table>\n",
178 :     " <tr>\n",
179 :     " <th align=left>PEGs with hypothetical functions and in subsystem:</th>\n",
180 :     " <td align=right><a href=./genome_statistics.cgi?user=$user&genome=$genome&request=hypo_sub>$hypo_sub ($fracHS)</a></td>\n",
181 :     " </tr>\n",
182 :     " <tr>\n",
183 :     " <th align=left>PEGs with nonhypothetical functions and in subsystem:</th>\n",
184 :     " <td align=right><a href=./genome_statistics.cgi?user=$user&genome=$genome&request=nothypo_sub>$nothypo_sub ($fracNHS)</a></td>\n",
185 :     " </tr>\n",
186 :     " <tr>\n",
187 :     " <th align=left>PEGs with hypothetical functions and not in subsystem:</th>\n",
188 :     " <td align=right><a href=./genome_statistics.cgi?user=$user&genome=$genome&request=hypo_nosub>$hypo_nosub ($fracHNS)</a></td>\n",
189 :     " </tr>\n",
190 :     " <tr>\n",
191 :     " <th align=left>PEGs with nonhypothetical functions and not in subsystem:</th>\n",
192 :     " <td align=right><a href=./genome_statistics.cgi?user=$user&genome=$genome&request=nothypo_nosub>$nothypo_nosub ($fracNHNS)</a></td>\n",
193 :     " </tr>\n",
194 :     "</table>\n";
195 : overbeek 1.4 }
196 :    
197 : overbeek 1.5 sub handle_show_subsystems {
198 : overbeek 1.4 my($fig,$cgi,$html,$genome) = @_;
199 : overbeek 1.19 my(%in,$sub,$role,$protein,$sub_link,$tuple,$categories);
200 : overbeek 1.4
201 : overbeek 1.6 my($subsystem_data,$assignments_data) = &get_data($fig,$cgi,$genome);
202 :     foreach $_ (@$subsystem_data)
203 :     {
204 :     ($sub,$role,$protein) = @$_;
205 :     push(@{$in{$sub}->{$role}},&HTML::fid_link($cgi,$protein,0) . ": " . scalar $fig->function_of($protein));
206 :     }
207 : overbeek 1.19
208 :     my @subs = sort { ($a->[0] cmp $b->[0]) or
209 :     ($a->[1] cmp $b->[1]) or
210 :     ($a->[2] cmp $b->[2]) or
211 :     ($a->[3] cmp $b->[3]) or
212 :     ($a->[4] cmp $b->[4])
213 :     }
214 :     map { $sub = $_;
215 :     $categories = $fig->subsystem_classification($sub);
216 :     $categories = ((@$categories > 0) && $categories->[0]) ? $categories : ["Misc"];
217 :     [@$categories,$sub]
218 :     }
219 :     keys(%in);
220 :    
221 : overbeek 1.20 my $last1 = "";
222 :     my $last2 = "";
223 : overbeek 1.19 foreach $tuple (@subs)
224 :     {
225 :     $sub = pop @{$tuple};
226 :     my $topic = $tuple->[0];
227 : overbeek 1.20
228 :     if ($topic ne $last1)
229 :     {
230 :     push(@$html,$cgi->h1($topic));
231 :     $last1 = $topic;
232 :     $last2 = "";
233 :     }
234 :    
235 :     $topic = $tuple->[1] ? $tuple->[1] : "";
236 :     if ($topic && ($topic ne $last2))
237 : overbeek 1.19 {
238 :     push(@$html,$cgi->h2($topic));
239 : overbeek 1.20 $last2 = $topic;
240 : overbeek 1.19 }
241 : overbeek 1.20
242 : overbeek 1.6 $sub_link = &sub_link($cgi,$sub);
243 : overbeek 1.19 push(@$html,$cgi->h3($sub_link));
244 :    
245 : overbeek 1.6 my $roles = [];
246 :     foreach $role (sort keys(%{$in{$sub}}))
247 :     {
248 :     push(@$roles,$cgi->ul($cgi->li($in{$sub}->{$role})));
249 :     }
250 :     push(@$html,$cgi->ul($cgi->li($roles)));
251 :     }
252 : overbeek 1.4 }
253 : overbeek 1.5
254 : overbeek 1.21 sub handle_show_reactions {
255 :     my($fig,$cgi,$html,$genome) = @_;
256 :     my($react_for_role,$r,%topic,%reaction,$sub,$role,$protein,$sub_link,$tuple,$categories);
257 :     my(%reactions_for_sub,%class,$reactions,$classL,$category);
258 :    
259 :     my($subsystem_data,$assignments_data) = &get_data($fig,$cgi,$genome);
260 :     foreach $_ (@$subsystem_data)
261 :     {
262 :     ($sub,$role,$protein) = @$_;
263 :     if (! defined($reactions_for_sub{$sub}))
264 :     {
265 :     my $subsystem = new Subsystem($sub,$fig,0);
266 :     $reactions = $subsystem->get_reactions;
267 :     $reactions = $reactions ? $reactions : "";
268 :     $reactions_for_sub{$sub} = $reactions;
269 :     $class{$sub} = $fig->subsystem_classification($sub);
270 :     }
271 :    
272 :     if (($reactions = $reactions_for_sub{$sub}) && ($react_for_role = $reactions->{$role}))
273 :     {
274 :     $classL = $class{$sub};
275 :     $category = ((@$classL > 0) && $classL->[0]) ? $classL->[0] : "Misc";
276 :     foreach $r (@$react_for_role)
277 :     {
278 :     $reaction{$r}->{$protein} = 1;
279 :     push(@{$topic{$category}},$r);
280 :     }
281 :     }
282 :     }
283 :    
284 :     my @all = sort { $a cmp $b } keys(%topic);
285 :     foreach $category (@all)
286 :     {
287 :     &show_class_react($fig,$cgi,$html,$category,$topic{$category},\%reaction);
288 :     }
289 :    
290 :     if ($_ = $topic{"Misc"})
291 :     {
292 :     &show_class_react($fig,$cgi,$html,'Misc',$_,\%reaction);
293 :     }
294 :     }
295 :    
296 :     sub show_class_react {
297 :     my($fig,$cgi,$html,$class,$for_topic,$reaction) = @_;
298 :     my($r,@pegs,$peg);
299 :    
300 :     push(@$html,$cgi->h1($class));
301 :     foreach $r (@$for_topic)
302 :     {
303 :     my $disp_react = $fig->displayable_reaction($r);
304 :     $disp_react =~ s/^R\d+\: //;
305 :    
306 :     my $rstring = &HTML::reaction_link($r) . ": $disp_react";
307 :     push(@$html,$cgi->h2($rstring),"\n");
308 :     @pegs = sort { &FIG::by_fig_id($a,$b) } keys(%{$reaction->{$r}});
309 :     push(@$html,"<ul>\n");
310 :     foreach $peg (@pegs)
311 :     {
312 :     push(@$html,"<li>" . &HTML::fid_link($cgi,$peg) . " " . scalar $fig->function_of($peg) . "\n");
313 :     }
314 :     push(@$html,"</ul>\n");
315 :     }
316 :     }
317 :    
318 : overbeek 1.5 sub handle_hypo_sub {
319 :     my($fig,$cgi,$html,$genome) = @_;
320 :    
321 : overbeek 1.6 my($subsystem_data,$assignments_data) = &get_data($fig,$cgi,$genome);
322 :     my %subs = map { $_->[2] => $_->[0] } @$subsystem_data;
323 :     my $col_hdrs = ["PEG","Function","Subsystem"];
324 :     my $tab = [];
325 :     foreach $_ (@$assignments_data)
326 :     {
327 :     my($peg,$func) = @$_;
328 :     if (&FIG::hypo($func) && ($subs{$peg}))
329 :     {
330 :     push(@$tab,[&HTML::fid_link($cgi,$peg,0),$func,&sub_link($cgi,$subs{$peg})]);
331 :     }
332 :     }
333 :     $_ = @$tab;
334 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"$_ Hypothetical Pegs in Subsystems"));
335 : overbeek 1.5 }
336 :    
337 :     sub handle_hypo_nosub {
338 :     my($fig,$cgi,$html,$genome) = @_;
339 :    
340 : overbeek 1.6 my($subsystem_data,$assignments_data) = &get_data($fig,$cgi,$genome);
341 :     my %subs = map { $_->[2] => $_->[0] } @$subsystem_data;
342 :     my $col_hdrs = ["PEG","Function"];
343 :     my $tab = [];
344 :     foreach $_ (@$assignments_data)
345 :     {
346 :     my($peg,$func) = @$_;
347 :     if (&FIG::hypo($func) && (! $subs{$peg}))
348 :     {
349 :     push(@$tab,[&HTML::fid_link($cgi,$peg,0),$func]);
350 :     }
351 :     }
352 :     $_ = @$tab;
353 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"$_ Hypothetical Pegs NOT in Subsystems"));
354 : overbeek 1.5 }
355 :    
356 :     sub handle_nothypo_sub {
357 :     my($fig,$cgi,$html,$genome) = @_;
358 :    
359 : overbeek 1.6 my($subsystem_data,$assignments_data) = &get_data($fig,$cgi,$genome);
360 :     my %subs = map { $_->[2] => $_->[0] } @$subsystem_data;
361 :     my $col_hdrs = ["PEG","Function","Subsystem"];
362 :     my $tab = [];
363 :     foreach $_ (@$assignments_data)
364 :     {
365 :     my($peg,$func) = @$_;
366 :     if ((! &FIG::hypo($func)) && ($subs{$peg}))
367 :     {
368 :     push(@$tab,[&HTML::fid_link($cgi,$peg,0),$func,&sub_link($cgi,$subs{$peg})]);
369 :     }
370 :     }
371 :     $_ = @$tab;
372 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"$_ Nonhypothetical Pegs in Subsystems"));
373 : overbeek 1.5 }
374 :    
375 :     sub handle_nothypo_nosub {
376 :     my($fig,$cgi,$html,$genome) = @_;
377 :    
378 : overbeek 1.6 my($subsystem_data,$assignments_data) = &get_data($fig,$cgi,$genome);
379 :     my %subs = map { $_->[2] => $_->[0] } @$subsystem_data;
380 :     my $col_hdrs = ["PEG","Function"];
381 :     my $tab = [];
382 :     foreach $_ (@$assignments_data)
383 :     {
384 :     my($peg,$func) = @$_;
385 :     if ((! &FIG::hypo($func)) && (! $subs{$peg}))
386 :     {
387 :     push(@$tab,[&HTML::fid_link($cgi,$peg,0),$func]);
388 :     }
389 :     }
390 :     $_ = @$tab;
391 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"$_ Nonhypothetical Pegs NOT in Subsystems"));
392 : overbeek 1.5 }
393 :    
394 : overbeek 1.6 sub get_data {
395 :     my($fig,$cgi,$genome) = @_;
396 :    
397 :     my $rdbH = $fig->db_handle;
398 :     my $subsystem_data = $rdbH->SQL("SELECT DISTINCT subsystem,role,protein FROM subsystem_index WHERE ( protein like 'fig\|$genome.peg.%')");
399 :     my $assignment_data = $rdbH->SQL("SELECT prot,assigned_function FROM assigned_functions WHERE ( prot like 'fig\|$genome.peg.%' AND made_by = 'master' )");
400 :    
401 :     return ($subsystem_data,$assignment_data);
402 :     }
403 :    
404 :     sub sub_link {
405 :     my($cgi,$sub) = @_;
406 :    
407 :     my $user = $cgi->param('user');
408 : overbeek 1.19 $user = defined($user) ? $user : "";
409 :     my $sub_link = "<a href=./subsys.cgi?ssa_name=$sub&request=show_ssa&user=$user>$sub</a>";
410 :    
411 : overbeek 1.6 return $sub_link;
412 :     }
413 : overbeek 1.8
414 :     sub get_basic_stats {
415 :     my($fig,$genome) = @_;
416 :    
417 :     my $rdbH = $fig->db_handle;
418 :     my $relational_db_response = $rdbH->SQL("SELECT gname,szdna,pegs,rnas,taxonomy FROM genome WHERE genome = '$genome'");
419 :     my($gname,$szdna,$pegs,$rnas,$taxonomy) = @{$relational_db_response->[0]};
420 :     my $szdna = &commify($szdna);
421 :     my $num_contigs = scalar $fig->all_contigs($genome);
422 :     return ($gname,$szdna,$num_contigs,$pegs,$rnas,$taxonomy);
423 :     }
424 :    
425 :     sub table_of_genomes {
426 :     my($fig,$cgi,$html) = @_;
427 :     my(@genomes);
428 :    
429 :     push(@$html,"<pre>\n");
430 :     if ($cgi->param('complete'))
431 :     {
432 :     @genomes = $fig->genomes("complete");
433 :     }
434 :     else
435 :     {
436 :     @genomes = $fig->genomes;
437 :     }
438 :    
439 :     my $genome;
440 :     push(@$html,join("\t","Genome ID","Complete","Genome Name","Size (bp)","Number Contigs","CDSs","RNAs","Taxonomy") . "\n");
441 :     my $genome;
442 :     foreach $genome (@genomes)
443 :     {
444 :     push(@$html,join("\t",($genome,$fig->is_complete($genome),&get_basic_stats($fig,$genome))) . "\n");
445 :     }
446 :     push(@$html,"</pre>\n");
447 :     }
448 :    
449 :    
450 : overbeek 1.9 sub subsys_summary {
451 :     my($fig,$cgi,$html) = @_;
452 :     my($Nsubs,$genome,$sub,$role,$peg,$genome_instances,%genomes_in_use,$peg_instances,%pegs_in_use);
453 :     foreach $sub ($fig->all_subsystems)
454 :     {
455 :     $Nsubs++;
456 :     foreach $genome (map { $_->[0] } @{$fig->subsystem_genomes($sub)})
457 :     {
458 :     $genome_instances++;
459 :     $genomes_in_use{$genome}++;
460 :     foreach $role ($fig->subsystem_to_roles($sub))
461 :     {
462 :     foreach $peg ($fig->pegs_in_subsystem_cell($sub,$genome,$role))
463 :     {
464 :     $peg_instances++;
465 :     $pegs_in_use{$peg}++;
466 :     }
467 :     }
468 :     }
469 :     }
470 :     my $Ngenomes = scalar keys(%genomes_in_use);
471 :     my $Npegs = scalar keys(%pegs_in_use);
472 :     my $g_in_sub = int($genome_instances / $Nsubs);
473 :     my $p_in_sub = int($peg_instances / $Nsubs);
474 :     push(@$html,$cgi->h1('Subsystem Summary'));
475 :     push(@$html,$cgi->br,
476 :     "<b>Number Subsystems:</b> $Nsubs",$cgi->br,
477 :     "<b>Genomes in Subsystems:</b> $Ngenomes",$cgi->br,
478 :     "<b>PEGs in Subsystems:</b> $Npegs",$cgi->br,
479 :     "<b>Avg genomes per subsystem:</b> $g_in_sub",$cgi->br,
480 :     "<b>Avg PEGs per subsystem:</b> $p_in_sub",$cgi->br
481 :     );
482 :     return
483 :    
484 :     }
485 : redwards 1.10
486 : redwards 1.14 sub kv_peg_stats {
487 :     my ($fig, $cgi, $html, $genome)=@_;
488 :    
489 :     #RAE Added the coverage of each genome with different attributes for the PEGs to find the number of genes that are in pirsf, etc
490 :     push(@$html, "\n<div class=\"pegattributes\">\n<p><h2>PEG Attributes for ", $fig->genus_species($genome), "</h2></p>\n");
491 :    
492 :     my $pegtags=$fig->get_tags('peg');
493 :     foreach my $type (sort {$a cmp $b} keys %$pegtags)
494 :     {
495 : redwards 1.15 my @result = grep {$_ =~ /$genome/} @{$pegtags->{$type}};
496 :     push(@$html, "\nPEGS with tag: $type : ", scalar(@result), $cgi->br, "\n");
497 : redwards 1.14 }
498 :     }
499 :    
500 : redwards 1.10 sub kv_stats {
501 :     my ($fig, $cgi, $html, $genome, $edit)=@_;
502 :    
503 :     # RAE Added tables for key value pairs for an organism, and allow you to edit them
504 :     # figure out kv's for the organism, and make a table with them
505 :    
506 :     # if the optional edit boolean is set and a user is supplied, we will make a table where you can edit the KV pairs
507 :     # else we will just make a blank table
508 :    
509 : redwards 1.12 # prepare the html so we can add form fields here
510 :     push(@$html, "\n<div class=\"attributes\">\n<p><h2>Attributes for ", $fig->genus_species($genome), "</h2></p>\n");
511 :     if ($edit) {push(@$html, $cgi->start_form(-action=>"genome_statistics.cgi"))}
512 :    
513 :    
514 : redwards 1.10 my $tab=[];
515 :     my $user=$cgi->param('user');
516 :     my $col_hdrs=["Attribute", "Value"];
517 : redwards 1.11 if ($user && $edit) {$col_hdrs=["Attribute", "Value", "URL"]}
518 : redwards 1.10
519 :     my $known;
520 : redwards 1.13 foreach my $key (sort {$a->[1] cmp $b->[1]} $fig->get_attributes($genome)) {
521 :     $known->{$key->[1]}=1;
522 : redwards 1.10 if ($user && $edit) {
523 :     push @$tab,
524 :     [
525 : redwards 1.13 $key->[1],
526 :     $cgi->textfield(-name=>"value.".$key->[1], -default=>$key->[2], -size=>50),
527 :     $cgi->textfield(-name=>"url.".$key->[1], -default=>$key->[3], -size=>50),
528 : redwards 1.10 ];
529 :     } else {
530 : redwards 1.13 if ($key->[3] && $key->[3] =~ /^http/) {$key->[2] = "<a href=\"" . $key->[3] . "\">". $key->[2] . "</a>"}
531 : redwards 1.10 push @$tab,
532 :     [
533 : redwards 1.13 $key->[1],
534 :     $key->[2],
535 : redwards 1.10 ];
536 :     }
537 :     }
538 :    
539 :    
540 :     if ($edit) {
541 :     # now we want to add some pull down menus for things that we can add. And some blank boxes too for free text entry.
542 :     # start with three of each
543 :     my $opt=$fig->get_tags("genome"); # all the tags we know about
544 :     my @options=sort {uc($a) cmp uc($b)} grep {!$known->{$_}} keys %$opt;
545 :     unshift(@options, undef); # a blank field at the start
546 :     for (my $i=1; $i<= (scalar @options + 5); $i++) {
547 :    
548 :     # we have the options, and 5 blank fields for free text entry
549 :     my $choice=$cgi->popup_menu(-name=>"key.$i", -values=>\@options);
550 :     if ($i >= scalar @options) {$choice = $cgi->textfield(-name=>"key.$i", -size=>50)}
551 :     push @$tab,
552 :     [
553 :     $choice,
554 :     $cgi->textfield(-name=>"value.$i", -size=>50),
555 :     $cgi->textfield(-name=>"url.$i", -size=>50),
556 :     ];
557 :     }
558 : redwards 1.12 # we need to know how many possibilities we have to look through later. Just pass it as a hidden, rather than counting it next time
559 :     push(@$html, $cgi->hidden(-name=>"max new keys", -value=>scalar @options + 5));
560 : redwards 1.10 }
561 :    
562 :     # now just write the html
563 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Attributes"));
564 :     if ($edit) {
565 :     push(@$html, $cgi->hidden("genome"), $cgi->hidden("user"), $cgi->hidden("request"));
566 :     push(@$html, $cgi->submit('Change'), $cgi->reset());
567 :     }
568 :     else {
569 :     push(@$html,"<p><a href=./genome_statistics.cgi?genome=$genome&request=edit_kv_stats&change=0&user=$user>Edit Key Value Pairs</a></p>\n</div>\n");
570 :     }
571 :     }
572 :    
573 :    
574 :     sub edit_kv_stats {
575 :     my ($fig, $cgi, $html, $genome)=@_;
576 :     if ($cgi->param("Change")) {
577 :     # we have changed the values
578 :     # get the old kv pairs so we can see what has changed
579 :     my $changed; my $deleted;
580 :     foreach my $key ($fig->get_attributes($genome)) {
581 : redwards 1.13 if (!$cgi->param('value.'.$key->[1]) && !$cgi->param('url.'.$key->[1])) {
582 :     $fig->delete_attribute($genome, $key->[1]);
583 : redwards 1.10 push @$key, ["deleted", "td colspan=2 style=\"text-align: center\""];
584 :     push @$deleted, $key;
585 :     }
586 : redwards 1.13 elsif (($cgi->param('value.'.$key->[1]) ne $key->[2]) || ($cgi->param('url.'.$key->[1]) ne $key->[3])) {
587 :     $fig->change_attribute($genome, $key->[1], $cgi->param('value.'.$key->[1]), $cgi->param('url.'.$key->[1]));
588 :     push @$key, $cgi->param('value.'.$key->[1]), $cgi->param('url.'.$key->[1]);
589 : redwards 1.10 push @$changed, $key;
590 :     }
591 :     }
592 :    
593 :     my $added;
594 : redwards 1.12 for (my $i=0; $i <= $cgi->param("max new keys"); $i++) {
595 : redwards 1.10 if ($cgi->param("key.$i")) {
596 :     $fig->add_attribute($genome, $cgi->param("key.$i"), $cgi->param("value.$i"), $cgi->param("url.$i"));
597 :     push @$added, [$cgi->param("key.$i"), ["added", "td colspan=2 style=\"text-align: center\""], $cgi->param("value.$i"), $cgi->param("url.$i")];
598 :     }
599 :     }
600 :    
601 :     # now all we have to do is create a table to report what we have done.
602 :     my $tab=[];
603 :     push (@$html, "<div class=\"altered\"><p><h2>Attributes Altered for ", $fig->genus_species($genome), " ($genome)</h2></p>");
604 :     my $col_hdrs=["Attribute", "Original Value", "Original URL", "New Value", "New URL"];
605 :     if ($changed) {push @$tab, [["<strong>Changed Attributes", "td colspan=5 bgcolor=gray style=\"text-align: center\""]], @$changed}
606 :     if ($deleted) {push @$tab, [["<strong>Deleted Attributes", "td colspan=5 bgcolor=gray style=\"text-align: center\""]], @$deleted}
607 :     if ($added) {push @$tab, [["<strong>Added Attributes", "td colspan=5 bgcolor=gray style=\"text-align: center\""]], @$added}
608 :    
609 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Changed Data"));
610 :     }
611 :     else {
612 :     return kv_stats($fig, $cgi, $html, $genome, 1);
613 :     }
614 :     }
615 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3