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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.100 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3