| 1 | # GenPerl module | ||
| 2 | # | ||
| 3 | |||
| 4 | # POD documentation - main docs before the code | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Genetics::API::DB::Query | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use Genetics::API ; | ||
| 13 | |||
| 14 | $api = new Genetics::API(DSN => {driver => "mysql", | ||
| 15 | host => $Host, | ||
| 16 | database => $Database}, | ||
| 17 | user => $UserName, | ||
| 18 | password => $Password) ; | ||
| 19 | |||
| 20 | $sv = $api->getObject({TYPE => "StudyVariable", | ||
| 21 | NAME => "Aff", | ||
| 22 | FULL => 1}) ; | ||
| 23 | |||
| 24 | @affSubjects = $api->getSubjectsByPhenotype($sv, 2) ; | ||
| 25 | |||
| 26 | @kindreds = $api->getObjects({TYPE => "Kindred"}) ; | ||
| 27 | |||
| 28 | @d1s = $api->getObjects({TYPE => "Marker", Name => "D1S*"}) ; | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | The Genetics::API::DB packages provide an interface for the manipulation of | ||
| 33 | Genperl objects in a realtional database. This package contains methods to | ||
| 34 | query for and return objects based on attributes/relationships other than id | ||
| 35 | (for methods to do that, see Genetics::API::DB::Read). | ||
| 36 | |||
| 37 | =head1 LICENSE | ||
| 38 | |||
| 39 | This program is free software; you can redistribute it and/or modify | ||
| 40 | it under the terms of the GNU General Public License as published by | ||
| 41 | the Free Software Foundation; either version 2 of the License, or | ||
| 42 | (at your option) any later version. | ||
| 43 | |||
| 44 | This program is distributed in the hope that it will be useful, | ||
| 45 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 46 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 47 | GNU General Public License for more details. | ||
| 48 | |||
| 49 | You should have received a copy of the GNU General Public License | ||
| 50 | along with this program; if not, write to the Free Software | ||
| 51 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 52 | |||
| 53 | =head1 FEEDBACK | ||
| 54 | |||
| 55 | Currently, all feedback should be sent directly to the author. | ||
| 56 | |||
| 57 | =head1 AUTHOR - Steve Mathias | ||
| 58 | |||
| 59 | Email: mathias@genomica.com | ||
| 60 | |||
| 61 | Phone: (720) 565-4029 | ||
| 62 | |||
| 63 | Address: Genomica Corporation | ||
| 64 | 1745 38th Street | ||
| 65 | Boulder, CO 80301 | ||
| 66 | |||
| 67 | =head1 DETAILS | ||
| 68 | |||
| 69 | The rest of the documentation describes each of the object variables and | ||
| 70 | methods. The names of internal variables and methods are preceded with an | ||
| 71 | underscore (_). | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | ################## | ||
| 76 | # # | ||
| 77 | # Begin the code # | ||
| 78 | # # | ||
| 79 | ################## | ||
| 80 | |||
| 81 | package Genetics::API::DB::Query ; | ||
| 82 | |||
| 83 | BEGIN { | ||
| 84 | $ID = "Genetics::API::Query" ; | ||
| 85 | #$DEBUG = $main::DEBUG ; | ||
| 86 | $DEBUG = 0 ; | ||
| 87 | $DEBUG and warn "Debugging in $ID is on" ; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head1 Imported Packages | ||
| 91 | |||
| 92 | strict Just to be anal | ||
| 93 | vars Global variables | ||
| 94 | Carp Error reporting | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | use strict ; | ||
| 99 | use vars qw(@ISA @EXPORT @EXPORT_OK $ID $DEBUG) ; | ||
| 100 | use Carp ; | ||
| 101 | use Exporter ; | ||
| 102 | |||
| 103 | =head1 Inheritance | ||
| 104 | |||
| 105 | Exporter Make methods available to importing packages | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | @ISA = qw(Exporter) ; | ||
| 110 | |||
| 111 | @EXPORT = qw(getObject getObjects countObjects | ||
| 112 | getClusterContents getClustersByType | ||
| 113 | getSubjectsByPhenotype getSiblings getSubjectsByKindred | ||
| 114 | getFounders getPOsByChromosome | ||
| 115 | getPhenotypesBySubject getGenotypesBySubject | ||
| 116 | getAllelesByType getPOAllelesByType | ||
| 117 | getAlleleCounts getAlleleFreqs getSNPGtCounts | ||
| 118 | getGtAlleles getGtAllelesByGt | ||
| 119 | getPtValue | ||
| 120 | _generateSQL) ; | ||
| 121 | @EXPORT_OK = qw(); | ||
| 122 | |||
| 123 | =head1 Public Methods | ||
| 124 | |||
| 125 | =head2 getObject | ||
| 126 | |||
| 127 | Function : Get and return Genetics::Object object(s) based on a set of query | ||
| 128 | parameters.. | ||
| 129 | Arguments : A reference to a hash containing the query parameters: | ||
| 130 | %query = ( | ||
| 131 | ID => integer | ||
| 132 | TYPE => Object type | ||
| 133 | NAME => text | ||
| 134 | WHERE => SQL where clause | ||
| 135 | FULL => Boolean | ||
| 136 | ) | ||
| 137 | If $query{FULL} is false (the default) mini objects returned. | ||
| 138 | Returns : A Genetics::Object object, or undef there are zero or more than | ||
| 139 | one that satisfy the query parameters. | ||
| 140 | Scope : Public | ||
| 141 | Comments : This is experimental and the interface may change. | ||
| 142 | If the NAME parameter contains Perl wildcard characters * or ?, | ||
| 143 | these are converted to the SQL wildcards % and _. This hopefully | ||
| 144 | results in the expected behavior. | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub getObject { | ||
| 149 | my($self, $qPtr) = @_ ; | ||
| 150 | my($sql, $sth, $arrayRef, $returnCount, $row, $id, $type, | ||
| 151 | $methodName, $methodRef, $obj) ; | ||
| 152 | my $dbh = $self->{dbh} ; | ||
| 153 | |||
| 154 | $sql = $self->_generateSQL($qPtr) or | ||
| 155 | croak "ERROR [getObject]: Could not process query parameters!" ; | ||
| 156 | |||
| 157 | $sth = $dbh->prepare( "$sql" ) ; | ||
| 158 | $sth->execute() ; | ||
| 159 | $arrayRef = $sth->fetchall_arrayref() ; | ||
| 160 | $returnCount = scalar(@$arrayRef) ; | ||
| 161 | if ( $returnCount == 0 ) { | ||
| 162 | carp "WARNING [getObject]: No object matching query:\n$sql\n" ; | ||
| 163 | return(undef) ; | ||
| 164 | } elsif ( $returnCount > 1) { | ||
| 165 | croak "ERROR [getObject]: Query:\n$sql\nfound $returnCount objects!" ; | ||
| 166 | } | ||
| 167 | $row = shift(@$arrayRef) ; | ||
| 168 | ( $id, $type ) = @$row ; | ||
| 169 | |||
| 170 | $$qPtr{FULL} ? ($methodName = "Genetics::API::get" . $type) | ||
| 171 | : ($methodName = "Genetics::API::getMini" . $type) ; | ||
| 172 | $methodRef = \&$methodName ; | ||
| 173 | $DEBUG and carp " ->[getObject] calling $methodName($id)" ; | ||
| 174 | $obj = &$methodRef($self, $id) ; | ||
| 175 | |||
| 176 | return($obj) ; | ||
| 177 | } | ||
| 178 | |||
| 179 | =head2 getObjects | ||
| 180 | |||
| 181 | Function : Get and return Genetics::Object object(s) based on a set of query | ||
| 182 | parameters.. | ||
| 183 | Arguments : A reference to a hash containing the query parameters: | ||
| 184 | %query = ( | ||
| 185 | TYPE => Object type | ||
| 186 | NAME => text | ||
| 187 | KW => {KeywordType.name => Keyword.value} | ||
| 188 | WHERE => SQL where clause | ||
| 189 | FULL => Boolean | ||
| 190 | ) | ||
| 191 | If $query{FULL} is false (the default) mini objects returned. | ||
| 192 | Returns : An array of Genetics::Object object(s) which satisfy the query | ||
| 193 | parameters, or undef if there are none. | ||
| 194 | Scope : Public | ||
| 195 | Comments : This is experimental and the interface may change. | ||
| 196 | If the NAME parameter contains Perl wildcard characters * or ?, | ||
| 197 | these are converted to the SQL wildcards % and _. This hopefully | ||
| 198 | results in the expected behavior. | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub getObjects { | ||
| 203 | my($self, $qPtr) = @_ ; | ||
| 204 | my($sql, $sth, $arrayRef, $row, $id, $type, $methodName, $methodRef, | ||
| 205 | $obj, @objects) ; | ||
| 206 | my $dbh = $self->{dbh} ; | ||
| 207 | |||
| 208 | $sql = $self->_generateSQL($qPtr) or | ||
| 209 | croak "ERROR [getObjects]: Could not process query parameters!" ; | ||
| 210 | |||
| 211 | $sth = $dbh->prepare( "$sql" ) ; | ||
| 212 | $sth->execute() ; | ||
| 213 | $arrayRef = $sth->fetchall_arrayref() ; | ||
| 214 | |||
| 215 | if (scalar(@$arrayRef) == 0) { | ||
| 216 | carp "WARNING [getObjects]: No objects matching query:\n$sql\n" ; | ||
| 217 | return(undef) ; | ||
| 218 | } | ||
| 219 | |||
| 220 | foreach $row (@$arrayRef) { | ||
| 221 | ( $id, $type ) = @$row ; | ||
| 222 | $$qPtr{FULL} ? ($methodName = "Genetics::API::get" . $type) | ||
| 223 | : ($methodName = "Genetics::API::getMini" . $type) ; | ||
| 224 | $methodRef = \&$methodName ; | ||
| 225 | $DEBUG and carp " ->[getObjects] calling $methodName($id)" ; | ||
| 226 | $obj = &$methodRef($self, $id) ; | ||
| 227 | push(@objects, $obj) ; | ||
| 228 | } | ||
| 229 | |||
| 230 | return(@objects) ; | ||
| 231 | } | ||
| 232 | |||
| 233 | =head2 countObjects | ||
| 234 | |||
| 235 | Function : Return the count of Genetics::Object object(s) that match a set | ||
| 236 | of query parameters. | ||
| 237 | Arguments : A reference to a hash containing the query parameters: | ||
| 238 | %query = ( | ||
| 239 | TYPE => Object type | ||
| 240 | NAME => text | ||
| 241 | WHERE => SQL where clause | ||
| 242 | ) | ||
| 243 | Returns : Scalar | ||
| 244 | Scope : Public | ||
| 245 | Comments : | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | sub countObjects { | ||
| 250 | my($self, $qPtr) = @_ ; | ||
| 251 | my($sql, $arrayRef) ; | ||
| 252 | my $dbh = $self->{dbh} ; | ||
| 253 | |||
| 254 | $sql = $self->_generateSQL($qPtr) or | ||
| 255 | croak "ERROR [getObjects]: Could not process query parameters!" ; | ||
| 256 | |||
| 257 | $arrayRef = $dbh->selectall_arrayref( "$sql" ) ; | ||
| 258 | |||
| 259 | return(scalar(@$arrayRef)) ; | ||
| 260 | } | ||
| 261 | |||
| 262 | =head2 getClusterContents | ||
| 263 | |||
| 264 | Function : Get the objects referenced by a Cluster. | ||
| 265 | Argument : A Genetics::Cluster object | ||
| 266 | Returns : A list of Genetics::Object objects. | ||
| 267 | Scope : Public | ||
| 268 | Comments : | ||
| 269 | |||
| 270 | =cut | ||
| 271 | |||
| 272 | sub getClusterContents { | ||
| 273 | my($self, $cluster, $miniFlag) = @_ ; | ||
| 274 | my($contentsListPtr, $type, $methodName, $methodRef, $ptr, $obj, @contents) ; | ||
| 275 | my $dbh = $self->{dbh} ; | ||
| 276 | |||
| 277 | defined($miniFlag) or $miniFlag = 0 ; | ||
| 278 | |||
| 279 | $contentsListPtr = $cluster->field("Contents") ; | ||
| 280 | $type = $cluster->field("clusterType") ; | ||
| 281 | $type eq "Mixed" and croak "ERROR [getClusterContents]: Mixed Clusters not supported." ; | ||
| 282 | $miniFlag == 1 ? ($methodName = "Genetics::API::getMini" . $type) | ||
| 283 | : ($methodName = "Genetics::API::get" . $type) ; | ||
| 284 | $methodRef = \&$methodName ; | ||
| 285 | |||
| 286 | foreach $ptr (@$contentsListPtr) { | ||
| 287 | $obj = &$methodRef($self, $$ptr{id}) ; | ||
| 288 | push(@contents, $obj) ; | ||
| 289 | } | ||
| 290 | |||
| 291 | return(@contents) ; | ||
| 292 | } | ||
| 293 | |||
| 294 | =head2 getClustersByType | ||
| 295 | |||
| 296 | Function : | ||
| 297 | Arguments : | ||
| 298 | Returns : | ||
| 299 | Example : getClustersByType() | ||
| 300 | Scope : | ||
| 301 | Comments : | ||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | sub getClustersByType { | ||
| 306 | my($self, $clusterType) = @_ ; | ||
| 307 | my($sth, $arrRef, @clusters) ; | ||
| 308 | my $dbh = $self->{dbh} ; | ||
| 309 | |||
| 310 | $sth = $dbh->prepare("select id from Object, Cluster | ||
| 311 | where clusterType = '$clusterType' and | ||
| 312 | id = clusterID") ; | ||
| 313 | $sth->execute() ; | ||
| 314 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 315 | push(@clusters, $self->getCluster($$arrRef[0])) ; | ||
| 316 | } | ||
| 317 | |||
| 318 | return(@clusters) ; | ||
| 319 | } | ||
| 320 | |||
| 321 | =head2 getFounders | ||
| 322 | |||
| 323 | Function : Query for and return the founders (Subjects without parents) in a | ||
| 324 | Kindred. | ||
| 325 | Argument : A Genetics::Kindred object. | ||
| 326 | Returns : A list of Genetics::Subject objects. | ||
| 327 | |||
| 328 | Scope : Public | ||
| 329 | Comments : | ||
| 330 | |||
| 331 | =cut | ||
| 332 | |||
| 333 | sub getFounders { | ||
| 334 | my($self, $kindred) = @_ ; | ||
| 335 | my(@subjects, $subject, @founders) ; | ||
| 336 | my $dbh = $self->{dbh} ; | ||
| 337 | |||
| 338 | $DEBUG and carp " ->[getFounders] $kindred" ; | ||
| 339 | |||
| 340 | @subjects = $self->getSubjectsByKindred($kindred) ; | ||
| 341 | foreach $subject (@subjects) { | ||
| 342 | #$DEBUG and carp " ->[getFounders] Checking $subject" ; | ||
| 343 | if ( ! $subject->hasParents()) { | ||
| 344 | push(@founders, $subject) ; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | return(@founders) ; | ||
| 349 | } | ||
| 350 | |||
| 351 | =head2 getSubjectsByPhenotype | ||
| 352 | |||
| 353 | Function : Query for Subjects based on their associated Phenotype values. | ||
| 354 | Argument : A Genetics::Object::StudyVariable object and a phenotype value. | ||
| 355 | Returns : Genetics::Object::Subject objects that have associated Phenotypes | ||
| 356 | with the query value. | ||
| 357 | Scope : Public | ||
| 358 | Comments : | ||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | sub getSubjectsByPhenotype { | ||
| 363 | my($self, $sv, $queryValue) = @_ ; | ||
| 364 | my($format, $valueFieldName, $sth, $arrRef, @subjects) ; | ||
| 365 | my $dbh = $self->{dbh} ; | ||
| 366 | |||
| 367 | $DEBUG and carp " ->[getSubjectsByPhenotype] Start" ; | ||
| 368 | |||
| 369 | $format = $sv->field("format") ; | ||
| 370 | $valueFieldName = lc($format) . "Value" ; | ||
| 371 | |||
| 372 | $sth = $dbh->prepare("select subjectID from Phenotype | ||
| 373 | where $valueFieldName = '$queryValue'") ; | ||
| 374 | $sth->execute() ; | ||
| 375 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 376 | push(@subjects, $self->getSubject($$arrRef[0])) ; | ||
| 377 | } | ||
| 378 | |||
| 379 | return(@subjects) ; | ||
| 380 | } | ||
| 381 | |||
| 382 | =head2 getPhenotypesBySubject | ||
| 383 | |||
| 384 | Function : Query for Phenotypes based on their associated Subject. | ||
| 385 | Argument : A Genetics::Object::Subject object. | ||
| 386 | Returns : Genetics::Object::Phenotype objects. | ||
| 387 | Scope : Public | ||
| 388 | Comments : Returns only active Phenotypes. | ||
| 389 | |||
| 390 | =cut | ||
| 391 | |||
| 392 | sub getPhenotypesBySubject { | ||
| 393 | my($self, $subject) = @_ ; | ||
| 394 | my($format, $subjID, $sth, $arrRef, @pts) ; | ||
| 395 | my $dbh = $self->{dbh} ; | ||
| 396 | |||
| 397 | $DEBUG and carp " ->[getPhenotypesBySubject] Start" ; | ||
| 398 | |||
| 399 | $subjID = $subject->id() ; | ||
| 400 | $sth = $dbh->prepare("select ptID from Phenotype | ||
| 401 | where subjectID = $subjID") ; | ||
| 402 | $sth->execute() ; | ||
| 403 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 404 | push(@pts, $self->getPhenotype($$arrRef[0])) ; | ||
| 405 | } | ||
| 406 | |||
| 407 | return(@pts) ; | ||
| 408 | } | ||
| 409 | |||
| 410 | =head2 getGenotypesBySubject | ||
| 411 | |||
| 412 | Function : Query for Genotypes based on their associated Subject. | ||
| 413 | Argument : A Genetics::Object::Subject object. | ||
| 414 | Returns : An array of Genetics::Object::Genotype objects. | ||
| 415 | Scope : Public | ||
| 416 | Comments : Returns only the active Genotypes. | ||
| 417 | |||
| 418 | =cut | ||
| 419 | |||
| 420 | sub getGenotypesBySubject { | ||
| 421 | my($self, $subject) = @_ ; | ||
| 422 | my($format, $subjID, $sth, $arrRef, @gts) ; | ||
| 423 | my $dbh = $self->{dbh} ; | ||
| 424 | |||
| 425 | $DEBUG and carp " ->[getGenotypesBySubject] Start" ; | ||
| 426 | |||
| 427 | $subjID = $subject->id() ; | ||
| 428 | $sth = $dbh->prepare("select gtID from Genotype | ||
| 429 | where subjectID = $subjID") ; | ||
| 430 | $sth->execute() ; | ||
| 431 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 432 | push(@gts, $self->getGenotype($$arrRef[0])) ; | ||
| 433 | } | ||
| 434 | |||
| 435 | return(@gts) ; | ||
| 436 | } | ||
| 437 | |||
| 438 | =head2 getSubjectsByKindred | ||
| 439 | |||
| 440 | Function : Query for and return all the Subjects in a Kindred. | ||
| 441 | Argument : A Kindred object | ||
| 442 | Returns : A list of Subject objects | ||
| 443 | Scope : Public | ||
| 444 | Comments : | ||
| 445 | |||
| 446 | =cut | ||
| 447 | |||
| 448 | sub getSubjectsByKindred { | ||
| 449 | my($self, $kindred) = @_ ; | ||
| 450 | my($kindredID, $sth, $arrRef, @subjects) ; | ||
| 451 | my $dbh = $self->{dbh} ; | ||
| 452 | |||
| 453 | $DEBUG and carp " ->[getSubjectsByKindred] Start" ; | ||
| 454 | |||
| 455 | $kindredID = $kindred->field("id") ; | ||
| 456 | |||
| 457 | $sth = $dbh->prepare("select subjectID from KindredSubject | ||
| 458 | where kindredID = $kindredID") ; | ||
| 459 | $sth->execute() ; | ||
| 460 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 461 | push(@subjects, $self->getSubject($$arrRef[0])) ; | ||
| 462 | } | ||
| 463 | |||
| 464 | return(@subjects) ; | ||
| 465 | } | ||
| 466 | |||
| 467 | =head2 getSiblings | ||
| 468 | |||
| 469 | Function : Query for and return all the siblings of a Subject. | ||
| 470 | Argument : A Subject object | ||
| 471 | Returns : A list of Subject objects | ||
| 472 | Scope : Public | ||
| 473 | Comments : | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | sub getSiblings { | ||
| 478 | my($self, $subject) = @_ ; | ||
| 479 | my($subjectID, $momID, $dadID, $sth, $arrRef, @siblings) ; | ||
| 480 | my $dbh = $self->{dbh} ; | ||
| 481 | |||
| 482 | $subjectID = $subject->field("id") ; | ||
| 483 | |||
| 484 | ($momID, $dadID) = $dbh->selectrow_array( "select motherID, fatherID | ||
| 485 | from Subject | ||
| 486 | where subjectID = $subjectID" ) ; | ||
| 487 | |||
| 488 | $sth = $dbh->prepare( "select subjectID from Subject | ||
| 489 | where motherID = $momID | ||
| 490 | and fatherID = $dadID | ||
| 491 | and subjectID != $subjectID" ) ; | ||
| 492 | $sth->execute() ; | ||
| 493 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 494 | |||
| 495 | push(@siblings, $self->getSubject($$arrRef[0])) ; | ||
| 496 | } | ||
| 497 | |||
| 498 | return(@siblings) ; | ||
| 499 | } | ||
| 500 | |||
| 501 | =head2 getPOsByChromosome | ||
| 502 | |||
| 503 | Function : | ||
| 504 | Arguments : | ||
| 505 | Returns : | ||
| 506 | Example : getPOsByChromosome() | ||
| 507 | Scope : | ||
| 508 | Comments : Right now this returns all SequenceObject types. This works | ||
| 509 | because all SequenceObject types are also PolymorphicObjects. | ||
| 510 | It will have to be modified if new SequenceObject types are | ||
| 511 | introduced that are not also PolymorphicObjects. | ||
| 512 | |||
| 513 | =cut | ||
| 514 | |||
| 515 | sub getPOsByChromosome { | ||
| 516 | my($self, $chr) = @_ ; | ||
| 517 | my($sth, $arrRef, @markers) ; | ||
| 518 | my $dbh = $self->{dbh} ; | ||
| 519 | |||
| 520 | $sth = $dbh->prepare("select id from Object, SequenceObject | ||
| 521 | where id = seqObjectID and chromosome = '$chr'") ; | ||
| 522 | $sth->execute() ; | ||
| 523 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 524 | push(@markers, $self->getMarker($$arrRef[0])) ; | ||
| 525 | } | ||
| 526 | |||
| 527 | return(@markers) ; | ||
| 528 | } | ||
| 529 | |||
| 530 | =head2 getAllelesByType | ||
| 531 | |||
| 532 | Function : Query for and return a Markers allele names, by type. | ||
| 533 | Argument : A Marker or SNP object and a string containing an allele type. | ||
| 534 | Returns : An array of allele names. | ||
| 535 | Scope : Public | ||
| 536 | Comments : The returned allele names are sorted, first numerically then | ||
| 537 | alphabetically. | ||
| 538 | |||
| 539 | =cut | ||
| 540 | |||
| 541 | sub getAllelesByType { | ||
| 542 | my($self, $po, $alleleType) = @_ ; | ||
| 543 | my($poID, $sth, $aoaRef, $row, @alleleNames, @sortedAlleleNames, ) ; | ||
| 544 | my $dbh = $self->{dbh} ; | ||
| 545 | |||
| 546 | $DEBUG and carp " ->[getAllelesByType] $po, $alleleType" ; | ||
| 547 | |||
| 548 | $poID = $po->field("id") ; | ||
| 549 | |||
| 550 | $sth = $dbh->prepare( "select name from Allele | ||
| 551 | where poID = $poID | ||
| 552 | and type = '$alleleType'" ) ; | ||
| 553 | $sth->execute() ; | ||
| 554 | $aoaRef = $sth->fetchall_arrayref() ; | ||
| 555 | foreach $row (@$aoaRef) { | ||
| 556 | push(@alleleNames, $$row[0]) ; | ||
| 557 | } | ||
| 558 | |||
| 559 | if ($alleleType =~ /^(Code|Size|RepeatNumber)$/) { | ||
| 560 | @sortedAlleleNames = sort { $a <=> $b} @alleleNames ; | ||
| 561 | } else { | ||
| 562 | @sortedAlleleNames = sort @alleleNames ; | ||
| 563 | } | ||
| 564 | |||
| 565 | return(@sortedAlleleNames) ; | ||
| 566 | } | ||
| 567 | |||
| 568 | =head2 getPOAllelesByType | ||
| 569 | |||
| 570 | Function : Query for and return a Markers alleles by type. | ||
| 571 | Argument : A Marker or SNP object and a string containing an allele type. | ||
| 572 | Returns : An array of allele names. | ||
| 573 | Scope : Public | ||
| 574 | Comments : The returned allele names are sorted, first numerically then | ||
| 575 | alphabetically. | ||
| 576 | |||
| 577 | =cut | ||
| 578 | |||
| 579 | sub getPOAllelesByType { | ||
| 580 | my($self, $po, $alleleType) = @_ ; | ||
| 581 | my($poID, $sth, $aoaRef, $row, @alleleNames, @sortedAlleleNames, ) ; | ||
| 582 | my $dbh = $self->{dbh} ; | ||
| 583 | |||
| 584 | $DEBUG and carp " ->[getPOAllelesByType] $po, $alleleType" ; | ||
| 585 | |||
| 586 | $poID = $po->field("id") ; | ||
| 587 | |||
| 588 | $sth = $dbh->prepare( "select name, alleleID from Allele | ||
| 589 | where poID = $poID | ||
| 590 | and type = '$alleleType'" ) ; | ||
| 591 | $sth->execute() ; | ||
| 592 | $aoaRef = $sth->fetchall_arrayref() ; | ||
| 593 | foreach $row (@$aoaRef) { | ||
| 594 | push(@alleleNames, $$row[0]) ; | ||
| 595 | } | ||
| 596 | |||
| 597 | @sortedAlleleNames = sort { $a <=> $b or $a cmp $b } @alleleNames ; | ||
| 598 | |||
| 599 | return(@sortedAlleleNames) ; | ||
| 600 | } | ||
| 601 | |||
| 602 | =head2 getAlleleCounts | ||
| 603 | |||
| 604 | Function : Query for and return raw allele counts. | ||
| 605 | Argument : A Marker or SNP object, a string containing an allele type, and | ||
| 606 | a Subject Cluster object. | ||
| 607 | Returns : Hash pointer. The hash structure is: | ||
| 608 | $count{AlleleName} = $number | ||
| 609 | Scope : Public | ||
| 610 | Comments : | ||
| 611 | |||
| 612 | =cut | ||
| 613 | |||
| 614 | sub getAlleleCounts { | ||
| 615 | my($self, $marker, $alleleType, $sc) = @_ ; | ||
| 616 | my($markerID, $sth, $aoaRef, $row, %alleleName2ID, @alleleNames, | ||
| 617 | $contentsListPtr, $subjPtr, $gtID, $alleleName, %count) ; | ||
| 618 | my $dbh = $self->{dbh} ; | ||
| 619 | |||
| 620 | $markerID = $marker->field("id") ; | ||
| 621 | |||
| 622 | $sth = $dbh->prepare( "select name, alleleID from Allele | ||
| 623 | where poID = $markerID | ||
| 624 | and type = '$alleleType'" ) ; | ||
| 625 | $sth->execute() ; | ||
| 626 | $aoaRef = $sth->fetchall_arrayref() ; | ||
| 627 | foreach $row (@$aoaRef) { | ||
| 628 | push(@alleleNames, $$row[0]) ; | ||
| 629 | $alleleName2ID{$$row[0]} = $$row[1] ; | ||
| 630 | } | ||
| 631 | |||
| 632 | $sth = $dbh->prepare( "select alleleCallID from AlleleCall | ||
| 633 | where gtID = ? | ||
| 634 | and alleleID = ?" ) ; | ||
| 635 | |||
| 636 | $contentsListPtr = $sc->field("Contents") ; | ||
| 637 | foreach $subjPtr (@$contentsListPtr) { | ||
| 638 | # foreach Subject, get the active Genotype for this Marker... | ||
| 639 | ($gtID) = $dbh->selectrow_array( "select gtID from Genotype | ||
| 640 | where isActive = 1 | ||
| 641 | and subjectID = $$subjPtr{id} | ||
| 642 | and poID = $markerID" ) ; | ||
| 643 | defined($gtID) or next ; | ||
| 644 | |||
| 645 | # ...then get the count of each Allele in that Genotype... | ||
| 646 | foreach $alleleName (@alleleNames) { | ||
| 647 | $sth->execute($gtID, $alleleName2ID{$alleleName}) ; | ||
| 648 | $aoaRef = $sth->fetchall_arrayref() ; | ||
| 649 | # ...and add it to the total for that allele in this cluster | ||
| 650 | $count{$alleleName} += scalar(@$aoaRef) ; | ||
| 651 | } | ||
| 652 | } | ||
| 653 | $sth->finish() ; | ||
| 654 | |||
| 655 | return( { %count } ) ; | ||
| 656 | } | ||
| 657 | |||
| 658 | =head2 getAlleleFreqs | ||
| 659 | |||
| 660 | Function : Query for and return the allele frequencies for a group of Subjects. | ||
| 661 | Argument : A Marker object, a string containing an allele type, and one of | ||
| 662 | the following defining the Subject group: | ||
| 663 | - a Subject Cluster object | ||
| 664 | - an array reference to a list of Subject objects | ||
| 665 | - a Kindred Cluster object | ||
| 666 | - an array reference to a list of Kindred objects | ||
| 667 | Returns : Hash reference to a hash with the following structure: | ||
| 668 | $freqs{AlleleName} = $number | ||
| 669 | Scope : Public | ||
| 670 | Comments : | ||
| 671 | |||
| 672 | =cut | ||
| 673 | |||
| 674 | sub getAlleleFreqs { | ||
| 675 | my($self, $marker, $alleleType, $sg) = @_ ; | ||
| 676 | my($markerID, $markerName, $sth, $aoaRef, $row, %alleleName2ID, @alleleNames, | ||
| 677 | $type, $obj, $contentsListPtr, $ptr, $kindred, $subject, @subjects, | ||
| 678 | @subjIDs, $subjID, $gtID, $alleleName, %count, $totalAlleleCounts, %freqs, | ||
| 679 | $freq) ; | ||
| 680 | my $dbh = $self->{dbh} ; | ||
| 681 | |||
| 682 | defined($alleleType) or $alleleType = "Code" ; | ||
| 683 | |||
| 684 | $DEBUG and carp " ->[getAlleleFreqs] $marker, $alleleType, $sg" ; | ||
| 685 | |||
| 686 | $markerID = $marker->field("id") ; | ||
| 687 | |||
| 688 | $sth = $dbh->prepare( "select name, alleleID from Allele | ||
| 689 | where poID = $markerID | ||
| 690 | and type = '$alleleType'" ) ; | ||
| 691 | $sth->execute() ; | ||
| 692 | $aoaRef = $sth->fetchall_arrayref() ; | ||
| 693 | foreach $row (@$aoaRef) { | ||
| 694 | next if ($alleleType eq "Nucleotide" and $$row[0] eq "N") ; | ||
| 695 | push(@alleleNames, $$row[0]) ; | ||
| 696 | $alleleName2ID{$$row[0]} = $$row[1] ; | ||
| 697 | } | ||
| 698 | |||
| 699 | if (ref($sg) eq "Genetics::Cluster") { | ||
| 700 | $type = $sg->field("clusterType") ; | ||
| 701 | $contentsListPtr = $sg->field("Contents") ; | ||
| 702 | if ($type eq "Subject") { | ||
| 703 | foreach $ptr (@$contentsListPtr) { | ||
| 704 | push @subjIDs, $$ptr{id} ; | ||
| 705 | } | ||
| 706 | } elsif ($type eq "Kindred") { | ||
| 707 | foreach $ptr (@$contentsListPtr) { | ||
| 708 | $kindred = $self->getKindred($$ptr{id}) ; | ||
| 709 | @subjects = $self->getSubjectsByKindred($kindred) ; | ||
| 710 | foreach $subject (@subjects) { | ||
| 711 | push @subjIDs, $subject->field("id") ; | ||
| 712 | } | ||
| 713 | } | ||
| 714 | } else { | ||
| 715 | croak "ERROR [getAlleleFreqs]: Invalid Cluster $sg." ; | ||
| 716 | } | ||
| 717 | } elsif (ref($sg) eq "ARRAY") { | ||
| 718 | foreach $obj (@$sg) { | ||
| 719 | if (ref($obj) eq "Genetics::Subject") { | ||
| 720 | push @subjIDs, $obj->field("id") ; | ||
| 721 | } elsif (ref($obj) eq "Genetics::Kindred") { | ||
| 722 | @subjects = $self->getSubjectsByKindred($obj) ; | ||
| 723 | foreach $subject (@subjects) { | ||
| 724 | push @subjIDs, $subject->field("id") ; | ||
| 725 | } | ||
| 726 | } else { | ||
| 727 | carp "WARNING [getAlleleFreqs]: Skipping invalid object $obj in Cluster $sg" ; | ||
| 728 | } | ||
| 729 | } | ||
| 730 | } else { | ||
| 731 | croak "ERROR [getAlleleFreqs]: Unsupported subject sample type: $sg" ; | ||
| 732 | } | ||
| 733 | |||
| 734 | $sth = $dbh->prepare( "select alleleCallID from AlleleCall | ||
| 735 | where gtID = ? | ||
| 736 | and alleleID = ?" ) ; | ||
| 737 | |||
| 738 | foreach $subjID (@subjIDs) { | ||
| 739 | # foreach Subject, get the active Genotype for this Marker... | ||
| 740 | ($gtID) = $dbh->selectrow_array( "select gtID from Genotype | ||
| 741 | where isActive = 1 | ||
| 742 | and subjectID = $subjID | ||
| 743 | and poID = $markerID" ) ; | ||
| 744 | defined($gtID) or next ; | ||
| 745 | # ...then get the count of each Allele in that Genotype... | ||
| 746 | foreach $alleleName (@alleleNames) { | ||
| 747 | $sth->execute($gtID, $alleleName2ID{$alleleName}) ; | ||
| 748 | $aoaRef = $sth->fetchall_arrayref() ; | ||
| 749 | # ...and add it to the total for that allele in this cluster | ||
| 750 | $count{$alleleName} += scalar(@$aoaRef) ; | ||
| 751 | $totalAlleleCounts += scalar(@$aoaRef) ; | ||
| 752 | } | ||
| 753 | } | ||
| 754 | $sth->finish() ; | ||
| 755 | |||
| 756 | foreach $alleleName (@alleleNames) { | ||
| 757 | $freq = $count{$alleleName} / $totalAlleleCounts ; | ||
| 758 | $freqs{$alleleName} = &_formatFloat($freq) ; | ||
| 759 | } | ||
| 760 | |||
| 761 | return( \%freqs ) ; | ||
| 762 | } | ||
| 763 | |||
| 764 | =head2 getGtAlleles | ||
| 765 | |||
| 766 | Function : For a given Subject and Marker, query for and return the allele | ||
| 767 | names from the active Genotype. | ||
| 768 | Argument : A Subject object and a Marker object | ||
| 769 | Returns : An array containing the allele names or undef if there is not an | ||
| 770 | active Genotype associated with the input Subject and Marker. | ||
| 771 | Scope : Public | ||
| 772 | Comments : | ||
| 773 | |||
| 774 | =cut | ||
| 775 | |||
| 776 | sub getGtAlleles { | ||
| 777 | my($self, $subject, $marker) = @_ ; | ||
| 778 | my($sth, $subjectID, $markerID, $gtID, $alleleID, $alleleName, @alleles) ; | ||
| 779 | my $dbh = $self->{dbh} ; | ||
| 780 | |||
| 781 | $sth = $dbh->prepare( "select alleleID from AlleleCall | ||
| 782 | where gtID = ?" ) ; | ||
| 783 | |||
| 784 | $subjectID = $subject->field("id") ; | ||
| 785 | $markerID = $marker->field("id") ; | ||
| 786 | ($gtID) = $dbh->selectrow_array( "select gtID from Genotype | ||
| 787 | where isActive = 1 | ||
| 788 | and subjectID = $subjectID | ||
| 789 | and poID = $markerID" ) ; | ||
| 790 | defined($gtID) or return(undef) ; | ||
| 791 | $sth->execute($gtID) ; | ||
| 792 | while ( ($alleleID) = $sth->fetchrow_array() ) { | ||
| 793 | ($alleleName) = $dbh->selectrow_array( "select name from Allele | ||
| 794 | where alleleID = $alleleID" ) ; | ||
| 795 | push(@alleles, $alleleName) ; | ||
| 796 | } | ||
| 797 | |||
| 798 | return(@alleles) ; | ||
| 799 | } | ||
| 800 | |||
| 801 | =head2 getSNPGtCounts | ||
| 802 | |||
| 803 | Function : | ||
| 804 | Arguments : | ||
| 805 | Returns : | ||
| 806 | Example : getSNPGtCounts() | ||
| 807 | Scope : | ||
| 808 | Comments : | ||
| 809 | |||
| 810 | =cut | ||
| 811 | |||
| 812 | sub getSNPGtCounts { | ||
| 813 | my($self, $snp, $sc) = @_ ; | ||
| 814 | my($snpID, $snpName, $clusterName, $cListPtr, $subjPtr, $gtID, $allelesStr, | ||
| 815 | %allCounts, %noNCounts, @alleles, $i, $gt1, $gt2, $gt3, ) ; | ||
| 816 | my $dbh = $self->{dbh} ; | ||
| 817 | |||
| 818 | if ( ref($snp) ne "Genetics::SNP" ) { | ||
| 819 | croak "ERROR [getSNPGtCounts]: Invalid input SNP $snp." ; | ||
| 820 | } | ||
| 821 | $snpID = $snp->id() ; | ||
| 822 | $snpName = $snp->name() ; | ||
| 823 | if ( ref($sc) ne "Genetics::Cluster" ) { | ||
| 824 | croak "ERROR [calculateHW]: Invalid input Cluster $sc." ; | ||
| 825 | if ( $sc->clusterType() ne "Subject" ) { | ||
| 826 | croak "ERROR [getSNPGtCounts]: Invalid input Cluster type." ; | ||
| 827 | } | ||
| 828 | } | ||
| 829 | $clusterName = $sc->name() ; | ||
| 830 | $cListPtr = $sc->Contents() ; | ||
| 831 | foreach $subjPtr (@$cListPtr) { | ||
| 832 | ($gtID) = $dbh->selectrow_array( "select gtID from Genotype | ||
| 833 | where isActive = 1 | ||
| 834 | and subjectID = $$subjPtr{id} | ||
| 835 | and poID = $snpID" ) ; | ||
| 836 | next unless defined($gtID) ; | ||
| 837 | $allelesStr = join "", $self->getGtAllelesByGt($gtID) ; | ||
| 838 | $allCounts{$allelesStr}++ ; # %allCounts includes gts w/ Ns | ||
| 839 | } | ||
| 840 | @alleles = $self->getAllelesByType($snp, "Nucleotide") ; | ||
| 841 | for ($i=0 ; $i<=$#alleles ; $i++) { | ||
| 842 | splice(@alleles, $i, 1) if $alleles[$i] eq "N" ; | ||
| 843 | } | ||
| 844 | $gt1 = $alleles[0] . $alleles[0] ; | ||
| 845 | $noNCounts{$gt1} = $allCounts{$gt1} || 0 ; | ||
| 846 | $gt2 = $alleles[0] . $alleles[1] ; | ||
| 847 | $noNCounts{$gt2} = $allCounts{$gt2} || 0 ; | ||
| 848 | $gt3 = $alleles[1] . $alleles[1] ; | ||
| 849 | $noNCounts{$gt3} = $allCounts{$gt3} || 0 ; | ||
| 850 | |||
| 851 | return( {%noNCounts} ) ; | ||
| 852 | } | ||
| 853 | |||
| 854 | =head2 getGtAllelesByGt | ||
| 855 | |||
| 856 | Function : For a given genotype, query for and return the allele names. | ||
| 857 | Argument : A Genotype object or id. | ||
| 858 | Returns : An array containing the allele names or undef if there is not an | ||
| 859 | active Genotype associated with the input Subject and Marker. | ||
| 860 | Scope : Public | ||
| 861 | Comments : | ||
| 862 | |||
| 863 | =cut | ||
| 864 | |||
| 865 | sub getGtAllelesByGt { | ||
| 866 | my($self, $arg) = @_ ; | ||
| 867 | my($gtID, $sth, $alleleID, $alleleName, @alleles, ) ; | ||
| 868 | my $dbh = $self->{dbh} ; | ||
| 869 | |||
| 870 | ref($arg) eq "Genetics::Genotype" ? ($gtID = $arg->id()) | ||
| 871 | : ($gtID = $arg) ; | ||
| 872 | |||
| 873 | $sth = $dbh->prepare( "select alleleID from AlleleCall | ||
| 874 | where gtID = ?" ) ; | ||
| 875 | $sth->execute($gtID) ; | ||
| 876 | while ( ($alleleID) = $sth->fetchrow_array() ) { | ||
| 877 | ($alleleName) = $dbh->selectrow_array( "select name from Allele | ||
| 878 | where alleleID = $alleleID" ) ; | ||
| 879 | push(@alleles, $alleleName) ; | ||
| 880 | } | ||
| 881 | |||
| 882 | return(@alleles) ; | ||
| 883 | } | ||
| 884 | |||
| 885 | =head2 getPtValue | ||
| 886 | |||
| 887 | Function : For a given Subject and StudyVariable, query for and return the | ||
| 888 | associated Phenotype value for the active Phenotype. | ||
| 889 | Argument : A Subject object and a StudyVariable object | ||
| 890 | Returns : An scalar containing the Phenotype value or undef if there is not | ||
| 891 | an active Phenotype associated with the input Subject and | ||
| 892 | StudyVariable. | ||
| 893 | Scope : Public | ||
| 894 | Comments : | ||
| 895 | |||
| 896 | =cut | ||
| 897 | |||
| 898 | sub getPtValue { | ||
| 899 | my($self, $subject, $sv) = @_ ; | ||
| 900 | my($subjectID, $svID, $format, $valueFieldName, $value) ; | ||
| 901 | my $dbh = $self->{dbh} ; | ||
| 902 | |||
| 903 | $subjectID = $subject->field("id") ; | ||
| 904 | $svID = $sv->field("id") ; | ||
| 905 | $format = $sv->field("format") ; | ||
| 906 | $valueFieldName = lc($format) . "Value" ; | ||
| 907 | |||
| 908 | ($value) = $dbh->selectrow_array( "select $valueFieldName from Phenotype | ||
| 909 | where isActive = 1 | ||
| 910 | and subjectID = $subjectID | ||
| 911 | and svID = $svID" ) ; | ||
| 912 | defined($value) or return(undef) ; | ||
| 913 | |||
| 914 | return($value) ; | ||
| 915 | } | ||
| 916 | |||
| 917 | =head1 Private Methods | ||
| 918 | |||
| 919 | =head2 _generateSQL | ||
| 920 | |||
| 921 | Function : Generate SQL based on a hash reference of query parameters. | ||
| 922 | Arguments : A reference to a hash containing the query parameters: | ||
| 923 | %query = ( | ||
| 924 | ID => integer | ||
| 925 | TYPE => Object type | ||
| 926 | NAME => text | ||
| 927 | KW => KeywordType.name=Keyword.value | ||
| 928 | WHERE => SQL where clause | ||
| 929 | ) | ||
| 930 | Returns : A string containing the SQL. | ||
| 931 | Scope : Private instance method | ||
| 932 | Called by : getObject() and getObjects() | ||
| 933 | Comments : If the NAME parameter contains Perl wildcard characters * or ?, | ||
| 934 | these are converted to the SQL wildcards % and _. This hopefully | ||
| 935 | results in the expected behavior. | ||
| 936 | NB. KW can only be used by itself or combined with TYPE | ||
| 937 | |||
| 938 | =cut | ||
| 939 | |||
| 940 | sub _generateSQL { | ||
| 941 | my($self, $qPtr) = @_ ; | ||
| 942 | my $dbh = $self->{dbh} ; | ||
| 943 | my($sql, $id, $type, $name, $likeFlag, $where, $kwQStr, | ||
| 944 | $kwtName, $kwValue, $kwtID, $dataType, $kwvFieldName) ; | ||
| 945 | |||
| 946 | # Construct the SQL. Probably there is a more clever way to do this. | ||
| 947 | defined($id = $$qPtr{ID}) or $id = 0 ; | ||
| 948 | defined($type = $$qPtr{TYPE}) or $type = 0 ; | ||
| 949 | if ( defined($name = $$qPtr{NAME}) ) { | ||
| 950 | $likeFlag = $name =~ tr/*?/%_/ ; | ||
| 951 | } else { | ||
| 952 | $name = 0 ; | ||
| 953 | } | ||
| 954 | if ( defined($where = $$qPtr{WHERE}) ) { | ||
| 955 | $where =~ s/^\s*where\s+//i ; | ||
| 956 | } else { | ||
| 957 | $where = 0 ; | ||
| 958 | } | ||
| 959 | if ( defined($kwQStr = $$qPtr{KW}) ) { | ||
| 960 | ($kwtName, $kwValue) = $kwQStr =~ /^([\w ]+)=([\w ]+)/ ; | ||
| 961 | ($kwtID, $dataType) = $dbh->selectrow_array( "select keywordTypeID, dataType | ||
| 962 | from KeywordType | ||
| 963 | where name = '$kwtName'" ) ; | ||
| 964 | $kwvFieldName = "kw." . lc($dataType) . "Value" ; | ||
| 965 | } else { | ||
| 966 | $kwQStr = 0 ; | ||
| 967 | } | ||
| 968 | |||
| 969 | if ($id) { | ||
| 970 | # Query by id only | ||
| 971 | $sql = "select id, objType from Object where id = $id" ; | ||
| 972 | } else { | ||
| 973 | $sql = "select id, objType from Object " ; | ||
| 974 | if ($type) { | ||
| 975 | if ($name) { | ||
| 976 | if ($where) { | ||
| 977 | # Query by type, name and input where clause | ||
| 978 | $likeFlag ? ($sql .= "where objType = '$type' and name like '$name' and $where") | ||
| 979 | : ($sql .= "where objType = '$type' and name = '$name' and $where") ; | ||
| 980 | } elsif ($kwQStr) { | ||
| 981 | # Query by type, name and keyword | ||
| 982 | return(undef) ; | ||
| 983 | } else { | ||
| 984 | # Query by type and name | ||
| 985 | $likeFlag ? ($sql .= "where objType = '$type' and name like '$name'") | ||
| 986 | : ($sql .= "where objType = '$type' and name = '$name'") ; | ||
| 987 | } | ||
| 988 | } else { | ||
| 989 | if ($where) { | ||
| 990 | # Query by type and input where clause | ||
| 991 | $sql .= "where objType = '$type' and $where" ; | ||
| 992 | } elsif ($kwQStr) { | ||
| 993 | # Query by type and keyword | ||
| 994 | $sql = "select obj.id, obj.objType from Object obj, Keyword kw | ||
| 995 | where objType = '$type' and | ||
| 996 | kw.keywordTypeID = $kwtID and | ||
| 997 | $kwvFieldName = '$kwValue' and | ||
| 998 | obj.id = kw.objID" ; | ||
| 999 | } else { | ||
| 1000 | # Query by type only. | ||
| 1001 | $sql .= "where objType = '$type'" ; | ||
| 1002 | } | ||
| 1003 | } | ||
| 1004 | } elsif ($name) { | ||
| 1005 | if ($where) { | ||
| 1006 | # Query by name and input where clause | ||
| 1007 | $likeFlag ? ($sql .= "where name like '$name' and $where") | ||
| 1008 | : ($sql .= "where name = '$name' and $where") ; | ||
| 1009 | } elsif ($kwQStr) { | ||
| 1010 | # Query by name and keyword | ||
| 1011 | return(undef) ; | ||
| 1012 | } else { | ||
| 1013 | # Query by name only | ||
| 1014 | $likeFlag ? ($sql .= "where name like '$name'") | ||
| 1015 | : ($sql .= "where name = '$name'") ; | ||
| 1016 | } | ||
| 1017 | } elsif ($where) { | ||
| 1018 | # Query by input where clause only | ||
| 1019 | return(undef) if $kwQStr ; | ||
| 1020 | $sql .= "where $where" ; | ||
| 1021 | } elsif ($kwQStr) { | ||
| 1022 | # Query by keyword only | ||
| 1023 | $sql = "select obj.id, obj.objType from Object obj, Keyword kw | ||
| 1024 | where kw.keywordTypeID = $kwtID and | ||
| 1025 | $kwvFieldName = '$kwValue' and | ||
| 1026 | obj.id = kw.objID" ; | ||
| 1027 | } else { | ||
| 1028 | carp "WARNING [_generateSQL]: Insufficient query criteria!" ; | ||
| 1029 | return(undef) ; | ||
| 1030 | } | ||
| 1031 | } | ||
| 1032 | #print " ->[_generateSQL] SQL: $sql\n" ; | ||
| 1033 | $DEBUG and carp " ->[_generateSQL] SQL: $sql" ; | ||
| 1034 | |||
| 1035 | return($sql) ; | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | sub _formatFloat { | ||
| 1039 | my($f) = @_; | ||
| 1040 | my($g) ; | ||
| 1041 | |||
| 1042 | if ( $f =~ /(0.\d{3})(\d)(\d)/ ) { | ||
| 1043 | if ($3 >= 5) { | ||
| 1044 | $g = "$1" . ($2 + 1) ; | ||
| 1045 | } else { | ||
| 1046 | $g = $1 . $2 ; | ||
| 1047 | } | ||
| 1048 | } else { | ||
| 1049 | $g = $f ; | ||
| 1050 | } | ||
| 1051 | |||
| 1052 | return($g) ; | ||
| 1053 | } | ||
| 1054 | |||
| 1055 | 1; |