[91a5d7]: / integration_MOFA.R

Download this file

234 lines (204 with data), 11.3 kB

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