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

Annotation of /FigWebServices/subsys_hope.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3