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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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