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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3