| 1 | # GenPerl module | ||
| 2 | # | ||
| 3 | |||
| 4 | # POD documentation - main docs before the code | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Genetics::API::DB::Update | ||
| 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 | $subject = $api->getSubject(3) ; | ||
| 21 | $subject->name("Elvis") ; | ||
| 22 | $api->updateSubject($subject) ; | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | The Genetics::API::DB packages provide an interface for the manipulation of | ||
| 27 | Genperl objects in a relational database. This package contains the methods | ||
| 28 | for updating objects that have previously been saved to the database. To save | ||
| 29 | new objects, see Genetics::API::DB::Create. | ||
| 30 | |||
| 31 | The following describes the update behavior implemented by the methods in | ||
| 32 | this package: | ||
| 33 | - The data in each object field will completely replace the data in the | ||
| 34 | database for that field. | ||
| 35 | - Data for fields not present in an object will not be affected. | ||
| 36 | - In order to delete data for a particular field, the value of that field | ||
| 37 | should be set to "DELETE". | ||
| 38 | - In order to add to existing data for a particular field, use an appropriate | ||
| 39 | method in Genetics::API or handle it manually. | ||
| 40 | |||
| 41 | Examples: | ||
| 42 | |||
| 43 | To completely replace a SNPs set of Alleles: | ||
| 44 | @alleles = ( {name => "A", type => "Nucleotide"}, | ||
| 45 | name => "C", type => "Nucleotide"} ) ; | ||
| 46 | $snp = $api->getSNP(11) ; | ||
| 47 | $snp->Alleles(\@alleles) ; | ||
| 48 | $api->updateSNP($snp) ; | ||
| 49 | |||
| 50 | To add an Allele to a SNP: | ||
| 51 | $snp = $api->getSNP(11) ; | ||
| 52 | $alleleListptr = $snp->Alleles ; | ||
| 53 | push( @$alleleListptr, {name => "A", type => "Nucleotide"} ) ; | ||
| 54 | $snp->Alleles($alleleListptr) ; | ||
| 55 | $api->updateSNP($snp) ; | ||
| 56 | |||
| 57 | =head1 LICENSE | ||
| 58 | |||
| 59 | This program is free software; you can redistribute it and/or modify | ||
| 60 | it under the terms of the GNU General Public License as published by | ||
| 61 | the Free Software Foundation; either version 2 of the License, or | ||
| 62 | (at your option) any later version. | ||
| 63 | |||
| 64 | This program is distributed in the hope that it will be useful, | ||
| 65 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 66 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 67 | GNU General Public License for more details. | ||
| 68 | |||
| 69 | You should have received a copy of the GNU General Public License | ||
| 70 | along with this program; if not, write to the Free Software | ||
| 71 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 72 | |||
| 73 | =head1 FEEDBACK | ||
| 74 | |||
| 75 | Currently, all feedback should be sent directly to the author. | ||
| 76 | |||
| 77 | =head1 AUTHOR - Steve Mathias | ||
| 78 | |||
| 79 | Email: mathias@genomica.com | ||
| 80 | |||
| 81 | Phone: (720) 565-4029 | ||
| 82 | |||
| 83 | Address: Genomica Corporation | ||
| 84 | 1745 38th Street | ||
| 85 | Boulder, CO 80301 | ||
| 86 | |||
| 87 | =head1 DETAILS | ||
| 88 | |||
| 89 | The rest of the documentation describes each of the object variables and | ||
| 90 | methods. The names of internal variables and methods are preceded with an | ||
| 91 | underscore (_). | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | ################## | ||
| 96 | # # | ||
| 97 | # Begin the code # | ||
| 98 | # # | ||
| 99 | ################## | ||
| 100 | |||
| 101 | package Genetics::API::DB::Update ; | ||
| 102 | |||
| 103 | BEGIN { | ||
| 104 | $ID = "Genetics::API::DB::Update" ; | ||
| 105 | #$DEBUG = $main::DEBUG ; | ||
| 106 | $DEBUG = 0 ; | ||
| 107 | $DEBUG and warn "Debugging in $ID is on" ; | ||
| 108 | } | ||
| 109 | |||
| 110 | =head1 Imported Packages | ||
| 111 | |||
| 112 | strict Just to be anal | ||
| 113 | vars Global variables | ||
| 114 | Carp Error reporting | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | use strict ; | ||
| 119 | use vars qw(@ISA @EXPORT @EXPORT_OK $ID $DEBUG) ; | ||
| 120 | use Carp ; | ||
| 121 | use Exporter ; | ||
| 122 | |||
| 123 | =head1 Inheritance | ||
| 124 | |||
| 125 | Exporter Make methods available to importing packages | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | @ISA = qw(Exporter) ; | ||
| 130 | |||
| 131 | @EXPORT = qw(updateCluster updateSubject updateKindred updateMarker | ||
| 132 | updateSNP updateGenotype updateStudyVariable | ||
| 133 | updatePhenotype updateFrequencySource updateHtMarkerCollection | ||
| 134 | updateHaplotype updateDNASample updateTissueSample updateMap | ||
| 135 | _updateObjAssocData _updateSubjectKindredRefs | ||
| 136 | _updateKindredSubjectRefs _updateAssayAttrs) ; | ||
| 137 | @EXPORT_OK = qw(); | ||
| 138 | |||
| 139 | =head1 Public Methods | ||
| 140 | |||
| 141 | =head2 updateCluster | ||
| 142 | |||
| 143 | Function : Update a Genetics::Object::Cluster object in the database. | ||
| 144 | Argument : The Genetics::Object::Cluster object to be updated. | ||
| 145 | Returns : 1 on success, undef otherwise. | ||
| 146 | Scope : Public | ||
| 147 | Comments : Cluster.clusterType cannot be modified, so this method does | ||
| 148 | not touch the Cluster table. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub updateCluster { | ||
| 153 | my($self, $cluster) = @_ ; | ||
| 154 | my($id, $actualType, $sth, $listPtr, $objRef) ; | ||
| 155 | my $dbh = $self->{dbh} ; | ||
| 156 | |||
| 157 | $DEBUG and carp " ->[updateCluster] $cluster." ; | ||
| 158 | |||
| 159 | $id = $cluster->field("id") ; | ||
| 160 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 161 | where id = $id") ; | ||
| 162 | if ( $actualType ne "Cluster") { | ||
| 163 | carp " ->[updateCluster] Object with ID = $id is not a Cluster!" ; | ||
| 164 | return(undef) ; | ||
| 165 | } | ||
| 166 | # Object | ||
| 167 | $self->_updateObjAssocData($cluster) ; | ||
| 168 | # Contents | ||
| 169 | if ( defined ($listPtr = $cluster->field("Contents")) ) { | ||
| 170 | $dbh->do( "delete from ClusterContents | ||
| 171 | where clusterID = $id" ) ; | ||
| 172 | $sth = $dbh->prepare( "insert into ClusterContents | ||
| 173 | (clusterID, objID) | ||
| 174 | values (?, ?)" ) ; | ||
| 175 | foreach $objRef (@$listPtr) { | ||
| 176 | $sth->execute($id, $$objRef{id}) ; | ||
| 177 | } | ||
| 178 | $sth->finish() ; | ||
| 179 | } | ||
| 180 | |||
| 181 | $DEBUG and carp " ->[updateCluster] End." ; | ||
| 182 | |||
| 183 | return(1) ; | ||
| 184 | } | ||
| 185 | |||
| 186 | =head2 updateSubject | ||
| 187 | |||
| 188 | Function : Update a Genetics::Object::Subject object in the database. | ||
| 189 | Argument : The Genetics::Object::Subject object to be updated. | ||
| 190 | Returns : 1 on success, undef otherwise. | ||
| 191 | Scope : Public | ||
| 192 | Comments : If Subject.kindredID is modified, the approprate updates are also | ||
| 193 | made to KindredSubject. In other words, the reciprocal | ||
| 194 | relationships Kindred->Subjects and Subject->Kindred are kept in | ||
| 195 | synch. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub updateSubject { | ||
| 200 | my($self, $subject) = @_ ; | ||
| 201 | my($id, $actualType, $sth, $sth1, $orgPtr, $orgID, $kindredRef, | ||
| 202 | $momRef, $dadRef, $sex, $date, $isProband) ; | ||
| 203 | my $dbh = $self->{dbh} ; | ||
| 204 | |||
| 205 | $DEBUG and carp " ->[updateSubject] $subject." ; | ||
| 206 | |||
| 207 | $id = $subject->field("id") ; | ||
| 208 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 209 | where id = $id") ; | ||
| 210 | if ( $actualType ne "Subject") { | ||
| 211 | carp " ->[updateSubject] Object with ID = $id is not a Subject!" ; | ||
| 212 | return(undef) ; | ||
| 213 | } | ||
| 214 | # Object | ||
| 215 | $self->_updateObjAssocData($subject) ; | ||
| 216 | # Subject fields | ||
| 217 | if ( defined($orgPtr = $subject->field("Organism")) ) { | ||
| 218 | $sth = $dbh->prepare( "update Subject | ||
| 219 | set organismID = ? | ||
| 220 | where subjectID = ?" ) ; | ||
| 221 | if ( ref($orgPtr) eq "HASH" ) { | ||
| 222 | $orgID = $self->_getOrganismID($orgPtr) ; | ||
| 223 | $sth->execute($orgID, $id) ; | ||
| 224 | } elsif ( ! ref($orgPtr) and ($orgPtr eq "DELETE") ) { | ||
| 225 | $sth->execute(undef, $id) ; | ||
| 226 | } else { | ||
| 227 | carp " ->[_updateSubject] Inappropriate Organism value in $subject." ; | ||
| 228 | } | ||
| 229 | $sth->finish() ; | ||
| 230 | } | ||
| 231 | if ( defined($kindredRef = $subject->field("Kindred")) ) { | ||
| 232 | $sth = $dbh->prepare( "update Subject | ||
| 233 | set kindredID = ? | ||
| 234 | where subjectID = ?" ) ; | ||
| 235 | if ( ref($kindredRef) eq "HASH" ) { | ||
| 236 | $sth->execute($$kindredRef{id}, $id) ; | ||
| 237 | $self->_updateSubjectKindredRefs($id, $$kindredRef{id}) ; | ||
| 238 | } elsif ( ! ref($kindredRef) and ($kindredRef eq "DELETE") ) { | ||
| 239 | $sth->execute(undef, $id) ; | ||
| 240 | $self->_updateSubjectKindredRefs($id, undef) ; | ||
| 241 | } else { | ||
| 242 | carp " ->[_updateSubject] Inappropriate Kindred value in $subject." ; | ||
| 243 | } | ||
| 244 | $sth->finish() ; | ||
| 245 | } | ||
| 246 | if ( defined($momRef = $subject->field("Mother")) ) { | ||
| 247 | $sth = $dbh->prepare( "update Subject | ||
| 248 | set motherID = ? | ||
| 249 | where subjectID = ?" ) ; | ||
| 250 | if ( ref($momRef) eq "HASH" ) { | ||
| 251 | $sth->execute($$momRef{id}, $id) ; | ||
| 252 | } elsif ( ! ref($momRef) and ($momRef eq "DELETE") ) { | ||
| 253 | $sth->execute(undef, $id) ; | ||
| 254 | } else { | ||
| 255 | carp " ->[_updateSubject] Inappropriate Mother value in $subject." ; | ||
| 256 | } | ||
| 257 | $sth->finish() ; | ||
| 258 | } | ||
| 259 | if ( defined($dadRef = $subject->field("Father")) ) { | ||
| 260 | $sth = $dbh->prepare( "update Subject | ||
| 261 | set fatherID = ? | ||
| 262 | where subjectID = ?" ) ; | ||
| 263 | if ( ref($dadRef) eq "HASH" ) { | ||
| 264 | $sth->execute($$dadRef{id}, $id) ; | ||
| 265 | } elsif ( ! ref($dadRef) and ($dadRef eq "DELETE") ) { | ||
| 266 | $sth->execute(undef, $id) ; | ||
| 267 | } else { | ||
| 268 | carp " ->[_updateSubject] Inappropriate Father value in $subject." ; | ||
| 269 | } | ||
| 270 | $sth->finish() ; | ||
| 271 | } | ||
| 272 | $sex = $subject->field("gender") ; | ||
| 273 | $sth = $dbh->prepare( "update Subject | ||
| 274 | set gender = ? | ||
| 275 | where subjectID = ?" ) ; | ||
| 276 | $sth->execute($sex, $id) ; | ||
| 277 | $sth->finish() ; | ||
| 278 | if ( defined($date = $subject->field("dateOfBirth")) ) { | ||
| 279 | $sth = $dbh->prepare( "update Subject | ||
| 280 | set dateOfBirth = ? | ||
| 281 | where subjectID = ?" ) ; | ||
| 282 | if ($date eq "DELETE") { | ||
| 283 | $sth->execute(undef, $id) ; | ||
| 284 | } else { | ||
| 285 | $sth->execute($date, $id) ; | ||
| 286 | } | ||
| 287 | $sth->finish() ; | ||
| 288 | } | ||
| 289 | if ( defined($date = $subject->field("dateOfDeath")) ) { | ||
| 290 | $sth = $dbh->prepare( "update Subject | ||
| 291 | set dateOfDeath = ? | ||
| 292 | where subjectID = ?" ) ; | ||
| 293 | if ($date eq "DELETE") { | ||
| 294 | $sth->execute(undef, $id) ; | ||
| 295 | } else { | ||
| 296 | $sth->execute($date, $id) ; | ||
| 297 | } | ||
| 298 | $sth->finish() ; | ||
| 299 | } | ||
| 300 | $isProband = $subject->field("isProband") ; | ||
| 301 | $sth = $dbh->prepare( "update Subject | ||
| 302 | set isProband = ? | ||
| 303 | where subjectID = ?" ) ; | ||
| 304 | $sth->execute($isProband, $id) ; | ||
| 305 | $sth->finish() ; | ||
| 306 | |||
| 307 | $DEBUG and carp " ->[updateSubject] End." ; | ||
| 308 | |||
| 309 | return(1) ; | ||
| 310 | } | ||
| 311 | |||
| 312 | =head2 updateKindred | ||
| 313 | |||
| 314 | Function : Update a Genetics::Object::Kindred object in the database. | ||
| 315 | Argument : The Genetics::Object::Kindred object to be updated. | ||
| 316 | Returns : 1 on success, undef otherwise. | ||
| 317 | Scope : Public | ||
| 318 | Comments : If the set of Subjects contained in a Kindred is modified, | ||
| 319 | the approprate updates are also made to the Subject.kindredID | ||
| 320 | field of each of the Subjects. In other words, the reciprocal | ||
| 321 | relationships Kindred->Subjects and Subject->Kindred are kept | ||
| 322 | in synch. This only applies to primary Kindreds, of course. | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub updateKindred { | ||
| 327 | my($self, $kindred) = @_ ; | ||
| 328 | my($id, $actualType, $sth, $kindredRef, $subjRef, $subjListPtr, @subjIDs) ; | ||
| 329 | my $dbh = $self->{dbh} ; | ||
| 330 | |||
| 331 | $DEBUG and carp " ->[updateKindred] $kindred." ; | ||
| 332 | |||
| 333 | $id = $kindred->field("id") ; | ||
| 334 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 335 | where id = $id") ; | ||
| 336 | if ( $actualType ne "Kindred") { | ||
| 337 | carp " ->[updateKindred] Object with ID = $id is not a Kindred!" ; | ||
| 338 | return(undef) ; | ||
| 339 | } | ||
| 340 | # Object | ||
| 341 | $self->_updateObjAssocData($kindred) ; | ||
| 342 | # Kindred | ||
| 343 | # Can't update isDerived | ||
| 344 | if ( defined($kindredRef = $kindred->field("DerivedFrom")) ) { | ||
| 345 | $sth = $dbh->prepare( "update Kindred | ||
| 346 | set parentID = ? | ||
| 347 | where kindredID = ?" ) ; | ||
| 348 | if ( ref($kindredRef) eq "HASH" ) { | ||
| 349 | $sth->execute($$kindredRef{id}, $id) ; | ||
| 350 | } elsif ( ! ref($kindredRef) and ($kindredRef eq "DELETE") ) { | ||
| 351 | $sth->execute(undef, $id) ; | ||
| 352 | } else { | ||
| 353 | carp " ->[_updateKindred] Inappropriate DerivedFrom value in $kindred." ; | ||
| 354 | } | ||
| 355 | $sth->finish() ; | ||
| 356 | } | ||
| 357 | # KindredSubject | ||
| 358 | if ( defined($subjListPtr = $kindred->field("Subjects")) ) { | ||
| 359 | $sth = $dbh->prepare( "update Subject | ||
| 360 | set kindredID = ? | ||
| 361 | where subjectID = ?" ) ; | ||
| 362 | if ( ref($subjListPtr) eq "ARRAY" ) { | ||
| 363 | foreach $subjRef (@$subjListPtr) { | ||
| 364 | push(@subjIDs, $$subjRef{id}) ; | ||
| 365 | $sth->execute($id, $$subjRef{id}) ; | ||
| 366 | } | ||
| 367 | $self->_updateKindredSubjectRefs($id, \@subjIDs) ; | ||
| 368 | } elsif ( ! ref($subjListPtr) and ($subjListPtr eq "DELETE") ) { | ||
| 369 | foreach $subjRef (@$subjListPtr) { | ||
| 370 | push(@subjIDs, $$subjRef{id}) ; | ||
| 371 | $sth->execute(undef, $$subjRef{id}) ; | ||
| 372 | } | ||
| 373 | $self->_updateKindredSubjectRefs(undef, \@subjIDs) ; | ||
| 374 | } else { | ||
| 375 | carp " ->[_updateKindred] Inappropriate Subjects value in $kindred." ; | ||
| 376 | } | ||
| 377 | $sth->finish() ; | ||
| 378 | } | ||
| 379 | |||
| 380 | $DEBUG and carp " ->[updateKindred] End." ; | ||
| 381 | |||
| 382 | return(1) ; | ||
| 383 | } | ||
| 384 | |||
| 385 | =head2 updateMarker | ||
| 386 | |||
| 387 | Function : Update a Genetics::Object::Marker object in the database. | ||
| 388 | Argument : The Genetics::Object::Marker object to be updated. | ||
| 389 | Returns : 1 on success, undef otherwise. | ||
| 390 | Scope : Public | ||
| 391 | |||
| 392 | =cut | ||
| 393 | |||
| 394 | sub updateMarker { | ||
| 395 | my($self, $marker) = @_ ; | ||
| 396 | my($id, $actualType, $sth, $sth1, $chr, $orgPtr, $orgID, $seqPtr, $oldSeqID, | ||
| 397 | $newSeqID, $ploidy, $polyType, $idx, $seq, $alleleListPtr, $allelePtr, | ||
| 398 | $iscnListPtr, $iscnMapLocID, $iscnPtr, $iscnID) ; | ||
| 399 | my $dbh = $self->{dbh} ; | ||
| 400 | |||
| 401 | $DEBUG and carp " ->[updateMarker] $marker" ; | ||
| 402 | |||
| 403 | $id = $marker->field("id") ; | ||
| 404 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 405 | where id = $id") ; | ||
| 406 | if ( $actualType ne "Marker") { | ||
| 407 | carp " ->[updateMarker] Object with ID = $id is not a Marker!" ; | ||
| 408 | return(undef) ; | ||
| 409 | } | ||
| 410 | # Object | ||
| 411 | $self->_updateObjAssocData($marker) ; | ||
| 412 | # SequenceObject | ||
| 413 | if ( defined($chr = $marker->field("chromosome")) ) { | ||
| 414 | $sth = $dbh->prepare( "update SequenceObject | ||
| 415 | set chromosome = ? | ||
| 416 | where seqObjectID = ?" ) ; | ||
| 417 | if ($chr eq "DELETE") { | ||
| 418 | $sth->execute(undef, $id) ; | ||
| 419 | } else { | ||
| 420 | $sth->execute($chr, $id) ; | ||
| 421 | } | ||
| 422 | $sth->finish() ; | ||
| 423 | } | ||
| 424 | if ( defined($orgPtr = $marker->field("Organism")) ) { | ||
| 425 | $sth = $dbh->prepare( "update SequenceObject | ||
| 426 | set organismID = ? | ||
| 427 | where seqObjectID = ?" ) ; | ||
| 428 | if ( ref($orgPtr) eq "HASH" ) { | ||
| 429 | $orgID = $self->_getOrganismID($orgPtr) ; | ||
| 430 | $sth->execute($orgID, $id) ; | ||
| 431 | } elsif ( ! ref($orgPtr) and ($orgPtr eq "DELETE") ) { | ||
| 432 | $sth->execute(undef, $id) ; | ||
| 433 | } else { | ||
| 434 | carp " ->[_updateMarker] Inappropriate Organism value in $marker." ; | ||
| 435 | } | ||
| 436 | $sth->finish() ; | ||
| 437 | } | ||
| 438 | if ( defined($seqPtr = $marker->field("Sequence")) ) { | ||
| 439 | $sth = $dbh->prepare( "insert into Sequence | ||
| 440 | (sequenceID, sequence, length, lengthUnits) | ||
| 441 | values (?, ?, ?, ?)" ) ; | ||
| 442 | $sth1 = $dbh->prepare( "update SequenceObject | ||
| 443 | set sequenceID = ? | ||
| 444 | where seqObjectID = ?" ) ; | ||
| 445 | if ( ref($seqPtr) eq "HASH" ) { | ||
| 446 | ( $oldSeqID ) = $dbh->selectrow_array( "select sequenceID | ||
| 447 | from SequenceObject | ||
| 448 | where seqObjectID = $id" ) ; | ||
| 449 | if ( defined($oldSeqID) ) { | ||
| 450 | $dbh->do( "delete from Sequence | ||
| 451 | where sequenceID = $oldSeqID" ) ; | ||
| 452 | } | ||
| 453 | $sth->execute(undef, $$seqPtr{sequence}, $$seqPtr{length}, $$seqPtr{lengthUnits}) ; | ||
| 454 | $newSeqID = $sth->{'mysql_insertid'} ; | ||
| 455 | $sth1->execute($newSeqID, $id) ; | ||
| 456 | } elsif ( ! ref($seqPtr) and ($seqPtr eq "DELETE") ) { | ||
| 457 | ( $oldSeqID ) = $dbh->selectrow_array( "select sequenceID | ||
| 458 | from SequenceObject | ||
| 459 | where seqObjectID = $id" ) ; | ||
| 460 | if ( defined($oldSeqID) ) { | ||
| 461 | $dbh->do( "delete from Sequence | ||
| 462 | where sequenceID = $oldSeqID" ) ; | ||
| 463 | } | ||
| 464 | $sth1->execute(undef, $id) ; | ||
| 465 | } else { | ||
| 466 | carp " ->[_updateMarker] Inappropriate Sequence value in $marker." ; | ||
| 467 | } | ||
| 468 | $sth->finish() ; | ||
| 469 | $sth1->finish() ; | ||
| 470 | } | ||
| 471 | if ( defined($ploidy = $marker->field("malePloidy")) ) { | ||
| 472 | $sth = $dbh->prepare( "update SequenceObject | ||
| 473 | set malePloidy = ? | ||
| 474 | where seqObjectID = ?" ) ; | ||
| 475 | if ($ploidy eq "DELETE") { | ||
| 476 | $sth->execute(undef, $id) ; | ||
| 477 | } else { | ||
| 478 | $sth->execute($ploidy, $id) ; | ||
| 479 | } | ||
| 480 | $sth->finish() ; | ||
| 481 | } | ||
| 482 | if ( defined($ploidy = $marker->field("femalePloidy")) ) { | ||
| 483 | $sth = $dbh->prepare( "update SequenceObject | ||
| 484 | set femalePloidy = ? | ||
| 485 | where seqObjectID = ?" ) ; | ||
| 486 | if ($ploidy eq "DELETE") { | ||
| 487 | $sth->execute(undef, $id) ; | ||
| 488 | } else { | ||
| 489 | $sth->execute($ploidy, $id) ; | ||
| 490 | } | ||
| 491 | $sth->finish() ; | ||
| 492 | } | ||
| 493 | # Marker | ||
| 494 | if ( defined($polyType = $marker->field("polymorphismType")) ) { | ||
| 495 | $sth = $dbh->prepare( "update Marker | ||
| 496 | set polymorphismType = ? | ||
| 497 | where markerID = ?" ) ; | ||
| 498 | if ($polyType eq "DELETE") { | ||
| 499 | $sth->execute(undef, $id) ; | ||
| 500 | } else { | ||
| 501 | $sth->execute($polyType, $id) ; | ||
| 502 | } | ||
| 503 | $sth->finish() ; | ||
| 504 | } | ||
| 505 | if ( defined($idx = $marker->field("polymorphismIndex1")) ) { | ||
| 506 | $sth = $dbh->prepare( "update Marker | ||
| 507 | set polymorphismIndex1 = ? | ||
| 508 | where markerID = ?" ) ; | ||
| 509 | if ($idx eq "DELETE") { | ||
| 510 | $sth->execute(undef, $id) ; | ||
| 511 | } else { | ||
| 512 | $sth->execute($idx, $id) ; | ||
| 513 | } | ||
| 514 | $sth->finish() ; | ||
| 515 | } | ||
| 516 | if ( defined($idx = $marker->field("polymorphismIndex2")) ) { | ||
| 517 | $sth = $dbh->prepare( "update Marker | ||
| 518 | set polymorphismIndex2 = ? | ||
| 519 | where markerID = ?" ) ; | ||
| 520 | if ($idx eq "DELETE") { | ||
| 521 | $sth->execute(undef, $id) ; | ||
| 522 | } else { | ||
| 523 | $sth->execute($idx, $id) ; | ||
| 524 | } | ||
| 525 | $sth->finish() ; | ||
| 526 | } | ||
| 527 | if ( defined($seq = $marker->field("repeatSequence")) ) { | ||
| 528 | $sth = $dbh->prepare( "update Marker | ||
| 529 | set repeatSequence = ? | ||
| 530 | where markerID = ?" ) ; | ||
| 531 | if ($seq eq "DELETE") { | ||
| 532 | $sth->execute(undef, $id) ; | ||
| 533 | } else { | ||
| 534 | $sth->execute($seq, $id) ; | ||
| 535 | } | ||
| 536 | $sth->finish() ; | ||
| 537 | } | ||
| 538 | # Allele data | ||
| 539 | if ( defined($alleleListPtr = $marker->field("Alleles")) ) { | ||
| 540 | if ( ref($alleleListPtr) eq "ARRAY" ) { | ||
| 541 | $dbh->do( "delete from Allele | ||
| 542 | where poID = $id" ) ; | ||
| 543 | $sth = $dbh->prepare( "insert into Allele | ||
| 544 | (alleleID, poID, name, type) | ||
| 545 | values (?, ?, ?, ?)" ) ; | ||
| 546 | foreach $allelePtr (@$alleleListPtr) { | ||
| 547 | $sth->execute(undef, $id, $$allelePtr{name}, $$allelePtr{type}) ; | ||
| 548 | } | ||
| 549 | $sth->finish() ; | ||
| 550 | } elsif ( ! ref($alleleListPtr) and ($alleleListPtr eq "DELETE") ) { | ||
| 551 | $dbh->do( "delete from Allele | ||
| 552 | where poID = $id" ) ; | ||
| 553 | } else { | ||
| 554 | carp " ->[_updateMarker] Inappropriate Alleles value in $marker." ; | ||
| 555 | } | ||
| 556 | } | ||
| 557 | # ISCNMapLocation data | ||
| 558 | if ( defined($iscnListPtr = $marker->field("ISCNMapLocations")) ) { | ||
| 559 | $sth = $dbh->prepare( "select iscnMapLocID | ||
| 560 | from SeqObjISCN | ||
| 561 | where seqObjectID = $id" ) ; | ||
| 562 | if ( ref($iscnListPtr) eq "ARRAY" ) { | ||
| 563 | $sth->execute() ; | ||
| 564 | while ( ($iscnMapLocID) = $sth->fetchrow_array() ) { | ||
| 565 | $dbh->do( "delete from ISCNMapLocation | ||
| 566 | where iscnMapLocID = $iscnMapLocID" ) ; | ||
| 567 | } | ||
| 568 | $sth->finish() ; | ||
| 569 | $dbh->do( "delete from SeqObjISCN | ||
| 570 | where seqObjectID = $id" ) ; | ||
| 571 | $sth = $dbh->prepare( "insert into ISCNMapLocation | ||
| 572 | (iscnMapLocID, chrNumber, chrArm, band, bandingMethod) | ||
| 573 | values (?, ?, ?, ?, ?)" ) ; | ||
| 574 | $sth1 = $dbh->prepare( "insert into SeqObjISCN | ||
| 575 | (seqObjectID, iscnMapLocID) | ||
| 576 | values (?, ?)" ) ; | ||
| 577 | foreach $iscnPtr (@$iscnListPtr) { | ||
| 578 | $sth->execute(undef, $$iscnPtr{chrNumber}, $$iscnPtr{chrArm}, $$iscnPtr{band}, | ||
| 579 | $$iscnPtr{bandingMethod}) ; | ||
| 580 | $iscnID = $sth->{'mysql_insertid'} ; | ||
| 581 | $sth1->execute($id, $iscnID) ; | ||
| 582 | } | ||
| 583 | } elsif ( ! ref($iscnListPtr) and ($iscnListPtr eq "DELETE") ) { | ||
| 584 | $sth->execute() ; | ||
| 585 | while ( ($iscnMapLocID) = $sth->fetchrow_array() ) { | ||
| 586 | $dbh->do( "delete from ISCNMapLocation | ||
| 587 | where iscnMapLocID = $iscnMapLocID" ) ; | ||
| 588 | } | ||
| 589 | $sth->finish() ; | ||
| 590 | $dbh->do( "delete from SeqObjISCN | ||
| 591 | where seqObjectID = $id" ) ; | ||
| 592 | } else { | ||
| 593 | carp " ->[_updateMarker] Inappropriate ISCNMapLocations value in $marker." ; | ||
| 594 | } | ||
| 595 | } | ||
| 596 | |||
| 597 | $DEBUG and carp " ->[updateMarker] End." ; | ||
| 598 | |||
| 599 | return(1) ; | ||
| 600 | } | ||
| 601 | |||
| 602 | =head2 updateSNP | ||
| 603 | |||
| 604 | Function : Update a Genetics::Object::SNP object in the database. | ||
| 605 | Argument : The Genetics::Object::SNP object to be updated. | ||
| 606 | Returns : 1 on success, undef otherwise. | ||
| 607 | Scope : Public | ||
| 608 | |||
| 609 | =cut | ||
| 610 | |||
| 611 | sub updateSNP { | ||
| 612 | my($self, $snp) = @_ ; | ||
| 613 | my($id, $actualType, $sth, $sth1, $chr, $orgPtr, $orgID, $seqPtr, $oldSeqID, | ||
| 614 | $newSeqID, $ploidy, $type, $class, $idx, $conf, $method, $alleleListPtr, | ||
| 615 | $allelePtr, $iscnListPtr, $iscnMapLocID, $iscnPtr, $iscnID) ; | ||
| 616 | my $dbh = $self->{dbh} ; | ||
| 617 | |||
| 618 | $DEBUG and carp " ->[updateSNP] $snp" ; | ||
| 619 | |||
| 620 | $id = $snp->field("id") ; | ||
| 621 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 622 | where id = $id") ; | ||
| 623 | if ( $actualType ne "SNP") { | ||
| 624 | carp " ->[updateSNP] Object with ID = $id is not a SNP!" ; | ||
| 625 | return(undef) ; | ||
| 626 | } | ||
| 627 | # Object | ||
| 628 | $self->_updateObjAssocData($snp) ; | ||
| 629 | # SequenceObject | ||
| 630 | if ( defined($chr = $snp->field("chromosome")) ) { | ||
| 631 | $sth = $dbh->prepare( "update SequenceObject | ||
| 632 | set chromosome = ? | ||
| 633 | where seqObjectID = ?" ) ; | ||
| 634 | if ($chr eq "DELETE") { | ||
| 635 | $sth->execute(undef, $id) ; | ||
| 636 | } else { | ||
| 637 | $sth->execute($chr, $id) ; | ||
| 638 | } | ||
| 639 | $sth->finish() ; | ||
| 640 | } | ||
| 641 | if ( defined($orgPtr = $snp->field("Organism")) ) { | ||
| 642 | $sth = $dbh->prepare( "update SequenceObject | ||
| 643 | set organismID = ? | ||
| 644 | where seqObjectID = ?" ) ; | ||
| 645 | if ( ref($orgPtr) eq "HASH" ) { | ||
| 646 | $orgID = $self->_getOrganismID($orgPtr) ; | ||
| 647 | $sth->execute($orgID, $id) ; | ||
| 648 | } elsif ( ! ref($orgPtr) and ($orgPtr eq "DELETE") ) { | ||
| 649 | $sth->execute(undef, $id) ; | ||
| 650 | } else { | ||
| 651 | carp " ->[_updateSNP] Inappropriate Organism value in $snp." ; | ||
| 652 | } | ||
| 653 | $sth->finish() ; | ||
| 654 | } | ||
| 655 | if ( defined($seqPtr = $snp->field("Sequence")) ) { | ||
| 656 | $sth = $dbh->prepare( "insert into Sequence | ||
| 657 | (sequenceID, sequence, length, lengthUnits) | ||
| 658 | values (?, ?, ?, ?)" ) ; | ||
| 659 | $sth1 = $dbh->prepare( "update SequenceObject | ||
| 660 | set sequenceID = ? | ||
| 661 | where seqObjectID = ?" ) ; | ||
| 662 | if ( ref($seqPtr) eq "HASH" ) { | ||
| 663 | ( $oldSeqID ) = $dbh->selectrow_array( "select sequenceID | ||
| 664 | from SequenceObject | ||
| 665 | where seqObjectID = $id" ) ; | ||
| 666 | if ( defined($oldSeqID) ) { | ||
| 667 | $dbh->do( "delete from Sequence | ||
| 668 | where sequenceID = $oldSeqID" ) ; | ||
| 669 | } | ||
| 670 | $sth->execute(undef, $$seqPtr{sequence}, $$seqPtr{length}, $$seqPtr{lengthUnits}) ; | ||
| 671 | $newSeqID = $sth->{'mysql_insertid'} ; | ||
| 672 | $sth1->execute($newSeqID, $id) ; | ||
| 673 | } elsif ( ! ref($seqPtr) and ($seqPtr eq "DELETE") ) { | ||
| 674 | ( $oldSeqID ) = $dbh->selectrow_array( "select sequenceID | ||
| 675 | from SequenceObject | ||
| 676 | where seqObjectID = $id" ) ; | ||
| 677 | if ( defined($oldSeqID) ) { | ||
| 678 | $dbh->do( "delete from Sequence | ||
| 679 | where sequenceID = $oldSeqID" ) ; | ||
| 680 | } | ||
| 681 | $sth1->execute(undef, $id) ; | ||
| 682 | } else { | ||
| 683 | carp " ->[updateSNP] Inappropriate Sequence value in $snp." ; | ||
| 684 | } | ||
| 685 | $sth->finish() ; | ||
| 686 | $sth1->finish() ; | ||
| 687 | } | ||
| 688 | if ( defined($ploidy = $snp->field("malePloidy")) ) { | ||
| 689 | $sth = $dbh->prepare( "update SequenceObject | ||
| 690 | set malePloidy = ? | ||
| 691 | where seqObjectID = ?" ) ; | ||
| 692 | if ($ploidy eq "DELETE") { | ||
| 693 | $sth->execute(undef, $id) ; | ||
| 694 | } else { | ||
| 695 | $sth->execute($ploidy, $id) ; | ||
| 696 | } | ||
| 697 | $sth->finish() ; | ||
| 698 | } | ||
| 699 | if ( defined($ploidy = $snp->field("femalePloidy")) ) { | ||
| 700 | $sth = $dbh->prepare( "update SequenceObject | ||
| 701 | set femalePloidy = ? | ||
| 702 | where seqObjectID = ?" ) ; | ||
| 703 | if ($ploidy eq "DELETE") { | ||
| 704 | $sth->execute(undef, $id) ; | ||
| 705 | } else { | ||
| 706 | $sth->execute($ploidy, $id) ; | ||
| 707 | } | ||
| 708 | $sth->finish() ; | ||
| 709 | } | ||
| 710 | # SNP | ||
| 711 | if ( defined($type = $snp->field("snpType")) ) { | ||
| 712 | $sth = $dbh->prepare( "update SNP | ||
| 713 | set snpType = ? | ||
| 714 | where snpID = ?" ) ; | ||
| 715 | if ($type eq "DELETE") { | ||
| 716 | $sth->execute(undef, $id) ; | ||
| 717 | } else { | ||
| 718 | $sth->execute($type, $id) ; | ||
| 719 | } | ||
| 720 | $sth->finish() ; | ||
| 721 | } | ||
| 722 | if ( defined($class = $snp->field("functionClass")) ) { | ||
| 723 | $sth = $dbh->prepare( "update SNP | ||
| 724 | set functionClass = ? | ||
| 725 | where snpID = ?" ) ; | ||
| 726 | if ($class eq "DELETE") { | ||
| 727 | $sth->execute(undef, $id) ; | ||
| 728 | } else { | ||
| 729 | $sth->execute($class, $id) ; | ||
| 730 | } | ||
| 731 | $sth->finish() ; | ||
| 732 | } | ||
| 733 | if ( defined($idx = $snp->field("snpIndex")) ) { | ||
| 734 | $sth = $dbh->prepare( "update SNP | ||
| 735 | set snpIndex = ? | ||
| 736 | where snpID = ?" ) ; | ||
| 737 | if ($idx eq "DELETE") { | ||
| 738 | $sth->execute(undef, $id) ; | ||
| 739 | } else { | ||
| 740 | $sth->execute($idx, $id) ; | ||
| 741 | } | ||
| 742 | $sth->finish() ; | ||
| 743 | } | ||
| 744 | if ( defined($conf = $snp->field("isConfirmed")) ) { | ||
| 745 | $sth = $dbh->prepare( "update SNP | ||
| 746 | set isConfirmed = ? | ||
| 747 | where snpID = ?" ) ; | ||
| 748 | if ($conf eq "DELETE") { | ||
| 749 | $sth->execute(undef, $id) ; | ||
| 750 | } else { | ||
| 751 | $sth->execute($conf, $id) ; | ||
| 752 | } | ||
| 753 | $sth->finish() ; | ||
| 754 | } | ||
| 755 | if ( defined($method = $snp->field("confirmMethod")) ) { | ||
| 756 | $sth = $dbh->prepare( "update SNP | ||
| 757 | set confirmMethod = ? | ||
| 758 | where snpID = ?" ) ; | ||
| 759 | if ($method eq "DELETE") { | ||
| 760 | $sth->execute(undef, $id) ; | ||
| 761 | } else { | ||
| 762 | $sth->execute($method, $id) ; | ||
| 763 | } | ||
| 764 | $sth->finish() ; | ||
| 765 | } | ||
| 766 | # Allele data | ||
| 767 | if ( defined($alleleListPtr = $snp->field("Alleles")) ) { | ||
| 768 | if ( ref($alleleListPtr) eq "ARRAY" ) { | ||
| 769 | $dbh->do( "delete from Allele | ||
| 770 | where poID = $id" ) ; | ||
| 771 | $sth = $dbh->prepare( "insert into Allele | ||
| 772 | (alleleID, poID, name, type) | ||
| 773 | values (?, ?, ?, ?)" ) ; | ||
| 774 | foreach $allelePtr (@$alleleListPtr) { | ||
| 775 | $sth->execute(undef, $id, $$allelePtr{name}, $$allelePtr{type}) ; | ||
| 776 | } | ||
| 777 | $sth->finish() ; | ||
| 778 | } elsif ( ! ref($alleleListPtr) and ($alleleListPtr eq "DELETE") ) { | ||
| 779 | $dbh->do( "delete from Allele | ||
| 780 | where poID = $id" ) ; | ||
| 781 | } else { | ||
| 782 | carp " ->[_updateSNP] Inappropriate Alleles value in $snp." ; | ||
| 783 | } | ||
| 784 | } | ||
| 785 | # ISCNMapLocation data | ||
| 786 | if ( defined($iscnListPtr = $snp->field("ISCNMapLocations")) ) { | ||
| 787 | $sth = $dbh->prepare( "select iscnMapLocID | ||
| 788 | from SeqObjISCN | ||
| 789 | where seqObjectID = $id" ) ; | ||
| 790 | if ( ref($iscnListPtr) eq "ARRAY" ) { | ||
| 791 | $sth->execute() ; | ||
| 792 | while ( ($iscnMapLocID) = $sth->fetchrow_array() ) { | ||
| 793 | $dbh->do( "delete from ISCNMapLocation | ||
| 794 | where iscnMapLocID = $iscnMapLocID" ) ; | ||
| 795 | } | ||
| 796 | $sth->finish() ; | ||
| 797 | $dbh->do( "delete from SeqObjISCN | ||
| 798 | where seqObjectID = $id" ) ; | ||
| 799 | $sth = $dbh->prepare( "insert into ISCNMapLocation | ||
| 800 | (iscnMapLocID, chrNumber, chrArm, band, bandingMethod) | ||
| 801 | values (?, ?, ?, ?, ?)" ) ; | ||
| 802 | $sth1 = $dbh->prepare( "insert into SeqObjISCN | ||
| 803 | (seqObjectID, iscnMapLocID) | ||
| 804 | values (?, ?)" ) ; | ||
| 805 | foreach $iscnPtr (@$iscnListPtr) { | ||
| 806 | $sth->execute(undef, $$iscnPtr{chrNumber}, $$iscnPtr{chrArm}, $$iscnPtr{band}, | ||
| 807 | $$iscnPtr{bandingMethod}) ; | ||
| 808 | $iscnID = $sth->{'mysql_insertid'} ; | ||
| 809 | $sth1->execute($id, $iscnID) ; | ||
| 810 | } | ||
| 811 | } elsif ( ! ref($iscnListPtr) and ($iscnListPtr eq "DELETE") ) { | ||
| 812 | $sth->execute() ; | ||
| 813 | while ( ($iscnMapLocID) = $sth->fetchrow_array() ) { | ||
| 814 | $dbh->do( "delete from ISCNMapLocation | ||
| 815 | where iscnMapLocID = $iscnMapLocID" ) ; | ||
| 816 | } | ||
| 817 | $sth->finish() ; | ||
| 818 | $dbh->do( "delete from SeqObjISCN | ||
| 819 | where seqObjectID = $id" ) ; | ||
| 820 | } else { | ||
| 821 | carp " ->[_updateSNP] Inappropriate ISCNMapLocations value in $snp." ; | ||
| 822 | } | ||
| 823 | } | ||
| 824 | |||
| 825 | $DEBUG and carp " ->[updateSNP] End." ; | ||
| 826 | |||
| 827 | return(1) ; | ||
| 828 | } | ||
| 829 | |||
| 830 | =head2 updateGenotype | ||
| 831 | |||
| 832 | Function : Update a Genetics::Object::Genotype object in the database. | ||
| 833 | Argument : The Genetics::Object::Genotype object to be updated. | ||
| 834 | Returns : 1 on success, undef otherwise. | ||
| 835 | Scope : Public | ||
| 836 | |||
| 837 | =cut | ||
| 838 | |||
| 839 | sub updateGenotype { | ||
| 840 | my($self, $gt) = @_ ; | ||
| 841 | my($id, $actualType, $sth, $active, $icResult, $date, $acListPtr, | ||
| 842 | $poID, $sthAC, $sthA, $sortOrder, $acPtr, $alleleID, $aaListPtr, | ||
| 843 | $alleleCallID) ; | ||
| 844 | my $dbh = $self->{dbh} ; | ||
| 845 | |||
| 846 | $DEBUG and carp " ->[updateGenotype] $gt" ; | ||
| 847 | |||
| 848 | $id = $gt->field("id") ; | ||
| 849 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 850 | where id = $id") ; | ||
| 851 | if ( $actualType ne "Genotype") { | ||
| 852 | carp " ->[updateGenotype] Object with ID = $id is not a Genotype!" ; | ||
| 853 | return(undef) ; | ||
| 854 | } | ||
| 855 | # Object | ||
| 856 | $self->_updateObjAssocData($gt) ; | ||
| 857 | # Genotype | ||
| 858 | if ( defined($active = $gt->field("isActive")) ) { | ||
| 859 | $sth = $dbh->prepare( "update Genotype | ||
| 860 | set isActive = ? | ||
| 861 | where gtID = ?" ) ; | ||
| 862 | $sth->execute($active, $id) ; | ||
| 863 | $sth->finish() ; | ||
| 864 | } | ||
| 865 | if ( defined($icResult = $gt->field("icResult")) ) { | ||
| 866 | $sth = $dbh->prepare( "update Genotype | ||
| 867 | set icResult = ? | ||
| 868 | where gtID = ?" ) ; | ||
| 869 | if ($icResult eq "DELETE") { | ||
| 870 | $sth->execute(undef, $id) ; | ||
| 871 | } else { | ||
| 872 | $sth->execute($icResult, $id) ; | ||
| 873 | } | ||
| 874 | $sth->finish() ; | ||
| 875 | } | ||
| 876 | if ( defined($date = $gt->field("dateCollected")) ) { | ||
| 877 | $sth = $dbh->prepare( "update Genotype | ||
| 878 | set dateCollected = ? | ||
| 879 | where gtID = ?" ) ; | ||
| 880 | if ($date eq "DELETE") { | ||
| 881 | $sth->execute(undef, $id) ; | ||
| 882 | } else { | ||
| 883 | $sth->execute($date, $id) ; | ||
| 884 | } | ||
| 885 | $sth->finish() ; | ||
| 886 | } | ||
| 887 | if ( defined($acListPtr = $gt->field("AlleleCalls")) ) { | ||
| 888 | ( $poID ) = $dbh->selectrow_array( "select poID | ||
| 889 | from Genotype | ||
| 890 | where gtID = $id" ) ; | ||
| 891 | if ( ref($acListPtr) eq "ARRAY" ) { | ||
| 892 | $dbh->do( "delete from AlleleCall | ||
| 893 | where gtID = $id" ) ; | ||
| 894 | $sthAC = $dbh->prepare( "insert into AlleleCall | ||
| 895 | (alleleCallID, gtID, alleleID, sortOrder, phase) | ||
| 896 | values (?, ?, ?, ?, ?)" ) ; | ||
| 897 | $sthA = $dbh->prepare( "insert into Allele | ||
| 898 | (alleleID, poID, name, type) | ||
| 899 | values (?, ?, ?, ?)" ) ; | ||
| 900 | $sortOrder = 1 ; | ||
| 901 | foreach $acPtr (@$acListPtr) { | ||
| 902 | # Check if the Marker already has an Allele w/ the same name and type... | ||
| 903 | ( $alleleID ) = $dbh->selectrow_array( "select alleleID from Allele | ||
| 904 | where poID = $poID and | ||
| 905 | name = '$$acPtr{alleleName}' and | ||
| 906 | type = '$$acPtr{alleleType}'" ) ; | ||
| 907 | if ( ! defined $alleleID) { | ||
| 908 | # ...if not, create a new Allele | ||
| 909 | $sthA->execute(undef, $poID, $$acPtr{alleleName}, $$acPtr{alleleType}) ; | ||
| 910 | $alleleID = $sthA->{'mysql_insertid'} ; | ||
| 911 | } | ||
| 912 | $sthAC->execute(undef, $id, $alleleID, $sortOrder, $$acPtr{phase}) ; | ||
| 913 | $alleleCallID = $sthAC->{'mysql_insertid'} ; | ||
| 914 | $sortOrder++ ; | ||
| 915 | # AlleleCall AssayAttributes | ||
| 916 | if ( defined ($aaListPtr = $$acPtr{AssayAttrs}) ) { | ||
| 917 | $self->_updateAssayAttrs($aaListPtr, "AlleleCall", $alleleCallID) ; | ||
| 918 | } | ||
| 919 | } | ||
| 920 | $sthAC->finish() ; | ||
| 921 | $sthA->finish() ; | ||
| 922 | } | ||
| 923 | } else { | ||
| 924 | carp " ->[_updateGenotype] Inappropriate AlleleCalls value in $gt." ; | ||
| 925 | } | ||
| 926 | # Genotype AssayAttributes | ||
| 927 | if ( defined ($aaListPtr = $gt->field("AssayAttrs")) ) { | ||
| 928 | $self->_updateAssayAttrs($aaListPtr, "Genotype", $id) ; | ||
| 929 | } | ||
| 930 | |||
| 931 | $DEBUG and carp " ->[updateGenotype] End." ; | ||
| 932 | |||
| 933 | return(1) ; | ||
| 934 | } | ||
| 935 | |||
| 936 | =head2 updateStudyVariable | ||
| 937 | |||
| 938 | Function : Update a Genetics::Object::StudyVariable object in the database. | ||
| 939 | Argument : The Genetics::Object::StudyVariable object to be updated. | ||
| 940 | Returns : 1 on success, undef otherwise. | ||
| 941 | Scope : Public | ||
| 942 | Comments : StudyVariable.format cannot be modified. | ||
| 943 | |||
| 944 | =cut | ||
| 945 | |||
| 946 | sub updateStudyVariable { | ||
| 947 | my($self, $sv) = @_ ; | ||
| 948 | my($id, $format, $category, $actualType, $sth, $isX, $desc, $bound, | ||
| 949 | $codesListPtr, $codePtr, $arrRef, $sth1, $cdID, $oldAsdID, $asdPtr, $asdID, $aseListPtr, | ||
| 950 | $asePtr, $oldLcdID, $lcDefPtr, $lcdID, $lcListPtr, $lcPtr) ; | ||
| 951 | my $dbh = $self->{dbh} ; | ||
| 952 | |||
| 953 | $DEBUG and carp " ->[updateStudyVariable] $sv" ; | ||
| 954 | |||
| 955 | $id = $sv->field("id") ; | ||
| 956 | $format = $sv->field("format") ; | ||
| 957 | $category = $sv->field("category") ; | ||
| 958 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 959 | where id = $id") ; | ||
| 960 | if ( $actualType ne "StudyVariable") { | ||
| 961 | carp " ->[updateStudyVariable] Object with ID = $id is not a StudyVariable!" ; | ||
| 962 | return(undef) ; | ||
| 963 | } | ||
| 964 | # Object | ||
| 965 | $self->_updateObjAssocData($sv) ; | ||
| 966 | # StudyVariable data | ||
| 967 | $sth = $dbh->prepare( "update StudyVariable | ||
| 968 | set category = ? | ||
| 969 | where studyVariableID = ?" ) ; | ||
| 970 | $sth->execute($category, $id) ; | ||
| 971 | $sth->finish() ; | ||
| 972 | if ( defined($isX = $sv->field("isXLinked")) ) { | ||
| 973 | $sth = $dbh->prepare( "update StudyVariable | ||
| 974 | set isXLinked = ? | ||
| 975 | where studyVariableID = ?" ) ; | ||
| 976 | $sth->execute($isX, $id) ; | ||
| 977 | $sth->finish() ; | ||
| 978 | } | ||
| 979 | if ( defined($desc = $sv->field("description")) ) { | ||
| 980 | $sth = $dbh->prepare( "update StudyVariable | ||
| 981 | set description = ? | ||
| 982 | where studyVariableID = ?" ) ; | ||
| 983 | if ($desc eq "DELETE") { | ||
| 984 | $sth->execute(undef, $id) ; | ||
| 985 | } else { | ||
| 986 | $sth->execute($desc, $id) ; | ||
| 987 | } | ||
| 988 | $sth->finish() ; | ||
| 989 | } | ||
| 990 | if ($format eq "Number") { | ||
| 991 | if ( defined($bound = $sv->field("lowerBound")) ) { | ||
| 992 | $sth = $dbh->prepare( "update StudyVariable | ||
| 993 | set numberLowerBound = ? | ||
| 994 | where studyVariableID = ?" ) ; | ||
| 995 | if ($bound eq "DELETE") { | ||
| 996 | $sth->execute(undef, $id) ; | ||
| 997 | } else { | ||
| 998 | $sth->execute($bound, $id) ; | ||
| 999 | } | ||
| 1000 | $sth->finish() ; | ||
| 1001 | } | ||
| 1002 | if ( defined($bound = $sv->field("upperBound")) ) { | ||
| 1003 | $sth = $dbh->prepare( "update StudyVariable | ||
| 1004 | set numberUpperBound = ? | ||
| 1005 | where studyVariableID = ?" ) ; | ||
| 1006 | if ($bound eq "DELETE") { | ||
| 1007 | $sth->execute(undef, $id) ; | ||
| 1008 | } else { | ||
| 1009 | $sth->execute($bound, $id) ; | ||
| 1010 | } | ||
| 1011 | $sth->finish() ; | ||
| 1012 | } | ||
| 1013 | } | ||
| 1014 | if ($format eq "Date") { | ||
| 1015 | if ( defined($bound = $sv->field("lowerBound")) ) { | ||
| 1016 | $sth = $dbh->prepare( "update StudyVariable | ||
| 1017 | set dateLowerBound = ? | ||
| 1018 | where studyVariableID = ?" ) ; | ||
| 1019 | if ($bound eq "DELETE") { | ||
| 1020 | $sth->execute(undef, $id) ; | ||
| 1021 | } else { | ||
| 1022 | $sth->execute($bound, $id) ; | ||
| 1023 | } | ||
| 1024 | $sth->finish() ; | ||
| 1025 | } | ||
| 1026 | if ( defined($bound = $sv->field("upperBound")) ) { | ||
| 1027 | $sth = $dbh->prepare( "update StudyVariable | ||
| 1028 | set dateUpperBound = ? | ||
| 1029 | where studyVariableID = ?" ) ; | ||
| 1030 | if ($bound eq "DELETE") { | ||
| 1031 | $sth->execute(undef, $id) ; | ||
| 1032 | } else { | ||
| 1033 | $sth->execute($bound, $id) ; | ||
| 1034 | } | ||
| 1035 | $sth->finish() ; | ||
| 1036 | } | ||
| 1037 | } | ||
| 1038 | if ($format eq "Code") { | ||
| 1039 | if ( defined($codesListPtr = $sv->field("Codes")) ) { | ||
| 1040 | $sth = $dbh->prepare(" select codeDerivationID from CodeDerivation | ||
| 1041 | where studyVariableID = $id" ) ; | ||
| 1042 | $sth->execute() ; | ||
| 1043 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 1044 | $dbh->do( "delete from StaticLCPenetrance | ||
| 1045 | where cdID = $$arrRef[0]" ) ; | ||
| 1046 | } | ||
| 1047 | $dbh->do( "delete from CodeDerivation | ||
| 1048 | where studyVariableID = $id" ) ; | ||
| 1049 | |||
| 1050 | $sth = $dbh->prepare( "insert into CodeDerivation | ||
| 1051 | (codeDerivationID, studyVariableID, code, description, formula) | ||
| 1052 | values (?, ?, ?, ?, ?)" ) ; | ||
| 1053 | if ($category eq "StaticLiabilityClass") { | ||
| 1054 | $sth1 = $dbh->prepare( "insert into StaticLCPenetrance | ||
| 1055 | (cdID, pen11, pen12, pen22, malePen1, malePen2) | ||
| 1056 | values (?, ?, ?, ?, ?, ?)" ) ; | ||
| 1057 | } | ||
| 1058 | foreach $codePtr (@$codesListPtr) { | ||
| 1059 | $sth->execute(undef, $id, $$codePtr{code}, $$codePtr{description}, undef) ; | ||
| 1060 | $cdID = $sth->{'mysql_insertid'} ; | ||
| 1061 | if ($category eq "StaticLiabilityClass") { | ||
| 1062 | $sth1->execute($cdID, $$codePtr{pen11}, $$codePtr{pen12}, $$codePtr{pen22}, $$codePtr{malePen1}, $$codePtr{malePen2}) ; | ||
| 1063 | } | ||
| 1064 | } | ||
| 1065 | $sth->finish() ; | ||
| 1066 | } | ||
| 1067 | } | ||
| 1068 | if ($category =~ /AffectionStatus$/) { | ||
| 1069 | ( $oldAsdID ) = $dbh->selectrow_array( "select asDefID from AffectionStatusDefinition | ||
| 1070 | where studyVariableID = $id" ) ; | ||
| 1071 | if ( defined($asdPtr = $sv->field("AffStatDef")) ) { | ||
| 1072 | $dbh->do( "delete from AffectionStatusDefinition | ||
| 1073 | where studyVariableID = $id" ) ; | ||
| 1074 | $sth = $dbh->prepare( "insert into AffectionStatusDefinition | ||
| 1075 | (asDefID, studyVariableID, name, diseaseAlleleFreq, | ||
| 1076 | pen11, pen12, pen22, malePen1, malePen2) | ||
| 1077 | values (?, ?, ?, ?, ?, ?, ?, ?, ?)" ) ; | ||
| 1078 | $sth->execute(undef, $id, $$asdPtr{name}, $$asdPtr{diseaseAlleleFreq}, $$asdPtr{pen11}, $$asdPtr{pen12}, $$asdPtr{pen22}, $$asdPtr{malePen1}, $$asdPtr{malePen2}) ; | ||
| 1079 | $asdID = $sth->{'mysql_insertid'} ; | ||
| 1080 | $sth->finish() ; | ||
| 1081 | if ( defined($aseListPtr = $$asdPtr{AffStatElements}) ) { | ||
| 1082 | if (defined $oldAsdID) { | ||
| 1083 | $dbh->do( "delete from AffectionStatusElement | ||
| 1084 | where asDefID = $oldAsdID" ) ; | ||
| 1085 | } | ||
| 1086 | $sth = $dbh->prepare( "insert into AffectionStatusElement | ||
| 1087 | (asElementID, asDefID, code, type, formula) | ||
| 1088 | values (?, ?, ?, ?, ?)" ) ; | ||
| 1089 | foreach $asePtr (@$aseListPtr) { | ||
| 1090 | $sth->execute(undef, $asdID, $$asePtr{code}, $$asePtr{type}, $$asePtr{formula}) ; | ||
| 1091 | } | ||
| 1092 | $sth->finish() ; | ||
| 1093 | } | ||
| 1094 | } | ||
| 1095 | if ( defined($lcDefPtr = $sv->field("LCDef")) ) { | ||
| 1096 | ( $oldLcdID ) = $dbh->selectrow_array( "select lcDefID from LiabilityClassDefinition | ||
| 1097 | where studyVariableID = $id" ) ; | ||
| 1098 | $dbh->do( "delete from LiabilityClassDefinition | ||
| 1099 | where studyVariableID = $id" ) ; | ||
| 1100 | $sth = $dbh->prepare( "insert into LiabilityClassDefinition | ||
| 1101 | (lcDefID, studyVariableID, name) | ||
| 1102 | values (?, ?, ?)" ) ; | ||
| 1103 | $sth->execute(undef, $id, $$lcDefPtr{name}) ; | ||
| 1104 | $lcdID = $sth->{'mysql_insertid'} ; | ||
| 1105 | $sth->finish() ; | ||
| 1106 | if ( defined($lcListPtr = $$lcDefPtr{LiabilityClasses}) ) { | ||
| 1107 | $dbh->do( "delete from LiabilityClass | ||
| 1108 | where lcDefID = $oldLcdID" ) ; | ||
| 1109 | $sth = $dbh->prepare( "insert into LiabilityClass | ||
| 1110 | (lcID, lcDefID, code, description, pen11, | ||
| 1111 | pen12, pen22, malePen1, malePen2, formula) | ||
| 1112 | values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ) ; | ||
| 1113 | foreach $lcPtr (@$lcListPtr) { | ||
| 1114 | $sth->execute(undef, $lcdID, $$lcPtr{code}, $$lcPtr{description}, $$lcPtr{pen11}, $$lcPtr{pen12}, $$lcPtr{pen22}, $$lcPtr{malePen1}, $$lcPtr{malePen2}, $$lcPtr{formula}) ; | ||
| 1115 | } | ||
| 1116 | $sth->finish() ; | ||
| 1117 | } | ||
| 1118 | } | ||
| 1119 | } | ||
| 1120 | |||
| 1121 | $DEBUG and carp " ->[updateStudyVariable] End." ; | ||
| 1122 | |||
| 1123 | return(1) ; | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | =head2 updatePhenotype | ||
| 1127 | |||
| 1128 | Function : Update a Genetics::Object::Phenotype object in the database. | ||
| 1129 | Argument : The Genetics::Object::Phenotype object to be updated. | ||
| 1130 | Returns : 1 on success, undef otherwise. | ||
| 1131 | Scope : Public | ||
| 1132 | Comments : | ||
| 1133 | |||
| 1134 | =cut | ||
| 1135 | |||
| 1136 | sub updatePhenotype { | ||
| 1137 | my($self, $pt) = @_ ; | ||
| 1138 | my($id, $actualType, $sth, $active, $date, $svFormat, $valueFieldName, $aaListPtr) ; | ||
| 1139 | my $dbh = $self->{dbh} ; | ||
| 1140 | |||
| 1141 | $DEBUG and carp " ->[updatePhenotype] $pt" ; | ||
| 1142 | |||
| 1143 | $id = $pt->field("id") ; | ||
| 1144 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 1145 | where id = $id") ; | ||
| 1146 | if ( $actualType ne "Phenotype") { | ||
| 1147 | carp " ->[updatePhenotype] Object with ID = $id is not a Phenotype!" ; | ||
| 1148 | return(undef) ; | ||
| 1149 | } | ||
| 1150 | # Object | ||
| 1151 | $self->_updateObjAssocData($pt) ; | ||
| 1152 | # Phenotype | ||
| 1153 | if ( defined($active = $pt->field("isActive")) ) { | ||
| 1154 | $sth = $dbh->prepare( "update Phenotype | ||
| 1155 | set isActive = ? | ||
| 1156 | where ptID = ?" ) ; | ||
| 1157 | $sth->execute($active, $id) ; | ||
| 1158 | $sth->finish() ; | ||
| 1159 | } | ||
| 1160 | if ( defined($date = $pt->field("dateCollected")) ) { | ||
| 1161 | $sth = $dbh->prepare( "update Phenotype | ||
| 1162 | set dateCollected = ? | ||
| 1163 | where ptID = ?" ) ; | ||
| 1164 | if ($date eq "DELETE") { | ||
| 1165 | $sth->execute(undef, $id) ; | ||
| 1166 | } else { | ||
| 1167 | $sth->execute($date, $id) ; | ||
| 1168 | } | ||
| 1169 | $sth->finish() ; | ||
| 1170 | } | ||
| 1171 | ( $svFormat ) = $dbh->selectrow_array( "select format from StudyVariable, Phenotype | ||
| 1172 | where ptID = $id | ||
| 1173 | and Phenotype.svID = StudyVariable.studyVariableID" ) ; | ||
| 1174 | $valueFieldName = lc($svFormat) . "Value" ; | ||
| 1175 | $sth = $dbh->prepare( "update Phenotype | ||
| 1176 | set $valueFieldName = ? | ||
| 1177 | where ptID = ?" ) ; | ||
| 1178 | $sth->execute($pt->field("value"), $id) ; | ||
| 1179 | $sth->finish() ; | ||
| 1180 | # Phenotype AssayAttributes | ||
| 1181 | if ( defined ($aaListPtr = $pt->field("AssayAttrs")) ) { | ||
| 1182 | $self->_updateAssayAttrs($aaListPtr, "Phenotype", $id) ; | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | $DEBUG and carp " ->[updatePhenotype] End." ; | ||
| 1186 | |||
| 1187 | return(1) ; | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | =head2 updateFrequencySource | ||
| 1191 | |||
| 1192 | Function : Update a Genetics::Object::FrequencySource object in the database. | ||
| 1193 | Argument : The Genetics::Object::FrequencySource object to be updated. | ||
| 1194 | Returns : 1 on success, undef otherwise. | ||
| 1195 | Scope : Public | ||
| 1196 | |||
| 1197 | =cut | ||
| 1198 | |||
| 1199 | sub updateFrequencySource { | ||
| 1200 | my($self, $fs) = @_ ; | ||
| 1201 | my($id, $actualType, $sth, $sthA, $sthOF, $sthFSOF, $listPtr, $arrRef, | ||
| 1202 | $oafPtr, $allelePtr, $poID, $alleleID, $obsFreqID, $ohfPtr, $htID) ; | ||
| 1203 | my $dbh = $self->{dbh} ; | ||
| 1204 | |||
| 1205 | $DEBUG and carp " ->[updateFrequencySource] $fs" ; | ||
| 1206 | |||
| 1207 | $id = $fs->field("id") ; | ||
| 1208 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 1209 | where id = $id") ; | ||
| 1210 | if ( $actualType ne "FrequencySource") { | ||
| 1211 | carp " ->[updateFrequencySource] Object with ID = $id is not a FrequencySource!" ; | ||
| 1212 | return(undef) ; | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | # Object | ||
| 1216 | $self->_updateObjAssocData($fs) ; | ||
| 1217 | # FrequencySource data | ||
| 1218 | $sthOF = $dbh->prepare( "insert into ObsFrequency | ||
| 1219 | (obsFreqID, type, alleleID, htID, frequency) | ||
| 1220 | values (?, ?, ?, ?, ?)" ) ; | ||
| 1221 | $sthFSOF = $dbh->prepare( "insert into FreqSourceObsFrequency | ||
| 1222 | (freqSourceID, obsFreqID) | ||
| 1223 | values (?, ?)" ) ; | ||
| 1224 | # Allele Freqs | ||
| 1225 | if ( defined ($listPtr = $fs->field("ObsAlleleFrequencies")) ) { | ||
| 1226 | if ( ref($listPtr) eq "ARRAY") { | ||
| 1227 | # Get rid of old allele freqs... | ||
| 1228 | $sth = $dbh->prepare( "select FreqSourceObsFrequency.obsFreqID | ||
| 1229 | from ObsFrequency, FreqSourceObsFrequency | ||
| 1230 | where freqSourceID = $id | ||
| 1231 | and ObsFrequency.obsFreqID = FreqSourceObsFrequency.obsFreqID | ||
| 1232 | and ObsFrequency.type = 'Allele'" ) ; | ||
| 1233 | $sth->execute() ; | ||
| 1234 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 1235 | $dbh->do( "delete from ObsFrequency | ||
| 1236 | where obsFreqID = $$arrRef[0]" ) ; | ||
| 1237 | $dbh->do( "delete from FreqSourceObsFrequency | ||
| 1238 | where obsFreqID = $$arrRef[0]" ) ; | ||
| 1239 | } | ||
| 1240 | # ...the add new ones | ||
| 1241 | $sthA = $dbh->prepare( "insert into Allele | ||
| 1242 | (alleleID, poID, name, type) | ||
| 1243 | values (?, ?, ?, ?)" ) ; | ||
| 1244 | foreach $oafPtr (@$listPtr) { | ||
| 1245 | # Figure out what Allele we're talking about. First find the Marker ID... | ||
| 1246 | $allelePtr = $$oafPtr{Allele} ; | ||
| 1247 | $poID = $allelePtr->{Marker}->{id} ; | ||
| 1248 | # ...then see if the Marker already has an Allele w/ the same name and type... | ||
| 1249 | ( $alleleID ) = $dbh->selectrow_array( "select alleleID from Allele | ||
| 1250 | where poID = '$poID' and | ||
| 1251 | name = '$$allelePtr{name}' and | ||
| 1252 | type = '$$allelePtr{type}'" ) ; | ||
| 1253 | if ( ! defined $alleleID) { | ||
| 1254 | # ...if not, create a new Allele | ||
| 1255 | $sthA->execute(undef, $poID, $$allelePtr{name}, $$allelePtr{type}) ; | ||
| 1256 | $alleleID = $sthA->{'mysql_insertid'} ; | ||
| 1257 | } | ||
| 1258 | # Create the ObsFrequency | ||
| 1259 | $sthOF->execute(undef, "Allele", $alleleID, undef, $$oafPtr{frequency}) ; | ||
| 1260 | $obsFreqID = $sthOF->{'mysql_insertid'} ; | ||
| 1261 | # Add row to FreqSourceObsFrequency | ||
| 1262 | $sthFSOF->execute($id, $obsFreqID) ; | ||
| 1263 | } | ||
| 1264 | } | ||
| 1265 | $sthA->finish() ; | ||
| 1266 | } elsif ( ! ref($listPtr) and ($listPtr eq "DELETE") ) { | ||
| 1267 | $sth = $dbh->prepare( "select FreqSourceObsFrequency.obsFreqID | ||
| 1268 | from ObsFrequency, FreqSourceObsFrequency | ||
| 1269 | where freqSourceID = $id | ||
| 1270 | and ObsFrequency.obsFreqID = FreqSourceObsFrequency.obsFreqID | ||
| 1271 | and ObsFrequency.type = 'Allele'" ) ; | ||
| 1272 | $sth->execute() ; | ||
| 1273 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 1274 | $dbh->do( "delete from ObsFrequency | ||
| 1275 | where obsFreqID = $$arrRef[0]" ) ; | ||
| 1276 | $dbh->do( "delete from FreqSourceObsFrequency | ||
| 1277 | where obsFreqID = $$arrRef[0]" ) ; | ||
| 1278 | } | ||
| 1279 | } else { | ||
| 1280 | carp " ->[_updateFrequencySource] Inappropriate ObsAlleleFrequencies value in $fs." ; | ||
| 1281 | } | ||
| 1282 | # Haplotype Freqs | ||
| 1283 | if ( defined ($listPtr = $fs->field("ObsHtFrequencies")) ) { | ||
| 1284 | if ( ref($listPtr) eq "ARRAY") { | ||
| 1285 | # Get rid of old ht freqs... | ||
| 1286 | $sth = $dbh->prepare( "select FreqSourceObsFrequency.obsFreqID | ||
| 1287 | from ObsFrequency, FreqSourceObsFrequency | ||
| 1288 | where freqSourceID = $id | ||
| 1289 | and ObsFrequency.obsFreqID = FreqSourceObsFrequency.obsFreqID | ||
| 1290 | and ObsFrequency.type = 'Ht'" ) ; | ||
| 1291 | $sth->execute() ; | ||
| 1292 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 1293 | $dbh->do( "delete from ObsFrequency | ||
| 1294 | where obsFreqID = $$arrRef[0]" ) ; | ||
| 1295 | $dbh->do( "delete from FreqSourceObsFrequency | ||
| 1296 | where obsFreqID = $$arrRef[0]" ) ; | ||
| 1297 | } | ||
| 1298 | # ...then add new ones | ||
| 1299 | foreach $ohfPtr (@$listPtr) { | ||
| 1300 | # Figure out what Haplotype we're talking about. | ||
| 1301 | $htID = $ohfPtr->{Haplotype}->{id} ; | ||
| 1302 | # Create the ObsFrequency | ||
| 1303 | $sthOF->execute(undef, "Ht", undef, $htID, $$ohfPtr{frequency}) ; | ||
| 1304 | $obsFreqID = $sthOF->{'mysql_insertid'} ; | ||
| 1305 | # Add row to FreqSourceObsFrequency | ||
| 1306 | $sthFSOF->execute($id, $obsFreqID) ; | ||
| 1307 | } | ||
| 1308 | } elsif ( ! ref($listPtr) and ($listPtr eq "DELETE") ) { | ||
| 1309 | $sth = $dbh->prepare( "select FreqSourceObsFrequency.obsFreqID | ||
| 1310 | from ObsFrequency, FreqSourceObsFrequency | ||
| 1311 | where freqSourceID = $id | ||
| 1312 | and ObsFrequency.obsFreqID = FreqSourceObsFrequency.obsFreqID | ||
| 1313 | and ObsFrequency.type = 'Ht'" ) ; | ||
| 1314 | $sth->execute() ; | ||
| 1315 | while ($arrRef = $sth->fetchrow_arrayref()) { | ||
| 1316 | $dbh->do( "delete from ObsFrequency | ||
| 1317 | where obsFreqID = $$arrRef[0]" ) ; | ||
| 1318 | $dbh->do( "delete from FreqSourceObsFrequency | ||
| 1319 | where obsFreqID = $$arrRef[0]" ) ; | ||
| 1320 | } | ||
| 1321 | } else { | ||
| 1322 | carp " ->[_updateFrequencySource] Inappropriate ObsHtFrequencies value in $fs." ; | ||
| 1323 | } | ||
| 1324 | } | ||
| 1325 | $sthOF->finish() ; | ||
| 1326 | $sthFSOF->finish() ; | ||
| 1327 | |||
| 1328 | $DEBUG and carp " ->[updateFrequencySource] End." ; | ||
| 1329 | |||
| 1330 | return(1) ; | ||
| 1331 | } | ||
| 1332 | |||
| 1333 | =head2 updateHtMarkerCollection | ||
| 1334 | |||
| 1335 | Function : Update a Genetics::Object::HtMarkerCollection object in the database. | ||
| 1336 | Argument : The Genetics::Object::HtMarkerCollection object to be updated. | ||
| 1337 | Returns : 1 on success, undef otherwise. | ||
| 1338 | Scope : Public | ||
| 1339 | |||
| 1340 | =cut | ||
| 1341 | |||
| 1342 | sub updateHtMarkerCollection { | ||
| 1343 | my($self, $hmc) = @_ ; | ||
| 1344 | my($id, $actualType, $sth, $units, $poListPtr, $sortOrder, $poPtr) ; | ||
| 1345 | my $dbh = $self->{dbh} ; | ||
| 1346 | |||
| 1347 | $DEBUG and carp " ->[updateHtMarkerCollection] $hmc" ; | ||
| 1348 | |||
| 1349 | $id = $hmc->field("id") ; | ||
| 1350 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 1351 | where id = $id") ; | ||
| 1352 | if ( $actualType ne "HtMarkerCollection") { | ||
| 1353 | carp " ->[updateHtMarkerCollection] Object with ID = $id is not a HtMarkerCollection!" ; | ||
| 1354 | return(undef) ; | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | # Object | ||
| 1358 | $self->_updateObjAssocData($hmc) ; | ||
| 1359 | # HtMarkerCollection data | ||
| 1360 | if ( defined($units = $hmc->field("distanceUnits")) ) { | ||
| 1361 | $sth = $dbh->prepare( "update HtMarkerCollection | ||
| 1362 | set distanceUnits = ? | ||
| 1363 | where hmcID = ?" ) ; | ||
| 1364 | $sth->execute($units, $id) ; | ||
| 1365 | $sth->finish() ; | ||
| 1366 | } | ||
| 1367 | if ( defined($poListPtr = $hmc->field("Markers")) ) { | ||
| 1368 | $dbh->do( "delete from HMCPolyObj | ||
| 1369 | where hmcID = $id" ) ; | ||
| 1370 | $sth = $dbh->prepare( "insert into HMCPolyObj | ||
| 1371 | (hmcID, poID, sortOrder, distance) | ||
| 1372 | values (?, ?, ?, ?)" ) ; | ||
| 1373 | $sortOrder = 1 ; | ||
| 1374 | foreach $poPtr (@$poListPtr) { | ||
| 1375 | $sth->execute($id, $$poPtr{id}, $sortOrder, $$poPtr{distToNext}) ; | ||
| 1376 | $sortOrder++ ; | ||
| 1377 | } | ||
| 1378 | $sth->finish() ; | ||
| 1379 | } | ||
| 1380 | |||
| 1381 | $DEBUG and carp " ->[updateHtMarkerCollection] End." ; | ||
| 1382 | |||
| 1383 | return(1) ; | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | =head2 updateHaplotype | ||
| 1387 | |||
| 1388 | Function : Update a Genetics::Object::Haplotype object in the database. | ||
| 1389 | Argument : The Genetics::Object::Haplotype object to be updated. | ||
| 1390 | Returns : 1 on success, undef otherwise. | ||
| 1391 | Scope : Public | ||
| 1392 | |||
| 1393 | =cut | ||
| 1394 | |||
| 1395 | sub updateHaplotype { | ||
| 1396 | my($self, $ht) = @_ ; | ||
| 1397 | my($id, $actualType, $sth, $sthA, $hmcPtr, $hmcID, $alleleListPtr, | ||
| 1398 | $sortOrder, $allelePtr, $poID, $alleleID) ; | ||
| 1399 | my $dbh = $self->{dbh} ; | ||
| 1400 | |||
| 1401 | $DEBUG and carp " ->[updateHaplotype] $ht" ; | ||
| 1402 | |||
| 1403 | $id = $ht->field("id") ; | ||
| 1404 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 1405 | where id = $id") ; | ||
| 1406 | if ( $actualType ne "Haplotype") { | ||
| 1407 | carp " ->[updateHaplotype] Object with ID = $id is not a Haplotype!" ; | ||
| 1408 | return(undef) ; | ||
| 1409 | } | ||
| 1410 | |||
| 1411 | # Object | ||
| 1412 | $self->_updateObjAssocData($ht) ; | ||
| 1413 | # Haplotype | ||
| 1414 | $hmcPtr = $ht->field("MarkerCollection") ; | ||
| 1415 | $hmcID = $$hmcPtr{id} ; | ||
| 1416 | $sth = $dbh->prepare( "update Haplotype | ||
| 1417 | set hmcID = ? | ||
| 1418 | where haplotypeID = ?" ) ; | ||
| 1419 | $sth->execute($hmcID, $id) ; | ||
| 1420 | $sth->finish() ; | ||
| 1421 | # HaplotypeAllele | ||
| 1422 | $dbh->do( "delete from HaplotypeAllele | ||
| 1423 | where haplotypeID = $id" ) ; | ||
| 1424 | $alleleListPtr = $ht->field("Alleles") ; | ||
| 1425 | $sth = $dbh->prepare( "insert into HaplotypeAllele | ||
| 1426 | (haplotypeID, alleleID, sortOrder) | ||
| 1427 | values (?, ?, ?)" ) ; | ||
| 1428 | $sthA = $dbh->prepare( "insert into Allele | ||
| 1429 | (alleleID, poID, name, type) | ||
| 1430 | values (?, ?, ?, ?)" ) ; | ||
| 1431 | $sortOrder = 1 ; | ||
| 1432 | foreach $allelePtr (@$alleleListPtr) { | ||
| 1433 | # First find the Marker ID... | ||
| 1434 | ( $poID ) = $dbh->selectrow_array( "select poID from HMCPolyObj | ||
| 1435 | where hmcID = '$hmcID' and | ||
| 1436 | sortOrder = '$sortOrder'") ; | ||
| 1437 | # ...then see if the Marker already has an Allele w/ the same name and type... | ||
| 1438 | ( $alleleID ) = $dbh->selectrow_array( "select alleleID from Allele | ||
| 1439 | where poID = '$poID' and | ||
| 1440 | name = '$$allelePtr{name}' and | ||
| 1441 | type = '$$allelePtr{type}'" ) ; | ||
| 1442 | if ( ! defined $alleleID) { | ||
| 1443 | # ...if not, create a new Allele | ||
| 1444 | $sthA->execute(undef, $poID, $$allelePtr{name}, $$allelePtr{type}) ; | ||
| 1445 | $alleleID = $sthA->{'mysql_insertid'} ; | ||
| 1446 | } | ||
| 1447 | $sth->execute($id, $alleleID, $sortOrder) ; | ||
| 1448 | $sortOrder++ ; | ||
| 1449 | } | ||
| 1450 | $sth->finish() ; | ||
| 1451 | $sthA->finish() ; | ||
| 1452 | |||
| 1453 | $DEBUG and carp " ->[updateHaplotype] End." ; | ||
| 1454 | |||
| 1455 | return(1) ; | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | =head2 updateDNASample | ||
| 1459 | |||
| 1460 | Function : Update a Genetics::Object::DNASample object in the database. | ||
| 1461 | Argument : The Genetics::Object::DNASample object to be updated. | ||
| 1462 | Returns : 1 on success, undef otherwise. | ||
| 1463 | Scope : Public | ||
| 1464 | |||
| 1465 | =cut | ||
| 1466 | |||
| 1467 | sub updateDNASample { | ||
| 1468 | my($self, $sample) = @_ ; | ||
| 1469 | my($id, $actualType, $sth, $date, $amt, $units, $conc, $subjPtr, | ||
| 1470 | $gtListPtr, $gtPtr) ; | ||
| 1471 | my $dbh = $self->{dbh} ; | ||
| 1472 | |||
| 1473 | $DEBUG and carp " ->[updateDNASample] $sample" ; | ||
| 1474 | |||
| 1475 | $id = $sample->field("id") ; | ||
| 1476 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 1477 | where id = $id") ; | ||
| 1478 | if ( $actualType ne "DNASample") { | ||
| 1479 | carp " ->[updateDNASample] Object with ID = $id is not a DNASample!" ; | ||
| 1480 | return(undef) ; | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | # Object | ||
| 1484 | $self->_updateObjAssocData($sample) ; | ||
| 1485 | |||
| 1486 | # Sample | ||
| 1487 | if ( defined($date = $sample->field("dateCollected")) ) { | ||
| 1488 | $sth = $dbh->prepare( "update Sample | ||
| 1489 | set dateCollected = ? | ||
| 1490 | where sampleID = ?" ) ; | ||
| 1491 | if ($date eq "DELETE") { | ||
| 1492 | $sth->execute(undef, $id) ; | ||
| 1493 | } else { | ||
| 1494 | $sth->execute($date, $id) ; | ||
| 1495 | } | ||
| 1496 | $sth->finish() ; | ||
| 1497 | } | ||
| 1498 | # DNASample | ||
| 1499 | if ( defined($amt = $sample->field("amount")) ) { | ||
| 1500 | $sth = $dbh->prepare( "update DNASample | ||
| 1501 | set amount = ? | ||
| 1502 | where dnaSampleID = ?" ) ; | ||
| 1503 | if ($amt eq "DELETE") { | ||
| 1504 | $sth->execute(undef, $id) ; | ||
| 1505 | } else { | ||
| 1506 | $sth->execute($amt, $id) ; | ||
| 1507 | } | ||
| 1508 | $sth->finish() ; | ||
| 1509 | } | ||
| 1510 | if ( defined($units = $sample->field("amountUnits")) ) { | ||
| 1511 | $sth = $dbh->prepare( "update DNASample | ||
| 1512 | set amountUnits = ? | ||
| 1513 | where dnaSampleID = ?" ) ; | ||
| 1514 | if ($units eq "DELETE") { | ||
| 1515 | $sth->execute(undef, $id) ; | ||
| 1516 | } else { | ||
| 1517 | $sth->execute($units, $id) ; | ||
| 1518 | } | ||
| 1519 | $sth->finish() ; | ||
| 1520 | } | ||
| 1521 | if ( defined($conc = $sample->field("concentration")) ) { | ||
| 1522 | $sth = $dbh->prepare( "update DNASample | ||
| 1523 | set concentration = ? | ||
| 1524 | where dnaSampleID = ?" ) ; | ||
| 1525 | if ($conc eq "DELETE") { | ||
| 1526 | $sth->execute(undef, $id) ; | ||
| 1527 | } else { | ||
| 1528 | $sth->execute($conc, $id) ; | ||
| 1529 | } | ||
| 1530 | $sth->finish() ; | ||
| 1531 | } | ||
| 1532 | if ( defined($units = $sample->field("concUnits")) ) { | ||
| 1533 | $sth = $dbh->prepare( "update DNASample | ||
| 1534 | set concUnits = ? | ||
| 1535 | where dnaSampleID = ?" ) ; | ||
| 1536 | if ($units eq "DELETE") { | ||
| 1537 | $sth->execute(undef, $id) ; | ||
| 1538 | } else { | ||
| 1539 | $sth->execute($units, $id) ; | ||
| 1540 | } | ||
| 1541 | $sth->finish() ; | ||
| 1542 | } | ||
| 1543 | # SubjectSample | ||
| 1544 | if ( defined($subjPtr = $sample->field("Subject")) ) { | ||
| 1545 | $sth = $dbh->prepare( "insert into SubjectSample | ||
| 1546 | (subjectID, sampleID) | ||
| 1547 | values (?, ?)" ) ; | ||
| 1548 | if ( ref($subjPtr) eq "HASH" ) { | ||
| 1549 | $dbh->do( "delete from SubjectSample | ||
| 1550 | where sampleID = $id" ) ; | ||
| 1551 | $sth->execute($$subjPtr{id}, $id) ; | ||
| 1552 | } elsif ( ! ref($subjPtr) and ($subjPtr eq "DELETE") ) { | ||
| 1553 | $dbh->do( "delete from SubjectSample | ||
| 1554 | where sampleID = $id" ) ; | ||
| 1555 | $sth->execute(undef, $id) ; | ||
| 1556 | } else { | ||
| 1557 | carp " ->[updateDNASample] Inappropriate Subject value in $sample." ; | ||
| 1558 | } | ||
| 1559 | $sth->finish() ; | ||
| 1560 | } | ||
| 1561 | # SampleGenotype | ||
| 1562 | if ( defined($gtListPtr = $sample->field("Genotypes")) ) { | ||
| 1563 | $sth = $dbh->prepare( "insert into SampleGenotype | ||
| 1564 | (sampleID, gtID) | ||
| 1565 | values (?, ?)" ) ; | ||
| 1566 | if ( ref($gtListPtr) eq "ARRAY" ) { | ||
| 1567 | $dbh->do( "delete from SampleGenotype | ||
| 1568 | where sampleID = $id" ) ; | ||
| 1569 | foreach $gtPtr (@$gtListPtr) { | ||
| 1570 | $sth->execute($id, $$gtPtr{id}) ; | ||
| 1571 | } | ||
| 1572 | } elsif ( ! ref($gtListPtr) and ($gtListPtr eq "DELETE") ) { | ||
| 1573 | $dbh->do( "delete from SampleGenotype | ||
| 1574 | where sampleID = $id" ) ; | ||
| 1575 | } else { | ||
| 1576 | carp " ->[updateDNASample] Inappropriate Genotypes value in $sample." ; | ||
| 1577 | } | ||
| 1578 | $sth->finish() ; | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | $DEBUG and carp " ->[updateDNASample] End." ; | ||
| 1582 | |||
| 1583 | return(1) ; | ||
| 1584 | } | ||
| 1585 | |||
| 1586 | =head2 updateTissueSample | ||
| 1587 | |||
| 1588 | Function : Update a Genetics::Object::TissueSample object in the database. | ||
| 1589 | Argument : The Genetics::Object::TissueSample object to be updated. | ||
| 1590 | Returns : 1 on success, undef otherwise. | ||
| 1591 | Scope : Public | ||
| 1592 | |||
| 1593 | =cut | ||
| 1594 | |||
| 1595 | sub updateTissueSample { | ||
| 1596 | my($self, $sample) = @_ ; | ||
| 1597 | my($id, $actualType, $sth, $date, $tissue, $amt, $units, $subjPtr, | ||
| 1598 | $dsListPtr, $dsPtr) ; | ||
| 1599 | my $dbh = $self->{dbh} ; | ||
| 1600 | |||
| 1601 | $DEBUG and carp " ->[updateTissueSample] $sample" ; | ||
| 1602 | |||
| 1603 | $id = $sample->field("id") ; | ||
| 1604 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 1605 | where id = $id") ; | ||
| 1606 | if ( $actualType ne "TissueSample") { | ||
| 1607 | carp " ->[updateTissueSample] Object with ID = $id is not a TissueSample!" ; | ||
| 1608 | return(undef) ; | ||
| 1609 | } | ||
| 1610 | |||
| 1611 | # Object | ||
| 1612 | $self->_updateObjAssocData($sample) ; | ||
| 1613 | |||
| 1614 | # Sample | ||
| 1615 | if ( defined($date = $sample->field("dateCollected")) ) { | ||
| 1616 | $sth = $dbh->prepare( "update Sample | ||
| 1617 | set dateCollected = ? | ||
| 1618 | where sampleID = ?" ) ; | ||
| 1619 | if ($date eq "DELETE") { | ||
| 1620 | $sth->execute(undef, $id) ; | ||
| 1621 | } else { | ||
| 1622 | $sth->execute($date, $id) ; | ||
| 1623 | } | ||
| 1624 | $sth->finish() ; | ||
| 1625 | } | ||
| 1626 | # TissueSample | ||
| 1627 | $tissue = $sample->field("tissue") ; | ||
| 1628 | $dbh->do( "update TissueSample | ||
| 1629 | set tissue = '$tissue' | ||
| 1630 | where tissueSampleID = $id" ) ; | ||
| 1631 | if ( defined($amt = $sample->field("amount")) ) { | ||
| 1632 | $sth = $dbh->prepare( "update TissueSample | ||
| 1633 | set amount = ? | ||
| 1634 | where tissueSampleID = ?" ) ; | ||
| 1635 | if ($amt eq "DELETE") { | ||
| 1636 | $sth->execute(undef, $id) ; | ||
| 1637 | } else { | ||
| 1638 | $sth->execute($amt, $id) ; | ||
| 1639 | } | ||
| 1640 | $sth->finish() ; | ||
| 1641 | } | ||
| 1642 | if ( defined($units = $sample->field("amountUnits")) ) { | ||
| 1643 | $sth = $dbh->prepare( "update TissueSample | ||
| 1644 | set amountUnits = ? | ||
| 1645 | where tissueSampleID = ?" ) ; | ||
| 1646 | if ($units eq "DELETE") { | ||
| 1647 | $sth->execute(undef, $id) ; | ||
| 1648 | } else { | ||
| 1649 | $sth->execute($units, $id) ; | ||
| 1650 | } | ||
| 1651 | $sth->finish() ; | ||
| 1652 | } | ||
| 1653 | # SubjectSample | ||
| 1654 | if ( defined($subjPtr = $sample->field("Subject")) ) { | ||
| 1655 | $sth = $dbh->prepare( "insert into SubjectSample | ||
| 1656 | (subjectID, sampleID) | ||
| 1657 | values (?, ?)" ) ; | ||
| 1658 | if ( ref($subjPtr) eq "HASH" ) { | ||
| 1659 | $dbh->do( "delete from SubjectSample | ||
| 1660 | where sampleID = $id" ) ; | ||
| 1661 | $sth->execute($$subjPtr{id}, $id) ; | ||
| 1662 | } elsif ( ! ref($subjPtr) and ($subjPtr eq "DELETE") ) { | ||
| 1663 | $dbh->do( "delete from SubjectSample | ||
| 1664 | where sampleID = $id" ) ; | ||
| 1665 | $sth->execute(undef, $id) ; | ||
| 1666 | } else { | ||
| 1667 | carp " ->[updateTissueSample] Inappropriate Subject value in $sample." ; | ||
| 1668 | } | ||
| 1669 | $sth->finish() ; | ||
| 1670 | } | ||
| 1671 | # TissueDNASample | ||
| 1672 | if ( defined($dsListPtr = $sample->field("DNASamples")) ) { | ||
| 1673 | $sth = $dbh->prepare( "insert into TissueDNASample | ||
| 1674 | (tissueSampleID, dnaSampleID) | ||
| 1675 | values (?, ?)" ) ; | ||
| 1676 | if ( ref($dsListPtr) eq "ARRAY" ) { | ||
| 1677 | $dbh->do( "delete from TissueDNASample | ||
| 1678 | where tissueSampleID = $id" ) ; | ||
| 1679 | foreach $dsPtr (@$dsListPtr) { | ||
| 1680 | $sth->execute($id, $$dsPtr{id}) ; | ||
| 1681 | } | ||
| 1682 | } elsif ( ! ref($dsListPtr) and ($dsListPtr eq "DELETE") ) { | ||
| 1683 | $dbh->do( "delete from TissueDNASample | ||
| 1684 | where tissueSampleID = $id" ) ; | ||
| 1685 | } else { | ||
| 1686 | carp " ->[updateTissueSample] Inappropriate DNASamples value in $sample." ; | ||
| 1687 | } | ||
| 1688 | $sth->finish() ; | ||
| 1689 | } | ||
| 1690 | |||
| 1691 | $DEBUG and carp " ->[updateTissueSample] End." ; | ||
| 1692 | |||
| 1693 | return(1) ; | ||
| 1694 | } | ||
| 1695 | |||
| 1696 | =head2 updateMap | ||
| 1697 | |||
| 1698 | Function : Update a Genetics::Object::Map object in the database. | ||
| 1699 | Argument : The Genetics::Object::Map object to be updated. | ||
| 1700 | Returns : 1 on success, undef otherwise. | ||
| 1701 | Scope : Public | ||
| 1702 | |||
| 1703 | =cut | ||
| 1704 | |||
| 1705 | sub updateMap { | ||
| 1706 | my($self, $map) = @_ ; | ||
| 1707 | my($id, $actualType, $sth, $method, $units, $chr, $orgPtr, $orgID, $sortOrder, | ||
| 1708 | $omeListPtr, $omePtr, $soPtr, $soID, $omeName) ; | ||
| 1709 | my $dbh = $self->{dbh} ; | ||
| 1710 | |||
| 1711 | $DEBUG and carp " ->[updateMap] $map" ; | ||
| 1712 | |||
| 1713 | $id = $map->field("id") ; | ||
| 1714 | ( $actualType ) = $dbh->selectrow_array("select objType from Object | ||
| 1715 | where id = $id") ; | ||
| 1716 | if ( $actualType ne "Map") { | ||
| 1717 | carp " ->[updateMap] Object with ID = $id is not a Map!" ; | ||
| 1718 | return(undef) ; | ||
| 1719 | } | ||
| 1720 | |||
| 1721 | # Object | ||
| 1722 | $self->_updateObjAssocData($map) ; | ||
| 1723 | # Map data | ||
| 1724 | $method = $map->field("orderingMethod") ; | ||
| 1725 | $units = $map->field("distanceUnits") ; | ||
| 1726 | $dbh->do( "update Map | ||
| 1727 | set orderingMethod = '$method', | ||
| 1728 | distanceUnits = '$units' | ||
| 1729 | where mapID = $id" ) ; | ||
| 1730 | if ( defined($chr = $map->field("chromosome")) ) { | ||
| 1731 | $sth = $dbh->prepare( "update Map | ||
| 1732 | set chromosome = ? | ||
| 1733 | where mapID = ?" ) ; | ||
| 1734 | if ($chr eq "DELETE") { | ||
| 1735 | $sth->execute(undef, $id) ; | ||
| 1736 | } else { | ||
| 1737 | $sth->execute($chr, $id) ; | ||
| 1738 | } | ||
| 1739 | $sth->finish() ; | ||
| 1740 | } | ||
| 1741 | if ( defined($orgPtr = $map->field("Organism")) ) { | ||
| 1742 | $sth = $dbh->prepare( "update Map | ||
| 1743 | set organismID = ? | ||
| 1744 | where mapID = ?" ) ; | ||
| 1745 | if ( ref($orgPtr) eq "HASH" ) { | ||
| 1746 | $orgID = $self->_getOrganismID($orgPtr) ; | ||
| 1747 | $sth->execute($orgID, $id) ; | ||
| 1748 | } elsif ( ! ref($orgPtr) and ($orgPtr eq "DELETE") ) { | ||
| 1749 | $sth->execute(undef, $id) ; | ||
| 1750 | } else { | ||
| 1751 | carp " ->[_updateMap] Inappropriate Organism value in $map." ; | ||
| 1752 | } | ||
| 1753 | $sth->finish() ; | ||
| 1754 | } | ||
| 1755 | # OME | ||
| 1756 | $sortOrder = 1 ; | ||
| 1757 | $omeListPtr = $map->field("OrderedMapElements") ; | ||
| 1758 | $dbh->do( "delete from OrderedMapElement | ||
| 1759 | where mapID = $id" ) ; | ||
| 1760 | $sth = $dbh->prepare( "insert into OrderedMapElement | ||
| 1761 | (omeID, mapID, soID, sortOrder, | ||
| 1762 | name, distance, comment) | ||
| 1763 | values (?, ?, ?, ?, ?, ?, ?)" ) ; | ||
| 1764 | foreach $omePtr (@$omeListPtr) { | ||
| 1765 | $soPtr = $$omePtr{SeqObj} ; | ||
| 1766 | $soID = $$soPtr{id} ; | ||
| 1767 | if ( ! defined ($omeName = $$soPtr{name}) ) { | ||
| 1768 | ( $omeName ) = $dbh->selectrow_array( "select name from Object | ||
| 1769 | where id = $soID" ) | ||
| 1770 | } | ||
| 1771 | $sth->execute(undef, $id, $soID, $sortOrder, $omeName, $$omePtr{distance}, $$omePtr{comment}) ; | ||
| 1772 | $sortOrder++ ; | ||
| 1773 | } | ||
| 1774 | $sth->finish() ; | ||
| 1775 | |||
| 1776 | $DEBUG and carp " ->[updateMap] End." ; | ||
| 1777 | |||
| 1778 | return(1) ; | ||
| 1779 | } | ||
| 1780 | |||
| 1781 | =head1 Private methods | ||
| 1782 | |||
| 1783 | =head2 _updateObjAssocData | ||
| 1784 | |||
| 1785 | Function : Update data in, and associated with, the Object table/object. | ||
| 1786 | Argument : The Genetics::Object object to be updated. | ||
| 1787 | Returns : 1 on success, undef otherwise. | ||
| 1788 | Scope : Private | ||
| 1789 | Called by : The various updateObjectSubClass methods. | ||
| 1790 | Comments : The following Object fields cannot be modified: id, objType, | ||
| 1791 | dateModified. | ||
| 1792 | |||
| 1793 | =cut | ||
| 1794 | |||
| 1795 | sub _updateObjAssocData { | ||
| 1796 | my($self, $obj) = @_ ; | ||
| 1797 | my($sth, $sth1, $id, $name, $date, $comment, $url, $naListPtr, $naPtr, | ||
| 1798 | $contactName, $contactID, $newContactID, $contactPtr, $xRefListPtr, $xRefPtr, | ||
| 1799 | $kwListPtr, $kwvPtr, $kwtName, $dataType, $descr, $value, $valueFieldName, | ||
| 1800 | $kwtID, $newKwtID) ; | ||
| 1801 | my $dbh = $self->{dbh} ; | ||
| 1802 | |||
| 1803 | $DEBUG and carp " ->[_updateObjAssocData] $obj." ; | ||
| 1804 | |||
| 1805 | $id = $obj->field("id") ; | ||
| 1806 | # Object fields | ||
| 1807 | if ( defined($name = $obj->field("name")) ) { | ||
| 1808 | $sth = $dbh->prepare( "update Object | ||
| 1809 | set name = ? | ||
| 1810 | where id = ?" ) ; | ||
| 1811 | $sth->execute($name, $id) ; | ||
| 1812 | $sth->finish() ; | ||
| 1813 | } | ||
| 1814 | if ( defined($date = $obj->field("dateCreated")) ) { | ||
| 1815 | $sth = $dbh->prepare( "update Object | ||
| 1816 | set dateCreated = ? | ||
| 1817 | where id = ?" ) ; | ||
| 1818 | $sth->execute($date, $id) ; | ||
| 1819 | $sth->finish() ; | ||
| 1820 | } | ||
| 1821 | if ( defined($comment = $obj->field("comment")) ) { | ||
| 1822 | $sth = $dbh->prepare( "update Object | ||
| 1823 | set comment = ? | ||
| 1824 | where id = ?" ) ; | ||
| 1825 | if ($comment eq "DELETE") { | ||
| 1826 | $sth->execute(undef, $id) ; | ||
| 1827 | } else { | ||
| 1828 | $sth->execute($comment, $id) ; | ||
| 1829 | } | ||
| 1830 | $sth->finish() ; | ||
| 1831 | } | ||
| 1832 | if ( defined($url = $obj->field("url")) ) { | ||
| 1833 | $sth = $dbh->prepare( "update Object | ||
| 1834 | set url = ? | ||
| 1835 | where id = ?" ) ; | ||
| 1836 | if ($url eq "DELETE") { | ||
| 1837 | $sth->execute(undef, $id) ; | ||
| 1838 | } else { | ||
| 1839 | $sth->execute($url, $id) ; | ||
| 1840 | } | ||
| 1841 | $sth->finish() ; | ||
| 1842 | } | ||
| 1843 | # NameAlias | ||
| 1844 | if ( defined ($naListPtr = $obj->field("NameAliases")) ) { | ||
| 1845 | if ( ref($naListPtr) eq "ARRAY" ) { | ||
| 1846 | $dbh->do( "delete from NameAlias | ||
| 1847 | where objID = $id" ) ; | ||
| 1848 | $sth = $dbh->prepare( "insert into NameAlias | ||
| 1849 | (objID, name, contactID) | ||
| 1850 | values (?, ?, ?)" ) ; | ||
| 1851 | $sth1 = $dbh->prepare( "insert into Contact | ||
| 1852 | (contactID, name) | ||
| 1853 | values (?, ?)" ) ; | ||
| 1854 | foreach $naPtr (@$naListPtr) { | ||
| 1855 | if ( defined ($contactName = $$naPtr{contactName}) ) { | ||
| 1856 | # See if there is already a Contact with the same name... | ||
| 1857 | ( $contactID ) = $dbh->selectrow_array( "select contactID from Contact | ||
| 1858 | where name = '$contactName'" ) ; | ||
| 1859 | if ( defined ($contactID) ) { | ||
| 1860 | # ...if so, insert a NamesAlias referencing the existing Contact | ||
| 1861 | $sth->execute($id, $$naPtr{name}, $contactID) ; | ||
| 1862 | } else { | ||
| 1863 | # ...if not, create a new Contact... | ||
| 1864 | $sth1->execute(undef, $contactName) ; | ||
| 1865 | $newContactID = $sth1->{'mysql_insertid'} ; | ||
| 1866 | # ...then insert NameAlias referencing the new Contact | ||
| 1867 | $sth->execute($id, $$naPtr{name}, $newContactID) ; | ||
| 1868 | } | ||
| 1869 | } else { | ||
| 1870 | # Insert a NameAlias w/o a Contact reference | ||
| 1871 | $sth->execute($id, $$naPtr{name}, undef) ; | ||
| 1872 | } | ||
| 1873 | } | ||
| 1874 | $sth->finish() ; | ||
| 1875 | $sth1->finish() ; | ||
| 1876 | } elsif ( ! ref($naListPtr) and ($naListPtr eq "DELETE") ) { | ||
| 1877 | $dbh->do( "delete from NameAlias | ||
| 1878 | where objID = $id" ) ; | ||
| 1879 | } else { | ||
| 1880 | carp " ->[_updateObjAssocData] Inappropriate NameAliases value in $obj." ; | ||
| 1881 | } | ||
| 1882 | } | ||
| 1883 | # Contact | ||
| 1884 | if ( defined ($contactPtr = $obj->field("Contact")) ) { | ||
| 1885 | if ( ref($contactPtr) eq "HASH" ) { | ||
| 1886 | # Don't delete Contacts, just delete references from Object to Contact | ||
| 1887 | # Need to check for an Address and deal with it if there is one | ||
| 1888 | $contactName = $$contactPtr{name} ; | ||
| 1889 | # See if there is already a Contact with the same name | ||
| 1890 | ( $contactID ) = $dbh->selectrow_array( "select contactID from Contact | ||
| 1891 | where name = '$contactName'" ) ; | ||
| 1892 | if ( defined ($contactID) ) { | ||
| 1893 | # If so, just add the contactID to Object | ||
| 1894 | $sth = $dbh->prepare( "update Object | ||
| 1895 | set contactID = ? | ||
| 1896 | where id = ? " ) ; | ||
| 1897 | $sth->execute($contactID, $id) ; | ||
| 1898 | $sth->finish() ; | ||
| 1899 | } else { | ||
| 1900 | # If not, create a new Contact... | ||
| 1901 | $sth = $dbh->prepare( "insert into Contact | ||
| 1902 | (contactID, addressID, name, organization, comment) | ||
| 1903 | values (?, ?, ?, ?, ?)" ) ; | ||
| 1904 | $sth->execute(undef, undef, $contactName, $$contactPtr{organization}, $$contactPtr{comment}) ; | ||
| 1905 | $newContactID = $sth->{'mysql_insertid'} ; | ||
| 1906 | $sth->finish() ; | ||
| 1907 | # then add the contactID to Object | ||
| 1908 | $sth = $dbh->prepare( "update Object | ||
| 1909 | set contactID = ? | ||
| 1910 | where id = ? " ) ; | ||
| 1911 | $sth->execute($newContactID, $id) ; | ||
| 1912 | $sth->finish() ; | ||
| 1913 | } | ||
| 1914 | } elsif ( ! ref($contactPtr) and ($contactPtr eq "DELETE")) { | ||
| 1915 | $sth = $dbh->prepare( "update Object | ||
| 1916 | set contactID = ? | ||
| 1917 | where id = ? " ) ; | ||
| 1918 | $sth->execute(undef, $id) ; | ||
| 1919 | $sth->finish() ; | ||
| 1920 | } else { | ||
| 1921 | carp " ->[_updateObjAssocData] Inappropriate Contact value in $obj." ; | ||
| 1922 | } | ||
| 1923 | } | ||
| 1924 | # DBXReferences | ||
| 1925 | if ( defined ($xRefListPtr = $obj->field("DBXReferences")) ) { | ||
| 1926 | if ( ref($xRefListPtr) eq "ARRAY" ) { | ||
| 1927 | $dbh->do( "delete from DBXReference | ||
| 1928 | where objID = $id" ) ; | ||
| 1929 | $sth = $dbh->prepare( "insert into DBXReference | ||
| 1930 | (dbXRefID, objID, accessionNumber, databaseName, | ||
| 1931 | schemaName, comment) | ||
| 1932 | values (?, ?, ?, ?, ?, ?)" ) ; | ||
| 1933 | foreach $xRefPtr (@$xRefListPtr) { | ||
| 1934 | $sth->execute(undef, $id, $$xRefPtr{accessionNumber}, $$xRefPtr{databaseName}, $$xRefPtr{schemaName}, $$xRefPtr{comment}) ; | ||
| 1935 | } | ||
| 1936 | $sth->finish() ; | ||
| 1937 | } elsif ( ! ref($xRefListPtr) and ($xRefListPtr eq "DELETE") ) { | ||
| 1938 | $dbh->do( "delete from DBXReference | ||
| 1939 | where objID = $id" ) ; | ||
| 1940 | } else { | ||
| 1941 | carp " ->[_updateObjAssocData] Inappropriate DBXReferences value in $obj." ; | ||
| 1942 | } | ||
| 1943 | } | ||
| 1944 | # Keywords | ||
| 1945 | if ( defined ($kwListPtr = $obj->field("Keywords")) ) { | ||
| 1946 | if ( ref($kwListPtr) eq "ARRAY" ) { | ||
| 1947 | $dbh->do( "delete from Keyword | ||
| 1948 | where objID = $id" ) ; | ||
| 1949 | foreach $kwvPtr (@$kwListPtr) { | ||
| 1950 | $kwtName = $$kwvPtr{name} ; | ||
| 1951 | $dataType = $$kwvPtr{dataType} ; | ||
| 1952 | $descr = $$kwvPtr{description} ; | ||
| 1953 | $value = $$kwvPtr{value} ; | ||
| 1954 | # Need to do this b/c there is only 1 value but there are 4 possible | ||
| 1955 | # columns in which to put it: | ||
| 1956 | $valueFieldName = (lc $dataType) . "Value" ; | ||
| 1957 | ( $kwtID ) = $dbh->selectrow_array( "select keywordTypeID from KeywordType | ||
| 1958 | where name = '$kwtName' | ||
| 1959 | and dataType = '$dataType'" ) ; | ||
| 1960 | if (defined $kwtID) { | ||
| 1961 | # Just add the value to Keyword | ||
| 1962 | $sth = $dbh->prepare( "insert into Keyword | ||
| 1963 | (keywordID, objID, keywordTypeID, $valueFieldName) | ||
| 1964 | values (?, ?, ?, ?) " ) ; | ||
| 1965 | $sth->execute(undef, $id, $kwtID, $value) ; | ||
| 1966 | $sth->finish() ; | ||
| 1967 | } else { | ||
| 1968 | # Create new KeywordType... | ||
| 1969 | $sth = $dbh->prepare( "insert into KeywordType | ||
| 1970 | (keywordTypeID, name, dataType, description) | ||
| 1971 | values (?, ?, ?, ?)" ) ; | ||
| 1972 | $sth->execute(undef, $kwtName, $dataType, $descr) ; | ||
| 1973 | $newKwtID = $sth->{'mysql_insertid'} ; | ||
| 1974 | $sth->finish() ; | ||
| 1975 | # then add the value to Keyword, referencing the new KeywordType | ||
| 1976 | $sth = $dbh->prepare( "insert into Keyword | ||
| 1977 | (keywordID, objID, keywordTypeID, $valueFieldName) | ||
| 1978 | values (?, ?, ?, ?) " ) ; | ||
| 1979 | $sth->execute(undef, $id, $newKwtID, $value) ; | ||
| 1980 | $sth->finish() ; | ||
| 1981 | } | ||
| 1982 | } | ||
| 1983 | } elsif ( ! ref($kwListPtr) and ($kwListPtr eq "DELETE") ) { | ||
| 1984 | $dbh->do( "delete from Keyword | ||
| 1985 | where objID = $id" ) ; | ||
| 1986 | } else { | ||
| 1987 | carp " ->[_updateObjAssocData] Inappropriate Keywords value in $obj." ; | ||
| 1988 | } | ||
| 1989 | } | ||
| 1990 | |||
| 1991 | $DEBUG and carp " ->[_updateObjAssocData] End." ; | ||
| 1992 | |||
| 1993 | return(1) ; | ||
| 1994 | } | ||
| 1995 | |||
| 1996 | =head2 _updateSubjectKindredRefs | ||
| 1997 | |||
| 1998 | Function : Updates references between Subjects and Kindreds. | ||
| 1999 | Argument : A Subject ID and a Kindred ID (can be undef). | ||
| 2000 | Returns : N/A | ||
| 2001 | Scope : Private | ||
| 2002 | Called By : updateSubject() when Subject.kindredID is modified. | ||
| 2003 | Comments : This method updates the KindredSubject table based on | ||
| 2004 | a change to a Subject.kindredID field. | ||
| 2005 | |||
| 2006 | =cut | ||
| 2007 | |||
| 2008 | sub _updateSubjectKindredRefs { | ||
| 2009 | my($self, $subjID, $kindredID) = @_ ; | ||
| 2010 | my $dbh = $self->{dbh} ; | ||
| 2011 | |||
| 2012 | $DEBUG and carp " ->[_updateSubjectKindredRefs]" ; | ||
| 2013 | |||
| 2014 | $dbh->do( "delete from KindredSubject | ||
| 2015 | where subjectID = $subjID" ) ; | ||
| 2016 | |||
| 2017 | $dbh->do( "insert into KindredSubject | ||
| 2018 | (kindredID, subjectID) | ||
| 2019 | values ($kindredID, $subjID)" ) ; | ||
| 2020 | |||
| 2021 | return(1) ; | ||
| 2022 | } | ||
| 2023 | |||
| 2024 | =head2 _updateKindredSubjectRefs | ||
| 2025 | |||
| 2026 | Function : Updates references between Kindreds and Subjects. | ||
| 2027 | Argument : A Kindred ID and an array reference to a list of Subject IDs. | ||
| 2028 | Returns : N/A | ||
| 2029 | Scope : Private | ||
| 2030 | Called By : updateSubject() and updateKindred(). | ||
| 2031 | Comments : This method updates the KindredSubject table based on a changes | ||
| 2032 | made to a Kindred->Subjects field. | ||
| 2033 | This method also updates the Subject.kindredID field of each of | ||
| 2034 | the relevant Subjects. | ||
| 2035 | |||
| 2036 | =cut | ||
| 2037 | |||
| 2038 | sub _updateKindredSubjectRefs { | ||
| 2039 | my($self, $kindredID, $subjIDListPtr) = @_ ; | ||
| 2040 | my($subjID) ; | ||
| 2041 | my $dbh = $self->{dbh} ; | ||
| 2042 | |||
| 2043 | $DEBUG and carp " ->[_updateKindredSubjectRefs]" ; | ||
| 2044 | |||
| 2045 | $dbh->do( "delete from KindredSubject | ||
| 2046 | where kindredID = $kindredID" ) ; | ||
| 2047 | |||
| 2048 | foreach $subjID (@$subjIDListPtr) { | ||
| 2049 | $dbh->do( "insert into KindredSubject | ||
| 2050 | (kindredID, subjectID) | ||
| 2051 | values ($kindredID, $subjID)" ) ; | ||
| 2052 | $dbh->do( "update Subject | ||
| 2053 | set kindredID = $kindredID | ||
| 2054 | where subjectID = $subjID" ) ; | ||
| 2055 | } | ||
| 2056 | |||
| 2057 | return(1) ; | ||
| 2058 | } | ||
| 2059 | |||
| 2060 | =head2 _updateAssayAttrs | ||
| 2061 | |||
| 2062 | Function : Update AssayAttributes associated with a Genotype, AlleleCall | ||
| 2063 | or Phenotype. | ||
| 2064 | Arguments : Array reference to the list of AssayAttributes, scalar | ||
| 2065 | containing the type of object with which the AssayAttributes | ||
| 2066 | are associated, and another scalar containing the id of that | ||
| 2067 | object. | ||
| 2068 | Returns : N/A | ||
| 2069 | Scope : Private | ||
| 2070 | Called by : updateGenotype(), updatePhenotype(). | ||
| 2071 | Comments : | ||
| 2072 | |||
| 2073 | =cut | ||
| 2074 | |||
| 2075 | sub _updateAssayAttrs { | ||
| 2076 | my($self, $aaListPtr, $type, $id) = @_ ; | ||
| 2077 | my($linkFieldName, $sthAA, $sthAV, $aaPtr, $aaName, $dataType, | ||
| 2078 | $descr, $value, $valueFieldName, $aaID) ; | ||
| 2079 | my $dbh = $self->{dbh} ; | ||
| 2080 | |||
| 2081 | $DEBUG and carp " ->[_updateAssayAttrs] Start ($type)." ; | ||
| 2082 | |||
| 2083 | # Need to derive $linkFieldName: AttributeValues can be | ||
| 2084 | # associated with Gts/Pts or with AlleleCalls | ||
| 2085 | if ($type eq "AlleleCall") { | ||
| 2086 | $linkFieldName = "alleleCallID" ; | ||
| 2087 | } else { | ||
| 2088 | $linkFieldName = "objID" ; | ||
| 2089 | } | ||
| 2090 | |||
| 2091 | if ( ref($aaListPtr) eq "ARRAY" ) { | ||
| 2092 | # Delete old AttributeValues... | ||
| 2093 | $dbh->do( "delete from AttributeValue | ||
| 2094 | where $linkFieldName = $id" ) ; | ||
| 2095 | # ...then create the new ones. | ||
| 2096 | $sthAA = $dbh->prepare( "insert into AssayAttribute | ||
| 2097 | (attrID, name, dataType, description) | ||
| 2098 | values (?, ?, ?, ?)" ) ; | ||
| 2099 | foreach $aaPtr (@$aaListPtr) { | ||
| 2100 | $aaName = $$aaPtr{name} ; | ||
| 2101 | $dataType = $$aaPtr{dataType} ; | ||
| 2102 | $descr = $$aaPtr{description} ; | ||
| 2103 | $value = $$aaPtr{value} ; | ||
| 2104 | # Need to derive $valueFieldName: there is only 1 value, | ||
| 2105 | # but there are 4 possible columns in which to put it: | ||
| 2106 | $valueFieldName = (lc $dataType) . "Value" ; | ||
| 2107 | $sthAV = $dbh->prepare( "insert into AttributeValue | ||
| 2108 | (attrValueID, $linkFieldName, attrID, $valueFieldName) | ||
| 2109 | values (?, ?, ?, ?) ") ; | ||
| 2110 | # Get the AssayAttribute ID | ||
| 2111 | ( $aaID ) = $dbh->selectrow_array( "select attrID from AssayAttribute | ||
| 2112 | where name = '$aaName' | ||
| 2113 | and dataType = '$dataType'" ) ; | ||
| 2114 | if ( ! defined $aaID) { | ||
| 2115 | $sthAA->execute(undef, $aaName, $dataType, $descr) ; | ||
| 2116 | $aaID = $sthAA->{'mysql_insertid'} ; | ||
| 2117 | } | ||
| 2118 | $sthAV->execute(undef, $id, $aaID, $value) ; | ||
| 2119 | $sthAV->finish() ; | ||
| 2120 | } | ||
| 2121 | $sthAA->finish() ; | ||
| 2122 | } elsif ( ! ref($aaListPtr) and ($aaListPtr eq "DELETE") ) { | ||
| 2123 | # Delete the old AttributeValues | ||
| 2124 | $dbh->do( "delete from AttributeValue | ||
| 2125 | where $linkFieldName = $id" ) ; | ||
| 2126 | } else { | ||
| 2127 | carp " ->[_updateAssayAttrs] Inappropriate AssayAttrs value." ; | ||
| 2128 | } | ||
| 2129 | |||
| 2130 | $DEBUG and carp " ->[_updateAssayAttrs] End." ; | ||
| 2131 | |||
| 2132 | return(1) ; | ||
| 2133 | } | ||
| 2134 | |||
| 2135 | 1; | ||
| 2136 |