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

Annotation of /FigWebServices/diagram.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (view) (download)

1 : paarmann 1.1 #
2 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 :    
18 : parrello 1.9 # $Id: diagram.cgi,v 1.8 2008/04/29 08:03:55 parrello Exp $
19 : paarmann 1.2
20 : paarmann 1.1 use strict;
21 :     use warnings;
22 : paarmann 1.3 no warnings qw( numeric ); # variant code comparison with >
23 : paarmann 1.1
24 :     use FIG_CGI;
25 :     use FIG_Config;
26 :     use CGI;
27 :    
28 :     use Diagram;
29 :    
30 :     eval {
31 :     &main;
32 :     };
33 :    
34 :     if ($@)
35 :     {
36 :     my $cgi = new CGI();
37 :    
38 :     print $cgi->header();
39 :     print $cgi->start_html();
40 :     print "<pre>$@</pre>";
41 :     print $cgi->end_html();
42 :    
43 :     }
44 :    
45 :     sub main {
46 :    
47 :     my ($fig, $cgi, $user) = FIG_CGI::init(debug_save => 0, debug_load => 0, print_params => 0);
48 :    
49 :     # print out the page
50 :     print $cgi->header();
51 : paarmann 1.6 print "<html>\n<head>\n";
52 :     print "<title>The SEED - Subsystem Diagram</title>\n";
53 : parrello 1.9 print "<link rel='stylesheet' type='text/css' href='$FIG_Config::cgi_url/Html/default.css' />\n";
54 : paarmann 1.6 print "</head> \n <body> \n";
55 : paarmann 1.1
56 :     print &get_Diagram($fig, $cgi);
57 :    
58 : paarmann 1.2 print $cgi->end_html;
59 :    
60 : paarmann 1.1 }
61 :    
62 :    
63 :     sub get_Diagram {
64 :     # get parameters
65 :     my ($fig, $cgi) = @_;
66 :    
67 : paarmann 1.3 # get the subsystem
68 :     unless ($cgi->param('subsystem_name')) {
69 : paarmann 1.1 return '<p>CGI Parameter missing.</p>';
70 :     }
71 : paarmann 1.3 my $subsystem_name = $cgi->param('subsystem_name') || '';
72 : paarmann 1.1 my $subsystem_pretty = $subsystem_name;
73 :     $subsystem_pretty =~ s/_/ /g;
74 : paarmann 1.3 my $subsystem = $fig->get_subsystem($subsystem_name);
75 : paarmann 1.1
76 : paarmann 1.3 # check subsystem
77 :     unless ($subsystem) {
78 :     return "<p>Unable to find a subsystem called '$subsystem_name'.</p>";
79 :     }
80 : paarmann 1.1
81 :    
82 : paarmann 1.3 # if diagram.cgi is called without the CGI param diagram (the diagram id)
83 :     # it will try to load the first 'new' diagram from the subsystem and
84 :     # print out an error message if there is no 'new' diagram
85 :     my $diagram_id = $cgi->param('diagram') || '';
86 :     unless ($diagram_id) {
87 :     foreach my $d ($subsystem->get_diagrams) {
88 :     my ($id) = @$d;
89 :     if ($subsystem->is_new_diagram($id)) {
90 :     $diagram_id = $id;
91 :     last;
92 : paarmann 1.1 }
93 :     }
94 : paarmann 1.3 }
95 : paarmann 1.1
96 : paarmann 1.3 # check diagram id
97 :     unless ($diagram_id) {
98 :     return "<h1>Subsystem: $subsystem_pretty</h1>".
99 :     "<p><em>Unable to find a diagram for this subsystem.</em><p>";
100 :     }
101 :     unless ($subsystem->is_new_diagram($diagram_id)) {
102 :     return "<h1>Subsystem: $subsystem_pretty</h1>".
103 :     "<p><em>Diagram '$diagram_id' is not a new diagram.</em><p>";
104 :     }
105 : paarmann 1.1
106 :    
107 : paarmann 1.6 # find out about sort order
108 :     my $sort_by = $cgi->param('sort_by') || 'name';
109 :    
110 : paarmann 1.7 # get the genomes from the subsystem
111 : paarmann 1.3 my @genomes;
112 :     my $genome = $cgi->param('genome_id');
113 : paarmann 1.6 if ($sort_by eq 'variant_code') {
114 : paarmann 1.7 @genomes = sort { ($subsystem->get_variant_code( $subsystem->get_genome_index($a) ) cmp
115 :     $subsystem->get_variant_code( $subsystem->get_genome_index($b) )) or
116 :     ( $fig->genus_species($a) cmp $fig->genus_species($b) )
117 :     } $subsystem->get_genomes()
118 : paarmann 1.6 }
119 :     else {
120 : paarmann 1.7 @genomes = sort { $fig->genus_species($a) cmp $fig->genus_species($b) } $subsystem->get_genomes();
121 :     }
122 :    
123 :     # show only genomes with zero or positive variant codes
124 :     # unless user switched that off
125 :     unless ($cgi->param('show_negative')) {
126 :     my @temp;
127 :     foreach (@genomes) {
128 :     my $vcode = $subsystem->get_variant_code( $subsystem->get_genome_index( $_ ) );
129 :     push @temp, $_ if ($vcode >= 0);
130 : paarmann 1.6 }
131 : paarmann 1.7 @genomes = @temp;
132 : paarmann 1.3 }
133 :    
134 : paarmann 1.4 my %genome_labels = map { $_ => $fig->genus_species($_)." ( $_ ) [".
135 :     $subsystem->get_variant_code( $subsystem->get_genome_index( $_ ) )."]"
136 :     } @genomes;
137 : paarmann 1.6
138 :     @genomes = ('0', @genomes);
139 :     $genome_labels{'0'} = 'please select a genome to color the diagram with' ;
140 : paarmann 1.3
141 :    
142 :     # generate the content
143 :     my $content = "<h1>Subsystem: $subsystem_pretty</h1>";
144 :     $content .= '<hr/>';
145 :    
146 : parrello 1.8 $content .= $cgi->start_form( -id => 'diagram_select_genome', -action => 'diagram.cgi' );
147 : paarmann 1.3 $content .= $cgi->hidden( -name => 'subsystem_name',
148 :     -value => $subsystem_name );
149 :     $content .= $cgi->hidden( -name => 'diagram',
150 :     -value => $diagram_id );
151 : paarmann 1.5 $content .= $cgi->hidden( -name => 'dont_scale', -value => 1 )
152 :     if ($cgi->param('dont_scale'));
153 : paarmann 1.7 $content .= $cgi->hidden( -name => 'show_negative', -value => 1 )
154 :     if ($cgi->param('show_negative'));
155 : paarmann 1.5 $content .= $cgi->hidden( -name => 'debug', -value => 1 )
156 :     if ($cgi->param('debug'));
157 : paarmann 1.6 $content .= '<p>Sort by: '.
158 :     $cgi->radio_group( -name => 'sort_by',
159 :     -values => ['name', 'variant_code'],
160 :     -default => $sort_by,
161 :     -labels => { 'name' => 'Genome name',
162 :     'variant_code' => 'Variant code, then genome name' },
163 :     -onChange => 'document.getElementById("diagram_select_genome").submit();',
164 : paarmann 1.7 ).' &nbsp; | &nbsp; ';
165 :     $content .= ''.$cgi->checkbox( -name => 'show_negative',
166 :     -value => 1,
167 :     -label => 'Show genomes with negative variant codes',
168 :     -onChange => 'document.getElementById("diagram_select_genome").submit();',
169 : paarmann 1.6 ).'</p>';
170 : paarmann 1.3 $content .= $cgi->popup_menu( -name => 'genome_id',
171 :     -values => \@genomes,
172 :     -default => $genome,
173 :     -labels => \%genome_labels,
174 :     );
175 :     $content .= $cgi->submit( -name => 'Color diagram' );
176 :     $content .= $cgi->end_form();
177 :    
178 :     # initialise a status string (log)
179 :     my $status = '';
180 :    
181 :     # fetch the diagram
182 :     my $diagram_dir = $subsystem->{dir}."/diagrams/$diagram_id/";
183 :     my $d = Diagram->new($subsystem_name, $diagram_dir);
184 : paarmann 1.5
185 :     # turn off scaling?
186 :     $d->min_scale(1) if ($cgi->param('dont_scale'));
187 : paarmann 1.3
188 :    
189 :     # DEBUG: test all items of the diagram against the subsystem
190 :     # (for debug purposes during introduction of new diagrams)
191 :     # (remove when no longer needed)
192 :     # (1) roles
193 :     my $types = [ 'role', 'role_and', 'role_or' ];
194 :     foreach my $t (@$types) {
195 :     foreach my $id (@{$d->item_ids_of_type($t)}) {
196 :     unless ($subsystem->get_role_from_abbr($id) or
197 :     scalar($subsystem->get_subsetC_roles($id))) {
198 :     $status .= "Diagram item '$t' = '$id' not found in the subsystem.\n";
199 :     }
200 :     }
201 :     }
202 :     # (2) subsystem
203 :     foreach my $s (@{$d->item_ids_of_type('subsystem')}) {
204 :     unless ($fig->subsystem_version($s)) {
205 :     $status .= "Diagram item 'subsystem' = '$s' is not a subsystem.\n";
206 :     }
207 :     }
208 :     # END
209 : paarmann 1.6
210 :    
211 :     # add notes to roles
212 :     # to reduce the total number of loos role_or, role_and get their notes
213 :     # attached in the loops further down
214 :     foreach my $id (@{$d->item_ids_of_type('role')}) {
215 :     my $role = $subsystem->get_role_from_abbr($id);
216 :     if ($role) {
217 :     $d->add_note('role', $id, $role);
218 :     }
219 :     }
220 :    
221 : paarmann 1.3
222 : paarmann 1.5 # build a lookup hash, make one entry for each role_and and role_or item
223 :     # the index references to the inner hash of the role_and/role_or hash
224 :     # to set a value there use $lookup->{role_abbr}->{role_abbr} = 1;
225 :     # declared outside if to be available for debug output
226 :     my $lookup = {};
227 :    
228 : paarmann 1.6 # find out about role_and
229 :     my $role_and = {};
230 :     if (scalar(@{$d->item_ids_of_type('role_and')})) {
231 :     foreach my $subset (@{$d->item_ids_of_type('role_and')}) {
232 :    
233 :     $role_and->{$subset} = {};
234 :    
235 :     my $note = '';
236 :     foreach my $r ($subsystem->get_subsetC_roles($subset)) {
237 :     my $r_abbr = $subsystem->get_abbr_for_role($r);
238 :     unless ($r_abbr) {
239 :     die "Unable to get the abbreviation for role '$r'.";
240 :     }
241 : paarmann 1.3
242 : paarmann 1.6 $note .= "<li>$r</li>";
243 :     $lookup->{$r_abbr} = $role_and->{$subset};
244 :     $role_and->{$subset}->{$r_abbr} = 0;
245 : paarmann 1.1 }
246 : paarmann 1.6 $d->add_note('role_and', $subset, "<h4>Requires all of:</h4><ul>$note</ul>");
247 : paarmann 1.3 }
248 : paarmann 1.6 }
249 :    
250 :     # find out about role_or
251 :     my $role_or = {};
252 :     if (scalar(@{$d->item_ids_of_type('role_or')})) {
253 :     foreach my $subset (@{$d->item_ids_of_type('role_or')}) {
254 :    
255 :     $role_or->{$subset} = {};
256 :    
257 :     my $note = '';
258 :     foreach my $r ($subsystem->get_subsetC_roles($subset)) {
259 :     my $r_abbr = $subsystem->get_abbr_for_role($r);
260 : paarmann 1.1
261 : paarmann 1.6 unless ($r_abbr) {
262 :     die "Unable to get the abbreviation for role '$r'.";
263 : paarmann 1.1 }
264 : paarmann 1.6
265 :     $note .= "<li>$r</li>";
266 :     $lookup->{$r_abbr} = $role_or->{$subset};
267 :     $role_or->{$subset}->{$r_abbr} = 0;
268 : paarmann 1.1 }
269 : paarmann 1.6 $d->add_note('role_or', $subset, "<h4>Requires any of:</h4><ul>$note</ul>");
270 : paarmann 1.3 }
271 : paarmann 1.6 }
272 :    
273 :    
274 :     if ($genome) {
275 : paarmann 1.3
276 : paarmann 1.6 my @roles = $subsystem->get_roles_for_genome($genome);
277 : paarmann 1.3
278 :     # check if genome is present in subsystem
279 :     # genomes not present, unfortunately return @roles = ( undef )
280 :     if (scalar(@roles) == 0 or
281 :     (scalar(@roles) and !defined($roles[0]))) {
282 :     $content .= "<p><em>Genome '$genome' is not present in this subsystem.</em><p>";
283 :     shift(@roles);
284 :     }
285 :     else {
286 : paarmann 1.4 $content .= "<p><em>Showing colors for genome: ".
287 :     $fig->genus_species($genome)." ( $genome ), variant code ".
288 :     $subsystem->get_variant_code($subsystem->get_genome_index($genome)) ."</em><p>";
289 : paarmann 1.3 }
290 :    
291 :    
292 :     # iterate over all roles present in a subsystem:
293 :     # -> map roles to abbr in the foreach loop
294 :     # -> color simple roles present
295 :     # -> tag roles being part of a logical operator in $lookup
296 :     foreach (map { $subsystem->get_abbr_for_role($_) } @roles) {
297 : paarmann 1.1
298 : paarmann 1.3 # color normal roles
299 :     if ($d->has_item('role', $_)) {
300 :     $d->color_item('role',$_,'green');
301 :     next;
302 :     }
303 : paarmann 1.1
304 : paarmann 1.3 # try to find role_and / role_or
305 :     if (exists($lookup->{$_})) {
306 :     $lookup->{$_}->{$_} = 1;
307 :     next;
308 : paarmann 1.1 }
309 : paarmann 1.3
310 :     $status .= "Role '$_' not found in the diagram.\n";
311 : paarmann 1.1 }
312 : paarmann 1.3
313 :     # check if to color any role_and
314 :     foreach my $id_role_and (keys(%$role_and)) {
315 :     my $result = 1;
316 :     foreach (keys(%{$role_and->{$id_role_and}})) {
317 :     $result = 0 unless ($role_and->{$id_role_and}->{$_});
318 :     }
319 :     $d->color_item('role_and', $id_role_and, 'green') if ($result);
320 : paarmann 1.1 }
321 : paarmann 1.5
322 :     # check if to color any role_or
323 :     foreach my $id_role_or (keys(%$role_or)) {
324 :     foreach (keys(%{$role_or->{$id_role_or}})) {
325 :     if ($role_or->{$id_role_or}->{$_}) {
326 :     $d->color_item('role_or', $id_role_or, 'green');
327 :     last;
328 :     }
329 :     }
330 :     }
331 :    
332 : paarmann 1.3 }
333 :     else {
334 :     $content .= '<p><em>You have not provided a genome id to color the diagram with.</em><p>';
335 : paarmann 1.1 }
336 :    
337 : paarmann 1.5 # add an info line about diagram scaling
338 :     my $scale = $d->calculate_scale * 100;
339 :     unless ($scale == 100) {
340 :     $content .= '<p><em>This diagram has been scaled to '.sprintf("%.2f", $scale).'%. ';
341 : paarmann 1.7 $content .= "(<a href='?subsystem_name=$subsystem_name&diagram=$diagram_id&dont_scale=1'>".
342 : paarmann 1.5 "view in original size</a>)";
343 :     $content .= '</em></p>';
344 :     }
345 :     if ($cgi->param('dont_scale')) {
346 :     $content .= '<p><em>You have switched off scaling this diagram down. ';
347 : paarmann 1.7 $content .= "(<a href='?subsystem_name=$subsystem_name&diagram=$diagram_id'>".
348 : paarmann 1.5 "Allow scaling</a>)";
349 :     $content .= '</em></p>';
350 :     }
351 :    
352 :     # print diagram
353 : paarmann 1.3 $content .= $d->html;
354 : paarmann 1.5
355 :     # print status
356 : paarmann 1.3 $content .= '<hr/><p><em>Below follows a status message to help test the new diagrams:</em><p>'.
357 :     "<pre>$status</pre>" if ($status);
358 :    
359 : paarmann 1.5 # print debug
360 :     if ($cgi->param('debug')) {
361 :     require Data::Dumper;
362 :     $content .= '<hr/>';
363 :     $content .= "<h2>Diagram dump:</h2><pre>".Data::Dumper->Dump([ $d ])."</pre>";
364 :     $content .= "<h2>Lookup dump:</h2><pre>".Data::Dumper->Dump([ $lookup ])."</pre>";
365 :     }
366 :    
367 : paarmann 1.1 return $content;
368 :     }
369 :    
370 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3