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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.103 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3