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

Annotation of /FigWebServices/diagram.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (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 : paarmann 1.5 # $Id: diagram.cgi,v 1.4 2007/03/05 17:14:09 paarmann 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 :     my $css = qq~body {
50 :     font-family: Verdana, Arial, sans-serif;
51 :     font-size: 12px;
52 :     font-weight: normal;
53 :     color: #000;
54 :     background-color: #FFFFFF;
55 :     }~;
56 :    
57 :     # print out the page
58 :     print $cgi->header();
59 :     print $cgi->start_html(-title => 'The SEED - Subsystem Diagram',
60 :     -style => { -code => $css }
61 :     );
62 :    
63 :     print &get_Diagram($fig, $cgi);
64 :    
65 : paarmann 1.2 print $cgi->end_html;
66 :    
67 : paarmann 1.1 }
68 :    
69 :    
70 :     sub get_Diagram {
71 :     # get parameters
72 :     my ($fig, $cgi) = @_;
73 :    
74 : paarmann 1.3 # get the subsystem
75 :     unless ($cgi->param('subsystem_name')) {
76 : paarmann 1.1 return '<p>CGI Parameter missing.</p>';
77 :     }
78 : paarmann 1.3 my $subsystem_name = $cgi->param('subsystem_name') || '';
79 : paarmann 1.1 my $subsystem_pretty = $subsystem_name;
80 :     $subsystem_pretty =~ s/_/ /g;
81 : paarmann 1.3 my $subsystem = $fig->get_subsystem($subsystem_name);
82 : paarmann 1.1
83 : paarmann 1.3 # check subsystem
84 :     unless ($subsystem) {
85 :     return "<p>Unable to find a subsystem called '$subsystem_name'.</p>";
86 :     }
87 : paarmann 1.1
88 :    
89 : paarmann 1.3 # if diagram.cgi is called without the CGI param diagram (the diagram id)
90 :     # it will try to load the first 'new' diagram from the subsystem and
91 :     # print out an error message if there is no 'new' diagram
92 :     my $diagram_id = $cgi->param('diagram') || '';
93 :     unless ($diagram_id) {
94 :     foreach my $d ($subsystem->get_diagrams) {
95 :     my ($id) = @$d;
96 :     if ($subsystem->is_new_diagram($id)) {
97 :     $diagram_id = $id;
98 :     last;
99 : paarmann 1.1 }
100 :     }
101 : paarmann 1.3 }
102 : paarmann 1.1
103 : paarmann 1.3 # check diagram id
104 :     unless ($diagram_id) {
105 :     return "<h1>Subsystem: $subsystem_pretty</h1>".
106 :     "<p><em>Unable to find a diagram for this subsystem.</em><p>";
107 :     }
108 :     unless ($subsystem->is_new_diagram($diagram_id)) {
109 :     return "<h1>Subsystem: $subsystem_pretty</h1>".
110 :     "<p><em>Diagram '$diagram_id' is not a new diagram.</em><p>";
111 :     }
112 : paarmann 1.1
113 :    
114 : paarmann 1.3 # get the genomes from the subsystem with positive variant codes
115 :     my @genomes;
116 :     my $genome = $cgi->param('genome_id');
117 :     foreach (sort { $fig->genus_species($a) cmp $fig->genus_species($b) } $subsystem->get_genomes()) {
118 :     my $vcode = $subsystem->get_variant_code( $subsystem->get_genome_index( $_ ) );
119 :     push @genomes, $_ if ($vcode > 0);
120 :     }
121 :    
122 : paarmann 1.4 my %genome_labels = map { $_ => $fig->genus_species($_)." ( $_ ) [".
123 :     $subsystem->get_variant_code( $subsystem->get_genome_index( $_ ) )."]"
124 :     } @genomes;
125 : paarmann 1.3
126 :    
127 :     # generate the content
128 :     my $content = "<h1>Subsystem: $subsystem_pretty</h1>";
129 :     $content .= '<hr/>';
130 :    
131 :     $content .= $cgi->start_form();
132 :     $content .= $cgi->hidden( -name => 'subsystem_name',
133 :     -value => $subsystem_name );
134 :     $content .= $cgi->hidden( -name => 'diagram',
135 :     -value => $diagram_id );
136 : paarmann 1.5 $content .= $cgi->hidden( -name => 'dont_scale', -value => 1 )
137 :     if ($cgi->param('dont_scale'));
138 :     $content .= $cgi->hidden( -name => 'debug', -value => 1 )
139 :     if ($cgi->param('debug'));
140 : paarmann 1.3 $content .= $cgi->popup_menu( -name => 'genome_id',
141 :     -values => \@genomes,
142 :     -default => $genome,
143 :     -labels => \%genome_labels,
144 :     );
145 :     $content .= $cgi->submit( -name => 'Color diagram' );
146 :     $content .= $cgi->end_form();
147 :    
148 :     # initialise a status string (log)
149 :     my $status = '';
150 :    
151 :     # fetch the diagram
152 :     my $diagram_dir = $subsystem->{dir}."/diagrams/$diagram_id/";
153 :     my $d = Diagram->new($subsystem_name, $diagram_dir);
154 : paarmann 1.5
155 :     # turn off scaling?
156 :     $d->min_scale(1) if ($cgi->param('dont_scale'));
157 : paarmann 1.3
158 :    
159 :     # DEBUG: test all items of the diagram against the subsystem
160 :     # (for debug purposes during introduction of new diagrams)
161 :     # (remove when no longer needed)
162 :     # (1) roles
163 :     my $types = [ 'role', 'role_and', 'role_or' ];
164 :     foreach my $t (@$types) {
165 :     foreach my $id (@{$d->item_ids_of_type($t)}) {
166 :     unless ($subsystem->get_role_from_abbr($id) or
167 :     scalar($subsystem->get_subsetC_roles($id))) {
168 :     $status .= "Diagram item '$t' = '$id' not found in the subsystem.\n";
169 :     }
170 :     }
171 :     }
172 :     # (2) subsystem
173 :     foreach my $s (@{$d->item_ids_of_type('subsystem')}) {
174 :     unless ($fig->subsystem_version($s)) {
175 :     $status .= "Diagram item 'subsystem' = '$s' is not a subsystem.\n";
176 :     }
177 :     }
178 :     # END
179 :    
180 : paarmann 1.5 # build a lookup hash, make one entry for each role_and and role_or item
181 :     # the index references to the inner hash of the role_and/role_or hash
182 :     # to set a value there use $lookup->{role_abbr}->{role_abbr} = 1;
183 :     # declared outside if to be available for debug output
184 :     my $lookup = {};
185 :    
186 : paarmann 1.3 if ($genome) {
187 :    
188 :     my @roles = $subsystem->get_roles_for_genome($genome);
189 :    
190 :     # find out about role_and
191 :     my $role_and = {};
192 :     if (scalar(@{$d->item_ids_of_type('role_and')})) {
193 :     foreach my $subset (@{$d->item_ids_of_type('role_and')}) {
194 :    
195 :     $role_and->{$subset} = {};
196 :    
197 :     foreach my $r ($subsystem->get_subsetC_roles($subset)) {
198 :     my $r_abbr = $subsystem->get_abbr_for_role($r);
199 :     unless ($r_abbr) {
200 :     die "Unable to get the abbreviation for role '$r'.";
201 : paarmann 1.1 }
202 :    
203 : paarmann 1.3 $lookup->{$r_abbr} = $role_and->{$subset};
204 :     $role_and->{$subset}->{$r_abbr} = 0;
205 : paarmann 1.1 }
206 :     }
207 : paarmann 1.3 }
208 :    
209 :     # find out about role_or
210 :     my $role_or = {};
211 :     if (scalar(@{$d->item_ids_of_type('role_or')})) {
212 :     foreach my $subset (@{$d->item_ids_of_type('role_or')}) {
213 : paarmann 1.5
214 : paarmann 1.3 $role_or->{$subset} = {};
215 : paarmann 1.1
216 : paarmann 1.3 foreach my $r ($subsystem->get_subsetC_roles($subset)) {
217 :     my $r_abbr = $subsystem->get_abbr_for_role($r);
218 : paarmann 1.5
219 : paarmann 1.3 unless ($r_abbr) {
220 :     die "Unable to get the abbreviation for role '$r'.";
221 :     }
222 :    
223 : paarmann 1.5 $lookup->{$r_abbr} = $role_or->{$subset};
224 : paarmann 1.3 $role_or->{$subset}->{$r_abbr} = 0;
225 : paarmann 1.1 }
226 :     }
227 : paarmann 1.3 }
228 :    
229 :    
230 :     # check if genome is present in subsystem
231 :     # genomes not present, unfortunately return @roles = ( undef )
232 :     if (scalar(@roles) == 0 or
233 :     (scalar(@roles) and !defined($roles[0]))) {
234 :     $content .= "<p><em>Genome '$genome' is not present in this subsystem.</em><p>";
235 :     shift(@roles);
236 :     }
237 :     else {
238 : paarmann 1.4 $content .= "<p><em>Showing colors for genome: ".
239 :     $fig->genus_species($genome)." ( $genome ), variant code ".
240 :     $subsystem->get_variant_code($subsystem->get_genome_index($genome)) ."</em><p>";
241 : paarmann 1.3 }
242 :    
243 :    
244 :     # iterate over all roles present in a subsystem:
245 :     # -> map roles to abbr in the foreach loop
246 :     # -> color simple roles present
247 :     # -> tag roles being part of a logical operator in $lookup
248 :     foreach (map { $subsystem->get_abbr_for_role($_) } @roles) {
249 : paarmann 1.1
250 : paarmann 1.3 # color normal roles
251 :     if ($d->has_item('role', $_)) {
252 :     $d->color_item('role',$_,'green');
253 :     next;
254 :     }
255 : paarmann 1.1
256 : paarmann 1.3 # try to find role_and / role_or
257 :     if (exists($lookup->{$_})) {
258 :     $lookup->{$_}->{$_} = 1;
259 :     next;
260 : paarmann 1.1 }
261 : paarmann 1.3
262 :     $status .= "Role '$_' not found in the diagram.\n";
263 : paarmann 1.1 }
264 : paarmann 1.3
265 :     # check if to color any role_and
266 :     foreach my $id_role_and (keys(%$role_and)) {
267 :     my $result = 1;
268 :     foreach (keys(%{$role_and->{$id_role_and}})) {
269 :     $result = 0 unless ($role_and->{$id_role_and}->{$_});
270 :     }
271 :     $d->color_item('role_and', $id_role_and, 'green') if ($result);
272 : paarmann 1.1 }
273 : paarmann 1.5
274 :     # check if to color any role_or
275 :     foreach my $id_role_or (keys(%$role_or)) {
276 :     foreach (keys(%{$role_or->{$id_role_or}})) {
277 :     if ($role_or->{$id_role_or}->{$_}) {
278 :     $d->color_item('role_or', $id_role_or, 'green');
279 :     last;
280 :     }
281 :     }
282 :     }
283 :    
284 : paarmann 1.3 }
285 :     else {
286 :     $content .= '<p><em>You have not provided a genome id to color the diagram with.</em><p>';
287 : paarmann 1.1 }
288 :    
289 : paarmann 1.5 # add an info line about diagram scaling
290 :     my $scale = $d->calculate_scale * 100;
291 :     unless ($scale == 100) {
292 :     $content .= '<p><em>This diagram has been scaled to '.sprintf("%.2f", $scale).'%. ';
293 :     $content .= "(<a href='?subsystem_name=$subsystem_name&diagram_id=$diagram_id&dont_scale=1'>".
294 :     "view in original size</a>)";
295 :     $content .= '</em></p>';
296 :     }
297 :     if ($cgi->param('dont_scale')) {
298 :     $content .= '<p><em>You have switched off scaling this diagram down. ';
299 :     $content .= "(<a href='?subsystem_name=$subsystem_name&diagram_id=$diagram_id'>".
300 :     "Allow scaling</a>)";
301 :     $content .= '</em></p>';
302 :     }
303 :    
304 :     # print diagram
305 : paarmann 1.3 $content .= $d->html;
306 : paarmann 1.5
307 :     # print status
308 : paarmann 1.3 $content .= '<hr/><p><em>Below follows a status message to help test the new diagrams:</em><p>'.
309 :     "<pre>$status</pre>" if ($status);
310 :    
311 : paarmann 1.5 # print debug
312 :     if ($cgi->param('debug')) {
313 :     require Data::Dumper;
314 :     $content .= '<hr/>';
315 :     $content .= "<h2>Diagram dump:</h2><pre>".Data::Dumper->Dump([ $d ])."</pre>";
316 :     $content .= "<h2>Lookup dump:</h2><pre>".Data::Dumper->Dump([ $lookup ])."</pre>";
317 :     }
318 :    
319 : paarmann 1.1 return $content;
320 :     }
321 :    
322 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3