PTreeGenerator  1.0
Simple phylogenetic tree generation from multiple sequence alignment.
 All Classes Namespaces Files Functions Variables
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ptreegen.parsimony.SmallParsimony Class Reference

Represents a solution to the small parsimony problem (tree is known and we are intersted in the parsimony score of the tree). More...

Public Member Functions

def __init__
 Constructor takes tree and a corresponding alignment as input and saves references to them as SmallParsimony::_tree and SmallParsimony::_alignment.
def cost
 A getter for the cost value.

Private Member Functions

def _solve
 Iteraterates over each column of the alignment and computes the parsimony score for each.
def _assign
 Recursive method implementing the Fitch's algorithm.

Private Attributes

 _tree
 Refernce to the tree being scored.
 _alignment
 Refernce to the coresponding alignment.
 _sequencesDict
 Associates sequence id string with a sequence instance.
 _treeCharacterDict
 A dictionary that stores the character sets asigned to each node everytime the SmallParsimony::_assign() method is executed for each column of the alignment.
 _cost
 Variable to store the intermediate results.

Detailed Description

Represents a solution to the small parsimony problem (tree is known and we are intersted in the parsimony score of the tree).

It implements the Fitch's algorithm to score the tree, therefore the input tree should be a rooted binary tree, even though this implementation is extended to work with any type of tree.

See Also
LargeParsimony

Definition at line 26 of file parsimony.py.

Constructor & Destructor Documentation

def ptreegen.parsimony.SmallParsimony.__init__ (   self,
  tree,
  alignment 
)

Constructor takes tree and a corresponding alignment as input and saves references to them as SmallParsimony::_tree and SmallParsimony::_alignment.

Parameters
treethe tree to be scored
alignmentalignment corresponding to the input tree

Definition at line 35 of file parsimony.py.

35 
36  def __init__(self, tree, alignment):
37  self._tree = tree
38  self._alignment = alignment
39  self._sequencesDict = {x.id : x.seq for x in list(alignment)}
40  leaf_names = set(tree.get_leaf_names())
41  if len(self._sequencesDict) != len(leaf_names):
42  raise RuntimeError("Number of sequnces in alignment:\n" + repr(alignment)
43  + "\ndoes not match the number of leaves in tree:\n" + repr(tree))
44  for seq_id in self._sequencesDict:
45  if seq_id not in leaf_names:
46  raise RuntimeError("Sequence ID (" + seq_id
47  + ") does not match any leaf in tree:\n" + repr(tree))
48 
49  self._treeCharacterDict = dict()
50  self._cost = float('inf')

Member Function Documentation

def ptreegen.parsimony.SmallParsimony._assign (   self,
  node,
  site_idx 
)
private

Recursive method implementing the Fitch's algorithm.

It traverses the tree from an arbitrary node (preferably root) and computes the overall parsimony score.

Parameters
nodethe node to start traversing the tree from (preferably root)
site_idxindex of the column in the alignment that is being processed

Definition at line 81 of file parsimony.py.

References ptreegen.parsimony.SmallParsimony._assign(), ptreegen.parsimony.SmallParsimony._cost, ptreegen.parsimony.SmallParsimony._sequencesDict, and ptreegen.parsimony.SmallParsimony._treeCharacterDict.

Referenced by ptreegen.parsimony.SmallParsimony._assign(), and ptreegen.parsimony.SmallParsimony._solve().

81 
82  def _assign(self, node, site_idx):
83  if node.is_leaf():
84  self._treeCharacterDict[node] = set(self._sequencesDict[node.name][site_idx])
85  else:
86  for child in node.children:
87  self._assign(child, site_idx)
88 
89  character_set = set(self._treeCharacterDict[node.children[0]])
90  for child in node.children:
91  character_set.intersection_update(self._treeCharacterDict[child])
92 
93  if character_set:
94  self._treeCharacterDict[node] = character_set
95  else:
96  for child in node.children:
97  character_set.update(self._treeCharacterDict[child])
98  self._treeCharacterDict[node] = character_set
self._cost += 1
def ptreegen.parsimony.SmallParsimony._solve (   self)
private

Iteraterates over each column of the alignment and computes the parsimony score for each.

It calls the SmallParsimony::_assign() method to score each column and add the score to the SmallParsimony::_cost member.

Definition at line 69 of file parsimony.py.

References ptreegen.parsimony.SmallParsimony._assign(), ptreegen.parsimony.SmallParsimony._tree, and ptreegen.parsimony.SmallParsimony._treeCharacterDict.

Referenced by ptreegen.parsimony.SmallParsimony.cost().

69 
70  def _solve(self):
71  for col_idx in range(self._alignment.get_alignment_length()):
72  self._treeCharacterDict = dict()
73  self._assign(self._tree, col_idx)
def ptreegen.parsimony.SmallParsimony.cost (   self)

A getter for the cost value.

Calls the SmallParsimony::_solve() method to compute it.

Returns
parsimony score as a single value

Definition at line 57 of file parsimony.py.

References ptreegen.parsimony.SmallParsimony._cost, and ptreegen.parsimony.SmallParsimony._solve().

57 
58  def cost(self):
59  self._cost = 0
60  self._solve()
61  return self._cost

Member Data Documentation

ptreegen.parsimony.SmallParsimony._alignment
private

Refernce to the coresponding alignment.

Definition at line 37 of file parsimony.py.

Referenced by ptreegen.parsimony.LargeParsimony.cost(), and ptreegen.parsimony.LargeParsimony.getOptimalQuartets().

ptreegen.parsimony.SmallParsimony._cost
private

Variable to store the intermediate results.

Definition at line 49 of file parsimony.py.

Referenced by ptreegen.parsimony.SmallParsimony._assign(), and ptreegen.parsimony.SmallParsimony.cost().

ptreegen.parsimony.SmallParsimony._sequencesDict
private

Associates sequence id string with a sequence instance.

Definition at line 38 of file parsimony.py.

Referenced by ptreegen.parsimony.SmallParsimony._assign().

ptreegen.parsimony.SmallParsimony._tree
private

Refernce to the tree being scored.

Definition at line 36 of file parsimony.py.

Referenced by ptreegen.parsimony.SmallParsimony._solve(), ptreegen.visualization.Visualization.printToStdout(), and ptreegen.parsimony.LargeParsimony.tree().

ptreegen.parsimony.SmallParsimony._treeCharacterDict
private

A dictionary that stores the character sets asigned to each node everytime the SmallParsimony::_assign() method is executed for each column of the alignment.

Definition at line 48 of file parsimony.py.

Referenced by ptreegen.parsimony.SmallParsimony._assign(), and ptreegen.parsimony.SmallParsimony._solve().


The documentation for this class was generated from the following file: