def checknsample(prefix, groups,eq,gmfile): linespg={} lenght=[] namesingm=[] for k, va in groups.iteritems(): lenght.append(len(va)) linespg[k]=[] for l in open(prefix+'.gm'): for sn in va: if sn == '': continue if eq[sn] in l: linespg[k].append(l) minimum=min(lenght) #minimum=40 #workaround to get contacts... uncomment previous line to restore move(prefix+'.gm',prefix+'_beforesampling.gm') landm = land2dict(prefix+'.landmarks') move(prefix+'.landmarks',prefix+'_beforesampling.landmarks') fout=open(prefix+'.gm','w') outf=open(prefix+'.landmarks','w') for ke, val in linespg.iteritems(): gout = open(ke+'.gm','w') S = sample(val,minimum) for i in S: fout.write(i) gout.write(i) namesingm.append(find_key(eq,i[1:i.find(':')])) for key, value in landm.iteritems(): if key in i: outf.write('>'+key+'\n'+value+'\n') gout.close() return namesingm
def checknsample(prefix, groups, eq, gmfile): linespg = {} lenght = [] namesingm = [] for k, va in groups.iteritems(): lenght.append(len(va)) linespg[k] = [] for l in open(prefix + '.gm'): for sn in va: if sn == '': continue if eq[sn] in l: linespg[k].append(l) minimum = min(lenght) #minimum=40 #workaround to get contacts... uncomment previous line to restore move(prefix + '.gm', prefix + '_beforesampling.gm') landm = land2dict(prefix + '.landmarks') move(prefix + '.landmarks', prefix + '_beforesampling.landmarks') fout = open(prefix + '.gm', 'w') outf = open(prefix + '.landmarks', 'w') for ke, val in linespg.iteritems(): gout = open(ke + '.gm', 'w') S = sample(val, minimum) for i in S: fout.write(i) gout.write(i) namesingm.append(find_key(eq, i[1:i.find(':')])) for key, value in landm.iteritems(): if key in i: outf.write('>' + key + '\n' + value + '\n') gout.close() return namesingm
def plot(prefix, masterd, fontsize, symlog, threeD): """ will plot two axes of the PCoA, colored by taxonomic level """ fil = open(prefix + "_PCoA_axes%d_%d.equivalences" % (X, Y), "w") F = fasta(prefix + ".fasta") markers = [ "k.", "b+", "g*", "r.", "c+", "m*", "y.", "k+", "b*", "g.", "r+", "c*", "m.", "y+", "k*", "b.", "g+", "r*", "c.", "m+", "y*", ] count = 0 c = 0 fig = plt.figure() # figsize=(6.83 , 9.19), dpi=300) ax = fig.add_subplot(111) if threeD: fig3D = plt.figure() ax3D = fig3D.gca(projection="3d") ax.spines["top"].set_color("none") ax.xaxis.tick_bottom() ax.spines["right"].set_color("none") ax.yaxis.tick_left() for k in masterd: x = masterd[k][1] y = masterd[k][2] z = masterd[k][3] ax.plot(x, y, markers[c], label=k) if threeD: ax3D.plot(x, y, z, markers[c], label=k) c += 1 f = 0 for e in range(len(masterd[k][1])): ax.annotate(count, (x[f] + 0.1, y[f] + 0.1), fontsize=fontsize) if threeD: ax3D.text(x[f] + 0.1, y[f] + 0.1, z[f] + 0.1, str(count), fontsize=fontsize) fil.write(masterd[k][0][e] + "\t" + find_key(F.chains, masterd[k][0][e]) + "\t" + str(count) + "\n") count += 1 f += 1 if symlog: ax.set_xscale("symlog") ax.set_yscale("symlog") ax.set_xlabel("Axis %d (symmetrical log)" % (X), fontsize=fontsize) ax.set_ylabel("Axis %d (symmetrical log)" % (Y), fontsize=fontsize) else: ax.set_xlabel("Axis %d" % (X), fontsize=fontsize) ax.set_ylabel("Axis %d" % (Y), fontsize=fontsize) ax.legend(loc=0, fancybox=True, shadow=True) # bbox_to_anchor=(0.5, 1.1), ncol=4, fig.tight_layout() if threeD: ax3D.set_xlabel("Axis 1", fontsize=fontsize) ax3D.set_ylabel("Axis 2", fontsize=fontsize) ax3D.set_zlabel("Axis 3", fontsize=fontsize) ax3D.view_init(30, 45) fig3D.tight_layout() ax3D.legend(loc=0, fancybox=True, shadow=True) # bbox_to_anchor=(0.5, -0.075),ncol=4, plt.show() fig.savefig(prefix + "_Axis%d_%dPCoA.png" % (X, Y), dpi=300) if threeD: fig3D.savefig(prefix + "_3AxesPCoA.png", dpi=300) fil.close()
def plot(prefix, masterd, fontsize, symlog, threeD): ''' will plot two axes of the PCoA, colored by taxonomic level ''' fil = open(prefix + '_PCoA_axes%d_%d.equivalences' % (X, Y), 'w') F = fasta(prefix + '.fasta') markers = [ 'k.', 'b+', 'g*', 'r.', 'c+', 'm*', 'y.', 'k+', 'b*', 'g.', 'r+', 'c*', 'm.', 'y+', 'k*', 'b.', 'g+', 'r*', 'c.', 'm+', 'y*' ] count = 0 c = 0 fig = plt.figure() #figsize=(6.83 , 9.19), dpi=300) ax = fig.add_subplot(111) if threeD: fig3D = plt.figure() ax3D = fig3D.gca(projection='3d') ax.spines['top'].set_color('none') ax.xaxis.tick_bottom() ax.spines['right'].set_color('none') ax.yaxis.tick_left() for k in masterd: x = masterd[k][1] y = masterd[k][2] z = masterd[k][3] ax.plot(x, y, markers[c], label=k) if threeD: ax3D.plot(x, y, z, markers[c], label=k) c += 1 f = 0 for e in range(len(masterd[k][1])): ax.annotate(count, (x[f] + 0.1, y[f] + 0.1), fontsize=fontsize) if threeD: ax3D.text(x[f] + 0.1, y[f] + 0.1, z[f] + 0.1, str(count), fontsize=fontsize) fil.write(masterd[k][0][e] + '\t' + find_key(F.chains, masterd[k][0][e]) + '\t' + str(count) + '\n') count += 1 f += 1 if symlog: ax.set_xscale("symlog") ax.set_yscale("symlog") ax.set_xlabel('Axis %d (symmetrical log)' % (X), fontsize=fontsize) ax.set_ylabel('Axis %d (symmetrical log)' % (Y), fontsize=fontsize) else: ax.set_xlabel('Axis %d' % (X), fontsize=fontsize) ax.set_ylabel('Axis %d' % (Y), fontsize=fontsize) ax.legend( loc=0, #bbox_to_anchor=(0.5, 1.1), ncol=4, fancybox=True, shadow=True) fig.tight_layout() if threeD: ax3D.set_xlabel('Axis 1', fontsize=fontsize) ax3D.set_ylabel('Axis 2', fontsize=fontsize) ax3D.set_zlabel('Axis 3', fontsize=fontsize) ax3D.view_init(30, 45) fig3D.tight_layout() ax3D.legend( loc=0, #bbox_to_anchor=(0.5, -0.075),ncol=4, fancybox=True, shadow=True) plt.show() fig.savefig(prefix + '_Axis%d_%dPCoA.png' % (X, Y), dpi=300) if threeD: fig3D.savefig(prefix + '_3AxesPCoA.png', dpi=300) fil.close()