/Scripts/xbbtools/xbb_search.py

https://github.com/GunioRobot/biopython · Python · 171 lines · 130 code · 36 blank · 5 comment · 21 complexity · f5b3651f880248af0ff3abcffadd0a21 MD5 · raw file

  1. #!/usr/bin/env python
  2. # Created: Sun Dec 3 13:38:52 2000
  3. # Last changed: Time-stamp: <01/09/04 09:51:21 thomas>
  4. # thomas@cbs.dtu.dk, http://www.cbs.dtu.dk/thomas
  5. # File: xbb_search.py
  6. import re
  7. import os, sys, commands
  8. sys.path.insert(0, '.')
  9. from Tkinter import *
  10. from tkColorChooser import askcolor
  11. from Bio.Data.IUPACData import ambiguous_dna_values
  12. import re
  13. from Bio.Seq import reverse_complement
  14. class DNAsearch:
  15. def __init__(self):
  16. self.init_alphabet()
  17. self.sequence = ''
  18. def init_alphabet(self):
  19. self.alphabet = ambiguous_dna_values
  20. other = ''.join(self.alphabet.keys())
  21. self.alphabet['N'] = self.alphabet['N'] + other
  22. for key in self.alphabet.keys():
  23. if key == 'N': continue
  24. if key in self.alphabet[key]: continue
  25. self.alphabet[key] = self.alphabet[key] + key
  26. def SetSeq(self, seq): self.sequence = seq
  27. def SetPattern(self, pattern):
  28. self.pattern = pattern
  29. self.rx_pattern = self.IUPAC2regex(pattern)
  30. self.rx = re.compile(self.rx_pattern)
  31. def IUPAC2regex(self, s):
  32. rx = ''
  33. for i in s:
  34. r = self.alphabet.get(i,i)
  35. if len(r) > 1:
  36. rx = '%s[%s]' % (rx, r)
  37. else:
  38. rx += r
  39. return rx
  40. def _Search(self, start = 0):
  41. pos = self.rx.search(self.sequence, start)
  42. return pos
  43. def Search(self, start = 0):
  44. pos = self.rx.search(self.sequence, start)
  45. if pos:
  46. return pos.start()
  47. else:
  48. return -1
  49. def SearchAll(self):
  50. pos = -1
  51. positions = []
  52. while 1:
  53. m = self._Search(pos+1)
  54. if not m: break
  55. pos = m.start()
  56. if pos == -1:
  57. break
  58. positions.append(pos)
  59. return positions
  60. class XDNAsearch(Toplevel, DNAsearch):
  61. def __init__(self, seq= '', master= None, highlight = 0):
  62. DNAsearch.__init__(self)
  63. self.master = master
  64. self.highlight = highlight
  65. self.colors = []
  66. self.init_graphics()
  67. self.sequence = seq
  68. self.cur_pos = 0
  69. def init_graphics(self):
  70. Toplevel.__init__(self, self.master)
  71. self.frame = Frame(self)
  72. self.frame.pack(fill = BOTH, expand = 1)
  73. self.search_entry = Entry(self.frame)
  74. self.search_entry.pack(fill = BOTH, expand = 1)
  75. f2 = Frame(self.frame)
  76. f2.pack(side = TOP, fill = BOTH, expand = 1)
  77. f = f2
  78. self.forward = Button(f, text = 'Search +', command = self.do_search)
  79. self.forward.pack(side = LEFT)
  80. self.forward = Button(f, text = 'Search -',
  81. command = lambda x=self.do_search: x(other_strand=1))
  82. self.forward.pack(side = LEFT)
  83. self.cancel = Button(f, text = 'Cancel', command = self.exit)
  84. self.cancel.pack(side = LEFT)
  85. self.current_color = 'cyan'
  86. self.colorb = Button(f, text = 'Color', command = self.change_color, foreground = self.current_color)
  87. self.colorb.pack(side = LEFT)
  88. self.config_color(self.current_color)
  89. def config_color(self, color = None):
  90. if not self.highlight: return
  91. if not color:
  92. try:
  93. color = askcolor()[1]
  94. except:
  95. color = 'cyan'
  96. self.current_color = color
  97. self.current_tag = 'searched_%s' % self.current_color
  98. self.master.tag_config(self.current_tag, background=self.current_color)
  99. self.master.tag_config(self.current_tag+'R', background=self.current_color, underline = 1)
  100. self.colors.append(color)
  101. def change_color(self):
  102. self.config_color()
  103. self.colorb.configure(foreground = self.current_color)
  104. self.colorb.update()
  105. def get_pattern(self):
  106. pattern = self.search_entry.get()
  107. return pattern
  108. def do_search(self, other_strand = 0):
  109. pattern = self.get_pattern()
  110. if other_strand: pattern = reverse_complement(pattern)
  111. self.SetPattern(pattern)
  112. pos = self.Search(self.cur_pos)
  113. self.cur_pos = pos +1
  114. w = self.master
  115. if pos != -1:
  116. if self.highlight:
  117. start, stop = pos, pos + len(self.pattern)
  118. if other_strand:
  119. w.tag_add(self.current_tag+'R', '1.%d' % start, '1.%s' % stop)
  120. else:
  121. w.tag_add(self.current_tag, '1.%d' % start, '1.%s' % stop)
  122. w.see('1.%d' % start)
  123. def exit(self):
  124. for c in self.colors:
  125. self.master.tag_remove('searched_%s' % c, 1.0, END)
  126. self.master.tag_remove('searched_%sR' % c, 1.0, END)
  127. self.destroy()
  128. del(self)
  129. def showcolor(self):
  130. pass
  131. if __name__ == '__main__':
  132. seq = 'ATGGTGTGTGTGTACGATCGCCCCCCCCAGTCGATCGATGCATCGTA'
  133. win = Tk()
  134. xtest = XDNAsearch(seq = seq, master = win)
  135. win.mainloop()