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

Diff of /Sprout/SHWordSearch.pm

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

revision 1.2, Wed Nov 8 23:19:22 2006 UTC revision 1.9, Mon Jul 16 20:04:51 2007 UTC
# Line 4  Line 4 
4    
5      use strict;      use strict;
6      use Tracer;      use Tracer;
     use SearchHelper;  
7      use CGI;      use CGI;
8      use HTML;      use HTML;
9      use Sprout;      use Sprout;
10      use FeatureData;      use RHFeatures;
11      use FeatureQuery;      use base 'SearchHelper';
   
     our @ISA = qw(SearchHelper);  
12    
13  =head1 Simple Keyword Search Feature Search Helper  =head1 Simple Keyword Search Feature Search Helper
14    
# Line 69  Line 66 
66    
67  =head3 Form  =head3 Form
68    
69  C<< my $html = $shelp->Include(); >>  C<< my $html = $shelp->Form(); >>
70    
71  Generate the HTML for a form to request a new search.  Generate the HTML for a form to request a new search.
72    
# Line 86  Line 83 
83      # Declare a variable to hold the table rows.      # Declare a variable to hold the table rows.
84      my @rows = ();      my @rows = ();
85      # The first row is for the keyword search expression.      # The first row is for the keyword search expression.
86      my $expressionString = $cgi->param('keywords') || "";      push @rows, RHFeatures::WordSearchRow($self);
87      push @rows, $cgi->Tr($cgi->td("Search Words"),      # The second row is for special options.
88                           $cgi->td({colspan => 2}, $cgi->textfield(-name => 'keywords',      push @rows, RHFeatures::FeatureFilterFormRows($self, 'options');
89                                                                    -value => $expressionString,      # The last row is for the submit button.
                                                                   -size => 40)));  
     # The other row is for the submit button.  
90      push @rows, $self->SubmitRow();      push @rows, $self->SubmitRow();
91      # Finally, if groups are specified, we include them as hidden fields and display      # Finally, if groups are specified, we include them as hidden fields and display
92      # an explanation.      # an explanation.
# Line 138  Line 133 
133      # Declare the return variable. If it remains undefined, the caller will      # Declare the return variable. If it remains undefined, the caller will
134      # know that an error occurred.      # know that an error occurred.
135      my $retVal;      my $retVal;
136      # Get the keyword list. We need to prove that there is at least one keyword      # Get the result helper.
137      # that does not begin with a "-".      my $rhelp = RHFeatures->new($self);
138      my $keywords = $cgi->param('keywords') || "";      # Validate the filtering parameters.
139      my @wordList = split /\s+/, $keywords;      $rhelp->KeywordsRequired();
140      my @plusWords = grep { $_ =~ /^[^\-]/ } @wordList;      if ($rhelp->Valid()) {
     if (! @wordList) {  
         $self->SetMessage("No search words specified.");  
     } elsif (! @plusWords) {  
         $self->SetMessage("At least one keyword must be positive. All the keywords entered are preceded by minus signs.");  
     } else {  
         # We have good keywords. Initialize the session file.  
         $self->OpenSession();  
141          # Initialize the result counter.          # Initialize the result counter.
142          $retVal = 0;          $retVal = 0;
143            # Get the default columns.
144            $self->DefaultColumns($rhelp);
145            Trace("Column list is " . join(", ", @{$rhelp->GetColumnHeaders()})) if T(3);
146            # Start the output session.
147            $self->OpenSession($rhelp);
148            # Get the keywords.
149            my $keywords = $cgi->param('keywords') || '';
150          # Check for groups.          # Check for groups.
151          my @groups = $cgi->param('group');          my @groups = $cgi->param('group');
152          if (@groups) {          if (@groups) {
153              # Here we do the search a group at a time.              # Here we do the search a group at a time.
154              for my $group (@groups) {              for my $group (@groups) {
155                    Trace("Starting the search.") if T(3);
156                    $self->PrintLine("Submitting search query for $group.<br />");
157                  my $query = $sprout->Search($keywords, 0, ['Feature', 'HasFeature', 'Genome'],                  my $query = $sprout->Search($keywords, 0, ['Feature', 'HasFeature', 'Genome'],
158                                              "Genome(primary-group) = ?", [$group]);                                              "Genome(primary-group) = ?", [$group]);
159                  $retVal += $self->ProcessQuery($query);                  Trace("Processing results.") if T(3);
160                    $retVal += $self->ProcessQuery($rhelp, $query);
161                    Trace("Results processed.") if T(3);
162              }              }
163          } else {          } else {
164              # Here we do one search just for features.              # Here we do one search just for features.
165                Trace("Starting the search.") if T(3);
166                $self->PrintLine("Submitting search query for all genomes.<br />");
167              my $query = $sprout->Search($keywords, 0, ['Feature']);              my $query = $sprout->Search($keywords, 0, ['Feature']);
168              $retVal += $self->ProcessQuery($query);              Trace("Processing results.") if T(3);
169                $retVal += $self->ProcessQuery($rhelp, $query);
170                Trace("Results processed.") if T(3);
171          }          }
172          # Close the session file.          # Close the session file.
173          $self->CloseSession();          $self->CloseSession();
174            Trace("Session closed.") if T(3);
175      }      }
176      # Return the result count.      # Return the result count.
177      return $retVal;      return $retVal;
178  }  }
179    
180    =head3 SearchTitle
181    
182    C<< my $titleHtml = $shelp->SearchTitle(); >>
183    
184    Return the display title for this search. The display title appears above the search results.
185    If no result is returned, no title will be displayed. The result should be an html string
186    that can be legally put inside a block tag such as C<h3> or C<p>.
187    
188    =cut
189    
190    sub SearchTitle {
191        # Get the parameters.
192        my ($self) = @_;
193        # Compute the title.
194        my $cgi = $self->Q();
195        my $words = $cgi->param('keywords');
196        my $retVal = "Keyword Search for $words.";
197        # Return it.
198        return $retVal;
199    }
200    
201  =head3 Description  =head3 Description
202    
203  C<< my $htmlText = $shelp->Description(); >>  C<< my $htmlText = $shelp->Description(); >>
# Line 187  Line 212 
212      # Get the parameters.      # Get the parameters.
213      my ($self) = @_;      my ($self) = @_;
214      # Return the result.      # Return the result.
215      return "Search for features based on keywords.";      return "Search for genes based on keywords.";
216  }  }
217    
218  =head3 ProcessQuery  =head3 ProcessQuery
219    
220  C<< my $count = $shelp->ProcessQuery($query); >>  C<< my $count = $shelp->ProcessQuery($rhelp, $query); >>
221    
222  Run through the results of a query, sending all the features retrieved to the output  Run through the results of a query, sending all the features retrieved to the output
223  cache. The number of features found will be returned to the caller.  cache. The number of features found will be returned to the caller.
224    
225  =over 4  =over 4
226    
227    =item rhelp
228    
229    Current result helper object, which should be B<RHFeatures>.
230    
231  =item query  =item query
232    
233  A B<DBQuery> object that returns features.  A B<DBQuery> object that returns features.
# Line 213  Line 242 
242    
243  sub ProcessQuery {  sub ProcessQuery {
244      # Get the parameters.      # Get the parameters.
245      my ($self, $query) = @_;      my ($self, $rhelp, $query) = @_;
246        my $cgi = $self->Q();
247      # Clear the result counter.      # Clear the result counter.
248      my $retVal = 0;      my $retVal = 0;
249      # Create a feature data object for storing each result.      $self->PrintLine("Processing query results.<br />");
     my $fd = FeatureData->new($self);  
250      Trace("Starting feature loop.") if T(3);      Trace("Starting feature loop.") if T(3);
251      # Loop through all the records returned by the query.      # Loop through all the records returned by the query.
252      while (my $record = $query->Fetch()) {      while (my $record = $query->Fetch()) {
253            # Compute the sort key.
254            my $sort = $rhelp->SortKey($record);
255          # Store this feature.          # Store this feature.
256          $fd->Store($record);          $rhelp->PutData($sort, $record->PrimaryValue('Feature(id)'), $record);
         # Send it to the output.  
         $self->PutFeature($fd);  
257          # Increment the result counter.          # Increment the result counter.
258          $retVal++;          $retVal++;
259            if ($retVal % 100 == 0) {
260                $self->PrintLine("$retVal results processed.<br />");
261            }
262      }      }
263        $self->PrintLine("Results found: $retVal.<br />");
264      # Return the counter.      # Return the counter.
265      return $retVal;      return $retVal;
266  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.9

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3