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

Annotation of /FigKernelPackages/FIG_CGI.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3