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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.101 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3