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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.57 - (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.56 =head3 ReadCountVector
309 :    
310 :     my $vector = ServerThing::ReadCountVector($qh, $field, $rawFlag);
311 :    
312 :     Extract a count vector from a query. The query can contain zero or more results,
313 :     and the vectors in the specified result field of the query must be concatenated
314 :     together in order. This method is optimized for the case (expected to be most
315 :     common) where there is only one result.
316 :    
317 :     =over 4
318 :    
319 :     =item qh
320 :    
321 :     Handle for the query from which results are to be extracted.
322 :    
323 :     =item field
324 :    
325 :     Name of the field containing the count vectors.
326 :    
327 :     =item rawFlag
328 :    
329 :     TRUE if the vector is to be returned as a raw string, FALSE if it is to be returned
330 :     as reference to a list of numbers.
331 :    
332 :     =item RETURN
333 :    
334 :     Returns the desired vector, either encoded as a string or as a reference to a list
335 :     of numbers.
336 :    
337 :     =back
338 :    
339 :     =cut
340 :    
341 :     sub ReadCountVector {
342 :     # Get the parameters.
343 :     my ($qh, $field, $rawFlag) = @_;
344 :     # Declare the return variable.
345 :     my $retVal;
346 :     # Loop through the query results.
347 :     while (my $resultRow = $qh->Fetch()) {
348 :     # Get this vector.
349 :     my ($levelVector) = $resultRow->Value($field, $rawFlag);
350 :     # Is this the first result?
351 :     if (! defined $retVal) {
352 :     # Yes. Assign the result directly.
353 :     $retVal = $levelVector;
354 :     } elsif ($rawFlag) {
355 :     # This is a second result and the vectors are coded as strings.
356 :     $retVal .= $levelVector;
357 :     } else {
358 :     # This is a second result and the vectors are coded as array references.
359 :     push @$retVal, @$levelVector;
360 :     }
361 :     }
362 :     # Return the result.
363 :     return $retVal;
364 :     }
365 :    
366 : parrello 1.36
367 : parrello 1.37 =head2 Gene Correspondence File Methods
368 : parrello 1.36
369 : parrello 1.37 These methods relate to gene correspondence files, which are generated by the
370 :     L<svr_corresponding_genes.pl> script. Correspondence files are cached in the
371 :     organism cache (I<$FIG_Config::orgCache>) directory. Eventually they will be
372 :     copied into the organism directories themselves. At that point, the code below
373 :     will be modified to check the organism directories first and use the cache
374 :     directory if no file is found there.
375 :    
376 :     A gene correspondence file contains correspondences from a source genome to a
377 :     target genome. Most such correspondences are bidirectional best hits. A unidirectional
378 :     best hit may exist from the source genome to the target genome or in the reverse
379 :     direction from the targtet genome to the source genome. The cache directory itself
380 :     is divided into subdirectories by organism. The subdirectory has the source genome
381 :     name and the files themselves are named by the target genome.
382 :    
383 :     Some of the files are invalid and will be erased when they are found. A file is
384 :     considered invalid if it has a non-numeric value in a numeric column or if it
385 :     does not have any unidirectional hits from the target genome to the source
386 :     genome.
387 :    
388 :     The process of managing the correspondence files is tricky and dangerous because
389 :     of the possibility of race conditions. It can take several minutes to generate a
390 :     file, and if two processes try to generate the same file at the same time we need
391 :     to make sure they don't step on each other.
392 :    
393 :     In stored files, the source genome ID is always lexically lower than the target
394 :     genome ID. If a correspondence in the reverse direction is desired, the converse
395 :     file is found and the contents flipped automatically as they are read. So, the
396 :     correspondence from B<360108.3> to B<100226.1> would be found in a file with the
397 :     name B<360108.3> in the directory for B<100226.1>. Since this file actually has
398 :     B<100226.1> as the source and B<360108.3> as the target, the columns are
399 :     re-ordered and the arrows reversed before the file contents are passed to the
400 :     caller.
401 :    
402 :     =head4 Gene Correspondence List
403 :    
404 :     A gene correspondence file contains 18 columns. These are usually packaged as
405 :     a reference to list of lists. Each sub-list has the following format.
406 :    
407 :     =over 4
408 :    
409 :     =item 0
410 :    
411 :     The ID of a PEG in genome 1.
412 :    
413 :     =item 1
414 :    
415 :     The ID of a PEG in genome 2 that is our best estimate of a "corresponding gene".
416 :    
417 :     =item 2
418 :    
419 :     Count of the number of pairs of matching genes were found in the context.
420 :    
421 :     =item 3
422 :    
423 :     Pairs of corresponding genes from the contexts.
424 :    
425 :     =item 4
426 :    
427 :     The function of the gene in genome 1.
428 :    
429 :     =item 5
430 :    
431 :     The function of the gene in genome 2.
432 :    
433 :     =item 6
434 :    
435 :     Comma-separated list of aliases for the gene in genome 1 (any protein with an
436 :     identical sequence is considered an alias, whether or not it is actually the
437 :     name of the same gene in the same genome).
438 :    
439 :     =item 7
440 :    
441 :     Comma-separated list of aliases for the gene in genome 2 (any protein with an
442 :     identical sequence is considered an alias, whether or not it is actually the
443 :     name of the same gene in the same genome).
444 :    
445 :     =item 8
446 :    
447 :     Bi-directional best hits will contain "<=>" in this column; otherwise, "->" will appear.
448 :    
449 :     =item 9
450 :    
451 :     Percent identity over the region of the detected match.
452 :    
453 :     =item 10
454 :    
455 :     The P-score for the detected match.
456 :    
457 :     =item 11
458 :    
459 :     Beginning match coordinate in the protein encoded by the gene in genome 1.
460 :    
461 :     =item 12
462 :    
463 :     Ending match coordinate in the protein encoded by the gene in genome 1.
464 :    
465 :     =item 13
466 :    
467 :     Length of the protein encoded by the gene in genome 1.
468 :    
469 :     =item 14
470 :    
471 :     Beginning match coordinate in the protein encoded by the gene in genome 2.
472 :    
473 :     =item 15
474 :    
475 :     Ending match coordinate in the protein encoded by the gene in genome 2.
476 :    
477 :     =item 16
478 :    
479 :     Length of the protein encoded by the gene in genome 2.
480 :    
481 :     =item 17
482 :    
483 :     Bit score for the match. Divide by the length of the longer PEG to get
484 :     what we often refer to as a "normalized bit score".
485 :    
486 :     =back
487 :    
488 :     In the actual files, there will also be reverse correspondences indicated by a
489 :     back-arrow ("<-") in item (8). The output returned by the servers, however,
490 :     is filtered so that only forward correspondences occur. If a converse file
491 :     is used, the columns are re-ordered and the arrows reversed so that it looks
492 :     correct.
493 :    
494 :     =cut
495 :    
496 :     # hash for reversing the arrows
497 :     use constant ARROW_FLIP => { '->' => '<-', '<=>' => '<=>', '<-' => '->' };
498 :     # list of columns that contain numeric values that need to be validated
499 :     use constant NUM_COLS => [2,9,10,11,12,13,14,15,16,17];
500 :    
501 :     =head3 CheckForGeneCorrespondenceFile
502 :    
503 :     my ($fileName, $converse) = ServerThing::CheckForGeneCorrespondenceFile($genome1, $genome2);
504 :    
505 :     Try to find a gene correspondence file for the specified genome pairing. If the
506 :     file exists, its name and an indication of whether or not it is in the correct
507 :     direction will be returned.
508 : parrello 1.36
509 :     =over 4
510 :    
511 :     =item genome1
512 :    
513 : parrello 1.37 Source genome for the desired correspondence.
514 : parrello 1.36
515 :     =item genome2
516 :    
517 : parrello 1.37 Target genome for the desired correspondence.
518 : parrello 1.36
519 :     =item RETURN
520 :    
521 : parrello 1.37 Returns a two-element list. The first element is the name of the file containing the
522 :     correspondence, or C<undef> if the file does not exist. The second element is TRUE
523 :     if the correspondence would be forward or FALSE if the file needs to be flipped.
524 : parrello 1.36
525 :     =back
526 :    
527 :     =cut
528 :    
529 : parrello 1.37 sub CheckForGeneCorrespondenceFile {
530 : parrello 1.36 # Get the parameters.
531 :     my ($genome1, $genome2) = @_;
532 : parrello 1.37 # Declare the return variables.
533 :     my ($fileName, $converse);
534 :     # Determine the ordering of the genome IDs.
535 :     my ($corrFileName, $genomeA, $genomeB) = ComputeCorrespondenceFileName($genome1, $genome2);
536 :     $converse = ($genomeA ne $genome1);
537 :     # Look for a file containing the desired correspondence. (The code to check for a
538 :     # pre-computed file in the organism directories is currently turned off, because
539 :     # these files are all currently invalid.)
540 :     my $testFileName = "$FIG_Config::organisms/$genomeA/CorrToReferenceGenomes/$genomeB";
541 :     if (0 && -f $testFileName) {
542 : parrello 1.36 # Use the pre-computed file.
543 : parrello 1.44 Trace("Using pre-computed file $fileName for genome correspondence.") if T(Corr => 3);
544 : parrello 1.37 $fileName = $testFileName;
545 :     } elsif (-f $corrFileName) {
546 :     $fileName = $corrFileName;
547 : parrello 1.44 Trace("Using cached file $fileName for genome correspondence.") if T(Corr => 3);
548 : parrello 1.37 }
549 :     # Return the result.
550 :     return ($fileName, $converse);
551 :     }
552 :    
553 :    
554 :     =head3 ComputeCorrespondenceFileName
555 :    
556 :     my ($fileName, $genomeA, $genomeB) = ServerThing::ComputeCorrespondenceFileName($genome1, $genome2);
557 :    
558 :     Compute the name to be given to a genome correspondence file in the organism cache
559 :     and return the source and target genomes that would be in it.
560 :    
561 :     =over 4
562 :    
563 :     =item genome1
564 :    
565 :     Source genome for the desired correspondence.
566 :    
567 :     =item genome2
568 :    
569 :     Target genome for the desired correspondence.
570 :    
571 :     =item RETURN
572 :    
573 :     Returns a three-element list. The first element is the name of the file to contain the
574 :     correspondence, the second element is the name of the genome that would act as the
575 :     source genome in the file, and the third element is the name of the genome that would
576 :     act as the target genome in the file.
577 :    
578 :     =back
579 :    
580 :     =cut
581 :    
582 :     sub ComputeCorrespondenceFileName {
583 :     # Get the parameters.
584 :     my ($genome1, $genome2) = @_;
585 :     # Declare the return variables.
586 :     my ($fileName, $genomeA, $genomeB);
587 :     # Determine the ordering of the genome IDs.
588 : parrello 1.41 if (MustFlipGenomeIDs($genome1, $genome2)) {
589 : parrello 1.43 ($genomeA, $genomeB) = ($genome2, $genome1);
590 :     } else {
591 : parrello 1.37 ($genomeA, $genomeB) = ($genome1, $genome2);
592 :     }
593 :     # Insure the source organism has a subdirectory in the organism cache.
594 : parrello 1.47 my $orgDir = ComputeCorrespondenceDirectory($genomeA);
595 : parrello 1.37 # Compute the name of the correspondence file for the appropriate target genome.
596 :     $fileName = "$orgDir/$genomeB";
597 :     # Return the results.
598 :     return ($fileName, $genomeA, $genomeB);
599 :     }
600 :    
601 :    
602 : parrello 1.47 =head3 ComputeCorresopndenceDirectory
603 :    
604 :     my $dirName = ServerThing::ComputeCorrespondenceDirectory($genome);
605 :    
606 :     Return the name of the directory that would contain the correspondence files
607 :     for the specified genome.
608 :    
609 :     =over 4
610 :    
611 :     =item genome
612 :    
613 :     ID of the genome whose correspondence file directory is desired.
614 :    
615 :     =item RETURN
616 :    
617 :     Returns the name of the directory of interest.
618 :    
619 :     =back
620 :    
621 :     =cut
622 :    
623 :     sub ComputeCorrespondenceDirectory {
624 :     # Get the parameters.
625 :     my ($genome) = @_;
626 :     # Insure the source organism has a subdirectory in the organism cache.
627 :     my $retVal = "$FIG_Config::orgCache/$genome";
628 :     Tracer::Insure($retVal, 0777);
629 :     # Return it.
630 :     return $retVal;
631 :     }
632 :    
633 :    
634 : parrello 1.37 =head3 CreateGeneCorrespondenceFile
635 :    
636 :     my ($fileName, $converse) = ServerThing::CheckForGeneCorrespondenceFile($genome1, $genome2);
637 :    
638 :     Create a new gene correspondence file in the organism cache for the specified
639 :     genome correspondence. The name of the new file will be returned along with
640 :     an indicator of whether or not it is in the correct direction.
641 :    
642 :     =over 4
643 :    
644 :     =item genome1
645 :    
646 :     Source genome for the desired correspondence.
647 :    
648 :     =item genome2
649 :    
650 :     Target genome for the desired correspondence.
651 :    
652 :     =item RETURN
653 :    
654 :     Returns a two-element list. The first element is the name of the file containing the
655 :     correspondence, or C<undef> if an error occurred. The second element is TRUE
656 :     if the correspondence would be forward or FALSE if the file needs to be flipped.
657 :    
658 :     =back
659 :    
660 :     =cut
661 :    
662 :     sub CreateGeneCorrespondenceFile {
663 :     # Get the parameters.
664 :     my ($genome1, $genome2) = @_;
665 :     # Declare the return variables.
666 :     my ($fileName, $converse);
667 :     # Compute the ultimate name for the correspondence file.
668 :     my ($corrFileName, $genomeA, $genomeB) = ComputeCorrespondenceFileName($genome1, $genome2);
669 :     $converse = ($genome1 ne $genomeA);
670 :     # Generate a temporary file name in the same directory. We'll build the temporary
671 :     # file and then rename it when we're done.
672 :     my $tempFileName = "$corrFileName.$$.tmp";
673 :     # This will be set to FALSE if we detect an error.
674 :     my $fileOK = 1;
675 :     # The file handles will be put in here.
676 :     my ($ih, $oh);
677 :     # Protect from errors.
678 :     eval {
679 :     # Open the temporary file for output.
680 :     $oh = Open(undef, ">$tempFileName");
681 :     # Open a pipe to get the correspondence data.
682 :     $ih = Open(undef, "$FIG_Config::bin/svr_corresponding_genes -u localhost $genomeA $genomeB |");
683 :     Trace("Creating correspondence file for $genomeA to $genomeB in temporary file $tempFileName.") if T(3);
684 :     # Copy the pipe date into the temporary file.
685 :     while (! eof $ih) {
686 :     my $line = <$ih>;
687 :     print $oh $line;
688 :     }
689 :     # Close both files. If the close fails we need to know: it means there was a pipe
690 :     # error.
691 :     $fileOK &&= close $ih;
692 :     $fileOK &&= close $oh;
693 :     };
694 :     if ($@) {
695 :     # Here a fatal error of some sort occurred. We need to force the files closed.
696 :     close $ih if $ih;
697 :     close $oh if $oh;
698 :     } elsif ($fileOK) {
699 :     # Here everything worked. Try to rename the temporary file to the real
700 :     # file name.
701 :     if (rename $tempFileName, $corrFileName) {
702 :     # Everything is ok, fix the permissions and return the file name.
703 :     chmod 0664, $corrFileName;
704 :     $fileName = $corrFileName;
705 : parrello 1.44 Trace("Created correspondence file $fileName.") if T(Corr => 3);
706 : parrello 1.37 }
707 :     }
708 :     # If the temporary file exists, delete it.
709 :     if (-f $tempFileName) {
710 :     unlink $tempFileName;
711 :     }
712 :     # Return the results.
713 :     return ($fileName, $converse);
714 :     }
715 :    
716 :    
717 : parrello 1.41 =head3 MustFlipGenomeIDs
718 :    
719 :     my $converse = ServerThing::MustFlipGenomeIDs($genome1, $genome2);
720 :    
721 :     Return TRUE if the specified genome IDs are out of order. When genome IDs are out of
722 :     order, they are stored in the converse order in correspondence files on the server.
723 :     This is a simple method that allows the caller to check for the need to flip.
724 :    
725 :     =over 4
726 :    
727 :     =item genome1
728 :    
729 :     ID of the proposed source genome.
730 :    
731 :     =item genome2
732 :    
733 :     ID of the proposed target genome.
734 :    
735 :     =item RETURN
736 :    
737 :     Returns TRUE if the first genome would be stored on the server as a target, FALSE if
738 :     it would be stored as a source.
739 :    
740 : parrello 1.55 =back
741 :    
742 : parrello 1.41 =cut
743 :    
744 :     sub MustFlipGenomeIDs {
745 :     # Get the parameters.
746 :     my ($genome1, $genome2) = @_;
747 :     # Return an indication.
748 :     return ($genome1 gt $genome2);
749 :     }
750 :    
751 :    
752 : parrello 1.37 =head3 ReadGeneCorrespondenceFile
753 :    
754 : parrello 1.40 my $list = ServerThing::ReadGeneCorrespondenceFile($fileName, $converse, $all);
755 : parrello 1.37
756 :     Return the contents of the specified gene correspondence file in the form of
757 :     a list of lists, with backward correspondences filtered out. If the file is
758 :     for the converse of the desired correspondence, the columns will be reordered
759 :     automatically so that it looks as if the file were designed for the proper
760 :     direction.
761 :    
762 :     =over 4
763 :    
764 :     =item fileName
765 :    
766 :     The name of the gene correspondence file to read.
767 :    
768 :     =item converse (optional)
769 :    
770 :     TRUE if the file is for the converse of the desired correspondence, else FALSE.
771 :     If TRUE, the file columns will be reorderd automatically. The default is FALSE,
772 :     meaning we want to use the file as it appears on disk.
773 :    
774 : parrello 1.40 =item all (optional)
775 :    
776 :     TRUE if backward unidirectional correspondences should be included in the output.
777 :     The default is FALSE, in which case only forward and bidirectional correspondences
778 :     are included.
779 :    
780 : parrello 1.37 =item RETURN
781 :    
782 :     Returns a L</Gene Correspondence List> in the form of a reference to a list of lists.
783 :     If the file's contents are invalid or an error occurs, an undefined value will be
784 :     returned.
785 :    
786 :     =back
787 :    
788 :     =cut
789 :    
790 :     sub ReadGeneCorrespondenceFile {
791 :     # Get the parameters.
792 : parrello 1.40 my ($fileName, $converse, $all) = @_;
793 : parrello 1.37 # Declare the return variable. We will only put something in here if we are
794 :     # completely successful.
795 :     my $retVal;
796 :     # This value will be set to 1 if an error is detected.
797 :     my $error = 0;
798 :     # Try to open the file.
799 :     my $ih;
800 :     Trace("Reading correspondence file $fileName.") if T(3);
801 :     if (! open $ih, "<$fileName") {
802 :     # Here the open failed, so we have an error.
803 : parrello 1.44 Trace("Failed to open gene correspondence file $fileName: $!") if T(Corr => 1);
804 : parrello 1.37 $error = 1;
805 :     }
806 :     # The gene correspondence list will be built in here.
807 :     my @corrList;
808 :     # This variable will be set to TRUE if we find a reverse correspondence somewhere
809 :     # in the file. Not finding one is an error.
810 :     my $reverseFound = 0;
811 :     # Loop until we hit the end of the file or an error occurs. We must check the error
812 :     # first in case the file handle failed to open.
813 :     while (! $error && ! eof $ih) {
814 :     # Get the current line.
815 :     my @row = Tracer::GetLine($ih);
816 :     # Get the correspondence direction and check for a reverse arrow.
817 :     $reverseFound = 1 if ($row[8] eq '<-');
818 :     # If we're in converse mode, reformat the line.
819 :     if ($converse) {
820 : parrello 1.39 ReverseGeneCorrespondenceRow(\@row);
821 : parrello 1.37 }
822 :     # Validate the row.
823 :     if (ValidateGeneCorrespondenceRow(\@row)) {
824 : parrello 1.44 Trace("Invalid row $. found in correspondence file $fileName.") if T(Corr => 1);
825 : parrello 1.37 $error = 1;
826 :     }
827 :     # If this row is in the correct direction, keep it.
828 : parrello 1.40 if ($all || $row[8] ne '<-') {
829 : parrello 1.37 push @corrList, \@row;
830 :     }
831 :     }
832 :     # Close the input file.
833 :     close $ih;
834 : parrello 1.57 # If we have no errors, keep the result.
835 : parrello 1.37 if (! $error) {
836 : parrello 1.57 $retVal = \@corrList;
837 : parrello 1.37 }
838 :     # Return the result (if any).
839 :     return $retVal;
840 :     }
841 :    
842 : parrello 1.39 =head3 ReverseGeneCorrespondenceRow
843 :    
844 :     ServerThing::ReverseGeneCorrespondenceRow($row)
845 :    
846 :     Convert a gene correspondence row to represent the converse correspondence. The
847 :     elements in the row will be reordered to represent a correspondence from the
848 :     target genome to the source genome.
849 :    
850 :     =over 4
851 :    
852 :     =item row
853 :    
854 :     Reference to a list containing a single row from a L</Gene Correspondence List>.
855 :    
856 :     =back
857 :    
858 :     =cut
859 :    
860 :     sub ReverseGeneCorrespondenceRow {
861 :     # Get the parameters.
862 :     my ($row) = @_;
863 :     # Flip the row in place.
864 :     ($row->[1], $row->[0], $row->[2], $row->[3], $row->[5], $row->[4], $row->[7],
865 : parrello 1.41 $row->[6], $row->[8], $row->[9], $row->[10], $row->[14],
866 : parrello 1.39 $row->[15], $row->[16], $row->[11], $row->[12], $row->[13], $row->[17]) = @$row;
867 : parrello 1.41 # Flip the arrow.
868 :     $row->[8] = ARROW_FLIP->{$row->[8]};
869 :     # Flip the pairs.
870 : parrello 1.42 my @elements = split /,/, $row->[3];
871 : parrello 1.45 $row->[3] = join(",", map { join(":", reverse split /:/, $_) } @elements);
872 : parrello 1.39 }
873 : parrello 1.37
874 :     =head3 ValidateGeneCorrespondenceRow
875 :    
876 :     my $errorCount = ServerThing::ValidateGeneCorrespondenceRow($row);
877 :    
878 :     Validate a gene correspondence row. The numeric fields are checked to insure they
879 :     are numeric and the source and target gene IDs are validated. The return value will
880 :     indicate the number of errors found.
881 :    
882 :     =over 4
883 :    
884 :     =item row
885 :    
886 :     Reference to a list containing a single row from a L</Gene Correspondence List>.
887 :    
888 :     =item RETURN
889 :    
890 :     Returns the number of errors found in the row. A return of C<0> indicates the row
891 :     is valid.
892 :    
893 :     =back
894 :    
895 :     =cut
896 :    
897 :     sub ValidateGeneCorrespondenceRow {
898 :     # Get the parameters.
899 :     my ($row, $genome1, $genome2) = @_;
900 :     # Denote no errors have been found so far.
901 :     my $retVal = 0;
902 :     # Check for non-numeric values in the number columns.
903 :     for my $col (@{NUM_COLS()}) {
904 :     unless ($row->[$col] =~ /^-?\d+\.?\d*(?:e[+-]?\d+)?$/) {
905 : parrello 1.44 Trace("Gene correspondence error. \"$row->[$col]\" not numeric.") if T(Corr => 2);
906 : parrello 1.37 $retVal++;
907 :     }
908 :     }
909 :     # Check the gene IDs.
910 :     for my $col (0, 1) {
911 :     unless ($row->[$col] =~ /^fig\|\d+\.\d+\.\w+\.\d+$/) {
912 : parrello 1.44 Trace("Gene correspondence error. \"$row->[$col]\" not a gene ID.") if T(Corr => 2);
913 : parrello 1.37 $retVal++;
914 : parrello 1.36 }
915 :     }
916 : parrello 1.37 # Verify the arrow.
917 :     unless (exists ARROW_FLIP->{$row->[8]}) {
918 : parrello 1.44 Trace("Gene correspondence error. \"$row->[8]\" not an arrow.") if T(Corr => 2);
919 : parrello 1.37 $retVal++;
920 :     }
921 :     # Return the error count.
922 : parrello 1.36 return $retVal;
923 :     }
924 :    
925 : parrello 1.53 =head3 GetCorrespondenceData
926 :    
927 :     my $corrList = ServerThing::GetCorrespondenceData($genome1, $genome2, $passive, $full);
928 :    
929 :     Return the L</Gene Correspondence List> for the specified source and target genomes. If the
930 :     list is in a file, it will be read. If the file does not exist, it may be created.
931 :    
932 :     =over 4
933 :    
934 :     =item genome1
935 :    
936 :     ID of the source genome.
937 :    
938 :     =item genome2
939 :    
940 :     ID of the target genome.
941 :    
942 :     =item passive
943 :    
944 :     If TRUE, then the correspondence file will not be created if it does not exist.
945 :    
946 :     =item full
947 :    
948 :     If TRUE, then both directions of the correspondence will be represented; otherwise, only
949 :     correspondences from the source to the target (including bidirectional corresopndences)
950 :     will be included.
951 :    
952 :     =item RETURN
953 :    
954 :     Returns a L</Gene Correspondence List> in the form of a reference to a list of lists, or an
955 :     undefined value if an error occurs or no file exists and passive mode was specified.
956 :    
957 :     =back
958 :    
959 :     =cut
960 :    
961 :     sub GetCorrespondenceData {
962 :     # Get the parameters.
963 :     my ($genome1, $genome2, $passive, $full) = @_;
964 :     # Declare the return variable.
965 :     my $retVal;
966 :     # Check for a gene correspondence file.
967 :     my ($fileName, $converse) = ServerThing::CheckForGeneCorrespondenceFile($genome1, $genome2);
968 :     if ($fileName) {
969 :     # Here we found one, so read it in.
970 :     $retVal = ServerThing::ReadGeneCorrespondenceFile($fileName, $converse, $full);
971 :     }
972 :     # Were we successful?
973 :     if (! defined $retVal) {
974 :     # Here we either don't have a correspondence file, or the one that's there is
975 :     # invalid. If we are NOT in passive mode, then this means we need to create
976 :     # the file.
977 :     if (! $passive) {
978 :     ($fileName, $converse) = ServerThing::CreateGeneCorrespondenceFile($genome1, $genome2);
979 :     # Now try reading the new file.
980 :     if (defined $fileName) {
981 :     $retVal = ServerThing::ReadGeneCorrespondenceFile($fileName, $converse);
982 :     }
983 :     }
984 :     }
985 :     # Return the result.
986 :     return $retVal;
987 :    
988 :     }
989 :    
990 : parrello 1.9
991 :     =head2 Internal Utility Methods
992 :    
993 :     The methods in this section are used internally by this package.
994 :    
995 : parrello 1.6 =head3 RunRequest
996 :    
997 : parrello 1.9 ServerThing::RunRequest($cgi, $serverName);
998 : parrello 1.6
999 :     Run a request from the specified server using the incoming CGI parameter
1000 :     object for the parameters.
1001 :    
1002 :     =over 4
1003 :    
1004 :     =item cgi
1005 :    
1006 : parrello 1.49 CGI query object containing the parameters from the web service request. The
1007 :     significant parameters are as follows.
1008 :    
1009 :     =over 8
1010 :    
1011 :     =item function
1012 :    
1013 :     Name of the function to run.
1014 :    
1015 :     =item args
1016 :    
1017 :     Parameters for the function.
1018 :    
1019 :     =item encoding
1020 :    
1021 :     Encoding scheme for the function parameters, either C<yaml> (the default) or C<json> (used
1022 :     by the Java interface).
1023 :    
1024 :     =back
1025 :    
1026 :     Certain unusual requests can come in outside of the standard function interface.
1027 :     These are indicated by special parameters that override all the others.
1028 :    
1029 :     =over 8
1030 :    
1031 :     =item pod
1032 :    
1033 :     Display a POD documentation module.
1034 :    
1035 :     =item code
1036 :    
1037 :     Display an example code file.
1038 :    
1039 :     =item file
1040 :    
1041 :     Transfer a file (not implemented).
1042 :    
1043 :     =back
1044 : parrello 1.6
1045 : parrello 1.13 =item serverThing
1046 : parrello 1.6
1047 : parrello 1.13 Server object against which to run the request.
1048 : parrello 1.6
1049 :     =back
1050 :    
1051 :     =cut
1052 :    
1053 :     sub RunRequest {
1054 :     # Get the parameters.
1055 : parrello 1.13 my ($cgi, $serverThing, $docURL) = @_;
1056 : parrello 1.9 # Determine the request type.
1057 : parrello 1.52 my $module = $cgi->param('pod');
1058 :     if ($module) {
1059 :     # Here we have a documentation request.
1060 :     if ($module eq 'ServerScripts') {
1061 :     # Here we list the server scripts.
1062 :     require ListServerScripts;
1063 :     ListServerScripts::main();
1064 :     } else {
1065 :     # In this case, we produce POD HTML.
1066 :     ProducePod($cgi->param('pod'));
1067 :     }
1068 : disz 1.31 } elsif ($cgi->param('code')) {
1069 : parrello 1.32 # Here the user wants to see the code for one of our scripts.
1070 :     LineNumberize($cgi->param('code'));
1071 : parrello 1.9 } elsif ($cgi->param('file')) {
1072 :     # Here we have a file request. Process according to the type.
1073 :     my $type = $cgi->param('file');
1074 :     if ($type eq 'open') {
1075 :     OpenFile($cgi->param('name'));
1076 :     } elsif ($type eq 'create') {
1077 :     CreateFile();
1078 :     } elsif ($type eq 'read') {
1079 :     ReadChunk($cgi->param('name'), $cgi->param('location'), $cgi->param('size'));
1080 :     } elsif ($type eq 'write') {
1081 :     WriteChunk($cgi->param('name'), $cgi->param('data'));
1082 :     } else {
1083 :     Die("Invalid file function \"$type\".");
1084 : parrello 1.4 }
1085 : parrello 1.1 } else {
1086 : parrello 1.9 # The default is a function request. Get the function name.
1087 : parrello 1.4 my $function = $cgi->param('function') || "";
1088 : parrello 1.15 Trace("Server function for task $$ is $function.") if T(3);
1089 : parrello 1.4 # Insure the function name is valid.
1090 :     Die("Invalid function name.")
1091 :     if $function =~ /\W/;
1092 : parrello 1.51 # Determing the encoding scheme. The default is YAML.
1093 :     my $encoding = $cgi->param('encoding') || 'yaml';
1094 : parrello 1.4 # The parameter structure will go in here.
1095 : parrello 1.54 my $args = {};
1096 : parrello 1.4 # Start the timer.
1097 :     my $start = time();
1098 :     # The output document goes in here.
1099 :     my $document;
1100 :     # The sapling database goes in here.
1101 :     my $sapling;
1102 :     # Protect from errors.
1103 : parrello 1.1 eval {
1104 : parrello 1.49 # Here we parse the arguments. This is affected by the encoding parameter.
1105 :     # Get the argument string.
1106 :     my $argString = $cgi->param('args');
1107 : parrello 1.54 # Only proceed if we found one.
1108 :     if ($argString) {
1109 :     if ($encoding eq 'yaml') {
1110 :     # Parse the arguments using YAML.
1111 :     $args = YAML::Load($argString);
1112 :     } elsif ($encoding eq 'json') {
1113 :     # Parse the arguments using JSON.
1114 :     Trace("Incoming string is:\n$argString") if T(3);
1115 :     $args = JSON::Any->jsonToObj($argString);
1116 :     } else {
1117 :     Die("Invalid encoding type $encoding.");
1118 :     }
1119 : parrello 1.49 }
1120 : parrello 1.1 };
1121 : parrello 1.4 # Check to make sure we got everything.
1122 : parrello 1.1 if ($@) {
1123 : parrello 1.10 SendError($@, "Error formatting parameters.");
1124 : parrello 1.4 } elsif (! $function) {
1125 : parrello 1.10 SendError("No function specified.", "No function specified.");
1126 : parrello 1.1 } else {
1127 : parrello 1.30 $document = eval { $serverThing->$function($args) };
1128 : parrello 1.1 # If we have an error, create an error document.
1129 :     if ($@) {
1130 : parrello 1.13 SendError($@, "Error detected by service.");
1131 : parrello 1.32 Trace("Error encountered by service: $@") if T(0);
1132 : parrello 1.1 } else {
1133 : parrello 1.51 # No error, so we output the result. Start with an HTML header.
1134 : parrello 1.13 print $cgi->header(-type => 'text/plain');
1135 : parrello 1.51 # The nature of the output depends on the encoding type.
1136 :     my $string;
1137 :     if ($encoding eq 'yaml') {
1138 :     $string = YAML::Dump($document);
1139 :     } else {
1140 :     $string = JSON::Any->objToJson($document);
1141 :     }
1142 : parrello 1.18 print $string;
1143 :     MemTrace(length($string) . " bytes returned from $function by task $$.") if T(Memory => 3);
1144 : parrello 1.1 }
1145 :     }
1146 : parrello 1.4 # Stop the timer.
1147 :     my $duration = int(time() - $start + 0.5);
1148 : parrello 1.29 Trace("Function $function executed in $duration seconds by task $$.") if T(2);
1149 : parrello 1.1 }
1150 :     }
1151 :    
1152 : parrello 1.9 =head3 CreateFile
1153 :    
1154 :     ServerThing::CreateFile();
1155 :    
1156 :     Create a new, empty temporary file and send its name back to the client.
1157 :    
1158 :     =cut
1159 :    
1160 :     sub CreateFile {
1161 :     ##TODO: Code
1162 :     }
1163 :    
1164 :     =head3 OpenFile
1165 : parrello 1.6
1166 : parrello 1.9 ServerThing::OpenFile($name);
1167 : parrello 1.1
1168 : parrello 1.9 Send the length of the named file back to the client.
1169 :    
1170 :     =over 4
1171 :    
1172 :     =item name
1173 :    
1174 :     ##TODO: name description
1175 :    
1176 :     =back
1177 :    
1178 :     =cut
1179 :    
1180 :     sub OpenFile {
1181 :     # Get the parameters.
1182 :     my ($name) = @_;
1183 :     ##TODO: Code
1184 :     }
1185 : parrello 1.1
1186 : parrello 1.9 =head3 ReadChunk
1187 : parrello 1.1
1188 : parrello 1.9 ServerThing::ReadChunk($name, $location, $size);
1189 : parrello 1.1
1190 : parrello 1.9 Read the indicated number of bytes from the specified location of the
1191 :     named file and send them back to the client.
1192 : parrello 1.1
1193 :     =over 4
1194 :    
1195 :     =item name
1196 :    
1197 : parrello 1.9 ##TODO: name description
1198 : parrello 1.1
1199 : parrello 1.9 =item location
1200 : parrello 1.1
1201 : parrello 1.9 ##TODO: location description
1202 : parrello 1.1
1203 : parrello 1.9 =item size
1204 : parrello 1.1
1205 : parrello 1.9 ##TODO: size description
1206 : parrello 1.1
1207 :     =back
1208 :    
1209 :     =cut
1210 :    
1211 : parrello 1.9 sub ReadChunk {
1212 : parrello 1.1 # Get the parameters.
1213 : parrello 1.9 my ($name, $location, $size) = @_;
1214 :     ##TODO: Code
1215 : parrello 1.1 }
1216 :    
1217 : parrello 1.9 =head3 WriteChunk
1218 : parrello 1.1
1219 : parrello 1.9 ServerThing::WriteChunk($name, $data);
1220 : parrello 1.8
1221 : parrello 1.9 Write the specified data to the named file.
1222 : parrello 1.8
1223 :     =over 4
1224 :    
1225 :     =item name
1226 :    
1227 : parrello 1.9 ##TODO: name description
1228 :    
1229 :     =item data
1230 :    
1231 :     ##TODO: data description
1232 :    
1233 :     =back
1234 :    
1235 :     =cut
1236 :    
1237 :     sub WriteChunk {
1238 :     # Get the parameters.
1239 :     my ($name, $data) = @_;
1240 :     ##TODO: Code
1241 :     }
1242 :    
1243 :    
1244 : disz 1.31 =head3 LineNumberize
1245 :    
1246 :     ServerThing::LineNumberize($module);
1247 :    
1248 :     Output the module line by line with line numbers
1249 :    
1250 :     =over 4
1251 :    
1252 :     =item module
1253 :    
1254 :     Name of the module to line numberized
1255 :    
1256 :     =back
1257 :    
1258 :     =cut
1259 :    
1260 :     sub LineNumberize {
1261 :     # Get the parameters.
1262 :     my ($module) = @_;
1263 :     my $fks_path = "$FIG_Config::fig_disk/dist/releases/current/FigKernelScripts/$module";
1264 :     # Start the output page.
1265 :     print CGI::header();
1266 :     print CGI::start_html(-title => 'Documentation Page',
1267 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
1268 :     # Protect from errors.
1269 :     eval {
1270 : parrello 1.32 if (-e $fks_path) {
1271 :     print "<pre>\n";
1272 :     my $i = 1;
1273 :     foreach my $line (`cat $fks_path`) {
1274 :     print "$i.\t$line";
1275 :     $i++;
1276 :     }
1277 :     print "</pre>\n";
1278 :     } else {
1279 :     print "File $fks_path not found";
1280 :     }
1281 : disz 1.31 };
1282 :     # Process any error.
1283 :     if ($@) {
1284 :     print CGI::blockquote({ class => 'error' }, $@);
1285 :     }
1286 :     # Close off the page.
1287 :     print CGI::end_html();
1288 :    
1289 :     }
1290 :    
1291 : parrello 1.9 =head3 ProducePod
1292 :    
1293 :     ServerThing::ProducePod($module);
1294 :    
1295 :     Output the POD documentation for the specified module.
1296 :    
1297 :     =over 4
1298 : parrello 1.8
1299 : parrello 1.9 =item module
1300 : parrello 1.8
1301 : parrello 1.9 Name of the module whose POD document is to be displayed.
1302 : parrello 1.8
1303 :     =back
1304 :    
1305 :     =cut
1306 :    
1307 : parrello 1.9 sub ProducePod {
1308 : parrello 1.8 # Get the parameters.
1309 : parrello 1.9 my ($module) = @_;
1310 :     # Start the output page.
1311 :     print CGI::header();
1312 : parrello 1.46 print CGI::start_html(-title => "$module Documentation Page",
1313 : parrello 1.9 -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
1314 :     # Protect from errors.
1315 :     eval {
1316 :     # We'll format the HTML text in here.
1317 :     require DocUtils;
1318 : parrello 1.20 my $html = DocUtils::ShowPod($module, "http://servers.nmpdr.org/sapling/server.cgi?pod=");
1319 : parrello 1.9 # Output the POD HTML.
1320 :     print $html;
1321 :     };
1322 :     # Process any error.
1323 :     if ($@) {
1324 :     print CGI::blockquote({ class => 'error' }, $@);
1325 : parrello 1.8 }
1326 : parrello 1.9 # Close off the page.
1327 :     print CGI::end_html();
1328 :    
1329 : parrello 1.8 }
1330 :    
1331 :     =head3 TraceErrorLog
1332 :    
1333 :     ServerThing::TraceErrorLog($name, $errorLog);
1334 :    
1335 :     Trace the specified error log file. This is a very dinky routine that
1336 :     performs a task required by L</RunTool> in multiple places.
1337 :    
1338 :     =over 4
1339 :    
1340 :     =item name
1341 :    
1342 :     Name of the tool relevant to the log file.
1343 :    
1344 :     =item errorLog
1345 :    
1346 :     Name of the log file.
1347 :    
1348 :     =back
1349 :    
1350 :     =cut
1351 :    
1352 :     sub TraceErrorLog {
1353 :     my ($name, $errorLog) = @_;
1354 :     my $errorData = Tracer::GetFile($errorLog);
1355 :     Trace("$name error log:\n$errorData");
1356 :     }
1357 :    
1358 : parrello 1.10 =head3 SendError
1359 :    
1360 :     ServerThing::SendError($message, $status);
1361 :    
1362 :     Fail an HTTP request with the specified error message and the specified
1363 :     status message.
1364 :    
1365 :     =over 4
1366 :    
1367 :     =item message
1368 :    
1369 :     Detailed error message. This is sent as the page content.
1370 :    
1371 :     =item status
1372 :    
1373 :     Status message. This is sent as part of the status code.
1374 :    
1375 :     =back
1376 :    
1377 :     =cut
1378 :    
1379 :     sub SendError {
1380 :     # Get the parameters.
1381 :     my ($message, $status) = @_;
1382 :     Trace("Error \"$status\" $message") if T(2);
1383 : parrello 1.30 # Check for a DBserver error. These can be retried and get a special status
1384 :     # code.
1385 :     my $realStatus;
1386 :     if ($message =~ /DBServer Error:\s+/) {
1387 :     $realStatus = "503 $status";
1388 :     } else {
1389 :     $realStatus = "500 $status";
1390 :     }
1391 : parrello 1.10 # Print the header and the status message.
1392 :     print CGI::header(-type => 'text/plain',
1393 : parrello 1.30 -status => $realStatus);
1394 : parrello 1.10 # Print the detailed message.
1395 :     print $message;
1396 :     }
1397 :    
1398 :    
1399 : disz 1.31 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3