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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.93 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3