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

Annotation of /FigWebServices/diagram.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (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.6 # $Id: diagram.cgi,v 1.5 2007/03/06 16:39:39 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 : paarmann 1.6 }
56 :     th, td {
57 :     font-size: 12px;
58 :     }
59 :     ~;
60 : paarmann 1.1
61 :     # print out the page
62 :     print $cgi->header();
63 : paarmann 1.6 print "<html>\n<head>\n";
64 :     print "<title>The SEED - Subsystem Diagram</title>\n";
65 :     print "<style type='text/css'> \n <!-- \n $css \n --> \n </style> \n";
66 :     print "</head> \n <body> \n";
67 : paarmann 1.1
68 :     print &get_Diagram($fig, $cgi);
69 :    
70 : paarmann 1.2 print $cgi->end_html;
71 :    
72 : paarmann 1.1 }
73 :    
74 :    
75 :     sub get_Diagram {
76 :     # get parameters
77 :     my ($fig, $cgi) = @_;
78 :    
79 : paarmann 1.3 # get the subsystem
80 :     unless ($cgi->param('subsystem_name')) {
81 : paarmann 1.1 return '<p>CGI Parameter missing.</p>';
82 :     }
83 : paarmann 1.3 my $subsystem_name = $cgi->param('subsystem_name') || '';
84 : paarmann 1.1 my $subsystem_pretty = $subsystem_name;
85 :     $subsystem_pretty =~ s/_/ /g;
86 : paarmann 1.3 my $subsystem = $fig->get_subsystem($subsystem_name);
87 : paarmann 1.1
88 : paarmann 1.3 # check subsystem
89 :     unless ($subsystem) {
90 :     return "<p>Unable to find a subsystem called '$subsystem_name'.</p>";
91 :     }
92 : paarmann 1.1
93 :    
94 : paarmann 1.3 # if diagram.cgi is called without the CGI param diagram (the diagram id)
95 :     # it will try to load the first 'new' diagram from the subsystem and
96 :     # print out an error message if there is no 'new' diagram
97 :     my $diagram_id = $cgi->param('diagram') || '';
98 :     unless ($diagram_id) {
99 :     foreach my $d ($subsystem->get_diagrams) {
100 :     my ($id) = @$d;
101 :     if ($subsystem->is_new_diagram($id)) {
102 :     $diagram_id = $id;
103 :     last;
104 : paarmann 1.1 }
105 :     }
106 : paarmann 1.3 }
107 : paarmann 1.1
108 : paarmann 1.3 # check diagram id
109 :     unless ($diagram_id) {
110 :     return "<h1>Subsystem: $subsystem_pretty</h1>".
111 :     "<p><em>Unable to find a diagram for this subsystem.</em><p>";
112 :     }
113 :     unless ($subsystem->is_new_diagram($diagram_id)) {
114 :     return "<h1>Subsystem: $subsystem_pretty</h1>".
115 :     "<p><em>Diagram '$diagram_id' is not a new diagram.</em><p>";
116 :     }
117 : paarmann 1.1
118 :    
119 : paarmann 1.6 # find out about sort order
120 :     my $sort_by = $cgi->param('sort_by') || 'name';
121 :    
122 :     # get the genomes from the subsystem with zero or positive variant codes
123 : paarmann 1.3 my @genomes;
124 :     my $genome = $cgi->param('genome_id');
125 : paarmann 1.6 if ($sort_by eq 'variant_code') {
126 :     foreach (sort { ($subsystem->get_variant_code( $subsystem->get_genome_index($a) ) cmp
127 :     $subsystem->get_variant_code( $subsystem->get_genome_index($b) )) or
128 :     ( $fig->genus_species($a) cmp $fig->genus_species($b) )
129 :     } $subsystem->get_genomes()) {
130 :     my $vcode = $subsystem->get_variant_code( $subsystem->get_genome_index( $_ ) );
131 :     push @genomes, $_ if ($vcode >= 0);
132 :     }
133 :     }
134 :     else {
135 :     foreach (sort { $fig->genus_species($a) cmp $fig->genus_species($b) } $subsystem->get_genomes()) {
136 :     my $vcode = $subsystem->get_variant_code( $subsystem->get_genome_index( $_ ) );
137 :     push @genomes, $_ if ($vcode >= 0);
138 :     }
139 : paarmann 1.3 }
140 :    
141 : paarmann 1.4 my %genome_labels = map { $_ => $fig->genus_species($_)." ( $_ ) [".
142 :     $subsystem->get_variant_code( $subsystem->get_genome_index( $_ ) )."]"
143 :     } @genomes;
144 : paarmann 1.6
145 :     @genomes = ('0', @genomes);
146 :     $genome_labels{'0'} = 'please select a genome to color the diagram with' ;
147 : paarmann 1.3
148 :    
149 :     # generate the content
150 :     my $content = "<h1>Subsystem: $subsystem_pretty</h1>";
151 :     $content .= '<hr/>';
152 :    
153 : paarmann 1.6 $content .= $cgi->start_form( -id => 'diagram_select_genome' );
154 : paarmann 1.3 $content .= $cgi->hidden( -name => 'subsystem_name',
155 :     -value => $subsystem_name );
156 :     $content .= $cgi->hidden( -name => 'diagram',
157 :     -value => $diagram_id );
158 : paarmann 1.5 $content .= $cgi->hidden( -name => 'dont_scale', -value => 1 )
159 :     if ($cgi->param('dont_scale'));
160 :     $content .= $cgi->hidden( -name => 'debug', -value => 1 )
161 :     if ($cgi->param('debug'));
162 : paarmann 1.6 $content .= '<p>Sort by: '.
163 :     $cgi->radio_group( -name => 'sort_by',
164 :     -values => ['name', 'variant_code'],
165 :     -default => $sort_by,
166 :     -labels => { 'name' => 'Genome name',
167 :     'variant_code' => 'Variant code, then genome name' },
168 :     -onChange => 'document.getElementById("diagram_select_genome").submit();',
169 :     ).'</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 :     $content .= "(<a href='?subsystem_name=$subsystem_name&diagram_id=$diagram_id&dont_scale=1'>".
342 :     "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 :     $content .= "(<a href='?subsystem_name=$subsystem_name&diagram_id=$diagram_id'>".
348 :     "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