def draw_ChIP_over_genome_var_colors(ChIP, wig_size, n_peaks = 10000):
    """Draw heatmap bars of ChIP regions over the genome
    
    Parameters:
    ChIP: a Bed object of ChIP regions with scores
    wig_size: a dictionary contains the start and end positions of chromosomes
    """

    #
    # parameters for the graphics and mathematical process
    #
    
    # page dimension
    width = 8.5         # inch
    height = 11.5       # inch
    pixel = 1.0/72      # line width
    mar=[4,4,5,3.8] # marbin
    oma=[4, 2, 4, 2]    # outer margin
    rough_plot_width = 6.5
    
    # page segmentation
    widths = [1, 1]
    heights = [1, 5.5]
    mat = [[1, 0], [2, 2]]
     
    # percentage of scores to consider
    percentage = 98    
    
    # number of colors in the colormap
    n_colors = 16
    
    #
    # get info from chromosomes and ChIP regions
    #
    
    # get the chromosomes
    if wig_size != None:
        chroms = wig_size.keys()
        chroms = sort_chroms(chroms)
    else:
        chroms = ChIP.get_chroms()
    n_chroms = len(chroms)
    if n_chroms == 0: return rscript
    
    # bar height: adjusted according to the number of chromosomes
    hdelta = (0.4 - 0.2)/(24 - 6) * (n_chroms - 6) + 0.2 
    
    # get the maximum chromosome size
    if wig_size != None:
        minx = min([wig_size[chrom][0] for chrom in chroms])
        maxx = max([wig_size[chrom][1] for chrom in chroms]) 
    else:
        minx = min([ChIP[chrom]['start'][0] for chrom in chroms])
        maxx = max([ChIP[chrom]['end'][-1] for chrom in chroms])
    maxchrom = [minx, maxx]
    
    # if there are more than n_peaks, just select n_peaks by score
    reducedChIP = select_ChIP_by_n_peaks(ChIP, n_peaks = n_peaks, descend = True)
    # because the resulting pdf does not have infinite resolution, we need to get rid of redundant signals in the pdf.
    interval = (maxchrom[1] - maxchrom[0]) / (rough_plot_width * int(1/pixel))   # numerator: maximum chromosome length, denominator: how many bars can be drawn in rough_plot_width (6.5 in by default), interval is the bar width in bp
    reducedChIP = downsample_ChIP(reducedChIP, interval = interval)
    
    # get the histogram of bed
    left, right = get_percent_from_BED(reducedChIP, percentage, twoside=False)
    ylim=[left, right]
    maxmax = max([abs(ylim[0]), ylim[1]])       # get the largest boundary
    
    n_bins = 16
    if ylim[0] < 0: # if negative score, make symmetric
        ylim = [-1*maxmax, maxmax]
        breaks = linspace(-1*maxmax, maxmax, n_bins+1)
    else: # asymmetric
        ylim = [0, ylim[1]] 
        breaks = linspace(0, ylim[1]+0.2, n_bins+1) # add a small value to the maximum to include the max value
    histogram = get_histogram_BED_scores(reducedChIP, breaks)
    
    #
    # header print out in the R script
    #
    rscript = '\n'
    rscript+=R.comment('')
    rscript+=R.comment('ChIP regions over the genome')
    rscript+=R.comment('')
    rscript+= '\n'
    
    #
    # draw the color bar and heatmaps
    #
        
    # set the margins
    rscript += R.par(mar=mar,oma=oma)
    rscript += R.layout(mat=mat,widths=widths,heights=heights)
    
    # select color scheme
    if ylim[0] < 0:
        colormap = "GBR"
    else:
        colormap = "BR"
    cmd = 'cm = graphics.%s' %colormap
    exec(cmd) 
    
    # generate color map using R's colorRampPalette function
    rscript += R.colorRampPalette(cm, rval=colormap)
    cmap = 'cmap'   # color map name
    rscript += '%s <- %s(%s)\n' %(cmap, colormap, str(n_colors))
    rscript += draw_colorbar(ylim, vertical=False, cmap = cmap, hist=histogram, linecol='cyan')
    
    # iterate through chromosomes
    rscript += R.plot(maxchrom, [1 - hdelta, n_chroms + hdelta], tp="n", col="", ylab="Chromosome", xlim=maxchrom, ylim=[1 - hdelta, n_chroms + hdelta], frame=False, xaxt="s", yaxt="n", main="ChIP Regions over Chromosomes", xlab="Chromosome Size (bp)")
    count = n_chroms
    for chrom in chroms:
        bot = count - hdelta
        top = count + hdelta
        try:
            rscript += R.rectangles_with_heights_and_colors(reducedChIP[chrom]['start'], reducedChIP[chrom]['end'], reducedChIP[chrom]['score'], ylim, bot=bot, top=top, xaxt = "n", cmap = cmap)
            rscript += R.mtext(chrom.rsplit('chr', 1)[-1], side=2, at=(top+bot)/2)
        except KeyError:    # when ChIP does not have this chromosome
            rscript += R.rectangles_with_heights_and_colors(None, None, None, [minx, maxx], ylim, bot=bot, top=top, xaxt = "n")
            rscript += R.mtext(chrom.rsplit('chr', 1)[-1], side=2, at=(top+bot)/2)
        count -= 1
        
    return rscript
dataset/ETHPy150Open taoliu/taolib/Assoc/inout.py/draw_ChIP_over_genome_var_colors/original