1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
from __future__ import absolute_import
# #START_LICENSE###########################################################
#
#
# This file is part of the Environment for Tree Exploration program
# (ETE). http://etetoolkit.org
#
# ETE is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ETE is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ETE. If not, see <http://www.gnu.org/licenses/>.
#
#
# ABOUT THE ETE PACKAGE
# =====================
#
# ETE is distributed under the GPL copyleft license (2008-2015).
#
# If you make use of ETE in published work, please cite:
#
# Jaime Huerta-Cepas, Joaquin Dopazo and Toni Gabaldon.
# ETE: a python Environment for Tree Exploration. Jaime BMC
# Bioinformatics 2010,:24doi:10.1186/1471-2105-11-24
#
# Note that extra references to the specific methods implemented in
# the toolkit may be available in the documentation.
#
# More info at http://etetoolkit.org. Contact: huerta@embl.de
#
#
# #END_LICENSE#############################################################
import types
import signal
from .qt import *
from .qt4_gui import _GUI, _PropertiesDialog, _BasicNodeActions
from . import layouts
from .main import save
from .qt4_render import _TreeScene, render, get_tree_img_map, init_tree_style
__all__ = ["show_tree", "render_tree"]
_QApp = None
GUI_TIMEOUT = None
def exit_gui(a, b):
_QApp.exit(0)
def init_scene(t, layout, ts):
global _QApp
ts = init_tree_style(t, ts)
if layout:
ts.layout_fn = layout
if not _QApp:
_QApp = QApplication(["ETE"])
scene = _TreeScene()
#ts._scale = None
return scene, ts
def show_tree(t, layout=None, tree_style=None, win_name=None):
""" Interactively shows a tree."""
scene, img = init_scene(t, layout, tree_style)
tree_item, n2i, n2f = render(t, img)
scene.init_values(t, img, n2i, n2f)
tree_item.setParentItem(scene.master_item)
scene.addItem(scene.master_item)
mainapp = _GUI(scene)
if win_name:
mainapp.setObjectName(win_name)
mainapp.show()
mainapp.on_actionFit2tree_triggered()
# Restore Ctrl-C behavior
signal.signal(signal.SIGINT, signal.SIG_DFL)
if GUI_TIMEOUT is not None:
signal.signal(signal.SIGALRM, exit_gui)
signal.alarm(GUI_TIMEOUT)
_QApp.exec_()
def render_tree(t, imgName, w=None, h=None, layout=None,
tree_style = None, header=None, units="px",
dpi=90):
""" Render tree image into a file."""
global _QApp
for nid, n in enumerate(t.traverse("preorder")):
n.add_feature("_nid", nid)
scene, img = init_scene(t, layout, tree_style)
tree_item, n2i, n2f = render(t, img)
scene.init_values(t, img, n2i, n2f)
tree_item.setParentItem(scene.master_item)
scene.master_item.setPos(0,0)
scene.addItem(scene.master_item)
if imgName.startswith("%%inline"):
imgmap = save(scene, imgName, w=w, h=h, units=units, dpi=dpi)
else:
x_scale, y_scale = save(scene, imgName, w=w, h=h, units=units, dpi=dpi)
imgmap = get_tree_img_map(n2i, x_scale, y_scale)
return imgmap
class RenderThread(QThread):
def __init__(self, tree, layout, tree_style, w, h, dpi, units, rformat):
self.tree = tree
self.layout = layout
self.tree_style = tree_style
self.w = w
self.h = h
self.dpi = dpi
self.units = units
self.return_format = rformat
self.result = None
QThread.__init__(self)
def run(self):
scene, img = init_scene(self.tree, self.layout, self.tree_style)
tree_item, n2i, n2f = render(self.tree, img)
scene.init_values(self.tree, img, n2i, n2f)
tree_item.setParentItem(scene.master_item)
scene.master_item.setPos(0, 0)
scene.addItem(scene.master_item)
x_scale, y_scale, imgdata = save(scene, self.return_format, w=self.w,
h=self.h, units=self.units, dpi=self.dpi)
if 'PNG' in self.return_format:
img_map = get_tree_img_map(n2i, x_scale, y_scale)
else:
img_map = {}
self.result = [imgdata, img_map]
def get_img(t, w=None, h=None, layout=None, tree_style = None,
header=None, units="px", dpi=90, return_format="%%return"):
global _QApp
if not _QApp:
_QApp = QApplication(["ETE"])
r = RenderThread(t, layout, tree_style, w, h, dpi, units, return_format)
r.start()
r.wait()
return r.result
|