allpy
changeset 5:76816df24fe2
dummy_pytale.py: file added
Can do: display an alignment, colour by identity.
Features: Both sequence names and sequences are TextCtrl. Extremely slow.
author | grishin@gorilla |
---|---|
date | Wed, 09 Jun 2010 14:46:02 +0400 |
parents | 0905a39738f2 |
children | a185e7e255b3 |
files | pytale/dummy_pytale.py |
diffstat | 1 files changed, 162 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pytale/dummy_pytale.py Wed Jun 09 14:46:02 2010 +0400 1.3 @@ -0,0 +1,162 @@ 1.4 +#!/usr/bin/python 1.5 + 1.6 +import wx 1.7 +import os.path 1.8 +import project 1.9 + 1.10 +class MainFrame(wx.Frame): 1.11 + """ Main application frame. 1.12 + 1.13 + The frame contains: 1.14 + 1) MenuBar 1.15 + 2) Resizable panel with ListBox containing sequence names 1.16 + 3) Panel with RichTextBox containing the alignment itself 1.17 + 4) StatusBar 1.18 + """ 1.19 + def __init__(self, parent, title, size=(1000, 600)): 1.20 + global settings 1.21 + wx.Frame.__init__(self, parent, title=title, size=size) 1.22 + self.current_project = None 1.23 + self.CreateStatusBar() 1.24 + # Draw panels and split the frame 1.25 + self.splitter = wx.SplitterWindow(self, -1) 1.26 + self.names_panel = SeqNamesPanel(self.splitter) 1.27 + self.sequences_panel = SequencesPanel(self.splitter) 1.28 + self.splitter.SplitVertically(self.names_panel, self.sequences_panel) 1.29 + self.splitter.SetSashGravity(0.0) # do not widen the name panel when resizing 1.30 + self.splitter.SetSashPosition(200, redraw=True) 1.31 + # Set up the menubar 1.32 + self.menubar = MenuBar() 1.33 + self.SetMenuBar(self.menubar) 1.34 + # Bindings (shouldn't menu bindings be in the menu class?) 1.35 + self.Bind(wx.EVT_MENU, self.Open, self.menubar.filemenu.menu_open) 1.36 + self.Bind(wx.EVT_MENU, self.Save, self.menubar.filemenu.menu_save) 1.37 + self.Bind(wx.EVT_MENU, self.Exit, self.menubar.filemenu.menu_exit) 1.38 + # Oh, shit, that's awful! 1.39 + self.Bind(wx.EVT_MENU, lambda event: \ 1.40 + self.sequences_panel.ColourByIdentity(event, self.current_project), \ 1.41 + self.menubar.colourmenu.by_identity) 1.42 + 1.43 + def Exit(self, event): 1.44 + self.Close(True) 1.45 + 1.46 + def Open(self, event): 1.47 + dialog = wx.FileDialog(self, "Choose a file...", '', '', "*.*", wx.OPEN) 1.48 + if dialog.ShowModal() == wx.ID_OK: 1.49 + self.filename = dialog.GetFilename() 1.50 + self.dirname = dialog.GetDirectory() 1.51 + file = open(os.path.join(self.dirname, self.filename)) 1.52 + # create and display the loaded project (fasta alignment) 1.53 + current_project = project.Project() 1.54 + current_project.sequences, current_project.alignment = \ 1.55 + project.Project.get_from_fasta(file) 1.56 + self.names_panel.DisplayNames(current_project) 1.57 + self.sequences_panel.DisplayAlignment(current_project) 1.58 + self.current_project = current_project 1.59 + file.close() 1.60 + dialog.Destroy() 1.61 + 1.62 + def Save(self, event): 1.63 + pass 1.64 + 1.65 +class MenuBar(wx.MenuBar): 1.66 + def __init__(self): 1.67 + wx.MenuBar.__init__(self) 1.68 + self.filemenu = FileMenu() 1.69 + self.colourmenu = ColourMenu() 1.70 + self.Append(self.filemenu, "&File") 1.71 + self.Append(self.colourmenu, "&Colour") 1.72 + 1.73 +class FileMenu(wx.Menu): 1.74 + def __init__(self): 1.75 + wx.Menu.__init__(self) 1.76 + self.menu_open = self.Append(wx.ID_OPEN, "&Open", "Open alignment") 1.77 + self.menu_save = self.Append(wx.ID_SAVE, "&Save", "Save alignment") 1.78 + self.menu_exit = self.Append(wx.ID_EXIT, "E&xit", "Quit the editor") 1.79 + 1.80 +class ColourMenu(wx.Menu): 1.81 + def __init__(self): 1.82 + wx.Menu.__init__(self) 1.83 + self.by_identity = self.Append(wx.ID_ANY, "By &Identity", "Colour by identity") 1.84 + 1.85 +class SeqNamesPanel(wx.Panel): 1.86 + def __init__(self, parent): 1.87 + wx.Panel.__init__(self, parent) 1.88 + global settings 1.89 + self.settings = settings 1.90 + self.box = wx.BoxSizer(wx.VERTICAL) 1.91 + self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_DONTWRAP) 1.92 + self.text.SetFont(self.settings.font) 1.93 + self.box.Add(self.text, 1, wx.EXPAND) 1.94 + self.SetSizer(self.box) 1.95 + self.SetAutoLayout(1) 1.96 + 1.97 + def DisplayNames(self, current_project): 1.98 + names = map(lambda seq: seq.name, current_project.sequences) 1.99 + for name in names: 1.100 + self.text.AppendText(name + '\n') 1.101 + 1.102 +class SequencesPanel(wx.Panel): 1.103 + def __init__(self, parent): 1.104 + wx.Panel.__init__(self, parent) 1.105 + global settings 1.106 + self.settings = settings 1.107 + self.lines = [] # to know which sequence resides in particular line 1.108 + self.box = wx.BoxSizer(wx.VERTICAL) 1.109 + self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_DONTWRAP) 1.110 + self.text.SetFont(self.settings.font) 1.111 + self.box.Add(self.text, 1, wx.EXPAND) 1.112 + self.SetSizer(self.box) 1.113 + self.SetAutoLayout(1) 1.114 + 1.115 + def DisplayAlignment(self, current_project): 1.116 + for aligned_seq in current_project.alignment: 1.117 + seq_string = '' 1.118 + for monomer in current_project.alignment[aligned_seq]: 1.119 + if monomer is not None: 1.120 + seq_string += monomer.code 1.121 + else: 1.122 + seq_string += '-' 1.123 + seq_string += '\n' 1.124 + self.lines.append(aligned_seq) 1.125 + self.text.AppendText(seq_string) 1.126 + 1.127 + def ColourByIdentity(self, event, current_project): 1.128 + # Looks awful but works. Fine for now. 1.129 + if not hasattr(current_project, 'identity_percentages'): 1.130 + current_project.calc_identity() 1.131 + print 'calculation done' 1.132 + thresholds = {1:0.9, 2:0.7, 3:0.5, 0:0.0} 1.133 + prj_len = len(current_project) 1.134 + for line in range(len(self.lines)): 1.135 + for column in range(prj_len): 1.136 + position = current_project.identity_percentages[self.lines[line]][column] 1.137 + for threshold in [0, 3, 2, 1]: 1.138 + if position > thresholds[threshold]: 1.139 + conser = threshold 1.140 + if position is None: 1.141 + continue 1.142 + start = self.text.XYToPosition(column, line) 1.143 + end = self.text.XYToPosition(column+1, line) 1.144 + self.text.SetStyle(start, end, self.settings.conservation_styles[conser]) 1.145 + print 'colouring done' 1.146 + 1.147 + 1.148 +class Settings(object): 1.149 + def __init__(self): 1.150 + self.font = wx.Font(pointSize=10, family=wx.FONTFAMILY_MODERN, \ 1.151 + style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_NORMAL) 1.152 + self.conservation_styles = { 1.153 + 1: wx.TextAttr(colText=wx.Colour(255, 255, 255), colBack=wx.Colour(0, 0, 0)), 1.154 + 2: wx.TextAttr(colBack=wx.Colour(150, 150, 150)), 1.155 + 3: wx.TextAttr(colBack=wx.Colour(200, 200, 200)), 1.156 + 0: wx.TextAttr() 1.157 + } 1.158 + 1.159 +if __name__ == "__main__": 1.160 + app = wx.App(False) 1.161 + settings = Settings() 1.162 + frame = MainFrame(None, 'Python Alignment Editor') 1.163 + frame.Show(True) 1.164 + app.MainLoop() 1.165 +