PTreeGenerator  1.0
Simple phylogenetic tree generation from multiple sequence alignment.
 All Classes Namespaces Files Functions Variables
visualization.py
Go to the documentation of this file.
1 ## @package visualization
2 # Contains just the ptreegen::visualization::Visualization class.
3 #
4 
5 import codecs
6 import os
7 
8 from ete2 import TreeStyle
9 
10 from enums import *
11 
12 
13 ##
14 # Similar to the ptreegen::computation::Computation class.
15 # It manages the visual components of the computation process
16 # and displays results to the user.
17 #
19 
20  ##
21  # Besides member initialization,
22  # it handles initial styling of the tree.
23  #
24  # @param tree input phylogenetic tree that will be processed
25  # @param options set of options (same set as in the case of
26  # the ptreegen::computation::Computation class)
27  def __init__(self, tree, options):
28  self._tree = tree
29  self._options = options
31  self._storageDir = None
32  self._filePrefix = None
33  self._style = TreeStyle()
34  self._style.show_leaf_name = True
35  self._style.show_branch_length = False
36  self._style.scale = 80
37  self._style.branch_vertical_margin = 15
38  self._style.rotation = 90
39  self._style.arc_start = -180
40  self._style.arc_span = 180
41 
42  self.parseOptions(options)
43 
44  ##
45  # Parses options selected by the user
46  # and prepares the necessary workflow.
47  #
48  # @param options set of options (same set as in the case of
49  # the ptreegen::computation::Computation class)
50  def parseOptions(self, options):
51  self._storageDir = os.path.dirname(options["alignment_file"])
52  self._filePrefix = os.path.basename(options["alignment_file"])
53  if '.' in self._filePrefix:
54  self._filePrefix = self._filePrefix[0:self._filePrefix.rfind('.')]
55  out_formats = options['out_form'].split(',')
56  for form in out_formats:
57  if form == OutputForm.NEWICK:
58  self._formatingMethods.append(self.saveNewick)
59  elif form == OutputForm.IMAGE_PNG:
60  self._formatingMethods.append(self.savePNG)
61  elif form == OutputForm.IMAGE_SVG:
62  self._formatingMethods.append(self.saveSVG)
63  elif form == OutputForm.GUI:
64  self._formatingMethods.append(self.showGUI)
65  elif form == OutputForm.PRINT:
66  self._formatingMethods.append(self.printToStdout)
67  else:
68  raise RuntimeError("Unknown output format: " + form)
69  if options['tree_type'] == TreeType.CIRC:
70  self._style.mode = 'c'
71  elif options['tree_type'] == TreeType.RECT:
72  self._style.mode = 'r'
73  else:
74  raise RuntimeError("Unknown tree type: " + options['tree_type'])
75 
76  ##
77  # Creates a file in the same
78  # directory as the input alignment
79  # and saves the tree in the Newick format.
80  #
81  def saveNewick(self):
82  file_path = os.path.join(self._storageDir, self._filePrefix + ".newick")
83  with codecs.open(file_path, "w", "utf-8") as outfile:
84  outfile.write(self._tree.write(format=5))
85  ##
86  # Creates a file in the same
87  # directory as the input alignment
88  # and saves the tree as a PNG image.
89  #
90  def savePNG(self):
91  file_path = os.path.join(self._storageDir, self._filePrefix + ".png")
92  self._tree.render(file_path, tree_style=self._style, dpi=300)
93 
94  ##
95  # Creates a file in the same
96  # directory as the input alignment
97  # and saves the tree as an SVG image.
98  #
99  def saveSVG(self):
100  file_path = os.path.join(self._storageDir, self._filePrefix + ".svg")
101  self._tree.render(file_path, tree_style=self._style, dpi=300)
102 
103  ##
104  # Shows the tree in a windowed
105  # viewer app.
106  #
107  def showGUI(self):
108  self._tree.show(tree_style=self._style)
109 
110  ##
111  # Prints the tree topology to stdout.
112  #
113  def printToStdout(self):
114  print self._tree
115 
116  ##
117  # Executes the workflow specified
118  # by the Visualization::_formatingMethods member.
119  #
120  # \sa Visualization::_formatingMethods
121  #
122  def show(self):
123  for func in self._formatingMethods:
124  func()
125 
126  ##
127  # @var _tree
128  # reference to the tree being displayed
129  # @var _options
130  # set of options (same set as in the case of
131  # the ptreegen::computation::Computation class)
132  # @var _formatingMethods
133  # A list of function pointers that represents
134  # the workflow of operations.
135  # @var _storageDir
136  # the output directory
137  # @var _filePrefix
138  # prefix for the output files
139  # @var _style
140  # Instance representing the rendered
141  # style of the tree.
142  #