Back to index

python-biopython  1.60
xbb_search.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Created: Sun Dec  3 13:38:52 2000
00003 # Last changed: Time-stamp: <01/09/04 09:51:21 thomas>
00004 # thomas@cbs.dtu.dk, http://www.cbs.dtu.dk/thomas
00005 # File: xbb_search.py
00006 
00007 import re
00008 import os, sys, commands
00009 sys.path.insert(0, '.')
00010 from Tkinter import *
00011 from tkColorChooser import askcolor
00012 from Bio.Data.IUPACData import ambiguous_dna_values
00013 import re
00014 
00015 from Bio.Seq import reverse_complement
00016 
00017 class DNAsearch:
00018     def __init__(self):
00019         self.init_alphabet()
00020         self.sequence = ''
00021         
00022     def init_alphabet(self):
00023         self.alphabet = ambiguous_dna_values
00024         other = ''.join(self.alphabet.keys())
00025         self.alphabet['N'] = self.alphabet['N'] + other
00026         for key in self.alphabet.keys():
00027             if key == 'N': continue
00028             if key in self.alphabet[key]: continue
00029             self.alphabet[key] = self.alphabet[key] + key
00030 
00031     def SetSeq(self, seq): self.sequence = seq
00032     
00033     def SetPattern(self, pattern):
00034         self.pattern = pattern
00035         self.rx_pattern = self.IUPAC2regex(pattern)
00036         self.rx = re.compile(self.rx_pattern)
00037     
00038     def IUPAC2regex(self, s):
00039         rx = ''
00040         for i in s:
00041             r = self.alphabet.get(i,i)
00042             if len(r) > 1:
00043                 rx = '%s[%s]' % (rx, r)
00044             else:
00045                 rx += r
00046         return rx
00047     
00048     def _Search(self, start = 0):
00049         pos = self.rx.search(self.sequence, start)
00050         return pos
00051     
00052     def Search(self, start = 0):
00053         pos = self.rx.search(self.sequence, start)
00054         if pos:
00055             return pos.start()
00056         else:
00057             return -1
00058         
00059 
00060     def SearchAll(self):
00061         pos = -1
00062         positions = []
00063         while 1:
00064             m   = self._Search(pos+1)
00065             if not m: break
00066             pos = m.start()
00067             if pos == -1:
00068                 break
00069             
00070             positions.append(pos)
00071         return positions
00072     
00073         
00074 class XDNAsearch(Toplevel, DNAsearch):
00075     def __init__(self, seq= '', master= None, highlight = 0):
00076         DNAsearch.__init__(self)
00077         self.master = master
00078         self.highlight = highlight
00079         self.colors = []
00080         self.init_graphics()
00081         self.sequence = seq
00082         self.cur_pos = 0
00083         
00084     def init_graphics(self):
00085         Toplevel.__init__(self, self.master)
00086         self.frame = Frame(self)
00087         self.frame.pack(fill = BOTH, expand = 1)
00088 
00089         self.search_entry = Entry(self.frame)
00090         self.search_entry.pack(fill = BOTH, expand = 1)
00091 
00092         f2 = Frame(self.frame)
00093         f2.pack(side = TOP, fill = BOTH, expand = 1)
00094         
00095         f = f2
00096         self.forward = Button(f, text = 'Search +', command = self.do_search)
00097         self.forward.pack(side = LEFT)
00098         self.forward = Button(f, text = 'Search -',
00099                               command = lambda x=self.do_search: x(other_strand=1))
00100         self.forward.pack(side = LEFT)
00101         self.cancel = Button(f, text = 'Cancel', command = self.exit)
00102         self.cancel.pack(side = LEFT)
00103         self.current_color = 'cyan'
00104         self.colorb = Button(f, text = 'Color', command = self.change_color, foreground = self.current_color)
00105         self.colorb.pack(side = LEFT)
00106         self.config_color(self.current_color)
00107 
00108         
00109 
00110         
00111     def config_color(self, color = None):
00112         if not self.highlight: return
00113         if not color:
00114             try:
00115                 color = askcolor()[1]
00116             except:
00117                 color = 'cyan'
00118         self.current_color = color
00119         self.current_tag = 'searched_%s' % self.current_color
00120         self.master.tag_config(self.current_tag, background=self.current_color)
00121         self.master.tag_config(self.current_tag+'R', background=self.current_color, underline = 1)
00122         self.colors.append(color)
00123         
00124     def change_color(self):
00125         self.config_color()
00126         self.colorb.configure(foreground = self.current_color)
00127         self.colorb.update()
00128             
00129     def get_pattern(self):
00130         pattern = self.search_entry.get()
00131         return pattern
00132         
00133     def do_search(self, other_strand = 0):
00134         pattern = self.get_pattern()
00135         if other_strand: pattern = reverse_complement(pattern)
00136         self.SetPattern(pattern)
00137         pos = self.Search(self.cur_pos)
00138         self.cur_pos = pos +1
00139         w = self.master
00140         if pos != -1:
00141             if self.highlight:
00142                 start, stop = pos, pos + len(self.pattern)
00143                 if other_strand:
00144                     w.tag_add(self.current_tag+'R', '1.%d' % start, '1.%s' % stop)
00145                 else:
00146                     w.tag_add(self.current_tag, '1.%d' % start, '1.%s' % stop)
00147                 w.see('1.%d' % start)
00148 
00149 
00150     
00151     def exit(self):
00152         for c in self.colors:
00153             self.master.tag_remove('searched_%s' % c, 1.0, END)
00154             self.master.tag_remove('searched_%sR' % c, 1.0, END)
00155         self.destroy()
00156         del(self)
00157     
00158     def showcolor(self):
00159         pass
00160 
00161     
00162 
00163 if __name__ == '__main__':
00164     seq = 'ATGGTGTGTGTGTACGATCGCCCCCCCCAGTCGATCGATGCATCGTA'
00165     win = Tk()
00166     xtest = XDNAsearch(seq = seq, master = win)
00167 
00168     win.mainloop()
00169     
00170     
00171