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

Annotation of /FigWebServices/subsys_summary.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3