def convert_tojs(params, config): import os import json import paplot.subcode.tools as tools import paplot.convert as convert import paplot.color as color # data read try: json_data = json.load(open(params["data"])) except Exception as e: print("failure open data %s, %s" % (params["data"], e.message)) return None key_id_list = tools.config_getstr(config, "result_format_pmsignature", "key_id") key_ref = tools.config_getstr(config, "result_format_pmsignature", "key_ref") key_alt = tools.config_getstr(config, "result_format_pmsignature", "key_alt") key_strand = tools.config_getstr(config, "result_format_pmsignature", "key_strand") key_mutations = tools.config_getstr(config, "result_format_pmsignature", "key_mutation") key_mutation_count = tools.config_getstr(config, "result_format_pmsignature", "key_mutation_count") sig_num = len(json_data[key_ref]) if sig_num == 0: print("no data %s" % params["data"]) return {} # signature names signature_list = [] for s in range(sig_num): signature_list.append("Signature %d" % (s + 1)) # each signature colors sig_color_list = color.create_color_array(sig_num, color.r_set2) # use background? if tools.config_getboolean(config, "result_format_pmsignature", "background"): signature_list.append("Background ") sig_color_list.append(color.r_set2_gray) # Id list id_txt = "" if key_id_list in json_data: id_txt = convert.list_to_text(json_data[key_id_list]) # mutations mutations_txt = "" if key_mutations in json_data: for m in json_data[key_mutations]: mutations_txt += "[%d,%d,%f]," % (m[0], m[1], m[2]) # signature dataset_ref = "" for sig in json_data[key_ref]: tmp = "" for sub in sig: tmp += "[" + ",".join(map(str, sub)) + "]," dataset_ref += ("[" + tmp + "],") dataset_alt = "" for sig in json_data[key_alt]: tmp = "" for sub in sig: tmp += "[" + ",".join(map(str, sub)) + "]," dataset_alt += ("[" + tmp + "],") dataset_strand = "" for sig in json_data[key_strand]: dataset_strand += "[" + ",".join(map(str, sig)) + "]," # tooltips # for ref keys_di = { "a": "", "c": "", "g": "", "t": "", "ca": "", "cg": "", "ct": "", "ta": "", "tc": "", "tg": "", "plus": "", "minus": "", "id": "", "sig": "" } tooltip_refs_txt = "" for r in range(len(json_data[key_ref][0])): tooltip_refs_txt += js_tooltip_ref_template.format( index=r, tooltip_format=convert.pyformat_to_jstooltip_text( keys_di, config, "pmsignature", "", "tooltip_format_ref")) mutation_count_txt = "" if (key_mutation_count != "") and (key_mutation_count in json_data.keys()): for v in json_data[key_mutation_count]: mutation_count_txt += "%d," % v # output sig_num_sift = 0 if tools.config_getboolean(config, "result_format_pmsignature", "background"): sig_num_sift = 1 ellipsis = "%s%d" % (params["ellipsis"], (sig_num + sig_num_sift)) js_file = "data_%s.js" % ellipsis html_file = "graph_%s.html" % ellipsis f = open(params["dir"] + "/" + js_file, "w") f.write(js_header + js_dataset.format( Ids=id_txt, color_A=tools.config_getstr(config, "pmsignature", "color_A", "#06B838"), color_C=tools.config_getstr(config, "pmsignature", "color_C", "#609CFF"), color_G=tools.config_getstr(config, "pmsignature", "color_G", "#B69D02"), color_T=tools.config_getstr(config, "pmsignature", "color_T", "#F6766D"), color_plus=tools.config_getstr(config, "pmsignature", "color_plus", "#00BEC3"), color_minus=tools.config_getstr(config, "pmsignature", "color_minus", "#F263E2"), signatures=convert.list_to_text(signature_list), colors=convert.list_to_text(sig_color_list), mutations=mutations_txt, dataset_ref=dataset_ref, dataset_alt=dataset_alt, dataset_strand=dataset_strand, tooltip_ref=tooltip_refs_txt, tooltip_alt=convert.pyformat_to_jstooltip_text( keys_di, config, "pmsignature", "", "tooltip_format_alt"), tooltip_strand=convert.pyformat_to_jstooltip_text( keys_di, config, "pmsignature", "", "tooltip_format_strand"), mutation_title=convert.pyformat_to_jstooltip_text( keys_di, config, "pmsignature", "", "tooltip_format_mutation_title"), mutation_partial=convert.pyformat_to_jstooltip_text( keys_di, config, "pmsignature", "", "tooltip_format_mutation_partial"), mutation_count=mutation_count_txt, )) f_template = open( os.path.dirname(os.path.abspath(__file__)) + "/templates/data_pmsignature.js") js_function = f_template.read() f_template.close() f.write(js_function) f.write(js_footer) f.close() integral = True if key_id_list == "" or key_mutations == "" or key_mutation_count == "": integral = False return { "sig_num": sig_num, "js": js_file, "html": html_file, "intergral": integral, }
def convert_tojs(params, config): import json import math import itertools import paplot.subcode.tools as tools import paplot.convert as convert import paplot.color as color # data read try: jsonData = json.load(open(params["data"])) except Exception as e: print ("failure open data %s, %s" % (params["data"], e.message)) return None key_Ids = tools.config_getstr(config, "result_format_signature", "key_id") key_signature = tools.config_getstr(config, "result_format_signature", "key_signature") key_mutations = tools.config_getstr(config, "result_format_signature", "key_mutation") key_mutation_count = tools.config_getstr(config, "result_format_signature", "key_mutation_count") sig_num = len(jsonData[key_signature]) if sig_num == 0: print ("no data %s" % params["data"]) return {} # signature names signature_list = [] for s in range(sig_num): signature_list.append("Signature %d" % (s+1)) # each signature colors sig_color_list = color.create_color_array(sig_num, color.r_set2) # use background? if tools.config_getboolean(config, "result_format_signature", "background"): signature_list.append("Background ") sig_color_list.append(color.r_set2_gray) # axis-y max sig_y_max = tools.config_getint(config, "signature", "signature_y_max") if (sig_y_max < 0): for sig in jsonData[key_signature]: for sub in sig: m = max(sub) if sig_y_max < m: sig_y_max = m # route list sub_num = len(jsonData[key_signature][0][0]) log = math.log(sub_num, 4) if log % 1 > 0: print ("substitution's list length is invalid (%d, not number 4^N)" % sub_num) return None route_id = [] route_list = [] for p in itertools.product(("A","C","G","T"), repeat = int(log)): route_id.append("".join(p)) route_list.append(p) # substruction sub_di = [ {"name":"C > A", "ref":"C", "color":tools.config_getstr(config, "signature", "alt_color_CtoA")}, {"name":"C > G", "ref":"C", "color":tools.config_getstr(config, "signature", "alt_color_CtoG")}, {"name":"C > T", "ref":"C", "color":tools.config_getstr(config, "signature", "alt_color_CtoT")}, {"name":"T > A", "ref":"T", "color":tools.config_getstr(config, "signature", "alt_color_TtoA")}, {"name":"T > C", "ref":"T", "color":tools.config_getstr(config, "signature", "alt_color_TtoC")}, {"name":"T > G", "ref":"T", "color":tools.config_getstr(config, "signature", "alt_color_TtoG")}, ] substruction = "" for sub in sub_di: route = [] for r in route_list: route.append("p".join(r[0:int(log/2)]) + "p" + sub["ref"] + "p" + "p".join(r[int(log/2):])) substruction += js_substruction_template.format(name = sub["name"], color = sub["color"], route = convert.list_to_text(route)) # Id list id_txt = "" if key_Ids in jsonData: id_txt = convert.list_to_text(jsonData[key_Ids]) # mutations mutations_txt = "" if key_mutations in jsonData: for m in jsonData[key_mutations]: mutations_txt += "[%d,%d,%f]," % (m[0],m[1],m[2]) # signature dataset_sig = "" for sig in jsonData[key_signature]: tmp = "" for sub in sig: tmp += "[" + ",".join(map(str, sub)) + "]," dataset_sig += ("[" + tmp + "],") mutation_count_txt = "" if (key_mutation_count != "") and (key_mutation_count in jsonData.keys()): for v in jsonData[key_mutation_count]: mutation_count_txt += "%d," % v # output sig_num_sift = 0 if tools.config_getboolean(config, "result_format_signature", "background"): sig_num_sift = 1 ellipsis = "%s%d" % (params["ellipsis"], (sig_num + sig_num_sift)) js_file = "data_%s.js" % ellipsis html_file = "graph_%s.html" % ellipsis keys_di = {"sig":"", "route":"", "id":""} f = open(params["dir"] + "/" + js_file, "w") f.write(js_header \ + js_dataset.format(Ids = id_txt, \ signatures = convert.list_to_text(signature_list), \ colors = convert.list_to_text(sig_color_list), \ dataset_sig_max = sig_y_max, \ mutations = mutations_txt, \ dataset_sig = dataset_sig, \ route_id = convert.list_to_text(route_id), \ substruction = substruction, \ signature_title = convert.pyformat_to_jstooltip_text(keys_di, config, "signature", "", "tooltip_format_signature_title"), \ signature_partial = convert.pyformat_to_jstooltip_text(keys_di, config, "signature", "", "tooltip_format_signature_partial"), \ mutation_title = convert.pyformat_to_jstooltip_text(keys_di, config, "signature", "", "tooltip_format_mutation_title"), \ mutation_partial = convert.pyformat_to_jstooltip_text(keys_di, config, "signature", "", "tooltip_format_mutation_partial"), \ mutation_count = mutation_count_txt, \ ) + js_function) f.close() integral = True if key_Ids == "" or key_mutations == "" or key_mutation_count == "": integral = False return {"sig_num": sig_num, "js": js_file, "html": html_file, "intergral": integral, }