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

Annotation of /FigKernelPackages/FIG_CGI.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3