Projects/laydi
Projects
/
laydi
Archived
7
0
Fork 0
This commit is contained in:
Arnar Flatberg 2007-08-21 10:25:23 +00:00
parent 26ab6c3fe7
commit e06eeb6d17
3 changed files with 125 additions and 82 deletions

View File

@ -5,7 +5,7 @@ import networkx as nx
def plot_corrloads(R, pc1=0,pc2=1,s=20, c='b', zorder=5,expvar=None,ax=None,drawback=True, labels=None):
""" Correlation loading plot."""
# backgorund
# background
if ax==None or drawback==True:
radius = 1
center = (0,0)

View File

@ -2,7 +2,7 @@
import scipy
import rpy
silent_eval = rpy.with_mode(rpy.NO_CONVERSION, rpy.r)
import collections
def goterms_from_gene(genelist, ontology='BP', garbage=None):
""" Returns the go-terms from a specified genelist (Entrez id).
@ -167,3 +167,47 @@ def gene_GO_hypergeo_test(genelist,universe="entrezUniverse",ontology="BP",chip
result = rpy.r.summary(rpy.r.hyperGTest(params))
return rpy.r.summary(result), params
def data_aff2loc_hgu133a(X, aff_ids, verbose=False):
aff_ids = scipy.asarray(aff_ids)
if verbose:
print "\nNumber of probesets in affy list: %s" %len(aff_ids)
import rpy
rpy.r.library("hgu133a")
trans_table = rpy.r.as_list(rpy.r.hgu133aENTREZID)
if verbose:
print "Number of entrez ids: %d" %(scipy.asarray(trans_table.values())>0).sum()
enz2aff = collections.defaultdict(list)
#aff2enz = collections.defaultdict(list)
for aff, enz in trans_table.items():
if int(enz)>0 and (aff in aff_ids):
enz2aff[enz].append(aff)
#aff2enz[aff].append(enz)
if verbose:
print "\nNumber of translated entrez ids: %d" %len(enz2aff)
aff2ind = dict(zip(aff_ids, scipy.arange(len(aff_ids))))
var_x = X.var(0)
new_data = []
new_ids = []
m = 0
s = 0
for enz, aff_id_list in enz2aff.items():
index = [aff2ind[aff_id] for aff_id in aff_id_list]
if len(index)>1:
m+=1
if verbose:
pass
#print "\nEntrez id: %s has %d probesets" %(enz, len(index))
#print index
xsub = X[:,index]
choose_this = scipy.argmax(xsub.var(0))
new_data.append(xsub[:,choose_this].ravel())
else:
s+=1
new_data.append(X[:,index].ravel())
new_ids.append(enz)
if verbose:
print "Ids with multiple probesets: %d" %m
print "Ids with unique probeset: %d" %s
X = scipy.asarray(new_data).T
return X, new_ids

View File

@ -11,10 +11,43 @@ import cx_stats
from plots_lpls import plot_corrloads
######## DATA ##########
use_data='uma'
if use_data=='smoker':
# full smoker data
DX = dataset.read_ftsv(open("../../data/smokers-full/Smokers.ftsv"))
DY = dataset.read_ftsv(open("../../data/smokers-full/Yg.ftsv"))
Y = DY.asarray().astype('d')
gene_ids = DX.get_identifiers('gene_ids', sorted=True)
elif use_data=='scherf':
DX = dataset.read_ftsv(open("../../data/scherf/Scherf.ftsv"))
DY = dataset.read_ftsv(open("../../data/scherf/Yd.ftsv"))
Y = DY.asarray().astype('d')
gene_ids = DX.get_identifiers('gene_ids', sorted=True)
elif use_data=='staunton':
pass
elif use_data=='uma':
DX = dataset.read_ftsv(open("../../data/uma/X133.ftsv"))
DY = dataset.read_ftsv(open("../../data/uma/Yg133.ftsv"))
Y = DY.asarray().astype('d')
gene_ids = DX.get_identifiers('gene_ids', sorted=True)
# Use only subset defined on GO
ontology = 'BP'
print "\n\nFiltering genes by Go terms "
gene2goterms = rpy_go.goterms_from_gene(gene_ids)
all_terms = set()
for t in gene2goterms.values():
all_terms.update(t)
terms = list(all_terms)
print "\nNumber of go-terms: %s" %len(terms)
# update genelist
gene_ids = gene2goterms.keys()
print "\nNumber of genes: %s" %len(gene_ids)
# use subset based on SAM or IQR
subset = 'm'
if subset=='sam':
# select subset genes by SAM
rpy.r.library("siggenes")
rpy.r.library("qvalue")
@ -32,84 +65,51 @@ print "SAM done"
qq = rpy.r('qobj<-qvalue(sam.out@p.value)')
qvals = asarray(qq['qvalues'])
# cut off
cutoff = 0.05
cutoff = 0.001
index = where(qvals<cutoff)[0]
# Subset data
X = DX.asarray()
Xr = X[:,index]
#Xr = X[:,index]
gene_ids = DX.get_identifiers('gene_ids', index)
print "\nWorking on subset with %s genes " %len(gene_ids)
#gene2ind = {}
#for i, gene in enumerate(gene_ids):
# gene2ind[gene] = i
else:
# noimp (smoker data is prefiltered)
pass
### Build GO data ####
print "\n\nFiltering genes by Go terms "
gene2goterms = rpy_go.goterms_from_gene(gene_ids)
all_terms = set()
for t in gene2goterms.values():
all_terms.update(t)
terms = list(all_terms)
print "\nNumber of go-terms: %s" %len(terms)
# update genelist
gene_ids = gene2goterms.keys()
print "\nNumber of genes: %s" %len(gene_ids)
rpy.r.library("GOSim")
# Go-term similarity matrix
methods = ("JiangConrath","Resnik","Lin","CoutoEnriched","CoutoJiangConrath","CoutoResnik","CoutoLin")
meth = methods[0]
meth = methods[2]
print "Term-term similarity matrix (method = %s)" %meth
if meth=="CoutoEnriched":
rpy.r('setEnrichmentFactors(alpha=0.1,beta=0.5)')
print "\nCalculating term-term similarity matrix"
rpytmat1 = rpy.with_mode(rpy.NO_CONVERSION, rpy.r.getTermSim)(terms, method=meth,verbose=False)
tmat1 = rpy.r.assign("haha", rpytmat1)
# check if all terms where found
nanindex = where(isnan(tmat1[:,0]))[0]
if len(nanindex)>0:
raise valueError("NANs in tmat")
# Z-matrix
#Z, newind = rpy_go.genego_matrix(terms, tmat, gene_ids, terms,func=mean)
#Z = Z.T
Z = rpy_go.genego_sim(gene2goterms,gene_ids,terms,rpytmat1,go_term_sim="OA",term_sim=meth)
#### do another
#meth = methods[4]
#rpytmat = rpy.with_mode(rpy.NO_CONVERSION, rpy.r.getTermSim)(terms, method=meth,verbose=False)
#tmat = rpy.r.assign("haha", rpytmat)
# check if all terms where found
#nanindex = where(isnan(tmat[:,0]))[0]
#if len(nanindex)>0:
# raise valueError("NANs in tmat")
# Z-matrix
#Z, newind = rpy_go.genego_matrix(terms, tmat, gene_ids, terms,func=mean)
#Z = Z.T
#Z = rpy_go.genego_sim(gene2goterms,gene_ids,terms,rpytmat,go_term_sim="OA",term_sim=meth)
rpytmat = rpy.with_mode(rpy.NO_CONVERSION, rpy.r.getTermSim)(terms, method=meth,verbose=False)
tmat = rpy.r.assign("haha", rpytmat)
print "\n Calculating Z matrix"
Z = rpy_go.genego_sim(gene2goterms,gene_ids,terms,rpytmat,go_term_sim="OA",term_sim=meth)
# update data (X) matrix
#newind = [gene2ind[gene] for gene in gene_ids]
newind = DX.get_indices('gene_ids', gene_ids)
Xr = X[:,newind]
#new_gene_ids = asarray(gene_ids)[newind]
Xr = DX.asarray()[:,newind]
######## LPLSR ########
print "LPLSR ..."
a_max = 5
aopt = 3
alpha=.4
mean_ctr = [2, 0, 1]
xz_alpha = .5
w_alpha = .1
mean_ctr = [2, 0, 2]
# standardize Z?
sdtz = False
if sdtz:
Z = Z/Z.std(0)
T, W, P, Q, U, L, K, B, b0, evx, evy, evz = nipals_lpls(Xr,Y,Z, a_max,
alpha=alpha,
alpha=xz_alpha,
mean_ctr=mean_ctr)
# Correlation loadings
@ -117,13 +117,13 @@ dx,Rx,rssx = correlation_loadings(Xr, T, P)
dx,Ry,rssy = correlation_loadings(Y, T, Q)
cadz,Rz,rssz = correlation_loadings(Z.T, W, L)
# Prediction error
rmsep , yhat, class_error = cv_lpls(Xr, Y, Z, a_max, alpha=alpha,mean_ctr=mean_ctr)
alpha_check=True
rmsep , yhat, class_error = cv_lpls(Xr, Y, Z, a_max, alpha=xz_alpha,mean_ctr=mean_ctr)
alpha_check=False
if alpha_check:
Alpha = arange(0.01, 1, .1)
Rmsep,Yhat, CE = [],[],[]
for a in Alpha:
rmsep , yhat, ce = cv_lpls(Xr, Y, Z, a_max, alpha=alpha)
rmsep , yhat, ce = cv_lpls(Xr, Y, Z, a_max, alpha=xz_alpha,mean_ctr=mean_ctr)
Rmsep.append(rmsep)
Yhat.append(yhat)
CE.append(ce)
@ -131,10 +131,8 @@ if alpha_check:
Yhat = asarray(Yhat)
CE = asarray(CE)
# Significance Hotellings T
Wx, Wz, Wy, = jk_lpls(Xr, Y, Z, aopt)
Wx, Wz, Wy, = jk_lpls(Xr, Y, Z, aopt, mean_ctr=mean_ctr,alpha=w_alpha)
Ws = W*apply_along_axis(norm, 0, T)
tsqx = cx_stats.hotelling(Wx, Ws[:,:aopt])
tsqz = cx_stats.hotelling(Wz, L[:,:aopt])
@ -157,7 +155,8 @@ ylim([class_error.min()-.05, class_error.max()+.05])
title('Classification accuracy')
figure(3) # Hypoid correlations
plot_corrloads(Rz, pc1=0, pc2=1, s=tsqz/10.0, c='b', zorder=5, expvar=evz, ax=None)
tsqz_s = 250*tsqz/tsqz.max()
plot_corrloads(Rz, pc1=0, pc2=1, s=tsqz_s, c='b', zorder=5, expvar=evz, ax=None)
ax = gca()
ylabels = DY.get_identifiers('_status', sorted=True)
plot_corrloads(Ry, pc1=0, pc2=1, s=150, c='g', zorder=5, expvar=evy, ax=ax,labels=ylabels)