A collection of Taxon objects representing a self-contained and complete domain of distinct operational taxonomic unit definitions. Provides the common semantic context in which operational taxonomic units referenced by various phylogenetic data objects (e.g., trees or alignments) can be related.
| Parameters: |
|
|---|
Notes
An empty TaxonNamespace can be created (with optional) label and Taxon objects added later:
>>> tns = dendropy.TaxonNamespace(label="taxa")
>>> t1 = Taxon("a")
>>> tns.add_taxon(t1)
>>> t2 = Taxon("b")
>>> tns.add_taxon(t2)
>>> tns.add_taxon("c")
>>> tns
<TaxonNamespace 0x106509090 'taxa': [<Taxon 0x10661f050 'a'>, <Taxon 0x10651c590 'b'>, <Taxon 0x106642a90 'c'>]>
Alternatively, an iterable can be passed in as an initializer, and all Taxon objects will be added directly while, for each string, a new Taxon object will be created and added. So, the below are all equivalent to the above:
>>> tns = dendropy.TaxonNamespace(["a", "b", "c"], label="taxa")
>>> taxa = [Taxon(n) for n in ["a", "b", "c"]]
>>> tns = dendropy.taxonnamespace(taxa, label="taxa")
>>> t1 = Taxon("a")
>>> t2 = Taxon("b")
>>> taxa = [t1, t2, "c"]
>>> tns = dendropy.TaxonNamespace(taxa, label="taxa")
If a TaxonNamespace object is passed as the initializer argument, a shallow copy of the object is constructed:
>>> tns1 = dendropy.TaxonNamespace(["a", "b", "c"], label="taxa1")
>>> tns1
<TaxonNamespace 0x1097275d0 'taxa1': [<Taxon 0x109727610 'a'>, <Taxon 0x109727e10 'b'>, <Taxon 0x109727e90 'c'>]>
>>> tns2 = dendropy.TaxonNamespace(tns1, label="2")
>>> tns2
<TaxonNamespace 0x109727d50 'taxa1': [<Taxon 0x109727610 'a'>, <Taxon 0x109727e10 'b'>, <Taxon 0x109727e90 'c'>]>
Thus, while “tns1” and “tns2” are independent collections, and addition/deletion of Taxon instances to one will not effect the other, the label of a Taxon instance that is an element in one will of course effect the same instance if it is in the other:
>>> print(tns1[0].label)
>>> a
>>> print(tns2[0].label)
>>> a
>>> tns1[0].label = "Z"
>>> print(tns1[0].label)
>>> Z
>>> print(tns2[0].label)
>>> Z
In contrast to actual data (i.e., the Taxon objects), alll metadata associated with “tns2” (i.e., the AnnotationSet object, in the TaxonNamespace.annotations attribute), will be a full, independent deep-copy.
If what is needed is a true deep-copy of the data of a particular TaxonNamespace object, including copies of the member Taxon instances, then this can be achieved using copy.deepcopy.
>>> import copy
>>> tns1 = dendropy.TaxonNamespace(["a", "b", "c"], label="taxa1")
>>> tns2 = copy.deepcopy(tns1)
Returns number of Taxon objects in this TaxonNamespace.
Returns the accession index of taxon. Note that this may not be the same as the list index of the taxon if taxa have been deleted from the namespace.
| Parameters: | taxon (Taxon) – Taxon object for which to return the accession index. |
|---|---|
| Returns: | h (integer) – The accession index. |
Adds multiple Taxon objects to self.
Each Taxon object in taxa that is not already in the collection of Taxon objects in this namespace is added to it. If any of the Taxon objects are already in the collection, then nothing happens. If the namespace is immutable, then TypeError is raised when trying to add Taxon objects.
| Parameters: | taxa (collections.Iterable [Taxon]) – A list of Taxon objects to be accessioned or registered in this collection. |
|---|---|
| Raises: | TypeError – If this namespace is immutable (i.e. TaxonNamespace.is_mutable is False). |
Adds a new Taxon object to self.
If taxon is not already in the collection of Taxon objects in this namespace, and this namespace is mutable, it is added to the collection. If it is already in the collection, then nothing happens. If it is not already in the collection, but the namespace is not mutable, then TypeError is raised.
| Parameters: | taxon (Taxon) – The Taxon object to be accessioned or registered in this collection. |
|---|---|
| Raises: | TypeError – If this namespace is immutable (i.e. TaxonNamespace.is_mutable is False). |
Returns mask of all taxa.
| Returns: | h (integer) – Bitmask spanning all Taxon objects in self. |
|---|
Represents a split as a newick string.
| Parameters: |
|
|---|---|
| Returns: | s (string) – NEWICK representation of split specified by bitmask. |
Returns list of Taxon objects represented by split bitmask.
| Parameters: | |
|---|---|
| Returns: | taxa (list [Taxon]) – List of Taxon objects specified or spanned by bitmask. |
Returns description of object, up to level depth.
Removes all Taxon objects with label matching label from the collection in this namespace.
| Parameters: |
|
|---|
See also
Return list of Taxon object(s) with label matching label.
| Parameters: |
|
|---|---|
| Returns: | taxa (list [Taxon]) – A list containing zero or more Taxon objects with labels matching label. |
Retrieves list of Taxon objects with given labels.
| Parameters: |
|
|---|---|
| Returns: | taxa (list [Taxon]) – A list containing zero or more Taxon objects with labels matching label. |
Retrieves a Taxon object with the given label.
If multiple Taxon objects exist with labels that match label, then only the first one is returned. If no Taxon object is found in this namespace with the specified critieria, None is returned.
| Parameters: |
|
|---|---|
| Returns: | taxon (|Taxon| object or |None|) – The first Taxon object in this namespace collection with a label matching label, or None if no such Taxon object exists. |
Checks for presence of Taxon objects with the given labels.
| Parameters: |
|
|---|---|
| Returns: | b (boolean) – Returns True if, for every element in the iterable labels, there is at least one Taxon object that has a label attribute that matches this. False otherwise. |
Checks for presence of a Taxon object with the given label.
| Parameters: |
|
|---|---|
| Returns: | b (boolean) – True if there is at least one Taxon object in this namespace with a label matching the value of label. Otherwise, False. |
Returns dictionary with taxon labels as keys and corresponding Taxon objects as values.
If the TaxonNamespace is currently case-insensitive, then the dictionary returned will have case-insensitive keys, other the dictionary will be case-sensitive. You can override this by explicitly specifying is_case_sensitive to False or True.
No attempt is made to handle collisions.
| Returns: | d (dictonary-like) – Dictionary with Taxon.label values of Taxon objects in self as keys and corresponding Taxon objects as values. |
|---|
Returns list of labels of all Taxon objects in self.
| Returns: | labels (list [string]) – List of Taxon.label values of Taxon objects in self. |
|---|
Creates and add a new Taxon with corresponding label for each label in labels. Returns list of Taxon objects created.
| Parameters: | labels (collections.Iterable [string]) – The values of the label attributes of the new Taxon objects to be created, added to this namespace collection, and returned. |
|---|---|
| Returns: | taxa (collections.Iterable [Taxon]) – A list of Taxon objects created and added. |
| Raises: | TypeError – If this namespace is immutable (i.e. TaxonNamespace.is_mutable is False). |
Creates, adds, and returns a new Taxon object with corresponding label.
| Parameters: | label (string or string-like) – The name or label of the new operational taxonomic unit concept. |
|---|---|
| Returns: | taxon (|Taxon|) – The new Taxon object, |
Removes specified Taxon object from the collection in this namespace.
| Parameters: | taxon (a Taxon object) – The Taxon object to be removed. |
|---|---|
| Raises: | ValueError – If taxon is not in the collection of this namespace. |
Removes all Taxon objects with label matching label from the collection in this namespace.
| Parameters: |
|
|---|---|
| Raises: | LookupError – If no Taxon objects are found with matching label(s). |
See also
Retrieves a Taxon object with the given label, creating it if necessary.
Retrieves a Taxon object with the label, label. If multiple Taxon objects exist with labels that match label, then only the first one is returned. If no such Taxon object exists in the current namespace and the TaxonNamespace is NOT mutable, an exception is raised. If no such Taxon object exists in the current namespace and TaxonNamespace is mutable, then a new Taxon is created, added, and returned.
| Parameters: |
|
|---|---|
| Returns: | taxon (|Taxon| object or |None|) – A Taxon object in this namespace collection with a label matching label. |
| Raises: | TypeError – If no Taxon object is currently in the collection with a label matching the input label and the is_mutable attribute of self is False. |
Sorts Taxon objects in collection. If key is not given, defaults to sorting by label (i.e., key = lambda x: x.label).
| Parameters: |
|
|---|
Represents a split as a newick string.
| Parameters: |
|
|---|---|
| Returns: | s (string) – NEWICK representation of split specified by bitmask. |
Returns a bipartition that represents all taxa specified by keyword-specified list of taxon objects (taxa=) or labels (labels=).
| Parameters: | **kwargs (keyword arguments) – Requires one of: |
|---|---|
| Returns: | b (list [integer]) – List of split hash bitmask values for specified Taxon objects. |
Retrieves the list of split hash bitmask values representing all taxa specified by keyword-specified list of taxon objects (taxa=) or labels (labels=).
| Parameters: | **kwargs (keyword arguments) – Requires one of: |
|---|---|
| Returns: | b (list [integer]) – List of split hash bitmask values for specified Taxon objects. |
A taxon associated with a sequence or a node on a tree.
| Parameters: | label (string or Taxon object) – Label or name of this operational taxonomic unit concept. If a string, then the label attribute of self is set to this value. If a Taxon object, then the label attribute of self is set to the same value as the label attribute the other Taxon object and all annotations/metadata are copied. |
|---|
Provides infrastructure for the maintenance of references to taxa.
Move this object and all members to a new operational taxonomic unit concept namespace scope.
Current self.taxon_namespace value will be replaced with value given in taxon_namespace if this is not None, or a new TaxonNamespace object. Following this, reconstruct_taxon_namespace() will be called: each distinct Taxon object associated with self or members of self that is not alread in taxon_namespace will be replaced with a new Taxon object that will be created with the same label and added to self.taxon_namespace. Calling this method results in the object (and all its member objects) being associated with a new, independent taxon namespace.
Label mapping case sensitivity follows the self.taxon_namespace.is_case_sensitive setting. If False and unify_taxa_by_label is also True, then the establishment of correspondence between Taxon objects in the old and new namespaces with be based on case-insensitive matching of labels. E.g., if there are four Taxon objects with labels ‘Foo’, ‘Foo’, ‘FOO’, and ‘FoO’ in the old namespace, then all objects that reference these will reference a single new Taxon object in the new namespace (with a label some existing casing variant of ‘foo’). If True: if unify_taxa_by_label is True, Taxon objects with labels identical except in case will be considered distinct.
| Parameters: |
|
|---|
Examples
Use this method to move an object from one taxon namespace to another.
For example, to get a copy of an object associated with another taxon namespace and associate it with a different namespace:
# Get handle to the new TaxonNamespace
other_taxon_namespace = some_other_data.taxon_namespace
# Get a taxon-namespace scoped copy of a tree
# in another namespace
t2 = Tree(t1)
# Replace taxon namespace of copy
t2.migrate_taxon_namespace(other_taxon_namespace)
You can also use this method to get a copy of a structure and then move it to a new namespace:
t2 = Tree(t1) t2.migrate_taxon_namespace(TaxonNamespace())
# Note: the same effect can be achived by: t3 = copy.deepcopy(t1)
See also
Returns a set populated with all of Taxon instances associated with self.
| Parameters: | taxa (set()) – Set to populate. If not specified, a new one will be created. |
|---|---|
| Returns: | taxa (set[|Taxon|]) – Set of taxa associated with self. |
Remove all Taxon instances in self.taxon_namespace that are not associated with self or any item in self.
Repopulates the current taxon namespace with new taxon objects, preserving labels. Each distinct Taxon object associated with self or members of self that is not already in self.taxon_namespace will be replaced with a new Taxon object that will be created with the same label and added to self.taxon_namespace.
Label mapping case sensitivity follows the self.taxon_namespace.is_case_sensitive setting. If False and unify_taxa_by_label is also True, then the establishment of correspondence between Taxon objects in the old and new namespaces with be based on case-insensitive matching of labels. E.g., if there are four Taxon objects with labels ‘Foo’, ‘Foo’, ‘FOO’, and ‘FoO’ in the old namespace, then all objects that reference these will reference a single new Taxon object in the new namespace (with a label some existing casing variant of ‘foo’). If True: if unify_taxa_by_label is True, Taxon objects with labels identical except in case will be considered distinct.
Note
Existing Taxon objects in self.taxon_namespace are not removed. This method should thus only be called only when self.taxon_namespace has been changed. In fact, typical usage would not involve calling this method directly, but rather through
| Parameters: |
|
|---|
DEPRECATED: Use reconstruct_taxon_namespace instead. Derived classes should override this to ensure that their various components, attributes and members all refer to the same TaxonNamespace object as self.taxon_namespace, and that self.taxon_namespace has all the Taxon objects in the various members.
DEPRECATED: Use migrate_taxon_namespace() instead. Rebuilds taxon_namespace from scratch, or assigns Taxon objects from given TaxonNamespace object taxon_namespace based on label values.