[Bio] / Sprout / SearchHelper.pm Repository:
ViewVC logotype

Annotation of /Sprout/SearchHelper.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     package SearchHelper;
4 :    
5 :     use strict;
6 :     use Tracer;
7 :     use PageBuilder;
8 :     use Digest::MD5;
9 :     use File::Basename;
10 :     use File::Path;
11 :     use File::stat;
12 :     use LWP::UserAgent;
13 :     use Time::HiRes 'gettimeofday';
14 :     use Sprout;
15 :     use SFXlate;
16 :     use FIGRules;
17 :     use HTML;
18 :     use BasicLocation;
19 : parrello 1.2 use FeatureQuery;
20 : parrello 1.3 use URI::Escape;
21 :     use PageBuilder;
22 : parrello 1.1
23 :     =head1 Search Helper Base Class
24 :    
25 :     =head2 Introduction
26 :    
27 :     The search helper is a base class for all search objects. It has methods for performing
28 :     all the common tasks required to build and manage a search cache. The subclass must
29 :     provide methods for generating and processing search forms. The base class has the
30 :     following object fields.
31 :    
32 :     =over 4
33 :    
34 :     =item cols
35 :    
36 :     Reference to a list of column header descriptions. If undefined, then the session cache
37 :     file has been opened but nothing has been written to it.
38 :    
39 :     =item fileHandle
40 :    
41 :     File handle for the session cache file.
42 :    
43 :     =item query
44 :    
45 :     CGI query object, which includes the search parameters and the various
46 :     session status variables kept between requests from the user.
47 :    
48 :     =item type
49 :    
50 :     Session type: C<old> if there is an existing cache file from which we are
51 :     displaying search results, or C<new> if the cache file needs to be built.
52 :    
53 :     =item class
54 :    
55 :     Name of the search helper class as it would appear in the CGI query object
56 :     (i.e. without the C<SH> prefix.
57 :    
58 :     =item sprout
59 :    
60 :     Sprout object for accessing the database.
61 :    
62 :     =item message
63 :    
64 :     Message to display if an error has been detected.
65 :    
66 :     =item orgs
67 :    
68 :     Reference to a hash mapping genome IDs to organism names.
69 :    
70 :     =item name
71 :    
72 :     Name to use for this object's form.
73 :    
74 :     =item scriptQueue
75 :    
76 :     List of JavaScript statements to be executed after the form is closed.
77 :    
78 : parrello 1.3 =item genomeHash
79 :    
80 :     Cache of the genome group hash used to build genome selection controls.
81 :    
82 :     =item genomeParms
83 :    
84 :     List of the parameters that are used to select multiple genomes.
85 :    
86 :     =item filtered
87 :    
88 :     TRUE if this is a feature-filtered search, else FALSE. B<NOTE> that this
89 :     field is updated by the B<FeatureQuery> object.
90 :    
91 : parrello 1.1 =back
92 :    
93 : parrello 1.2 =head2 Adding a new Search Tool
94 :    
95 :     To add a new search tool to the system, you must
96 :    
97 :     =over 4
98 :    
99 :     =item 1
100 :    
101 :     Choose a class name for your search tool.
102 :    
103 :     =item 2
104 :    
105 :     Create a new subclass of this object and implement each of the virtual methods. The
106 :     name of the subclass must be C<SH>I<className>.
107 :    
108 :     =item 3
109 :    
110 :     Create an include file among the web server pages that describes how to use
111 :     the search tool. The include file must be in the B<includes> directory, and
112 :     its name must be C<SearchHelp_>I<className>C<.inc>.
113 :    
114 :     =item 4
115 :    
116 :     In the C<SearchSkeleton.cgi> script, add a C<use> statement for your search tool
117 :     and then put the class name in the C<@advancedClasses> list.
118 :    
119 :     =back
120 :    
121 :     =head3 Building a Search Form
122 :    
123 :     All search forms are three-column tables. In general, you want one form
124 :     variable per table row. The first column should contain the label and
125 :     the second should contain the form control for specifying the variable
126 :     value. If the control is wide, you should use C<colspan="2"> to give it
127 :     extra room. B<Do not> specify a width in any of your table cells, as
128 :     width management is handled by this class.
129 :    
130 :     The general code for creating the form should be
131 :    
132 :     sub Form {
133 :     my ($self) = @_;
134 :     # Get the CGI object.
135 :     my $cgi = @self->Q();
136 :     # Start the form.
137 :     my $retVal = $self->FormStart("form title");
138 :     # Assemble the table rows.
139 :     my @rows = ();
140 :     ... push table row Html into @rows ...
141 :     push @rows, $self->SubmitRow();
142 :     ... push more Html into @rows ...
143 :     # Build the table from the rows.
144 :     $retVal .= $self->MakeTable(\@rows);
145 :     # Close the form.
146 :     $retVal .= $self->FormEnd();
147 :     # Return the form Html.
148 :     return $retVal;
149 :     }
150 :    
151 :     Several helper methods are provided for particular purposes.
152 :    
153 :     =over 4
154 :    
155 :     =item 1
156 :    
157 : parrello 1.3 L</NmpdrGenomeMenu> generates a control for selecting one or more genomes. Use
158 :     L</GetGenomes> to retrieve all the genomes passed in for a specified parameter
159 :     name. Note that as an assist to people working with GET-style links, if no
160 :     genomes are specified and the incoming request style is GET, all genomes will
161 :     be returned.
162 : parrello 1.2
163 :     =item 2
164 :    
165 :     L</FeatureFilterRow> formats several rows of controls for filtering features.
166 :     When you start building the code for the L</Find> method, you can use a
167 :     B<FeatureQuery> object to automatically filter each genome's features using
168 :     the values from the filter controls.
169 :    
170 :     =item 3
171 :    
172 :     L</QueueFormScript> allows you to queue JavaScript statements for execution
173 :     after the form is fully generated. If you are using very complicated
174 :     form controls, the L</QueueFormScript> method allows you to perform
175 :     JavaScript initialization. The L</NmpdrGenomeMenu> control uses this
176 :     facility to display a list of the pre-selected genomes.
177 :    
178 :     =back
179 :    
180 :     Finally, when generating the code for your controls, be sure to use any incoming
181 :     query parameters as default values so that the search request is persistent.
182 :    
183 :     =head3 Finding Search Results
184 :    
185 :     The L</Find> method is used to create the search results. For a search that
186 :     wants to return features (which is most of them), the basic code structure
187 :     would work as follows. It is assumed that the L</FeatureFilterRows> method
188 :     has been used to create feature filtering parameters.
189 :    
190 :     sub Find {
191 :     my ($self) = @_;
192 :     # Get the CGI and Sprout objects.
193 :     my $cgi = $self->Q();
194 :     my $sprout = $self->DB();
195 :     # Declare the return variable. If it remains undefined, the caller will
196 :     # know that an error occurred.
197 :     my $retVal;
198 :     ... validate the parameters ...
199 :     if (... invalid parameters...) {
200 :     $self->SetMessage(...appropriate message...);
201 :     } elsif (FeatureQuery::Valid($self)) {
202 :     # Initialize the session file.
203 :     $self->OpenSession();
204 :     # Initialize the result counter.
205 :     $retVal = 0;
206 :     ... get a list of genomes ...
207 :     for my $genomeID (... each genome ...) {
208 :     my $fq = FeatureQuery->new($self, $genomeID);
209 :     while (my $feature = $fq->Fetch()) {
210 :     ... examine the feature ...
211 :     if (... we want to keep it ...) {
212 :     $self->PutFeature($fq);
213 :     $retVal++;
214 :     }
215 :     }
216 :     }
217 : parrello 1.9 # Close the session file.
218 :     $self->CloseSession();
219 : parrello 1.2 }
220 :     # Return the result count.
221 :     return $retVal;
222 :     }
223 :    
224 :     A Find method is of course much more complicated than generating a form, and there
225 :     are variations on the above them. For example, you could eschew feature filtering
226 :     entirely in favor of your own custom filtering, you could include extra columns
227 :     in the output, or you could search for something that's not a feature at all. The
228 :     above code is just a loose framework.
229 :    
230 :     If you wish to add your own extra columns to the output, use the B<AddExtraColumns>
231 :     method of the feature query object.
232 :    
233 :     $fq->AddExtraColumns(score => $sc);
234 :    
235 :     The L</Find> method must return C<undef> if the search parameters are invalid. If this
236 :     is the case, then a message describing the problem should be passed to the framework
237 :     by calling L</SetMessage>. If the parameters are valid, then the method must return
238 :     the number of items found.
239 :    
240 : parrello 1.1 =cut
241 :    
242 :     # This counter is used to insure every form on the page has a unique name.
243 :     my $formCount = 0;
244 :    
245 :     =head2 Public Methods
246 :    
247 :     =head3 new
248 :    
249 :     C<< my $shelp = SearchHelper->new($query); >>
250 :    
251 :     Construct a new SearchHelper object.
252 :    
253 :     =over 4
254 :    
255 :     =item query
256 :    
257 :     The CGI query object for the current script.
258 :    
259 :     =back
260 :    
261 :     =cut
262 :    
263 :     sub new {
264 :     # Get the parameters.
265 :     my ($class, $query) = @_;
266 :     # Check for a session ID.
267 :     my $session_id = $query->param("SessionID");
268 :     my $type = "old";
269 :     if (! $session_id) {
270 :     # Here we're starting a new session. We create the session ID and
271 :     # store it in the query object.
272 :     $session_id = NewSessionID();
273 :     $type = "new";
274 :     $query->param(-name => 'SessionID', -value => $session_id);
275 :     }
276 :     # Compute the subclass name.
277 :     $class =~ /SH(.+)$/;
278 :     my $subClass = $1;
279 :     # Insure everybody knows we're in Sprout mode.
280 :     $query->param(-name => 'SPROUT', -value => 1);
281 :     # Generate the form name.
282 :     my $formName = "$class$formCount";
283 :     $formCount++;
284 :     # Create the shelp object. It contains the query object (with the session ID)
285 :     # as well as an indicator as to whether or not the session is new, plus the
286 : parrello 1.2 # class name and a placeholder for the Sprout object.
287 : parrello 1.1 my $retVal = {
288 :     query => $query,
289 :     type => $type,
290 :     class => $subClass,
291 : parrello 1.2 sprout => undef,
292 : parrello 1.1 orgs => {},
293 :     name => $formName,
294 :     scriptQueue => [],
295 : parrello 1.3 genomeList => undef,
296 :     genomeParms => [],
297 :     filtered => 0,
298 : parrello 1.1 };
299 :     # Bless and return it.
300 :     bless $retVal, $class;
301 :     return $retVal;
302 :     }
303 :    
304 :     =head3 Q
305 :    
306 :     C<< my $query = $shelp->Q(); >>
307 :    
308 :     Return the CGI query object.
309 :    
310 :     =cut
311 :    
312 :     sub Q {
313 :     # Get the parameters.
314 :     my ($self) = @_;
315 :     # Return the result.
316 :     return $self->{query};
317 :     }
318 :    
319 : parrello 1.9
320 :    
321 : parrello 1.1 =head3 DB
322 :    
323 :     C<< my $sprout = $shelp->DB(); >>
324 :    
325 :     Return the Sprout database object.
326 :    
327 :     =cut
328 :    
329 :     sub DB {
330 :     # Get the parameters.
331 :     my ($self) = @_;
332 : parrello 1.2 # Insure we have a database.
333 :     my $retVal = $self->{sprout};
334 :     if (! defined $retVal) {
335 :     $retVal = SFXlate->new_sprout_only();
336 :     $self->{sprout} = $retVal;
337 :     }
338 : parrello 1.1 # Return the result.
339 : parrello 1.2 return $retVal;
340 : parrello 1.1 }
341 :    
342 :     =head3 IsNew
343 :    
344 :     C<< my $flag = $shelp->IsNew(); >>
345 :    
346 :     Return TRUE if this is a new session, FALSE if this is an old session. An old
347 :     session already has search results ready to process.
348 :    
349 :     =cut
350 :    
351 :     sub IsNew {
352 :     # Get the parameters.
353 :     my ($self) = @_;
354 :     # Return the result.
355 :     return ($self->{type} eq 'new');
356 :     }
357 :    
358 :     =head3 ID
359 :    
360 :     C<< my $sessionID = $shelp->ID(); >>
361 :    
362 :     Return the current session ID.
363 :    
364 :     =cut
365 :    
366 :     sub ID {
367 :     # Get the parameters.
368 :     my ($self) = @_;
369 :     # Return the result.
370 :     return $self->Q()->param("SessionID");
371 :     }
372 :    
373 :     =head3 FormName
374 :    
375 :     C<< my $name = $shelp->FormName(); >>
376 :    
377 :     Return the name of the form this helper object will generate.
378 :    
379 :     =cut
380 :    
381 :     sub FormName {
382 :     # Get the parameters.
383 :     my ($self) = @_;
384 :     # Return the result.
385 :     return $self->{name};
386 :     }
387 :    
388 :     =head3 QueueFormScript
389 :    
390 :     C<< $shelp->QueueFormScript($statement); >>
391 :    
392 :     Add the specified statement to the queue of JavaScript statements that are to be
393 :     executed when the form has been fully defined. This is necessary because until
394 :     the closing </FORM> tag is emitted, the form elements cannot be referenced by
395 :     name. When generating the statement, you can refer to the variable C<thisForm>
396 :     in order to reference the form in progress. Thus,
397 :    
398 :     thisForm.simLimit.value = 1e-10;
399 :    
400 :     would set the value of the form element C<simLimit> in the current form to
401 :     C<1e-10>.
402 :    
403 :     =over 4
404 :    
405 :     =item statement
406 :    
407 :     JavaScript statement to be queued for execution after the form is built.
408 :     The trailing semi-colon is required. Theoretically, you could include
409 :     multiple statements separated by semi-colons, but one at a time works
410 :     just as well.
411 :    
412 :     =back
413 :    
414 :     =cut
415 :    
416 :     sub QueueFormScript {
417 :     # Get the parameters.
418 :     my ($self, $statement) = @_;
419 :     # Push the statement onto the script queue.
420 :     push @{$self->{scriptQueue}}, $statement;
421 :     }
422 :    
423 :     =head3 FormStart
424 :    
425 :     C<< my $html = $shelp->FormStart($title); >>
426 :    
427 :     Return the initial section of a form designed to perform another search of the
428 :     same type. The form header is included along with hidden fields to persist the
429 :     tracing, sprout status, and search class.
430 :    
431 :     A call to L</FormEnd> is required to close the form.
432 :    
433 :     =over 4
434 :    
435 :     =item title
436 :    
437 :     Title to be used for the form.
438 :    
439 :     =item RETURN
440 :    
441 :     Returns the initial HTML for the search form.
442 :    
443 :     =back
444 :    
445 :     =cut
446 :    
447 :     sub FormStart {
448 :     # Get the parameters.
449 :     my ($self, $title) = @_;
450 :     # Get the CGI object.
451 :     my $cgi = $self->Q();
452 :     # Start the form.
453 :     my $retVal = "<div class=\"search\">\n" .
454 :     $cgi->start_form(-method => 'POST',
455 :     -action => $cgi->url(-relative => 1),
456 :     -name => $self->FormName()) .
457 :     $cgi->hidden(-name => 'Class',
458 :     -value => $self->{class}) .
459 :     $cgi->hidden(-name => 'SPROUT',
460 :     -value => 1) .
461 :     $cgi->h3($title);
462 :     # If tracing is on, add it to the form.
463 :     if ($cgi->param('Trace')) {
464 :     $retVal .= $cgi->hidden(-name => 'Trace',
465 :     -value => $cgi->param('Trace')) .
466 :     $cgi->hidden(-name => 'TF',
467 :     -value => ($cgi->param('TF') ? 1 : 0));
468 :     }
469 :     # Put in an anchor tag in case there's a table of contents.
470 :     my $anchorName = $self->FormName();
471 :     $retVal .= "<a name=\"$anchorName\"></a>\n";
472 :     # Return the result.
473 :     return $retVal;
474 :     }
475 :    
476 :     =head3 FormEnd
477 :    
478 :     C<< my $htmlText = $shelp->FormEnd(); >>
479 :    
480 :     Return the HTML text for closing a search form. This closes both the C<form> and
481 :     C<div> tags.
482 :    
483 :     =cut
484 :    
485 :     sub FormEnd {
486 :     # Get the parameters.
487 :     my ($self) = @_;
488 :     # Declare the return variable, closing the form and the DIV block.
489 :     my $retVal = "</form></div>\n";
490 :     # Now we flush out the statement queue.
491 :     my @statements = @{$self->{scriptQueue}};
492 :     if (@statements > 0) {
493 :     # Switch to JavaScript and set the "thisForm" variable.
494 :     $retVal .= "<SCRIPT language=\"JavaScript\">\n" .
495 :     " thisForm = document.$self->{name};\n";
496 :     # Unroll the statements.
497 :     while (@statements > 0) {
498 :     my $statement = shift @statements;
499 :     $retVal .= " $statement\n";
500 :     }
501 :     # Close the JavaScript.
502 :     $retVal .= "</SCRIPT>\n";
503 :     }
504 :     # Return the result.
505 :     return $retVal;
506 :     }
507 :    
508 :     =head3 SetMessage
509 :    
510 :     C<< $shelp->SetMessage($msg); >>
511 :    
512 :     Store the specified text as the result message. The result message is displayed
513 :     if an invalid parameter value is specified.
514 :    
515 :     =over 4
516 :    
517 :     =item msg
518 :    
519 :     Text of the result message to be displayed.
520 :    
521 :     =back
522 :    
523 :     =cut
524 :    
525 :     sub SetMessage {
526 :     # Get the parameters.
527 :     my ($self, $msg) = @_;
528 :     # Store the message.
529 :     $self->{message} = $msg;
530 :     }
531 :    
532 :     =head3 Message
533 :    
534 :     C<< my $text = $shelp->Message(); >>
535 :    
536 :     Return the result message. The result message is displayed if an invalid parameter
537 :     value is specified.
538 :    
539 :     =cut
540 :    
541 :     sub Message {
542 :     # Get the parameters.
543 :     my ($self) = @_;
544 :     # Return the result.
545 :     return $self->{message};
546 :     }
547 :    
548 :     =head3 OpenSession
549 :    
550 :     C<< $shelp->OpenSession(); >>
551 :    
552 :     Set up to open the session cache file for writing. Note we don't actually
553 :     open the file until after we know the column headers.
554 :    
555 :     =cut
556 :    
557 :     sub OpenSession {
558 :     # Get the parameters.
559 :     my ($self) = @_;
560 :     # Denote we have not yet written out the column headers.
561 :     $self->{cols} = undef;
562 :     }
563 :    
564 :     =head3 GetCacheFileName
565 :    
566 :     C<< my $fileName = $shelp->GetCacheFileName(); >>
567 :    
568 :     Return the name to be used for this session's cache file.
569 :    
570 :     =cut
571 :    
572 :     sub GetCacheFileName {
573 :     # Get the parameters.
574 :     my ($self) = @_;
575 :     # Return the result.
576 :     return $self->GetTempFileName('cache');
577 :     }
578 :    
579 :     =head3 GetTempFileName
580 :    
581 :     C<< my $fileName = $shelp->GetTempFileName($type); >>
582 :    
583 :     Return the name to be used for a temporary file of the specified type. The
584 :     name is computed from the session name with the type as a suffix.
585 :    
586 :     =over 4
587 :    
588 :     =item type
589 :    
590 :     Type of temporary file to be generated.
591 :    
592 :     =item RETURN
593 :    
594 :     Returns a file name generated from the session name and the specified type.
595 :    
596 :     =back
597 :    
598 :     =cut
599 :    
600 :     sub GetTempFileName {
601 :     # Get the parameters.
602 :     my ($self, $type) = @_;
603 :     # Compute the file name. Note it gets stuffed in the FIG temporary
604 :     # directory.
605 :     my $retVal = "$FIG_Config::temp/tmp_" . $self->ID() . ".$type";
606 :     # Return the result.
607 :     return $retVal;
608 :     }
609 :    
610 :     =head3 PutFeature
611 :    
612 : parrello 1.2 C<< $shelp->PutFeature($fquery); >>
613 : parrello 1.1
614 :     Store a feature in the result cache. This is the workhorse method for most
615 :     searches, since the primary data item in the database is features.
616 :    
617 :     For each feature, there are certain columns that are standard: the feature name, the
618 :     GBrowse and protein page links, the functional assignment, and so forth. If additional
619 : parrello 1.2 columns are required by a particular search subclass, they should be stored in
620 :     the feature query object using the B<AddExtraColumns> method. For example, the following
621 :     code adds columns for essentiality and virulence.
622 : parrello 1.1
623 : parrello 1.2 $fq->AddExtraColumns(essential => $essentialFlag, virulence => $vfactor);
624 :     $shelp->PutFeature($fq);
625 : parrello 1.1
626 :     For correct results, all values should be specified for all extra columns in all calls to
627 :     B<PutFeature>. (In particular, the column header names are computed on the first
628 :     call.) If a column is to be blank for the current feature, its value can be given
629 :     as C<undef>.
630 :    
631 :     if (! $essentialFlag) {
632 :     $essentialFlag = undef;
633 :     }
634 : parrello 1.2 $fq->AddExtraColumns(essential => $essentialFlag, virulence => $vfactor);
635 :     $shelp->PutFeature($fq);
636 : parrello 1.1
637 :     =over 4
638 :    
639 : parrello 1.2 =item fquery
640 : parrello 1.1
641 : parrello 1.2 FeatureQuery object containing the current feature data.
642 : parrello 1.1
643 :     =back
644 :    
645 :     =cut
646 :    
647 :     sub PutFeature {
648 : parrello 1.2 # Get the parameters.
649 :     my ($self, $fq) = @_;
650 : parrello 1.3 # Get the CGI query object.
651 :     my $cgi = $self->Q();
652 : parrello 1.2 # Get the feature data.
653 :     my $record = $fq->Feature();
654 :     my $extraCols = $fq->ExtraCols();
655 : parrello 1.1 # Check for a first-call situation.
656 :     if (! defined $self->{cols}) {
657 :     # Here we need to set up the column information. Start with the defaults.
658 :     $self->{cols} = $self->DefaultFeatureColumns();
659 : parrello 1.3 # Add the externals if they were requested.
660 :     if ($cgi->param('ShowAliases')) {
661 :     push @{$self->{cols}}, 'alias';
662 :     }
663 : parrello 1.2 # Append the extras, sorted by column name.
664 :     for my $col (sort keys %{$extraCols}) {
665 :     push @{$self->{cols}}, "X=$col";
666 : parrello 1.1 }
667 :     # Write out the column headers. This also prepares the cache file to receive
668 :     # output.
669 :     $self->WriteColumnHeaders(map { $self->FeatureColumnTitle($_) } @{$self->{cols}});
670 :     }
671 :     # Get the feature ID.
672 :     my ($fid) = $record->Value('Feature(id)');
673 :     # Loop through the column headers, producing the desired data.
674 :     my @output = ();
675 :     for my $colName (@{$self->{cols}}) {
676 : parrello 1.2 push @output, $self->FeatureColumnValue($colName, $record, $extraCols);
677 : parrello 1.1 }
678 : parrello 1.9 # Compute the sort key. The sort key usually floats NMPDR organism features to the
679 : parrello 1.1 # top of the return list.
680 : parrello 1.4 my $key = $self->SortKey($record);
681 : parrello 1.1 # Write the feature data.
682 :     $self->WriteColumnData($key, @output);
683 :     }
684 :    
685 :     =head3 WriteColumnHeaders
686 :    
687 :     C<< $shelp->WriteColumnHeaders(@colNames); >>
688 :    
689 :     Write out the column headers for the current search session. The column headers
690 :     are sent to the cache file, and then the cache is re-opened as a sort pipe and
691 :     the handle saved.
692 :    
693 :     =over 4
694 :    
695 :     =item colNames
696 :    
697 :     A list of column names in the desired presentation order.
698 :    
699 :     =back
700 :    
701 :     =cut
702 :    
703 :     sub WriteColumnHeaders {
704 :     # Get the parameters.
705 :     my ($self, @colNames) = @_;
706 :     # Get the cache file name and open it for output.
707 :     my $fileName = $self->GetCacheFileName();
708 :     my $handle1 = Open(undef, ">$fileName");
709 :     # Write the column headers and close the file.
710 :     Tracer::PutLine($handle1, \@colNames);
711 :     close $handle1;
712 :     # Now open the sort pipe and save the file handle. Note how we append the
713 :     # sorted data to the column header row already in place. The output will
714 :     # contain a sort key followed by the real columns. The sort key is
715 :     # hacked off before going to the output file.
716 :     $self->{fileHandle} = Open(undef, "| sort | cut --fields=2- >>$fileName");
717 :     }
718 :    
719 :     =head3 WriteColumnData
720 :    
721 :     C<< $shelp->WriteColumnData($key, @colValues); >>
722 :    
723 :     Write a row of column values to the current search session. It is assumed that
724 :     the session file is already open for output.
725 :    
726 :     =over 4
727 :    
728 :     =item key
729 :    
730 :     Sort key.
731 :    
732 :     =item colValues
733 :    
734 :     List of column values to write to the search result cache file for this session.
735 :    
736 :     =back
737 :    
738 :     =cut
739 :    
740 :     sub WriteColumnData {
741 :     # Get the parameters.
742 :     my ($self, $key, @colValues) = @_;
743 :     # Write them to the cache file.
744 :     Tracer::PutLine($self->{fileHandle}, [$key, @colValues]);
745 :     }
746 :    
747 :     =head3 CloseSession
748 :    
749 :     C<< $shelp->CloseSession(); >>
750 :    
751 :     Close the session file.
752 :    
753 :     =cut
754 :    
755 :     sub CloseSession {
756 :     # Get the parameters.
757 :     my ($self) = @_;
758 :     # Check for an open session file.
759 :     if (defined $self->{fileHandle}) {
760 :     # We found one, so close it.
761 : parrello 1.9 Trace("Closing session file.") if T(2);
762 : parrello 1.1 close $self->{fileHandle};
763 :     }
764 :     }
765 :    
766 :     =head3 NewSessionID
767 :    
768 :     C<< my $id = SearchHelpers::NewSessionID(); >>
769 :    
770 :     Generate a new session ID for the current user.
771 :    
772 :     =cut
773 :    
774 :     sub NewSessionID {
775 :     # Declare the return variable.
776 :     my $retVal;
777 :     # Get a digest encoder.
778 :     my $md5 = Digest::MD5->new();
779 : parrello 1.4 # Add the PID, the IP, and the time stamp. Note that the time stamp is
780 :     # actually two numbers, and we get them both because we're in list
781 :     # context.
782 :     $md5->add($$, $ENV{REMOTE_ADDR}, $ENV{REMOTE_PORT}, gettimeofday());
783 :     # Hash up all this identifying data.
784 :     $retVal = $md5->hexdigest();
785 :     # Return the result.
786 : parrello 1.1 return $retVal;
787 :     }
788 :    
789 :     =head3 OrganismData
790 :    
791 :     C<< my ($orgName, $group) = $shelp->Organism($genomeID); >>
792 :    
793 :     Return the name and status of the organism corresponding to the specified genome ID.
794 :     For performance reasons, this information is cached in a special hash table, so we
795 :     only compute it once per run.
796 :    
797 :     =over 4
798 :    
799 :     =item genomeID
800 :    
801 :     ID of the genome whose name is desired.
802 :    
803 :     =item RETURN
804 :    
805 :     Returns a list of two items. The first item in the list is the organism name,
806 :     and the second is the name of the NMPDR group, or an empty string if the
807 :     organism is not in an NMPDR group.
808 :    
809 :     =back
810 :    
811 :     =cut
812 :    
813 :     sub OrganismData {
814 :     # Get the parameters.
815 :     my ($self, $genomeID) = @_;
816 :     # Declare the return variables.
817 :     my ($orgName, $group);
818 :     # Check the cache.
819 :     my $cache = $self->{orgs};
820 :     if (exists $cache->{$genomeID}) {
821 :     ($orgName, $group) = @{$cache->{$genomeID}};
822 :     } else {
823 :     # Here we have to use the database.
824 :     my $sprout = $self->DB();
825 :     my ($genus, $species, $strain, $group) = $sprout->GetEntityValues('Genome', $genomeID,
826 :     ['Genome(genus)', 'Genome(species)',
827 :     'Genome(unique-characterization)',
828 :     'Genome(primary-group)']);
829 :     # Null out the supporting group.
830 :     $group = "" if ($group eq $FIG_Config::otherGroup);
831 :     # If the organism does not exist, format an unknown name.
832 :     if (! defined($genus)) {
833 :     $orgName = "Unknown Genome $genomeID";
834 :     } else {
835 :     # It does exist, so format the organism name.
836 :     $orgName = "$genus $species";
837 :     if ($strain) {
838 :     $orgName .= " $strain";
839 :     }
840 :     }
841 :     # Save this organism in the cache.
842 :     $cache->{$genomeID} = [$orgName, $group];
843 :     }
844 :     # Return the result.
845 :     return ($orgName, $group);
846 :     }
847 :    
848 :     =head3 Organism
849 :    
850 :     C<< my $orgName = $shelp->Organism($genomeID); >>
851 :    
852 :     Return the name of the relevant organism. The name is computed from the genus,
853 :     species, and unique characterization. A cache is used to improve performance.
854 :    
855 :     =over 4
856 :    
857 :     =item genomeID
858 :    
859 :     ID of the genome whose name is desired.
860 :    
861 :     =item RETURN
862 :    
863 :     Returns the display name of the specified organism.
864 :    
865 :     =back
866 :    
867 :     =cut
868 :    
869 :     sub Organism {
870 :     # Get the parameters.
871 :     my ($self, $genomeID) = @_;
872 :     # Get the organism data.
873 :     my ($retVal, $group) = $self->OrganismData($genomeID);
874 :     # Return the result.
875 :     return $retVal;
876 :     }
877 :    
878 :     =head3 FeatureGroup
879 :    
880 :     C<< my $groupName = $shelp->FeatureGroup($fid); >>
881 :    
882 :     Return the group name for the specified feature.
883 :    
884 :     =over 4
885 :    
886 :     =item fid
887 :    
888 :     ID of the relevant feature.
889 :    
890 :     =item RETURN
891 :    
892 :     Returns the name of the NMPDR group to which the feature belongs, or an empty
893 :     string if it is not part of an NMPDR group.
894 :    
895 :     =back
896 :    
897 :     =cut
898 :    
899 :     sub FeatureGroup {
900 :     # Get the parameters.
901 :     my ($self, $fid) = @_;
902 :     # Parse the feature ID to get the genome ID.
903 :     my ($genomeID) = FIGRules::ParseFeatureID($fid);
904 :     # Get the organism data.
905 :     my (undef, $retVal) = $self->OrganismData($genomeID);
906 :     # Return the result.
907 :     return $retVal;
908 :     }
909 :    
910 :     =head3 FeatureName
911 :    
912 :     C<< my $fidName = $shelp->FeatureName($fid); >>
913 :    
914 :     Return the display name of the specified feature.
915 :    
916 :     =over 4
917 :    
918 :     =item fid
919 :    
920 :     ID of the feature whose name is desired.
921 :    
922 :     =item RETURN
923 :    
924 :     A displayable feature name, consisting of the organism name plus some feature
925 :     type and location information.
926 :    
927 :     =back
928 :    
929 :     =cut
930 :    
931 :     sub FeatureName {
932 :     # Get the parameters.
933 :     my ($self, $fid) = @_;
934 :     # Declare the return variable
935 :     my $retVal;
936 :     # Parse the feature ID.
937 :     my ($genomeID, $type, $num) = FIGRules::ParseFeatureID($fid);
938 :     if (! defined $genomeID) {
939 :     # Here the feature ID has an invalid format.
940 :     $retVal = "External: $fid";
941 :     } else {
942 :     # Here we can get its genome data.
943 :     $retVal = $self->Organism($genomeID);
944 : parrello 1.4 # Append the FIG ID.
945 :     $retVal .= " [$fid]";
946 : parrello 1.1 }
947 :     # Return the result.
948 :     return $retVal;
949 :     }
950 :    
951 :     =head3 ComputeFASTA
952 :    
953 :     C<< my $fasta = $shelp->ComputeFASTA($incomingType, $desiredType, $sequence); >>
954 :    
955 :     Parse a sequence input and convert it into a FASTA string of the desired type. Note
956 :     that it is possible to convert a DNA sequence into a protein sequence, but the reverse
957 :     is not possible.
958 :    
959 :     =over 4
960 :    
961 :     =item incomingType
962 :    
963 :     C<dna> if this is a DNA sequence, C<prot> if this is a protein sequence.
964 :    
965 :     =item desiredType
966 :    
967 :     C<dna> to return a DNA sequence, C<prot> to return a protein sequence. If the
968 :     I<$incomingType> is C<prot> and this value is C<dna>, an error will be thrown.
969 :    
970 :     =item sequence
971 :    
972 :     Sequence to return. It may be a DNA or protein sequence in FASTA form or a feature ID.
973 :     If a feature ID is specified, the feature's DNA or translation will be returned. The
974 :     feature ID is recognized by the presence of a vertical bar in the input. Otherwise,
975 :     if the input does not begin with a greater-than sign (FASTA label line), a default label
976 :     line will be provided.
977 :    
978 :     =item RETURN
979 :    
980 :     Returns a string in FASTA format representing the content of the desired sequence with
981 :     an appropriate label. If the input is invalid, a message will be stored and we will
982 :     return C<undef>. Note that the output will include a trailing new-line.
983 :    
984 :     =back
985 :    
986 :     =cut
987 :    
988 :     sub ComputeFASTA {
989 :     # Get the parameters.
990 :     my ($self, $incomingType, $desiredType, $sequence) = @_;
991 :     # Declare the return variable. If an error occurs, it will remain undefined.
992 :     my $retVal;
993 :     # Create variables to hold the FASTA label and data.
994 :     my ($fastaLabel, $fastaData);
995 :     # Check for a feature specification.
996 :     if ($sequence =~ /^\s*(\w+\|\S+)\s*$/) {
997 :     # Here we have a feature ID in $1. We'll need the Sprout object to process
998 :     # it.
999 :     my $fid = $1;
1000 :     my $sprout = $self->DB();
1001 :     # Get the FIG ID. Note that we only use the first feature found. We are not
1002 :     # supposed to have redundant aliases, though we may have an ID that doesn't
1003 :     # exist.
1004 :     my ($figID) = $sprout->FeaturesByAlias($fid);
1005 :     if (! $figID) {
1006 :     $self->SetMessage("No feature found with the ID \"$fid\".");
1007 :     } else {
1008 :     # Set the FASTA label.
1009 :     my $fastaLabel = $fid;
1010 :     # Now proceed according to the sequence type.
1011 :     if ($desiredType =~ /prot/i) {
1012 :     # We want protein, so get the translation.
1013 :     $fastaData = $sprout->FeatureTranslation($figID);
1014 :     } else {
1015 :     # We want DNA, so get the DNA sequence. This is a two-step process.
1016 :     my @locList = $sprout->FeatureLocation($figID);
1017 :     $fastaData = $sprout->DNASeq(\@locList);
1018 :     }
1019 :     }
1020 :     } elsif ($incomingType =~ /prot/ && $desiredType =~ /dna/) {
1021 :     # Here we're being asked to do an impossible conversion.
1022 :     $self->SetMessage("Cannot convert a protein sequence to DNA.");
1023 :     } else {
1024 :     # Here we are expecting a FASTA. We need to see if there's a label.
1025 :     if ($sequence =~ /^>\s*(\S.*)\s*\n(.+)$/) {
1026 :     # Here we have a label, so we split it from the data.
1027 :     $fastaLabel = $1;
1028 :     $fastaData = $2;
1029 :     } else {
1030 :     # Here we have no label, so we create one and use the entire sequence
1031 :     # as data.
1032 :     $fastaLabel = "User-specified $incomingType sequence";
1033 :     $fastaData = $sequence;
1034 :     }
1035 :     # The next step is to clean the junk out of the sequence.
1036 :     $fastaData =~ s/\n//g;
1037 :     $fastaData =~ s/\s+//g;
1038 :     # Finally, if the user wants to convert to protein, we do it here. Note that
1039 :     # we've already prevented a conversion from protein to DNA.
1040 :     if ($incomingType ne $desiredType) {
1041 :     $fastaData = Sprout::Protein($fastaData);
1042 :     }
1043 :     }
1044 :     # At this point, either "$fastaLabel" and "$fastaData" have values or an error is
1045 :     # in progress.
1046 :     if (defined $fastaLabel) {
1047 :     # We need to format the sequence into 60-byte chunks. We use the infamous
1048 :     # grep-split trick. The split, because of the presence of the parentheses,
1049 :     # includes the matched delimiters in the output list. The grep strips out
1050 :     # the empty list items that appear between the so-called delimiters, since
1051 :     # the delimiters are what we want.
1052 :     my @chunks = grep { $_ } split /(.{1,60})/, $fastaData;
1053 :     my $retVal = join("\n", ">$fastaLabel", @chunks, "");
1054 :     }
1055 :     # Return the result.
1056 :     return $retVal;
1057 :     }
1058 :    
1059 :     =head3 NmpdrGenomeMenu
1060 :    
1061 : parrello 1.3 C<< my $htmlText = $shelp->NmpdrGenomeMenu($menuName, $multiple, \@selected, $rows); >>
1062 : parrello 1.1
1063 :     This method creates a hierarchical HTML menu for NMPDR genomes organized by category. The
1064 :     category indicates the low-level NMPDR group. Organizing the genomes in this way makes it
1065 :     easier to select all genomes from a particular category.
1066 :    
1067 :     =over 4
1068 :    
1069 :     =item menuName
1070 :    
1071 :     Name to give to the menu.
1072 :    
1073 : parrello 1.3 =item multiple
1074 : parrello 1.1
1075 : parrello 1.3 TRUE if the user is allowed to select multiple genomes, else FALSE.
1076 : parrello 1.1
1077 :     =item selected
1078 :    
1079 :     Reference to a list containing the IDs of the genomes to be pre-selected. If the menu
1080 :     is not intended to allow multiple selections, the list should be a singleton. If the
1081 :     list is empty, nothing will be pre-selected.
1082 :    
1083 : parrello 1.3 =item rows (optional)
1084 :    
1085 :     Number of rows to display. If omitted, the default is 1 for a single-select list
1086 :     and 10 for a multi-select list.
1087 :    
1088 : parrello 1.8 =item crossMenu (optional)
1089 :    
1090 :     If specified, is presumed to be the name of another genome menu whose contents
1091 :     are to be mutually exclusive with the contents of this menu. As a result, instead
1092 :     of the standard onChange event, the onChange event will deselect any entries in
1093 :     the other menu.
1094 :    
1095 : parrello 1.1 =item RETURN
1096 :    
1097 :     Returns the HTML text to generate a C<SELECT> menu inside a form.
1098 :    
1099 :     =back
1100 :    
1101 :     =cut
1102 :    
1103 :     sub NmpdrGenomeMenu {
1104 :     # Get the parameters.
1105 : parrello 1.8 my ($self, $menuName, $multiple, $selected, $rows, $cross) = @_;
1106 : parrello 1.1 # Get the Sprout and CGI objects.
1107 :     my $sprout = $self->DB();
1108 :     my $cgi = $self->Q();
1109 : parrello 1.3 # Compute the row count.
1110 :     if (! defined $rows) {
1111 :     $rows = ($multiple ? 10 : 1);
1112 :     }
1113 :     # Create the multiple tag.
1114 :     my $multipleTag = ($multiple ? " multiple" : "");
1115 : parrello 1.1 # Get the form name.
1116 :     my $formName = $self->FormName();
1117 : parrello 1.3 # Check to see if we already have a genome list in memory.
1118 :     my $genomes = $self->{genomeList};
1119 :     my $groupHash;
1120 :     if (defined $genomes) {
1121 :     # We have a list ready to use.
1122 :     $groupHash = $genomes;
1123 :     } else {
1124 :     # Get a list of all the genomes in group order. In fact, we only need them ordered
1125 :     # by name (genus,species,strain), but putting primary-group in front enables us to
1126 :     # take advantage of an existing index.
1127 :     my @genomeList = $sprout->GetAll(['Genome'],
1128 :     "ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
1129 :     [], ['Genome(primary-group)', 'Genome(id)',
1130 :     'Genome(genus)', 'Genome(species)',
1131 :     'Genome(unique-characterization)']);
1132 :     # Create a hash to organize the genomes by group. Each group will contain a list of
1133 :     # 2-tuples, the first element being the genome ID and the second being the genome
1134 :     # name.
1135 :     my %gHash = ();
1136 :     for my $genome (@genomeList) {
1137 :     # Get the genome data.
1138 :     my ($group, $genomeID, $genus, $species, $strain) = @{$genome};
1139 :     # Form the genome name.
1140 :     my $name = "$genus $species";
1141 :     if ($strain) {
1142 :     $name .= " $strain";
1143 :     }
1144 :     # Push the genome into the group's list.
1145 :     push @{$gHash{$group}}, [$genomeID, $name];
1146 : parrello 1.1 }
1147 : parrello 1.3 # Save the genome list for future use.
1148 :     $self->{genomeList} = \%gHash;
1149 :     $groupHash = \%gHash;
1150 : parrello 1.1 }
1151 :     # Now we are ready to unroll the menu out of the group hash. First, we sort the groups, putting
1152 :     # the supporting-genome group last.
1153 : parrello 1.3 my @groups = sort grep { $_ ne $FIG_Config::otherGroup } keys %{$groupHash};
1154 : parrello 1.1 push @groups, $FIG_Config::otherGroup;
1155 : parrello 1.3 # Next, create a hash that specifies the pre-selected entries. Note that we need to deal
1156 :     # with the possibility of undefined values in the incoming list.
1157 :     my %selectedHash = ();
1158 :     if (defined $selected) {
1159 :     %selectedHash = map { $_ => 1 } grep { defined($_) } @{$selected};
1160 :     }
1161 : parrello 1.1 # Now it gets complicated. We need a way to mark all the NMPDR genomes.
1162 :     # Create the type counters.
1163 :     my $groupCount = 1;
1164 :     # Compute the ID for the status display.
1165 :     my $divID = "${formName}_${menuName}_status";
1166 :     # Compute the JavaScript call for updating the status.
1167 :     my $showSelect = "showSelected($menuName, '$divID', 1000);";
1168 :     # If multiple selection is supported, create an onChange event.
1169 :     my $onChange = "";
1170 : parrello 1.8 if ($cross) {
1171 :     $onChange = " onChange=\"crossUnSelect($menuName, '$divID', $cross, '${formName}_${cross}_status', 1000)\"";
1172 :     } elsif ($multiple) {
1173 : parrello 1.1 $onChange = " onChange=\"$showSelect\"";
1174 :     }
1175 :     # Create the SELECT tag and stuff it into the output array.
1176 : parrello 1.3 my $select = "<SELECT name=\"$menuName\"$onChange$multipleTag size=\"$rows\">";
1177 : parrello 1.1 my @lines = ($select);
1178 :     # Loop through the groups.
1179 :     for my $group (@groups) {
1180 :     # Create the option group tag.
1181 :     my $tag = "<OPTGROUP label=\"$group\">";
1182 :     push @lines, " $tag";
1183 :     # Compute the label for this group's options. This is seriously dirty stuff, as the
1184 :     # label option may have functionality in future browsers. If that happens, we'll need
1185 :     # to modify the genome text so that the "selectSome" method can tell which are NMPDR
1186 :     # organisms and which aren't. Sadly, the OPTGROUP tag is invisible in the DOM Javascript
1187 : parrello 1.3 # hierarchy, so we can't use it.
1188 : parrello 1.1 my $label = ($group eq $FIG_Config::otherGroup ? "other" : "nmpdr");
1189 :     # Get the genomes in the group.
1190 : parrello 1.3 for my $genome (@{$groupHash->{$group}}) {
1191 : parrello 1.1 my ($genomeID, $name) = @{$genome};
1192 :     # See if it's selected.
1193 :     my $select = ($selectedHash{$genomeID} ? " selected" : "");
1194 :     # Generate the option tag.
1195 :     my $optionTag = "<OPTION value=\"$genomeID\" label=\"$label\"$select>$name <em>($genomeID)</em></OPTION>";
1196 :     push @lines, " $optionTag";
1197 :     }
1198 :     # Close the option group.
1199 :     push @lines, " </OPTGROUP>";
1200 :     }
1201 :     # Close the SELECT tag.
1202 :     push @lines, "</SELECT>";
1203 :     # Check for multiple selection.
1204 : parrello 1.3 if ($multiple) {
1205 :     # Since multi-select is on, we set up some buttons to set and clear selections.
1206 : parrello 1.1 push @lines, "<br />";
1207 :     push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll($menuName); $showSelect\" />";
1208 :     push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\" value=\"Clear All\" onClick=\"clearAll($menuName); $showSelect\" />";
1209 :     push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\" value=\"Select NMPDR\" onClick=\"selectSome($menuName, 'nmpdr'); $showSelect\" />";
1210 :     push @lines, "<INPUT type=\"button\" name=\"OtherOnly\" class=\"bigButton\" value=\"Select Supporting\" onClick=\"selectSome($menuName, 'other'); $showSelect\" />";
1211 : parrello 1.3 # Now add the search box. This allows the user to type text and have all genomes containing
1212 :     # the text selected automatically.
1213 :     my $searchThingName = "${menuName}_SearchThing";
1214 :     push @lines, "<br>Select genomes containing <INPUT type=\"text\" name=\"$searchThingName\" size=\"30\" />&nbsp;" .
1215 :     "<INPUT type=\"button\" name=\"Select\" class=\"button\" value=\"Search\" onClick=\"selectViaSearch($menuName, $searchThingName); $showSelect\" />";
1216 : parrello 1.1 # Add the status display, too.
1217 :     push @lines, "<DIV id=\"$divID\" class=\"selectStatus\"></DIV>";
1218 :     # Queue to update the status display when the form loads. We need to modify the show statement
1219 :     # slightly because the queued statements are executed outside the form. This may seem like a lot of
1220 :     # trouble, but we want all of the show statement calls to be generated from a single line of code,
1221 :     # in case we decide to twiddle the parameters.
1222 :     $showSelect =~ s/showSelected\(/showSelected\(thisForm\./;
1223 :     $self->QueueFormScript($showSelect);
1224 : parrello 1.3 # Finally, add this parameter to the list of genome parameters. This enables us to
1225 :     # easily find all the parameters used to select one or more genomes.
1226 :     push @{$self->{genomeParms}}, $menuName;
1227 : parrello 1.1 }
1228 :     # Assemble all the lines into a string.
1229 :     my $retVal = join("\n", @lines, "");
1230 :     # Return the result.
1231 :     return $retVal;
1232 :     }
1233 :    
1234 : parrello 1.3 =head3 PropertyMenu
1235 :    
1236 :     C<< my $htmlText = $shelp->PropertyMenu($menuName, $selected, $force); >>
1237 :    
1238 :     Generate a property name dropdown menu.
1239 :    
1240 :     =over 4
1241 :    
1242 :     =item menuName
1243 :    
1244 :     Name to give to the menu.
1245 :    
1246 :     =item selected
1247 :    
1248 :     Value of the property name to pre-select.
1249 :    
1250 :     =item force (optional)
1251 :    
1252 :     If TRUE, then the user will be forced to choose a property name. If FALSE,
1253 :     then an additional menu choice will be provided to select nothing.
1254 :    
1255 :     =item RETURN
1256 :    
1257 :     Returns a dropdown menu box that allows the user to select a property name. An additional
1258 :     selection entry will be provided for selecting no property name
1259 :    
1260 :     =back
1261 :    
1262 :     =cut
1263 :    
1264 :     sub PropertyMenu {
1265 :     # Get the parameters.
1266 :     my ($self, $menuName, $selected, $force) = @_;
1267 :     # Get the CGI and Sprout objects.
1268 :     my $sprout = $self->DB();
1269 :     my $cgi = $self->Q();
1270 :     # Create the property name list.
1271 :     my @propNames = ();
1272 :     if (! $force) {
1273 :     push @propNames, "";
1274 :     }
1275 :     # Get all the property names, putting them after the null choice if one exists.
1276 :     push @propNames, $sprout->GetChoices('Property', 'property-name');
1277 :     # Create a menu from them.
1278 :     my $retVal = $cgi->popup_menu(-name=> $menuName, -values => \@propNames,
1279 :     -default => $selected);
1280 :     # Return the result.
1281 :     return $retVal;
1282 :     }
1283 :    
1284 : parrello 1.1 =head3 MakeTable
1285 :    
1286 :     C<< my $htmlText = $shelp->MakeTable(\@rows); >>
1287 :    
1288 :     Create a table from a group of table rows. The table rows must be fully pre-formatted: in
1289 :     other words, each must have the TR and TD tags included.
1290 :    
1291 :     The purpose of this method is to provide a uniform look for search form tables. It is
1292 :     almost impossible to control a table using styles, so rather than have a table style,
1293 :     we create the TABLE tag in this method. Note also that the first TD or TH in each row will
1294 :     be updated with an explicit width so the forms look pretty when they are all on one
1295 :     page.
1296 :    
1297 :     =over 4
1298 :    
1299 :     =item rows
1300 :    
1301 :     Reference to a list of table rows. Each table row must be in HTML form with all
1302 :     the TR and TD tags set up. The first TD or TH tag in each row will be modified to
1303 :     set the width. Everything else will be left as is.
1304 :    
1305 :     =item RETURN
1306 :    
1307 :     Returns the full HTML for a table in the approved NMPDR Search Form style.
1308 :    
1309 :     =back
1310 :    
1311 :     =cut
1312 :    
1313 :     sub MakeTable {
1314 :     # Get the parameters.
1315 :     my ($self, $rows) = @_;
1316 :     # Get the CGI object.
1317 :     my $cgi = $self->Q();
1318 :     # Fix the widths on the first column. Note that we eschew the use of the "g"
1319 :     # modifier becase we only want to change the first tag. Also, if a width
1320 :     # is already specified on the first column bad things will happen.
1321 :     for my $row (@{$rows}) {
1322 :     $row =~ s/(<td|th)/$1 width="150"/i;
1323 :     }
1324 :     # Create the table.
1325 :     my $retVal = $cgi->table({border => 2, cellspacing => 2,
1326 :     width => 700, class => 'search'},
1327 :     @{$rows});
1328 :     # Return the result.
1329 :     return $retVal;
1330 :     }
1331 :    
1332 :     =head3 SubmitRow
1333 :    
1334 :     C<< my $htmlText = $shelp->SubmitRow(); >>
1335 :    
1336 :     Returns the HTML text for the row containing the page size control
1337 :     and the submit button. All searches should have this row somewhere
1338 :     near the top of the form.
1339 :    
1340 :     =cut
1341 :    
1342 :     sub SubmitRow {
1343 :     # Get the parameters.
1344 :     my ($self) = @_;
1345 :     my $cgi = $self->Q();
1346 : parrello 1.3 # Get the current page size.
1347 :     my $pageSize = $cgi->param('PageSize');
1348 :     # Get the incoming external-link flag.
1349 :     my $aliases = ($cgi->param('ShowAliases') ? 1 : 0);
1350 :     # Create the row.
1351 : parrello 1.1 my $retVal = $cgi->Tr($cgi->td("Results/Page"),
1352 :     $cgi->td($cgi->popup_menu(-name => 'PageSize',
1353 : parrello 1.7 -values => [10, 25, 50, 100, 1000],
1354 : parrello 1.3 -default => $pageSize) . " " .
1355 :     $cgi->checkbox(-name => 'ShowURL',
1356 :     -value => 1,
1357 :     -label => 'Show URL')),
1358 : parrello 1.1 $cgi->td($cgi->submit(-class => 'goButton',
1359 :     -name => 'Search',
1360 :     -value => 'Go')));
1361 :     # Return the result.
1362 :     return $retVal;
1363 :     }
1364 : parrello 1.2
1365 :     =head3 FeatureFilterRows
1366 :    
1367 :     C<< my $htmlText = $shelp->FeatureFilterRows(); >>
1368 :    
1369 :     This method creates table rows that can be used to filter features. There are
1370 :     two rows returned, and the values can be used to select features by genome
1371 :     using the B<FeatureQuery> object.
1372 :    
1373 :     =cut
1374 :    
1375 :     sub FeatureFilterRows {
1376 :     # Get the parameters.
1377 :     my ($self) = @_;
1378 :     # Return the result.
1379 :     return FeatureQuery::FilterRows($self);
1380 :     }
1381 :    
1382 : parrello 1.1 =head3 GBrowseFeatureURL
1383 :    
1384 :     C<< my $url = SearchHelper::GBrowseFeatureURL($sprout, $feat); >>
1385 :    
1386 :     Compute the URL required to pull up a Gbrowse page for the the specified feature.
1387 :     In order to do this, we need to pull out the ID of the feature's Genome, its
1388 :     contig ID, and some rough starting and stopping offsets.
1389 :    
1390 :     =over 4
1391 :    
1392 :     =item sprout
1393 :    
1394 :     Sprout object for accessing the database.
1395 :    
1396 :     =item feat
1397 :    
1398 :     ID of the feature whose Gbrowse URL is desired.
1399 :    
1400 :     =item RETURN
1401 :    
1402 :     Returns a GET-style URL for the Gbrowse CGI, with parameters specifying the genome
1403 :     ID, contig ID, starting offset, and stopping offset.
1404 :    
1405 :     =back
1406 :    
1407 :     =cut
1408 :    
1409 :     sub GBrowseFeatureURL {
1410 :     # Get the parameters.
1411 :     my ($sprout, $feat) = @_;
1412 :     # Declare the return variable.
1413 :     my $retVal;
1414 :     # Compute the genome ID.
1415 :     my ($genomeID) = FIGRules::ParseFeatureID($feat);
1416 :     # Only proceed if the feature ID produces a valid genome.
1417 :     if ($genomeID) {
1418 :     # Get the feature location string.
1419 :     my $loc = $sprout->FeatureLocation($feat);
1420 :     # Compute the contig, start, and stop points.
1421 : parrello 1.6 my($contig, $start, $stop) = BasicLocation::Parse($loc);
1422 : parrello 1.5 Trace("Start and stop are ($start,$stop) on contig $contig.") if T(3);
1423 : parrello 1.1 # Now we need to do some goofiness to insure that the location is not too
1424 :     # big and that we get some surrounding stuff.
1425 :     my $mid = int(($start + $stop) / 2);
1426 :     my $chunk_len = 20000;
1427 :     my $max_feature = 40000;
1428 :     my $feat_len = abs($stop - $start);
1429 :     if ($feat_len > $chunk_len) {
1430 :     if ($feat_len > $max_feature) {
1431 :     $chunk_len = $max_feature;
1432 :     } else {
1433 :     $chunk_len = $feat_len + 100;
1434 :     }
1435 :     }
1436 :     my($show_start, $show_stop);
1437 :     if ($chunk_len == $max_feature) {
1438 :     $show_start = $start - 300;
1439 :     } else {
1440 :     $show_start = $mid - int($chunk_len / 2);
1441 :     }
1442 :     if ($show_start < 1) {
1443 :     $show_start = 1;
1444 :     }
1445 :     $show_stop = $show_start + $chunk_len - 1;
1446 :     my $clen = $sprout->ContigLength($contig);
1447 :     if ($show_stop > $clen) {
1448 :     $show_stop = $clen;
1449 :     }
1450 :     my $seg_id = $contig;
1451 :     $seg_id =~ s/:/--/g;
1452 : parrello 1.5 Trace("Show limits are ($show_start,$show_stop) in genome $genomeID with ref $seg_id.") if T(3);
1453 : parrello 1.1 # Assemble all the pieces.
1454 :     $retVal = "gbrowse.cgi/GB_$genomeID?ref=$seg_id&start=$show_start&stop=$show_stop";
1455 :     }
1456 :     # Return the result.
1457 :     return $retVal;
1458 :     }
1459 :    
1460 : parrello 1.3 =head3 GetGenomes
1461 :    
1462 :     C<< my @genomeList = $shelp->GetGenomes($parmName); >>
1463 :    
1464 :     Return the list of genomes specified by the specified CGI query parameter.
1465 :     If the request method is POST, then the list of genome IDs is returned
1466 :     without preamble. If the request method is GET and the parameter is not
1467 :     specified, then it is treated as a request for all genomes. This makes it
1468 :     easier for web pages to link to a search that wants to specify all genomes.
1469 :    
1470 :     =over 4
1471 :    
1472 :     =item parmName
1473 :    
1474 :     Name of the parameter containing the list of genomes. This will be the
1475 :     first parameter passed to the L</NmpdrGenomeMenu> call that created the
1476 :     genome selection control on the form.
1477 :    
1478 :     =item RETURN
1479 :    
1480 :     Returns a list of the genomes to process.
1481 :    
1482 :     =back
1483 :    
1484 :     =cut
1485 :    
1486 :     sub GetGenomes {
1487 :     # Get the parameters.
1488 :     my ($self, $parmName) = @_;
1489 :     # Get the CGI query object.
1490 :     my $cgi = $self->Q();
1491 :     # Get the list of genome IDs in the request header.
1492 :     my @retVal = $cgi->param($parmName);
1493 :     Trace("Genome list for $parmName is (" . join(", ", @retVal) . ") with method " . $cgi->request_method() . ".") if T(3);
1494 :     # Check for the special GET case.
1495 :     if ($cgi->request_method() eq "GET" && ! @retVal) {
1496 :     # Here the caller wants all the genomes.
1497 :     my $sprout = $self->DB();
1498 :     @retVal = $sprout->Genomes();
1499 :     }
1500 :     # Return the result.
1501 :     return @retVal;
1502 :     }
1503 :    
1504 :     =head3 GetHelpText
1505 :    
1506 :     C<< my $htmlText = $shelp->GetHelpText(); >>
1507 :    
1508 :     Get the help text for this search. The help text is stored in files on the template
1509 :     server. The help text for a specific search is taken from a file named
1510 :     C<SearchHelp_>I<class>C<.inc> in the template directory C<$FIG_Config::template_url>.
1511 :     There are also three standard help files: C<SearchHelp1_Filtering.inc> describes the
1512 :     feature filtering performed by the B<FeatureQuery> object, C<SearchHelp1_GenomeControl.inc>
1513 :     describes how to use a multiple-selection genome control, and C<SearchHelp1_Standard.inc>
1514 :     describes the standard controls for a search, such as page size, URL display, and
1515 :     external alias display.
1516 :    
1517 :     =cut
1518 :    
1519 :     sub GetHelpText {
1520 :     # Get the parameters.
1521 :     my ($self) = @_;
1522 :     # Create a list to hold the pieces of the help.
1523 :     my @helps = ();
1524 :     # Get the template directory URL.
1525 :     my $urlBase = $FIG_Config::template_url;
1526 :     # Start with the specific help.
1527 :     my $class = $self->{class};
1528 :     push @helps, PageBuilder::GetPage("$urlBase/SearchHelp_$class.inc");
1529 :     # Add the genome control help if needed.
1530 :     if (scalar @{$self->{genomeParms}}) {
1531 :     push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_GenomeControl.inc");
1532 :     }
1533 :     # Next the filter help.
1534 :     if ($self->{filtered}) {
1535 :     push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_Filtering.inc");
1536 :     }
1537 :     # Finally, the standard help.
1538 :     push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_Standard.inc");
1539 :     # Assemble the pieces.
1540 :     my $retVal = join("\n<p>&nbsp;</p>\n", @helps);
1541 :     # Return the result.
1542 :     return $retVal;
1543 :     }
1544 :    
1545 :     =head3 ComputeSearchURL
1546 :    
1547 :     C<< my $url = $shelp->ComputeSearchURL(); >>
1548 :    
1549 :     Compute the GET-style URL for the current search. In order for this to work, there
1550 :     must be a copy of the search form on the current page. This will always be the
1551 :     case if the search is coming from C<SearchSkeleton.cgi>.
1552 :    
1553 :     A little expense is involved in order to make the URL as smart as possible. The
1554 :     main complication is that if the user specified all genomes, we'll want to
1555 :     remove the parameter entirely from a get-style URL.
1556 :    
1557 :     =cut
1558 :    
1559 :     sub ComputeSearchURL {
1560 :     # Get the parameters.
1561 :     my ($self) = @_;
1562 :     # Get the database and CGI query object.
1563 :     my $cgi = $self->Q();
1564 :     my $sprout = $self->DB();
1565 :     # Start with the full URL.
1566 :     my $retVal = $cgi->url(-full => 1);
1567 :     # Get all the query parameters in a hash.
1568 :     my %parms = $cgi->Vars();
1569 :     # Now we need to do some fixing. Each multi-valued parameter is encoded as a string with null
1570 :     # characters separating the individual values. We have to convert those to lists. In addition,
1571 :     # the multiple-selection genome parameters and the feature type parameter must be checked to
1572 :     # determine whether or not they can be removed from the URL. First, we get a list of the
1573 :     # genome parameters and a list of all genomes. Note that we only need the list if a
1574 :     # multiple-selection genome parameter has been found on the form.
1575 :     my %genomeParms = map { $_ => 1 } @{$self->{genomeParms}};
1576 :     my @genomeList;
1577 :     if (keys %genomeParms) {
1578 :     @genomeList = $sprout->Genomes();
1579 :     }
1580 :     # Create a list to hold the URL parameters we find.
1581 :     my @urlList = ();
1582 :     # Now loop through the parameters in the hash, putting them into the output URL.
1583 :     for my $parmKey (keys %parms) {
1584 :     # Get a list of the parameter values. If there's only one, we'll end up with
1585 :     # a singleton list, but that's okay.
1586 :     my @values = split (/\0/, $parms{$parmKey});
1587 :     # Check for special cases.
1588 :     if ($parmKey eq 'featureTypes') {
1589 :     # Here we need to see if the user wants all the feature types. If he
1590 :     # does, we erase all the values so that the parameter is not output.
1591 :     my %valueCheck = map { $_ => 1 } @values;
1592 :     my @list = FeatureQuery::AllFeatureTypes();
1593 :     my $okFlag = 1;
1594 :     for (my $i = 0; $okFlag && $i <= $#list; $i++) {
1595 :     if (! $valueCheck{$list[$i]}) {
1596 :     $okFlag = 0;
1597 :     }
1598 :     }
1599 :     if ($okFlag) {
1600 :     @values = ();
1601 :     }
1602 :     } elsif (grep { $_ eq $parmKey } qw(SessionID ResultCount Page PageSize Trace TF ShowURL)) {
1603 :     # These are bookkeeping parameters we don't need to start a search.
1604 :     @values = ();
1605 :     } elsif ($parmKey =~ /_SearchThing$/) {
1606 :     # Here the value coming in is from a genome control's search thing. It does
1607 :     # not affect the results of the search, so we clear it.
1608 :     @values = ();
1609 :     } elsif ($genomeParms{$parmKey}) {
1610 :     # Here we need to see if the user wants all the genomes. If he does,
1611 :     # we erase all the values just like with features.
1612 :     my $allFlag = $sprout->IsAllGenomes(\@values, \@genomeList);
1613 :     if ($allFlag) {
1614 :     @values = ();
1615 :     }
1616 :     }
1617 :     # If we still have values, create the URL parameters.
1618 :     if (@values) {
1619 :     push @urlList, map { "$parmKey=" . uri_escape($_) } @values;
1620 :     }
1621 :     }
1622 :     # Add the parameters to the URL.
1623 :     $retVal .= "?" . join(";", @urlList);
1624 :     # Return the result.
1625 :     return $retVal;
1626 :     }
1627 :    
1628 :     =head3 GetRunTimeValue
1629 :    
1630 :     C<< my $htmlText = $shelp->GetRunTimeValue($text); >>
1631 :    
1632 :     Compute a run-time column value.
1633 :    
1634 :     =over 4
1635 :    
1636 :     =item text
1637 :    
1638 :     The run-time column text. It consists of 2 percent signs, a column type, an equal
1639 :     sign, and the data for the current row.
1640 :    
1641 :     =item RETURN
1642 :    
1643 :     Returns the fully-formatted HTML text to go into the current column of the current row.
1644 :    
1645 :     =back
1646 :    
1647 :     =cut
1648 :    
1649 :     sub GetRunTimeValue {
1650 :     # Get the parameters.
1651 :     my ($self, $text) = @_;
1652 :     # Declare the return variable.
1653 :     my $retVal;
1654 :     # Parse the incoming text.
1655 :     if ($text =~ /^%%([^=]+)=(.*)$/) {
1656 :     $retVal = $self->RunTimeColumns($1, $2);
1657 :     } else {
1658 :     Confess("Invalid run-time column string \"$text\" encountered in session file.");
1659 :     }
1660 :     # Return the result.
1661 :     return $retVal;
1662 :     }
1663 :    
1664 : parrello 1.9 =head3 FeatureTypeMap
1665 :    
1666 :     C<< my %features = SearchHelper::FeatureTypeMap(); >>
1667 :    
1668 :     Return a map of feature types to descriptions. The feature type data is stored
1669 :     in the B<FIG_Config> file. Currently, it only contains a space-delimited list of
1670 :     feature types. The map returned by this method is a hash mapping the type codes to
1671 :     descriptive names.
1672 :    
1673 :     The reason we have to convert the list from a string is that the B<NMPDRSetup.pl>
1674 :     script is only able to insert strings into the generated B<FIG_Config> file.
1675 :    
1676 :     =cut
1677 :    
1678 :     sub FeatureTypeMap {
1679 :     my @list = split /\s+/, $FIG_Config::feature_types;
1680 :     my %retVal = map { $_ => $_ } @list;
1681 :     return %retVal;
1682 :     }
1683 :    
1684 :     =head3 AdvancedClassList
1685 :    
1686 :     C<< my @classes = SearchHelper::AdvancedClassList(); >>
1687 :    
1688 :     Return a list of advanced class names. This list is used to generate the directory
1689 :     of available searches on the search page.
1690 :    
1691 :     The reason we have to convert the list from a string is that the B<NMPDRSetup.pl>
1692 :     script is only able to insert strings into the generated B<FIG_Config> file.
1693 :    
1694 :     =cut
1695 :    
1696 :     sub AdvancedClassList {
1697 :     return split /\s+/, $FIG_Config::advanced_classes;
1698 :     }
1699 :    
1700 : parrello 1.1 =head2 Feature Column Methods
1701 :    
1702 :     The methods in this column manage feature column data. If you want to provide the
1703 :     capability to include new types of data in feature columns, then all the changes
1704 :     are made to this section of the source file. Technically, this should be implemented
1705 :     using object-oriented methods, but this is simpler for non-programmers to maintain.
1706 :     To add a new column of feature data, you must first give it a name. For example,
1707 :     the name for the protein page link column is C<protlink>. If the column is to appear
1708 :     in the default list of feature columns, add it to the list returned by
1709 :     L</DefaultFeatureColumns>. Then add code to produce the column title to
1710 :     L</FeatureColumnTitle> and code to produce its value to L</FeatureColumnValue>, and
1711 :     everything else will happen automatically.
1712 :    
1713 :     There is one special column name syntax for extra columns (that is, nonstandard
1714 :     feature columns). If the column name begins with C<X=>, then it is presumed to be
1715 :     an extra column. The column title is the text after the C<X=>, and its value is
1716 :     pulled from the extra column hash.
1717 :    
1718 :     =head3 DefaultFeatureColumns
1719 :    
1720 :     C<< my $colNames = $shelp->DefaultFeatureColumns(); >>
1721 :    
1722 :     Return a reference to a list of the default feature column identifiers. These
1723 :     identifiers can be passed to L</FeatureColumnTitle> and L</FeatureColumnValue> in
1724 :     order to produce the column titles and row values.
1725 :    
1726 :     =cut
1727 :    
1728 :     sub DefaultFeatureColumns {
1729 :     # Get the parameters.
1730 :     my ($self) = @_;
1731 :     # Return the result.
1732 : parrello 1.4 return ['orgName', 'function', 'gblink', 'protlink',
1733 :     FeatureQuery::AdditionalColumns($self)];
1734 : parrello 1.1 }
1735 :    
1736 :     =head3 FeatureColumnTitle
1737 :    
1738 :     C<< my $title = $shelp->FeatureColumnTitle($colName); >>
1739 :    
1740 :     Return the column heading title to be used for the specified feature column.
1741 :    
1742 :     =over 4
1743 :    
1744 :     =item name
1745 :    
1746 :     Name of the desired feature column.
1747 :    
1748 :     =item RETURN
1749 :    
1750 :     Returns the title to be used as the column header for the named feature column.
1751 :    
1752 :     =back
1753 :    
1754 :     =cut
1755 :    
1756 :     sub FeatureColumnTitle {
1757 :     # Get the parameters.
1758 :     my ($self, $colName) = @_;
1759 :     # Declare the return variable. We default to a blank column name.
1760 :     my $retVal = "&nbsp;";
1761 :     # Process the column name.
1762 :     if ($colName =~ /^X=(.+)$/) {
1763 :     # Here we have an extra column.
1764 :     $retVal = $1;
1765 :     } elsif ($colName eq 'orgName') {
1766 :     $retVal = "Name";
1767 :     } elsif ($colName eq 'fid') {
1768 :     $retVal = "FIG ID";
1769 :     } elsif ($colName eq 'alias') {
1770 :     $retVal = "External Aliases";
1771 :     } elsif ($colName eq 'function') {
1772 :     $retVal = "Functional Assignment";
1773 :     } elsif ($colName eq 'gblink') {
1774 :     $retVal = "GBrowse";
1775 :     } elsif ($colName eq 'protlink') {
1776 :     $retVal = "NMPDR Protein Page";
1777 :     } elsif ($colName eq 'group') {
1778 :     $retVal = "NMDPR Group";
1779 :     }
1780 :     # Return the result.
1781 :     return $retVal;
1782 :     }
1783 :    
1784 :     =head3 FeatureColumnValue
1785 :    
1786 :     C<< my $value = $shelp->FeatureColumnValue($colName, $fid, \%extraCols); >>
1787 :    
1788 :     Return the value to be displayed in the specified feature column.
1789 :    
1790 :     =over 4
1791 :    
1792 :     =item colName
1793 :    
1794 :     Name of the column to be displayed.
1795 :    
1796 :     =item record
1797 :    
1798 :     DBObject record for the feature being displayed in the current row.
1799 :    
1800 :     =item extraCols
1801 :    
1802 :     Reference to a hash of extra column names to values. If the incoming column name
1803 :     begins with C<X=>, its value will be taken from this hash.
1804 :    
1805 :     =item RETURN
1806 :    
1807 :     Returns the HTML to be displayed in the named column for the specified feature.
1808 :    
1809 :     =back
1810 :    
1811 :     =cut
1812 :    
1813 :     sub FeatureColumnValue {
1814 :     # Get the parameters.
1815 :     my ($self, $colName, $record, $extraCols) = @_;
1816 :     # Get the sprout and CGI objects.
1817 :     my $cgi = $self->Q();
1818 :     my $sprout = $self->DB();
1819 :     # Get the feature ID.
1820 :     my ($fid) = $record->Value('Feature(id)');
1821 :     # Declare the return variable. Denote that we default to a non-breaking space,
1822 :     # which will translate to an empty table cell (rather than a table cell with no
1823 :     # interior, which is what you get for a null string).
1824 :     my $retVal = "&nbsp;";
1825 :     # Process according to the column name.
1826 :     if ($colName =~ /^X=(.+)$/) {
1827 :     # Here we have an extra column. Only update if the value exists. Note that
1828 :     # a value of C<undef> is treated as a non-existent value, because the
1829 :     # caller may have put "colName => undef" in the "PutFeature" call in order
1830 :     # to insure we know the extra column exists.
1831 :     if (defined $extraCols->{$1}) {
1832 :     $retVal = $extraCols->{$1};
1833 :     }
1834 :     } elsif ($colName eq 'orgName') {
1835 :     # Here we want the formatted organism name and feature number.
1836 :     $retVal = $self->FeatureName($fid);
1837 :     } elsif ($colName eq 'fid') {
1838 :     # Here we have the raw feature ID. We hyperlink it to the protein page.
1839 :     $retVal = HTML::set_prot_links($fid);
1840 :     } elsif ($colName eq 'alias') {
1841 :     # In this case, the user wants a list of external aliases for the feature.
1842 : parrello 1.3 # These are very expensive, so we compute them when the row is displayed.
1843 :     $retVal = "%%aliases=$fid";
1844 : parrello 1.1 } elsif ($colName eq 'function') {
1845 :     # The functional assignment is just a matter of getting some text.
1846 :     ($retVal) = $record->Value('Feature(assignment)');
1847 :     } elsif ($colName eq 'gblink') {
1848 :     # Here we want a link to the GBrowse page using the official GBrowse button.
1849 :     my $gurl = "GetGBrowse.cgi?fid=$fid";
1850 :     $retVal = $cgi->a({ href => $gurl, title => "GBrowse for $fid" },
1851 :     $cgi->img({ src => "../images/button-gbrowse.png",
1852 :     border => 0 })
1853 :     );
1854 :     } elsif ($colName eq 'protlink') {
1855 :     # Here we want a link to the protein page using the official NMPDR button.
1856 :     my $hurl = HTML::fid_link($cgi, $fid, 0, 1);
1857 :     $retVal = $cgi->a({ href => $hurl, title => "Protein page for $fid" },
1858 :     $cgi->img({ src => "../images/button-nmpdr.png",
1859 :     border => 0 })
1860 :     );
1861 :     } elsif ($colName eq 'group') {
1862 :     # Get the NMPDR group name.
1863 :     my (undef, $group) = $self->OrganismData($fid);
1864 :     # Dress it with a URL to the group's main page.
1865 :     my $nurl = $sprout->GroupPageName($group);
1866 :     $retVal = $cgi->a({ href => $nurl, title => "$group summary" },
1867 :     $group);
1868 :     }
1869 :     # Return the result.
1870 :     return $retVal;
1871 :     }
1872 :    
1873 : parrello 1.3 =head3 RunTimeColumns
1874 :    
1875 :     C<< my $htmlText = $shelp->RunTimeColumns($type, $text); >>
1876 :    
1877 :     Return the HTML text for a run-time column. Run-time columns are evaluated when the
1878 :     list is displayed, rather than when it is generated.
1879 :    
1880 :     =over 4
1881 :    
1882 :     =item type
1883 :    
1884 :     Type of column.
1885 :    
1886 :     =item text
1887 :    
1888 :     Data relevant to this row of the column.
1889 :    
1890 :     =item RETURN
1891 :    
1892 :     Returns the fully-formatted HTML text to go in the specified column.
1893 :    
1894 :     =back
1895 :    
1896 :     =cut
1897 :    
1898 :     sub RunTimeColumns {
1899 :     # Get the parameters.
1900 :     my ($self, $type, $text) = @_;
1901 :     # Declare the return variable.
1902 :     my $retVal = "";
1903 :     # Get the Sprout and CGI objects.
1904 :     my $sprout = $self->DB();
1905 :     my $cgi = $self->Q();
1906 :     # Separate the text into a type and data.
1907 :     if ($type eq 'aliases') {
1908 :     # Here the caller wants external alias links for a feature. The text
1909 :     # is the feature ID.
1910 :     my $fid = $text;
1911 :     # The complicated part is we have to hyperlink them. First, get the
1912 :     # aliases.
1913 :     Trace("Generating aliases for feature $fid.") if T(4);
1914 :     my @aliases = $sprout->FeatureAliases($fid);
1915 :     # Only proceed if we found some.
1916 :     if (@aliases) {
1917 :     # Join the aliases into a comma-delimited list.
1918 :     my $aliasList = join(", ", @aliases);
1919 :     # Ask the HTML processor to hyperlink them.
1920 :     $retVal = HTML::set_prot_links($cgi, $aliasList);
1921 :     }
1922 :     }
1923 :     # Return the result.
1924 :     return $retVal;
1925 :     }
1926 :    
1927 : parrello 1.4 =head2 Virtual Methods
1928 :    
1929 :     =head3 Form
1930 :    
1931 :     C<< my $html = $shelp->Form(); >>
1932 :    
1933 :     Generate the HTML for a form to request a new search.
1934 :    
1935 :     =head3 Find
1936 :    
1937 :     C<< my $resultCount = $shelp->Find(); >>
1938 :    
1939 :     Conduct a search based on the current CGI query parameters. The search results will
1940 :     be written to the session cache file and the number of results will be
1941 :     returned. If the search parameters are invalid, a result count of C<undef> will be
1942 :     returned and a result message will be stored in this object describing the problem.
1943 :    
1944 :     =head3 Description
1945 :    
1946 :     C<< my $htmlText = $shelp->Description(); >>
1947 :    
1948 :     Return a description of this search. The description is used for the table of contents
1949 :     on the main search tools page. It may contain HTML, but it should be character-level,
1950 :     not block-level, since the description is going to appear in a list.
1951 :    
1952 :     =head3 SortKey
1953 :    
1954 :     C<< my $key = $shelp->SortKey($record); >>
1955 :    
1956 :     Return the sort key for the specified record. The default is to sort by feature name,
1957 :     floating NMPDR organisms to the top. This sort may be overridden by the search class
1958 :     to provide fancier functionality. This method is called by B<PutFeature>, so it
1959 :     is only used for feature searches. A non-feature search would presumably have its
1960 :     own sort logic.
1961 :    
1962 :     =over 4
1963 :    
1964 :     =item record
1965 :    
1966 :     The C<DBObject> from which the current row of data is derived.
1967 :    
1968 :     =item RETURN
1969 :    
1970 :     Returns a key field that can be used to sort this row in among the results.
1971 :    
1972 :     =back
1973 :    
1974 :     =cut
1975 :    
1976 :     sub SortKey {
1977 :     # Get the parameters.
1978 :     my ($self, $record) = @_;
1979 :     # Get the feature ID from the record.
1980 :     my ($fid) = $record->Value('Feature(id)');
1981 :     # Get the group from the feature ID.
1982 :     my $group = $self->FeatureGroup($fid);
1983 :     # Ask the feature query object to form the sort key.
1984 :     my $retVal = FeatureQuery::SortKey($self, $group, $record);
1985 :     # Return the result.
1986 :     return $retVal;
1987 :     }
1988 : parrello 1.9
1989 :    
1990 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3