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

Annotation of /Sprout/SearchHelper.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.32 - (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.27 =item extraPos
92 :    
93 : parrello 1.30 Hash indicating which extra columns should be put at the end. Extra columns
94 :     not mentioned in this hash are put at the beginning. Use the L</SetExtraPos>
95 :     method to change this option.
96 : parrello 1.27
97 : parrello 1.1 =back
98 :    
99 : parrello 1.2 =head2 Adding a new Search Tool
100 :    
101 :     To add a new search tool to the system, you must
102 :    
103 :     =over 4
104 :    
105 :     =item 1
106 :    
107 :     Choose a class name for your search tool.
108 :    
109 :     =item 2
110 :    
111 :     Create a new subclass of this object and implement each of the virtual methods. The
112 :     name of the subclass must be C<SH>I<className>.
113 :    
114 :     =item 3
115 :    
116 :     Create an include file among the web server pages that describes how to use
117 :     the search tool. The include file must be in the B<includes> directory, and
118 :     its name must be C<SearchHelp_>I<className>C<.inc>.
119 :    
120 :     =item 4
121 :    
122 : parrello 1.15 In the C<SearchSkeleton.cgi> script and add a C<use> statement for your search tool.
123 : parrello 1.2
124 :     =back
125 :    
126 :     =head3 Building a Search Form
127 :    
128 :     All search forms are three-column tables. In general, you want one form
129 :     variable per table row. The first column should contain the label and
130 :     the second should contain the form control for specifying the variable
131 :     value. If the control is wide, you should use C<colspan="2"> to give it
132 :     extra room. B<Do not> specify a width in any of your table cells, as
133 :     width management is handled by this class.
134 :    
135 :     The general code for creating the form should be
136 :    
137 :     sub Form {
138 :     my ($self) = @_;
139 :     # Get the CGI object.
140 :     my $cgi = @self->Q();
141 :     # Start the form.
142 :     my $retVal = $self->FormStart("form title");
143 :     # Assemble the table rows.
144 :     my @rows = ();
145 :     ... push table row Html into @rows ...
146 :     push @rows, $self->SubmitRow();
147 :     ... push more Html into @rows ...
148 :     # Build the table from the rows.
149 :     $retVal .= $self->MakeTable(\@rows);
150 :     # Close the form.
151 :     $retVal .= $self->FormEnd();
152 :     # Return the form Html.
153 :     return $retVal;
154 :     }
155 :    
156 :     Several helper methods are provided for particular purposes.
157 :    
158 :     =over 4
159 :    
160 :     =item 1
161 :    
162 : parrello 1.3 L</NmpdrGenomeMenu> generates a control for selecting one or more genomes. Use
163 :     L</GetGenomes> to retrieve all the genomes passed in for a specified parameter
164 :     name. Note that as an assist to people working with GET-style links, if no
165 :     genomes are specified and the incoming request style is GET, all genomes will
166 :     be returned.
167 : parrello 1.2
168 :     =item 2
169 :    
170 :     L</FeatureFilterRow> formats several rows of controls for filtering features.
171 :     When you start building the code for the L</Find> method, you can use a
172 :     B<FeatureQuery> object to automatically filter each genome's features using
173 :     the values from the filter controls.
174 :    
175 :     =item 3
176 :    
177 :     L</QueueFormScript> allows you to queue JavaScript statements for execution
178 :     after the form is fully generated. If you are using very complicated
179 :     form controls, the L</QueueFormScript> method allows you to perform
180 :     JavaScript initialization. The L</NmpdrGenomeMenu> control uses this
181 :     facility to display a list of the pre-selected genomes.
182 :    
183 :     =back
184 :    
185 : parrello 1.14 If you are doing a feature search, you can also change the list of feature
186 :     columns displayed and their display order by overriding
187 :     L</DefaultFeatureColumns>.
188 :    
189 : parrello 1.2 Finally, when generating the code for your controls, be sure to use any incoming
190 :     query parameters as default values so that the search request is persistent.
191 :    
192 :     =head3 Finding Search Results
193 :    
194 :     The L</Find> method is used to create the search results. For a search that
195 :     wants to return features (which is most of them), the basic code structure
196 :     would work as follows. It is assumed that the L</FeatureFilterRows> method
197 :     has been used to create feature filtering parameters.
198 :    
199 :     sub Find {
200 :     my ($self) = @_;
201 :     # Get the CGI and Sprout objects.
202 :     my $cgi = $self->Q();
203 :     my $sprout = $self->DB();
204 :     # Declare the return variable. If it remains undefined, the caller will
205 :     # know that an error occurred.
206 :     my $retVal;
207 :     ... validate the parameters ...
208 :     if (... invalid parameters...) {
209 :     $self->SetMessage(...appropriate message...);
210 :     } elsif (FeatureQuery::Valid($self)) {
211 :     # Initialize the session file.
212 :     $self->OpenSession();
213 :     # Initialize the result counter.
214 :     $retVal = 0;
215 :     ... get a list of genomes ...
216 :     for my $genomeID (... each genome ...) {
217 :     my $fq = FeatureQuery->new($self, $genomeID);
218 :     while (my $feature = $fq->Fetch()) {
219 :     ... examine the feature ...
220 :     if (... we want to keep it ...) {
221 :     $self->PutFeature($fq);
222 :     $retVal++;
223 :     }
224 :     }
225 :     }
226 : parrello 1.9 # Close the session file.
227 :     $self->CloseSession();
228 : parrello 1.2 }
229 :     # Return the result count.
230 :     return $retVal;
231 :     }
232 :    
233 :     A Find method is of course much more complicated than generating a form, and there
234 : parrello 1.14 are variations on the above theme. For example, you could eschew feature filtering
235 : parrello 1.2 entirely in favor of your own custom filtering, you could include extra columns
236 :     in the output, or you could search for something that's not a feature at all. The
237 :     above code is just a loose framework.
238 :    
239 : parrello 1.28 In addition to the finding and filtering, it is necessary to send status messages
240 :     to the output so that the user does not get bored waiting for results. The L</PrintLine>
241 :     method performs this function. The single parameter should be text to be
242 :     output to the browser. In general, you'll invoke it as follows.
243 :    
244 :     $self->PrintLine("...my message text...<br />");
245 :    
246 :     The break tag is optional. When the Find method gets control, a paragraph will
247 :     have been started so that everything is XHTML-compliant.
248 :    
249 : parrello 1.2 If you wish to add your own extra columns to the output, use the B<AddExtraColumns>
250 :     method of the feature query object.
251 :    
252 :     $fq->AddExtraColumns(score => $sc);
253 :    
254 :     The L</Find> method must return C<undef> if the search parameters are invalid. If this
255 :     is the case, then a message describing the problem should be passed to the framework
256 :     by calling L</SetMessage>. If the parameters are valid, then the method must return
257 :     the number of items found.
258 :    
259 : parrello 1.1 =cut
260 :    
261 :     # This counter is used to insure every form on the page has a unique name.
262 :     my $formCount = 0;
263 : parrello 1.16 # This counter is used to generate unique DIV IDs.
264 :     my $divCount = 0;
265 : parrello 1.1
266 :     =head2 Public Methods
267 :    
268 :     =head3 new
269 :    
270 : parrello 1.28 C<< my $shelp = SearchHelper->new($cgi); >>
271 : parrello 1.1
272 :     Construct a new SearchHelper object.
273 :    
274 :     =over 4
275 :    
276 : parrello 1.19 =item cgi
277 : parrello 1.1
278 :     The CGI query object for the current script.
279 :    
280 :     =back
281 :    
282 :     =cut
283 :    
284 :     sub new {
285 :     # Get the parameters.
286 : parrello 1.19 my ($class, $cgi) = @_;
287 : parrello 1.1 # Check for a session ID.
288 : parrello 1.19 my $session_id = $cgi->param("SessionID");
289 : parrello 1.1 my $type = "old";
290 :     if (! $session_id) {
291 : parrello 1.26 Trace("No session ID found.") if T(3);
292 : parrello 1.1 # Here we're starting a new session. We create the session ID and
293 :     # store it in the query object.
294 :     $session_id = NewSessionID();
295 :     $type = "new";
296 : parrello 1.19 $cgi->param(-name => 'SessionID', -value => $session_id);
297 : parrello 1.26 } else {
298 :     Trace("Session ID is $session_id.") if T(3);
299 : parrello 1.1 }
300 :     # Compute the subclass name.
301 : parrello 1.19 my $subClass;
302 :     if ($class =~ /SH(.+)$/) {
303 :     # Here we have a real search class.
304 :     $subClass = $1;
305 :     } else {
306 :     # Here we have a bare class. The bare class cannot search, but it can
307 :     # process search results.
308 :     $subClass = 'SearchHelper';
309 :     }
310 : parrello 1.1 # Insure everybody knows we're in Sprout mode.
311 : parrello 1.19 $cgi->param(-name => 'SPROUT', -value => 1);
312 : parrello 1.1 # Generate the form name.
313 :     my $formName = "$class$formCount";
314 :     $formCount++;
315 :     # Create the shelp object. It contains the query object (with the session ID)
316 :     # as well as an indicator as to whether or not the session is new, plus the
317 : parrello 1.2 # class name and a placeholder for the Sprout object.
318 : parrello 1.1 my $retVal = {
319 : parrello 1.19 query => $cgi,
320 : parrello 1.1 type => $type,
321 :     class => $subClass,
322 : parrello 1.2 sprout => undef,
323 : parrello 1.1 orgs => {},
324 :     name => $formName,
325 :     scriptQueue => [],
326 : parrello 1.3 genomeList => undef,
327 :     genomeParms => [],
328 :     filtered => 0,
329 : parrello 1.30 extraPos => {},
330 : parrello 1.1 };
331 :     # Bless and return it.
332 :     bless $retVal, $class;
333 :     return $retVal;
334 :     }
335 :    
336 :     =head3 Q
337 :    
338 :     C<< my $query = $shelp->Q(); >>
339 :    
340 :     Return the CGI query object.
341 :    
342 :     =cut
343 :    
344 :     sub Q {
345 :     # Get the parameters.
346 :     my ($self) = @_;
347 :     # Return the result.
348 :     return $self->{query};
349 :     }
350 :    
351 : parrello 1.9
352 :    
353 : parrello 1.1 =head3 DB
354 :    
355 :     C<< my $sprout = $shelp->DB(); >>
356 :    
357 :     Return the Sprout database object.
358 :    
359 :     =cut
360 :    
361 :     sub DB {
362 :     # Get the parameters.
363 :     my ($self) = @_;
364 : parrello 1.2 # Insure we have a database.
365 :     my $retVal = $self->{sprout};
366 :     if (! defined $retVal) {
367 :     $retVal = SFXlate->new_sprout_only();
368 :     $self->{sprout} = $retVal;
369 :     }
370 : parrello 1.1 # Return the result.
371 : parrello 1.2 return $retVal;
372 : parrello 1.1 }
373 :    
374 :     =head3 IsNew
375 :    
376 :     C<< my $flag = $shelp->IsNew(); >>
377 :    
378 :     Return TRUE if this is a new session, FALSE if this is an old session. An old
379 :     session already has search results ready to process.
380 :    
381 :     =cut
382 :    
383 :     sub IsNew {
384 :     # Get the parameters.
385 :     my ($self) = @_;
386 :     # Return the result.
387 :     return ($self->{type} eq 'new');
388 :     }
389 :    
390 : parrello 1.27 =head3 SetExtraPos
391 :    
392 : parrello 1.30 C<< $shelp->SetExtraPos(@columnMap); >>
393 : parrello 1.27
394 :     Indicate whether the extra columns should be in the front (C<0>) or end (C<1>) columns of the results.
395 :    
396 :     =over 4
397 :    
398 : parrello 1.30 =item columnMap
399 : parrello 1.27
400 : parrello 1.30 A list of extra columns to display at the end.
401 : parrello 1.27
402 :     =back
403 :    
404 :     =cut
405 :    
406 :     sub SetExtraPos {
407 : parrello 1.30 # Get the parameters.
408 :     my ($self, @columnMap) = @_;
409 :     # Convert the column map to a hash.
410 :     my %map = map { $_ => 1 } @columnMap;
411 :     # Save a reference to it.
412 :     $self->{extraPos} = \%map;
413 : parrello 1.27 }
414 :    
415 : parrello 1.1 =head3 ID
416 :    
417 :     C<< my $sessionID = $shelp->ID(); >>
418 :    
419 :     Return the current session ID.
420 :    
421 :     =cut
422 :    
423 :     sub ID {
424 :     # Get the parameters.
425 :     my ($self) = @_;
426 :     # Return the result.
427 :     return $self->Q()->param("SessionID");
428 :     }
429 :    
430 :     =head3 FormName
431 :    
432 :     C<< my $name = $shelp->FormName(); >>
433 :    
434 :     Return the name of the form this helper object will generate.
435 :    
436 :     =cut
437 :    
438 :     sub FormName {
439 :     # Get the parameters.
440 :     my ($self) = @_;
441 :     # Return the result.
442 :     return $self->{name};
443 :     }
444 :    
445 :     =head3 QueueFormScript
446 :    
447 :     C<< $shelp->QueueFormScript($statement); >>
448 :    
449 :     Add the specified statement to the queue of JavaScript statements that are to be
450 :     executed when the form has been fully defined. This is necessary because until
451 :     the closing </FORM> tag is emitted, the form elements cannot be referenced by
452 :     name. When generating the statement, you can refer to the variable C<thisForm>
453 :     in order to reference the form in progress. Thus,
454 :    
455 :     thisForm.simLimit.value = 1e-10;
456 :    
457 :     would set the value of the form element C<simLimit> in the current form to
458 :     C<1e-10>.
459 :    
460 :     =over 4
461 :    
462 :     =item statement
463 :    
464 :     JavaScript statement to be queued for execution after the form is built.
465 :     The trailing semi-colon is required. Theoretically, you could include
466 :     multiple statements separated by semi-colons, but one at a time works
467 :     just as well.
468 :    
469 :     =back
470 :    
471 :     =cut
472 :    
473 :     sub QueueFormScript {
474 :     # Get the parameters.
475 :     my ($self, $statement) = @_;
476 :     # Push the statement onto the script queue.
477 :     push @{$self->{scriptQueue}}, $statement;
478 :     }
479 :    
480 :     =head3 FormStart
481 :    
482 :     C<< my $html = $shelp->FormStart($title); >>
483 :    
484 :     Return the initial section of a form designed to perform another search of the
485 :     same type. The form header is included along with hidden fields to persist the
486 :     tracing, sprout status, and search class.
487 :    
488 :     A call to L</FormEnd> is required to close the form.
489 :    
490 :     =over 4
491 :    
492 :     =item title
493 :    
494 :     Title to be used for the form.
495 :    
496 :     =item RETURN
497 :    
498 :     Returns the initial HTML for the search form.
499 :    
500 :     =back
501 :    
502 :     =cut
503 :    
504 :     sub FormStart {
505 :     # Get the parameters.
506 :     my ($self, $title) = @_;
507 :     # Get the CGI object.
508 :     my $cgi = $self->Q();
509 : parrello 1.18 # Start the form. Note we use the override option on the Class value, in
510 :     # case the Advanced button was used.
511 : parrello 1.1 my $retVal = "<div class=\"search\">\n" .
512 :     $cgi->start_form(-method => 'POST',
513 :     -action => $cgi->url(-relative => 1),
514 :     -name => $self->FormName()) .
515 :     $cgi->hidden(-name => 'Class',
516 : parrello 1.18 -value => $self->{class},
517 :     -override => 1) .
518 : parrello 1.1 $cgi->hidden(-name => 'SPROUT',
519 :     -value => 1) .
520 :     $cgi->h3($title);
521 :     # If tracing is on, add it to the form.
522 :     if ($cgi->param('Trace')) {
523 :     $retVal .= $cgi->hidden(-name => 'Trace',
524 :     -value => $cgi->param('Trace')) .
525 :     $cgi->hidden(-name => 'TF',
526 :     -value => ($cgi->param('TF') ? 1 : 0));
527 :     }
528 :     # Put in an anchor tag in case there's a table of contents.
529 :     my $anchorName = $self->FormName();
530 :     $retVal .= "<a name=\"$anchorName\"></a>\n";
531 :     # Return the result.
532 :     return $retVal;
533 :     }
534 :    
535 :     =head3 FormEnd
536 :    
537 :     C<< my $htmlText = $shelp->FormEnd(); >>
538 :    
539 :     Return the HTML text for closing a search form. This closes both the C<form> and
540 :     C<div> tags.
541 :    
542 :     =cut
543 :    
544 :     sub FormEnd {
545 :     # Get the parameters.
546 :     my ($self) = @_;
547 :     # Declare the return variable, closing the form and the DIV block.
548 :     my $retVal = "</form></div>\n";
549 :     # Now we flush out the statement queue.
550 :     my @statements = @{$self->{scriptQueue}};
551 :     if (@statements > 0) {
552 :     # Switch to JavaScript and set the "thisForm" variable.
553 :     $retVal .= "<SCRIPT language=\"JavaScript\">\n" .
554 :     " thisForm = document.$self->{name};\n";
555 :     # Unroll the statements.
556 :     while (@statements > 0) {
557 :     my $statement = shift @statements;
558 :     $retVal .= " $statement\n";
559 :     }
560 :     # Close the JavaScript.
561 :     $retVal .= "</SCRIPT>\n";
562 :     }
563 :     # Return the result.
564 :     return $retVal;
565 :     }
566 :    
567 :     =head3 SetMessage
568 :    
569 :     C<< $shelp->SetMessage($msg); >>
570 :    
571 :     Store the specified text as the result message. The result message is displayed
572 :     if an invalid parameter value is specified.
573 :    
574 :     =over 4
575 :    
576 :     =item msg
577 :    
578 :     Text of the result message to be displayed.
579 :    
580 :     =back
581 :    
582 :     =cut
583 :    
584 :     sub SetMessage {
585 :     # Get the parameters.
586 :     my ($self, $msg) = @_;
587 :     # Store the message.
588 :     $self->{message} = $msg;
589 :     }
590 :    
591 :     =head3 Message
592 :    
593 :     C<< my $text = $shelp->Message(); >>
594 :    
595 :     Return the result message. The result message is displayed if an invalid parameter
596 :     value is specified.
597 :    
598 :     =cut
599 :    
600 :     sub Message {
601 :     # Get the parameters.
602 :     my ($self) = @_;
603 :     # Return the result.
604 :     return $self->{message};
605 :     }
606 :    
607 :     =head3 OpenSession
608 :    
609 :     C<< $shelp->OpenSession(); >>
610 :    
611 :     Set up to open the session cache file for writing. Note we don't actually
612 :     open the file until after we know the column headers.
613 :    
614 :     =cut
615 :    
616 :     sub OpenSession {
617 :     # Get the parameters.
618 :     my ($self) = @_;
619 :     # Denote we have not yet written out the column headers.
620 :     $self->{cols} = undef;
621 :     }
622 :    
623 :     =head3 GetCacheFileName
624 :    
625 :     C<< my $fileName = $shelp->GetCacheFileName(); >>
626 :    
627 :     Return the name to be used for this session's cache file.
628 :    
629 :     =cut
630 :    
631 :     sub GetCacheFileName {
632 :     # Get the parameters.
633 :     my ($self) = @_;
634 :     # Return the result.
635 :     return $self->GetTempFileName('cache');
636 :     }
637 :    
638 :     =head3 GetTempFileName
639 :    
640 :     C<< my $fileName = $shelp->GetTempFileName($type); >>
641 :    
642 :     Return the name to be used for a temporary file of the specified type. The
643 :     name is computed from the session name with the type as a suffix.
644 :    
645 :     =over 4
646 :    
647 :     =item type
648 :    
649 :     Type of temporary file to be generated.
650 :    
651 :     =item RETURN
652 :    
653 :     Returns a file name generated from the session name and the specified type.
654 :    
655 :     =back
656 :    
657 :     =cut
658 :    
659 :     sub GetTempFileName {
660 :     # Get the parameters.
661 :     my ($self, $type) = @_;
662 :     # Compute the file name. Note it gets stuffed in the FIG temporary
663 :     # directory.
664 :     my $retVal = "$FIG_Config::temp/tmp_" . $self->ID() . ".$type";
665 :     # Return the result.
666 :     return $retVal;
667 :     }
668 :    
669 :     =head3 PutFeature
670 :    
671 : parrello 1.11 C<< $shelp->PutFeature($fdata); >>
672 : parrello 1.1
673 :     Store a feature in the result cache. This is the workhorse method for most
674 :     searches, since the primary data item in the database is features.
675 :    
676 :     For each feature, there are certain columns that are standard: the feature name, the
677 :     GBrowse and protein page links, the functional assignment, and so forth. If additional
678 : parrello 1.2 columns are required by a particular search subclass, they should be stored in
679 :     the feature query object using the B<AddExtraColumns> method. For example, the following
680 :     code adds columns for essentiality and virulence.
681 : parrello 1.1
682 : parrello 1.11 $fd->AddExtraColumns(essential => $essentialFlag, virulence => $vfactor);
683 :     $shelp->PutFeature($fd);
684 : parrello 1.1
685 :     For correct results, all values should be specified for all extra columns in all calls to
686 :     B<PutFeature>. (In particular, the column header names are computed on the first
687 :     call.) If a column is to be blank for the current feature, its value can be given
688 :     as C<undef>.
689 :    
690 :     if (! $essentialFlag) {
691 :     $essentialFlag = undef;
692 :     }
693 : parrello 1.11 $fd->AddExtraColumns(essential => $essentialFlag, virulence => $vfactor);
694 :     $shelp->PutFeature($fd);
695 : parrello 1.1
696 :     =over 4
697 :    
698 : parrello 1.11 =item fdata
699 : parrello 1.1
700 : parrello 1.11 B<FeatureData> object containing the current feature data.
701 : parrello 1.1
702 :     =back
703 :    
704 :     =cut
705 :    
706 :     sub PutFeature {
707 : parrello 1.2 # Get the parameters.
708 : parrello 1.11 my ($self, $fd) = @_;
709 : parrello 1.3 # Get the CGI query object.
710 :     my $cgi = $self->Q();
711 : parrello 1.2 # Get the feature data.
712 : parrello 1.11 my $record = $fd->Feature();
713 :     my $extraCols = $fd->ExtraCols();
714 : parrello 1.1 # Check for a first-call situation.
715 :     if (! defined $self->{cols}) {
716 : parrello 1.14 Trace("Setting up the columns.") if T(3);
717 : parrello 1.28 # Tell the user what's happening.
718 :     $self->PrintLine("Creating output columns.<br />");
719 : parrello 1.27 # Here we need to set up the column information. First we accumulate the extras,
720 : parrello 1.30 # sorted by column name and separate by whether they go in the beginning or the
721 :     # end.
722 :     my @xtraNamesFront = ();
723 :     my @xtraNamesEnd = ();
724 :     my $xtraPosMap = $self->{extraPos};
725 : parrello 1.27 for my $col (sort keys %{$extraCols}) {
726 : parrello 1.30 if ($xtraPosMap->{$col}) {
727 :     push @xtraNamesEnd, "X=$col";
728 :     } else {
729 :     push @xtraNamesFront, "X=$col";
730 :     }
731 : parrello 1.27 }
732 :     # Set up the column name array.
733 : parrello 1.14 my @colNames = ();
734 : parrello 1.30 # Put in the extra columns that go in the beginning.
735 :     push @colNames, @xtraNamesFront;
736 : parrello 1.14 # Add the default columns.
737 :     push @colNames, $self->DefaultFeatureColumns();
738 :     # Add any additional columns requested by the feature filter.
739 :     push @colNames, FeatureQuery::AdditionalColumns($self);
740 : parrello 1.27 # If extras go at the end, put them in here.
741 : parrello 1.30 push @colNames, @xtraNamesEnd;
742 : parrello 1.23 Trace("Full column list determined.") if T(3);
743 : parrello 1.14 # Save the full list.
744 :     $self->{cols} = \@colNames;
745 : parrello 1.28 # Write out the column names. This also prepares the cache file to receive
746 : parrello 1.1 # output.
747 : parrello 1.23 Trace("Writing column headers.") if T(3);
748 : parrello 1.28 $self->WriteColumnHeaders(@{$self->{cols}});
749 : parrello 1.23 Trace("Column headers written.") if T(3);
750 : parrello 1.1 }
751 :     # Get the feature ID.
752 : parrello 1.11 my $fid = $fd->FID();
753 : parrello 1.29 # Loop through the column headers, producing the desired data. The first column
754 :     # is the feature ID. The feature ID does not show up in the output: its purpose
755 :     # is to help the various output formatters.
756 :     my @output = ($fid);
757 : parrello 1.1 for my $colName (@{$self->{cols}}) {
758 : parrello 1.2 push @output, $self->FeatureColumnValue($colName, $record, $extraCols);
759 : parrello 1.1 }
760 : parrello 1.9 # Compute the sort key. The sort key usually floats NMPDR organism features to the
761 : parrello 1.1 # top of the return list.
762 : parrello 1.11 my $key = $self->SortKey($fd);
763 : parrello 1.1 # Write the feature data.
764 :     $self->WriteColumnData($key, @output);
765 :     }
766 :    
767 :     =head3 WriteColumnHeaders
768 :    
769 :     C<< $shelp->WriteColumnHeaders(@colNames); >>
770 :    
771 :     Write out the column headers for the current search session. The column headers
772 :     are sent to the cache file, and then the cache is re-opened as a sort pipe and
773 :     the handle saved.
774 :    
775 :     =over 4
776 :    
777 :     =item colNames
778 :    
779 :     A list of column names in the desired presentation order.
780 :    
781 :     =back
782 :    
783 :     =cut
784 :    
785 :     sub WriteColumnHeaders {
786 :     # Get the parameters.
787 :     my ($self, @colNames) = @_;
788 :     # Get the cache file name and open it for output.
789 :     my $fileName = $self->GetCacheFileName();
790 :     my $handle1 = Open(undef, ">$fileName");
791 :     # Write the column headers and close the file.
792 :     Tracer::PutLine($handle1, \@colNames);
793 :     close $handle1;
794 : parrello 1.32 Trace("Column headers are: " . join("; ", @colNames) . ".") if T(3);
795 : parrello 1.1 # Now open the sort pipe and save the file handle. Note how we append the
796 :     # sorted data to the column header row already in place. The output will
797 :     # contain a sort key followed by the real columns. The sort key is
798 :     # hacked off before going to the output file.
799 :     $self->{fileHandle} = Open(undef, "| sort | cut --fields=2- >>$fileName");
800 :     }
801 :    
802 :     =head3 WriteColumnData
803 :    
804 :     C<< $shelp->WriteColumnData($key, @colValues); >>
805 :    
806 :     Write a row of column values to the current search session. It is assumed that
807 :     the session file is already open for output.
808 :    
809 :     =over 4
810 :    
811 :     =item key
812 :    
813 :     Sort key.
814 :    
815 :     =item colValues
816 :    
817 :     List of column values to write to the search result cache file for this session.
818 :    
819 :     =back
820 :    
821 :     =cut
822 :    
823 :     sub WriteColumnData {
824 :     # Get the parameters.
825 :     my ($self, $key, @colValues) = @_;
826 :     # Write them to the cache file.
827 :     Tracer::PutLine($self->{fileHandle}, [$key, @colValues]);
828 : parrello 1.32 Trace("Column data is " . join("; ", $key, @colValues) . ".") if T(4);
829 : parrello 1.1 }
830 :    
831 :     =head3 CloseSession
832 :    
833 :     C<< $shelp->CloseSession(); >>
834 :    
835 :     Close the session file.
836 :    
837 :     =cut
838 :    
839 :     sub CloseSession {
840 :     # Get the parameters.
841 :     my ($self) = @_;
842 :     # Check for an open session file.
843 :     if (defined $self->{fileHandle}) {
844 :     # We found one, so close it.
845 : parrello 1.9 Trace("Closing session file.") if T(2);
846 : parrello 1.1 close $self->{fileHandle};
847 : parrello 1.28 # Tell the user.
848 :     my $cgi = $self->Q();
849 :     $self->PrintLine("Output formatting complete.<br />");
850 : parrello 1.1 }
851 :     }
852 :    
853 :     =head3 NewSessionID
854 :    
855 :     C<< my $id = SearchHelpers::NewSessionID(); >>
856 :    
857 :     Generate a new session ID for the current user.
858 :    
859 :     =cut
860 :    
861 :     sub NewSessionID {
862 :     # Declare the return variable.
863 :     my $retVal;
864 :     # Get a digest encoder.
865 :     my $md5 = Digest::MD5->new();
866 : parrello 1.4 # Add the PID, the IP, and the time stamp. Note that the time stamp is
867 :     # actually two numbers, and we get them both because we're in list
868 :     # context.
869 :     $md5->add($$, $ENV{REMOTE_ADDR}, $ENV{REMOTE_PORT}, gettimeofday());
870 :     # Hash up all this identifying data.
871 :     $retVal = $md5->hexdigest();
872 :     # Return the result.
873 : parrello 1.1 return $retVal;
874 :     }
875 :    
876 :     =head3 OrganismData
877 :    
878 :     C<< my ($orgName, $group) = $shelp->Organism($genomeID); >>
879 :    
880 :     Return the name and status of the organism corresponding to the specified genome ID.
881 :     For performance reasons, this information is cached in a special hash table, so we
882 :     only compute it once per run.
883 :    
884 :     =over 4
885 :    
886 :     =item genomeID
887 :    
888 :     ID of the genome whose name is desired.
889 :    
890 :     =item RETURN
891 :    
892 : parrello 1.32 Returns a list of three items. The first item in the list is the organism name,
893 : parrello 1.1 and the second is the name of the NMPDR group, or an empty string if the
894 : parrello 1.32 organism is not in an NMPDR group. The third item is the organism's domain.
895 : parrello 1.1
896 :     =back
897 :    
898 :     =cut
899 :    
900 :     sub OrganismData {
901 :     # Get the parameters.
902 :     my ($self, $genomeID) = @_;
903 :     # Declare the return variables.
904 : parrello 1.32 my ($orgName, $group, $domain);
905 : parrello 1.1 # Check the cache.
906 :     my $cache = $self->{orgs};
907 :     if (exists $cache->{$genomeID}) {
908 : parrello 1.32 ($orgName, $group, $domain) = @{$cache->{$genomeID}};
909 : parrello 1.1 } else {
910 :     # Here we have to use the database.
911 :     my $sprout = $self->DB();
912 : parrello 1.32 my ($genus, $species, $strain, $group, $taxonomy) = $sprout->GetEntityValues('Genome', $genomeID,
913 :     ['Genome(genus)', 'Genome(species)',
914 :     'Genome(unique-characterization)',
915 :     'Genome(primary-group)',
916 :     'Genome(taxonomy)']);
917 : parrello 1.10 # Format and cache the name and display group.
918 : parrello 1.32 ($orgName, $group, $domain) = $self->SaveOrganismData($group, $genomeID, $genus, $species,
919 :     $strain, $taxonomy);
920 : parrello 1.1 }
921 :     # Return the result.
922 : parrello 1.32 return ($orgName, $group, $domain);
923 : parrello 1.1 }
924 :    
925 :     =head3 Organism
926 :    
927 :     C<< my $orgName = $shelp->Organism($genomeID); >>
928 :    
929 :     Return the name of the relevant organism. The name is computed from the genus,
930 :     species, and unique characterization. A cache is used to improve performance.
931 :    
932 :     =over 4
933 :    
934 :     =item genomeID
935 :    
936 :     ID of the genome whose name is desired.
937 :    
938 :     =item RETURN
939 :    
940 :     Returns the display name of the specified organism.
941 :    
942 :     =back
943 :    
944 :     =cut
945 :    
946 :     sub Organism {
947 :     # Get the parameters.
948 :     my ($self, $genomeID) = @_;
949 :     # Get the organism data.
950 : parrello 1.32 my ($retVal) = $self->OrganismData($genomeID);
951 : parrello 1.1 # Return the result.
952 :     return $retVal;
953 :     }
954 :    
955 :     =head3 FeatureGroup
956 :    
957 :     C<< my $groupName = $shelp->FeatureGroup($fid); >>
958 :    
959 :     Return the group name for the specified feature.
960 :    
961 :     =over 4
962 :    
963 :     =item fid
964 :    
965 :     ID of the relevant feature.
966 :    
967 :     =item RETURN
968 :    
969 :     Returns the name of the NMPDR group to which the feature belongs, or an empty
970 :     string if it is not part of an NMPDR group.
971 :    
972 :     =back
973 :    
974 :     =cut
975 :    
976 :     sub FeatureGroup {
977 :     # Get the parameters.
978 :     my ($self, $fid) = @_;
979 :     # Parse the feature ID to get the genome ID.
980 :     my ($genomeID) = FIGRules::ParseFeatureID($fid);
981 :     # Get the organism data.
982 :     my (undef, $retVal) = $self->OrganismData($genomeID);
983 :     # Return the result.
984 :     return $retVal;
985 :     }
986 :    
987 :     =head3 FeatureName
988 :    
989 :     C<< my $fidName = $shelp->FeatureName($fid); >>
990 :    
991 :     Return the display name of the specified feature.
992 :    
993 :     =over 4
994 :    
995 :     =item fid
996 :    
997 :     ID of the feature whose name is desired.
998 :    
999 :     =item RETURN
1000 :    
1001 :     A displayable feature name, consisting of the organism name plus some feature
1002 :     type and location information.
1003 :    
1004 :     =back
1005 :    
1006 :     =cut
1007 :    
1008 :     sub FeatureName {
1009 :     # Get the parameters.
1010 :     my ($self, $fid) = @_;
1011 :     # Declare the return variable
1012 :     my $retVal;
1013 :     # Parse the feature ID.
1014 :     my ($genomeID, $type, $num) = FIGRules::ParseFeatureID($fid);
1015 :     if (! defined $genomeID) {
1016 :     # Here the feature ID has an invalid format.
1017 :     $retVal = "External: $fid";
1018 :     } else {
1019 :     # Here we can get its genome data.
1020 :     $retVal = $self->Organism($genomeID);
1021 : parrello 1.4 # Append the FIG ID.
1022 :     $retVal .= " [$fid]";
1023 : parrello 1.1 }
1024 :     # Return the result.
1025 :     return $retVal;
1026 :     }
1027 :    
1028 :     =head3 ComputeFASTA
1029 :    
1030 : parrello 1.32 C<< my $fasta = $shelp->ComputeFASTA($desiredType, $sequence, $flankingWidth); >>
1031 : parrello 1.1
1032 : parrello 1.32 Parse a sequence input and convert it into a FASTA string of the desired type with
1033 :     the desired flanking width.
1034 : parrello 1.1
1035 :     =over 4
1036 :    
1037 :     =item desiredType
1038 :    
1039 : parrello 1.18 C<dna> to return a DNA sequence, C<prot> to return a protein sequence.
1040 : parrello 1.1
1041 :     =item sequence
1042 :    
1043 :     Sequence to return. It may be a DNA or protein sequence in FASTA form or a feature ID.
1044 :     If a feature ID is specified, the feature's DNA or translation will be returned. The
1045 :     feature ID is recognized by the presence of a vertical bar in the input. Otherwise,
1046 :     if the input does not begin with a greater-than sign (FASTA label line), a default label
1047 :     line will be provided.
1048 :    
1049 : parrello 1.32 =item flankingWidth
1050 :    
1051 :     If the DNA FASTA of a feature is desired, the number of base pairs to either side of the
1052 :     feature that should be included. Currently we can't do this for Proteins because the
1053 :     protein translation of a feature doesn't always match the DNA and is taken directly
1054 :     from the database.
1055 :    
1056 : parrello 1.1 =item RETURN
1057 :    
1058 :     Returns a string in FASTA format representing the content of the desired sequence with
1059 :     an appropriate label. If the input is invalid, a message will be stored and we will
1060 :     return C<undef>. Note that the output will include a trailing new-line.
1061 :    
1062 :     =back
1063 :    
1064 :     =cut
1065 :    
1066 :     sub ComputeFASTA {
1067 :     # Get the parameters.
1068 : parrello 1.32 my ($self, $desiredType, $sequence, $flankingWidth) = @_;
1069 : parrello 1.1 # Declare the return variable. If an error occurs, it will remain undefined.
1070 :     my $retVal;
1071 : parrello 1.11 # This variable will be cleared if an error is detected.
1072 :     my $okFlag = 1;
1073 : parrello 1.1 # Create variables to hold the FASTA label and data.
1074 :     my ($fastaLabel, $fastaData);
1075 : parrello 1.18 Trace("FASTA desired type is $desiredType.") if T(4);
1076 : parrello 1.29 # Check for a feature specification. The smoking gun for that is a vertical bar.
1077 : parrello 1.1 if ($sequence =~ /^\s*(\w+\|\S+)\s*$/) {
1078 :     # Here we have a feature ID in $1. We'll need the Sprout object to process
1079 :     # it.
1080 :     my $fid = $1;
1081 : parrello 1.11 Trace("Feature ID for fasta is $fid.") if T(3);
1082 : parrello 1.1 my $sprout = $self->DB();
1083 :     # Get the FIG ID. Note that we only use the first feature found. We are not
1084 :     # supposed to have redundant aliases, though we may have an ID that doesn't
1085 :     # exist.
1086 :     my ($figID) = $sprout->FeaturesByAlias($fid);
1087 :     if (! $figID) {
1088 : parrello 1.17 $self->SetMessage("No gene found with the ID \"$fid\".");
1089 : parrello 1.11 $okFlag = 0;
1090 : parrello 1.1 } else {
1091 : parrello 1.29 # Set the FASTA label. The ID is the first favored alias.
1092 :     my $favored = $self->Q()->param('FavoredAlias') || 'fig';
1093 :     my $favorLen = length $favored;
1094 :     ($fastaLabel) = grep { substr($_, 0, $favorLen) eq $favored } $sprout->FeatureAliases($fid);
1095 :     if (! $fastaLabel) {
1096 :     # In an emergency, fall back to the original ID.
1097 :     $fastaLabel = $fid;
1098 :     }
1099 : parrello 1.1 # Now proceed according to the sequence type.
1100 : parrello 1.11 if ($desiredType eq 'prot') {
1101 : parrello 1.1 # We want protein, so get the translation.
1102 :     $fastaData = $sprout->FeatureTranslation($figID);
1103 : parrello 1.11 Trace(length $fastaData . " characters returned for translation of $fastaLabel.") if T(3);
1104 : parrello 1.1 } else {
1105 : parrello 1.32 # We want DNA, so get the DNA sequence. This is a two-step process. First, we get the
1106 :     # locations.
1107 : parrello 1.1 my @locList = $sprout->FeatureLocation($figID);
1108 : parrello 1.32 if ($flankingWidth > 0) {
1109 :     # Here we need to add flanking data. Convert the locations to a list
1110 :     # of location objects.
1111 :     my @locObjects = map { BasicLocation->new($_) } @locList;
1112 :     # Initialize the return variable. We will put the DNA in here segment by segment.
1113 :     $fastaData = "";
1114 :     # Now we widen each location by the flanking width and stash the results. This
1115 :     # requires getting the contig length for each contig so we don't fall off the end.
1116 :     for my $locObject (@locObjects) {
1117 :     Trace("Current location is " . $locObject->String . ".") if T(4);
1118 :     # Remember the current start and length.
1119 :     my ($start, $len) = ($locObject->Left, $locObject->Length);
1120 :     # Get the contig length.
1121 :     my $contigLen = $sprout->ContigLength($locObject->Contig);
1122 :     # Widen the location and get its DNA.
1123 :     $locObject->Widen($flankingWidth, $contigLen);
1124 :     my $fastaSegment = $sprout->DNASeq([$locObject->String()]);
1125 :     # Now we need to do some case changing. The main DNA is upper case and
1126 :     # the flanking DNA is lower case.
1127 :     my $leftFlank = $start - $locObject->Left;
1128 :     my $rightFlank = $leftFlank + $len;
1129 :     Trace("Wide location is " . $locObject->String . ". Flanks are $leftFlank and $rightFlank. Contig len is $contigLen.") if T(4);
1130 :     my $fancyFastaSegment = lc(substr($fastaSegment, 0, $leftFlank)) .
1131 :     uc(substr($fastaSegment, $leftFlank, $rightFlank - $leftFlank)) .
1132 :     lc(substr($fastaSegment, $rightFlank));
1133 :     $fastaData .= $fancyFastaSegment;
1134 :     }
1135 :     } else {
1136 :     # Here we have just the raw sequence.
1137 :     $fastaData = $sprout->DNASeq(\@locList);
1138 :     }
1139 :     Trace((length $fastaData) . " characters returned for DNA of $fastaLabel.") if T(3);
1140 : parrello 1.1 }
1141 :     }
1142 :     } else {
1143 : parrello 1.11 Trace("Analyzing FASTA sequence.") if T(4);
1144 : parrello 1.1 # Here we are expecting a FASTA. We need to see if there's a label.
1145 : parrello 1.11 if ($sequence =~ /^>[\n\s]*(\S[^\n]*)\n(.+)$/s) {
1146 :     Trace("Label \"$1\" found in match to sequence:\n$sequence") if T(4);
1147 : parrello 1.1 # Here we have a label, so we split it from the data.
1148 :     $fastaLabel = $1;
1149 :     $fastaData = $2;
1150 :     } else {
1151 : parrello 1.11 Trace("No label found in match to sequence:\n$sequence") if T(4);
1152 : parrello 1.1 # Here we have no label, so we create one and use the entire sequence
1153 :     # as data.
1154 : parrello 1.18 $fastaLabel = "User-specified $desiredType sequence";
1155 : parrello 1.1 $fastaData = $sequence;
1156 :     }
1157 :     # The next step is to clean the junk out of the sequence.
1158 :     $fastaData =~ s/\n//g;
1159 :     $fastaData =~ s/\s+//g;
1160 : parrello 1.18 # Finally, verify that it's DNA if we're doing DNA stuff.
1161 : parrello 1.24 if ($desiredType eq 'dna' && $fastaData =~ /[^agctxn]/i) {
1162 :     $self->SetMessage("Invalid characters detected. Is the input really a DNA sequence?");
1163 : parrello 1.11 $okFlag = 0;
1164 : parrello 1.1 }
1165 :     }
1166 : parrello 1.11 Trace("FASTA data sequence: $fastaData") if T(4);
1167 :     # Only proceed if no error was detected.
1168 :     if ($okFlag) {
1169 : parrello 1.1 # We need to format the sequence into 60-byte chunks. We use the infamous
1170 :     # grep-split trick. The split, because of the presence of the parentheses,
1171 :     # includes the matched delimiters in the output list. The grep strips out
1172 :     # the empty list items that appear between the so-called delimiters, since
1173 :     # the delimiters are what we want.
1174 :     my @chunks = grep { $_ } split /(.{1,60})/, $fastaData;
1175 : parrello 1.11 $retVal = join("\n", ">$fastaLabel", @chunks, "");
1176 : parrello 1.1 }
1177 :     # Return the result.
1178 :     return $retVal;
1179 :     }
1180 :    
1181 : parrello 1.16 =head3 SubsystemTree
1182 :    
1183 :     C<< my $tree = SearchHelper::SubsystemTree($sprout, %options); >>
1184 :    
1185 :     This method creates a subsystem selection tree suitable for passing to
1186 :     L</SelectionTree>. Each leaf node in the tree will have a link to the
1187 :     subsystem display page. In addition, each node can have a radio button. The
1188 :     radio button alue is either C<classification=>I<string>, where I<string> is
1189 :     a classification string, or C<id=>I<string>, where I<string> is a subsystem ID.
1190 :     Thus, it can either be used to filter by a group of related subsystems or a
1191 :     single subsystem.
1192 :    
1193 :     =over 4
1194 :    
1195 :     =item sprout
1196 :    
1197 :     Sprout database object used to get the list of subsystems.
1198 :    
1199 :     =item options
1200 :    
1201 :     Hash containing options for building the tree.
1202 :    
1203 :     =item RETURN
1204 :    
1205 :     Returns a reference to a tree list suitable for passing to L</SelectionTree>.
1206 :    
1207 :     =back
1208 :    
1209 :     The supported options are as follows.
1210 :    
1211 :     =over 4
1212 :    
1213 :     =item radio
1214 :    
1215 :     TRUE if the tree should be configured for radio buttons. The default is FALSE.
1216 :    
1217 :     =item links
1218 :    
1219 :     TRUE if the tree should be configured for links. The default is TRUE.
1220 :    
1221 :     =back
1222 :    
1223 :     =cut
1224 :    
1225 :     sub SubsystemTree {
1226 :     # Get the parameters.
1227 :     my ($sprout, %options) = @_;
1228 :     # Process the options.
1229 :     my $optionThing = Tracer::GetOptions({ radio => 0, links => 1 }, \%options);
1230 :     # Read in the subsystems.
1231 :     my @subs = $sprout->GetAll(['Subsystem'], "ORDER BY Subsystem(classification), Subsystem(id)", [],
1232 :     ['Subsystem(classification)', 'Subsystem(id)']);
1233 : parrello 1.26 # Put any unclassified subsystems at the end. They will always be at the beginning, so if one
1234 :     # is at the end, ALL subsystems are unclassified and we don't bother.
1235 :     if ($#subs >= 0 && $subs[$#subs]->[0] ne '') {
1236 :     while ($subs[0]->[0] eq '') {
1237 :     my $classLess = shift @subs;
1238 :     push @subs, $classLess;
1239 :     }
1240 :     }
1241 : parrello 1.16 # Declare the return variable.
1242 :     my @retVal = ();
1243 :     # Each element in @subs represents a leaf node, so as we loop through it we will be
1244 :     # producing one leaf node at a time. The leaf node is represented as a 2-tuple. The
1245 :     # first element is a semi-colon-delimited list of the classifications for the
1246 :     # subsystem. There will be a stack of currently-active classifications, which we will
1247 :     # compare to the incoming classifications from the end backward. A new classification
1248 :     # requires starting a new branch. A different classification requires closing an old
1249 :     # branch and starting a new one. Each classification in the stack will also contain
1250 :     # that classification's current branch. We'll add a fake classification at the
1251 :     # beginning that we can use to represent the tree as a whole.
1252 :     my $rootName = '<root>';
1253 :     # Create the classification stack. Note the stack is a pair of parallel lists,
1254 :     # one containing names and the other containing content.
1255 :     my @stackNames = ($rootName);
1256 :     my @stackContents = (\@retVal);
1257 :     # Add a null entry at the end of the subsystem list to force an unrolling.
1258 : parrello 1.27 push @subs, ['', undef];
1259 : parrello 1.16 # Loop through the subsystems.
1260 :     for my $sub (@subs) {
1261 :     # Pull out the classification list and the subsystem ID.
1262 :     my ($classString, $id) = @{$sub};
1263 :     Trace("Processing class \"$classString\" and subsystem $id.") if T(4);
1264 :     # Convert the classification string to a list with the root classification in
1265 :     # the front.
1266 :     my @classList = ($rootName, split($FIG_Config::splitter, $classString));
1267 :     # Find the leftmost point at which the class list differs from the stack.
1268 :     my $matchPoint = 0;
1269 :     while ($matchPoint <= $#stackNames && $matchPoint <= $#classList &&
1270 :     $stackNames[$matchPoint] eq $classList[$matchPoint]) {
1271 :     $matchPoint++;
1272 :     }
1273 :     Trace("Match point is $matchPoint. Stack length is " . scalar(@stackNames) .
1274 :     ". Class List length is " . scalar(@classList) . ".") if T(4);
1275 :     # Unroll the stack to the matchpoint.
1276 :     while ($#stackNames >= $matchPoint) {
1277 :     my $popped = pop @stackNames;
1278 :     pop @stackContents;
1279 :     Trace("\"$popped\" popped from stack.") if T(4);
1280 :     }
1281 :     # Start branches for any new classifications.
1282 :     while ($#stackNames < $#classList) {
1283 :     # The branch for a new classification contains its radio button
1284 :     # data and then a list of children. So, at this point, if radio buttons
1285 :     # are desired, we put them into the content.
1286 :     my $newLevel = scalar(@stackNames);
1287 :     my @newClassContent = ();
1288 :     if ($optionThing->{radio}) {
1289 :     my $newClassString = join($FIG_Config::splitter, @classList[1..$newLevel]);
1290 :     push @newClassContent, { value => "classification=$newClassString%" };
1291 :     }
1292 :     # The new classification node is appended to its parent's content
1293 :     # and then pushed onto the stack. First, we need the node name.
1294 :     my $nodeName = $classList[$newLevel];
1295 :     # Add the classification to its parent. This makes it part of the
1296 :     # tree we'll be returning to the user.
1297 :     push @{$stackContents[$#stackNames]}, $nodeName, \@newClassContent;
1298 :     # Push the classification onto the stack.
1299 :     push @stackContents, \@newClassContent;
1300 :     push @stackNames, $nodeName;
1301 :     Trace("\"$nodeName\" pushed onto stack.") if T(4);
1302 :     }
1303 :     # Now the stack contains all our parent branches. We add the subsystem to
1304 :     # the branch at the top of the stack, but only if it's NOT the dummy node.
1305 :     if (defined $id) {
1306 :     # Compute the node name from the ID.
1307 :     my $nodeName = $id;
1308 :     $nodeName =~ s/_/ /g;
1309 :     # Create the node's leaf hash. This depends on the value of the radio
1310 :     # and link options.
1311 :     my $nodeContent = {};
1312 :     if ($optionThing->{links}) {
1313 :     # Compute the link value.
1314 :     my $linkable = uri_escape($id);
1315 : parrello 1.28 $nodeContent->{link} = "../FIG/display_subsys.cgi?ssa_name=$linkable;request=show_ssa;sort=by_phylo;show_clusters=1;SPROUT=1";
1316 : parrello 1.16 }
1317 :     if ($optionThing->{radio}) {
1318 :     # Compute the radio value.
1319 :     $nodeContent->{value} = "id=$id";
1320 :     }
1321 :     # Push the node into its parent branch.
1322 :     Trace("\"$nodeName\" added to node list.") if T(4);
1323 :     push @{$stackContents[$#stackNames]}, $nodeName, $nodeContent;
1324 :     }
1325 :     }
1326 :     # Return the result.
1327 :     return \@retVal;
1328 :     }
1329 :    
1330 :    
1331 : parrello 1.1 =head3 NmpdrGenomeMenu
1332 :    
1333 : parrello 1.3 C<< my $htmlText = $shelp->NmpdrGenomeMenu($menuName, $multiple, \@selected, $rows); >>
1334 : parrello 1.1
1335 :     This method creates a hierarchical HTML menu for NMPDR genomes organized by category. The
1336 :     category indicates the low-level NMPDR group. Organizing the genomes in this way makes it
1337 :     easier to select all genomes from a particular category.
1338 :    
1339 :     =over 4
1340 :    
1341 :     =item menuName
1342 :    
1343 :     Name to give to the menu.
1344 :    
1345 : parrello 1.3 =item multiple
1346 : parrello 1.1
1347 : parrello 1.3 TRUE if the user is allowed to select multiple genomes, else FALSE.
1348 : parrello 1.1
1349 :     =item selected
1350 :    
1351 :     Reference to a list containing the IDs of the genomes to be pre-selected. If the menu
1352 :     is not intended to allow multiple selections, the list should be a singleton. If the
1353 :     list is empty, nothing will be pre-selected.
1354 :    
1355 : parrello 1.3 =item rows (optional)
1356 :    
1357 :     Number of rows to display. If omitted, the default is 1 for a single-select list
1358 :     and 10 for a multi-select list.
1359 :    
1360 : parrello 1.8 =item crossMenu (optional)
1361 :    
1362 :     If specified, is presumed to be the name of another genome menu whose contents
1363 :     are to be mutually exclusive with the contents of this menu. As a result, instead
1364 :     of the standard onChange event, the onChange event will deselect any entries in
1365 :     the other menu.
1366 :    
1367 : parrello 1.1 =item RETURN
1368 :    
1369 :     Returns the HTML text to generate a C<SELECT> menu inside a form.
1370 :    
1371 :     =back
1372 :    
1373 :     =cut
1374 :    
1375 :     sub NmpdrGenomeMenu {
1376 :     # Get the parameters.
1377 : parrello 1.8 my ($self, $menuName, $multiple, $selected, $rows, $cross) = @_;
1378 : parrello 1.1 # Get the Sprout and CGI objects.
1379 :     my $sprout = $self->DB();
1380 :     my $cgi = $self->Q();
1381 : parrello 1.3 # Compute the row count.
1382 :     if (! defined $rows) {
1383 :     $rows = ($multiple ? 10 : 1);
1384 :     }
1385 :     # Create the multiple tag.
1386 :     my $multipleTag = ($multiple ? " multiple" : "");
1387 : parrello 1.1 # Get the form name.
1388 :     my $formName = $self->FormName();
1389 : parrello 1.3 # Check to see if we already have a genome list in memory.
1390 : parrello 1.32 my $groupHash;
1391 :     my @groups;
1392 :     my $nmpdrGroupCount;
1393 : parrello 1.3 my $genomes = $self->{genomeList};
1394 :     if (defined $genomes) {
1395 :     # We have a list ready to use.
1396 :     $groupHash = $genomes;
1397 : parrello 1.32 @groups = @{$self->{groupList}};
1398 :     $nmpdrGroupCount = $self->{groupCount};
1399 : parrello 1.3 } else {
1400 :     # Get a list of all the genomes in group order. In fact, we only need them ordered
1401 :     # by name (genus,species,strain), but putting primary-group in front enables us to
1402 :     # take advantage of an existing index.
1403 :     my @genomeList = $sprout->GetAll(['Genome'],
1404 :     "ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
1405 :     [], ['Genome(primary-group)', 'Genome(id)',
1406 :     'Genome(genus)', 'Genome(species)',
1407 : parrello 1.32 'Genome(unique-characterization)',
1408 :     'Genome(taxonomy)']);
1409 : parrello 1.3 # Create a hash to organize the genomes by group. Each group will contain a list of
1410 :     # 2-tuples, the first element being the genome ID and the second being the genome
1411 :     # name.
1412 :     my %gHash = ();
1413 :     for my $genome (@genomeList) {
1414 :     # Get the genome data.
1415 : parrello 1.32 my ($group, $genomeID, $genus, $species, $strain, $taxonomy) = @{$genome};
1416 : parrello 1.10 # Compute and cache its name and display group.
1417 : parrello 1.32 my ($name, $displayGroup, $domain) = $self->SaveOrganismData($group, $genomeID, $genus, $species,
1418 :     $strain, $taxonomy);
1419 : parrello 1.10 # Push the genome into the group's list. Note that we use the real group
1420 :     # name here, not the display group name.
1421 : parrello 1.32 push @{$gHash{$group}}, [$genomeID, $name, $domain];
1422 :     }
1423 :     # We are almost ready to unroll the menu out of the group hash. The final step is to separate
1424 :     # the supporting genomes by domain. First, we sort the NMPDR groups.
1425 :     @groups = sort grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
1426 :     # Remember the number of NMPDR groups.
1427 :     $nmpdrGroupCount = scalar @groups;
1428 :     # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
1429 :     # of the domains found.
1430 :     my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
1431 :     my @domains = ();
1432 :     for my $genomeData (@otherGenomes) {
1433 :     my ($genomeID, $name, $domain) = @{$genomeData};
1434 :     if (exists $gHash{$domain}) {
1435 :     push @{$gHash{$domain}}, $genomeData;
1436 :     } else {
1437 :     $gHash{$domain} = [$genomeData];
1438 :     push @domains, $domain;
1439 :     }
1440 : parrello 1.1 }
1441 : parrello 1.32 # Add the domain groups at the end of the main group list. The main group list will now
1442 :     # contain all the categories we need to display the genomes.
1443 :     push @groups, sort @domains;
1444 :     # Delete the supporting group.
1445 :     delete $gHash{$FIG_Config::otherGroup};
1446 : parrello 1.3 # Save the genome list for future use.
1447 :     $self->{genomeList} = \%gHash;
1448 : parrello 1.32 $self->{groupList} = \@groups;
1449 :     $self->{groupCount} = $nmpdrGroupCount;
1450 : parrello 1.3 $groupHash = \%gHash;
1451 : parrello 1.1 }
1452 : parrello 1.3 # Next, create a hash that specifies the pre-selected entries. Note that we need to deal
1453 :     # with the possibility of undefined values in the incoming list.
1454 :     my %selectedHash = ();
1455 :     if (defined $selected) {
1456 :     %selectedHash = map { $_ => 1 } grep { defined($_) } @{$selected};
1457 :     }
1458 : parrello 1.13 # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
1459 :     # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
1460 :     # and use that to make the selections.
1461 :     my $nmpdrCount = 0;
1462 : parrello 1.1 # Create the type counters.
1463 :     my $groupCount = 1;
1464 :     # Compute the ID for the status display.
1465 :     my $divID = "${formName}_${menuName}_status";
1466 :     # Compute the JavaScript call for updating the status.
1467 :     my $showSelect = "showSelected($menuName, '$divID', 1000);";
1468 :     # If multiple selection is supported, create an onChange event.
1469 :     my $onChange = "";
1470 : parrello 1.8 if ($cross) {
1471 : parrello 1.13 # Here we have a paired menu. Selecting something in our menu unselects it in the
1472 :     # other and redisplays the status of both.
1473 : parrello 1.8 $onChange = " onChange=\"crossUnSelect($menuName, '$divID', $cross, '${formName}_${cross}_status', 1000)\"";
1474 :     } elsif ($multiple) {
1475 : parrello 1.13 # This is an unpaired menu, so all we do is redisplay our status.
1476 : parrello 1.1 $onChange = " onChange=\"$showSelect\"";
1477 :     }
1478 :     # Create the SELECT tag and stuff it into the output array.
1479 : parrello 1.13 my @lines = ("<SELECT name=\"$menuName\"$onChange$multipleTag size=\"$rows\">");
1480 : parrello 1.1 # Loop through the groups.
1481 :     for my $group (@groups) {
1482 :     # Create the option group tag.
1483 :     my $tag = "<OPTGROUP label=\"$group\">";
1484 :     push @lines, " $tag";
1485 :     # Get the genomes in the group.
1486 : parrello 1.3 for my $genome (@{$groupHash->{$group}}) {
1487 : parrello 1.13 # Count this organism if it's NMPDR.
1488 : parrello 1.32 if ($nmpdrGroupCount > 0) {
1489 : parrello 1.13 $nmpdrCount++;
1490 :     }
1491 : parrello 1.32 # Get the organism ID, name, and domain.
1492 :     my ($genomeID, $name, $domain) = @{$genome};
1493 : parrello 1.1 # See if it's selected.
1494 :     my $select = ($selectedHash{$genomeID} ? " selected" : "");
1495 :     # Generate the option tag.
1496 : parrello 1.32 my $optionTag = "<OPTION class=\"$domain\" value=\"$genomeID\"$select>$name <em>($genomeID)</em></OPTION>";
1497 : parrello 1.1 push @lines, " $optionTag";
1498 :     }
1499 :     # Close the option group.
1500 :     push @lines, " </OPTGROUP>";
1501 : parrello 1.32 # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
1502 :     # groups.
1503 :     $nmpdrGroupCount--;
1504 : parrello 1.1 }
1505 :     # Close the SELECT tag.
1506 :     push @lines, "</SELECT>";
1507 :     # Check for multiple selection.
1508 : parrello 1.3 if ($multiple) {
1509 : parrello 1.15 # Multi-select is on, so we need to add some selection helpers. First is
1510 :     # the search box. This allows the user to type text and have all genomes containing
1511 :     # the text selected automatically.
1512 :     my $searchThingName = "${menuName}_SearchThing";
1513 : parrello 1.18 push @lines, "<br />" .
1514 :     "<INPUT type=\"button\" name=\"Search\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectViaSearch($menuName, $searchThingName); $showSelect\" />&nbsp;" .
1515 :     "<INPUT type=\"text\" name=\"$searchThingName\" size=\"30\" />";
1516 : parrello 1.15 # Next are the buttons to set and clear selections.
1517 : parrello 1.1 push @lines, "<br />";
1518 : parrello 1.15 push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\" value=\"Clear All\" onClick=\"clearAll($menuName); $showSelect\" />";
1519 : parrello 1.1 push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll($menuName); $showSelect\" />";
1520 : parrello 1.13 push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\" value=\"Select NMPDR\" onClick=\"selectSome($menuName, $nmpdrCount, true); $showSelect\" />";
1521 :     push @lines, "<INPUT type=\"button\" name=\"OtherOnly\" class=\"bigButton\" value=\"Select Supporting\" onClick=\"selectSome($menuName, $nmpdrCount, false); $showSelect\" />";
1522 : parrello 1.1 # Add the status display, too.
1523 :     push @lines, "<DIV id=\"$divID\" class=\"selectStatus\"></DIV>";
1524 :     # Queue to update the status display when the form loads. We need to modify the show statement
1525 :     # slightly because the queued statements are executed outside the form. This may seem like a lot of
1526 :     # trouble, but we want all of the show statement calls to be generated from a single line of code,
1527 :     # in case we decide to twiddle the parameters.
1528 :     $showSelect =~ s/showSelected\(/showSelected\(thisForm\./;
1529 :     $self->QueueFormScript($showSelect);
1530 : parrello 1.3 # Finally, add this parameter to the list of genome parameters. This enables us to
1531 :     # easily find all the parameters used to select one or more genomes.
1532 :     push @{$self->{genomeParms}}, $menuName;
1533 : parrello 1.1 }
1534 :     # Assemble all the lines into a string.
1535 :     my $retVal = join("\n", @lines, "");
1536 :     # Return the result.
1537 :     return $retVal;
1538 :     }
1539 :    
1540 : parrello 1.3 =head3 PropertyMenu
1541 :    
1542 :     C<< my $htmlText = $shelp->PropertyMenu($menuName, $selected, $force); >>
1543 :    
1544 :     Generate a property name dropdown menu.
1545 :    
1546 :     =over 4
1547 :    
1548 :     =item menuName
1549 :    
1550 :     Name to give to the menu.
1551 :    
1552 :     =item selected
1553 :    
1554 :     Value of the property name to pre-select.
1555 :    
1556 :     =item force (optional)
1557 :    
1558 :     If TRUE, then the user will be forced to choose a property name. If FALSE,
1559 :     then an additional menu choice will be provided to select nothing.
1560 :    
1561 :     =item RETURN
1562 :    
1563 :     Returns a dropdown menu box that allows the user to select a property name. An additional
1564 :     selection entry will be provided for selecting no property name
1565 :    
1566 :     =back
1567 :    
1568 :     =cut
1569 :    
1570 :     sub PropertyMenu {
1571 :     # Get the parameters.
1572 :     my ($self, $menuName, $selected, $force) = @_;
1573 :     # Get the CGI and Sprout objects.
1574 :     my $sprout = $self->DB();
1575 :     my $cgi = $self->Q();
1576 :     # Create the property name list.
1577 :     my @propNames = ();
1578 :     if (! $force) {
1579 :     push @propNames, "";
1580 :     }
1581 :     # Get all the property names, putting them after the null choice if one exists.
1582 :     push @propNames, $sprout->GetChoices('Property', 'property-name');
1583 :     # Create a menu from them.
1584 :     my $retVal = $cgi->popup_menu(-name=> $menuName, -values => \@propNames,
1585 :     -default => $selected);
1586 :     # Return the result.
1587 :     return $retVal;
1588 :     }
1589 :    
1590 : parrello 1.1 =head3 MakeTable
1591 :    
1592 :     C<< my $htmlText = $shelp->MakeTable(\@rows); >>
1593 :    
1594 :     Create a table from a group of table rows. The table rows must be fully pre-formatted: in
1595 :     other words, each must have the TR and TD tags included.
1596 :    
1597 :     The purpose of this method is to provide a uniform look for search form tables. It is
1598 :     almost impossible to control a table using styles, so rather than have a table style,
1599 :     we create the TABLE tag in this method. Note also that the first TD or TH in each row will
1600 :     be updated with an explicit width so the forms look pretty when they are all on one
1601 :     page.
1602 :    
1603 :     =over 4
1604 :    
1605 :     =item rows
1606 :    
1607 :     Reference to a list of table rows. Each table row must be in HTML form with all
1608 : parrello 1.29 the TR and TD tags set up. The first TD or TH tag in the first non-colspanned row
1609 :     will be modified to set the width. Everything else will be left as is.
1610 : parrello 1.1
1611 :     =item RETURN
1612 :    
1613 :     Returns the full HTML for a table in the approved NMPDR Search Form style.
1614 :    
1615 :     =back
1616 :    
1617 :     =cut
1618 :    
1619 :     sub MakeTable {
1620 :     # Get the parameters.
1621 :     my ($self, $rows) = @_;
1622 :     # Get the CGI object.
1623 :     my $cgi = $self->Q();
1624 : parrello 1.29 # The first column of the first row must have its width fixed.
1625 :     # This flag will be set to FALSE when that happens.
1626 :     my $needWidth = 1;
1627 : parrello 1.1 # modifier becase we only want to change the first tag. Also, if a width
1628 :     # is already specified on the first column bad things will happen.
1629 :     for my $row (@{$rows}) {
1630 : parrello 1.29 # See if this row needs a width.
1631 :     if ($needWidth && $row =~ /<(td|th) ([^>]+)>/i) {
1632 :     # Here we have a first cell and its tag parameters are in $2.
1633 :     my $elements = $2;
1634 :     if ($elements !~ /colspan/i) {
1635 :     Trace("No colspan tag found in element \'$elements\'.") if T(3);
1636 :     # Here there's no colspan, so we plug in the width. We
1637 :     # eschew the "g" modifier on the substitution because we
1638 :     # only want to update the first cell.
1639 :     $row =~ s/(<(td|th))/$1 width="150"/i;
1640 :     # Denote we don't need this any more.
1641 :     $needWidth = 0;
1642 :     }
1643 :     }
1644 : parrello 1.1 }
1645 :     # Create the table.
1646 :     my $retVal = $cgi->table({border => 2, cellspacing => 2,
1647 :     width => 700, class => 'search'},
1648 :     @{$rows});
1649 :     # Return the result.
1650 :     return $retVal;
1651 :     }
1652 :    
1653 :     =head3 SubmitRow
1654 :    
1655 : parrello 1.18 C<< my $htmlText = $shelp->SubmitRow($caption); >>
1656 : parrello 1.1
1657 :     Returns the HTML text for the row containing the page size control
1658 :     and the submit button. All searches should have this row somewhere
1659 :     near the top of the form.
1660 :    
1661 : parrello 1.18 =over 4
1662 :    
1663 :     =item caption (optional)
1664 :    
1665 :     Caption to be put on the search button. The default is C<Go>.
1666 :    
1667 :     =item RETURN
1668 :    
1669 :     Returns a table row containing the controls for submitting the search
1670 :     and tuning the results.
1671 :    
1672 :     =back
1673 :    
1674 : parrello 1.1 =cut
1675 :    
1676 :     sub SubmitRow {
1677 :     # Get the parameters.
1678 : parrello 1.18 my ($self, $caption) = @_;
1679 : parrello 1.1 my $cgi = $self->Q();
1680 : parrello 1.18 # Compute the button caption.
1681 :     my $realCaption = (defined $caption ? $caption : 'Go');
1682 : parrello 1.3 # Get the current page size.
1683 :     my $pageSize = $cgi->param('PageSize');
1684 :     # Get the incoming external-link flag.
1685 :     my $aliases = ($cgi->param('ShowAliases') ? 1 : 0);
1686 :     # Create the row.
1687 : parrello 1.1 my $retVal = $cgi->Tr($cgi->td("Results/Page"),
1688 :     $cgi->td($cgi->popup_menu(-name => 'PageSize',
1689 : parrello 1.7 -values => [10, 25, 50, 100, 1000],
1690 : parrello 1.25 -default => $pageSize)),
1691 : parrello 1.1 $cgi->td($cgi->submit(-class => 'goButton',
1692 :     -name => 'Search',
1693 : parrello 1.18 -value => $realCaption)));
1694 : parrello 1.1 # Return the result.
1695 :     return $retVal;
1696 :     }
1697 : parrello 1.2
1698 :     =head3 FeatureFilterRows
1699 :    
1700 : parrello 1.27 C<< my $htmlText = $shelp->FeatureFilterRows(@subset); >>
1701 : parrello 1.2
1702 : parrello 1.11 This method creates table rows that can be used to filter features. The form
1703 :     values can be used to select features by genome using the B<FeatureQuery>
1704 :     object.
1705 : parrello 1.2
1706 : parrello 1.27 =over 4
1707 :    
1708 :     =item subset
1709 :    
1710 :     List of rows to display. The default (C<all>) is to display all rows.
1711 :     C<words> displays the word search box, C<subsys> displays the subsystem
1712 :     selector, and C<options> displays the options row.
1713 :    
1714 :     =item RETURN
1715 :    
1716 :     Returns the html text for table rows containing the desired feature filtering controls.
1717 :    
1718 :     =back
1719 :    
1720 : parrello 1.2 =cut
1721 :    
1722 :     sub FeatureFilterRows {
1723 :     # Get the parameters.
1724 : parrello 1.27 my ($self, @subset) = @_;
1725 :     if (@subset == 0 || $subset[0] eq 'all') {
1726 :     @subset = qw(words subsys options);
1727 :     }
1728 : parrello 1.2 # Return the result.
1729 : parrello 1.27 return FeatureQuery::FilterRows($self, @subset);
1730 : parrello 1.2 }
1731 :    
1732 : parrello 1.1 =head3 GBrowseFeatureURL
1733 :    
1734 :     C<< my $url = SearchHelper::GBrowseFeatureURL($sprout, $feat); >>
1735 :    
1736 :     Compute the URL required to pull up a Gbrowse page for the the specified feature.
1737 :     In order to do this, we need to pull out the ID of the feature's Genome, its
1738 :     contig ID, and some rough starting and stopping offsets.
1739 :    
1740 :     =over 4
1741 :    
1742 :     =item sprout
1743 :    
1744 :     Sprout object for accessing the database.
1745 :    
1746 :     =item feat
1747 :    
1748 :     ID of the feature whose Gbrowse URL is desired.
1749 :    
1750 :     =item RETURN
1751 :    
1752 :     Returns a GET-style URL for the Gbrowse CGI, with parameters specifying the genome
1753 :     ID, contig ID, starting offset, and stopping offset.
1754 :    
1755 :     =back
1756 :    
1757 :     =cut
1758 :    
1759 :     sub GBrowseFeatureURL {
1760 :     # Get the parameters.
1761 :     my ($sprout, $feat) = @_;
1762 :     # Declare the return variable.
1763 :     my $retVal;
1764 :     # Compute the genome ID.
1765 :     my ($genomeID) = FIGRules::ParseFeatureID($feat);
1766 :     # Only proceed if the feature ID produces a valid genome.
1767 :     if ($genomeID) {
1768 :     # Get the feature location string.
1769 :     my $loc = $sprout->FeatureLocation($feat);
1770 :     # Compute the contig, start, and stop points.
1771 : parrello 1.6 my($contig, $start, $stop) = BasicLocation::Parse($loc);
1772 : parrello 1.5 Trace("Start and stop are ($start,$stop) on contig $contig.") if T(3);
1773 : parrello 1.1 # Now we need to do some goofiness to insure that the location is not too
1774 :     # big and that we get some surrounding stuff.
1775 :     my $mid = int(($start + $stop) / 2);
1776 :     my $chunk_len = 20000;
1777 :     my $max_feature = 40000;
1778 :     my $feat_len = abs($stop - $start);
1779 :     if ($feat_len > $chunk_len) {
1780 :     if ($feat_len > $max_feature) {
1781 :     $chunk_len = $max_feature;
1782 :     } else {
1783 :     $chunk_len = $feat_len + 100;
1784 :     }
1785 :     }
1786 :     my($show_start, $show_stop);
1787 :     if ($chunk_len == $max_feature) {
1788 :     $show_start = $start - 300;
1789 :     } else {
1790 :     $show_start = $mid - int($chunk_len / 2);
1791 :     }
1792 :     if ($show_start < 1) {
1793 :     $show_start = 1;
1794 :     }
1795 :     $show_stop = $show_start + $chunk_len - 1;
1796 :     my $clen = $sprout->ContigLength($contig);
1797 :     if ($show_stop > $clen) {
1798 :     $show_stop = $clen;
1799 :     }
1800 :     my $seg_id = $contig;
1801 :     $seg_id =~ s/:/--/g;
1802 : parrello 1.5 Trace("Show limits are ($show_start,$show_stop) in genome $genomeID with ref $seg_id.") if T(3);
1803 : parrello 1.1 # Assemble all the pieces.
1804 : parrello 1.18 $retVal = "gbrowse.cgi/GB_$genomeID?ref=$seg_id;start=$show_start;stop=$show_stop";
1805 : parrello 1.1 }
1806 :     # Return the result.
1807 :     return $retVal;
1808 :     }
1809 :    
1810 : parrello 1.3 =head3 GetGenomes
1811 :    
1812 :     C<< my @genomeList = $shelp->GetGenomes($parmName); >>
1813 :    
1814 :     Return the list of genomes specified by the specified CGI query parameter.
1815 :     If the request method is POST, then the list of genome IDs is returned
1816 :     without preamble. If the request method is GET and the parameter is not
1817 :     specified, then it is treated as a request for all genomes. This makes it
1818 :     easier for web pages to link to a search that wants to specify all genomes.
1819 :    
1820 :     =over 4
1821 :    
1822 :     =item parmName
1823 :    
1824 :     Name of the parameter containing the list of genomes. This will be the
1825 :     first parameter passed to the L</NmpdrGenomeMenu> call that created the
1826 :     genome selection control on the form.
1827 :    
1828 :     =item RETURN
1829 :    
1830 :     Returns a list of the genomes to process.
1831 :    
1832 :     =back
1833 :    
1834 :     =cut
1835 :    
1836 :     sub GetGenomes {
1837 :     # Get the parameters.
1838 :     my ($self, $parmName) = @_;
1839 :     # Get the CGI query object.
1840 :     my $cgi = $self->Q();
1841 :     # Get the list of genome IDs in the request header.
1842 :     my @retVal = $cgi->param($parmName);
1843 :     Trace("Genome list for $parmName is (" . join(", ", @retVal) . ") with method " . $cgi->request_method() . ".") if T(3);
1844 :     # Check for the special GET case.
1845 :     if ($cgi->request_method() eq "GET" && ! @retVal) {
1846 :     # Here the caller wants all the genomes.
1847 :     my $sprout = $self->DB();
1848 :     @retVal = $sprout->Genomes();
1849 :     }
1850 :     # Return the result.
1851 :     return @retVal;
1852 :     }
1853 :    
1854 :     =head3 GetHelpText
1855 :    
1856 :     C<< my $htmlText = $shelp->GetHelpText(); >>
1857 :    
1858 :     Get the help text for this search. The help text is stored in files on the template
1859 :     server. The help text for a specific search is taken from a file named
1860 :     C<SearchHelp_>I<class>C<.inc> in the template directory C<$FIG_Config::template_url>.
1861 :     There are also three standard help files: C<SearchHelp1_Filtering.inc> describes the
1862 :     feature filtering performed by the B<FeatureQuery> object, C<SearchHelp1_GenomeControl.inc>
1863 :     describes how to use a multiple-selection genome control, and C<SearchHelp1_Standard.inc>
1864 :     describes the standard controls for a search, such as page size, URL display, and
1865 :     external alias display.
1866 :    
1867 :     =cut
1868 :    
1869 :     sub GetHelpText {
1870 :     # Get the parameters.
1871 :     my ($self) = @_;
1872 :     # Create a list to hold the pieces of the help.
1873 :     my @helps = ();
1874 :     # Get the template directory URL.
1875 :     my $urlBase = $FIG_Config::template_url;
1876 :     # Start with the specific help.
1877 :     my $class = $self->{class};
1878 :     push @helps, PageBuilder::GetPage("$urlBase/SearchHelp_$class.inc");
1879 :     # Add the genome control help if needed.
1880 :     if (scalar @{$self->{genomeParms}}) {
1881 :     push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_GenomeControl.inc");
1882 :     }
1883 :     # Next the filter help.
1884 :     if ($self->{filtered}) {
1885 :     push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_Filtering.inc");
1886 :     }
1887 :     # Finally, the standard help.
1888 :     push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_Standard.inc");
1889 :     # Assemble the pieces.
1890 :     my $retVal = join("\n<p>&nbsp;</p>\n", @helps);
1891 :     # Return the result.
1892 :     return $retVal;
1893 :     }
1894 :    
1895 :     =head3 ComputeSearchURL
1896 :    
1897 : parrello 1.19 C<< my $url = $shelp->ComputeSearchURL(%overrides); >>
1898 : parrello 1.3
1899 :     Compute the GET-style URL for the current search. In order for this to work, there
1900 :     must be a copy of the search form on the current page. This will always be the
1901 :     case if the search is coming from C<SearchSkeleton.cgi>.
1902 :    
1903 :     A little expense is involved in order to make the URL as smart as possible. The
1904 :     main complication is that if the user specified all genomes, we'll want to
1905 :     remove the parameter entirely from a get-style URL.
1906 :    
1907 : parrello 1.19 =over 4
1908 :    
1909 :     =item overrides
1910 :    
1911 :     Hash containing override values for the parameters, where the parameter name is
1912 :     the key and the parameter value is the override value. If the override value is
1913 :     C<undef>, the parameter will be deleted from the result.
1914 :    
1915 :     =item RETURN
1916 :    
1917 :     Returns a GET-style URL for invoking the search with the specified overrides.
1918 :    
1919 :     =back
1920 :    
1921 : parrello 1.3 =cut
1922 :    
1923 :     sub ComputeSearchURL {
1924 :     # Get the parameters.
1925 : parrello 1.19 my ($self, %overrides) = @_;
1926 : parrello 1.3 # Get the database and CGI query object.
1927 :     my $cgi = $self->Q();
1928 :     my $sprout = $self->DB();
1929 :     # Start with the full URL.
1930 :     my $retVal = $cgi->url(-full => 1);
1931 :     # Get all the query parameters in a hash.
1932 :     my %parms = $cgi->Vars();
1933 :     # Now we need to do some fixing. Each multi-valued parameter is encoded as a string with null
1934 :     # characters separating the individual values. We have to convert those to lists. In addition,
1935 :     # the multiple-selection genome parameters and the feature type parameter must be checked to
1936 :     # determine whether or not they can be removed from the URL. First, we get a list of the
1937 :     # genome parameters and a list of all genomes. Note that we only need the list if a
1938 :     # multiple-selection genome parameter has been found on the form.
1939 :     my %genomeParms = map { $_ => 1 } @{$self->{genomeParms}};
1940 :     my @genomeList;
1941 :     if (keys %genomeParms) {
1942 :     @genomeList = $sprout->Genomes();
1943 :     }
1944 :     # Create a list to hold the URL parameters we find.
1945 :     my @urlList = ();
1946 :     # Now loop through the parameters in the hash, putting them into the output URL.
1947 :     for my $parmKey (keys %parms) {
1948 :     # Get a list of the parameter values. If there's only one, we'll end up with
1949 :     # a singleton list, but that's okay.
1950 :     my @values = split (/\0/, $parms{$parmKey});
1951 :     # Check for special cases.
1952 : parrello 1.26 if (grep { $_ eq $parmKey } qw(SessionID ResultCount Page PageSize Trace TF)) {
1953 : parrello 1.3 # These are bookkeeping parameters we don't need to start a search.
1954 :     @values = ();
1955 :     } elsif ($parmKey =~ /_SearchThing$/) {
1956 :     # Here the value coming in is from a genome control's search thing. It does
1957 :     # not affect the results of the search, so we clear it.
1958 :     @values = ();
1959 :     } elsif ($genomeParms{$parmKey}) {
1960 :     # Here we need to see if the user wants all the genomes. If he does,
1961 :     # we erase all the values just like with features.
1962 :     my $allFlag = $sprout->IsAllGenomes(\@values, \@genomeList);
1963 :     if ($allFlag) {
1964 :     @values = ();
1965 :     }
1966 : parrello 1.19 } elsif (exists $overrides{$parmKey}) {
1967 :     # Here the value is being overridden, so we skip it for now.
1968 :     @values = ();
1969 : parrello 1.3 }
1970 :     # If we still have values, create the URL parameters.
1971 :     if (@values) {
1972 :     push @urlList, map { "$parmKey=" . uri_escape($_) } @values;
1973 :     }
1974 :     }
1975 : parrello 1.19 # Now do the overrides.
1976 :     for my $overKey (keys %overrides) {
1977 :     # Only use this override if it's not a delete marker.
1978 :     if (defined $overrides{$overKey}) {
1979 :     push @urlList, "$overKey=" . uri_escape($overrides{$overKey});
1980 :     }
1981 :     }
1982 : parrello 1.3 # Add the parameters to the URL.
1983 :     $retVal .= "?" . join(";", @urlList);
1984 :     # Return the result.
1985 :     return $retVal;
1986 :     }
1987 :    
1988 :     =head3 GetRunTimeValue
1989 :    
1990 :     C<< my $htmlText = $shelp->GetRunTimeValue($text); >>
1991 :    
1992 :     Compute a run-time column value.
1993 :    
1994 :     =over 4
1995 :    
1996 :     =item text
1997 :    
1998 :     The run-time column text. It consists of 2 percent signs, a column type, an equal
1999 :     sign, and the data for the current row.
2000 :    
2001 :     =item RETURN
2002 :    
2003 :     Returns the fully-formatted HTML text to go into the current column of the current row.
2004 :    
2005 :     =back
2006 :    
2007 :     =cut
2008 :    
2009 :     sub GetRunTimeValue {
2010 :     # Get the parameters.
2011 :     my ($self, $text) = @_;
2012 :     # Declare the return variable.
2013 :     my $retVal;
2014 :     # Parse the incoming text.
2015 :     if ($text =~ /^%%([^=]+)=(.*)$/) {
2016 :     $retVal = $self->RunTimeColumns($1, $2);
2017 :     } else {
2018 :     Confess("Invalid run-time column string \"$text\" encountered in session file.");
2019 :     }
2020 :     # Return the result.
2021 :     return $retVal;
2022 :     }
2023 :    
2024 : parrello 1.9 =head3 AdvancedClassList
2025 :    
2026 :     C<< my @classes = SearchHelper::AdvancedClassList(); >>
2027 :    
2028 :     Return a list of advanced class names. This list is used to generate the directory
2029 :     of available searches on the search page.
2030 :    
2031 : parrello 1.15 We use the %INC variable to accomplish this.
2032 : parrello 1.9
2033 :     =cut
2034 :    
2035 :     sub AdvancedClassList {
2036 : parrello 1.15 my @retVal = map { $_ =~ /^SH(\w+)\.pm/; $1 } grep { $_ =~ /^SH/ } keys %INC;
2037 : parrello 1.30 return sort @retVal;
2038 : parrello 1.9 }
2039 :    
2040 : parrello 1.16 =head3 SelectionTree
2041 :    
2042 :     C<< my $htmlText = SearchHelper::SelectionTree($cgi, \%tree, %options); >>
2043 :    
2044 :     Display a selection tree.
2045 :    
2046 :     This method creates the HTML for a tree selection control. The tree is implemented as a set of
2047 :     nested HTML unordered lists. Each selectable element of the tree will contain a radio button. In
2048 :     addition, some of the tree nodes can contain hyperlinks.
2049 :    
2050 :     The tree itself is passed in as a multi-level list containing node names followed by
2051 :     contents. Each content element is a reference to a similar list. The first element of
2052 :     each list may be a hash reference. If so, it should contain one or both of the following
2053 :     keys.
2054 :    
2055 :     =over 4
2056 :    
2057 :     =item link
2058 :    
2059 :     The navigation URL to be popped up if the user clicks on the node name.
2060 :    
2061 :     =item value
2062 :    
2063 :     The form value to be returned if the user selects the tree node.
2064 :    
2065 :     =back
2066 :    
2067 :     The presence of a C<link> key indicates the node name will be hyperlinked. The presence of
2068 :     a C<value> key indicates the node name will have a radio button. If a node has no children,
2069 :     you may pass it a hash reference instead of a list reference.
2070 :    
2071 :     The following example shows the hash for a three-level tree with links on the second level and
2072 :     radio buttons on the third.
2073 :    
2074 :     [ Objects => [
2075 :     Entities => [
2076 :     {link => "../docs/WhatIsAnEntity.html"},
2077 :     Genome => {value => 'GenomeData'},
2078 :     Feature => {value => 'FeatureData'},
2079 :     Contig => {value => 'ContigData'},
2080 :     ],
2081 :     Relationships => [
2082 :     {link => "../docs/WhatIsARelationShip.html"},
2083 :     HasFeature => {value => 'GenomeToFeature'},
2084 :     IsOnContig => {value => 'FeatureToContig'},
2085 :     ]
2086 :     ]
2087 :     ]
2088 :    
2089 :     Note how each leaf of the tree has a hash reference for its value, while the branch nodes
2090 :     all have list references.
2091 :    
2092 :     This next example shows how to set up a taxonomy selection field. The value returned
2093 :     by the tree control will be the taxonomy string for the selected node ready for use
2094 :     in a LIKE-style SQL filter. Only the single branch ending in campylobacter is shown for
2095 :     reasons of space.
2096 :    
2097 :     [ All => [
2098 :     {value => "%"},
2099 :     Bacteria => [
2100 :     {value => "Bacteria%"},
2101 :     Proteobacteria => [
2102 :     {value => "Bacteria; Proteobacteria%"},
2103 :     Epsilonproteobacteria => [
2104 :     {value => "Bacteria; Proteobacteria;Epsilonproteobacteria%"},
2105 :     Campylobacterales => [
2106 :     {value => "Bacteria; Proteobacteria; Epsilonproteobacteria; Campylobacterales%"},
2107 :     Campylobacteraceae =>
2108 :     {value => "Bacteria; Proteobacteria; Epsilonproteobacteria; Campylobacterales; Campylobacteraceae%"},
2109 :     ...
2110 :     ]
2111 :     ...
2112 :     ]
2113 :     ...
2114 :     ]
2115 :     ...
2116 :     ]
2117 :     ...
2118 :     ]
2119 :     ]
2120 :    
2121 :    
2122 :     This method of tree storage allows the caller to control the order in which the tree nodes
2123 :     are displayed and to completely control value selection and use of hyperlinks. It is, however
2124 :     a bit complicated. Eventually, tree-building classes will be provided to simplify things.
2125 :    
2126 :     The parameters to this method are as follows.
2127 :    
2128 :     =over 4
2129 :    
2130 :     =item cgi
2131 :    
2132 :     CGI object used to generate the HTML.
2133 :    
2134 :     =item tree
2135 :    
2136 :     Reference to a hash describing a tree. See the description above.
2137 :    
2138 :     =item options
2139 :    
2140 :     Hash containing options for the tree display.
2141 :    
2142 :     =back
2143 :    
2144 :     The allowable options are as follows
2145 :    
2146 :     =over 4
2147 :    
2148 :     =item nodeImageClosed
2149 :    
2150 :     URL of the image to display next to the tree nodes when they are collapsed. Clicking
2151 :     on the image will expand a section of the tree. The default is C<../FIG/Html/plus.gif>.
2152 :    
2153 :     =item nodeImageOpen
2154 :    
2155 :     URL of the image to display next to the tree nodes when they are expanded. Clicking
2156 :     on the image will collapse a section of the tree. The default is C<../FIG/Html/minus.gif>.
2157 :    
2158 :     =item style
2159 :    
2160 :     Style to use for the tree. The default is C<tree>. Because the tree style is implemented
2161 :     as nested lists, the key components of this style are the definitions for the C<ul> and
2162 :     C<li> tags. The default style file contains the following definitions.
2163 :    
2164 :     .tree ul {
2165 :     margin-left: 0; padding-left: 22px
2166 :     }
2167 :     .tree li {
2168 :     list-style-type: none;
2169 :     }
2170 :    
2171 :     The default image is 22 pixels wide, so in the above scheme each tree level is indented from its
2172 :     parent by the width of the node image. This use of styles limits the things we can do in formatting
2173 :     the tree, but it has the advantage of vastly simplifying the tree creation.
2174 :    
2175 :     =item name
2176 :    
2177 :     Field name to give to the radio buttons in the tree. The default is C<selection>.
2178 :    
2179 :     =item target
2180 :    
2181 :     Frame target for links. The default is C<_self>.
2182 :    
2183 :     =item selected
2184 :    
2185 :     If specified, the value of the radio button to be pre-selected.
2186 :    
2187 :     =back
2188 :    
2189 :     =cut
2190 :    
2191 :     sub SelectionTree {
2192 :     # Get the parameters.
2193 :     my ($cgi, $tree, %options) = @_;
2194 :     # Get the options.
2195 :     my $optionThing = Tracer::GetOptions({ name => 'selection',
2196 :     nodeImageClosed => '../FIG/Html/plus.gif',
2197 :     nodeImageOpen => '../FIG/Html/minus.gif',
2198 :     style => 'tree',
2199 :     target => '_self',
2200 :     selected => undef},
2201 :     \%options);
2202 :     # Declare the return variable. We'll do the standard thing with creating a list
2203 :     # of HTML lines and rolling them together at the end.
2204 :     my @retVal = ();
2205 :     # Only proceed if the tree is present.
2206 :     if (defined($tree)) {
2207 :     # Validate the tree.
2208 :     if (ref $tree ne 'ARRAY') {
2209 :     Confess("Selection tree is not a list reference.");
2210 :     } elsif (scalar @{$tree} == 0) {
2211 :     # The tree is empty, so we do nothing.
2212 :     } elsif ($tree->[0] eq 'HASH') {
2213 :     Confess("Hash reference found at start of selection tree. The tree as a whole cannot have attributes, only tree nodes.");
2214 :     } else {
2215 :     # Here we have a real tree. Apply the tree style.
2216 :     push @retVal, $cgi->start_div({ class => $optionThing->{style} });
2217 :     # Give us a DIV ID.
2218 :     my $divID = GetDivID($optionThing->{name});
2219 :     # Show the tree.
2220 :     push @retVal, ShowBranch($cgi, "(root)", $divID, $tree, $optionThing, 'block');
2221 :     # Close the DIV block.
2222 :     push @retVal, $cgi->end_div();
2223 :     }
2224 :     }
2225 :     # Return the result.
2226 :     return join("\n", @retVal, "");
2227 :     }
2228 :    
2229 :     =head3 ShowBranch
2230 :    
2231 :     C<< my @htmlLines = SearchHelper::ShowBranch($cgi, $label, $id, $branch, $options, $displayType); >>
2232 :    
2233 :     This is a recursive method that displays a branch of the tree.
2234 :    
2235 :     =over 4
2236 :    
2237 :     =item cgi
2238 :    
2239 :     CGI object used to format HTML.
2240 :    
2241 :     =item label
2242 :    
2243 :     Label of this tree branch. It is only used in error messages.
2244 :    
2245 :     =item id
2246 :    
2247 :     ID to be given to this tree branch. The ID is used in the code that expands and collapses
2248 :     tree nodes.
2249 :    
2250 :     =item branch
2251 :    
2252 :     Reference to a list containing the content of the tree branch. The list contains an optional
2253 :     hash reference that is ignored and the list of children, each child represented by a name
2254 :     and then its contents. The contents could by a hash reference (indicating the attributes
2255 :     of a leaf node), or another tree branch.
2256 :    
2257 :     =item options
2258 :    
2259 :     Options from the original call to L</SelectionTree>.
2260 :    
2261 :     =item displayType
2262 :    
2263 :     C<block> if the contents of this list are to be displayed, C<none> if they are to be
2264 :     hidden.
2265 :    
2266 :     =item RETURN
2267 :    
2268 :     Returns one or more HTML lines that can be used to display the tree branch.
2269 :    
2270 :     =back
2271 :    
2272 :     =cut
2273 :    
2274 :     sub ShowBranch {
2275 :     # Get the parameters.
2276 :     my ($cgi, $label, $id, $branch, $options, $displayType) = @_;
2277 :     # Declare the return variable.
2278 :     my @retVal = ();
2279 :     # Start the branch.
2280 :     push @retVal, $cgi->start_ul({ id => $id, style => "display:$displayType" });
2281 :     # Check for the hash and choose the start location accordingly.
2282 :     my $i0 = (ref $branch->[0] eq 'HASH' ? 1 : 0);
2283 :     # Get the list length.
2284 :     my $i1 = scalar(@{$branch});
2285 :     # Verify we have an even number of elements.
2286 :     if (($i1 - $i0) % 2 != 0) {
2287 :     Trace("Branch elements are from $i0 to $i1.") if T(3);
2288 :     Confess("Odd number of elements in tree branch $label.");
2289 :     } else {
2290 :     # Loop through the elements.
2291 :     for (my $i = $i0; $i < $i1; $i += 2) {
2292 :     # Get this node's label and contents.
2293 :     my ($myLabel, $myContent) = ($branch->[$i], $branch->[$i+1]);
2294 :     # Get an ID for this node's children (if any).
2295 :     my $myID = GetDivID($options->{name});
2296 :     # Now we need to find the list of children and the options hash.
2297 :     # This is a bit ugly because we allow the shortcut of a hash without an
2298 :     # enclosing list. First, we need some variables.
2299 :     my $attrHash = {};
2300 :     my @childHtml = ();
2301 :     my $hasChildren = 0;
2302 :     if (! ref $myContent) {
2303 :     Confess("Invalid tree definition. Scalar found as content of node \"$myLabel\".");
2304 :     } elsif (ref $myContent eq 'HASH') {
2305 :     # Here the node is a leaf and its content contains the link/value hash.
2306 :     $attrHash = $myContent;
2307 :     } elsif (ref $myContent eq 'ARRAY') {
2308 :     # Here the node may be a branch. Its content is a list.
2309 :     my $len = scalar @{$myContent};
2310 :     if ($len >= 1) {
2311 :     # Here the first element of the list could by the link/value hash.
2312 :     if (ref $myContent->[0] eq 'HASH') {
2313 :     $attrHash = $myContent->[0];
2314 :     # If there's data in the list besides the hash, it's our child list.
2315 :     # We can pass the entire thing as the child list, because the hash
2316 :     # is ignored.
2317 :     if ($len > 1) {
2318 :     $hasChildren = 1;
2319 :     }
2320 :     } else {
2321 :     $hasChildren = 1;
2322 :     }
2323 :     # If we have children, create the child list with a recursive call.
2324 :     if ($hasChildren) {
2325 :     Trace("Processing children of $myLabel.") if T(4);
2326 :     push @childHtml, ShowBranch($cgi, $myLabel, $myID, $myContent, $options, 'none');
2327 : parrello 1.27 Trace("Children of $myLabel finished.") if T(4);
2328 : parrello 1.16 }
2329 :     }
2330 :     }
2331 :     # Okay, it's time to pause and take stock. We have the label of the current node
2332 :     # in $myLabel, its attributes in $attrHash, and if it is NOT a leaf node, we
2333 :     # have a child list in @childHtml. If it IS a leaf node, $hasChildren is 0.
2334 :     # Compute the image HTML. It's tricky, because we have to deal with the open and
2335 :     # closed images.
2336 :     my @images = ($options->{nodeImageOpen}, $options->{nodeImageClosed});
2337 :     my $image = $images[$hasChildren];
2338 :     my $prefixHtml = $cgi->img({src => $image, id => "${myID}img"});
2339 :     if ($hasChildren) {
2340 :     # If there are children, we wrap the image in a toggle hyperlink.
2341 :     $prefixHtml = $cgi->a({ onClick => "javascript:treeToggle('$myID','$images[0]', '$images[1]')" },
2342 :     $prefixHtml);
2343 :     }
2344 :     # Now the radio button, if any. Note we use "defined" in case the user wants the
2345 :     # value to be 0.
2346 :     if (defined $attrHash->{value}) {
2347 :     # Due to a glitchiness in the CGI stuff, we have to build the attribute
2348 :     # hash for the "input" method. If the item is pre-selected, we add
2349 :     # "checked => undef" to the hash. Otherwise, we can't have "checked"
2350 :     # at all.
2351 :     my $radioParms = { type => 'radio',
2352 :     name => $options->{name},
2353 :     value => $attrHash->{value},
2354 :     };
2355 :     if (defined $options->{selected} && $options->{selected} eq $attrHash->{value}) {
2356 :     $radioParms->{checked} = undef;
2357 :     }
2358 :     $prefixHtml .= $cgi->input($radioParms);
2359 :     }
2360 :     # Next, we format the label.
2361 :     my $labelHtml = $myLabel;
2362 : parrello 1.27 Trace("Formatting tree node for \"$myLabel\".") if T(4);
2363 : parrello 1.16 # Apply a hyperlink if necessary.
2364 :     if (defined $attrHash->{link}) {
2365 :     $labelHtml = $cgi->a({ href => $attrHash->{link}, target => $options->{target} },
2366 :     $labelHtml);
2367 :     }
2368 :     # Finally, roll up the child HTML. If there are no children, we'll get a null string
2369 :     # here.
2370 :     my $childHtml = join("\n", @childHtml);
2371 :     # Now we have all the pieces, so we can put them together.
2372 :     push @retVal, $cgi->li("$prefixHtml$labelHtml$childHtml");
2373 :     }
2374 :     }
2375 :     # Close the tree branch.
2376 :     push @retVal, $cgi->end_ul();
2377 :     # Return the result.
2378 :     return @retVal;
2379 :     }
2380 :    
2381 :     =head3 GetDivID
2382 :    
2383 :     C<< my $idString = SearchHelper::GetDivID($name); >>
2384 :    
2385 :     Return a new HTML ID string.
2386 :    
2387 :     =over 4
2388 :    
2389 :     =item name
2390 :    
2391 :     Name to be prefixed to the ID string.
2392 :    
2393 :     =item RETURN
2394 :    
2395 :     Returns a hopefully-unique ID string.
2396 :    
2397 :     =back
2398 :    
2399 :     =cut
2400 :    
2401 :     sub GetDivID {
2402 :     # Get the parameters.
2403 :     my ($name) = @_;
2404 :     # Compute the ID.
2405 :     my $retVal = "elt_$name$divCount";
2406 :     # Increment the counter to make sure this ID is not re-used.
2407 :     $divCount++;
2408 :     # Return the result.
2409 :     return $retVal;
2410 :     }
2411 :    
2412 : parrello 1.31
2413 :     =head3 PrintLine
2414 :    
2415 :     C<< $shelp->PrintLine($message); >>
2416 :    
2417 :     Print a line of CGI output. This is used during the operation of the B<Find> method while
2418 :     searching, so the user sees progress in real-time.
2419 :    
2420 :     =over 4
2421 :    
2422 :     =item message
2423 :    
2424 :     HTML text to display.
2425 :    
2426 :     =back
2427 :    
2428 :     =cut
2429 :    
2430 :     sub PrintLine {
2431 :     # Get the parameters.
2432 :     my ($self, $message) = @_;
2433 :     # Send them to the output.
2434 :     print "$message\n";
2435 :     }
2436 :    
2437 : parrello 1.1 =head2 Feature Column Methods
2438 :    
2439 : parrello 1.19 The methods in this section manage feature column data. If you want to provide the
2440 : parrello 1.1 capability to include new types of data in feature columns, then all the changes
2441 :     are made to this section of the source file. Technically, this should be implemented
2442 :     using object-oriented methods, but this is simpler for non-programmers to maintain.
2443 :     To add a new column of feature data, you must first give it a name. For example,
2444 :     the name for the protein page link column is C<protlink>. If the column is to appear
2445 :     in the default list of feature columns, add it to the list returned by
2446 :     L</DefaultFeatureColumns>. Then add code to produce the column title to
2447 : parrello 1.28 L</FeatureColumnTitle> and code to produce its value to L</FeatureColumnValue>. If the
2448 :     feature column should be excluded from downloads, add it to the C<FeatureColumnSkip>
2449 :     hash. Everything else will happen automatically.
2450 : parrello 1.1
2451 : parrello 1.28 There is a special column name syntax for extra columns (that is, nonstandard
2452 : parrello 1.1 feature columns). If the column name begins with C<X=>, then it is presumed to be
2453 :     an extra column. The column title is the text after the C<X=>, and its value is
2454 :     pulled from the extra column hash.
2455 :    
2456 : parrello 1.28 =cut
2457 :    
2458 :     # This hash is used to determine which columns should not be included in downloads.
2459 :     my %FeatureColumnSkip = map { $_ => 1 } qw(gblink viewerlink protlink);
2460 :    
2461 : parrello 1.1 =head3 DefaultFeatureColumns
2462 :    
2463 : parrello 1.14 C<< my @colNames = $shelp->DefaultFeatureColumns(); >>
2464 : parrello 1.1
2465 : parrello 1.14 Return a list of the default feature column identifiers. These identifiers can
2466 :     be passed to L</FeatureColumnTitle> and L</FeatureColumnValue> in order to
2467 :     produce the column titles and row values.
2468 : parrello 1.1
2469 :     =cut
2470 :    
2471 :     sub DefaultFeatureColumns {
2472 :     # Get the parameters.
2473 :     my ($self) = @_;
2474 :     # Return the result.
2475 : parrello 1.14 return qw(orgName function gblink protlink);
2476 : parrello 1.1 }
2477 :    
2478 :     =head3 FeatureColumnTitle
2479 :    
2480 :     C<< my $title = $shelp->FeatureColumnTitle($colName); >>
2481 :    
2482 :     Return the column heading title to be used for the specified feature column.
2483 :    
2484 :     =over 4
2485 :    
2486 :     =item name
2487 :    
2488 :     Name of the desired feature column.
2489 :    
2490 :     =item RETURN
2491 :    
2492 :     Returns the title to be used as the column header for the named feature column.
2493 :    
2494 :     =back
2495 :    
2496 :     =cut
2497 :    
2498 :     sub FeatureColumnTitle {
2499 :     # Get the parameters.
2500 :     my ($self, $colName) = @_;
2501 :     # Declare the return variable. We default to a blank column name.
2502 :     my $retVal = "&nbsp;";
2503 :     # Process the column name.
2504 :     if ($colName =~ /^X=(.+)$/) {
2505 :     # Here we have an extra column.
2506 :     $retVal = $1;
2507 : parrello 1.13 } elsif ($colName eq 'alias') {
2508 :     $retVal = "External Aliases";
2509 : parrello 1.1 } elsif ($colName eq 'fid') {
2510 :     $retVal = "FIG ID";
2511 :     } elsif ($colName eq 'function') {
2512 :     $retVal = "Functional Assignment";
2513 :     } elsif ($colName eq 'gblink') {
2514 :     $retVal = "GBrowse";
2515 : parrello 1.13 } elsif ($colName eq 'group') {
2516 :     $retVal = "NMDPR Group";
2517 :     } elsif ($colName =~ /^keyword:(.+)$/) {
2518 :     $retVal = ucfirst $1;
2519 :     } elsif ($colName eq 'orgName') {
2520 : parrello 1.21 $retVal = "Organism and Gene ID";
2521 : parrello 1.1 } elsif ($colName eq 'protlink') {
2522 :     $retVal = "NMPDR Protein Page";
2523 : parrello 1.28 } elsif ($colName eq 'viewerlink') {
2524 :     $retVal = "Annotation Page";
2525 : parrello 1.13 } elsif ($colName eq 'subsystem') {
2526 :     $retVal = "Subsystems";
2527 : parrello 1.31 } elsif ($colName eq 'pdb') {
2528 :     $retVal = "Best PDB Match";
2529 : parrello 1.1 }
2530 :     # Return the result.
2531 :     return $retVal;
2532 :     }
2533 :    
2534 : parrello 1.28 =head3 FeatureColumnDownload
2535 :    
2536 :     C<< my $keep = $shelp->FeatureColumnDownload($colName); >>
2537 :    
2538 :     Return TRUE if the named feature column is to be kept when downloading, else FALSE.
2539 :    
2540 :     =over 4
2541 :    
2542 :     =item colName
2543 :    
2544 :     Name of the relevant feature column.
2545 :    
2546 :     =item RETURN
2547 :    
2548 :     Return TRUE if the named column should be kept while downloading, else FALSE. In general,
2549 :     FALSE is returned if the column generates a button, image, or other purely-HTML value.
2550 :    
2551 :     =back
2552 :    
2553 :     =cut
2554 :    
2555 :     sub FeatureColumnDownload {
2556 :     # Get the parameters.
2557 :     my ($self, $colName) = @_;
2558 :     # Return the determination. We download the column if it's not in the skip-hash.
2559 :     # Note we return 0 and 1 instead of 1 and undef because it simplifies some tracing.
2560 :     return (exists $FeatureColumnSkip{$colName} ? 0 : 1);
2561 :     }
2562 :    
2563 : parrello 1.13
2564 : parrello 1.1 =head3 FeatureColumnValue
2565 :    
2566 :     C<< my $value = $shelp->FeatureColumnValue($colName, $fid, \%extraCols); >>
2567 :    
2568 :     Return the value to be displayed in the specified feature column.
2569 :    
2570 :     =over 4
2571 :    
2572 :     =item colName
2573 :    
2574 :     Name of the column to be displayed.
2575 :    
2576 :     =item record
2577 :    
2578 : parrello 1.28 ERDBObject record for the feature being displayed in the current row.
2579 : parrello 1.1
2580 :     =item extraCols
2581 :    
2582 :     Reference to a hash of extra column names to values. If the incoming column name
2583 :     begins with C<X=>, its value will be taken from this hash.
2584 :    
2585 :     =item RETURN
2586 :    
2587 :     Returns the HTML to be displayed in the named column for the specified feature.
2588 :    
2589 :     =back
2590 :    
2591 :     =cut
2592 :    
2593 :     sub FeatureColumnValue {
2594 :     # Get the parameters.
2595 :     my ($self, $colName, $record, $extraCols) = @_;
2596 :     # Get the sprout and CGI objects.
2597 :     my $cgi = $self->Q();
2598 :     my $sprout = $self->DB();
2599 :     # Get the feature ID.
2600 :     my ($fid) = $record->Value('Feature(id)');
2601 :     # Declare the return variable. Denote that we default to a non-breaking space,
2602 :     # which will translate to an empty table cell (rather than a table cell with no
2603 :     # interior, which is what you get for a null string).
2604 :     my $retVal = "&nbsp;";
2605 :     # Process according to the column name.
2606 :     if ($colName =~ /^X=(.+)$/) {
2607 :     # Here we have an extra column. Only update if the value exists. Note that
2608 :     # a value of C<undef> is treated as a non-existent value, because the
2609 :     # caller may have put "colName => undef" in the "PutFeature" call in order
2610 :     # to insure we know the extra column exists.
2611 :     if (defined $extraCols->{$1}) {
2612 :     $retVal = $extraCols->{$1};
2613 :     }
2614 : parrello 1.13 } elsif ($colName eq 'alias') {
2615 :     # In this case, the user wants a list of external aliases for the feature.
2616 :     # These are very expensive, so we compute them when the row is displayed.
2617 : parrello 1.29 # To do the computation, we need to know the favored alias type and the
2618 :     # feature ID.
2619 :     my $favored = $cgi->param("FavoredAlias") || "fig";
2620 :     $retVal = "%%alias=$fid,$favored";
2621 : parrello 1.1 } elsif ($colName eq 'fid') {
2622 :     # Here we have the raw feature ID. We hyperlink it to the protein page.
2623 :     $retVal = HTML::set_prot_links($fid);
2624 :     } elsif ($colName eq 'function') {
2625 :     # The functional assignment is just a matter of getting some text.
2626 :     ($retVal) = $record->Value('Feature(assignment)');
2627 :     } elsif ($colName eq 'gblink') {
2628 :     # Here we want a link to the GBrowse page using the official GBrowse button.
2629 : parrello 1.23 $retVal = FakeButton('GBrowse', "GetGBrowse.cgi", undef,
2630 : parrello 1.18 fid => $fid);
2631 : parrello 1.13 } elsif ($colName eq 'group') {
2632 :     # Get the NMPDR group name.
2633 :     my (undef, $group) = $self->OrganismData($fid);
2634 :     # Dress it with a URL to the group's main page.
2635 :     my $nurl = $sprout->GroupPageName($group);
2636 :     $retVal = $cgi->a({ href => $nurl, title => "$group summary" },
2637 :     $group);
2638 :     } elsif ($colName =~ /^keyword:(.+)$/) {
2639 :     # Here we want keyword-related values. This is also expensive, so
2640 :     # we compute them when the row is displayed.
2641 : parrello 1.14 $retVal = "%%$colName=$fid";
2642 : parrello 1.13 } elsif ($colName eq 'orgName') {
2643 :     # Here we want the formatted organism name and feature number.
2644 :     $retVal = $self->FeatureName($fid);
2645 : parrello 1.1 } elsif ($colName eq 'protlink') {
2646 :     # Here we want a link to the protein page using the official NMPDR button.
2647 : parrello 1.23 $retVal = FakeButton('NMPDR', "protein.cgi", undef,
2648 : parrello 1.18 prot => $fid, SPROUT => 1, new_framework => 0,
2649 :     user => '');
2650 : parrello 1.28 } elsif ($colName eq 'viewerlink') {
2651 :     # Here we want a link to the SEED viewer page using the official viewer button.
2652 :     $retVal = FakeButton('Annotation', "index.cgi", undef,
2653 :     action => 'ShowAnnotation', prot => $fid);
2654 :     } elsif ($colName eq 'subsystem') {
2655 : parrello 1.13 # Another run-time column: subsystem list.
2656 :     $retVal = "%%subsystem=$fid";
2657 : parrello 1.31 } elsif ($colName eq 'pdb') {
2658 :     $retVal = "%%pdb=$fid";
2659 : parrello 1.1 }
2660 :     # Return the result.
2661 :     return $retVal;
2662 :     }
2663 :    
2664 : parrello 1.3 =head3 RunTimeColumns
2665 :    
2666 :     C<< my $htmlText = $shelp->RunTimeColumns($type, $text); >>
2667 :    
2668 :     Return the HTML text for a run-time column. Run-time columns are evaluated when the
2669 :     list is displayed, rather than when it is generated.
2670 :    
2671 :     =over 4
2672 :    
2673 :     =item type
2674 :    
2675 :     Type of column.
2676 :    
2677 :     =item text
2678 :    
2679 :     Data relevant to this row of the column.
2680 :    
2681 :     =item RETURN
2682 :    
2683 :     Returns the fully-formatted HTML text to go in the specified column.
2684 :    
2685 :     =back
2686 :    
2687 :     =cut
2688 :    
2689 :     sub RunTimeColumns {
2690 :     # Get the parameters.
2691 :     my ($self, $type, $text) = @_;
2692 :     # Declare the return variable.
2693 :     my $retVal = "";
2694 :     # Get the Sprout and CGI objects.
2695 :     my $sprout = $self->DB();
2696 :     my $cgi = $self->Q();
2697 : parrello 1.14 Trace("Runtime column $type with text \"$text\" found.") if T(4);
2698 : parrello 1.3 # Separate the text into a type and data.
2699 : parrello 1.13 if ($type eq 'alias') {
2700 : parrello 1.3 # Here the caller wants external alias links for a feature. The text
2701 : parrello 1.29 # parameter for computing the alias is the feature ID followed by
2702 :     # the favored alias type.
2703 :     my ($fid, $favored) = split /\s*,\s*/, $text;
2704 :     # The complicated part is we have to hyperlink them and handle the
2705 :     # favorites. First, get the aliases.
2706 : parrello 1.3 Trace("Generating aliases for feature $fid.") if T(4);
2707 : parrello 1.29 my @aliases = sort $sprout->FeatureAliases($fid);
2708 : parrello 1.3 # Only proceed if we found some.
2709 :     if (@aliases) {
2710 : parrello 1.29 # Split the aliases into favored and unfavored.
2711 :     my @favored = ();
2712 :     my @unfavored = ();
2713 :     for my $alias (@aliases) {
2714 :     # Use substr instead of pattern match because $favored is specified by the user
2715 :     # and we don't want him to put funny meta-characters in there.
2716 :     if (substr($alias, 0, length($favored)) eq $favored) {
2717 :     push @favored, $alias;
2718 :     } else {
2719 :     push @unfavored, $alias;
2720 :     }
2721 :     }
2722 :     # Rejoin the aliases into a comma-delimited list, with the favored ones first.
2723 :     my $aliasList = join(", ", @favored, @unfavored);
2724 : parrello 1.3 # Ask the HTML processor to hyperlink them.
2725 :     $retVal = HTML::set_prot_links($cgi, $aliasList);
2726 :     }
2727 : parrello 1.13 } elsif ($type eq 'subsystem') {
2728 :     # Here the caller wants the subsystems in which this feature participates.
2729 :     # The text is the feature ID. We will list the subsystem names with links
2730 :     # to the subsystem's summary page.
2731 :     my $fid = $text;
2732 :     # Get the subsystems.
2733 :     Trace("Generating subsystems for feature $fid.") if T(4);
2734 :     my %subs = $sprout->SubsystemsOf($fid);
2735 : parrello 1.19 # Extract the subsystem names.
2736 :     my @names = map { HTML::sub_link($cgi, $_) } sort keys %subs;
2737 : parrello 1.13 # String them into a list.
2738 : parrello 1.19 $retVal = join(", ", @names);
2739 : parrello 1.13 } elsif ($type =~ /^keyword:(.+)$/) {
2740 :     # Here the caller wants the value of the named keyword. The text is the
2741 :     # feature ID.
2742 :     my $keywordName = $1;
2743 :     my $fid = $text;
2744 :     # Get the attribute values.
2745 :     Trace("Getting $keywordName values for feature $fid.") if T(4);
2746 :     my @values = $sprout->GetFlat(['Feature'], "Feature(id) = ?", [$fid],
2747 :     "Feature($keywordName)");
2748 :     # String them into a list.
2749 :     $retVal = join(", ", @values);
2750 : parrello 1.31 } elsif ($type eq 'pdb') {
2751 :     # Here the caller wants the best PDB match to this feature. The text
2752 :     # is the feature ID. We will display the PDB with a link to the
2753 :     # PDB page along with the match score. If there are docking results we
2754 :     # will display a link to the docking result search.
2755 :     my $fid = $text;
2756 :     # Ask for the best PDB.
2757 :     my ($bestPDB) = $sprout->GetAll(['IsProteinForFeature', 'PDB'],
2758 :     "IsProteinForFeature(from-link) = ? ORDER BY IsProteinForFeature(score) LIMIT 1",
2759 :     [$fid], ['PDB(id)', 'PDB(docking-count)', 'IsProteinForFeature(score)']);
2760 :     # Only proceed if there is a PDB.
2761 :     if ($bestPDB) {
2762 :     my ($pdbID, $dockingCount, $score) = @{$bestPDB};
2763 :     # Convert the PDB ID to a hyperlink.
2764 :     my $pdbLink = SHDrugSearch::PDBLink($cgi, $pdbID);
2765 :     # Append the score.
2766 :     $retVal = "$pdbLink ($score)";
2767 :     # If there are docking results, append a docking results link.
2768 :     if ($dockingCount > 0) {
2769 :     my $dockString = "$dockingCount docking results";
2770 :     my $dockLink = $cgi->a({ href => $cgi->url() . "?Class=DrugSearch;PDB=$pdbID;NoForm=1",
2771 :     alt => "View computed docking results for $pdbID",
2772 :     title => "View computed docking results for $pdbID",
2773 :     target => "_blank"},
2774 :     $dockString);
2775 :     }
2776 :     }
2777 : parrello 1.3 }
2778 :     # Return the result.
2779 :     return $retVal;
2780 :     }
2781 :    
2782 : parrello 1.10 =head3 SaveOrganismData
2783 :    
2784 : parrello 1.32 C<< my ($name, $displayGroup) = $shelp->SaveOrganismData($group, $genomeID, $genus, $species, $strain, $taxonomy); >>
2785 : parrello 1.10
2786 :     Format the name of an organism and the display version of its group name. The incoming
2787 :     data should be the relevant fields from the B<Genome> record in the database. The
2788 :     data will also be stored in the genome cache for later use in posting search results.
2789 :    
2790 :     =over 4
2791 :    
2792 :     =item group
2793 :    
2794 :     Name of the genome's group as it appears in the database.
2795 :    
2796 :     =item genomeID
2797 :    
2798 :     ID of the relevant genome.
2799 :    
2800 :     =item genus
2801 :    
2802 :     Genus of the genome's organism. If undefined or null, it will be assumed the genome is not
2803 :     in the database. In this case, the organism name is derived from the genomeID and the group
2804 :     is automatically the supporting-genomes group.
2805 :    
2806 :     =item species
2807 :    
2808 :     Species of the genome's organism.
2809 :    
2810 :     =item strain
2811 :    
2812 :     Strain of the species represented by the genome.
2813 :    
2814 : parrello 1.32 =item taxonomy
2815 :    
2816 :     Taxonomy of the species represented by the genome.
2817 :    
2818 : parrello 1.10 =item RETURN
2819 :    
2820 : parrello 1.32 Returns a three-element list. The first element is the formatted genome name. The second
2821 :     element is the display name of the genome's group. The third is the genome's domain.
2822 : parrello 1.10
2823 :     =back
2824 :    
2825 :     =cut
2826 :    
2827 :     sub SaveOrganismData {
2828 :     # Get the parameters.
2829 : parrello 1.32 my ($self, $group, $genomeID, $genus, $species, $strain, $taxonomy) = @_;
2830 : parrello 1.10 # Declare the return values.
2831 :     my ($name, $displayGroup);
2832 :     # If the organism does not exist, format an unknown name and a blank group.
2833 :     if (! defined($genus)) {
2834 :     $name = "Unknown Genome $genomeID";
2835 :     $displayGroup = "";
2836 :     } else {
2837 :     # It does exist, so format the organism name.
2838 :     $name = "$genus $species";
2839 :     if ($strain) {
2840 :     $name .= " $strain";
2841 :     }
2842 :     # Compute the display group. This is currently the same as the incoming group
2843 :     # name unless it's the supporting group, which is nulled out.
2844 :     $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
2845 :     }
2846 : parrello 1.32 # Compute the domain from the taxonomy.
2847 :     my ($domain) = split /\s*;\s*/, $taxonomy, 2;
2848 : parrello 1.10 # Cache the group and organism data.
2849 :     my $cache = $self->{orgs};
2850 : parrello 1.32 $cache->{$genomeID} = [$name, $displayGroup, $domain];
2851 : parrello 1.10 # Return the result.
2852 : parrello 1.32 return ($name, $displayGroup, $domain);
2853 : parrello 1.10 }
2854 :    
2855 : parrello 1.16 =head3 ValidateKeywords
2856 :    
2857 :     C<< my $okFlag = $shelp->ValidateKeywords($keywordString, $required); >>
2858 :    
2859 :     Insure that a keyword string is reasonably valid. If it is invalid, a message will be
2860 :     set.
2861 :    
2862 :     =over 4
2863 :    
2864 :     =item keywordString
2865 :    
2866 :     Keyword string specified as a parameter to the current search.
2867 :    
2868 :     =item required
2869 :    
2870 :     TRUE if there must be at least one keyword specified, else FALSE.
2871 :    
2872 :     =item RETURN
2873 :    
2874 :     Returns TRUE if the keyword string is valid, else FALSE. Note that a null keyword string
2875 :     is acceptable if the I<$required> parameter is not specified.
2876 :    
2877 :     =back
2878 :    
2879 :     =cut
2880 :    
2881 :     sub ValidateKeywords {
2882 :     # Get the parameters.
2883 :     my ($self, $keywordString, $required) = @_;
2884 :     # Declare the return variable.
2885 :     my $retVal = 0;
2886 :     my @wordList = split /\s+/, $keywordString;
2887 :     # Right now our only real worry is a list of all minus words. The problem with it is that
2888 :     # it will return an incorrect result.
2889 :     my @plusWords = grep { $_ =~ /^[^\-]/ } @wordList;
2890 :     if (! @wordList) {
2891 :     if ($required) {
2892 :     $self->SetMessage("No search words specified.");
2893 : parrello 1.22 } else {
2894 :     $retVal = 1;
2895 : parrello 1.16 }
2896 :     } elsif (! @plusWords) {
2897 :     $self->SetMessage("At least one keyword must be positive. All the keywords entered are preceded by minus signs.");
2898 :     } else {
2899 :     $retVal = 1;
2900 :     }
2901 :     # Return the result.
2902 :     return $retVal;
2903 :     }
2904 :    
2905 : parrello 1.23 =head3 FakeButton
2906 :    
2907 :     C<< my $html = SearchHelper::FakeButton($caption, $url, $target, %parms); >>
2908 :    
2909 :     Create a fake button that hyperlinks to the specified URL with the specified parameters.
2910 :     Unlike a real button, this one won't visibly click, but it will take the user to the
2911 :     correct place.
2912 :    
2913 :     The parameters of this method are deliberately identical to L</Formlet> so that we
2914 :     can switch easily from real buttons to fake ones in the code.
2915 :    
2916 :     =over 4
2917 :    
2918 :     =item caption
2919 :    
2920 :     Caption to be put on the button.
2921 :    
2922 :     =item url
2923 :    
2924 :     URL for the target page or script.
2925 :    
2926 :     =item target
2927 :    
2928 :     Frame or target in which the new page should appear. If C<undef> is specified,
2929 :     the default target will be used.
2930 :    
2931 :     =item parms
2932 :    
2933 :     Hash containing the parameter names as keys and the parameter values as values.
2934 :     These will be appended to the URL.
2935 :    
2936 :     =back
2937 :    
2938 :     =cut
2939 :    
2940 :     sub FakeButton {
2941 :     # Get the parameters.
2942 :     my ($caption, $url, $target, %parms) = @_;
2943 :     # Declare the return variable.
2944 :     my $retVal;
2945 :     # Compute the target URL.
2946 :     my $targetUrl = "$url?" . join(";", map { "$_=" . uri_escape($parms{$_}) } keys %parms);
2947 :     # Compute the target-frame HTML.
2948 :     my $targetHtml = ($target ? " target=\"$target\"" : "");
2949 :     # Assemble the result.
2950 :     return "<a href=\"$targetUrl\" $targetHtml><div class=\"button2 button\">$caption</div></a>";
2951 :     }
2952 :    
2953 : parrello 1.18 =head3 Formlet
2954 :    
2955 :     C<< my $html = SearchHelper::Formlet($caption, $url, $target, %parms); >>
2956 :    
2957 :     Create a mini-form that posts to the specified URL with the specified parameters. The
2958 :     parameters will be stored in hidden fields, and the form's only visible control will
2959 :     be a submit button with the specified caption.
2960 :    
2961 :     Note that we don't use B<CGI.pm> services here because they generate forms with extra characters
2962 :     and tags that we don't want to deal with.
2963 :    
2964 :     =over 4
2965 :    
2966 :     =item caption
2967 :    
2968 :     Caption to be put on the form button.
2969 :    
2970 :     =item url
2971 :    
2972 :     URL to be put in the form's action parameter.
2973 :    
2974 :     =item target
2975 :    
2976 :     Frame or target in which the form results should appear. If C<undef> is specified,
2977 :     the default target will be used.
2978 :    
2979 :     =item parms
2980 :    
2981 :     Hash containing the parameter names as keys and the parameter values as values.
2982 :    
2983 :     =back
2984 :    
2985 :     =cut
2986 :    
2987 :     sub Formlet {
2988 :     # Get the parameters.
2989 :     my ($caption, $url, $target, %parms) = @_;
2990 :     # Compute the target HTML.
2991 :     my $targetHtml = ($target ? " target=\"$target\"" : "");
2992 :     # Start the form.
2993 :     my $retVal = "<form method=\"POST\" action=\"$url\"$target>";
2994 :     # Add the parameters.
2995 :     for my $parm (keys %parms) {
2996 :     $retVal .= "<input type=\"hidden\" name=\"$parm\" value=\"$parms{$parm}\" />";
2997 :     }
2998 :     # Put in the button.
2999 :     $retVal .= "<input type=\"submit\" name=\"submit\" value=\"$caption\" class=\"button\" />";
3000 :     # Close the form.
3001 :     $retVal .= "</form>";
3002 :     # Return the result.
3003 :     return $retVal;
3004 :     }
3005 :    
3006 : parrello 1.30 =head3 TuningParameters
3007 :    
3008 :     C<< my $options = $shelp->TuningParameters(%parmHash); >>
3009 :    
3010 :     Retrieve tuning parameters from the CGI query object. The parameter is a hash that maps parameter names
3011 :     to their default values. The parameters and their values will be returned as a hash reference.
3012 :    
3013 :     =over 4
3014 :    
3015 :     =item parmHash
3016 :    
3017 :     Hash mapping parameter names to their default values.
3018 :    
3019 :     =item RETURN
3020 :    
3021 :     Returns a reference to a hash containing the parameter names mapped to their actual values.
3022 :    
3023 :     =back
3024 :    
3025 :     =cut
3026 :    
3027 :     sub TuningParameters {
3028 :     # Get the parameters.
3029 :     my ($self, %parmHash) = @_;
3030 :     # Declare the return variable.
3031 :     my $retVal = {};
3032 :     # Get the CGI Query Object.
3033 :     my $cgi = $self->Q();
3034 :     # Loop through the parameter names.
3035 :     for my $parm (keys %parmHash) {
3036 :     # Get the incoming value for this parameter.
3037 :     my $value = $cgi->param($parm);
3038 :     # Zero might be a valid value, so we do an is-defined check rather than an OR.
3039 :     if (defined($value)) {
3040 :     $retVal->{$parm} = $value;
3041 :     } else {
3042 :     $retVal->{$parm} = $parmHash{$parm};
3043 :     }
3044 :     }
3045 :     # Return the result.
3046 :     return $retVal;
3047 :     }
3048 :    
3049 : parrello 1.4 =head2 Virtual Methods
3050 :    
3051 :     =head3 Form
3052 :    
3053 :     C<< my $html = $shelp->Form(); >>
3054 :    
3055 :     Generate the HTML for a form to request a new search.
3056 :    
3057 :     =head3 Find
3058 :    
3059 :     C<< my $resultCount = $shelp->Find(); >>
3060 :    
3061 :     Conduct a search based on the current CGI query parameters. The search results will
3062 :     be written to the session cache file and the number of results will be
3063 :     returned. If the search parameters are invalid, a result count of C<undef> will be
3064 :     returned and a result message will be stored in this object describing the problem.
3065 :    
3066 :     =head3 Description
3067 :    
3068 :     C<< my $htmlText = $shelp->Description(); >>
3069 :    
3070 :     Return a description of this search. The description is used for the table of contents
3071 :     on the main search tools page. It may contain HTML, but it should be character-level,
3072 :     not block-level, since the description is going to appear in a list.
3073 :    
3074 :     =head3 SortKey
3075 :    
3076 : parrello 1.11 C<< my $key = $shelp->SortKey($fdata); >>
3077 : parrello 1.4
3078 : parrello 1.11 Return the sort key for the specified feature data. The default is to sort by feature name,
3079 : parrello 1.10 floating NMPDR organisms to the top. If a full-text search is used, then the default
3080 :     sort is by relevance followed by feature name. This sort may be overridden by the
3081 :     search class to provide fancier functionality. This method is called by
3082 :     B<PutFeature>, so it is only used for feature searches. A non-feature search
3083 :     would presumably have its own sort logic.
3084 : parrello 1.4
3085 :     =over 4
3086 :    
3087 :     =item record
3088 :    
3089 : parrello 1.11 The C<FeatureData> containing the current feature.
3090 : parrello 1.4
3091 :     =item RETURN
3092 :    
3093 :     Returns a key field that can be used to sort this row in among the results.
3094 :    
3095 :     =back
3096 :    
3097 :     =cut
3098 :    
3099 :     sub SortKey {
3100 :     # Get the parameters.
3101 : parrello 1.11 my ($self, $fdata) = @_;
3102 : parrello 1.4 # Get the feature ID from the record.
3103 : parrello 1.11 my $fid = $fdata->FID();
3104 : parrello 1.4 # Get the group from the feature ID.
3105 :     my $group = $self->FeatureGroup($fid);
3106 :     # Ask the feature query object to form the sort key.
3107 : parrello 1.11 my $retVal = $fdata->SortKey($self, $group);
3108 : parrello 1.4 # Return the result.
3109 :     return $retVal;
3110 :     }
3111 : parrello 1.9
3112 : parrello 1.31 =head3 SearchTitle
3113 :    
3114 :     C<< my $titleHtml = $shelp->SearchTitle(); >>
3115 :    
3116 :     Return the display title for this search. The display title appears above the search results.
3117 :     If no result is returned, no title will be displayed. The result should be an html string
3118 :     that can be legally put inside a block tag such as C<h3> or C<p>.
3119 :    
3120 :     =cut
3121 :    
3122 :     sub SearchTitle {
3123 :     # Get the parameters.
3124 :     my ($self) = @_;
3125 :     # Declare the return variable.
3126 :     my $retVal;
3127 :     # Return it.
3128 :     return $retVal;
3129 :     }
3130 :    
3131 :     =head3 DownloadFormatAvailable
3132 : parrello 1.28
3133 : parrello 1.31 C<< my $okFlag = $shelp->DownloadFormatAvailable($format); >>
3134 : parrello 1.28
3135 : parrello 1.31 This method returns TRUE if a specified download format is legal for this type of search
3136 :     and FALSE otherwise. For any feature-based search, there is no need to override this
3137 :     method.
3138 : parrello 1.28
3139 :     =over 4
3140 :    
3141 : parrello 1.31 =item format
3142 :    
3143 :     Download format type code.
3144 :    
3145 :     =item RETURN
3146 : parrello 1.28
3147 : parrello 1.31 Returns TRUE if the download format is legal for this search and FALSE otherwise.
3148 : parrello 1.28
3149 :     =back
3150 :    
3151 :     =cut
3152 :    
3153 : parrello 1.31 sub DownloadFormatAvailable {
3154 : parrello 1.28 # Get the parameters.
3155 : parrello 1.31 my ($self, $format) = @_;
3156 :     # Declare the return variable.
3157 :     my $retVal = 1;
3158 :     # Return the result.
3159 :     return $retVal;
3160 : parrello 1.28 }
3161 :    
3162 : parrello 1.32 =head3 ColumnTitle
3163 :    
3164 :     C<< my $title = $shelp->ColumnTitle($colName); >>
3165 :    
3166 :     Return the column heading title to be used for the specified column name. The
3167 :     default implementation is to simply call L</FeatureColumnTitle>.
3168 :    
3169 :     =over 4
3170 :    
3171 :     =item colName
3172 :    
3173 :     Name of the desired column.
3174 :    
3175 :     =item RETURN
3176 :    
3177 :     Returns the title to be used as the column header for the named column.
3178 :    
3179 :     =back
3180 :    
3181 :     =cut
3182 :    
3183 :     sub ColumnTitle {
3184 :     my ($self, $colName) = @_;
3185 :     return $self->FeatureColumnTitle($colName);
3186 :     }
3187 :    
3188 :    
3189 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3