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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.207 - (view) (download)

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