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

Annotation of /Sprout/TargetCriterionGeneric.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     #
4 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
5 :     # for Interpretations of Genomes. All Rights Reserved.
6 :     #
7 :     # This file is part of the SEED Toolkit.
8 :     #
9 :     # The SEED Toolkit is free software. You can redistribute
10 :     # it and/or modify it under the terms of the SEED Toolkit
11 :     # Public License.
12 :     #
13 :     # You should have received a copy of the SEED Toolkit Public License
14 :     # along with this program; if not write to the University of Chicago
15 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
16 :     # Genomes at veronika@thefig.info or download a copy from
17 :     # http://www.theseed.org/LICENSE.TXT.
18 :     #
19 :    
20 :     package TargetCriterionGeneric;
21 :    
22 :     use strict;
23 :     use Tracer;
24 :     use Sprout;
25 :     use base qw(TargetCriterionQuery);
26 :    
27 :     =head1 Generic Match Target Search Criterion Object
28 :    
29 :     =head2 Introduction
30 :    
31 :     This is a search criterion object for search criteria involving a match against a
32 :     single database text field. The user inputs a string that is to be matched either to
33 :     a substring of the text field or a prefix of the text field. Unlike a normal query-based
34 :     search, the sanity of this one is specified in the constructor.
35 :    
36 :     This object uses the followng additional object fields.
37 :    
38 :     =over
39 :    
40 :     =item infix
41 :    
42 :     Style of match: C<exact> for an exact match, C<prefix> if only the prefix of the field
43 :     needs to match, or C<scan> if any substring of the field can match.
44 :    
45 :     =item sanity
46 :    
47 :     TRUE if this search should be considered sane, else FALSE.
48 :    
49 :     =back
50 :    
51 :     =head2 Special Methods
52 :    
53 :     =head3 new
54 :    
55 :     my $tc = TargetCriterionGeneric->new($rhelp, $name, $label, $hint, $infix, $field => @path);
56 :    
57 :     Construct a new TargetCriterionGeneric object. The following parameters are
58 :     expected.
59 :    
60 :     =over 4
61 :    
62 :     =item rhelp
63 :    
64 :     [[ResultHelperPm]] object for the active search.
65 :    
66 :     =item name
67 :    
68 :     Identifying name of this criterion.
69 :    
70 :     =item label
71 :    
72 :     Label to display for this criterion in the type dropdown.
73 :    
74 :     =item hint
75 :    
76 :     The hint tooltip to be displayed for this criterion.
77 :    
78 :     =item infix
79 :    
80 :     Style of match: C<exact> for an exact match, C<prefix> if only the prefix of the field
81 :     needs to match, or C<scan> if any substring of the field can match.
82 :    
83 :     =item sanity
84 :    
85 :     TRUE if this search should be considered sane, else FALSE.
86 :    
87 :     =item field
88 :    
89 :     Name of the database field containing the code value.
90 :    
91 :     =item path
92 :    
93 :     List of object names, indicating the path from the Feature or Genome table to the
94 :     table containing the code value. The first object will be C<Feature> for a feature-based
95 :     criterion and C<Genome> for a genome-based one. Frequently, the path will stop with
96 :     the first object. When this happens, the criterion can be processed very efficiently.
97 :    
98 :     =back
99 :    
100 :     =cut
101 :    
102 :     sub new {
103 :     # Get the parameters.
104 :     my ($class, $rhelp, $name, $label, $hint, $infix, $sanity, $field, @path) = @_;
105 :     # Construct the underlying object.
106 :     my $retVal = TargetCriterionQuery::new($class, $rhelp, { label => $label,
107 :     hint => $hint,
108 :     text => 1,
109 :     name => $name },
110 :     $field, @path);
111 :     # Remember the local flags.
112 :     $retVal->{infix} = $infix;
113 :     $retVal->{sanity} = $sanity;
114 :     # Return the object.
115 :     return $retVal;
116 :     }
117 :    
118 :     =head2 Virtual Methods
119 :    
120 :     =head3 Validate
121 :    
122 :     my $okFlag = $tc->Validate($parms);
123 :    
124 :     Return TRUE if the specified parameters are valid for a search criterion of this type
125 :     and FALSE otherwise. If an error is detected, the error message can be retrieved using
126 :     the L</message> method.
127 :    
128 :     =over 4
129 :    
130 :     =item parms
131 :    
132 :     A Criterion Parameter Object whose fields are to be validated.
133 :    
134 :     =item RETURN
135 :    
136 :     Returns TRUE if the parameters are valid, else FALSE.
137 :    
138 :     =back
139 :    
140 :     =cut
141 :    
142 :     sub Validate {
143 :     # Get the parameters.
144 :     my ($self, $parms) = @_;
145 :     # Default to valid.
146 :     my $retVal = 1;
147 :     # Get the string value.
148 :     my $value = $parms->{stringValue};
149 :     # It's only invalid if it's blank.
150 :     if (! defined $value || $value eq '' || $value =~ /^\s+$/) {
151 :     $retVal = 0;
152 :     $self->SetMessage("No value specified for $self->{label}.");
153 :     }
154 :     # Return the validation code.
155 :     return $retVal;
156 :     }
157 :    
158 :     =head3 ComputeQuery
159 :    
160 :     my ($joins, $filterString, $parms) = $tc->ComputeQuery($criterion);
161 :    
162 :     Compute the SQL filter, join list, and parameter list for this
163 :     criterion. If the criterion cannot be processed by SQL, then nothing is
164 :     returned, and the criterion must be handled during post-processing.
165 :    
166 :     The join list and the parameter list should both be list references. The
167 :     filter string is a true string.
168 :    
169 :     If the filter string only uses the B<Genome> and B<Feature> tables, then the
170 :     join list can be left empty. Otherwise, the join list should start with the
171 :     particular starting point (B<Genome> or B<Feature>) and list the path through
172 :     the other relevant entities and relationships. Each criterion will have its
173 :     own separate join path.
174 :    
175 :     =over 4
176 :    
177 :     =item criterion
178 :    
179 :     Reference to a Criterion Parameter Object.
180 :    
181 :     =item RETURN
182 :    
183 :     Returns a 3-tuple consisting of the join list, the relevant filter string,
184 :     and the matching parameters. If the criterion cannot be processed using
185 :     SQL, then the return list contains three undefined values. (This is what happens if
186 :     you don't override this method.)
187 :    
188 :     =back
189 :    
190 :     =cut
191 :    
192 :     sub ComputeQuery {
193 :     # Get the parameters.
194 :     my ($self, $criterion) = @_;
195 :     # Get the name of the relevant field with the appropriate suffix.
196 :     my $fieldName = $self->RelevantField($criterion->{idx});
197 :     # Compute the join list.
198 :     my $joins = $self->JoinList();
199 :     # Compute the filter string.
200 :     my $filterString = "$fieldName LIKE ?";
201 :     # Get the parameter value.
202 :     my $parm = $criterion->{stringValue};
203 :     # Add the vild cards.
204 :     my $infix = $self->{infix};
205 :     if ($infix eq 'scan') {
206 :     $parm = "%$parm%";
207 :     } elsif ($infix eq 'prefix') {
208 :     $parm .= "%";
209 :     }
210 :     # Return the results.
211 :     return ($joins, $filterString, [$parm]);
212 :     }
213 :    
214 :     =head3 CheckValue
215 :    
216 :     my $match = $tc->CheckValue($criterion, $valueData);
217 :    
218 :     Return TRUE if the current feature matches this criterion, else FALSE.
219 :    
220 :     =over 4
221 :    
222 :     =item criterion
223 :    
224 :     Criterion Parameter object describing this criterion's parameters.
225 :    
226 :     =item valueData
227 :    
228 :     Value computed for the current feature by the L</GetValueData> method.
229 :    
230 :     =item RETURN
231 :    
232 :     Returns TRUE if the current feature matches the criterion, else FALSE.
233 :    
234 :     =back
235 :    
236 :     =cut
237 :    
238 :     sub CheckValue {
239 :     # Get the parameters.
240 :     my ($self, $criterion, $valueData) = @_;
241 :     # Get the criterion value to compare against it. We fold to lower case because
242 :     # we're using the case-insensitive LIKE in the SQL.
243 :     my $comparator = lc $criterion->{stringValue};
244 :     # Get the comparison type.
245 :     my $infix = $self->{infix};
246 :     # Declare the return value.
247 :     my $retVal;
248 :     # Look for the comparator value in the field. Note that the process is case-insensitive,
249 :     # because this is an SQL "LIKE" operation. Also, we exit the loop immediately if we
250 :     # have a match.
251 :     for my $value (@$valueData) { last if $retVal;
252 :     my $lcValue = lc $value;
253 :     if ($infix eq 'exact') {
254 :     $retVal = ($lcValue eq $comparator);
255 :     } else {
256 :     my $found = index($comparator, $lcValue);
257 :     if ($infix eq 'prefix') {
258 :     $retVal = ($found == 0);
259 :     } else {
260 :     $retVal = ($found > 0);
261 :     }
262 :     }
263 :     }
264 :     # Return the result.
265 :     return $retVal;
266 :     }
267 :    
268 :     =head3 Sane
269 :    
270 :     my $flag = $tc->Sane($parms);
271 :    
272 :     Return TRUE if this is a sane criterion, else FALSE. Every search must have at least one
273 :     sane criterion in order to be valid.
274 :    
275 :     =over 4
276 :    
277 :     =item parms (optional)
278 :    
279 :     A Criterion Parameter Object for the current query.
280 :    
281 :     =item RETURN
282 :    
283 :     Returns TRUE if this query returns a relatively limited result set and uses SQL,
284 :     else FALSE. If you do not override this method, it returns FALSE.
285 :    
286 :     =back
287 :    
288 :     =cut
289 :    
290 :     sub Sane {
291 :     my ($self) = @_;
292 :     return $self->{sanity};
293 :     }
294 :    
295 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3