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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.186 - (view) (download)

1 : overbeek 1.1 # -*- perl -*-
2 : olson 1.130 #
3 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
4 :     # for Interpretations of Genomes. All Rights Reserved.
5 :     #
6 :     # This file is part of the SEED Toolkit.
7 :     #
8 :     # The SEED Toolkit is free software. You can redistribute
9 :     # it and/or modify it under the terms of the SEED Toolkit
10 :     # Public License.
11 :     #
12 :     # You should have received a copy of the SEED Toolkit Public License
13 :     # along with this program; if not write to the University of Chicago
14 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
15 :     # Genomes at veronika@thefig.info or download a copy from
16 :     # http://www.theseed.org/LICENSE.TXT.
17 :     #
18 :    
19 : overbeek 1.1 use FIG;
20 : golsen 1.103 use FIGjs; # mouseover()
21 : mkubal 1.165 use GD;
22 :     use MIME::Base64;
23 :    
24 : overbeek 1.1 my $fig = new FIG;
25 : overbeek 1.9
26 : overbeek 1.1 use Subsystem;
27 :    
28 : golsen 1.75 use URI::Escape; # uri_escape()
29 : overbeek 1.1 use HTML;
30 :     use strict;
31 :     use tree_utilities;
32 :    
33 : overbeek 1.158 use raelib;
34 :     my $raelib=new raelib; #this is for the excel workbook stuff.
35 :    
36 : overbeek 1.1 use CGI;
37 : overbeek 1.135 use CGI::Carp qw(fatalsToBrowser); # this makes debugging a lot easier by throwing errors out to the browser
38 : overbeek 1.9
39 : overbeek 1.1 my $cgi = new CGI;
40 : redwards 1.121
41 : olson 1.143 $ENV{"PATH"} = "$FIG_Config::bin:$FIG_Config::ext_bin:" . $ENV{"PATH"};
42 : redwards 1.121
43 : overbeek 1.146 if (0)
44 : overbeek 1.1 {
45 :     my $VAR1;
46 :     eval(join("",`cat /tmp/ssa_parms`));
47 :     $cgi = $VAR1;
48 :     # print STDERR &Dumper($cgi);
49 :     }
50 :    
51 :     if (0)
52 :     {
53 :     print $cgi->header;
54 :     my @params = $cgi->param;
55 :     print "<pre>\n";
56 :     foreach $_ (@params)
57 :     {
58 : parrello 1.127 print "$_\t:",join(",",$cgi->param($_)),":\n";
59 : overbeek 1.1 }
60 :    
61 :     if (0)
62 :     {
63 : parrello 1.127 if (open(TMP,">/tmp/ssa_parms"))
64 :     {
65 :     print TMP &Dumper($cgi);
66 :     close(TMP);
67 :     }
68 : overbeek 1.1 }
69 :     exit;
70 :     }
71 :    
72 :     # request to display the phylogenetic tree
73 :     #
74 :     my $request = $cgi->param("request");
75 :     if ($request && ($request eq "show_tree"))
76 :     {
77 :     print $cgi->header;
78 :     &show_tree;
79 :     exit;
80 :     }
81 :    
82 : golsen 1.186 # Some timing code that can be deleted: ## time ##
83 :     my $time_it = 0; ## time ##
84 :     my @times; ## time ##
85 :     push @times, scalar time() if $time_it; ## time ##
86 :    
87 : overbeek 1.1 my $html = [];
88 : golsen 1.186 push @$html, ( $cgi->param('ssa_name') ? "<TITLE>SEED Subsystem: " . $cgi->param('ssa_name') . "</TITLE>\n"
89 :     : "<TITLE>SEED Subsystems</TITLE>\n"
90 :     ); # RAE: every page deserves a title
91 : overbeek 1.1
92 : overbeek 1.128 my $user = $cgi->param('user');
93 : overbeek 1.175 if ($user !~ /^master:/) { $user = "master:$user" }
94 : redwards 1.121
95 : overbeek 1.1 $fig->set_user($user);
96 :    
97 : overbeek 1.14 if ($cgi->param('resynch_peg_connections') && (my $ssa = $cgi->param('ssa_name')))
98 : overbeek 1.9 {
99 :     my $subsystem = new Subsystem($ssa,$fig,0);
100 :     $subsystem->db_sync(0);
101 :     undef $subsystem;
102 :     &one_cycle($fig,$cgi,$html);
103 :     }
104 : overbeek 1.14 elsif ($user && ($cgi->param("extend_with_billogix")))
105 : overbeek 1.1 {
106 :     #
107 :     # Start a bg task to extend the subsystem.
108 :     #
109 :    
110 :     my $ssa = $cgi->param('ssa_name');
111 :    
112 :     my $sub = $fig->get_subsystem($ssa);
113 :    
114 : overbeek 1.14 if ($sub && ($user eq $sub->get_curator))
115 : overbeek 1.1 {
116 : parrello 1.127 #
117 :     # See if there's already an extend job running.
118 :     #
119 :    
120 :     my $curpid = $sub->get_current_extend_pid();
121 :     if ($curpid)
122 :     {
123 :     warn "Found current pid $curpid\n";
124 :     my $j = $fig->get_job($curpid);
125 :     warn "job is $j\n";
126 :     warn "running is ", $j->running(), "\n" if $j;
127 :     if ($j && $j->running())
128 :     {
129 :     push(@$html, "Subsystem extension is already running as job number $curpid. <br>",
130 :     "Click <a href=\"seed_ctl.cgi?user=$user\">here</a> to see currently running jobs and their status");
131 :     last;
132 :     }
133 :     }
134 :    
135 :     my $pid = $fig->run_in_background(sub {$sub->extend_with_billogix($user);});
136 :    
137 :     push(@$html,
138 :     "Subsystem extension started as background job number $pid <br>\n",
139 :     "Click <a href=\"seed_ctl.cgi?user=$user\">here</a> to see currently running jobs and their status");
140 :    
141 :     $sub->set_current_extend_pid($pid);
142 : overbeek 1.1 }
143 :     else
144 :     {
145 : parrello 1.127 push(@$html, "Subsystem '$ssa' could not be loaded");
146 : overbeek 1.1 }
147 :     &HTML::show_page($cgi, $html);
148 :     exit;
149 :     }
150 : overbeek 1.180 elsif ($cgi->param('lock annotations') && ($user = $cgi->param('user')))
151 :     {
152 :     my @orgs = $cgi->param('genome_to_lock');
153 :     @orgs = map { $_ =~ /^(\d+\.\d+)/; $1 } @orgs;
154 :     my @roles = $cgi->param('roles_to_lock');
155 :     my $ssa = $cgi->param('ssa_name');
156 :     push(@$html,"<br>");
157 :    
158 :     foreach my $genome (@orgs)
159 :     {
160 :     foreach my $role (@roles)
161 :     {
162 :     foreach my $peg ($fig->pegs_in_subsystem_cell($ssa,$genome,$role))
163 :     {
164 :     $user =~ s/master://;
165 :     $fig->lock_fid($user,$peg);
166 :     push(@$html,"locked $peg<br>\n");
167 :     }
168 :     }
169 :     }
170 :     &HTML::show_page($cgi, $html);
171 :     exit;
172 :     }
173 :     elsif ($cgi->param('unlock annotations') && ($user = $cgi->param('user')))
174 :     {
175 :     my @orgs = $cgi->param('genome_to_lock');
176 :     @orgs = map { $_ =~ /^(\d+\.\d+)/; $1 } @orgs;
177 :     my @roles = $cgi->param('roles_to_lock');
178 :     my $ssa = $cgi->param('ssa_name');
179 :     push(@$html,"<br>");
180 :    
181 :     foreach my $genome (@orgs)
182 :     {
183 :     foreach my $role (@roles)
184 :     {
185 :     foreach my $peg ($fig->pegs_in_subsystem_cell($ssa,$genome,$role))
186 :     {
187 :     $user =~ s/master://;
188 :     $fig->unlock_fid($user,$peg);
189 :     push(@$html,"unlocked $peg<br>\n");
190 :     }
191 :     }
192 :     }
193 :     &HTML::show_page($cgi, $html);
194 :     exit;
195 :     }
196 : overbeek 1.1 else
197 :     {
198 :     $request = defined($request) ? $request : "";
199 : overbeek 1.8
200 : overbeek 1.14 if (($request eq "reset") && $user)
201 : overbeek 1.1 {
202 : parrello 1.127 &reset_ssa($fig,$cgi,$html); # allow user to go back to a previous version of the ss
203 : overbeek 1.1 }
204 : overbeek 1.14 elsif (($request eq "reset_to") && $user)
205 : overbeek 1.1 {
206 : parrello 1.127 &reset_ssa_to($fig,$cgi,$html); # this actually resets to the previous version
207 :     &one_cycle($fig,$cgi,$html);
208 : overbeek 1.1 }
209 : overbeek 1.14 elsif (($request eq "make_exchangable") && $user)
210 : overbeek 1.1 {
211 : parrello 1.127 &make_exchangable($fig,$cgi,$html);
212 :     &show_initial($fig,$cgi,$html);
213 : overbeek 1.1 }
214 : overbeek 1.14 elsif (($request eq "make_unexchangable") && $user)
215 : overbeek 1.1 {
216 : parrello 1.127 &make_unexchangable($fig,$cgi,$html);
217 :     &show_initial($fig,$cgi,$html);
218 : overbeek 1.1 }
219 :     elsif ($request eq "show_ssa")
220 :     {
221 : overbeek 1.185 my $ssa = $cgi->param('ssa_name');
222 :     if (-s "$FIG_Config::data/Subsystems/$ssa/warnings")
223 :     {
224 :     my $ts = localtime($^T - ((-M "$FIG_Config::data/Subsystems/$ssa/warnings") * 24 * 60 * 60));
225 :     push(@$html,$cgi->h1("Last check was at $ts"));
226 :    
227 :     my @tmp = `cut -f2 $FIG_Config::data/Subsystems/$ssa/warnings`;
228 :     my @mismatches = grep { $_ =~ /mismatch/ } @tmp;
229 :     my $mismatchesN = (@mismatches > 0) ? @mismatches : 0;
230 :     if ($mismatchesN)
231 :     {
232 :     push(@$html,$cgi->h2("$mismatchesN entries mismatch the role"));
233 :     }
234 :     my @left_out = grep { $_ =~ /left-out/ } @tmp;
235 :     my $left_outN = (@left_out > 0) ? @left_out : 0;
236 :     if ($left_outN)
237 :     {
238 :     push(@$html,$cgi->h2("$left_outN entries should be added for existing genomes"));
239 :     }
240 :     my @maybe_add = grep { $_ =~ /maybe-add/ } @tmp;
241 :     my $maybe_addN = (@maybe_add > 0) ? @maybe_add : 0;
242 :     if ($maybe_addN)
243 :     {
244 :     push(@$html,$cgi->h2("$maybe_addN genomes maybe should be added"));
245 :     }
246 :     if ($mismatchesN || $left_outN || $maybe_addN)
247 :     {
248 :     push(@$html,"<b>To see results of the last check:</b>&nbsp;",
249 :     $cgi->a({href => "check_subsys.cgi?user=$user&subsystem=$ssa&request=check_ssa&fast=1",
250 :     target => 'check_window'
251 :     },
252 :     "click here")
253 :     );
254 :     }
255 :     push(@$html,"<br>","If you wish to run a new check now: ",
256 :     $cgi->a({href => "check_subsys.cgi?user=$user&subsystem=$ssa&request=check_ssa",
257 :     target => 'check_window'
258 :     },
259 :     "click here<hr><br>")
260 :     );
261 :    
262 :     }
263 : parrello 1.127 &one_cycle($fig,$cgi,$html);
264 : overbeek 1.1 }
265 :     #
266 :     # Note that this is a little different; I added another submit button
267 :     # to the delete_or_export_ssa form, so have to distinguish between them
268 :     # here based on $cgi->param('delete_export') - the original button,
269 :     # or $cgi->param('publish') - the new one.
270 :     #
271 : overbeek 1.14 elsif (($request eq "delete_or_export_ssa") && $user &&
272 : parrello 1.127 defined($cgi->param('delete_export')))
273 : overbeek 1.1 {
274 : parrello 1.127 my($ssa,$exported);
275 :     $exported = 0;
276 :     foreach $ssa ($cgi->param('export'))
277 :     {
278 :     if (! $exported)
279 :     {
280 :     print $cgi->header;
281 :     print "<pre>\n";
282 :     }
283 :     &export($fig,$cgi,$ssa);
284 :     $exported = 1;
285 :     }
286 :    
287 :     foreach $ssa ($cgi->param('export_assignments'))
288 :     {
289 :     &export_assignments($fig,$cgi,$ssa);
290 :     }
291 :    
292 :     foreach $ssa ($cgi->param('delete'))
293 :     {
294 :     my $sub = $fig->get_subsystem($ssa);
295 :     $sub->delete_indices();
296 : overbeek 1.139
297 : parrello 1.127 my $cmd = "rm -rf '$FIG_Config::data/Subsystems/$ssa'";
298 :     my $rc = system $cmd;
299 :     }
300 :    
301 :     if (! $exported)
302 :     {
303 : overbeek 1.139 &show_initial($fig,$cgi,$html);
304 :     }
305 :     else
306 :     {
307 :     print "</pre>\n";
308 :     exit;
309 :     }
310 :     }
311 :     elsif (($request eq "delete_or_export_ssa") && $user &&
312 :     defined($cgi->param('publish')))
313 :     {
314 :     my($ssa,$exported);
315 :     my($ch) = $fig->get_clearinghouse();
316 :    
317 :     print $cgi->header;
318 :    
319 :     if (!defined($ch))
320 :     {
321 :     print "cannot publish: clearinghouse not available\n";
322 :     exit;
323 :     }
324 :    
325 :     foreach $ssa ($cgi->param('publish_to_clearinghouse'))
326 :     {
327 :     print "<h2>Publishing $ssa to clearinghouse...</h2>\n";
328 :     $| = 1;
329 :     print "<pre>\n";
330 :     my $res = $fig->publish_subsystem_to_clearinghouse($ssa);
331 :     print "</pre>\n";
332 :     if ($res)
333 :     {
334 :     print "Published <i>$ssa </i> to clearinghouse<br>\n";
335 : parrello 1.127 }
336 : overbeek 1.139 else
337 : parrello 1.127 {
338 : overbeek 1.139 print "<b>Failed</b> to publish <i>$ssa</i> to clearinghouse<br>\n";
339 :     }
340 :     }
341 :     exit;
342 :     }
343 :     elsif (($request eq "delete_or_export_ssa") && $user &&
344 :     defined($cgi->param('reindex')))
345 :     {
346 : parrello 1.127
347 : overbeek 1.139 my @ss=$cgi->param('index_subsystem');
348 :     my $job = $fig->index_subsystems(@ss);
349 :     push @$html, "<h2>ReIndexing these subsystems...</h2>\n<ul>", map {"<li>$_</li>"} @ss;
350 :     push @$html, "</ul>\n<p>... is running in the background with job id $job. You may check it in the ",
351 :     "<a href=\"seed_ctl.cgi?user=$user\">SEED Control Panel</a></p>\n";
352 :     &show_initial($fig,$cgi,$html);
353 :     }
354 :     elsif (($request eq "delete_or_export_ssa") && $user &&
355 : redwards 1.176 defined($cgi->param('save_clicks')))
356 : overbeek 1.139 {
357 :     my @userss=$cgi->param("users_ss");
358 :     my %nmpdrss=map {($_=>1)} $cgi->param("nmpdr_ss");
359 : redwards 1.176 my %distss=map {($_=>1)} $cgi->param("dist_ss");
360 :     my %autoss=map {($_=>1)} $cgi->param("auto_update_ok");
361 : parrello 1.127
362 : overbeek 1.139 foreach my $ssa (@userss)
363 :     {
364 :     $nmpdrss{$ssa} ? $fig->nmpdr_subsystem($ssa, 1) : $fig->nmpdr_subsystem($ssa, -1);
365 : redwards 1.176 $distss{$ssa} ? $fig->distributable_subsystem($ssa, 1) : $fig->distributable_subsystem($ssa, -1);
366 :     $autoss{$ssa} ? $fig->ok_to_auto_update_subsys($ssa, 1) : $fig->ok_to_auto_update_subsys($ssa, -1);
367 : overbeek 1.140 }
368 :     &manage_subsystems($fig,$cgi,$html);
369 :     }
370 : overbeek 1.14 elsif ($user && ($request eq "new_ssa") && ($cgi->param('copy_from1')) && (! $cgi->param('cols_to_take1')))
371 : overbeek 1.1 {
372 : parrello 1.127 my $name = $cgi->param('ssa_name');
373 :     my $copy_from1 = $cgi->param('copy_from1');
374 :     my $copy_from2 = $cgi->param('copy_from2');
375 : overbeek 1.1 my(@roles1,@roles2);
376 :    
377 : parrello 1.127 push(@$html,$cgi->start_form(-action => "subsys.cgi",
378 : overbeek 1.139 -method => 'post'),
379 :     $cgi->hidden(-name => 'copy_from1', -value => $copy_from1, -override => 1),
380 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
381 :     $cgi->hidden(-name => 'ssa_name', -value => $name, -override => 1),
382 :     $cgi->hidden(-name => 'request', -value => 'new_ssa', -override => 1)
383 :     );
384 : parrello 1.127
385 :     @roles1 = $fig->subsystem_to_roles($copy_from1);
386 :     if (@roles1 > 0)
387 :     {
388 :     push(@$html,$cgi->h1("select columns to be taken from $copy_from1"),
389 : overbeek 1.139 $cgi->scrolling_list(-name => 'cols_to_take1',
390 :     -values => ['all',@roles1],
391 :     -size => 10,
392 :     -multiple => 1
393 :     ),
394 :     $cgi->hr
395 :     );
396 : parrello 1.127 }
397 :    
398 :     if ($copy_from2)
399 :     {
400 :     @roles2 = $fig->subsystem_to_roles($copy_from2);
401 :     if (@roles2 > 0)
402 :     {
403 :     push(@$html,$cgi->hidden(-name => 'copy_from2', -value => $copy_from2, -override => 1));
404 :     push(@$html,$cgi->h1("select columns to be taken from $copy_from2"),
405 : overbeek 1.139 $cgi->scrolling_list(-name => 'cols_to_take2',
406 :     -values => ['all',@roles2],
407 :     -size => 10,
408 :     -multiple => 1
409 :     ),
410 :     $cgi->hr
411 :     );
412 : parrello 1.127 }
413 :     }
414 :     push(@$html,$cgi->submit('build new subsystem'),
415 : overbeek 1.139 $cgi->end_form
416 :     );
417 : overbeek 1.1 }
418 : redwards 1.89 elsif ($user && ($request eq "new_ssa") && ($cgi->param('move_from')))
419 :     {
420 : parrello 1.127 my $name = $cgi->param('ssa_name');
421 :     $name=$fig->clean_spaces($name);
422 :     $name=~s/ /_/g;
423 :     my $move_from = $cgi->param('move_from');
424 : redwards 1.89 if (-d "$FIG_Config::data/Subsystems/$move_from" && !(-e "$FIG_Config::data/Subsystems/$name")) {
425 : overbeek 1.139 my $res=`mv $FIG_Config::data/Subsystems/$move_from $FIG_Config::data/Subsystems/$name`;
426 :     my $job = $fig->index_subsystems($name);
427 :     push @$html, "<p>The subsystem <b>$move_from</b> was moved to <b>$name</b> and got the result $res. The new subsystem is being indexed with job id $job\n",
428 :     "(check the <a href=\"seed_ctl.cgi?user=$user\">SEED control panel</a> for more information</p>\n";
429 : parrello 1.127 }
430 :     elsif (-e "$FIG_Config::data/Subsystems/$name")
431 :     {
432 : overbeek 1.139 push @$html, "<p>The subsystem <b>$move_from</b> was <b><i>NOT</i></b> moved because the subsystem $name already exists</p>";
433 : parrello 1.127 }
434 :     else {
435 : overbeek 1.139 push @$html, "<p>The subsystem <b>$move_from</b> was not found. Sorry</p>";
436 : parrello 1.127 }
437 : redwards 1.89 &show_initial($fig,$cgi,$html);
438 : parrello 1.127 }
439 : overbeek 1.1 elsif ($request eq "new_ssa")
440 :     {
441 : parrello 1.127 &new_ssa($fig,$cgi,$html);
442 : overbeek 1.1 }
443 : mkubal 1.165
444 : redwards 1.108 #RAE: undelete these 5 commented out line for the new interface
445 : overbeek 1.112 elsif ($request eq "manage_ss")
446 :     # else
447 : redwards 1.108 {
448 :     &manage_subsystems($fig,$cgi,$html);
449 : overbeek 1.1 }
450 : overbeek 1.112 else
451 :     {
452 : overbeek 1.141 # push @$html, $cgi->div({class=>"diagnostic"}, "Request: $request\n");
453 : parrello 1.127 &show_initial($fig,$cgi,$html);
454 : overbeek 1.112 }
455 : overbeek 1.1 }
456 :    
457 :     &HTML::show_page($cgi,$html);
458 : golsen 1.91 exit;
459 : overbeek 1.1
460 : redwards 1.108 sub show_initial {
461 :     # a new first page written by Rob
462 :     my($fig,$cgi,$html) = @_;
463 : overbeek 1.1
464 : redwards 1.121 # we get this information here and set things so that when we create the links later everything is already set.
465 : overbeek 1.128 my $sort = $cgi->param('sortby');
466 : redwards 1.121 unless ($sort) {$sort="Classification"}
467 :     my $show_clusters=$cgi->param('show_clusters');
468 :     my $sort_ss=$cgi->param('sort');
469 :     my $minus=$cgi->param('show_minus1');
470 :     my $show_genomes=$cgi->param('showgenomecounts');
471 :    
472 :    
473 :     # now set the values into $cgi so that we have them for later
474 :     $cgi->param('sortby', $sort); # this is the table sort
475 :     $cgi->param('show_clusters', $show_clusters); # whether or not to show the clusters
476 :     $cgi->param('sort', $sort_ss); # this is the sort of the organisms in display
477 :     $cgi->param('show_minus1', $minus); # whether to show -1 variants
478 :     $cgi->param('showgenomecounts', $show_genomes); # whether to show genomes on the first page
479 : redwards 1.108
480 :     my @ssa = map {
481 :     my $ss=$_;
482 :     my ($version, $curator, $pedigree, $roles)=$fig->subsystem_info($ss->[0]);
483 : overbeek 1.112 push @$ss, scalar(@$roles), $version;
484 :     push @$ss, scalar(@{$fig->subsystem_genomes($ss->[0])}) if ($cgi->param('showgenomecounts'));
485 : redwards 1.114 $fig->subsystem_classification($ss->[0], [$cgi->param($ss->[0].".class1"), $cgi->param($ss->[0].".class2")]) if ($cgi->param($ss->[0].".class1"));
486 : redwards 1.108 unshift @$ss, @{$fig->subsystem_classification($ss->[0])};
487 :     if ($ss->[3] eq $user) {$ss->[3] = [$ss->[3], "td style='background-color: #BA55D3'"]}
488 :     $_=$ss;
489 :     }
490 :     &existing_subsystem_annotations($fig);
491 :    
492 :     # sort the cells
493 : overbeek 1.162 if ($sort eq "Classification") {@ssa=sort {uc($a->[0]) cmp uc($b->[0]) || uc($a->[1]) cmp uc($b->[1]) || uc($a->[2]) cmp uc($b->[2])} @ssa}
494 :     elsif ($sort eq "Subsystem") {@ssa=sort {uc($a->[2]) cmp uc($b->[2])} @ssa}
495 :     elsif ($sort eq "Curator") {@ssa=sort {uc($a->[3]) cmp uc($b->[3])} @ssa}
496 : redwards 1.108 elsif ($sort eq "Number of Roles") {@ssa=sort {$a->[4] <=> $b->[4]} @ssa}
497 :     elsif ($sort eq "Version") {@ssa=sort {$a->[5] <=> $b->[5]} @ssa}
498 : redwards 1.114
499 :     ##### Add the ability to change empty classifications
500 :    
501 :     # get the complete list of classifications
502 :     my %class1=(""=>1); my %class2=(""=>1);
503 :     map {$class1{$_->[0]}++; $class2{$_->[1]}++} @ssa;
504 :    
505 : redwards 1.121
506 :     # replace empty classifications with the popup_menus and create links
507 : overbeek 1.117 # Disabled this because it is causing the page to load _very_ slowly as the browser has to render all the menus
508 :     # two alternatives: put only a popup for the first field if both are empty and then a popup for the second if neither are empty
509 :     # or put textfields to allow people to cut/paste.
510 :    
511 : overbeek 1.119 map {
512 :     my $ss=$_;
513 :     unless (1 || $ss->[0]) # remove the '1 ||' from this line to reinstate the menus
514 :     {
515 :     $ss->[0] = $cgi->popup_menu(-name=>$ss->[2].".class1", -values=>[sort {$a cmp $b} keys %class1]);
516 :     $ss->[1] = $cgi->popup_menu(-name=>$ss->[2].".class2", -values=>[sort {$a cmp $b} keys %class2]);
517 :     }
518 :     $ss->[2]=&ssa_link($fig, $ss->[2], $user);
519 :     $_=$ss;
520 :     } @ssa;
521 : redwards 1.114
522 : overbeek 1.112 my $col_hdrs=[["Classification", "th colspan=2 style='text-align: center'"], "Subsystem", "Curator", "Number of Roles", "Version"];
523 :     push @$col_hdrs, "Number of Genomes" if ($cgi->param('showgenomecounts'));
524 :    
525 : redwards 1.108 my $tab=HTML->merge_table_rows(\@ssa);
526 :     my $url = &FIG::cgi_url . "/subsys.cgi?user=$user&request=manage_ss";
527 :     my $target = "window$$";
528 :    
529 : overbeek 1.112 my %sortmenu=(
530 : parrello 1.127 unsorted=>"None",
531 :     alphabetic=>"Alphabetical",
532 :     by_pattern=>"Patterns",
533 :     by_phylo=>"Phylogeny",
534 :     by_tax_id=>"Taxonomy",
535 :     by_variant=>"Variant Code",
536 : overbeek 1.112 );
537 : mkubal 1.165
538 : redwards 1.108 push(@$html,
539 : overbeek 1.125 $cgi->start_form(-action => "subsys.cgi"),
540 : redwards 1.108 "<div class='ssinstructions'>\n",
541 :     "Please choose one of the subsystems from this list, or begin working on your own by entering a name in the box at the bottom of the page. ",
542 :     "We suggest that you take some time to look at the subsystems others have developed before working on your own.",
543 :     "<ul><li>Please do not ever edit someone else's spreadsheet</li>\n<li>Please do not open multiple windows to process the same spreadsheet.</li>",
544 :     "<li>Feel free to open a subsystem spreadsheet and then open multiple other SEED windows to access data and modify annotations.</li>",
545 :     "<li>You can access someone else's subsystem spreadsheet using your ID</li>",
546 : redwards 1.114 "<li>To change the classification of an unclassified subsystem, choose the desired classification from the menus and click Update Table View</li>");
547 :    
548 :     push @$html, "<li>You can <a href='$url&manage=mine'>manage your subsystems</a></li>" if ($user);
549 :     push(@$html,
550 : redwards 1.108 "<li>You can <a href='$url'>manage all subsystems</a></li>",
551 :     "</ul></div>",
552 :     "<div class='page_settings' style='width: 75%; margin-left: auto; margin-right: auto'>Please enter your username: ", $cgi->textfield(-name=>"user"), "\n",
553 :     "<table border=1>\n",
554 :     "<tr><th>Settings for this page</th><th>Settings for the links to the next page.<br>Change these and click Update Table View.</th></tr>\n",
555 :     "<tr><td>",
556 : parrello 1.127 "<table><tr>",
557 :     "<td valign=center>Sort table by</td><td valign=center>",
558 :     $cgi->popup_menu(-name=>'sortby', -values=>['Classification', 'Subsystem', 'Curator', 'Number of Roles', 'Version'], -default=>$sort), "</td></tr></table\n",
559 : redwards 1.108 "</td>\n<td>",
560 :     "<table><tr>",
561 : overbeek 1.161 "<td valign=center>Show clusters</td><td valign=center>", $cgi->checkbox(-name=>'show_clusters', -label=>''), "</td>\n",
562 : parrello 1.127 "<td valign=center>Default Spreadsheet Sorted By:</td><td valign=center>",
563 : overbeek 1.128 $cgi->popup_menu(-name => 'sort', -values => [keys %sortmenu], -labels=>\%sortmenu),
564 : parrello 1.127 "</td></tr></table>\n",
565 : redwards 1.108 "</td></tr></table>\n",
566 :     $cgi->submit('Update Table View'), $cgi->reset, $cgi->p,
567 :     "</div>\n",
568 :     &HTML::make_table($col_hdrs,$tab,"Subsystems"),
569 :     $cgi->end_form(),
570 :    
571 :    
572 :     # $cgi->h3('To start a new subsystem'), $cgi->p("Please enter the name of the subsystem that you would like to start. You will be provided with a blank",
573 :     # " form that you can fill in with the roles and genomes to create a subsystem like those above."),
574 :     # $cgi->start_form(-action => "subsys.cgi",
575 : parrello 1.127 # -target => $target,
576 :     # -method => 'post'),
577 : redwards 1.108 # $cgi->hidden(-name => 'user', -value => $user, -override => 1),
578 :     # $cgi->hidden(-name => 'request', -value => 'new_ssa', -override => 1),
579 :     # "Name of New Subsystem: ",
580 :     # $cgi->textfield(-name => "ssa_name", -size => 50),
581 :     # $cgi->hidden(-name => 'can_alter', -value => 1, -override => 1),
582 :     # $cgi->br,
583 :     #
584 :     # $cgi->submit('start new subsystem'),
585 :     );
586 : redwards 1.121
587 : redwards 1.108 }
588 :    
589 : mkubal 1.172 #sub make_link_to_painted_diagram{
590 :     # my($fig, $cgi, $html ) = @_;
591 :     # my $new_html = [];
592 :     # push(@$new_html,"<br><br>");
593 :     # push(@$new_html,"<a href='$FIG_Config::temp_url/painted_diagram.html'>data painted on diagram</a>");
594 :     # push(@$new_html,"<br>");
595 :     # &HTML::show_page($cgi,$new_html);
596 :     # exit;
597 :     #}
598 :    
599 : mkubal 1.165 sub make_link_to_painted_diagram{
600 : mkubal 1.156 my($fig, $cgi, $html ) = @_;
601 : mkubal 1.172 my $script = "<script>
602 :     window.open('$FIG_Config::temp_url/painted_diagram.html');
603 :     </script>";
604 :     push(@$html,$script);
605 :     &HTML::show_page($cgi,$html);
606 : mkubal 1.156 exit;
607 :     }
608 :    
609 : mkubal 1.165 sub find_roles_to_color
610 :     {
611 :     my ($fig,$cgi,$html)=@_;
612 :     my ($genome_id,$key,$value);
613 :    
614 :     if($cgi->param('att_data_genome_id')){$genome_id = $cgi->param('att_data_genome_id');}
615 :    
616 :     if($cgi->param('color_diagram_by_peg_tag')){$key = $cgi->param('color_diagram_by_peg_tag');}
617 :    
618 :     if($cgi->param('value_to_color')){$value = $cgi->param('value_to_color');}
619 : mkubal 1.168
620 :     my @results;
621 :     if($value eq "all"){
622 :     @results = $fig->get_attributes(undef,$key,undef);
623 :     }
624 :     else{
625 :     @results = $fig->get_attributes(undef,$key,$value);
626 :     }
627 : mkubal 1.165
628 : mkubal 1.168 my (@pegs,%roles,%p2v);
629 : mkubal 1.165 foreach my $result (@results){
630 :     my($p,$a,$v,$l)= @$result;
631 :     if($p =~/$genome_id/){
632 :     push(@pegs,$p);
633 : mkubal 1.168 $p2v{$p} = $v;
634 : mkubal 1.165 }
635 :     }
636 :    
637 :     foreach my $peg (@pegs){
638 : mkubal 1.168 my $value = $p2v{$peg};
639 : mkubal 1.165 my $function = $fig->function_of($peg);
640 :     my @function_roles = $fig->roles_of_function($function);
641 : mkubal 1.168 foreach my $fr (@function_roles){$roles{$fr} = $value;}
642 : mkubal 1.165 }
643 :    
644 : mkubal 1.168 return \%roles;
645 : mkubal 1.165 }
646 :    
647 :     sub color_diagram_role_by_av
648 :     {
649 :    
650 : mkubal 1.166 my ($fig,$cgi,$ss_name,$ss_obj,$roles,$diagram_name)=@_;
651 : mkubal 1.165 my $dir = "$FIG_Config::temp_url";
652 :     my $genome_id = $cgi->param('att_data_genome_id');
653 :     my $attribute=$cgi->param('color_diagram_by_peg_tag');
654 : mkubal 1.166 my $diagram_id = "d01";
655 : mkubal 1.165
656 : mkubal 1.166 my @all_diagrams = $ss_obj->get_diagrams();
657 :     foreach my $ad (@all_diagrams){
658 :     if ($diagram_name eq @$ad[1]){
659 :     $diagram_id = @$ad[0];
660 :     }
661 :     }
662 :    
663 :     my $diagram_html_file = $ss_obj->get_diagram_html_file($diagram_id);
664 : mkubal 1.165 open(IN2, $diagram_html_file);
665 :     open(OUT2,">$FIG_Config::temp/painted_diagram.html");
666 :    
667 :     my %role_to_abbr;
668 :     my @r_and_abbr = $ss_obj->roles_with_abbreviations();
669 :     foreach my $r (@r_and_abbr){
670 :     $role_to_abbr{@$r[1]} = @$r[0];
671 :     }
672 :    
673 :     my %abbr_to_coords;
674 :     while ($_ = <IN2>){
675 :     chomp($_);
676 :     my @temp = split("<AREA SHAPE",$_);
677 :     foreach my $t (@temp){
678 :     if( $t =~/COORDS=\"(\d+,\d+,\d+,\d+)\".*Role=\"(\w+)\"/){
679 :     $abbr_to_coords{$2} = $1;
680 :     }
681 :     }
682 :     }
683 :    
684 : mkubal 1.169 print OUT2 qq(<html><head><title>Painted Diagram</title><link rel='stylesheet' title='default' href='../FIG/Html/css/default.css' type='text/css'>
685 :     <link rel='alternate stylesheet' title='Sans Serif' href='../FIG/Html/css/sanserif.css' type='text/css'>
686 :     <link rel='alternate' title='SEED RSS feeds' href='../FIG/Html/rss/SEED.rss' type='application/rss+xml'>
687 :     <script src="../FIG/Html/css/FIG.js" type="text/javascript"></script></HEAD>
688 :     <script src="../FIG/Html/css/coloring.js" type="text/javascript"></script>);
689 :    
690 :     print OUT2 qq(<style type="text/css">
691 :     .colored {
692 :     background-repeat:repeat;
693 :     border: 0;
694 :     border-style: solid;
695 :     margin: 0;
696 :     border: 0;
697 :     font-size: 8pt;
698 :     }
699 :     .colored[class] {
700 :     background-image: url(../FIG/Html/diagram_overlay.png);
701 :     }
702 :    
703 :     .coloredRed {
704 :     background-repeat:repeat;
705 :     border: 0;
706 :     border-style: solid;
707 :     margin: 0;
708 :     border: 0;
709 :     font-size: 8pt;
710 :     }
711 :     .coloredRed[class] {
712 :     background-image: url(../FIG/Html/diagram_overlay_red.png);
713 :     }
714 :    
715 :     .coloredBlue {
716 :     background-repeat:repeat;
717 :     border: 0;
718 :     border-style: solid;
719 :     margin: 0;
720 :     border: 0;
721 :     font-size: 8pt;
722 :     }
723 :     .coloredBlue[class] {
724 :     background-image: url(../FIG/Html/diagram_overlay_blue.png);
725 :     }
726 :    
727 :     .coloredGreen {
728 :     background-repeat:repeat;
729 :     border: 0;
730 :     border-style: solid;
731 :     margin: 0;
732 :     border: 0;
733 :     font-size: 8pt;
734 :     }
735 :     .coloredGreen[class] {
736 :     background-image: url(../FIG/Html/diagram_overlay_green.png);
737 :     }
738 :    
739 :     .coloredGray {
740 :     background-repeat:repeat;
741 :     border: 0;
742 :     border-style: solid;
743 :     margin: 0;
744 :     border: 0;
745 :     font-size: 8pt;
746 :     }
747 :     .coloredGray[class] {
748 :     background-image: url(../FIG/Html/diagram_overlay_gray.png);
749 :     }
750 :    
751 :     .xcolored {
752 :     background-color: red
753 :     }
754 :    
755 :     .transparent {
756 :     background-color: transparent
757 :     }
758 :     </style>);
759 :    
760 :     print OUT2 qq(<body onload="onBodyLoad()">);
761 :     print OUT2 qq(<div id="map_div" style="position:relative; left:0px; top:0px;"><MAP NAME="painted_diagram">);
762 : mkubal 1.168
763 : mkubal 1.165 #iterate through roles passed in to subroutine for consideration
764 : mkubal 1.169 my(@RedRoles,@BlueRoles,@GrayRoles,@GreenRoles);
765 : mkubal 1.168 foreach my $role (keys(%$roles)){
766 :     my %temp_hash = %$roles;
767 : mkubal 1.165 if($role_to_abbr{$role}){
768 :     my $abbr =$role_to_abbr{$role};
769 :     if($abbr_to_coords{$abbr}){
770 : mkubal 1.169 my $temp = $abbr_to_coords{$abbr};
771 :     my @coords = split(",",$temp);
772 :     my $x1 = $coords[0];
773 :     my $y1 = $coords[1];
774 :     my $x2 = $coords[2];
775 :     my $y2 = $coords[3];
776 :     print OUT2 qq(<AREA SHAPE="rect" COORDS="$x1,$y1,$x2,$y2" NOHREF Role="$abbr">);
777 : mkubal 1.168 my $value = $temp_hash{$role};
778 : mkubal 1.171 if($value eq "essential"){
779 :     $abbr = "'".$abbr."'";
780 : mkubal 1.169 push(@RedRoles,$abbr);
781 : mkubal 1.168 }
782 : mkubal 1.170 elsif($value eq "nonessential"){
783 :     $abbr = "'".$abbr."'";
784 : mkubal 1.169 push(@BlueRoles,$abbr);
785 : mkubal 1.168 }
786 : mkubal 1.171 elsif($value eq "undetermined"){
787 :     $abbr = "'".$abbr."'";
788 : mkubal 1.169 push(@GrayRoles,$abbr);
789 : mkubal 1.168 }
790 : mkubal 1.171 else{
791 : mkubal 1.172 $abbr = qq("$abbr");
792 : mkubal 1.169 push(@GreenRoles,$abbr);
793 : mkubal 1.168 }
794 :     }
795 :     }
796 : mkubal 1.165 }
797 : mkubal 1.168
798 : mkubal 1.169 system `cp $FIG_Config::data/Subsystems/$ss_name/diagrams/$diagram_id/diagram.jpg $FIG_Config::temp/painted_diagram.jpg`;
799 :     print OUT2 qq(</MAP><img border="0" src="$FIG_Config::temp_url/painted_diagram.jpg" usemap="#painted_diagram"></div>);
800 :    
801 : mkubal 1.170 my $BlueRolesString = join(",",@BlueRoles);
802 : mkubal 1.171 my $GreenRolesString = join(",",@GreenRoles);
803 :     my $RedRolesString = join(",",@RedRoles);
804 :     my $GrayRolesString = join(",",@GrayRoles);
805 : mkubal 1.172
806 : mkubal 1.169 print OUT2 qq(<script language="JavaScript">
807 :     function onBodyLoad()
808 :     {
809 : mkubal 1.172 var rolesToColorGreen = new Array($GreenRolesString);
810 :     var rolesToColorRed = new Array($RedRolesString);
811 :     var rolesToColorBlue = new Array($BlueRolesString);
812 :     var rolesToColorGray = new Array($GrayRolesString);
813 :     colorEngine = new ActiveDiagram("map_div");
814 : mkubal 1.169 colorEngine.load();
815 : mkubal 1.172 colorEngine.colorRedRoles(rolesToColorRed);
816 :     colorEngine.colorBlueRoles(rolesToColorBlue);
817 :     colorEngine.colorGrayRoles(rolesToColorGray);
818 :     colorEngine.colorGreenRoles(rolesToColorGreen);
819 : mkubal 1.169 }
820 :     </script>);
821 :    
822 : mkubal 1.165 print OUT2 "</BODY></HTML>";
823 :     }
824 :    
825 : mkubal 1.156 sub paint_ma_data
826 :     {
827 :    
828 :     my ($fig,$cgi,$ss_name,$ss_obj)=@_;
829 :     my @inputs;
830 :     my $dir = "$FIG_Config::temp_url";
831 :     my $genome_id = $cgi->param('ma_data_genome_id');
832 : mkubal 1.165
833 : mkubal 1.156 my %peg_to_level;
834 : mkubal 1.165 my $ma_data = 0;
835 :    
836 : mkubal 1.156 if ($cgi->upload('ma_data_file'))
837 :     {
838 : mkubal 1.165 my $fh=$cgi->upload('ma_data_file');
839 :     @inputs = <$fh> ;
840 :     $ma_data = 1;
841 :    
842 :     foreach my $i (@inputs){
843 :     chomp($i);
844 :     my @temp = split("\t",$i);
845 :     $peg_to_level{$temp[0]} = $temp[1];
846 :     }
847 : mkubal 1.156 }
848 : mkubal 1.165
849 : mkubal 1.156 my $diagram_html_file = $ss_obj->get_diagram_html_file("d01");
850 :     open(IN2, $diagram_html_file);
851 : mkubal 1.165 open(OUT2,">$FIG_Config::temp/painted_diagram.html");
852 : redwards 1.108
853 : mkubal 1.156 my %role_to_coords;
854 :    
855 :     while ($_ = <IN2>){
856 :     chomp($_);
857 :     my @temp = split("<AREA SHAPE",$_);
858 :     foreach my $t (@temp){
859 :    
860 :     if( $t =~/COORDS=\"(\d+,\d+,\d+,\d+)\".*Role=\"(\w+)\"/){
861 :     $role_to_coords{$2} = $1;
862 :     }
863 :     }
864 :     }
865 : redwards 1.108
866 : mkubal 1.156 print OUT2 "<HTML><HEAD>
867 :     <TITLE>microarray data painted on subsystem diagram</TITLE>
868 : mkubal 1.165 </HEAD>";
869 :    
870 :     print OUT2 "<BODY><MAP NAME='painted_diagram'>";
871 :    
872 : mkubal 1.156 my @roles = keys(%role_to_coords);
873 :     my $color;
874 :     foreach my $role (@roles){
875 : mkubal 1.165 my $temp = $role_to_coords{$role};
876 :     my @coords = split(",",$temp);
877 : mkubal 1.156 my @pegs = $ss_obj->get_pegs_from_cell($genome_id,$role);
878 :     foreach my $peg (@pegs){
879 :     my $temp = $role_to_coords{$role};
880 :     my @coords = split(",",$temp);
881 : mkubal 1.178 my $top = $coords[1] - 35;
882 : mkubal 1.165 #my $top = $coords[0];
883 : mkubal 1.178 my $left = $coords[0] + 15;
884 : mkubal 1.165 #my $left = $coords[1];
885 :     if($ma_data){
886 :     my $tag = $peg_to_level{$peg};
887 : mkubal 1.178 if($tag < -.99){$color ="#009900" }
888 :     elsif($tag < 1){$color ="#FF0099" }
889 :     #elsif($tag < .50){$color ="#00FF00" }
890 :     #elsif($tag < 2){$color ="#CCFF00" }
891 :     #elsif($tag < 20){$color ="#FF00FF" }
892 :     #elsif($tag < 40){$color ="#FF00CC" }
893 :     #elsif($tag < 80){$color ="#FF0066" }
894 : mkubal 1.165 elsif($tag < 100){$color ="#FF0033" }
895 :     else{$color ="#FF0000" }
896 :     print OUT2 "<h5 STYLE='position: absolute; top:$top; left:$left'><font Color='$color'>$tag</font></h5>\n";
897 :     }
898 :     # else{
899 :     # my @rets = $fig->get_attributes($peg,$attribute);
900 :     # foreach my $ret (@rets){
901 :     # my($p,$t,$value,$l) = @$ret;
902 :     # #print STDERR "value:$value\n";
903 :     # print OUT2 "<h5 STYLE='position: absolute; top:$top; left:$left'><font Color='$color'>$value</font></h5>\n";
904 :     # }
905 :     # }
906 :     }
907 : mkubal 1.156 }
908 : mkubal 1.165
909 :     my $jpg_file = "$FIG_Config::data/Subsystems/$ss_name/diagrams/d01/diagram.jpg";
910 :     system "cp $jpg_file $FIG_Config::temp/painted_diagram.jpg";
911 : mkubal 1.156
912 : mkubal 1.165 my $width;
913 :     my $height;
914 :    
915 :     if($cgi->param('image_file_width')){
916 :     $width = $cgi->param('image_file_width');
917 :     $height = $cgi->param('image_file_height');
918 :     }
919 : mkubal 1.156
920 : mkubal 1.165 print OUT2 "</MAP><IMG SRC='painted_diagram.jpg' WIDTH='$width' HEIGHT='$height' USEMAP='#painted_diagram' BORDER='0'></BODY></HTML>";
921 : mkubal 1.156 }
922 : redwards 1.108
923 :     sub manage_subsystems {
924 : overbeek 1.1 my($fig,$cgi,$html) = @_;
925 :     my($set,$when,$comment);
926 :    
927 : redwards 1.108 my $ss_to_manage=$cgi->param('manage'); # we will only display a subset of subsystems on the old SS page
928 :     if ($ss_to_manage eq "mine") {$ss_to_manage=$user}
929 :    
930 : overbeek 1.51 my @ssa = &existing_subsystem_annotations($fig);
931 : overbeek 1.112 # RAE comment out the next line to hide selection
932 :     $ss_to_manage && (@ssa=grep {$_->[1] eq $ss_to_manage} @ssa); # limit the set if we want to
933 : overbeek 1.1
934 :     if (@ssa > 0)
935 :     {
936 : parrello 1.127 &format_ssa_table($cgi,$html,$user,\@ssa);
937 : overbeek 1.1 }
938 :    
939 :     my $target = "window$$";
940 :     push(@$html, $cgi->h1('To Start or Copy a Subsystem'),
941 :     $cgi->start_form(-action => "subsys.cgi",
942 : parrello 1.127 -target => $target,
943 :     -method => 'post'),
944 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
945 :     $cgi->hidden(-name => 'request', -value => 'new_ssa', -override => 1),
946 :     "Name of New Subsystem: ",
947 :     $cgi->textfield(-name => "ssa_name", -size => 50),
948 :     $cgi->hidden(-name => 'can_alter', -value => 1, -override => 1),
949 :     $cgi->br,
950 :    
951 :     "Copy from (leave blank to start from scratch): ",
952 :     $cgi->textfield(-name => "copy_from1", -size => 50),
953 :     $cgi->br,
954 :    
955 :     "Copy from (leave blank to start from scratch): ",
956 :     $cgi->textfield(-name => "copy_from2", -size => 50),
957 :     $cgi->br,
958 :    
959 :     "Rename an existing subsystem: ",
960 :     $cgi->textfield(-name => "move_from", -size => 50),
961 :     $cgi->br,
962 :    
963 :     $cgi->submit('start new subsystem'),
964 :     $cgi->end_form,
965 :     "<br>You can start a subsystem from scratch, in which case you should leave these two \"copy from\"
966 : overbeek 1.1 fields blank. If you wish to just copy a subsystem (in order to become the owner so that you can modify it),
967 :     just fill in one of the \"copy from\" fields with the name of the subsystem you wish to copy. If you wish to
968 :     extract a a subset of the columns to build a smaller spreadsheet (which could later be merged with another one),
969 :     fill in the name of the subsystem. You will be prompted for the columns that you wish to extract (choose <i>all</i> to
970 :     just copy all of the columns). Finally, if you wish to build a new spreadsheet by including columns from two existing
971 :     spreadsheets (including a complete merger), fill in the names of both the existing \"copy from\" subsystems"
972 : parrello 1.127 );
973 :     }
974 : overbeek 1.1
975 :     sub new_ssa {
976 :     my($fig,$cgi,$html) = @_;
977 :    
978 : redwards 1.82 my $name = $fig->clean_spaces($cgi->param('ssa_name')); # RAE remove extraneous spaces in the name
979 : overbeek 1.1
980 :     if (! $user)
981 :     {
982 : parrello 1.127 push(@$html,$cgi->h1('You need to specify a user before starting a new subsystem annotation'));
983 :     return;
984 : overbeek 1.1 }
985 :    
986 :     if (! $name)
987 :     {
988 : parrello 1.127 push(@$html,$cgi->h1("You need to specify a subsystem name, $name is not valid"));
989 :     return;
990 : overbeek 1.1 }
991 :    
992 :     my $ssa = $name;
993 :     $ssa =~ s/[ \/]/_/g;
994 :    
995 :     &FIG::verify_dir("$FIG_Config::data/Subsystems");
996 :    
997 :     if (-d "$FIG_Config::data/Subsystems/$ssa")
998 :     {
999 : parrello 1.127 push(@$html,$cgi->h1("You need to specify a new subsystem name; $ssa already is being used"));
1000 :     return;
1001 : overbeek 1.1 }
1002 :    
1003 :     my $subsystem = new Subsystem($ssa,$fig,1); # create new subsystem
1004 :    
1005 :     my $copy_from1 = $cgi->param('copy_from1');
1006 :     $copy_from1 =~ s/[ \/]/_/g;
1007 :     my $copy_from2 = $cgi->param('copy_from2');
1008 :     $copy_from2 =~ s/[ \/]/_/g;
1009 :     my @cols_to_take1 = $cgi->param('cols_to_take1');
1010 :     my @cols_to_take2 = $cgi->param('cols_to_take2');
1011 :    
1012 :    
1013 :     if ($copy_from1 && (@cols_to_take1 > 0))
1014 :     {
1015 : parrello 1.127 $subsystem->add_to_subsystem($copy_from1,\@cols_to_take1,"take notes"); # add columns and notes
1016 : overbeek 1.1 }
1017 :    
1018 :     if ($copy_from2 && (@cols_to_take2 > 0))
1019 :     {
1020 : parrello 1.127 $subsystem->add_to_subsystem($copy_from2,\@cols_to_take2,"take notes"); # add columns and notes
1021 : overbeek 1.1 }
1022 :    
1023 : olson 1.120 $subsystem->db_sync();
1024 : overbeek 1.1 $subsystem->write_subsystem();
1025 :    
1026 : redwards 1.82 $cgi->param(-name => "ssa_name",
1027 : parrello 1.127 -value => $ssa); # RAE this line was needed because otherwise a newly created subsystem was not opened!
1028 : redwards 1.82 $cgi->param(-name => "can_alter",
1029 : parrello 1.127 -value => 1);
1030 : overbeek 1.1 &one_cycle($fig,$cgi,$html);
1031 :     }
1032 :    
1033 :     # The basic update logic (cycle) includes the following steps:
1034 :     #
1035 :     # 1. Load the existing spreadsheet
1036 :     # 2. reconcile row and subset changes
1037 : overbeek 1.9 # 3. process spreadsheet changes (fill/refill/add genomes/update variants)
1038 : overbeek 1.1 # 4. write the updated spreadsheet back to disk
1039 :     # 5. render the spreadsheet
1040 :     #
1041 :     sub one_cycle {
1042 :     my($fig,$cgi,$html) = @_;
1043 : overbeek 1.57 my $subsystem;
1044 : overbeek 1.1
1045 :     my $ssa = $cgi->param('ssa_name');
1046 :    
1047 : overbeek 1.57 if ((! $ssa) || (! ($subsystem = new Subsystem($ssa,$fig,0))))
1048 : overbeek 1.1 {
1049 : parrello 1.127 push(@$html,$cgi->h1('You need to specify a subsystem'));
1050 :     return;
1051 : overbeek 1.1 }
1052 : olson 1.109
1053 :     #
1054 :     # Initialize can_alter if it is not set.
1055 :     #
1056 :    
1057 :     my $can_alter = $cgi->param("can_alter");
1058 :     if (!defined($can_alter))
1059 :     {
1060 : parrello 1.127 if ($user and ($user eq $subsystem->get_curator))
1061 :     {
1062 :     $can_alter = 1;
1063 :     $cgi->param(-name => 'can_alter', -value => 1);
1064 :     }
1065 : olson 1.109 }
1066 : overbeek 1.115
1067 :     #
1068 :     # If we're not the curator, force the active subsets to All.
1069 :     #
1070 :    
1071 :     if (not $can_alter)
1072 :     {
1073 : parrello 1.127 $subsystem->set_active_subsetC("All");
1074 :     $subsystem->set_active_subsetR("All");
1075 : overbeek 1.115 }
1076 : olson 1.104
1077 : redwards 1.121 if ($cgi->param('can_alter') && $user && ($user eq $subsystem->get_curator))
1078 : olson 1.104 {
1079 : parrello 1.127 handle_diagram_changes($fig, $subsystem, $cgi, $html);
1080 : olson 1.104 }
1081 : overbeek 1.1
1082 :     if (&handle_role_and_subset_changes($fig,$subsystem,$cgi,$html))
1083 :     {
1084 : parrello 1.127 &process_spreadsheet_changes($fig,$subsystem,$cgi,$html);
1085 : overbeek 1.10
1086 : parrello 1.127 if ($cgi->param('can_alter') && $user && ($user eq $subsystem->get_curator))
1087 :     {
1088 :     $subsystem->write_subsystem();
1089 :     # RAE: Adding a call to HTML.pm to write the changes to the RSS feed. Not 100% sure we want to do this
1090 :     # everytime we write a SS, but we'll see
1091 :    
1092 :     # note in the RSS we want a barebones link because anyone can access it.
1093 :     my $esc_ssa=uri_escape($ssa);
1094 :     my $url = &FIG::cgi_url . "/subsys.cgi?user=&ssa_name=$esc_ssa&request=show_ssa";
1095 :    
1096 :     &HTML::rss_feed(
1097 :     ["SEEDsubsystems.rss"],
1098 :     {
1099 :     "title" => "Updated $ssa",
1100 :     "description" => "$ssa was updated with some changes, and saved",
1101 :     "link" => $url,
1102 :     });
1103 :     }
1104 :    
1105 :     my $col;
1106 :     if ($cgi->param('show_sequences_in_column') &&
1107 :     ($col = $cgi->param('col_to_align')) &&
1108 :     ($col =~ /^\s*(\d+)\s*$/))
1109 :     {
1110 :     &show_sequences_in_column($fig,$cgi,$html,$subsystem,$col);
1111 :     }
1112 :     else
1113 :     {
1114 :     if ($cgi->param('align_column') &&
1115 :     ($col = $cgi->param('col_to_align')) && ($col =~ /^\s*(\d+)\s*$/))
1116 :     {
1117 :     my $col = $1;
1118 :     &align_column($fig,$cgi,$html,$col,$subsystem);
1119 :     $cgi->delete('col_to_align');
1120 :     }
1121 :     elsif ($cgi->param('realign_column') &&
1122 :     ($col = $cgi->param('subcol_to_realign')) && ($col =~ /^\s*(\d+)\.(\d+)\s*$/))
1123 :     {
1124 :     &align_subcolumn($fig,$cgi,$html,$1,$2,$subsystem);
1125 :     $cgi->delete('subcol_to_realign');
1126 :     }
1127 :     &produce_html_to_display_subsystem($fig,$subsystem,$cgi,$html,$ssa);
1128 :     }
1129 : overbeek 1.1 }
1130 :     }
1131 :    
1132 :     sub handle_role_and_subset_changes {
1133 :     my($fig,$subsystem,$cgi,$html) = @_;
1134 :    
1135 : redwards 1.121 if ((! $cgi->param('can_alter')) || (!$user) || ($user ne $subsystem->get_curator))
1136 : overbeek 1.1 {
1137 : parrello 1.127 return 1; # no changes, so...
1138 : overbeek 1.1 }
1139 :     else
1140 :     {
1141 : parrello 1.127 my @roles = $subsystem->get_roles;
1142 :     my($rparm,$vparm);
1143 :     foreach $rparm (grep { $_ =~ /^react\d+/ } $cgi->param)
1144 :     {
1145 :     if ($vparm = $cgi->param($rparm))
1146 :     {
1147 :     $vparm =~ s/ //g;
1148 :     $rparm =~ /^react(\d+)/;
1149 :     my $roleN = $1 - 1;
1150 :     $subsystem->set_reaction($roles[$roleN],$vparm);
1151 :     }
1152 :     }
1153 : overbeek 1.97
1154 : parrello 1.127 my($role,$p,$abr,$r,$n);
1155 :     my @tuplesR = ();
1156 : overbeek 1.97
1157 : overbeek 1.182 ### NOTE: the meaning (order) of @roles shifts here to the NEW order
1158 : parrello 1.127 @roles = grep { $_ =~ /^role/ } $cgi->param();
1159 :     if (@roles == 0) { return 1 } # initial call, everything is as it was
1160 :    
1161 :     foreach $role (@roles)
1162 :     {
1163 :     if (($role =~ /^role(\d+)/) && defined($n = $1))
1164 :     {
1165 :     if ($r = $cgi->param("role$n"))
1166 :     {
1167 :     $r =~ s/^\s+//;
1168 :     $r =~ s/\s+$//;
1169 :    
1170 :     if (($p = $cgi->param("posR$n")) && ($abr = $cgi->param("abbrev$n")))
1171 :     {
1172 : overbeek 1.182 push(@tuplesR,[$p,$r,$abr,$n]);
1173 : parrello 1.127 }
1174 :     else
1175 :     {
1176 :     push(@$html,$cgi->h1("You need to give a position and abbreviation for $r"));
1177 :     return 0;
1178 :     }
1179 :     }
1180 :     }
1181 :     }
1182 :     @tuplesR = sort { $a->[0] <=> $b->[0] } @tuplesR;
1183 : overbeek 1.182
1184 : parrello 1.127 $subsystem->set_roles([map { [$_->[1],$_->[2]] } @tuplesR]);
1185 :    
1186 :     my($subset_name,$s,$test,$entries,$entry);
1187 :     my @subset_names = grep { $_ =~ /^nameCS/ } $cgi->param();
1188 :    
1189 :     if (@subset_names == 0) { return 1 }
1190 :    
1191 :     my %defined_subsetsC;
1192 :     foreach $s (@subset_names)
1193 :     {
1194 :     if (($s =~ /^nameCS(\d+)/) && defined($n = $1) && ($subset_name = $cgi->param($s)))
1195 :     {
1196 :    
1197 :     my($text);
1198 :     $entries = [];
1199 :     if ($text = $cgi->param("subsetC$n"))
1200 :     {
1201 :     foreach $entry (split(/[\s,]+/,$text))
1202 :     {
1203 :     if ($role = &to_role($entry,\@tuplesR))
1204 :     {
1205 :     push(@$entries,$role);
1206 :     }
1207 :     else
1208 :     {
1209 :     push(@$html,$cgi->h1("Invalid role designation in subset $s: $entry"));
1210 :     return 0;
1211 :     }
1212 :     }
1213 :     }
1214 :     $defined_subsetsC{$subset_name} = $entries;
1215 :     }
1216 :     }
1217 :    
1218 :     foreach $s ($subsystem->get_subset_namesC)
1219 :     {
1220 :     next if ($s eq "All");
1221 :     if ($entries = $defined_subsetsC{$s})
1222 :     {
1223 :     $subsystem->set_subsetC($s,$entries);
1224 :     delete $defined_subsetsC{$s};
1225 :     }
1226 :     else
1227 :     {
1228 :     $subsystem->delete_subsetC($s);
1229 :     }
1230 :     }
1231 : overbeek 1.1
1232 : parrello 1.127 foreach $s (keys(%defined_subsetsC))
1233 :     {
1234 :     $subsystem->set_subsetC($s,$defined_subsetsC{$s});
1235 :     }
1236 :    
1237 :     my $active_subsetC;
1238 :     if ($active_subsetC = $cgi->param('active_subsetC'))
1239 :     {
1240 :     $subsystem->set_active_subsetC($active_subsetC);
1241 :     }
1242 : overbeek 1.1 }
1243 :     return 1;
1244 :     }
1245 :    
1246 :     sub to_role {
1247 :     my($x,$role_tuples) = @_;
1248 :     my $i;
1249 :    
1250 :     for ($i=0; ($i < @$role_tuples) &&
1251 : overbeek 1.182 ($role_tuples->[$i]->[3] != $x) &&
1252 :     ($role_tuples->[$i]->[2] ne $x); $i++) {}
1253 : overbeek 1.1 if ($i < @$role_tuples)
1254 :     {
1255 : parrello 1.127 return $role_tuples->[$i]->[1];
1256 : overbeek 1.1 }
1257 :     return undef;
1258 :     }
1259 :    
1260 :     sub process_spreadsheet_changes {
1261 :     my($fig,$subsystem,$cgi,$html) = @_;
1262 :    
1263 : redwards 1.121 if ((! $cgi->param('can_alter')) || (!$user) || ($user ne $subsystem->get_curator))
1264 : overbeek 1.1 {
1265 : parrello 1.127 return 1; # no changes, so...
1266 : overbeek 1.1 }
1267 :     else
1268 :     {
1269 : parrello 1.127 my $notes = $cgi->param('notes');
1270 :     if ($notes)
1271 :     {
1272 :     $subsystem->set_notes($notes);
1273 :     }
1274 :     if ($cgi->param('classif1t') || $cgi->param('classif2t'))
1275 :     {
1276 :     $subsystem->set_classification([$cgi->param('classif1t'), $cgi->param('classif2t')]);
1277 :     }
1278 :     elsif ($cgi->param('classif1') || $cgi->param('classif2'))
1279 :     {
1280 :     $subsystem->set_classification([$cgi->param('classif1'), $cgi->param('classif2')]);
1281 :     }
1282 :    
1283 :     my(@param,$param,$genome,$val);
1284 :     @param = grep { $_ =~ /^genome\d+\.\d+$/ } $cgi->param;
1285 :    
1286 :     my %removed;
1287 :     foreach $param (@param)
1288 :     {
1289 :     if ($cgi->param($param) =~ /^\s*$/)
1290 :     {
1291 :     $param =~ /^genome(\d+\.\d+)/;
1292 :     $genome = $1;
1293 :     $subsystem->remove_genome($genome);
1294 :     $removed{$genome} = 1;
1295 :     }
1296 :     }
1297 :    
1298 :     @param = grep { $_ =~ /^vcode\d+\.\d+$/ } $cgi->param;
1299 :     foreach $param (@param)
1300 :     {
1301 :     if ($cgi->param($param) =~ /^\s*(\S+)\s*$/)
1302 :     {
1303 :     $val = $1;
1304 :     $param =~ /^vcode(\d+\.\d+)/;
1305 :     $genome = $1;
1306 :     if (! $removed{$genome})
1307 :     {
1308 :     $subsystem->set_variant_code($subsystem->get_genome_index($genome),$val);
1309 :     }
1310 :     }
1311 :     }
1312 :    
1313 :     if ($cgi->param('refill'))
1314 :     {
1315 :     &refill_spreadsheet($fig,$subsystem);
1316 :     }
1317 :     elsif ($cgi->param('precise_fill'))
1318 :     {
1319 :     &fill_empty_cells($fig,$subsystem);
1320 :     }
1321 :    
1322 :     my @orgs = $cgi->param('new_genome');
1323 :     @orgs = map { $_ =~ /\((\d+\.\d+)\)/; $1 } @orgs;
1324 :    
1325 : overbeek 1.136 # RAE: Add organisms to extend with from checkboxes
1326 :     # moregenomes takes either a specifically encoded list like phylogeny, a file that must be present in the organisms dir (e.g. COMPLETE or NMPDR)
1327 :     # or a set of attributes
1328 : overbeek 1.147 if ($cgi->param('moregenomes')) {push @orgs, &moregenomes}
1329 :    
1330 : overbeek 1.136
1331 :     # flatten the list so we don't add more than we need to
1332 :     {
1333 :     my %flatlist=map {($_=>1)} @orgs;
1334 :     @orgs=keys %flatlist;
1335 :     }
1336 :    
1337 : parrello 1.127 my $org;
1338 :     foreach $org (@orgs)
1339 :     {
1340 :     &add_genome($fig,$subsystem,$cgi,$html,$org);
1341 :     }
1342 :    
1343 :     my $active_subsetR;
1344 :     if ($active_subsetR = $cgi->param('active_subsetR'))
1345 :     {
1346 :     $subsystem->set_active_subsetR($active_subsetR);
1347 :     }
1348 : overbeek 1.1 }
1349 :     }
1350 :    
1351 :     sub refill_spreadsheet {
1352 :     my($fig,$subsystem) = @_;
1353 : overbeek 1.5 my($genome,$role,@pegs1,@pegs2,$i);
1354 : overbeek 1.1
1355 :     foreach $genome ($subsystem->get_genomes())
1356 :     {
1357 : parrello 1.127 foreach $role ($subsystem->get_roles())
1358 :     {
1359 :     @pegs1 = sort $subsystem->get_pegs_from_cell($genome,$role);
1360 :     @pegs2 = sort $fig->seqs_with_role($role,"master",$genome);
1361 :    
1362 :     if (@pegs1 != @pegs2)
1363 :     {
1364 :     $subsystem->set_pegs_in_cell($genome,$role,\@pegs2);
1365 :     }
1366 :     else
1367 :     {
1368 :     for ($i=0; ($i < @pegs1) && ($pegs1[$i] eq $pegs2[$i]); $i++) {}
1369 :     if ($i < @pegs1)
1370 :     {
1371 :     $subsystem->set_pegs_in_cell($genome,$role,\@pegs2);
1372 :     }
1373 :     }
1374 :     }
1375 : overbeek 1.1 }
1376 :     }
1377 :    
1378 :     sub fill_empty_cells {
1379 :     my($fig,$subsystem) = @_;
1380 :     my($genome,$role,@pegs);
1381 :    
1382 :     foreach $genome ($subsystem->get_genomes())
1383 :     {
1384 : parrello 1.127 foreach $role ($subsystem->get_roles())
1385 :     {
1386 :     @pegs = $subsystem->get_pegs_from_cell($genome,$role);
1387 :     if (@pegs == 0)
1388 :     {
1389 :     @pegs = $fig->seqs_with_role($role,"master",$genome);
1390 :     if (@pegs > 0)
1391 :     {
1392 :     $subsystem->set_pegs_in_cell($genome,$role,\@pegs);
1393 :     }
1394 :     }
1395 :     }
1396 : overbeek 1.1 }
1397 :     }
1398 :    
1399 :     sub add_genome {
1400 :     my($fig,$subsystem,$cgi,$html,$genome) = @_;
1401 :     my($role,@pegs);
1402 :    
1403 :     $subsystem->add_genome($genome);
1404 :     foreach $role ($subsystem->get_roles())
1405 :     {
1406 : parrello 1.127 @pegs = $fig->seqs_with_role($role,"master",$genome);
1407 :     $subsystem->set_pegs_in_cell($genome,$role,\@pegs);
1408 : overbeek 1.1 }
1409 :     }
1410 :    
1411 :     sub produce_html_to_display_subsystem {
1412 : overbeek 1.20 my($fig,$subsystem,$cgi,$html,$ssa) = @_;
1413 : overbeek 1.1
1414 :     my $ssa = $cgi->param('ssa_name');
1415 : overbeek 1.14 my $can_alter = ($cgi->param('can_alter') && $user && ($user eq $subsystem->get_curator));
1416 : redwards 1.83 my $tagvalcolor; # RAE: this is a reference to a hash that stores the colors of cells by tag. This has to be consistent over the whole table.
1417 : overbeek 1.1
1418 :     my $name = $ssa;
1419 :     $name =~ s/_/ /g;
1420 :     $ssa =~ s/[ \/]/_/g;
1421 : overbeek 1.154 my $curator = &subsystem_curator($ssa);
1422 :    
1423 : overbeek 1.1 push(@$html, $cgi->h1("Subsystem: $name"),
1424 : overbeek 1.157 $cgi->h1("Author: $curator"));
1425 :    
1426 :     my($t,@spreadsheets);
1427 :     if (opendir(BACKUP,"$FIG_Config::data/Subsystems/$ssa/Backup"))
1428 :     {
1429 :     @spreadsheets = sort { $b <=> $a }
1430 :     map { $_ =~ /^spreadsheet.(\d+)/; $1 }
1431 :     grep { $_ =~ /^spreadsheet/ }
1432 :     readdir(BACKUP);
1433 :     closedir(BACKUP);
1434 :     if ($t = shift @spreadsheets)
1435 :     {
1436 :     my $last_modified = &FIG::epoch_to_readable($t);
1437 :     push(@$html, $cgi->h1("Last modified: $last_modified"));
1438 :     }
1439 :     }
1440 :    
1441 : golsen 1.186 if ( $time_it ) ## time ##
1442 :     { ## time ##
1443 :     push @times, scalar time(); ## time ##
1444 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
1445 :     } ## time ##
1446 :    
1447 : overbeek 1.157 push(@$html, $cgi->start_form(-action => "subsys.cgi",
1448 : parrello 1.127 -method => 'post',
1449 :     -enctype => &CGI::MULTIPART),
1450 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
1451 :     $cgi->hidden(-name => 'request', -value => 'show_ssa', -override => 1),
1452 :     $cgi->hidden(-name => 'can_alter', -value => $can_alter, -override => 1),
1453 :     $cgi->hidden(-name => 'ssa_name', -value => $name, -override => 1),
1454 :     $cgi->br,
1455 :     );
1456 : overbeek 1.1
1457 : redwards 1.25 # RAE: First, a sanity check.
1458 :     # We may have to move this a little earlier, and show probably throw some nicer
1459 :     # errors to the end user (.e.g try setting can_alter and choosing an illegitimate ss
1460 :     # Do we know about this subsystem:
1461 : overbeek 1.26 my $ssaQ = quotemeta $ssa;
1462 : overbeek 1.124
1463 :     if (! -d "$FIG_Config::data/Subsystems/$ssa")
1464 :     ###### unless (grep {/$ssaQ/} map {$_->[0]} &existing_subsystem_annotations($fig))
1465 : redwards 1.25 {
1466 :     # No, we don't know about this subsystem
1467 :     my $url = &FIG::cgi_url . "/subsys.cgi?user=$user";
1468 :     push @$html, "Sorry. $name is not a valid subsystem. <p>\n",
1469 :     "Please return to the <a href=\"$url\">Subsystems Page</a> and choose an exisiting subsystem. <p>\n",
1470 :     "Sorry.";
1471 :     return undef;
1472 :     }
1473 :    
1474 : olson 1.109 &format_js_data($fig,$cgi,$html,$subsystem,$can_alter);
1475 : redwards 1.25
1476 : overbeek 1.14 &format_roles($fig,$cgi,$html,$subsystem,$can_alter);
1477 :     &format_subsets($fig,$cgi,$html,$subsystem,$can_alter);
1478 : olson 1.18
1479 : overbeek 1.115
1480 :     my $have_diagrams = &format_diagrams($fig, $cgi, $html, $subsystem, $can_alter);
1481 : olson 1.104
1482 :     #
1483 : olson 1.18 # Put link into constructs tool.
1484 :     #
1485 :    
1486 :     if ($can_alter)
1487 :     {
1488 : parrello 1.127 push(@$html, $cgi->p,
1489 :     $cgi->a({href => "construct.cgi?ssa=$ssa&user=$user",
1490 :     target => "_blank"},
1491 :     "Define higher level constructs."),
1492 :     $cgi->p);
1493 : olson 1.18 }
1494 :    
1495 :    
1496 : golsen 1.91 # Display the subsystem table rows, saving the list genomes displayed
1497 : olson 1.18
1498 : overbeek 1.115 my $active_genome_list = &format_rows($fig,$cgi,$html,$subsystem, $tagvalcolor,$have_diagrams);
1499 : redwards 1.64
1500 : golsen 1.91
1501 :     if ( $can_alter ) { format_extend_with($fig,$cgi,$html,$subsystem) }
1502 : golsen 1.73
1503 : golsen 1.75 my $esc_ssa = uri_escape( $ssa );
1504 : golsen 1.73 push @$html, "<TABLE width=\"100%\">\n",
1505 :     " <TR>\n",
1506 :     ($can_alter) ? " <TD>" . $cgi->checkbox(-name => 'precise_fill', -value => 1, -checked => 0, -override => 1,-label => 'fill') . "</TD>\n" : (),
1507 : parrello 1.127 " <TD><a href=\"Html/conflict_resolution.html\" class=\"help\" target=\"help\">Help on conflict resolution</a></TD>\n",
1508 :     " <TD><a href=\"Html/seedtips.html#edit_variants\" class=\"help\" target=\"help\">Help on editing variants</a></TD>\n",
1509 :     " <TD><a href=\"ss_export.cgi?user=$user&ssa_name=$esc_ssa\" class=\"help\">Export subsystem data</a></TD>\n",
1510 : golsen 1.73 " </TR>\n",
1511 :     "</TABLE>\n";
1512 : redwards 1.64
1513 :     if ($can_alter)
1514 :     {
1515 : overbeek 1.180 push(@$html,$cgi->submit('update spreadsheet')," OR ");
1516 : overbeek 1.1 }
1517 :     else
1518 :     {
1519 : parrello 1.127 push(@$html,$cgi->br);
1520 :     push(@$html,$cgi->submit('show spreadsheet'),$cgi->br);
1521 : overbeek 1.1 }
1522 : overbeek 1.134
1523 :    
1524 : redwards 1.24 push(@$html,$cgi->checkbox(-name => 'ignore_alt', -value => 1, -override => 1, -label => 'ignore alternatives', -checked => ($cgi->param('ignore_alt'))),$cgi->br);
1525 : overbeek 1.17 push(@$html,$cgi->checkbox(-name => 'ext_ids', -value => 1, -checked => 0, -label => 'use external ids'),$cgi->br);
1526 : redwards 1.121 push(@$html,$cgi->checkbox(-name => 'show_clusters', -value => 1, -label => 'show clusters'),$cgi->br);
1527 : golsen 1.186
1528 :     my @options = ();
1529 :     @options = sort {uc($a) cmp uc($b)} $fig->get_genome_keys(); # get all the genome keys
1530 : redwards 1.52 unshift(@options, undef); # a blank field at the start
1531 : redwards 1.54 push(@$html,"color rows by each organism's attribute: &nbsp; ", $cgi->popup_menu(-name => 'color_by_ga', -values=>\@options), $cgi->br);
1532 :    
1533 : golsen 1.186 # Compile and order the attribute keys found on pegs:
1534 :    
1535 :     my $high_priority = qr/(essential|fitness)/i;
1536 :     @options = sort { $b =~ /$high_priority/o <=> $a =~ /$high_priority/o
1537 :     || uc($a) cmp uc($b)
1538 :     }
1539 :     $fig->get_peg_keys();
1540 :     unshift @options, undef; # Start list with empty
1541 :    
1542 :     push( @$html, "color columns by each PEGs attribute: &nbsp; ",
1543 :     $cgi->popup_menu(-name => 'color_by_peg_tag', -values=>\@options),
1544 :     $cgi->br
1545 :     );
1546 :    
1547 :     if ( $time_it ) ## time ##
1548 :     { ## time ##
1549 :     push @times, scalar time(); ## time ##
1550 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
1551 :     } ## time ##
1552 : mkubal 1.173
1553 : golsen 1.91 push @$html, $cgi->checkbox(-name => 'show_missing', -value => 1, -checked => 0, -override => 1,-label => 'show missing'),
1554 :     $cgi->br, $cgi->br;
1555 :    
1556 : overbeek 1.3
1557 : golsen 1.91 # Format the organism list for a pop-up menu:
1558 : overbeek 1.3
1559 : golsen 1.92 my @genomes = sort { lc $a->[1] cmp lc $b->[1] } map { [ $_->[0], "$_->[1] [$_->[0]]" ] } @$active_genome_list;
1560 : golsen 1.91 unshift @genomes, [ '', 'select it in this menu' ];
1561 : mkubal 1.36
1562 : golsen 1.91 # Make a list of index number and roles for pop-up selections:
1563 :    
1564 :     my @roles = map { [ $subsystem->get_role_index( $_ ) + 1, $_ ] } $subsystem->get_roles;
1565 :     unshift @roles, [ '', 'select it in this menu' ];
1566 :    
1567 :     push @$html, "<table><tr><td>",
1568 :     $cgi->checkbox(-name => 'show_missing_including_matches', -value => 1, -checked => 0, -override => 1,-label => 'show missing with matches'), $cgi->br,
1569 : parrello 1.127 $cgi->checkbox(-name => 'show_missing_including_matches_in_ss', -value => 1, -checked => 0, -override => 1,-label => 'show missing with matches in ss'), "&nbsp;&nbsp;",
1570 : golsen 1.93 "</td>\n<td><big><big><big>} {</big></big></big></td>",
1571 : golsen 1.91 "<td>",
1572 : parrello 1.127 "[To restrict to a single genome: ",
1573 : golsen 1.91 $cgi->popup_menu( -name => 'just_genome',
1574 :     -values => [ map { $_->[0] } @genomes ],
1575 :     -labels => { map { ( $_->[0], $_->[1] ) } @genomes }
1576 :     ), "]", $cgi->br,
1577 : parrello 1.127 "[To restrict to a single role: ",
1578 : golsen 1.91 $cgi->popup_menu( -name => 'just_role',
1579 :     -values => [ map { $_->[0] } @roles ],
1580 :     -labels => { map { ( $_->[0], $_->[1] ) } @roles }
1581 :     ),
1582 : parrello 1.127 "]</td></tr></table>\n",
1583 :     $cgi->br;
1584 : golsen 1.91
1585 :    
1586 :     push @$html, "<table><tr><td>",
1587 :     $cgi->checkbox(-name => 'check_assignments', -value => 1, -checked => 0, -override => 1, -label => 'check assignments'),
1588 :     "&nbsp;&nbsp;[", $cgi->checkbox(-name => 'strict_check', -value => 1, -checked => 0, -override => 1, -label => 'strict'), "]&nbsp;&nbsp;",
1589 : golsen 1.93 "</td>\n<td><big><big><big>{</big></big></big></td>",
1590 : golsen 1.91 "<td>",
1591 : parrello 1.127 "[To restrict to a single genome: ",
1592 : golsen 1.91 $cgi->popup_menu( -name => 'just_genome_assignments',
1593 :     -values => [ map { $_->[0] } @genomes ],
1594 :     -labels => { map { ( $_->[0], $_->[1] ) } @genomes }
1595 :     ), "]", $cgi->br,
1596 : parrello 1.127 "[To restrict to a single role: ",
1597 : golsen 1.91 $cgi->popup_menu( -name => 'just_role_assignments',
1598 :     -values => [ map { $_->[0] } @roles ],
1599 :     -labels => { map { ( $_->[0], $_->[1] ) } @roles }
1600 :     ),
1601 : parrello 1.127 "]</td></tr></table>\n",
1602 :     $cgi->br;
1603 : mkubal 1.36
1604 : overbeek 1.3
1605 : golsen 1.186 if ( $time_it ) ## time ##
1606 :     { ## time ##
1607 :     push @times, scalar time(); ## time ##
1608 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
1609 :     } ## time ##
1610 :    
1611 : overbeek 1.14 if ($can_alter)
1612 :     {
1613 : parrello 1.127 push(@$html,$cgi->checkbox(-name => 'refill', -value => 1, -checked => 0, -override => 1,-label => 'refill spreadsheet from scratch'),$cgi->br);
1614 : overbeek 1.14 }
1615 :    
1616 : overbeek 1.1 push(@$html,$cgi->checkbox(-name => 'show_dups', -value => 1, -checked => 0, -override => 1,-label => 'show duplicates'),$cgi->br);
1617 :     push(@$html,$cgi->checkbox(-name => 'check_problems', -value => 1, -checked => 0, -override => 1,-label => 'show PEGs in roles that do not match precisely'),$cgi->br);
1618 : overbeek 1.14 if ($can_alter)
1619 :     {
1620 : parrello 1.127 push(@$html,$cgi->checkbox(-name => 'add_solid', -value => 1, -checked => 0, -override => 1,-label => 'add genomes with solid hits'),$cgi->br);
1621 : overbeek 1.14 }
1622 :    
1623 : overbeek 1.1 push(@$html,$cgi->checkbox(-name => 'show_coupled_fast', -value => 1, -checked => 0, -override => 1,-label => 'show coupled PEGs fast [depends on existing pins/clusters]'),$cgi->br);
1624 : golsen 1.94
1625 :     push(@$html,$cgi->checkbox(-name => 'show_coupled', -value => 1, -checked => 0, -override => 1,-label => 'show coupled PEGs [figure 2 minutes per PEG in spreadsheet]'),$cgi->br);
1626 :    
1627 : redwards 1.63 # RAE Hide -1 variants
1628 : redwards 1.121 push(@$html,$cgi->checkbox(-name => 'show_minus1', -value=> 1, -label => 'show -1 variants'),$cgi->br);
1629 : golsen 1.94
1630 : overbeek 1.158 # RAE Create excel spreadsheet of tables
1631 : overbeek 1.163 push(@$html, $raelib->excel_file_link, $cgi->checkbox(-name => 'create_excel', -value=> 1, -label => "Create Excel file of tables"), $cgi->br, "\n");
1632 : overbeek 1.158
1633 :    
1634 : golsen 1.94 # Alignment functions:
1635 :    
1636 :     push @$html, $cgi->hr,
1637 : parrello 1.127 # $cgi->br, "Column (specify the number of the column): ",
1638 :     # $cgi->textfield(-name => "col_to_align", -size => 7),
1639 :     "For sequences in a column (i.e., role): ",
1640 : golsen 1.94 $cgi->popup_menu( -name => 'col_to_align',
1641 :     -values => [ map { $_->[0] } @roles ],
1642 :     -labels => { map { ( $_->[0], $_->[1] ) } @roles }
1643 :     ),
1644 : parrello 1.127 $cgi->br,
1645 :     $cgi->submit(-value => "Show Sequences in Column",
1646 :     -name => "show_sequences_in_column"),
1647 :     $cgi->br,
1648 :     $cgi->submit(-value => "Align Sequences in Column",
1649 :     -name => "align_column"),
1650 :     $cgi->br,
1651 :     $cgi->br, "Realign subgroup within a column (adding homologs): ",
1652 :     $cgi->textfield(-name => "subcol_to_realign", -size => 7),
1653 :     $cgi->br, "Include homologs that pass the following threshhold: ",
1654 :     $cgi->textfield(-name => "include_homo", -size => 10)," (leave blank to see just column)",
1655 :     " Max homologous seqs: ",$cgi->textfield(-name => "max_homo", -value => 100, -size => 6),
1656 :     $cgi->br,
1657 :     $cgi->submit(-value => "Realign Sequences in Column",
1658 :     -name => "realign_column"),
1659 :     $cgi->hr;
1660 : golsen 1.94
1661 : golsen 1.186 if ( $time_it ) ## time ##
1662 :     { ## time ##
1663 :     push @times, scalar time(); ## time ##
1664 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
1665 :     } ## time ##
1666 :    
1667 : redwards 1.22 # RAE: A new function to reannotate a single column
1668 :     # I don't understand how you get CGI.pm to reset (and never have).
1669 :     # $cgi->delete("col_to_annotate"); # this does nothing to my script and there is always the last number in this box
1670 :     #push(@$html, $cgi->br,"Change annotation for column: ", $cgi->textfield(-name => "col_to_annotate", -size => 7));
1671 :     push(@$html, $cgi->br,"Change annotation for column: ", '<input type="text" name="col_to_annotate" value="" size="7">');
1672 : overbeek 1.1
1673 :     if ($can_alter)
1674 :     {
1675 : parrello 1.127 push(@$html,
1676 : overbeek 1.185 $cgi->p. $cgi->hr,
1677 : parrello 1.127 $cgi->p,
1678 :     $cgi->hr,
1679 :     "You should resynch PEG connections only if you detect PEGs that should be connected to the
1680 : overbeek 1.9 spreadsheet, but do not seem to be. This can only reflect an error in the code. If you find
1681 :     yourself having to use it, send mail to Ross.",
1682 : parrello 1.127 $cgi->br,
1683 :     $cgi->submit(-value => "Resynch PEG Connections",
1684 :     -name => "resynch_peg_connections"),
1685 :     $cgi->br);
1686 : overbeek 1.1 }
1687 : overbeek 1.10
1688 : golsen 1.186 if ( $time_it ) ## time ##
1689 :     { ## time ##
1690 :     push @times, scalar time(); ## time ##
1691 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
1692 :     } ## time ##
1693 :    
1694 : overbeek 1.12 my $notes = $subsystem->get_notes();
1695 : overbeek 1.14 if ($can_alter)
1696 :     {
1697 : parrello 1.127 push(@$html,$cgi->hr,"NOTES:\n",$cgi->br,$cgi->textarea(-name => 'notes', -rows => 40, -cols => 100, -value => $notes));
1698 : overbeek 1.14 }
1699 :     elsif ($notes)
1700 :     {
1701 : overbeek 1.161 $notes =~ s/(.{80}\s+)/$1\n/g;
1702 :     push(@$html,$cgi->h2('notes'),"<pre>$notes</pre>");
1703 : overbeek 1.14 }
1704 : overbeek 1.10
1705 : golsen 1.186 if ( $time_it ) ## time ##
1706 :     { ## time ##
1707 :     push @times, scalar time(); ## time ##
1708 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
1709 :     } ## time ##
1710 :    
1711 : redwards 1.41 # RAE Modified to add a line with the classification
1712 :     my $class=$subsystem->get_classification();
1713 :     if ($can_alter)
1714 :     {
1715 : redwards 1.108 my $menu1; my $menu2; # the two menus for the classification of subsystems
1716 :     # make sure we have empty blanks
1717 :     $menu1->{''}=$menu2->{''}=1;
1718 :     map {$menu1->{$_->[0]}=1; $menu2->{$_->[1]}=1} $fig->all_subsystem_classifications();
1719 :    
1720 :     push(@$html, $cgi->hr, "<table><tr><th colspan=2 style='text-align: center'>Subsystem Classification</th></tr>\n",
1721 :     "<tr><td>Please use ours:</td><td>", $cgi->popup_menu(-name=>"classif1", -values=>[sort {$a cmp $b} keys %$menu1], -default=>$$class[0]), "</td><td>",
1722 : parrello 1.127 $cgi->popup_menu(-name=>"classif2", -values=>[sort {$a cmp $b} keys %$menu2], -default=>$$class[1]), "</td></tr>\n<tr><td>Or make your own:</td><td>",
1723 :     $cgi->textfield(-name=>"classif1t", -size=>50), "</td><td>", $cgi->textfield(-name=>"classif2t", -size=>50), "</td></tr></table>\n"
1724 :     );
1725 : redwards 1.41 }
1726 :     elsif ($class)
1727 :     {
1728 : redwards 1.42 push (@$html, $cgi->h2('Classification'), "<table><tr><td>$$class[0]</td><td>$$class[1]</td></tr></table>\n");
1729 : redwards 1.41 }
1730 :    
1731 : golsen 1.186 if ( $time_it ) ## time ##
1732 :     { ## time ##
1733 :     push @times, scalar time(); ## time ##
1734 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
1735 :     } ## time ##
1736 :    
1737 : overbeek 1.180 my @orgs = map { "$_->[0]: " . $_->[1] }
1738 : overbeek 1.181 sort { $a->[1] cmp $b->[1] }
1739 : overbeek 1.180 map { [$_,$fig->genus_species($_)] }
1740 :     grep { $subsystem->get_variant_code($subsystem->get_genome_index($_)) ne "-1" }
1741 :     $subsystem->get_genomes;
1742 :     my @roles = $subsystem->get_roles;
1743 :     push(@$html,$cgi->hr,$cgi->h1('Lock PEGs in Cells'));
1744 :     push(@$html, $cgi->scrolling_list( -name => 'genome_to_lock',
1745 :     -values => [ @orgs ],
1746 :     -size => 10,
1747 :     -multiple => 1
1748 : golsen 1.184 ),
1749 :     $cgi->br, # Was unquoted <br>; read from undefined file handle
1750 : overbeek 1.180 $cgi->scrolling_list( -name => 'roles_to_lock',
1751 :     -values => [ @roles ],
1752 :     -size => 10,
1753 :     -multiple => 1
1754 : golsen 1.184 ),
1755 :     $cgi->br
1756 :     );
1757 : overbeek 1.180
1758 :     push(@$html,$cgi->submit('lock annotations')," OR ");
1759 :     push(@$html,$cgi->submit('unlock annotations'),$cgi->br);
1760 :    
1761 : overbeek 1.1 push(@$html, $cgi->end_form);
1762 :    
1763 : overbeek 1.19 my $target = "align$$";
1764 :     my @roles = $subsystem->get_roles;
1765 :     my $i;
1766 :     my $dir = $subsystem->get_dir;
1767 : overbeek 1.76 my $rolesA = &existing_trees($dir,\@roles);
1768 :    
1769 : overbeek 1.19 if (@$rolesA > 0)
1770 :     {
1771 : parrello 1.127 push(@$html, $cgi->hr,
1772 :     $cgi->h1('To Assign Using a Tree'),
1773 :     $cgi->start_form(-action => "assign_using_tree.cgi",
1774 :     -target => $target,
1775 :     -method => 'post'),
1776 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
1777 :     $cgi->hidden(-name => 'ali_dir', -value => "$dir/Alignments", -override => 1),
1778 :     $cgi->scrolling_list(-name => 'ali_num',
1779 :     -values => $rolesA,
1780 :     -size => 10,
1781 :     -multiple => 0
1782 :     ),
1783 :     $cgi->br,
1784 :     $cgi->submit(-value => "use_tree",
1785 :     -name => "use_tree"),
1786 :     $cgi->end_form
1787 :     );
1788 : overbeek 1.19 }
1789 :    
1790 : overbeek 1.1 push(@$html, $cgi->hr);
1791 :    
1792 :     if ($cgi->param('show_missing'))
1793 :     {
1794 : parrello 1.127 &format_missing($fig,$cgi,$html,$subsystem);
1795 : overbeek 1.1 }
1796 :    
1797 :     if ($cgi->param('show_missing_including_matches'))
1798 :     {
1799 : parrello 1.127 &format_missing_including_matches($fig,$cgi,$html,$subsystem);
1800 : overbeek 1.1 }
1801 : mkubal 1.36 if ($cgi->param('show_missing_including_matches_in_ss'))
1802 :     {
1803 : parrello 1.127 &format_missing_including_matches_in_ss($fig,$cgi,$html,$subsystem);
1804 : mkubal 1.36 }
1805 :    
1806 : overbeek 1.1
1807 : overbeek 1.3 if ($cgi->param('check_assignments'))
1808 :     {
1809 : parrello 1.127 &format_check_assignments($fig,$cgi,$html,$subsystem);
1810 : overbeek 1.3 }
1811 :    
1812 : overbeek 1.1 if ($cgi->param('show_dups'))
1813 :     {
1814 : parrello 1.127 &format_dups($fig,$cgi,$html,$subsystem);
1815 : overbeek 1.1 }
1816 :    
1817 :     if ($cgi->param('show_coupled'))
1818 :     {
1819 : parrello 1.127 &format_coupled($fig,$cgi,$html,$subsystem,"careful");
1820 : overbeek 1.1 }
1821 :     elsif ($cgi->param('show_coupled_fast'))
1822 :     {
1823 : parrello 1.127 &format_coupled($fig,$cgi,$html,$subsystem,"fast");
1824 : overbeek 1.1 }
1825 :    
1826 :     my $col;
1827 : overbeek 1.76 if ($col = $cgi->param('col_to_annotate'))
1828 : redwards 1.22 {
1829 :     &annotate_column($fig,$cgi,$html,$col,$subsystem);
1830 :     }
1831 : mkubal 1.156
1832 :     if ($cgi->param('ma_data_diagram_action'))
1833 :     {
1834 :     &paint_ma_data($fig,$cgi,$ssa,$subsystem);
1835 : mkubal 1.165 &make_link_to_painted_diagram($fig,$cgi,$html);
1836 : mkubal 1.156 }
1837 :    
1838 : mkubal 1.165 if ($cgi->param('paint_diagram_role_by_attribute_value'))
1839 :     {
1840 : mkubal 1.166 if ($cgi->param('paint_diagram_role_by_attribute_value')){
1841 :     my $diagram_name = $cgi->param('diagram_to_color');
1842 :     my $possible_roles_to_color = &find_roles_to_color($fig,$cgi,$html,$subsystem);
1843 :     &color_diagram_role_by_av($fig,$cgi,$ssa,$subsystem,$possible_roles_to_color,$diagram_name);
1844 :     &make_link_to_painted_diagram($fig,$cgi,$html);
1845 :     }
1846 : mkubal 1.165 }
1847 : golsen 1.186
1848 :     if ( $time_it ) ## time ##
1849 :     { ## time ##
1850 :     push @times, scalar time(); ## time ##
1851 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
1852 :     } ## time ##
1853 : overbeek 1.1 }
1854 :    
1855 : golsen 1.29
1856 :     #-----------------------------------------------------------------------------
1857 :     # Selection list of complete genomes not in spreadsheet:
1858 :     #-----------------------------------------------------------------------------
1859 :    
1860 : overbeek 1.1 sub format_extend_with {
1861 : golsen 1.29 my( $fig, $cgi, $html, $subsystem ) = @_;
1862 : overbeek 1.1
1863 :     my %genomes = map { $_ => 1 } $subsystem->get_genomes();
1864 :    
1865 : golsen 1.44 #
1866 :     # Use $fig->genomes( complete, restricted, domain ) to get org list:
1867 :     #
1868 :     my $req_comp = $cgi->param( 'complete' ) || 'Only "complete"';
1869 :     my $complete = ( $req_comp =~ /^all$/i ) ? undef : "complete";
1870 : overbeek 1.147
1871 : golsen 1.184 # What domains are to be displayed in the genome picker?
1872 :     # These are the canonical domain names defined in compute_genome_counts
1873 :     # and entered in the DBMS:
1874 :    
1875 :     my %maindomain = ( Archaea => 'A',
1876 :     Bacteria => 'B',
1877 :     Eukaryota => 'E',
1878 :     Plasmid => 'P',
1879 :     Virus => 'V',
1880 :     'Environmental Sample' => 'M', # Metagenome
1881 :     unknown => 'U'
1882 :     );
1883 :    
1884 :     my %label = ( Archaea => 'Archaea [A]',
1885 :     Bacteria => 'Bacteria [B]',
1886 :     Eukaryota => 'Eucarya [E]',
1887 :     Plasmid => 'Plasmids [P]',
1888 :     Virus => 'Viruses [V]',
1889 :     'Environmental Sample' => 'Environmental (metagenomes) [M]',
1890 :     unknown => 'unknown [U]'
1891 :     );
1892 :    
1893 :     # Currently, compute_genome_counts marks everything that is not Archae,
1894 :     # Bacteria or Eukcayra to not complete. So, the completeness status must
1895 :     # be ignored on the others.
1896 :    
1897 :     my %honor_complete = ( Archaea => 1, Bacteria => 1, Eukaryota => 1 );
1898 :    
1899 :     # Requested domains or default:
1900 :    
1901 :     my @picker_domains = grep { $maindomain{ $_ } }
1902 :     $cgi->param( 'picker_domains' );
1903 :     if ( ! @picker_domains ) { @picker_domains = qw( Archaea Bacteria Eukaryota ) }
1904 :    
1905 :     my %picker_domains = map { ( $_ => 1 ) } @picker_domains;
1906 :    
1907 :     # Build the domain selection checkboxes:
1908 :    
1909 :     my @domain_checkboxes = ();
1910 :     my %domain_abbrev = reverse %maindomain;
1911 :     foreach ( map { $domain_abbrev{ $_ } } qw( A B E P V M U ) )
1912 :     {
1913 :     push @domain_checkboxes, $cgi->checkbox( -name => 'picker_domains',
1914 :     -value => $_,
1915 :     -checked => ( $picker_domains{ $_ } ? 1 : 0 ),
1916 :     -label => $label{ $_ },
1917 :     -override => 1
1918 :     )
1919 :     }
1920 :    
1921 :     # Assemble the genome list for the picker. This could be optimized for
1922 :     # some special cases, but it is far from rate limiting. Most of the time
1923 :     # is looking up the name and domain, not the call to genomes().
1924 :     # Each org is represented as [ id, genus_species, domain ]
1925 :    
1926 :     my @orgs = ();
1927 :     foreach my $domain ( @picker_domains )
1928 : golsen 1.186 {
1929 :     push @orgs, map { [ $_, $fig->genus_species_domain( $_ ) ] }
1930 : golsen 1.184 grep { ! $genomes{ $_ } }
1931 :     $fig->genomes( $complete && $honor_complete{ $domain }, undef, $domain )
1932 :     }
1933 : golsen 1.29
1934 : golsen 1.44 #
1935 :     # Put it in the order requested by the user:
1936 :     #
1937 : golsen 1.29 my $pick_order = $cgi->param('pick_order') || 'Alphabetic';
1938 :     if ( $pick_order eq "Phylogenetic" )
1939 :     {
1940 : golsen 1.184 @orgs = sort { $a->[-1] cmp $b->[-1] }
1941 :     map { push @$_, lc $fig->taxonomy_of( $_->[0] ); $_ }
1942 : parrello 1.127 @orgs;
1943 : golsen 1.29 }
1944 :     elsif ( $pick_order eq "Genome ID" )
1945 :     {
1946 : golsen 1.184 @orgs = sort { $a->[-1]->[0] <=> $b->[-1]->[0] || $a->[-1]->[1] <=> $b->[-1]->[1] }
1947 :     map { push @$_, [ split /\./, $_->[0] ]; $_ }
1948 : parrello 1.127 @orgs;
1949 : golsen 1.29 }
1950 :     else
1951 :     {
1952 : parrello 1.127 $pick_order = 'Alphabetic';
1953 : golsen 1.184 @orgs = sort { $a->[-1] cmp $b->[-1] }
1954 :     map { push @$_, lc $_->[1]; $_ }
1955 :     @orgs;
1956 : golsen 1.29 }
1957 : overbeek 1.1
1958 : golsen 1.184 # Build the displayed name:
1959 :    
1960 :     @orgs = map { "$_->[1] [$maindomain{$_->[2]}] ($_->[0])" } @orgs;
1961 : golsen 1.29
1962 : golsen 1.44 #
1963 :     # Radio buttons to let the user choose the order they want for the list:
1964 :     #
1965 : golsen 1.29 my @order_opt = $cgi->radio_group( -name => 'pick_order',
1966 :     -values => [ 'Alphabetic', 'Phylogenetic', 'Genome ID' ],
1967 :     -default => $pick_order,
1968 :     -override => 1
1969 :     );
1970 :    
1971 : golsen 1.44 #
1972 :     # Radio buttons to let the user choose to include incomplete genomes:
1973 :     #
1974 :     my @complete = $cgi->radio_group( -name => 'complete',
1975 :     -default => $req_comp,
1976 :     -override => 1,
1977 :     -values => [ 'All', 'Only "complete"' ]
1978 :     );
1979 :    
1980 :     #
1981 :     # Display the pick list, and options:
1982 :     #
1983 : overbeek 1.180 my @roles = $subsystem->get_roles;
1984 : golsen 1.184 push( @$html, $cgi->h2('Pick Genomes to Extend with'), "\n",
1985 : golsen 1.29 "<TABLE>\n",
1986 : golsen 1.184 " <TR VAlign=top>\n",
1987 : golsen 1.29 " <TD>",
1988 : golsen 1.30 $cgi->scrolling_list( -name => 'new_genome',
1989 : golsen 1.29 -values => [ @orgs ],
1990 :     -size => 10,
1991 :     -multiple => 1
1992 :     ),
1993 :     " </TD>\n",
1994 : golsen 1.184
1995 : golsen 1.44 " <TD>",
1996 :     join( "<BR>\n", "<b>Order of selection list:</b>", @order_opt,
1997 :     "<b>Completeness?</b>", @complete
1998 :     ), "\n",
1999 : golsen 1.29 " </TD>\n",
2000 : golsen 1.184
2001 :     " <TD>&nbsp;&nbsp;&nbsp;</TD>\n",
2002 :    
2003 :     " <TD>\n",
2004 :     join( "<BR>\n", "<B>Include in selection list:</B>", @domain_checkboxes ), "\n",
2005 :     " </TD>\n",
2006 :    
2007 : golsen 1.29 " </TR>\n",
2008 :     "</TABLE>\n",
2009 : golsen 1.184
2010 : overbeek 1.136 $cgi->p("Add a specific group of genomes:"),
2011 :     $cgi->checkbox_group( -name=>"moregenomes",
2012 : overbeek 1.164 -values=>["NMPDR", "BRC", "Cyanobacteria", "Higher Plants", "Photosynthetic Eukaryotes", "Anoxygenic Phototrophs", "Hundred by a hundred"],
2013 : golsen 1.184 )
2014 :     );
2015 :    
2016 : golsen 1.186 if ( $time_it ) ## time ##
2017 :     { ## time ##
2018 :     push @times, scalar time(); ## time ##
2019 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
2020 :     } ## time ##
2021 : golsen 1.184
2022 :     push @$html, $cgi->hr;
2023 : overbeek 1.1 }
2024 :    
2025 : olson 1.109 #
2026 :     # Write out information about this subsystem as javascript
2027 :     # data structures. Used for the diagram coloring currently.
2028 :     #
2029 :     sub format_js_data
2030 :     {
2031 :     my($fig,$cgi,$html,$subsystem,$can_alter) = @_;
2032 :    
2033 :     push(@$html, qq(<script language="JavaScript">\n),
2034 : parrello 1.127 "subsystemInfo = {\n");
2035 : olson 1.109
2036 : parrello 1.127 my $first = 1;
2037 : olson 1.109 for my $g ($subsystem->get_genomes())
2038 :     {
2039 : parrello 1.127 my $txt = '';
2040 :     #
2041 :     # Determine which roles this genome has.
2042 :     #
2043 :     if (!$first)
2044 :     {
2045 :     $txt .= ", ";
2046 :     }
2047 :     else
2048 :     {
2049 :     $first = 0;
2050 :     }
2051 :    
2052 :     $txt .= "'$g': [";
2053 :    
2054 :     my $gi = $subsystem->get_genome_index($g);
2055 :    
2056 :     my $row = $subsystem->get_row($gi);
2057 :    
2058 :     my @r;
2059 :     for (my $ri = 0; $ri < @$row; $ri++)
2060 :     {
2061 :     my $cell = $row->[$ri];
2062 :     if ($#$cell > -1)
2063 :     {
2064 :     push(@r, "'" . $subsystem->get_role_abbr($ri) . "'");
2065 :     }
2066 :     }
2067 :    
2068 :     $txt .= join(", ", @r);
2069 :     $txt .= "]\n";
2070 :     push(@$html, $txt);
2071 : olson 1.109 }
2072 :     push(@$html, "};\n");
2073 :     push(@$html, "</script>\n");
2074 :     }
2075 : golsen 1.29
2076 : overbeek 1.1 sub format_roles {
2077 : overbeek 1.14 my($fig,$cgi,$html,$subsystem,$can_alter) = @_;
2078 : overbeek 1.1 my($i);
2079 :    
2080 : overbeek 1.97 my @roles = $subsystem->get_roles;
2081 : overbeek 1.96 my $sub_dir = $subsystem->get_dir;
2082 :    
2083 : overbeek 1.97 my $reactions = $subsystem->get_reactions;
2084 :    
2085 :     my $n = 1;
2086 :     my $col_hdrs = ["Column","Abbrev","Functional Role"];
2087 :    
2088 : overbeek 1.99 if ($can_alter)
2089 : overbeek 1.97 {
2090 : parrello 1.127 push(@$col_hdrs,"KEGG Reactions");
2091 :     push(@$col_hdrs,"Edit Reactions");
2092 : overbeek 1.99 }
2093 :     elsif ($reactions)
2094 :     {
2095 : parrello 1.127 push(@$col_hdrs,"KEGG Reactions");
2096 : overbeek 1.96 }
2097 :    
2098 : overbeek 1.1 my $tab = [];
2099 :    
2100 : overbeek 1.97 &format_existing_roles($fig,$cgi,$html,$subsystem,$tab,\$n,$can_alter,$reactions,\@roles);
2101 : overbeek 1.1 if ($cgi->param('can_alter'))
2102 :     {
2103 : parrello 1.127 for ($i=0; ($i < 5); $i++)
2104 :     {
2105 :     &format_role($fig,$cgi,$html,$subsystem,$tab,$n,"",$can_alter,undef);
2106 :     $n++;
2107 :     }
2108 : overbeek 1.1 }
2109 : overbeek 1.163 my %options; if ($cgi->param("create_excel")) {%options=(excelfile=>$cgi->param('ssa_name'), no_excel_link=>1)}
2110 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Functional Roles", %options),
2111 : parrello 1.127 $cgi->hr
2112 :     );
2113 : golsen 1.186
2114 :     if ( $time_it ) ## time ##
2115 :     { ## time ##
2116 :     push @times, scalar time(); ## time ##
2117 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
2118 :     } ## time ##
2119 : overbeek 1.1 }
2120 :    
2121 :     sub format_existing_roles {
2122 : overbeek 1.97 my($fig,$cgi,$html,$subsystem,$tab,$nP,$can_alter,$reactions,$roles) = @_;
2123 : overbeek 1.1 my($role);
2124 :    
2125 : overbeek 1.97 foreach $role (@$roles)
2126 : overbeek 1.1 {
2127 : parrello 1.127 &format_role($fig,$cgi,$html,$subsystem,$tab,$$nP,$role,$can_alter,$reactions);
2128 :     $$nP++;
2129 : overbeek 1.1 }
2130 :     }
2131 :    
2132 :     sub format_role {
2133 : overbeek 1.96 my($fig,$cgi,$html,$subsystem,$tab,$n,$role,$can_alter,$reactions) = @_;
2134 :     my($abbrev,$reactT);
2135 :    
2136 : overbeek 1.101 my $react = $reactions ? join(",", map { &HTML::reaction_link($_) } @{$reactions->{$role}}) : "";
2137 : overbeek 1.1
2138 :     $abbrev = $role ? $subsystem->get_role_abbr($subsystem->get_role_index($role)) : "";
2139 :    
2140 :     my($posT,$abbrevT,$roleT);
2141 : overbeek 1.14 if ($can_alter)
2142 : overbeek 1.1 {
2143 : parrello 1.127 $posT = $cgi->textfield(-name => "posR$n", -size => 3, -value => $n, -override => 1);
2144 :     $abbrevT = $cgi->textfield(-name => "abbrev$n", -size => 7, -value => $abbrev, -override => 1);
2145 :     $roleT = $cgi->textfield(-name => "role$n", -size => 80, -value => $role, -override => 1);
2146 :     $reactT = $cgi->textfield(-name => "react$n", -size => 20, -value => "", -override => 1);
2147 : overbeek 1.1 }
2148 :     else
2149 :     {
2150 : parrello 1.127 push(@$html,$cgi->hidden(-name => "posR$n", -value => $n, -override => 1),
2151 :     $cgi->hidden(-name => "abbrev$n", -value => $abbrev, -override => 1),
2152 :     $cgi->hidden(-name => "role$n", -value => $role, -override => 1));
2153 :     $posT = $n;
2154 :     $abbrevT = $abbrev;
2155 :     $roleT = $role;
2156 : overbeek 1.1 }
2157 :     #
2158 :     # Wrap the first element in the table with a <A NAME="role_rolename"> tag
2159 :     # so we can zing to it from elsewhere. We remove any non-alphanumeric
2160 :     # chars in the role name.
2161 :     #
2162 : overbeek 1.158 # Is there a reason for doing this ... it is not used.
2163 : overbeek 1.1
2164 :     my $posT_html;
2165 :     {
2166 : parrello 1.127 my $rn = $role;
2167 :     $rn =~ s/[ \/]/_/g;
2168 :     $rn =~ s/\W//g;
2169 : overbeek 1.1
2170 : parrello 1.127 $posT_html = "<a name=\"$rn\">$posT</a>";
2171 : overbeek 1.1 }
2172 :    
2173 : overbeek 1.158 #my $row = [$posT_html,$abbrevT,$roleT];
2174 :     my $row = [$posT,$abbrevT,$roleT];
2175 : overbeek 1.99 if ($can_alter)
2176 :     {
2177 : parrello 1.127 push(@$row,$react);
2178 :     push(@$row,$reactT);
2179 : overbeek 1.99 }
2180 :     elsif ($reactions)
2181 : overbeek 1.97 {
2182 : parrello 1.127 push(@$row,$react);
2183 : overbeek 1.97 }
2184 :     push(@$tab,$row);
2185 : overbeek 1.1
2186 :     if ($cgi->param('check_problems'))
2187 :     {
2188 : parrello 1.127 my @roles = grep { $_->[0] ne $role } &gene_functions_in_col($fig,$role,$subsystem);
2189 :     my($x,$peg);
2190 :     foreach $x (@roles)
2191 :     {
2192 :     push(@$tab,["","",$x->[0]]);
2193 :     push(@$tab,["","",join(",",map { &HTML::fid_link($cgi,$_) } @{$x->[1]})]);
2194 :     }
2195 : overbeek 1.1 }
2196 :     }
2197 :    
2198 :     sub gene_functions_in_col {
2199 :     my($fig,$role,$subsystem) = @_;
2200 :     my(%roles,$peg,$func);
2201 : redwards 1.21
2202 :    
2203 :     # RAE this is dying if $subsystem->get_col($subsystem->get_role_index($role) + 1) is not defined
2204 :     # it is also not returning the right answer, so we need to fix it.
2205 :     # I am not sure why this is incremented by one here (see the note) because it is not right
2206 :     # and if you don't increment it by one it is right.
2207 :    
2208 :     # incr by 1 to get col indexed from 1 (not 0)
2209 :     #my @pegs = map { @$_ } @{$subsystem->get_col($subsystem->get_role_index($role) + 1)};
2210 :    
2211 :     return undef unless ($role); # this takes care of one error
2212 :     my $col_role=$subsystem->get_col($subsystem->get_role_index($role));
2213 :     return undef unless (defined $col_role);
2214 :     my @pegs = map { @$_ } @$col_role;
2215 : overbeek 1.1
2216 :     foreach $peg (@pegs)
2217 :     {
2218 : parrello 1.127 if ($func = $fig->function_of($peg))
2219 :     {
2220 :     push(@{$roles{$func}},$peg);
2221 :     }
2222 : overbeek 1.1 }
2223 :     return map { [$_,$roles{$_}] } sort keys(%roles);
2224 :     }
2225 :    
2226 :     sub format_subsets {
2227 : overbeek 1.14 my($fig,$cgi,$html,$subsystem,$can_alter) = @_;
2228 : overbeek 1.1
2229 : overbeek 1.14 &format_subsetsC($fig,$cgi,$html,$subsystem,$can_alter);
2230 : overbeek 1.115 &format_subsetsR($fig,$cgi,$html,$subsystem,$can_alter);
2231 : overbeek 1.1 }
2232 :    
2233 :     sub format_subsetsC {
2234 : overbeek 1.14 my($fig,$cgi,$html,$subsystem,$can_alter) = @_;
2235 : overbeek 1.1
2236 :     my $col_hdrs = ["Subset","Includes These Roles"];
2237 :     my $tab = [];
2238 :    
2239 :     my $n = 1;
2240 : overbeek 1.14 &format_existing_subsetsC($cgi,$html,$subsystem,$tab,\$n,$can_alter);
2241 : overbeek 1.9
2242 : overbeek 1.14 if ($can_alter)
2243 : overbeek 1.1 {
2244 : parrello 1.127 my $i;
2245 :     for ($i=0; ($i < 5); $i++)
2246 :     {
2247 :     &format_subsetC($cgi,$html,$subsystem,$tab,$n,"");
2248 :     $n++;
2249 :     }
2250 : overbeek 1.1 }
2251 : overbeek 1.9
2252 : overbeek 1.163 my %options; if ($cgi->param("create_excel")) {%options=(excelfile=>$cgi->param('ssa_name'), no_excel_link=>1)}
2253 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Subsets of Roles", %options),
2254 : parrello 1.127 $cgi->hr
2255 :     );
2256 : overbeek 1.1
2257 : overbeek 1.145 my @subset_names = sort $subsystem->get_subset_namesC;
2258 : overbeek 1.1 if (@subset_names > 1)
2259 :     {
2260 : parrello 1.127 my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
2261 :     push(@$html,$cgi->scrolling_list(-name => 'active_subsetC',
2262 :     -values => [@subset_names],
2263 :     -default => $active_subsetC
2264 :     ),
2265 :     $cgi->br, "\n",
2266 :     );
2267 : overbeek 1.1 }
2268 :     else
2269 :     {
2270 : parrello 1.127 push(@$html,$cgi->hidden(-name => 'active_subsetC', -value => 'All', -override => 1));
2271 : overbeek 1.1 }
2272 : golsen 1.186
2273 :     if ( $time_it ) ## time ##
2274 :     { ## time ##
2275 :     push @times, scalar time(); ## time ##
2276 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
2277 :     } ## time ##
2278 : overbeek 1.1 }
2279 :    
2280 :     sub format_subsetsR {
2281 : overbeek 1.115 my($fig,$cgi,$html,$subsystem,$can_alter) = @_;
2282 : overbeek 1.1 my($i);
2283 :    
2284 :     my $link = &tree_link;
2285 : overbeek 1.147 push(@$html, $cgi->h2("Limit display"), $link,$cgi->br);
2286 : overbeek 1.1
2287 : overbeek 1.115 #
2288 :     # Default to showing All unless you're a curator.
2289 :     #
2290 :    
2291 :     my $active_subsetR;
2292 :    
2293 :     my $default_activeSubsetR = $can_alter ? $subsystem->get_active_subsetR : "All";
2294 :    
2295 :     $active_subsetR = ($cgi->param('active_subsetR') or $default_activeSubsetR);
2296 : overbeek 1.1
2297 :     my @tmp = grep { $_ ne "All" } sort $subsystem->get_subset_namesR;
2298 : overbeek 1.147
2299 :     # RAE: provide some alternative choices, and a little explantion
2300 :     my %options=(
2301 :     "higher_plants" => "Higher Plants",
2302 :     "eukaryotic_ps" => "Photosynthetic Eukaryotes",
2303 :     "nonoxygenic_ps" => "Anoxygenic Phototrophs",
2304 :     "hundred_hundred" => "Hundred by a hundred",
2305 : overbeek 1.152 "functional_coupling_paper" => "Functional Coupling Paper",
2306 : overbeek 1.158 "ecoli_essentiality_paper" => "E. coli Essentiality Paper",
2307 : redwards 1.177 "has_essentiality_data" => "Genomes with essentiality data",
2308 : overbeek 1.147 "" => "All",
2309 :     );
2310 : overbeek 1.151
2311 : overbeek 1.147 push(@$html,
2312 :     $cgi->p("Limit display of the the genomes in the table based on phylogeny or one of the preselected groups:"),
2313 :     "\n<table><tr><td>",
2314 :     $cgi->scrolling_list(-name => 'active_subsetR',
2315 : parrello 1.127 -values => ["All",@tmp],
2316 :     -default => $active_subsetR,
2317 :     -size => 5
2318 :     ),
2319 : overbeek 1.147 "</td><td>\n",
2320 : redwards 1.177 $cgi->radio_group(-name=>"active_key", -values=>[keys %options], -labels=>\%options, -linebreak=>'true', -default=>"", columns=>4),
2321 : overbeek 1.147 "</td></tr>\n</table>",
2322 : parrello 1.127 );
2323 : golsen 1.186
2324 :     if ( $time_it ) ## time ##
2325 :     { ## time ##
2326 :     push @times, scalar time(); ## time ##
2327 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
2328 :     } ## time ##
2329 : overbeek 1.1 }
2330 :    
2331 :     sub format_existing_subsetsC {
2332 : overbeek 1.14 my($cgi,$html,$subsystem,$tab,$nP,$can_alter) = @_;
2333 : overbeek 1.1 my($nameCS);
2334 :    
2335 :     foreach $nameCS (sort $subsystem->get_subset_namesC)
2336 :     {
2337 : parrello 1.127 if ($nameCS !~ /all/i)
2338 :     {
2339 :     &format_subsetC($cgi,$html,$subsystem,$tab,$$nP,$nameCS);
2340 :     $$nP++;
2341 :     }
2342 : overbeek 1.1 }
2343 :     }
2344 :    
2345 :     sub format_subsetC {
2346 :     my($cgi,$html,$subsystem,$tab,$n,$nameCS) = @_;
2347 :    
2348 :     if ($nameCS ne "All")
2349 :     {
2350 : parrello 1.127 my $subset = $nameCS ? join(",",map { $subsystem->get_role_index($_) + 1 } $subsystem->get_subsetC_roles($nameCS)) : "";
2351 : overbeek 1.9
2352 : parrello 1.127 $nameCS = $subset ? $nameCS : "";
2353 : overbeek 1.9
2354 : parrello 1.127 my($posT,$subsetT);
2355 : overbeek 1.9
2356 : parrello 1.127 $posT = $cgi->textfield(-name => "nameCS$n", -size => 30, -value => $nameCS, -override => 1);
2357 :     $subsetT = $cgi->textfield(-name => "subsetC$n", -size => 80, -value => $subset, -override => 1);
2358 :     push(@$tab,[$posT,$subsetT]);
2359 : overbeek 1.1 }
2360 :     }
2361 :    
2362 : olson 1.104
2363 :     #
2364 :     # Handle changes to diagrams.
2365 :     #
2366 :    
2367 :     sub handle_diagram_changes
2368 :     {
2369 :     my($fig, $subsystem, $cgi, $html) = @_;
2370 :     my $changed;
2371 : olson 1.111 my $sub_name = $subsystem->get_name();
2372 : olson 1.104
2373 :     return unless $cgi->param("diagram_action");
2374 :    
2375 :     my @actions = grep { /^diagram_/ } $cgi->param();
2376 :    
2377 :     for my $action (@actions)
2378 :     {
2379 : parrello 1.127 my $value = $cgi->param($action);
2380 :     if ($action =~ /^diagram_delete_(\S+)/ and $value eq "on")
2381 :     {
2382 :     warn "Delete diagram $sub_name $1\n";
2383 :     $subsystem->delete_diagram($1);
2384 :     $changed++;
2385 :     }
2386 :     elsif ($action =~ /^diagram_rename_(\S+)/ and $value ne "")
2387 :     {
2388 :     warn "Rename diagram $sub_name $1 to $value\n";
2389 :     $subsystem->rename_diagram($1, $value);
2390 :     $changed++;
2391 :     }
2392 :     elsif ($action =~ /^diagram_new_image_(\S+)/ and $value ne '')
2393 :     {
2394 :     my $fh = $cgi->upload($action);
2395 :     warn "Upload new image $fh $value for diagram $sub_name $1\n";
2396 :     $subsystem->upload_new_image($1, $cgi->upload($action));
2397 :     $changed++;
2398 :     }
2399 :     elsif ($action =~ /^diagram_new_html_(\S+)/ and $value ne '')
2400 :     {
2401 :     my $fh = $cgi->upload($action);
2402 :     warn "Upload new html $fh $value for diagram $sub_name $1\n";
2403 :     $subsystem->upload_new_html($1, $cgi->upload($action));
2404 :     $changed++;
2405 :     }
2406 :    
2407 : olson 1.104 }
2408 :    
2409 :     my $fh = $cgi->upload("diagram_image_file");
2410 : olson 1.109 my $html_fh = $cgi->upload("diagram_html_file");
2411 : olson 1.104
2412 :     if ($fh)
2413 :     {
2414 : parrello 1.127 my $name = $cgi->param("diagram_new_name");
2415 :    
2416 :     warn "Create new diagram $fh $html_fh name=$name\n";
2417 :     $subsystem->create_new_diagram($fh, $html_fh, $name);
2418 :     $changed++;
2419 : olson 1.104 }
2420 :    
2421 :     $subsystem->incr_version() if $changed;
2422 :     }
2423 :    
2424 :     #
2425 :     # Format the list of diagrams that a subsystem has.
2426 :     #
2427 :    
2428 :     sub format_diagrams
2429 :     {
2430 :     my($fig, $cgi, $html, $subsystem, $can_alter) = @_;
2431 :    
2432 :     my @diagrams = $subsystem->get_diagrams();
2433 : mkubal 1.166 my @diagram_names;
2434 : olson 1.104
2435 :     if (@diagrams or $can_alter)
2436 :     {
2437 : parrello 1.127 push(@$html, $cgi->hr, $cgi->h2("Subsystem Diagrams"));
2438 : olson 1.104 }
2439 :    
2440 : golsen 1.186 if ( $time_it ) ## time ##
2441 :     { ## time ##
2442 :     push @times, scalar time(); ## time ##
2443 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
2444 :     } ## time ##
2445 :    
2446 : olson 1.104 if (@diagrams)
2447 :     {
2448 : parrello 1.127 my @hdr = ("Diagram Name");
2449 : olson 1.104
2450 : parrello 1.127 if ($can_alter)
2451 :     {
2452 :     push(@hdr, "Delete", "Rename", "New image", "New html");
2453 :     }
2454 :    
2455 :     my @tbl;
2456 :     for my $dent (@diagrams)
2457 :     {
2458 :     my($id, $name, $link) = @$dent;
2459 : mkubal 1.166 push(@diagram_names,$name);
2460 :    
2461 :     my @row;
2462 : parrello 1.127
2463 :     my $js = "showDiagram('$link', '$id'); return false;";
2464 :    
2465 :     push(@row, qq(<a href="$link" onclick="$js" target="show_ss_diagram_$id">$name</a>));
2466 :    
2467 :     if ($can_alter)
2468 :     {
2469 :     push(@row, $cgi->checkbox(-name => "diagram_delete_$id", -label => "",
2470 :     -value => undef,
2471 :     -override => 1));
2472 :     push(@row, $cgi->textfield(-name => "diagram_rename_$id",
2473 :     -value => "",
2474 :     -override => 1));
2475 :     push(@row, $cgi->filefield(-name => "diagram_new_image_$id",
2476 :     -value => "",
2477 :     -override => 1,
2478 :     -size => 30));
2479 :     push(@row, $cgi->filefield(-name => "diagram_new_html_$id",
2480 :     -value => "",
2481 :     -override => 1,
2482 :     -size => 30));
2483 :     }
2484 :    
2485 :     push(@tbl, \@row);
2486 :     }
2487 :     push(@$html, &HTML::make_table(\@hdr, \@tbl));
2488 : olson 1.104 }
2489 :    
2490 : mkubal 1.169
2491 : parrello 1.127 my @tbl;
2492 : mkubal 1.156 my @tbl_ma;
2493 : mkubal 1.165 my @tbl_attribute;
2494 : parrello 1.127 push(@tbl, ["Diagram name:", $cgi->textfield(-name => "diagram_new_name",
2495 :     -value => "",
2496 :     -override => 1,
2497 :     -size => 30)]);
2498 :     push(@tbl, ["Diagram image file:", $cgi->filefield(-name => "diagram_image_file",
2499 :     -size => 50)]);
2500 :     push(@tbl, ["Diagram html file:", $cgi->filefield(-name => "diagram_html_file",
2501 :     -size => 50)]);
2502 :     push(@$html, $cgi->h3("Upload a new diagram"));
2503 :     push(@$html, &HTML::make_table(undef, \@tbl));
2504 :     push(@$html, $cgi->submit(-name => 'diagram_action',
2505 :     -label => 'Process diagram actions'));
2506 : mkubal 1.156 push(@tbl_ma, ["Genome ID:", $cgi->textfield(-name => "ma_data_genome_id",
2507 :     -value => "",
2508 :     -override => 1,
2509 :     -size => 30)]);
2510 :     push(@tbl_ma, ["Image File Width:", $cgi->textfield(-name => "image_file_width",
2511 :     -value => "",
2512 :     -override => 1,
2513 :     -size => 30)]);
2514 :    
2515 :     push(@tbl_ma, ["Image File Height:", $cgi->textfield(-name => "image_file_height",
2516 :     -value => "",
2517 :     -override => 1,
2518 :     -size => 30)]);
2519 :     push(@tbl_ma, ["Microarray data file:", $cgi->filefield(-name => "ma_data_file",
2520 :     -size => 50)]);
2521 :     push(@$html, $cgi->h3("View microarray data on diagram"));
2522 :     push(@$html, &HTML::make_table(undef, \@tbl_ma));
2523 :    
2524 :     push(@$html, $cgi->submit(-name => 'ma_data_diagram_action',
2525 :     -label => 'View microarray data on diagram'));
2526 : golsen 1.186
2527 :     my @select_keys = ( undef, sort { uc($a) cmp uc($b) }
2528 :     grep { /(Essential|fitness)/i }
2529 :     $fig->get_peg_keys()
2530 :     );
2531 :    
2532 : mkubal 1.165 push(@tbl_attribute, ["Genome ID:", $cgi->textfield(-name => "att_data_genome_id",
2533 :     -value => "",
2534 :     -override => 1,
2535 :     -size => 30)]);
2536 :     push(@tbl_attribute,["Select attribute", $cgi->popup_menu(-name => 'color_diagram_by_peg_tag', -values=>\@select_keys), $cgi->br]);
2537 : mkubal 1.168 my @values = ("all","essential","nonessential","potential_essential","undetermined");
2538 : mkubal 1.165
2539 : mkubal 1.169 push(@tbl_attribute,["Select diagram", $cgi->popup_menu(-name => 'diagram_to_color', -values=>\@diagram_names), $cgi->br]);
2540 : mkubal 1.165 push(@tbl_attribute,["Select value", $cgi->popup_menu(-name => 'value_to_color', -values=>\@values), $cgi->br]);
2541 :    
2542 :     push(@$html, $cgi->h3("Color Diagram Roles by Essentiality Attribute Value"));
2543 : mkubal 1.173 push(@$html, $cgi->p("red=essential, blue=nonessential, gray=undetermined white=gene with matching value not present"));
2544 : mkubal 1.165 push(@$html, &HTML::make_table(undef, \@tbl_attribute));
2545 :    
2546 :     push(@$html, $cgi->submit(-name => 'paint_diagram_role_by_attribute_value',
2547 :     -label => 'Color Matching Roles'));
2548 : mkubal 1.169
2549 : golsen 1.186 if ( $time_it ) ## time ##
2550 :     { ## time ##
2551 :     push @times, scalar time(); ## time ##
2552 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
2553 :     } ## time ##
2554 :    
2555 : overbeek 1.115 return @diagrams > 0;
2556 : olson 1.104 }
2557 :    
2558 : overbeek 1.1 sub tree_link {
2559 :     my $target = "window$$";
2560 :     my $url = &FIG::cgi_url . "/subsys.cgi?request=show_tree";
2561 : overbeek 1.147 return "<a href=$url target=$target>Show Phylogenetic Tree</a> (Shows the tree for all organisms in the SEED)";
2562 : overbeek 1.1 }
2563 :    
2564 : golsen 1.91
2565 :     # There is a lot of blood, sweat and tears that go into computing the active
2566 :     # set of rows. This is useful information to have later, when the user can
2567 :     # select genomes to be checked. We will return the genome list as a reference
2568 :     # to a list of [ genomme_number => name ] pairs. -- GJO
2569 :    
2570 : overbeek 1.1 sub format_rows {
2571 : overbeek 1.115 my($fig,$cgi,$html,$subsystem, $tagvalcolor, $have_diagrams) = @_;
2572 : overbeek 1.1 my($i,%alternatives);
2573 : golsen 1.91 my $active_genome_list = [];
2574 : overbeek 1.1
2575 :     my $ignore_alt = $cgi->param('ignore_alt');
2576 :    
2577 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
2578 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
2579 :    
2580 : redwards 1.59 # RAE:
2581 :     # added this to allow determination of an active_subsetR based on a tag value pair
2582 :     if ($cgi->param('active_key'))
2583 :     {
2584 :     $active_subsetR = $cgi->param('active_key');
2585 : parrello 1.127 my $active_value = undef;
2586 :     $active_value = $cgi->param('active_value') if ($cgi->param('active_value'));
2587 :     $subsystem->load_row_subsets_by_kv($active_subsetR, $active_value);
2588 :     $subsystem->set_active_subsetR($active_subsetR);
2589 : redwards 1.59 }
2590 :    
2591 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
2592 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
2593 :    
2594 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
2595 :     my %activeR = map { $_ => 1 } @subsetR;
2596 :    
2597 :     if (! $ignore_alt)
2598 :     {
2599 : parrello 1.127 my $subset;
2600 : overbeek 1.145 foreach $subset (grep { $_ =~ /^\*/ } sort $subsystem->get_subset_namesC)
2601 : parrello 1.127 {
2602 :     my @mem = grep { $activeC{$_} } $subsystem->get_subsetC_roles($subset);
2603 :     if (@mem > 1)
2604 :     {
2605 :     my $mem = [@mem];
2606 :     foreach $_ (@mem)
2607 :     {
2608 : overbeek 1.145 $alternatives{$_}->{$subset} = $mem;
2609 : parrello 1.127 }
2610 :     }
2611 :     }
2612 : overbeek 1.1 }
2613 :    
2614 :     my @in = $subsystem->get_genomes;
2615 : redwards 1.32
2616 : overbeek 1.1 if (@in > 0)
2617 :     {
2618 : overbeek 1.148 my $col_hdrs = ["Genome ID","Organism"];
2619 :    
2620 :     if ($cgi->param('can_alter') && $user && ($user eq $subsystem->get_curator))
2621 :     {
2622 :     my $ssa = $cgi->param('ssa_name');
2623 :     $ssa =~ s/[ \/]/_/g;
2624 : overbeek 1.164 push @$col_hdrs, "<a href=\"set_variants.cgi?user=$user&subsystem=$ssa&request=show_variants\">Variant Code</a>";
2625 : overbeek 1.148 }
2626 :     else
2627 :     {
2628 :     push @$col_hdrs, "Variant Code";
2629 :     }
2630 : parrello 1.127
2631 : overbeek 1.148
2632 : parrello 1.127 if ($cgi->param('color_by_ga')) {push @{$col_hdrs}, "Attribute"}
2633 :    
2634 :     my @row_guide = ();
2635 :    
2636 :     # Add pop-up tool tip with role name to abbreviations in column header
2637 :     # (a wonderful suggestion from Carl Woese). -- GJO
2638 :    
2639 : overbeek 1.145 my( $role, %in_col, %set_shown, $abbrev, $mem, $abbrev_html );
2640 : parrello 1.127 foreach $role (grep { $activeC{$_} } $subsystem->get_roles)
2641 :     {
2642 : overbeek 1.145 if ( $_ = $alternatives{ $role } )
2643 :     {
2644 :     my @in = grep { ! $set_shown{$_} } sort keys(%$_);
2645 :     foreach $abbrev (@in)
2646 :     {
2647 :     $set_shown{$abbrev} = 1;
2648 :     $mem = $_->{$abbrev};
2649 :    
2650 :     push( @row_guide, [ map { [ $_, "-" . ($subsystem->get_role_index($_) + 1) ] } @$mem ] );
2651 :     foreach $_ ( @$mem ) { $in_col{ $_ } = 1 }; # Mark the roles that are done
2652 :     my $rolelist = join '<br>', map { substr($_->[1],1) . ". $_->[0]" } @{$row_guide[-1]};
2653 :     $abbrev_html = "<a " . FIGjs::mouseover("Roles of $abbrev", $rolelist, '') . ">$abbrev</a>";
2654 :     push( @$col_hdrs, $abbrev_html );
2655 :     }
2656 :     }
2657 :     elsif (! $in_col{$role})
2658 :     {
2659 :     push( @row_guide, [ [ $role, "" ] ] ); # No suffix on peg number
2660 :     $abbrev = $subsystem->get_role_abbr( $subsystem->get_role_index( $role ) );
2661 :     $abbrev_html = "<a " . FIGjs::mouseover("Role of $abbrev", $role, '') . ">$abbrev</a>";
2662 :     push( @$col_hdrs, $abbrev_html );
2663 :     }
2664 : parrello 1.127 }
2665 :    
2666 :     my $tab = [];
2667 :     my($genome,@pegs,@cells,$set,$peg_set,$pair,$role,$suffix,$row,$peg,$color_of,$cell,%count,$color,@colors);
2668 :    
2669 :     #
2670 :     # Simplified code for checking variants -- GJO
2671 :     # If specific variants are requested, make a hash of those to keep:
2672 :     #
2673 :     my $variant_list = undef;
2674 :     if ( $cgi->param( 'include_these_variants' ) )
2675 :     {
2676 :     $variant_list = { map { ($_, 1) } split( /\s*,\s*/, $cgi->param( 'include_these_variants' ) ) };
2677 :     }
2678 :    
2679 :     foreach $genome (grep { $activeR{$_} } @in)
2680 :     {
2681 :     my($genomeV,$vcodeV,$vcode_value);
2682 : overbeek 1.1
2683 : parrello 1.127 # Get (and if necessary check) the variant code:
2684 : redwards 1.84
2685 : parrello 1.127 $vcode_value = $subsystem->get_variant_code( $subsystem->get_genome_index( $genome ) );
2686 :     next if ( $variant_list && ( ! $variant_list->{ $vcode_value } ) );
2687 : overbeek 1.1
2688 : parrello 1.127 $row = [ $genome, &ext_genus_species($fig,$genome), $vcode_value ];
2689 :     push @$active_genome_list, [ $row->[0], $row->[1] ]; # Save a list of the active genomes
2690 : golsen 1.98
2691 : parrello 1.127 @pegs = ();
2692 :     @cells = ();
2693 :    
2694 :     foreach $set (@row_guide)
2695 :     {
2696 :     $peg_set = [];
2697 :     foreach $pair (@$set)
2698 :     {
2699 :     ($role,$suffix) = @$pair;
2700 :     foreach $peg ($subsystem->get_pegs_from_cell($genome,$role))
2701 :     {
2702 :     push(@$peg_set,[$peg,$suffix]);
2703 :     }
2704 :     }
2705 :     push(@pegs,map { $_->[0] } @$peg_set);
2706 :     push(@cells,$peg_set);
2707 :     }
2708 :     $color_of = &group_by_clusters($fig,\@pegs);
2709 :     # RAE added a new call to get tag/value pairs
2710 :     # Note that $color_of is not overwritten.
2711 :     my $superscript;
2712 :     if ($cgi->param('color_by_ga'))
2713 :     {
2714 : redwards 1.52 # add colors based on the genome attributes
2715 : parrello 1.127 # get the value
2716 :     my $ga=$cgi->param('color_by_ga');
2717 :     my $valuetype=$fig->guess_value_format($ga);
2718 :     my @array=$fig->get_attributes($genome, $ga);
2719 :     unless ($array[0]) {$array[0]=[]}
2720 :     # for the purposes of this page, we are going to color on the
2721 :     # value of the last attribute
2722 :     my ($gotpeg, $gottag, $value, $url)=@{$array[0]};
2723 : redwards 1.52 if (defined $value) # we don't want to color undefined values
2724 : parrello 1.127 {
2725 :     my @color=&cool_colors();
2726 :     my $colval; # what we are basing the color on.
2727 :     if ($valuetype->[0] eq "float")
2728 :     {
2729 :     # Initially spllit numbers into groups of 10.
2730 :     # $valuetype->[2] is the maximum number for this value
2731 :     # but I don't like this
2732 :     # $colval = int($value/$valuetype->[2]*10);
2733 :    
2734 :     # we want something like 0-1, 1-2, 2-3, 3-4 as the labels.
2735 :     # so we will do it in groups of ten
2736 :     my ($type, $min, $max)=@$valuetype;
2737 :     for (my $i=$min; $i<$max; $i+=$max/10) {
2738 :     if ($value >= $i && $value < $i+$max/10) {$colval = $i . "-" . ($i+($max/10))}
2739 :     }
2740 :     }
2741 :     else {$colval=$value}
2742 : redwards 1.58
2743 : parrello 1.127 if (!$tagvalcolor->{$colval}) {
2744 :     # figure out the highest number used in the array
2745 :     $tagvalcolor->{$colval}=0;
2746 :     foreach my $t (keys %$tagvalcolor) {
2747 :     ($tagvalcolor->{$t} > $tagvalcolor->{$colval}) ? $tagvalcolor->{$colval}=$tagvalcolor->{$t} : 1;
2748 :     }
2749 :     $tagvalcolor->{$colval}++;
2750 :     }
2751 :     # RAE Add a column for the description
2752 :     splice @$row, 3, 0, $colval;
2753 :    
2754 :     foreach my $cell (@cells) {
2755 :     foreach $_ (@$cell)
2756 :     {
2757 :     $color_of->{$_->[0]} = $color[$tagvalcolor->{$colval}]
2758 :     }
2759 :     }
2760 :     }
2761 :     else
2762 :     {
2763 :     # RAE Add a column for the description
2764 :     splice @$row, 3, 0, " &nbsp; ";
2765 :     }
2766 :     }
2767 : redwards 1.54 if ($cgi->param("color_by_peg_tag"))
2768 : parrello 1.127 {
2769 :     ($color_of, $superscript, $tagvalcolor) = color_by_tag($fig, \@pegs, $color_of, $tagvalcolor, $cgi->param("color_by_peg_tag"));
2770 :     }
2771 :     foreach $cell ( @cells ) # $cell = [peg, suffix]
2772 :     {
2773 :     # Deal with the trivial case (no pegs) at the start
2774 :    
2775 :     if ( ! @$cell )
2776 :     {
2777 :     # Push an empty cell onto the row
2778 :    
2779 : overbeek 1.158 push @$row, [" &nbsp; ", "td bgcolor='#FFFFFF'"];
2780 : parrello 1.127 next;
2781 :     }
2782 :    
2783 :     # Figure out html text for each peg and cluster by color.
2784 :    
2785 :     my ( $peg, $suffix, $txt, $color );
2786 :     my @colors = ();
2787 :     my %text_by_color; # Gather like-colored peg text
2788 :     foreach ( @$cell )
2789 :     {
2790 :     ( $peg, $suffix ) = @$_;
2791 :     # Hyperlink each peg, and add its suffix:
2792 :     $txt = ( $cgi->param('ext_ids') ? external_id($fig,$cgi,$peg)
2793 :     : HTML::fid_link($cgi,$peg, "local") )
2794 :     . ( $suffix ? $suffix : '' );
2795 :     $color = $color_of->{ $peg };
2796 :     defined( $text_by_color{ $color } ) or push @colors, $color;
2797 :     push @{ $text_by_color{ $color } }, $txt;
2798 :     }
2799 :     my $ncolors = @colors;
2800 :    
2801 :     # Join text strings within a color (and remove last comma):
2802 :    
2803 :     my @str_by_color = map { [ $_, join( ', ', @{ $text_by_color{$_} }, '' ) ] } @colors;
2804 :     $str_by_color[-1]->[1] =~ s/, $//;
2805 :    
2806 :     # Build the "superscript" string:
2807 :    
2808 :     my $sscript = "";
2809 :     if ( $superscript && @$cell )
2810 :     {
2811 :     my ( %sscript, $ss );
2812 :     foreach my $cv ( @$cell ) # Should this be flattened across all pegs?
2813 :     {
2814 :     next unless ( $ss = $superscript->{ $cv->[0] } );
2815 :     # my %flatten = map { ( $_, 1 ) } @$ss;
2816 :     # $sscript{ join ",", sort { $a <=> $b } keys %flatten } = 1; # string of all values for peg
2817 :     foreach ( @$ss ) { $sscript{ $_ } = 1 }
2818 :     }
2819 :     if (scalar keys %sscript) # order by number, and format
2820 :     {
2821 :     my @ss = map { $_->[0] }
2822 :     sort { $a->[1] <=> $b->[1] }
2823 :     map { my ( $num ) = $_ =~ /\>(\d+)\</; [ $_, $num || 0 ] } keys %sscript;
2824 :     $sscript = "&nbsp;<sup>[" . join( ", ", @ss ) . "]</sup>"
2825 :     }
2826 :     }
2827 :    
2828 :     my $cell_data;
2829 :    
2830 :     # If there is one color, just write a unicolor cell.
2831 :    
2832 :     if ( $ncolors == 1 )
2833 :     {
2834 :     my ( $color, $txt ) = @{ shift @str_by_color };
2835 : overbeek 1.158 #$cell_data = qq(\@bgcolor="$color":) . $txt . $sscript;
2836 :     # using this format allows other things (like excel writing to easily parse out data and formatting)
2837 :     # the cell is a reference to an array. The first element is the data, and the second the formatting options
2838 :     $cell_data = [$txt . $sscript, "td bgcolor=\"$color\""];
2839 : parrello 1.127 }
2840 :    
2841 :     # Otherwise, write pegs into a subtable with one cell per color.
2842 : overbeek 1.135 # RAE: used style for this rather than a separate table per cell. All the small tables are crap
2843 :     # for rendering, especially if you have a lot of pegs in a ss
2844 : parrello 1.127
2845 : overbeek 1.135 elsif(0)
2846 : parrello 1.127 {
2847 : overbeek 1.135 # original way
2848 : parrello 1.127 $cell_data = '<table><tr valign=bottom>'
2849 :     . join( '', map { ( $color, $txt ) = @$_ ; qq(<td bgcolor="$color">$txt</td>) } @str_by_color )
2850 :     . ( $sscript ? "<td>$sscript</td>" : '' )
2851 :     . '</tr></table>';
2852 :     }
2853 :    
2854 : overbeek 1.135 else
2855 :     {
2856 :     $cell_data = join( '', map { ( $color, $txt ) = @$_ ; qq(<span style="background-color: $color">$txt</span>) } @str_by_color )
2857 :     . ( $sscript ? $sscript : '' );
2858 :     }
2859 :    
2860 :    
2861 :    
2862 : parrello 1.127 # Push the cell data onto the row:
2863 :    
2864 :     push(@$row, $cell_data);
2865 :     }
2866 :     push(@$tab,$row);
2867 :     }
2868 :    
2869 :    
2870 : golsen 1.183 my $sort = $cgi->param('sort') || 'by_phylo';
2871 :     if ($sort eq "by_pattern")
2872 : parrello 1.127 {
2873 : golsen 1.183 my @tmp = ();
2874 :     my $row;
2875 :     foreach $row (@$tab)
2876 :     {
2877 :     my @var = ();
2878 :     my $i;
2879 :     for ($i=3; ($i < @$row); $i++)
2880 : parrello 1.127 {
2881 : golsen 1.183 if (ref($row->[$i]) eq "ARRAY")
2882 :     {
2883 :     push(@var, ($row->[$i]->[0] =~ /\|/) ? 1 : 0);
2884 :     }
2885 :     else
2886 : parrello 1.127 {
2887 : golsen 1.183 push(@var, ($row->[$i] =~ /\|/) ? 1 : 0);
2888 : parrello 1.127 }
2889 :     }
2890 : golsen 1.183 push(@tmp,[join("",@var),$row]);
2891 : parrello 1.127 }
2892 : golsen 1.183 $tab = [map { $_->[1] } sort { $a->[0] cmp $b->[0] } @tmp];
2893 :     }
2894 :     elsif ($sort eq "by_phylo")
2895 :     {
2896 :     $tab = [map { $_->[0] }
2897 :     sort { ($a->[1] cmp $b->[1]) or ($a->[0]->[1] cmp $b->[0]->[1]) }
2898 :     map { [$_, $fig->taxonomy_of($_->[0])] }
2899 :     @$tab];
2900 :     }
2901 :     elsif ($sort eq "by_tax_id")
2902 :     {
2903 :     $tab = [sort { $a->[0] <=> $b->[0] } @$tab];
2904 :     }
2905 :     elsif ($sort eq "alphabetic")
2906 :     {
2907 :     $tab = [sort { ($a->[1] cmp $b->[1]) or ($a->[0] <=> $b->[0]) } @$tab];
2908 :     }
2909 :     elsif ($sort eq "by_variant")
2910 :     {
2911 :     $tab = [sort { ($a->[2] cmp $b->[2]) or ($a->[1] <=> $b->[1]) } @$tab];
2912 : parrello 1.127 }
2913 : redwards 1.121
2914 : parrello 1.127 foreach $row (@$tab)
2915 :     {
2916 :     next if ($row->[2] == -1 && !$cgi->param('show_minus1')); # RAE don't show -1 variants if checked
2917 :     my($genomeV,$vcodeV,$vcode_value);
2918 :     $genome = $row->[0];
2919 :     $vcode_value = $row->[2];
2920 :     if ($cgi->param('can_alter'))
2921 :     {
2922 :     $genomeV = $cgi->textfield(-name => "genome$genome", -size => 15, -value => $genome, -override => 1);
2923 :     $vcodeV = $cgi->textfield(-name => "vcode$genome", -value => $vcode_value, -size => 10);
2924 :     }
2925 :     else
2926 :     {
2927 :     push(@$html,$cgi->hidden(-name => "genome$genome", -value => $genome, -override => 1),
2928 :     $cgi->hidden(-name => "vcode$genome", -value => $vcode_value), "\n");
2929 :     $genomeV = $genome;
2930 :     $vcodeV = $vcode_value;
2931 :     }
2932 :    
2933 :     $row->[0] = $genomeV;
2934 :     $row->[2] = $vcodeV;
2935 : overbeek 1.1
2936 : parrello 1.127 #
2937 :     # JS link for coloring diagrams.
2938 :     #
2939 : redwards 1.121
2940 : parrello 1.127 if ($have_diagrams)
2941 :     {
2942 : mkubal 1.165 #my @roles = ("aspA");
2943 :     #my $colorJS = qq(<a href="" onclick="colorAttributeValue(@roles); return false;">Color</a>);
2944 : parrello 1.127 my $colorJS = qq(<a href="" onclick="colorGenome('$genome'); return false;">Color</a>);
2945 :     $row->[0] .= " " . $colorJS;
2946 :     }
2947 :     }
2948 :    
2949 :     my $tab1 = [];
2950 :    
2951 :     foreach $row (@$tab)
2952 :     {
2953 :     next if ($row->[2] == -1 && !$cgi->param('show_minus1')); # RAE don't show -1 variants if checked
2954 :     if ((@$tab1 > 0) && ((@$tab1 % 10) == 0))
2955 :     {
2956 : overbeek 1.158 #push(@$tab1,[map { "<b>$_</b>" } @$col_hdrs]) ;
2957 :     # set this up using the table format feature so that we know it is a header
2958 :     push(@$tab1,[map { [$_, "th"] } @$col_hdrs]) ;
2959 : parrello 1.127 }
2960 :     push(@$tab1,$row);
2961 :     }
2962 :    
2963 : overbeek 1.163 my %options; if ($cgi->param("create_excel")) {%options=(excelfile=>$cgi->param('ssa_name'), no_excel_link=>1)}
2964 :     $options{"class"}="white";
2965 :     push(@$html,$cgi->div({class=>"spreadsheet"}, &HTML::make_table($col_hdrs,$tab1,"Basic Spreadsheet", %options), $cgi->br),
2966 : parrello 1.127 $cgi->hr
2967 :     );
2968 :    
2969 :    
2970 :     my %sortmenu = (
2971 : golsen 1.123 unsorted => "None",
2972 :     alphabetic => "Alphabetical",
2973 : parrello 1.127 by_pattern => "Patterns",
2974 :     by_phylo => "Phylogeny",
2975 :     by_tax_id => "Taxonomy",
2976 :     by_variant => "Variant Code",
2977 : redwards 1.121 );
2978 : golsen 1.123
2979 : parrello 1.127 push @$html, "Sort spreadsheet genomes by ",
2980 : golsen 1.183 $cgi->popup_menu( -name => 'sort',
2981 :     -values => [sort keys %sortmenu],
2982 :     -labels => \%sortmenu,
2983 :     -default => $sort,
2984 :     -override => 1
2985 : parrello 1.127 );
2986 : mkubal 1.47
2987 :     push(@$html,'<br><br>Enter comma-separated list of variants to display in spreadsheet<br>',
2988 :     $cgi->textfield(-name => "include_these_variants", -size => 50)
2989 : parrello 1.127 );
2990 : mkubal 1.47 }
2991 : redwards 1.52
2992 : golsen 1.186 if ( $time_it ) ## time ##
2993 :     { ## time ##
2994 :     push @times, scalar time(); ## time ##
2995 :     push @$html, "<br>dT = @{[$times[-1]-$times[-2]]}, T = @{[$times[-1]-$times[0]]}<br>"; ## time ##
2996 :     } ## time ##
2997 :    
2998 : redwards 1.52 # add an explanation for the colors if we want one.
2999 :     if ($cgi->param('color_by_ga'))
3000 :     {
3001 : redwards 1.59 push(@$html, &HTML::make_table(undef,&describe_colors($tagvalcolor),"Color Descriptions<br><small>Link limits display to those organisms</small>"));
3002 : redwards 1.52 }
3003 : golsen 1.91
3004 :     return $active_genome_list; # [ [ id1, gs1 ], [ id2, gs2 ], ... ]
3005 : overbeek 1.1 }
3006 :    
3007 : golsen 1.91
3008 : overbeek 1.1 sub group_by_clusters {
3009 :     my($fig,$pegs) = @_;
3010 :     my($peg,@clusters,@cluster,@colors,$color,%seen,%conn,$x,$peg1,@pegs,$i);
3011 :    
3012 :     my $color_of = {};
3013 :     foreach $peg (@$pegs) { $color_of->{$peg} = '#FFFFFF' }
3014 :    
3015 :     if ($cgi->param('show_clusters'))
3016 :     {
3017 : parrello 1.127 @pegs = keys(%$color_of); # Use of keys makes @pegs entries unique
3018 : overbeek 1.144 @clusters = $fig->compute_clusters(\@pegs,undef,5000);
3019 : parrello 1.127 @colors = &cool_colors();
3020 :    
3021 :     if (@clusters > @colors) { splice(@clusters,0,(@clusters - @colors)) } # make sure we have enough colors
3022 : overbeek 1.1
3023 : parrello 1.127 my($cluster);
3024 :     foreach $cluster (@clusters)
3025 :     {
3026 : redwards 1.179 # RAE only color pegs if we have > 1 functional role involved in the cluster
3027 :     my %countfunctions=map{(scalar $fig->function_of($_)=>1)} @$cluster;
3028 :     next unless (scalar(keys %countfunctions) > 1);
3029 :    
3030 : parrello 1.127 $color = shift @colors;
3031 :     foreach $peg (@$cluster)
3032 :     {
3033 :     $color_of->{$peg} = $color;
3034 :     }
3035 :     }
3036 : overbeek 1.1 }
3037 :     return $color_of;
3038 :     }
3039 :    
3040 : redwards 1.32
3041 :     =head1 color_by_tag
3042 :    
3043 :     Change the color of cells by the pir superfamily. This is taken from the key/value pair
3044 :     Note that we will not change the color if $cgi->param('show_clusters') is set.
3045 :    
3046 :     This is gneric and takes the following arguments:
3047 :     fig,
3048 :     pointer to list of pegs,
3049 :     pointer to hash of colors by peg,
3050 :     pointer to a hash that retains numbers across rows. The number is based on the value.
3051 :     tag to use in encoding
3052 :    
3053 :     eg. ($color_of, $superscript, $tagvalcolor) = color_by_tag($fig, $pegs, $color_of, $tagvalcolor, "PIRSF");
3054 :    
3055 :     =cut
3056 :    
3057 :     sub color_by_tag {
3058 : redwards 1.35 # RAE added this so we can color individual cells across a column
3059 : redwards 1.32 my ($fig, $pegs, $color_of, $tagvalcolor, $want)=@_;
3060 :     # figure out the colors and the superscripts for the pirsf
3061 :     # superscript will be a number
3062 :     # color will be related to the number somehow
3063 :     # url will be the url for each number
3064 :     my $number; my $url;
3065 : redwards 1.33 my $count=0;
3066 : redwards 1.32 #count has to be the highest number if we increment it
3067 : redwards 1.33 foreach my $t (keys %$tagvalcolor) {($tagvalcolor->{$t} > $count) ? $count=$tagvalcolor->{$t} : 1}
3068 :     $count++; # this should now be the next number to assign
3069 : redwards 1.32 foreach my $peg (@$pegs) {
3070 : redwards 1.54 next unless (my @attr=$fig->get_attributes($peg));
3071 : redwards 1.32 foreach my $attr (@attr) {
3072 : redwards 1.54 next unless (defined $attr);
3073 : redwards 1.67 my ($gotpeg, $tag, $val, $link)=@$attr;
3074 : redwards 1.32 next unless ($tag eq $want);
3075 :     if ($tagvalcolor->{$val}) {
3076 :     $number->{$peg}=$tagvalcolor->{$val};
3077 : overbeek 1.129 push (@{$url->{$peg}}, "<a " . FIGjs::mouseover($tag, $val) . " href='$link'>" . $number->{$peg} . "</a>");
3078 : redwards 1.32 }
3079 :     else {
3080 :     $number->{$peg}=$tagvalcolor->{$val}=$count++;
3081 : overbeek 1.129 push (@{$url->{$peg}}, "<a " . FIGjs::mouseover($tag, $val) . "href='$link'>" . $number->{$peg} . "</a>");
3082 : redwards 1.32 }
3083 :     #### This is a botch at the moment. I want PIRSF to go to my page that I am working on, not PIR
3084 :     #### so I am just correcting those. This is not good, and I should change the urls in the tag/value pairs or something
3085 :     if ($want eq "PIRSF") {
3086 : redwards 1.66 pop @{$url->{$peg}};
3087 : redwards 1.32 $val =~ /(^PIRSF\d+)/;
3088 : redwards 1.66 push (@{$url->{$peg}}, $cgi->a({href => "pir.cgi?&user=$user&pirsf=$1"}, $number->{$peg}));
3089 : redwards 1.32 }
3090 :     }
3091 :     }
3092 :    
3093 :    
3094 :     # if we want to assign some colors, lets do so now
3095 : redwards 1.52 my @colors = &cool_colors();
3096 : redwards 1.32 unless ($cgi->param('show_clusters')) {
3097 :     foreach my $peg (@$pegs) { $color_of->{$peg} = '#FFFFFF' }
3098 :     foreach my $peg (keys %$number) {
3099 :     # the color is going to be the location in @colors
3100 :     unless ($number->{$peg} > @colors) {$color_of->{$peg}=$colors[$number->{$peg}-1]}
3101 :     }
3102 :     }
3103 :     return ($color_of, $url, $tagvalcolor);
3104 :     }
3105 :    
3106 :    
3107 : overbeek 1.1 sub format_ssa_table {
3108 :     my($cgi,$html,$user,$ssaP) = @_;
3109 :     my($ssa,$curator);
3110 :     my($url1,$link1);
3111 :    
3112 :     my $can_alter = $cgi->param('can_alter');
3113 :     push(@$html, $cgi->start_form(-action => "subsys.cgi",
3114 : parrello 1.127 -method => 'post'),
3115 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
3116 :     $cgi->hidden(-name => 'can_alter', -value => $can_alter, -override => 1),
3117 :     $cgi->hidden(-name => 'request', -value => 'delete_or_export_ssa', -override => 1)
3118 :     );
3119 : overbeek 1.1 push(@$html,"<font size=\"+2\">Please do not ever edit someone else\'s spreadsheet (by using their
3120 : parrello 1.127 user ID), and <b>never open multiple windows to
3121 : overbeek 1.1 process the same spreadsheet</b></font>. It is, of course, standard practice to open a subsystem
3122 :     spreadsheet and then to have multiple other SEED windows to access data and modify annotations. Further,
3123 : parrello 1.127 you can access someone else's subsystem spreadsheet using your ID (which will make it impossible
3124 : overbeek 1.1 for you to edit the spreadsheet).
3125 : redwards 1.46 Just do not open the same subsystem spreadsheet for editing in multiple windows simultaneously.
3126 : parrello 1.127 A gray color means that the subsystem has no genomes attached to it. Go ahead and make these your own\n",
3127 :     "<a href=\"Html/conflict_resolution.html\" class=\"help\" target=\"help\">Help on conflict resolution</a>\n",
3128 :     $cgi->br,
3129 :     $cgi->br
3130 : overbeek 1.1 );
3131 :    
3132 : redwards 1.65 # RAE: removed this from above push because VV want's it kept secret
3133 : parrello 1.127 # "<a href=\"/FIG/Html/seedtips.html#change_ownership\" class=\"help\" target=\"help\">Help on changing subsystem ownership</a>\n",
3134 : redwards 1.65
3135 : redwards 1.81 # RAE: Added a new cgi param colsort for sort by column. This url will just recall the script with username to allow column sorting.
3136 : redwards 1.88 # RAE: Added a column to allow indexing of one subsystem. This is also going to be used in the renaming of a subsystem, too
3137 : redwards 1.81
3138 : overbeek 1.1 my $col_hdrs = [
3139 : overbeek 1.128 "<a href='" . &FIG::cgi_url . "/subsys.cgi?user=$user&request=manage_ss'>Name</a><br><small>Sort by Subsystem</small>",
3140 :     "<a href='" . &FIG::cgi_url . "/subsys.cgi?user=$user&colsort=curator&request=manage_ss'>Curator</a><br><small>Sort by curator</small>",
3141 : redwards 1.176 "NMPDR<br>Subsystem", "Distributable<br>Subsystem", "OK to Automatically<br>Extend", "Exchangable","Version",
3142 : parrello 1.127 "Reset to Previous Timestamp","Delete",
3143 :     "Export Full Subsystem","Export Just Assignments", "Publish to Clearinghouse", "Reindex Subsystem",
3144 :     ];
3145 : overbeek 1.1 my $title = "Existing Subsystem Annotations";
3146 :     my $tab = [];
3147 : overbeek 1.139 my $userss; # this is a reference to a hash of all the subsystems the user can edit.
3148 : overbeek 1.1 foreach $_ (@$ssaP)
3149 :     {
3150 : parrello 1.127 my($publish_checkbox, $index_checkbox);
3151 :     ($ssa,$curator) = @$_;
3152 : overbeek 1.1
3153 : parrello 1.127 my $esc_ssa = uri_escape($ssa);
3154 : overbeek 1.139 if ($curator eq $user) {push @$userss, $ssa}
3155 : olson 1.74
3156 : parrello 1.127 my($url,$link);
3157 :     if ((-d "$FIG_Config::data/Subsystems/$ssa/Backup") && ($curator eq $user))
3158 :     {
3159 :     $url = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$esc_ssa&request=reset";
3160 :     $link = "<a href=$url>reset</a>";
3161 :     }
3162 :     else
3163 :     {
3164 :     $link = "";
3165 :     }
3166 :    
3167 : overbeek 1.139 # do we want to allow this in the NMPDR
3168 :     my $nmpdr;
3169 :     if ($curator eq $user)
3170 :     {
3171 : overbeek 1.140 $nmpdr=$cgi->checkbox(-name=> "nmpdr_ss", -value=>$ssa, -label=>"", -checked=>$fig->nmpdr_subsystem($ssa));
3172 :     }
3173 :     # do we want to allow this to be shared
3174 :     my $dist;
3175 :     if ($curator eq $user)
3176 :     {
3177 : overbeek 1.142 $dist=$cgi->checkbox(-name=> "dist_ss", -value=>$ssa, -label=>"", -checked=>$fig->distributable_subsystem($ssa));
3178 : overbeek 1.139 }
3179 : redwards 1.176
3180 :     # do we want to allow this to be automatically updated
3181 :     my $auto_update;
3182 :     if ($curator eq $user)
3183 :     {
3184 :     $auto_update=$cgi->checkbox(-name=> "auto_update_ok", -value=>$ssa, -label=>"", -checked=>$fig->ok_to_auto_update_subsys($ssa));
3185 :     }
3186 : overbeek 1.139
3187 : parrello 1.127 if (($fig->is_exchangable_subsystem($ssa)) && ($curator eq $user))
3188 :     {
3189 :     $url1 = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$esc_ssa&request=make_unexchangable";
3190 :     $link1 = "Exchangable<br><a href=$url1>Make not exchangable</a>";
3191 :     }
3192 :     elsif ($curator eq $user)
3193 :     {
3194 :     $url1 = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$esc_ssa&request=make_exchangable";
3195 :     $link1 = "Not exchangable<br><a href=$url1>Make exchangable</a>";
3196 :     }
3197 :     else
3198 :     {
3199 :     $link1 = "";
3200 :     }
3201 :    
3202 :     #
3203 :     # Only allow publish for subsystems we are curating?
3204 :     #
3205 :     if ($curator eq $user)
3206 :     {
3207 :     $publish_checkbox = $cgi->checkbox(-name => "publish_to_clearinghouse",
3208 :     -value => $ssa,
3209 :     -label => "Publish");
3210 :    
3211 :     }
3212 :    
3213 :     #
3214 :     # Initially I am going to allow indexing of any subsystem since you may want to index it to allow
3215 :     # better searhing on a local system
3216 :     $index_checkbox=$cgi->checkbox(-name => "index_subsystem", -value=> $ssa, -label => "Index");
3217 :    
3218 :     # RAE color the background if the subsystem is empty
3219 :     # this uses a modification to HTML.pm that I made earlier to accept refs to arrays as cell data
3220 :     my $cell1=&ssa_link($fig,$ssa,$user);
3221 :     #unless (scalar $fig->subsystem_to_roles($ssa)) {$cell1 = [$cell1, 'td bgcolor="Dark grey"']} ## THIS IS DOG SLOW, BUT WORKS
3222 :     #unless (scalar $fig->get_subsystem($ssa)->get_genomes()) {$cell1 = [$cell1, 'td bgcolor="#A9A9A9"']} ## WORKS PERFECTLY, but sort of slow
3223 :     unless (scalar @{$fig->subsystem_genomes($ssa, 1)}) {$cell1 = [$cell1, 'td bgcolor="silver"']}
3224 :    
3225 :     push(@$tab,[
3226 :     $cell1,
3227 :     $curator,
3228 : overbeek 1.139 $nmpdr,
3229 : overbeek 1.140 $dist,
3230 : redwards 1.176 $auto_update,
3231 : parrello 1.127 $link1,
3232 :     $fig->subsystem_version($ssa),
3233 :     $link,
3234 :     ($curator eq $user) ? $cgi->checkbox(-name => "delete", -value => $ssa) : "",
3235 :     $cgi->checkbox(-name => "export", -value => $ssa, -label => "Export full"),
3236 :     $cgi->checkbox(-name => "export_assignments", -value => $ssa, -label => "Export assignments"),
3237 :     $publish_checkbox, $index_checkbox,
3238 :     ]);
3239 : overbeek 1.1 }
3240 :     push(@$html,
3241 : parrello 1.127 &HTML::make_table($col_hdrs,$tab,$title),
3242 : overbeek 1.139 $cgi->hidden(-name => "users_ss",
3243 :     -value=> $userss),
3244 :     $cgi->hidden(-name => "manage"),
3245 : redwards 1.176 $cgi->submit(-name => "save_clicks",
3246 :     -label => "Process Choices"),
3247 : parrello 1.127 $cgi->submit(-name => 'delete_export',
3248 :     -label => 'Process marked deletions and exports'),
3249 :     $cgi->submit(-name => 'publish',
3250 :     -label => "Publish marked subsystems"),
3251 : redwards 1.88 $cgi->submit(-name => 'reindex',
3252 : parrello 1.127 -label => "Reindex selected subsystems"),
3253 :     $cgi->end_form
3254 :     );
3255 : overbeek 1.1 }
3256 :    
3257 : redwards 1.25 # RAE: I think this should be placed as a method in
3258 :     # Subsystems.pm and called subsystems I know about or something.
3259 :     # Cowardly didn't do though :-)
3260 : overbeek 1.1 sub existing_subsystem_annotations {
3261 : overbeek 1.51 my($fig) = @_;
3262 : overbeek 1.1 my($ssa,$name);
3263 :     my @ssa = ();
3264 :     if (opendir(SSA,"$FIG_Config::data/Subsystems"))
3265 :     {
3266 : overbeek 1.154 @ssa = map { $ssa = $_; $name = $ssa; $ssa =~ s/[ \/]/_/g; [$name,&subsystem_curator($ssa)] } grep { $_ !~ /^\./ } readdir(SSA);
3267 : parrello 1.127 closedir(SSA);
3268 : overbeek 1.1 }
3269 : redwards 1.81 # RAE Adding sort of current subsystems
3270 :     if ($cgi->param('colsort') && $cgi->param('colsort') eq "curator")
3271 :     {
3272 :     # sort by the ss curator
3273 :     return sort { (lc $a->[1]) cmp (lc $b->[1]) || (lc $a->[0]) cmp (lc $b->[0]) } @ssa;
3274 :     }
3275 :     else
3276 :     {
3277 :     return sort { (lc $a->[0]) cmp (lc $b->[0]) } @ssa;
3278 :     }
3279 : overbeek 1.1 }
3280 :    
3281 :     sub ssa_link {
3282 : overbeek 1.51 my($fig,$ssa,$user) = @_;
3283 : overbeek 1.1 my $name = $ssa; $name =~ s/_/ /g;
3284 :     my $target = "window$$";
3285 : overbeek 1.9 if ($name =~ /([a-zA-Z]{3})/)
3286 :     {
3287 : parrello 1.127 $target .= ".$1";
3288 : overbeek 1.9 }
3289 :    
3290 : overbeek 1.80 my $check;
3291 : overbeek 1.154 my $can_alter = $check = &subsystem_curator($ssa) eq $user;
3292 : redwards 1.108 my $sort=$cgi->param('sort');
3293 :     my $show_clusters=$cgi->param('show_clusters');
3294 : redwards 1.121 my $minus=$cgi->param('show_minus1');
3295 : redwards 1.108
3296 : olson 1.74 my $esc_ssa = uri_escape($ssa);
3297 : redwards 1.121 my $url = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$esc_ssa&request=show_ssa&can_alter=$can_alter&check=$check&sort=$sort&show_clusters=$show_clusters&show_minus1=$minus";
3298 : overbeek 1.1 return "<a href=$url target=$target>$name</a>";
3299 :     }
3300 :    
3301 :     sub log_update {
3302 :     my($ssa,$user) = @_;
3303 :    
3304 :     $ssa =~ s/[ \/]/_/g;
3305 :    
3306 :     if (open(LOG,">>$FIG_Config::data/Subsystems/$ssa/curation.log"))
3307 :     {
3308 : parrello 1.127 my $time = time;
3309 :     print LOG "$time\t$user\tupdated\n";
3310 :     close(LOG);
3311 : overbeek 1.1 }
3312 :     else
3313 :     {
3314 : parrello 1.127 print STDERR "failed to open $FIG_Config::data/Subsystems/$ssa/curation.log\n";
3315 : overbeek 1.1 }
3316 :     }
3317 :    
3318 :     sub export {
3319 :     my($fig,$cgi,$ssa) = @_;
3320 :     my($line);
3321 :    
3322 :     my ($exportable,$notes) = $fig->exportable_subsystem($ssa);
3323 :     foreach $line (@$exportable,@$notes)
3324 :     {
3325 : parrello 1.127 print $line;
3326 : overbeek 1.1 }
3327 :     }
3328 : parrello 1.127
3329 : overbeek 1.1 sub export_assignments {
3330 :     my($fig,$cgi,$ssa) = @_;
3331 :     my(@roles,$i,$entry,$id,$user);
3332 :    
3333 : redwards 1.121 if ($user && open(SSA,"<$FIG_Config::data/Subsystems/$ssa/spreadsheet"))
3334 : overbeek 1.1 {
3335 : parrello 1.127 $user =~ s/^master://;
3336 :     &FIG::verify_dir("$FIG_Config::data/Assignments/$user");
3337 : overbeek 1.154 my $who = &subsystem_curator($ssa);
3338 : parrello 1.127 my $file = &FIG::epoch_to_readable(time) . ":$who:generated_from_subsystem_$ssa";
3339 :    
3340 :     if (open(OUT,">$FIG_Config::data/Assignments/$user/$file"))
3341 :     {
3342 :     while (defined($_ = <SSA>) && ($_ !~ /^\/\//))
3343 :     {
3344 :     chop;
3345 :     push(@roles,$_);
3346 :     }
3347 :     while (defined($_ = <SSA>) && ($_ !~ /^\/\//)) {}
3348 :     while (defined($_ = <SSA>))
3349 :     {
3350 :     chop;
3351 :     my @flds = split(/\t/,$_);
3352 :     my $genome = $flds[0];
3353 :     for ($i=2; ($i < @flds); $i++)
3354 :     {
3355 :     my @entries = split(/,/,$flds[$i]);
3356 :     foreach $id (@entries)
3357 :     {
3358 :     my $peg = "fig|$genome.peg.$id";
3359 :     my $func = $fig->function_of($peg);
3360 :     print OUT "$peg\t$func\n";
3361 :     }
3362 :     }
3363 :     }
3364 :     close(OUT);
3365 :     }
3366 :     close(SSA);
3367 : overbeek 1.1 }
3368 :     }
3369 :    
3370 :     sub format_missing {
3371 :     my($fig,$cgi,$html,$subsystem) = @_;
3372 :     my($org,$abr,$role,$missing);
3373 :    
3374 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
3375 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
3376 :    
3377 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
3378 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
3379 :    
3380 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
3381 :    
3382 : overbeek 1.145 my @alt_sets = grep { ($_ =~ /^\*/) } sort $subsystem->get_subset_namesC;
3383 : overbeek 1.1 my($set,$col,%in);
3384 :     foreach $set (@alt_sets)
3385 :     {
3386 : parrello 1.127 my @mem = grep { $activeC{$_} } $subsystem->get_subsetC_roles($set);
3387 :     foreach $col (@mem)
3388 :     {
3389 :     $in{$col} = $set;
3390 :     }
3391 : overbeek 1.1 }
3392 :     push(@$html,$cgi->h1('To Check Missing Entries:'));
3393 :    
3394 :     foreach $org (@subsetR)
3395 :     {
3396 : parrello 1.127 my @missing = &columns_missing_entries($cgi,$subsystem,$org,\@subsetC,\%in);
3397 : overbeek 1.1
3398 : parrello 1.127 $missing = [];
3399 :     foreach $role (@missing)
3400 :     {
3401 :     $abr = $subsystem->get_role_abbr($subsystem->get_role_index($role));
3402 :     my $roleE = $cgi->escape($role);
3403 :    
3404 :     my $link = "<a href=" . &FIG::cgi_url . "/pom.cgi?user=$user&request=find_in_org&role=$roleE&org=$org>$abr $role</a>";
3405 :     push(@$missing,$link);
3406 :     }
3407 :    
3408 :     if (@$missing > 0)
3409 :     {
3410 :     my $genus_species = &ext_genus_species($fig,$org);
3411 :     push(@$html,$cgi->h2("$org: $genus_species"));
3412 :     push(@$html,$cgi->ul($cgi->li($missing)));
3413 :     }
3414 : overbeek 1.1 }
3415 :     }
3416 :    
3417 :     sub columns_missing_entries {
3418 :     my($cgi,$subsystem,$org,$roles,$in) = @_;
3419 :    
3420 : overbeek 1.71 my $just_genome = $cgi->param('just_genome');
3421 : overbeek 1.72 if ($just_genome && ($just_genome =~ /(\d+\.\d+)/) && ($org != $1)) { return () }
3422 : overbeek 1.71
3423 : overbeek 1.1 my $just_col = $cgi->param('just_col');
3424 :     my(@really_missing) = ();
3425 :    
3426 :     my($role,%missing_cols);
3427 :     foreach $role (@$roles)
3428 :     {
3429 : parrello 1.127 next if ($just_col && ($role ne $just_col));
3430 :     if ($subsystem->get_pegs_from_cell($org,$role) == 0)
3431 :     {
3432 :     $missing_cols{$role} = 1;
3433 :     }
3434 : overbeek 1.1 }
3435 :    
3436 :     foreach $role (@$roles)
3437 :     {
3438 : parrello 1.127 if ($missing_cols{$role})
3439 :     {
3440 :     my($set);
3441 :     if (($set = $in->{$role}) && (! $cgi->param('ignore_alt')))
3442 :     {
3443 :     my @set = $subsystem->get_subsetC_roles($set);
3444 :    
3445 :     my($k);
3446 :     for ($k=0; ($k < @set) && $missing_cols{$set[$k]}; $k++) {}
3447 :     if ($k == @set)
3448 :     {
3449 :     push(@really_missing,$role);
3450 :     }
3451 :     }
3452 :     else
3453 :     {
3454 :     push(@really_missing,$role);
3455 :     }
3456 :     }
3457 : overbeek 1.1 }
3458 :     return @really_missing;
3459 :     }
3460 :    
3461 :     sub format_missing_including_matches
3462 :     {
3463 :     my($fig,$cgi,$html,$subsystem) = @_;
3464 :     my($org,$abr,$role,$missing);
3465 :    
3466 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
3467 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
3468 :    
3469 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
3470 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
3471 :    
3472 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
3473 :    
3474 : overbeek 1.145 my @alt_sets = grep { ($_ =~ /^\*/) } sort $subsystem->get_subset_namesC;
3475 : overbeek 1.1 my($set,$col,%in);
3476 :     foreach $set (@alt_sets)
3477 :     {
3478 : parrello 1.127 my @mem = grep { $activeC{$_} } $subsystem->get_subsetC_roles($set);
3479 :     foreach $col (@mem)
3480 :     {
3481 :     $in{$col} = $set;
3482 :     }
3483 : overbeek 1.1 }
3484 :     push(@$html,$cgi->h1('To Check Missing Entries:'));
3485 :    
3486 :     push(@$html, $cgi->start_form(-action=> "fid_checked.cgi"));
3487 :    
3488 :     my $can_alter = $cgi->param('can_alter');
3489 :     push(@$html,
3490 : parrello 1.127 $cgi->hidden(-name => 'user', -value => $user, -override => 1),
3491 :     $cgi->hidden(-name => 'can_alter', -value => $can_alter, -override => 1));
3492 : overbeek 1.11
3493 : overbeek 1.14 my $just_role = &which_role($subsystem,$cgi->param('just_role'));
3494 : overbeek 1.70 # print STDERR "There are ", scalar @subsetR, " organisms to check\n";
3495 : overbeek 1.1 foreach $org (@subsetR)
3496 :     {
3497 : parrello 1.127 my @missing = &columns_missing_entries($cgi,$subsystem,$org,\@subsetC,\%in);
3498 :     $missing = [];
3499 :     foreach $role (@missing)
3500 :     {
3501 :     # next if (($_ = $cgi->param('just_role')) && ($_ != ($subsystem->get_role_index($role) + 1)));
3502 :     next if ($just_role && ($just_role ne $role));
3503 :    
3504 :     my @hits = $fig->find_role_in_org($role, $org, $user, $cgi->param("sims_cutoff"));
3505 :     push(@$missing,@hits);
3506 :     }
3507 : overbeek 1.70 # print STDERR "Found ", scalar @$missing, " for $org\n";
3508 : parrello 1.127 if (@$missing > 0)
3509 :     {
3510 :     my $genus_species = &ext_genus_species($fig,$org);
3511 :     push(@$html,$cgi->h2("$org: $genus_species"));
3512 :    
3513 :     my $colhdr = ["Assign", "P-Sc", "PEG", "Len", "Current fn", "Matched peg", "Len", "Function"];
3514 :     my $tbl = [];
3515 :    
3516 :     for my $hit (@$missing)
3517 :     {
3518 :     my($psc, $my_peg, $my_len, $my_fn, $match_peg, $match_len, $match_fn) = @$hit;
3519 :    
3520 :     my $my_peg_link = &HTML::fid_link($cgi, $my_peg, 1);
3521 :     my $match_peg_link = &HTML::fid_link($cgi, $match_peg, 0);
3522 :    
3523 :     my $checkbox = $cgi->checkbox(-name => "checked",
3524 :     -value => "to=$my_peg,from=$match_peg",
3525 :     -label => "");
3526 :    
3527 :     push(@$tbl, [$checkbox,
3528 :     $psc,
3529 :     $my_peg_link, $my_len, $my_fn,
3530 :     $match_peg_link, $match_len, $match_fn]);
3531 :     }
3532 : overbeek 1.1
3533 : parrello 1.127 push(@$html, &HTML::make_table($colhdr, $tbl, ""));
3534 :     }
3535 : overbeek 1.1 }
3536 :     push(@$html,
3537 : parrello 1.127 $cgi->submit(-value => "Process assignments",
3538 :     -name => "batch_assign"),
3539 :     $cgi->end_form);
3540 : overbeek 1.1 }
3541 :    
3542 : mkubal 1.36
3543 :    
3544 :     sub columns_missing_entries {
3545 :     my($cgi,$subsystem,$org,$roles,$in) = @_;
3546 :    
3547 :     next if (($_ = $cgi->param('just_genome')) && ($org != $_));
3548 :     my $just_col = $cgi->param('just_col');
3549 :     my(@really_missing) = ();
3550 :    
3551 :     my($role,%missing_cols);
3552 :     foreach $role (@$roles)
3553 :     {
3554 : parrello 1.127 next if ($just_col && ($role ne $just_col));
3555 :     if ($subsystem->get_pegs_from_cell($org,$role) == 0)
3556 :     {
3557 :     $missing_cols{$role} = 1;
3558 :     }
3559 : mkubal 1.36 }
3560 :    
3561 :     foreach $role (@$roles)
3562 :     {
3563 : parrello 1.127 if ($missing_cols{$role})
3564 :     {
3565 :     my($set);
3566 :     if (($set = $in->{$role}) && (! $cgi->param('ignore_alt')))
3567 :     {
3568 :     my @set = $subsystem->get_subsetC_roles($set);
3569 :    
3570 :     my($k);
3571 :     for ($k=0; ($k < @set) && $missing_cols{$set[$k]}; $k++) {}
3572 :     if ($k == @set)
3573 :     {
3574 :     push(@really_missing,$role);
3575 :     }
3576 :     }
3577 :     else
3578 :     {
3579 :     push(@really_missing,$role);
3580 :     }
3581 :     }
3582 : mkubal 1.36 }
3583 :     return @really_missing;
3584 :     }
3585 :    
3586 :     sub format_missing_including_matches_in_ss
3587 :     {
3588 :     my($fig,$cgi,$html,$subsystem) = @_;
3589 :     my($org,$abr,$role,$missing);
3590 :    
3591 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
3592 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
3593 :    
3594 :     my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
3595 :     my %activeC = map { $_ => 1 } @subsetC;
3596 :    
3597 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
3598 :    
3599 : overbeek 1.145 my @alt_sets = grep { ($_ =~ /^\*/) } sort $subsystem->get_subset_namesC;
3600 : mkubal 1.36 my($set,$col,%in);
3601 :     foreach $set (@alt_sets)
3602 :     {
3603 : parrello 1.127 my @mem = grep { $activeC{$_} } $subsystem->get_subsetC_roles($set);
3604 :     foreach $col (@mem)
3605 :     {
3606 :     $in{$col} = $set;
3607 :     }
3608 : mkubal 1.36 }
3609 :     push(@$html,$cgi->h1('To Check Missing Entries:'));
3610 :    
3611 :     push(@$html, $cgi->start_form(-action=> "fid_checked.cgi"));
3612 :    
3613 :     my $can_alter = $cgi->param('can_alter');
3614 :     push(@$html,
3615 : parrello 1.127 $cgi->hidden(-name => 'user', -value => $user, -override => 1),
3616 :     $cgi->hidden(-name => 'can_alter', -value => $can_alter, -override => 1));
3617 : mkubal 1.36
3618 :     my $just_role = &which_role($subsystem,$cgi->param('just_role'));
3619 :    
3620 :     foreach $org (@subsetR)
3621 :     {
3622 : parrello 1.127 my @missing = &columns_missing_entries($cgi,$subsystem,$org,\@subsetC,\%in);
3623 :     $missing = [];
3624 :     foreach $role (@missing)
3625 :     {
3626 :     # next if (($_ = $cgi->param('just_role')) && ($_ != ($subsystem->get_role_index($role) + 1)));
3627 :     next if ($just_role && ($just_role ne $role));
3628 : mkubal 1.36
3629 : mkubal 1.40 my $flag = 0;
3630 : mkubal 1.48 my $filler;
3631 : mkubal 1.40 my $rdbH = $fig->db_handle;
3632 : olson 1.45 my $q = "SELECT subsystem, role FROM subsystem_index WHERE role = ?";
3633 :     if (my $relational_db_response = $rdbH->SQL($q, 0, $role))
3634 : mkubal 1.40 {
3635 : parrello 1.127 my $pair;
3636 : mkubal 1.40 foreach $pair (@$relational_db_response)
3637 :     {
3638 :     my ($ss, $role) = @$pair;
3639 : mkubal 1.48 #if($ss =="")
3640 :     #{
3641 :     # $filler = 1;
3642 :     #}
3643 :    
3644 : mkubal 1.40 if ($ss !~/Unique/)
3645 :     {
3646 :     $flag = 1;
3647 : parrello 1.127 }
3648 :     }
3649 : mkubal 1.40 }
3650 :    
3651 : mkubal 1.48 if ($flag == 1)
3652 : mkubal 1.40 {
3653 :     my @hits = $fig->find_role_in_org($role, $org, $user, $cgi->param("sims_cutoff"));
3654 : parrello 1.127 push(@$missing,@hits);
3655 :     }
3656 :     }
3657 :    
3658 :     if (@$missing > 0)
3659 :     {
3660 :     my $genus_species = &ext_genus_species($fig,$org);
3661 :     push(@$html,$cgi->h2("$org: $genus_species"));
3662 :    
3663 :     my $colhdr = ["Assign","Sub(s)", "P-Sc", "PEG", "Len", "Current fn", "Matched peg", "Len", "Function"];
3664 :     my $tbl = [];
3665 :    
3666 :     for my $hit (@$missing)
3667 :     {
3668 :     my($psc, $my_peg, $my_len, $my_fn, $match_peg, $match_len, $match_fn) = @$hit;
3669 :     my $my_peg_link = &HTML::fid_link($cgi, $my_peg, 1);
3670 :     my $match_peg_link = &HTML::fid_link($cgi, $match_peg, 0);
3671 :    
3672 :     my $checkbox = $cgi->checkbox(-name => "checked",
3673 :     -value => "to=$my_peg,from=$match_peg",
3674 :     -label => "");
3675 : mkubal 1.48 my $good = 0;
3676 : mkubal 1.40 my @list_of_ss = ();
3677 :     my $ss_table_entry = "none";
3678 : mkubal 1.48
3679 : mkubal 1.40 my (@list_of_returned_ss,$ss_name,$ss_role);
3680 : mkubal 1.48 @list_of_returned_ss = $fig->subsystems_for_peg($match_peg);
3681 : mkubal 1.40 if (@list_of_returned_ss > 0)
3682 :     {
3683 :     for my $ret_ss (@list_of_returned_ss)
3684 :     {
3685 :     ($ss_name,$ss_role)= @$ret_ss;
3686 :     if ($ss_name !~/Unique/)
3687 :     {
3688 : parrello 1.127 $good = 1;
3689 : mkubal 1.48 }
3690 :     }
3691 :     }
3692 : parrello 1.127
3693 : mkubal 1.48 if ($good)
3694 :     {
3695 :     my (@list_of_returned_ss,$ss_name,$ss_role);
3696 :     @list_of_returned_ss = $fig->subsystems_for_peg($my_peg);
3697 :     if (@list_of_returned_ss > 0)
3698 :     {
3699 :     for my $ret_ss (@list_of_returned_ss)
3700 :     {
3701 :     ($ss_name,$ss_role)= @$ret_ss;
3702 :     if ($ss_name !~/Unique/)
3703 :     {
3704 : parrello 1.127 push (@list_of_ss,$ss_name);
3705 : mkubal 1.40 $ss_table_entry = join("<br>",@list_of_ss);
3706 :    
3707 :     }
3708 :     }
3709 :     }
3710 : parrello 1.127
3711 : mkubal 1.48 push(@$tbl, [$checkbox,$ss_table_entry,
3712 : parrello 1.127 $psc,
3713 :     $my_peg_link, $my_len, $my_fn,
3714 :     $match_peg_link, $match_len, $match_fn]);
3715 : mkubal 1.48 }
3716 :    
3717 : parrello 1.127
3718 : mkubal 1.48 }
3719 : mkubal 1.36
3720 : parrello 1.127 push(@$html, &HTML::make_table($colhdr, $tbl, ""));
3721 :     }
3722 : mkubal 1.36 }
3723 :     push(@$html,
3724 : parrello 1.127 $cgi->submit(-value => "Process assignments",
3725 :     -name => "batch_assign"),
3726 :     $cgi->end_form);
3727 : mkubal 1.36 }
3728 :    
3729 :    
3730 : overbeek 1.3 sub format_check_assignments {
3731 :     my($fig,$cgi,$html,$subsystem) = @_;
3732 :     my($org,$role);
3733 :    
3734 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
3735 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
3736 :    
3737 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
3738 : overbeek 1.3 my %activeC = map { $_ => 1 } @subsetC;
3739 :    
3740 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
3741 :    
3742 :     push(@$html,$cgi->h1('Potentially Bad Assignments:'));
3743 :    
3744 :     foreach $org (@subsetR)
3745 :     {
3746 : parrello 1.127 next if (($_ = $cgi->param('just_genome_assignments')) && ($_ != $org));
3747 :     my @bad = ();
3748 :    
3749 :     foreach $role (@subsetC)
3750 :     {
3751 :     next if (($_ = $cgi->param('just_role_assignments')) && ($_ != ($subsystem->get_role_index($role) + 1)));
3752 :     push(@bad,&checked_assignments($cgi,$subsystem,$org,$role));
3753 :     }
3754 : overbeek 1.3
3755 : parrello 1.127 if (@bad > 0)
3756 :     {
3757 :     my $genus_species = &ext_genus_species($fig,$org);
3758 :     push(@$html,$cgi->h2("$org: $genus_species"),
3759 :     $cgi->ul($cgi->li(\@bad)));
3760 :    
3761 :     }
3762 : overbeek 1.3 }
3763 :     push(@$html,$cgi->hr);
3764 :     }
3765 :    
3766 :     sub checked_assignments {
3767 :     my($cgi,$subsystem,$genome,$role) = @_;
3768 :     my($peg,$line1,$line2,@out,$curr,$auto);
3769 :    
3770 :     my(@bad) = ();
3771 :     my @pegs = $subsystem->get_pegs_from_cell($genome,$role);
3772 :     if (@pegs > 0)
3773 :     {
3774 : parrello 1.127 my $tmp = "/tmp/tmp.pegs.$$";
3775 :     open(TMP,">$tmp") || die "could not open $tmp";
3776 :     foreach $peg (@pegs)
3777 :     {
3778 :     print TMP "$peg\n";
3779 :     }
3780 :     close(TMP);
3781 :     my $strict = $cgi->param('strict_check') ? "strict" : "";
3782 :     @out = `$FIG_Config::bin/check_peg_assignments $strict < $tmp 2> /dev/null`;
3783 :     unlink($tmp);
3784 :    
3785 :     while (($_ = shift @out) && ($_ =~ /^(fig\|\d+\.\d+\.peg\.\d+)/))
3786 :     {
3787 :     $peg = $1;
3788 :     if (($line1 = shift @out) && ($line1 =~ /^current:\s+(\S.*\S)/) && ($curr = $1) &&
3789 :     ($line2 = shift @out) && ($line2 =~ /^auto:\s+(\S.*\S)/) && ($auto = $1))
3790 :     {
3791 :     if (! $fig->same_func($curr,$auto))
3792 :     {
3793 :     my $link = &HTML::fid_link($cgi,$peg);
3794 :     push(@bad,"$link<br>$line1<br>$line2<br><br>");
3795 :     }
3796 :     }
3797 :     }
3798 : overbeek 1.3 }
3799 :     return @bad;
3800 :     }
3801 :    
3802 : overbeek 1.1 sub format_dups {
3803 :     my($fig,$cgi,$html,$subsystem) = @_;
3804 :    
3805 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
3806 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
3807 :    
3808 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
3809 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
3810 :    
3811 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
3812 :    
3813 :     push(@$html,$cgi->h1('To Check Duplicates:'));
3814 :    
3815 :     my($org,$duplicates,$role,$genus_species);
3816 :     foreach $org (@subsetR)
3817 :     {
3818 : parrello 1.127 $duplicates = [];
3819 :     foreach $role (@subsetC)
3820 :     {
3821 :     my(@pegs,$peg,$func);
3822 :     if ((@pegs = $subsystem->get_pegs_from_cell($org,$role)) > 1)
3823 :     {
3824 :     push(@$duplicates,"$role<br>" . $cgi->ul($cgi->li([map { $peg = $_; $func = $fig->function_of($peg,$user); &HTML::fid_link($cgi,$peg) . " $func" } @pegs])));
3825 :     }
3826 :     }
3827 :    
3828 :     if (@$duplicates > 0)
3829 :     {
3830 :     $genus_species = &ext_genus_species($fig,$org);
3831 :     push(@$html,$cgi->h2("$org: $genus_species"));
3832 :     push(@$html,$cgi->ul($cgi->li($duplicates)));
3833 :     }
3834 : overbeek 1.1 }
3835 :     }
3836 :    
3837 :     sub format_coupled {
3838 :     my($fig,$cgi,$html,$subsystem,$type) = @_;
3839 :     my($i,$j,@show,$user,$org,$link,$gs,$func,$peg,$peg1,$peg2,%in,%seen,%seen2);
3840 :     my(@cluster,$sc,$x,$id2,@in,$sim,@coupled);
3841 :     my($org,$role);
3842 :    
3843 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
3844 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
3845 :    
3846 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
3847 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
3848 :    
3849 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
3850 :    
3851 :     foreach $org (@subsetR)
3852 :     {
3853 : parrello 1.127 foreach $role (@subsetC)
3854 :     {
3855 :     push(@in,$subsystem->get_pegs_from_cell($org,$role));
3856 :     }
3857 : overbeek 1.1 }
3858 :    
3859 :     %in = map { $_ => 1 } @in;
3860 :     @show = ();
3861 :     foreach $peg1 (@in)
3862 :     {
3863 : parrello 1.127 if ($type eq "careful")
3864 :     {
3865 :     @coupled = $fig->coupling_and_evidence($peg1,5000,1.0e-10,0.2,1);
3866 :     }
3867 :     else
3868 :     {
3869 :     @coupled = $fig->fast_coupling($peg1,5000,1);
3870 :     }
3871 :    
3872 :     foreach $x (@coupled)
3873 :     {
3874 :     ($sc,$peg2) = @$x;
3875 :     if ((! $in{$peg2}) && ((! $seen{$peg2}) || ($seen{$peg2} < $sc)))
3876 :     {
3877 :     $seen{$peg2} = $sc;
3878 :     # print STDERR "$sc\t$peg1 -> $peg2\n";
3879 :     }
3880 :     }
3881 : overbeek 1.1 }
3882 : parrello 1.127
3883 : overbeek 1.1 foreach $peg1 (sort { $seen{$b} <=> $seen{$a} } keys(%seen))
3884 :     {
3885 : parrello 1.127 if (! $seen2{$peg1})
3886 :     {
3887 :     @cluster = ($peg1);
3888 :     $seen2{$peg1} = 1;
3889 :     for ($i=0; ($i < @cluster); $i++)
3890 :     {
3891 :     foreach $sim ($fig->sims($cluster[$i],1000,1.0e-10,"fig"))
3892 :     {
3893 :     $id2 = $sim->id2;
3894 :     if ($seen{$id2} && (! $seen2{$id2}))
3895 :     {
3896 :     push(@cluster,$id2);
3897 :     $seen2{$id2} = 1;
3898 :     }
3899 :     }
3900 :     }
3901 :     push(@show, [scalar @cluster,
3902 :     $cgi->br .
3903 :     $cgi->ul($cgi->li([map { $peg = $_;
3904 :     $sc = $seen{$peg};
3905 :     $func = $fig->function_of($peg,$user);
3906 :     $gs = $fig->genus_species($fig->genome_of($peg));
3907 :     $link = &HTML::fid_link($cgi,$peg);
3908 :     "$sc: $link: $func \[$gs\]" }
3909 :     sort { $seen{$b} <=> $seen{$a} }
3910 :     @cluster]))
3911 :     ]);
3912 :     }
3913 : overbeek 1.1 }
3914 :    
3915 :     if (@show > 0)
3916 :     {
3917 : parrello 1.127 @show = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @show;
3918 :     push(@$html,$cgi->h1('Coupled, but not in Spreadsheet:'));
3919 :     push(@$html,$cgi->ul($cgi->li(\@show)));
3920 : overbeek 1.1 }
3921 :     }
3922 :    
3923 : golsen 1.184 # Former behavior would convert Environmental Sample to E (for Eukaryota).
3924 :     # -- GJO
3925 :    
3926 : overbeek 1.1 sub ext_genus_species {
3927 : golsen 1.184 my( $fig, $genome ) = @_;
3928 : overbeek 1.1
3929 : golsen 1.186 my ( $gs, $c ) = $fig->genus_species_domain( $genome );
3930 : golsen 1.184 $c = ( $c =~ m/^Environ/i ) ? 'M' : substr($c, 0, 1); # M for metagenomic
3931 : overbeek 1.1 return "$gs [$c]";
3932 :     }
3933 : golsen 1.184
3934 :    
3935 : overbeek 1.1 sub show_tree {
3936 :    
3937 :     my($id,$gs);
3938 :     my($tree,$ids) = $fig->build_tree_of_complete;
3939 :     my $relabel = {};
3940 :     foreach $id (@$ids)
3941 :     {
3942 : parrello 1.127 if ($gs = $fig->genus_species($id))
3943 :     {
3944 :     $relabel->{$id} = "$gs ($id)";
3945 :     }
3946 : overbeek 1.1 }
3947 :     $_ = &display_tree($tree,$relabel);
3948 :     print $cgi->pre($_),"\n";
3949 :     }
3950 :    
3951 :     sub export_align_input
3952 :     {
3953 :    
3954 :     }
3955 :    
3956 : redwards 1.22 sub annotate_column {
3957 :     # RAE: I added this function to allow you to reannotate a single column all at once
3958 :     # this is because I wanted to update some of my annotations after looking at UniProt
3959 :     # and couldn't see an easy way to do it.
3960 :     my($fig,$cgi,$html,$col,$subsystem) = @_;
3961 :     my $checked;
3962 :     my $roles = [$subsystem->get_roles];
3963 : overbeek 1.76 my $role = &which_role_for_column($col,$roles);
3964 : overbeek 1.155 my @checked = &seqs_to_align($cgi,$role,$subsystem);
3965 : redwards 1.22 return undef unless (@checked);
3966 :    
3967 :     # the following is read from fid_checked.cgi
3968 :     push( @$html, "<table border=1>\n",
3969 : parrello 1.127 "<tr><td>Protein</td><td>Organism</td><td>Current Function</td><td>By Whom</td></tr>"
3970 :     );
3971 : redwards 1.22
3972 :     foreach my $peg ( @checked ) {
3973 :     my @funcs = $fig->function_of( $peg );
3974 :     if ( ! @funcs ) { @funcs = ( ["", ""] ) }
3975 :     my $nfunc = @funcs;
3976 : parrello 1.127 my $org = $fig->org_of( $peg );
3977 :     push( @$html, "<tr>",
3978 :     "<td rowspan=$nfunc>$peg</td>",
3979 :     "<td rowspan=$nfunc>$org</td>"
3980 :     );
3981 :     my ($who, $what);
3982 :     push( @$html, join( "</tr>\n<tr>", map { ($who,$what) = @$_; "<td>$what</td><td>$who</td>" } @funcs ) );
3983 :     push( @$html, "</tr>\n" );
3984 : redwards 1.22 }
3985 :     push( @$html, "</table>\n" );
3986 :    
3987 :     push( @$html, $cgi->start_form(-action => "fid_checked.cgi", -target=>"_blank"),
3988 :     $cgi->br, $cgi->br,
3989 :     "<table>\n",
3990 :     "<tr><td>New Function:</td>",
3991 :     "<td>", $cgi->textfield(-name => "function", -size => 60), "</td></tr>",
3992 :     "<tr><td colspan=2>", $cgi->hr, "</td></tr>",
3993 :     "<tr><td>New Annotation:</td>",
3994 :     "<td rowspan=2>", $cgi->textarea(-name => "annotation", -rows => 30, -cols => 60), "</td></tr>",
3995 : parrello 1.127 "<tr><td valign=top width=20%><br>", $cgi->submit('add annotation'),
3996 :     "<p><b>Please note:</b> At the moment you need to make sure that the annotation in the table at the ",
3997 :     "top of this page reflects the new annotation. This may not be updated automatically.</p>",
3998 :     "</td></tr>",
3999 : redwards 1.22 "</table>",
4000 :     $cgi->hidden(-name => 'user', -value => $user),
4001 :     $cgi->hidden(-name => 'checked', -value => [@checked]),
4002 :     $cgi->end_form
4003 :     );
4004 :     }
4005 :    
4006 :    
4007 : overbeek 1.76
4008 : overbeek 1.1 sub align_column {
4009 : overbeek 1.76 my($fig,$cgi,$html,$colN,$subsystem) = @_;
4010 :     my(@pegs,$peg,$pseq,$role);
4011 : overbeek 1.1
4012 :     my $roles = [$subsystem->get_roles];
4013 : overbeek 1.76 my $name = $subsystem->get_name;
4014 :     &check_index("$FIG_Config::data/Subsystems/$name/Alignments",$roles);
4015 :     if (($role = &which_role_for_column($colN,$roles)) &&
4016 : overbeek 1.155 ((@pegs = &seqs_to_align($cgi,$role,$subsystem)) > 1))
4017 : overbeek 1.76 {
4018 : parrello 1.127 my $tmpF = "/tmp/seqs.fasta.$$";
4019 :     open(TMP,">$tmpF") || die "could not open $tmpF";
4020 : redwards 1.22
4021 : parrello 1.127 foreach $peg (@pegs)
4022 :     {
4023 :     if ($pseq = $fig->get_translation($peg))
4024 :     {
4025 :     $pseq =~ s/[uU]/x/g;