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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.142 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3