Example #1
0
def show_rna_structs(xvals, yvals, structs, energies, pfracs,
                     rname, rtype,ns,rfid,figsize,colors,
                     seq, n, selection_type,vert_idxs):


      verts = rutils.struct_verts([structs['structs'][i] for i in vert_idxs] 
                                  ,seq,rfid)
      
      f = myplots.fignum(3,figsize)
      ax = f.add_subplot(111)
      myplots.padded_limits(ax, xvals, yvals, .2)
      
      for vi, v in enumerate(verts):
            

            i = vert_idxs[vi]
            dims = [30]
            shadow_width = 10
            pkw0 = {'linewidth':shadow_width,
                    'color':'white',
                    'alpha':1,
                    'zorder':1.1}
            rplots.show_rna([xvals[i],yvals[i]], v,
                            dims = dims,
                            pkw = pkw0)
            
            pkw0 = {'linewidth':shadow_width,
                    'color':'white',
                    'alpha':.8,
                    'zorder':vi+2}
            rplots.show_rna([xvals[i],yvals[i]], v,
                            dims = dims,
                            pkw = pkw0)
            
            
            pkw1 = {'linewidth':2,
                    'color':colors[i],
                    'zorder':vi+2}
            rplots.show_rna([xvals[i],yvals[i]], v,
                            dims = dims, pkw = pkw1)
            ax.set_ylabel('mutation score')
            ax.set_xlabel('free energy (-kCal)')
            ax.annotate('''Suboptimal foldings, positioned by energy and
a mutation based evolutionary score.
Color indicates a second score from paired BL.''' , [0,1],xycoords ='axes fraction',
                        xytext = [10,-10], textcoords='offset pixels',
                        va = 'top')
          
      
      f.savefig(figfile.format('{3}_frac_silent_{0}_{1}{2}'.\
                                   format(rname,selection_type,n,rtype)))
      return vert_idxs
Example #2
0
def show_rna_structs(xvals, yvals, structs, energies, pfracs, rname, rtype, ns,
                     rfid, figsize, colors, seq, n, selection_type, vert_idxs):

    verts = rutils.struct_verts([structs['structs'][i] for i in vert_idxs],
                                seq, rfid)

    f = myplots.fignum(3, figsize)
    ax = f.add_subplot(111)
    myplots.padded_limits(ax, xvals, yvals, .2)

    for vi, v in enumerate(verts):

        i = vert_idxs[vi]
        dims = [30]
        shadow_width = 10
        pkw0 = {
            'linewidth': shadow_width,
            'color': 'white',
            'alpha': 1,
            'zorder': 1.1
        }
        rplots.show_rna([xvals[i], yvals[i]], v, dims=dims, pkw=pkw0)

        pkw0 = {
            'linewidth': shadow_width,
            'color': 'white',
            'alpha': .8,
            'zorder': vi + 2
        }
        rplots.show_rna([xvals[i], yvals[i]], v, dims=dims, pkw=pkw0)

        pkw1 = {'linewidth': 2, 'color': colors[i], 'zorder': vi + 2}
        rplots.show_rna([xvals[i], yvals[i]], v, dims=dims, pkw=pkw1)
        ax.set_ylabel('mutation score')
        ax.set_xlabel('free energy (-kCal)')
        ax.annotate('''Suboptimal foldings, positioned by energy and
a mutation based evolutionary score.
Color indicates a second score from paired BL.''', [0, 1],
                    xycoords='axes fraction',
                    xytext=[10, -10],
                    textcoords='offset pixels',
                    va='top')


    f.savefig(figfile.format('{3}_frac_silent_{0}_{1}{2}'.\
                                 format(rname,selection_type,n,rtype)))
    return vert_idxs
Example #3
0
def show_conservation(fidx = 0, reset = False):
    fnum = flist[fidx]
    rfid = 'RF{0:05}'.format(fnum)
    print rfid
    if fnum ==50: ftype = 'riboswitch'
    else: ftype = 'all'
    
    
    out = mem.getOrSet(setFamData,
                              **mem.rc({}, reset =reset,
                                       on_fail = 'compute',
                                       hardcopy = False,
                                       register = 'fdat'+rfid,
                                       ftype = ftype,
                                       rfid = rfid))

    
    mvals, tvals, structs = mem.getOrSet(setTree,
                                         **mem.rc({},reset = reset,
                                                  on_fail = 'compute',
                                                  hardcopy = True,
                                                  register = 'st'+rfid,
                                                  rfid = rfid,
                                                  ftype = ftype))
    
    idxs, tidx  = sutils.show_paired_v_energy(rfid,rfid,mvals,tvals,structs,ftype)
    
    all_pairs = structs['structs']
    all_energies = structs['energies']
    
    pints,eints, mints, tints = [structs['structs'][i] for i in idxs],\
        [ structs['energies'][i] for i in idxs],\
        [ mvals[tidx][i] for i in idxs],\
        [ tvals[tidx][i] for i in idxs]
    seq = structs['seq']
    
    if do_make_subopts:
        subopts = rutils.suboptimals(seq, n = 400)
        verts = rutils.struct_verts(subopts, seq, rfid)
        f = myplots.fignum(4,figsize)
        rplots.grid_rnas(verts, dims = [40])
        f.savefig(figfile.format('{0}_grid_rnas'.\
                                     format(rfid)))

    
                



    aff = rutils.struct_affinity_matrix(all_pairs, len(seq))
    pca = rutils.project_structs(all_pairs,
                          ptype ='pca',
                          affinities = aff,
                          n_comp = 3) 

    for metric in ['n_comp']:# ['frac_silent','frac_paired','n_comp']:
      scolors = []
      for i in range(len(tvals[tidx])):
          m_silent, pidxs, frac_good = sutils.metric(
              mvals[tidx][i],tvals[tidx][i],
              mtype = metric)
          
          scolors.append(mean(m_silent))
      scolors = myplots.rescale(scolors, [0.,1.])[:,newaxis] * array([1.,0.,0.])
      
      
      f = myplots.fignum(4,figsize)
      ax = f.add_subplot(111)
      xvals, yvals = pca[:,:2].T
      myplots.padded_limits(ax, xvals, yvals)
      
      ax.scatter(xvals,yvals,300,linewidth = 1,
                 edgecolor = 'black', color = scolors)

      ax.scatter(pca[idxs,0],pca[idxs,1], 2100 ,alpha = 1, 
                 color = 'black')
      ax.scatter(pca[idxs,0],pca[idxs,1], 2000 ,alpha = 1, 
                 color = 'white')
      ax.scatter(pca[idxs,0],pca[idxs,1], 400 ,alpha = 1, 
                 color = scolors[idxs],
                 )


      ax.annotate('''Conservation metric: {0}
Projected onto C=2 Principal Components'''.format(metric),
                  [0,1],xycoords = 'axes fraction', va = 'top',
                  xytext = [10,-10],textcoords='offset points')
      
      f.savefig(figfile.format('{0}_pca_{1}'.\
                                 format(rfid, metric)))
Example #4
0
def show_paired_v_energy(rname,rfid, all_muts, all_times, structs,rtype):
    if all_times == {}:
        return
    resolved_frac =  [ mean(list(it.chain(*[s_times['frac_resolved'] 
                                       for s_times in t_times.values()])))
                       for t_times in all_times.values()]
    total_lens = [mean(list(it.chain(*[s_times['total_time'] 
                                       for s_times in t_times.values()])))
                  for t_times in all_times.values()]
    total_lens_res = [mean(list(it.chain(*[s_times['total_time_res'] 
                                       for s_times in t_times.values()])))
                  for t_times in all_times.values()]    
    focus_tree = all_times.keys()[argmax(total_lens_res)]
    
    muts = all_muts[focus_tree]
    times = all_times[focus_tree]


    ns = len(muts.keys())
    s2 = dict(structs)
    s2['energies'] = s2['energies'][:ns]
    s2['structs'] = s2['structs'][:ns]
    structs = s2

    energies = structs['energies']


    
    f = myplots.fignum(3,figsize)

    xvals, yvals , pfracs, ugfracs = [], [], [], []
    for i, vals in enumerate(zip(muts.values(),times.values())):
        mvals, tvals = vals
        xvals.append( energies[i])
        
        frac_ug = metric(mvals, tvals, 'frac_ug')[0]
        pfrac,pinds,frac_good = metric(mvals, tvals, 'frac_silent')
        sfrac = metric(mvals, tvals, 'frac_silent')[0]
        

        ugfracs.append(frac_ug)
        pfracs.append(  mean(pfrac))
        yvals.append( mean(sfrac)*frac_good)
        
    colors = array(pfracs)
    colors = (colors - min(colors)) /(max(colors) -  min(colors))
    colors = colors[:,newaxis] * [0,1,0]

    ax = f.add_subplot(111)

    ax.scatter(xvals,yvals,array(ugfracs) * 200,
               color = colors)
    ax.set_ylabel('mutation score')
    ax.set_xlabel('free energy (-kCal)')
    ax.annotate('''Evaluated structures positioned by energy 
and a mutation based evolutionary score.
Color indicates fractional frequency of double mutants.
Radius indicates percentage of ungapped base pairs.''' , [0,1],xycoords = 'axes fraction',
                        xytext = [10,-10], textcoords='offset pixels',
                        va = 'top')
          
    myplots.padded_limits(ax, xvals, yvals, .2)
    f.savefig(figfile.format('{1}_frac_double_{0}'.format(rname,rtype)))
    f.clear()
            
    colors = array(pfracs)
    colors = (colors - min(colors)) /(max(colors) -  min(colors))
    colors = colors[:,newaxis] * [1,0,0]

    f = myplots.fignum(3,figsize)
    


    xvals, yvals , pfracs, ugfracs = [], [], [], []
    for i, vals in enumerate(zip(muts.values(),times.values())):
        mvals, tvals = vals
        xvals.append( energies[i])
        
        frac_ug = metric(mvals, tvals, 'frac_ug')[0]
        pfrac,pinds,frac_good = metric(mvals, tvals, 'frac_paired')
        sfrac = metric(mvals, tvals, 'frac_silent')[0]
        

        ugfracs.append(frac_ug)
        pfracs.append(  mean(pfrac)*frac_good)
        yvals.append( mean(sfrac)*frac_good)
        
    colors = array(pfracs)
    colors = (colors - min(colors)) /(max(colors) -  min(colors))
    colors = colors[:,newaxis] * [1,0,0]

    ax = f.add_subplot(111)

    ax.scatter(xvals,yvals,array(ugfracs) * 200,
               color = colors)
    ax.set_ylabel('mutation score')
    ax.set_xlabel('free energy (-kCal)')
    ax.annotate('''Evaluated structures positioned by energy 
and a mutation based evolutionary score.
Color indicates a second score from paired BL.
Radius indicates percentage of ungapped base pairs.''' , [0,1],
                xycoords = 'axes fraction',
                        xytext = [10,-10], textcoords='offset pixels',
                        va = 'top')
          
    myplots.padded_limits(ax, xvals, yvals, .2)
    f.savefig(figfile.format('{1}_frac_silent_{0}'.format(rname,rtype)))


    seq = structs['seq']
    n, selection_type = [4,'both']
    idxs = get_interesting_inds(xvals, yvals, structs, energies, pfracs,
                                rname, rtype, ns,rfid,figsize, colors,
                                seq,n,selection_type)

    if draw_single:
        show_rna_structs(xvals, yvals, structs, energies, pfracs,
                         rname, rtype, ns,rfid,figsize, colors,
                         seq,n,selection_type, idxs)
    if draw_many:
        for n, selection_type in \
                [[5,'ptime'],[5,'energy'],[ns,'energy']]:
            
            m_idxs = get_interesting_inds(xvals, yvals, structs, energies, pfracs,
                                rname, rtype, ns,rfid,figsize, colors,
                                seq,n,selection_type)
            show_rna_structs(xvals, yvals, structs, energies, pfracs,
                                    rname, rtype, ns,rfid,figsize, colors,
                                    seq,n,selection_type, m_idxs)
        
    return idxs,focus_tree
Example #5
0
def show_paired_v_energy(rname, rfid, all_muts, all_times, structs, rtype):
    if all_times == {}:
        return
    resolved_frac = [
        mean(
            list(
                it.chain(
                    *
                    [s_times['frac_resolved']
                     for s_times in t_times.values()])))
        for t_times in all_times.values()
    ]
    total_lens = [
        mean(
            list(
                it.chain(
                    *[s_times['total_time'] for s_times in t_times.values()])))
        for t_times in all_times.values()
    ]
    total_lens_res = [
        mean(
            list(
                it.chain(*[
                    s_times['total_time_res'] for s_times in t_times.values()
                ]))) for t_times in all_times.values()
    ]
    focus_tree = all_times.keys()[argmax(total_lens_res)]

    muts = all_muts[focus_tree]
    times = all_times[focus_tree]

    ns = len(muts.keys())
    s2 = dict(structs)
    s2['energies'] = s2['energies'][:ns]
    s2['structs'] = s2['structs'][:ns]
    structs = s2

    energies = structs['energies']

    f = myplots.fignum(3, figsize)

    xvals, yvals, pfracs, ugfracs = [], [], [], []
    for i, vals in enumerate(zip(muts.values(), times.values())):
        mvals, tvals = vals
        xvals.append(energies[i])

        frac_ug = metric(mvals, tvals, 'frac_ug')[0]
        pfrac, pinds, frac_good = metric(mvals, tvals, 'frac_silent')
        sfrac = metric(mvals, tvals, 'frac_silent')[0]

        ugfracs.append(frac_ug)
        pfracs.append(mean(pfrac))
        yvals.append(mean(sfrac) * frac_good)

    colors = array(pfracs)
    colors = (colors - min(colors)) / (max(colors) - min(colors))
    colors = colors[:, newaxis] * [0, 1, 0]

    ax = f.add_subplot(111)

    ax.scatter(xvals, yvals, array(ugfracs) * 200, color=colors)
    ax.set_ylabel('mutation score')
    ax.set_xlabel('free energy (-kCal)')
    ax.annotate('''Evaluated structures positioned by energy 
and a mutation based evolutionary score.
Color indicates fractional frequency of double mutants.
Radius indicates percentage of ungapped base pairs.''', [0, 1],
                xycoords='axes fraction',
                xytext=[10, -10],
                textcoords='offset pixels',
                va='top')

    myplots.padded_limits(ax, xvals, yvals, .2)
    f.savefig(figfile.format('{1}_frac_double_{0}'.format(rname, rtype)))
    f.clear()

    colors = array(pfracs)
    colors = (colors - min(colors)) / (max(colors) - min(colors))
    colors = colors[:, newaxis] * [1, 0, 0]

    f = myplots.fignum(3, figsize)

    xvals, yvals, pfracs, ugfracs = [], [], [], []
    for i, vals in enumerate(zip(muts.values(), times.values())):
        mvals, tvals = vals
        xvals.append(energies[i])

        frac_ug = metric(mvals, tvals, 'frac_ug')[0]
        pfrac, pinds, frac_good = metric(mvals, tvals, 'frac_paired')
        sfrac = metric(mvals, tvals, 'frac_silent')[0]

        ugfracs.append(frac_ug)
        pfracs.append(mean(pfrac) * frac_good)
        yvals.append(mean(sfrac) * frac_good)

    colors = array(pfracs)
    colors = (colors - min(colors)) / (max(colors) - min(colors))
    colors = colors[:, newaxis] * [1, 0, 0]

    ax = f.add_subplot(111)

    ax.scatter(xvals, yvals, array(ugfracs) * 200, color=colors)
    ax.set_ylabel('mutation score')
    ax.set_xlabel('free energy (-kCal)')
    ax.annotate('''Evaluated structures positioned by energy 
and a mutation based evolutionary score.
Color indicates a second score from paired BL.
Radius indicates percentage of ungapped base pairs.''', [0, 1],
                xycoords='axes fraction',
                xytext=[10, -10],
                textcoords='offset pixels',
                va='top')

    myplots.padded_limits(ax, xvals, yvals, .2)
    f.savefig(figfile.format('{1}_frac_silent_{0}'.format(rname, rtype)))

    seq = structs['seq']
    n, selection_type = [4, 'both']
    idxs = get_interesting_inds(xvals, yvals, structs, energies, pfracs, rname,
                                rtype, ns, rfid, figsize, colors, seq, n,
                                selection_type)

    if draw_single:
        show_rna_structs(xvals, yvals, structs, energies, pfracs, rname, rtype,
                         ns, rfid, figsize, colors, seq, n, selection_type,
                         idxs)
    if draw_many:
        for n, selection_type in \
                [[5,'ptime'],[5,'energy'],[ns,'energy']]:

            m_idxs = get_interesting_inds(xvals, yvals, structs, energies,
                                          pfracs, rname, rtype, ns, rfid,
                                          figsize, colors, seq, n,
                                          selection_type)
            show_rna_structs(xvals, yvals, structs, energies, pfracs, rname,
                             rtype, ns, rfid, figsize, colors, seq, n,
                             selection_type, m_idxs)

    return idxs, focus_tree