forked from mengqvist/DNApy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeaturelist_GUI.py
More file actions
360 lines (274 loc) · 11.7 KB
/
Copy pathfeaturelist_GUI.py
File metadata and controls
360 lines (274 loc) · 11.7 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python
#DNApy is a DNA editor written purely in python.
#The program is intended to be an intuitive and fully featured
#editor for molecular and synthetic biology.
#Enjoy!
#
#copyright (C) 2014-2015 Martin Engqvist |
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#LICENSE:
#
#DNApy 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.
#
#DNApy 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 Library General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software Foundation,
#Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#Get source code at: https://github.com/mengqvist/DNApy
#
#TODO
#nothing at the moment
import ast
from wx.lib.agw import ultimatelistctrl as ULC
import wx
from wx.lib.pubsub import setupkwargs #this line not required in wxPython2.9.
#See documentation for more detail
from wx.lib.pubsub import pub
import sys, os
import string
import copy
import colcol
import genbank
from base_class import DNApyBaseClass
import featureedit_GUI
files={} #list with all configuration files
files['default_dir'] = os.path.abspath(os.path.dirname(sys.argv[0]))+"/"
files['default_dir']=string.replace(files['default_dir'], "\\", "/")
files['default_dir']=string.replace(files['default_dir'], "library.zip", "")
settings=files['default_dir']+"settings" ##path to the file of the global settings
execfile(settings) #gets all the pre-assigned settings
class FeatureList(DNApyBaseClass):
"""
Class for viewing features as a list and buttons for editing the features.
"""
def __init__(self, parent, id):
wx.Panel.__init__(self, parent)
self.feature_list = ULC.UltimateListCtrl(self, id=3001, agwStyle=ULC.ULC_REPORT|ULC.ULC_SINGLE_SEL)
self.feature_list.InsertColumn(0, "Feature", format=wx.LIST_FORMAT_LEFT, width=200)
self.feature_list.InsertColumn(1, "Type", format=wx.LIST_FORMAT_LEFT, width=100)
self.feature_list.InsertColumn(2, "Location on DNA", format=wx.LIST_FORMAT_LEFT, width=200)
self.feature_list.InsertColumn(3, "Strand", format=wx.LIST_FORMAT_LEFT, width=120)
font = wx.Font(pointSize=10, family=wx.FONTFAMILY_DEFAULT, style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_NORMAL, underline=False, faceName='Monospace', encoding=wx.FONTENCODING_DEFAULT)
self.feature_list.SetFont(font)
self.feature_list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.ListOnSelect)
self.feature_list.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.ListOnActivate)
#buttons
padding = 10 #how much to add around the picture
imageFile = files['default_dir']+"/icon/new_small.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
newfeature = wx.BitmapButton(self, id=1, bitmap=image1, size = (image1.GetWidth()+padding, image1.GetHeight()+padding), name = "share")
imageFile = files['default_dir']+"/icon/remove_small.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
deletefeature = wx.BitmapButton(self, id=2, bitmap=image1, size = (image1.GetWidth()+padding, image1.GetHeight()+padding), name = "share")
imageFile = files['default_dir']+"/icon/move_up.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
moveup = wx.BitmapButton(self, id=4, bitmap=image1, size = (image1.GetWidth()+padding, image1.GetHeight()+padding), name = "share")
imageFile = files['default_dir']+"/icon/move_down.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
movedown = wx.BitmapButton(self, id=5, bitmap=image1, size = (image1.GetWidth()+padding, image1.GetHeight()+padding), name = "share")
imageFile = files['default_dir']+"/icon/edit.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
edit = wx.BitmapButton(self, id=6, bitmap=image1, size = (image1.GetWidth()+padding, image1.GetHeight()+padding), name = "edit")
#bind feature list buttons
self.Bind(wx.EVT_BUTTON, self.OnNew, id=1)
self.Bind(wx.EVT_BUTTON, self.OnDelete, id=2)
self.Bind(wx.EVT_BUTTON, self.OnMoveFeatureUp, id=4)
self.Bind(wx.EVT_BUTTON, self.OnMoveFeatureDown, id=5)
self.Bind(wx.EVT_BUTTON, self.OnEditFeature, id=6)
#arrange buttons vertically
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(item=newfeature)
sizer.Add(item=deletefeature)
sizer.Add(item=edit)
sizer.Add(item=moveup)
sizer.Add(item=movedown)
#add feature list and buttons horisontally
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(item=sizer, proportion=0, flag=wx.EXPAND)
sizer2.Add(item=self.feature_list, proportion=3, flag=wx.EXPAND)
self.SetSizer(sizer2)
####### Modify methods from base calss to fit current needs #########
def update_globalUI(self):
'''
Method should be modified as to update other panels in response to changes in own panel.
'''
MSG_CHANGE_TEXT = "change.text"
pub.sendMessage(MSG_CHANGE_TEXT, text="Feature list says update!")
def update_ownUI(self):
'''
Refreshes only the UI of this panel by re-filling the table from features stored in the genbank object
'''
#need to figure out how to do this without changing the selection....
self.feature_list.DeleteAllItems()
item = 0 #for feautrecolor
features = genbank.gb.get_all_features()
if features != None:
for entry in features:
# print(entry)
if len(entry['qualifiers']) == 0:
col0 = entry['key']
else:
col0 = entry['qualifiers'][0].split('=')[1]
col1 = entry['key']
locationstring = ''
for location in entry['location']:
if locationstring != '':
locationstring += ', '
locationstring += str(location)
col2 = locationstring
if entry['complement'] == True:
col3 = 'complement'
else:
col3 = 'leading'
# col4 = entry['qualifiers']
self.feature_list.Append([col0, col1, col2, col3])
#coloring
self.get_feature_color(entry)
hexcolor = self.current_highlight_color #get hex color
r, g, b = colcol.hex_to_rgb(hexcolor) #convert to RGB
color = wx.Colour(r, g, b) #make color object
self.feature_list.SetItemBackgroundColour(item, color)
item += 1
try:
if genbank.feature_selection != None: #focus on the selected feature
self.focus_feature_selection()
except:
pass
######################################################
def ListOnSelect(self, event):
'''Updates selection depending on which feature is chosen'''
index = self.feature_list.GetFirstSelected()
genbank.feature_selection = copy.copy(index)
def ListOnActivate(self, event):
'''Updates feature AND DNA selection based on which feature is chosen'''
index = self.feature_list.GetFirstSelected()
genbank.feature_selection = copy.copy(index)
locations = genbank.gb.get_feature_location(index)
start = genbank.gb.get_location(locations[0])[0]
finish = genbank.gb.get_location(locations[-1])[1]
genbank.dna_selection = copy.copy((start, finish))
self.update_globalUI()
######
######
######
######
def OnNew(self, event):
'''Make new feature'''
#make feature and update interface
start, finish = self.get_dna_selection()
genbank.gb.add_feature(key='misc_feature', qualifiers=['/note=New feature'], location=['%s..%s' % (start, finish)], complement=False, join=False, order=False)
genbank.feature_selection = copy.copy(len(genbank.gb.get_all_features())-1)
dlg = featureedit_GUI.FeatureEditDialog(None, 'New Feature') # creation of a dialog with a title
dlg.Center()
dlg.ShowModal()
self.update_ownUI() #update the feature view
self.update_globalUI()
def OnDelete(self, event):
'''Delete selected feature'''
index = self.feature_list.GetFirstSelected()
feature = genbank.gb.get_feature(index)
genbank.gb.remove_feature(feature)
self.update_ownUI() #update the feature view
self.update_globalUI()
#set highlight, focus and selection
if index == self.feature_list.GetItemCount():
index = index-1
if index < 0:
self.update_feature_selection(None)
else:
self.update_feature_selection(index)
def OnMoveFeatureUp(self, event):
'''Move feature up one step'''
index = self.feature_list.GetFirstSelected()
feature = genbank.gb.get_feature(index)
genbank.gb.move_feature(feature, 'u')
self.update_ownUI()
#set highlight, focus and selection
if index != 0:
index = index-1
self.update_feature_selection(index)
def OnMoveFeatureDown(self, event):
'''Move feature up down step'''
index = self.feature_list.GetFirstSelected()
feature = genbank.gb.get_feature(index)
genbank.gb.move_feature(feature, 'd')
self.update_ownUI()
#set highlight, focus and selection
if index != self.feature_list.GetItemCount()-1:
index = index+1
self.update_feature_selection(index)
def OnEditFeature(self, event):
'''Edit a feature that is already present'''
dlg = featureedit_GUI.FeatureEditDialog(None, 'Edit Feature') # creation of a dialog with a title
dlg.Center()
dlg.ShowModal()
self.update_ownUI() #update the feature view
self.update_globalUI()
def focus_feature_selection(self):
index = copy.copy(genbank.feature_selection)
self.feature_list.SetItemState(item=index, state=ULC.ULC_STATE_SELECTED, stateMask=wx.LIST_STATE_SELECTED) #for the highlight
self.feature_list.Select(index, True) #to select it
self.feature_list.Focus(index) #to focus it
def update_feature_selection(self, index):
'''Updates which feature is selected'''
genbank.feature_selection = copy.copy(index)
if index == None:
pass
else:
self.focus_feature_selection()
def OnCopyFASTA(self, event):
pass
def OnCopyDNA(self, event):
pass
def OnCopyTranslation(self, event):
pass
def get_feature_color(self, feature):
'''Takes single feature and finds its color, if any'''
#piece of code to check if there are assigned colors to the features
try:
Key = eval(feature['key'])
except:
Key = grey
complement = feature['complement']
if complement == True: self.current_highlight_color = Key['rv']
else: self.current_highlight_color = Key['fw']
def get_dna_selection(self):
'''This method is needed to get the dna selection for creating new features.'''
return genbank.dna_selection
######################################
######################################
##### main loop
class MyApp(wx.App):
def OnInit(self):
frame = wx.Frame(None, -1, title="Feature List", size=(700,500))
panel = FeatureList(frame, -1)
frame.Centre()
frame.Show(True)
self.SetTopWindow(frame)
panel.update_ownUI()
return True
if __name__ == '__main__': #if script is run by itself and not loaded
files={} #list with all configuration files
files['default_dir'] = os.path.abspath(os.path.dirname(sys.argv[0]))+"/"
files['default_dir']=string.replace(files['default_dir'], "\\", "/")
files['default_dir']=string.replace(files['default_dir'], "library.zip", "")
settings=files['default_dir']+"settings" ##path to the file of the global settings
execfile(settings) #gets all the pre-assigned settings
genbank.dna_selection = (1, -1) #variable for storing current DNA selection
genbank.feature_selection = False #variable for storing current feature selection
import sys
assert len(sys.argv) == 2, 'Error, this script requires a path to a genbank file as an argument.'
print('Opening %s' % str(sys.argv[1]))
genbank.gb = genbank.gbobject(str(sys.argv[1])) #make a genbank object and read file
app = MyApp(0)
app.MainLoop()