Example #1
0
def group_list(colmun, mode, name, config):

    import paplot.color as color

    option_input = ""

    if mode == "mutation":
        option_input = "result_format_mutation"
    elif mode == "ca":
        option_input = "result_format_ca"
    else:
        return []

    sept = tools.config_getstr(config, option_input, "sept_%s" % name)
    limited_list = text_to_list(
        tools.config_getstr(config, mode, "limited_%s" % name), ",")
    nouse_list = text_to_list(
        tools.config_getstr(config, mode, "nouse_%s" % name), ",")

    funcs = []
    for row in colmun:
        splt = []
        if sept == "": splt.append(row)
        else: splt = row.split(sept)

        for func in splt:
            func = func.strip()

            if func == "":
                continue

            if len(limited_list) > 0:
                #if (func in limited_list) == False:
                if fnmatch_list(func, limited_list) == False:
                    continue

            #if func in nouse_list:
            if fnmatch_list(func, nouse_list):
                continue
            funcs.append(func)

    # sort list
    funcs = list(set(funcs))
    funcs.sort()

    color_list = {}
    for f in tools.config_getstr(config, mode, "%s_color" % name).split(","):
        if len(f) == 0: continue
        cols = text_to_list(f, ":")
        if len(cols) >= 2:
            color_list[cols[0]] = color.name_to_value(cols[1])

    color_list = color.create_color_dict(funcs, color_list, color.metro_colors)

    # dict to value
    colors = []
    for key in funcs:
        colors.append(color_list[key])

    return [funcs, colors]
Example #2
0
def group_list(colmun, mode, name, config):

    import paplot.color as color
    
    option_input = ""

    if mode == "mut":
        option_input = "result_format_mutation"
    elif mode == "sv":
        option_input = "result_format_sv"
    else:
        return []
    
    sept = tools.config_getstr(config, option_input, "sept_%s" % name)
    limited_list = text_to_list(tools.config_getstr(config, mode, "limited_%ss" % name), ",")
    nouse_list = text_to_list(tools.config_getstr(config, mode, "nouse_%ss" % name), ",")
    
    funcs = []
    for row in colmun:
        splt = []
        if sept == "": splt.append(row)
        else: splt = row.split(sept)
        
        for func in splt:
            func = func.strip()
            
            if func == "": continue
            if len(limited_list) > 0:
                if (func in limited_list) == False: continue   
            if func in nouse_list: continue
            funcs.append(func)
            
    # sort list
    funcs = list(set(funcs))
    funcs.sort() 
    
    color_n_list = {};
    for f in tools.config_getstr(config, mode, "%s_colors" % name).split(","):
        if len(f) == 0: continue
        cols = text_to_list(f, ":")
        if len(cols) >= 2:
            color_n_list[cols[0]] = color.name_to_value(cols[1])
    
    color_n_list = color.create_color_dict(funcs, color_n_list, color.metro_colors) 
    
    # dict to value
    colors_n = []    
    for key in funcs:
        colors_n.append(color_n_list[key])
        
    return [funcs, colors_n]
Example #3
0
def load_subdata(ids, sec, config):
    import os
    import paplot.subcode.tools as tools
    import paplot.convert as convert
    import paplot.color as color

    input_file = tools.config_getpath(config, sec, "path", default = "")
    if os.path.exists(input_file) == False:
        print ("[ERROR] file is not exist. %s" % input_file)
        return None

    sept = tools.config_getstr(config, sec, "sept").replace("\\t", "\t").replace("\\n", "\n").replace("\\r", "\r")
    mode = tools.config_getstr(config, sec, "mode")
    comment = tools.config_getstr(config, sec, "comment")
    title = tools.config_getstr(config, sec, "title")
    
    label = []
    item = []
    colors_n_di = {}
    colors_h_di = {}
    for name_set in tools.config_getstr(config, sec, "name_set").split(","):
        name_set_split = convert.text_to_list(name_set, ":")
        for i in range(len(name_set_split)):
            text = name_set_split[i]
            if i == 0:
                item.append(text)
                if len(name_set_split) == 1:
                    label.append(text)
            elif i == 1:
                label.append(text)
            elif i == 2:
                colors_n_di[name_set_split[0]] = color.name_to_value(text)
            elif i == 3:
                colors_h_di[name_set_split[0]] = color.name_to_value(text)
    
    # fill in undefined items
    colors_n_di = color.create_color_dict(item, colors_n_di, color.osaka_subway_colors) 
    colors_h_di2 = {}
    for key in colors_n_di:
        if key in colors_h_di: continue
        colors_h_di2[key] = color.saturation_down(colors_n_di[key])
    
    # dict to value
    colors_n = []
    for key in item:
        colors_n.append(colors_n_di[key])
    
    if mode == "range":
        item.remove(item[0])
    
    header = []
    if tools.config_getboolean(config, sec, "header") == True:
        pos_value = -1
        pos_id = -1
    else:
        pos_value = tools.config_getint(config, sec, "col_value")-1
        pos_id = tools.config_getint(config, sec, "col_ID")-1
        header = ["",""]
    
    # copy id_list for find check
    unlookup = []
    for iid in ids:
        unlookup.append(iid)
        
    # read
    data_text = ""
    values = []
    for line in open(input_file):
        line = line.strip()
        if len(line.replace(sept, "")) == 0:
            continue
        
        if comment != "" and line.find(comment) == 0:
            continue
        
        if len(header) == 0:
            header = convert.text_to_list(line,sept)
            try:
                colname = tools.config_getstr(config, sec, "col_value")
                pos_value = header.index(colname)
                colname = tools.config_getstr(config, sec, "col_ID")
                pos_id = header.index(colname)
            except Exception as e:
                print(e.message)
                return None
                
            continue
        
        cols = convert.text_to_list(line,sept)
        if (cols[pos_id] in ids) == False: continue
        else: unlookup.remove(cols[pos_id])

        id_pos = ids.index(cols[pos_id])
        
        if mode == "fix":
            if cols[pos_value] in item:
                data_text += subdata_data_template.format(id = id_pos, item = item.index(cols[pos_value]))
            else:
                print("[" + sec + "] name_set: data is undefined." + cols[pos_value] + "\n")
                continue
        elif mode == "range" or mode == "gradient":
            try:
                values.append(float(cols[pos_value]))
            except Exception as e:
                print(colname + ": data type is invalid.\n" + e.message)
                continue
            
            data_text += subdata_data_template.format(id = id_pos, item = cols[pos_value])

    if len(unlookup) > 0:
        print("[WARNING] can't find IDs subplot data.")
        print(unlookup)
        
    if mode == "gradient" and len(values) > 0:
        item[0] = min(values)
        item[1] = max(values)
        
    return [data_text, item, colors_n, label, title] 
Example #4
0
def convert_tojs(input_file, output_file, positions, config):

    import os
    import paplot.subcode.data_frame as data_frame
    import paplot.subcode.merge as merge
    import paplot.subcode.tools as tools
    import paplot.convert as convert
    import paplot.color as color

    cols_di = merge.position_to_dict(positions)

    # data read
    try:
        df = data_frame.load_file(input_file, header = 1, \
            sept = tools.config_getstr(config, "result_format_qc", "sept"), \
            comment = tools.config_getstr(config, "result_format_qc", "comment") \
            )
    except Exception as e:
        print("failure open data %s, %s" % (input_file, e.message))
        return None

    if len(df.data) == 0:
        print("no data %s" % input_file)
        return None

    # chart list
    plots_text = ""
    plots_option = []

    config_sections = config.sections()
    config_sections.sort()
    if "qc_chart_brush" in config_sections:
        config_sections.remove("qc_chart_brush")
        config_sections.insert(0, "qc_chart_brush")

    for sec in config.sections():
        if not sec.startswith("qc_chart_"):
            continue

        chart_id = sec.replace("qc_chart_", "chart_")

        stack_id = []
        label = []
        colors_di = {}
        counter = 0
        for name_set in tools.config_getstr(config, sec,
                                            "name_set").split(","):
            name_set_split = convert.text_to_list(name_set, ":")
            if len(name_set_split) == 0:
                continue

            stack_id.append("stack" + str(counter))
            label.append(name_set_split[0])

            if len(name_set_split) > 1:
                colors_di[name_set_split[0]] = color.name_to_value(
                    name_set_split[1])
            counter += 1

        # fill in undefined items
        colors_di = color.create_color_dict(label, colors_di,
                                            color.metro_colors)

        # dict to value
        colors_li = []
        for key in label:
            colors_li.append(colors_di[key])

        plots_text += plot_template.format(
            chart_id = chart_id, \
            title = tools.config_getstr(config, sec, "title"), \
            title_y = tools.config_getstr(config, sec, "title_y"), \
            stack = convert.pyformat_to_jstooltip_text(cols_di, config, sec, "result_format_qc", "stack"), \
            stack_id = convert.list_to_text(stack_id), \
            label = convert.list_to_text(label), \
            color = convert.list_to_text(colors_li), \
            tooltip = convert.pyformat_to_jstooltip_text(cols_di, config, sec, "result_format_qc", "tooltip_format"), \
            )
        plots_option.append(chart_id)

    # ID list
    id_list = []
    for row in df.data:
        iid = row[df.name_to_index(cols_di["id"])]
        if iid != "": id_list.append(iid)
    id_list = list(set(id_list))
    id_list.sort()

    # header
    headers = tools.dict_keys(cols_di)

    f = open(output_file, "w")
    f.write(js_header)
    f.write(js_dataset.format(IDs = convert.list_to_text(id_list), \
                            header = convert.list_to_text(headers), \
                            plots = plots_text))
    f.write(js_data1)

    # values
    for row in df.data:
        iid = row[df.name_to_index(cols_di["id"])]
        if iid == "": continue

        values = ""
        for item in headers:
            if len(values) > 0:
                values += ","
            val = row[df.name_to_index(cols_di[item])]
            if type(val) == type(""):
                values += "'" + val + "'"
            elif type(val) == type(0.0):
                values += str('%.2f' % val)
            else:
                values += str(val)

        f.write("[" + values + "],")

    f.write(js_data2)

    f_template = open(
        os.path.dirname(os.path.abspath(__file__)) + "/templates/data_qc.js")
    js_function = f_template.read()
    f_template.close()
    f.write(js_function)
    f.write(js_footer)

    f.close()

    return {"plots": plots_option}
Example #5
0
def convert_tojs(input_file, output_file, positions, config):

    import paplot.subcode.data_frame as data_frame
    import paplot.subcode.merge as merge
    import paplot.subcode.tools as tools
    import paplot.convert as convert
    import paplot.color as color
    
    cols_di = merge.position_to_dict(positions)

    # data read
    try:
        df = data_frame.load_file(input_file, header = 1, \
            sept = tools.config_getstr(config, "merge_format_qc", "sept"), \
            comment = tools.config_getstr(config, "result_format_qc", "comment") \
            )
    except Exception as e:
        print ("failure open data %s, %s" % (input_file, e.message))
        return None

    if len(df.data) == 0:
        print ("no data %s" % input_file)
        return None

    # chart list
    plots_text = ""
    plots_option = []
    
    config_sections = config.sections()
    config_sections.sort()
    if "qc_chart_brush" in config_sections:
        config_sections.remove("qc_chart_brush")
        config_sections.insert(0, "qc_chart_brush")
        
    for sec in config.sections():
        if not sec.startswith("qc_chart_"):
            continue
        
        chart_id = sec.replace("qc_chart_", "chart_")
        
        stack_id = []
        label = []
        colors_di = {}
        counter = 0
        for name_set in tools.config_getstr(config, sec, "name_set").split(","):
            name_set_split = convert.text_to_list(name_set, ":")
            if len(name_set_split) == 0:
                continue
            
            stack_id.append("stack" + str(counter))
            label.append(name_set_split[0])
            
            if len(name_set_split) > 1:
                colors_di[name_set_split[0]] = color.name_to_value(name_set_split[1])
            counter += 1
            
        # fill in undefined items
        colors_di = color.create_color_dict(label, colors_di, color.metro_colors) 
        
        # dict to value
        colors_li = []
        for key in label:
            colors_li.append(colors_di[key])
        
        plots_text += plot_template.format(
            chart_id = chart_id, \
            title = tools.config_getstr(config, sec, "title"), \
            title_y = tools.config_getstr(config, sec, "title_y"), \
            stack = convert.pyformat_to_jstooltip_text(cols_di, config, sec, "result_format_qc", "stack"), \
            stack_id = convert.list_to_text(stack_id), \
            label = convert.list_to_text(label), \
            color = convert.list_to_text(colors_li), \
            tooltip = convert.pyformat_to_jstooltip_text(cols_di, config, sec, "result_format_qc", "tooltip_format"), \
            )
        plots_option.append(chart_id)
    
    # ID list
    Ids = []
    for row in df.data:
        iid = row[df.name_to_index(cols_di["id"])]
        if iid != "": Ids.append(iid)
    Ids = list(set(Ids))
    Ids.sort()
    
    # header 
    headers = cols_di.keys()
    headers.sort()
    
    f = open(output_file, "w")
    f.write(js_header)
    f.write(js_dataset.format(IDs = convert.list_to_text(Ids), \
                            header = convert.list_to_text(headers), \
                            plots = plots_text))    
    f.write(js_data1)
                        
    # values
    for row in df.data:
        iid = row[df.name_to_index(cols_di["id"])]
        if iid == "": continue
            
        values = ""
        for item in headers:
            if len(values) > 0:
                values += ","
            val = row[df.name_to_index(cols_di[item])]
            if type(val) == type(""):
                values += "'" + val + "'"
            elif type(val) == type(0.0):
                values += str('%.2f' % val)
            else:
                values += str(val)
        
        f.write("[" + values + "],")

    f.write(js_data2)
    f.write(js_function)
    f.close()
    
    return {"plots": plots_option}
Example #6
0
def load_subdata(ids, sec, config):
    import os
    import paplot.subcode.tools as tools
    import paplot.convert as convert
    import paplot.color as color

    input_file = tools.config_getpath(config, sec, "path", default = "../../example/sample_summary.csv")
    if os.path.exists(input_file) == False:
        print ("[ERROR] file is not exist. %s" % input_file)
        return None

    sept = tools.config_getstr(config, sec, "sept")
    mode = tools.config_getstr(config, sec, "mode")
    comment = tools.config_getstr(config, sec, "comment")
    title = tools.config_getstr(config, sec, "title")
    
    label = []
    item = []
    colors_n_di = {}
    colors_h_di = {}
    for name_set in tools.config_getstr(config, sec, "name_set").split(","):
        name_set_split = convert.text_to_list(name_set, ":")
        for i in range(len(name_set_split)):
            text = name_set_split[i]
            if i == 0:
                item.append(text)
                if len(name_set_split) == 1:
                    label.append(text)
            elif i == 1:
                label.append(text)
            elif i == 2:
                colors_n_di[name_set_split[0]] = color.name_to_value(text)
            elif i == 3:
                colors_h_di[name_set_split[0]] = color.name_to_value(text)
    
    # fill in undefined items
    colors_n_di = color.create_color_dict(item, colors_n_di, color.osaka_subway_colors) 
    colors_h_di2 = {}
    for key in colors_n_di:
        if colors_h_di.has_key(key): continue
        colors_h_di2[key] = color.Saturation_down(colors_n_di[key])
    
    # dict to value
    colors_n = []
    for key in item:
        colors_n.append(colors_n_di[key])
    
    if mode == "range":
        item.remove(item[0])
    
    header = []
    if tools.config_getboolean(config, sec, "header") == True:
        pos_value = -1
        pos_ID = -1
    else:
        pos_value = tools.config_getint(config, sec, "col_value")
        pos_ID = tools.config_getint(config, sec, "col_ID")
        header = ["",""]
    
    # copy Ids for find check
    unlookup = []
    for iid in ids:
        unlookup.append(iid)
        
    # read
    data_text = ""
    values = []
    for line in open(input_file):
        line = line.strip()
        if len(line.replace(sept, "")) == 0:
            continue
        
        if comment != "" and line.find(comment) == 0:
            continue
        
        if len(header) == 0:
            header = convert.text_to_list(line,sept)
            try:
                colname = tools.config_getstr(config, sec, "col_value")
                pos_value = header.index(colname)
                colname = tools.config_getstr(config, sec, "col_ID")
                pos_ID = header.index(colname)
            except Exception as e:
                print(e.message)
                return None
                
            continue
        
        cols = convert.text_to_list(line,sept)
        if (cols[pos_ID] in ids) == False: continue
        else: unlookup.remove(cols[pos_ID])

        id_pos = ids.index(cols[pos_ID])
        
        if mode == "fix":
            if cols[pos_value] in item:
                data_text += subdata_data_template.format(id = id_pos, item = item.index(cols[pos_value]))
            else:
                print("[" + sec + "] name_set: data is undefined." + cols[pos_value] + "\n")
                continue
        elif mode == "range":
            try:
                values.append(float(cols[pos_value]))
            except Exception as e:
                print(colname + ": data type is invalid.\n" + e.message)
                continue
            
            data_text += subdata_data_template.format(id = id_pos, item = cols[pos_value])
            
        elif mode == "gradient":
            try:
                values.append(float(cols[pos_value]))
            except Exception as e:
                print(colname + ": data type is invalid.\n" + e.message)
                continue
            
            data_text += subdata_data_template.format(id = id_pos, item = cols[pos_value])
    
    if len(unlookup) > 0:
        print("[WARNING] can't find IDs subplot data.")
        print(unlookup)
        
    if mode == "gradient" and len(values) > 0:
        item[0] = min(values)
        item[1] = max(values)
        
    return [data_text, item, colors_n, label, title] 
Example #7
0
def group_list(colmun, mode, name, config):
    """
    Create and return group names and their color values

    Parameters
    ----------
    colmun: list: colmun data for group title
    mode  : str : ca or mutation
    name  : str : "group"
    config: configparser.RawConfigParser

    Return
    ------
    A nested list with elements funcs and colors
    funcs : list: group names
    colors: list: color values
    """

    import paplot.color as color

    # option_input: section name
    option_input = ""
    if mode == "mutation":
        option_input = "result_format_mutation"
    elif mode == "ca":
        option_input = "result_format_ca"
    else:
        return []

    # Get values from a configuration file
    sept = tools.config_getstr(config, option_input,
                               "sept_%s" % name)  # key: sept_group
    limited_list = text_to_list(
        tools.config_getstr(config, mode, "limited_%s" % name),
        ",")  # key: limited_group
    nouse_list = text_to_list(
        tools.config_getstr(config, mode, "nouse_%s" % name),
        ",")  # key: nouse_group

    # Create funcs that is a list with group names as elements
    funcs = []
    for row in colmun:
        # Split row if necessary
        splt = []
        if sept == "":
            splt.append(row)
        else:
            splt = row.split(sept)
        # Limit the elements to be added to funcs
        for func in splt:
            func = func.strip()
            # Ignore empty string
            if func == "":
                continue
            # Ignore if func is not in non-empty limited_list
            if len(limited_list) > 0 and fnmatch_list(func,
                                                      limited_list) is False:
                continue
            # Ignore if func is in nouse_list
            if fnmatch_list(func, nouse_list):
                continue
            funcs.append(func)
    # Sort after eliminating duplicated elements of funcs
    funcs = list(set(funcs))
    funcs.sort()

    # Create color_di that is a dictionary with group names as keys and color values as values
    color_di = {}
    for f in tools.config_getstr(config, mode, "%s_color" %
                                 name).split(","):  # key: group_color
        # Ignore empty string
        if len(f) == 0:
            continue
        # f assumes something like "A:#66C2A5"
        cols = text_to_list(f, ":")
        if len(cols) >= 2:
            color_di[cols[0]] = color.name_to_value(cols[1])

    # Determine color values for groups
    color_di = color.create_color_dict(funcs, color_di, color.metro_colors)

    # Create color list for groups
    colors = []
    for key in funcs:
        colors.append(color_di[key])

    return [funcs, colors]