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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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