|
a |
|
b/integration_MOFA.R |
|
|
1 |
#################################################### |
|
|
2 |
### Script to perform MOFA ### |
|
|
3 |
#################################################### |
|
|
4 |
|
|
|
5 |
# get options |
|
|
6 |
library("optparse") |
|
|
7 |
|
|
|
8 |
option_list = list( |
|
|
9 |
make_option(c("-g", "--group_file"), type="character", default=NULL, help="file with sample groups [default= %default]", metavar="character"), |
|
|
10 |
make_option(c("-o", "--out"), type="character", default="MOFA_out", help="output directory name [default= %default]", metavar="character"), |
|
|
11 |
make_option(c("-s", "--suffix"), type="character", default="count.txt", help="Suffix for output names [default= %default]", metavar="character"), |
|
|
12 |
make_option(c("-R", "--expression"), type="character", default=NULL, help="Expression dataset [default= %default]", metavar="character"), |
|
|
13 |
make_option(c("-M", "--methylation"), type="character", default=NULL, help="Methylation dataset [default= %default]", metavar="character"), |
|
|
14 |
make_option(c("-S", "--mutation"), type="character", default=NULL, help="Mutations dataset [default= %default]", metavar="character"), |
|
|
15 |
make_option(c("-r", "--robustness"), action="store_true", default=FALSE, help="Perform robustness analysis [default= %default]"), |
|
|
16 |
make_option(c("-i", "--maxiter"), type="numeric", default=10000, help="Maximum number of iterations [default= %default]"), |
|
|
17 |
make_option(c("-n", "--nconsreps"), type="numeric", default=100, help="Number of iterations for consensus clustering [default= %default]"), |
|
|
18 |
make_option(c("-m", "--nreps"), type="numeric", default=10, help="Maximum number of iterations for Latent Factor robustness analysis [default= %default]"), |
|
|
19 |
make_option(c("-d", "--dropLFthres"), type="numeric", default=0, help="Threshold proportion of variance explained to drop latent factors [default= %default]"), |
|
|
20 |
make_option(c("-t", "--tol"), type="numeric", default=0.01, help="Tolerated delta ELBO to call convergence [default= %default]"), |
|
|
21 |
make_option(c("-F", "--nLF"), type="numeric", default=10, help="Number of latent factors [default= %default]"), |
|
|
22 |
make_option(c("-p", "--sparsity"), type="logical", action="store_false", default=TRUE, help="Sparsity of latent factors [default= %default]") |
|
|
23 |
); |
|
|
24 |
|
|
|
25 |
require(MOFAtools) |
|
|
26 |
## add something to specify class of group file columns |
|
|
27 |
|
|
|
28 |
opt_parser = OptionParser(option_list=option_list); |
|
|
29 |
opt = parse_args(opt_parser); |
|
|
30 |
|
|
|
31 |
# read data |
|
|
32 |
data = list() |
|
|
33 |
if(!(is.null(opt$mutation))) data$Mutation = read.table(opt$mutation,h=T,row.names = 1) |
|
|
34 |
if(!(is.null(opt$expression))) data$RNA = read.table(opt$expression,h=T,row.names = 1) |
|
|
35 |
if(!(is.null(opt$methylation))) data$Methyl = read.table(opt$methylation,h=T,row.names = 1) |
|
|
36 |
|
|
|
37 |
if(!is.null(opt$robustness) ){ |
|
|
38 |
print("Perform robustness analysis") |
|
|
39 |
nrep = as.numeric(opt$nconsreps) |
|
|
40 |
pItem = 0.8 |
|
|
41 |
for(i in 1:nrep){ |
|
|
42 |
print(i) |
|
|
43 |
print(c("outfile",paste(opt$out,"/MOFA",opt$suffix,"_sub",i,".hdf5",sep=""))) |
|
|
44 |
rm( MOFAobjecttmp ) |
|
|
45 |
samptmp = sort(unique( unlist(sapply(data,colnames)) )) |
|
|
46 |
nsamp = length(samptmp) |
|
|
47 |
samptmp = sort(sample(samptmp,size = round(pItem*nsamp),replace=F) ) |
|
|
48 |
data2 = lapply(data, function(x) x[,unlist(sapply(samptmp, function(xx) which(colnames(x)==xx) )) ] ) |
|
|
49 |
print(sapply(data2,dim)) |
|
|
50 |
V2 = lapply(data2, function(x) apply(x,1,var) ) |
|
|
51 |
|
|
|
52 |
zeroV2 = lapply(V2, function(x) which(x==0)) |
|
|
53 |
for(j in 1:length(data2)){ |
|
|
54 |
print(paste("Remove",length(zeroV2[[j]]),"samples with 0 variance in view",j) ) |
|
|
55 |
if( length(zeroV2[[j]])>0 ) data2[[j]] = data2[[j]][-zeroV2[[j]],] |
|
|
56 |
} |
|
|
57 |
print(sapply(data2,dim)) |
|
|
58 |
MOFAobjecttmp <- createMOFAobject(data2) |
|
|
59 |
ModelOptions <- getDefaultModelOptions(MOFAobjecttmp) |
|
|
60 |
ModelOptions$likelihood[names(ModelOptions$likelihood)=="Mutation"] = "bernoulli" |
|
|
61 |
ModelOptions$numFactors <- as.numeric(opt$nLF) |
|
|
62 |
ModelOptions$sparsity <- as.logical(opt$sparsity) |
|
|
63 |
TrainOptions <- getDefaultTrainOptions() |
|
|
64 |
TrainOptions$maxiter = as.numeric(opt$maxiter) |
|
|
65 |
TrainOptions$DropFactorThreshold = as.numeric(opt$dropLFthres) |
|
|
66 |
TrainOptions$tolerance = as.numeric(opt$tol) |
|
|
67 |
DataOptions <- getDefaultDataOptions() |
|
|
68 |
|
|
|
69 |
print(ModelOptions) |
|
|
70 |
print(TrainOptions) |
|
|
71 |
print(DataOptions) |
|
|
72 |
MOFAobjecttmp <- prepareMOFA(MOFAobjecttmp, ModelOptions = ModelOptions,TrainOptions = TrainOptions,DataOptions = DataOptions) |
|
|
73 |
|
|
|
74 |
# run |
|
|
75 |
MOFAobjecttmp <- runMOFA(MOFAobjecttmp, paste(opt$out,"/MOFA",opt$suffix,"_sub",i,".hdf5",sep="") ) |
|
|
76 |
} |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
#perform MOFA run |
|
|
80 |
print("Perform multiple runs") |
|
|
81 |
MOFAfinal=c() |
|
|
82 |
bestELBO = -Inf |
|
|
83 |
bestmodel = 1 |
|
|
84 |
nrep = as.numeric(opt$nreps) |
|
|
85 |
for(i in 1:nrep){ |
|
|
86 |
print(i) |
|
|
87 |
print(c("outfile",paste(opt$out,"/MOFA",opt$suffix,"_run",i,".hdf5",sep="")) ) |
|
|
88 |
MOFAobjecttmp <- createMOFAobject(data) |
|
|
89 |
ModelOptions <- getDefaultModelOptions(MOFAobjecttmp) |
|
|
90 |
ModelOptions$likelihood[names(ModelOptions$likelihood)=="Mutation"] = "bernoulli" |
|
|
91 |
ModelOptions$numFactors <- as.numeric(opt$nLF) |
|
|
92 |
ModelOptions$sparsity <- as.logical(opt$sparsity) |
|
|
93 |
TrainOptions <- getDefaultTrainOptions() |
|
|
94 |
TrainOptions$maxiter = as.numeric(opt$maxiter) |
|
|
95 |
TrainOptions$DropFactorThreshold = as.numeric(opt$dropLFthres) |
|
|
96 |
TrainOptions$tolerance = as.numeric(opt$tol) |
|
|
97 |
DataOptions <- getDefaultDataOptions() |
|
|
98 |
print(ModelOptions) |
|
|
99 |
print(TrainOptions) |
|
|
100 |
print(DataOptions) |
|
|
101 |
MOFAobjecttmp <- prepareMOFA(MOFAobjecttmp, ModelOptions = ModelOptions,TrainOptions = TrainOptions,DataOptions = DataOptions) |
|
|
102 |
|
|
|
103 |
# run |
|
|
104 |
MOFAobjecttmp <- runMOFA(MOFAobjecttmp, paste(opt$out,"/MOFA",opt$suffix,"_run",i,".hdf5",sep="") ) |
|
|
105 |
|
|
|
106 |
if( rev(MOFAobjecttmp@TrainStats$elbo)[1]> bestELBO){ |
|
|
107 |
bestELBO = rev(MOFAobjecttmp@TrainStats$elbo)[1] |
|
|
108 |
MOFAfinal = MOFAobjecttmp |
|
|
109 |
bestmodel = i |
|
|
110 |
} |
|
|
111 |
} |
|
|
112 |
MOFAobject = MOFAfinal |
|
|
113 |
print(paste("Best model:",bestmodel)) |
|
|
114 |
|
|
|
115 |
# convergence plot |
|
|
116 |
pdf(paste(opt$out,"/Convergence_ELBO",opt$suffix,".pdf",sep=""),h=4,w=4*2) |
|
|
117 |
par(mfrow=c(1,1),las=1,family="Times") |
|
|
118 |
plot( diff(MOFAobject@TrainStats$elbo),type="l",xlab="Iteration",ylab=expression(Delta~"ELBO"),main="",log="y") |
|
|
119 |
abline(h=0,lty=2) |
|
|
120 |
dev.off() |
|
|
121 |
|
|
|
122 |
# analyses |
|
|
123 |
# correlate other variables |
|
|
124 |
if(!is.null(opt$group_file)){ |
|
|
125 |
meta = read.table(opt$group_file,h=T,sep="\t") |
|
|
126 |
|
|
|
127 |
idsurv = grep("surv", colnames(meta) ,ignore.case = T) |
|
|
128 |
if( length(idsurv)>0){ |
|
|
129 |
surv = meta[,idsurv] |
|
|
130 |
meta = meta[,-idsurv] |
|
|
131 |
issurv = TRUE |
|
|
132 |
}else{ |
|
|
133 |
issurv = FALSE |
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
MOFAfactors.nona = MOFAfactors[ rowSums( is.na( meta ) )==0 ,] |
|
|
137 |
|
|
|
138 |
lml = lapply(1:3, function(i) lm( as.formula(paste("MOFAfactors.nona[,",i+1,"]~", paste(colnames(meta) ,collapse = "+"),"+ Sex:Type + Sex:Smoking_Hx + Age:Smoking_Hx + Smoking_Hx:Type") ),data = na.omit(meta) ) ) |
|
|
139 |
suml = lapply(lml,summary) |
|
|
140 |
anl = lapply(lml,anova) |
|
|
141 |
|
|
|
142 |
require(MASS) |
|
|
143 |
#lml.dropped = vector("list",3) |
|
|
144 |
#for(i in 1:3){ |
|
|
145 |
# lmtmp = lml[[i]] |
|
|
146 |
# metatmp = meta |
|
|
147 |
# while(1){ |
|
|
148 |
# dttmp = dropterm(lmtmp) |
|
|
149 |
# if(max(dttmp$AIC,na.rm = T)< dttmp$AIC[1] ) ttmp = rownames(dttmp)[which.min(dttmp$AIC)] |
|
|
150 |
# else break |
|
|
151 |
#if(ttmp=="<none>") break |
|
|
152 |
# metatmp = metatmp[,!(colnames(metatmp)==ttmp)] |
|
|
153 |
# lmtmp = lm( as.formula(paste("MOFAfactors[,",i+1,"]~", paste(colnames(metatmp),collapse = "+")) ),data = metatmp ) |
|
|
154 |
#lmtmp2 = update(lmtmp, paste(".~. - ",ttmp)) |
|
|
155 |
#if( anova(lmtmp,lmtmp2)$`Pr(>F)`[2] >0.05/3) lmtmp = lmtmp2 |
|
|
156 |
# print(dttmp) |
|
|
157 |
# #print(lmtmp) |
|
|
158 |
# } |
|
|
159 |
# lml.dropped[[i]] = lmtmp |
|
|
160 |
#} |
|
|
161 |
#anl.dropped = lapply(lml.dropped,anova,) |
|
|
162 |
lml.dropped = lapply(1:3, function(i) stepAIC(lml[[i]], direction = "both",test="F") ) |
|
|
163 |
lml.final = lapply(1:3, function(i) lm( as.formula(paste("MOFAfactors[,",i+1,"]~", as.character(lml.dropped[[i]]$call$formula)[3] ) ) ,data = meta) ) |
|
|
164 |
for(i in 1:3) write.table( anl[[i]],file = paste(opt$out,"/Anova",opt$suffix,"_LF",i,".txt",sep=""),quote = F) |
|
|
165 |
anl.final = lapply(lml.final,anova) |
|
|
166 |
|
|
|
167 |
lsignif = lapply(1:3, function(i) rownames(anl.final[[i]])[which( anl.final[[i]]$`Pr(>F)`<0.05/3 )] ) |
|
|
168 |
#lsignifall = unique( unlist(strsplit(unlist( lsignif ),":")) ) |
|
|
169 |
lsignifall = unique( unlist(lsignif) ) |
|
|
170 |
|
|
|
171 |
meta2 = cbind(meta, "Type:Sex" = interaction(meta$Type,meta$Sex), "Sex:Smoking_Hx" = interaction(meta$Sex,meta$Smoking_Hx) , "Age:Smoking_Hx" = interaction(meta$Age,meta$Smoking_Hx) ) |
|
|
172 |
|
|
|
173 |
svg(paste(opt$out,"/Pie",opt$suffix,"_signif.svg",sep=""),h=3*length(lsignifall),w=3*K) |
|
|
174 |
par(mfrow=c(length(lsignifall),K),family="Times",las=1) |
|
|
175 |
for(j in 1:length(lsignifall)){ |
|
|
176 |
for(i in 1:K) pie( table(factor(meta2[,lsignifall[j]])[kmb$cluster==i] ) ,radius = sqrt(sum(kmb$cluster==i)/70), init.angle = 90,col= rainbow(length(levels(factor(meta2[,lsignifall[j]])))) ) |
|
|
177 |
} |
|
|
178 |
dev.off() |
|
|
179 |
|
|
|
180 |
fisher.test(table(meta2[,lsignifall[j]],kmb$cluster )) |
|
|
181 |
|
|
|
182 |
survS <- Surv( as.numeric(surv[,1]), as.numeric(factor(surv[,2],levels=c("alive","dead") ))-1) |
|
|
183 |
fitS <- survfit(survS ~ MOFAfactors[,2] + MOFAfactors[,3] ) |
|
|
184 |
fitS.clust <- survfit(survS ~ factor(kmb$cluster) ) |
|
|
185 |
fitS.Type <- survfit(survS ~ factor(meta$Type) ) |
|
|
186 |
coxS <- coxph(survS ~ MOFAfactors[,2] + MOFAfactors[,3]) |
|
|
187 |
coxS.clust <- coxph(survS ~ factor(kmb$cluster) ) |
|
|
188 |
coxS.Type <- coxph(survS ~ factor(meta$Type) ) |
|
|
189 |
summary(coxS) |
|
|
190 |
summary(coxS.clust) |
|
|
191 |
summary(coxS.Type) |
|
|
192 |
extractAIC(coxS) # better |
|
|
193 |
extractAIC(coxS.clust) |
|
|
194 |
extractAIC(coxS.Type) |
|
|
195 |
#summary(fitCarc.clust2)$table |
|
|
196 |
|
|
|
197 |
svg(paste(opt$out,"/Surv",opt$suffix,".svg",sep=""),h=4,w=4) |
|
|
198 |
par(mfrow=c(1,1),las=1,family="Times") |
|
|
199 |
plot(fitS.clust,col=1:4) |
|
|
200 |
legend("bottomright",legend = levels(as.factor(kmb$cluster)),col=1:3 , lty=1) |
|
|
201 |
legend("bottomleft",legend = paste("logrank P =", format(summary(coxS.clust)$logtest[3],digits = 3,scientific = T)) ) |
|
|
202 |
#plot(fitCarc.Type,col=2:1) |
|
|
203 |
#legend("bottomright",legend = levels(as.factor(TypeCarc)),col=2:1 , lty=1) |
|
|
204 |
#legend("bottomleft",legend = paste("logrank P =", format(summary(coxCarc.Type)$logtest[3],digits = 3,scientific = T)) ) |
|
|
205 |
dev.off() |
|
|
206 |
|
|
|
207 |
svg(paste(opt$out,"/Surv",opt$suffix,"_Type.svg",sep=""),h=4,w=4) |
|
|
208 |
par(mfrow=c(1,1),las=1,family="Times") |
|
|
209 |
plot(fitS.Type,col=1:4) |
|
|
210 |
legend("bottomright",legend = levels(as.factor(kmb$cluster)),col=1:3 , lty=1) |
|
|
211 |
legend("bottomleft",legend = paste("logrank P =", format(summary(coxS.Type)$logtest[3],digits = 3,scientific = T)) ) |
|
|
212 |
#plot(fitCarc.Type,col=2:1) |
|
|
213 |
#legend("bottomright",legend = levels(as.factor(TypeCarc)),col=2:1 , lty=1) |
|
|
214 |
#legend("bottomleft",legend = paste("logrank P =", format(summary(coxCarc.Type)$logtest[3],digits = 3,scientific = T)) ) |
|
|
215 |
dev.off() |
|
|
216 |
|
|
|
217 |
if(sum(colnames(meta)=="Type")>0){ |
|
|
218 |
svg( paste(opt$out,"/ScatterPlot",opt$suffix,"_Type.svg",sep=""),h=4,w=4) |
|
|
219 |
par(mfrow=c(1,1),family="Times",las=1) |
|
|
220 |
plot(MOFAfactors[,2:3], xlab = "Latent Factor 1", ylab="Latent Factor 2", col = prettycolors[c(2,3,1,6)][addNA(as.factor(meta$Type))], pch=16 ) |
|
|
221 |
legend("topleft",legend = levels(addNA(as.factor(meta$Type))) , col = prettycolors[c(2,3,1,6)], pch=16 ) |
|
|
222 |
dev.off() |
|
|
223 |
} |
|
|
224 |
|
|
|
225 |
svg(paste(opt$out,"Ki67_AC-TC-LCNEC.svg",sep=""),h=4,w=4*2) |
|
|
226 |
par(mfrow=c(1,2),las=1,family="Times") |
|
|
227 |
boxplot( MOFAobject@TrainData$RNA[which( rownames( MOFAobject@TrainData$RNA )=="MKI67" ),] ~ meta$Type ) |
|
|
228 |
boxplot( MOFAobject@TrainData$RNA[which( rownames( MOFAobject@TrainData$RNA )=="MKI67" ),] ~ kmb$cluster ) |
|
|
229 |
dev.off() |
|
|
230 |
|
|
|
231 |
} |
|
|
232 |
|
|
|
233 |
save(MOFAobject,MOFAfactors, file = paste(opt$out,"/integration_MOFA",opt$suffix,".RData",sep="") ) |