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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3