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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3