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

Annotation of /FigWebServices/subsys_summary.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3