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

Annotation of /FigWebServices/subsys_summary.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (view) (download)

1 : redwards 1.1
2 :     =pod
3 :    
4 :     =head1
5 :    
6 :     Create a summary of subsystems
7 :    
8 :     =cut
9 :    
10 :     use strict;
11 :     use FIG;
12 :     use HTML;
13 :     use raelib;
14 :     use CGI;
15 :     my $cgi=new CGI;
16 :    
17 :    
18 :     my $fig;
19 :     eval {
20 :     $fig = new FIG;
21 :     };
22 :    
23 :     if ($@ ne "")
24 :     {
25 :     my $err = $@;
26 :    
27 :     my(@html);
28 :    
29 :     push(@html, $cgi->p("Error connecting to SEED database."));
30 :     if ($err =~ /Could not connect to DBI:.*could not connect to server/)
31 :     {
32 :     push(@html, $cgi->p("Could not connect to relational database of type $FIG_Config::dbms named $FIG_Config::db on port $FIG_Config::dbport."));
33 :     }
34 :     else
35 :     {
36 :     push(@html, $cgi->pre($err));
37 :     }
38 :     &HTML::show_page($cgi, \@html, 1);
39 :     exit;
40 :     }
41 :    
42 :    
43 :    
44 :     $ENV{"PATH"} = "$FIG_Config::bin:$FIG_Config::ext_bin:" . $ENV{"PATH"};
45 :    
46 :     my $html = [];
47 :    
48 :    
49 :     if ($cgi->param('submit') eq "Summarize Subsystems")
50 :     {
51 :     summarize_ss();
52 :     }
53 : redwards 1.18 elsif ($cgi->param('submit') eq "Save Selections")
54 :     {
55 :     save_data();
56 :     }
57 : redwards 1.1 else
58 :     {
59 :     show_initial();
60 :     }
61 :    
62 :     unshift @$html, "<TITLE>The SEED - Subsystem summarizer </TITLE>\n";
63 :     &HTML::show_page($cgi, $html, 1);
64 :     exit;
65 :    
66 :    
67 :    
68 :    
69 :    
70 :    
71 :    
72 :    
73 :    
74 :    
75 :     sub summarize_ss {
76 :     my @wants;
77 :     foreach my $w ($cgi->param('korgs')) {
78 :     $w =~ /\((\d+\.\d+)\)/;
79 :     push @wants, $1;
80 :     }
81 :    
82 :    
83 : redwards 1.5 my @wantheaders;
84 :     foreach my $gen (@wants) {push @wantheaders, $fig->abbrev($fig->genus_species($gen))}
85 :    
86 : redwards 1.1 my @th;
87 : redwards 1.2 if ($cgi->param('levels')) {push @th, ('Level 1', 'Level 2')}
88 :     push @th, 'Subsystem'; # we should have this, at least
89 :     if ($cgi->param('genomes')) {push @th, ('Organisms')}
90 : redwards 1.9 if ($cgi->param('stats')) {push @th, ('Non-zero variants', 'Roles', 'Total Pegs', @wantheaders)}
91 : redwards 1.5 elsif ($cgi->param('korgs')) {push @th, ('Roles', @wantheaders)}
92 : redwards 1.1
93 :     my $tableheader="<tr><th>". (join "</th><th>", @th) . "</th></tr>\n\n";
94 :    
95 :     my $cols=scalar @th;
96 :     my $table;
97 :     foreach my $subsys ($fig->all_subsystems) {
98 :    
99 :     # figure out how many roles we have, as this will determine the number of rows in the table
100 :     my @roles=$fig->subsystem_to_roles($subsys);
101 :     my $rows=1;
102 : redwards 1.3 if (!$cgi->param('stats') && $cgi->param('korgs')) {$rows=scalar @roles}
103 : redwards 1.1
104 :     # figure out which genomes are in this subsystem
105 :     my %genomes;
106 : redwards 1.14 foreach my $gen ($fig->get_subsystem($subsys)->get_genomes()) {$genomes{$gen}=$fig->genus_species($gen)}
107 : redwards 1.1
108 : redwards 1.2 # start a new row for this data
109 : redwards 1.3 # tablerows is a refence to an array.
110 : redwards 1.4 # each element is an array of tablecells with the following values value colspan rowspan width
111 :     # if colspan or rowspan are undef they are believed to be 1
112 :     # if width is ommitted it is ignored
113 :    
114 : redwards 1.3 my $tablerows;
115 : redwards 1.1 # get the classification if required
116 :     if ($cgi->param('levels')) {
117 : redwards 1.2 # get the classification and make sure there are now 2 columns as andrei doesn't like #3.
118 : redwards 1.1 my $class=$fig->subsystem_classification($subsys);
119 : redwards 1.2 unless ($class) {@$class=(' &nbsp; ',' &nbsp; ')}
120 :     unless ($$class[0]) {$$class[0] = " &nbsp; "}
121 :     unless ($$class[1]) {$$class[1] = " &nbsp; "}
122 :     # remove anything more than $$class[1];
123 :     @$class=($$class[0], $$class[1]); # could have done this with splice. Ech.
124 :    
125 : redwards 1.3 foreach my $c (@$class) {
126 : redwards 1.5 push @$tablerows, [$c, 1, $rows, 20];
127 : redwards 1.3 }
128 : redwards 1.1 }
129 :    
130 : redwards 1.2 # add the subsystem to the table
131 : redwards 1.12 my $link=&HTML::sub_link($cgi,$subsys);
132 :     push @$tablerows, [$link, '1', $rows];
133 : redwards 1.2
134 : redwards 1.1 # add the genomes in the subsystem
135 : redwards 1.4 if ($cgi->param('genomes') && $cgi->param('stats')) {push @$tablerows, [(scalar keys %genomes), '1', $rows]}
136 : redwards 1.3 elsif ($cgi->param('genomes')) {
137 :     my $c=join "<br>\n", sort {$a cmp $b} values %genomes;
138 : redwards 1.4 push @$tablerows, [$c, 1, $rows];
139 : redwards 1.3 }
140 : redwards 1.2
141 : redwards 1.3 #
142 :     # This is convolvuted because we may want to skip some empty cells, but we won't know whether
143 :     # we want to skip them. Also, we are using rowspan/colspan to determine the size of the cells
144 :     # so we need everything defined
145 :     #
146 :     # I store the table initially in @$tablerows, and then push it into @$table. Later on we
147 :     # reconstruct @$table with <td> etc....
148 :     #
149 :     # Now I am going to store the rows in @$toadd, and if we want them I will add them to @$table.
150 :     #
151 :     # For stats, there is only one row in @$toadd, but for other data there are several because
152 :     # of the use of rowspan
153 :    
154 :     my $pegcount;
155 :     my $toadd; #this is what we will add if we need to
156 : redwards 1.2 if ($cgi->param('stats')) {
157 : redwards 1.3 my $totalpegs;
158 : redwards 1.9 my $nzv=0; # non-zero variant codes
159 : redwards 1.2 # there should be a quicker way of getting this ... ?
160 : redwards 1.16 foreach my $genome (keys %genomes) {
161 :     foreach my $role (@roles) {
162 : redwards 1.2 my $count = scalar ($fig->pegs_in_subsystem_cell($subsys, $genome, $role));
163 :     $pegcount->{$genome} += $count;
164 :     $totalpegs += $count;
165 :     }
166 : redwards 1.16 my $vc=$fig->get_subsystem($subsys)->get_variant_code_for_genome($genome);
167 :     unless ($vc == 0) {$nzv++}
168 : redwards 1.2 }
169 : redwards 1.9
170 :     # add non-zero variant codes
171 :     push @$tablerows, [$nzv, 1, $rows];
172 :     # add roles
173 :     push @$tablerows, [(scalar @roles), '1', $rows];
174 :     # add total pegs
175 : redwards 1.4 push @$tablerows, [$totalpegs, '1', $rows];
176 : redwards 1.2 foreach my $w (@wants) {
177 : redwards 1.4 push @$tablerows, [$pegcount->{$w}, '1', $rows]; # pegs for each of the chosen genomes
178 : redwards 1.2 }
179 : redwards 1.3 push @$toadd, $tablerows;
180 : redwards 1.2 }
181 : redwards 1.3 elsif ($cgi->param('korgs')) {
182 : redwards 1.1 my $first=1;
183 :     foreach my $role (@roles) {
184 :     my $genomeroles;
185 :     foreach my $gen (@wants) {
186 : redwards 1.2 my $cell = ' &nbsp; ';
187 : redwards 1.1 if ($genomes{$gen}) {
188 :     foreach my $peg ($fig->pegs_in_subsystem_cell($subsys, $gen, $role)) {
189 : redwards 1.6 $cell .= &HTML::fid_link($cgi,$peg, 'local') . "; ";
190 : redwards 1.1 }
191 :     }
192 : redwards 1.4 push @$genomeroles, [$cell, 1, 1];
193 : redwards 1.1 }
194 :     # because I am using rowspan to make long columns, we don't want the tr on the first of these.
195 : redwards 1.3 if ($first) {
196 : redwards 1.7 push @$tablerows, [$role, 1, 1], @$genomeroles;
197 : redwards 1.8 push @$toadd, $tablerows;
198 : redwards 1.4 undef $first;
199 : redwards 1.3 }
200 :     else {
201 : redwards 1.4 my @tr=([$role, 1, 1], @$genomeroles);
202 : redwards 1.8 push @$toadd, \@tr;
203 : redwards 1.3 }
204 : redwards 1.1 }
205 :     }
206 : redwards 1.5
207 :     unless ($toadd) {
208 : redwards 1.8 push @$toadd, $tablerows;
209 : redwards 1.5 } # in case we didn't define it
210 :    
211 :    
212 : redwards 1.3 # now decide if we want the table
213 :     if ($cgi->param('orgpegs')) {
214 :     my $mustadd;
215 :     foreach my $org (@wants) {if ($pegcount->{$org}) {$mustadd=1}} # we'll add it if one of the orgs has a role
216 : redwards 1.8 if ($mustadd) {push @$table, @$toadd}
217 : redwards 1.3 }
218 :     elsif ($cgi->param("haveroles")) {
219 : redwards 1.8 if (scalar(@roles)) {push @$table, @$toadd}
220 : redwards 1.3 }
221 :     else {
222 : redwards 1.8 push @$table, @$toadd;
223 : redwards 1.3 }
224 :     }
225 :    
226 :     # now convert $table into a table
227 : redwards 1.15
228 : redwards 1.4 # sort the table
229 :     # if we have levels we want to sort on level 1 and then level 2 and then subsysname
230 :     # if not, we just sort on subsys name
231 : redwards 1.17 if ($cgi->param("levels") && !$cgi->param('korgs')) {
232 : redwards 1.4 @$table = sort {
233 :     $a->[0]->[0] cmp $b->[0]->[0]
234 :     ||
235 :     $a->[1]->[0] cmp $b->[1]->[0]
236 :     ||
237 :     $a->[2]->[0] cmp $b->[2]->[0]
238 :     } @$table;
239 :     }
240 : redwards 1.17 elsif (!$cgi->param('korgs')) {
241 : redwards 1.4 @$table = sort {
242 :     $a->[0]->[0] cmp $b->[0]->[0]
243 :     } @$table;
244 :     }
245 :    
246 :    
247 : redwards 1.3 my $tab;
248 :     foreach my $row (@$table) {
249 :     $tab .= "\n<tr>";
250 :     foreach my $cell (@$row) {
251 : redwards 1.5 my $width='';
252 : redwards 1.6 if ($cell->[3]) {$width="width=" . $cell->[3]}
253 : redwards 1.5 $tab .= "\n\t<td valign=top $width colspan=$cell->[1] rowspan=$cell->[2]>$cell->[0]</td>";
254 : redwards 1.3 }
255 :     $tab .= "\n</tr>\n";
256 : redwards 1.1 }
257 : redwards 1.3
258 :     push @$html, ("\n<table border=1>" . $tableheader . $tab . "</table>\n");
259 : redwards 1.1 }
260 :    
261 :    
262 :    
263 :    
264 :    
265 :    
266 :    
267 :    
268 :    
269 :    
270 :     sub show_initial {
271 :     #############
272 :     #
273 :     # Stolen from index.cgi
274 :     #
275 :     #
276 : redwards 1.13
277 :    
278 : redwards 1.1 my @display = ( 'All', 'Archaea', 'Bacteria', 'Eucarya', 'Viruses', 'Environmental samples' );
279 :    
280 :     #
281 :     # Canonical names must match the keywords used in the DBMS. They are
282 :     # defined in compute_genome_counts.pl
283 :     #
284 :     my %canonical = (
285 :     'All' => undef,
286 :     'Archaea' => 'Archaea',
287 :     'Bacteria' => 'Bacteria',
288 :     'Eucarya' => 'Eukaryota',
289 :     'Viruses' => 'Virus',
290 :     'Environmental samples' => 'Environmental Sample'
291 :     );
292 :    
293 :     my $req_dom = $cgi->param( 'domain' ) || 'All';
294 :     my @domains = $cgi->radio_group( -name => 'domain',
295 :     -default => $req_dom,
296 :     -override => 1,
297 :     -values => [ @display ]
298 :     );
299 :    
300 :     my $n_domain = 0;
301 :     my %dom_num = map { ( $_, $n_domain++ ) } @display;
302 :     my $req_dom_num = $dom_num{ $req_dom } || 0;
303 :    
304 :     #
305 :     # Viruses and Environmental samples must have completeness = All (that is
306 :     # how they are in the database). Otherwise, default is Only "complete".
307 :     #
308 :     my $req_comp = ( $req_dom_num > $dom_num{ 'Eucarya' } ) ? 'All'
309 :     : $cgi->param( 'complete' ) || 'Only "complete"';
310 :     my @complete = $cgi->radio_group( -name => 'complete',
311 :     -default => $req_comp,
312 :     -override => 1,
313 :     -values => [ 'All', 'Only "complete"' ]
314 :     );
315 :     #
316 :     # Use $fig->genomes( complete, restricted, domain ) to get org list:
317 :     #
318 :     my $complete = ( $req_comp =~ /^all$/i ) ? undef : "complete";
319 :    
320 :     my @orgs = sort map { my $org = $_; my $gs = $fig->genus_species($org); my $gc=scalar $fig->all_contigs($org); "$gs ($org) [$gc contigs]" }
321 :     $fig->genomes( $complete, undef, $canonical{ $req_dom } );
322 :    
323 :     my $n_genomes = @orgs;
324 :    
325 :     push @$html, $cgi->start_form,
326 :     $cgi->h2("Please choose one or more organisms from the list below to see in the subsystems table"),
327 :     "<TABLE>\n",
328 :     " <TR>\n",
329 :     " <TD>",
330 :     $cgi->scrolling_list( -name => 'korgs',
331 :     -size => 10,
332 :     -multiple => 1,
333 :     -values => \@orgs,
334 :     ),
335 :     $cgi->br,
336 :     "$n_genomes genomes shown ",
337 :     $cgi->submit( 'Update List' ), $cgi->reset, $cgi->submit('submit', 'Summarize Subsystems'),
338 :     "</TD>",
339 :     " <TD>",
340 :     join( "<br>", "<b>Domain(s) to show:</b>", @domains), "<br>\n",
341 :     join( "<br>", "<b>Completeness?</b>", @complete), "\n",
342 :     "</TD>",
343 :     " </TR>\n",
344 :     "</TABLE>\n",
345 :     $cgi->p,
346 : redwards 1.14 "Enter user: ", $cgi->textfield(-name=>'user', size=>20), $cgi->p,
347 : redwards 1.2 $cgi->checkbox(-name=>"stats", -label=>"Only show statistics", -checked=>'on'), $cgi->p,
348 : redwards 1.1 $cgi->checkbox(-name=>"levels", -label=>"Show subsystem classification", -checked=>'on'), $cgi->p,
349 :     $cgi->checkbox(-name=>"genomes", -label=>"Show genomes in subsystem", -checked=>'on'), $cgi->p,
350 : redwards 1.4 $cgi->checkbox(-name=>"haveroles", -label=>"Show only subsystems that have roles defined"), $cgi->p,
351 :     $cgi->checkbox(-name=>"orgpegs", -label=>"Show only subsystems that have roles in the organism(s) you have chosen"), $cgi->p,
352 : redwards 1.3
353 : redwards 1.1 $cgi->end_form,
354 :    
355 :     }
356 : redwards 1.18
357 :    
358 :     sub save_data {
359 :     my $max=$cgi->param('max');
360 : redwards 1.19 open (OUT, ">>/home/fig/for_annotators/chosen_subsystems.txt") || die "Can't open /home/fig/for_annotators/chosen_subsystems.txt";
361 : redwards 1.18 my $txt;
362 :     for (my $i=1; $i<=$max; $i++) {
363 :     print OUT "$i\t", $cgi->param("ss$i"), "\n";
364 : redwards 1.19 $txt.=$cgi->param('user'),"\t$i\t", $cgi->param("ss$i"), "\n";
365 : redwards 1.18 }
366 :     push @$html, $cgi->"These were chosen<p><pre>$txt</pre><p>\n";
367 :     }
368 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3