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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| #!/usr/bin/env python
import os
import sys
import tempfile
import shutil
import glob
import sys
import subprocess
import Pmw
import Tkinter
#from pymol import cmd, stored, util
lst_items = []
started = False
start_pos = None
moved = False
lst_sel = set()
def addSequence(name, seq, canvas, coord):
"""
brief add sequence to canvas and tag it
param name of sequence
param seq a string containing the sequence in amino or protein block
param canvas the canvas object to add widget
param coord position on y to place the sequence
"""
for ite, aa in enumerate(seq):
tag_name_position = name + "_" + str(ite)
t=canvas.create_text((10*ite),\
coord,\
anchor = 'nw',
text = aa,\
font = ("Courier", 12),\
activefill="red",\
tags=(name, tag_name_position+"_s","aaa",)\
)
canvas.create_rectangle(10*ite,\
coord+2,\
10+10*ite,\
coord+15,\
fill="red",\
tags=(name, tag_name_position+"_b", "bg"),\
state=Tkinter.HIDDEN\
)
print t
#canvas.tag_bind(t, '<Any-Motion>', lambda event, item=t:select_residue3(event, item))
"""
We assigned tags
name -> name of the sequence
tag_name_position_x tag contening all information in one tag. x equals to s if it is sequence or b is
it is the rectangle in background
bg -> only for rectangle to control the position in background
"""
#======================================================================
# put all rectangle in background
#======================================================================
canvas.tag_lower("bg")
return None
def start(event=None):
print event.num
global started, start_pos
start_pos = event.x, event.y
started = True
print("click started")
def end(event=None):
can = event.widget
global moved, started, lst_sel
print("click ended")
started = False
#===========================================================================
# we click just one time without move
#===========================================================================
if moved==False:
a,b = start_pos
c,d = event.x, event.y
tmp = can.find_overlapping(a,b,c,d)
for item in tmp:
#===================================================================
# already selected? -> we unhighligth and remove
#===================================================================
if item in lst_sel:
tags = can.gettags(item)
type, num, _ = tags[1].split("_")
cur_item_sq = can.find_withtag("sq%s_%s_b"%(type[-1], num))
can.itemconfig(cur_item_sq, state = Tkinter.HIDDEN)#, tags = tuple(list(tag_sq)+["sel"]))
cur_item_sq = can.find_withtag("pb%s_%s_b"%(type[-1], num))
can.itemconfig(cur_item_sq, state = Tkinter.HIDDEN)#, tags = tuple(list(tag_sq)+["sel"]))
lst_sel.remove(item)
print("removeeeeeeeeeeeee 1")
#===================================================================
# a new one
#===================================================================
else:
tags = can.gettags(item)
type, num, _ = tags[1].split("_")
cur_item_sq = can.find_withtag("sq%s_%s_b"%(type[-1], num))
can.itemconfig(cur_item_sq, state = Tkinter.NORMAL)#, tags = tuple(list(tag_sq)+["sel"]))
cur_item_sq = can.find_withtag("pb%s_%s_b"%(type[-1], num))
can.itemconfig(cur_item_sq, state = Tkinter.NORMAL)#, tags = tuple(list(tag_sq)+["sel"]))
lst_sel.add(item)
#=======================================================================
# click out of the alignment
#=======================================================================
if len(tmp)==0:
for item in lst_sel:
tags = can.gettags(item)
type, num, _ = tags[1].split("_")
cur_item_sq = can.find_withtag("sq%s_%s_b"%(type[-1], num))
can.itemconfig(cur_item_sq, state = Tkinter.HIDDEN)#, tags = tuple(list(tag_sq)+["sel"]))
cur_item_sq = can.find_withtag("pb%s_%s_b"%(type[-1], num))
can.itemconfig(cur_item_sq, state = Tkinter.HIDDEN)#, tags = tuple(list(tag_sq)+["sel"]))
print("removeeeeeeeeeeeee 2")
lst_sel = set()
print lst_sel
moved = False
def update(event=None):
can = event.widget
global started,moved, lst_sel
#===========================================================================
# we already clicked
#===========================================================================
if started:
print("move")
a,b = start_pos
c,d = event.x, event.y
tmp = can.find_overlapping(a,b,c,d)
if len(tmp)!=0:
moved = True
pass
for item in tmp:
tags = can.gettags(item)
type, num, _ = tags[1].split("_")
cur_item_sq = can.find_withtag("sq%s_%s_b"%(type[-1], num))
can.itemconfig(cur_item_sq, state = Tkinter.NORMAL)#, tags = tuple(list(tag_sq)+["sel"]))
cur_item_sq = can.find_withtag("pb%s_%s_b"%(type[-1], num))
can.itemconfig(cur_item_sq, state = Tkinter.NORMAL)#, tags = tuple(list(tag_sq)+["sel"]))
lst_sel.add(item)
def highlight(item, can):
"""
"""
global list_sel_item
tags = can.gettags(item)
type, num, _ = tags[1].split("_")
cur_item_sq = can.find_withtag("sq%s_%s_b"%(type[-1], num))
cur_item_pb = can.find_withtag("pb%s_%s_b"%(type[-1], num))
tag_sq = can.gettags("sq%s_%s_b"%(type[-1], num))
tag_pb = can.gettags("pb%s_%s_b"%(type[-1], num))
if "sel" not in tags:
can.itemconfig(cur_item_sq, state = Tkinter.NORMAL, tags = tuple(list(tag_sq)+["sel"]))
can.itemconfig(cur_item_pb, state = Tkinter.NORMAL, tags = tuple(list(tag_pb)+["sel"]))
list_sel_item.append(cur_item_sq)
list_sel_item.append(cur_item_pb)
print cur_item_sq, cur_item_pb
else:
new_tag_sq = list(tag_sq)
new_tag_sq.remove("sel")
new_tag_pb = list(tag_pb)
new_tag_pb.remove("sel")
can.itemconfig(cur_item_sq, state = Tkinter.HIDDEN, tags = tuple(new_tag_sq))
can.itemconfig(cur_item_pb, state = Tkinter.HIDDEN, tags = tuple(new_tag_pb))
list_sel_item.remove(cur_item_sq)
list_sel_item.remove(cur_item_pb)
print "rem", cur_item_sq, cur_item_pb
#canvas.dtag(cur_item, "sel")
#canvas.itemconfig(cur_item, state = Tkinter.NORMAL, tags = tuple(list(cur_tag)+["sel"]))
def unselect(can):
"""
"""
global list_sel_item
print list_sel_item
for sel_item in list_sel_item:
tags = can.gettags(sel_item)
new_tag = list(tags)
new_tag.remove("sel")
can.itemconfig(sel_item, state = Tkinter.HIDDEN, tags = tuple(new_tag))
list_sel_item = []
def getOtherSequence( sequence):
"""
brief select all sequence except one given
param sequence to unselect
return allsequence is a list with other aa in other sequence but with the same position
"""
allsequence = []
for i in ["1", "2"]:
if i==sequence:
continue
else:
allsequence.append(i)
return allsequence
parent = Tkinter.Tk()#Tkinter.Toplevel()
scrollbar = Tkinter.Scrollbar(parent, orient=Tkinter.HORIZONTAL)#, width = 50,)
canvas = Tkinter.Canvas(parent,\
#bd=2,\
width = 1000,
height = 80,
)
canvas.grid(row=1, column=1, sticky=Tkinter.W)#, padx = 5)#, pady = 2)
scrollbar.grid(row=2, column=1, sticky=Tkinter.W+Tkinter.E)#, padx = 5)#, pady = 2)#side=Tkinter.BOTTOM, fill=Tkinter.X)
addSequence("pb1", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", canvas, 0)
addSequence("sq1", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", canvas, 15)
addSequence("pb2", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", canvas, 45)
addSequence("sq2", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", canvas, 60)
#===============================================================================
canvas.bind('<ButtonPress>', start)
canvas.bind('<ButtonRelease>', end)
canvas.bind('<Motion>', update)
canvas.config(xscrollcommand=scrollbar.set)
scrollbar.config(command= canvas.xview)
#Tkinter.Sizegrip(parent)
#canvas.xview_moveto(0.0)
canvas.config(scrollregion=canvas.bbox(Tkinter.ALL))
parent.mainloop() |
Partager