forked from riasatazim/CDSImpute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.R
291 lines (235 loc) · 6.15 KB
/
Library.R
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#Please Update The File path on Data Load Function
LoadUtil <- function() {
library(R.matlab)
library("dplyr")
library(ggplot2)
#library(devtools)
library(RColorBrewer)
library(cluster)
library(pvclust)
library(xtable)
library(limma)
library(plyr)
library(ggplot2)
library(lattice)
library(RColorBrewer)
library(scales)
library(factoextra)
library(gridExtra)
library('proxy')
library("energy")
library(RColorBrewer)
library(cluster)
library(pvclust)
library(FactoMineR)
library(xtable)
library(limma)
library(plyr)
library(ggplot2)
library(car)
library(lattice)
library(RColorBrewer)
library(scales)
library(factoextra)
library(gridExtra)
library(NMF)
library(apcluster)
library(R.matlab)
library(fpc)
require(stats)
library(igraph)
library("corrplot")
library("madness")
library("factoextra")
library("Rtsne")
library(rmatio)
library(umap)
library("fossil")
library("SummarizedExperiment")
}
LoadPollenDataSet <- function() {
file="D:/Codes/DropOut/data/Pollen.mat"
dataset=read.mat(file)
data=dataset$data
genes=dataset$genes
classes =dataset$labels
colnames(data)<-classes
rownames(data)<-genes
return (data)
}
LoadKolodDataSet <- function() {
file="D:/Codes/DropOut/data/Kolod.mat"
#704*10685
dataset=read.mat(file)
data=t(dataset$in_X)
genes=as.character(dataset$Genes)
classes =dataset$true_labs
colnames(data)<-classes
rownames(data)<-genes
return (data)
}
LoadUsoskinDataSet <- function() {
file="D:/Codes/DropOut/data/Usoskin.mat"
dataset=read.mat(file)
data=dataset$in_X
classes =dataset$true_labs
data<-t(data)
colnames(data)<-classes
return (data)
}
LoadKolodziejczykDataSet <- function() {
file="D:/Codes/DropOut/data/Kolodziejczyk.mat"
dataset=read.mat(file)
data=dataset$data
classes =dataset$labels
gene=dataset$genes
data<-as.data.frame(data)
colnames(data)<-unname(classes)
rownames(data)<-unname(gene)
# nrow(data)
#ncol(data)
#length(classes)
#length(gene)
#View(data[1:10,1:10])
return (data)
}
LoadTest_2_Kolod<- function() {
load("D:/Codes/DropOut/data/Test_2_Kolod.RData")
dataset<-Test_2_Kolod
data=dataset$in_X
classes =(dataset$true_labs)
classes<-unlist(classes)
#data<-t(data)
colnames(data)<-classes
return (data)
}
SCC_filtration <- function(data,celltype)
{
pca<-prcomp(t(data))
pcadata<-pca$x
dis<-matrix(nrow=ncol(data),ncol=2)
dis[,1]<-pcadata[,1]
dis[,2]<-pcadata[,2]
neardis<-dist(dis,p=2)
dismatrix<-matrix(nrow=ncol(data),ncol=ncol(data))
k<-1
for (i in 1:(ncol(data)-1))
{
for (j in (i+1):ncol(data))
{
dismatrix[j,i]<-neardis[k]
dismatrix[i,j]<-neardis[k]
k<-k+1
}
}
for (i in 1:ncol(data)) dismatrix[i,i]<-1000000
nearnode<-rep(0,ncol(data))
nearnodedis<-rep(1000000,ncol(data))
for (i in 1:ncol(data))
{
for (j in 1:ncol(data))
{
if (dismatrix[i,j]<nearnodedis[i])
{
nearnode[i]<-j
nearnodedis[i]<-dismatrix[i,j]
}
}
}
sortdis<-sort(nearnodedis)
q1<-floor(length(sortdis)/4)
q3<-length(sortdis)-q1
maxdis<-sortdis[q3]+1.5*(sortdis[q3]-sortdis[q1])
removedCells<-array()
remove=0
for (i in 1:ncol(data))
{
if (nearnodedis[i]>maxdis)
{
data<-data[,-i]
celltype<-celltype[-i]
removedCells[remove]<-i
remove=remove+1
}
}
# result<-list(data,celltype)
# result$data<-data
# result$celltype<-celltype
return (removedCells)
}
#load the rda file#
#load(file = "D:/Codes/DropOut/data/usoskin.rda")
scSimulator <- function(N=3, nDG=150, nMK=10, nNDG=8180, k=50,
seed=17,logmean=5.25,logsd=1, v=9.2){
set.seed(seed)
scmdSimulator <- function(N,logmean,logsd,nDG){
trD <- array(0,dim=c(nDG,N))
for (i in 1:N){
trD[,i] <- rlnorm(nDG,meanlog = logmean,sdlog= logsd) -1
}
return(trD)
}
## Simulating Differentially Expressed Genes
DG <- scmdSimulator(N,logmean,logsd,nDG)
## Simulating Marker Genes
mkSimulator <- function(N,logmean,logsd,nMK){
y <- array(0,dim=c(nMK*N,N))
for (i in 1:N){
y[(i*nMK-nMK+1):(i*nMK) ,i] <- rlnorm(nMK,meanlog = logmean,sdlog = logsd) -1
}
return(y)
}
MK <- mkSimulator(N,logmean,logsd,nMK)
DG <- rbind(DG,MK)
## Simulating the non-differentially expressed genes
NDG0 <- rlnorm(nNDG,meanlog = logmean,sdlog=logsd) -1
NDG <- array(NA,dim=c(nNDG,N))
for(i in 1:N){
NDG[,i] <- NDG0
}
## Expected Values Matrix
EM <- rbind(DG,NDG)
## Simulating noise and drop-outs
design <- rep(k,N)
u <-0.7
Simulator <- function(EM,design){
a<-list() ## annotation
b<-list() ## expression matrix - expected values
c<-list() ## expression matrix - dropouts simulated
d<-list() ## tags matrix
TZ <- nrow(EM)
for (i in 1:ncol(EM)){
a[[i]]<- array(0,dim=c(TZ,design[i]))
b[[i]]<- array(NA,dim=c(TZ,design[i]))
c[[i]]<- array(NA,dim=c(TZ,design[i]))
d[[i]]<- array(NA,dim=c(TZ,design[i]))
a[[i]][EM[,i]>0,] <- 1
## probability function
pi <- 1/(1 + exp(u * (log2(EM[,i]+1) - v)))
for (j in 1:design[i]){
b[[i]][,j] <- EM[,i]
c[[i]][,j] <- EM[,i]
for (k in 1:TZ){
if(rbinom(1,1,pi[k])){
c[[i]][k,j] <- max(rpois(1,1)-1,0)
a[[i]][k,j] <- 2 * a[[i]][k,j]
}
}
## simulating noise
nj<-rpois(TZ,lambda=round(c[[i]][,j]))*(a[[i]][,j]==1)
nj <- nj+c[[i]][,j]*(a[[i]][,j]==2)
nj[nj<0]<-0
d[[i]][,j]<-nj
}
}
A <- do.call(cbind,a)
B <- do.call(cbind,b)
C <- do.call(cbind,c)
D <- do.call(cbind,d)
G <-list(A,B,C,D)
names(G) <-c("annotation","expectedValues","dropoutsSimulated","tags")
return(G)
}
sData <- Simulator(EM,design)
return(sData)
}