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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.111 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3