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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3