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

Annotation of /FigWebServices/subsys.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3