[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.1, Tue Sep 26 13:46:03 2006 UTC revision 1.20, Tue Nov 21 17:23:41 2006 UTC
# Line 16  Line 16 
16      use FIGRules;      use FIGRules;
17      use HTML;      use HTML;
18      use BasicLocation;      use BasicLocation;
19        use FeatureQuery;
20        use URI::Escape;
21        use PageBuilder;
22    
23  =head1 Search Helper Base Class  =head1 Search Helper Base Class
24    
# Line 72  Line 75 
75    
76  List of JavaScript statements to be executed after the form is closed.  List of JavaScript statements to be executed after the form is closed.
77    
78    =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    =back
92    
93    =head2 Adding a new Search Tool
94    
95    To add a new search tool to the system, you must
96    
97    =over 4
98    
99    =item 1
100    
101    Choose a class name for your search tool.
102    
103    =item 2
104    
105    Create a new subclass of this object and implement each of the virtual methods. The
106    name of the subclass must be C<SH>I<className>.
107    
108    =item 3
109    
110    Create an include file among the web server pages that describes how to use
111    the search tool. The include file must be in the B<includes> directory, and
112    its name must be C<SearchHelp_>I<className>C<.inc>.
113    
114    =item 4
115    
116    In the C<SearchSkeleton.cgi> script and add a C<use> statement for your search tool.
117    
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    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    
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  =back
178    
179    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    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                # Close the session file.
221                $self->CloseSession();
222            }
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    are variations on the above theme. For example, you could eschew feature filtering
229    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  =cut  =cut
244    
245  # This counter is used to insure every form on the page has a unique name.  # This counter is used to insure every form on the page has a unique name.
246  my $formCount = 0;  my $formCount = 0;
247    # This counter is used to generate unique DIV IDs.
248    my $divCount = 0;
249    
250  =head2 Public Methods  =head2 Public Methods
251    
# Line 89  Line 257 
257    
258  =over 4  =over 4
259    
260  =item query  =item cgi
261    
262  The CGI query object for the current script.  The CGI query object for the current script.
263    
# Line 99  Line 267 
267    
268  sub new {  sub new {
269      # Get the parameters.      # Get the parameters.
270      my ($class, $query) = @_;      my ($class, $cgi) = @_;
271      # Check for a session ID.      # Check for a session ID.
272      my $session_id = $query->param("SessionID");      my $session_id = $cgi->param("SessionID");
273      my $type = "old";      my $type = "old";
274      if (! $session_id) {      if (! $session_id) {
275          # 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
276          # store it in the query object.          # store it in the query object.
277          $session_id = NewSessionID();          $session_id = NewSessionID();
278          $type = "new";          $type = "new";
279          $query->param(-name => 'SessionID', -value => $session_id);          $cgi->param(-name => 'SessionID', -value => $session_id);
280      }      }
281      # Compute the subclass name.      # Compute the subclass name.
282      $class =~ /SH(.+)$/;      my $subClass;
283      my $subClass = $1;      if ($class =~ /SH(.+)$/) {
284      # Create the Sprout object.          # Here we have a real search class.
285      my $sprout = SFXlate->new_sprout_only();          $subClass = $1;
286        } else {
287            # Here we have a bare class. The bare class cannot search, but it can
288            # process search results.
289            $subClass = 'SearchHelper';
290        }
291      # Insure everybody knows we're in Sprout mode.      # Insure everybody knows we're in Sprout mode.
292      $query->param(-name => 'SPROUT', -value => 1);      $cgi->param(-name => 'SPROUT', -value => 1);
293      # Generate the form name.      # Generate the form name.
294      my $formName = "$class$formCount";      my $formName = "$class$formCount";
295      $formCount++;      $formCount++;
296      # 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)
297      # 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
298      # class name and the Sprout object.      # class name and a placeholder for the Sprout object.
299      my $retVal = {      my $retVal = {
300                    query => $query,                    query => $cgi,
301                    type => $type,                    type => $type,
302                    class => $subClass,                    class => $subClass,
303                    sprout => $sprout,                    sprout => undef,
304                    orgs => {},                    orgs => {},
305                    name => $formName,                    name => $formName,
306                    scriptQueue => [],                    scriptQueue => [],
307                      genomeList => undef,
308                      genomeParms => [],
309                      filtered => 0,
310                   };                   };
311      # Bless and return it.      # Bless and return it.
312      bless $retVal, $class;      bless $retVal, $class;
# Line 152  Line 328 
328      return $self->{query};      return $self->{query};
329  }  }
330    
331    
332    
333  =head3 DB  =head3 DB
334    
335  C<< my $sprout = $shelp->DB(); >>  C<< my $sprout = $shelp->DB(); >>
# Line 163  Line 341 
341  sub DB {  sub DB {
342      # Get the parameters.      # Get the parameters.
343      my ($self) = @_;      my ($self) = @_;
344        # Insure we have a database.
345        my $retVal = $self->{sprout};
346        if (! defined $retVal) {
347            $retVal = SFXlate->new_sprout_only();
348            $self->{sprout} = $retVal;
349        }
350      # Return the result.      # Return the result.
351      return $self->{sprout};      return $retVal;
352  }  }
353    
354  =head3 IsNew  =head3 IsNew
# Line 277  Line 461 
461      my ($self, $title) = @_;      my ($self, $title) = @_;
462      # Get the CGI object.      # Get the CGI object.
463      my $cgi = $self->Q();      my $cgi = $self->Q();
464      # Start the form.      # Start the form. Note we use the override option on the Class value, in
465        # case the Advanced button was used.
466      my $retVal = "<div class=\"search\">\n" .      my $retVal = "<div class=\"search\">\n" .
467                   $cgi->start_form(-method => 'POST',                   $cgi->start_form(-method => 'POST',
468                                    -action => $cgi->url(-relative => 1),                                    -action => $cgi->url(-relative => 1),
469                                    -name => $self->FormName()) .                                    -name => $self->FormName()) .
470                   $cgi->hidden(-name => 'Class',                   $cgi->hidden(-name => 'Class',
471                                -value => $self->{class}) .                                -value => $self->{class},
472                                  -override => 1) .
473                   $cgi->hidden(-name => 'SPROUT',                   $cgi->hidden(-name => 'SPROUT',
474                                -value => 1) .                                -value => 1) .
475                   $cgi->h3($title);                   $cgi->h3($title);
# Line 437  Line 623 
623    
624  =head3 PutFeature  =head3 PutFeature
625    
626  C<< $shelp->PutFeature($record, %extraCols); >>  C<< $shelp->PutFeature($fdata); >>
627    
628  Store a feature in the result cache. This is the workhorse method for most  Store a feature in the result cache. This is the workhorse method for most
629  searches, since the primary data item in the database is features.  searches, since the primary data item in the database is features.
630    
631  For each feature, there are certain columns that are standard: the feature name, the  For each feature, there are certain columns that are standard: the feature name, the
632  GBrowse and protein page links, the functional assignment, and so forth. If additional  GBrowse and protein page links, the functional assignment, and so forth. If additional
633  columns are required by a particular search subclass, they should be included in the  columns are required by a particular search subclass, they should be stored in
634  parameters, in key-value form. For example, the following call adds columns for  the feature query object using the B<AddExtraColumns> method. For example, the following
635  essentiality and virulence.  code adds columns for essentiality and virulence.
636    
637      $shelp->PutFeature($record, essential => $essentialFlag, virulence => $vfactor);      $fd->AddExtraColumns(essential => $essentialFlag, virulence => $vfactor);
638        $shelp->PutFeature($fd);
639    
640  For correct results, all values should be specified for all extra columns in all calls to  For correct results, all values should be specified for all extra columns in all calls to
641  B<PutFeature>. (In particular, the column header names are computed on the first  B<PutFeature>. (In particular, the column header names are computed on the first
# Line 458  Line 645 
645      if (! $essentialFlag) {      if (! $essentialFlag) {
646          $essentialFlag = undef;          $essentialFlag = undef;
647      }      }
648      $shelp->PutFeature($record, essential => $essentialFlag, virulence = $vfactor);      $fd->AddExtraColumns(essential => $essentialFlag, virulence => $vfactor);
649        $shelp->PutFeature($fd);
650    
651  =over 4  =over 4
652    
653  =item record  =item fdata
   
 DBObject record for the feature.  
654    
655  =item extraCols  B<FeatureData> object containing the current feature data.
656    
657  =back  =back
658    
659  =cut  =cut
660    
661  sub PutFeature {  sub PutFeature {
662      # Get the parameters. Note that the extra columns are read in as a list      # Get the parameters.
663      # instead of a hash so that the column order is preserved.      my ($self, $fd) = @_;
664      my ($self, $record, @extraColList) = @_;      # Get the CGI query object.
665        my $cgi = $self->Q();
666        # Get the feature data.
667        my $record = $fd->Feature();
668        my $extraCols = $fd->ExtraCols();
669      # Check for a first-call situation.      # Check for a first-call situation.
670      if (! defined $self->{cols}) {      if (! defined $self->{cols}) {
671          # Here we need to set up the column information. Start with the defaults.          Trace("Setting up the columns.") if T(3);
672          $self->{cols} = $self->DefaultFeatureColumns();          # Here we need to set up the column information. Start with the extras,
673          # Append the extras. Note we proceed by twos because the columns are          # sorted by column name.
674          # specified in the form name => value.          my @colNames = ();
675          for (my $i = 0; $i <= $#extraColList; $i += 2) {          for my $col (sort keys %{$extraCols}) {
676              push @{$self->{cols}}, "X=$extraColList[$i]";              push @colNames, "X=$col";
677          }          }
678            # Add the default columns.
679            push @colNames, $self->DefaultFeatureColumns();
680            # Add any additional columns requested by the feature filter.
681            push @colNames, FeatureQuery::AdditionalColumns($self);
682            # Save the full list.
683            $self->{cols} = \@colNames;
684          # Write out the column headers. This also prepares the cache file to receive          # Write out the column headers. This also prepares the cache file to receive
685          # output.          # output.
686          $self->WriteColumnHeaders(map { $self->FeatureColumnTitle($_) } @{$self->{cols}});          $self->WriteColumnHeaders(map { $self->FeatureColumnTitle($_) } @{$self->{cols}});
687      }      }
688      # Get the feature ID.      # Get the feature ID.
689      my ($fid) = $record->Value('Feature(id)');      my $fid = $fd->FID();
     # Now we process the columns themselves. First, convert the extra column list  
     # to a hash.  
     my %extraCols = @extraColList;  
690      # Loop through the column headers, producing the desired data.      # Loop through the column headers, producing the desired data.
691      my @output = ();      my @output = ();
692      for my $colName (@{$self->{cols}}) {      for my $colName (@{$self->{cols}}) {
693          push @output, $self->FeatureColumnValue($colName, $record, \%extraCols);          push @output, $self->FeatureColumnValue($colName, $record, $extraCols);
694      }      }
695      # Compute the sort key. The sort key floats NMPDR organism features to the      # Compute the sort key. The sort key usually floats NMPDR organism features to the
696      # top of the return list.      # top of the return list.
697      my $group = $self->FeatureGroup($fid);      my $key = $self->SortKey($fd);
     my $key = ($group ? "A$group" : "ZZ");  
698      # Write the feature data.      # Write the feature data.
699      $self->WriteColumnData($key, @output);      $self->WriteColumnData($key, @output);
700  }  }
# Line 583  Line 775 
775      # Check for an open session file.      # Check for an open session file.
776      if (defined $self->{fileHandle}) {      if (defined $self->{fileHandle}) {
777          # We found one, so close it.          # We found one, so close it.
778            Trace("Closing session file.") if T(2);
779          close $self->{fileHandle};          close $self->{fileHandle};
780      }      }
781  }  }
# Line 600  Line 793 
793      my $retVal;      my $retVal;
794      # Get a digest encoder.      # Get a digest encoder.
795      my $md5 = Digest::MD5->new();      my $md5 = Digest::MD5->new();
796      # If we have a randomization file, use it to seed the digester.      # Add the PID, the IP, and the time stamp. Note that the time stamp is
797      if (open(R, "/dev/urandom")) {      # actually two numbers, and we get them both because we're in list
798          my $b;      # context.
799          read(R, $b, 1024);      $md5->add($$, $ENV{REMOTE_ADDR}, $ENV{REMOTE_PORT}, gettimeofday());
800          $md5->add($b);      # Hash up all this identifying data.
801      }      $retVal = $md5->hexdigest();
802      # Add the PID and the time stamp.      # Return the result.
     $md5->add($$, gettimeofday);  
     # Hash it up and clean the result so that it works as a file name.  
     $retVal = $md5->b64digest();  
     $retVal =~ s,/,\$,g;  
     $retVal =~ s,\+,@,g;  
     # Return it.  
803      return $retVal;      return $retVal;
804  }  }
805    
# Line 656  Line 843 
843                                                      ['Genome(genus)', 'Genome(species)',                                                      ['Genome(genus)', 'Genome(species)',
844                                                       'Genome(unique-characterization)',                                                       'Genome(unique-characterization)',
845                                                       'Genome(primary-group)']);                                                       'Genome(primary-group)']);
846          # Null out the supporting group.          # Format and cache the name and display group.
847          $group = "" if ($group eq $FIG_Config::otherGroup);          ($orgName, $group) = $self->SaveOrganismData($group, $genomeID, $genus, $species,
848          # If the organism does not exist, format an unknown name.                                                              $strain);
         if (! defined($genus)) {  
             $orgName = "Unknown Genome $genomeID";  
         } else {  
             # It does exist, so format the organism name.  
             $orgName = "$genus $species";  
             if ($strain) {  
                 $orgName .= " $strain";  
             }  
         }  
         # Save this organism in the cache.  
         $cache->{$genomeID} = [$orgName, $group];  
849      }      }
850      # Return the result.      # Return the result.
851      return ($orgName, $group);      return ($orgName, $group);
# Line 771  Line 947 
947      } else {      } else {
948          # Here we can get its genome data.          # Here we can get its genome data.
949          $retVal = $self->Organism($genomeID);          $retVal = $self->Organism($genomeID);
950          # Append the type and number.          # Append the FIG ID.
951          $retVal .= " [$type $num]";          $retVal .= " [$fid]";
952      }      }
953      # Return the result.      # Return the result.
954      return $retVal;      return $retVal;
# Line 780  Line 956 
956    
957  =head3 ComputeFASTA  =head3 ComputeFASTA
958    
959  C<< my $fasta = $shelp->ComputeFASTA($incomingType, $desiredType, $sequence); >>  C<< my $fasta = $shelp->ComputeFASTA($desiredType, $sequence); >>
960    
961  Parse a sequence input and convert it into a FASTA string of the desired type. Note  Parse a sequence input and convert it into a FASTA string of the desired type.
 that it is possible to convert a DNA sequence into a protein sequence, but the reverse  
 is not possible.  
962    
963  =over 4  =over 4
964    
 =item incomingType  
   
 C<dna> if this is a DNA sequence, C<prot> if this is a protein sequence.  
   
965  =item desiredType  =item desiredType
966    
967  C<dna> to return a DNA sequence, C<prot> to return a protein sequence. If the  C<dna> to return a DNA sequence, C<prot> to return a protein sequence.
 I<$incomingType> is C<prot> and this value is C<dna>, an error will be thrown.  
968    
969  =item sequence  =item sequence
970    
# Line 817  Line 986 
986    
987  sub ComputeFASTA {  sub ComputeFASTA {
988      # Get the parameters.      # Get the parameters.
989      my ($self, $incomingType, $desiredType, $sequence) = @_;      my ($self, $desiredType, $sequence) = @_;
990      # Declare the return variable. If an error occurs, it will remain undefined.      # Declare the return variable. If an error occurs, it will remain undefined.
991      my $retVal;      my $retVal;
992        # This variable will be cleared if an error is detected.
993        my $okFlag = 1;
994      # Create variables to hold the FASTA label and data.      # Create variables to hold the FASTA label and data.
995      my ($fastaLabel, $fastaData);      my ($fastaLabel, $fastaData);
996        Trace("FASTA desired type is $desiredType.") if T(4);
997      # Check for a feature specification.      # Check for a feature specification.
998      if ($sequence =~ /^\s*(\w+\|\S+)\s*$/) {      if ($sequence =~ /^\s*(\w+\|\S+)\s*$/) {
999          # Here we have a feature ID in $1. We'll need the Sprout object to process          # Here we have a feature ID in $1. We'll need the Sprout object to process
1000          # it.          # it.
1001          my $fid = $1;          my $fid = $1;
1002            Trace("Feature ID for fasta is $fid.") if T(3);
1003          my $sprout = $self->DB();          my $sprout = $self->DB();
1004          # Get the FIG ID. Note that we only use the first feature found. We are not          # Get the FIG ID. Note that we only use the first feature found. We are not
1005          # supposed to have redundant aliases, though we may have an ID that doesn't          # supposed to have redundant aliases, though we may have an ID that doesn't
1006          # exist.          # exist.
1007          my ($figID) = $sprout->FeaturesByAlias($fid);          my ($figID) = $sprout->FeaturesByAlias($fid);
1008          if (! $figID) {          if (! $figID) {
1009              $self->SetMessage("No feature found with the ID \"$fid\".");              $self->SetMessage("No gene found with the ID \"$fid\".");
1010                $okFlag = 0;
1011          } else {          } else {
1012              # Set the FASTA label.              # Set the FASTA label.
1013              my $fastaLabel = $fid;              my $fastaLabel = $fid;
1014              # Now proceed according to the sequence type.              # Now proceed according to the sequence type.
1015              if ($desiredType =~ /prot/i) {              if ($desiredType eq 'prot') {
1016                  # We want protein, so get the translation.                  # We want protein, so get the translation.
1017                  $fastaData = $sprout->FeatureTranslation($figID);                  $fastaData = $sprout->FeatureTranslation($figID);
1018                    Trace(length $fastaData . " characters returned for translation of $fastaLabel.") if T(3);
1019              } else {              } else {
1020                  # We want DNA, so get the DNA sequence. This is a two-step process.                  # We want DNA, so get the DNA sequence. This is a two-step process.
1021                  my @locList = $sprout->FeatureLocation($figID);                  my @locList = $sprout->FeatureLocation($figID);
1022                  $fastaData = $sprout->DNASeq(\@locList);                  $fastaData = $sprout->DNASeq(\@locList);
1023                    Trace(length $fastaData . " characters returned for DNA of $fastaLabel.") if T(3);
1024              }              }
1025          }          }
     } elsif ($incomingType =~ /prot/ && $desiredType =~ /dna/) {  
         # Here we're being asked to do an impossible conversion.  
         $self->SetMessage("Cannot convert a protein sequence to DNA.");  
1026      } else {      } else {
1027            Trace("Analyzing FASTA sequence.") if T(4);
1028          # Here we are expecting a FASTA. We need to see if there's a label.          # Here we are expecting a FASTA. We need to see if there's a label.
1029          if ($sequence =~ /^>\s*(\S.*)\s*\n(.+)$/) {          if ($sequence =~ /^>[\n\s]*(\S[^\n]*)\n(.+)$/s) {
1030                Trace("Label \"$1\" found in match to sequence:\n$sequence") if T(4);
1031              # Here we have a label, so we split it from the data.              # Here we have a label, so we split it from the data.
1032              $fastaLabel = $1;              $fastaLabel = $1;
1033              $fastaData = $2;              $fastaData = $2;
1034          } else {          } else {
1035                Trace("No label found in match to sequence:\n$sequence") if T(4);
1036              # 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
1037              # as data.              # as data.
1038              $fastaLabel = "User-specified $incomingType sequence";              $fastaLabel = "User-specified $desiredType sequence";
1039              $fastaData = $sequence;              $fastaData = $sequence;
1040          }          }
1041          # The next step is to clean the junk out of the sequence.          # The next step is to clean the junk out of the sequence.
1042          $fastaData =~ s/\n//g;          $fastaData =~ s/\n//g;
1043          $fastaData =~ s/\s+//g;          $fastaData =~ s/\s+//g;
1044          # Finally, if the user wants to convert to protein, we do it here. Note that          # Finally, verify that it's DNA if we're doing DNA stuff.
1045          # we've already prevented a conversion from protein to DNA.          if ($desiredType eq 'dna' && $fastaData =~ /[^agct]/i) {
1046          if ($incomingType ne $desiredType) {              $self->SetMessage("Invaid characters detected. Is the input really a DNA sequence?");
1047              $fastaData = Sprout::Protein($fastaData);              $okFlag = 0;
1048          }          }
1049      }      }
1050      # At this point, either "$fastaLabel" and "$fastaData" have values or an error is      Trace("FASTA data sequence: $fastaData") if T(4);
1051      # in progress.      # Only proceed if no error was detected.
1052      if (defined $fastaLabel) {      if ($okFlag) {
1053          # 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
1054          # grep-split trick. The split, because of the presence of the parentheses,          # grep-split trick. The split, because of the presence of the parentheses,
1055          # includes the matched delimiters in the output list. The grep strips out          # includes the matched delimiters in the output list. The grep strips out
1056          # the empty list items that appear between the so-called delimiters, since          # the empty list items that appear between the so-called delimiters, since
1057          # the delimiters are what we want.          # the delimiters are what we want.
1058          my @chunks = grep { $_ } split /(.{1,60})/, $fastaData;          my @chunks = grep { $_ } split /(.{1,60})/, $fastaData;
1059          my $retVal = join("\n", ">$fastaLabel", @chunks, "");          $retVal = join("\n", ">$fastaLabel", @chunks, "");
1060      }      }
1061      # Return the result.      # Return the result.
1062      return $retVal;      return $retVal;
1063  }  }
1064    
1065    =head3 SubsystemTree
1066    
1067    C<< my $tree = SearchHelper::SubsystemTree($sprout, %options); >>
1068    
1069    This method creates a subsystem selection tree suitable for passing to
1070    L</SelectionTree>. Each leaf node in the tree will have a link to the
1071    subsystem display page. In addition, each node can have a radio button. The
1072    radio button alue is either C<classification=>I<string>, where I<string> is
1073    a classification string, or C<id=>I<string>, where I<string> is a subsystem ID.
1074    Thus, it can either be used to filter by a group of related subsystems or a
1075    single subsystem.
1076    
1077    =over 4
1078    
1079    =item sprout
1080    
1081    Sprout database object used to get the list of subsystems.
1082    
1083    =item options
1084    
1085    Hash containing options for building the tree.
1086    
1087    =item RETURN
1088    
1089    Returns a reference to a tree list suitable for passing to L</SelectionTree>.
1090    
1091    =back
1092    
1093    The supported options are as follows.
1094    
1095    =over 4
1096    
1097    =item radio
1098    
1099    TRUE if the tree should be configured for radio buttons. The default is FALSE.
1100    
1101    =item links
1102    
1103    TRUE if the tree should be configured for links. The default is TRUE.
1104    
1105    =back
1106    
1107    =cut
1108    
1109    sub SubsystemTree {
1110        # Get the parameters.
1111        my ($sprout, %options) = @_;
1112        # Process the options.
1113        my $optionThing = Tracer::GetOptions({ radio => 0, links => 1 }, \%options);
1114        # Read in the subsystems.
1115        my @subs = $sprout->GetAll(['Subsystem'], "ORDER BY Subsystem(classification), Subsystem(id)", [],
1116                                   ['Subsystem(classification)', 'Subsystem(id)']);
1117        # Declare the return variable.
1118        my @retVal = ();
1119        # Each element in @subs represents a leaf node, so as we loop through it we will be
1120        # producing one leaf node at a time. The leaf node is represented as a 2-tuple. The
1121        # first element is a semi-colon-delimited list of the classifications for the
1122        # subsystem. There will be a stack of currently-active classifications, which we will
1123        # compare to the incoming classifications from the end backward. A new classification
1124        # requires starting a new branch. A different classification requires closing an old
1125        # branch and starting a new one. Each classification in the stack will also contain
1126        # that classification's current branch. We'll add a fake classification at the
1127        # beginning that we can use to represent the tree as a whole.
1128        my $rootName = '<root>';
1129        # Create the classification stack. Note the stack is a pair of parallel lists,
1130        # one containing names and the other containing content.
1131        my @stackNames = ($rootName);
1132        my @stackContents = (\@retVal);
1133        # Add a null entry at the end of the subsystem list to force an unrolling.
1134        push @subs, ['', undef];
1135        # Loop through the subsystems.
1136        for my $sub (@subs) {
1137            # Pull out the classification list and the subsystem ID.
1138            my ($classString, $id) = @{$sub};
1139            Trace("Processing class \"$classString\" and subsystem $id.") if T(4);
1140            # Convert the classification string to a list with the root classification in
1141            # the front.
1142            my @classList = ($rootName, split($FIG_Config::splitter, $classString));
1143            # Find the leftmost point at which the class list differs from the stack.
1144            my $matchPoint = 0;
1145            while ($matchPoint <= $#stackNames && $matchPoint <= $#classList &&
1146                   $stackNames[$matchPoint] eq $classList[$matchPoint]) {
1147                $matchPoint++;
1148            }
1149            Trace("Match point is $matchPoint. Stack length is " . scalar(@stackNames) .
1150                  ". Class List length is " . scalar(@classList) . ".") if T(4);
1151            # Unroll the stack to the matchpoint.
1152            while ($#stackNames >= $matchPoint) {
1153                my $popped = pop @stackNames;
1154                pop @stackContents;
1155                Trace("\"$popped\" popped from stack.") if T(4);
1156            }
1157            # Start branches for any new classifications.
1158            while ($#stackNames < $#classList) {
1159                # The branch for a new classification contains its radio button
1160                # data and then a list of children. So, at this point, if radio buttons
1161                # are desired, we put them into the content.
1162                my $newLevel = scalar(@stackNames);
1163                my @newClassContent = ();
1164                if ($optionThing->{radio}) {
1165                    my $newClassString = join($FIG_Config::splitter, @classList[1..$newLevel]);
1166                    push @newClassContent, { value => "classification=$newClassString%" };
1167                }
1168                # The new classification node is appended to its parent's content
1169                # and then pushed onto the stack. First, we need the node name.
1170                my $nodeName = $classList[$newLevel];
1171                # Add the classification to its parent. This makes it part of the
1172                # tree we'll be returning to the user.
1173                push @{$stackContents[$#stackNames]}, $nodeName, \@newClassContent;
1174                # Push the classification onto the stack.
1175                push @stackContents, \@newClassContent;
1176                push @stackNames, $nodeName;
1177                Trace("\"$nodeName\" pushed onto stack.") if T(4);
1178            }
1179            # Now the stack contains all our parent branches. We add the subsystem to
1180            # the branch at the top of the stack, but only if it's NOT the dummy node.
1181            if (defined $id) {
1182                # Compute the node name from the ID.
1183                my $nodeName = $id;
1184                $nodeName =~ s/_/ /g;
1185                # Create the node's leaf hash. This depends on the value of the radio
1186                # and link options.
1187                my $nodeContent = {};
1188                if ($optionThing->{links}) {
1189                    # Compute the link value.
1190                    my $linkable = uri_escape($id);
1191                    $nodeContent->{link} = "../FIG/display_subsys.cgi?ssa_name=$linkable;request=show_ssa;sort=by_phylo;SPROUT=1";
1192                }
1193                if ($optionThing->{radio}) {
1194                    # Compute the radio value.
1195                    $nodeContent->{value} = "id=$id";
1196                }
1197                # Push the node into its parent branch.
1198                Trace("\"$nodeName\" added to node list.") if T(4);
1199                push @{$stackContents[$#stackNames]}, $nodeName, $nodeContent;
1200            }
1201        }
1202        # Return the result.
1203        return \@retVal;
1204    }
1205    
1206    
1207  =head3 NmpdrGenomeMenu  =head3 NmpdrGenomeMenu
1208    
1209  C<< my $htmlText = $shelp->NmpdrGenomeMenu($menuName, \%options, \@selected); >>  C<< my $htmlText = $shelp->NmpdrGenomeMenu($menuName, $multiple, \@selected, $rows); >>
1210    
1211  This method creates a hierarchical HTML menu for NMPDR genomes organized by category. The  This method creates a hierarchical HTML menu for NMPDR genomes organized by category. The
1212  category indicates the low-level NMPDR group. Organizing the genomes in this way makes it  category indicates the low-level NMPDR group. Organizing the genomes in this way makes it
# Line 900  Line 1218 
1218    
1219  Name to give to the menu.  Name to give to the menu.
1220    
1221  =item options  =item multiple
1222    
1223  Reference to a hash containing the options to be applied to the C<SELECT> tag form the menu.  TRUE if the user is allowed to select multiple genomes, else FALSE.
 Typical options would include C<multiple> to specify  
 that multiple selections are allowed and C<size> to set the number of rows to display  
 in the menu.  
1224    
1225  =item selected  =item selected
1226    
# Line 913  Line 1228 
1228  is not intended to allow multiple selections, the list should be a singleton. If the  is not intended to allow multiple selections, the list should be a singleton. If the
1229  list is empty, nothing will be pre-selected.  list is empty, nothing will be pre-selected.
1230    
1231    =item rows (optional)
1232    
1233    Number of rows to display. If omitted, the default is 1 for a single-select list
1234    and 10 for a multi-select list.
1235    
1236    =item crossMenu (optional)
1237    
1238    If specified, is presumed to be the name of another genome menu whose contents
1239    are to be mutually exclusive with the contents of this menu. As a result, instead
1240    of the standard onChange event, the onChange event will deselect any entries in
1241    the other menu.
1242    
1243  =item RETURN  =item RETURN
1244    
1245  Returns the HTML text to generate a C<SELECT> menu inside a form.  Returns the HTML text to generate a C<SELECT> menu inside a form.
# Line 923  Line 1250 
1250    
1251  sub NmpdrGenomeMenu {  sub NmpdrGenomeMenu {
1252      # Get the parameters.      # Get the parameters.
1253      my ($self, $menuName, $options, $selected) = @_;      my ($self, $menuName, $multiple, $selected, $rows, $cross) = @_;
1254      # Get the Sprout and CGI objects.      # Get the Sprout and CGI objects.
1255      my $sprout = $self->DB();      my $sprout = $self->DB();
1256      my $cgi = $self->Q();      my $cgi = $self->Q();
1257        # Compute the row count.
1258        if (! defined $rows) {
1259            $rows = ($multiple ? 10 : 1);
1260        }
1261        # Create the multiple tag.
1262        my $multipleTag = ($multiple ? " multiple" : "");
1263      # Get the form name.      # Get the form name.
1264      my $formName = $self->FormName();      my $formName = $self->FormName();
1265        # Check to see if we already have a genome list in memory.
1266        my $genomes = $self->{genomeList};
1267        my $groupHash;
1268        if (defined $genomes) {
1269            # We have a list ready to use.
1270            $groupHash = $genomes;
1271        } else {
1272      # Get a list of all the genomes in group order. In fact, we only need them ordered      # Get a list of all the genomes in group order. In fact, we only need them ordered
1273      # by name (genus,species,strain), but putting primary-group in front enables us to      # by name (genus,species,strain), but putting primary-group in front enables us to
1274      # take advantage of an existing index.      # take advantage of an existing index.
# Line 940  Line 1280 
1280      # Create a hash to organize the genomes by group. Each group will contain a list of      # Create a hash to organize the genomes by group. Each group will contain a list of
1281      # 2-tuples, the first element being the genome ID and the second being the genome      # 2-tuples, the first element being the genome ID and the second being the genome
1282      # name.      # name.
1283      my %groupHash = ();          my %gHash = ();
1284      for my $genome (@genomeList) {      for my $genome (@genomeList) {
1285          # Get the genome data.          # Get the genome data.
1286          my ($group, $genomeID, $genus, $species, $strain) = @{$genome};          my ($group, $genomeID, $genus, $species, $strain) = @{$genome};
1287          # Form the genome name.              # Compute and cache its name and display group.
1288          my $name = "$genus $species";              my ($name, $displayGroup) = $self->SaveOrganismData($group, $genomeID, $genus, $species,
1289          if ($strain) {                                                                  $strain);
1290              $name .= " $strain";              # Push the genome into the group's list. Note that we use the real group
1291          }              # name here, not the display group name.
1292          # Push the genome into the group's list.              push @{$gHash{$group}}, [$genomeID, $name];
1293          push @{$groupHash{$group}}, [$genomeID, $name];          }
1294            # Save the genome list for future use.
1295            $self->{genomeList} = \%gHash;
1296            $groupHash = \%gHash;
1297      }      }
1298      # Now we are ready to unroll the menu out of the group hash. First, we sort the groups, putting      # Now we are ready to unroll the menu out of the group hash. First, we sort the groups, putting
1299      # the supporting-genome group last.      # the supporting-genome group last.
1300      my @groups = sort grep { $_ ne $FIG_Config::otherGroup } keys %groupHash;      my @groups = sort grep { $_ ne $FIG_Config::otherGroup } keys %{$groupHash};
1301      push @groups, $FIG_Config::otherGroup;      push @groups, $FIG_Config::otherGroup;
1302      # Next, create a hash that specifies the pre-selected entries.      # Next, create a hash that specifies the pre-selected entries. Note that we need to deal
1303      my %selectedHash = map { $_ => 1 } @{$selected};      # with the possibility of undefined values in the incoming list.
1304      # Now it gets complicated. We need a way to mark all the NMPDR genomes.      my %selectedHash = ();
1305        if (defined $selected) {
1306            %selectedHash = map { $_ => 1 } grep { defined($_) } @{$selected};
1307        }
1308        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
1309        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
1310        # and use that to make the selections.
1311        my $nmpdrCount = 0;
1312      # Create the type counters.      # Create the type counters.
1313      my $groupCount = 1;      my $groupCount = 1;
1314      # Compute the ID for the status display.      # Compute the ID for the status display.
# Line 967  Line 1317 
1317      my $showSelect = "showSelected($menuName, '$divID', 1000);";      my $showSelect = "showSelected($menuName, '$divID', 1000);";
1318      # If multiple selection is supported, create an onChange event.      # If multiple selection is supported, create an onChange event.
1319      my $onChange = "";      my $onChange = "";
1320      if ($options->{multiple}) {      if ($cross) {
1321            # Here we have a paired menu. Selecting something in our menu unselects it in the
1322            # other and redisplays the status of both.
1323            $onChange = " onChange=\"crossUnSelect($menuName, '$divID', $cross, '${formName}_${cross}_status', 1000)\"";
1324        } elsif ($multiple) {
1325            # This is an unpaired menu, so all we do is redisplay our status.
1326          $onChange = " onChange=\"$showSelect\"";          $onChange = " onChange=\"$showSelect\"";
1327      }      }
1328      # Create the SELECT tag and stuff it into the output array.      # Create the SELECT tag and stuff it into the output array.
1329      my $select = "<" . join(" ", "SELECT name=\"$menuName\"$onChange", map { " $_=\"$options->{$_}\"" } keys %{$options}) . ">";      my @lines = ("<SELECT name=\"$menuName\"$onChange$multipleTag size=\"$rows\">");
     my @lines = ($select);  
1330      # Loop through the groups.      # Loop through the groups.
1331      for my $group (@groups) {      for my $group (@groups) {
1332          # Create the option group tag.          # Create the option group tag.
1333          my $tag = "<OPTGROUP label=\"$group\">";          my $tag = "<OPTGROUP label=\"$group\">";
1334          push @lines, "  $tag";          push @lines, "  $tag";
         # Compute the label for this group's options. This is seriously dirty stuff, as the  
         # label option may have functionality in future browsers. If that happens, we'll need  
         # to modify the genome text so that the "selectSome" method can tell which are NMPDR  
         # organisms and which aren't. Sadly, the OPTGROUP tag is invisible in the DOM Javascript  
         # hierarchy.  
         my $label = ($group eq $FIG_Config::otherGroup ? "other" : "nmpdr");  
1335          # Get the genomes in the group.          # Get the genomes in the group.
1336          for my $genome (@{$groupHash{$group}}) {          for my $genome (@{$groupHash->{$group}}) {
1337                # Count this organism if it's NMPDR.
1338                if ($group ne $FIG_Config::otherGroup) {
1339                    $nmpdrCount++;
1340                }
1341                # Get the organism ID and name.
1342              my ($genomeID, $name) = @{$genome};              my ($genomeID, $name) = @{$genome};
1343              # See if it's selected.              # See if it's selected.
1344              my $select = ($selectedHash{$genomeID} ? " selected" : "");              my $select = ($selectedHash{$genomeID} ? " selected" : "");
1345              # Generate the option tag.              # Generate the option tag.
1346              my $optionTag = "<OPTION value=\"$genomeID\" label=\"$label\"$select>$name <em>($genomeID)</em></OPTION>";              my $optionTag = "<OPTION value=\"$genomeID\"$select>$name <em>($genomeID)</em></OPTION>";
1347              push @lines, "    $optionTag";              push @lines, "    $optionTag";
1348          }          }
1349          # Close the option group.          # Close the option group.
# Line 999  Line 1352 
1352      # Close the SELECT tag.      # Close the SELECT tag.
1353      push @lines, "</SELECT>";      push @lines, "</SELECT>";
1354      # Check for multiple selection.      # Check for multiple selection.
1355      if ($options->{multiple}) {      if ($multiple) {
1356          # Since multi-select is on, we can set up some buttons to set and clear selections.          # Multi-select is on, so we need to add some selection helpers. First is
1357            # the search box. This allows the user to type text and have all genomes containing
1358            # the text selected automatically.
1359            my $searchThingName = "${menuName}_SearchThing";
1360            push @lines, "<br />" .
1361                         "<INPUT type=\"button\" name=\"Search\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectViaSearch($menuName, $searchThingName); $showSelect\" />&nbsp;" .
1362                         "<INPUT type=\"text\" name=\"$searchThingName\" size=\"30\" />";
1363            # Next are the buttons to set and clear selections.
1364          push @lines, "<br />";          push @lines, "<br />";
         push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll($menuName); $showSelect\" />";  
1365          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\" />";
1366          push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome($menuName, 'nmpdr'); $showSelect\" />";          push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll($menuName); $showSelect\" />";
1367          push @lines, "<INPUT type=\"button\" name=\"OtherOnly\" class=\"bigButton\" value=\"Select Supporting\" onClick=\"selectSome($menuName, 'other'); $showSelect\" />";          push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome($menuName, $nmpdrCount, true); $showSelect\" />";
1368            push @lines, "<INPUT type=\"button\" name=\"OtherOnly\" class=\"bigButton\" value=\"Select Supporting\" onClick=\"selectSome($menuName, $nmpdrCount, false); $showSelect\" />";
1369          # Add the status display, too.          # Add the status display, too.
1370          push @lines, "<DIV id=\"$divID\" class=\"selectStatus\"></DIV>";          push @lines, "<DIV id=\"$divID\" class=\"selectStatus\"></DIV>";
1371          # 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 1014  Line 1374 
1374          # in case we decide to twiddle the parameters.          # in case we decide to twiddle the parameters.
1375          $showSelect =~ s/showSelected\(/showSelected\(thisForm\./;          $showSelect =~ s/showSelected\(/showSelected\(thisForm\./;
1376          $self->QueueFormScript($showSelect);          $self->QueueFormScript($showSelect);
1377            # Finally, add this parameter to the list of genome parameters. This enables us to
1378            # easily find all the parameters used to select one or more genomes.
1379            push @{$self->{genomeParms}}, $menuName;
1380      }      }
1381      # Assemble all the lines into a string.      # Assemble all the lines into a string.
1382      my $retVal = join("\n", @lines, "");      my $retVal = join("\n", @lines, "");
# Line 1021  Line 1384 
1384      return $retVal;      return $retVal;
1385  }  }
1386    
1387    =head3 PropertyMenu
1388    
1389    C<< my $htmlText = $shelp->PropertyMenu($menuName, $selected, $force); >>
1390    
1391    Generate a property name dropdown menu.
1392    
1393    =over 4
1394    
1395    =item menuName
1396    
1397    Name to give to the menu.
1398    
1399    =item selected
1400    
1401    Value of the property name to pre-select.
1402    
1403    =item force (optional)
1404    
1405    If TRUE, then the user will be forced to choose a property name. If FALSE,
1406    then an additional menu choice will be provided to select nothing.
1407    
1408    =item RETURN
1409    
1410    Returns a dropdown menu box that allows the user to select a property name. An additional
1411    selection entry will be provided for selecting no property name
1412    
1413    =back
1414    
1415    =cut
1416    
1417    sub PropertyMenu {
1418        # Get the parameters.
1419        my ($self, $menuName, $selected, $force) = @_;
1420        # Get the CGI and Sprout objects.
1421        my $sprout = $self->DB();
1422        my $cgi = $self->Q();
1423        # Create the property name list.
1424        my @propNames = ();
1425        if (! $force) {
1426            push @propNames, "";
1427        }
1428        # Get all the property names, putting them after the null choice if one exists.
1429        push @propNames, $sprout->GetChoices('Property', 'property-name');
1430        # Create a menu from them.
1431        my $retVal = $cgi->popup_menu(-name=> $menuName, -values => \@propNames,
1432                                      -default => $selected);
1433        # Return the result.
1434        return $retVal;
1435    }
1436    
1437  =head3 MakeTable  =head3 MakeTable
1438    
1439  C<< my $htmlText = $shelp->MakeTable(\@rows); >>  C<< my $htmlText = $shelp->MakeTable(\@rows); >>
# Line 1071  Line 1484 
1484    
1485  =head3 SubmitRow  =head3 SubmitRow
1486    
1487  C<< my $htmlText = $shelp->SubmitRow(); >>  C<< my $htmlText = $shelp->SubmitRow($caption); >>
1488    
1489  Returns the HTML text for the row containing the page size control  Returns the HTML text for the row containing the page size control
1490  and the submit button. All searches should have this row somewhere  and the submit button. All searches should have this row somewhere
1491  near the top of the form.  near the top of the form.
1492    
1493    =over 4
1494    
1495    =item caption (optional)
1496    
1497    Caption to be put on the search button. The default is C<Go>.
1498    
1499    =item RETURN
1500    
1501    Returns a table row containing the controls for submitting the search
1502    and tuning the results.
1503    
1504    =back
1505    
1506  =cut  =cut
1507    
1508  sub SubmitRow {  sub SubmitRow {
1509      # Get the parameters.      # Get the parameters.
1510      my ($self) = @_;      my ($self, $caption) = @_;
1511      my $cgi = $self->Q();      my $cgi = $self->Q();
1512      # Declare the return variable.      # Compute the button caption.
1513        my $realCaption = (defined $caption ? $caption : 'Go');
1514        # Get the current page size.
1515        my $pageSize = $cgi->param('PageSize');
1516        # Get the incoming external-link flag.
1517        my $aliases = ($cgi->param('ShowAliases') ? 1 : 0);
1518        # Create the row.
1519      my $retVal = $cgi->Tr($cgi->td("Results/Page"),      my $retVal = $cgi->Tr($cgi->td("Results/Page"),
1520                            $cgi->td($cgi->popup_menu(-name => 'PageSize',                            $cgi->td($cgi->popup_menu(-name => 'PageSize',
1521                                                      -values => [10, 25, 45, 100, 1000],                                                      -values => [10, 25, 50, 100, 1000],
1522                                                      -default => $cgi->param('PageSize'))),                                                      -default => $pageSize) . " " .
1523                                       $cgi->checkbox(-name => 'ShowURL',
1524                                                      -value => 1,
1525                                                      -label => 'Show URL')),
1526                            $cgi->td($cgi->submit(-class => 'goButton',                            $cgi->td($cgi->submit(-class => 'goButton',
1527                                                  -name => 'Search',                                                  -name => 'Search',
1528                                                  -value => 'Go')));                                                  -value => $realCaption)));
1529      # Return the result.      # Return the result.
1530      return $retVal;      return $retVal;
1531  }  }
1532    
1533    =head3 FeatureFilterRows
1534    
1535    C<< my $htmlText = $shelp->FeatureFilterRows(); >>
1536    
1537    This method creates table rows that can be used to filter features. The form
1538    values can be used to select features by genome using the B<FeatureQuery>
1539    object.
1540    
1541    =cut
1542    
1543    sub FeatureFilterRows {
1544        # Get the parameters.
1545        my ($self) = @_;
1546        # Return the result.
1547        return FeatureQuery::FilterRows($self);
1548    }
1549    
1550  =head3 GBrowseFeatureURL  =head3 GBrowseFeatureURL
1551    
1552  C<< my $url = SearchHelper::GBrowseFeatureURL($sprout, $feat); >>  C<< my $url = SearchHelper::GBrowseFeatureURL($sprout, $feat); >>
# Line 1133  Line 1586 
1586          # Get the feature location string.          # Get the feature location string.
1587          my $loc = $sprout->FeatureLocation($feat);          my $loc = $sprout->FeatureLocation($feat);
1588          # Compute the contig, start, and stop points.          # Compute the contig, start, and stop points.
1589          my($start, $stop, $contig) = BasicLocation::Parse($loc);          my($contig, $start, $stop) = BasicLocation::Parse($loc);
1590            Trace("Start and stop are ($start,$stop) on contig $contig.") if T(3);
1591          # Now we need to do some goofiness to insure that the location is not too          # Now we need to do some goofiness to insure that the location is not too
1592          # big and that we get some surrounding stuff.          # big and that we get some surrounding stuff.
1593          my $mid = int(($start + $stop) / 2);          my $mid = int(($start + $stop) / 2);
# Line 1163  Line 1617 
1617          }          }
1618          my $seg_id = $contig;          my $seg_id = $contig;
1619          $seg_id =~ s/:/--/g;          $seg_id =~ s/:/--/g;
1620            Trace("Show limits are ($show_start,$show_stop) in genome $genomeID with ref $seg_id.") if T(3);
1621          # Assemble all the pieces.          # Assemble all the pieces.
1622          $retVal = "gbrowse.cgi/GB_$genomeID?ref=$seg_id&start=$show_start&stop=$show_stop";          $retVal = "gbrowse.cgi/GB_$genomeID?ref=$seg_id;start=$show_start;stop=$show_stop";
1623      }      }
1624      # Return the result.      # Return the result.
1625      return $retVal;      return $retVal;
1626  }  }
1627    
1628  =head2 Feature Column Methods  =head3 GetGenomes
1629    
1630  The methods in this column manage feature column data. If you want to provide the  C<< my @genomeList = $shelp->GetGenomes($parmName); >>
 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>, and  
 everything else will happen automatically.  
1631    
1632  There is one special column name syntax for extra columns (that is, nonstandard  Return the list of genomes specified by the specified CGI query parameter.
1633  feature columns). If the column name begins with C<X=>, then it is presumed to be  If the request method is POST, then the list of genome IDs is returned
1634  an extra column. The column title is the text after the C<X=>, and its value is  without preamble. If the request method is GET and the parameter is not
1635  pulled from the extra column hash.  specified, then it is treated as a request for all genomes. This makes it
1636    easier for web pages to link to a search that wants to specify all genomes.
1637    
1638  =head3 DefaultFeatureColumns  =over 4
1639    
1640    =item parmName
1641    
1642  C<< my $colNames = $shelp->DefaultFeatureColumns(); >>  Name of the parameter containing the list of genomes. This will be the
1643    first parameter passed to the L</NmpdrGenomeMenu> call that created the
1644    genome selection control on the form.
1645    
1646  Return a reference to a list of the default feature column identifiers. These  =item RETURN
1647  identifiers can be passed to L</FeatureColumnTitle> and L</FeatureColumnValue> in  
1648  order to produce the column titles and row values.  Returns a list of the genomes to process.
1649    
1650    =back
1651    
1652  =cut  =cut
1653    
1654  sub DefaultFeatureColumns {  sub GetGenomes {
1655        # Get the parameters.
1656        my ($self, $parmName) = @_;
1657        # Get the CGI query object.
1658        my $cgi = $self->Q();
1659        # Get the list of genome IDs in the request header.
1660        my @retVal = $cgi->param($parmName);
1661        Trace("Genome list for $parmName is (" . join(", ", @retVal) . ") with method " . $cgi->request_method() . ".") if T(3);
1662        # Check for the special GET case.
1663        if ($cgi->request_method() eq "GET" && ! @retVal) {
1664            # Here the caller wants all the genomes.
1665            my $sprout = $self->DB();
1666            @retVal = $sprout->Genomes();
1667        }
1668        # Return the result.
1669        return @retVal;
1670    }
1671    
1672    =head3 GetHelpText
1673    
1674    C<< my $htmlText = $shelp->GetHelpText(); >>
1675    
1676    Get the help text for this search. The help text is stored in files on the template
1677    server. The help text for a specific search is taken from a file named
1678    C<SearchHelp_>I<class>C<.inc> in the template directory C<$FIG_Config::template_url>.
1679    There are also three standard help files: C<SearchHelp1_Filtering.inc> describes the
1680    feature filtering performed by the B<FeatureQuery> object, C<SearchHelp1_GenomeControl.inc>
1681    describes how to use a multiple-selection genome control, and C<SearchHelp1_Standard.inc>
1682    describes the standard controls for a search, such as page size, URL display, and
1683    external alias display.
1684    
1685    =cut
1686    
1687    sub GetHelpText {
1688      # Get the parameters.      # Get the parameters.
1689      my ($self) = @_;      my ($self) = @_;
1690        # Create a list to hold the pieces of the help.
1691        my @helps = ();
1692        # Get the template directory URL.
1693        my $urlBase = $FIG_Config::template_url;
1694        # Start with the specific help.
1695        my $class = $self->{class};
1696        push @helps, PageBuilder::GetPage("$urlBase/SearchHelp_$class.inc");
1697        # Add the genome control help if needed.
1698        if (scalar @{$self->{genomeParms}}) {
1699            push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_GenomeControl.inc");
1700        }
1701        # Next the filter help.
1702        if ($self->{filtered}) {
1703            push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_Filtering.inc");
1704        }
1705        # Finally, the standard help.
1706        push @helps, PageBuilder::GetPage("$urlBase/SearchHelp1_Standard.inc");
1707        # Assemble the pieces.
1708        my $retVal = join("\n<p>&nbsp;</p>\n", @helps);
1709      # Return the result.      # Return the result.
1710      return ['orgName', 'function', 'gblink', 'protlink'];      return $retVal;
1711  }  }
1712    
1713  =head3 FeatureColumnTitle  =head3 ComputeSearchURL
1714    
1715  C<< my $title = $shelp->FeatureColumnTitle($colName); >>  C<< my $url = $shelp->ComputeSearchURL(%overrides); >>
1716    
1717  Return the column heading title to be used for the specified feature column.  Compute the GET-style URL for the current search. In order for this to work, there
1718    must be a copy of the search form on the current page. This will always be the
1719    case if the search is coming from C<SearchSkeleton.cgi>.
1720    
1721    A little expense is involved in order to make the URL as smart as possible. The
1722    main complication is that if the user specified all genomes, we'll want to
1723    remove the parameter entirely from a get-style URL.
1724    
1725  =over 4  =over 4
1726    
1727  =item name  =item overrides
1728    
1729  Name of the desired feature column.  Hash containing override values for the parameters, where the parameter name is
1730    the key and the parameter value is the override value. If the override value is
1731    C<undef>, the parameter will be deleted from the result.
1732    
1733  =item RETURN  =item RETURN
1734    
1735  Returns the title to be used as the column header for the named feature column.  Returns a GET-style URL for invoking the search with the specified overrides.
1736    
1737  =back  =back
1738    
1739  =cut  =cut
1740    
1741  sub FeatureColumnTitle {  sub ComputeSearchURL {
1742      # Get the parameters.      # Get the parameters.
1743      my ($self, $colName) = @_;      my ($self, %overrides) = @_;
1744      # Declare the return variable. We default to a blank column name.      # Get the database and CGI query object.
1745      my $retVal = "&nbsp;";      my $cgi = $self->Q();
1746      # Process the column name.      my $sprout = $self->DB();
1747      if ($colName =~ /^X=(.+)$/) {      # Start with the full URL.
1748          # Here we have an extra column.      my $retVal = $cgi->url(-full => 1);
1749          $retVal = $1;      # Get all the query parameters in a hash.
1750      } elsif ($colName eq 'orgName') {      my %parms = $cgi->Vars();
1751          $retVal = "Name";      # Now we need to do some fixing. Each multi-valued parameter is encoded as a string with null
1752      } elsif ($colName eq 'fid') {      # characters separating the individual values. We have to convert those to lists. In addition,
1753          $retVal = "FIG ID";      # the multiple-selection genome parameters and the feature type parameter must be checked to
1754      } elsif ($colName eq 'alias') {      # determine whether or not they can be removed from the URL. First, we get a list of the
1755          $retVal = "External Aliases";      # genome parameters and a list of all genomes. Note that we only need the list if a
1756      } elsif ($colName eq 'function') {      # multiple-selection genome parameter has been found on the form.
1757          $retVal = "Functional Assignment";      my %genomeParms = map { $_ => 1 } @{$self->{genomeParms}};
1758      } elsif ($colName eq 'gblink') {      my @genomeList;
1759          $retVal = "GBrowse";      if (keys %genomeParms) {
1760      } elsif ($colName eq 'protlink') {          @genomeList = $sprout->Genomes();
1761          $retVal = "NMPDR Protein Page";      }
1762      } elsif ($colName eq 'group') {      # Create a list to hold the URL parameters we find.
1763          $retVal = "NMDPR Group";      my @urlList = ();
1764        # Now loop through the parameters in the hash, putting them into the output URL.
1765        for my $parmKey (keys %parms) {
1766            # Get a list of the parameter values. If there's only one, we'll end up with
1767            # a singleton list, but that's okay.
1768            my @values = split (/\0/, $parms{$parmKey});
1769            # Check for special cases.
1770            if (grep { $_ eq $parmKey } qw(SessionID ResultCount Page PageSize Trace TF ShowURL)) {
1771                # These are bookkeeping parameters we don't need to start a search.
1772                @values = ();
1773            } elsif ($parmKey =~ /_SearchThing$/) {
1774                # Here the value coming in is from a genome control's search thing. It does
1775                # not affect the results of the search, so we clear it.
1776                @values = ();
1777            } elsif ($genomeParms{$parmKey}) {
1778                # Here we need to see if the user wants all the genomes. If he does,
1779                # we erase all the values just like with features.
1780                my $allFlag = $sprout->IsAllGenomes(\@values, \@genomeList);
1781                if ($allFlag) {
1782                    @values = ();
1783                }
1784            } elsif (exists $overrides{$parmKey}) {
1785                # Here the value is being overridden, so we skip it for now.
1786                @values = ();
1787            }
1788            # If we still have values, create the URL parameters.
1789            if (@values) {
1790                push @urlList, map { "$parmKey=" . uri_escape($_) } @values;
1791            }
1792        }
1793        # Now do the overrides.
1794        for my $overKey (keys %overrides) {
1795            # Only use this override if it's not a delete marker.
1796            if (defined $overrides{$overKey}) {
1797                push @urlList, "$overKey=" . uri_escape($overrides{$overKey});
1798      }      }
1799        }
1800        # Add the parameters to the URL.
1801        $retVal .= "?" . join(";", @urlList);
1802      # Return the result.      # Return the result.
1803      return $retVal;      return $retVal;
1804  }  }
1805    
1806  =head3 FeatureColumnValue  =head3 GetRunTimeValue
1807    
1808  C<< my $value = $shelp->FeatureColumnValue($colName, $fid, \%extraCols); >>  C<< my $htmlText = $shelp->GetRunTimeValue($text); >>
1809    
1810  Return the value to be displayed in the specified feature column.  Compute a run-time column value.
1811    
1812  =over 4  =over 4
1813    
1814  =item colName  =item text
   
 Name of the column to be displayed.  
   
 =item record  
   
 DBObject record for the feature being displayed in the current row.  
   
 =item extraCols  
1815    
1816  Reference to a hash of extra column names to values. If the incoming column name  The run-time column text. It consists of 2 percent signs, a column type, an equal
1817  begins with C<X=>, its value will be taken from this hash.  sign, and the data for the current row.
1818    
1819  =item RETURN  =item RETURN
1820    
1821  Returns the HTML to be displayed in the named column for the specified feature.  Returns the fully-formatted HTML text to go into the current column of the current row.
1822    
1823    =back
1824    
1825    =cut
1826    
1827    sub GetRunTimeValue {
1828        # Get the parameters.
1829        my ($self, $text) = @_;
1830        # Declare the return variable.
1831        my $retVal;
1832        # Parse the incoming text.
1833        if ($text =~ /^%%([^=]+)=(.*)$/) {
1834            $retVal = $self->RunTimeColumns($1, $2);
1835        } else {
1836            Confess("Invalid run-time column string \"$text\" encountered in session file.");
1837        }
1838        # Return the result.
1839        return $retVal;
1840    }
1841    
1842    =head3 AdvancedClassList
1843    
1844    C<< my @classes = SearchHelper::AdvancedClassList(); >>
1845    
1846    Return a list of advanced class names. This list is used to generate the directory
1847    of available searches on the search page.
1848    
1849    We use the %INC variable to accomplish this.
1850    
1851    =cut
1852    
1853    sub AdvancedClassList {
1854        my @retVal = map { $_ =~ /^SH(\w+)\.pm/; $1 } grep { $_ =~ /^SH/ } keys %INC;
1855        return @retVal;
1856    }
1857    
1858    =head3 SelectionTree
1859    
1860    C<< my $htmlText = SearchHelper::SelectionTree($cgi, \%tree, %options); >>
1861    
1862    Display a selection tree.
1863    
1864    This method creates the HTML for a tree selection control. The tree is implemented as a set of
1865    nested HTML unordered lists. Each selectable element of the tree will contain a radio button. In
1866    addition, some of the tree nodes can contain hyperlinks.
1867    
1868    The tree itself is passed in as a multi-level list containing node names followed by
1869    contents. Each content element is a reference to a similar list. The first element of
1870    each list may be a hash reference. If so, it should contain one or both of the following
1871    keys.
1872    
1873    =over 4
1874    
1875    =item link
1876    
1877    The navigation URL to be popped up if the user clicks on the node name.
1878    
1879    =item value
1880    
1881    The form value to be returned if the user selects the tree node.
1882    
1883    =back
1884    
1885    The presence of a C<link> key indicates the node name will be hyperlinked. The presence of
1886    a C<value> key indicates the node name will have a radio button. If a node has no children,
1887    you may pass it a hash reference instead of a list reference.
1888    
1889    The following example shows the hash for a three-level tree with links on the second level and
1890    radio buttons on the third.
1891    
1892        [   Objects => [
1893                Entities => [
1894                    {link => "../docs/WhatIsAnEntity.html"},
1895                    Genome => {value => 'GenomeData'},
1896                    Feature => {value => 'FeatureData'},
1897                    Contig => {value => 'ContigData'},
1898                ],
1899                Relationships => [
1900                    {link => "../docs/WhatIsARelationShip.html"},
1901                    HasFeature => {value => 'GenomeToFeature'},
1902                    IsOnContig => {value => 'FeatureToContig'},
1903                ]
1904            ]
1905        ]
1906    
1907    Note how each leaf of the tree has a hash reference for its value, while the branch nodes
1908    all have list references.
1909    
1910    This next example shows how to set up a taxonomy selection field. The value returned
1911    by the tree control will be the taxonomy string for the selected node ready for use
1912    in a LIKE-style SQL filter. Only the single branch ending in campylobacter is shown for
1913    reasons of space.
1914    
1915        [   All => [
1916                {value => "%"},
1917                Bacteria => [
1918                    {value => "Bacteria%"},
1919                    Proteobacteria => [
1920                        {value => "Bacteria; Proteobacteria%"},
1921                        Epsilonproteobacteria => [
1922                            {value => "Bacteria; Proteobacteria;Epsilonproteobacteria%"},
1923                            Campylobacterales => [
1924                                {value => "Bacteria; Proteobacteria; Epsilonproteobacteria; Campylobacterales%"},
1925                                Campylobacteraceae =>
1926                                    {value => "Bacteria; Proteobacteria; Epsilonproteobacteria; Campylobacterales; Campylobacteraceae%"},
1927                                ...
1928                            ]
1929                            ...
1930                        ]
1931                        ...
1932                    ]
1933                    ...
1934                ]
1935                ...
1936            ]
1937        ]
1938    
1939    
1940    This method of tree storage allows the caller to control the order in which the tree nodes
1941    are displayed and to completely control value selection and use of hyperlinks. It is, however
1942    a bit complicated. Eventually, tree-building classes will be provided to simplify things.
1943    
1944    The parameters to this method are as follows.
1945    
1946    =over 4
1947    
1948    =item cgi
1949    
1950    CGI object used to generate the HTML.
1951    
1952    =item tree
1953    
1954    Reference to a hash describing a tree. See the description above.
1955    
1956    =item options
1957    
1958    Hash containing options for the tree display.
1959    
1960    =back
1961    
1962    The allowable options are as follows
1963    
1964    =over 4
1965    
1966    =item nodeImageClosed
1967    
1968    URL of the image to display next to the tree nodes when they are collapsed. Clicking
1969    on the image will expand a section of the tree. The default is C<../FIG/Html/plus.gif>.
1970    
1971    =item nodeImageOpen
1972    
1973    URL of the image to display next to the tree nodes when they are expanded. Clicking
1974    on the image will collapse a section of the tree. The default is C<../FIG/Html/minus.gif>.
1975    
1976    =item style
1977    
1978    Style to use for the tree. The default is C<tree>. Because the tree style is implemented
1979    as nested lists, the key components of this style are the definitions for the C<ul> and
1980    C<li> tags. The default style file contains the following definitions.
1981    
1982        .tree ul {
1983           margin-left: 0; padding-left: 22px
1984        }
1985        .tree li {
1986            list-style-type: none;
1987        }
1988    
1989    The default image is 22 pixels wide, so in the above scheme each tree level is indented from its
1990    parent by the width of the node image. This use of styles limits the things we can do in formatting
1991    the tree, but it has the advantage of vastly simplifying the tree creation.
1992    
1993    =item name
1994    
1995    Field name to give to the radio buttons in the tree. The default is C<selection>.
1996    
1997    =item target
1998    
1999    Frame target for links. The default is C<_self>.
2000    
2001    =item selected
2002    
2003    If specified, the value of the radio button to be pre-selected.
2004    
2005    =back
2006    
2007    =cut
2008    
2009    sub SelectionTree {
2010        # Get the parameters.
2011        my ($cgi, $tree, %options) = @_;
2012        # Get the options.
2013        my $optionThing = Tracer::GetOptions({ name => 'selection',
2014                                               nodeImageClosed => '../FIG/Html/plus.gif',
2015                                               nodeImageOpen => '../FIG/Html/minus.gif',
2016                                               style => 'tree',
2017                                               target => '_self',
2018                                               selected => undef},
2019                                             \%options);
2020        # Declare the return variable. We'll do the standard thing with creating a list
2021        # of HTML lines and rolling them together at the end.
2022        my @retVal = ();
2023        # Only proceed if the tree is present.
2024        if (defined($tree)) {
2025            # Validate the tree.
2026            if (ref $tree ne 'ARRAY') {
2027                Confess("Selection tree is not a list reference.");
2028            } elsif (scalar @{$tree} == 0) {
2029                # The tree is empty, so we do nothing.
2030            } elsif ($tree->[0] eq 'HASH') {
2031                Confess("Hash reference found at start of selection tree. The tree as a whole cannot have attributes, only tree nodes.");
2032            } else {
2033                # Here we have a real tree. Apply the tree style.
2034                push @retVal, $cgi->start_div({ class => $optionThing->{style} });
2035                # Give us a DIV ID.
2036                my $divID = GetDivID($optionThing->{name});
2037                # Show the tree.
2038                push @retVal, ShowBranch($cgi, "(root)", $divID, $tree, $optionThing, 'block');
2039                # Close the DIV block.
2040                push @retVal, $cgi->end_div();
2041            }
2042        }
2043        # Return the result.
2044        return join("\n", @retVal, "");
2045    }
2046    
2047    =head3 ShowBranch
2048    
2049    C<< my @htmlLines = SearchHelper::ShowBranch($cgi, $label, $id, $branch, $options, $displayType); >>
2050    
2051    This is a recursive method that displays a branch of the tree.
2052    
2053    =over 4
2054    
2055    =item cgi
2056    
2057    CGI object used to format HTML.
2058    
2059    =item label
2060    
2061    Label of this tree branch. It is only used in error messages.
2062    
2063    =item id
2064    
2065    ID to be given to this tree branch. The ID is used in the code that expands and collapses
2066    tree nodes.
2067    
2068    =item branch
2069    
2070    Reference to a list containing the content of the tree branch. The list contains an optional
2071    hash reference that is ignored and the list of children, each child represented by a name
2072    and then its contents. The contents could by a hash reference (indicating the attributes
2073    of a leaf node), or another tree branch.
2074    
2075    =item options
2076    
2077    Options from the original call to L</SelectionTree>.
2078    
2079    =item displayType
2080    
2081    C<block> if the contents of this list are to be displayed, C<none> if they are to be
2082    hidden.
2083    
2084    =item RETURN
2085    
2086    Returns one or more HTML lines that can be used to display the tree branch.
2087    
2088    =back
2089    
2090    =cut
2091    
2092    sub ShowBranch {
2093        # Get the parameters.
2094        my ($cgi, $label, $id, $branch, $options, $displayType) = @_;
2095        # Declare the return variable.
2096        my @retVal = ();
2097        # Start the branch.
2098        push @retVal, $cgi->start_ul({ id => $id, style => "display:$displayType" });
2099        # Check for the hash and choose the start location accordingly.
2100        my $i0 = (ref $branch->[0] eq 'HASH' ? 1 : 0);
2101        # Get the list length.
2102        my $i1 = scalar(@{$branch});
2103        # Verify we have an even number of elements.
2104        if (($i1 - $i0) % 2 != 0) {
2105            Trace("Branch elements are from $i0 to $i1.") if T(3);
2106            Confess("Odd number of elements in tree branch $label.");
2107        } else {
2108            # Loop through the elements.
2109            for (my $i = $i0; $i < $i1; $i += 2) {
2110                # Get this node's label and contents.
2111                my ($myLabel, $myContent) = ($branch->[$i], $branch->[$i+1]);
2112                # Get an ID for this node's children (if any).
2113                my $myID = GetDivID($options->{name});
2114                # Now we need to find the list of children and the options hash.
2115                # This is a bit ugly because we allow the shortcut of a hash without an
2116                # enclosing list. First, we need some variables.
2117                my $attrHash = {};
2118                my @childHtml = ();
2119                my $hasChildren = 0;
2120                if (! ref $myContent) {
2121                    Confess("Invalid tree definition. Scalar found as content of node \"$myLabel\".");
2122                } elsif (ref $myContent eq 'HASH') {
2123                    # Here the node is a leaf and its content contains the link/value hash.
2124                    $attrHash = $myContent;
2125                } elsif (ref $myContent eq 'ARRAY') {
2126                    # Here the node may be a branch. Its content is a list.
2127                    my $len = scalar @{$myContent};
2128                    if ($len >= 1) {
2129                        # Here the first element of the list could by the link/value hash.
2130                        if (ref $myContent->[0] eq 'HASH') {
2131                            $attrHash = $myContent->[0];
2132                            # If there's data in the list besides the hash, it's our child list.
2133                            # We can pass the entire thing as the child list, because the hash
2134                            # is ignored.
2135                            if ($len > 1) {
2136                                $hasChildren = 1;
2137                            }
2138                        } else {
2139                            $hasChildren = 1;
2140                        }
2141                        # If we have children, create the child list with a recursive call.
2142                        if ($hasChildren) {
2143                            Trace("Processing children of $myLabel.") if T(4);
2144                            push @childHtml, ShowBranch($cgi, $myLabel, $myID, $myContent, $options, 'none');
2145                        }
2146                    }
2147                }
2148                # Okay, it's time to pause and take stock. We have the label of the current node
2149                # in $myLabel, its attributes in $attrHash, and if it is NOT a leaf node, we
2150                # have a child list in @childHtml. If it IS a leaf node, $hasChildren is 0.
2151                # Compute the image HTML. It's tricky, because we have to deal with the open and
2152                # closed images.
2153                my @images = ($options->{nodeImageOpen}, $options->{nodeImageClosed});
2154                my $image = $images[$hasChildren];
2155                my $prefixHtml = $cgi->img({src => $image, id => "${myID}img"});
2156                if ($hasChildren) {
2157                    # If there are children, we wrap the image in a toggle hyperlink.
2158                    $prefixHtml = $cgi->a({ onClick => "javascript:treeToggle('$myID','$images[0]', '$images[1]')" },
2159                                          $prefixHtml);
2160                }
2161                # Now the radio button, if any. Note we use "defined" in case the user wants the
2162                # value to be 0.
2163                if (defined $attrHash->{value}) {
2164                    # Due to a glitchiness in the CGI stuff, we have to build the attribute
2165                    # hash for the "input" method. If the item is pre-selected, we add
2166                    # "checked => undef" to the hash. Otherwise, we can't have "checked"
2167                    # at all.
2168                    my $radioParms = { type => 'radio',
2169                                       name => $options->{name},
2170                                       value => $attrHash->{value},
2171                                     };
2172                    if (defined $options->{selected} && $options->{selected} eq $attrHash->{value}) {
2173                        $radioParms->{checked} = undef;
2174                    }
2175                    $prefixHtml .= $cgi->input($radioParms);
2176                }
2177                # Next, we format the label.
2178                my $labelHtml = $myLabel;
2179                Trace("Formatting tree node for $myLabel.") if T(4);
2180                # Apply a hyperlink if necessary.
2181                if (defined $attrHash->{link}) {
2182                    $labelHtml = $cgi->a({ href => $attrHash->{link}, target => $options->{target} },
2183                                         $labelHtml);
2184                }
2185                # Finally, roll up the child HTML. If there are no children, we'll get a null string
2186                # here.
2187                my $childHtml = join("\n", @childHtml);
2188                # Now we have all the pieces, so we can put them together.
2189                push @retVal, $cgi->li("$prefixHtml$labelHtml$childHtml");
2190            }
2191        }
2192        # Close the tree branch.
2193        push @retVal, $cgi->end_ul();
2194        # Return the result.
2195        return @retVal;
2196    }
2197    
2198    =head3 GetDivID
2199    
2200    C<< my $idString = SearchHelper::GetDivID($name); >>
2201    
2202    Return a new HTML ID string.
2203    
2204    =over 4
2205    
2206    =item name
2207    
2208    Name to be prefixed to the ID string.
2209    
2210    =item RETURN
2211    
2212    Returns a hopefully-unique ID string.
2213    
2214    =back
2215    
2216    =cut
2217    
2218    sub GetDivID {
2219        # Get the parameters.
2220        my ($name) = @_;
2221        # Compute the ID.
2222        my $retVal = "elt_$name$divCount";
2223        # Increment the counter to make sure this ID is not re-used.
2224        $divCount++;
2225        # Return the result.
2226        return $retVal;
2227    }
2228    
2229    =head2 Feature Column Methods
2230    
2231    The methods in this section manage feature column data. If you want to provide the
2232    capability to include new types of data in feature columns, then all the changes
2233    are made to this section of the source file. Technically, this should be implemented
2234    using object-oriented methods, but this is simpler for non-programmers to maintain.
2235    To add a new column of feature data, you must first give it a name. For example,
2236    the name for the protein page link column is C<protlink>. If the column is to appear
2237    in the default list of feature columns, add it to the list returned by
2238    L</DefaultFeatureColumns>. Then add code to produce the column title to
2239    L</FeatureColumnTitle> and code to produce its value to L</FeatureColumnValue>, and
2240    everything else will happen automatically.
2241    
2242    There is one special column name syntax for extra columns (that is, nonstandard
2243    feature columns). If the column name begins with C<X=>, then it is presumed to be
2244    an extra column. The column title is the text after the C<X=>, and its value is
2245    pulled from the extra column hash.
2246    
2247    =head3 DefaultFeatureColumns
2248    
2249    C<< my @colNames = $shelp->DefaultFeatureColumns(); >>
2250    
2251    Return a list of the default feature column identifiers. These identifiers can
2252    be passed to L</FeatureColumnTitle> and L</FeatureColumnValue> in order to
2253    produce the column titles and row values.
2254    
2255    =cut
2256    
2257    sub DefaultFeatureColumns {
2258        # Get the parameters.
2259        my ($self) = @_;
2260        # Return the result.
2261        return qw(orgName function gblink protlink);
2262    }
2263    
2264    =head3 FeatureColumnTitle
2265    
2266    C<< my $title = $shelp->FeatureColumnTitle($colName); >>
2267    
2268    Return the column heading title to be used for the specified feature column.
2269    
2270    =over 4
2271    
2272    =item name
2273    
2274    Name of the desired feature column.
2275    
2276    =item RETURN
2277    
2278    Returns the title to be used as the column header for the named feature column.
2279    
2280    =back
2281    
2282    =cut
2283    
2284    sub FeatureColumnTitle {
2285        # Get the parameters.
2286        my ($self, $colName) = @_;
2287        # Declare the return variable. We default to a blank column name.
2288        my $retVal = "&nbsp;";
2289        # Process the column name.
2290        if ($colName =~ /^X=(.+)$/) {
2291            # Here we have an extra column.
2292            $retVal = $1;
2293        } elsif ($colName eq 'alias') {
2294            $retVal = "External Aliases";
2295        } elsif ($colName eq 'fid') {
2296            $retVal = "FIG ID";
2297        } elsif ($colName eq 'function') {
2298            $retVal = "Functional Assignment";
2299        } elsif ($colName eq 'gblink') {
2300            $retVal = "GBrowse";
2301        } elsif ($colName eq 'group') {
2302            $retVal = "NMDPR Group";
2303        } elsif ($colName =~ /^keyword:(.+)$/) {
2304            $retVal = ucfirst $1;
2305        } elsif ($colName eq 'orgName') {
2306            $retVal = "Gene Name";
2307        } elsif ($colName eq 'protlink') {
2308            $retVal = "NMPDR Protein Page";
2309        } elsif ($colName eq 'subsystem') {
2310            $retVal = "Subsystems";
2311        }
2312        # Return the result.
2313        return $retVal;
2314    }
2315    
2316    
2317    =head3 FeatureColumnValue
2318    
2319    C<< my $value = $shelp->FeatureColumnValue($colName, $fid, \%extraCols); >>
2320    
2321    Return the value to be displayed in the specified feature column.
2322    
2323    =over 4
2324    
2325    =item colName
2326    
2327    Name of the column to be displayed.
2328    
2329    =item record
2330    
2331    DBObject record for the feature being displayed in the current row.
2332    
2333    =item extraCols
2334    
2335    Reference to a hash of extra column names to values. If the incoming column name
2336    begins with C<X=>, its value will be taken from this hash.
2337    
2338    =item RETURN
2339    
2340    Returns the HTML to be displayed in the named column for the specified feature.
2341    
2342  =back  =back
2343    
# Line 1303  Line 2364 
2364          if (defined $extraCols->{$1}) {          if (defined $extraCols->{$1}) {
2365              $retVal = $extraCols->{$1};              $retVal = $extraCols->{$1};
2366          }          }
2367      } elsif ($colName eq 'orgName') {      } elsif ($colName eq 'alias') {
2368          # Here we want the formatted organism name and feature number.          # In this case, the user wants a list of external aliases for the feature.
2369          $retVal = $self->FeatureName($fid);          # These are very expensive, so we compute them when the row is displayed.
2370            $retVal = "%%alias=$fid";
2371      } elsif ($colName eq 'fid') {      } elsif ($colName eq 'fid') {
2372          # Here we have the raw feature ID. We hyperlink it to the protein page.          # Here we have the raw feature ID. We hyperlink it to the protein page.
2373          $retVal = HTML::set_prot_links($fid);          $retVal = HTML::set_prot_links($fid);
     } elsif ($colName eq 'alias') {  
         # In this case, the user wants a list of external aliases for the feature.  
         # The complicated part is we have to hyperlink them. First, get the  
         # aliases.  
         my @aliases = $sprout->FeatureAliases($fid);  
         # Only proceed if we found some.  
         if (@aliases) {  
             # Join the aliases into a comma-delimited list.  
             my $aliasList = join(", ", @aliases);  
             # Ask the HTML processor to hyperlink them.  
             $retVal = HTML::set_prot_links($aliasList);  
         }  
2374      } elsif ($colName eq 'function') {      } elsif ($colName eq 'function') {
2375          # The functional assignment is just a matter of getting some text.          # The functional assignment is just a matter of getting some text.
2376          ($retVal) = $record->Value('Feature(assignment)');          ($retVal) = $record->Value('Feature(assignment)');
2377      } elsif ($colName eq 'gblink') {      } elsif ($colName eq 'gblink') {
2378          # Here we want a link to the GBrowse page using the official GBrowse button.          # Here we want a link to the GBrowse page using the official GBrowse button.
2379          my $gurl = "GetGBrowse.cgi?fid=$fid";          $retVal = Formlet('GBrowse', "GetGBrowse.cgi", undef,
2380          $retVal = $cgi->a({ href => $gurl, title => "GBrowse for $fid" },                            fid => $fid);
                           $cgi->img({ src => "../images/button-gbrowse.png",  
                                       border => 0 })  
                          );  
     } elsif ($colName eq 'protlink') {  
         # Here we want a link to the protein page using the official NMPDR button.  
         my $hurl = HTML::fid_link($cgi, $fid, 0, 1);  
         $retVal = $cgi->a({ href => $hurl, title => "Protein page for $fid" },  
                           $cgi->img({ src => "../images/button-nmpdr.png",  
                                      border => 0 })  
                          );  
2381      } elsif ($colName eq 'group') {      } elsif ($colName eq 'group') {
2382          # Get the NMPDR group name.          # Get the NMPDR group name.
2383          my (undef, $group) = $self->OrganismData($fid);          my (undef, $group) = $self->OrganismData($fid);
# Line 1345  Line 2385 
2385          my $nurl = $sprout->GroupPageName($group);          my $nurl = $sprout->GroupPageName($group);
2386          $retVal = $cgi->a({ href => $nurl, title => "$group summary" },          $retVal = $cgi->a({ href => $nurl, title => "$group summary" },
2387                            $group);                            $group);
2388        } elsif ($colName =~ /^keyword:(.+)$/) {
2389            # Here we want keyword-related values. This is also expensive, so
2390            # we compute them when the row is displayed.
2391            $retVal = "%%$colName=$fid";
2392        } elsif ($colName eq 'orgName') {
2393            # Here we want the formatted organism name and feature number.
2394            $retVal = $self->FeatureName($fid);
2395        } elsif ($colName eq 'protlink') {
2396            # Here we want a link to the protein page using the official NMPDR button.
2397            $retVal = Formlet('NMPDR', "protein.cgi", undef,
2398                              prot => $fid, SPROUT => 1, new_framework => 0,
2399                              user => '');
2400        }elsif ($colName eq 'subsystem') {
2401            # Another run-time column: subsystem list.
2402            $retVal = "%%subsystem=$fid";
2403        }
2404        # Return the result.
2405        return $retVal;
2406    }
2407    
2408    =head3 RunTimeColumns
2409    
2410    C<< my $htmlText = $shelp->RunTimeColumns($type, $text); >>
2411    
2412    Return the HTML text for a run-time column. Run-time columns are evaluated when the
2413    list is displayed, rather than when it is generated.
2414    
2415    =over 4
2416    
2417    =item type
2418    
2419    Type of column.
2420    
2421    =item text
2422    
2423    Data relevant to this row of the column.
2424    
2425    =item RETURN
2426    
2427    Returns the fully-formatted HTML text to go in the specified column.
2428    
2429    =back
2430    
2431    =cut
2432    
2433    sub RunTimeColumns {
2434        # Get the parameters.
2435        my ($self, $type, $text) = @_;
2436        # Declare the return variable.
2437        my $retVal = "";
2438        # Get the Sprout and CGI objects.
2439        my $sprout = $self->DB();
2440        my $cgi = $self->Q();
2441        Trace("Runtime column $type with text \"$text\" found.") if T(4);
2442        # Separate the text into a type and data.
2443        if ($type eq 'alias') {
2444            # Here the caller wants external alias links for a feature. The text
2445            # is the feature ID.
2446            my $fid = $text;
2447            # The complicated part is we have to hyperlink them. First, get the
2448            # aliases.
2449            Trace("Generating aliases for feature $fid.") if T(4);
2450            my @aliases = $sprout->FeatureAliases($fid);
2451            # Only proceed if we found some.
2452            if (@aliases) {
2453                # Join the aliases into a comma-delimited list.
2454                my $aliasList = join(", ", @aliases);
2455                # Ask the HTML processor to hyperlink them.
2456                $retVal = HTML::set_prot_links($cgi, $aliasList);
2457            }
2458        } elsif ($type eq 'subsystem') {
2459            # Here the caller wants the subsystems in which this feature participates.
2460            # The text is the feature ID. We will list the subsystem names with links
2461            # to the subsystem's summary page.
2462            my $fid = $text;
2463            # Get the subsystems.
2464            Trace("Generating subsystems for feature $fid.") if T(4);
2465            my %subs = $sprout->SubsystemsOf($fid);
2466            # Extract the subsystem names.
2467            my @names = map { HTML::sub_link($cgi, $_) } sort keys %subs;
2468            # String them into a list.
2469            $retVal = join(", ", @names);
2470        } elsif ($type =~ /^keyword:(.+)$/) {
2471            # Here the caller wants the value of the named keyword. The text is the
2472            # feature ID.
2473            my $keywordName = $1;
2474            my $fid = $text;
2475            # Get the attribute values.
2476            Trace("Getting $keywordName values for feature $fid.") if T(4);
2477            my @values = $sprout->GetFlat(['Feature'], "Feature(id) = ?", [$fid],
2478                                          "Feature($keywordName)");
2479            # String them into a list.
2480            $retVal = join(", ", @values);
2481      }      }
2482      # Return the result.      # Return the result.
2483      return $retVal;      return $retVal;
2484  }  }
2485    
2486    =head3 SaveOrganismData
2487    
2488    C<< my ($name, $displayGroup) = $shelp->SaveOrganismData($group, $genomeID, $genus, $species, $strain); >>
2489    
2490    Format the name of an organism and the display version of its group name. The incoming
2491    data should be the relevant fields from the B<Genome> record in the database. The
2492    data will also be stored in the genome cache for later use in posting search results.
2493    
2494    =over 4
2495    
2496    =item group
2497    
2498    Name of the genome's group as it appears in the database.
2499    
2500    =item genomeID
2501    
2502    ID of the relevant genome.
2503    
2504    =item genus
2505    
2506    Genus of the genome's organism. If undefined or null, it will be assumed the genome is not
2507    in the database. In this case, the organism name is derived from the genomeID and the group
2508    is automatically the supporting-genomes group.
2509    
2510    =item species
2511    
2512    Species of the genome's organism.
2513    
2514    =item strain
2515    
2516    Strain of the species represented by the genome.
2517    
2518    =item RETURN
2519    
2520    Returns a two-element list. The first element is the formatted genome name. The second
2521    element is the display name of the genome's group.
2522    
2523    =back
2524    
2525    =cut
2526    
2527    sub SaveOrganismData {
2528        # Get the parameters.
2529        my ($self, $group, $genomeID, $genus, $species, $strain) = @_;
2530        # Declare the return values.
2531        my ($name, $displayGroup);
2532        # If the organism does not exist, format an unknown name and a blank group.
2533        if (! defined($genus)) {
2534            $name = "Unknown Genome $genomeID";
2535            $displayGroup = "";
2536        } else {
2537            # It does exist, so format the organism name.
2538            $name = "$genus $species";
2539            if ($strain) {
2540                $name .= " $strain";
2541            }
2542            # Compute the display group. This is currently the same as the incoming group
2543            # name unless it's the supporting group, which is nulled out.
2544            $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
2545        }
2546        # Cache the group and organism data.
2547        my $cache = $self->{orgs};
2548        $cache->{$genomeID} = [$name, $displayGroup];
2549        # Return the result.
2550        return ($name, $displayGroup);
2551    }
2552    
2553    =head3 ValidateKeywords
2554    
2555    C<< my $okFlag = $shelp->ValidateKeywords($keywordString, $required); >>
2556    
2557    Insure that a keyword string is reasonably valid. If it is invalid, a message will be
2558    set.
2559    
2560    =over 4
2561    
2562    =item keywordString
2563    
2564    Keyword string specified as a parameter to the current search.
2565    
2566    =item required
2567    
2568    TRUE if there must be at least one keyword specified, else FALSE.
2569    
2570    =item RETURN
2571    
2572    Returns TRUE if the keyword string is valid, else FALSE. Note that a null keyword string
2573    is acceptable if the I<$required> parameter is not specified.
2574    
2575    =back
2576    
2577    =cut
2578    
2579    sub ValidateKeywords {
2580        # Get the parameters.
2581        my ($self, $keywordString, $required) = @_;
2582        # Declare the return variable.
2583        my $retVal = 0;
2584        my @wordList = split /\s+/, $keywordString;
2585        # Right now our only real worry is a list of all minus words. The problem with it is that
2586        # it will return an incorrect result.
2587        my @plusWords = grep { $_ =~ /^[^\-]/ } @wordList;
2588        if (! @wordList) {
2589            if ($required) {
2590                $self->SetMessage("No search words specified.");
2591            }
2592        } elsif (! @plusWords) {
2593            $self->SetMessage("At least one keyword must be positive. All the keywords entered are preceded by minus signs.");
2594        } else {
2595            $retVal = 1;
2596        }
2597        # Return the result.
2598        return $retVal;
2599    }
2600    
2601    =head3 Formlet
2602    
2603    C<< my $html = SearchHelper::Formlet($caption, $url, $target, %parms); >>
2604    
2605    Create a mini-form that posts to the specified URL with the specified parameters. The
2606    parameters will be stored in hidden fields, and the form's only visible control will
2607    be a submit button with the specified caption.
2608    
2609    Note that we don't use B<CGI.pm> services here because they generate forms with extra characters
2610    and tags that we don't want to deal with.
2611    
2612    =over 4
2613    
2614    =item caption
2615    
2616    Caption to be put on the form button.
2617    
2618    =item url
2619    
2620    URL to be put in the form's action parameter.
2621    
2622    =item target
2623    
2624    Frame or target in which the form results should appear. If C<undef> is specified,
2625    the default target will be used.
2626    
2627    =item parms
2628    
2629    Hash containing the parameter names as keys and the parameter values as values.
2630    
2631    =back
2632    
2633    =cut
2634    
2635    sub Formlet {
2636        # Get the parameters.
2637        my ($caption, $url, $target, %parms) = @_;
2638        # Compute the target HTML.
2639        my $targetHtml = ($target ? " target=\"$target\"" : "");
2640        # Start the form.
2641        my $retVal = "<form method=\"POST\" action=\"$url\"$target>";
2642        # Add the parameters.
2643        for my $parm (keys %parms) {
2644            $retVal .= "<input type=\"hidden\" name=\"$parm\" value=\"$parms{$parm}\" />";
2645        }
2646        # Put in the button.
2647        $retVal .= "<input type=\"submit\" name=\"submit\" value=\"$caption\" class=\"button\" />";
2648        # Close the form.
2649        $retVal .= "</form>";
2650        # Return the result.
2651        return $retVal;
2652    }
2653    
2654    =head2 Virtual Methods
2655    
2656    =head3 Form
2657    
2658    C<< my $html = $shelp->Form(); >>
2659    
2660    Generate the HTML for a form to request a new search.
2661    
2662    =head3 Find
2663    
2664    C<< my $resultCount = $shelp->Find(); >>
2665    
2666    Conduct a search based on the current CGI query parameters. The search results will
2667    be written to the session cache file and the number of results will be
2668    returned. If the search parameters are invalid, a result count of C<undef> will be
2669    returned and a result message will be stored in this object describing the problem.
2670    
2671    =head3 Description
2672    
2673    C<< my $htmlText = $shelp->Description(); >>
2674    
2675    Return a description of this search. The description is used for the table of contents
2676    on the main search tools page. It may contain HTML, but it should be character-level,
2677    not block-level, since the description is going to appear in a list.
2678    
2679    =head3 SortKey
2680    
2681    C<< my $key = $shelp->SortKey($fdata); >>
2682    
2683    Return the sort key for the specified feature data. The default is to sort by feature name,
2684    floating NMPDR organisms to the top. If a full-text search is used, then the default
2685    sort is by relevance followed by feature name. This sort may be overridden by the
2686    search class to provide fancier functionality. This method is called by
2687    B<PutFeature>, so it is only used for feature searches. A non-feature search
2688    would presumably have its own sort logic.
2689    
2690    =over 4
2691    
2692    =item record
2693    
2694    The C<FeatureData> containing the current feature.
2695    
2696    =item RETURN
2697    
2698    Returns a key field that can be used to sort this row in among the results.
2699    
2700    =back
2701    
2702    =cut
2703    
2704    sub SortKey {
2705        # Get the parameters.
2706        my ($self, $fdata) = @_;
2707        # Get the feature ID from the record.
2708        my $fid = $fdata->FID();
2709        # Get the group from the feature ID.
2710        my $group = $self->FeatureGroup($fid);
2711        # Ask the feature query object to form the sort key.
2712        my $retVal = $fdata->SortKey($self, $group);
2713        # Return the result.
2714        return $retVal;
2715    }
2716    
2717  1;  1;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.20

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3