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
|
from collections import OrderedDict from tkinter import * from tkinter import filedialog, messagebox from tkinter.scrolledtext import ScrolledText import time import re
file_name = '' codon_table = { 'Ala':['GCG','GCA','GCT','GCC'], 'Cys':['TGT','TGC'], 'Asp':['GAT','GAC'], 'Glu':['GAG' ,'GAA'], 'Phe':['TTT' ,'TTC'], 'Gly':['GGG','GGA','GGT','GGC'], 'His':['CAT','CAC'], 'Ile':['ATA','ATT','ATC'], 'Lys':['AAG','AAA'], 'Leu':['TTG','TTA','CTG','CTA','CTT','CTC'], 'Met':['ATG'], 'Asn':['AAT','AAC'], 'Pro':['CCG','CCA','CCT','CCC'], 'Gln':['CAG','CAA'], 'Arg':['AGG','AGA','CGG','CGA','CGA','CGT','CGC'], 'Ser':['AGT','AGC','TCG','TCA','TCT','TCC'], 'Thr':['ACG','ACA','ACT','ACC'], 'Val':['GTG','GTA','GTT','GTC'], 'Trp':['TGG'], 'Tyr':['TAT','TAC'], '*':['TGA','TAG','TAA'] }
def fasta_generator(file_path): with open(file_path, 'r') as file: first_line = file.readline().strip() if not first_line.startswith('>'): raise ValueError('Invalid file format') sequence_id = first_line[1:] sequence = '' for line in file: line = line.strip() if line.startswith('>'): if sequence_id: yield {'id': sequence_id, 'seq': sequence} sequence_id = line[1:] sequence = '' else: sequence += line if sequence_id: yield {'id': sequence_id, 'seq': sequence}
class MY_GUI():
def __init__(self,init_window_name): self.init_window_name = init_window_name self.image_file = None
def set_init_window(self): self.init_window_name.title("密码子统计工具_v1.0") self.init_window_name.geometry('1048x680+400+150') self.init_window_name.attributes("-alpha",1) self.init_window_name.iconbitmap('pic/bitbug_favicon.ico') self.init_window_name.resizable(0,0) canvas = Canvas(self.init_window_name, width=1024, height=680, bg=None) self.image_file = PhotoImage(file="pic/bg.gif") self.resized_image = self.image_file.zoom(2, 2) canvas.create_image(520, 35, anchor='n', image=self.resized_image) canvas.grid(row=0,rowspan = 20,column=0,columnspan=23) self.init_data_label = Label(self.init_window_name, text="导入序列") self.init_data_label.grid(row=0, column=0) self.result_data_label = Label(self.init_window_name, text="Number(Frequency)") self.result_data_label.grid(row=0, column=12) self.result_out_label = Label(self.init_window_name, text="Codon Usage results") self.result_out_label.grid(row=6,column=13) self.log_label = Label(self.init_window_name, text="运行日志") self.log_label.grid(row=11, column=0) self.init_data_Text = ScrolledText(self.init_window_name, width=60, height=35) self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10, padx=20,pady=5) self.init_data_Text.bind('<KeyPress>', lambda f: 'break') self.log_data_Text = ScrolledText(self.init_window_name, width=60, height=9,) self.log_data_Text.grid(row=12, column=0, rowspan=5, columnspan=10,padx=20,pady=5) self.log_data_Text.bind('<KeyPress>', lambda f: 'break') self.result_data_Text = ScrolledText(self.init_window_name, width=60, height=20, wrap='none') self.result_data_Text.grid(row=1, column=12, rowspan=5, columnspan=10, padx=10,pady=5) self.result_data_Text.bind('<KeyPress>', lambda f: 'break') self.result_fre_Text = ScrolledText(self.init_window_name, width=40, height=20) self.result_fre_Text.grid(row=7, column=12, rowspan=10, columnspan=10,padx=10,pady=5) self.result_fre_Text.bind('<KeyPress>', lambda f: 'break') self.load_file_button = Button(self.init_window_name, text = "导入fasta文件",borderwidth=2,relief=RAISED, command=self.select_file) self.load_file_button.grid(row=2, column=11) self.codon_count_button = Button(self.init_window_name, text="开始统计",borderwidth=2,relief=RAISED,command=self.codon_count) self.codon_count_button.grid(row=3, column=11) self.export_count_button = Button(self.init_window_name, text="导出表格",borderwidth=2,relief=RAISED,command=self.export_count) self.export_count_button.grid(row=4, column=11) self.export_fre_button = Button(self.init_window_name, text="导出结果", borderwidth=2,relief=RAISED,command=self.export_fre) self.export_fre_button.grid(row=5,column=11) self.clean_button = Button(self.init_window_name, text='清空窗口', borderwidth=2,relief=RAISED,command=self.window_clean) self.clean_button.grid(row=6,column=11) self.scrollbar_x = Scrollbar(self.init_window_name, orient=HORIZONTAL) self.result_data_Text.config(xscrollcommand=self.scrollbar_x.set) self.scrollbar_x.config(command=self.result_data_Text.xview) self.scrollbar_x.grid(row=5, column=12, rowspan=1,columnspan=10, sticky="ESW",padx=10)
def select_file(self): global file_name file_name = filedialog.askopenfilename(title="选择文件") if file_name != '': try: records = fasta_generator(file_name) number = 0 for i in records: name = i['id'] seq = i['seq'] if len(seq)>48: seq = seq[:45] + '...' + seq[-3:] self.init_data_Text.insert(END,f"Name:{name}\nSeq:'{seq}'\n") number += 1 self.write_log_to_Text(f"INFO:fasta文件导入成功!共加载{number}条序列。") except: messagebox.showerror("ERROR", "载入失败,请检查文件是否为fasta格式!") self.write_log_to_Text("ERROR:载入失败,请检查文件格式。")
def codon_count(self): if file_name != '': records = fasta_generator(file_name) CodonsDict = OrderedDict([('GCG', 0), ('GCA', 0), ('GCT', 0), ('GCC', 0), ('TGT', 0), ('TGC', 0), ('GAT', 0), ('GAC', 0), ('GAG', 0), ('GAA', 0), ('TTT', 0), ('TTC', 0), ('GGG', 0), ('GGA', 0), ('GGT', 0), ('GGC', 0), ('CAT', 0), ('CAC', 0), ('ATA', 0), ('ATT', 0), ('ATC', 0), ('AAG', 0), ('AAA', 0), ('TTG', 0), ('TTA', 0), ('CTG', 0), ('CTA', 0), ('CTT', 0), ('CTC', 0), ('ATG', 0), ('AAT', 0), ('AAC', 0), ('CCG', 0), ('CCA', 0), ('CCT', 0), ('CCC', 0), ('CAG', 0), ('CAA', 0), ('AGG', 0), ('AGA', 0), ('CGG', 0), ('CGA', 0), ('CGT', 0), ('CGC', 0), ('AGT', 0), ('AGC', 0), ('TCG', 0), ('TCA', 0), ('TCT', 0), ('TCC', 0), ('ACG', 0), ('ACA', 0), ('ACT', 0), ('ACC', 0), ('GTG', 0), ('GTA', 0), ('GTT', 0), ('GTC', 0), ('TGG', 0), ('TAT', 0), ('TAC', 0), ('TGA', 0), ('TAG', 0), ('TAA', 0)]) self.result_data_Text.insert(END, f"{'Name':<15}") for key in CodonsDict: if key != 'TAA': self.result_data_Text.insert(END, f'{key:<12}') else: self.result_data_Text.insert(END,f'{key:<12}\n') for i in records: if i['seq'].startswith('ATG') and 'N' not in i['seq'] and len(i['seq']) % 3 ==0: for j in range(0, len(str(i['seq'])), 3): codon = str(i['seq'])[j:j+3] if codon in CodonsDict.keys(): CodonsDict[codon] +=1 else: self.write_log_to_Text("WARNING:序列%s存在未识别的密码子,跳过。" % (i['id'])) break total = sum([CodonsDict[key] for key in CodonsDict.keys()]) name = i['id'] self.result_data_Text.insert(END, f'{name:<15}') self.result_fre_Text.insert(END,'Results for %d residue sequence "%s":\n\nAA\tCodon\tNumber\tFrequency\n\n' % (total, i['id'])) for key in CodonsDict.keys(): frequency = "%.2f" % (CodonsDict[key]*3000/total) content = str(CodonsDict[key]) + '(%s)' % (frequency) self.result_data_Text.insert(END,f'{content:<12}') if key in codon_table['Ala']: self.result_fre_Text.insert(END,'Ala\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Cys']: self.result_fre_Text.insert(END,'Cys\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Asp']: self.result_fre_Text.insert(END,'Asp\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Glu']: self.result_fre_Text.insert(END,'Glu\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Phe']: self.result_fre_Text.insert(END,'Phe\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Gly']: self.result_fre_Text.insert(END,'Gly\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['His']: self.result_fre_Text.insert(END,'His\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Ile']: self.result_fre_Text.insert(END,'Ile\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Lys']: self.result_fre_Text.insert(END,'Lys\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Leu']: self.result_fre_Text.insert(END,'Leu\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Met']: self.result_fre_Text.insert(END,'Met\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Asn']: self.result_fre_Text.insert(END,'Asn\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Pro']: self.result_fre_Text.insert(END,'Pro\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Gln']: self.result_fre_Text.insert(END,'Gln\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Arg']: self.result_fre_Text.insert(END,'Arg\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Ser']: self.result_fre_Text.insert(END,'Ser\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Thr']: self.result_fre_Text.insert(END,'Thr\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Val']: self.result_fre_Text.insert(END,'Val\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Trp']: self.result_fre_Text.insert(END,'Trp\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['Tyr']: self.result_fre_Text.insert(END,'Tyr\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) elif key in codon_table['*']: self.result_fre_Text.insert(END,'*\t%s\t%d\t%s\n' % (key, CodonsDict[key], frequency)) self.result_fre_Text.insert(END,'\n----------------------------------------\n' ) self.result_data_Text.insert(END,'\n') else: self.write_log_to_Text("WARNING:序列%s非ATG开头/存在屏蔽序列/非3的倍数,该序列将不会出现在统计结果中。" % (i['id'])) self.write_log_to_Text("INFO:统计结束!") else: self.write_log_to_Text("ERROR:请先载入fasta文件!")
def export_count(self): content = self.result_data_Text.get('1.0', END) if 'Name' not in content: self.write_log_to_Text("ERROR:请先点击“载入fasta文件”并点击“开始统计”按钮!") else: try: count_file = filedialog.asksaveasfilename() count_file = count_file + '.csv' with open(count_file, 'w') as count: content = re.sub(r"[^\S\r\n]+",',',content) count.write(content) self.write_log_to_Text("INFO:密码子频数频率统计表保存成功!文件路径:%s" % (count_file)) except: self.write_log_to_Text("ERROR:ERROR:导出失败,请检查是否有同名文件未关闭!")
def export_fre(self): content = self.result_fre_Text.get('1.0', END) if 'Results' not in content: self.write_log_to_Text("ERROR:请先点击“载入fasta文件”并点击“开始统计”按钮!") else: try: result_file = filedialog.asksaveasfilename() result_file = result_file + '.txt' with open(result_file, 'w') as out: out.write(content) self.write_log_to_Text("INFO:结果文件保存成功!保存路径:%s" % (result_file)) except: self.write_log_to_Text("ERROR:导出失败,请检查是否有同名文件未关闭!")
def window_clean(self): self.init_data_Text.delete(1.0, END) self.log_data_Text.delete(1.0, END) self.result_data_Text.delete(1.0, END) self.result_fre_Text.delete(1.0, END)
def write_log_to_Text(self,logmsg): current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) logmsg_in = str(current_time) +" " + str(logmsg) + "\n" self.log_data_Text.insert(END, logmsg_in)
def gui_start(): init_window = Tk() GUI = MY_GUI(init_window) GUI.set_init_window() init_window.mainloop()
gui_start()
|