[Bio] / FigKernelPackages / FIG_CGI.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/FIG_CGI.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (view) (download) (as text)

1 : olson 1.3 #
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 : olson 1.1
19 : parrello 1.4 =head1 FIG CGI Script Utility Module
20 :    
21 :     This package contains utility methods for initializing and debugging CGI scripts
22 :     in the FIG framework.
23 :    
24 :     =cut
25 : olson 1.1
26 :     package FIG_CGI;
27 :    
28 : parrello 1.4 require Exporter;
29 :     @ISA = ('Exporter');
30 :     @EXPORT = qw(is_sprout);
31 :    
32 :     =head2 Public Methods
33 :    
34 :     =cut
35 : olson 1.1
36 :     use strict;
37 :     use FIG;
38 : olson 1.6 use FIGV;
39 : olson 1.1 use FIG_Config;
40 :     use CGI;
41 :     use Data::Dumper;
42 :     use SproutFIG;
43 :    
44 :     use Tracer;
45 :    
46 :     =head3 init
47 :    
48 :     C<< my($fig, $cgi, $user) = FIG_CGI::init(debug_save => 0, debug_load => 0, print_params => 0); >>
49 :    
50 :     Initialize a FIG and CGI object for use in the CGI script. Depending on the
51 :     CGI parameters passed in, the FIG object will be either an actual FIG object
52 :     (when we are in SEED mode), or a SFXlate object (when we are in Sprout mode).
53 :    
54 :     =over4
55 : parrello 1.4
56 : olson 1.1 =item debug_save
57 :    
58 :     Set this flag to true if the script should save its parameters to a
59 :     file. (Default filename is the name of the script minus the .cgi
60 : parrello 1.4 suffix, placed in the /tmp/ directory).
61 : olson 1.1
62 :     =item debug_load
63 :    
64 :     Set this flag to true if the script should load its parameters from a
65 :     file as saved with debug_save.
66 :    
67 :     =item print_params
68 :    
69 :     Set this flag to true if the script should print its CGI parameters
70 :     before exiting.
71 :    
72 : parrello 1.4 =item RETURN
73 :    
74 :     Returns a three-tuple. The first element is a FIG or Sprout object. The second
75 :     is a CGI object describing the environment of the calling script. The third
76 :     is the name of the current user.
77 :    
78 : olson 1.1 =back
79 :    
80 :     =cut
81 :    
82 : parrello 1.4 sub init {
83 :     # Get the parameters. The calling syntax uses parameter pairs, so we stash
84 :     # them in a hash.
85 :     my (%args) = @_;
86 :     # Get the CGI and FIG objects.
87 : olson 1.1 my $cgi = new CGI;
88 :     my $fig = init_fig($cgi);
89 : parrello 1.4 # If we're debugging, we need to know which file is to receive the debugging
90 :     # information.
91 : olson 1.1 my $script_name = determine_script_name();
92 :     my $file = "/tmp/${script_name}_parms";
93 :    
94 : olson 1.2 # warn "fig_cgi init $file\n";
95 : olson 1.1
96 : parrello 1.4 # Check to see if we're supposed to display the parameters. Since "debug_save"
97 :     # mode also prints the parameters, we remember here whether or not we printed
98 :     # them so we don't print them twice.
99 : olson 1.1 my $printed_params;
100 :     if ($args{print_params})
101 :     {
102 : parrello 1.4 do_print_params($cgi);
103 :     $printed_params++;
104 : olson 1.1 }
105 :    
106 : parrello 1.4 # Check to see if we're supposed to save the parameters to a debug file or
107 :     # load them from a debug file.
108 : olson 1.1 if ($args{debug_save})
109 :     {
110 : parrello 1.4 do_print_params($cgi) unless $printed_params;
111 :     print "Wrote params to $file<p>\n";
112 :     $cgi = do_debug_save($cgi, $file);
113 : olson 1.1 }
114 :     elsif ($args{debug_load})
115 :     {
116 : parrello 1.4 $cgi = do_debug_load($cgi, $file);
117 : olson 1.1 }
118 : parrello 1.4 # Now the debugging stuff is done and the $cgi object looks exactly the way we
119 :     # want it.
120 :    
121 :     # Get the user's name.
122 :     my $user = $cgi->param('user') || "";
123 :    
124 :     return($fig, $cgi, $user);
125 :     }
126 :    
127 :     =head3 is_sprout
128 :    
129 :     C<< my $flag = is_sprout($object); >>
130 :    
131 :     Return TRUE if we are running in Sprout mode, else FALSE.
132 :    
133 :     =over 4
134 : olson 1.1
135 : parrello 1.4 =item object
136 : olson 1.1
137 : parrello 1.4 FIG, SFXlate, or CGI object. If a FIG object is passed in, the result is always
138 :     FALSE. If an SFXlate object is passed in, the result is always TRUE. If a CGI
139 :     object is passed in, the value of the C<SPROUT> parameter will be returned.
140 : olson 1.1
141 : parrello 1.4 =item RETURN
142 :    
143 :     Returns TRUE if we're in Sprout mode, else FALSE.
144 :    
145 :     =back
146 :    
147 :     =cut
148 :    
149 :     sub is_sprout {
150 :     # Get the parameters.
151 :     my ($object) = @_;
152 :     # Declare the return variable.
153 :     my $retVal = 0;
154 :     # Check the object type. Note that an unknown object or scalar will
155 :     # default to FALSE. This includes FIG objects, because we don't
156 :     # explicity check for them.
157 :     my $type = ref $object;
158 :     if ($type eq 'SFXlate') {
159 :     $retVal = 1;
160 :     } elsif ($type eq 'CGI') {
161 :     $retVal = $object->param('SPROUT');
162 :     }
163 :     # Return the result.
164 :     return $retVal;
165 :     }
166 :    
167 : olson 1.1 sub init_tracing
168 :     {
169 :     my($cgi) = @_;
170 :     my $traceData = $cgi->param('trace');
171 :     if ($traceData) {
172 : parrello 1.4 TSetup($traceData, "QUEUE");
173 :     $traceData = 1;
174 : olson 1.1 } else {
175 : parrello 1.4 TSetup(0, "NONE");
176 :     $traceData = 0;
177 : olson 1.1 }
178 :     }
179 :    
180 :     sub init_fig
181 :     {
182 :     my($cgi) = @_;
183 :    
184 :     my $fig;
185 :     if ($cgi->param('SPROUT'))
186 :     {
187 : parrello 1.4 $fig = new SproutFIG($FIG_Config::sproutDB, $FIG_Config::sproutData);
188 : olson 1.1 }
189 :     else
190 :     {
191 : olson 1.6 if (my $job = $cgi->param("48hr_job"))
192 :     {
193 :     my $jobdir = "/vol/48-hour/Jobs/$job";
194 :     my $genome = &FIG::file_head("$jobdir/GENOME_ID");
195 :     chomp $genome;
196 :     if ($genome !~ /^\d+\.\d+/)
197 :     {
198 :     die "Cannnot find genome ID for jobdir $jobdir\n";
199 :     }
200 :     my $orgdir = "$jobdir/rp/$genome";
201 :     if (! -d $orgdir)
202 :     {
203 :     die "Cannot find orgdir $orgdir\n";
204 :     }
205 :     $fig = new FIGV($orgdir);
206 :     }
207 :     else
208 :     {
209 :     $fig = new FIG;
210 :     }
211 : olson 1.1 }
212 :     return $fig;
213 :     }
214 :    
215 :     sub do_print_params
216 :     {
217 :     my($cgi) = @_;
218 : parrello 1.4
219 : olson 1.1 print $cgi->header;
220 :     my @params = $cgi->param;
221 :     print "<pre>\n";
222 :     foreach $_ (@params) {
223 : parrello 1.4 print "$_\t:",join(",",$cgi->param($_)),":\n";
224 : olson 1.1 }
225 :     print "</pre>\n";
226 :     }
227 :    
228 :     sub do_debug_load
229 :     {
230 :     my($cgi, $file) = @_;
231 :     my $VAR1;
232 :     if (-f $file)
233 :     {
234 : parrello 1.4 eval(&FIG::file_read($file));
235 :     $cgi = $VAR1;
236 : olson 1.1 }
237 :     else
238 :     {
239 : parrello 1.4 print $cgi->header;
240 :     print "Attempting debug load, but file $file does not exist\n";
241 :     die "Attempting debug load, but file $file does not exist\n";
242 : olson 1.1 }
243 :    
244 :     return $cgi;
245 :     }
246 :    
247 :     sub do_debug_save
248 :     {
249 :     my($cgi, $file) = @_;
250 :    
251 :     if (open(TMP,">$file")) {
252 : parrello 1.4 print TMP &Dumper($cgi);
253 :     close(TMP);
254 :     # warn "Loaded cgi from $file\n";
255 : olson 1.1 }
256 :     else
257 :     {
258 : parrello 1.4 print $cgi->header;
259 :     print "Attempting debug load, but file $file does not exist\n";
260 :     warn "Attempting debug load, but file $file does not exist\n";
261 : olson 1.1 }
262 :     exit;
263 :     }
264 :    
265 :     sub determine_script_name
266 :     {
267 :     my $path = $ENV{SCRIPT_NAME};
268 :     my $name;
269 :    
270 :     if ($path eq '')
271 :     {
272 : parrello 1.4 #
273 :     # We're probably being invoked from the command line.
274 :     #
275 : olson 1.1
276 : parrello 1.4 $path = $0;
277 : olson 1.1 }
278 :    
279 :     if ($path =~ m,/([^/]+)$,)
280 :     {
281 : parrello 1.4 $name = $1;
282 : olson 1.1 }
283 :     else
284 :     {
285 : parrello 1.4 $name = $path;
286 :     $name =~ s,/,_,g;
287 : olson 1.1 }
288 :     $name =~ s/\.cgi$//;
289 :     return $name;
290 :     }
291 :    
292 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3