Beispiel #1
0
def testdata_string(name):
    return resource_string(__name__, 'data/' + name, __file__)
Beispiel #2
0
def eps_formatter(logodata, format) :
    """ Generate a logo in Encapsulated Postscript (EPS)"""
    substitutions = {}
    from_format =[
        "creation_date",    "logo_width",           "logo_height",      
        "lines_per_logo",   "line_width",           "line_height",
        "line_margin_right","line_margin_left",     "line_margin_bottom",
        "line_margin_top",  "title_height",         "xaxis_label_height",
        "creator_text",     "logo_title",           "logo_margin",
        "stroke_width",     "tic_length",           
        "stacks_per_line",  "stack_margin",
        "yaxis_label",      "yaxis_tic_interval",   "yaxis_minor_tic_interval",
        "xaxis_label",      "xaxis_tic_interval",   "number_interval",
        "fineprint",        "shrink_fraction",      "errorbar_fraction",
        "errorbar_width_fraction",
        "errorbar_gray",    "small_fontsize",       "fontsize",
        "title_fontsize",   "number_fontsize",      "text_font",
        "logo_font",        "title_font",          
        "logo_label",       "yaxis_scale",          "end_type",
        "debug",            "show_title",           "show_xaxis",
        "show_xaxis_label", "show_yaxis",           "show_yaxis_label",
        "show_boxes",       "show_errorbars",       "show_fineprint",
        "rotate_numbers",   "show_ends",            "stack_height",
        "stack_width"
        ]
   
    for s in from_format :
        substitutions[s] = getattr(format,s)

    substitutions["shrink"] = str(format.show_boxes).lower()


    # --------- COLORS --------------
    def format_color(color):
        return  " ".join( ("[",str(color.red) , str(color.green), 
            str(color.blue), "]"))  

    substitutions["default_color"] = format_color(format.default_color)

    colors = []  
    for group in format.color_scheme.groups :
        cf = format_color(group.color)
        for s in group.symbols :
            colors.append( "  ("+s+") " + cf )
    substitutions["color_dict"] = "\n".join(colors)
        
    data = []
    
    # Unit conversion. 'None' for probability units
    conv_factor = std_units[format.unit_name]
    
    data.append("StartLine")

    seq_from = format.logo_start- format.first_index
    seq_to = format.logo_end - format.first_index +1

    # seq_index : zero based index into sequence data
    # logo_index : User visible coordinate, first_index based
    # stack_index : zero based index of visible stacks
    for seq_index in range(seq_from, seq_to) :
        logo_index = seq_index + format.first_index 
        stack_index = seq_index - seq_from
        
        if stack_index!=0 and (stack_index % format.stacks_per_line) ==0 :
            data.append("")
            data.append("EndLine")
            data.append("StartLine")
            data.append("")
        
        data.append("(%s) StartStack" % format.annotate[seq_index] )

        if conv_factor: 
            stack_height = logodata.entropy[seq_index] * std_units[format.unit_name]
        else :
            stack_height = 1.0 # Probability

        # Sort by frequency. If equal frequency then reverse alphabetic
        # (So sort reverse alphabetic first, then frequencty)
        # TODO: doublecheck this actual works
        s = list(zip(logodata.counts[seq_index], logodata.alphabet))
        s.sort(key= lambda x: x[1])
        s.reverse()
        s.sort(key= lambda x: x[0])
        
        
        if not format.reverse_stacks: s.reverse()

        C = float(sum(logodata.counts[seq_index])) 
        if C > 0.0 :
            fraction_width = 1.0
            if format.scale_width :
                fraction_width = logodata.weight[seq_index] 
            # print(fraction_width, file=sys.stderr)
            for c in s:
                data.append(" %f %f (%s) ShowSymbol" % (fraction_width, c[0]*stack_height/C, c[1]) )

        # Draw error bar on top of logo. Replaced by DrawErrorbarFirst above.
        if logodata.entropy_interval is not None and conv_factor and C>0.0:

            low, high = logodata.entropy_interval[seq_index]
            center = logodata.entropy[seq_index]
            low *= conv_factor
            high *= conv_factor
            center *=conv_factor
            if high> format.yaxis_scale : high = format.yaxis_scale 

            down = (center - low) 
            up   = (high - center) 
            data.append(" %f %f DrawErrorbar" % (down, up) )
            
        data.append("EndStack")
        data.append("")
               
    data.append("EndLine")
    substitutions["logo_data"] = "\n".join(data)  


    # Create and output logo
    template = resource_string( __name__, 'template.eps', __file__).decode()
    logo = Template(template).substitute(substitutions)

    return logo.encode()
Beispiel #3
0
def data_string( name ): 
    """Load the specified resource as a string."""
    fn = _resource_filenames[name]
    return resource_string(__name__, fn , __file__)    
Beispiel #4
0
def data_string(name):
    """Load the specified resource as a string."""
    fn = _resource_filenames[name]
    return resource_string(__name__, fn, __file__)
Beispiel #5
0
def testdata_string( name ): 
    return resource_string(__name__, 'data/'+name, __file__)    
Beispiel #6
0
def eps_formatter(logodata, format, fout):
    """ Generate a logo in Encapsulated Postscript (EPS)"""

    substitutions = {}
    from_format = [
        "creation_date", "logo_width", "logo_height", "lines_per_logo",
        "line_width", "line_height", "line_margin_right", "line_margin_left",
        "line_margin_bottom", "line_margin_top", "title_height",
        "xaxis_label_height", "creator_text", "logo_title", "logo_margin",
        "stroke_width", "tic_length", "stacks_per_line", "stack_margin",
        "yaxis_label", "yaxis_tic_interval", "yaxis_minor_tic_interval",
        "xaxis_label", "xaxis_tic_interval", "number_interval", "fineprint",
        "shrink_fraction", "errorbar_fraction", "errorbar_width_fraction",
        "errorbar_gray", "small_fontsize", "fontsize", "title_fontsize",
        "number_fontsize", "text_font", "logo_font", "title_font",
        "logo_label", "yaxis_scale", "end_type", "debug", "show_title",
        "show_xaxis", "show_xaxis_label", "show_yaxis", "show_yaxis_label",
        "show_boxes", "show_errorbars", "show_fineprint", "rotate_numbers",
        "show_ends", "stack_height", "stack_width", "stack_neg",
        "ovline_start", "ovline_end"
    ]

    for s in from_format:
        substitutions[s] = getattr(format, s)

    substitutions["shrink"] = str(format.show_boxes).lower()

    # --------- COLORS --------------
    def format_color(color):
        return " ".join(
            ("[", str(color.red), str(color.green), str(color.blue), "]"))

    substitutions["default_color"] = format_color(format.default_color)

    colors = []
    for group in format.color_scheme.groups:
        cf = format_color(group.color)
        for s in group.symbols:
            colors.append("  (" + s + ") " + cf)
    substitutions["color_dict"] = "\n".join(colors)

    ovline_start, ovline_end = format.ovline_start, format.ovline_end

    data = []

    # Unit conversion. 'None' for probability units
    conv_factor = std_units[format.unit_name]

    data.append("StartLine")

    seq_from = format.logo_start - format.first_index
    seq_to = format.logo_end - format.first_index + 1

    # seq_index : zero based index into sequence data
    # logo_index : User visible coordinate, first_index based
    # stack_index : zero based index of visible stacks
    for seq_index in range(seq_from, seq_to):
        logo_index = seq_index + format.first_index
        stack_index = seq_index - seq_from

        if stack_index != 0 and (stack_index % format.stacks_per_line) == 0:
            data.append("")
            data.append("EndLine")
            data.append("StartLine")
            data.append("")

        data.append("(%s) StartStack" % format.annotate[seq_index])

        if conv_factor:
            stack_height = logodata.entropy[seq_index] * std_units[
                format.unit_name]
        else:
            stack_height = 1.0  # Probability

        s = zip(logodata.counts[seq_index], logodata.alphabet)

        def mycmp(c1, c2):
            # Sort by frequency. If equal frequency then reverse alphabetic
            if c1[0] == c2[0]: return cmp(c2[1], c1[1])
            return cmp(c1[0], c2[0])

        s.sort(mycmp)
        if format.reverse_stacks: s.reverse()

        C = float(sum(logodata.counts[seq_index]))
        if C > 0.0:
            fraction_width = 1.0
            ## only if you scale the width, we don't do that
            ## if format.scale_width :
            ##     fraction_width = logodata.weight[seq_index]
            # print >>sys.stderr, fraction_width
            for c in s:
                data.append(" %f %f (%s) ShowSymbol" %
                            (fraction_width, c[0] * stack_height / C, c[1]))

        data.append("EndStack")
        if ovline_start <= seq_index <= ovline_end:
            data.append("DrawOverline")

        data.append("")

    data.append("EndLine")
    substitutions["logo_data"] = "\n".join(data)

    # Create and output logo
    template = resource_string(__name__, 'template.eps', __file__)
    logo = Template(template).substitute(substitutions)
    print >> fout, logo
Beispiel #7
0
def eps_formatter( logodata, format, fout, exeSort=True, p_title="P-value") :
    """ Generate a logo in Encapsulated Postscript (EPS)"""
    
    substitutions = {}
    from_format =[
        "creation_date",    "logo_width",           "logo_height",
        "lines_per_logo",   "line_width",           "line_height",
        "line_margin_right","line_margin_left",     "line_margin_bottom",
        "line_margin_top",  "title_height",         "xaxis_label_height",
        "creator_text",     "logo_title",           "logo_margin",
        "stroke_width",     "tic_length",
        "stacks_per_line",  "stack_margin",
        "yaxis_label",      "yaxis_tic_interval",   "yaxis_minor_tic_interval",
        "xaxis_label",      "xaxis_tic_interval",   "number_interval",
        "fineprint",        "shrink_fraction",      "errorbar_fraction",
        "errorbar_width_fraction",
        "errorbar_gray",    "small_fontsize",       "fontsize",
        "title_fontsize",   "number_fontsize",      "text_font",
        "logo_font",        "title_font",          
        "logo_label",       "yaxis_scale",          "end_type",
        "debug",            "show_title",           "show_xaxis",
        "show_xaxis_label", "show_yaxis",           "show_yaxis_label",
        "show_boxes",       "show_errorbars",       "show_fineprint",
        "rotate_numbers",   "show_ends",            "stack_height",
        "stack_width",      "show_colorkey"
        ]
   
    for s in from_format :
        substitutions[s] = getattr(format,s)

    substitutions["p_title"] = p_title
    substitutions["shrink"] = str(format.show_boxes).lower()

    # --------- COLORS --------------
    def format_color(color):
        return  " ".join( ("[",str(color.red) , str(color.green), 
            str(color.blue), "]"))

    def choose_color(x, groups):
        color = None
        d = 1000
        for key in groups.keys():
            cur_d = abs(abs(key)-abs(x))
            if cur_d < d:
               d = cur_d
               color = groups[key].color
        return color

    def format_heat_color(col, p_ref, m_ref=None):
        heat_colors = []
        if m_ref is None:
            for c in col:
                cf = format_color(choose_color(c[0], p_ref))
                heat_colors.append( "  ("+c[1]+") " + cf )
        else:
            for c in col:
                if c[0] >= 0:
                    cf = format_color(choose_color(c[0], p_ref))
                else:
                    cf = format_color(choose_color(c[0], m_ref))
                heat_colors.append( "  ("+c[1]+") " + cf )
        return "\n".join(heat_colors)

    plus_ref = {}
    minus_ref = {}
    p_values = "%\n["
    p_colors = " %\n["
    for group in format.heat_scheme.groups:
        value = float(group.pvalues)
        cf = format_color(group.color)
        p_values += " ("+group.pvalues+") %\n"
        p_colors += cf+" %\n"
        if value >= 0:
            plus_ref.update( {value:group} ) 
        else:
            minus_ref.update( {value:group} )
    if len(minus_ref) == 0:
        minusref = None
    if len(plus_ref) == 0:
        plus_ref = minus_ref
        minus_ref = None
    substitutions["p_values"] = p_values+"]"
    substitutions["p_colors"] = p_colors+"]"

    substitutions["default_color"] = format_color(format.default_color)
    colors = []
    if logodata.alphabet in [codon_dna_alphabet, codon_rna_alphabet]:
        for group in format.color_scheme.groups :
            cf = format_color(group.color)
            colors.append( "  ("+group.symbols+") " + cf )
    else:
        for group in format.color_scheme.groups :
            cf = format_color(group.color)
            for s in group.symbols :
                colors.append( "  ("+s+") " + cf )
    substitutions["color_dict"] = "\n".join(colors)

    data = []
    
    # Unit conversion. 'None' for probability units
    conv_factor = std_units[format.unit_name]
    
    data.append("StartLine")

    seq_from = format.logo_start - format.first_index
    seq_to = format.logo_end - format.first_index + 1

    # seq_index : zero based index into sequence data
    # logo_index : User visible coordinate, first_index based
    # stack_index : zero based index of visible stacks
    for seq_index in range(seq_from, seq_to) :
        logo_index = seq_index + format.first_index 
        stack_index = seq_index - seq_from
        
        if stack_index!=0 and (stack_index % format.stacks_per_line) ==0 :
            data.append("")
            data.append("EndLine")
            data.append("StartLine")
            data.append("")
        
        # color dictionary
        if format.make_heatmap == True:
            data.append("/color_dict <<")
            col = zip(logodata.pvalue[seq_index], logodata.alphabet)
            data.append(format_heat_color(col, plus_ref, minus_ref))
            data.append(">> def")
        data.append("(%s) StartStack" % format.annotate[seq_index] )

        if conv_factor:
            stack_height = logodata.entropy[seq_index] * std_units[format.unit_name]
        else :
            stack_height = 1.0 # Probability

        s = zip(logodata.counts[seq_index], logodata.alphabet)
        def mycmp( c1, c2 ) :
            # Sort by frequency. If equal frequency then reverse alphabetic
            if c1[0] == c2[0] : return cmp(c2[1], c1[1])
            return cmp(c1[0], c2[0])
        
        if exeSort: s.sort(mycmp)
        if not format.reverse_stacks: s.reverse()

        C = float(sum(logodata.counts[seq_index]))
        if C > 0.0 :
            fraction_width = 1.0
            if format.scale_width :
                fraction_width = logodata.weight[seq_index] 
            # print >>sys.stderr, fraction_width
            for c in s:
                data.append(" %f %f (%s) ShowSymbol" % (fraction_width, c[0]*stack_height/C, c[1]) )

        # Draw error bar on top of logo. Replaced by DrawErrorbarFirst above.
        if logodata.entropy_interval is not None and conv_factor and C>0.0:

            low, high = logodata.entropy_interval[seq_index]
            center = logodata.entropy[seq_index]
            low *= conv_factor
            high *= conv_factor
            center *=conv_factor
            if high> format.yaxis_scale : high = format.yaxis_scale 

            down = (center - low) 
            up   = (high - center) 
            data.append(" %f %f DrawErrorbar" % (down, up) )
            
        data.append("EndStack")
        data.append("")

    data.append("EndLine")
    substitutions["logo_data"] = "\n".join(data)

    # Create and output logo
    template = resource_string( __name__, 'template.eps', __file__)
    logo = Template(template).substitute(substitutions)
    print >>fout, logo