Пример #1
0
def draw_scatterplot(props,xy_coords,x_len,y_len,size,background_color,
                        label_color,sample_location,alpha):
    """Create scatterplot figure"""
    
    fig = figure(figsize=(x_len,y_len))
    xPC=int(props['xlabel'][1:2])
    yPC=int(props['ylabel'][1:2])
    sorted_keys = xy_coords.keys()
    scatters = {} 
    size_ct =  shape_ct = 0 
    
    xPC=xPC-1
    yPC=yPC-1
    #Iterate through coords and add points to the scatterplot
    for s_label in sorted_keys:
        s_data = xy_coords[s_label]
        if s_data[0]==[]:
            pass
        else:
            c = s_data[3]
            m = s_data[4][0]
            
            ax = fig.add_subplot(111,axisbg=background_color)
            #set tick colors and width
            for line in ax.yaxis.get_ticklines():
                # line is a matplotlib.lines.Line2D instance
                line.set_color(label_color)
                line.set_markeredgewidth(1)

            for line in ax.xaxis.get_ticklines():
                # line is a matplotlib.lines.Line2D instance
                line.set_color(label_color)
                line.set_markeredgewidth(1)
            
            if isarray(s_data[5][0]) and isarray(s_data[6][0]) and isarray(s_data[7][0]):
                matrix_low=s_data[5][0]
                matrix_high=s_data[6][0]
                ellipse_ave=s_data[7][0]
                
                ellipse_x=[ellipse_ave[sample_location[s_label], xPC]]
                ellipse_y=[ellipse_ave[sample_location[s_label], yPC]]
                
                width=[fabs(matrix_high[sample_location[s_label], xPC] - \
                        matrix_low[sample_location[s_label], xPC])]
                height=[fabs(matrix_high[sample_location[s_label], yPC] - \
                        matrix_low[sample_location[s_label], yPC])]
                
                sc_plot = scatter_ellipse(ax,ellipse_x, \
                           ellipse_y, width, height, c=c, a=0.0, \
                            alpha=alpha)
                sc_plot.scatter(ellipse_x, ellipse_y, c=c, marker=m, \
                              alpha=1.0)
            else:
                sc_plot = ax.scatter(s_data[0], s_data[1], c=c, marker=m, \
                             alpha=1.0,s=size, linewidth=1,edgecolor=c)
            size_ct += 1 
            shape_ct += 1
            scatters[s_label] = sc_plot
        
    return sc_plot
Пример #2
0
    def test_isarray(self):
        "isarray: tests the isarray function"
        test1 = asarray('Test')
        test2 = 'Test'

        exp1 = True
        exp2 = False

        self.assertEqual(isarray(test1), exp1)
        self.assertEqual(isarray(test2), exp2)
Пример #3
0
 def test_isarray(self):
     "isarray: tests the isarray function"
     test1=asarray('Test')
     test2 = 'Test'
     
     exp1 = True
     exp2 = False
     
     self.assertEqual(isarray(test1),exp1)
     self.assertEqual(isarray(test2),exp2)
Пример #4
0
def draw_scatterplot(props, xy_coords, x_len, y_len, size, background_color, label_color, sample_location, alpha):
    """Create scatterplot figure"""

    fig = figure(figsize=(x_len, y_len))
    xPC = int(props["xlabel"][2:3])
    yPC = int(props["ylabel"][2:3])
    sorted_keys = xy_coords.keys()
    scatters = {}
    size_ct = shape_ct = 0

    xPC = xPC - 1
    yPC = yPC - 1
    # Iterate through coords and add points to the scatterplot
    for s_label in sorted_keys:
        s_data = xy_coords[s_label]
        if s_data[0] == []:
            pass
        else:
            c = s_data[3]
            m = s_data[4][0]

            ax = fig.add_subplot(111, axisbg=background_color)
            # set tick colors and width
            for line in ax.yaxis.get_ticklines():
                # line is a matplotlib.lines.Line2D instance
                line.set_color(label_color)
                line.set_markeredgewidth(1)

            for line in ax.xaxis.get_ticklines():
                # line is a matplotlib.lines.Line2D instance
                line.set_color(label_color)
                line.set_markeredgewidth(1)

            if isarray(s_data[5][0]) and isarray(s_data[6][0]) and isarray(s_data[7][0]):
                matrix_low = s_data[5][0]
                matrix_high = s_data[6][0]
                ellipse_ave = s_data[7][0]

                ellipse_x = [ellipse_ave[sample_location[s_label], xPC]]
                ellipse_y = [ellipse_ave[sample_location[s_label], yPC]]

                width = [fabs(matrix_high[sample_location[s_label], xPC] - matrix_low[sample_location[s_label], xPC])]
                height = [fabs(matrix_high[sample_location[s_label], yPC] - matrix_low[sample_location[s_label], yPC])]

                sc_plot = scatter_ellipse(ax, ellipse_x, ellipse_y, width, height, c=c, a=0.0, alpha=alpha)
                sc_plot.scatter(ellipse_x, ellipse_y, c=c, marker=m, alpha=1.0)
            else:
                sc_plot = ax.scatter(s_data[0], s_data[1], c=c, marker=m, alpha=1.0, s=size, linewidth=1, edgecolor=c)
            size_ct += 1
            shape_ct += 1
            scatters[s_label] = sc_plot

    return sc_plot
Пример #5
0
def extract_and_color_xy_coords(p1d,p2d,p1dr,p2dr,mat_ave,colors, data_colors, \
                                groups, coords, tooltipDict):
    """Extract coords from appropriate columns and attach their \
       corresponding colors based on the group"""
    
    xy_coords = {}
    shape_ct = 0 
    for group_name, ids in (groups.items()):
        x=0
        if isinstance(colors[group_name], list):
            color = colors[group_name]#[c if c.startswith("#") else data_colors[c].toHex() for c in colors[group_name]]
        else:
            color = colors[group_name] if colors[group_name].startswith("#") else data_colors[colors[group_name]].toHex() ## so I can use my own hex colors, defined in prefs.txt
        #color = data_colors[colors[group_name]].toHex()
        m =shape[shape_ct % len(shape)]
        shape_ct += 1
        for id_ in (ids):
            cur_labs = []
            cur_x = []
            cur_y = []
            cur_color = []
            cur_shape=[]
            cur_1r=[]
            cur_2r=[]
            new_mat_ave=[]
            if id_ in coords['pc vector number']:
                cur_labs.append(tooltipDict[id_])   #id_+': '+group_name)
                cur_x.append(p2d[id_])
                cur_y.append(p1d[id_])
                #pdb.set_trace()
                cur_color.append(color)
                cur_shape.append(m)

                if isarray(p2dr) and isarray(p1dr) and isarray(mat_ave):
                    cur_1r.append(p1dr)
                    cur_2r.append(p2dr)
                    new_mat_ave.append(mat_ave)
                else:
                    cur_1r=[None]
                    cur_2r=[None]
                    new_mat_ave=[None]
                    
            xy_coords["%s" % id_] = (cur_x, cur_y, cur_labs, cur_color, \
                                     cur_shape, cur_1r, cur_2r,new_mat_ave)
        
    return xy_coords
Пример #6
0
def extract_and_color_xy_coords(p1d,p2d,p1dr,p2dr,mat_ave,colors, data_colors, \
                                groups, coords, tooltipDict):
    """Extract coords from appropriate columns and attach their \
       corresponding colors based on the group"""
    
    xy_coords = {}
    shape_ct = 0 
    for group_name, ids in (groups.items()):
        x=0
        if isinstance(colors[group_name], list):
            color = colors[group_name]#[c if c.startswith("#") else data_colors[c].toHex() for c in colors[group_name]]
        else:
            color = colors[group_name] if colors[group_name].startswith("#") else data_colors[colors[group_name]].toHex() ## so I can use my own hex colors, defined in prefs.txt
        #color = data_colors[colors[group_name]].toHex()
        m =shape[shape_ct % len(shape)]
        shape_ct += 1
        for id_ in (ids):
            cur_labs = []
            cur_x = []
            cur_y = []
            cur_color = []
            cur_shape=[]
            cur_1r=[]
            cur_2r=[]
            new_mat_ave=[]
            if id_ in coords['pc vector number']:
                cur_labs.append(tooltipDict[id_])   #id_+': '+group_name)
                cur_x.append(p2d[id_])
                cur_y.append(p1d[id_])
                #pdb.set_trace()
                cur_color.append(color)
                cur_shape.append(m)

                if isarray(p2dr) and isarray(p1dr) and isarray(mat_ave):
                    cur_1r.append(p1dr)
                    cur_2r.append(p2dr)
                    new_mat_ave.append(mat_ave)
                else:
                    cur_1r=[None]
                    cur_2r=[None]
                    new_mat_ave=[None]
                    
            xy_coords["%s" % id_] = (cur_x, cur_y, cur_labs, cur_color, \
                                     cur_shape, cur_1r, cur_2r,new_mat_ave)
        
    return xy_coords
Пример #7
0
def extract_and_color_xy_coords(
        p1d, p2d, p1dr, p2dr, mat_ave, colors, data_colors,
        groups, coords):
    """Extract coords from appropriate columns and attach their \
       corresponding colors based on the group"""

    xy_coords = {}
    shape_ct = 0
    for group_name, ids in (groups.items()):
        x = 0
        color = data_colors[colors[group_name]].toHex()
        m = shape[shape_ct % len(shape)]
        shape_ct += 1
        for id_ in (ids):
            cur_labs = []
            cur_x = []
            cur_y = []
            cur_color = []
            cur_shape = []
            cur_1r = []
            cur_2r = []
            new_mat_ave = []
            if id_ in coords['pc vector number']:
                cur_labs.append(id_ + ': ' + group_name)
                cur_x.append(p2d[id_])
                cur_y.append(p1d[id_])
                cur_color.append(color)
                cur_shape.append(m)

                if isarray(p2dr) and isarray(p1dr) and isarray(mat_ave):
                    cur_1r.append(p1dr)
                    cur_2r.append(p2dr)
                    new_mat_ave.append(mat_ave)
                else:
                    cur_1r = [None]
                    cur_2r = [None]
                    new_mat_ave = [None]

            xy_coords["%s" % id_] = (cur_x, cur_y, cur_labs, cur_color,
                                     cur_shape, cur_1r, cur_2r, new_mat_ave)

    return xy_coords
Пример #8
0
def extract_and_color_xy_coords(
        p1d, p2d, p1dr, p2dr, mat_ave, colors, data_colors,
        groups, coords):
    """Extract coords from appropriate columns and attach their \
       corresponding colors based on the group"""

    xy_coords = {}
    shape_ct = 0
    for group_name, ids in (groups.items()):
        x = 0
        color = data_colors[colors[group_name]].toHex()
        m = shape[shape_ct % len(shape)]
        shape_ct += 1
        for id_ in (ids):
            cur_labs = []
            cur_x = []
            cur_y = []
            cur_color = []
            cur_shape = []
            cur_1r = []
            cur_2r = []
            new_mat_ave = []
            if id_ in coords['pc vector number']:
                cur_labs.append(id_ + ': ' + group_name)
                cur_x.append(p2d[id_])
                cur_y.append(p1d[id_])
                cur_color.append(color)
                cur_shape.append(m)

                if isarray(p2dr) and isarray(p1dr) and isarray(mat_ave):
                    cur_1r.append(p1dr)
                    cur_2r.append(p2dr)
                    new_mat_ave.append(mat_ave)
                else:
                    cur_1r = [None]
                    cur_2r = [None]
                    new_mat_ave = [None]

            xy_coords["%s" % id_] = (cur_x, cur_y, cur_labs, cur_color,
                                     cur_shape, cur_1r, cur_2r, new_mat_ave)

    return xy_coords
Пример #9
0
def draw_pcoa_graph(plot_label, dir_path, data_file_link, coord_1, coord_2, \
                    coord_1r,coord_2r, mat_ave, sample_location, \
                    data, prefs,groups, colors, background_color, label_color,\
                    data_colors,data_color_order,\
                    generate_eps=True):
                    
    """Draw PCoA graphs"""

    coords,pct_var=convert_coord_data_to_dict(data)
    mapping = data['map']
    tooltipDict = dict([(sample[0], "%s/%s/%s<BR>%s<BR>%s<BR>%s"%(sample[0],sample[1],sample[3], sample[2], sample[4], sample[5][:120])) for sample in mapping])

    if coord_1 not in coords:
        raise ValueError, "Principal coordinate: %s not available." % coord_1

    if coord_2 not in coords:
        raise ValueError, "Principal coordinate: %s not available." % coord_2

    #Handle matplotlib scale bug when all coords are 0.0
    if not len([x for x in map(float, coords[coord_2]) if x != 0.0]):
        for ix in range(len(coords[coord_2])):
            coords[coord_2][ix] = '1e-255'
    if not len([x for x in map(float, coords[coord_1]) if x != 0.0]):
         for ix in range(len(coords[coord_1])):
            coords[coord_1][ix] = '1e-255'

    #Write figure labels
    props = {}
    props["title"] = "PCoA - PC%s vs PC%s" % (coord_1, coord_2)
    props["imgID"] = "PC%s%s" % (coord_1, coord_2)
    props["ylabel"] = "PC%s - Percent variation explained %.2f%%" \
                        % (coord_2, float(pct_var[coord_2]))
    props["xlabel"] = "PC%s - Percent variation explained %.2f%%" \
                        % (coord_1, float(pct_var[coord_1])) 

    labels = coords['pc vector number']
    p1 = map(float, coords[coord_2])
    p2 = map(float, coords[coord_1])
    if isarray(coord_1r) and isarray(coord_2r) and isarray(mat_ave):
        p1r = coord_2r
        p2r = coord_1r
    else:
        p1r = None
        p2r = None
        mat_ave=None
        
    if len(p1) != len(p2):
        raise ValueError, "Principal coordinate vectors unequal length."
    p1d = dict(zip(labels, p1))
    p2d = dict(zip(labels, p2))

    alpha=data['alpha']

    xy_coords = extract_and_color_xy_coords(p1d, p2d, p1r, p2r,mat_ave,colors, \
                                            data_colors, groups, coords, tooltipDict)
 
    img_src, img_map, eps_link =  make_interactive_scatter(plot_label,dir_path,
                                    data_file_link,background_color,label_color,
                                    sample_location,alpha,
                                    xy_coords=xy_coords,props=props,x_len=12, 
                                    y_len=12,size=20,draw_axes=True,
                                    generate_eps=generate_eps)

    return img_src + img_map, eps_link
Пример #10
0
def draw_scatterplot(props,xy_coords,x_len,y_len,size,background_color,
                        label_color,sample_location,alpha):
    """Create scatterplot figure"""
    print "Figsize", x_len, y_len
    fig = figure(figsize=(x_len,y_len))
    xPC=int(props['xlabel'][2:3])
    yPC=int(props['ylabel'][2:3])
    sorted_keys = xy_coords.keys()
    scatters = {} 
    size_ct =  shape_ct = 0 
    
    xPC=xPC-1
    yPC=yPC-1
    #Iterate through coords and add points to the scatterplot
    for s_label in sorted_keys:
        s_data = xy_coords[s_label]
        if s_data[0]==[]:
            pass
        else:
            c = s_data[3]
            m = s_data[4][0]
            
            ax = fig.add_subplot(111,axisbg=background_color)
            #set tick colors and width
            for line in ax.yaxis.get_ticklines():
                # line is a matplotlib.lines.Line2D instance
                line.set_color(label_color)
                line.set_markeredgewidth(1)

            for line in ax.xaxis.get_ticklines():
                # line is a matplotlib.lines.Line2D instance
                line.set_color(label_color)
                line.set_markeredgewidth(1)
            
            if isarray(s_data[5][0]) and isarray(s_data[6][0]) and \
                                                    isarray(s_data[7][0]):
                matrix_low=s_data[5][0]
                matrix_high=s_data[6][0]
                ellipse_ave=s_data[7][0]
                
                ellipse_x=[ellipse_ave[sample_location[s_label], xPC]]
                ellipse_y=[ellipse_ave[sample_location[s_label], yPC]]
                
                width=[fabs(matrix_high[sample_location[s_label], xPC] - \
                        matrix_low[sample_location[s_label], xPC])]
                height=[fabs(matrix_high[sample_location[s_label], yPC] - \
                        matrix_low[sample_location[s_label], yPC])]
                
                sc_plot = scatter_ellipse(ax,ellipse_x, \
                           ellipse_y, width, height, c=c, a=0.0, \
                            alpha=alpha)
                sc_plot.scatter(ellipse_x, ellipse_y, c=c, marker=m, \
                              alpha=1.0)
            else:
                #pdb.set_trace()
                if s_data[2][0].split("/")[0] == "Sabkha":
                    ax.scatter(s_data[0], s_data[1], c='r', marker='o', 
                               alpha=0.5,s=18, linewidth=2,edgecolor='k')
                colorlist = flatten(c)[:3] if c and isinstance(c[0], list) else c
                for idx, col in enumerate(colorlist):                    
                    dotsize = 15-6*idx
                    try:
                        sc_plot = ax.scatter(s_data[0], s_data[1], c=col, marker='o', 
                                             alpha=1,s=dotsize, linewidth=0) #,edgecolor=c)
                    except:
                        pdb.set_trace()
            size_ct += 1 
            shape_ct += 1
            scatters[s_label] = sc_plot
        
    return sc_plot
Пример #11
0
def generate_2d_plots(prefs, data, html_dir_path, data_dir_path, filename,
                      background_color, label_color, generate_scree):
    """Generate interactive 2D scatterplots"""
    coord_tups = [("1", "2"), ("3", "2"), ("1", "3")]
    mapping = data['map']
    out_table = ''
    #Iterate through prefs and generate html files for each colorby option
    #Sort by the column name first
    sample_location = {}

    groups_and_colors = iter_color_groups(mapping, prefs)
    groups_and_colors = list(groups_and_colors)

    for i in range(len(groups_and_colors)):
        labelname = groups_and_colors[i][0]
        groups = groups_and_colors[i][1]
        colors = groups_and_colors[i][2]
        data_colors = groups_and_colors[i][3]
        data_color_order = groups_and_colors[i][4]

        data_file_dir_path = get_random_directory_name(
            output_dir=data_dir_path)

        new_link = os.path.split(data_file_dir_path)
        data_file_link=os.path.join('.', os.path.split(new_link[-2])[-1], \
                                    new_link[-1])

        new_col_name = labelname
        img_data = {}
        plot_label = labelname

        if data.has_key('support_pcoas'):
            matrix_average, matrix_low, matrix_high, eigval_average, m_names = \
                summarize_pcoas(data['coord'], data['support_pcoas'],
                method=data['ellipsoid_method'])
            data['coord'] = \
                (m_names,matrix_average,data['coord'][2],data['coord'][3])
            for i in range(len(m_names)):
                sample_location[m_names[i]] = i
        else:
            matrix_average = None
            matrix_low = None
            matrix_high = None
            eigval_average = None
            m_names = None
        iterator = 0

        for coord_tup in coord_tups:
            if isarray(matrix_low) and isarray(matrix_high) and \
                                                isarray(matrix_average):
                coord_1r = asarray(matrix_low)
                coord_2r = asarray(matrix_high)
                mat_ave = asarray(matrix_average)
            else:
                coord_1r = None
                coord_2r = None
                mat_ave = None
                sample_location = None

            coord_1, coord_2 = coord_tup
            img_data[coord_tup] = draw_pcoa_graph(plot_label,data_file_dir_path,
                                                 data_file_link,coord_1,coord_2,
                                                 coord_1r, coord_2r, mat_ave,\
                                                 sample_location,
                                                 data,prefs,groups,colors,
                                                 background_color,label_color,
                                                 data_colors,data_color_order,
                                                 generate_eps=True)

        out_table += TABLE_HTML % (labelname, "<br>".join(
            img_data[("1", "2")]), "<br>".join(
                img_data[("3", "2")]), "<br>".join(img_data[("1", "3")]))

    if generate_scree:
        data_file_dir_path = get_random_directory_name(
            output_dir=data_dir_path)
        new_link = os.path.split(data_file_dir_path)
        data_file_link = os.path.join('.',
                                      os.path.split(new_link[-2])[-1],
                                      new_link[-1])

        img_src, download_link = draw_scree_graph(data_file_dir_path,
                                                  data_file_link,
                                                  background_color,
                                                  label_color,
                                                  generate_eps=True,
                                                  data=data)

        out_table += SCREE_TABLE_HTML % ("<br>".join((img_src, download_link)))

    outfile = create_html_filename(filename, '.html')
    outfile = os.path.join(html_dir_path, outfile)

    write_html_file(out_table, outfile)
Пример #12
0
def draw_pcoa_graph(plot_label, dir_path, data_file_link, coord_1, coord_2, \
                    coord_1r,coord_2r, mat_ave, sample_location, \
                    data, prefs,groups, colors, background_color, label_color,\
                    data_colors,data_color_order,\
                    generate_eps=True):
    """Draw PCoA graphs"""

    coords, pct_var = convert_coord_data_to_dict(data)
    mapping = data['map']

    if coord_1 not in coords:
        raise ValueError, "Principal coordinate: %s not available." % coord_1

    if coord_2 not in coords:
        raise ValueError, "Principal coordinate: %s not available." % coord_2

    #Handle matplotlib scale bug when all coords are 0.0
    if not len([x for x in map(float, coords[coord_2]) if x != 0.0]):
        for ix in range(len(coords[coord_2])):
            coords[coord_2][ix] = '1e-255'
    if not len([x for x in map(float, coords[coord_1]) if x != 0.0]):
        for ix in range(len(coords[coord_1])):
            coords[coord_1][ix] = '1e-255'

    #Write figure labels
    props = {}
    props["title"] = "PCoA - PC%s vs PC%s" % (coord_1, coord_2)
    props["ylabel"] = "PC%s - Percent variation explained %.2f%%" \
                        % (coord_2, float(pct_var[coord_2]))
    props["xlabel"] = "PC%s - Percent variation explained %.2f%%" \
                        % (coord_1, float(pct_var[coord_1]))

    labels = coords['pc vector number']
    p1 = map(float, coords[coord_2])
    p2 = map(float, coords[coord_1])
    if isarray(coord_1r) and isarray(coord_2r) and isarray(mat_ave):
        p1r = coord_2r
        p2r = coord_1r
    else:
        p1r = None
        p2r = None
        mat_ave = None

    if len(p1) != len(p2):
        raise ValueError, "Principal coordinate vectors unequal length."
    p1d = dict(zip(labels, p1))
    p2d = dict(zip(labels, p2))

    alpha = data['alpha']

    xy_coords = extract_and_color_xy_coords(p1d, p2d, p1r, p2r,mat_ave,colors, \
                                            data_colors, groups, coords)

    img_src, img_map, eps_link = make_interactive_scatter(
        plot_label,
        dir_path,
        data_file_link,
        background_color,
        label_color,
        sample_location,
        alpha,
        xy_coords=xy_coords,
        props=props,
        x_len=4.5,
        y_len=4.5,
        size=20,
        draw_axes=True,
        generate_eps=generate_eps)

    return img_src + img_map, eps_link
Пример #13
0
def draw_scatterplot(props,xy_coords,x_len,y_len,size,background_color,
                        label_color,sample_location,alpha):
    """Create scatterplot figure"""
    print "Figsize", x_len, y_len
    fig = figure(figsize=(x_len,y_len))
    xPC=int(props['xlabel'][2:3])
    yPC=int(props['ylabel'][2:3])
    sorted_keys = xy_coords.keys()
    scatters = {} 
    size_ct =  shape_ct = 0 
    
    xPC=xPC-1
    yPC=yPC-1
    #Iterate through coords and add points to the scatterplot
    for s_label in sorted_keys:
        s_data = xy_coords[s_label]
        if s_data[0]==[]:
            pass
        else:
            c = s_data[3]
            m = s_data[4][0]
            
            ax = fig.add_subplot(111,axisbg=background_color)
            #set tick colors and width
            for line in ax.yaxis.get_ticklines():
                # line is a matplotlib.lines.Line2D instance
                line.set_color(label_color)
                line.set_markeredgewidth(1)

            for line in ax.xaxis.get_ticklines():
                # line is a matplotlib.lines.Line2D instance
                line.set_color(label_color)
                line.set_markeredgewidth(1)
            
            if isarray(s_data[5][0]) and isarray(s_data[6][0]) and \
                                                    isarray(s_data[7][0]):
                matrix_low=s_data[5][0]
                matrix_high=s_data[6][0]
                ellipse_ave=s_data[7][0]
                
                ellipse_x=[ellipse_ave[sample_location[s_label], xPC]]
                ellipse_y=[ellipse_ave[sample_location[s_label], yPC]]
                
                width=[fabs(matrix_high[sample_location[s_label], xPC] - \
                        matrix_low[sample_location[s_label], xPC])]
                height=[fabs(matrix_high[sample_location[s_label], yPC] - \
                        matrix_low[sample_location[s_label], yPC])]
                
                sc_plot = scatter_ellipse(ax,ellipse_x, \
                           ellipse_y, width, height, c=c, a=0.0, \
                            alpha=alpha)
                sc_plot.scatter(ellipse_x, ellipse_y, c=c, marker=m, \
                              alpha=1.0)
            else:
                #pdb.set_trace()
                if s_data[2][0].split("/")[0] == "Sabkha":
                    ax.scatter(s_data[0], s_data[1], c='r', marker='o', 
                               alpha=0.5,s=18, linewidth=2,edgecolor='k')
                colorlist = flatten(c)[:3] if c and isinstance(c[0], list) else c
                for idx, col in enumerate(colorlist):                    
                    dotsize = 15-6*idx
                    try:
                        sc_plot = ax.scatter(s_data[0], s_data[1], c=col, marker='o', 
                                             alpha=1,s=dotsize, linewidth=0) #,edgecolor=c)
                    except:
                        pdb.set_trace()
            size_ct += 1 
            shape_ct += 1
            scatters[s_label] = sc_plot
        
    return sc_plot
Пример #14
0
def draw_pcoa_graph(plot_label, dir_path, data_file_link, coord_1, coord_2,
                    coord_1r, coord_2r, mat_ave, sample_location,
                    data, prefs, groups, colors, background_color, label_color,
                    data_colors, data_color_order,
                    generate_eps=True,
                    pct_variation_below_one=False):
    """Draw PCoA graphs"""

    coords, pct_var = convert_coord_data_to_dict(data)

    if coord_1 not in coords:
        raise ValueError("Principal coordinate: %s not available." % coord_1)

    if coord_2 not in coords:
        raise ValueError("Principal coordinate: %s not available." % coord_2)

    # Handle matplotlib scale bug when all coords are 0.0
    if not len([x for x in map(float, coords[coord_2]) if x != 0.0]):
        for ix in range(len(coords[coord_2])):
            coords[coord_2][ix] = '1e-255'
    if not len([x for x in map(float, coords[coord_1]) if x != 0.0]):
        for ix in range(len(coords[coord_1])):
            coords[coord_1][ix] = '1e-255'

    # Write figure labels
    pct_exp1 = float(pct_var[coord_1])
    pct_exp2 = float(pct_var[coord_2])
    if float(pct_var['1']) < 1 and not pct_variation_below_one:
        pct_exp1 *= 100
        pct_exp2 *= 100
    props = {}
    props["title"] = "PCoA - PC%s vs PC%s" % (coord_1, coord_2)
    props["ylabel"] = "PC%s - Percent variation explained %.2f%%" \
        % (coord_2, pct_exp2)
    props["xlabel"] = "PC%s - Percent variation explained %.2f%%" \
        % (coord_1, pct_exp1)

    labels = coords['pc vector number']
    p1 = map(float, coords[coord_2])
    p2 = map(float, coords[coord_1])
    if isarray(coord_1r) and isarray(coord_2r) and isarray(mat_ave):
        p1r = coord_2r
        p2r = coord_1r
    else:
        p1r = None
        p2r = None
        mat_ave = None

    if len(p1) != len(p2):
        raise ValueError("Principal coordinate vectors unequal length.")
    p1d = dict(zip(labels, p1))
    p2d = dict(zip(labels, p2))

    alpha = data['alpha']

    xy_coords = extract_and_color_xy_coords(
        p1d, p2d, p1r, p2r, mat_ave, colors,
        data_colors, groups, coords)

    img_src, img_map, eps_link = make_interactive_scatter(
        plot_label, dir_path,
        data_file_link, background_color, label_color,
        sample_location, alpha,
        xy_coords=xy_coords, props=props, x_len=4.5,
        y_len=4.5, size=20, draw_axes=True,
        generate_eps=generate_eps)

    return img_src + img_map, eps_link
Пример #15
0
def generate_2d_plots(prefs,data,html_dir_path,data_dir_path,filename,
                        background_color,label_color,generate_scree):
    """Generate interactive 2D scatterplots"""
    coord_tups = [("1", "2"), ("3", "2"), ("1", "3")]
    mapping=data['map']
    out_table=''
    #Iterate through prefs and generate html files for each colorby option
    #Sort by the column name first
    sample_location={}

    groups_and_colors=iter_color_groups(mapping,prefs)
    groups_and_colors=list(groups_and_colors)
    radiobuttons = []
    for i in range(len(groups_and_colors)):
        labelname=groups_and_colors[i][0] #'EnvoID'
        groups=groups_and_colors[i][1]    #defaultdict(<type 'list'>, {'mangrove biome/marine habitat/ocean water': ['BBA.number1.filt..660397', 'BBA.number2.filt..660380', ...}
        colors=groups_and_colors[i][2]    #{'mangrove biome/marine habitat/ocean water': 'red5', 'Small lake biome/marine habitat/saline lake sediment': 'cyan1', 
        data_colors=groups_and_colors[i][3]#{'orange1': <qiime.colors.Color object at 0x25f1210>, 'orange3': 
        data_color_order=groups_and_colors[i][4]#['red1', 'blue1', 'orange1', 'green1', 'purple1', 'yellow1', 'cyan1', 'pink1', 'teal1', ...]
        
        data_file_dir_path = get_random_directory_name(output_dir=data_dir_path)
        
        new_link=os.path.split(data_file_dir_path)
        data_file_link=os.path.join('.', os.path.split(new_link[-2])[-1], \
                                    new_link[-1])

        new_col_name=labelname
        img_data = {}
        plot_label=labelname
        
        if data.has_key('support_pcoas'):
            matrix_average, matrix_low, matrix_high, eigval_average, m_names = \
                summarize_pcoas(data['coord'], data['support_pcoas'], 
                method=data['ellipsoid_method'])
            data['coord'] = \
                (m_names,matrix_average,data['coord'][2],data['coord'][3])
            for i in range(len(m_names)):
                sample_location[m_names[i]]=i
        else: 
            matrix_average = None
            matrix_low =  None
            matrix_high =  None
            eigval_average =  None
            m_names =  None
        iterator=0

        for coord_tup in coord_tups: # change, if you want more thatn one PCoA plot! (i.e involving PC3)
            if isarray(matrix_low) and isarray(matrix_high) and \
                                                isarray(matrix_average):
                coord_1r=asarray(matrix_low)
                coord_2r=asarray(matrix_high)
                mat_ave=asarray(matrix_average)
            else:
                coord_1r=None
                coord_2r=None
                mat_ave=None
                sample_location=None
            
            coord_1, coord_2 = coord_tup
    
            img_data[coord_tup] = draw_pcoa_graph(plot_label,data_file_dir_path,
                                                 data_file_link,coord_1,coord_2,
                                                 coord_1r, coord_2r, mat_ave,\
                                                 sample_location,
                                                 data,prefs,groups,colors,
                                                 background_color,label_color,
                                                 data_colors,data_color_order,
                                                 generate_eps=True) 
        radiobuttons.append(RADIO % (data_file_link, labelname))
    
        

    
        if i == 0: ## only create first table!
            out_table += TABLE_HTML % (labelname, 
                                       "<br>".join(img_data[("1", "2")]),
                                       "<br>".join(img_data[("3", "2")]),
                                       "<br>".join(img_data[("1", "3")]))


    if generate_scree:
        data_file_dir_path = get_random_directory_name(output_dir = data_dir_path)
        new_link = os.path.split(data_file_dir_path)
        data_file_link = os.path.join('.', os.path.split(new_link[-2])[-1], new_link[-1])

        img_src, download_link = draw_scree_graph(data_file_dir_path, data_file_link, background_color,
                            label_color, generate_eps = True, data = data)

        out_table += SCREE_TABLE_HTML % ("<br>".join((img_src, download_link)))

    out_table = "\n".join(radiobuttons) + out_table
    outfile = create_html_filename(filename,'.html')
    outfile = os.path.join(html_dir_path,outfile)
        
    write_html_file(out_table,outfile)
Пример #16
0
def generate_2d_plots(prefs,data,html_dir_path,data_dir_path,filename,
                        background_color,label_color,generate_scree):
    """Generate interactive 2D scatterplots"""
    coord_tups = [("1", "2"), ("3", "2"), ("1", "3")]
    mapping=data['map']
    out_table=''
    #Iterate through prefs and generate html files for each colorby option
    #Sort by the column name first
    sample_location={}

    groups_and_colors=iter_color_groups(mapping,prefs)
    groups_and_colors=list(groups_and_colors)
    radiobuttons = []
    for i in range(len(groups_and_colors)):
        labelname=groups_and_colors[i][0] #'EnvoID'
        groups=groups_and_colors[i][1]    #defaultdict(<type 'list'>, {'mangrove biome/marine habitat/ocean water': ['BBA.number1.filt..660397', 'BBA.number2.filt..660380', ...}
        colors=groups_and_colors[i][2]    #{'mangrove biome/marine habitat/ocean water': 'red5', 'Small lake biome/marine habitat/saline lake sediment': 'cyan1', 
        data_colors=groups_and_colors[i][3]#{'orange1': <qiime.colors.Color object at 0x25f1210>, 'orange3': 
        data_color_order=groups_and_colors[i][4]#['red1', 'blue1', 'orange1', 'green1', 'purple1', 'yellow1', 'cyan1', 'pink1', 'teal1', ...]
        
        data_file_dir_path = get_random_directory_name(output_dir=data_dir_path)
        
        new_link=os.path.split(data_file_dir_path)
        data_file_link=os.path.join('.', os.path.split(new_link[-2])[-1], \
                                    new_link[-1])

        new_col_name=labelname
        img_data = {}
        plot_label=labelname
        
        if data.has_key('support_pcoas'):
            matrix_average, matrix_low, matrix_high, eigval_average, m_names = \
                summarize_pcoas(data['coord'], data['support_pcoas'], 
                method=data['ellipsoid_method'])
            data['coord'] = \
                (m_names,matrix_average,data['coord'][2],data['coord'][3])
            for i in range(len(m_names)):
                sample_location[m_names[i]]=i
        else: 
            matrix_average = None
            matrix_low =  None
            matrix_high =  None
            eigval_average =  None
            m_names =  None
        iterator=0

        for coord_tup in coord_tups: # change, if you want more thatn one PCoA plot! (i.e involving PC3)
            if isarray(matrix_low) and isarray(matrix_high) and \
                                                isarray(matrix_average):
                coord_1r=asarray(matrix_low)
                coord_2r=asarray(matrix_high)
                mat_ave=asarray(matrix_average)
            else:
                coord_1r=None
                coord_2r=None
                mat_ave=None
                sample_location=None
            
            coord_1, coord_2 = coord_tup
    
            img_data[coord_tup] = draw_pcoa_graph(plot_label,data_file_dir_path,
                                                 data_file_link,coord_1,coord_2,
                                                 coord_1r, coord_2r, mat_ave,\
                                                 sample_location,
                                                 data,prefs,groups,colors,
                                                 background_color,label_color,
                                                 data_colors,data_color_order,
                                                 generate_eps=True) 
        radiobuttons.append(RADIO % (data_file_link, labelname))
    
        

    
        if i == 0: ## only create first table!
            out_table += TABLE_HTML % (labelname, 
                                       "<br>".join(img_data[("1", "2")]),
                                       "<br>".join(img_data[("3", "2")]),
                                       "<br>".join(img_data[("1", "3")]))


    if generate_scree:
        data_file_dir_path = get_random_directory_name(output_dir = data_dir_path)
        new_link = os.path.split(data_file_dir_path)
        data_file_link = os.path.join('.', os.path.split(new_link[-2])[-1], new_link[-1])

        img_src, download_link = draw_scree_graph(data_file_dir_path, data_file_link, background_color,
                            label_color, generate_eps = True, data = data)

        out_table += SCREE_TABLE_HTML % ("<br>".join((img_src, download_link)))

    out_table = "\n".join(radiobuttons) + out_table
    outfile = create_html_filename(filename,'.html')
    outfile = os.path.join(html_dir_path,outfile)
        
    write_html_file(out_table,outfile)
Пример #17
0
def generate_2d_plots(prefs, data, html_dir_path, data_dir_path, filename,
                      background_color, label_color, generate_scree):
    """Generate interactive 2D scatterplots"""
    coord_tups = [("1", "2"), ("3", "2"), ("1", "3")]
    mapping = data['map']
    out_table = ''
    # Iterate through prefs and generate html files for each colorby option
    # Sort by the column name first
    sample_location = {}

    groups_and_colors = iter_color_groups(mapping, prefs)
    groups_and_colors = list(groups_and_colors)

    for i in range(len(groups_and_colors)):
        labelname = groups_and_colors[i][0]
        groups = groups_and_colors[i][1]
        colors = groups_and_colors[i][2]
        data_colors = groups_and_colors[i][3]
        data_color_order = groups_and_colors[i][4]

        data_file_dir_path = mkdtemp(dir=data_dir_path)

        new_link = os.path.split(data_file_dir_path)
        data_file_link = os.path.join('.', os.path.split(new_link[-2])[-1],
                                      new_link[-1])

        new_col_name = labelname
        img_data = {}
        plot_label = labelname

        if 'support_pcoas' in data:
            matrix_average, matrix_low, matrix_high, eigval_average, m_names = \
                summarize_pcoas(data['coord'], data['support_pcoas'],
                                method=data['ellipsoid_method'])
            data['coord'] = \
                (m_names, matrix_average, data['coord'][2], data['coord'][3])
            for i in range(len(m_names)):
                sample_location[m_names[i]] = i
        else:
            matrix_average = None
            matrix_low = None
            matrix_high = None
            eigval_average = None
            m_names = None
        iterator = 0

        for coord_tup in coord_tups:
            if isarray(matrix_low) and isarray(matrix_high) and \
                    isarray(matrix_average):
                coord_1r = asarray(matrix_low)
                coord_2r = asarray(matrix_high)
                mat_ave = asarray(matrix_average)
            else:
                coord_1r = None
                coord_2r = None
                mat_ave = None
                sample_location = None

            coord_1, coord_2 = coord_tup
            img_data[coord_tup] = draw_pcoa_graph(
                plot_label, data_file_dir_path,
                data_file_link, coord_1, coord_2,
                coord_1r, coord_2r, mat_ave,
                sample_location,
                data, prefs, groups, colors,
                background_color, label_color,
                data_colors, data_color_order,
                generate_eps=True)

        out_table += TABLE_HTML % (labelname,
                                   "<br>".join(img_data[("1", "2")]),
                                   "<br>".join(img_data[("3", "2")]),
                                   "<br>".join(img_data[("1", "3")]))

    if generate_scree:
        data_file_dir_path = mkdtemp(dir=data_dir_path)
        new_link = os.path.split(data_file_dir_path)
        data_file_link = os.path.join(
            '.',
            os.path.split(new_link[-2])[-1],
            new_link[-1])

        img_src, download_link = draw_scree_graph(
            data_file_dir_path, data_file_link, background_color,
            label_color, generate_eps=True, data=data)

        out_table += SCREE_TABLE_HTML % ("<br>".join((img_src, download_link)))

    outfile = create_html_filename(filename, '.html')
    outfile = os.path.join(html_dir_path, outfile)

    write_html_file(out_table, outfile)