pyham 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyham/genome.py ADDED
@@ -0,0 +1,148 @@
1
+ import abc
2
+ from ete4 import Tree
3
+
4
+
5
+ class Genome(metaclass=abc.ABCMeta):
6
+ """
7
+ Genome is an abstract class representing extant or ancestral genomes. An Genome is defined by a unique taxon (node)
8
+ in the Taxonomy.tree.
9
+
10
+ Attributes:
11
+ | taxon (:obj:`ete3.TreeNode` of the :obj:`pyham.taxonomy`.tree): corresponding taxon.
12
+ | name (:obj:`str`): Name of the Genome. Get from the newick tree if specified otherwise build it by concatenating all children genome names.
13
+ | genes (:obj:`list`): list of :obj:`pyham.abstractgene.AbstractGene` related to this Genome.
14
+
15
+ """
16
+
17
+ def __init__(self):
18
+ self.genes = []
19
+ self.taxon = None
20
+ self.name = None
21
+
22
+ def add_gene(self, gene):
23
+
24
+ """
25
+ This method adds an AbstractGene to the genes attributes and updates the AbstractGene.genome attribute.
26
+
27
+ Attributes:
28
+ | taxon (Taxonomy.tree node): corresponding taxon.
29
+ | name (:obj:`str`): Name of the Genome. Get from the newick tree if specified otherwise build it by concatenating all children genome names.
30
+ | genes (:obj:`list`): list of :obj:`AbstractGene` related to this Genome.
31
+
32
+ Raises:
33
+ TypeError: if gene is not an AbstractGene.
34
+ """
35
+ from .abstractgene import AbstractGene
36
+ if not isinstance(gene, AbstractGene):
37
+ raise TypeError("expect subclass obj of '{}', got {}"
38
+ .format(AbstractGene.__name__,
39
+ type(gene).__name__))
40
+
41
+ self.genes.append(gene)
42
+ gene.set_genome(self)
43
+
44
+ def set_taxon(self, taxon):
45
+
46
+ """
47
+ This method sets the taxon of the Genome.
48
+
49
+ Attributes:
50
+ taxon (Taxonomy.tree node): corresponding taxon.
51
+
52
+ Raises:
53
+ TypeError: if taxon is not an ete4.Tree.
54
+ EvolutionaryConceptError: if the genome already has a taxon set.
55
+ """
56
+
57
+ if not isinstance(taxon, Tree):
58
+ raise TypeError("expect subclass obj of '{}', got {}"
59
+ .format(Tree.__name__,
60
+ type(taxon).__name__))
61
+ if self.taxon is not None and taxon != self.taxon:
62
+ raise EvolutionaryConceptError("only one taxon can refer to one genome")
63
+ self.taxon = taxon
64
+
65
+ @abc.abstractmethod
66
+ def get_number_genes(self):
67
+ """ Return the number of genes"""
68
+ pass
69
+
70
+ def __str__(self):
71
+ return self.name
72
+
73
+
74
+ class AncestralGenome(Genome):
75
+
76
+ """
77
+ AncestralGenome class representing ancestral genomes.
78
+
79
+ Attributes:
80
+ ancestral_clustering (dict): a dictionary that map each of this ancestral genome HOGs to its list of descendant
81
+ extant genes.
82
+
83
+ """
84
+
85
+ def __init__(self):
86
+ super(AncestralGenome, self).__init__()
87
+ self.ancestral_clustering = None
88
+
89
+ def get_ancestral_clustering(self):
90
+
91
+ """ Lazy getter of the ancestral_clustering attribute.
92
+
93
+ Returns:
94
+ :obj:`dict`.
95
+
96
+ """
97
+
98
+ if self.ancestral_clustering is None:
99
+ self.ancestral_clustering = {}
100
+ for hog in self.genes:
101
+ self.ancestral_clustering[hog] = hog.get_all_descendant_genes()
102
+ return self.ancestral_clustering
103
+
104
+ def get_number_genes(self):
105
+ return len(self.genes)
106
+
107
+
108
+ class ExtantGenome(Genome):
109
+
110
+ """
111
+ ExtantGenome class representing extant genomes. An ExtantGenome is defined by a unique name and a NCBI taxId.
112
+
113
+ Attributes:
114
+ | name (:obj:`str`): unique genome name. This is the unique identifier of ExtantGenome.
115
+ | taxid (dict): NCBI taxId.
116
+
117
+ """
118
+
119
+ def __init__(self, name, NCBITaxId='-1', **kwargs):
120
+ super(ExtantGenome, self).__init__()
121
+ self.name = name
122
+ self.taxid = NCBITaxId
123
+ self.taxon_id = kwargs.get('taxon_id', None)
124
+
125
+ def get_number_genes(self, singleton=True): # TODO: UT
126
+
127
+ """ Get the number of genes of this ExtantGenome.
128
+
129
+ Args:
130
+ | singleton (:obj:`Bool`): boolean to take into account singleton or not.
131
+
132
+ Returns:
133
+ :obj:`int`.
134
+
135
+ """
136
+
137
+ if singleton:
138
+ return len(self.genes)
139
+ else:
140
+ nbr = 0
141
+ for g in self.genes:
142
+ if not g.is_singleton():
143
+ nbr += 1
144
+ return nbr
145
+
146
+
147
+ class EvolutionaryConceptError(Exception):
148
+ pass