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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.87 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3