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

Annotation of /Sprout/SearchHelper.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3