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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3