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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3