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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.168 - (view) (download)

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