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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     package ServerThing;
4 :    
5 :     use strict;
6 :     use Tracer;
7 :     use YAML;
8 :     use ERDB;
9 :     use TestUtils;
10 :     use Time::HiRes;
11 : parrello 1.9 use File::Temp;
12 : parrello 1.10 use ErrorMessage;
13 : parrello 1.1 use CGI;
14 : parrello 1.9 no warnings qw(once);
15 : parrello 1.1
16 : parrello 1.11 # Maximum number of requests to run per invocation.
17 : parrello 1.29 use constant MAX_REQUESTS => 5000;
18 : parrello 1.11
19 : parrello 1.1 =head1 General Server Helper
20 :    
21 :     This package provides a method-- I<RunServer>-- that can be called from a CGI
22 : parrello 1.9 script to perform the duties of a FIG server. RunServer is called with two
23 :     parameters: the name of the server package (e.g. C<SAP> for B<SAP.pm>) and
24 :     the first command-line parameter. The command-line parameter (if defined) will
25 :     be used as the tracing key, and also indicates that the script is being invoked
26 :     from the command line rather than over the web.
27 : parrello 1.1
28 :     =cut
29 :    
30 :     sub RunServer {
31 :     # Get the parameters.
32 : parrello 1.9 my ($serverName, $key) = @_;
33 : parrello 1.25 # Set up tracing. We never do CGI tracing here; the only question is whether
34 :     # or not the caller passed in a tracing key. If he didn't, we use the server
35 :     # name.
36 : parrello 1.32 ETracing($key || $serverName, destType => 'APPEND', level => '0 ServerThing');
37 : parrello 1.9 # Turn off YAML compression, which causes problems with some of our hash keys.
38 :     $YAML::CompressSeries = 0;
39 : parrello 1.13 # Create the server object.
40 : parrello 1.15 Trace("Requiring $serverName for task $$.") if T(3);
41 : parrello 1.13 eval {
42 :     require "$serverName.pm";
43 :     };
44 :     # If we have an error, create an error document.
45 :     if ($@) {
46 :     SendError($@, "Could not load server module.");
47 :     } else {
48 :     # Having successfully loaded the server code, we create the object.
49 :     my $serverThing = eval("$serverName" . '->new()');
50 : parrello 1.15 Trace("$serverName object created for task $$.") if T(2);
51 : parrello 1.13 # If we have an error, create an error document.
52 :     if ($@) {
53 :     SendError($@, "Could not start server.");
54 : parrello 1.3 } else {
55 : parrello 1.13 # No error, so now we can process the request.
56 :     my $cgi;
57 :     if (! defined $key) {
58 :     # No tracing key, so presume we're a web service. Check for Fast CGI.
59 :     if ($ENV{REQUEST_METHOD} eq '') {
60 :     # Count the number of requests.
61 :     my $requests = 0;
62 : parrello 1.24 Trace("Starting Fast CGI loop.") if T(3);
63 : parrello 1.13 # Loop through the fast CGI requests. If we have request throttling,
64 :     # we exit after a maximum number of requests has been exceeded.
65 :     require CGI::Fast;
66 : parrello 1.23 while ((MAX_REQUESTS == 0 || ++$requests < MAX_REQUESTS) &&
67 :     ($cgi = new CGI::Fast())) {
68 : parrello 1.13 RunRequest($cgi, $serverThing);
69 : parrello 1.16 Trace("Request $requests complete in task $$.") if T(3);
70 : parrello 1.13 }
71 : parrello 1.15 Trace("Terminating FastCGI task $$ after $requests requests.") if T(2);
72 : parrello 1.13 } else {
73 :     # Here we have a normal web service (non-Fast).
74 :     my $cgi = CGI->new();
75 :     # Check for a source parameter. This gets used as the tracing key.
76 :     $key = $cgi->param('source');
77 :     # Run this request.
78 :     RunRequest($cgi, $serverThing);
79 :     }
80 : parrello 1.6 } else {
81 : parrello 1.13 # We're being invoked from the command line. Use the tracing
82 :     # key to find the parm file and create the CGI object from that.
83 :     my $ih = Open(undef, "<$FIG_Config::temp/$key.parms");
84 :     $cgi = CGI->new($ih);
85 :     # Run this request.
86 :     RunRequest($cgi, $serverThing);
87 : parrello 1.6 }
88 : parrello 1.3 }
89 : parrello 1.1 }
90 : parrello 1.6 }
91 :    
92 :    
93 : parrello 1.9 =head2 Server Utility Methods
94 :    
95 :     The methods in this section are utilities of general use to the various
96 :     server modules.
97 :    
98 : parrello 1.21 =head3 AddSubsystemFilter
99 :    
100 :     ServerThing::AddSubsystemFilter(\$filter, $args);
101 :    
102 :     Add subsystem filtering information to the specified query filter clause
103 :     based on data in the argument hash. The argument hash will be checked for
104 : parrello 1.22 the C<-usable> parameter, which includes or excludes unusuable subsystems, and
105 :     the C<-exclude> parameter, which lists types of subsystems that should be
106 : parrello 1.21 excluded.
107 :    
108 :     =over 4
109 :    
110 :     =item filter
111 :    
112 :     Reference to the current filter string. If additional filtering is required,
113 :     this string will be updated.
114 :    
115 :     =item args
116 :    
117 :     Reference to the parameter hash for the current server call. This hash will
118 : parrello 1.22 be examined for the C<-usable> and C<-exclude> parameters.
119 : parrello 1.21
120 :     =back
121 :    
122 :     =cut
123 :    
124 :     use constant SS_TYPE_EXCLUDE_ITEMS => { 'cluster-based' => 1,
125 :     experimental => 1,
126 :     private => 1 };
127 :    
128 :     sub AddSubsystemFilter {
129 :     # Get the parameters.
130 :     my ($filter, $args) = @_;
131 :     # We'll put the new filter stuff in here.
132 :     my @newFilters;
133 :     # Unless unusable subsystems are desired, we must add a clause to the filter.
134 : parrello 1.22 # The default is that only usable subsystems are included.
135 :     my $usable = 1;
136 :     # This default can be overridden by the "-usable" parameter.
137 :     if (exists $args->{-usable}) {
138 :     $usable = $args->{-usable};
139 :     }
140 :     # If we're restricting to usable subsystems, add a filter to that effect.
141 :     if ($usable) {
142 : parrello 1.21 push @newFilters, "Subsystem(usable) = 1";
143 :     }
144 :     # Check for exclusion filters.
145 :     my $exclusions = ServerThing::GetIdList(-exclude => $args, 1);
146 :     for my $exclusion (@$exclusions) {
147 :     if (! SS_TYPE_EXCLUDE_ITEMS->{$exclusion}) {
148 :     Confess("Invalid exclusion type \"$exclusion\".");
149 :     } else {
150 :     # Here we have to exclude subsystems of the specified type.
151 :     push @newFilters, "Subsystem($exclusion) = 0";
152 :     }
153 :     }
154 :     # Do we need to update the incoming filter?
155 :     if (@newFilters) {
156 :     # Yes. If the incoming filter is nonempty, push it onto the list
157 :     # so it gets included in the result.
158 :     if ($$filter) {
159 :     push @newFilters, $$filter;
160 :     }
161 :     # Put all the filters together to form the new filter.
162 :     $$filter = join(" AND ", @newFilters);
163 : parrello 1.26 Trace("Subsystem filter is $$filter.") if T(ServerUtilities => 3);
164 : parrello 1.21 }
165 :     }
166 :    
167 :    
168 :    
169 : parrello 1.9 =head3 GetIdList
170 :    
171 : parrello 1.19 my $ids = ServerThing::GetIdList($name => $args, $optional);
172 : parrello 1.9
173 :     Get a named list of IDs from an argument structure. If the IDs are
174 :     missing, or are not a list, an error will occur.
175 :    
176 :     =over 4
177 :    
178 :     =item name
179 :    
180 :     Name of the argument structure member that should contain the ID list.
181 :    
182 :     =item args
183 :    
184 :     Argument structure from which the ID list is to be extracted.
185 :    
186 : parrello 1.19 =item optional (optional)
187 :    
188 :     If TRUE, then a missing value will not generate an error. Instead, an empty list
189 :     will be returned. The default is FALSE.
190 :    
191 : parrello 1.9 =item RETURN
192 :    
193 :     Returns a reference to a list of IDs taken from the argument structure.
194 :    
195 :     =back
196 :    
197 :     =cut
198 :    
199 :     sub GetIdList {
200 :     # Get the parameters.
201 : parrello 1.19 my ($name, $args, $optional) = @_;
202 : parrello 1.35 # Declare the return variable.
203 :     my $retVal;
204 : parrello 1.32 # Check the argument format.
205 : parrello 1.35 if (! defined $args && $optional) {
206 :     # Here there are no parameters, but the arguments are optional so it's
207 :     # okay.
208 :     $retVal = [];
209 :     } elsif (ref $args ne 'HASH') {
210 :     # Here we have an invalid parameter structure.
211 : parrello 1.32 Confess("No '$name' parameter present.");
212 : parrello 1.35 } else {
213 :     # Here we have a hash with potential parameters in it. Try to get the
214 :     # IDs from the argument structure.
215 :     $retVal = $args->{$name};
216 :     # Was a member found?
217 :     if (! defined $retVal) {
218 :     # No. If we're optional, return an empty list; otherwise throw an error.
219 :     if ($optional) {
220 :     $retVal = [];
221 :     } else {
222 :     Confess("No '$name' parameter found.");
223 :     }
224 : parrello 1.19 } else {
225 : parrello 1.35 # Here we found something. Get the parameter type. We want a list reference.
226 :     # If it's a scalar, we'll convert it to a singleton list. If it's anything
227 :     # else, it's an error.
228 :     my $type = ref $retVal;
229 :     if (! $type) {
230 :     $retVal = [$retVal];
231 :     } elsif ($type ne 'ARRAY') {
232 :     Confess("The '$name' parameter must be a list.");
233 :     }
234 : parrello 1.19 }
235 : parrello 1.9 }
236 :     # Return the result.
237 :     return $retVal;
238 :     }
239 :    
240 :    
241 :     =head3 RunTool
242 :    
243 :     ServerThing::RunTool($name => $cmd);
244 :    
245 :     Run a command-line tool. A non-zero return value from the tool will cause
246 :     a fatal error, and the tool's error log will be traced.
247 :    
248 :     =over 4
249 :    
250 :     =item name
251 :    
252 :     Name to give to the tool in the error output.
253 :    
254 :     =item cmd
255 :    
256 :     Command to use for running the tool. This should be the complete command line.
257 :     The command should not contain any fancy piping, though it may redirect the
258 :     standard input and output. The command will be modified by this method to
259 :     redirect the error output to a temporary file.
260 :    
261 :     =back
262 :    
263 :     =cut
264 :    
265 :     sub RunTool {
266 :     # Get the parameters.
267 :     my ($name, $cmd) = @_;
268 :     # Compute the log file name.
269 :     my $errorLog = "$FIG_Config::temp/errors$$.log";
270 :     # Execute the command.
271 : parrello 1.26 Trace("Executing command: $cmd") if T(ServerUtilities => 3);
272 : parrello 1.9 my $res = system("$cmd 2> $errorLog");
273 : parrello 1.26 Trace("Return from $name tool is $res.") if T(ServerUtilities => 3);
274 : parrello 1.9 # Check the result code.
275 :     if ($res != 0) {
276 :     # We have an error. If tracing is on, trace it.
277 : parrello 1.26 if (T(ServerUtilities => 1)) {
278 : parrello 1.9 TraceErrorLog($name, $errorLog);
279 :     }
280 :     # Delete the error log.
281 :     unlink $errorLog;
282 :     # Confess the error.
283 : parrello 1.10 Confess("$name command failed with error code $res.");
284 : parrello 1.9 } else {
285 :     # Everything worked. Trace the error log if necessary.
286 : parrello 1.26 if (T(ServerUtilities => 3) && -s $errorLog) {
287 : parrello 1.9 TraceErrorLog($name, $errorLog);
288 :     }
289 :     # Delete the error log if there is one.
290 :     unlink $errorLog;
291 :     }
292 :     }
293 :    
294 : parrello 1.36
295 : parrello 1.37 =head2 Gene Correspondence File Methods
296 : parrello 1.36
297 : parrello 1.37 These methods relate to gene correspondence files, which are generated by the
298 :     L<svr_corresponding_genes.pl> script. Correspondence files are cached in the
299 :     organism cache (I<$FIG_Config::orgCache>) directory. Eventually they will be
300 :     copied into the organism directories themselves. At that point, the code below
301 :     will be modified to check the organism directories first and use the cache
302 :     directory if no file is found there.
303 :    
304 :     A gene correspondence file contains correspondences from a source genome to a
305 :     target genome. Most such correspondences are bidirectional best hits. A unidirectional
306 :     best hit may exist from the source genome to the target genome or in the reverse
307 :     direction from the targtet genome to the source genome. The cache directory itself
308 :     is divided into subdirectories by organism. The subdirectory has the source genome
309 :     name and the files themselves are named by the target genome.
310 :    
311 :     Some of the files are invalid and will be erased when they are found. A file is
312 :     considered invalid if it has a non-numeric value in a numeric column or if it
313 :     does not have any unidirectional hits from the target genome to the source
314 :     genome.
315 :    
316 :     The process of managing the correspondence files is tricky and dangerous because
317 :     of the possibility of race conditions. It can take several minutes to generate a
318 :     file, and if two processes try to generate the same file at the same time we need
319 :     to make sure they don't step on each other.
320 :    
321 :     In stored files, the source genome ID is always lexically lower than the target
322 :     genome ID. If a correspondence in the reverse direction is desired, the converse
323 :     file is found and the contents flipped automatically as they are read. So, the
324 :     correspondence from B<360108.3> to B<100226.1> would be found in a file with the
325 :     name B<360108.3> in the directory for B<100226.1>. Since this file actually has
326 :     B<100226.1> as the source and B<360108.3> as the target, the columns are
327 :     re-ordered and the arrows reversed before the file contents are passed to the
328 :     caller.
329 :    
330 :     =head4 Gene Correspondence List
331 :    
332 :     A gene correspondence file contains 18 columns. These are usually packaged as
333 :     a reference to list of lists. Each sub-list has the following format.
334 :    
335 :     =over 4
336 :    
337 :     =item 0
338 :    
339 :     The ID of a PEG in genome 1.
340 :    
341 :     =item 1
342 :    
343 :     The ID of a PEG in genome 2 that is our best estimate of a "corresponding gene".
344 :    
345 :     =item 2
346 :    
347 :     Count of the number of pairs of matching genes were found in the context.
348 :    
349 :     =item 3
350 :    
351 :     Pairs of corresponding genes from the contexts.
352 :    
353 :     =item 4
354 :    
355 :     The function of the gene in genome 1.
356 :    
357 :     =item 5
358 :    
359 :     The function of the gene in genome 2.
360 :    
361 :     =item 6
362 :    
363 :     Comma-separated list of aliases for the gene in genome 1 (any protein with an
364 :     identical sequence is considered an alias, whether or not it is actually the
365 :     name of the same gene in the same genome).
366 :    
367 :     =item 7
368 :    
369 :     Comma-separated list of aliases for the gene in genome 2 (any protein with an
370 :     identical sequence is considered an alias, whether or not it is actually the
371 :     name of the same gene in the same genome).
372 :    
373 :     =item 8
374 :    
375 :     Bi-directional best hits will contain "<=>" in this column; otherwise, "->" will appear.
376 :    
377 :     =item 9
378 :    
379 :     Percent identity over the region of the detected match.
380 :    
381 :     =item 10
382 :    
383 :     The P-score for the detected match.
384 :    
385 :     =item 11
386 :    
387 :     Beginning match coordinate in the protein encoded by the gene in genome 1.
388 :    
389 :     =item 12
390 :    
391 :     Ending match coordinate in the protein encoded by the gene in genome 1.
392 :    
393 :     =item 13
394 :    
395 :     Length of the protein encoded by the gene in genome 1.
396 :    
397 :     =item 14
398 :    
399 :     Beginning match coordinate in the protein encoded by the gene in genome 2.
400 :    
401 :     =item 15
402 :    
403 :     Ending match coordinate in the protein encoded by the gene in genome 2.
404 :    
405 :     =item 16
406 :    
407 :     Length of the protein encoded by the gene in genome 2.
408 :    
409 :     =item 17
410 :    
411 :     Bit score for the match. Divide by the length of the longer PEG to get
412 :     what we often refer to as a "normalized bit score".
413 :    
414 :     =back
415 :    
416 :     In the actual files, there will also be reverse correspondences indicated by a
417 :     back-arrow ("<-") in item (8). The output returned by the servers, however,
418 :     is filtered so that only forward correspondences occur. If a converse file
419 :     is used, the columns are re-ordered and the arrows reversed so that it looks
420 :     correct.
421 :    
422 :     =cut
423 :    
424 :     # hash for reversing the arrows
425 :     use constant ARROW_FLIP => { '->' => '<-', '<=>' => '<=>', '<-' => '->' };
426 :     # list of columns that contain numeric values that need to be validated
427 :     use constant NUM_COLS => [2,9,10,11,12,13,14,15,16,17];
428 :    
429 :     =head3 CheckForGeneCorrespondenceFile
430 :    
431 :     my ($fileName, $converse) = ServerThing::CheckForGeneCorrespondenceFile($genome1, $genome2);
432 :    
433 :     Try to find a gene correspondence file for the specified genome pairing. If the
434 :     file exists, its name and an indication of whether or not it is in the correct
435 :     direction will be returned.
436 : parrello 1.36
437 :     =over 4
438 :    
439 :     =item genome1
440 :    
441 : parrello 1.37 Source genome for the desired correspondence.
442 : parrello 1.36
443 :     =item genome2
444 :    
445 : parrello 1.37 Target genome for the desired correspondence.
446 : parrello 1.36
447 :     =item RETURN
448 :    
449 : parrello 1.37 Returns a two-element list. The first element is the name of the file containing the
450 :     correspondence, or C<undef> if the file does not exist. The second element is TRUE
451 :     if the correspondence would be forward or FALSE if the file needs to be flipped.
452 : parrello 1.36
453 :     =back
454 :    
455 :     =cut
456 :    
457 : parrello 1.37 sub CheckForGeneCorrespondenceFile {
458 : parrello 1.36 # Get the parameters.
459 :     my ($genome1, $genome2) = @_;
460 : parrello 1.37 # Declare the return variables.
461 :     my ($fileName, $converse);
462 :     # Determine the ordering of the genome IDs.
463 :     my ($corrFileName, $genomeA, $genomeB) = ComputeCorrespondenceFileName($genome1, $genome2);
464 :     $converse = ($genomeA ne $genome1);
465 :     # Look for a file containing the desired correspondence. (The code to check for a
466 :     # pre-computed file in the organism directories is currently turned off, because
467 :     # these files are all currently invalid.)
468 :     my $testFileName = "$FIG_Config::organisms/$genomeA/CorrToReferenceGenomes/$genomeB";
469 :     if (0 && -f $testFileName) {
470 : parrello 1.36 # Use the pre-computed file.
471 :     Trace("Using pre-computed file $fileName for genome correspondence.") if T(3);
472 : parrello 1.37 $fileName = $testFileName;
473 :     } elsif (-f $corrFileName) {
474 :     $fileName = $corrFileName;
475 :     Trace("Using cached file $fileName for genome correspondence.") if T(3);
476 :     }
477 :     # Return the result.
478 :     return ($fileName, $converse);
479 :     }
480 :    
481 :    
482 :     =head3 ComputeCorrespondenceFileName
483 :    
484 :     my ($fileName, $genomeA, $genomeB) = ServerThing::ComputeCorrespondenceFileName($genome1, $genome2);
485 :    
486 :     Compute the name to be given to a genome correspondence file in the organism cache
487 :     and return the source and target genomes that would be in it.
488 :    
489 :     =over 4
490 :    
491 :     =item genome1
492 :    
493 :     Source genome for the desired correspondence.
494 :    
495 :     =item genome2
496 :    
497 :     Target genome for the desired correspondence.
498 :    
499 :     =item RETURN
500 :    
501 :     Returns a three-element list. The first element is the name of the file to contain the
502 :     correspondence, the second element is the name of the genome that would act as the
503 :     source genome in the file, and the third element is the name of the genome that would
504 :     act as the target genome in the file.
505 :    
506 :     =back
507 :    
508 :     =cut
509 :    
510 :     sub ComputeCorrespondenceFileName {
511 :     # Get the parameters.
512 :     my ($genome1, $genome2) = @_;
513 :     # Declare the return variables.
514 :     my ($fileName, $genomeA, $genomeB);
515 :     # Determine the ordering of the genome IDs.
516 :     if ($genome1 lt $genome2) {
517 :     ($genomeA, $genomeB) = ($genome1, $genome2);
518 : parrello 1.36 } else {
519 : parrello 1.37 ($genomeA, $genomeB) = ($genome2, $genome1);
520 :     }
521 :     # Insure the source organism has a subdirectory in the organism cache.
522 :     my $orgDir = "$FIG_Config::orgCache/$genomeA";
523 :     Tracer::Insure($orgDir, 0777);
524 :     # Compute the name of the correspondence file for the appropriate target genome.
525 :     $fileName = "$orgDir/$genomeB";
526 :     # Return the results.
527 :     return ($fileName, $genomeA, $genomeB);
528 :     }
529 :    
530 :    
531 :     =head3 CreateGeneCorrespondenceFile
532 :    
533 :     my ($fileName, $converse) = ServerThing::CheckForGeneCorrespondenceFile($genome1, $genome2);
534 :    
535 :     Create a new gene correspondence file in the organism cache for the specified
536 :     genome correspondence. The name of the new file will be returned along with
537 :     an indicator of whether or not it is in the correct direction.
538 :    
539 :     =over 4
540 :    
541 :     =item genome1
542 :    
543 :     Source genome for the desired correspondence.
544 :    
545 :     =item genome2
546 :    
547 :     Target genome for the desired correspondence.
548 :    
549 :     =item RETURN
550 :    
551 :     Returns a two-element list. The first element is the name of the file containing the
552 :     correspondence, or C<undef> if an error occurred. The second element is TRUE
553 :     if the correspondence would be forward or FALSE if the file needs to be flipped.
554 :    
555 :     =back
556 :    
557 :     =cut
558 :    
559 :     sub CreateGeneCorrespondenceFile {
560 :     # Get the parameters.
561 :     my ($genome1, $genome2) = @_;
562 :     # Declare the return variables.
563 :     my ($fileName, $converse);
564 :     # Compute the ultimate name for the correspondence file.
565 :     my ($corrFileName, $genomeA, $genomeB) = ComputeCorrespondenceFileName($genome1, $genome2);
566 :     $converse = ($genome1 ne $genomeA);
567 :     # Generate a temporary file name in the same directory. We'll build the temporary
568 :     # file and then rename it when we're done.
569 :     my $tempFileName = "$corrFileName.$$.tmp";
570 :     # This will be set to FALSE if we detect an error.
571 :     my $fileOK = 1;
572 :     # The file handles will be put in here.
573 :     my ($ih, $oh);
574 :     # Protect from errors.
575 :     eval {
576 :     # Open the temporary file for output.
577 :     $oh = Open(undef, ">$tempFileName");
578 :     # Open a pipe to get the correspondence data.
579 :     $ih = Open(undef, "$FIG_Config::bin/svr_corresponding_genes -u localhost $genomeA $genomeB |");
580 :     Trace("Creating correspondence file for $genomeA to $genomeB in temporary file $tempFileName.") if T(3);
581 :     # Copy the pipe date into the temporary file.
582 :     while (! eof $ih) {
583 :     my $line = <$ih>;
584 :     print $oh $line;
585 :     }
586 :     # Close both files. If the close fails we need to know: it means there was a pipe
587 :     # error.
588 :     $fileOK &&= close $ih;
589 :     $fileOK &&= close $oh;
590 :     };
591 :     if ($@) {
592 :     # Here a fatal error of some sort occurred. We need to force the files closed.
593 :     close $ih if $ih;
594 :     close $oh if $oh;
595 :     } elsif ($fileOK) {
596 :     # Here everything worked. Try to rename the temporary file to the real
597 :     # file name.
598 :     if (rename $tempFileName, $corrFileName) {
599 :     # Everything is ok, fix the permissions and return the file name.
600 :     chmod 0664, $corrFileName;
601 :     $fileName = $corrFileName;
602 :     Trace("Created correspondence file $fileName.") if T(3);
603 :     }
604 :     }
605 :     # If the temporary file exists, delete it.
606 :     if (-f $tempFileName) {
607 :     unlink $tempFileName;
608 :     }
609 :     # Return the results.
610 :     return ($fileName, $converse);
611 :     }
612 :    
613 :    
614 :     =head3 ReadGeneCorrespondenceFile
615 :    
616 :     my $list = ServerThing::ReadGeneCorrespondenceFile($fileName, $converse);
617 :    
618 :     Return the contents of the specified gene correspondence file in the form of
619 :     a list of lists, with backward correspondences filtered out. If the file is
620 :     for the converse of the desired correspondence, the columns will be reordered
621 :     automatically so that it looks as if the file were designed for the proper
622 :     direction.
623 :    
624 :     =over 4
625 :    
626 :     =item fileName
627 :    
628 :     The name of the gene correspondence file to read.
629 :    
630 :     =item converse (optional)
631 :    
632 :     TRUE if the file is for the converse of the desired correspondence, else FALSE.
633 :     If TRUE, the file columns will be reorderd automatically. The default is FALSE,
634 :     meaning we want to use the file as it appears on disk.
635 :    
636 :     =item RETURN
637 :    
638 :     Returns a L</Gene Correspondence List> in the form of a reference to a list of lists.
639 :     If the file's contents are invalid or an error occurs, an undefined value will be
640 :     returned.
641 :    
642 :     =back
643 :    
644 :     =cut
645 :    
646 :     sub ReadGeneCorrespondenceFile {
647 :     # Get the parameters.
648 :     my ($fileName, $converse) = @_;
649 :     # Declare the return variable. We will only put something in here if we are
650 :     # completely successful.
651 :     my $retVal;
652 :     # This value will be set to 1 if an error is detected.
653 :     my $error = 0;
654 :     # Try to open the file.
655 :     my $ih;
656 :     Trace("Reading correspondence file $fileName.") if T(3);
657 :     if (! open $ih, "<$fileName") {
658 :     # Here the open failed, so we have an error.
659 :     Trace("Failed to open gene correspondence file $fileName: $!") if T(3);
660 :     $error = 1;
661 :     }
662 :     # The gene correspondence list will be built in here.
663 :     my @corrList;
664 :     # This variable will be set to TRUE if we find a reverse correspondence somewhere
665 :     # in the file. Not finding one is an error.
666 :     my $reverseFound = 0;
667 :     # Loop until we hit the end of the file or an error occurs. We must check the error
668 :     # first in case the file handle failed to open.
669 :     while (! $error && ! eof $ih) {
670 :     # Get the current line.
671 :     my @row = Tracer::GetLine($ih);
672 :     # Get the correspondence direction and check for a reverse arrow.
673 :     $reverseFound = 1 if ($row[8] eq '<-');
674 :     # If we're in converse mode, reformat the line.
675 :     if ($converse) {
676 :     ($row[1], $row[0], $row[2], $row[3], $row[5], $row[4], $row[7], $row[6],
677 :     ARROW_FLIP->{$row[8]}, $row[9], $row[10], $row[14], $row[15], $row[16],
678 :     $row[11], $row[12], $row[13], $row[17]) = @row;
679 :     }
680 :     # Validate the row.
681 :     if (ValidateGeneCorrespondenceRow(\@row)) {
682 :     Trace("Invalid row $. found in correspondence file $fileName.") if T(3);
683 :     $error = 1;
684 :     }
685 :     # If this row is in the correct direction, keep it.
686 : parrello 1.38 if ($row[8] ne '<-') {
687 : parrello 1.37 push @corrList, \@row;
688 :     }
689 :     }
690 :     # Close the input file.
691 :     close $ih;
692 :     # If we have no errors and we found a reverse arrow, keep the result.
693 :     if (! $error) {
694 :     if ($reverseFound) {
695 :     $retVal = \@corrList;
696 : parrello 1.36 } else {
697 : parrello 1.37 Trace("No reverse arrow found in correspondence file $fileName.") if T(3);
698 :     }
699 :     }
700 :     # Return the result (if any).
701 :     return $retVal;
702 :     }
703 :    
704 :    
705 :     =head3 ValidateGeneCorrespondenceRow
706 :    
707 :     my $errorCount = ServerThing::ValidateGeneCorrespondenceRow($row);
708 :    
709 :     Validate a gene correspondence row. The numeric fields are checked to insure they
710 :     are numeric and the source and target gene IDs are validated. The return value will
711 :     indicate the number of errors found.
712 :    
713 :     =over 4
714 :    
715 :     =item row
716 :    
717 :     Reference to a list containing a single row from a L</Gene Correspondence List>.
718 :    
719 :     =item RETURN
720 :    
721 :     Returns the number of errors found in the row. A return of C<0> indicates the row
722 :     is valid.
723 :    
724 :     =back
725 :    
726 :     =cut
727 :    
728 :     sub ValidateGeneCorrespondenceRow {
729 :     # Get the parameters.
730 :     my ($row, $genome1, $genome2) = @_;
731 :     # Denote no errors have been found so far.
732 :     my $retVal = 0;
733 :     # Check for non-numeric values in the number columns.
734 :     for my $col (@{NUM_COLS()}) {
735 :     unless ($row->[$col] =~ /^-?\d+\.?\d*(?:e[+-]?\d+)?$/) {
736 :     $retVal++;
737 :     }
738 :     }
739 :     # Check the gene IDs.
740 :     for my $col (0, 1) {
741 :     unless ($row->[$col] =~ /^fig\|\d+\.\d+\.\w+\.\d+$/) {
742 :     $retVal++;
743 : parrello 1.36 }
744 :     }
745 : parrello 1.37 # Verify the arrow.
746 :     unless (exists ARROW_FLIP->{$row->[8]}) {
747 :     $retVal++;
748 :     }
749 :     # Return the error count.
750 : parrello 1.36 return $retVal;
751 :     }
752 :    
753 : parrello 1.9
754 :     =head2 Internal Utility Methods
755 :    
756 :     The methods in this section are used internally by this package.
757 :    
758 : parrello 1.6 =head3 RunRequest
759 :    
760 : parrello 1.9 ServerThing::RunRequest($cgi, $serverName);
761 : parrello 1.6
762 :     Run a request from the specified server using the incoming CGI parameter
763 :     object for the parameters.
764 :    
765 :     =over 4
766 :    
767 :     =item cgi
768 :    
769 :     CGI query object containing the parameters from the web service request.
770 :    
771 : parrello 1.13 =item serverThing
772 : parrello 1.6
773 : parrello 1.13 Server object against which to run the request.
774 : parrello 1.6
775 :     =back
776 :    
777 :     =cut
778 :    
779 :     sub RunRequest {
780 :     # Get the parameters.
781 : parrello 1.13 my ($cgi, $serverThing, $docURL) = @_;
782 : parrello 1.9 # Determine the request type.
783 :     if ($cgi->param('pod')) {
784 : parrello 1.4 # Here we have a documentation request. In this case, we produce POD HTML.
785 : parrello 1.9 ProducePod($cgi->param('pod'));
786 : disz 1.31 } elsif ($cgi->param('code')) {
787 : parrello 1.32 # Here the user wants to see the code for one of our scripts.
788 :     LineNumberize($cgi->param('code'));
789 : parrello 1.9 } elsif ($cgi->param('file')) {
790 :     # Here we have a file request. Process according to the type.
791 :     my $type = $cgi->param('file');
792 :     if ($type eq 'open') {
793 :     OpenFile($cgi->param('name'));
794 :     } elsif ($type eq 'create') {
795 :     CreateFile();
796 :     } elsif ($type eq 'read') {
797 :     ReadChunk($cgi->param('name'), $cgi->param('location'), $cgi->param('size'));
798 :     } elsif ($type eq 'write') {
799 :     WriteChunk($cgi->param('name'), $cgi->param('data'));
800 :     } else {
801 :     Die("Invalid file function \"$type\".");
802 : parrello 1.4 }
803 : parrello 1.1 } else {
804 : parrello 1.9 # The default is a function request. Get the function name.
805 : parrello 1.4 my $function = $cgi->param('function') || "";
806 : parrello 1.15 Trace("Server function for task $$ is $function.") if T(3);
807 : parrello 1.4 # Insure the function name is valid.
808 :     Die("Invalid function name.")
809 :     if $function =~ /\W/;
810 :     # The parameter structure will go in here.
811 :     my $args;
812 :     # Start the timer.
813 :     my $start = time();
814 :     # The output document goes in here.
815 :     my $document;
816 :     # The sapling database goes in here.
817 :     my $sapling;
818 :     # Protect from errors.
819 : parrello 1.1 eval {
820 : parrello 1.4 # Parse the arguments.
821 :     $args = YAML::Load($cgi->param('args'));
822 : parrello 1.1 };
823 : parrello 1.4 # Check to make sure we got everything.
824 : parrello 1.1 if ($@) {
825 : parrello 1.10 SendError($@, "Error formatting parameters.");
826 : parrello 1.4 } elsif (! $function) {
827 : parrello 1.10 SendError("No function specified.", "No function specified.");
828 : parrello 1.1 } else {
829 : parrello 1.30 $document = eval { $serverThing->$function($args) };
830 : parrello 1.1 # If we have an error, create an error document.
831 :     if ($@) {
832 : parrello 1.13 SendError($@, "Error detected by service.");
833 : parrello 1.32 Trace("Error encountered by service: $@") if T(0);
834 : parrello 1.1 } else {
835 : parrello 1.13 # No error, so we output the result.
836 :     print $cgi->header(-type => 'text/plain');
837 : parrello 1.18 my $string = YAML::Dump($document);
838 :     print $string;
839 :     MemTrace(length($string) . " bytes returned from $function by task $$.") if T(Memory => 3);
840 : parrello 1.1 }
841 :     }
842 : parrello 1.4 # Stop the timer.
843 :     my $duration = int(time() - $start + 0.5);
844 : parrello 1.29 Trace("Function $function executed in $duration seconds by task $$.") if T(2);
845 : parrello 1.1 }
846 :     }
847 :    
848 : parrello 1.9 =head3 CreateFile
849 :    
850 :     ServerThing::CreateFile();
851 :    
852 :     Create a new, empty temporary file and send its name back to the client.
853 :    
854 :     =cut
855 :    
856 :     sub CreateFile {
857 :     ##TODO: Code
858 :     }
859 :    
860 :     =head3 OpenFile
861 : parrello 1.6
862 : parrello 1.9 ServerThing::OpenFile($name);
863 : parrello 1.1
864 : parrello 1.9 Send the length of the named file back to the client.
865 :    
866 :     =over 4
867 :    
868 :     =item name
869 :    
870 :     ##TODO: name description
871 :    
872 :     =back
873 :    
874 :     =cut
875 :    
876 :     sub OpenFile {
877 :     # Get the parameters.
878 :     my ($name) = @_;
879 :     ##TODO: Code
880 :     }
881 : parrello 1.1
882 : parrello 1.9 =head3 ReadChunk
883 : parrello 1.1
884 : parrello 1.9 ServerThing::ReadChunk($name, $location, $size);
885 : parrello 1.1
886 : parrello 1.9 Read the indicated number of bytes from the specified location of the
887 :     named file and send them back to the client.
888 : parrello 1.1
889 :     =over 4
890 :    
891 :     =item name
892 :    
893 : parrello 1.9 ##TODO: name description
894 : parrello 1.1
895 : parrello 1.9 =item location
896 : parrello 1.1
897 : parrello 1.9 ##TODO: location description
898 : parrello 1.1
899 : parrello 1.9 =item size
900 : parrello 1.1
901 : parrello 1.9 ##TODO: size description
902 : parrello 1.1
903 :     =back
904 :    
905 :     =cut
906 :    
907 : parrello 1.9 sub ReadChunk {
908 : parrello 1.1 # Get the parameters.
909 : parrello 1.9 my ($name, $location, $size) = @_;
910 :     ##TODO: Code
911 : parrello 1.1 }
912 :    
913 : parrello 1.9 =head3 WriteChunk
914 : parrello 1.1
915 : parrello 1.9 ServerThing::WriteChunk($name, $data);
916 : parrello 1.8
917 : parrello 1.9 Write the specified data to the named file.
918 : parrello 1.8
919 :     =over 4
920 :    
921 :     =item name
922 :    
923 : parrello 1.9 ##TODO: name description
924 :    
925 :     =item data
926 :    
927 :     ##TODO: data description
928 :    
929 :     =back
930 :    
931 :     =cut
932 :    
933 :     sub WriteChunk {
934 :     # Get the parameters.
935 :     my ($name, $data) = @_;
936 :     ##TODO: Code
937 :     }
938 :    
939 :    
940 : disz 1.31 =head3 LineNumberize
941 :    
942 :     ServerThing::LineNumberize($module);
943 :    
944 :     Output the module line by line with line numbers
945 :    
946 :     =over 4
947 :    
948 :     =item module
949 :    
950 :     Name of the module to line numberized
951 :    
952 :     =back
953 :    
954 :     =cut
955 :    
956 :     sub LineNumberize {
957 :     # Get the parameters.
958 :     my ($module) = @_;
959 :     my $fks_path = "$FIG_Config::fig_disk/dist/releases/current/FigKernelScripts/$module";
960 :     # Start the output page.
961 :     print CGI::header();
962 :     print CGI::start_html(-title => 'Documentation Page',
963 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
964 :     # Protect from errors.
965 :     eval {
966 : parrello 1.32 if (-e $fks_path) {
967 :     print "<pre>\n";
968 :     my $i = 1;
969 :     foreach my $line (`cat $fks_path`) {
970 :     print "$i.\t$line";
971 :     $i++;
972 :     }
973 :     print "</pre>\n";
974 :     } else {
975 :     print "File $fks_path not found";
976 :     }
977 : disz 1.31 };
978 :     # Process any error.
979 :     if ($@) {
980 :     print CGI::blockquote({ class => 'error' }, $@);
981 :     }
982 :     # Close off the page.
983 :     print CGI::end_html();
984 :    
985 :     }
986 :    
987 : parrello 1.9 =head3 ProducePod
988 :    
989 :     ServerThing::ProducePod($module);
990 :    
991 :     Output the POD documentation for the specified module.
992 :    
993 :     =over 4
994 : parrello 1.8
995 : parrello 1.9 =item module
996 : parrello 1.8
997 : parrello 1.9 Name of the module whose POD document is to be displayed.
998 : parrello 1.8
999 :     =back
1000 :    
1001 :     =cut
1002 :    
1003 : parrello 1.9 sub ProducePod {
1004 : parrello 1.8 # Get the parameters.
1005 : parrello 1.9 my ($module) = @_;
1006 :     # Start the output page.
1007 :     print CGI::header();
1008 :     print CGI::start_html(-title => 'Documentation Page',
1009 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
1010 :     # Protect from errors.
1011 :     eval {
1012 :     # We'll format the HTML text in here.
1013 :     require DocUtils;
1014 : parrello 1.20 my $html = DocUtils::ShowPod($module, "http://servers.nmpdr.org/sapling/server.cgi?pod=");
1015 : parrello 1.9 # Output the POD HTML.
1016 :     print $html;
1017 :     };
1018 :     # Process any error.
1019 :     if ($@) {
1020 :     print CGI::blockquote({ class => 'error' }, $@);
1021 : parrello 1.8 }
1022 : parrello 1.9 # Close off the page.
1023 :     print CGI::end_html();
1024 :    
1025 : parrello 1.8 }
1026 :    
1027 :     =head3 TraceErrorLog
1028 :    
1029 :     ServerThing::TraceErrorLog($name, $errorLog);
1030 :    
1031 :     Trace the specified error log file. This is a very dinky routine that
1032 :     performs a task required by L</RunTool> in multiple places.
1033 :    
1034 :     =over 4
1035 :    
1036 :     =item name
1037 :    
1038 :     Name of the tool relevant to the log file.
1039 :    
1040 :     =item errorLog
1041 :    
1042 :     Name of the log file.
1043 :    
1044 :     =back
1045 :    
1046 :     =cut
1047 :    
1048 :     sub TraceErrorLog {
1049 :     my ($name, $errorLog) = @_;
1050 :     my $errorData = Tracer::GetFile($errorLog);
1051 :     Trace("$name error log:\n$errorData");
1052 :     }
1053 :    
1054 : parrello 1.10 =head3 SendError
1055 :    
1056 :     ServerThing::SendError($message, $status);
1057 :    
1058 :     Fail an HTTP request with the specified error message and the specified
1059 :     status message.
1060 :    
1061 :     =over 4
1062 :    
1063 :     =item message
1064 :    
1065 :     Detailed error message. This is sent as the page content.
1066 :    
1067 :     =item status
1068 :    
1069 :     Status message. This is sent as part of the status code.
1070 :    
1071 :     =back
1072 :    
1073 :     =cut
1074 :    
1075 :     sub SendError {
1076 :     # Get the parameters.
1077 :     my ($message, $status) = @_;
1078 :     Trace("Error \"$status\" $message") if T(2);
1079 : parrello 1.30 # Check for a DBserver error. These can be retried and get a special status
1080 :     # code.
1081 :     my $realStatus;
1082 :     if ($message =~ /DBServer Error:\s+/) {
1083 :     $realStatus = "503 $status";
1084 :     } else {
1085 :     $realStatus = "500 $status";
1086 :     }
1087 : parrello 1.10 # Print the header and the status message.
1088 :     print CGI::header(-type => 'text/plain',
1089 : parrello 1.30 -status => $realStatus);
1090 : parrello 1.10 # Print the detailed message.
1091 :     print $message;
1092 :     }
1093 :    
1094 :    
1095 : disz 1.31 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3