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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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