dendropy.datamodel.basemodel
: Core Infrastructure for Phylogenetic Data Objects¶
The DataObject
Class¶
-
class
dendropy.datamodel.basemodel.
DataObject
(label=None)[source]¶ Base class for all phylogenetic data objects.
-
clone
(depth=1)[source]¶ Creates and returns a copy of
self
.- Parameters
depth (integer) –
The depth of the copy:
0: shallow-copy: All member objects are references, except for :attr:
annotation_set
of top-level object and memberAnnotation
objects: these are full, independent instances (though any complex objects in thevalue
field ofAnnotation
objects are also just references).1: taxon-namespace-scoped copy: All member objects are full independent instances, except for
TaxonNamespace
andTaxon
instances: these are references.2: Exhaustive deep-copy: all objects are cloned.
-
taxon_namespace_scoped_copy
(memo=None)[source]¶ Cloning level: 1. Taxon-namespace-scoped copy: All member objects are full independent instances, except for
TaxonNamespace
andTaxon
objects: these are preserved as references.
-
The Annotable
Class¶
-
class
dendropy.datamodel.basemodel.
Annotable
[source]¶ Mixin class which all classes that need to persist object attributes or other information as metadata should subclass.
-
__copy__
(memo=None)[source]¶ Cloning level: 0. :attr:
annotation_set
of top-level object and memberAnnotation
objects are full, independent instances. All other member objects (include objects referenced by dynamically-bound attribute values ofAnnotation
objects) are references. All member objects are references, except for
-
copy_annotations_from
(other, attribute_object_mapper=None)[source]¶ Copies annotations from
other
, which must be ofAnnotable
type.Copies are deep-copies, in that the
Annotation
objects added to theannotation_set
AnnotationSet
collection ofself
are independent copies of those in theannotate_set
collection ofother
. However, dynamic bound-attribute annotations retain references to the original objects as given inother
, which may or may not be desirable. This is handled by updated the objects to which attributes are bound via mappings found inattribute_object_mapper
. In dynamic bound-attribute annotations, the_value
attribute of the annotations object (Annotation._value
) is a tuple consisting of “(obj, attr_name)
”, which instructs theAnnotation
object to return “getattr(obj, attr_name)
” (via: “getattr(*self._value)
”) when returning the value of the Annotation. “obj
” is typically the object to which theAnnotationSet
belongs (i.e.,self
). When a copy ofAnnotation
is created, the object reference given in the first element of the_value
tuple of dynamic bound-attribute annotations are unchanged, unless the id of the object reference is fo- Parameters
other (
Annotable
) – Source of annotations to copy.attribute_object_mapper (dict) – Like the
memo
of__deepcopy__
, maps object id’s to objects. The purpose of this is to update the parent or owner objects of dynamic attribute annotations. If a dynamic attributeAnnotation
gives objectx
as the parent or owner of the attribute (that is, the first element of theAnnotation._value
tuple isother
) andid(x)
is found inattribute_object_mapper
, then in the copy the owner of the attribute is changed toattribute_object_mapper[id(x)]
. Ifattribute_object_mapper
isNone
(default), then the following mapping is automatically inserted:id(other): self
. That is, any references toother
in anyAnnotation
object will be remapped toself
. If really no reattribution mappings are desired, then an empty dictionary should be passed instead.
-
deep_copy_annotations_from
(other, memo=None)[source]¶ Note that all references to
other
in any annotation value (and sub-annotation, and sub-sub-sub-annotation, etc.) will be replaced with references toself
. This may not always make sense (i.e., a reference to a particular entity may be absolute regardless of context).
-
The Annotation
Class¶
-
class
dendropy.datamodel.basemodel.
Annotation
(name, value, datatype_hint=None, name_prefix=None, namespace=None, name_is_prefixed=False, is_attribute=False, annotate_as_reference=False, is_hidden=False, label=None, real_value_format_specifier=None)[source]¶ Metadata storage, composition and persistance, with the following attributes:
name
value
datatype_hint
name_prefix
namespace
annotate_as_reference
is_hidden
real_value_format_specifier
- format specifier for printing or rendering values as string, given in Python’s format specification mini-language. E.g., ‘.8f’, ‘4E’, ‘>04d’.
-
clone
(attribute_object_mapper=None)[source]¶ Essentially a shallow-copy, except that any objects in the
_value
field with anid
found inattribute_object_mapper
will be replaced withattribute_object_mapper[id]
.
-
copy_annotations_from
(other, attribute_object_mapper=None)¶ Copies annotations from
other
, which must be ofAnnotable
type.Copies are deep-copies, in that the
Annotation
objects added to theannotation_set
AnnotationSet
collection ofself
are independent copies of those in theannotate_set
collection ofother
. However, dynamic bound-attribute annotations retain references to the original objects as given inother
, which may or may not be desirable. This is handled by updated the objects to which attributes are bound via mappings found inattribute_object_mapper
. In dynamic bound-attribute annotations, the_value
attribute of the annotations object (Annotation._value
) is a tuple consisting of “(obj, attr_name)
”, which instructs theAnnotation
object to return “getattr(obj, attr_name)
” (via: “getattr(*self._value)
”) when returning the value of the Annotation. “obj
” is typically the object to which theAnnotationSet
belongs (i.e.,self
). When a copy ofAnnotation
is created, the object reference given in the first element of the_value
tuple of dynamic bound-attribute annotations are unchanged, unless the id of the object reference is fo- Parameters
other (
Annotable
) – Source of annotations to copy.attribute_object_mapper (dict) – Like the
memo
of__deepcopy__
, maps object id’s to objects. The purpose of this is to update the parent or owner objects of dynamic attribute annotations. If a dynamic attributeAnnotation
gives objectx
as the parent or owner of the attribute (that is, the first element of theAnnotation._value
tuple isother
) andid(x)
is found inattribute_object_mapper
, then in the copy the owner of the attribute is changed toattribute_object_mapper[id(x)]
. Ifattribute_object_mapper
isNone
(default), then the following mapping is automatically inserted:id(other): self
. That is, any references toother
in anyAnnotation
object will be remapped toself
. If really no reattribution mappings are desired, then an empty dictionary should be passed instead.
-
deep_copy_annotations_from
(other, memo=None)¶ Note that all references to
other
in any annotation value (and sub-annotation, and sub-sub-sub-annotation, etc.) will be replaced with references toself
. This may not always make sense (i.e., a reference to a particular entity may be absolute regardless of context).
The AnnotationSet
Class¶
-
class
dendropy.datamodel.basemodel.
AnnotationSet
(target, *args)[source]¶ -
__add__
(other)¶ Returns
OrderedSet
consisting of union of values inself
andother
.
-
__contains__
(value)¶ Returns
True
ifvalue
is inself
orFalse
otherwise.
-
__delitem__
(index)¶ Deletes value at
index
. Note takes index of than value as key.
-
__iter__
()¶ Returns iterator over values in
self
.
-
__reversed__
()¶ Returns
OrderedSet
with values in reversed order.
-
add
(value)¶ Adds a new element,
value
, toself
ifvalue
is not already inself
.
-
add_bibtex
(citation, store_as='bibtex', name_prefix=None, namespace=None, is_hidden=False)[source]¶ Add a citation as an annotation.
- Parameters
citation (string or dict or
BibTexEntry
) – The citation to be added. If a string, then it must be a BibTex-formatted entry. If a dictionary, then it must have BibTex fields as keys and contents as values.store_as (string, optional) – Specifies how to record the citation, with one of the following strings as values: “bibtex” (a set of annotations, where each BibTex field becomes a separate annotation); “prism” (a set of PRISM [Publishing Requirements for Industry Standard Metadata] annotations); “dublin” (A set of of Dublic Core annotations). Defaults to “bibtex”.
name_prefix (string, optional) – Mainly for NeXML output (e.g. “dc:”).
namespace (string, optional) – Mainly for NeXML output (e.g. “http://www.w3.org/XML/1998/namespace”).
is_hidden (boolean, optional) – Do not write or print this annotation when writing data.
- Returns
annotation (|Annotation|) – The new
Annotation
created.
-
add_bound_attribute
(attr_name, annotation_name=None, datatype_hint=None, name_prefix=None, namespace=None, name_is_prefixed=False, annotate_as_reference=False, is_hidden=False, real_value_format_specifier=None, owner_instance=None)[source]¶ Add an attribute of an object as a dynamic annotation. The value of the annotation will be dynamically bound to the value of the attribute.
- Parameters
attr_name (string) – The (string) name of the attribute to be used as the source of the content or value of the annotation.
annotation_name (string, optional) – Use this string as the annotation field/name rather than the attribute name.
datatype_hint (string, optional) – Mainly for NeXML output (e.g. “xsd:string”).
namespace_prefix (string, optional) – Mainly for NeXML output (e.g. “dc:”).
namespace (string, optional) – Mainly for NeXML output (e.g. “http://www.w3.org/XML/1998/namespace”).
name_is_prefixed (string, optional) – Mainly for NeXML input: name will be split into prefix and local part before storage (e.g., “dc:citations” will result in prefix = “dc” and name=”citations”)
annotate_as_reference (bool, optional) – The value should be interpreted as a URI that points to content.
is_hidden (bool, optional) – Do not write or print this annotation when writing data.
owner_instance (object, optional) – The object whose attribute is to be used as the value of the annotation. Defaults to
self.target
.
- Returns
annotation (|Annotation|) – The new
Annotation
created.
-
add_citation
(citation, read_as='bibtex', store_as='bibtex', name_prefix=None, namespace=None, is_hidden=False)[source]¶ Add a citation as an annotation.
- Parameters
citation (string or dict or
BibTexEntry
) – The citation to be added. If a string, then it must be a BibTex-formatted entry. If a dictionary, then it must have BibTex fields as keys and contents as values.read_as (string, optional) – Specifies the format/schema/structure of the citation. Currently only supports ‘bibtex’.
store_as (string, optional) – Specifies how to record the citation, with one of the following strings as values: “bibtex” (a set of annotations, where each BibTex field becomes a separate annotation); “prism” (a set of PRISM [Publishing Requirements for Industry Standard Metadata] annotations); “dublin” (A set of of Dublic Core annotations). Defaults to “bibtex”.
name_prefix (string, optional) – Mainly for NeXML output (e.g. “dc:”).
namespace (string, optional) – Mainly for NeXML output (e.g. “http://www.w3.org/XML/1998/namespace”).
is_hidden (boolean, optional) – Do not write or print this annotation when writing data.
- Returns
annotation (|Annotation|) – The new
Annotation
created.
-
add_new
(name, value, datatype_hint=None, name_prefix=None, namespace=None, name_is_prefixed=False, is_attribute=False, annotate_as_reference=False, is_hidden=False, real_value_format_specifier=None)[source]¶ Add an annotation.
- Parameters
name (string) – The property/subject/field of the annotation (e.g. “color”, “locality”, “dc:citation”)
value (string) – The content of the annotation.
datatype_hint (string, optional) – Mainly for NeXML output (e.g. “xsd:string”).
namespace_prefix (string, optional) – Mainly for NeXML output (e.g. “dc:”).
namespace (string, optional) – Mainly for NeXML output (e.g. “http://www.w3.org/XML/1998/namespace”).
name_is_prefixed (string, optional) – Mainly for NeXML input: name will be split into prefix and local part before storage (e.g., “dc:citations” will result in prefix = “dc” and name=”citations”)
is_attribute (boolean, optional) – If value is passed as a tuple of (object, “attribute_name”) and this is True, then actual content will be the result of calling
getattr(object, "attribute_name")
.annotate_as_reference (boolean, optional) – The value should be interpreted as a URI that points to content.
is_hidden (boolean, optional) – Do not write or print this annotation when writing data.
real_value_format_specifier (str) – Format specifier for printing or rendering values as string, given in Python’s format specification mini-language. E.g., ‘.8f’, ‘4E’, ‘>04d’.
- Returns
annotation (|Annotation|) – The new
Annotation
created.
-
discard
(key)¶ Deletes value of
key
fromself
. No error if no value ofkey
is not inself
.
-
drop
(**kwargs)[source]¶ Removes Annotation objects that match based on all criteria specified in keyword arguments.
Remove all annotation objects with
name
== “color”:>>> tree.annotations.drop(name="color")
Remove all annotation objects with
namespace
== “http://packages.python.org/DendroPy/”:>>> tree.annotations.drop(namespace="http://packages.python.org/DendroPy/")
Remove all annotation objects with
namespace
== “http://packages.python.org/DendroPy/” andname
== “color”:>>> tree.annotations.drop(namespace="http://packages.python.org/DendroPy/", name="color")
Remove all annotation objects with
name_prefix
== “dc”:>>> tree.annotations.drop(name_prefix="dc")
Remove all annotation objects with
prefixed_name
== “dc:color”:>>> tree.annotations.drop(prefixed_name="dc:color")
If no keyword argument filter criteria are given, all annotations are removed:
>>> tree.annotations.drop()
- Returns
results (|AnnotationSet|) –
AnnotationSet
containingAnnotation
objects that were removed.
-
find
(**kwargs)[source]¶ Returns the first Annotation associated with self.target which matches based on all criteria specified in keyword arguments:
>>> note = tree.annotations.find(name="color") >>> note = tree.annotations.find(name_prefix="dc", name="color") >>> note = tree.annotations.find(prefixed_name="dc:color")
If no match is found, None is returned.
If no keyword arguments are given, a TypeError is raised.
- Returns
results (|Annotation| or |None|) – First
Annotation
object found that matches criteria, orNone
if no matching annotations found.
-
findall
(**kwargs)[source]¶ Returns AnnotationSet of Annotation objects associated with self.target that match based on all criteria specified in keyword arguments:
>>> notes = tree.annotations.findall(name="color") >>> notes = tree.annotations.findall(namespace="http://packages.python.org/DendroPy/") >>> notes = tree.annotations.findall(namespace="http://packages.python.org/DendroPy/", name="color") >>> notes = tree.annotations.findall(name_prefix="dc") >>> notes = tree.annotations.findall(prefixed_name="dc:color")
If no matches are found, the return AnnotationSet is empty.
If no keyword arguments are given, all annotations are returned:
>>> notes = tree.annotations.findall()
- Returns
results (|AnnotationSet| or |None|) –
AnnotationSet
containingAnnotation
objects that match criteria, orNone
if no matching annotations found.
-
get_value
(name, default=None)[source]¶ Returns the value of the first Annotation associated with self.target which has
name
in the name field.If no match is found, then
default
is returned.- Parameters
name (string) – Name of
Annotation
object whose value is to be returned.default (any, optional) – Value to return if no matching
Annotation
object found.
- Returns
results (|Annotation| or |None|) –
value
of firstAnnotation
object found that matches criteria, orNone
if no matching annotations found.
-
index
(value)¶ Returns index of element with value of
value
.
-
next
()¶ Returns iterator over values in
self
.
-
pop
(last=True)¶ Removes and return value in
self
. By default, removes last value.
-
remove
(key)¶ Deletes value of
key
fromself
. KeyErrorif no value ofkey
is not inself
.
-
require_value
(name)[source]¶ Returns the value of the first Annotation associated with self.target which has
name
in the name field.If no match is found, then KeyError is raised.
- Parameters
name (string) – Name of
Annotation
object whose value is to be returned.- Returns
results (|Annotation| or |None|) –
value
of firstAnnotation
object found that matches criteria.
-
update
(other)¶ Updates
self
with values inother
for each value inother
that is not already inself
.
-
values_as_dict
(**kwargs)[source]¶ Returns annotation set as a dictionary. The keys and values for the dictionary will be generated based on the following keyword arguments:
- Keyword Arguments
key_attr (string) – String specifying an Annotation object attribute name to be used as keys for the dictionary.
key_fn (string) – Function that takes an Annotation object as an argument and returns the value to be used as a key for the dictionary.
value_attr (string) – String specifying an Annotation object attribute name to be used as values for the dictionary.
value_fn (string) – Function that takes an Annotation object as an argument and returns the value to be used as a value for the dictionary.
most one of key_attr or key_fn can be specified. If neither (At) –
specified, then by default the keys are generated from Annotation.name. (is) –
most one of value_attr or value_fn can be specified. If neither (At) –
specified, then by default the values are generated from Annotation.value. (is) –
collisions will result in the dictionary entry for that key being (Key) –
overwritten. –
- Returns
values (dict)
-
Supporting Classes¶
-
class
dendropy.utility.bibtex.
BibTexEntry
(citation=None)[source]¶ Tracks a single BibTeX entry.
Sets up internal dictionary of BibTeX fields, and initializes if argument is given.
-
__delattr__
(name)[source]¶ Allows bibtex fields (and any additional ones) to be treated like object attributes.
-
__getattr__
(name)[source]¶ Allows bibtex fields (and any additional ones) to be treated like object attributes.
-
__setattr__
(name, value)[source]¶ Allows bibtex fields (and any additional ones) to be treated like object attributes.
-
property
fields
¶ Returns list of populated fields in order (does not include bibtype and citekey).
-