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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.73 - (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.64
728 : golsen 1.73 if ( $can_alter ) { format_extend_with($fig,$cgi,$html,$subsystem) }
729 :    
730 :     push @$html, "<TABLE width=\"100%\">\n",
731 :     " <TR>\n",
732 :     ($can_alter) ? " <TD>" . $cgi->checkbox(-name => 'precise_fill', -value => 1, -checked => 0, -override => 1,-label => 'fill') . "</TD>\n" : (),
733 :     " <TD><a href=\"/FIG/Html/conflict_resolution.html\" class=\"help\" target=\"help\">Help on conflict resolution</a></TD>\n",
734 :     " <TD><a href=\"/FIG/Html/seedtips.html#edit_variants\" class=\"help\" target=\"help\">Help on editing variants</a></TD>\n",
735 :     " <TD><a href=\"ss_export.cgi?user=$user&ssa_name=$ssa\" class=\"help\">Export subsystem data</a></TD>\n",
736 :     " </TR>\n",
737 :     "</TABLE>\n";
738 : redwards 1.64
739 :     if ($can_alter)
740 :     {
741 :     push(@$html,$cgi->submit('update spreadsheet'),$cgi->br);
742 : overbeek 1.1 }
743 :     else
744 :     {
745 :     push(@$html,$cgi->br);
746 :     push(@$html,$cgi->submit('show spreadsheet'),$cgi->br);
747 :     }
748 : redwards 1.64
749 : redwards 1.24 push(@$html,$cgi->checkbox(-name => 'ignore_alt', -value => 1, -override => 1, -label => 'ignore alternatives', -checked => ($cgi->param('ignore_alt'))),$cgi->br);
750 : overbeek 1.17 push(@$html,$cgi->checkbox(-name => 'ext_ids', -value => 1, -checked => 0, -label => 'use external ids'),$cgi->br);
751 : overbeek 1.60 push(@$html,$cgi->checkbox(-name => 'show_clusters', -value => 1, -checked => 0,-label => 'show clusters'),$cgi->br);
752 : redwards 1.52 my $opt=$fig->get_tags("genome"); # all the tags we know about
753 :     my @options=sort {uc($a) cmp uc($b)} keys %$opt;
754 :     unshift(@options, undef); # a blank field at the start
755 : redwards 1.54 push(@$html,"color rows by each organism's attribute: &nbsp; ", $cgi->popup_menu(-name => 'color_by_ga', -values=>\@options), $cgi->br);
756 :    
757 :     $opt=$fig->get_tags("peg"); # all the peg tags
758 :     @options=sort {uc($a) cmp uc($b)} keys %$opt;
759 :     unshift(@options, undef);
760 :     push(@$html,"color columns by each PEGs attribute: &nbsp; ", $cgi->popup_menu(-name => 'color_by_peg_tag', -values=>\@options), $cgi->br);
761 :    
762 : overbeek 1.1 push(@$html,$cgi->checkbox(-name => 'show_missing', -value => 1, -checked => 0, -override => 1,-label => 'show missing'),$cgi->br);
763 : overbeek 1.3
764 : overbeek 1.1 push(@$html,$cgi->checkbox(-name => 'show_missing_including_matches', -value => 1, -checked => 0, -override => 1,-label => 'show missing with matches'),
765 :     "&nbsp; &nbsp; [To restrict to a single genome: ",
766 :     $cgi->textfield(-name => "just_genome", -size => 15),"]",
767 :     "&nbsp; &nbsp; [To restrict to a single role: ",
768 :     $cgi->textfield(-name => "just_role", -size => 15),"]",
769 : overbeek 1.3 $cgi->br,$cgi->br
770 :     );
771 :    
772 : mkubal 1.36
773 :     push(@$html,$cgi->checkbox(-name => 'show_missing_including_matches_in_ss', -value => 1, -checked => 0, -override => 1,-label => 'show missing with matches in ss'),
774 :     "&nbsp; &nbsp; [To restrict to a single genome: ",
775 :     $cgi->textfield(-name => "just_genome", -size => 15),"]",
776 :     "&nbsp; &nbsp; [To restrict to a single role: ",
777 :     $cgi->textfield(-name => "just_role", -size => 15),"]",
778 :     $cgi->br,$cgi->br
779 :     );
780 :    
781 :    
782 : overbeek 1.3 push(@$html,$cgi->checkbox(-name => 'check_assignments', -value => 1, -checked => 0, -override => 1,-label => 'check assignments'),
783 :     '&nbsp;&nbsp;[',
784 :     $cgi->checkbox(-name => 'strict_check', -value => 1, -checked => 0, -override => 1,-label => 'strict'),
785 :     ']',
786 :     "&nbsp; &nbsp; [To restrict to a single genome: ",
787 :     $cgi->textfield(-name => "just_genome_assignments", -size => 15),"]",
788 :     "&nbsp; &nbsp; [To restrict to a single role: ",
789 :     $cgi->textfield(-name => "just_role_assignments", -size => 15),"]",
790 :     $cgi->br.$cgi->br
791 : overbeek 1.1 );
792 : overbeek 1.3
793 : overbeek 1.14 if ($can_alter)
794 :     {
795 :     push(@$html,$cgi->checkbox(-name => 'refill', -value => 1, -checked => 0, -override => 1,-label => 'refill spreadsheet from scratch'),$cgi->br);
796 :     }
797 :    
798 : overbeek 1.1 push(@$html,$cgi->checkbox(-name => 'show_dups', -value => 1, -checked => 0, -override => 1,-label => 'show duplicates'),$cgi->br);
799 :     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);
800 : overbeek 1.14 if ($can_alter)
801 :     {
802 :     push(@$html,$cgi->checkbox(-name => 'add_solid', -value => 1, -checked => 0, -override => 1,-label => 'Add Genomes with Solid Hits'),$cgi->br);
803 :     }
804 :    
805 : 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);
806 :     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);
807 : redwards 1.63 # RAE Hide -1 variants
808 :     push(@$html,$cgi->checkbox(-name => 'hide_minus1', -value=> 1, -checked => 0, -label => 'hide -1 variants'),$cgi->br);
809 : overbeek 1.37 push(@$html,$cgi->br,"Align column (specify the number of the column): ",
810 : overbeek 1.1 $cgi->textfield(-name => "col_to_align", -size => 7),
811 :     $cgi->checkbox(-name => "show_align_input", -checked => 0,
812 :     -label => "show input to alignment tool"),
813 :     $cgi->br,"Include homologs that pass the following threshhold: ",
814 :     $cgi->textfield(-name => "include_homo", -size => 10)," (leave blank to see just column)",
815 :     " Max homologous seqs: ",$cgi->textfield(-name => "max_homo", -value => 100, -size => 6),
816 :     );
817 : redwards 1.22
818 :     # RAE: A new function to reannotate a single column
819 :     # I don't understand how you get CGI.pm to reset (and never have).
820 :     # $cgi->delete("col_to_annotate"); # this does nothing to my script and there is always the last number in this box
821 :     #push(@$html, $cgi->br,"Change annotation for column: ", $cgi->textfield(-name => "col_to_annotate", -size => 7));
822 :     push(@$html, $cgi->br,"Change annotation for column: ", '<input type="text" name="col_to_annotate" value="" size="7">');
823 : overbeek 1.1
824 :     if ($can_alter)
825 :     {
826 :     push(@$html,
827 : overbeek 1.20 $cgi->p. $cgi->hr."If you wish to check the subsystem, ",
828 :     $cgi->a({href => "check_subsys.cgi?user=$user&subsystem=$ssa&request=check_ssa"},
829 :     "click here"),
830 : overbeek 1.49 # $cgi->br,
831 :     # $cgi->p. $cgi->hr."If you wish to reset variants for the subsystem, ",
832 :     # $cgi->a({href => "set_variants.cgi?user=$user&subsystem=$ssa&request=show_variants",target => "set_variants"},
833 :     # "click here"),
834 : overbeek 1.20 $cgi->br,
835 : overbeek 1.1 $cgi->p,
836 : overbeek 1.9 $cgi->hr,
837 :     "You should resynch PEG connections only if you detect PEGs that should be connected to the
838 :     spreadsheet, but do not seem to be. This can only reflect an error in the code. If you find
839 :     yourself having to use it, send mail to Ross.",
840 :     $cgi->br,
841 :     $cgi->submit(-value => "Resynch PEG Connections",
842 :     -name => "resynch_peg_connections"),
843 :     $cgi->br,
844 : overbeek 1.1 $cgi->submit(-value => "Start automated subsystem extension",
845 :     -name => "extend_with_billogix"),
846 :     $cgi->br);
847 :     }
848 : overbeek 1.10
849 : overbeek 1.12 my $notes = $subsystem->get_notes();
850 : overbeek 1.14 if ($can_alter)
851 :     {
852 :     push(@$html,$cgi->hr,"NOTES:\n",$cgi->br,$cgi->textarea(-name => 'notes', -rows => 40, -cols => 100, -value => $notes));
853 :     }
854 :     elsif ($notes)
855 :     {
856 : redwards 1.31 push(@$html,$cgi->h2('notes'),"<pre width=80>$notes</pre>");
857 : overbeek 1.14 }
858 : overbeek 1.10
859 : redwards 1.41 # RAE Modified to add a line with the classification
860 :     my $class=$subsystem->get_classification();
861 :     if ($can_alter)
862 :     {
863 :     push(@$html, $cgi->hr, "CLASSIFICATION:\n", $cgi->textfield(-name=>"classif1", -value=>$$class[0], -size=>40),
864 :     $cgi->textfield(-name=>"classif2", -value=>$$class[1], -size=>40));
865 :     }
866 :     elsif ($class)
867 :     {
868 : redwards 1.42 push (@$html, $cgi->h2('Classification'), "<table><tr><td>$$class[0]</td><td>$$class[1]</td></tr></table>\n");
869 : redwards 1.41 }
870 :    
871 : overbeek 1.1 push(@$html, $cgi->end_form);
872 :    
873 : overbeek 1.19 my $target = "align$$";
874 :     my @roles = $subsystem->get_roles;
875 :     my $rolesA = [];
876 :     my $i;
877 :     my $dir = $subsystem->get_dir;
878 :     for ($i=1; ($i <= @roles); $i++)
879 :     {
880 :     if (-s "$dir/Alignments/$i/tree")
881 :     {
882 :     push(@$rolesA,"$i: $roles[$i-1]");
883 :     }
884 :     }
885 :     if (@$rolesA > 0)
886 :     {
887 :     push(@$html, $cgi->hr,
888 :     $cgi->h1('To Assign Using a Tree'),
889 :     $cgi->start_form(-action => "assign_using_tree.cgi",
890 :     -target => $target,
891 :     -method => 'post'),
892 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
893 :     $cgi->hidden(-name => 'ali_dir', -value => "$dir/Alignments", -override => 1),
894 :     $cgi->scrolling_list(-name => 'ali_num',
895 :     -values => $rolesA,
896 :     -size => 10,
897 :     -multiple => 0
898 :     ),
899 :     $cgi->br,
900 :     $cgi->submit(-value => "use_tree",
901 :     -name => "use_tree"),
902 :     $cgi->end_form
903 :     );
904 :     }
905 :    
906 : overbeek 1.1 push(@$html, $cgi->hr);
907 :    
908 :     if ($cgi->param('show_missing'))
909 :     {
910 :     &format_missing($fig,$cgi,$html,$subsystem);
911 :     }
912 :    
913 :     if ($cgi->param('show_missing_including_matches'))
914 :     {
915 :     &format_missing_including_matches($fig,$cgi,$html,$subsystem);
916 :     }
917 : mkubal 1.36 if ($cgi->param('show_missing_including_matches_in_ss'))
918 :     {
919 :     &format_missing_including_matches_in_ss($fig,$cgi,$html,$subsystem);
920 :     }
921 :    
922 : overbeek 1.1
923 : overbeek 1.3 if ($cgi->param('check_assignments'))
924 :     {
925 :     &format_check_assignments($fig,$cgi,$html,$subsystem);
926 :     }
927 :    
928 : overbeek 1.1 if ($cgi->param('show_dups'))
929 :     {
930 :     &format_dups($fig,$cgi,$html,$subsystem);
931 :     }
932 :    
933 :     if ($cgi->param('show_coupled'))
934 :     {
935 :     &format_coupled($fig,$cgi,$html,$subsystem,"careful");
936 :     }
937 :     elsif ($cgi->param('show_coupled_fast'))
938 :     {
939 :     &format_coupled($fig,$cgi,$html,$subsystem,"fast");
940 :     }
941 :    
942 :     my $col;
943 :     if ($col = $cgi->param('col_to_align'))
944 :     {
945 :     &align_column($fig,$cgi,$html,$col,$subsystem);
946 :     }
947 : redwards 1.22 elsif ($col = $cgi->param('col_to_annotate'))
948 :     {
949 :     &annotate_column($fig,$cgi,$html,$col,$subsystem);
950 :     }
951 : overbeek 1.1
952 :     }
953 :    
954 : golsen 1.29
955 :     #-----------------------------------------------------------------------------
956 :     # Selection list of complete genomes not in spreadsheet:
957 :     #-----------------------------------------------------------------------------
958 :    
959 : overbeek 1.1 sub format_extend_with {
960 : golsen 1.29 my( $fig, $cgi, $html, $subsystem ) = @_;
961 : overbeek 1.1
962 :     my %genomes = map { $_ => 1 } $subsystem->get_genomes();
963 :    
964 : golsen 1.44 #
965 :     # Use $fig->genomes( complete, restricted, domain ) to get org list:
966 :     #
967 :     my $req_comp = $cgi->param( 'complete' ) || 'Only "complete"';
968 :     my $complete = ( $req_comp =~ /^all$/i ) ? undef : "complete";
969 :     my @orgs = map { [ $_ , &ext_genus_species( $fig, $_ ) ] }
970 :     grep { ! $genomes{ $_ } }
971 :     $fig->genomes( $complete, undef );
972 : golsen 1.29
973 : golsen 1.44 #
974 :     # Put it in the order requested by the user:
975 :     #
976 : golsen 1.29 my $pick_order = $cgi->param('pick_order') || 'Alphabetic';
977 :     if ( $pick_order eq "Phylogenetic" )
978 :     {
979 :     @orgs = sort { $a->[2] cmp $b->[2] }
980 :     map { push @$_, $fig->taxonomy_of( $_->[0] ); $_ }
981 :     @orgs;
982 :     }
983 :     elsif ( $pick_order eq "Genome ID" )
984 :     {
985 :     @orgs = sort { $a->[2]->[0] <=> $b->[2]->[0] || $a->[2]->[1] <=> $b->[2]->[1] }
986 :     map { push @$_, [ split /\./ ]; $_ }
987 :     @orgs;
988 :     }
989 :     else
990 :     {
991 :     $pick_order = 'Alphabetic';
992 :     @orgs = sort { $a->[1] cmp $b->[1] } @orgs;
993 :     }
994 : overbeek 1.1
995 : golsen 1.29 @orgs = map { "$_->[1] ($_->[0])" } @orgs;
996 :    
997 : golsen 1.44 #
998 :     # Radio buttons to let the user choose the order they want for the list:
999 :     #
1000 : golsen 1.29 my @order_opt = $cgi->radio_group( -name => 'pick_order',
1001 :     -values => [ 'Alphabetic', 'Phylogenetic', 'Genome ID' ],
1002 :     -default => $pick_order,
1003 :     -override => 1
1004 :     );
1005 :    
1006 : golsen 1.44 #
1007 :     # Radio buttons to let the user choose to include incomplete genomes:
1008 :     #
1009 :     my @complete = $cgi->radio_group( -name => 'complete',
1010 :     -default => $req_comp,
1011 :     -override => 1,
1012 :     -values => [ 'All', 'Only "complete"' ]
1013 :     );
1014 :    
1015 :     #
1016 :     # Display the pick list, and options:
1017 :     #
1018 : golsen 1.29 push( @$html, $cgi->h1('Pick Organisms to Extend with'), "\n",
1019 :     "<TABLE>\n",
1020 :     " <TR>\n",
1021 :     " <TD>",
1022 : golsen 1.30 $cgi->scrolling_list( -name => 'new_genome',
1023 : golsen 1.29 -values => [ @orgs ],
1024 :     -size => 10,
1025 :     -multiple => 1
1026 :     ),
1027 :     " </TD>\n",
1028 : golsen 1.44 " <TD>",
1029 :     join( "<BR>\n", "<b>Order of selection list:</b>", @order_opt,
1030 :     "<b>Completeness?</b>", @complete
1031 :     ), "\n",
1032 : golsen 1.29 " </TD>\n",
1033 :     " </TR>\n",
1034 :     "</TABLE>\n",
1035 :     $cgi->hr
1036 :     );
1037 : overbeek 1.1 }
1038 :    
1039 : golsen 1.29
1040 : overbeek 1.1 sub format_roles {
1041 : overbeek 1.14 my($fig,$cgi,$html,$subsystem,$can_alter) = @_;
1042 : overbeek 1.1 my($i);
1043 :    
1044 :     my $col_hdrs = ["Column","Abbrev","Functional Role"];
1045 :     my $tab = [];
1046 :    
1047 :     my $n = 1;
1048 : overbeek 1.14 &format_existing_roles($fig,$cgi,$html,$subsystem,$tab,\$n,$can_alter);
1049 : overbeek 1.1 if ($cgi->param('can_alter'))
1050 :     {
1051 :     for ($i=0; ($i < 5); $i++)
1052 :     {
1053 : overbeek 1.15 &format_role($fig,$cgi,$html,$subsystem,$tab,$n,"",$can_alter);
1054 : overbeek 1.1 $n++;
1055 :     }
1056 :     }
1057 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Functional Roles"),
1058 :     $cgi->hr
1059 :     );
1060 :     }
1061 :    
1062 :     sub format_existing_roles {
1063 : overbeek 1.14 my($fig,$cgi,$html,$subsystem,$tab,$nP,$can_alter) = @_;
1064 : overbeek 1.1 my($role);
1065 :    
1066 :     foreach $role ($subsystem->get_roles)
1067 :     {
1068 : overbeek 1.14 &format_role($fig,$cgi,$html,$subsystem,$tab,$$nP,$role,$can_alter);
1069 : overbeek 1.1 $$nP++;
1070 :     }
1071 :     }
1072 :    
1073 :     sub format_role {
1074 : overbeek 1.14 my($fig,$cgi,$html,$subsystem,$tab,$n,$role,$can_alter) = @_;
1075 : overbeek 1.1 my($abbrev);
1076 :    
1077 :     $abbrev = $role ? $subsystem->get_role_abbr($subsystem->get_role_index($role)) : "";
1078 :    
1079 :     my($posT,$abbrevT,$roleT);
1080 : overbeek 1.14 if ($can_alter)
1081 : overbeek 1.1 {
1082 :     $posT = $cgi->textfield(-name => "posR$n", -size => 3, -value => $n, -override => 1);
1083 :     $abbrevT = $cgi->textfield(-name => "abbrev$n", -size => 7, -value => $abbrev, -override => 1);
1084 :     $roleT = $cgi->textfield(-name => "role$n", -size => 80, -value => $role, -override => 1);
1085 :     }
1086 :     else
1087 :     {
1088 :     push(@$html,$cgi->hidden(-name => "posR$n", -value => $n, -override => 1),
1089 :     $cgi->hidden(-name => "abbrev$n", -value => $abbrev, -override => 1),
1090 :     $cgi->hidden(-name => "role$n", -value => $role, -override => 1));
1091 :     $posT = $n;
1092 :     $abbrevT = $abbrev;
1093 :     $roleT = $role;
1094 :     }
1095 :     #
1096 :     # Wrap the first element in the table with a <A NAME="role_rolename"> tag
1097 :     # so we can zing to it from elsewhere. We remove any non-alphanumeric
1098 :     # chars in the role name.
1099 :     #
1100 :    
1101 :     my $posT_html;
1102 :     {
1103 :     my $rn = $role;
1104 :     $rn =~ s/[ \/]/_/g;
1105 :     $rn =~ s/\W//g;
1106 :    
1107 :     $posT_html = "<a name=\"$rn\">$posT</a>";
1108 :     }
1109 :    
1110 :    
1111 :     push(@$tab,[$posT_html,$abbrevT,$roleT]);
1112 :    
1113 :     if ($cgi->param('check_problems'))
1114 :     {
1115 :     my @roles = grep { $_->[0] ne $role } &gene_functions_in_col($fig,$role,$subsystem);
1116 :     my($x,$peg);
1117 :     foreach $x (@roles)
1118 :     {
1119 :     push(@$tab,["","",$x->[0]]);
1120 :     push(@$tab,["","",join(",",map { &HTML::fid_link($cgi,$_) } @{$x->[1]})]);
1121 :     }
1122 :     }
1123 :     }
1124 :    
1125 :     sub gene_functions_in_col {
1126 :     my($fig,$role,$subsystem) = @_;
1127 :     my(%roles,$peg,$func);
1128 : redwards 1.21
1129 :    
1130 :     # RAE this is dying if $subsystem->get_col($subsystem->get_role_index($role) + 1) is not defined
1131 :     # it is also not returning the right answer, so we need to fix it.
1132 :     # I am not sure why this is incremented by one here (see the note) because it is not right
1133 :     # and if you don't increment it by one it is right.
1134 :    
1135 :     # incr by 1 to get col indexed from 1 (not 0)
1136 :     #my @pegs = map { @$_ } @{$subsystem->get_col($subsystem->get_role_index($role) + 1)};
1137 :    
1138 :     return undef unless ($role); # this takes care of one error
1139 :     my $col_role=$subsystem->get_col($subsystem->get_role_index($role));
1140 :     return undef unless (defined $col_role);
1141 :     my @pegs = map { @$_ } @$col_role;
1142 : overbeek 1.1
1143 :     foreach $peg (@pegs)
1144 :     {
1145 :     if ($func = $fig->function_of($peg))
1146 :     {
1147 :     push(@{$roles{$func}},$peg);
1148 :     }
1149 :     }
1150 :     return map { [$_,$roles{$_}] } sort keys(%roles);
1151 :     }
1152 :    
1153 :     sub format_subsets {
1154 : overbeek 1.14 my($fig,$cgi,$html,$subsystem,$can_alter) = @_;
1155 : overbeek 1.1
1156 : overbeek 1.14 &format_subsetsC($fig,$cgi,$html,$subsystem,$can_alter);
1157 : overbeek 1.1 &format_subsetsR($fig,$cgi,$html,$subsystem);
1158 :     }
1159 :    
1160 :     sub format_subsetsC {
1161 : overbeek 1.14 my($fig,$cgi,$html,$subsystem,$can_alter) = @_;
1162 : overbeek 1.1
1163 :     my $col_hdrs = ["Subset","Includes These Roles"];
1164 :     my $tab = [];
1165 :    
1166 :     my $n = 1;
1167 : overbeek 1.14 &format_existing_subsetsC($cgi,$html,$subsystem,$tab,\$n,$can_alter);
1168 : overbeek 1.9
1169 : overbeek 1.14 if ($can_alter)
1170 : overbeek 1.1 {
1171 :     my $i;
1172 :     for ($i=0; ($i < 5); $i++)
1173 :     {
1174 :     &format_subsetC($cgi,$html,$subsystem,$tab,$n,"");
1175 :     $n++;
1176 :     }
1177 :     }
1178 : overbeek 1.9
1179 : overbeek 1.1 push(@$html,&HTML::make_table($col_hdrs,$tab,"Subsets of Roles"),
1180 :     $cgi->hr
1181 :     );
1182 :    
1183 :     my @subset_names = $subsystem->get_subset_namesC;
1184 :     if (@subset_names > 1)
1185 :     {
1186 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
1187 :     push(@$html,$cgi->scrolling_list(-name => 'active_subsetC',
1188 : overbeek 1.8 -values => [@subset_names],
1189 : overbeek 1.1 -default => $active_subsetC
1190 :     ),
1191 :     $cgi->br
1192 :     );
1193 :     }
1194 :     else
1195 :     {
1196 :     push(@$html,$cgi->hidden(-name => 'active_subsetC', -value => 'All', -override => 1));
1197 :     }
1198 :     }
1199 :    
1200 :     sub format_subsetsR {
1201 :     my($fig,$cgi,$html,$subsystem) = @_;
1202 :     my($i);
1203 :    
1204 :     my $link = &tree_link;
1205 :     push(@$html,$cgi->br,$link,$cgi->br);
1206 :    
1207 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
1208 :    
1209 :     my @tmp = grep { $_ ne "All" } sort $subsystem->get_subset_namesR;
1210 :     push(@$html,$cgi->scrolling_list(-name => 'active_subsetR',
1211 :     -values => ["All",@tmp],
1212 :     -default => $active_subsetR,
1213 :     -size => 5
1214 :     ),
1215 :     $cgi->br
1216 :     );
1217 :     }
1218 :    
1219 :     sub format_existing_subsetsC {
1220 : overbeek 1.14 my($cgi,$html,$subsystem,$tab,$nP,$can_alter) = @_;
1221 : overbeek 1.1 my($nameCS);
1222 :    
1223 :     foreach $nameCS (sort $subsystem->get_subset_namesC)
1224 :     {
1225 : overbeek 1.9 if ($nameCS !~ /all/i)
1226 :     {
1227 :     &format_subsetC($cgi,$html,$subsystem,$tab,$$nP,$nameCS);
1228 :     $$nP++;
1229 :     }
1230 : overbeek 1.1 }
1231 :     }
1232 :    
1233 :     sub format_subsetC {
1234 :     my($cgi,$html,$subsystem,$tab,$n,$nameCS) = @_;
1235 :    
1236 :     if ($nameCS ne "All")
1237 :     {
1238 : overbeek 1.4 my $subset = $nameCS ? join(",",map { $subsystem->get_role_index($_) + 1 } $subsystem->get_subsetC_roles($nameCS)) : "";
1239 : overbeek 1.9
1240 :     $nameCS = $subset ? $nameCS : "";
1241 :    
1242 : overbeek 1.1 my($posT,$subsetT);
1243 : overbeek 1.9
1244 : overbeek 1.14 $posT = $cgi->textfield(-name => "nameCS$n", -size => 30, -value => $nameCS, -override => 1);
1245 :     $subsetT = $cgi->textfield(-name => "subsetC$n", -size => 80, -value => $subset, -override => 1);
1246 :     push(@$tab,[$posT,$subsetT]);
1247 : overbeek 1.1 }
1248 :     }
1249 :    
1250 :     sub tree_link {
1251 :     my $target = "window$$";
1252 :     my $url = &FIG::cgi_url . "/subsys.cgi?request=show_tree";
1253 :     return "<a href=$url target=$target>Show Phylogenetic Tree</a>";
1254 :     }
1255 :    
1256 :     sub format_rows {
1257 : redwards 1.32 my($fig,$cgi,$html,$subsystem, $tagvalcolor) = @_;
1258 : overbeek 1.1 my($i,%alternatives);
1259 :    
1260 :     my $ignore_alt = $cgi->param('ignore_alt');
1261 :    
1262 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
1263 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
1264 :    
1265 : redwards 1.59 # RAE:
1266 :     # added this to allow determination of an active_subsetR based on a tag value pair
1267 :     if ($cgi->param('active_key'))
1268 :     {
1269 :     $active_subsetR = $cgi->param('active_key');
1270 :     my $active_value = undef;
1271 :     $active_value = $cgi->param('active_value') if ($cgi->param('active_value'));
1272 :     $subsystem->load_row_subsets_by_kv($active_subsetR, $active_value);
1273 :     $subsystem->set_active_subsetR($active_subsetR);
1274 :     }
1275 :    
1276 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
1277 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
1278 :    
1279 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
1280 :     my %activeR = map { $_ => 1 } @subsetR;
1281 :    
1282 :     if (! $ignore_alt)
1283 :     {
1284 :     my $subset;
1285 :     foreach $subset (grep { $_ =~ /^\*/ } $subsystem->get_subset_namesC)
1286 :     {
1287 : overbeek 1.4 my @mem = grep { $activeC{$_} } $subsystem->get_subsetC_roles($subset);
1288 : overbeek 1.1 if (@mem > 1)
1289 :     {
1290 :     my $mem = [@mem];
1291 :     foreach $_ (@mem)
1292 :     {
1293 :     $alternatives{$_} = [$subset,$mem];
1294 :     }
1295 :     }
1296 :     }
1297 :     }
1298 :    
1299 :     my @in = $subsystem->get_genomes;
1300 : redwards 1.32
1301 : overbeek 1.1 if (@in > 0)
1302 :     {
1303 :     my $col_hdrs = ["Genome ID","Organism","Variant Code"];
1304 :    
1305 :     my @row_guide = ();
1306 :    
1307 :     my($role,%in_col);
1308 :     foreach $role (grep { $activeC{$_} } $subsystem->get_roles)
1309 :     {
1310 :     if (! $in_col{$role})
1311 :     {
1312 :     if ($_ = $alternatives{$role})
1313 :     {
1314 :     my($abbrev,$mem) = @$_;
1315 :     push(@$col_hdrs,$abbrev);
1316 :     push(@row_guide,[map { [$_,"-" . ($subsystem->get_role_index($_) + 1)] } @$mem]);
1317 :     foreach $_ (@$mem) { $in_col{$_} = 1 };
1318 :     }
1319 :     else
1320 :     {
1321 :     push(@$col_hdrs,$subsystem->get_role_abbr($subsystem->get_role_index($role)));
1322 :     push(@row_guide,[[$role,""]]);
1323 :     }
1324 :     }
1325 :     }
1326 :    
1327 :     my $tab = [];
1328 :     my($genome,@pegs,@cells,$set,$peg_set,$pair,$role,$suffix,$row,$peg,$color_of,$cell,%count,$color,@colors);
1329 :     foreach $genome (grep { $activeR{$_} } @in)
1330 :     {
1331 : overbeek 1.7 my($genomeV,$vcodeV,$vcode_value);
1332 :     $vcode_value = $subsystem->get_variant_code($subsystem->get_genome_index($genome));
1333 : overbeek 1.8 $row = [$genome, &ext_genus_species($fig,$genome),$vcode_value];
1334 : overbeek 1.1
1335 :     @pegs = ();
1336 :     @cells = ();
1337 : mkubal 1.47
1338 :     my $skip = 0;
1339 :     my $good = 0;
1340 :     my $v;
1341 :     if ($cgi->param('include_these_variants'))
1342 :     {
1343 :     my @variant_list = split(',',$cgi->param('include_these_variants'));
1344 :     foreach $v (@variant_list)
1345 :     {
1346 :     if ($v == $vcode_value)
1347 :     {
1348 :     $good = 1;
1349 :     }
1350 :     }
1351 :     if($good == 0)
1352 :     {
1353 :     $skip = 1;
1354 :     }
1355 :    
1356 :     }
1357 :    
1358 :     next if ($skip);
1359 :    
1360 :    
1361 : overbeek 1.1 foreach $set (@row_guide)
1362 :     {
1363 :     $peg_set = [];
1364 :     foreach $pair (@$set)
1365 :     {
1366 :     ($role,$suffix) = @$pair;
1367 : overbeek 1.2 foreach $peg ($subsystem->get_pegs_from_cell($genome,$role))
1368 : overbeek 1.1 {
1369 :     push(@$peg_set,[$peg,$suffix]);
1370 :     }
1371 :     }
1372 :     push(@pegs,map { $_->[0] } @$peg_set);
1373 :     push(@cells,$peg_set);
1374 :     }
1375 :     $color_of = &group_by_clusters($fig,\@pegs);
1376 : redwards 1.32 # RAE added a new call to get tag/value pairs
1377 :     # Note that $color_of is not overwritten.
1378 :     my $superscript;
1379 : redwards 1.52 if ($cgi->param('color_by_ga'))
1380 :     {
1381 :     # add colors based on the genome attributes
1382 :     # get the value
1383 :     my $ga=$cgi->param('color_by_ga');
1384 :     my $valuetype=$fig->guess_value_format($ga);
1385 : redwards 1.66 my @array=$fig->get_attributes($genome, $ga);
1386 :     next unless ($array[0]);
1387 :     # for the purposes of this page, we are going to color on the
1388 :     # value of the last attribute
1389 : redwards 1.67 my ($gotpeg, $gottag, $value, $url)=@{$array[0]};
1390 : redwards 1.52 if (defined $value) # we don't want to color undefined values
1391 :     {
1392 :     my @color=&cool_colors();
1393 :     my $colval; # what we are basing the color on.
1394 :     if ($valuetype->[0] eq "string") {$colval=$value} # strings are easy, we color based on string;
1395 :     else {
1396 : redwards 1.58 # Initially spllit numbers into groups of 10.
1397 : redwards 1.52 # $valuetype->[2] is the maximum number for this value
1398 : redwards 1.58 # but I don't like this
1399 :     # $colval = int($value/$valuetype->[2]*10);
1400 :    
1401 :     # we want something like 0-1, 1-2, 2-3, 3-4 as the labels.
1402 :     # so we will do it in groups of ten
1403 :     my ($type, $min, $max)=@$valuetype;
1404 :     for (my $i=$min; $i<$max; $i+=$max/10) {
1405 :     if ($value >= $i && $value < $i+$max/10) {$colval = $i . "-" . ($i+($max/10))}
1406 :     }
1407 : redwards 1.52 }
1408 : redwards 1.58
1409 :     unless ($colval) {print STDERR "No color value found for |$value|\n"}
1410 : redwards 1.52
1411 :     if (!$tagvalcolor->{$colval}) {
1412 :     # figure out the highest number used in the array
1413 :     $tagvalcolor->{$colval}=0;
1414 :     foreach my $t (keys %$tagvalcolor) {
1415 :     ($tagvalcolor->{$t} > $tagvalcolor->{$colval}) ? $tagvalcolor->{$colval}=$tagvalcolor->{$t} : 1;
1416 :     }
1417 :     $tagvalcolor->{$colval}++;
1418 :     }
1419 :    
1420 :     foreach my $cell (@cells) {
1421 :     foreach $_ (@$cell)
1422 :     {
1423 :     $color_of->{$_->[0]} = $color[$tagvalcolor->{$colval}]
1424 :     }
1425 :     }
1426 :     }
1427 :     }
1428 : redwards 1.54 if ($cgi->param("color_by_peg_tag"))
1429 : redwards 1.32 {
1430 : redwards 1.54 ($color_of, $superscript, $tagvalcolor) = color_by_tag($fig, \@pegs, $color_of, $tagvalcolor, $cgi->param("color_by_peg_tag"));
1431 : redwards 1.32 }
1432 : overbeek 1.1 foreach $cell (@cells)
1433 :     {
1434 :     undef %count;
1435 :     foreach $_ (@$cell)
1436 :     {
1437 :     if (($color = $color_of->{$_->[0]}) ne '#FFFFFF')
1438 :     {
1439 :     $count{$color}++;
1440 :     }
1441 :     }
1442 :     @colors = sort { $count{$b} <=> $count{$a} } keys(%count);
1443 :     $color = (@colors > 0) ? $colors[0] : '#FFFFFF';
1444 : redwards 1.32 my $cell_data="\@bgcolor=\"$color\":";
1445 :     $cell_data .= join(", ",map { ($cgi->param('ext_ids') ? &external_id($fig,$cgi,$_->[0]) : &HTML::fid_link($cgi,$_->[0],"local")) . $_->[1] } @$cell);
1446 :     if ($superscript)
1447 :     {
1448 : redwards 1.33 # flatten this
1449 :     my %sscript;
1450 :     foreach my $cv (@$cell) {
1451 :     next unless ($superscript->{$cv->[0]});
1452 : redwards 1.66 my %flatten;
1453 :     foreach my $value (@{$superscript->{$cv->[0]}}) {$flatten{$value}=1}
1454 :     $sscript{join ",", sort {$a <=> $b} keys %flatten}=1;
1455 : redwards 1.33 }
1456 : redwards 1.34 if (scalar keys %sscript) {$cell_data .= " &nbsp; <sup> [" . (join ", ", keys %sscript) . "] </sup> "}
1457 : redwards 1.32 }
1458 : redwards 1.33 if (!$cell_data || $cell_data eq '<td bgcolor="#FFFFFF"></td>') {$cell_data = '<td bgcolor="#FFFFFF"> &nbsp; </td>'}
1459 : redwards 1.32 push(@$row, $cell_data);
1460 :     #push(@$row,"\@bgcolor=\"$color\":" . join(", ",map { ($cgi->param('ext_ids') ? &external_id($fig,$cgi,$_->[0]) : &HTML::fid_link($cgi,$_->[0],"local")) . $_->[1] } @$cell));
1461 : overbeek 1.1 }
1462 :     push(@$tab,$row);
1463 :     }
1464 :    
1465 :    
1466 :     my($sort);
1467 :     if ($sort = $cgi->param('sort'))
1468 :     {
1469 : overbeek 1.55 if ($sort eq "by_pattern")
1470 : overbeek 1.1 {
1471 : overbeek 1.8 my @tmp = ();
1472 :     my $row;
1473 :     foreach $row (@$tab)
1474 :     {
1475 :     my @var = ();
1476 :     my $i;
1477 :     for ($i=3; ($i < @$row); $i++)
1478 :     {
1479 : overbeek 1.39 push(@var, ($row->[$i] =~ /\|/) ? 1 : 0);
1480 : overbeek 1.8 }
1481 :     push(@tmp,[join("",@var),$row]);
1482 :     }
1483 :     $tab = [map { $_->[1] } sort { $a->[0] cmp $b->[0] } @tmp];
1484 : overbeek 1.1 }
1485 :     elsif ($sort eq "by_phylo")
1486 :     {
1487 :     $tab = [map { $_->[0] }
1488 :     sort { ($a->[1] cmp $b->[1]) or ($a->[0]->[1] cmp $b->[0]->[1]) }
1489 :     map { [$_, $fig->taxonomy_of($_->[0])] }
1490 :     @$tab];
1491 :     }
1492 :     elsif ($sort eq "by_tax_id")
1493 :     {
1494 :     $tab = [sort { $a->[0] <=> $b->[0] } @$tab];
1495 :     }
1496 :     elsif ($sort eq "alphabetic")
1497 :     {
1498 :     $tab = [sort { ($a->[1] cmp $b->[1]) or ($a->[0] <=> $b->[0]) } @$tab];
1499 :     }
1500 : overbeek 1.56 elsif ($sort eq "by_variant")
1501 :     {
1502 :     $tab = [sort { ($a->[2] cmp $b->[2]) or ($a->[1] <=> $b->[1]) } @$tab];
1503 :     }
1504 : overbeek 1.1 }
1505 :    
1506 : mkubal 1.47 foreach $row (@$tab)
1507 : overbeek 1.8 {
1508 : redwards 1.63 next if ($row->[2] == -1 && $cgi->param('hide_minus1')); # RAE don't show -1 variants if checked
1509 : overbeek 1.8 my($genomeV,$vcodeV,$vcode_value);
1510 :     $genome = $row->[0];
1511 :     $vcode_value = $row->[2];
1512 :     if ($cgi->param('can_alter'))
1513 :     {
1514 :     $genomeV = $cgi->textfield(-name => "genome$genome", -size => 15, -value => $genome, -override => 1);
1515 : overbeek 1.19 $vcodeV = $cgi->textfield(-name => "vcode$genome", -value => $vcode_value, -size => 10);
1516 : overbeek 1.8 }
1517 :     else
1518 :     {
1519 :     push(@$html,$cgi->hidden(-name => "genome$genome", -value => $genome, -override => 1),
1520 :     $cgi->hidden(-name => "vcode$genome", -value => $vcode_value));
1521 :     $genomeV = $genome;
1522 :     $vcodeV = $vcode_value;
1523 :     }
1524 :     $row->[0] = $genomeV;
1525 :     $row->[2] = $vcodeV;
1526 :     }
1527 :    
1528 : overbeek 1.6 my $tab1 = [];
1529 :     foreach $row (@$tab)
1530 :     {
1531 : redwards 1.63 next if ($row->[2] == -1 && $cgi->param('hide_minus1')); # RAE don't show -1 variants if checked
1532 : overbeek 1.6 if ((@$tab1 > 0) && ((@$tab1 % 10) == 0))
1533 :     {
1534 :     push(@$tab1,[map { "<b>$_</b>" } @$col_hdrs]) ;
1535 :     }
1536 :     push(@$tab1,$row);
1537 :     }
1538 :    
1539 :     push(@$html,&HTML::make_table($col_hdrs,$tab1,"Basic Spreadsheet"),
1540 : overbeek 1.1 $cgi->hr
1541 :     );
1542 :    
1543 :     push(@$html,$cgi->scrolling_list(-name => 'sort',
1544 : overbeek 1.56 -value => ['unsorted','alphabetic','by_pattern',
1545 :     'by_phylo','by_tax_id','by_variant'],
1546 : overbeek 1.1 -default => 'unsorted'
1547 :     ));
1548 : mkubal 1.47
1549 :     push(@$html,'<br><br>Enter comma-separated list of variants to display in spreadsheet<br>',
1550 :     $cgi->textfield(-name => "include_these_variants", -size => 50)
1551 :     );
1552 :     }
1553 : redwards 1.52
1554 :     # add an explanation for the colors if we want one.
1555 :     if ($cgi->param('color_by_ga'))
1556 :     {
1557 : redwards 1.59 push(@$html, &HTML::make_table(undef,&describe_colors($tagvalcolor),"Color Descriptions<br><small>Link limits display to those organisms</small>"));
1558 : redwards 1.52 }
1559 : overbeek 1.1 }
1560 :    
1561 :     sub group_by_clusters {
1562 :     my($fig,$pegs) = @_;
1563 :     my($peg,@clusters,@cluster,@colors,$color,%seen,%conn,$x,$peg1,@pegs,$i);
1564 :    
1565 :     my $color_of = {};
1566 :     foreach $peg (@$pegs) { $color_of->{$peg} = '#FFFFFF' }
1567 :    
1568 :     if ($cgi->param('show_clusters'))
1569 :     {
1570 :     @pegs = keys(%$color_of);
1571 :    
1572 :     foreach $peg (@pegs)
1573 :     {
1574 :     foreach $peg1 (grep { $color_of->{$_} && ($_ ne $peg) } $fig->close_genes($peg,5000))
1575 :     {
1576 :     push(@{$conn{$peg}},$peg1);
1577 :     }
1578 :     }
1579 :    
1580 :     @clusters = ();
1581 :     while ($peg = shift @pegs)
1582 :     {
1583 :     if (! $seen{$peg})
1584 :     {
1585 :     @cluster = ($peg);
1586 :     $seen{$peg} = 1;
1587 :     for ($i=0; ($i < @cluster); $i++)
1588 :     {
1589 :     $x = $conn{$cluster[$i]};
1590 :     foreach $peg1 (@$x)
1591 :     {
1592 :     if (! $seen{$peg1})
1593 :     {
1594 :     push(@cluster,$peg1);
1595 :     $seen{$peg1} = 1;
1596 :     }
1597 :     }
1598 :     }
1599 :     push(@clusters,[@cluster]);
1600 :     }
1601 :     }
1602 :    
1603 : redwards 1.52 @colors = &cool_colors();
1604 : overbeek 1.1
1605 :     @clusters = grep { @$_ > 1 } sort { @$a <=> @$b } @clusters;
1606 :    
1607 :     if (@clusters > @colors) { splice(@clusters,0,(@clusters - @colors)) } # make sure we have enough colors
1608 :    
1609 :     my($cluster);
1610 :     foreach $cluster (@clusters)
1611 :     {
1612 :     $color = shift @colors;
1613 :     foreach $peg (@$cluster)
1614 :     {
1615 :     $color_of->{$peg} = $color;
1616 :     }
1617 :     }
1618 :     }
1619 :     return $color_of;
1620 :     }
1621 :    
1622 : redwards 1.32
1623 :     =head1 color_by_tag
1624 :    
1625 :     Change the color of cells by the pir superfamily. This is taken from the key/value pair
1626 :     Note that we will not change the color if $cgi->param('show_clusters') is set.
1627 :    
1628 :     This is gneric and takes the following arguments:
1629 :     fig,
1630 :     pointer to list of pegs,
1631 :     pointer to hash of colors by peg,
1632 :     pointer to a hash that retains numbers across rows. The number is based on the value.
1633 :     tag to use in encoding
1634 :    
1635 :     eg. ($color_of, $superscript, $tagvalcolor) = color_by_tag($fig, $pegs, $color_of, $tagvalcolor, "PIRSF");
1636 :    
1637 :     =cut
1638 :    
1639 :     sub color_by_tag {
1640 : redwards 1.35 # RAE added this so we can color individual cells across a column
1641 : redwards 1.32 my ($fig, $pegs, $color_of, $tagvalcolor, $want)=@_;
1642 :     # figure out the colors and the superscripts for the pirsf
1643 :     # superscript will be a number
1644 :     # color will be related to the number somehow
1645 :     # url will be the url for each number
1646 :     my $number; my $url;
1647 : redwards 1.33 my $count=0;
1648 : redwards 1.32 #count has to be the highest number if we increment it
1649 : redwards 1.33 foreach my $t (keys %$tagvalcolor) {($tagvalcolor->{$t} > $count) ? $count=$tagvalcolor->{$t} : 1}
1650 :     $count++; # this should now be the next number to assign
1651 : redwards 1.32 foreach my $peg (@$pegs) {
1652 : redwards 1.54 next unless (my @attr=$fig->get_attributes($peg));
1653 : redwards 1.32 foreach my $attr (@attr) {
1654 : redwards 1.54 next unless (defined $attr);
1655 : redwards 1.67 my ($gotpeg, $tag, $val, $link)=@$attr;
1656 : redwards 1.32 next unless ($tag eq $want);
1657 :     if ($tagvalcolor->{$val}) {
1658 :     $number->{$peg}=$tagvalcolor->{$val};
1659 : redwards 1.66 push (@{$url->{$peg}}, "<a href='$link'>" . $number->{$peg} . "</a>");
1660 : redwards 1.32 }
1661 :     else {
1662 :     $number->{$peg}=$tagvalcolor->{$val}=$count++;
1663 : redwards 1.66 push (@{$url->{$peg}}, "<a href='$link'>" . $number->{$peg} . "</a>");
1664 : redwards 1.32 }
1665 :     #### This is a botch at the moment. I want PIRSF to go to my page that I am working on, not PIR
1666 :     #### so I am just correcting those. This is not good, and I should change the urls in the tag/value pairs or something
1667 :     if ($want eq "PIRSF") {
1668 : redwards 1.66 pop @{$url->{$peg}};
1669 : redwards 1.32 $val =~ /(^PIRSF\d+)/;
1670 : redwards 1.66 push (@{$url->{$peg}}, $cgi->a({href => "pir.cgi?&user=$user&pirsf=$1"}, $number->{$peg}));
1671 : redwards 1.32 }
1672 :     }
1673 :     }
1674 :    
1675 :    
1676 :     # if we want to assign some colors, lets do so now
1677 : redwards 1.52 my @colors = &cool_colors();
1678 : redwards 1.32 unless ($cgi->param('show_clusters')) {
1679 :     foreach my $peg (@$pegs) { $color_of->{$peg} = '#FFFFFF' }
1680 :     foreach my $peg (keys %$number) {
1681 :     # the color is going to be the location in @colors
1682 :     unless ($number->{$peg} > @colors) {$color_of->{$peg}=$colors[$number->{$peg}-1]}
1683 :     }
1684 :     }
1685 :     return ($color_of, $url, $tagvalcolor);
1686 :     }
1687 :    
1688 :    
1689 : overbeek 1.1 sub format_ssa_table {
1690 :     my($cgi,$html,$user,$ssaP) = @_;
1691 :     my($ssa,$curator);
1692 :     my($url1,$link1);
1693 :    
1694 :     my $can_alter = $cgi->param('can_alter');
1695 :     push(@$html, $cgi->start_form(-action => "subsys.cgi",
1696 :     -method => 'post'),
1697 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
1698 :     $cgi->hidden(-name => 'can_alter', -value => $can_alter, -override => 1),
1699 :     $cgi->hidden(-name => 'request', -value => 'delete_or_export_ssa', -override => 1)
1700 :     );
1701 :     push(@$html,"<font size=\"+2\">Please do not ever edit someone else\'s spreadsheet (by using their
1702 :     user ID), and <b>never open multiple windows to
1703 :     process the same spreadsheet</b></font>. It is, of course, standard practice to open a subsystem
1704 :     spreadsheet and then to have multiple other SEED windows to access data and modify annotations. Further,
1705 :     you can access someone else's subsystem spreadsheet using your ID (which will make it impossible
1706 :     for you to edit the spreadsheet).
1707 : redwards 1.46 Just do not open the same subsystem spreadsheet for editing in multiple windows simultaneously.
1708 : redwards 1.62 A gray color means that the subsystem has no genomes attached to it. Go ahead and make these your own\n",
1709 : redwards 1.64 "<a href=\"/FIG/Html/conflict_resolution.html\" class=\"help\" target=\"help\">Help on conflict resolution</a>\n",
1710 : overbeek 1.1 $cgi->br,
1711 :     $cgi->br
1712 :     );
1713 :    
1714 : redwards 1.65 # RAE: removed this from above push because VV want's it kept secret
1715 :     # "<a href=\"/FIG/Html/seedtips.html#change_ownership\" class=\"help\" target=\"help\">Help on changing subsystem ownership</a>\n",
1716 :    
1717 :    
1718 : overbeek 1.1 my $col_hdrs = [
1719 :     "Name","Curator","Exchangable","Version",
1720 :     "Reset to Previous Timestamp","Delete",
1721 :     "Export Full Subsystem","Export Just Assignments", "Publish to Clearinghouse",
1722 :     ];
1723 :     my $title = "Existing Subsystem Annotations";
1724 :     my $tab = [];
1725 :     foreach $_ (@$ssaP)
1726 :     {
1727 :     my($publish_checkbox);
1728 :     ($ssa,$curator) = @$_;
1729 :    
1730 :     my($url,$link);
1731 :     if ((-d "$FIG_Config::data/Subsystems/$ssa/Backup") && ($curator eq $cgi->param('user')))
1732 :     {
1733 :     $url = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$ssa&request=reset";
1734 :     $link = "<a href=$url>reset</a>";
1735 :     }
1736 :     else
1737 :     {
1738 :     $link = "";
1739 :     }
1740 :    
1741 :     if (($fig->is_exchangable_subsystem($ssa)) && ($curator eq $cgi->param('user')))
1742 :     {
1743 :     $url1 = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$ssa&request=make_unexchangable";
1744 :     $link1 = "Exchangable<br><a href=$url1>Make not exchangable</a>";
1745 :     }
1746 :     elsif ($curator eq $cgi->param('user'))
1747 :     {
1748 :     $url1 = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$ssa&request=make_exchangable";
1749 :     $link1 = "Not exchangable<br><a href=$url1>Make exchangable</a>";
1750 :     }
1751 :     else
1752 :     {
1753 :     $link1 = "";
1754 :     }
1755 :    
1756 :     #
1757 :     # Only allow publish for subsystems we are curating?
1758 :     #
1759 :     if ($curator eq $cgi->param('user'))
1760 :     {
1761 :     $publish_checkbox = $cgi->checkbox(-name => "publish_to_clearinghouse",
1762 :     -value => $ssa,
1763 :     -label => "Publish"),
1764 :    
1765 :     }
1766 : redwards 1.46
1767 :     # RAE color the background if the subsystem is empty
1768 :     # this uses a modification to HTML.pm that I made earlier to accept refs to arrays as cell data
1769 : overbeek 1.51 my $cell1=&ssa_link($fig,$ssa,$user);
1770 : redwards 1.46 #unless (scalar $fig->subsystem_to_roles($ssa)) {$cell1 = [$cell1, 'td bgcolor="Dark grey"']} ## THIS IS DOG SLOW, BUT WORKS
1771 :     #unless (scalar $fig->get_subsystem($ssa)->get_genomes()) {$cell1 = [$cell1, 'td bgcolor="#A9A9A9"']} ## WORKS PERFECTLY, but sort of slow
1772 :     unless (scalar @{$fig->subsystem_genomes($ssa, 1)}) {$cell1 = [$cell1, 'td bgcolor="silver"']}
1773 :    
1774 : overbeek 1.1 push(@$tab,[
1775 : redwards 1.46 $cell1,
1776 : overbeek 1.1 $curator,
1777 :     $link1,
1778 :     $fig->subsystem_version($ssa),
1779 :     $link,
1780 :     ($curator eq $cgi->param('user')) ? $cgi->checkbox(-name => "delete", -value => $ssa) : "",
1781 :     $cgi->checkbox(-name => "export", -value => $ssa, -label => "Export full"),
1782 :     $cgi->checkbox(-name => "export_assignments", -value => $ssa, -label => "Export assignments"),
1783 :     $publish_checkbox,
1784 :     ]);
1785 :     }
1786 :     push(@$html,
1787 :     &HTML::make_table($col_hdrs,$tab,$title),
1788 :     $cgi->submit(-name => 'delete_export',
1789 :     -label => 'Process marked deletions and exports'),
1790 :     $cgi->submit(-name => 'publish',
1791 :     -label => "Publish marked subsystems"),
1792 :     $cgi->end_form
1793 :     );
1794 :     }
1795 :    
1796 : redwards 1.25 # RAE: I think this should be placed as a method in
1797 :     # Subsystems.pm and called subsystems I know about or something.
1798 :     # Cowardly didn't do though :-)
1799 : overbeek 1.1 sub existing_subsystem_annotations {
1800 : overbeek 1.51 my($fig) = @_;
1801 : overbeek 1.1 my($ssa,$name);
1802 :     my @ssa = ();
1803 :     if (opendir(SSA,"$FIG_Config::data/Subsystems"))
1804 :     {
1805 : overbeek 1.51 @ssa = map { $ssa = $_; $name = $ssa; $ssa =~ s/[ \/]/_/g; [$name,$fig->subsystem_curator($ssa)] } grep { $_ !~ /^\./ } readdir(SSA);
1806 : overbeek 1.1 closedir(SSA);
1807 :     }
1808 : overbeek 1.50 return sort { (lc $a->[0]) cmp (lc $b->[0]) } @ssa;
1809 : overbeek 1.1 }
1810 :    
1811 :     sub ssa_link {
1812 : overbeek 1.51 my($fig,$ssa,$user) = @_;
1813 : overbeek 1.1 my $name = $ssa; $name =~ s/_/ /g;
1814 :     my $target = "window$$";
1815 : overbeek 1.9 if ($name =~ /([a-zA-Z]{3})/)
1816 :     {
1817 :     $target .= ".$1";
1818 :     }
1819 :    
1820 : overbeek 1.51 my $can_alter = $fig->subsystem_curator($ssa) eq $user;
1821 : overbeek 1.1
1822 :     my $url = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$ssa&request=show_ssa&can_alter=$can_alter";
1823 :     return "<a href=$url target=$target>$name</a>";
1824 :     }
1825 :    
1826 :     sub log_update {
1827 :     my($ssa,$user) = @_;
1828 :    
1829 :     $ssa =~ s/[ \/]/_/g;
1830 :    
1831 :     if (open(LOG,">>$FIG_Config::data/Subsystems/$ssa/curation.log"))
1832 :     {
1833 :     my $time = time;
1834 :     print LOG "$time\t$user\tupdated\n";
1835 :     close(LOG);
1836 :     }
1837 :     else
1838 :     {
1839 :     print STDERR "failed to open $FIG_Config::data/Subsystems/$ssa/curation.log\n";
1840 :     }
1841 :     }
1842 :    
1843 :     sub export {
1844 :     my($fig,$cgi,$ssa) = @_;
1845 :     my($line);
1846 :    
1847 :     my ($exportable,$notes) = $fig->exportable_subsystem($ssa);
1848 :     foreach $line (@$exportable,@$notes)
1849 :     {
1850 :     print $line;
1851 :     }
1852 :     }
1853 :    
1854 :     sub export_assignments {
1855 :     my($fig,$cgi,$ssa) = @_;
1856 :     my(@roles,$i,$entry,$id,$user);
1857 :    
1858 :     if (($user = $cgi->param('user')) && open(SSA,"<$FIG_Config::data/Subsystems/$ssa/spreadsheet"))
1859 :     {
1860 :     $user =~ s/^master://;
1861 :     &FIG::verify_dir("$FIG_Config::data/Assignments/$user");
1862 : overbeek 1.51 my $who = $fig->subsystem_curator($ssa);
1863 : overbeek 1.1 my $file = &FIG::epoch_to_readable(time) . ":$who:generated_from_subsystem_$ssa";
1864 :    
1865 :     if (open(OUT,">$FIG_Config::data/Assignments/$user/$file"))
1866 :     {
1867 :     while (defined($_ = <SSA>) && ($_ !~ /^\/\//))
1868 :     {
1869 :     chop;
1870 :     push(@roles,$_);
1871 :     }
1872 :     while (defined($_ = <SSA>) && ($_ !~ /^\/\//)) {}
1873 :     while (defined($_ = <SSA>))
1874 :     {
1875 :     chop;
1876 :     my @flds = split(/\t/,$_);
1877 :     my $genome = $flds[0];
1878 :     for ($i=2; ($i < @flds); $i++)
1879 :     {
1880 :     my @entries = split(/,/,$flds[$i]);
1881 :     foreach $id (@entries)
1882 :     {
1883 :     my $peg = "fig|$genome.peg.$id";
1884 :     my $func = $fig->function_of($peg);
1885 :     print OUT "$peg\t$func\n";
1886 :     }
1887 :     }
1888 :     }
1889 :     close(OUT);
1890 :     }
1891 :     close(SSA);
1892 :     }
1893 :     }
1894 :    
1895 :     sub format_missing {
1896 :     my($fig,$cgi,$html,$subsystem) = @_;
1897 :     my($org,$abr,$role,$missing);
1898 :    
1899 :     $user = $cgi->param('user');
1900 :    
1901 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
1902 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
1903 :    
1904 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
1905 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
1906 :    
1907 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
1908 :    
1909 :     my @alt_sets = grep { ($_ =~ /^\*/) } $subsystem->get_subset_namesC;
1910 :     my($set,$col,%in);
1911 :     foreach $set (@alt_sets)
1912 :     {
1913 : overbeek 1.4 my @mem = grep { $activeC{$_} } $subsystem->get_subsetC_roles($set);
1914 : overbeek 1.1 foreach $col (@mem)
1915 :     {
1916 :     $in{$col} = $set;
1917 :     }
1918 :     }
1919 :     push(@$html,$cgi->h1('To Check Missing Entries:'));
1920 :    
1921 :     foreach $org (@subsetR)
1922 :     {
1923 :     my @missing = &columns_missing_entries($cgi,$subsystem,$org,\@subsetC,\%in);
1924 :    
1925 :     $missing = [];
1926 :     foreach $role (@missing)
1927 :     {
1928 :     $abr = $subsystem->get_role_abbr($subsystem->get_role_index($role));
1929 :     my $roleE = $cgi->escape($role);
1930 :    
1931 :     my $link = "<a href=" . &FIG::cgi_url . "/pom.cgi?user=$user&request=find_in_org&role=$roleE&org=$org>$abr $role</a>";
1932 :     push(@$missing,$link);
1933 :     }
1934 :    
1935 :     if (@$missing > 0)
1936 :     {
1937 :     my $genus_species = &ext_genus_species($fig,$org);
1938 :     push(@$html,$cgi->h2("$org: $genus_species"));
1939 :     push(@$html,$cgi->ul($cgi->li($missing)));
1940 :     }
1941 :     }
1942 :     }
1943 :    
1944 :     sub columns_missing_entries {
1945 :     my($cgi,$subsystem,$org,$roles,$in) = @_;
1946 :    
1947 : overbeek 1.71 my $just_genome = $cgi->param('just_genome');
1948 : overbeek 1.72 if ($just_genome && ($just_genome =~ /(\d+\.\d+)/) && ($org != $1)) { return () }
1949 : overbeek 1.71
1950 : overbeek 1.1 my $just_col = $cgi->param('just_col');
1951 :     my(@really_missing) = ();
1952 :    
1953 :     my($role,%missing_cols);
1954 :     foreach $role (@$roles)
1955 :     {
1956 :     next if ($just_col && ($role ne $just_col));
1957 :     if ($subsystem->get_pegs_from_cell($org,$role) == 0)
1958 :     {
1959 :     $missing_cols{$role} = 1;
1960 :     }
1961 :     }
1962 :    
1963 :     foreach $role (@$roles)
1964 :     {
1965 :     if ($missing_cols{$role})
1966 :     {
1967 :     my($set);
1968 :     if (($set = $in->{$role}) && (! $cgi->param('ignore_alt')))
1969 :     {
1970 : overbeek 1.4 my @set = $subsystem->get_subsetC_roles($set);
1971 : overbeek 1.1
1972 :     my($k);
1973 :     for ($k=0; ($k < @set) && $missing_cols{$set[$k]}; $k++) {}
1974 :     if ($k == @set)
1975 :     {
1976 :     push(@really_missing,$role);
1977 :     }
1978 :     }
1979 :     else
1980 :     {
1981 :     push(@really_missing,$role);
1982 :     }
1983 :     }
1984 :     }
1985 :     return @really_missing;
1986 :     }
1987 :    
1988 :     sub format_missing_including_matches
1989 :     {
1990 :     my($fig,$cgi,$html,$subsystem) = @_;
1991 :     my($org,$abr,$role,$missing);
1992 :    
1993 :     my $user = $cgi->param('user');
1994 :    
1995 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
1996 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
1997 :    
1998 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
1999 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
2000 :    
2001 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
2002 :    
2003 :     my @alt_sets = grep { ($_ =~ /^\*/) } $subsystem->get_subset_namesC;
2004 :     my($set,$col,%in);
2005 :     foreach $set (@alt_sets)
2006 :     {
2007 : overbeek 1.4 my @mem = grep { $activeC{$_} } $subsystem->get_subsetC_roles($set);
2008 : overbeek 1.1 foreach $col (@mem)
2009 :     {
2010 :     $in{$col} = $set;
2011 :     }
2012 :     }
2013 :     push(@$html,$cgi->h1('To Check Missing Entries:'));
2014 :    
2015 :     push(@$html, $cgi->start_form(-action=> "fid_checked.cgi"));
2016 :    
2017 :     my $can_alter = $cgi->param('can_alter');
2018 :     push(@$html,
2019 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
2020 :     $cgi->hidden(-name => 'can_alter', -value => $can_alter, -override => 1));
2021 : overbeek 1.11
2022 : overbeek 1.14 my $just_role = &which_role($subsystem,$cgi->param('just_role'));
2023 : overbeek 1.70 # print STDERR "There are ", scalar @subsetR, " organisms to check\n";
2024 : overbeek 1.1 foreach $org (@subsetR)
2025 :     {
2026 :     my @missing = &columns_missing_entries($cgi,$subsystem,$org,\@subsetC,\%in);
2027 :     $missing = [];
2028 :     foreach $role (@missing)
2029 :     {
2030 : overbeek 1.14 # next if (($_ = $cgi->param('just_role')) && ($_ != ($subsystem->get_role_index($role) + 1)));
2031 :     next if ($just_role && ($just_role ne $role));
2032 : overbeek 1.1
2033 :     my @hits = $fig->find_role_in_org($role, $org, $user, $cgi->param("sims_cutoff"));
2034 :     push(@$missing,@hits);
2035 :     }
2036 : overbeek 1.70 # print STDERR "Found ", scalar @$missing, " for $org\n";
2037 : overbeek 1.1 if (@$missing > 0)
2038 :     {
2039 : overbeek 1.11 my $genus_species = &ext_genus_species($fig,$org);
2040 :     push(@$html,$cgi->h2("$org: $genus_species"));
2041 :    
2042 : overbeek 1.1 my $colhdr = ["Assign", "P-Sc", "PEG", "Len", "Current fn", "Matched peg", "Len", "Function"];
2043 :     my $tbl = [];
2044 :    
2045 :     for my $hit (@$missing)
2046 :     {
2047 :     my($psc, $my_peg, $my_len, $my_fn, $match_peg, $match_len, $match_fn) = @$hit;
2048 :    
2049 :     my $my_peg_link = &HTML::fid_link($cgi, $my_peg, 1);
2050 :     my $match_peg_link = &HTML::fid_link($cgi, $match_peg, 0);
2051 :    
2052 :     my $checkbox = $cgi->checkbox(-name => "checked",
2053 :     -value => "to=$my_peg,from=$match_peg",
2054 :     -label => "");
2055 :    
2056 :     push(@$tbl, [$checkbox,
2057 :     $psc,
2058 :     $my_peg_link, $my_len, $my_fn,
2059 :     $match_peg_link, $match_len, $match_fn]);
2060 :     }
2061 :    
2062 :     push(@$html, &HTML::make_table($colhdr, $tbl, ""));
2063 :     }
2064 :     }
2065 :     push(@$html,
2066 :     $cgi->submit(-value => "Process assignments",
2067 :     -name => "batch_assign"),
2068 :     $cgi->end_form);
2069 :     }
2070 :    
2071 : mkubal 1.36
2072 :    
2073 :     sub columns_missing_entries {
2074 :     my($cgi,$subsystem,$org,$roles,$in) = @_;
2075 :    
2076 :     next if (($_ = $cgi->param('just_genome')) && ($org != $_));
2077 :     my $just_col = $cgi->param('just_col');
2078 :     my(@really_missing) = ();
2079 :    
2080 :     my($role,%missing_cols);
2081 :     foreach $role (@$roles)
2082 :     {
2083 :     next if ($just_col && ($role ne $just_col));
2084 :     if ($subsystem->get_pegs_from_cell($org,$role) == 0)
2085 :     {
2086 :     $missing_cols{$role} = 1;
2087 :     }
2088 :     }
2089 :    
2090 :     foreach $role (@$roles)
2091 :     {
2092 :     if ($missing_cols{$role})
2093 :     {
2094 :     my($set);
2095 :     if (($set = $in->{$role}) && (! $cgi->param('ignore_alt')))
2096 :     {
2097 :     my @set = $subsystem->get_subsetC_roles($set);
2098 :    
2099 :     my($k);
2100 :     for ($k=0; ($k < @set) && $missing_cols{$set[$k]}; $k++) {}
2101 :     if ($k == @set)
2102 :     {
2103 :     push(@really_missing,$role);
2104 :     }
2105 :     }
2106 :     else
2107 :     {
2108 :     push(@really_missing,$role);
2109 :     }
2110 :     }
2111 :     }
2112 :     return @really_missing;
2113 :     }
2114 :    
2115 :     sub format_missing_including_matches_in_ss
2116 :     {
2117 :     my($fig,$cgi,$html,$subsystem) = @_;
2118 :     my($org,$abr,$role,$missing);
2119 :    
2120 :     my $user = $cgi->param('user');
2121 :    
2122 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
2123 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
2124 :    
2125 :     my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
2126 :     my %activeC = map { $_ => 1 } @subsetC;
2127 :    
2128 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
2129 :    
2130 :     my @alt_sets = grep { ($_ =~ /^\*/) } $subsystem->get_subset_namesC;
2131 :     my($set,$col,%in);
2132 :     foreach $set (@alt_sets)
2133 :     {
2134 :     my @mem = grep { $activeC{$_} } $subsystem->get_subsetC_roles($set);
2135 :     foreach $col (@mem)
2136 :     {
2137 :     $in{$col} = $set;
2138 :     }
2139 :     }
2140 :     push(@$html,$cgi->h1('To Check Missing Entries:'));
2141 :    
2142 :     push(@$html, $cgi->start_form(-action=> "fid_checked.cgi"));
2143 :    
2144 :     my $can_alter = $cgi->param('can_alter');
2145 :     push(@$html,
2146 :     $cgi->hidden(-name => 'user', -value => $user, -override => 1),
2147 :     $cgi->hidden(-name => 'can_alter', -value => $can_alter, -override => 1));
2148 :    
2149 :     my $just_role = &which_role($subsystem,$cgi->param('just_role'));
2150 :    
2151 :     foreach $org (@subsetR)
2152 :     {
2153 :     my @missing = &columns_missing_entries($cgi,$subsystem,$org,\@subsetC,\%in);
2154 :     $missing = [];
2155 :     foreach $role (@missing)
2156 :     {
2157 :     # next if (($_ = $cgi->param('just_role')) && ($_ != ($subsystem->get_role_index($role) + 1)));
2158 :     next if ($just_role && ($just_role ne $role));
2159 :    
2160 : mkubal 1.40 my $flag = 0;
2161 : mkubal 1.48 my $filler;
2162 : mkubal 1.40 my $rdbH = $fig->db_handle;
2163 : olson 1.45 my $q = "SELECT subsystem, role FROM subsystem_index WHERE role = ?";
2164 :     if (my $relational_db_response = $rdbH->SQL($q, 0, $role))
2165 : mkubal 1.40 {
2166 :     my $pair;
2167 :     foreach $pair (@$relational_db_response)
2168 :     {
2169 :     my ($ss, $role) = @$pair;
2170 : mkubal 1.48 #if($ss =="")
2171 :     #{
2172 :     # $filler = 1;
2173 :     #}
2174 :    
2175 : mkubal 1.40 if ($ss !~/Unique/)
2176 :     {
2177 :     $flag = 1;
2178 :     }
2179 :     }
2180 :     }
2181 :    
2182 : mkubal 1.48 if ($flag == 1)
2183 : mkubal 1.40 {
2184 :     my @hits = $fig->find_role_in_org($role, $org, $user, $cgi->param("sims_cutoff"));
2185 :     push(@$missing,@hits);
2186 :     }
2187 : mkubal 1.36 }
2188 :    
2189 :     if (@$missing > 0)
2190 :     {
2191 :     my $genus_species = &ext_genus_species($fig,$org);
2192 :     push(@$html,$cgi->h2("$org: $genus_species"));
2193 :    
2194 : mkubal 1.40 my $colhdr = ["Assign","Sub(s)", "P-Sc", "PEG", "Len", "Current fn", "Matched peg", "Len", "Function"];
2195 : mkubal 1.36 my $tbl = [];
2196 :    
2197 :     for my $hit (@$missing)
2198 :     {
2199 : mkubal 1.40 my($psc, $my_peg, $my_len, $my_fn, $match_peg, $match_len, $match_fn) = @$hit;
2200 : mkubal 1.36 my $my_peg_link = &HTML::fid_link($cgi, $my_peg, 1);
2201 :     my $match_peg_link = &HTML::fid_link($cgi, $match_peg, 0);
2202 :    
2203 :     my $checkbox = $cgi->checkbox(-name => "checked",
2204 :     -value => "to=$my_peg,from=$match_peg",
2205 :     -label => "");
2206 : mkubal 1.48 my $good = 0;
2207 : mkubal 1.40 my @list_of_ss = ();
2208 :     my $ss_table_entry = "none";
2209 : mkubal 1.48
2210 : mkubal 1.40 my (@list_of_returned_ss,$ss_name,$ss_role);
2211 : mkubal 1.48 @list_of_returned_ss = $fig->subsystems_for_peg($match_peg);
2212 : mkubal 1.40 if (@list_of_returned_ss > 0)
2213 :     {
2214 :     for my $ret_ss (@list_of_returned_ss)
2215 :     {
2216 :     ($ss_name,$ss_role)= @$ret_ss;
2217 :     if ($ss_name !~/Unique/)
2218 :     {
2219 : mkubal 1.48 $good = 1;
2220 :     }
2221 :     }
2222 :     }
2223 :    
2224 :     if ($good)
2225 :     {
2226 :     my (@list_of_returned_ss,$ss_name,$ss_role);
2227 :     @list_of_returned_ss = $fig->subsystems_for_peg($my_peg);
2228 :     if (@list_of_returned_ss > 0)
2229 :     {
2230 :     for my $ret_ss (@list_of_returned_ss)
2231 :     {
2232 :     ($ss_name,$ss_role)= @$ret_ss;
2233 :     if ($ss_name !~/Unique/)
2234 :     {
2235 :     push (@list_of_ss,$ss_name);
2236 : mkubal 1.40 $ss_table_entry = join("<br>",@list_of_ss);
2237 :    
2238 :     }
2239 :     }
2240 :     }
2241 : mkubal 1.48
2242 :     push(@$tbl, [$checkbox,$ss_table_entry,
2243 :     $psc,
2244 :     $my_peg_link, $my_len, $my_fn,
2245 :     $match_peg_link, $match_len, $match_fn]);
2246 :     }
2247 :    
2248 :    
2249 :     }
2250 : mkubal 1.36
2251 :     push(@$html, &HTML::make_table($colhdr, $tbl, ""));
2252 :     }
2253 :     }
2254 :     push(@$html,
2255 :     $cgi->submit(-value => "Process assignments",
2256 :     -name => "batch_assign"),
2257 :     $cgi->end_form);
2258 :     }
2259 :    
2260 :    
2261 : overbeek 1.3 sub format_check_assignments {
2262 :     my($fig,$cgi,$html,$subsystem) = @_;
2263 :     my($org,$role);
2264 :    
2265 :     my $user = $cgi->param('user');
2266 :    
2267 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
2268 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
2269 :    
2270 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
2271 : overbeek 1.3 my %activeC = map { $_ => 1 } @subsetC;
2272 :    
2273 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
2274 :    
2275 :     push(@$html,$cgi->h1('Potentially Bad Assignments:'));
2276 :    
2277 :     foreach $org (@subsetR)
2278 :     {
2279 :     next if (($_ = $cgi->param('just_genome_assignments')) && ($_ != $org));
2280 :     my @bad = ();
2281 :    
2282 :     foreach $role (@subsetC)
2283 :     {
2284 :     next if (($_ = $cgi->param('just_role_assignments')) && ($_ != ($subsystem->get_role_index($role) + 1)));
2285 :     push(@bad,&checked_assignments($cgi,$subsystem,$org,$role));
2286 :     }
2287 :    
2288 :     if (@bad > 0)
2289 :     {
2290 :     my $genus_species = &ext_genus_species($fig,$org);
2291 :     push(@$html,$cgi->h2("$org: $genus_species"),
2292 :     $cgi->ul($cgi->li(\@bad)));
2293 :    
2294 :     }
2295 :     }
2296 :     push(@$html,$cgi->hr);
2297 :     }
2298 :    
2299 :     sub checked_assignments {
2300 :     my($cgi,$subsystem,$genome,$role) = @_;
2301 :     my($peg,$line1,$line2,@out,$curr,$auto);
2302 :    
2303 :     my(@bad) = ();
2304 :     my @pegs = $subsystem->get_pegs_from_cell($genome,$role);
2305 :     if (@pegs > 0)
2306 :     {
2307 :     my $tmp = "/tmp/tmp.pegs.$$";
2308 :     open(TMP,">$tmp") || die "could not open $tmp";
2309 :     foreach $peg (@pegs)
2310 :     {
2311 :     print TMP "$peg\n";
2312 :     }
2313 :     close(TMP);
2314 :     my $strict = $cgi->param('strict_check') ? "strict" : "";
2315 :     @out = `$FIG_Config::bin/check_peg_assignments $strict < $tmp 2> /dev/null`;
2316 :     unlink($tmp);
2317 :    
2318 :     while (($_ = shift @out) && ($_ =~ /^(fig\|\d+\.\d+\.peg\.\d+)/))
2319 :     {
2320 :     $peg = $1;
2321 :     if (($line1 = shift @out) && ($line1 =~ /^current:\s+(\S.*\S)/) && ($curr = $1) &&
2322 :     ($line2 = shift @out) && ($line2 =~ /^auto:\s+(\S.*\S)/) && ($auto = $1))
2323 :     {
2324 :     if (! $fig->same_func($curr,$auto))
2325 :     {
2326 :     my $link = &HTML::fid_link($cgi,$peg);
2327 :     push(@bad,"$link<br>$line1<br>$line2<br><br>");
2328 :     }
2329 :     }
2330 :     }
2331 :     }
2332 :     return @bad;
2333 :     }
2334 :    
2335 : overbeek 1.1 sub format_dups {
2336 :     my($fig,$cgi,$html,$subsystem) = @_;
2337 :    
2338 :     my $user = $cgi->param('user');
2339 :    
2340 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
2341 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
2342 :    
2343 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
2344 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
2345 :    
2346 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
2347 :    
2348 :     push(@$html,$cgi->h1('To Check Duplicates:'));
2349 :    
2350 :     my($org,$duplicates,$role,$genus_species);
2351 :     foreach $org (@subsetR)
2352 :     {
2353 :     $duplicates = [];
2354 :     foreach $role (@subsetC)
2355 :     {
2356 :     my(@pegs,$peg,$func);
2357 :     if ((@pegs = $subsystem->get_pegs_from_cell($org,$role)) > 1)
2358 :     {
2359 :     push(@$duplicates,"$role<br>" . $cgi->ul($cgi->li([map { $peg = $_; $func = $fig->function_of($peg,$user); &HTML::fid_link($cgi,$peg) . " $func" } @pegs])));
2360 :     }
2361 :     }
2362 :    
2363 :     if (@$duplicates > 0)
2364 :     {
2365 :     $genus_species = &ext_genus_species($fig,$org);
2366 :     push(@$html,$cgi->h2("$org: $genus_species"));
2367 :     push(@$html,$cgi->ul($cgi->li($duplicates)));
2368 :     }
2369 :     }
2370 :     }
2371 :    
2372 :     sub format_coupled {
2373 :     my($fig,$cgi,$html,$subsystem,$type) = @_;
2374 :     my($i,$j,@show,$user,$org,$link,$gs,$func,$peg,$peg1,$peg2,%in,%seen,%seen2);
2375 :     my(@cluster,$sc,$x,$id2,@in,$sim,@coupled);
2376 :     my($org,$role);
2377 :    
2378 :     $user = $cgi->param('user');
2379 :    
2380 :     my $active_subsetC = ($cgi->param('active_subsetC') or $subsystem->get_active_subsetC );
2381 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
2382 :    
2383 : overbeek 1.4 my @subsetC = $subsystem->get_subsetC_roles($active_subsetC);
2384 : overbeek 1.1 my %activeC = map { $_ => 1 } @subsetC;
2385 :    
2386 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
2387 :    
2388 :     foreach $org (@subsetR)
2389 :     {
2390 :     foreach $role (@subsetC)
2391 :     {
2392 :     push(@in,$subsystem->get_pegs_from_cell($org,$role));
2393 :     }
2394 :     }
2395 :    
2396 :     %in = map { $_ => 1 } @in;
2397 :     @show = ();
2398 :     foreach $peg1 (@in)
2399 :     {
2400 :     if ($type eq "careful")
2401 :     {
2402 :     @coupled = $fig->coupling_and_evidence($peg1,5000,1.0e-10,0.2,1);
2403 :     }
2404 :     else
2405 :     {
2406 :     @coupled = $fig->fast_coupling($peg1,5000,1);
2407 :     }
2408 :    
2409 :     foreach $x (@coupled)
2410 :     {
2411 :     ($sc,$peg2) = @$x;
2412 :     if ((! $in{$peg2}) && ((! $seen{$peg2}) || ($seen{$peg2} < $sc)))
2413 :     {
2414 :     $seen{$peg2} = $sc;
2415 :     # print STDERR "$sc\t$peg1 -> $peg2\n";
2416 :     }
2417 :     }
2418 :     }
2419 :    
2420 :     foreach $peg1 (sort { $seen{$b} <=> $seen{$a} } keys(%seen))
2421 :     {
2422 :     if (! $seen2{$peg1})
2423 :     {
2424 :     @cluster = ($peg1);
2425 :     $seen2{$peg1} = 1;
2426 :     for ($i=0; ($i < @cluster); $i++)
2427 :     {
2428 :     foreach $sim ($fig->sims($cluster[$i],1000,1.0e-10,"fig"))
2429 :     {
2430 :     $id2 = $sim->id2;
2431 :     if ($seen{$id2} && (! $seen2{$id2}))
2432 :     {
2433 :     push(@cluster,$id2);
2434 :     $seen2{$id2} = 1;
2435 :     }
2436 :     }
2437 :     }
2438 :     push(@show, [scalar @cluster,
2439 :     $cgi->br .
2440 :     $cgi->ul($cgi->li([map { $peg = $_;
2441 :     $sc = $seen{$peg};
2442 :     $func = $fig->function_of($peg,$user);
2443 :     $gs = $fig->genus_species($fig->genome_of($peg));
2444 :     $link = &HTML::fid_link($cgi,$peg);
2445 :     "$sc: $link: $func \[$gs\]" }
2446 :     sort { $seen{$b} <=> $seen{$a} }
2447 :     @cluster]))
2448 :     ]);
2449 :     }
2450 :     }
2451 :    
2452 :     if (@show > 0)
2453 :     {
2454 :     @show = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @show;
2455 :     push(@$html,$cgi->h1('Coupled, but not in Spreadsheet:'));
2456 :     push(@$html,$cgi->ul($cgi->li(\@show)));
2457 :     }
2458 :     }
2459 :    
2460 :     sub ext_genus_species {
2461 :     my($fig,$genome) = @_;
2462 :    
2463 :     my $gs = $fig->genus_species($genome);
2464 :     my $c = substr($fig->taxonomy_of($genome),0,1);
2465 :     return "$gs [$c]";
2466 :     }
2467 :    
2468 :     sub show_tree {
2469 :    
2470 :     my($id,$gs);
2471 :     my($tree,$ids) = $fig->build_tree_of_complete;
2472 :     my $relabel = {};
2473 :     foreach $id (@$ids)
2474 :     {
2475 :     if ($gs = $fig->genus_species($id))
2476 :     {
2477 :     $relabel->{$id} = "$gs ($id)";
2478 :     }
2479 :     }
2480 :     $_ = &display_tree($tree,$relabel);
2481 :     print $cgi->pre($_),"\n";
2482 :     }
2483 :    
2484 :     sub export_align_input
2485 :     {
2486 :    
2487 :     }
2488 :    
2489 : redwards 1.22 sub annotate_column {
2490 :     # RAE: I added this function to allow you to reannotate a single column all at once
2491 :     # this is because I wanted to update some of my annotations after looking at UniProt
2492 :     # and couldn't see an easy way to do it.
2493 :     my($fig,$cgi,$html,$col,$subsystem) = @_;
2494 :     my $checked;
2495 :     my $roles = [$subsystem->get_roles];
2496 :     my $colN = &which_column($col,$roles);
2497 :     my @checked = &seqs_to_align($colN,$subsystem);
2498 :     return undef unless (@checked);
2499 :    
2500 :     # the following is read from fid_checked.cgi
2501 :     push( @$html, "<table border=1>\n",
2502 :     "<tr><td>Protein</td><td>Organism</td><td>Current Function</td><td>By Whom</td></tr>"
2503 :     );
2504 :    
2505 :     foreach my $peg ( @checked ) {
2506 :     my @funcs = $fig->function_of( $peg );
2507 :     if ( ! @funcs ) { @funcs = ( ["", ""] ) }
2508 :     my $nfunc = @funcs;
2509 :     my $org = $fig->org_of( $peg );
2510 :     push( @$html, "<tr>",
2511 :     "<td rowspan=$nfunc>$peg</td>",
2512 :     "<td rowspan=$nfunc>$org</td>"
2513 :     );
2514 :     my ($who, $what);
2515 :     push( @$html, join( "</tr>\n<tr>", map { ($who,$what) = @$_; "<td>$what</td><td>$who</td>" } @funcs ) );
2516 :     push( @$html, "</tr>\n" );
2517 :     }
2518 :     push( @$html, "</table>\n" );
2519 :    
2520 :     push( @$html, $cgi->start_form(-action => "fid_checked.cgi", -target=>"_blank"),
2521 :     $cgi->br, $cgi->br,
2522 :     "<table>\n",
2523 :     "<tr><td>New Function:</td>",
2524 :     "<td>", $cgi->textfield(-name => "function", -size => 60), "</td></tr>",
2525 :     "<tr><td colspan=2>", $cgi->hr, "</td></tr>",
2526 :     "<tr><td>New Annotation:</td>",
2527 :     "<td rowspan=2>", $cgi->textarea(-name => "annotation", -rows => 30, -cols => 60), "</td></tr>",
2528 :     "<tr><td valign=top width=20%><br>", $cgi->submit('add annotation'),
2529 :     "<p><b>Please note:</b> At the moment you need to make sure that the annotation in the table at the ",
2530 :     "top of this page reflects the new annotation. This may not be updated automatically.</p>",
2531 :     "</td></tr>",
2532 :     "</table>",
2533 :     $cgi->hidden(-name => 'user', -value => $user),
2534 :     $cgi->hidden(-name => 'checked', -value => [@checked]),
2535 :     $cgi->end_form
2536 :     );
2537 :     }
2538 :    
2539 :    
2540 : overbeek 1.1 sub align_column {
2541 :     my($fig,$cgi,$html,$col,$subsystem) = @_;
2542 :     my($colN,@checked,$cutoff);
2543 :    
2544 :     my $checked;
2545 :     my $roles = [$subsystem->get_roles];
2546 : redwards 1.22
2547 : overbeek 1.1 if (($colN = &which_column($col,$roles)) &&
2548 :     ((@checked = &seqs_to_align($colN,$subsystem)) > 1))
2549 :     {
2550 :     if ($cutoff = $cgi->param('include_homo'))
2551 :     {
2552 :     my $max = $cgi->param('max_homo');
2553 :     $max = $max ? $max : 100;
2554 :     push(@checked,&get_homologs($fig,\@checked,$cutoff,$max));
2555 :     }
2556 :     $checked = join("\' \'",@checked);
2557 :     }
2558 :     else
2559 :     {
2560 :     push(@$html,"<h1>You need to check at least two sequences</h1>\n");
2561 :     return;
2562 :     }
2563 :    
2564 :    
2565 :     #
2566 :     # See if we want to produce the alignment, or just produce the
2567 :     # input to the alignment.
2568 :     #
2569 :    
2570 :     if ($cgi->param("show_align_input"))
2571 :     {
2572 :     push(@$html, "<pre>\n");
2573 :     my $relabel;
2574 :     foreach my $id (@checked)
2575 :     {
2576 :     my $seq;
2577 :     if ($seq = $fig->get_translation($id))
2578 :     {
2579 :     push(@$html, ">$id\n$seq\n");
2580 :     my $func = $fig->function_of($id);
2581 :     $relabel->{$id} = "$id: $func";
2582 :     }
2583 :     else
2584 :     {
2585 :     push(@$html, "could not find translation for $id\n");
2586 :     }
2587 :     }
2588 :     push(@$html, "\n</pre>\n");
2589 :     }
2590 :     else
2591 :     {
2592 :     push(@$html,"<pre>\n");
2593 :     my %org = map { ( $_, $fig->org_of($_) ) } @checked;
2594 :     # Modified by GJO to compress tree and add organism names to tree:
2595 :     # push(@$html,`$FIG_Config::bin/align_with_clustal -org -func -tree \'$checked\'`);
2596 :    
2597 :     # Simpler version
2598 :     # push @$html, map { chomp;
2599 :     # /^ *\|[ |]*$/ # line that adds only tree height
2600 :     # ? () # remove it
2601 :     # : /- ([a-z]+\|\S+):/ && defined( $org{$1} ) # tree id?
2602 :     # ? "$_ [$org{$1}]\n" # add the name
2603 :     # : "$_\n" # otherwise leave unmodified
2604 :     # } `$FIG_Config::bin/align_with_clustal -org -func -tree \'$checked\'`;
2605 :    
2606 :     # More complex version the preserves double spaced tree tips
2607 :     my $tip = 0;
2608 :     my @out = ();
2609 : overbeek 1.43
2610 :     my $dir = "$FIG_Config::data/Subsystems/" .
2611 :     $cgi->param('ssa_name') .
2612 :     "/Alignments/" .
2613 :     $cgi->param('col_to_align');
2614 :    
2615 :     $dir =~ s/ /_/g;
2616 :     &FIG::verify_dir($dir);
2617 :     foreach ( `$FIG_Config::bin/align_with_clustal -org -func -tree -UniProt \'-save=$dir\' \'$checked\'` )
2618 : overbeek 1.1 {
2619 :     chomp;
2620 :     if ( /^ *\|[ |]*$/ ) {} # line that adds only tree height
2621 :     elsif ( /- ([a-z]+\|\S+):/ ) # line with tree tip
2622 :     {
2623 :     if ( defined( $org{$1} ) ) { $_ .= " [$org{$1}]" } # add org
2624 :     if ( $tip ) { push @out, " |\n" } # 2 tips in a row? add line
2625 :     push @out, "$_\n"; # output current line
2626 :     $tip = 1;
2627 :     }
2628 :     else # not a tip
2629 :     {
2630 :     push @out, "$_\n";
2631 :     $tip = 0;
2632 :     }
2633 :     }
2634 :     push(@$html,&set_links($cgi,\@out));
2635 :     push(@$html,"</pre>\n");
2636 :     }
2637 :     }
2638 :    
2639 :     sub which_column {
2640 :     my($col,$roles) = @_;
2641 :     my($i);
2642 :    
2643 :     if (($col =~ /^(\d+)/) && ($1 <= @$roles))
2644 :     {
2645 :     return $roles->[$1-1];
2646 :     }
2647 :     return undef;
2648 :     }
2649 :    
2650 :     sub seqs_to_align {
2651 :     my($role,$subsystem) = @_;
2652 :     my($genome);
2653 :    
2654 :     my $active_subsetR = ($cgi->param('active_subsetR') or $subsystem->get_active_subsetR );
2655 :     my @subsetR = $subsystem->get_subsetR($active_subsetR);
2656 :    
2657 :     my @seqs = ();
2658 :     foreach $genome (@subsetR)
2659 :     {
2660 :     push(@seqs,$subsystem->get_pegs_from_cell($genome,$role));
2661 :     }
2662 :     return @seqs;
2663 :     }
2664 :    
2665 :     sub get_homologs {
2666 :     my($fig,$checked,$cutoff,$max) = @_;
2667 :     my($peg,$sim,$id2);
2668 :    
2669 :     my @homologs = ();
2670 :     my %got = map { $_ => 1 } @$checked;
2671 :    
2672 :     foreach $peg (@$checked)
2673 :     {
2674 :     foreach $sim ($fig->sims($peg,$max,$cutoff,"fig"))
2675 :     {
2676 :     $id2 = $sim->id2;
2677 : overbeek 1.16 if ((! $got{$id2}) && ($id2 =~ /^fig\|(\d+\.\d+)/) && ($fig->is_complete($1)))
2678 : overbeek 1.1 {
2679 :     push(@homologs,[$sim->psc,$id2]);
2680 :     $got{$id2} = 1;
2681 :     }
2682 :     }
2683 :     }
2684 :     @homologs = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @homologs;
2685 :     if (@homologs > $max) { $#homologs = $max-1 }
2686 :    
2687 :     return @homologs;
2688 :     }
2689 :    
2690 :     sub set_links {
2691 :     my($cgi,$out) = @_;
2692 :    
2693 :     my @with_links = ();
2694 :     foreach $_ (@$out)
2695 :     {
2696 :     if ($_ =~ /^(.*)(fig\|\d+\.\d+\.peg\.\d+)(.*)$/)
2697 :     {
2698 :     my($before,$peg,$after) = ($1,$2,$3);
2699 :     push(@with_links, $before . &HTML::fid_link($cgi,$peg) . $after . "\n");
2700 :     }
2701 :     else
2702 :     {
2703 :     push(@with_links,$_);
2704 :     }
2705 :     }
2706 :     return @with_links;
2707 :     }
2708 :    
2709 :     sub reset_ssa {
2710 :     my($fig,$cgi,$html) = @_;
2711 :     my($ssa,@spreadsheets,$col_hdrs,$tab,$t,$readable,$url,$link,@tmp);
2712 :    
2713 :     if (($ssa = $cgi->param('ssa_name')) && opendir(BACKUP,"$FIG_Config::data/Subsystems/$ssa/Backup"))
2714 :     {
2715 :     @spreadsheets = sort { $b <=> $a }
2716 :     map { $_ =~ /^spreadsheet.(\d+)/; $1 }
2717 :     grep { $_ =~ /^spreadsheet/ }
2718 :     readdir(BACKUP);
2719 :     closedir(BACKUP);
2720 :     $col_hdrs = ["When","Number Genomes"];
2721 :     $tab = [];
2722 :     foreach $t (@spreadsheets)
2723 :     {
2724 :     $readable = &FIG::epoch_to_readable($t);
2725 :     $url = &FIG::cgi_url . "/subsys.cgi?user=$user&ssa_name=$ssa&request=reset_to&ts=$t";
2726 :     $link = "<a href=$url>$readable</a>";
2727 :     open(TMP,"<$FIG_Config::data/Subsystems/$ssa/Backup/spreadsheet.$t")
2728 :     || die "could not open $FIG_Config::data/Subsystems/$ssa/Backup/spreadsheet.$t";
2729 :     $/ = "//\n";
2730 :     $_ = <TMP>;
2731 :     $_ = <TMP>;
2732 :     $_ = <TMP>;
2733 :     chomp;
2734 :     $/ = "\n";
2735 :    
2736 :     @tmp = grep { $_ =~ /^\d+\.\d+/ } split(/\n/,$_);
2737 :     push(@$tab,[$link,scalar @tmp]);
2738 :     }
2739 :     }
2740 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Possible Points to Reset From"));
2741 :     }
2742 :    
2743 :     sub reset_ssa_to {
2744 :     my($fig,$cgi,$html) = @_;
2745 :     my($ts,$ssa);
2746 :    
2747 :     if (($ssa = $cgi->param('ssa_name')) &&
2748 :     ($ts = $cgi->param('ts')) &&
2749 :     (-s "$FIG_Config::data/Subsystems/$ssa/Backup/spreadsheet.$ts"))
2750 :     {
2751 :     system "cp -f $FIG_Config::data/Subsystems/$ssa/Backup/spreadsheet.$ts $FIG_Config::data/Subsystems/$ssa/spreadsheet";
2752 :     chmod(0777,"$FIG_Config::data/Subsystems/$ssa/spreadsheet");
2753 :     if (-s "$FIG_Config::data/Subsystems/$ssa/Backup/notes.$ts")
2754 :     {
2755 :     system "cp -f $FIG_Config::data/Subsystems/$ssa/Backup/notes.$ts $FIG_Config::data/Subsystems/$ssa/notes";
2756 :     chmod(0777,"$FIG_Config::data/Subsystems/$ssa/notes");
2757 :     }
2758 : overbeek 1.9
2759 :     my $subsystem = new Subsystem($ssa,$fig,0);
2760 :     $subsystem->db_sync(0);
2761 :     undef $subsystem;
2762 : overbeek 1.1 }
2763 :     }
2764 :    
2765 :     sub make_exchangable {
2766 :     my($fig,$cgi,$html) = @_;
2767 :     my($ssa);
2768 :    
2769 :     if (($ssa = $cgi->param('ssa_name')) &&
2770 :     (-s "$FIG_Config::data/Subsystems/$ssa/spreadsheet") &&
2771 :     open(TMP,">$FIG_Config::data/Subsystems/$ssa/EXCHANGABLE"))
2772 :     {
2773 :     print TMP "1\n";
2774 :     close(TMP);
2775 :     chmod(0777,"$FIG_Config::data/Subsystems/$ssa/EXCHANGABLE");
2776 :     }
2777 :     }
2778 :    
2779 :     sub make_unexchangable {
2780 :     my($fig,$cgi,$html) = @_;
2781 :     my($ssa);
2782 :    
2783 :     if (($ssa = $cgi->param('ssa_name')) &&
2784 :     (-s "$FIG_Config::data/Subsystems/$ssa/EXCHANGABLE"))
2785 :     {
2786 :     unlink("$FIG_Config::data/Subsystems/$ssa/EXCHANGABLE");
2787 :     }
2788 :     }
2789 : overbeek 1.14
2790 :     sub which_role {
2791 :     my($subsystem,$role_indicator) = @_;
2792 :     my($n,$role,$abbr);
2793 :    
2794 :     if (($role_indicator =~ /^\s*(\d+)\s*$/) && ($n = $1) && ($role = $subsystem->get_role($n-1)))
2795 :     {
2796 :     return $role;
2797 :     }
2798 :     elsif (($role_indicator =~ /^\s*(\S+)\s*$/) && ($abbr = $1) && ($role = $subsystem->get_role_from_abbr($abbr)))
2799 :     {
2800 :     return $role;
2801 :     }
2802 :     return "";
2803 :     }
2804 : overbeek 1.17
2805 :     sub external_id {
2806 :     my($fig,$cgi,$peg) = @_;
2807 :     my @tmp;
2808 :     my @aliases = ($fig->feature_aliases($peg),map { $_->[0] } $fig->mapped_prot_ids($peg));
2809 :     if ((@tmp = grep { $_ =~ /^uni\|/ } @aliases) > 0)
2810 :     {
2811 :     @aliases = map { &HTML::uni_link($cgi,$_) } @tmp;
2812 :     }
2813 :     elsif ((@tmp = grep { $_ =~ /^sp\|/ } @aliases) > 0)
2814 :     {
2815 :     @aliases = map { &HTML::sp_link($cgi,$_) } @tmp;
2816 :     }
2817 :     elsif ((@tmp = grep { $_ =~ /^gi\|/ } @aliases) > 0)
2818 :     {
2819 :     @aliases = map { &HTML::gi_link($cgi,$_) } @tmp;
2820 :     }
2821 :     elsif ((@tmp = grep { $_ =~ /^kegg\|/ } @aliases) > 0)
2822 :     {
2823 :     @aliases = map { &HTML::kegg_link($cgi,$_) } @tmp;
2824 :     }
2825 :     else
2826 :     {
2827 :     return wantarray() ? (&HTML::fid_link($cgi,$peg)) : &HTML::fid_link($cgi,$peg);
2828 :     }
2829 :    
2830 :     if (wantarray())
2831 :     {
2832 :     return @aliases;
2833 :     }
2834 :     else
2835 :     {
2836 :     return $aliases[0];
2837 :     }
2838 :     }
2839 : redwards 1.52
2840 :     sub cool_colors {
2841 :     # 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!)
2842 :     return (
2843 :     '#C0C0C0', '#FF40C0', '#FF8040', '#FF0080', '#FFC040', '#40C0FF', '#40FFC0', '#C08080', '#C0FF00', '#00FF80', '#00C040',
2844 :     "#6B8E23", "#483D8B", "#2E8B57", "#008000", "#006400", "#800000", "#00FF00", "#7FFFD4",
2845 :     "#87CEEB", "#A9A9A9", "#90EE90", "#D2B48C", "#8DBC8F", "#D2691E", "#87CEFA", "#E9967A", "#FFE4C4", "#FFB6C1",
2846 :     "#E0FFFF", "#FFA07A", "#DB7093", "#9370DB", "#008B8B", "#FFDEAD", "#DA70D6", "#DCDCDC", "#FF00FF", "#6A5ACD",
2847 :     "#00FA9A", "#228B22", "#1E90FF", "#FA8072", "#CD853F", "#DC143C", "#FF6347", "#98FB98", "#4682B4",
2848 :     "#D3D3D3", "#7B68EE", "#2F4F4F", "#FF7F50", "#FF69B4", "#BC8F8F", "#A0522D", "#DEB887", "#00DED1",
2849 :     "#6495ED", "#800080", "#FFD700", "#F5DEB3", "#66CDAA", "#FF4500", "#4B0082", "#CD5C5C",
2850 :     "#EE82EE", "#7CFC00", "#FFFF00", "#191970", "#FFFFE0", "#DDA0DD", "#00BFFF", "#DAA520", "#008080",
2851 :     "#00FF7F", "#9400D3", "#BA55D3", "#D8BFD8", "#8B4513", "#3CB371", "#00008B", "#5F9EA0",
2852 :     "#4169E1", "#20B2AA", "#8A2BE2", "#ADFF2F", "#556B2F",
2853 :     "#F0FFFF", "#B0E0E6", "#FF1493", "#B8860B", "#FF0000", "#F08080", "#7FFF00", "#8B0000",
2854 :     "#40E0D0", "#0000CD", "#48D1CC", "#8B008B", "#696969", "#AFEEEE", "#FF8C00", "#EEE8AA", "#A52A2A",
2855 :     "#FFE4B5", "#B0C4DE", "#FAF0E6", "#9ACD32", "#B22222", "#FAFAD2", "#808080", "#0000FF",
2856 :     "#000080", "#32CD32", "#FFFACD", "#9932CC", "#FFA500", "#F0E68C", "#E6E6FA", "#F4A460", "#C71585",
2857 :     "#BDB76B", "#00FFFF", "#FFDAB9", "#ADD8E6", "#778899",
2858 :     );
2859 :     }
2860 :    
2861 :     sub describe_colors {
2862 :     my ($tvc)=@_;
2863 :     my $tab = [];
2864 :     my @colors=&cool_colors();
2865 : redwards 1.58 my @labels=sort {$a cmp $b} keys %$tvc;
2866 : redwards 1.59 my $selfurl=$cgi->url();
2867 :     # recreate the url for the link
2868 :     $selfurl .= "?user=" . $cgi->param('user') . "&ssa_name=".$cgi->param('ssa_name') . "&request=" . $cgi->param('request') . "&can_alter=" . $cgi->param('can_alter');
2869 :    
2870 : redwards 1.52 my $row;
2871 :     for (my $i=0; $i<= scalar @labels; $i++) {
2872 :     next unless (defined $labels[$i]);
2873 : redwards 1.59 my $link='<a href="' . $selfurl . "&active_key=" . $cgi->param('color_by_ga') . "&active_value=" . $labels[$i] . '">' . $labels[$i] . "</a>\n";
2874 :     push @$row, [$link, "td style=\"background-color: $colors[$tvc->{$labels[$i]}]\""];
2875 : redwards 1.52 unless (($i+1) % 10) {
2876 :     push @$tab, $row;
2877 :     undef $row;
2878 :     }
2879 :     }
2880 :     push @$tab, $row;
2881 :     return $tab;
2882 :     }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3