File Coverage

blib/lib/Bio/Phylo/Util/CONSTANT.pm
Criterion Covered Total %
statement 49 49 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 13 13 100.0
pod 6 6 100.0
total 83 83 100.0


line stmt bran cond sub pod time code
1             package Bio::Phylo::Util::CONSTANT;
2 57     57   73020 use strict;
  57         163  
  57         1488  
3 57     57   256 use warnings;
  57         102  
  57         1373  
4 57     57   272 use base 'Exporter';
  57         105  
  57         5480  
5 57     57   332 use Scalar::Util 'blessed';
  57         99  
  57         3018  
6 57     57   3614 use Bio::Phylo::Util::Exceptions 'throw';
  57         117  
  57         2209  
7 57     57   14428 use Bio::Phylo::Util::CONSTANT::Int;
  57         140  
  57         8310  
8              
9             BEGIN {
10 57     57   161 our ( @EXPORT_OK, %EXPORT_TAGS );
11 57         506 @EXPORT_OK = qw(
12             _NONE_
13             _NODE_
14             _TREE_
15             _FOREST_
16             _TAXON_
17             _TAXA_
18             _CHAR_
19             _DATUM_
20             _MATRIX_
21             _MATRICES_
22             _SEQUENCE_
23             _ALIGNMENT_
24             _CHARSTATE_
25             _CHARSTATESEQ_
26             _MATRIXROW_
27             _PROJECT_
28             _ANNOTATION_
29             _DICTIONARY_
30             _DOMCREATOR_
31             _META_
32             _DESCRIPTION_
33             _RESOURCE_
34             _HTTP_SC_SEE_ALSO_
35             _DOCUMENT_
36             _ELEMENT_
37             _CHARACTERS_
38             _CHARACTER_
39             _SET_
40             _MODEL_
41             _OPERATION_
42             _DATATYPE_
43             looks_like_number
44             looks_like_object
45             looks_like_hash
46             looks_like_class
47             looks_like_instance
48             looks_like_implementor
49             _NS_OWL_
50             _NS_DC_
51             _NS_DCTERMS_
52             _NS_NEXML_
53             _NS_RDF_
54             _NS_RDFS_
55             _NS_XSI_
56             _NS_XSD_
57             _NS_XML_
58             _NS_TOL_
59             _NS_CDAO_
60             _NS_BIOPHYLO_
61             _NS_SKOS_
62             _NEXML_VERSION_
63             _PI_
64             _NS_PHYLOXML_
65             _NS_TB2PURL_
66             _NS_TNRS_
67             _NS_FIGTREE_
68             _NS_PHYLOMAP_
69             _NS_BIOVEL_
70             _NS_NHX_
71             _NS_DWC_
72             _NS_GBIF_
73             );
74 57         62826 %EXPORT_TAGS = (
75             'all' => [@EXPORT_OK],
76             'objecttypes' => [
77             qw(
78             _NONE_
79             _NODE_
80             _TREE_
81             _FOREST_
82             _TAXON_
83             _TAXA_
84             _CHAR_
85             _DATUM_
86             _MATRIX_
87             _MATRICES_
88             _SEQUENCE_
89             _ALIGNMENT_
90             _CHARSTATE_
91             _CHARSTATESEQ_
92             _MATRIXROW_
93             _PROJECT_
94             _ANNOTATION_
95             _DICTIONARY_
96             _DOMCREATOR_
97             _META_
98             _DESCRIPTION_
99             _RESOURCE_
100             _HTTP_SC_SEE_ALSO_
101             _DOCUMENT_
102             _ELEMENT_
103             _CHARACTERS_
104             _CHARACTER_
105             _SET_
106             _MODEL_
107             _OPERATION_
108             _DATATYPE_
109             )
110             ],
111             'functions' => [
112             qw(
113             looks_like_number
114             looks_like_object
115             looks_like_hash
116             looks_like_class
117             looks_like_instance
118             looks_like_implementor
119             )
120             ],
121             'namespaces' => [
122             qw(
123             _NS_OWL_
124             _NS_DC_
125             _NS_DCTERMS_
126             _NS_NEXML_
127             _NS_RDF_
128             _NS_RDFS_
129             _NS_XSI_
130             _NS_XSD_
131             _NS_XML_
132             _NS_TOL_
133             _NS_CDAO_
134             _NS_BIOPHYLO_
135             _NS_SKOS_
136             _NS_PHYLOXML_
137             _NS_TB2PURL_
138             _NS_TNRS_
139             _NS_FIGTREE_
140             _NS_PHYLOMAP_
141             _NS_BIOVEL_
142             _NS_NHX_
143             _NS_DWC_
144             _NS_GBIF_
145             )
146             ]
147             );
148             }
149              
150             # according to perlsub:
151             # "Functions with a prototype of () are potential candidates for inlining.
152             # If the result after optimization and constant folding is either a constant
153             # or a lexically-scoped scalar which has no other references, then it will
154             # be used in place of function calls made without & or do."
155             sub _NS_OWL_ () { 'http://www.w3.org/2002/07/owl#' }
156             sub _NS_DC_ () { 'http://purl.org/dc/elements/1.1/' }
157             sub _NS_DCTERMS_ () { 'http://purl.org/dc/terms/' }
158             sub _NS_NEXML_ () { 'http://www.nexml.org/2009' }
159             sub _NS_RDF_ () { 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' }
160             sub _NS_RDFS_ () { 'http://www.w3.org/2000/01/rdf-schema#' }
161             sub _NS_XSI_ () { 'http://www.w3.org/2001/XMLSchema-instance' }
162             sub _NS_XSD_ () { 'http://www.w3.org/2001/XMLSchema#' }
163             sub _NS_XML_ () { 'http://www.w3.org/XML/1998/namespace' }
164             sub _NS_TOL_ () { 'http://tolweb.org/tree/home.pages/downloadtree.html#' }
165             sub _NS_CDAO_ () { 'http://www.evolutionaryontology.org/cdao/1.0/cdao.owl#' }
166             sub _NS_BIOPHYLO_ () { 'http://search.cpan.org/dist/Bio-Phylo/terms#' }
167             sub _NS_SKOS_ () { 'http://www.w3.org/2004/02/skos/core#' }
168             sub _NS_PHYLOXML_ () { 'http://www.phyloxml.org/1.10/terms#' }
169             sub _NS_TB2PURL_ () { 'http://purl.org/phylo/treebase/phylows/' }
170             sub _NS_TNRS_ () { 'http://phylotastic.org/tnrs/terms#' }
171             sub _NS_FIGTREE_ () { 'http://tree.bio.ed.ac.uk/software/figtree/terms#' }
172             sub _NS_PHYLOMAP_ () { 'http://phylomap.org/terms.owl#' }
173             sub _NS_BIOVEL_ () { 'http://biovel.eu/terms#' }
174             sub _NS_NHX_ () { 'http://sites.google.com/site/cmzmasek/home/software/forester/nhx' }
175             sub _NS_DWC_ () { 'http://rs.tdwg.org/dwc/terms/' }
176             sub _NS_GBIF_ () { 'http://rs.gbif.org/terms/1.0/' }
177              
178             our $NS = {
179             'tnrs' => _NS_TNRS_(),
180             'pxml' => _NS_PHYLOXML_(),
181             'skos' => _NS_SKOS_(),
182             'bp' => _NS_BIOPHYLO_(),
183             'cdao' => _NS_CDAO_(),
184             'tol' => _NS_TOL_(),
185             'xml' => _NS_XML_(),
186             'xsd' => _NS_XSD_(),
187             'xsi' => _NS_XSI_(),
188             'rdf' => _NS_RDF_(),
189             'rdfs' => _NS_RDFS_(),
190             'nex' => _NS_NEXML_(),
191             'dc' => _NS_DC_(),
192             'owl' => _NS_OWL_(),
193             'bv' => _NS_BIOVEL_(),
194             'dcterms' => _NS_DCTERMS_(),
195             'fig' => _NS_FIGTREE_(),
196             'nhx' => _NS_NHX_(),
197             'dwc' => _NS_DWC_(),
198             'gbif' => _NS_GBIF_(),
199             };
200              
201             sub _NEXML_VERSION_ () { '0.9' }
202             sub _NONE_ () { Bio::Phylo::Util::CONSTANT::Int::_NONE_ }
203             sub _NODE_ () { Bio::Phylo::Util::CONSTANT::Int::_NODE_ }
204             sub _TREE_ () { Bio::Phylo::Util::CONSTANT::Int::_TREE_ }
205             sub _FOREST_ () { Bio::Phylo::Util::CONSTANT::Int::_FOREST_ }
206             sub _TAXON_ () { Bio::Phylo::Util::CONSTANT::Int::_TAXON_ }
207             sub _TAXA_ () { Bio::Phylo::Util::CONSTANT::Int::_TAXA_ }
208             sub _DATUM_ () { Bio::Phylo::Util::CONSTANT::Int::_DATUM_ }
209             sub _MATRIX_ () { Bio::Phylo::Util::CONSTANT::Int::_MATRIX_ }
210             sub _MATRICES_ () { Bio::Phylo::Util::CONSTANT::Int::_MATRICES_ }
211             sub _SEQUENCE_ () { Bio::Phylo::Util::CONSTANT::Int::_SEQUENCE_ }
212             sub _ALIGNMENT_ () { Bio::Phylo::Util::CONSTANT::Int::_ALIGNMENT_ }
213             sub _CHAR_ () { Bio::Phylo::Util::CONSTANT::Int::_CHAR_ }
214             sub _PROJECT_ () { Bio::Phylo::Util::CONSTANT::Int::_PROJECT_ }
215             sub _CHARSTATE_ () { Bio::Phylo::Util::CONSTANT::Int::_CHARSTATE_ }
216             sub _CHARSTATESEQ_ () { Bio::Phylo::Util::CONSTANT::Int::_CHARSTATESEQ_ }
217             sub _MATRIXROW_ () { Bio::Phylo::Util::CONSTANT::Int::_MATRIXROW_ }
218             sub _ANNOTATION_ () { Bio::Phylo::Util::CONSTANT::Int::_ANNOTATION_ }
219             sub _DICTIONARY_ () { Bio::Phylo::Util::CONSTANT::Int::_DICTIONARY_ }
220             sub _DOMCREATOR_ () { Bio::Phylo::Util::CONSTANT::Int::_DOMCREATOR_ }
221             sub _META_ () { Bio::Phylo::Util::CONSTANT::Int::_META_ }
222             sub _DESCRIPTION_ () { Bio::Phylo::Util::CONSTANT::Int::_DESCRIPTION_ }
223             sub _RESOURCE_ () { Bio::Phylo::Util::CONSTANT::Int::_RESOURCE_ }
224             sub _DOCUMENT_ () { Bio::Phylo::Util::CONSTANT::Int::_DOCUMENT_ }
225             sub _ELEMENT_ () { Bio::Phylo::Util::CONSTANT::Int::_ELEMENT_ }
226             sub _CHARACTERS_ () { Bio::Phylo::Util::CONSTANT::Int::_CHARACTERS_ }
227             sub _CHARACTER_ () { Bio::Phylo::Util::CONSTANT::Int::_CHARACTER_ }
228             sub _SET_ () { Bio::Phylo::Util::CONSTANT::Int::_SET_ }
229             sub _MODEL_ () { Bio::Phylo::Util::CONSTANT::Int::_MODEL_ }
230             sub _OPERATION_ () { Bio::Phylo::Util::CONSTANT::Int::_OPERATION_ }
231             sub _DATATYPE_ () { Bio::Phylo::Util::CONSTANT::Int::_DATATYPE_ }
232              
233             # for PhyloWS
234             sub _HTTP_SC_SEE_ALSO_ () { '303 See Other' }
235              
236             # for tree drawing
237             sub _PI_ () { 4 * atan2(1,1) }
238              
239             # this is a drop in replacement for Scalar::Util's function
240             my $looks_like_number;
241             {
242             eval { Scalar::Util::looks_like_number(0) };
243             if ($@) {
244             my $LOOKS_LIKE_NUMBER_RE =
245             qr/^([-+]?\d+(\.\d+)?([eE][-+]\d+)?|Inf|NaN)$/;
246             $looks_like_number = sub {
247             my $num = shift;
248             if ( defined $num and $num =~ $LOOKS_LIKE_NUMBER_RE ) {
249             return 1;
250             }
251             else {
252             return;
253             }
254             }
255             }
256             else {
257             $looks_like_number = \&Scalar::Util::looks_like_number;
258             }
259             undef($@);
260             }
261 62647     62647 1 253235 sub looks_like_number($) { return $looks_like_number->(shift) }
262              
263             sub looks_like_object($$) {
264 22023     22023 1 32841 my ( $object, $constant ) = @_;
265 22023         26174 my $type;
266 22023         29962 eval { $type = $object->_type };
  22023         42678  
267 22023 100 100     62000 if ( $@ or $type != $constant ) {
268 20         65 throw 'ObjectMismatch' => 'Invalid object!';
269             }
270             else {
271 22003         54652 return 1;
272             }
273             }
274              
275             sub looks_like_implementor($$) {
276 11411     11411 1 40525 return UNIVERSAL::can( $_[0], $_[1] );
277             }
278              
279             sub looks_like_instance($$) {
280 90562     90562 1 123831 my ( $object, $class ) = @_;
281 90562 100       128592 if ( ref $object ) {
282 88146 100       131059 if ( blessed $object ) {
283 218         921 return $object->isa($class);
284             }
285             else {
286 87928         190018 return ref $object eq $class;
287             }
288             }
289             else {
290 2416         5151 return;
291             }
292             }
293              
294             sub looks_like_hash(@) {
295 16745 100   16745 1 36481 if ( scalar(@_) % 2 ) {
296 1         4 throw 'OddHash' => 'Odd number of elements in hash assignment';
297             }
298             else {
299 16744         45859 return @_;
300             }
301             }
302              
303             sub looks_like_class($) {
304 1699     1699 1 2922 my $class = shift;
305 1699         2660 my $path = $class;
306 1699         6420 $path =~ s|::|/|g;
307 1699         2888 $path .= '.pm';
308 1699 100       4209 if ( not exists $INC{$path} ) {
309 178         330 eval { require $path };
  178         34035  
310 178 100       856 if ($@) {
311 96         433 throw 'ExtensionError' => $@;
312             }
313             }
314 1603         6121 return $class;
315             }
316             1;
317             __END__
318              
319             =head1 NAME
320              
321             Bio::Phylo::Util::CONSTANT - Global constants and utility functions
322              
323             =head1 DESCRIPTION
324              
325             This package defines globals used in the Bio::Phylo libraries. The constants
326             are called internally by the other packages, they have no direct usage. In
327             addition, several useful subroutines are optionally exported, which are
328             described below.
329              
330             =head1 SUBROUTINES
331              
332             The following subroutines are utility functions that can be imported using:
333              
334             use Bio::Phylo::Util::CONSTANT ':functions';
335              
336             The subroutines use prototypes for more concise syntax, e.g.:
337              
338             looks_like_number $num;
339             looks_like_object $obj, $const;
340             looks_like_hash @_;
341             looks_like_class $class;
342              
343             These subroutines are used for argument processing inside method calls.
344              
345             =over
346              
347             =item looks_like_instance()
348              
349             Tests if argument 1 looks like an instance of argument 2
350              
351             Type : Utility function
352             Title : looks_like_instance
353             Usage : do 'something' if looks_like_instance $var, $class;
354             Function: Tests whether $var looks like an instance of $class.
355             Returns : TRUE or undef
356             Args : $var = a variable to test, a $class to test against.
357             $class can also be anything returned by ref($var), e.g.
358             'HASH', 'CODE', etc.
359              
360             =item looks_like_implementor()
361              
362             Tests if argument 1 implements argument 2
363              
364             Type : Utility function
365             Title : looks_like_implementor
366             Usage : do 'something' if looks_like_implementor $var, $method;
367             Function: Tests whether $var implements $method
368             Returns : return value of UNIVERSAL::can or undef
369             Args : $var = a variable to test, a $method to test against.
370              
371             =item looks_like_number()
372              
373             Tests if argument looks like a number.
374              
375             Type : Utility function
376             Title : looks_like_number
377             Usage : do 'something' if looks_like_number $var;
378             Function: Tests whether $var looks like a number.
379             Returns : TRUE or undef
380             Args : $var = a variable to test
381              
382             =item looks_like_object()
383              
384             Tests if argument looks like an object of specified type constant.
385              
386             Type : Utility function
387             Title : looks_like_object
388             Usage : do 'something' if looks_like_object $obj, $const;
389             Function: Tests whether $obj looks like an object.
390             Returns : TRUE or throws ObjectMismatch
391             Args : $obj = an object to test
392             $const = a constant as defined in this package
393              
394             =item looks_like_hash()
395              
396             Tests if argument looks like a hash.
397              
398             Type : Utility function
399             Title : looks_like_hash
400             Usage : do 'something' if looks_like_hash @_;
401             Function: Tests whether argument looks like a hash.
402             Returns : hash (same order as arg) or throws OddHash
403             Args : An array of hopefully even key/value pairs
404              
405             =item looks_like_class()
406              
407             Tests if argument looks like a loadable class name.
408              
409             Type : Utility function
410             Title : looks_like_class
411             Usage : do 'something' if looks_like_class $class;
412             Function: Tests whether argument looks like a class.
413             Returns : $class or throws ExtensionError
414             Args : A hopefully loadable class name
415              
416             =back
417              
418             =head1 SEE ALSO
419              
420             There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
421             for any user or developer questions and discussions.
422              
423             =over
424              
425             =item L<Bio::Phylo::Manual>
426              
427             Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
428              
429             =back
430              
431             =head1 CITATION
432              
433             If you use Bio::Phylo in published research, please cite it:
434              
435             B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
436             and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
437             I<BMC Bioinformatics> B<12>:63.
438             L<http://dx.doi.org/10.1186/1471-2105-12-63>
439              
440              
441              
442             =cut
443