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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.61 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3