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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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