def draw_profile_plots(breaks,avg_upstreams,avg_downstreams,metagene_breaks,avg_metagenes,metaexon_breaks,avg_metaexons,metaintron_breaks,avg_metaintrons,metagene_breaks_lim=[-1000,1000],legends=[]):

    # comments
    R.comment('')
    R.comment('Draw wig profiles')
    R.comment('')
    
    # layout
    rscript=R.par(mar=[4,4,5,3.8],oma=[4,2,4,2])
    mat=[[1,2],[3,3],[4,5]]
    rscript+=R.layout(mat,widths=[1,1],heights=[1,1,1])
    n_profiles=len(avg_upstreams)
    
    # legends
    if legends:
        legends+=['All']
    else:
        legends=['Group %d' %i for i in range(1,n_profiles)]+['All']
    
    # colors
    #default_cols=['red','blue','hotpink','cyan','green','bown']
    #cols=default_cols[:n_profiles-1]+['black']
    
    # for metagene plot
    start_up,end_up=where(max(metagene_breaks_lim[0],breaks[0]),0,breaks)
    start_down,end_down=where(1,min(metagene_breaks_lim[1]+1,breaks[-1]+1),breaks)
    comb_breaks=breaks[start_up:end_up]+metagene_breaks+[b+metagene_breaks[-1] for b in breaks[start_down:end_down]]
    comb_metagenes=[up[start_up:end_up]+mg+down[start_down:end_down] for up,mg,down in zip(avg_upstreams,avg_metagenes,avg_downstreams)]
    
    ylim =[0, 0]
    no_empty_upstreams = filter_out_empty_rows(avg_upstreams)
    if no_empty_upstreams:
        ylim=[min(min_row_by_row(no_empty_upstreams,True))-abs(min(min_row_by_row(no_empty_upstreams,True)))*0.1,max(max_row_by_row(no_empty_upstreams,True))+abs(max(max_row_by_row(no_empty_upstreams,True)))*0.1]
    rscript+=draw_multiple_profiles(breaks,avg_upstreams,main='Average Profiles near TSS',xlab='Relative Distance (bp)',ylab='Average Profile',v=0,legends=legends,cols=[], ylim=ylim)
    
    ylim = [0, 0]
    no_empty_downstreams = filter_out_empty_rows(avg_downstreams)
    if no_empty_downstreams:
        ylim=[min(min_row_by_row(no_empty_downstreams,True))-abs(min(min_row_by_row(no_empty_downstreams,True)))*0.1,max(max_row_by_row(no_empty_downstreams,True))+abs(max(max_row_by_row(no_empty_downstreams,True)))*0.1]
    rscript+=draw_multiple_profiles(breaks,avg_downstreams,main='Average Profiles near TTS',xlab='Relative Distance (bp)',ylab='Average Profile',v=0,legends=legends, cols=[], ylim=ylim)
    
    ylim = [0, 0]
    no_empty_metagenes = filter_out_empty_rows(comb_metagenes)
    if no_empty_metagenes:
        ylim=[abs(min(min_row_by_row(no_empty_metagenes,True)))*0.1-min(min_row_by_row(no_empty_metagenes,True)),max(max_row_by_row(no_empty_metagenes,True))+abs(max(max_row_by_row(no_empty_metagenes,True)))*0.1]
    rscript+=draw_multiple_profiles(comb_breaks,comb_metagenes,main='Average Gene Profiles',xlab='Upstream (bp), %d bp of Meta-gene, Downstream (bp)' %int(metagene_breaks[-1]),ylab='Average Profile',legends=legends, cols=[], ylim=ylim)
    rscript+=R.abline(v=metagene_breaks[0],lty=2,col=['black'])
    rscript+=R.abline(v=metagene_breaks[-1],lty=2,col=['black'])
    
    ylim = [0, 0]
    no_empty_metaexons = filter_out_empty_rows(avg_metaexons)
    no_empty_metaintrons = filter_out_empty_rows(avg_metaintrons)
    if no_empty_metaexons and no_empty_metaintrons:
        ylim=[min(min(min_row_by_row(no_empty_metaexons,True)),min(min_row_by_row(no_empty_metaintrons,True))),max(max(max_row_by_row(no_empty_metaexons,True)),max(max_row_by_row(no_empty_metaintrons,True)))+abs(max(max(max_row_by_row(no_empty_metaexons,True)),max(max_row_by_row(no_empty_metaintrons,True))))*0.2]
    elif not no_empty_metaexons and no_empty_metaintrons:
        ylim = [min(min_row_by_row(no_empty_metaintrons,True))-abs(min(min_row_by_row(no_empty_metaintrons,True)))*0.1,max(max_row_by_row(no_empty_metaintrons,True))+abs(max(max_row_by_row(no_empty_metaintrons,True)))*0.1]
    elif no_empty_metaexons and not no_empty_metaintrons:
        ylim = [min(min_row_by_row(no_empty_metaexons,True))-abs(min(min_row_by_row(no_empty_metaexons,True)))*0.1,max(max_row_by_row(no_empty_metaexons,True))+abs(max(max_row_by_row(no_empty_metaexons,True)))*0.1]
    rscript+=draw_multiple_profiles(metaexon_breaks,avg_metaexons,main='Average Concatenated Exon Profiles',xlab='Relative Location (%)',ylab='Average Profile',ylim=ylim,cols=[], legends=legends)
    rscript+=draw_multiple_profiles(metaintron_breaks,avg_metaintrons,main='Average Concatenated Intron Profiles',xlab='Relative Location (%)',ylab='Average Profile',ylim=ylim,cols=[], legends=legends)
    
    return rscript
dataset/ETHPy150Open taoliu/taolib/Assoc/inout.py/draw_profile_plots/swapped operands of `-`