2007-07-20 11:36:26 +02:00
|
|
|
import sys
|
|
|
|
import rpy
|
|
|
|
from pylab import gca, figure, subplot
|
|
|
|
from scipy import *
|
|
|
|
from lpls import *
|
|
|
|
import rpy_go
|
|
|
|
sys.path.append("../../fluents") # home of dataset
|
|
|
|
sys.path.append("../../fluents/lib") # home of cx_stats
|
|
|
|
import dataset
|
|
|
|
import cx_stats
|
|
|
|
from plots_lpls import plot_corrloads
|
|
|
|
|
|
|
|
######## DATA ##########
|
2007-08-21 12:25:23 +02:00
|
|
|
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':
|
2007-09-20 18:10:40 +02:00
|
|
|
DX = dataset.read_ftsv(open("../../data/scherf/scherfX.ftsv"))
|
|
|
|
DY = dataset.read_ftsv(open("../../data/scherf/scherfY.ftsv"))
|
2007-08-21 12:25:23 +02:00
|
|
|
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"))
|
2007-09-20 18:10:40 +02:00
|
|
|
DYg = dataset.read_ftsv(open("../../data/uma/Yg133.ftsv"))
|
|
|
|
DY = dataset.read_ftsv(open("../../data/uma/Yd.ftsv"))
|
2007-08-21 12:25:23 +02:00
|
|
|
Y = DY.asarray().astype('d')
|
|
|
|
gene_ids = DX.get_identifiers('gene_ids', sorted=True)
|
|
|
|
|
2007-09-20 18:10:40 +02:00
|
|
|
# use subset with defined GO-terms
|
2007-07-28 18:05:11 +02:00
|
|
|
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)
|
2007-09-20 18:10:40 +02:00
|
|
|
|
2007-07-28 18:05:11 +02:00
|
|
|
# update genelist
|
|
|
|
gene_ids = gene2goterms.keys()
|
|
|
|
print "\nNumber of genes: %s" %len(gene_ids)
|
2007-08-21 12:25:23 +02:00
|
|
|
|
2007-09-20 18:10:40 +02:00
|
|
|
X = DX.asarray()
|
|
|
|
index = DX.get_indices('gene_ids', gene_ids)
|
|
|
|
X = X[:,index]
|
|
|
|
|
|
|
|
1/0
|
|
|
|
|
|
|
|
# Use only subset defined on GO
|
|
|
|
ontology = 'BP'
|
|
|
|
print "\n\nFiltering genes by Go terms "
|
|
|
|
|
2007-08-21 12:25:23 +02:00
|
|
|
# use subset based on SAM or IQR
|
2007-09-20 18:10:40 +02:00
|
|
|
subset = 'not'
|
2007-08-21 12:25:23 +02:00
|
|
|
if subset=='sam':
|
|
|
|
# select subset genes by SAM
|
|
|
|
rpy.r.library("siggenes")
|
|
|
|
rpy.r.library("qvalue")
|
2007-09-20 18:10:40 +02:00
|
|
|
rpy.r.assign("data", X.T)
|
|
|
|
cl = dot(DY.asarray(), diag(arange(Y.shape[1])+1)).sum(1)
|
2007-08-21 12:25:23 +02:00
|
|
|
rpy.r.assign("cl", cl)
|
|
|
|
rpy.r.assign("B", 20)
|
|
|
|
# Perform a SAM analysis.
|
|
|
|
print "Starting SAM"
|
|
|
|
sam = rpy.r('sam.out<-sam(data=data,cl=cl,B=B,rand=123)')
|
|
|
|
print "SAM done"
|
|
|
|
# Compute the q-values of the genes.
|
|
|
|
qq = rpy.r('qobj<-qvalue(sam.out@p.value)')
|
|
|
|
qvals = asarray(qq['qvalues'])
|
|
|
|
# cut off
|
2007-09-20 18:10:40 +02:00
|
|
|
cutoff = 0.01
|
2007-08-21 12:25:23 +02:00
|
|
|
index = where(qvals<cutoff)[0]
|
|
|
|
# Subset data
|
2007-09-20 18:10:40 +02:00
|
|
|
X = X[:,index]
|
|
|
|
|
|
|
|
gene_ids = [gid for i, gid in enumerate(gene_ids) if i in index]
|
2007-08-21 12:25:23 +02:00
|
|
|
print "\nWorking on subset with %s genes " %len(gene_ids)
|
2007-09-20 18:10:40 +02:00
|
|
|
|
|
|
|
# update valid 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)
|
2007-08-21 12:25:23 +02:00
|
|
|
else:
|
|
|
|
# noimp (smoker data is prefiltered)
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2007-07-20 11:36:26 +02:00
|
|
|
rpy.r.library("GOSim")
|
|
|
|
# Go-term similarity matrix
|
|
|
|
methods = ("JiangConrath","Resnik","Lin","CoutoEnriched","CoutoJiangConrath","CoutoResnik","CoutoLin")
|
2007-08-21 12:25:23 +02:00
|
|
|
meth = methods[2]
|
2007-07-20 11:36:26 +02:00
|
|
|
print "Term-term similarity matrix (method = %s)" %meth
|
2007-07-28 18:05:11 +02:00
|
|
|
|
2007-08-21 12:25:23 +02:00
|
|
|
print "\nCalculating term-term similarity matrix"
|
2007-07-28 18:05:11 +02:00
|
|
|
|
2007-08-21 12:25:23 +02:00
|
|
|
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)
|
2007-07-28 18:05:11 +02:00
|
|
|
|
|
|
|
# update data (X) matrix
|
|
|
|
newind = DX.get_indices('gene_ids', gene_ids)
|
2007-08-21 12:25:23 +02:00
|
|
|
Xr = DX.asarray()[:,newind]
|
2007-07-20 11:36:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
######## LPLSR ########
|
|
|
|
print "LPLSR ..."
|
2007-09-20 18:10:40 +02:00
|
|
|
a_max = 10
|
2007-08-02 13:19:16 +02:00
|
|
|
aopt = 3
|
2007-09-20 18:10:40 +02:00
|
|
|
xz_alpha = .6
|
2007-08-21 12:25:23 +02:00
|
|
|
w_alpha = .1
|
|
|
|
mean_ctr = [2, 0, 2]
|
|
|
|
|
|
|
|
# standardize Z?
|
|
|
|
sdtz = False
|
|
|
|
if sdtz:
|
|
|
|
Z = Z/Z.std(0)
|
|
|
|
|
2007-09-20 18:10:40 +02:00
|
|
|
T, W, P, Q, U, L, K, B, b0, evx, evy, evz,mnx,mny,mnz = nipals_lpls(Xr,Y,Z, a_max,
|
|
|
|
alpha=xz_alpha,
|
|
|
|
mean_ctr=mean_ctr)
|
2007-07-20 11:36:26 +02:00
|
|
|
|
|
|
|
# Correlation loadings
|
2007-07-23 15:25:34 +02:00
|
|
|
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)
|
2007-07-20 11:36:26 +02:00
|
|
|
# Prediction error
|
2007-08-21 12:25:23 +02:00
|
|
|
rmsep , yhat, class_error = cv_lpls(Xr, Y, Z, a_max, alpha=xz_alpha,mean_ctr=mean_ctr)
|
2007-09-20 18:10:40 +02:00
|
|
|
alpha_check=True
|
2007-07-24 16:34:47 +02:00
|
|
|
if alpha_check:
|
2007-09-20 18:10:40 +02:00
|
|
|
|
2007-07-24 16:34:47 +02:00
|
|
|
Alpha = arange(0.01, 1, .1)
|
|
|
|
Rmsep,Yhat, CE = [],[],[]
|
|
|
|
for a in Alpha:
|
2007-09-20 18:10:40 +02:00
|
|
|
print "alpha %f" %a
|
2007-08-21 12:25:23 +02:00
|
|
|
rmsep , yhat, ce = cv_lpls(Xr, Y, Z, a_max, alpha=xz_alpha,mean_ctr=mean_ctr)
|
2007-07-24 16:34:47 +02:00
|
|
|
Rmsep.append(rmsep)
|
|
|
|
Yhat.append(yhat)
|
2007-07-28 18:05:11 +02:00
|
|
|
CE.append(ce)
|
2007-07-24 16:34:47 +02:00
|
|
|
Rmsep = asarray(Rmsep)
|
|
|
|
Yhat = asarray(Yhat)
|
|
|
|
CE = asarray(CE)
|
2007-07-20 11:36:26 +02:00
|
|
|
|
2007-09-20 18:10:40 +02:00
|
|
|
|
2007-07-20 11:36:26 +02:00
|
|
|
# Significance Hotellings T
|
2007-09-20 18:10:40 +02:00
|
|
|
Wx, Wz, Wy, = jk_lpls(Xr, Y, Z, aopt, mean_ctr=mean_ctr,alpha=xz_alpha)
|
2007-07-23 15:25:34 +02:00
|
|
|
Ws = W*apply_along_axis(norm, 0, T)
|
2007-09-20 18:10:40 +02:00
|
|
|
tsqx = cx_stats.hotelling(Wx, Ws[:,:aopt], alpha=w_alpha)
|
|
|
|
tsqz = cx_stats.hotelling(Wz, L[:,:aopt], alpha=0)
|
2007-07-20 11:36:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
## plots ##
|
|
|
|
figure(1) #rmsep
|
2007-07-20 14:32:54 +02:00
|
|
|
bar_w = .2
|
|
|
|
bar_col = 'rgb'*5
|
|
|
|
m = Y.shape[1]
|
|
|
|
for a in range(m):
|
|
|
|
bar(arange(a_max)+a*bar_w+.1, rmsep[:,a], width=bar_w, color=bar_col[a])
|
2007-07-24 16:34:47 +02:00
|
|
|
ylim([rmsep.min()-.05, rmsep.max()+.05])
|
|
|
|
title('RMSEP')
|
2007-07-20 14:32:54 +02:00
|
|
|
|
2007-07-28 18:05:11 +02:00
|
|
|
figure(2)
|
|
|
|
for a in range(m):
|
|
|
|
bar(arange(a_max)+a*bar_w+.1, class_error[:,a], width=bar_w, color=bar_col[a])
|
|
|
|
ylim([class_error.min()-.05, class_error.max()+.05])
|
|
|
|
title('Classification accuracy')
|
|
|
|
|
|
|
|
figure(3) # Hypoid correlations
|
2007-08-21 12:25:23 +02:00
|
|
|
tsqz_s = 250*tsqz/tsqz.max()
|
2007-09-20 18:10:40 +02:00
|
|
|
plot_corrloads(Rz, pc1=0, pc2=1, s=tsqz_s, c=tsqz, zorder=5, expvar=evz, ax=None,alpha=.5)
|
2007-07-20 11:36:26 +02:00
|
|
|
ax = gca()
|
2007-09-20 18:10:40 +02:00
|
|
|
ylabels = DY.get_identifiers(DY.get_dim_name()[1], sorted=True)
|
|
|
|
plot_corrloads(Ry, pc1=0, pc2=1, s=150, c='g', zorder=5, expvar=evy, ax=ax,labels=ylabels,alpha=.5)
|
2007-07-20 11:36:26 +02:00
|
|
|
|
2007-09-20 18:10:40 +02:00
|
|
|
figure(4)
|
2007-07-20 11:36:26 +02:00
|
|
|
subplot(221)
|
|
|
|
ax = gca()
|
|
|
|
plot_corrloads(Rx, pc1=0, pc2=1, s=tsqx/2.0, c='b', zorder=5, expvar=evx, ax=ax)
|
|
|
|
# title('X correlation')
|
|
|
|
subplot(222)
|
|
|
|
ax = gca()
|
|
|
|
plot_corrloads(Ry, pc1=0, pc2=1, s=150, c='g', zorder=5, expvar=evy, ax=ax)
|
|
|
|
#title('Y correlation')
|
|
|
|
subplot(223)
|
|
|
|
ax = gca()
|
|
|
|
plot_corrloads(Rz, pc1=0, pc2=1, s=tsqz/10.0, c='r', zorder=5, expvar=evz, ax=ax)
|
|
|
|
#title('Z correlation')
|
|
|
|
subplot(224)
|
|
|
|
plot(arange(len(evx)), evx, 'b', label='X', linewidth=2)
|
|
|
|
plot(evy, 'g', label='Y', linewidth=2)
|
|
|
|
plot(evz, 'r', label='Z', linewidth=2)
|
|
|
|
legend(loc=2)
|
|
|
|
ylabel('Explained variance')
|
|
|
|
xlabel('Component')
|
|
|
|
show()
|