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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3