import os import random import time import pandas as pd from Bio import SeqIO from Bio.SeqUtils import GC from Simple_Drawing.settings import BASE_DIR tools_path = os.path.join(BASE_DIR, "tools_sofa") url_ = "http://127.0.0.1:8000" def random_color(): color_list = ["#009688", '#5FB878', '#393D49', '#1E9FFF', '#FFB800', '#FF5722', '#01AAED', '#2F4056'] return random.choice(color_list) # 保存单个文件 def keep_file(file_data, file_path, file_name=None, ui=0): if file_name is not None: file_path = os.path.join(file_path, file_name) if ui: with open(file_path, 'w') as f: f.write(file_data) else: with open(file_path, 'wb+') as f: for chunk1 in file_data.chunks(): f.write(chunk1) # 保存多个文件 def keep_files(na, c): cmd = ''' cd %s/file_keep/%s mkdir morefile ''' % (BASE_DIR, c) os.system(cmd) for file in na: with open('%s' % os.path.join(f'{BASE_DIR}/file_keep/{c}/morefile/', file.name), 'wb') as f: for i in file.chunks(): f.write(i) f.close() # 返回唯一文件编号 def generate_id(): a = time.asctime(time.localtime(time.time())) b = a[-15:] + str(random.randint(0, 1000)) c = b.replace(" ", "") c = c.replace(":", "0") return c # 给出文件路径 def path_out(sort, name=generate_id(), show=None, out=None): if show == 1: return os.path.join(BASE_DIR, "static", "img_eg", "show_chart", name) if out == 1: return os.path.join(BASE_DIR, 'file_keep', sort, "img", name) return os.path.join(BASE_DIR, 'file_keep', sort, "file", name), name # 选出需要的列 def pick_col(file_path, col, sep=","): midwest = pd.read_csv(file_path, sep=sep) col = set(midwest[col]) return col # 返回页数 def return_pages(su: int) -> int: if su % 12 != 0: return (su // 12) + 1 else: return su // 12 class ChartParameter(object): def __init__(self, data): if 'name' in data: self.name = data['name'] if 'html_name' in data: self.html_name = data['html_name'] if 'value' in data: self.value = data['value'] if 'astrict' in data: self.astrict = data['astrict'] if 'input_type' in data: self.input_type = data['input_type'] if 'Basics' in data: self.Basics = data['Basics'] if 'Senior' in data: self.Senior = data['Senior'] self.colors_dict = {} def html_out_Basics(self): code = "" for title in self.Basics: code += f"""
""" for d in self.Basics[title]: parameter = ChartParameter(d) if parameter.input_type == "text": parameter.input_type = f""" """ if parameter.input_type == "number": parameter.input_type = f""" """ if parameter.input_type == "date": parameter.input_type = f""" """ if parameter.input_type == "color_name": self.colors_dict[f"{parameter.name}"] = "color_name" parameter.input_type = f"""
""" if parameter.input_type == "rgb": self.colors_dict[f"{parameter.name}"] = "rgb" f"""
""" self.colors += 1 if parameter.input_type == "palettes": parameter.input_type = f""" """ if parameter.input_type == "add": parameter.input_type = f""" """ if parameter.input_type == "position": parameter.input_type = f""" """ code += f"""
{parameter.input_type}
""" return code def html_out_Senior(self): code = "" for title in self.Senior: code += f"""
""" for d in self.Senior[title]: parameter = ChartParameter(d) if parameter.input_type == "text": parameter.input_type = f""" """ if parameter.input_type == "number": parameter.input_type = f""" """ if parameter.input_type == "date": parameter.input_type = f""" """ if parameter.input_type == "color_name": self.colors_dict[f"{parameter.name}"] = "color_name" parameter.input_type = f"""
""" if parameter.input_type == "rgb": self.colors_dict[f"{parameter.name}"] = "rgb" f"""
""" if parameter.input_type == "palettes": parameter.input_type = f""" """ if parameter.input_type == "add": parameter.input_type = f""" """ if parameter.input_type == "position": parameter.input_type = f""" """ code += f"""
{parameter.input_type}
""" return code def js_out(self): code = "" for title in self.Basics: for d in self.Basics[title]: parameter = ChartParameter(d) if parameter.input_type in ["rgb", "hex", 'color_name']: code += f"""{parameter.name}:{parameter.name}, """ else: code += f"""{parameter.name}: (document.getElementById("{parameter.name}").value), """ print(parameter.input_type) for title in self.Senior: for d in self.Senior[title]: parameter = ChartParameter(d) print(parameter.input_type) if parameter.input_type in ["rgb", "hex", 'color_name']: code += f"""{parameter.name}:{parameter.name}, """ else: code += f"""{parameter.name}: (document.getElementById("{parameter.name}").value), """ return code def color_js(self): code = "" for i in self.colors_dict.keys(): if self.colors_dict[i] == "rgb": fl = "rgb" else: fl = "hex" code += """ layui.use('colorpicker', function() { var $ = layui.$ , colorpicker = layui.colorpicker; colorpicker.render({ elem: '#%s' , color: '%s' //设置默认色 , size: "xs" , format: '%s' , change: function (color) { %s = color } }); }) """ % (i, random_color(), fl, i) return code Treemap = { "Basics": { "title": [ {"name": "title", "value": "title", "astrict": "", "input_type": "text", "html_name": "Title"}, {"name": "title_font_size", "value": "12", "astrict": "", "input_type": "number", "html_name": "Title Size"} ], "data": [ {"name": "group_by_column", "value": "group_by_column", "astrict": "", "input_type": "text", "html_name": "Group By Column"}, {"name": "data_column", "value": "data_column", "astrict": "", "input_type": "text", "html_name": "Data Column"} ] }, "Senior": { } } class Basics(object): def __init__(self, file_path, args): self.args = args self.file_path = file_path self.out_file = [] self.f = SeqIO.parse(file_path, "fasta") # 读取多条序列 def Sequence_cleaning(self): f = self.f temp = [] for i in f: temp.append(f">{i.id}\n{str(i.seq)}") self.out_file.append(["Sequence_cleaning", temp]) def Sequence_length(self): f = self.f temp = [] for i in f: temp.append(f"{i.id}\t{len(i)}") self.out_file.append(["Sequence_length", temp]) def GC_content(self): f = self.f temp = [] for i in f: CONTENT = GC(str(i.seq)) temp.append(f"{i.id}\t{CONTENT}") self.out_file.append(["GC_content", temp]) def Sequence_flip(self): f = self.f temp = [] for i in f: temp.append(f">{i.id}\n{str(i.reverse_complement().seq)}") self.out_file.append(["Sequence_flip", temp]) def Reverse_complementarity(self): f = self.f temp = [] for i in f: temp.append(f">{i.id}\n{str(i.complement().seq)}") self.out_file.append(["Reverse_complementarity", temp]) # def Codon_preference(self): # f = self.f # temp = [] # for i in f: # temp.append(f">{i.id}\n{str(i.complement().seq)}") # self.out_file.append({"Reverse_complementarity": temp}) def run(self): for i in self.args.keys(): if self.args[i] == "YES": eval(f"self.{self.args[i]}")() data = "" for i in self.out_file: for j in i: data += f"\n{'-' * 15}\n{j[0]}\n" data += "\n".join(i[1]) with open(self.file_path, "w") as f: f.write(data) return data