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

Annotation of /FigWebServices/diagram.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (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.3 # $Id: diagram.cgi,v 1.2 2007/02/16 22:29:47 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 :     my %genome_labels = map { $_ => $fig->genus_species($_)." ( $_ )" } @genomes;
123 :    
124 :    
125 :     # generate the content
126 :     my $content = "<h1>Subsystem: $subsystem_pretty</h1>";
127 :     $content .= '<hr/>';
128 :    
129 :     $content .= $cgi->start_form();
130 :     $content .= $cgi->hidden( -name => 'subsystem_name',
131 :     -value => $subsystem_name );
132 :     $content .= $cgi->hidden( -name => 'diagram',
133 :     -value => $diagram_id );
134 :     $content .= $cgi->popup_menu( -name => 'genome_id',
135 :     -values => \@genomes,
136 :     -default => $genome,
137 :     -labels => \%genome_labels,
138 :     );
139 :     $content .= $cgi->submit( -name => 'Color diagram' );
140 :     $content .= $cgi->end_form();
141 :    
142 :     # initialise a status string (log)
143 :     my $status = '';
144 :    
145 :     # fetch the diagram
146 :     my $diagram_dir = $subsystem->{dir}."/diagrams/$diagram_id/";
147 :     my $d = Diagram->new($subsystem_name, $diagram_dir);
148 :    
149 :    
150 :     # DEBUG: test all items of the diagram against the subsystem
151 :     # (for debug purposes during introduction of new diagrams)
152 :     # (remove when no longer needed)
153 :     # (1) roles
154 :     my $types = [ 'role', 'role_and', 'role_or' ];
155 :     foreach my $t (@$types) {
156 :     foreach my $id (@{$d->item_ids_of_type($t)}) {
157 :     unless ($subsystem->get_role_from_abbr($id) or
158 :     scalar($subsystem->get_subsetC_roles($id))) {
159 :     $status .= "Diagram item '$t' = '$id' not found in the subsystem.\n";
160 :     }
161 :     }
162 :     }
163 :     # (2) subsystem
164 :     foreach my $s (@{$d->item_ids_of_type('subsystem')}) {
165 :     unless ($fig->subsystem_version($s)) {
166 :     $status .= "Diagram item 'subsystem' = '$s' is not a subsystem.\n";
167 :     }
168 :     }
169 :     # END
170 :    
171 :     if ($genome) {
172 :    
173 :     my @roles = $subsystem->get_roles_for_genome($genome);
174 :    
175 :     # build a lookup hash, make one entry for each role_and and role_or item
176 :     # the index references to the inner hash of the role_and/role_or hash
177 :     # to set a value there use $lookup->{role_abbr}->{role_abbr} = 1;
178 :     my $lookup = {};
179 :    
180 :     # find out about role_and
181 :     my $role_and = {};
182 :     if (scalar(@{$d->item_ids_of_type('role_and')})) {
183 :     foreach my $subset (@{$d->item_ids_of_type('role_and')}) {
184 :    
185 :     $role_and->{$subset} = {};
186 :    
187 :     foreach my $r ($subsystem->get_subsetC_roles($subset)) {
188 :     my $r_abbr = $subsystem->get_abbr_for_role($r);
189 :     unless ($r_abbr) {
190 :     die "Unable to get the abbreviation for role '$r'.";
191 : paarmann 1.1 }
192 :    
193 : paarmann 1.3 $lookup->{$r_abbr} = $role_and->{$subset};
194 :     $role_and->{$subset}->{$r_abbr} = 0;
195 : paarmann 1.1 }
196 :     }
197 : paarmann 1.3 }
198 :    
199 :     # find out about role_or
200 :     my $role_or = {};
201 :     if (scalar(@{$d->item_ids_of_type('role_or')})) {
202 :     foreach my $subset (@{$d->item_ids_of_type('role_or')}) {
203 : paarmann 1.1
204 : paarmann 1.3 $role_or->{$subset} = {};
205 : paarmann 1.1
206 : paarmann 1.3 foreach my $r ($subsystem->get_subsetC_roles($subset)) {
207 :     my $r_abbr = $subsystem->get_abbr_for_role($r);
208 :     unless ($r_abbr) {
209 :     die "Unable to get the abbreviation for role '$r'.";
210 :     }
211 :    
212 :     $lookup->{$r_abbr} = $role_and->{$subset};
213 :     $role_or->{$subset}->{$r_abbr} = 0;
214 : paarmann 1.1 }
215 :     }
216 : paarmann 1.3 }
217 :    
218 :    
219 :     # check if genome is present in subsystem
220 :     # genomes not present, unfortunately return @roles = ( undef )
221 :     if (scalar(@roles) == 0 or
222 :     (scalar(@roles) and !defined($roles[0]))) {
223 :     $content .= "<p><em>Genome '$genome' is not present in this subsystem.</em><p>";
224 :     shift(@roles);
225 :     }
226 :     else {
227 :     $content .= "<p><em>Showing colors for genome: $genome.</em><p>";
228 :     }
229 :    
230 :    
231 :     # iterate over all roles present in a subsystem:
232 :     # -> map roles to abbr in the foreach loop
233 :     # -> color simple roles present
234 :     # -> tag roles being part of a logical operator in $lookup
235 :     foreach (map { $subsystem->get_abbr_for_role($_) } @roles) {
236 : paarmann 1.1
237 : paarmann 1.3 # color normal roles
238 :     if ($d->has_item('role', $_)) {
239 :     $d->color_item('role',$_,'green');
240 :     next;
241 :     }
242 : paarmann 1.1
243 : paarmann 1.3 # try to find role_and / role_or
244 :     if (exists($lookup->{$_})) {
245 :     $lookup->{$_}->{$_} = 1;
246 :     next;
247 : paarmann 1.1 }
248 : paarmann 1.3
249 :     $status .= "Role '$_' not found in the diagram.\n";
250 : paarmann 1.1 }
251 : paarmann 1.3
252 :     # use Data::Dumper;
253 :     # $content .= "<pre>".Data::Dumper->Dump([ $lookup ])."</pre>";
254 :    
255 :     # check if to color any role_and
256 :     foreach my $id_role_and (keys(%$role_and)) {
257 :     my $result = 1;
258 :     foreach (keys(%{$role_and->{$id_role_and}})) {
259 :     $result = 0 unless ($role_and->{$id_role_and}->{$_});
260 :     }
261 :     $d->color_item('role_and', $id_role_and, 'green') if ($result);
262 : paarmann 1.1 }
263 : paarmann 1.3 }
264 :     else {
265 :     $content .= '<p><em>You have not provided a genome id to color the diagram with.</em><p>';
266 : paarmann 1.1 }
267 :    
268 : paarmann 1.3 $content .= $d->html;
269 :     $content .= '<hr/><p><em>Below follows a status message to help test the new diagrams:</em><p>'.
270 :     "<pre>$status</pre>" if ($status);
271 :    
272 : paarmann 1.1 return $content;
273 :     }
274 :    
275 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3