add comments to seqgen
@@ -13,6 +13,18 @@ class SeqGen(TreeAnc):
def sample_from_profile(self, p):
+ """returns a sequence sampled from a profile (column wise state probabilities)
+
+ Parameters
+ ----------
+ p : np.array
+ sequence profile with dimensions (L,q)
+
+ Returns
+ -------
+ np.array (character)
+ sequence as character array array(['A', 'C', 'G',...])
+ """
cum_p = p.cumsum(axis=1).T
prand = np.random.random(self.seq_len)
@@ -21,7 +33,17 @@ class SeqGen(TreeAnc):
def evolve(self, root_seq=None):
+ """generate sequences of all internal and terminal nodes given the
+ tree and the evolutionary model.
+
+ Parameters
+ ----------
+ root_seq : numpy character array, optional
+ sequence to be used as the root sequence of the tree. if not given,
+ will sample a sequence from the equilibrium probabilities of the GTR model.
+ """
self.seq_len = self.gtr.seq_len
+ # set root if not given
if root_seq:
self.tree.root.sequence = seq2array(root_seq)
else:
@@ -30,6 +52,7 @@ class SeqGen(TreeAnc):
else:
self.tree.root.sequence = self.sample_from_profile(np.repeat([self.gtr.Pi], self.seq_len, axis=0))
+ # generate sequences in preorder
for n in self.tree.get_nonterminals(order='preorder'):
profile_p = seq2prof(n.sequence, self.gtr.profile_map)
for c in n:
@@ -37,6 +60,7 @@ class SeqGen(TreeAnc):
c.sequence = self.sample_from_profile(profile)
self.make_reduced_alignment()
+ # gather mutations
for n in self.tree.find_clades():
if n==self.tree.root:
n.mutations=[]
@@ -45,6 +69,18 @@ class SeqGen(TreeAnc):
def get_aln(self, internal=False):
+ """helper function gathering the generated sequences into a Biopython alignment object
+
+ Parameters
+ ----------
+ internal : bool, optional
+ include sequences of internal nodes in the alignment
+
+ Returns
+ -------
+ Bio.Align.MultipleSeqAlignment
+ mutlple sequence alignment
+ """
from Bio import SeqRecord, Seq
from Bio.Align import MultipleSeqAlignment
