Genetics/API/DB/Insert.pm untested

filter: Statementstmt 0.0% 0 / 682 Branchbran 0.0% 0 / 208 Conditioncond 0.0% 0 / 113 MC/DCmcdc 0.0% 0 / 75 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 1122 CC 105 SCAR 45.1
CC
105
Cov
0%
insertSubject
306.0
insertTissueSample
110.0
insertStudyVariable
90.0
CRAP 90.6
1 # GenPerl module
2 #
3  
4 # POD documentation - main docs before the code
5  
6 =head1 NAME
7  
8 Genetics::API::DB::Insert
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 $snp = new Genetics::SNP( %init ) ;
21  
22 $id = $api->insertSNP($snp) ;
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 saving new objects (i.e. objects that have not been previously saved to
29 the database). To update objects that have already been saved in the
30 database, see Genetics::API::DB::Update. A better name for this package would
31 probably be Genetics::API::DB::Insert.
32  
33 =head1 LICENSE
34  
35 This program is free software; you can redistribute it and/or modify
36 it under the terms of the GNU General Public License as published by
37 the Free Software Foundation; either version 2 of the License, or
38 (at your option) any later version.
39  
40 This program is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
44  
45 You should have received a copy of the GNU General Public License
46 along with this program; if not, write to the Free Software
47 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48  
49 =head1 FEEDBACK
50  
51 Currently, all feedback should be sent directly to the author.
52  
53 =head1 AUTHOR - Steve Mathias
54  
55 Email: mathias@genomica.com
56  
57 Phone: (720) 565-4029
58  
59 Address: Genomica Corporation
60 1745 38th Street
61 Boulder, CO 80301
62  
63 =head1 DETAILS
64  
65 The rest of the documentation describes each of the object variables and
66 methods. The names of internal variables and methods are preceded with an
67 underscore (_).
68  
69 =cut
70  
71 ##################
72 # #
73 # Begin the code #
74 # #
75 ##################
76  
77 package Genetics::API::DB::Insert ;
78  
79 BEGIN {
80 $ID = "Genetics::API::DB::Insert" ;
81 #$DEBUG = $main::DEBUG ;
82 $DEBUG = 0 ;
83 $DEBUG and warn "Debugging in $ID is on" ;
84 }
85  
86 =head1 Imported Packages
87  
88 strict Just to be anal
89 vars Global variables
90 Carp Error reporting
91  
92 =cut
93  
94 use strict ;
95 use vars qw(@ISA @EXPORT @EXPORT_OK $ID $DEBUG) ;
96 use Carp ;
97 use Exporter ;
98  
99 =head1 Inheritance
100  
101 Exporter Make methods available to importing packages
102  
103 =cut
104  
105 @ISA = qw(Exporter) ;
106  
107 @EXPORT = qw(insertCluster insertSubject insertKindred setSubjectSubjectReferences
108 setSubjectKindredReferences setKindredSubjectReferences insertMarker
109 insertSNP insertGenotype insertStudyVariable insertPhenotype
110 insertFrequencySource insertHtMarkerCollection insertHaplotype
111 insertDNASample insertTissueSample insertMap
112 _insertObjectData _insertAssayAttrs) ;
113 @EXPORT_OK = qw();
114  
115 =head1 Public Methods
116  
117 =head2 insertCluster
118  
119 Function : Insert (create) a Genetics::Object::Cluster object to the database.
120 Argument : A Genetics::Object::Cluster object.
121 Returns : The id of the inserted object.
122 Scope : Public
123 Comments : Objects must be inserted before Clusters or this method will not be
124 able to set references to the Objects in a Cluster.
125  
126 =cut
127  
128 sub insertCluster {
129 my($self, $cluster) = @_ ;
130 my($id, $sth, $listPtr, $ref, $objectID) ;
131 my $dbh = $self->{dbh} ;
132  
133 $DEBUG and carp " ->[insertCluster] $cluster." ;
134  
135 # Object data
136 $id = $self->_insertObjectData($cluster) ;
137 # Cluster fields
138 $sth = $dbh->prepare( "insert into Cluster
139 (clusterID, clusterType)
140 values (?, ?)" ) ;
141 $sth->execute($id, $cluster->field("clusterType")) ;
142 $sth->finish() ;
143 # ClusterContents fields
144 if ( defined ($listPtr = $cluster->field("Contents")) ) {
145 $sth = $dbh->prepare( "insert into ClusterContents
146 (clusterID, objID)
147 values (?, ?)" ) ;
148 foreach $ref (@$listPtr) {
149 # First, get the id of each object...
150 if ( defined($$ref{id}) ) {
151 $objectID = $$ref{id} ;
152 } else {
153 $objectID = $self->_getIDByImportID($$ref{importID}) ;
154 }
155 if ( defined $objectID) {
156 # ...then add the row to ClusterContents
157 $sth->execute($id, $objectID) ;
158 } else {
159 carp " ->[insertCluster] Can't find an ID for Cluster item." ;
160 }
161 }
162 $sth->finish() ;
163 }
164
165 $DEBUG and carp " ->[insertCluster] End." ;
166  
167 return($id) ;
168 }
169  
170 =head2 insertSubject
171  
172 Function : Insert (create) a Genetics::Object::Subject object to the database.
173 Argument : A Genetics::Object::Subject object.
174 Returns : The id of the inserted object.
175 Scope : Public
176  
177 =cut
178  
179 sub insertSubject {
180 my($self, $subj) = @_ ;
181 my($id, $sth, $orgPtr, $orgID, $genusSpecies, $newOrgID, $kindredPtr,
182 $kindredID, $momPtr, $dadPtr, $subjID, $htListPtr, $htpPtr, $htID) ;
183 my %sql = (
184 InsertImportIDKeyword => "insert into Keyword
185 (keywordID, objID, keywordTypeID, stringValue)
186 values (?, ?, ?, ?) ",
187 ) ;
188 my $dbh = $self->{dbh} ;
189
190 $DEBUG and carp " ->[insertSubject] $subj." ;
191 # Object data
192 $id = $self->_insertObjectData($subj) ;
193 # Subject fields
194 $sth = $dbh->prepare( "insert into Subject
195 (subjectID, organismID, kindredID, motherID, fatherID,
196 gender, dateOfBirth, dateOfDeath, isProband)
197 values (?, ?, ?, ?, ?, ?, ?, ?, ?)" ) ;
198 $sth->execute($id, undef, undef, undef, undef, $subj->field("gender"), $subj->field("dateOfBirth"), $subj->field("dateOfDeath"), $subj->field("isProband")) ;
199 $sth->finish() ;
200 # Organism data
201 if (defined ($orgPtr = $subj->field("Organism")) ) {
202 $orgID = $self->_getOrganismID($orgPtr) ;
203 $sth = $dbh->prepare( "update Subject
204 set organismID = ?
205 where subjectID = ?" ) ;
206 $sth->execute($orgID, $id) ;
207 $sth->finish() ;
208 }
209 # Deal with Kindred, Mother and Father references
210 # In some cases, these will only be inserted as ImportID Keywords which
211 # means that the real references must be set by calling the appropriate
212 # set**References() method.
213 if ( defined($kindredPtr = $subj->field("Kindred")) ) {
214 $sth = $dbh->prepare( "update Subject
215 set kindredID = ?
216 where subjectID = ?" ) ;
217 if ( defined($kindredID = $$kindredPtr{id}) ) {
218 $sth->execute($kindredID, $id) ;
219 $sth->finish() ;
220 } elsif ( defined($$kindredPtr{importID}) ) {
221 if ( defined($kindredID = $self->_getIDByImportID($$kindredPtr{importID})) ) {
222 $sth->execute($kindredID, $id) ;
223 $sth->finish() ;
224 } else {
225 $sth = $dbh->prepare( $sql{InsertImportIDKeyword} ) ;
226 $sth->execute(undef, $id, 2, $$kindredPtr{importID}) ;
227 $sth->finish() ;
228 }
229 }
230 }
231 if ( defined($momPtr = $subj->field("Mother")) ) {
232 $sth = $dbh->prepare( "update Subject
233 set motherID = ?
234 where subjectID = ?" ) ;
235 if ( defined($subjID = $$momPtr{id}) ) {
236 $sth->execute($subjID, $id) ;
237 $sth->finish() ;
238 } elsif ( defined($$momPtr{importID}) ) {
239 if ( defined($subjID = $self->_getIDByImportID($$momPtr{importID})) ) {
240 $sth->execute($subjID, $id) ;
241 $sth->finish() ;
242 } else {
243 $sth = $dbh->prepare( $sql{InsertImportIDKeyword} ) ;
244 $sth->execute(undef, $id, 3, $$momPtr{importID}) ;
245 $sth->finish() ;
246 }
247 }
248 }
249 if ( defined($dadPtr = $subj->field("Father")) ) {
250 $sth = $dbh->prepare( "update Subject
251 set fatherID = ?
252 where subjectID = ?" ) ;
253 if ( defined($subjID = $$dadPtr{id}) ) {
254 $sth->execute($subjID, $id) ;
255 $sth->finish() ;
256 } elsif ( defined($$dadPtr{importID}) ) {
257 if ( defined($subjID = $self->_getIDByImportID($$dadPtr{importID})) ) {
258 $sth->execute($subjID, $id) ;
259 $sth->finish() ;
260 } else {
261 $sth = $dbh->prepare( $sql{InsertImportIDKeyword} ) ;
262 $sth->execute(undef, $id, 4, $$dadPtr{importID}) ;
263 $sth->finish() ;
264 }
265 }
266 }
267 # Haplotype assignments
268 if (defined ($htListPtr = $subj->field("Haplotypes")) ) {
269
270 $sth = $dbh->prepare( "insert into SubjectHaplotype
271 (haplotypeID, subjectID, phase)
272 values (?, ?, ?)" ) ;
273 foreach $htpPtr (@$htListPtr) {
274 if ( defined($htID = $htpPtr->{Haplotype}->{id}) ) {
275 $sth->execute($htID, $id, $$htpPtr{phase}) ;
276 } elsif ( defined($htID = $self->_getIDByImportID($htpPtr->{Haplotype}->{importID})) ) {
277 $sth->execute($htID, $id, $$htpPtr{phase}) ;
278 }
279 }
280 $sth->finish() ;
281 }
282  
283 $DEBUG and carp " ->[insertSubject] End." ;
284  
285 return($id) ;
286 }
287  
288 =head2 insertKindred
289  
290 Function : Insert (create) a Genetics::Object::Kindred object to the database.
291 Argument : A Genetics::Object::Kindred object.
292 Returns : The id of the inserted object.
293 Scope : Public
294 Comments : Subjects must be inserted before Kindreds or this method will not be
295 able to set references to the Subjects in a Kindred.
296  
297 =cut
298  
299 sub insertKindred {
300 my($self, $kindred) = @_ ;
301 my($id, $sth, $kindredPtr, $parentID, $subjListPtr, $subjPtr, $subjID,
302 $sthS, $sthKS) ;
303 my $dbh = $self->{dbh} ;
304  
305 $DEBUG and carp " ->[insertKindred] $kindred." ;
306  
307 # Object data
308 $id = $self->_insertObjectData($kindred) ;
309 # Kindred fields
310 $sth = $dbh->prepare( "insert into Kindred
311 (kindredID, isDerived, parentID)
312 values (?, ?, ?)" ) ;
313 if ($kindred->isDerived == 1) {
314 $kindredPtr = $kindred->DerivedFrom ;
315 if ( defined($$kindredPtr{id}) ) {
316 $parentID = $$kindredPtr{id} ;
317 } else {
318 $parentID = $self->_getIDByImportID($$kindredPtr{importID}) ;
319 }
320 $sth->execute($id, 1, $parentID) ;
321
322 } else {
323 $sth->execute($id, 0, undef) ;
324 }
325 $sth->finish() ;
326 # KindredSubject
327 if ( defined($subjListPtr = $kindred->field("Subjects")) ) {
328 $sthKS = $dbh->prepare( "insert into KindredSubject
329 (kindredID, subjectID)
330 values (?, ?)" ) ;
331 $sthS = $dbh->prepare( "update Subject
332 set kindredID = ?
333 where subjectID = ?" ) ;
334 foreach $subjPtr (@$subjListPtr) {
335 # Get the Subject ID...
336 if ( defined($$subjPtr{id}) ) {
337 $subjID = $$subjPtr{id} ;
338 } else {
339 $subjID = $self->_getIDByImportID($$subjPtr{importID}) ;
340 }
341 if (defined $subjID) {
342 # ...add a row to KindredSubject...
343 # This is not working with derived Kindreds when called from dns2gp.pl
344 # I'm not sure where the problem is. The print line is so the rows can
345 # be added to KindredSubject manually.
346 $kindred->isDerived == 1 and print "insert into KindredSubject (kindredID, subjectID) values ($id, $subjID);\n" ;
347 $sthKS->execute($id, $subjID) ;
348 # ...and if its not a derived Kindred, add a reference to the Subject table
349 $kindred->isDerived == 0 and $sthS->execute($id, $subjID) ;
350 }
351 }
352 $sthKS->finish() ;
353 $sthS->finish() ;
354 }
355  
356 $DEBUG and carp " ->[insertKindred] End." ;
357  
358 return($id) ;
359 }
360  
361 =head2 setSubjectSubjectReferences
362  
363 Function : Sets mother/father references between Subjects based on the
364 'Mother ImportID' and 'Father ImportID' Keywords associated
365 with the Subjects.
366 Argument : An array reference to a list of Subject IDs.
367 Returns : N/A
368 Scope : Public
369 Comments : The fields that are updated by this method are:
370 Subject.motherID
371 Subject.fatherID
372  
373 =cut
374  
375 sub setSubjectSubjectReferences {
376 my($self, $idListPtr) = @_ ;
377 my($sthM, $sthF, $sthS2, $sthS3, $id, $momImportID, $momID, $dadImportID, $dadID) ;
378 my $dbh = $self->{dbh} ;
379
380 $DEBUG and carp " ->[setSubjectSubjectReferences]" ;
381  
382 $sthM = $dbh->prepare("select stringValue from Keyword where keywordTypeID = 3 and objID = ?") ;
383 $sthF = $dbh->prepare("select stringValue from Keyword where keywordTypeID = 4 and objID = ?") ;
384 $sthS2 = $dbh->prepare("update Subject set motherID=? where subjectID = ?") ;
385 $sthS3 = $dbh->prepare("update Subject set fatherID=? where subjectID = ?") ;
386
387 foreach $id (@$idListPtr) {
388 # Mother refs
389 # First see if this Subject has a 'Mother PreImportID' Keyword...
390 $sthM->execute($id) ;
391 ( $momImportID ) = $sthM->fetchrow_array() ;
392 if (defined $momImportID) {
393 # ...if so, find the Object.id of that Subject...
394 $momID = $self->_getIDByImportID($momImportID) ;
395 if (defined $momID) {
396 # ...and then add the reference to the Subject table
397 $sthS2->execute($momID, $id) ;
398 }
399 }
400 # Father refs:
401 # First see if this Subject has a 'Father PreImportID' Keyword...
402 $sthF->execute($id) ;
403 ( $dadImportID ) = $sthF->fetchrow_array() ;
404 if (defined $dadImportID) {
405 # ...if so, find the Object.id of that Subject
406 $dadID = $self->_getIDByImportID($dadImportID) ;
407 if (defined $dadID) {
408 # ...and then add the reference to the Subject table
409 $sthS3->execute($dadID, $id) ;
410 }
411 }
412 }
413  
414 $sthM->finish() ;
415 $sthF->finish() ;
416 $sthS2->finish() ;
417 $sthS3->finish() ;
418  
419 return(1) ;
420 }
421  
422 =head2 setSubjectKindredReferences
423  
424 Function : Sets references between Subjects and Kindreds based on the
425 'Kindred ImportID' Keywords associated with the Subjects.
426 Argument : An array reference to a list of Subject IDs.
427 Returns : N/A
428 Scope : Public
429 Comments : A Subject can get its Kindred reference in two possible ways:
430 from Subject->field('Kindred') or
431 from Kindred->field('Subjects'). This method sets references
432 based on the former. To set references based on the latter,
433 use setKindredSubjectReferences.
434  
435 The fields that are updated by this method are:
436 Subject.kindredID
437 KindredSubject.kindredID
438 KindredSubject.SubjectID
439  
440 =cut
441  
442 sub setSubjectKindredReferences {
443 my($self, $idListPtr) = @_ ;
444 my($id, $sthK, $sthS1, $sthKS, $kindredImportID, $kindredID) ;
445 my $dbh = $self->{dbh} ;
446
447 $DEBUG and carp " ->[setSubjectKindredReferences]" ;
448  
449 $sthK = $dbh->prepare("select stringValue from Keyword where keywordTypeID = 2 and objID = ?") ;
450 $sthS1 = $dbh->prepare("update Subject set kindredID=? where subjectID = ?") ;
451 $sthKS = $dbh->prepare("insert into KindredSubject (kindredID, subjectID) values (?, ?)") ;
452  
453 foreach $id (@$idListPtr) {
454 # First see if this Subject has a 'Kindred ImportID' Keyword...
455 $sthK->execute($id) ;
456 ( $kindredImportID ) = $sthK->fetchrow_array() ;
457 if (defined $kindredImportID) {
458 # ...if so, find the Object.id of that Kindred...
459 $kindredID = $self->_getIDByImportID($kindredImportID) ;
460 # ...and then add the reference to the Subject table
461 $sthS1->execute($kindredID, $id) ;
462 # and add a corresponding row to KindredSubject
463 $sthKS->execute($kindredID, $id) ;
464 }
465 }
466
467 $sthK->finish() ;
468 $sthS1->finish() ;
469 $sthKS->finish() ;
470  
471 return(1) ;
472 }
473  
474 =head2 setKindredSubjectReferences
475  
476 Function : Sets references between Subjects and Kindreds based on the
477 'Subjects ImportID' Keywords associated with the Kindreds.
478 Argument : An array reference to a list of Kindred IDs.
479 Returns : N/A
480 Scope : Public
481 Comments : A Subject can get its Kindred reference in two possible ways:
482 from Subject->field('Kindred') or
483 from Kindred->field('Subjects'). This method sets references
484 based on the latter. To set references based on the former, use
485 setSubjectKindredReferences.
486  
487 The fields that are updated by this method are:
488 Subject.kindredID
489 KindredSubject.kindredID
490 KindredSubject.SubjectID
491  
492 =cut
493  
494 sub setKindredSubjectReferences {
495 my($self, $idListPtr) = @_ ;
496 my($id, $sthK, $sthS1, $sthKS, $subjImportID, $subjID) ;
497 my $dbh = $self->{dbh} ;
498
499 $DEBUG and carp " ->[setKindredSubjectReferences]" ;
500
501 $sthK = $dbh->prepare("select stringValue from Keyword where keywordTypeID = 5 and objID = ?") ;
502 $sthS1 = $dbh->prepare("update Subject set kindredID=? where subjectID = ?") ;
503 $sthKS = $dbh->prepare("insert into KindredSubject (kindredID, subjectID) values (?, ?)") ;
504  
505 foreach $id (@$idListPtr) {
506 # First see if the Kindred has any 'Subjects PreImportID' Keywords...
507 $sthK->execute($id) ;
508 while ( ($subjImportID) = $sthK->fetchrow_array() ) {
509 # ...if so, find the Object.id of each Subject...
510 $subjID = $self->_getIDByImportID($subjImportID) ;
511 if (defined $subjID) {
512 # ...and then add the reference to the Subject table
513 $sthS1->execute($id, $subjID) ;
514 # and add a corresponding row to KindredSubject
515 $sthKS->execute($id, $subjID) ;
516 }
517 }
518 }
519
520 $sthK->finish() ;
521 $sthS1->finish() ;
522 $sthKS->finish() ;
523  
524 return(1) ;
525 }
526  
527 =head2 insertMarker
528  
529 Function : Insert (create) a Genetics::Object::Marker object to the database.
530 Argument : A Genetics::Object::Marker object.
531 Returns : The id of the inserted object.
532 Scope : Public
533 Comments :
534  
535 =cut
536  
537 sub insertMarker {
538 my($self, $marker) = @_ ;
539 my($id, $sth, $sth2, $orgPtr, $genusSpecies, $orgID, $newOrgID, $seqPtr, $seqID, $iscnListPtr,
540 $iscnPtr, $iscnID, $alleleListPtr, $allelePtr, $alleleID) ;
541 my $dbh = $self->{dbh} ;
542  
543 $DEBUG and carp " ->[insertMarker] $marker." ;
544  
545 # Object data
546 $id = $self->_insertObjectData($marker) ;
547 # SequenceObject fields
548 $sth = $dbh->prepare( "insert into SequenceObject
549 (seqObjectID, chromosome, organismID,
550 sequenceID, malePloidy, femalePloidy)
551 values (?, ?, ?, ?, ?, ?)" ) ;
552 $sth->execute($id, $marker->field("chromosome"), undef, undef,
553 $marker->field("malePloidy"), $marker->field("femalePloidy")) ;
554 $sth->finish() ;
555 # Organism data
556 if (defined ($orgPtr = $marker->field("Organism")) ) {
557 $orgID = $self->_getOrganismID($orgPtr) ;
558 $sth = $dbh->prepare( "update SequenceObject
559 set organismID = ?
560 where seqObjectID = ?" ) ;
561 $sth->execute($orgID, $id) ;
562 $sth->finish() ;
563 }
564 # Sequence data
565 if ( defined ($seqPtr = $marker->field("Sequence")) ) {
566 # Insert the new Sequence...
567 $sth = $dbh->prepare( "insert into Sequence
568 (sequenceID, sequence, length, lengthUnits)
569 values (?, ?, ?, ?)" ) ;
570 $sth->execute(undef, $$seqPtr{sequence}, $$seqPtr{length}, $$seqPtr{lengthUnits}) ;
571 $seqID = $sth->{'mysql_insertid'} ;
572 $sth->finish() ;
573 # Add sequenceID to SequenceObject
574 $sth = $dbh->prepare( "update SequenceObject
575 set sequenceID = ?
576 where seqObjectID = ?" ) ;
577 $sth->execute($seqID, $id) ;
578 $sth->finish() ;
579 }
580 # ISCNMapLocation data
581 if ( defined ($iscnListPtr = $marker->field("ISCNMapLocations")) ) {
582 $sth = $dbh->prepare( "insert into ISCNMapLocation
583 (iscnMapLocID, chrNumber, chrArm, band, bandingMethod)
584 values (?, ?, ?, ?, ?)" ) ;
585 $sth2 = $dbh->prepare( "insert into SeqObjISCN
586 (seqObjectID, iscnMapLocID)
587 values (?, ?)" ) ;
588 foreach $iscnPtr (@$iscnListPtr) {
589 # Insert the new ISCNMapLocation
590 $sth->execute(undef, $$iscnPtr{chrNumber}, $$iscnPtr{chrArm},
591 $$iscnPtr{band}, $$iscnPtr{bandingMethod}) ;
592 $iscnID = $sth->{'mysql_insertid'} ;
593 # Add a row to SeqObjISCN
594 $sth2->execute($id, $iscnID) ;
595 }
596 $sth->finish() ;
597 $sth2->finish() ;
598 }
599 # Marker fields
600 $sth = $dbh->prepare( "insert into Marker
601 (markerID, polymorphismType, polymorphismIndex1,
602 polymorphismIndex2, repeatSequence)
603 values (?, ?, ?, ?, ?)" ) ;
604 $sth->execute($id, $marker->field("polymorphismType"), $marker->field("polymorphismIndex1"),
605 $marker->field("polymorphismIndex2"), $marker->field("repeatSequence") ) ;
606 $sth->finish() ;
607 # Allele fields
608 if ( defined ($alleleListPtr = $marker->field("Alleles")) ) {
609 $sth = $dbh->prepare( "insert into Allele
610 (alleleID, poID, name, type)
611 values (?, ?, ?, ?)" ) ;
612 foreach $allelePtr (@$alleleListPtr) {
613 # Check if the Marker already has an Allele w/ the same name and type...
614 ( $alleleID ) = $dbh->selectrow_array( "select alleleID from Allele
615 where poID = '$id' and
616 name = '$$allelePtr{name}' and
617 type = '$$allelePtr{type}'" ) ;
618 if ( ! defined $alleleID) {
619 # ...if not, create a new Allele
620 $sth->execute(undef, $id, $$allelePtr{name}, $$allelePtr{type}) ;
621 }
622 }
623 $sth->finish() ;
624 }
625
626 $DEBUG and carp " ->[insertMarker] End." ;
627
628 return($id) ;
629 }
630  
631 =head2 insertSNP
632  
633 Function : Insert (create) a Genetics::Object::SNP object to the database.
634 Argument : A Genetics::Object::SNP object.
635 Returns : The id of the inserted object.
636 Scope : Public
637 Comments :
638  
639 =cut
640  
641 sub insertSNP {
642 my($self, $snp) = @_ ;
643 my($id, $sth, $sth2, $orgPtr, $genusSpecies, $orgID, $newOrgID, $seqPtr, $seqID, $iscnListPtr,
644 $iscnPtr, $iscnID, $alleleListPtr, $allelePtr, $alleleID) ;
645 my $dbh = $self->{dbh} ;
646  
647 $DEBUG and carp " ->[insertSNP] $snp." ;
648  
649 # Object data
650 $id = $self->_insertObjectData($snp) ;
651 # SequenceObject fields
652 $sth = $dbh->prepare( "insert into SequenceObject
653 (seqObjectID, chromosome, organismID,
654 sequenceID, malePloidy, femalePloidy)
655 values (?, ?, ?, ?, ?, ?)" ) ;
656 $sth->execute($id, $snp->field("chromosome"), undef, undef,
657 $snp->field("malePloidy"), $snp->field("femalePloidy")) ;
658 $sth->finish() ;
659 # Organism data
660 if (defined ($orgPtr = $snp->field("Organism")) ) {
661 $orgID = $self->_getOrganismID($orgPtr) ;
662 $sth = $dbh->prepare( "update SequenceObject
663 set organismID = ?
664 where seqObjectID = ?" ) ;
665 $sth->execute($orgID, $id) ;
666 $sth->finish() ;
667 }
668 # Sequence data
669 if ( defined ($seqPtr = $snp->field("Sequence")) ) {
670 # Insert the new Sequence...
671 $sth = $dbh->prepare( "insert into Sequence
672 (sequenceID, sequence, length, lengthUnits)
673 values (?, ?, ?, ?)" ) ;
674 $sth->execute(undef, $$seqPtr{sequence}, $$seqPtr{length}, $$seqPtr{lengthUnits}) ;
675 $seqID = $sth->{'mysql_insertid'} ;
676 $sth->finish() ;
677 # Add sequenceID to SequenceObject
678 $sth = $dbh->prepare( "update SequenceObject
679 set sequenceID = ?
680 where seqObjectID = ?" ) ;
681 $sth->execute($seqID, $id) ;
682 $sth->finish() ;
683 }
684 # ISCNMapLocation data
685 if ( defined ($iscnListPtr = $snp->field("ISCNMapLocations")) ) {
686 $sth = $dbh->prepare( "insert into ISCNMapLocation
687 (iscnMapLocID, chrNumber, chrArm, band, bandingMethod)
688 values (?, ?, ?, ?, ?)" ) ;
689 $sth2 = $dbh->prepare( "insert into SeqObjISCN
690 (seqObjectID, iscnMapLocID)
691 values (?, ?)" ) ;
692 foreach $iscnPtr (@$iscnListPtr) {
693 # Insert the new ISCNMapLocation
694 $sth->execute(undef, $$iscnPtr{chrNumber}, $$iscnPtr{chrArm},
695 $$iscnPtr{band}, $$iscnPtr{bandingMethod}) ;
696 $iscnID = $sth->{'mysql_insertid'} ;
697 # Add a row to SeqObjISCN
698 $sth2->execute($id, $iscnID) ;
699 }
700 $sth->finish() ;
701 $sth2->finish() ;
702 }
703 # SNP fields
704 $sth = $dbh->prepare( "insert into SNP
705 (snpID, snpType, functionClass, snpIndex,
706 isConfirmed, confirmMethod)
707 values (?, ?, ?, ?, ?, ?)" ) ;
708 $sth->execute($id, $snp->field("snpType"), $snp->field("functionClass"), $snp->field("snpIndex"),
709 $snp->field("isConfirmed"), $snp->field("confirmMethod")) ;
710 $sth->finish() ;
711 # Allele fields
712 if ( defined ($alleleListPtr = $snp->field("Alleles")) ) {
713 $sth = $dbh->prepare( "insert into Allele
714 (alleleID, poID, name, type)
715 values (?, ?, ?, ?)" ) ;
716 foreach $allelePtr (@$alleleListPtr) {
717 # Check if the Marker already has an Allele w/ the same name and type...
718 ( $alleleID ) = $dbh->selectrow_array( "select alleleID from Allele
719 where poID = '$id' and
720 name = '$$allelePtr{name}' and
721 type = '$$allelePtr{type}'" ) ;
722 if ( ! defined $alleleID) {
723 # ...if not, create a new Allele
724 $sth->execute(undef, $id, $$allelePtr{name}, $$allelePtr{type}) ;
725 }
726 }
727 $sth->finish() ;
728 }
729
730 $DEBUG and carp " ->[insertSNP] End." ;
731  
732 return($id) ;
733 }
734  
735 =head2 insertGenotype
736  
737 Function : Insert (create) a Genetics::Object::Genotype object to the database.
738 Argument : A Genetics::Object::Genotype object.
739 Returns : The id of the inserted object.
740 Scope : Public
741 Comments :
742  
743 =cut
744  
745 sub insertGenotype {
746 my($self, $gt) = @_ ;
747 my($id, $sth, $subjPtr, $subjectID, $markerPtr, $markerID, $sortOrder, $sthA,
748 $sthAC, $alleleCallListPtr, $alleleCallPtr, $alleleID, $alleleCallID, $aaListPtr) ;
749 my $dbh = $self->{dbh} ;
750  
751 $DEBUG and carp " ->[insertGenotype] $gt." ;
752  
753 # Object data
754 $id = $self->_insertObjectData($gt) ;
755 # Get Subject ID
756 $subjPtr = $gt->field("Subject") ;
757 if ( defined($$subjPtr{id}) ) {
758 $subjectID = $$subjPtr{id} ;
759 } else {
760 $subjectID = $self->_getIDByImportID($$subjPtr{importID}) ;
761 }
762 if ( ! defined $subjectID) {
763 carp " ->[insertGenotype] Can't insert Genotype $gt. Can't find Subject.id!" ;
764 return(undef) ;
765 }
766 # Get Marker ID
767 $markerPtr = $gt->field("Marker") ;
768 if ( defined($$markerPtr{id}) ) {
769 $markerID = $$markerPtr{id} ;
770 } else {
771 $markerID = $self->_getIDByImportID($$markerPtr{importID}) ;
772 }
773 if ( ! defined $markerID) {
774 carp " ->[insertGenotype] Can't insert Genotype $gt. Can't find Marker.id!" ;
775 return(undef) ;
776 }
777 # Genotype fields
778 $sth = $dbh->prepare( "insert into Genotype
779 (gtID, subjectID, poID, isActive, icResult, dateCollected)
780 values (?, ?, ?, ?, ?, ?)" ) ;
781 $sth->execute($id, $subjectID, $markerID, $gt->field("isActive"), $gt->field("icResult"),
782 $gt->field("dateCollected")) ;
783 $sth->finish() ;
784 # Genotype AssayAttributes
785 if ( defined ($aaListPtr = $gt->field("AssayAttrs")) ) {
786 $self->_insertAssayAttrs($aaListPtr, "Genotype", $id) ;
787 }
788 # AlleleCall data
789 $sortOrder = 1 ;
790 $sthA = $dbh->prepare( "insert into Allele
791 (alleleID, poID, name, type)
792 values (?, ?, ?, ?)" ) ;
793 $sthAC = $dbh->prepare( "insert into AlleleCall
794 (alleleCallID, gtID, alleleID, sortOrder, phase)
795 values (?, ?, ?, ?, ?)" ) ;
796 $alleleCallListPtr = $gt->field("AlleleCalls") ;
797 foreach $alleleCallPtr (@$alleleCallListPtr) {
798 # Check if the Marker already has an Allele w/ the same name and type...
799 ( $alleleID ) = $dbh->selectrow_array( "select alleleID from Allele
800 where poID = '$markerID' and
801 name = '$$alleleCallPtr{alleleName}' and
802 type = '$$alleleCallPtr{alleleType}'" ) ;
803 if ( ! defined $alleleID) {
804 # ...if not, create a new Allele
805 $sthA->execute(undef, $markerID, $$alleleCallPtr{alleleName}, $$alleleCallPtr{alleleType}) ;
806 $alleleID = $sthA->{'mysql_insertid'} ;
807 }
808 $sthAC->execute(undef, $id, $alleleID, $sortOrder, $$alleleCallPtr{phase}) ;
809 $alleleCallID = $sthAC->{'mysql_insertid'} ;
810 $sortOrder++ ;
811 # AlleleCall AssayAttributes
812 if ( defined ($aaListPtr = $$alleleCallPtr{AssayAttrs}) ) {
813 $self->_insertAssayAttrs($aaListPtr, "AlleleCall", $alleleCallID) ;
814 }
815 }
816 $sthA->finish() ;
817 $sthAC->finish() ;
818  
819 $DEBUG and carp " ->[insertGenotype] End." ;
820  
821 return($id) ;
822 }
823  
824 =head2 insertStudyVariable
825  
826 Function : Insert (create) a Genetics::Object::StudyVariable object to the database.
827 Argument : A Genetics::Object::StudyVariable object.
828 Returns : The id of the inserted object.
829 Scope : Public
830 Comments :
831  
832 =cut
833  
834 sub insertStudyVariable {
835 my($self, $sv) = @_ ;
836 my($id, $svFormat, $svCategory, $sth, $codesListPtr, $codePtr, $cdID, $asdPtr,
837 $asdID, $aseListPtr, $asePtr, $sth1, $lcDefPtr, $lcdID, $lcListPtr, $lcPtr) ;
838 my $dbh = $self->{dbh} ;
839  
840 $DEBUG and carp " ->[insertStudyVariable] $sv." ;
841  
842 # Object data
843 $id = $self->_insertObjectData($sv) ;
844 # StudyVariable fields
845 $svFormat = $sv->field("format") ;
846 $svCategory = $sv->field("category") ;
847 $sth = $dbh->prepare( "insert into StudyVariable
848 (studyVariableID, category, format, isXLinked,
849 description, numberLowerBound, numberUpperBound,
850 dateLowerBound, dateUpperBound)
851 values (?, ?, ?, ?, ?, ?, ?, ?, ?)" ) ;
852 if ( $svFormat eq "Number") {
853 $sth->execute($id, $svCategory, $svFormat, $sv->field("isXLinked"), $sv->field("description"), $sv->field("lowerBound"), $sv->field("upperBound"), undef, undef) ;
854 } elsif ( $svFormat eq "Date") {
855 $sth->execute($id, $svCategory, $svFormat, $sv->field("isXLinked"), $sv->field("description"), undef, undef, $sv->field("lowerBound"), $sv->field("upperBound")) ;
856 } else {
857 $sth->execute($id, $svCategory, $svFormat, $sv->field("isXLinked"), $sv->field("description"), undef, undef, undef, undef) ;
858 }
859 $sth->finish() ;
860 # Code data
861 if ( $svFormat eq "Code" ) {
862 $sth = $dbh->prepare( "insert into CodeDerivation
863 (codeDerivationID, studyVariableID, code,
864 description, formula)
865 values (?, ?, ?, ?, ?)" ) ;
866 if ($svCategory eq "StaticLiabilityClass") {
867 $sth1 = $dbh->prepare( "insert into StaticLCPenetrance
868 (cdID, pen11, pen12, pen22, malePen1, malePen2)
869 values (?, ?, ?, ?, ?, ?)" ) ;
870 }
871 $codesListPtr = $sv->field("Codes") ;
872 foreach $codePtr (@$codesListPtr) {
873 $sth->execute(undef, $id, $$codePtr{code}, $$codePtr{description}, undef) ;
874 $cdID = $sth->{'mysql_insertid'} ;
875 if ($svCategory eq "StaticLiabilityClass") {
876 $sth1->execute($cdID, $$codePtr{pen11}, $$codePtr{pen12}, $$codePtr{pen22}, $$codePtr{malePen1}, $$codePtr{malePen2}) ;
877 }
878 }
879 }
880 # AffectionStatus data
881 if ( $svCategory =~ /AffectionStatus$/ ) {
882 $asdPtr = $sv->field("AffStatDef") ;
883 $sth = $dbh->prepare( "insert into AffectionStatusDefinition
884 (asDefID, studyVariableID, name, diseaseAlleleFreq,
885 pen11, pen12, pen22, malePen1, malePen2)
886 values (?, ?, ?, ?, ?, ?, ?, ?, ?)" ) ;
887 $sth->execute(undef, $id, $$asdPtr{name}, $$asdPtr{diseaseAlleleFreq}, $$asdPtr{pen11}, $$asdPtr{pen12}, $$asdPtr{pen22}, $$asdPtr{malePen1}, $$asdPtr{malePen2}) ;
888 $asdID = $sth->{'mysql_insertid'} ;
889 $sth->finish() ;
890 # AffectionStatusElement fields
891 if ( defined($aseListPtr = $$asdPtr{AffStatElements}) ) {
892 $sth = $dbh->prepare( "insert into AffectionStatusElement
893 (asElementID, asDefID, code, type, formula)
894 values (?, ?, ?, ?, ?)" ) ;
895 foreach $asePtr (@$aseListPtr) {
896 $sth->execute(undef, $asdID, $$asePtr{code}, $$asePtr{type}, $$asePtr{formula}) ;
897 }
898 $sth->finish() ;
899 }
900 # LiabilityClass data
901 # NB: these are dynamic LCs; if the SV is of category StaticLiabilityClass,
902 # the penetrance values are handled with the Codes and are stored in the
903 # StaticLCPenetrance table
904 if ( defined($lcDefPtr = $sv->field("LCDef")) ) {
905 $sth = $dbh->prepare( "insert into LiabilityClassDefinition
906 (lcDefID, studyVariableID, name)
907 values (?, ?, ?)" ) ;
908 $sth->execute(undef, $id, $$lcDefPtr{name}) ;
909 $lcdID = $sth->{'mysql_insertid'} ;
910 $sth->finish() ;
911 # LiabilityClass fields
912 $lcListPtr = $$lcDefPtr{LiabilityClasses} ;
913 $sth = $dbh->prepare( "insert into LiabilityClass
914 (lcID, lcDefID, code, description, pen11, pen12,
915 pen22, malePen1, malePen2, formula)
916 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ) ;
917 foreach $lcPtr (@$lcListPtr) {
918 $sth->execute(undef, $lcdID, $$lcPtr{code}, $$lcPtr{description}, $$lcPtr{pen11}, $$lcPtr{pen12}, $$lcPtr{pen22}, $$lcPtr{malePen1}, $$lcPtr{malePen2}, $$lcPtr{formula}) ;
919 }
920 $sth->finish() ;
921 }
922 }
923
924 $DEBUG and carp " ->[insertStudyVariable] End." ;
925  
926 return($id) ;
927 }
928  
929 =head2 insertPhenotype
930  
931 Function : Insert (create) a Genetics::Object::Phenotype object to the database.
932 Argument : A Genetics::Object::Phenotype object.
933 Returns : The id of the inserted object.
934 Scope : Public
935 Comments :
936  
937 =cut
938  
939 sub insertPhenotype {
940 my($self, $pt) = @_ ;
941 my($id, $sth, $subjPtr, $subjectID, $studyVarPtr, $studyVarID, $svFormat, $aaListPtr) ;
942 my $dbh = $self->{dbh} ;
943  
944 $DEBUG and carp " ->[insertPhenotype] $pt." ;
945  
946 # Object data
947 $id = $self->_insertObjectData($pt) ;
948 # Get Subject ID
949 $subjPtr = $pt->field("Subject") ;
950 if ( defined($$subjPtr{id}) ) {
951 $subjectID = $$subjPtr{id} ;
952 } else {
953 $subjectID = $self->_getIDByImportID($$subjPtr{importID}) ;
954 }
955 if ( ! defined $subjectID) {
956 carp " ->[insertPhenotype] Can't insert Phenotype $pt. Can't find Subject.id!" ;
957 return(undef) ;
958 }
959 # Get StudyVariable ID
960 $studyVarPtr = $pt->field("StudyVariable") ;
961 if ( defined($$studyVarPtr{id}) ) {
962 $studyVarID = $$studyVarPtr{id} ;
963 } else {
964 $studyVarID = $self->_getIDByImportID($$studyVarPtr{importID}) ;
965 }
966 if ( ! defined $studyVarID) {
967 carp " ->[insertPhenotype] Can't insert Phenotype $pt. Can't find StudyVariable.id!" ;
968 return(undef) ;
969 }
970 # Phenotype fields
971 # First, find out what the format of the value is:
972 ( $svFormat ) = $dbh->selectrow_array( "select format from StudyVariable
973 where studyVariableID = '$studyVarID'" ) ;
974 $sth = $dbh->prepare( "insert into Phenotype
975 (ptID, subjectID, svID, numberValue, codeValue,
976 dateValue, isActive, dateCollected)
977 values (?, ?, ?, ?, ?, ?, ?, ?)" ) ;
978 if ($svFormat eq "Number") {
979 $sth->execute($id, $subjectID, $studyVarID, $pt->field("value"), undef, undef, $pt->field("isActive"), $pt->field("dateCollected")) ;
980 } elsif ($svFormat eq "Code") {
981 $sth->execute($id, $subjectID, $studyVarID, undef, $pt->field("value"), undef, $pt->field("isActive"), $pt->field("dateCollected")) ;
982 } elsif ($svFormat eq "Date") {
983 $sth->execute($id, $subjectID, $studyVarID, undef, undef, $pt->field("value"), $pt->field("isActive"), $pt->field("dateCollected")) ;
984 } else {
985 carp " ->[insertPhenotype] Can't insert Phenotype $pt. Unknown StudyVariable format!" ;
986 return(undef) ;
987 }
988 $sth->finish() ;
989 # Phenotype AssayAttributes
990 if ( defined ($aaListPtr = $pt->field("AssayAttrs")) ) {
991 $self->_insertAssayAttrs($aaListPtr, "Phenotype", $id) ;
992 }
993
994 $DEBUG and carp " ->[insertPhenotype] End." ;
995  
996 return($id) ;
997 }
998  
999 =head2 insertFrequencySource
1000  
1001 Function : Insert (create) a Genetics::Object::FrequencySource object to the database.
1002 Argument : A Genetics::Object::FrequencySource object.
1003 Returns : The id of the inserted object.
1004 Scope : Public
1005 Comments :
1006  
1007 =cut
1008  
1009 sub insertFrequencySource {
1010 my($self, $fs) = @_ ;
1011 my($sthA, $sthOF, $sthFSOF, $id, $listPtr, $oafPtr, $allelePtr, $markerPtr, $markerID,
1012 $alleleID, $obsFreqID, $ohfPtr, $htPtr, $htID) ;
1013 my $dbh = $self->{dbh} ;
1014  
1015 $DEBUG and carp " ->[insertFrequencySource] $fs." ;
1016  
1017 $sthA = $dbh->prepare( "insert into Allele
1018 (alleleID, poID, name, type)
1019 values (?, ?, ?, ?)" ) ;
1020 $sthOF = $dbh->prepare( "insert into ObsFrequency
1021 (obsFreqID, type, alleleID, htID, frequency)
1022 values (?, ?, ?, ?, ?)" ) ;
1023 $sthFSOF = $dbh->prepare( "insert into FreqSourceObsFrequency
1024 (freqSourceID, obsFreqID)
1025 values (?, ?)" ) ;
1026  
1027 # Object data
1028 $id = $self->_insertObjectData($fs) ;
1029 # ObsFrequency data
1030 # Alleles
1031 if ( defined ($listPtr = $fs->field("ObsAlleleFrequencies")) ) {
1032 foreach $oafPtr (@$listPtr) {
1033 # Figure out what Allele we're talking about. First find the Marker ID...
1034 $allelePtr = $$oafPtr{Allele} ;
1035 $markerPtr = $$allelePtr{Marker} ;
1036 if ( defined($$markerPtr{id}) ) {
1037 $markerID = $$markerPtr{id} ;
1038 } else {
1039 $markerID = $self->_getIDByImportID($$markerPtr{importID}) ;
1040 }
1041 if ( ! defined $markerID) {
1042 carp " ->[insertFrequencySource] Can't find an ID for Marker $$markerPtr{name}" ;
1043 return(undef) ;
1044 }
1045 # ...then see if the Marker already has an Allele w/ the same name and type...
1046 ( $alleleID ) = $dbh->selectrow_array( "select alleleID from Allele
1047 where poID = '$markerID' and
1048 name = '$$allelePtr{name}' and
1049 type = '$$allelePtr{type}'" ) ;
1050 if ( ! defined $alleleID) {
1051 # ...if not, create a new Allele
1052 $sthA->execute(undef, $markerID, $$allelePtr{name}, $$allelePtr{type}) ;
1053 $alleleID = $sthA->{'mysql_insertid'} ;
1054 }
1055 # Insert the ObsFrequency
1056 $sthOF->execute(undef, "Allele", $alleleID, undef, $$oafPtr{frequency}) ;
1057 $obsFreqID = $sthOF->{'mysql_insertid'} ;
1058 # Add row to FreqSourceObsFrequency
1059 $sthFSOF->execute($id, $obsFreqID) ;
1060 }
1061 }
1062 $sthA->finish() ;
1063 # Haplotypes
1064 if ( defined ($listPtr = $fs->field("ObsHtFrequencies")) ) {
1065 foreach $ohfPtr (@$listPtr) {
1066 # Figure out what Haplotype we're talking about.
1067 $htPtr = $$ohfPtr{Haplotype} ;
1068 if ( defined($$htPtr{id}) ) {
1069 $htID = $$htPtr{id} ;
1070 } else {
1071 $htID = $self->_getIDByImportID($$htPtr{importID}) ;
1072 }
1073 if ( ! defined $htID) {
1074 carp " ->[insertFrequencySource] Can't find an ID for Haplotype $$htPtr{name}" ;
1075 return(undef) ;
1076 }
1077 # Insert the ObsFrequency
1078 $sthOF->execute(undef, "Ht", undef, $htID, $$ohfPtr{frequency}) ;
1079 $obsFreqID = $sthOF->{'mysql_insertid'} ;
1080 # Add row to FreqSourceObsFrequency
1081 $sthFSOF->execute($id, $obsFreqID) ;
1082 }
1083 }
1084 $sthOF->finish() ;
1085 $sthFSOF->finish() ;
1086  
1087 $DEBUG and carp " ->[insertFrequencySource] End." ;
1088  
1089 return($id) ;
1090 }
1091  
1092 =head2 insertHtMarkerCollection
1093  
1094 Function : Insert (create) a Genetics::Object::HtMarkerCollection object to the database.
1095 Argument : A Genetics::Object::HtMarkerCollection object.
1096 Returns : The id of the inserted object.
1097 Scope : Public
1098 Comments :
1099  
1100 =cut
1101  
1102 sub insertHtMarkerCollection {
1103 my($self, $hmc) = @_ ;
1104 my($id, $sth, $sortOrder, $poListPtr, $poPtr, $poID) ;
1105 my $dbh = $self->{dbh} ;
1106  
1107 $DEBUG and carp " ->[insertHtMarkerCollection] $hmc." ;
1108  
1109 # Object data
1110 $id = $self->_insertObjectData($hmc) ;
1111 # HtMarkerCollection fields
1112 $sth = $dbh->prepare( "insert into HtMarkerCollection
1113 (hmcID, distanceUnits)
1114 values (?, ?)" ) ;
1115 $sth->execute($id, $hmc->field("distanceUnits")) ;
1116 $sth->finish() ;
1117 # HMCPolyObj fields
1118 $sortOrder = 1 ;
1119 $poListPtr = $hmc->field("Markers") ;
1120 $sth = $dbh->prepare( "insert into HMCPolyObj
1121 (hmcID, poID, sortOrder, distance)
1122 values (?, ?, ?, ?)" ) ;
1123 foreach $poPtr (@$poListPtr) {
1124 if ( defined($$poPtr{id}) ) {
1125 $poID = $$poPtr{id} ;
1126 } else {
1127 $poID = $self->_getIDByImportID($$poPtr{importID}) ;
1128 }
1129 if ( ! defined $poID) {
1130 carp " ->[insertHtMarkerCollection] Can't find an ID for Marker $$poPtr{name}" ;
1131 return(undef) ;
1132 }
1133 $sth->execute($id, $poID, $sortOrder, $$poPtr{distToNext}) ;
1134 $sortOrder++ ;
1135 }
1136 $sth->finish() ;
1137  
1138 $DEBUG and carp " ->[insertHtMarkerCollection] End." ;
1139  
1140 return($id) ;
1141 }
1142  
1143 =head2 insertHaplotype
1144  
1145 Function : Insert (create) a Genetics::Object::Haplotype object to the database.
1146 Argument : A Genetics::Object::Haplotype object.
1147 Returns : The id of the inserted object.
1148 Scope : Public
1149 Comments :
1150  
1151 =cut
1152  
1153 sub insertHaplotype {
1154 my($self, $ht) = @_ ;
1155 my($id, $sth, $sthA, $hmcPtr, $hmcID, $sortOrder, $alleleListPtr, $allelePtr, $poID, $alleleID) ;
1156 my $dbh = $self->{dbh} ;
1157  
1158 $DEBUG and carp " ->[insertHaplotype] $ht." ;
1159  
1160 # Object data
1161 $id = $self->_insertObjectData($ht) ;
1162 # Haplotype fields
1163 $sth = $dbh->prepare( "insert into Haplotype
1164 (haplotypeID, hmcID)
1165 values (?, ?)" ) ;
1166 $hmcPtr = $ht->field("MarkerCollection") ;
1167 if ( defined($$hmcPtr{id}) ) {
1168 $hmcID = $$hmcPtr{id} ;
1169 } else {
1170 $hmcID = $self->_getIDByImportID($$hmcPtr{importID}) ;
1171 }
1172 if ( ! defined $hmcID) {
1173 carp " ->[insertHaplotype] Can't find ID for HtMarkerCollection $$hmcPtr{name}" ;
1174 return(undef) ;
1175 }
1176 $sth->execute($id, $hmcID) ;
1177 $sth->finish() ;
1178 # HaplotypeAllele fields
1179 $sortOrder = 1 ;
1180 $alleleListPtr = $ht->field("Alleles") ;
1181 $sth = $dbh->prepare( "insert into HaplotypeAllele
1182 (haplotypeID, alleleID, sortOrder)
1183 values (?, ?, ?)" ) ;
1184 $sthA = $dbh->prepare( "insert into Allele
1185 (alleleID, poID, name, type)
1186 values (?, ?, ?, ?)" ) ;
1187 foreach $allelePtr (@$alleleListPtr) {
1188 # First find the Marker ID...
1189 ( $poID ) = $dbh->selectrow_array( "select poID from HMCPolyObj
1190 where hmcID = '$hmcID' and
1191 sortOrder = '$sortOrder'") ;
1192 # ...then see if the Marker already has an Allele w/ the same name and type...
1193 ( $alleleID ) = $dbh->selectrow_array( "select alleleID from Allele
1194 where poID = '$poID' and
1195 name = '$$allelePtr{name}' and
1196 type = '$$allelePtr{type}'" ) ;
1197 if ( ! defined $alleleID) {
1198 # ...if not, create a new Allele
1199 $sthA->execute(undef, $poID, $$allelePtr{name}, $$allelePtr{type}) ;
1200 $alleleID = $sthA->{'mysql_insertid'} ;
1201 }
1202 $sth->execute($id, $alleleID, $sortOrder) ;
1203 $sortOrder++ ;
1204 }
1205 $sth->finish() ;
1206 $sthA->finish() ;
1207  
1208 $DEBUG and carp " ->[insertHaplotype] $ht." ;
1209  
1210 return($id) ;
1211 }
1212  
1213 =head2 insertDNASample
1214  
1215 Function : Insert (create) a Genetics::Object::DNASample object to the database.
1216 Argument : A Genetics::Object::DNASample object.
1217 Returns : The id of the inserted object.
1218 Scope : Public
1219 Comments :
1220  
1221 =cut
1222  
1223 sub insertDNASample {
1224 my($self, $sample) = @_ ;
1225 my($id, $sth, $subjPtr, $subjID, $gtListPtr, $gtPtr, $gtID) ;
1226 my $dbh = $self->{dbh} ;
1227  
1228 $DEBUG and carp " ->[insertDNASample] $sample." ;
1229  
1230 # Object data
1231 $id = $self->_insertObjectData($sample) ;
1232 # Sample fields
1233 $sth = $dbh->prepare( "insert into Sample
1234 (sampleID, type, dateCollected)
1235 values (?, ?, ?)" ) ;
1236 $sth->execute($id, "DNA", $sample->field("dateCollected")) ;
1237 $sth->finish() ;
1238 $sth = $dbh->prepare( "insert into DNASample
1239 (dnaSampleID, amount, amountUnits,
1240 concentration, concUnits)
1241 values (?, ?, ?, ?, ?)" ) ;
1242 $sth->execute($id, $sample->field("amount"), $sample->field("amountUnits"), $sample->field("concentration"), $sample->field("concUnits")) ;
1243 $sth->finish() ;
1244 # SubjectSample data
1245 if ( defined ($subjPtr = $sample->field("Subject")) ) {
1246 if ( defined($$subjPtr{id}) ) {
1247 $subjID = $$subjPtr{id} ;
1248 } else {
1249 $subjID = $self->_getIDByImportID($$subjPtr{importID}) ;
1250 }
1251 if (defined $subjID) {
1252 $sth = $dbh->prepare( "insert into SubjectSample
1253 (subjectID, sampleID)
1254 values (?, ?)" ) ;
1255 $sth->execute($subjID, $id) ;
1256 $sth->finish() ;
1257 }
1258 }
1259 # SampleGenotype data
1260 if ( defined ($gtListPtr = $sample->field("Genotypes")) ) {
1261 foreach $gtPtr (@$gtListPtr) {
1262 if ( defined($$gtPtr{id}) ) {
1263 $gtID = $$gtPtr{id} ;
1264 } else {
1265 $gtID = $self->_getIDByImportID($$gtPtr{importID}) ;
1266 }
1267 if (defined $gtID) {
1268 $sth = $dbh->prepare( "insert into SampleGenotype
1269 (sampleID, gtID)
1270 values (?, ?)" ) ;
1271 $sth->execute($id, $gtID) ;
1272 $sth->finish() ;
1273 }
1274 }
1275 }
1276  
1277 $DEBUG and carp " ->[insertDNASample] End." ;
1278  
1279 return($id) ;
1280 }
1281  
1282 =head2 insertTissueSample
1283  
1284 Function : Insert (create) a Genetics::Object::TissueSample object to the database.
1285 Argument : A Genetics::Object::TissueSample object.
1286 Returns : The id of the inserted object.
1287 Scope : Public
1288 Comments :
1289  
1290 =cut
1291  
1292 sub insertTissueSample {
1293 my($self, $sample) = @_ ;
1294 my($id, $sth, $dsListPtr, $dsPtr, $dsID, $subjPtr, $subjID, $gtListPtr, $gtPtr, $gtID) ;
1295 my $dbh = $self->{dbh} ;
1296  
1297 $DEBUG and carp " ->[insertTissueSample] $sample." ;
1298  
1299 # Object data
1300 $id = $self->_insertObjectData($sample) ;
1301 # Sample fields
1302 $sth = $dbh->prepare( "insert into Sample
1303 (sampleID, type, dateCollected)
1304 values (?, ?, ?)" ) ;
1305 $sth->execute($id, "Tissue", $sample->field("dateCollected")) ;
1306 $sth->finish() ;
1307 $sth = $dbh->prepare( "insert into TissueSample
1308 (tissueSampleID, tissue, amount, amountUnits)
1309 values (?, ?, ?, ?)" ) ;
1310 $sth->execute($id, $sample->field("tissue"), $sample->field("amount"), $sample->field("amountUnits")) ;
1311 $sth->finish() ;
1312 # TissueDNASample data
1313 if ( defined ($dsListPtr = $sample->field("DNASamples")) ) {
1314 foreach $dsPtr (@$dsListPtr) {
1315 if ( defined($$dsPtr{id}) ) {
1316 $dsID = $$dsPtr{id} ;
1317 } else {
1318 $dsID = $self->_getIDByImportID($$dsPtr{importID}) ;
1319 }
1320 if (defined $dsID) {
1321 $sth = $dbh->prepare( "insert into TissueDNASample
1322 (tissueSampleID, dnaSampleID)
1323 values (?, ?)" ) ;
1324 $sth->execute($id, $dsID) ;
1325 $sth->finish() ;
1326 }
1327 }
1328 }
1329 # SampleSubject data
1330 if ( defined ($subjPtr = $sample->field("Subject")) ) {
1331 if ( defined($$subjPtr{id}) ) {
1332 $subjID = $$subjPtr{id} ;
1333 } else {
1334 $subjID = $self->_getIDByImportID($$subjPtr{importID}) ;
1335 }
1336 if (defined $subjID) {
1337 $sth = $dbh->prepare( "insert into SubjectSample
1338 (subjectID, sampleID)
1339 values (?, ?)" ) ;
1340 $sth->execute($subjID, $id) ;
1341 $sth->finish() ;
1342 }
1343 }
1344 # SampleGenotype data
1345 if ( defined ($gtListPtr = $sample->field("Genotypes")) ) {
1346 foreach $gtPtr (@$gtListPtr) {
1347 if ( defined($$gtPtr{id}) ) {
1348 $gtID = $$gtPtr{id} ;
1349 } else {
1350 $gtID = $self->_getIDByImportID($$gtPtr{importID}) ;
1351 }
1352 if (defined $gtID) {
1353 $sth = $dbh->prepare( "insert into SampleGenotype
1354 (sampleID, gtID)
1355 values (?, ?)" ) ;
1356 $sth->execute($id, $gtID) ;
1357 $sth->finish() ;
1358 }
1359 }
1360 }
1361  
1362 $DEBUG and carp " ->[insertTissueSample] End." ;
1363  
1364 return($id) ;
1365 }
1366  
1367 =head2 insertMap
1368  
1369 Function : Insert (create) a Genetics::Object::Map object to the database.
1370 Argument : A Genetics::Object::Map object.
1371 Returns : The id of the inserted object.
1372 Scope : Public
1373 Comments :
1374  
1375 =cut
1376  
1377 sub insertMap {
1378 my($self, $map) = @_ ;
1379 my($id, $sth, $sortOrder, $omeListPtr, $omePtr, $soPtr, $soID, $omeName, $orgPtr,
1380 $genusSpecies, $orgID) ;
1381 my $dbh = $self->{dbh} ;
1382  
1383 $DEBUG and carp " ->[insertMap] $map." ;
1384  
1385 # Object data
1386 $id = $self->_insertObjectData($map) ;
1387 # Map fields
1388 $sth = $dbh->prepare( "insert into Map
1389 (mapID, chromosome, organismID, orderingMethod, distanceUnits)
1390 values (?, ?, ?, ?, ?)" ) ;
1391 $sth->execute($id, $map->field("chromosome"), undef, $map->field("orderingMethod"),
1392 $map->field("distanceUnits")) ;
1393 $sth->finish() ;
1394 # OrderedMapElement fields
1395 $sortOrder = 1 ;
1396 $sth = $dbh->prepare( "insert into OrderedMapElement
1397 (omeID, mapID, soID, sortOrder,name, distance, comment)
1398 values (?, ?, ?, ?, ?, ?, ?)" ) ;
1399 $omeListPtr = $map->field("OrderedMapElements") ;
1400 foreach $omePtr (@$omeListPtr) {
1401 $soPtr = $$omePtr{SeqObj} ;
1402 if ( defined($$soPtr{id}) ) {
1403 $soID = $$soPtr{id} ;
1404 } else {
1405 $soID = $self->_getIDByImportID($$soPtr{importID}) ;
1406 }
1407 if (defined $soID) {
1408 if ( ! defined ($omeName = $$soPtr{name}) ) {
1409 ( $omeName ) = $dbh->selectrow_array( "select name from Object
1410 where id = $soID" )
1411 }
1412 $sth->execute(undef, $id, $soID, $sortOrder, $omeName, $$omePtr{distance}, $$omePtr{comment}) ;
1413 $sortOrder++ ;
1414 } else {
1415 carp " ->[insertMap] Can't find an ID for SequenceObject $$soPtr{name}" ;
1416 return(undef) ;
1417 }
1418 }
1419 $sth->finish() ;
1420 # Organism data
1421 if (defined ($orgPtr = $map->field("Organism")) ) {
1422 $orgID = $self->_getOrganismID($orgPtr) ;
1423 $sth = $dbh->prepare( "update Map
1424 set organismID = ?
1425 where mapID = ?" ) ;
1426 $sth->execute($orgID, $id) ;
1427 $sth->finish() ;
1428 }
1429  
1430 $DEBUG and carp " ->[insertMap] End." ;
1431  
1432 return($id) ;
1433 }
1434  
1435 =head1 Private methods
1436  
1437 =head2 _insertObjectData
1438  
1439 Function : Insert data common to all Genetics::Object objects to the database.
1440 Argument : A GNOM-API object and a Genetics::Object object.
1441 Returns : The Object.id of the row inserted into the Object table.
1442 Scope : Private
1443 Called by : The various insertObjectSubClass methods.
1444  
1445 =cut
1446  
1447 sub _insertObjectData {
1448 my($self, $obj) = @_ ;
1449 my($sth, $sth2, $name, $importID, $id, $objType, $date, $comment, $url, $naListPtr, $naPtr,
1450 $contactName, $contactID, $newContactID, $contactPtr, $xRefListPtr, $xRefPtr,
1451 $kwListPtr, $kwvPtr, $kwtName, $dataType, $descr, $value, $valueFieldName,
1452 $kwtID, $newKwtID) ;
1453 my $dbh = $self->{dbh} ;
1454
1455 $DEBUG and carp " ->[_insertObjectData] $obj" ;
1456  
1457 $name = $obj->field("name") ;
1458 # We are saving a new object, so the id is generated by the database. Insert
1459 # the existing id as a Keyword.
1460 $importID = $obj->field("importID") ;
1461 $objType = ref $obj ;
1462 $objType =~ s/.*::// ;
1463 $date = $obj->field("dateCreated") ;
1464 $comment = $obj->field("comment") ;
1465 $url = $obj->field("url") ;
1466 # Insert Object fields
1467 $sth = $dbh->prepare( "insert into Object
1468 (name, id, objType, dateCreated, dateModified,
1469 comment, url, contactID)
1470 values (?, ?, ?, ?, ?, ?, ?, ?)" ) ;
1471 $sth->execute($name, undef, $objType, $date, undef, $comment, $url, undef) ;
1472 $id = $sth->{'mysql_insertid'} ;
1473 $sth->finish() ;
1474 # Insert NameAlias fields
1475 if ( defined ($naListPtr = $obj->field("NameAliases")) ) {
1476 $sth = $dbh->prepare( "insert into NameAlias
1477 (objID, name, contactID)
1478 values (?, ?, ?) " ) ;
1479 $sth2 = $dbh->prepare( "insert into Contact
1480 (contactID, name)
1481 values (?, ?)" ) ;
1482 foreach $naPtr (@$naListPtr) {
1483 if ( defined ($contactName = $$naPtr{contactName}) ) {
1484 # See if there is already a Contact with the same name...
1485 ( $contactID ) = $dbh->selectrow_array( "select contactID from Contact
1486 where name = '$contactName'" ) ;
1487 if ( defined ($contactID) ) {
1488 # ...if so, insert a NamesAlias referencing the existing Contact
1489 $sth->execute($id, $$naPtr{name}, $contactID) ;
1490 } else {
1491 # ...if not, create a new Contact...
1492 $sth2->execute(undef, $contactName) ;
1493 $newContactID = $sth2->{'mysql_insertid'} ;
1494 # ...then insert NameAlias referencing the new Contact
1495 $sth->execute($id, $$naPtr{name}, $newContactID) ;
1496 }
1497 } else {
1498 # Insert a NameAlias w/o a Contact reference
1499 $sth->execute($id, $$naPtr{name}, undef) ;
1500 }
1501 }
1502 $sth->finish() ;
1503 $sth2->finish() ;
1504 }
1505 # Insert Contact fields
1506 if ( defined ($contactPtr = $obj->field("Contact")) ) {
1507 # Need to check for an Address and insert it if there is one
1508 $contactName = $$contactPtr{name} ;
1509 # See if there is already a Contact with the same name
1510 ( $contactID ) = $dbh->selectrow_array( "select contactID from Contact
1511 where name = '$contactName'" ) ;
1512 if ( defined ($contactID) ) {
1513 # If so, just add the contactID to Object
1514 $sth = $dbh->prepare( "update Object
1515 set contactID = ?
1516 where id = ?" ) ;
1517 $sth->execute($contactID, $id) ;
1518 $sth->finish() ;
1519 } else {
1520 # If not, create a new Contact...
1521 $sth = $dbh->prepare( "insert into Contact
1522 (contactID, addressID, name, organization, comment)
1523 values (?, ?, ?, ?, ?)" ) ;
1524 $sth->execute(undef, undef, $contactName, $$contactPtr{organization}, $$contactPtr{comment}) ;
1525 $newContactID = $sth->{'mysql_insertid'} ;
1526 $sth->finish() ;
1527 # then add the contactID to Object
1528 $sth = $dbh->prepare( "update Object
1529 set contactID = ?
1530 where id = ?" ) ;
1531 $sth->execute($newContactID, $id) ;
1532 $sth->finish() ;
1533 }
1534 }
1535 # Insert DBXReferences fields
1536 if ( defined ($xRefListPtr = $obj->field("DBXReferences")) ) {
1537 $sth = $dbh->prepare( "insert into DBXReference
1538 (dbXRefID, objID, accessionNumber, databaseName,
1539 schemaName, comment)
1540 values (?, ?, ?, ?, ?, ?)" ) ;
1541 foreach $xRefPtr (@$xRefListPtr) {
1542 $sth->execute(undef, $id, $$xRefPtr{accessionNumber}, $$xRefPtr{databaseName}, $$xRefPtr{schemaName}, $$xRefPtr{comment}) ;
1543 }
1544 $sth->finish() ;
1545 }
1546 # Insert Keyword and KeywordType fields
1547 # First insert the ImportID Keyword
1548 $sth = $dbh->prepare( "insert into Keyword
1549 (keywordID, objID, keywordTypeID, stringValue)
1550 values (?, ?, ?, ?) " ) ;
1551 $sth->execute(undef, $id, 1, $importID) ;
1552 $sth->finish() ;
1553 # Insert other Keywords
1554 if ( defined ($kwListPtr = $obj->field("Keywords")) ) {
1555 foreach $kwvPtr (@$kwListPtr) {
1556 $kwtName = $$kwvPtr{name} ;
1557 $dataType = $$kwvPtr{dataType} ;
1558 $descr = $$kwvPtr{description} ;
1559 $value = $$kwvPtr{value} ;
1560 # Need to do this b/c there is only 1 value but there are 4 possible columns in
1561 # which to put it:
1562 $valueFieldName = (lc $dataType) . "Value" ;
1563 ( $kwtID ) = $dbh->selectrow_array( "select keywordTypeID from KeywordType
1564 where name = '$kwtName'
1565 and dataType = '$dataType'" ) ;
1566 if (defined $kwtID) {
1567 # Just add the value to Keyword
1568 $sth = $dbh->prepare( "insert into Keyword
1569 (keywordID, objID, keywordTypeID, $valueFieldName)
1570 values (?, ?, ?, ?)" ) ;
1571 $sth->execute(undef, $id, $kwtID, $value) ;
1572 $sth->finish() ;
1573 } else {
1574 # Insert new KeywordType...
1575 $sth = $dbh->prepare( "insert into KeywordType
1576 (keywordTypeID, name, dataType, description)
1577 values (?, ?, ?, ?)" ) ;
1578 $sth->execute(undef, $kwtName, $dataType, $descr) ;
1579 $newKwtID = $sth->{'mysql_insertid'} ;
1580 $sth->finish() ;
1581 # then add the value to Keyword, referencing the new KeywordType
1582 $sth = $dbh->prepare( "insert into Keyword
1583 (keywordID, objID, keywordTypeID, $valueFieldName)
1584 values (?, ?, ?, ?)" ) ;
1585 $sth->execute(undef, $id, $newKwtID, $value) ;
1586 $sth->finish() ;
1587 }
1588 }
1589 }
1590  
1591 $DEBUG and carp " ->[_insertObjectData] End. ID is $id." ;
1592  
1593 return($id) ;
1594 }
1595  
1596 =head2 _insertAssayAttrs
1597  
1598 Function : Insert AssayAttributes associated with a Genotype, AlleleCall or
1599 Phenotype.
1600 Arguments : Array reference to the list of AssayAttributes, scalar
1601 containing the type of object with which the AssayAttributes
1602 are associated, and another scalar containing the id of that
1603 object.
1604 Returns : N/A
1605 Scope : Private
1606 Called by :
1607 Comments :
1608  
1609 =cut
1610  
1611 sub _insertAssayAttrs {
1612 my($self, $aaListPtr, $type, $id) = @_ ;
1613 my($sth, $aaPtr, $aaName, $dataType, $descr, $value, $aaID,
1614 $linkFieldName, $valueFieldName) ;
1615 my $dbh = $self->{dbh} ;
1616  
1617 $DEBUG and carp " ->[_insertAssayAttrs] Start ($type)." ;
1618  
1619 foreach $aaPtr (@$aaListPtr) {
1620 $aaName = $$aaPtr{name} ;
1621 $dataType = $$aaPtr{dataType} ;
1622 $descr = $$aaPtr{description} ;
1623 $value = $$aaPtr{value} ;
1624
1625 ( $aaID ) = $dbh->selectrow_array( "select attrID from AssayAttribute
1626 where name = '$aaName'
1627 and dataType = '$dataType'" ) ;
1628 if ( ! defined $aaID) {
1629 # Insert new AssayAttribute...
1630 $sth = $dbh->prepare( "insert into AssayAttribute
1631 (attrID, name, dataType, description)
1632 values (?, ?, ?, ?)" ) ;
1633 $sth->execute(undef, $aaName, $dataType, $descr) ;
1634 $aaID = $sth->{'mysql_insertid'} ;
1635 $sth->finish() ;
1636 }
1637 # Add the AttributeValue
1638 # Need to derive $linkFieldName: AttributeValues can be
1639 # associated with Gts/Pts or with AlleleCalls
1640 if ($type eq "AlleleCall") {
1641 $linkFieldName = "alleleCallID" ;
1642 } else {
1643 $linkFieldName = "objID" ;
1644 }
1645 # Need to derive $valueFieldName: there is only 1 value,
1646 # but there are 4 possible columns in which to put it:
1647 $valueFieldName = (lc $dataType) . "Value" ;
1648 $sth = $dbh->prepare( "insert into AttributeValue
1649 (attrValueID, $linkFieldName, attrID, $valueFieldName)
1650 values (?, ?, ?, ?)" ) ;
1651 $sth->execute(undef, $id, $aaID, $value) ;
1652 $sth->finish() ;
1653 }
1654
1655 $DEBUG and carp " ->[_insertAssayAttrs] End." ;
1656  
1657 return(1) ;
1658 }
1659  
1660 1;
1661