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

Annotation of /Sprout/SearchHelper.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3