11# We will need to create Qt4 items
2- from PyQt4 import QtCore
3- from PyQt4 . QtGui import QGraphicsRectItem , QColor , QPen , QBrush
4- from PyQt4 . QtGui import QGraphicsSimpleTextItem , QFont
2+ from ... treeview . qt import QtCore
3+ from ... treeview . qt import QGraphicsRectItem , QColor , QPen , QBrush
4+ from ... treeview . qt import QGraphicsSimpleTextItem , QFont
55
6- from ... import faces , TreeStyle , PhyloTree , TextFace
6+ from ... import faces , TreeStyle , PhyloTree , TextFace , SequenceFace
77from random import random
88
99_aafgcolors = {
8787}
8888
8989
90- class MySequenceFace (faces .StaticItemFace ):
91- """ Creates a new molecular sequence face object.
92-
93-
94- :argument seq: Sequence string to be drawn
95- :argument seqtype: Type of sequence: "nt" or "aa"
96- :argument fsize: Font size, (default=10)
97-
98- You can set custom colors for amino-acids or nucleotides:
99-
100- :argument None codon : a string that corresponds to the reverse translation of the amino-acid sequence
101- :argument 11 col_w : width of the column (if col_w is lower than font size, letter wont be displayed)
102- :argument None fg_colors : dictionary of colors for foreground, with as keys each possible character in sequences, and as value the colors
103- :argument None bg_colors : dictionary of colors for background, with as keys each possible character in sequences, and as value the colors
104- :argument 3 alt_col_w : works together with special_col option, defines the width of given columns
105- :argument None special_col : list of lists containing the bounds of columns to be displayed with alt_col_w as width
106- :argument False interactive : more info can be displayed when mouse over sequence
107-
108- """
109- def __init__ (self , seq , seqtype = "aa" , fsize = 10 ,
110- fg_colors = None , bg_colors = None ,
111- codon = None , col_w = 11 , alt_col_w = 3 ,
112- special_col = None , interactive = False ):
113- self .seq = seq
114- self .codon = codon
115- self .fsize = fsize
116- self .style = seqtype
117- self .col_w = float (col_w )
118- self .alt_col_w = float (alt_col_w )
119- self .special_col = special_col if special_col else []
120- self .width = 0 # will store the width of the whole sequence
121- self .interact = interactive
122-
123- if self .style == "aa" :
124- if not fg_colors :
125- fg_colors = _aafgcolors
126- if not bg_colors :
127- bg_colors = _aabgcolors
128- else :
129- if not fg_colors :
130- fg_colors = _ntfgcolors
131- if not bg_colors :
132- bg_colors = _ntbgcolors
133-
134- self .fg_col = self .__init_col (fg_colors )
135- self .bg_col = self .__init_col (bg_colors )
136-
137- # for future?
138- self .row_h = 13.0
139-
140- super (MySequenceFace ,
141- self ).__init__ (QGraphicsRectItem (0 , 0 , self .width , self .row_h ))
142-
143- def __init_col (self , color_dic ):
144- """to speed up the drawing of colored rectangles and characters"""
145- new_color_dic = {}
146- for car in color_dic :
147- new_color_dic [car ] = QBrush (QColor (color_dic [car ]))
148- return new_color_dic
149-
150- def update_items (self ):
151- #self.item = QGraphicsRectItem(0,0,self._total_w, self.row_h)
152- seq_width = 0
153- nopen = QPen (QtCore .Qt .NoPen )
154- font = QFont ("Courier" , self .fsize )
155- rect_cls = self .InteractiveLetterItem if self .interact else QGraphicsRectItem
156- for i , letter in enumerate (self .seq ):
157- width = self .col_w
158- for m in self .special_col :
159- if m [0 ] < i <= m [1 ]:
160- width = self .alt_col_w
161- break
162- #load interactive item if called correspondingly
163- rectItem = rect_cls (0 , 0 , width , self .row_h , parent = self .item )
164- rectItem .setX (seq_width ) # to give correct X to children item
165- rectItem .setBrush (self .bg_col [letter ])
166- rectItem .setPen (nopen )
167- if self .interact :
168- if self .codon :
169- rectItem .codon = '%s, %d: %s' % (self .seq [i ], i ,
170- self .codon [i * 3 :i * 3 + 3 ])
171- else :
172- rectItem .codon = '%s, %d' % (self .seq [i ], i )
173- # write letter if enough space
174- if width >= self .fsize :
175- text = QGraphicsSimpleTextItem (letter , parent = rectItem )
176- text .setFont (font )
177- text .setBrush (self .fg_col [letter ])
178- # Center text according to rectItem size
179- tw = text .boundingRect ().width ()
180- th = text .boundingRect ().height ()
181- text .setPos ((width - tw )/ 2 , (self .row_h - th )/ 2 )
182- seq_width += width
183- self .width = seq_width
184-
185- class InteractiveLetterItem (QGraphicsRectItem ):
186- """This is a class"""
187- def __init__ (self , * arg , ** karg ):
188- QGraphicsRectItem .__init__ (self , * arg , ** karg )
189- self .codon = None
190- self .label = None
191- self .setAcceptsHoverEvents (True )
192-
193- def hoverEnterEvent (self , e ):
194- """ when mouse is over"""
195- if not self .label :
196- self .label = QGraphicsRectItem (parent = self )
197- #self.label.setY(-18)
198- self .label .setX (11 )
199- self .label .setBrush (QBrush (QColor ("white" )))
200- self .label .text = QGraphicsSimpleTextItem (parent = self .label )
201-
202- self .setZValue (1 )
203- self .label .text .setText (self .codon )
204- self .label .setRect (self .label .text .boundingRect ())
205- self .label .setVisible (True )
206-
207- def hoverLeaveEvent (self , e ):
208- """when mouse leaves area"""
209- if self .label :
210- self .label .setVisible (False )
211- self .setZValue (0 )
212-
213-
21490
21591def test_layout_evol (node ):
21692 '''
@@ -221,7 +97,7 @@ def test_layout_evol(node):
22197 node .img_style ["draw_descendants" ]= False
22298 if node .is_leaf ():
22399 if hasattr (node , "sequence" ):
224- seqface = MySequenceFace (node .sequence , "aa" ,
100+ seqface = SequenceFace (node .sequence , "aa" ,
225101 codon = node .nt_sequence , fsize = 10 ,
226102 col_w = 11 , interactive = True )
227103 faces .add_face_to_node (seqface , node , 1 , aligned = True )
@@ -235,7 +111,7 @@ def test_layout_phylo_aa(node):
235111 node .img_style ["draw_descendants" ]= False
236112 if node .is_leaf ():
237113 if hasattr (node , "sequence" ):
238- seqface = MySequenceFace (node .sequence , "aa" ,
114+ seqface = SequenceFace (node .sequence , "aa" ,
239115 fsize = 10 ,
240116 col_w = 11 , interactive = False )
241117 faces .add_face_to_node (seqface , node , 1 , aligned = True )
@@ -251,7 +127,7 @@ def test_layout_phylo_aa_motif(node):
251127 special_col = [[10 ,100 ],[150 ,1000 ],[1000 ,2000 ],[3000 ,4990 ]]
252128 if node .is_leaf ():
253129 if hasattr (node , "sequence" ):
254- seqface = MySequenceFace (node .sequence , "aa" ,
130+ seqface = SequenceFace (node .sequence , "aa" ,
255131 fsize = 10 ,special_col = special_col ,
256132 alt_col_w = 3 ,
257133 col_w = 11 , interactive = True )
@@ -267,7 +143,7 @@ def test_layout_phylo_nt(node):
267143 node .img_style ["draw_descendants" ]= False
268144 if node .is_leaf ():
269145 if hasattr (node , "sequence" ):
270- seqface = MySequenceFace (node .sequence , "nt" ,
146+ seqface = SequenceFace (node .sequence , "nt" ,
271147 fsize = 10 ,
272148 col_w = 11 , interactive = True )
273149 faces .add_face_to_node (seqface , node , 1 , aligned = True )
0 commit comments