-
Notifications
You must be signed in to change notification settings - Fork 0
/
realdata_rv144.R
150 lines (126 loc) · 4.17 KB
/
realdata_rv144.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
res <-readRDS("res.rds") # only CD4+
meta <-readRDS("meta.rds")
cd4_counts <- readRDS("cd4_counts.rds")
#names(rv144)
#rv144$pheno.name
#exprs(rv144$continuous.data[[1]])
#exprs(rv144$continuous.data[[1]])[,c(14,7,12,9,5,16)]
#rv144.continuous<-lapply(rv144$continuous.data,function(x)x[,c(14,7,12,9,5,15)])
#rv144.continuous<-lapply(rv144.continuous,exprs)
#########################################################
#########################################################
sub_negctrl_800 = which(meta$Stim =="negctrl 1" & meta$VISITNO == 800) # unstim & VISITNO
sub_negctrl_100 = which(meta$Stim =="negctrl 1" & meta$VISITNO == 100) # unstim & VISITNO
sub_stim_800= which(meta$Stim=="92TH023-ENV" & meta$VISITNO == 800) # stim & VISITNO
sub_stim_100 = which(meta$Stim=="92TH023-ENV" & meta$VISITNO == 100) # stim & VISITNO
subsub_800 = intersect(meta$PTID[sub_negctrl_800],meta$PTID[sub_stim_800])
subsub_100 = intersect(meta$PTID[sub_negctrl_100],meta$PTID[sub_stim_100])
sub_s=NULL; sub_u=NULL;
for ( i in 1:length(subsub_800)) {
sub_s =c(sub_s,which(meta$PTID[sub_stim_800]==as.numeric(subsub_800[i])))
sub_u = c(sub_u, which(meta$PTID[sub_negctrl_800]==as.numeric(subsub_800[i])))
}
sub_s = sub_stim_800[sub_s]; #index for the stim and unstim sample
sub_u = sub_negctrl_800[sub_u];
subs = NULL; subu= NULL;
for ( i in 1:length(subsub_100)) {
subs =c(subs,which(meta$PTID[sub_stim_100]==as.numeric(subsub_100[i])))
subu = c(subu, which(meta$PTID[sub_negctrl_100]==as.numeric(subsub_100[i])))
}
subs = sub_stim_100[subs]; #index for the stim and unstim sample
subu = sub_negctrl_100[subu];
sub_s = c(sub_s,subs);
sub_u = c(sub_u,subu);
#cd4
N_s = (cd4_counts[sub_s])
N_u = (cd4_counts[sub_u])
ys= vector(mode="list", length=length(sub_s));
yu =vector(mode="list", length=length(sub_u));
for (i in 1:length(sub_s)) {
ys[[i]] = res[[sub_s[i]]]
yu[[i]] = res[[sub_u[i]]]
}
placebo = meta$vaccine[sub_s]
###############################################################
library(caTools)
#library(R.basic)
I = length(yu);
M = dim(yu[[1]])[2]
K = 2^M;
K1=K-1;
y_u=yu; y_s = ys;
n_s = array(0,dim=c(I,K));
n_u = array(0,dim=c(I,K));
d = array(0,dim=c(K,M))
count=1;
for (kk in 1:M) {
set = combs(1:M,kk);
ll = dim(combs(1:M,kk));
for ( lll in 1:ll[1]) {
d[(count-1+lll),set[lll,]] = 1;
}
#count = count+nChooseK(M,kk);
count = count+choose(M,kk);
}
d = cbind(d,rowSums(d)); # last column the number of 1's
for (i in 1:I) {
count_s = 1; count_u = 1;
yui = 1*(yu[[i]]!=0)
ysi = 1*(ys[[i]]!=0)
for ( kk in 1:K1) {
if( length(yui)>0) {
sel = apply(yui,1,function(xrow)(identical(as.numeric(xrow),as.numeric(d[kk,1:M]))))
n_u[i,kk] = sum(sel)
if(n_u[i,kk]>0) {
y_u[[i]][count_u:(count_u+n_u[i,kk]-1),] = yu[[i]][sel,]
count_u = count_u+n_u[i,kk]
}
}
if(length(ysi)>0) {
sel = apply(ysi,1,function(xrow)(identical(as.numeric(xrow),as.numeric(d[kk,1:M]))))
n_s[i,kk] = sum(sel)
if(n_s[i,kk]>0) {
y_s[[i]][count_s:(count_s+n_s[i,kk]-1),] = ys[[i]][sel,]
count_s = count_s+n_s[i,kk]
}
}
}
n_s[i,K] = N_s[i]-sum(n_s[i,1:K1])
n_u[i,K] = N_u[i]-sum(n_u[i,1:K1])
}
#######take off zero columns #############
nu0 = which(colSums(n_u)==0)
ns0 = which(colSums(n_s)==0)
n0 = intersect(nu0,ns0)
d = d[-n0,]
n_s = n_s[,-n0]
n_u = n_u[,-n0]
K = K-length(n0)
#### additional delete #####
#del = c(7,17,21,22,23,24,26,29,34,35,37,38,39,40,42,43,46,47);
del = which(colSums(n_s)<=11);
for ( i in 1:I) {
cs = cumsum(n_s[i,]); cu = cumsum(n_u[i,]); posis = NULL; posiu = NULL;
for (j in del) {
if(n_s[i,j]>1) {
posis = c(posis, (cs[j-1]+1):cs[j])
} else if (n_s[i,j]==1) {
posis = c(posis,cs[j])
}
if(n_u[i,j]>1) {
posiu = c(posiu, (cu[j-1]+1):cu[j])
} else if (n_u[i,j] ==1) {
posiu = c(posiu,cu[j])
}
}
if (length(posis) >0) {y_s[[i]] = y_s[[i]][-posis,]}
if (length(posiu) >0) {y_u[[i]] = y_u[[i]][-posiu,]}
}
d = d[-del,]
n_s = n_s[,-del]
n_u = n_u[,-del]
K = dim(n_s)[2]
K1=K-1;
n_s=matrix(as.integer(n_s),nrow=I)
n_u=matrix(as.integer(n_u),nrow=I)
d=matrix(as.integer(d),nrow=K)