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

Diff of /Sprout/SearchHelper.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.32, Thu May 17 23:43:30 2007 UTC revision 1.37, Mon Aug 20 23:22:42 2007 UTC
# Line 10  Line 10 
10      use File::Path;      use File::Path;
11      use File::stat;      use File::stat;
12      use LWP::UserAgent;      use LWP::UserAgent;
13      use Time::HiRes 'gettimeofday';      use FIGRules;
14      use Sprout;      use Sprout;
15      use SFXlate;      use SFXlate;
16      use FIGRules;      use FIGRules;
17      use HTML;      use HTML;
18      use BasicLocation;      use BasicLocation;
     use FeatureQuery;  
19      use URI::Escape;      use URI::Escape;
20      use PageBuilder;      use PageBuilder;
21        use AliasAnalysis;
22        use FreezeThaw qw(freeze thaw);
23    
24  =head1 Search Helper Base Class  =head1 Search Helper Base Class
25    
# Line 65  Line 66 
66    
67  =item orgs  =item orgs
68    
69  Reference to a hash mapping genome IDs to organism names.  Reference to a hash mapping genome IDs to organism data. (Used to
70    improve performance.)
71    
72  =item name  =item name
73    
# Line 83  Line 85 
85    
86  List of the parameters that are used to select multiple genomes.  List of the parameters that are used to select multiple genomes.
87    
 =item filtered  
   
 TRUE if this is a feature-filtered search, else FALSE. B<NOTE> that this  
 field is updated by the B<FeatureQuery> object.  
   
 =item extraPos  
   
 Hash indicating which extra columns should be put at the end. Extra columns  
 not mentioned in this hash are put at the beginning. Use the L</SetExtraPos>  
 method to change this option.  
   
88  =back  =back
89    
90  =head2 Adding a new Search Tool  =head2 Adding a new Search Tool
# Line 109  Line 100 
100  =item 2  =item 2
101    
102  Create a new subclass of this object and implement each of the virtual methods. The  Create a new subclass of this object and implement each of the virtual methods. The
103  name of the subclass must be C<SH>I<className>.  name of the subclass must be C<SH>I<className>, where I<className> is the
104    type of search.
105    
106  =item 3  =item 3
107    
# Line 119  Line 111 
111    
112  =item 4  =item 4
113    
114  In the C<SearchSkeleton.cgi> script and add a C<use> statement for your search tool.  If your search produces a result for which a helper does not exist, you
115    must create a new subclass of B<ResultHelper>. Its name must be
116    C<RH>I<className>, where I<className> is the type of result.
117    
118  =back  =back
119    
# Line 155  Line 149 
149    
150  Several helper methods are provided for particular purposes.  Several helper methods are provided for particular purposes.
151    
 =over 4  
   
 =item 1  
   
152  L</NmpdrGenomeMenu> generates a control for selecting one or more genomes. Use  L</NmpdrGenomeMenu> generates a control for selecting one or more genomes. Use
153  L</GetGenomes> to retrieve all the genomes passed in for a specified parameter  L</GetGenomes> to retrieve all the genomes passed in for a specified parameter
154  name. Note that as an assist to people working with GET-style links, if no  name. Note that as an assist to people working with GET-style links, if no
155  genomes are specified and the incoming request style is GET, all genomes will  genomes are specified and the incoming request style is GET, all genomes will
156  be returned.  be returned.
157    
 =item 2  
   
 L</FeatureFilterRow> formats several rows of controls for filtering features.  
 When you start building the code for the L</Find> method, you can use a  
 B<FeatureQuery> object to automatically filter each genome's features using  
 the values from the filter controls.  
   
 =item 3  
   
158  L</QueueFormScript> allows you to queue JavaScript statements for execution  L</QueueFormScript> allows you to queue JavaScript statements for execution
159  after the form is fully generated. If you are using very complicated  after the form is fully generated. If you are using very complicated
160  form controls, the L</QueueFormScript> method allows you to perform  form controls, the L</QueueFormScript> method allows you to perform
161  JavaScript initialization. The L</NmpdrGenomeMenu> control uses this  JavaScript initialization. The L</NmpdrGenomeMenu> control uses this
162  facility to display a list of the pre-selected genomes.  facility to display a list of the pre-selected genomes.
163    
 =back  
   
 If you are doing a feature search, you can also change the list of feature  
 columns displayed and their display order by overriding  
 L</DefaultFeatureColumns>.  
   
164  Finally, when generating the code for your controls, be sure to use any incoming  Finally, when generating the code for your controls, be sure to use any incoming
165  query parameters as default values so that the search request is persistent.  query parameters as default values so that the search request is persistent.
166    
167  =head3 Finding Search Results  =head3 Finding Search Results
168    
169  The L</Find> method is used to create the search results. For a search that  The L</Find> method is used to create the search results. The basic code
170  wants to return features (which is most of them), the basic code structure  structure would work as follows.
 would work as follows. It is assumed that the L</FeatureFilterRows> method  
 has been used to create feature filtering parameters.  
171    
172      sub Find {      sub Find {
173          my ($self) = @_;          my ($self) = @_;
# Line 207  Line 180 
180          ... validate the parameters ...          ... validate the parameters ...
181          if (... invalid parameters...) {          if (... invalid parameters...) {
182              $self->SetMessage(...appropriate message...);              $self->SetMessage(...appropriate message...);
183          } elsif (FeatureQuery::Valid($self)) {          } else {
184                # Determine the result type.
185                my $rhelp = SearchHelper::GetHelper($self, RH => $resultType);
186                # Specify the columns.
187                $self->DefaultColumns($rhelp);
188                # You may want to add extra columns. $name is the column name and
189                # $loc is its location. The other parameters take their names from the
190                # corresponding column methods.
191                $rhelp->AddExtraColumn($name => $loc, style => $style, download => $flag,
192                    title => $title);
193                # Some searches require optional columns that are configured by the
194                # user or by the search query itself. There are some special methods
195                # for this in the result helpers, but there's also the direct approach
196                # shown below.
197                $rhelp->AddOptionalColumn($name => $loc);
198              # Initialize the session file.              # Initialize the session file.
199              $self->OpenSession();              $self->OpenSession($rhelp);
200              # Initialize the result counter.              # Initialize the result counter.
201              $retVal = 0;              $retVal = 0;
202              ... get a list of genomes ...              ... set up to loop through the results ...
203              for my $genomeID (... each genome ...) {              while (...more results...) {
204                  my $fq = FeatureQuery->new($self, $genomeID);                  ...compute extra columns and call PutExtraColumns...
205                  while (my $feature = $fq->Fetch()) {                  $rhelp->PutData($sortKey, $objectID, $record);
                     ... examine the feature ...  
                     if (... we want to keep it ...) {  
                         $self->PutFeature($fq);  
206                          $retVal++;                          $retVal++;
207                      }                      }
                 }  
             }  
208              # Close the session file.              # Close the session file.
209              $self->CloseSession();              $self->CloseSession();
210          }          }
# Line 231  Line 213 
213      }      }
214    
215  A Find method is of course much more complicated than generating a form, and there  A Find method is of course much more complicated than generating a form, and there
216  are variations on the above theme. For example, you could eschew feature filtering  are variations on the above theme.
 entirely in favor of your own custom filtering, you could include extra columns  
 in the output, or you could search for something that's not a feature at all. The  
 above code is just a loose framework.  
217    
218  In addition to the finding and filtering, it is necessary to send status messages  In addition to the finding and filtering, it is necessary to send status messages
219  to the output so that the user does not get bored waiting for results. The L</PrintLine>  to the output so that the user does not get bored waiting for results. The L</PrintLine>
# Line 246  Line 225 
225  The break tag is optional. When the Find method gets control, a paragraph will  The break tag is optional. When the Find method gets control, a paragraph will
226  have been started so that everything is XHTML-compliant.  have been started so that everything is XHTML-compliant.
227    
 If you wish to add your own extra columns to the output, use the B<AddExtraColumns>  
 method of the feature query object.  
   
     $fq->AddExtraColumns(score => $sc);  
   
228  The L</Find> method must return C<undef> if the search parameters are invalid. If this  The L</Find> method must return C<undef> if the search parameters are invalid. If this
229  is the case, then a message describing the problem should be passed to the framework  is the case, then a message describing the problem should be passed to the framework
230  by calling L</SetMessage>. If the parameters are valid, then the method must return  by calling L</SetMessage>. If the parameters are valid, then the method must return
# Line 291  Line 265 
265          Trace("No session ID found.") if T(3);          Trace("No session ID found.") if T(3);
266          # Here we're starting a new session. We create the session ID and          # Here we're starting a new session. We create the session ID and
267          # store it in the query object.          # store it in the query object.
268          $session_id = NewSessionID();          $session_id = FIGRules::NewSessionID();
269            Trace("New session ID is $session_id.") if T(3);
270          $type = "new";          $type = "new";
271          $cgi->param(-name => 'SessionID', -value => $session_id);          $cgi->param(-name => 'SessionID', -value => $session_id);
272      } else {      } else {
273          Trace("Session ID is $session_id.") if T(3);          Trace("Session ID is $session_id.") if T(3);
274      }      }
275        Trace("Computing subclass.") if T(3);
276      # Compute the subclass name.      # Compute the subclass name.
277      my $subClass;      my $subClass;
278      if ($class =~ /SH(.+)$/) {      if ($class =~ /SH(.+)$/) {
# Line 307  Line 283 
283          # process search results.          # process search results.
284          $subClass = 'SearchHelper';          $subClass = 'SearchHelper';
285      }      }
286        Trace("Subclass name is $subClass.") if T(3);
287      # Insure everybody knows we're in Sprout mode.      # Insure everybody knows we're in Sprout mode.
288      $cgi->param(-name => 'SPROUT', -value => 1);      $cgi->param(-name => 'SPROUT', -value => 1);
289      # Generate the form name.      # Generate the form name.
290      my $formName = "$class$formCount";      my $formName = "$class$formCount";
291      $formCount++;      $formCount++;
292        Trace("Creating helper.") if T(3);
293      # Create the shelp object. It contains the query object (with the session ID)      # Create the shelp object. It contains the query object (with the session ID)
294      # as well as an indicator as to whether or not the session is new, plus the      # as well as an indicator as to whether or not the session is new, plus the
295      # class name and a placeholder for the Sprout object.      # class name and a placeholder for the Sprout object.
# Line 325  Line 303 
303                    scriptQueue => [],                    scriptQueue => [],
304                    genomeList => undef,                    genomeList => undef,
305                    genomeParms => [],                    genomeParms => [],
                   filtered => 0,  
                   extraPos => {},  
306                   };                   };
307      # Bless and return it.      # Bless and return it.
308      bless $retVal, $class;      bless $retVal, $class;
# Line 387  Line 363 
363      return ($self->{type} eq 'new');      return ($self->{type} eq 'new');
364  }  }
365    
 =head3 SetExtraPos  
   
 C<< $shelp->SetExtraPos(@columnMap); >>  
   
 Indicate whether the extra columns should be in the front (C<0>) or end (C<1>) columns of the results.  
   
 =over 4  
   
 =item columnMap  
   
 A list of extra columns to display at the end.  
   
 =back  
   
 =cut  
   
 sub SetExtraPos {  
     # Get the parameters.  
     my ($self, @columnMap) = @_;  
     # Convert the column map to a hash.  
     my %map = map { $_ => 1 } @columnMap;  
     # Save a reference to it.  
     $self->{extraPos} = \%map;  
 }  
   
366  =head3 ID  =head3 ID
367    
368  C<< my $sessionID = $shelp->ID(); >>  C<< my $sessionID = $shelp->ID(); >>
# Line 517  Line 468 
468                                -override => 1) .                                -override => 1) .
469                   $cgi->hidden(-name => 'SPROUT',                   $cgi->hidden(-name => 'SPROUT',
470                                -value => 1) .                                -value => 1) .
471                   $cgi->h3($title);                   $cgi->h3("$title" . Hint($self->{class}, "Click here for more information."));
     # If tracing is on, add it to the form.  
     if ($cgi->param('Trace')) {  
         $retVal .= $cgi->hidden(-name => 'Trace',  
                                 -value => $cgi->param('Trace')) .  
                    $cgi->hidden(-name => 'TF',  
                                 -value => ($cgi->param('TF') ? 1 : 0));  
     }  
472      # Put in an anchor tag in case there's a table of contents.      # Put in an anchor tag in case there's a table of contents.
473      my $anchorName = $self->FormName();      my $anchorName = $self->FormName();
474      $retVal .= "<a name=\"$anchorName\"></a>\n";      $retVal .= "<a name=\"$anchorName\"></a>\n";
# Line 606  Line 550 
550    
551  =head3 OpenSession  =head3 OpenSession
552    
553  C<< $shelp->OpenSession(); >>  C<< $shelp->OpenSession($rhelp); >>
554    
555    Set up the session cache file and write out the column headers.
556    This method should not be called until all the columns have
557    been configured, including the extra columns.
558    
559  Set up to open the session cache file for writing. Note we don't actually  =over 4
560  open the file until after we know the column headers.  
561    =item rhelp
562    
563    Result helper for formatting the output. This has the column
564    headers stored in it.
565    
566    =back
567    
568  =cut  =cut
569    
570  sub OpenSession {  sub OpenSession {
571      # Get the parameters.      # Get the parameters.
572      my ($self) = @_;      my ($self, $rhelp) = @_;
573      # Denote we have not yet written out the column headers.      # Insure the result helper is valid.
574      $self->{cols} = undef;      if (! defined($rhelp)) {
575            Confess("No result type specified for $self->{class}.");
576        } elsif(! $rhelp->isa('ResultHelper')) {
577            Confess("Invalid result type specified for $self->{class}.");
578        } else {
579            # Get the column headers and write them out.
580            my $colHdrs = $rhelp->GetColumnHeaders();
581            Trace(scalar(@{$colHdrs}) . " column headers written to output.") if T(3);
582            $self->WriteColumnHeaders(@{$colHdrs});
583        }
584  }  }
585    
586  =head3 GetCacheFileName  =head3 GetCacheFileName
# Line 661  Line 624 
624      my ($self, $type) = @_;      my ($self, $type) = @_;
625      # Compute the file name. Note it gets stuffed in the FIG temporary      # Compute the file name. Note it gets stuffed in the FIG temporary
626      # directory.      # directory.
627      my $retVal = "$FIG_Config::temp/tmp_" . $self->ID() . ".$type";      my $retVal = FIGRules::GetTempFileName(sessionID => $self->ID(), extension => $type);
628      # Return the result.      # Return the result.
629      return $retVal;      return $retVal;
630  }  }
631    
 =head3 PutFeature  
   
 C<< $shelp->PutFeature($fdata); >>  
   
 Store a feature in the result cache. This is the workhorse method for most  
 searches, since the primary data item in the database is features.  
   
 For each feature, there are certain columns that are standard: the feature name, the  
 GBrowse and protein page links, the functional assignment, and so forth. If additional  
 columns are required by a particular search subclass, they should be stored in  
 the feature query object using the B<AddExtraColumns> method. For example, the following  
 code adds columns for essentiality and virulence.  
   
     $fd->AddExtraColumns(essential => $essentialFlag, virulence => $vfactor);  
     $shelp->PutFeature($fd);  
   
 For correct results, all values should be specified for all extra columns in all calls to  
 B<PutFeature>. (In particular, the column header names are computed on the first  
 call.) If a column is to be blank for the current feature, its value can be given  
 as C<undef>.  
   
     if (! $essentialFlag) {  
         $essentialFlag = undef;  
     }  
     $fd->AddExtraColumns(essential => $essentialFlag, virulence => $vfactor);  
     $shelp->PutFeature($fd);  
   
 =over 4  
   
 =item fdata  
   
 B<FeatureData> object containing the current feature data.  
   
 =back  
   
 =cut  
   
 sub PutFeature {  
     # Get the parameters.  
     my ($self, $fd) = @_;  
     # Get the CGI query object.  
     my $cgi = $self->Q();  
     # Get the feature data.  
     my $record = $fd->Feature();  
     my $extraCols = $fd->ExtraCols();  
     # Check for a first-call situation.  
     if (! defined $self->{cols}) {  
         Trace("Setting up the columns.") if T(3);  
         # Tell the user what's happening.  
         $self->PrintLine("Creating output columns.<br />");  
         # Here we need to set up the column information. First we accumulate the extras,  
         # sorted by column name and separate by whether they go in the beginning or the  
         # end.  
         my @xtraNamesFront = ();  
         my @xtraNamesEnd = ();  
         my $xtraPosMap = $self->{extraPos};  
         for my $col (sort keys %{$extraCols}) {  
             if ($xtraPosMap->{$col}) {  
                 push @xtraNamesEnd, "X=$col";  
             } else {  
                 push @xtraNamesFront, "X=$col";  
             }  
         }  
         # Set up the column name array.  
         my @colNames = ();  
         # Put in the extra columns that go in the beginning.  
         push @colNames, @xtraNamesFront;  
         # Add the default columns.  
         push @colNames, $self->DefaultFeatureColumns();  
         # Add any additional columns requested by the feature filter.  
         push @colNames, FeatureQuery::AdditionalColumns($self);  
         # If extras go at the end, put them in here.  
         push @colNames, @xtraNamesEnd;  
         Trace("Full column list determined.") if T(3);  
         # Save the full list.  
         $self->{cols} = \@colNames;  
         # Write out the column names. This also prepares the cache file to receive  
         # output.  
         Trace("Writing column headers.") if T(3);  
         $self->WriteColumnHeaders(@{$self->{cols}});  
         Trace("Column headers written.") if T(3);  
     }  
     # Get the feature ID.  
     my $fid = $fd->FID();  
     # Loop through the column headers, producing the desired data. The first column  
     # is the feature ID. The feature ID does not show up in the output: its purpose  
     # is to help the various output formatters.  
     my @output = ($fid);  
     for my $colName (@{$self->{cols}}) {  
         push @output, $self->FeatureColumnValue($colName, $record, $extraCols);  
     }  
     # Compute the sort key. The sort key usually floats NMPDR organism features to the  
     # top of the return list.  
     my $key = $self->SortKey($fd);  
     # Write the feature data.  
     $self->WriteColumnData($key, @output);  
 }  
   
632  =head3 WriteColumnHeaders  =head3 WriteColumnHeaders
633    
634  C<< $shelp->WriteColumnHeaders(@colNames); >>  C<< $shelp->WriteColumnHeaders(@colNames); >>
# Line 776  Line 641 
641    
642  =item colNames  =item colNames
643    
644  A list of column names in the desired presentation order.  A list of column names in the desired presentation order. For extra columns,
645    the column name is the hash supplied as the column definition.
646    
647  =back  =back
648    
# Line 788  Line 654 
654      # Get the cache file name and open it for output.      # Get the cache file name and open it for output.
655      my $fileName = $self->GetCacheFileName();      my $fileName = $self->GetCacheFileName();
656      my $handle1 = Open(undef, ">$fileName");      my $handle1 = Open(undef, ">$fileName");
657        # Freeze the column headers.
658        my @colHdrs = map { freeze($_) } @colNames;
659      # Write the column headers and close the file.      # Write the column headers and close the file.
660      Tracer::PutLine($handle1, \@colNames);      Tracer::PutLine($handle1, \@colHdrs);
661      close $handle1;      close $handle1;
     Trace("Column headers are: " . join("; ", @colNames) . ".") if T(3);  
662      # Now open the sort pipe and save the file handle. Note how we append the      # Now open the sort pipe and save the file handle. Note how we append the
663      # sorted data to the column header row already in place. The output will      # sorted data to the column header row already in place. The output will
664      # contain a sort key followed by the real columns. The sort key is      # contain a sort key followed by the real columns. The sort key is
# Line 799  Line 666 
666      $self->{fileHandle} = Open(undef, "| sort | cut --fields=2- >>$fileName");      $self->{fileHandle} = Open(undef, "| sort | cut --fields=2- >>$fileName");
667  }  }
668    
669    =head3 ReadColumnHeaders
670    
671    C<< my @colHdrs = $shelp->ReadColumnHeaders($fh); >>
672    
673    Read the column headers from the specified file handle. The column headers are
674    frozen strings intermixed with frozen hash references. The strings represent
675    column names defined in the result helper. The hash references represent the
676    definitions of the extra columns.
677    
678    =over 4
679    
680    =item fh
681    
682    File handle from which the column headers are to be read.
683    
684    =item RETURN
685    
686    Returns a list of the column headers pulled from the specified file's first line.
687    
688    =back
689    
690    =cut
691    
692    sub ReadColumnHeaders {
693        # Get the parameters.
694        my ($self, $fh) = @_;
695        # Read and thaw the columns.
696        my @retVal = map { thaw($_) } Tracer::GetLine($fh);
697        # Return them to the caller.
698        return @retVal;
699    }
700    
701  =head3 WriteColumnData  =head3 WriteColumnData
702    
703  C<< $shelp->WriteColumnData($key, @colValues); >>  C<< $shelp->WriteColumnData($key, @colValues); >>
# Line 850  Line 749 
749      }      }
750  }  }
751    
 =head3 NewSessionID  
   
 C<< my $id = SearchHelpers::NewSessionID(); >>  
   
 Generate a new session ID for the current user.  
   
 =cut  
   
 sub NewSessionID {  
     # Declare the return variable.  
     my $retVal;  
     # Get a digest encoder.  
     my $md5 = Digest::MD5->new();  
     # Add the PID, the IP, and the time stamp. Note that the time stamp is  
     # actually two numbers, and we get them both because we're in list  
     # context.  
     $md5->add($$, $ENV{REMOTE_ADDR}, $ENV{REMOTE_PORT}, gettimeofday());  
     # Hash up all this identifying data.  
     $retVal = $md5->hexdigest();  
     # Return the result.  
     return $retVal;  
 }  
   
752  =head3 OrganismData  =head3 OrganismData
753    
754  C<< my ($orgName, $group) = $shelp->Organism($genomeID); >>  C<< my ($orgName, $group) = $shelp->Organism($genomeID); >>
# Line 906  Line 782 
782      my $cache = $self->{orgs};      my $cache = $self->{orgs};
783      if (exists $cache->{$genomeID}) {      if (exists $cache->{$genomeID}) {
784          ($orgName, $group, $domain) = @{$cache->{$genomeID}};          ($orgName, $group, $domain) = @{$cache->{$genomeID}};
785            Trace("Cached organism $genomeID has group \"$group\".") if T(4);
786      } else {      } else {
787          # Here we have to use the database.          # Here we have to use the database.
788          my $sprout = $self->DB();          my $sprout = $self->DB();
789          my ($genus, $species, $strain, $group, $taxonomy) = $sprout->GetEntityValues('Genome', $genomeID,          my ($genus, $species, $strain, $newGroup, $taxonomy) = $sprout->GetEntityValues('Genome', $genomeID,
790                                                                  ['Genome(genus)', 'Genome(species)',                                                                  ['Genome(genus)', 'Genome(species)',
791                                                                   'Genome(unique-characterization)',                                                                   'Genome(unique-characterization)',
792                                                                   'Genome(primary-group)',                                                                   'Genome(primary-group)',
793                                                                   'Genome(taxonomy)']);                                                                   'Genome(taxonomy)']);
794          # Format and cache the name and display group.          # Format and cache the name and display group.
795          ($orgName, $group, $domain) = $self->SaveOrganismData($group, $genomeID, $genus, $species,          Trace("Caching organism $genomeID with group \"$newGroup\".") if T(4);
796            ($orgName, $group, $domain) = $self->SaveOrganismData($newGroup, $genomeID, $genus, $species,
797                                                                $strain, $taxonomy);                                                                $strain, $taxonomy);
798            Trace("Returning group $group.") if T(4);
799      }      }
800      # Return the result.      # Return the result.
801      return ($orgName, $group, $domain);      return ($orgName, $group, $domain);
# Line 952  Line 831 
831      return $retVal;      return $retVal;
832  }  }
833    
 =head3 FeatureGroup  
   
 C<< my $groupName = $shelp->FeatureGroup($fid); >>  
   
 Return the group name for the specified feature.  
   
 =over 4  
   
 =item fid  
   
 ID of the relevant feature.  
   
 =item RETURN  
   
 Returns the name of the NMPDR group to which the feature belongs, or an empty  
 string if it is not part of an NMPDR group.  
   
 =back  
   
 =cut  
   
 sub FeatureGroup {  
     # Get the parameters.  
     my ($self, $fid) = @_;  
     # Parse the feature ID to get the genome ID.  
     my ($genomeID) = FIGRules::ParseFeatureID($fid);  
     # Get the organism data.  
     my (undef, $retVal) = $self->OrganismData($genomeID);  
     # Return the result.  
     return $retVal;  
 }  
   
 =head3 FeatureName  
   
 C<< my $fidName = $shelp->FeatureName($fid); >>  
   
 Return the display name of the specified feature.  
   
 =over 4  
   
 =item fid  
   
 ID of the feature whose name is desired.  
   
 =item RETURN  
   
 A displayable feature name, consisting of the organism name plus some feature  
 type and location information.  
   
 =back  
   
 =cut  
   
 sub FeatureName {  
     # Get the parameters.  
     my ($self, $fid) = @_;  
     # Declare the return variable  
     my $retVal;  
     # Parse the feature ID.  
     my ($genomeID, $type, $num) = FIGRules::ParseFeatureID($fid);  
     if (! defined $genomeID) {  
         # Here the feature ID has an invalid format.  
         $retVal = "External: $fid";  
     } else {  
         # Here we can get its genome data.  
         $retVal = $self->Organism($genomeID);  
         # Append the FIG ID.  
         $retVal .= " [$fid]";  
     }  
     # Return the result.  
     return $retVal;  
 }  
   
834  =head3 ComputeFASTA  =head3 ComputeFASTA
835    
836  C<< my $fasta = $shelp->ComputeFASTA($desiredType, $sequence, $flankingWidth); >>  C<< my $fasta = $shelp->ComputeFASTA($desiredType, $sequence, $flankingWidth); >>
# Line 1036  Line 842 
842    
843  =item desiredType  =item desiredType
844    
845  C<dna> to return a DNA sequence, C<prot> to return a protein sequence.  C<dna> to return a DNA sequence, C<prot> to return a protein sequence, C<dnaPattern>
846    to return a DNA search pattern, C<protPattern> to return a protein search pattern.
847    
848  =item sequence  =item sequence
849    
# Line 1097  Line 904 
904                  $fastaLabel = $fid;                  $fastaLabel = $fid;
905              }              }
906              # Now proceed according to the sequence type.              # Now proceed according to the sequence type.
907              if ($desiredType eq 'prot') {              if ($desiredType =~ /prot/) {
908                  # We want protein, so get the translation.                  # We want protein, so get the translation.
909                  $fastaData = $sprout->FeatureTranslation($figID);                  $fastaData = $sprout->FeatureTranslation($figID);
910                  Trace(length $fastaData . " characters returned for translation of $fastaLabel.") if T(3);                  Trace(length $fastaData . " characters returned for translation of $fastaLabel.") if T(3);
911              } else {              } elsif ($desiredType =~ /dna/) {
912                  # We want DNA, so get the DNA sequence. This is a two-step process. First, we get the                  # We want DNA, so get the DNA sequence. This is a two-step process. First, we get the
913                  # locations.                  # locations.
914                  my @locList = $sprout->FeatureLocation($figID);                  my @locList = $sprout->FeatureLocation($figID);
# Line 1151  Line 958 
958              Trace("No label found in match to sequence:\n$sequence") if T(4);              Trace("No label found in match to sequence:\n$sequence") if T(4);
959              # Here we have no label, so we create one and use the entire sequence              # Here we have no label, so we create one and use the entire sequence
960              # as data.              # as data.
961              $fastaLabel = "User-specified $desiredType sequence";              $fastaLabel = "$desiredType sequence specified by user";
962              $fastaData = $sequence;              $fastaData = $sequence;
963          }          }
964          # The next step is to clean the junk out of the sequence.          # If we are not doing a pattern search, we need to clean the junk out of the sequence.
965            if ($desiredType !~ /pattern/i) {
966          $fastaData =~ s/\n//g;          $fastaData =~ s/\n//g;
967          $fastaData =~ s/\s+//g;          $fastaData =~ s/\s+//g;
968            }
969          # Finally, verify that it's DNA if we're doing DNA stuff.          # Finally, verify that it's DNA if we're doing DNA stuff.
970          if ($desiredType eq 'dna' && $fastaData =~ /[^agctxn]/i) {          if ($desiredType eq 'dna' && $fastaData =~ /[^agctxn-]/i) {
971              $self->SetMessage("Invalid characters detected. Is the input really a DNA sequence?");              $self->SetMessage("Invalid characters detected. Is the input really a DNA sequence?");
972              $okFlag = 0;              $okFlag = 0;
973          }          }
# Line 1166  Line 975 
975      Trace("FASTA data sequence: $fastaData") if T(4);      Trace("FASTA data sequence: $fastaData") if T(4);
976      # Only proceed if no error was detected.      # Only proceed if no error was detected.
977      if ($okFlag) {      if ($okFlag) {
978            if ($desiredType =~ /pattern/i) {
979                # For a scan, there is no label and no breakup.
980                $retVal = $fastaData;
981            } else {
982          # We need to format the sequence into 60-byte chunks. We use the infamous          # We need to format the sequence into 60-byte chunks. We use the infamous
983          # grep-split trick. The split, because of the presence of the parentheses,          # grep-split trick. The split, because of the presence of the parentheses,
984          # includes the matched delimiters in the output list. The grep strips out          # includes the matched delimiters in the output list. The grep strips out
# Line 1174  Line 987 
987          my @chunks = grep { $_ } split /(.{1,60})/, $fastaData;          my @chunks = grep { $_ } split /(.{1,60})/, $fastaData;
988          $retVal = join("\n", ">$fastaLabel", @chunks, "");          $retVal = join("\n", ">$fastaLabel", @chunks, "");
989      }      }
990        }
991      # Return the result.      # Return the result.
992      return $retVal;      return $retVal;
993  }  }
# Line 1511  Line 1325 
1325          # the text selected automatically.          # the text selected automatically.
1326          my $searchThingName = "${menuName}_SearchThing";          my $searchThingName = "${menuName}_SearchThing";
1327          push @lines, "<br />" .          push @lines, "<br />" .
1328                       "<INPUT type=\"button\" name=\"Search\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectViaSearch($menuName, $searchThingName); $showSelect\" />&nbsp;" .                       "<INPUT type=\"button\" name=\"MacroSearch\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectViaSearch($menuName, $searchThingName); $showSelect\" />&nbsp;" .
1329                       "<INPUT type=\"text\" name=\"$searchThingName\" size=\"30\" />";                       "<INPUT type=\"text\" name=\"$searchThingName\" size=\"30\" />" . Hint("Genome Control",
1330                                                                                                "Enter a genome number, then click the button to the left " .
1331                                                                                                "in order to select the genome with that number. " .
1332                                                                                                "Enter a genus, species, or strain and click the " .
1333                                                                                                "button to select all genomes with that genus, species, " .
1334                                                                                                "or strain name.");
1335          # Next are the buttons to set and clear selections.          # Next are the buttons to set and clear selections.
1336          push @lines, "<br />";          push @lines, "<br />";
1337          push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\"  value=\"Clear All\" onClick=\"clearAll($menuName); $showSelect\" />";          push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\"  value=\"Clear All\" onClick=\"clearAll($menuName); $showSelect\" />";
1338          push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll($menuName); $showSelect\" />";          push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll($menuName); $showSelect\" />";
1339          push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome($menuName, $nmpdrCount, true); $showSelect\" />";          push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome($menuName, $nmpdrCount, true); $showSelect\" />";
1340          push @lines, "<INPUT type=\"button\" name=\"OtherOnly\" class=\"bigButton\" value=\"Select Supporting\" onClick=\"selectSome($menuName, $nmpdrCount, false); $showSelect\" />";          # push @lines, "<INPUT type=\"button\" name=\"OtherOnly\" class=\"bigButton\" value=\"Select Supporting\" onClick=\"selectSome($menuName, $nmpdrCount, false); $showSelect\" />";
1341          # Add the status display, too.          # Add the status display, too.
1342          push @lines, "<DIV id=\"$divID\" class=\"selectStatus\"></DIV>";          push @lines, "<DIV id=\"$divID\" class=\"selectStatus\"></DIV>";
1343          # Queue to update the status display when the form loads. We need to modify the show statement          # Queue to update the status display when the form loads. We need to modify the show statement
# Line 1681  Line 1500 
1500      my $realCaption = (defined $caption ? $caption : 'Go');      my $realCaption = (defined $caption ? $caption : 'Go');
1501      # Get the current page size.      # Get the current page size.
1502      my $pageSize = $cgi->param('PageSize');      my $pageSize = $cgi->param('PageSize');
1503      # Get the incoming external-link flag.      # Get the current feature ID type.
1504      my $aliases = ($cgi->param('ShowAliases') ? 1 : 0);      my $aliasType = $self->GetPreferredAliasType();
1505      # Create the row.      # Create the rows.
1506      my $retVal = $cgi->Tr($cgi->td("Results/Page"),      my $retVal = $cgi->Tr($cgi->td("Identifier Type "),
1507                              $cgi->td({ colspan => 2 },
1508                                       $cgi->popup_menu(-name => 'AliasType',
1509                                                        -values => ['FIG', AliasAnalysis::AliasTypes() ],
1510                                                        -default => $aliasType) .
1511                                       Hint("Identifier type", "Specify how you want gene names to be displayed."))) .
1512                     "\n" .
1513                     $cgi->Tr($cgi->td("Results/Page"),
1514                            $cgi->td($cgi->popup_menu(-name => 'PageSize',                            $cgi->td($cgi->popup_menu(-name => 'PageSize',
1515                                                      -values => [10, 25, 50, 100, 1000],                                                      -values => [10, 25, 50, 100, 1000],
1516                                                      -default => $pageSize)),                                                      -default => $pageSize)),
# Line 1695  Line 1521 
1521      return $retVal;      return $retVal;
1522  }  }
1523    
 =head3 FeatureFilterRows  
   
 C<< my $htmlText = $shelp->FeatureFilterRows(@subset); >>  
   
 This method creates table rows that can be used to filter features. The form  
 values can be used to select features by genome using the B<FeatureQuery>  
 object.  
   
 =over 4  
   
 =item subset  
   
 List of rows to display. The default (C<all>) is to display all rows.  
 C<words> displays the word search box, C<subsys> displays the subsystem  
 selector, and C<options> displays the options row.  
   
 =item RETURN  
   
 Returns the html text for table rows containing the desired feature filtering controls.  
   
 =back  
   
 =cut  
   
 sub FeatureFilterRows {  
     # Get the parameters.  
     my ($self, @subset) = @_;  
     if (@subset == 0 || $subset[0] eq 'all') {  
         @subset = qw(words subsys options);  
     }  
     # Return the result.  
     return FeatureQuery::FilterRows($self, @subset);  
 }  
   
 =head3 GBrowseFeatureURL  
   
 C<< my $url = SearchHelper::GBrowseFeatureURL($sprout, $feat); >>  
   
 Compute the URL required to pull up a Gbrowse page for the the specified feature.  
 In order to do this, we need to pull out the ID of the feature's Genome, its  
 contig ID, and some rough starting and stopping offsets.  
   
 =over 4  
   
 =item sprout  
   
 Sprout object for accessing the database.  
   
 =item feat  
   
 ID of the feature whose Gbrowse URL is desired.  
   
 =item RETURN  
   
 Returns a GET-style URL for the Gbrowse CGI, with parameters specifying the genome  
 ID, contig ID, starting offset, and stopping offset.  
   
 =back  
   
 =cut  
   
 sub GBrowseFeatureURL {  
     # Get the parameters.  
     my ($sprout, $feat) = @_;  
     # Declare the return variable.  
     my $retVal;  
     # Compute the genome ID.  
     my ($genomeID) = FIGRules::ParseFeatureID($feat);  
     # Only proceed if the feature ID produces a valid genome.  
     if ($genomeID) {  
         # Get the feature location string.  
         my $loc = $sprout->FeatureLocation($feat);  
         # Compute the contig, start, and stop points.  
         my($contig, $start, $stop) = BasicLocation::Parse($loc);  
         Trace("Start and stop are ($start,$stop) on contig $contig.") if T(3);  
         # Now we need to do some goofiness to insure that the location is not too  
         # big and that we get some surrounding stuff.  
         my $mid = int(($start + $stop) / 2);  
         my $chunk_len = 20000;  
         my $max_feature = 40000;  
         my $feat_len = abs($stop - $start);  
         if ($feat_len > $chunk_len) {  
             if ($feat_len > $max_feature) {  
                 $chunk_len = $max_feature;  
             } else {  
                 $chunk_len = $feat_len + 100;  
             }  
         }  
         my($show_start, $show_stop);  
         if ($chunk_len == $max_feature) {  
             $show_start = $start - 300;  
         } else {  
             $show_start = $mid - int($chunk_len / 2);  
         }  
         if ($show_start < 1) {  
             $show_start = 1;  
         }  
         $show_stop = $show_start + $chunk_len - 1;  
         my $clen = $sprout->ContigLength($contig);  
         if ($show_stop > $clen) {  
             $show_stop = $clen;  
         }  
         my $seg_id = $contig;  
         $seg_id =~ s/:/--/g;  
         Trace("Show limits are ($show_start,$show_stop) in genome $genomeID with ref $seg_id.") if T(3);  
         # Assemble all the pieces.  
         $retVal = "gbrowse.cgi/GB_$genomeID?ref=$seg_id;start=$show_start;stop=$show_stop";  
     }  
     # Return the result.  
     return $retVal;  
 }  
   
1524  =head3 GetGenomes  =head3 GetGenomes
1525    
1526  C<< my @genomeList = $shelp->GetGenomes($parmName); >>  C<< my @genomeList = $shelp->GetGenomes($parmName); >>
# Line 1859  Line 1573 
1573  server. The help text for a specific search is taken from a file named  server. The help text for a specific search is taken from a file named
1574  C<SearchHelp_>I<class>C<.inc> in the template directory C<$FIG_Config::template_url>.  C<SearchHelp_>I<class>C<.inc> in the template directory C<$FIG_Config::template_url>.
1575  There are also three standard help files: C<SearchHelp1_Filtering.inc> describes the  There are also three standard help files: C<SearchHelp1_Filtering.inc> describes the
1576  feature filtering performed by the B<FeatureQuery> object, C<SearchHelp1_GenomeControl.inc>  feature filtering performed by the B<RHFeatures> object, C<SearchHelp1_GenomeControl.inc>
1577  describes how to use a multiple-selection genome control, and C<SearchHelp1_Standard.inc>  describes how to use a multiple-selection genome control, and C<SearchHelp1_Standard.inc>
1578  describes the standard controls for a search, such as page size, URL display, and  describes the standard controls for a search, such as page size, URL display, and
1579  external alias display.  external alias display.
# Line 1985  Line 1699 
1699      return $retVal;      return $retVal;
1700  }  }
1701    
 =head3 GetRunTimeValue  
   
 C<< my $htmlText = $shelp->GetRunTimeValue($text); >>  
   
 Compute a run-time column value.  
   
 =over 4  
   
 =item text  
   
 The run-time column text. It consists of 2 percent signs, a column type, an equal  
 sign, and the data for the current row.  
   
 =item RETURN  
   
 Returns the fully-formatted HTML text to go into the current column of the current row.  
   
 =back  
   
 =cut  
   
 sub GetRunTimeValue {  
     # Get the parameters.  
     my ($self, $text) = @_;  
     # Declare the return variable.  
     my $retVal;  
     # Parse the incoming text.  
     if ($text =~ /^%%([^=]+)=(.*)$/) {  
         $retVal = $self->RunTimeColumns($1, $2);  
     } else {  
         Confess("Invalid run-time column string \"$text\" encountered in session file.");  
     }  
     # Return the result.  
     return $retVal;  
 }  
   
1702  =head3 AdvancedClassList  =head3 AdvancedClassList
1703    
1704  C<< my @classes = SearchHelper::AdvancedClassList(); >>  C<< my @classes = SearchHelper::AdvancedClassList(); >>
# Line 2028  Line 1706 
1706  Return a list of advanced class names. This list is used to generate the directory  Return a list of advanced class names. This list is used to generate the directory
1707  of available searches on the search page.  of available searches on the search page.
1708    
1709  We use the %INC variable to accomplish this.  We do a file search to accomplish this, but to pull it off we need to look at %INC.
1710    
1711  =cut  =cut
1712    
1713  sub AdvancedClassList {  sub AdvancedClassList {
1714      my @retVal = map { $_ =~ /^SH(\w+)\.pm/; $1 } grep { $_ =~ /^SH/ } keys %INC;      # Determine the search helper module directory.
1715        my $libDirectory = $INC{'SearchHelper.pm'};
1716        $libDirectory =~ s/SearchHelper\.pm//;
1717        # Read it, keeping only the helper modules.
1718        my @modules = grep { /^SH\w+\.pm/ } Tracer::OpenDir($libDirectory, 0);
1719        # Convert the file names to search types.
1720        my @retVal = map { $_ =~ /^SH(\w+)\.pm/; $1 } @modules;
1721        # Return the result in alphabetical order.
1722      return sort @retVal;      return sort @retVal;
1723  }  }
1724    
# Line 2409  Line 2094 
2094      return $retVal;      return $retVal;
2095  }  }
2096    
   
2097  =head3 PrintLine  =head3 PrintLine
2098    
2099  C<< $shelp->PrintLine($message); >>  C<< $shelp->PrintLine($message); >>
# Line 2434  Line 2118 
2118      print "$message\n";      print "$message\n";
2119  }  }
2120    
2121  =head2 Feature Column Methods  =head3 GetHelper
   
 The methods in this section manage feature column data. If you want to provide the  
 capability to include new types of data in feature columns, then all the changes  
 are made to this section of the source file. Technically, this should be implemented  
 using object-oriented methods, but this is simpler for non-programmers to maintain.  
 To add a new column of feature data, you must first give it a name. For example,  
 the name for the protein page link column is C<protlink>. If the column is to appear  
 in the default list of feature columns, add it to the list returned by  
 L</DefaultFeatureColumns>. Then add code to produce the column title to  
 L</FeatureColumnTitle> and code to produce its value to L</FeatureColumnValue>. If the  
 feature column should be excluded from downloads, add it to the C<FeatureColumnSkip>  
 hash. Everything else will happen automatically.  
   
 There is a special column name syntax for extra columns (that is, nonstandard  
 feature columns). If the column name begins with C<X=>, then it is presumed to be  
 an extra column. The column title is the text after the C<X=>, and its value is  
 pulled from the extra column hash.  
   
 =cut  
   
 # This hash is used to determine which columns should not be included in downloads.  
 my %FeatureColumnSkip = map { $_ => 1 } qw(gblink viewerlink protlink);  
   
 =head3 DefaultFeatureColumns  
   
 C<< my @colNames = $shelp->DefaultFeatureColumns(); >>  
   
 Return a list of the default feature column identifiers. These identifiers can  
 be passed to L</FeatureColumnTitle> and L</FeatureColumnValue> in order to  
 produce the column titles and row values.  
   
 =cut  
   
 sub DefaultFeatureColumns {  
     # Get the parameters.  
     my ($self) = @_;  
     # Return the result.  
     return qw(orgName function gblink protlink);  
 }  
   
 =head3 FeatureColumnTitle  
   
 C<< my $title = $shelp->FeatureColumnTitle($colName); >>  
   
 Return the column heading title to be used for the specified feature column.  
   
 =over 4  
   
 =item name  
   
 Name of the desired feature column.  
   
 =item RETURN  
   
 Returns the title to be used as the column header for the named feature column.  
   
 =back  
   
 =cut  
   
 sub FeatureColumnTitle {  
     # Get the parameters.  
     my ($self, $colName) = @_;  
     # Declare the return variable. We default to a blank column name.  
     my $retVal = "&nbsp;";  
     # Process the column name.  
     if ($colName =~ /^X=(.+)$/) {  
         # Here we have an extra column.  
         $retVal = $1;  
     } elsif ($colName eq 'alias') {  
         $retVal = "External Aliases";  
     } elsif ($colName eq 'fid') {  
         $retVal = "FIG ID";  
     } elsif ($colName eq 'function') {  
         $retVal = "Functional Assignment";  
     } elsif ($colName eq 'gblink') {  
         $retVal = "GBrowse";  
     } elsif ($colName eq 'group') {  
         $retVal = "NMDPR Group";  
     } elsif ($colName =~ /^keyword:(.+)$/) {  
         $retVal = ucfirst $1;  
     } elsif ($colName eq 'orgName') {  
         $retVal = "Organism and Gene ID";  
     } elsif ($colName eq 'protlink') {  
         $retVal = "NMPDR Protein Page";  
     } elsif ($colName eq 'viewerlink') {  
         $retVal = "Annotation Page";  
     } elsif ($colName eq 'subsystem') {  
         $retVal = "Subsystems";  
     } elsif ($colName eq 'pdb') {  
         $retVal = "Best PDB Match";  
     }  
     # Return the result.  
     return $retVal;  
 }  
   
 =head3 FeatureColumnDownload  
   
 C<< my $keep = $shelp->FeatureColumnDownload($colName); >>  
   
 Return TRUE if the named feature column is to be kept when downloading, else FALSE.  
   
 =over 4  
   
 =item colName  
   
 Name of the relevant feature column.  
   
 =item RETURN  
   
 Return TRUE if the named column should be kept while downloading, else FALSE. In general,  
 FALSE is returned if the column generates a button, image, or other purely-HTML value.  
   
 =back  
   
 =cut  
   
 sub FeatureColumnDownload {  
     # Get the parameters.  
     my ($self, $colName) = @_;  
     # Return the determination. We download the column if it's not in the skip-hash.  
     # Note we return 0 and 1 instead of 1 and undef because it simplifies some tracing.  
     return (exists $FeatureColumnSkip{$colName} ? 0 : 1);  
 }  
   
   
 =head3 FeatureColumnValue  
2122    
2123  C<< my $value = $shelp->FeatureColumnValue($colName, $fid, \%extraCols); >>  C<< my $shelp = SearchHelper::GetHelper($parm, $type => $className); >>
2124    
2125  Return the value to be displayed in the specified feature column.  Return a helper object with the given class name. If no such class exists, an
2126    error will be thrown.
2127    
2128  =over 4  =over 4
2129    
2130  =item colName  =item parm
   
 Name of the column to be displayed.  
   
 =item record  
   
 ERDBObject record for the feature being displayed in the current row.  
   
 =item extraCols  
   
 Reference to a hash of extra column names to values. If the incoming column name  
 begins with C<X=>, its value will be taken from this hash.  
   
 =item RETURN  
2131    
2132  Returns the HTML to be displayed in the named column for the specified feature.  Parameter to pass to the constructor. This is a CGI object for a search helper
2133    and a search helper object for the result helper.
 =back  
   
 =cut  
   
 sub FeatureColumnValue {  
     # Get the parameters.  
     my ($self, $colName, $record, $extraCols) = @_;  
     # Get the sprout and CGI objects.  
     my $cgi = $self->Q();  
     my $sprout = $self->DB();  
     # Get the feature ID.  
     my ($fid) = $record->Value('Feature(id)');  
     # Declare the return variable. Denote that we default to a non-breaking space,  
     # which will translate to an empty table cell (rather than a table cell with no  
     # interior, which is what you get for a null string).  
     my $retVal = "&nbsp;";  
     # Process according to the column name.  
     if ($colName =~ /^X=(.+)$/) {  
         # Here we have an extra column. Only update if the value exists. Note that  
         # a value of C<undef> is treated as a non-existent value, because the  
         # caller may have put "colName => undef" in the "PutFeature" call in order  
         # to insure we know the extra column exists.  
         if (defined $extraCols->{$1}) {  
             $retVal = $extraCols->{$1};  
         }  
     } elsif ($colName eq 'alias') {  
         # In this case, the user wants a list of external aliases for the feature.  
         # These are very expensive, so we compute them when the row is displayed.  
         # To do the computation, we need to know the favored alias type and the  
         # feature ID.  
         my $favored = $cgi->param("FavoredAlias") || "fig";  
         $retVal = "%%alias=$fid,$favored";  
     } elsif ($colName eq 'fid') {  
         # Here we have the raw feature ID. We hyperlink it to the protein page.  
         $retVal = HTML::set_prot_links($fid);  
     } elsif ($colName eq 'function') {  
         # The functional assignment is just a matter of getting some text.  
         ($retVal) = $record->Value('Feature(assignment)');  
     } elsif ($colName eq 'gblink') {  
         # Here we want a link to the GBrowse page using the official GBrowse button.  
         $retVal = FakeButton('GBrowse', "GetGBrowse.cgi", undef,  
                           fid => $fid);  
     } elsif ($colName eq 'group') {  
         # Get the NMPDR group name.  
         my (undef, $group) = $self->OrganismData($fid);  
         # Dress it with a URL to the group's main page.  
         my $nurl = $sprout->GroupPageName($group);  
         $retVal = $cgi->a({ href => $nurl, title => "$group summary" },  
                           $group);  
     } elsif ($colName =~ /^keyword:(.+)$/) {  
         # Here we want keyword-related values. This is also expensive, so  
         # we compute them when the row is displayed.  
         $retVal = "%%$colName=$fid";  
     } elsif ($colName eq 'orgName') {  
         # Here we want the formatted organism name and feature number.  
         $retVal = $self->FeatureName($fid);  
     } elsif ($colName eq 'protlink') {  
         # Here we want a link to the protein page using the official NMPDR button.  
         $retVal = FakeButton('NMPDR', "protein.cgi", undef,  
                           prot => $fid, SPROUT => 1, new_framework => 0,  
                           user => '');  
     } elsif ($colName eq 'viewerlink') {  
         # Here we want a link to the SEED viewer page using the official viewer button.  
         $retVal = FakeButton('Annotation', "index.cgi", undef,  
                              action => 'ShowAnnotation', prot => $fid);  
     } elsif ($colName eq 'subsystem') {  
         # Another run-time column: subsystem list.  
         $retVal = "%%subsystem=$fid";  
     } elsif ($colName eq 'pdb') {  
         $retVal = "%%pdb=$fid";  
     }  
     # Return the result.  
     return $retVal;  
 }  
   
 =head3 RunTimeColumns  
   
 C<< my $htmlText = $shelp->RunTimeColumns($type, $text); >>  
   
 Return the HTML text for a run-time column. Run-time columns are evaluated when the  
 list is displayed, rather than when it is generated.  
   
 =over 4  
2134    
2135  =item type  =item type
2136    
2137  Type of column.  Type of helper: C<RH> for a result helper and C<SH> for a search helper.
2138    
2139  =item text  =item className
2140    
2141  Data relevant to this row of the column.  Class name for the helper object, without the preceding C<SH> or C<RH>. This is
2142    identical to what the script expects for the C<Class> or C<ResultType> parameter.
2143    
2144  =item RETURN  =item RETURN
2145    
2146  Returns the fully-formatted HTML text to go in the specified column.  Returns a helper object for the specified class.
2147    
2148  =back  =back
2149    
2150  =cut  =cut
2151    
2152  sub RunTimeColumns {  sub GetHelper {
2153      # Get the parameters.      # Get the parameters.
2154      my ($self, $type, $text) = @_;      my ($parm, $type, $className) = @_;
2155      # Declare the return variable.      # Declare the return variable.
2156      my $retVal = "";      my $retVal;
2157      # Get the Sprout and CGI objects.      # Try to create the helper.
2158      my $sprout = $self->DB();      eval {
2159      my $cgi = $self->Q();          # Load it into memory. If it's already there nothing will happen here.
2160      Trace("Runtime column $type with text \"$text\" found.") if T(4);          my $realName = "$type$className";
2161      # Separate the text into a type and data.          Trace("Requiring helper $realName.") if T(3);
2162      if ($type eq 'alias') {          require "$realName.pm";
2163          # Here the caller wants external alias links for a feature. The text          Trace("Constructing helper object.") if T(3);
2164          # parameter for computing the alias is the feature ID followed by          # Construct the object.
2165          # the favored alias type.          $retVal = eval("$realName->new(\$parm)");
2166          my ($fid, $favored) = split /\s*,\s*/, $text;          # Commit suicide if it didn't work.
2167          # The complicated part is we have to hyperlink them and handle the          if (! defined $retVal) {
2168          # favorites. First, get the aliases.              die "Could not find a $type handler of type $className.";
         Trace("Generating aliases for feature $fid.") if T(4);  
         my @aliases = sort $sprout->FeatureAliases($fid);  
         # Only proceed if we found some.  
         if (@aliases) {  
             # Split the aliases into favored and unfavored.  
             my @favored = ();  
             my @unfavored = ();  
             for my $alias (@aliases) {  
                 # Use substr instead of pattern match because $favored is specified by the user  
                 # and we don't want him to put funny meta-characters in there.  
                 if (substr($alias, 0, length($favored)) eq $favored) {  
                     push @favored, $alias;  
                 } else {  
                     push @unfavored, $alias;  
                 }  
             }  
             # Rejoin the aliases into a comma-delimited list, with the favored ones first.  
             my $aliasList = join(", ", @favored, @unfavored);  
             # Ask the HTML processor to hyperlink them.  
             $retVal = HTML::set_prot_links($cgi, $aliasList);  
         }  
     } elsif ($type eq 'subsystem') {  
         # Here the caller wants the subsystems in which this feature participates.  
         # The text is the feature ID. We will list the subsystem names with links  
         # to the subsystem's summary page.  
         my $fid = $text;  
         # Get the subsystems.  
         Trace("Generating subsystems for feature $fid.") if T(4);  
         my %subs = $sprout->SubsystemsOf($fid);  
         # Extract the subsystem names.  
         my @names = map { HTML::sub_link($cgi, $_) } sort keys %subs;  
         # String them into a list.  
         $retVal = join(", ", @names);  
     } elsif ($type =~ /^keyword:(.+)$/) {  
         # Here the caller wants the value of the named keyword. The text is the  
         # feature ID.  
         my $keywordName = $1;  
         my $fid = $text;  
         # Get the attribute values.  
         Trace("Getting $keywordName values for feature $fid.") if T(4);  
         my @values = $sprout->GetFlat(['Feature'], "Feature(id) = ?", [$fid],  
                                       "Feature($keywordName)");  
         # String them into a list.  
         $retVal = join(", ", @values);  
     } elsif ($type eq 'pdb') {  
         # Here the caller wants the best PDB match to this feature. The text  
         # is the feature ID. We will display the PDB with a link to the  
         # PDB page along with the match score. If there are docking results we  
         # will display a link to the docking result search.  
         my $fid = $text;  
         # Ask for the best PDB.  
         my ($bestPDB) = $sprout->GetAll(['IsProteinForFeature', 'PDB'],  
                                         "IsProteinForFeature(from-link) = ? ORDER BY IsProteinForFeature(score) LIMIT 1",  
                                         [$fid], ['PDB(id)', 'PDB(docking-count)', 'IsProteinForFeature(score)']);  
         # Only proceed if there is a PDB.  
         if ($bestPDB) {  
             my ($pdbID, $dockingCount, $score) = @{$bestPDB};  
             # Convert the PDB ID to a hyperlink.  
             my $pdbLink = SHDrugSearch::PDBLink($cgi, $pdbID);  
             # Append the score.  
             $retVal = "$pdbLink ($score)";  
             # If there are docking results, append a docking results link.  
             if ($dockingCount > 0) {  
                 my $dockString = "$dockingCount docking results";  
                 my $dockLink = $cgi->a({ href =>  $cgi->url() . "?Class=DrugSearch;PDB=$pdbID;NoForm=1",  
                                          alt =>   "View computed docking results for $pdbID",  
                                          title => "View computed docking results for $pdbID",  
                                          target => "_blank"},  
                                        $dockString);  
             }  
2169          }          }
2170        };
2171        # Check for errors.
2172        if ($@) {
2173            Confess("Error retrieving $type$className: $@");
2174      }      }
2175      # Return the result.      # Return the result.
2176      return $retVal;      return $retVal;
# Line 2781  Line 2178 
2178    
2179  =head3 SaveOrganismData  =head3 SaveOrganismData
2180    
2181  C<< my ($name, $displayGroup) = $shelp->SaveOrganismData($group, $genomeID, $genus, $species, $strain, $taxonomy); >>  C<< my ($name, $displayGroup, $domain) = $shelp->SaveOrganismData($group, $genomeID, $genus, $species, $strain, $taxonomy); >>
2182    
2183  Format the name of an organism and the display version of its group name. The incoming  Format the name of an organism and the display version of its group name. The incoming
2184  data should be the relevant fields from the B<Genome> record in the database. The  data should be the relevant fields from the B<Genome> record in the database. The
# Line 2842  Line 2239 
2239          # Compute the display group. This is currently the same as the incoming group          # Compute the display group. This is currently the same as the incoming group
2240          # name unless it's the supporting group, which is nulled out.          # name unless it's the supporting group, which is nulled out.
2241          $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);          $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
2242            Trace("Group = $displayGroup, translated from \"$group\".") if T(4);
2243      }      }
2244      # Compute the domain from the taxonomy.      # Compute the domain from the taxonomy.
2245      my ($domain) = split /\s*;\s*/, $taxonomy, 2;      my ($domain) = split /\s*;\s*/, $taxonomy, 2;
# Line 2902  Line 2300 
2300      return $retVal;      return $retVal;
2301  }  }
2302    
 =head3 FakeButton  
   
 C<< my $html = SearchHelper::FakeButton($caption, $url, $target, %parms); >>  
   
 Create a fake button that hyperlinks to the specified URL with the specified parameters.  
 Unlike a real button, this one won't visibly click, but it will take the user to the  
 correct place.  
   
 The parameters of this method are deliberately identical to L</Formlet> so that we  
 can switch easily from real buttons to fake ones in the code.  
   
 =over 4  
   
 =item caption  
   
 Caption to be put on the button.  
   
 =item url  
   
 URL for the target page or script.  
   
 =item target  
   
 Frame or target in which the new page should appear. If C<undef> is specified,  
 the default target will be used.  
   
 =item parms  
   
 Hash containing the parameter names as keys and the parameter values as values.  
 These will be appended to the URL.  
   
 =back  
   
 =cut  
   
 sub FakeButton {  
     # Get the parameters.  
     my ($caption, $url, $target, %parms) = @_;  
     # Declare the return variable.  
     my $retVal;  
     # Compute the target URL.  
     my $targetUrl = "$url?" . join(";", map { "$_=" . uri_escape($parms{$_}) } keys %parms);  
     # Compute the target-frame HTML.  
     my $targetHtml = ($target ? " target=\"$target\"" : "");  
     # Assemble the result.  
     return "<a href=\"$targetUrl\" $targetHtml><div class=\"button2 button\">$caption</div></a>";  
 }  
   
 =head3 Formlet  
   
 C<< my $html = SearchHelper::Formlet($caption, $url, $target, %parms); >>  
   
 Create a mini-form that posts to the specified URL with the specified parameters. The  
 parameters will be stored in hidden fields, and the form's only visible control will  
 be a submit button with the specified caption.  
   
 Note that we don't use B<CGI.pm> services here because they generate forms with extra characters  
 and tags that we don't want to deal with.  
   
 =over 4  
   
 =item caption  
   
 Caption to be put on the form button.  
   
 =item url  
   
 URL to be put in the form's action parameter.  
   
 =item target  
   
 Frame or target in which the form results should appear. If C<undef> is specified,  
 the default target will be used.  
   
 =item parms  
   
 Hash containing the parameter names as keys and the parameter values as values.  
   
 =back  
   
 =cut  
   
 sub Formlet {  
     # Get the parameters.  
     my ($caption, $url, $target, %parms) = @_;  
     # Compute the target HTML.  
     my $targetHtml = ($target ? " target=\"$target\"" : "");  
     # Start the form.  
     my $retVal = "<form method=\"POST\" action=\"$url\"$target>";  
     # Add the parameters.  
     for my $parm (keys %parms) {  
         $retVal .= "<input type=\"hidden\" name=\"$parm\" value=\"$parms{$parm}\" />";  
     }  
     # Put in the button.  
     $retVal .= "<input type=\"submit\" name=\"submit\" value=\"$caption\" class=\"button\" />";  
     # Close the form.  
     $retVal .= "</form>";  
     # Return the result.  
     return $retVal;  
 }  
   
2303  =head3 TuningParameters  =head3 TuningParameters
2304    
2305  C<< my $options = $shelp->TuningParameters(%parmHash); >>  C<< my $options = $shelp->TuningParameters(%parmHash); >>
# Line 3046  Line 2343 
2343      return $retVal;      return $retVal;
2344  }  }
2345    
2346    =head3 GetPreferredAliasType
2347    
2348    C<< my $type = $shelp->GetPreferredAliasType(); >>
2349    
2350    Return the preferred alias type for the current session. This information is stored
2351    in the C<AliasType> parameter of the CGI query object, and the default is C<FIG>
2352    (which indicates the FIG ID).
2353    
2354    =cut
2355    
2356    sub GetPreferredAliasType {
2357        # Get the parameters.
2358        my ($self) = @_;
2359        # Determine the preferred type.
2360        my $cgi = $self->Q();
2361        my $retVal = $cgi->param('AliasType') || 'FIG';
2362        # Return it.
2363        return $retVal;
2364    }
2365    
2366  =head2 Virtual Methods  =head2 Virtual Methods
2367    
2368  =head3 Form  =head3 Form
# Line 3063  Line 2380 
2380  returned. If the search parameters are invalid, a result count of C<undef> will be  returned. If the search parameters are invalid, a result count of C<undef> will be
2381  returned and a result message will be stored in this object describing the problem.  returned and a result message will be stored in this object describing the problem.
2382    
2383    =cut
2384    
2385    sub Find {
2386        # Get the parameters.
2387        my ($self) = @_;
2388        $self->Message("Call to pure virtual Find method in helper of type " . ref($self) . ".");
2389        return undef;
2390    }
2391    
2392  =head3 Description  =head3 Description
2393    
2394  C<< my $htmlText = $shelp->Description(); >>  C<< my $htmlText = $shelp->Description(); >>
# Line 3071  Line 2397 
2397  on the main search tools page. It may contain HTML, but it should be character-level,  on the main search tools page. It may contain HTML, but it should be character-level,
2398  not block-level, since the description is going to appear in a list.  not block-level, since the description is going to appear in a list.
2399    
 =head3 SortKey  
   
 C<< my $key = $shelp->SortKey($fdata); >>  
   
 Return the sort key for the specified feature data. The default is to sort by feature name,  
 floating NMPDR organisms to the top. If a full-text search is used, then the default  
 sort is by relevance followed by feature name. This sort may be overridden by the  
 search class to provide fancier functionality. This method is called by  
 B<PutFeature>, so it is only used for feature searches. A non-feature search  
 would presumably have its own sort logic.  
   
 =over 4  
   
 =item record  
   
 The C<FeatureData> containing the current feature.  
   
 =item RETURN  
   
 Returns a key field that can be used to sort this row in among the results.  
   
 =back  
   
2400  =cut  =cut
2401    
2402  sub SortKey {  sub Description {
2403      # Get the parameters.      # Get the parameters.
2404      my ($self, $fdata) = @_;      my ($self) = @_;
2405      # Get the feature ID from the record.      $self->Message("Call to pure virtual Description method in helper of type " . ref($self) . ".");
2406      my $fid = $fdata->FID();      return "Unknown search type";
     # Get the group from the feature ID.  
     my $group = $self->FeatureGroup($fid);  
     # Ask the feature query object to form the sort key.  
     my $retVal = $fdata->SortKey($self, $group);  
     # Return the result.  
     return $retVal;  
2407  }  }
2408    
2409  =head3 SearchTitle  =head3 SearchTitle
# Line 3123  Line 2420 
2420      # Get the parameters.      # Get the parameters.
2421      my ($self) = @_;      my ($self) = @_;
2422      # Declare the return variable.      # Declare the return variable.
2423      my $retVal;      my $retVal = "";
2424      # Return it.      # Return it.
2425      return $retVal;      return $retVal;
2426  }  }
2427    
2428  =head3 DownloadFormatAvailable  =head3 DefaultColumns
2429    
2430  C<< my $okFlag = $shelp->DownloadFormatAvailable($format); >>  C<< $shelp->DefaultColumns($rhelp); >>
2431    
2432  This method returns TRUE if a specified download format is legal for this type of search  Store the default columns in the result helper. The default action is just to ask
2433  and FALSE otherwise. For any feature-based search, there is no need to override this  the result helper for its default columns, but this may be changed by overriding
2434  method.  this method.
2435    
2436  =over 4  =over 4
2437    
2438  =item format  =item rhelp
   
 Download format type code.  
   
 =item RETURN  
2439    
2440  Returns TRUE if the download format is legal for this search and FALSE otherwise.  Result helper object in which the column list should be stored.
2441    
2442  =back  =back
2443    
2444  =cut  =cut
2445    
2446  sub DownloadFormatAvailable {  sub DefaultColumns {
2447      # Get the parameters.      # Get the parameters.
2448      my ($self, $format) = @_;      my ($self, $rhelp) = @_;
2449      # Declare the return variable.      # Get the default columns from the result helper.
2450      my $retVal = 1;      my @cols = $rhelp->DefaultResultColumns();
2451      # Return the result.      # Store them back.
2452      return $retVal;      $rhelp->SetColumns(@cols);
2453  }  }
2454    
2455  =head3 ColumnTitle  =head3 Hint
2456    
2457  C<< my $title = $shelp->ColumnTitle($colName); >>  C<< my $htmlText = SearchHelper::Hint($wikiPage, $hintText); >>
2458    
2459  Return the column heading title to be used for the specified column name. The  Return the HTML for a small question mark that displays the specified hint text when it is clicked.
2460  default implementation is to simply call L</FeatureColumnTitle>.  This HTML can be put in forms to provide a useful hinting mechanism.
2461    
2462  =over 4  =over 4
2463    
2464  =item colName  =item wikiPage
2465    
2466    Name of the wiki page to be popped up when the hint maek is clicked.
2467    
2468    =item hintText
2469    
2470  Name of the desired column.  Text to display for the hint. It is raw html, but may not contain any double quotes.
2471    
2472  =item RETURN  =item RETURN
2473    
2474  Returns the title to be used as the column header for the named column.  Returns the html for the hint facility. The resulting html shows a small button-like thing that
2475    uses the standard FIG popup technology.
2476    
2477  =back  =back
2478    
2479  =cut  =cut
2480    
2481  sub ColumnTitle {  sub Hint {
2482      my ($self, $colName) = @_;      # Get the parameters.
2483      return $self->FeatureColumnTitle($colName);      my ($wikiPage, $hintText) = @_;
2484        # Escape the single quotes in the hint text.
2485        my $quotedText = $hintText;
2486        $quotedText =~ s/'/\\'/g;
2487        # Convert the wiki page name to a URL.
2488        my $wikiURL = ucfirst $wikiPage;
2489        $wikiURL =~ s/ /_/g;
2490        $wikiURL = "../wiki/index.php/$wikiURL";
2491        # Create the html.
2492        my $retVal = "&nbsp;<input type=\"button\" class=\"hintbutton\" onMouseOver=\"javascript:if (!this.tooltip) { " .
2493                     "this.tooltip = new Popup_Tooltip(this, 'Search Hint', '$quotedText', '', 1); this.tooltip.addHandler(); } " .
2494                     "return false;\" value=\"?\" onClick=\"javascript:window.open('$wikiURL', 'nmpdrHelp');\" />";
2495        # Return it.
2496        return $retVal;
2497  }  }
2498    
2499    

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.37

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3