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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3