Genetics/Object.pm untested

filter: Statementstmt 0.0% 0 / 292 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 47 Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 525 CC 27 SCAR 29.2
CC
27
Cov
0%
_initialize
72.0
field
42.0
_generalHTMLParam
30.0
CRAP 18.5
1 # GenPerl module
2 #
3  
4 # POD documentation - main docs before the code
5  
6 =head1 NAME
7  
8 Genetics::Object
9  
10 =head1 SYNOPSIS
11  
12 A synopsis is not appropriate, as one would never normally instantiate
13 an instance of Genetics::Object.
14  
15 See the synopses for the individual subclasses.
16  
17 =head1 DESCRIPTION
18  
19 This is the base class for all GenPerl objects.
20  
21 For more information, see the GenPerl tutorial and the documentation for the
22 individual sub-classes. Also, see Genetics::API for a description of the GenPerl
23 database interface.
24  
25 =head1 LICENSE
26  
27 This program is free software; you can redistribute it and/or modify
28 it under the terms of the GNU General Public License as published by
29 the Free Software Foundation; either version 2 of the License, or
30 (at your option) any later version.
31  
32 This program is distributed in the hope that it will be useful,
33 but WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 GNU General Public License for more details.
36  
37 You should have received a copy of the GNU General Public License
38 along with this program; if not, write to the Free Software
39 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40  
41 =head1 FEEDBACK
42  
43 Currently, all feedback should be sent directly to the author.
44  
45 =head1 AUTHOR - Steve Mathias
46  
47 Email: mathias@genomica.com
48  
49 Phone: (720) 565-4029
50  
51 Address: Genomica Corporation
52 1745 38th Street
53 Boulder, CO 80301
54  
55 =head1 DETAILS
56  
57 The rest of the documentation describes each of the object variables and
58 methods. The names of internal variables and methods are preceded with an
59 underscore (_).
60  
61 =cut
62  
63 ##################
64 # #
65 # Begin the code #
66 # #
67 ##################
68  
69 package Genetics::Object ;
70  
71 BEGIN {
72 $ID = "Genetics::Object" ;
73 #$DEBUG = $main::DEBUG ;
74 $DEBUG = 0 ;
75 $DEBUG and $| = 1 ;
76 $DEBUG and warn "Debugging in $ID (v$VERSION) is on" ;
77 }
78  
79 =head1 Imported Packages
80  
81 strict Just to be anal
82 vars Global variables
83 Carp Error reporting
84 XML::Writer Used by generateXML methods
85 HTML::Template Used by asHTML methods
86  
87 Genetics::Cluster Subclass
88 Genetics::DNASample Subclass
89 Genetics::FrequencySource Subclass
90 Genetics::Genotype Subclass
91 Genetics::Haplotype Subclass
92 Genetics::HtMarkerCollection Subclass
93 Genetics::Kindred Subclass
94 Genetics::Map Subclass
95 Genetics::Marker Subclass
96 Genetics::Phenotype Subclass
97 Genetics::SNP Subclass
98 Genetics::StudyVariable Subclass
99 Genetics::Subject Subclass
100 Genetics::TissueSample Subclass
101  
102 =cut
103  
104 use strict;
105 use Carp ;
106 use XML::Writer ;
107 use HTML::Template ; # Comment this out for distribution. It's only used by the CGI app.
108  
109 use Genetics::Cluster ;
110 use Genetics::DNASample ;
111 use Genetics::FrequencySource ;
112 use Genetics::Genotype ;
113 use Genetics::Haplotype ;
114 use Genetics::HtMarkerCollection ;
115 use Genetics::Kindred ;
116 use Genetics::Map ;
117 use Genetics::Marker ;
118 use Genetics::Phenotype ;
119 use Genetics::SNP ;
120 use Genetics::StudyVariable ;
121 use Genetics::Subject ;
122 use Genetics::TissueSample ;
123  
124 require 5.004 ;
125  
126 use vars qw($ID $VERSION $DEBUG $AUTOLOAD
127 @OBJ_ATTRS @OBJ_REQD_ATTRS @OBJ_XML_ATTRS
128 $HTML_TEMPLATE_DIR) ;
129  
130 @OBJ_ATTRS = qw(name id importID dateCreated dateModified url comment
131 NameAliases Contact DBXReferences Keywords) ;
132  
133 @OBJ_REQD_ATTRS = qw(name) ; # an id OR importID is also required
134  
135 @OBJ_XML_ATTRS = qw(name id) ;
136  
137 $HTML_TEMPLATE_DIR = "/var/slm/work/GenPerl/templates" ;
138  
139  
140 =head1 Public methods
141  
142 =head2 new
143  
144 Function : Object constructor
145 Arguments : Class name and hash array
146 Returns : Blessed hash
147 Scope : Public
148 Called by : Main
149 Comments : Creates an empty hash, blesses it into the class name and calls
150 _initialize with the arguments passed
151  
152 =cut
153  
154 sub new {
155 my($pkg, %args) = @_ ;
156 my($self) = {} ;
157  
158 bless $self, ref($pkg) || $pkg ;
159 $DEBUG and carp "\n==>Creating new $ID object: $self" ;
160 $self->_initialize(%args) ;
161  
162 $DEBUG and carp "==>Successfully created new $ID object: $self" ;
163  
164 return($self) ;
165 }
166  
167 =head2 new
168  
169 Function : Return the object type: Subject, Genotype, etc.
170 Arguments : A Genetics::Object object.
171 Returns : Scalar
172 Scope : Public
173 Comments :
174  
175 =cut
176  
177 sub type {
178 my($self) = shift ;
179 my $pkg = ref($self) ;
180 my($type) = $pkg =~ /\.*::(\w+)/ ;
181
182 return $type ;
183 }
184  
185 =head2 field
186  
187 Function : Get or set specified field data
188 Argument : Field name and value (both optional)
189 Returns : Called with a single argument, this returns the value of the field
190 named by the argument. Called with tow argumetns, this sets the
191 value of the field named by the first argument to the value in the
192 second argument. Called with no arguments, this returns a list of
193 all field names.
194 Scope : Public
195 Called by :
196 Comments : As an alternative to using this method, field names can be used as
197 get/set methods (see AUTOLOAD).
198  
199 =cut
200  
201 sub field {
202 my($self, $field, $value) = @_ ;
203 my($class, $k, $v, @h) ;
204  
205 $class = ref $self ;
206  
207 @_ == 1 and return grep (/^[^_]/, sort keys %$self) ;
208 @_ == 2 and ! defined $self->{$field} and return undef ;
209 if (@_ == 3) {
210 if (not ref $value) {
211 $self->{$field} = $value ;
212 $DEBUG and ($value ne "") and carp " ->Setting $class attribute '$field' to '$value'" ;
213 } elsif (ref $value eq "ARRAY") {
214 $self->{$field} = $value ;
215 $DEBUG and carp " ->Setting $class attribute '$field' to [ ", join(", ", @$value), " ]" ;
216 } elsif (ref $value eq "HASH") {
217 $self->{$field} = $value ;
218 if ($DEBUG) {
219 while (($k,$v) = each %$value) {
220 push(@h, "$k => $v") ;
221 }
222 carp " ->Setting $class attribute '$field' to { ", join(", ", @h), " }" ;
223 }
224 } else {
225 carp " ->Value of $class attribute $field is a reference to an unsupported type" ;
226 }
227 }
228
229 return $self->{$field} ;
230 }
231  
232 =head2 print
233  
234 Function : Print out object in a raw format
235 Argument : N/A
236 Returns : N/A
237 Scope : Public
238 Called by : Main
239 Comments : This is mostly for debugging purposes.
240  
241 =cut
242  
243 sub print {
244 my($self) = @_ ;
245 my($field, $value, $printString) ;
246  
247 print "\n$self:\n" ;
248 foreach $field ($self->field()) {
249 $value = $self->field($field) ;
250 $printString = _attr2String($value) ;
251
252 printf "%-22s:\t%s\n", $field, $printString ;
253 }
254 print "\n" ;
255  
256 return 1 ;
257 }
258  
259 =head2 dump
260  
261 Function : Same as print, but returns a string instead of printing it.
262 Argument : N/A
263 Returns : Scalar text string.
264 Scope : Public
265 Called by : Main
266 Comments : This is mostly for debugging purposes.
267  
268 =cut
269  
270 sub dump {
271 my($self) = @_ ;
272 my($field, $value, $printString, $returnString) ;
273  
274 $returnString = "\n$self:\n" ;
275 foreach $field (sort $self->field()) {
276 $value = $self->field($field) ;
277 $printString = _attr2String($value) ;
278 $returnString .= sprintf "%-22s:\t%s\n", $field, $printString ;
279 }
280 $returnString .= "\n" ;
281  
282 return $returnString ;
283  
284 }
285  
286 =head2 printObjectXML
287  
288 Function : Generate and print XML common to all Genperl objects.
289 Argument : A Genetics::Object object and the XML::Writer object being used
290 to generate the XML.
291 Returns : N/A
292 Scope : Public Instance Method
293 Called by : printXML methods of Genetics::Object subclasses.
294 Comments :
295  
296 =cut
297  
298 sub printObjectXML {
299 my($self, $writer) = @_ ;
300 my($class, $attrName, %xmlAttr, $value, $hashPtr, $hashPtr2) ;
301
302 $class = ref $self ;
303 $class =~ s/.*::// ;
304
305 ## Object XML Attributes ##
306 foreach $attrName (@OBJ_XML_ATTRS) {
307 if (defined ($self->field($attrName))) {
308 $xmlAttr{$attrName} = $self->field($attrName) ;
309 }
310 }
311 $writer->startTag($class, %xmlAttr) ;
312  
313 ## Object XML Elements ##
314 # DateCreated
315 $writer->dataElement('DateCreated', $self->field('dateCreated')) ;
316 # CreatedBy
317 $hashPtr = $self->field('CreatedBy') ;
318 $writer->startTag('CreatedBy') ;
319 $writer->startTag('UserRef') ;
320 $writer->dataElement('Name', $$hashPtr{name}) ;
321 $writer->dataElement('ID', $$hashPtr{id}) ;
322 $writer->endTag('UserRef') ;
323 $writer->endTag('CreatedBy') ;
324 # DateModified
325 $writer->dataElement('DateModified', $self->field('dateModified')) ;
326 # ModifiedBy
327 $hashPtr = $self->field('ModifiedBy') ;
328 $writer->startTag('ModifiedBy') ;
329 $writer->startTag('UserRef') ;
330 $writer->dataElement('Name', $$hashPtr{name}) ;
331 $writer->dataElement('ID', $$hashPtr{id}) ;
332 $writer->endTag('UserRef') ;
333 $writer->endTag('ModifiedBy') ;
334 # DataContainer
335 if (defined ($value = $self->field('DataContainer'))) {
336 $writer->dataElement('DataContainer', $value) ;
337 }
338  
339 return(1) ;
340 }
341  
342 =head2 toStone
343  
344 Function :
345 Arguments :
346 Returns :
347 Example : toStone()
348 Scope : Public instance method
349 Comments :
350  
351 =cut
352  
353 sub toStone {
354 my($self) = @_ ;
355 my($stone, $field, $value, $valueAsString, %init) ;
356  
357 use Stone ;
358
359 foreach $field ($self->field()) {
360 $value = $self->field($field) ;
361 #$valueAsString = _attr2String($value) ;
362 $field = ucfirst $field ;
363 $init{$field} = $value ;
364 }
365 $stone = new Stone(%init) ;
366  
367 return $stone ;
368 }
369
370  
371 =head2 AUTOLOAD
372  
373 Function : Allow field names to be used as get/set methods instead of field().
374 Argument : Optional field value, in which case the field is set to this new
375 value (see field())
376 Returns : Field value
377 Scope : Public
378 Called by : Called automatically when an undefined method - in
379 Genetics::Object, or any of its subclasses - is invoked.
380  
381 =cut
382  
383 sub AUTOLOAD {
384 my $self = shift ;
385 my $class = ref $self ;
386 my($package, $methodName) = $AUTOLOAD =~ /(.+)::([^:]+)$/ ;
387  
388 no strict 'refs' ;
389 unless ( grep { $methodName eq $_ } (@OBJ_ATTRS, @{"${class}::ATTRS"}) ) {
390 croak "Can't locate object method \"$methodName\" via package \"$package\". \"$methodName\" must be a valid field in class \"$package\" in order to be invoked as a method in this way." ;
391 }
392 use strict 'refs' ;
393  
394 return $self->field($methodName, @_) ;
395 }
396  
397 =head2 DESTROY
398  
399 Function : Deallocate object storage
400 Argument : N/A
401 Returns : N/A
402 Scope : Public
403 Called by : Called automatically when the object goes out of scope
404 (ie the internal reference count in the symbol table is
405 zero). Can be called explicitly.
406  
407 =cut
408  
409 sub DESTROY {
410 my($self) = shift ;
411 my $pkg = ref $self ;
412  
413 $DEBUG and carp "\n==>Destroyed $ID object: $self" ;
414 }
415  
416 =head1 Private methods
417  
418 =head2 _initialize
419  
420 Function : Initialize object
421 Arguments : Hash array of attributes/values passed to new()
422 Returns : N/A
423 Scope : Private
424 Called by :
425 Comments :
426  
427 =cut
428  
429 sub _initialize {
430 my ($self, %args) = @_;
431 my ($class, $k, $v, $k2,$v2, @h) ;
432  
433 $class = ref $self ;
434  
435 #%{$self->{_arguments}} = %args ;
436 no strict 'refs' ;
437 while (($k,$v) = each %args) {
438 if (not ref $v) {
439 if ( grep { $k eq $_ } ( @OBJ_ATTRS, @{"${class}::ATTRS"} )) {
440 $self->{$k} = $v ;
441 $DEBUG and carp " ->Setting $class attribute '$k' to '$v'" ;
442 } else {
443 carp "<<- WARNING ->> Skipping invalid $class attribute '$k'" ;
444 next ;
445 }
446 } elsif (ref $v eq "ARRAY") {
447 if ( grep { $k eq $_ } ( @OBJ_ATTRS, @{"${class}::ATTRS"} )) {
448 $self->{$k} = $v ;
449 $DEBUG and carp " ->Setting $class attribute '$k' to [ ", join(", ", @$v), " ]" ;
450 } else {
451 carp "<<- WARNING ->> Skipping invalid $class attribute '$k'" ;
452 next ;
453 }
454 } elsif (ref $v eq "HASH") {
455 if ( grep { $k eq $_ } ( @OBJ_ATTRS, @{"${class}::ATTRS"} )) {
456 $self->{$k} = $v ;
457 if ($DEBUG) {
458 while (($k2,$v2) = each %$v) {
459 push(@h, "$k2 => $v2") ;
460 }
461 carp " ->Setting $class attribute '$k' to { ", join(", ", @h), " }" ;
462 }
463 } else {
464 carp "<<- WARNING ->> Skipping invalid $class attribute '$k'" ;
465 next ;
466 }
467 } else {
468 carp "<<- WARNING ->> Value of $class attribute $k is a reference to an unsupported type" ;
469 }
470 }
471 use strict 'refs' ;
472 $self->_setDefaults() ;
473 $self->_verifyRequired() ;
474
475 $DEBUG and carp ">=>Completed initialization of object: $self" ;
476  
477 return(1) ;
478 }
479  
480 =head2 _setDefaults
481  
482 Function : Set default attribute values
483 Arguments :
484 Returns : N/A
485 Scope : Private
486 Called by : Genetics::Object->_initialize
487 Comments :
488  
489 =cut
490  
491 sub _setDefaults {
492 my ($self) = @_;
493 my($class, $k, $v) ;
494  
495 $class = ref $self ;
496 no strict 'refs' ;
497 while ( ($k,$v) = each %{"${class}::DEFAULTS"} ) {
498 unless (defined $self->field($k) && $self->field($k) ne "") {
499 $self->field($k, $v) ;
500 $DEBUG and carp " ->Setting $ID field '$k' to default value '$v'" ;
501 }
502 }
503 use strict 'refs' ;
504  
505 $DEBUG and carp "==>Completed setting default attributes for object: $self" ;
506
507 return(1) ;
508 }
509  
510 =head2 _verifyRequired
511  
512 Function : Verify that required attributes are set
513 Arguments : N/A
514 Returns : N/A
515 Scope : Private
516 Called by : Genetics::Object->_initialize()
517 Comments : Verify that required fields have values.
518  
519 =cut
520  
521 sub _verifyRequired {
522 my ($self) = @_ ;
523 my ($class, $field, $key, $id, $dumpStr) ;
524
525 # An Object must have either an id or an importID
526 unless ( (defined ($id = $self->field("id")) && $id ne "" ) or
527 ((defined ($id = $self->field("importID")) && $id ne "" )) ) {
528 croak "<<- FATAL ->> Required attribute id/importID is not defined for object $self" ;
529 }
530  
531 $class = ref $self ;
532 no strict 'refs' ;
533 foreach $field ( @OBJ_REQD_ATTRS, @{"${class}::REQD_ATTRS"} ) {
534 unless (defined $self->field($field) && $self->field($field) ne "") {
535 $dumpStr = $self->dump() ;
536 croak "<<- FATAL ->> Required attribute/relationship '$field' is not defined for object: $dumpStr" ;
537 }
538 }
539 use strict 'refs' ;
540
541 $DEBUG and carp "==>Completed verification of object: $self" ;
542
543 return(1) ;
544 }
545  
546 =head2 _generalHTMLParam
547  
548 Function : Generate parameter hash, for passing to HTML::Template,
549 containing data common to all Genperl objects.
550 Argument : A Genetics::Object object and a reference to a parameter hash.
551 Returns : N/A
552 Scope : Private Instance Method
553 Called by : asHTML methods of Genetics::Object subclasses.
554 Comments :
555  
556 =cut
557  
558 sub _generalHTMLParam {
559 my($self, $paramPtr) = @_ ;
560 my($datem, $y, $m, $d, $h, $p, $s, $naListPtr, $naPtr, @nameAliases,
561 $dbxrListPtr, $dbxrPtr, @dbXRefs, $kwListPtr, $kwPtr, @keywords) ;
562  
563 $$paramPtr{NAME} = $self->name() ;
564 $$paramPtr{ID} = $self->id() ;
565 $$paramPtr{DATECREATED} = $self->dateCreated() ;
566 if ($datem = $self->dateModified()) {
567 ($y,$m,$d,$h,$p,$s) = $datem =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ ;
568 $$paramPtr{DATEMODIFIED} = $self->dateModified("$y-$m-$d $h:$p:$s") ;
569 }
570 $self->url() and $$paramPtr{URL} = $self->url() ;
571 $self->comment() and $$paramPtr{COMMENT} = $self->comment() ;
572 if ($naListPtr = $self->NameAliases()) {
573 foreach $naPtr (@$naListPtr) {
574 push @nameAliases, "$$naPtr{name}($$naPtr{contactName})" ;
575 }
576 }
577 $$paramPtr{NAMEALIASES} = join(", ", @nameAliases) ;
578 $self->Contact() and $$paramPtr{CONTACT} = ${$self->Contact()}->{name} ;
579 if ($dbxrListPtr = $self->DBXReferences()) {
580 foreach $dbxrPtr (@$dbxrListPtr) {
581 push @dbXRefs, "$$dbxrPtr{accessionNumber }:$$dbxrPtr{databaseName}" ;
582 }
583 }
584 $$paramPtr{DBXREFS} = join(", ", @dbXRefs) ;
585 if ($kwListPtr = $self->Keywords()) {
586 foreach $kwPtr (@$kwListPtr) {
587 push @keywords, "$$kwPtr{name}=$$kwPtr{value}" ;
588 }
589 }
590 $$paramPtr{KEYWORDS} = join(", ", @keywords) ;
591  
592 return 1 ;
593 }
594  
595 =head2 _attr2String
596  
597 Function : Convert a pointer to a complex attribute value into a string
598 for printing.
599 Arguments : N/A
600 Returns : N/A
601 Scope : Private Class Method
602 Called by : print(). _instantiate() and field() also call this method when
603 the debug flag is set.
604 Comments : Recurses if nested pointers exist in the data structure.
605  
606 =cut
607  
608 sub _attr2String {
609 my($value) = @_ ;
610 my($str, $k, $v, @data) ;
611  
612 if (not defined $value) {
613 $str = "" ;
614 } elsif (not ref $value) {
615 $str = $value ;
616 } elsif (ref $value eq "ARRAY") {
617 foreach $v (@$value) {
618 $str = _attr2String($v) ;
619 push(@data, $str)
620 }
621 $str = "[ " . join(", ", @data) . " ]" ;
622
623 } elsif (ref $value eq "HASH") {
624 @data = () ;
625 while (($k,$v) = each %$value) {
626 $str = _attr2String($v) ;
627 push(@data, "$k => $str") ;
628 }
629 $str = "{ " . join(", ", @data) . " }" ;
630 } else {
631 $str = "Reference to an unsupported type" ;
632 }
633  
634 return($str) ;
635 }
636  
637 1;
638