Genetics/API.pm untested

filter: Statementstmt 0.0% 0 / 169 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 313 CC 10 SCAR 19.0
CC
10
Cov
0%
_getIDByImportID
20.0
_getOrganismID
20.0
getTypeByID
6.0
CRAP 6.7
1 # GenPerl module
2 #
3  
4 # POD documentation - main docs before the code
5  
6 =head1 NAME
7  
8 Genetics::API
9  
10 =head1 SYNOPSIS
11  
12 use Genetics::API ;
13  
14 $api = new Genetics::API(DSN => {driver => "mysql",
15 host => $Host,
16 database => $Database},
17 user => $UserName,
18 password => $Password) ;
19  
20 $sv = $api->getObject({NAME => "Aff"}) ;
21  
22 @affSubjects = $api->getSubjectsByPhenotype($sv, 2) ;
23 $affCluster = $api->createCluster("HT Affecteds", \@affSubjects) ;
24 $api->insertCluster($affCluster) ;
25 @unaffSubjects = $api->getSubjectsByPhenotype($sv, 1) ;
26 $unaffCluster = $api->createCluster("Normals", \@unaffSubjects) ;
27 $api->insertCluster($unaffCluster) ;
28  
29 $marker = $api->getObject({TYPE => "Marker", NAME => "agtT174M"}) ;
30  
31 $api->chiSquareAssocTest(
32 MARKER => $marker,
33 SC1 => $affCluster,
34 SC2 => $unaffCluster,
35 ALLELETYPE => "Nucleotide",
36 ) ;
37  
38 See also the GenPerl Tutorial document.
39  
40 =head1 DESCRIPTION
41  
42 This module provides an API for interfacing with genperl objects.
43  
44 An instance of Genetics::API must be instantiated in order to interact with
45 GenPerl objects in a database (or to access any other API methods, for that
46 matter). The parameters passed to the Genetics::API constructor are the database
47 connection parameters. These parameters are passed directly to DBI->connect for
48 the creation of a database handle.
49  
50 The GenPerl API functionality is separated into the following packages.
51  
52 The Genetics::API package contains general API methods that do not fit anywhere
53 else right now. This is the only package that needs to be imported into your
54 programs.
55  
56 Teh Genetics::API::DB::* packages contain methods for managing the persistance
57 of GenPerl objects in a relational database.
58  
59 The Genetics::API::Analysis package contains methods for the analysis of data
60 contained in GenPerl objects.
61  
62 =head1 LICENSE
63  
64 This program is free software; you can redistribute it and/or modify
65 it under the terms of the GNU General Public License as published by
66 the Free Software Foundation; either version 2 of the License, or
67 (at your option) any later version.
68  
69 This program is distributed in the hope that it will be useful,
70 but WITHOUT ANY WARRANTY; without even the implied warranty of
71 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72 GNU General Public License for more details.
73  
74 You should have received a copy of the GNU General Public License
75 along with this program; if not, write to the Free Software
76 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
77  
78 =head1 FEEDBACK
79  
80 Currently, all feedback should be sent directly to the author.
81  
82 =head1 AUTHOR - Steve Mathias
83  
84 Email: mathias@genomica.com
85  
86 Phone: (720) 565-4029
87  
88 Address: Genomica Corporation
89 1745 38th Street
90 Boulder, CO 80301
91  
92 =head1 DETAILS
93  
94 The rest of the documentation describes each of the object variables and
95 methods. The names of internal variables and methods are preceded with an
96 underscore (_).
97  
98 =cut
99  
100 package Genetics::API ;
101  
102 BEGIN {
103 $ID = "Genetics::API" ;
104 #$DEBUG = $main::DEBUG ;
105 $DEBUG = 0 ;
106 $DEBUG and $| = 1 ;
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 DBI Database interface
116 Genetics::API::Insert
117 Genetics::API::Read
118 Genetics::API::Update
119 Genetics::API::DB::Query ;
120 Genetics::API::Delete
121 Genetics::API::Analysis Analysis functions
122 Genetics::API::Analysis::Linkage Linkage analysis functions
123 Genetics::Object GenPerl object modules
124  
125 =cut
126  
127 use strict ;
128 use vars qw(@ISA @EXPORT @EXPORT_OK $ID $DEBUG) ;
129 use Carp ;
130 use Exporter ;
131  
132 use DBI ;
133  
134 use Genetics::API::DB::Insert ;
135 use Genetics::API::DB::Read ;
136 use Genetics::API::DB::Update ;
137 use Genetics::API::DB::Delete ;
138 use Genetics::API::DB::Query ;
139 use Genetics::API::Analysis ;
140 use Genetics::API::Analysis::Linkage ;
141  
142 use Genetics::Object ;
143  
144 require 5.004 ;
145  
146 @ISA = qw(Exporter AutoLoader) ;
147 @EXPORT = qw() ;
148 @EXPORT_OK = qw();
149  
150 =head1 Public Methods
151  
152 =head2 new
153  
154 Function : Object constructor
155 Arguments : Class name and hash array containing initialization arguments.
156 Returns : Blessed hash
157 Scope : Public
158 Called by : Main
159 Comments : Creates an empty hash, blesses it into the class name and calls
160 _initialize with the arguments passed
161  
162 =cut
163  
164 sub new {
165 my($pkg, %args) = @_ ;
166  
167 my($self) = {} ;
168 bless $self, ref($pkg) || $pkg ;
169  
170 $DEBUG and carp "\n==>Creating new $ID object: $self" ;
171  
172 $self->_initialize(%args) or croak "FATAL ERROR: $ID new() failed to initialize object: $self" ;
173  
174 $DEBUG and carp "==>Successfully created new $ID object: $self" ;
175  
176 return($self) ;
177 }
178  
179 =head2 getDBH
180  
181 Function : Return the DBI database handle associated with an instance
182 of Genetics::API.
183 Arguments : Genetics::API instance.
184 Returns : DBI database handle object.
185 Scope : Public
186 Called by : Main
187 Comments :
188  
189 =cut
190  
191 sub getDBH {
192 my($self) = @_ ;
193  
194 return $self->{dbh} ;
195 }
196  
197 =head2 today
198  
199 Function : Generate a date string, corresponding to the current date-time,
200 suitable for the dateCreated created field.
201 Arguments : N/A
202 Returns : String
203 Example : today()
204 Scope : Public class method
205 Comments : The format of the date string generated is YYYY-MM-DD
206  
207 =cut
208  
209 sub today {
210 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time) ;
211 $year += 1900 ;
212 $mon++ ;
213 ($mon < 10) and ($mon = "0" . $mon) ;
214 ($mday < 10) and ($mday = "0" . $mday) ;
215  
216 return("$year-$mon-$mday") ;
217 }
218  
219 =head2 getTypeByID
220  
221 Function : Return the object type corresponding to a database ID.
222 Arguments : A database id.
223 Returns : 1 on success or undef if an object witht he input id was not
224 successfully deleted.
225 Scope : Public
226 Comments : Only objects saved in a GenPerl database should have an id.
227  
228 =cut
229  
230 sub getTypeByID {
231 my($self, $id) = @_ ;
232 my($type) ;
233 my $dbh = $self->{dbh} ;
234  
235 ( $type ) = $dbh->selectrow_array("select objType from Object
236 where id = $id") ;
237 defined($type) ? return($type)
238 : return(undef) ;
239 }
240  
241 =head2 getImportID
242  
243 Function : Return the ImportID associated with an object.
244 Arguments : A Genetics::Object object.
245 Returns : Scalar containing the ImportID associated with the object, or
246 undef if one does not exist.
247 Scope : Public
248 Comments : ImportIDs are stored as Keywords with keywordTypeID 1. ImportID
249 Keywords are created automatically (by _saveObjectData()) when a
250 new object is created.
251  
252 =cut
253  
254 sub getImportID {
255 my($self, $obj) = @_ ;
256 my($id, $importID) ;
257 my $dbh = $self->{dbh} ;
258
259 $id = $obj->field("id") ;
260 ( $importID ) = $dbh->selectrow_array( "select stringValue from Keyword
261 where keywordTypeID = 1
262 and objID = $id" ) ;
263  
264 unless ( defined($importID) and $importID ne "" ) {
265 carp " ->[getImportID] Can't find an ImportID associated with $obj!" ;
266 return(undef) ;
267 }
268  
269 return($importID) ;
270 }
271  
272 =head2 getKwTypesByObjectType
273  
274 Function : For a given object type, return the list of distinct KeywordType
275 names for which there are corresponding Keyword values in the
276 database.
277 Arguments : Scalar containing an object type.
278 Returns : Array of scalar text strings.
279 Scope : Public
280 Comments :
281  
282 =cut
283  
284 sub getKwTypesByObjectType {
285 my($self, $type) = @_ ;
286 my $dbh = $self->{dbh} ;
287 my $query = "select distinct kwt.name
288 from Object obj, Keyword kw, KeywordType kwt
289 where obj.objType = '$type' and
290 kwt.keywordTypeID = kw.keywordTypeID and
291 obj.id = kw.objID" ;
292 my($aroar, $arrRef, @kwTypes) ;
293  
294 $aroar = $dbh->selectall_arrayref($query) ;
295 foreach $arrRef (@$aroar) {
296 push @kwTypes, $$arrRef[0] ;
297 }
298  
299 return(@kwTypes) ;
300 }
301  
302 =head2 createCluster
303  
304 Function : Create a Genetics::Object::Cluster
305 Arguments : A string containing a name for the Cluster and an array
306 reference to a list of Genetics::Object objects.
307 Returns : A Genetics::Object::Cluster object.
308 Scope : Public
309 Comments : This method only creates a Cluster; it does not save it.
310  
311 =cut
312  
313 sub createCluster {
314 my($self, $name, $objListPtr) = @_ ;
315 my($obj, $type, %types, @contents, $today, $cluster) ;
316  
317 foreach $obj (@$objListPtr) {
318 $type = ref $obj ;
319 $type =~ s/^.*::// ;
320 $types{$type}++ ;
321 push(@contents, {name => $obj->name(), id => $obj->id()}) ;
322 }
323  
324 scalar(keys(%types)) > 1 and $type = "Mixed" ;
325 $today = today() ;
326  
327 $cluster = new Genetics::Cluster(name => $name,
328 importID => $$,
329 dateCreated => $today,
330 clusterType => $type,
331 Contents=> [ @contents ]
332 ) ;
333
334 return($cluster) ;
335 }
336  
337 =head2 writeMultiXML
338  
339 Function : Write a list of objects out in well-formed GnomML XML format.
340 Argument : Array reference to a list of Genetics::Object objects.
341 Returns : N/A
342 Scope : Public
343 Called by :
344  
345 =cut
346  
347 sub writeMultiXML {
348 my($pkg, $objListPtr) = @_ ;
349 my($writer, $gnom, $class, $name, $id) ;
350  
351 $writer = new XML::Writer(DATA_MODE => 1,
352 DATA_INDENT => 1) ;
353 $writer->xmlDecl("UTF-8") ;
354 $writer->doctype('GNOMObjects', "-//Genomica Corp.//Genetic and genomic data objects//EN", "http://admin.genomica.com/mathias/xml/GnomML.dtd") ;
355 $writer->startTag('GNOMObjects') ;
356 $writer->dataElement('Version', "0.4") ;
357 foreach $gnom (@$objListPtr) {
358 #$gnom->print ;
359 $gnom->writeXML($writer) ;
360 }
361 $writer->endTag('GNOMObjects') ;
362 $writer->end() ;
363  
364 return(1) ;
365 }
366  
367 =head2 DESTROY
368  
369 Function : Deallocate object storage
370 Argument : N/A
371 Returns : N/A
372 Scope : Public
373 Called by : Called automatically when the object goes out of scope
374 (ie the internal reference count in the symbol table is
375 zero). Can be called explicitly.
376  
377 =cut
378  
379 sub DESTROY {
380 my($self) = shift ;
381 my $dbh = $self->{dbh} ;
382  
383 defined $dbh and $dbh->disconnect() ;
384  
385 $DEBUG and carp "\n==>Destroyed $ID object: $self" ;
386 }
387  
388 =head1 Private methods
389  
390 =head2 _initialize
391  
392 Function : Initialize object
393 Arguments : Hash array of attributes/values passed to new
394 Returns : N/A
395 Scope : Private
396 Called by :
397 Comments :
398  
399 =cut
400  
401 sub _initialize {
402 my ($self, %args) = @_;
403 my ($dsnPtr, $driver, $db, $host, $port, $socket, $dataSourceStr, $dbh) ;
404  
405 if ( defined($dsnPtr = $args{DSN}) ) {
406 #defined($driver = $$dsnPtr{driver}) or $driver = "mysql" ;
407 $driver = "mysql" ;
408 defined($db = $$dsnPtr{database}) or $db = "gp" ;
409 defined($host = $$dsnPtr{host}) or $host = "localhost" ;
410 defined($port = $$dsnPtr{port}) or $port = 3306 ;
411 defined($socket = $$dsnPtr{socket}) or $socket = "/tmp/mysql.sock" ;
412 $dataSourceStr = "DBI:$driver:database=$db;host=$host;port=$port;mysql_socket=$socket" ;
413 $dbh = DBI->connect($dataSourceStr,
414 $args{user},
415 $args{password},
416 {PrintError => 0, # Don't report errors via warn()
417 RaiseError => 1} # Do report errors via die()
418 ) or carp "Can't connect to database: $DBI::errstr" ;
419 $self->{dbh} = $dbh ;
420 }
421  
422 $DEBUG and carp "==>Completed initialization of object: $self" ;
423  
424 return(1) ;
425 }
426  
427 =head2 _getIDByImportID
428  
429 Function : Return an Object ID based on an ImportID Keyword.
430 Arguments : Scalar containing an ImportID.
431 Returns : Scalar containing an Object ID or undef if one does
432 not exist.
433 Scope : Private
434 Comments : ImportID Keywords are created by _saveObjectData().
435  
436 =cut
437  
438 sub _getIDByImportID {
439 my($self, $importID) = @_ ;
440 my($sth, $id, $extra) ;
441 my $dbh = $self->{dbh} ;
442
443 if ( ! defined($importID) ) {
444 carp " ->[_getIDByImportID] No ImportID!" ;
445 return(undef) ;
446 }
447 $sth = $dbh->prepare("select objID from Keyword
448 where keywordTypeID = 1
449 and stringValue = ?") ;
450 $sth->execute($importID) ;
451 ( $id ) = $sth->fetchrow_array() ;
452 if (defined $id) {
453 ( $extra ) = $sth->fetchrow_array() ; # Check for duplicates
454 if (defined $extra) {
455 carp " ->[_getIDByImportID] There are multiple objects with Keyword ImportID = $importID!" ;
456 $sth->finish() ;
457 return(undef) ;
458 }
459 } else {
460 $sth->finish() ;
461 return(undef) ;
462 }
463  
464 return($id) ;
465 }
466  
467 =head2 _getOrganismID
468  
469 Function : Return an Organism ID.
470 Arguments : Hash reference to a hash containing Organism data.
471 Returns : Scalar containing an Organism ID.
472 Scope : Private
473 Called by : saveSubject(), saveMarker(), saveSNP, saveMap(), updateSubject(),
474 updateMarker(), updateSNP, updateMap().
475 Comments : A new Organism is created if one matching the data in %$orgPtr
476 does not exist.
477  
478 =cut
479  
480 sub _getOrganismID {
481 my($self, $orgPtr) = @_ ;
482 my($sth, $orgID) ;
483 my $dbh = $self->{dbh} ;
484
485 if (defined $$orgPtr{subspecies}) {
486 if (defined$$orgPtr{strain}) {
487 $sth = $dbh->prepare( "select organismID from Organism
488 where genusSpecies = ?
489 and subspecies = ?
490 and strain = ?" ) ;
491 $sth->execute($$orgPtr{genusSpecies}, $$orgPtr{subspecies}, $$orgPtr{strain}) ;
492 ( $orgID ) = $sth->fetchrow_array() ;
493 $sth->finish() ;
494 } else {
495 $sth = $dbh->prepare( "select organismID from Organism
496 where genusSpecies = ?
497 and subspecies = ?" ) ;
498 $sth->execute($$orgPtr{genusSpecies}, $$orgPtr{subspecies}) ;
499 ( $orgID ) = $sth->fetchrow_array() ;
500 $sth->finish() ;
501 }
502 } else {
503 $sth = $dbh->prepare( "select organismID from Organism
504 where genusSpecies = ?" ) ;
505 $sth->execute($$orgPtr{genusSpecies}) ;
506 ( $orgID ) = $sth->fetchrow_array() ;
507 $sth->finish() ;
508 }
509 if ( ! defined $orgID) {
510 # Create new Organism...
511 $sth = $dbh->prepare( "insert into Organism
512 (organismID, genusSpecies, subspecies, strain)
513 values (?, ?, ?, ?)" ) ;
514 $sth->execute(undef, $$orgPtr{genusSpecies}, $$orgPtr{subspecies}, $$orgPtr{strain}) ;
515 $orgID = $sth->{'mysql_insertid'} ;
516 $sth->finish() ;
517 }
518  
519 return($orgID) ;
520 }
521  
522 1;
523  
524 __END__