-
Notifications
You must be signed in to change notification settings - Fork 0
/
realdata_Casecontrol.R
163 lines (135 loc) · 4.52 KB
/
realdata_Casecontrol.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
res <-readRDS("Case-Control/res.rds") # only CD4+
meta <-readRDS("Case-Control/meta.rds")
cd4_counts <- readRDS("Case-Control/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)
#### delete bad ptids
del = c(523845,525881,600929,616848,631338,631772,644891,646477,704809,719186,849938,104173,125948,132409,134551,210617,211497,217402,223868,233547,323142,330034,330272,339553)
#### case-control
orig <- read.csv("Case-Control/PTID_map_case_control.csv", header=T, sep=",")
rPTID = unique(orig[,2:3])
#rPTID = data.frame(rPTID)
colnames(rPTID) = c("PTID", "PTID_orig")
st = NULL; st_d= NULL;
for (ii in 1:length(del)) {
st <- c(st, which(del[ii]==rPTID[,2]))
st_d <- c(st_d, which(meta$PTID == rPTID[st[ii],1]))
}
res <- res[-st_d]
meta <- meta[-st_d,]
cd4_counts <- cd4_counts[-st_d]
#########################################################
#########################################################
sub_negctrl_800 = which(meta$Stim =="negctrl 1" & meta$VISITNO == 800) # unstim & VISITNO
sub_stim_800= which(meta$Stim=="92TH023 Env" & meta$VISITNO == 800) # stim & VISITNO
subsub_800 = intersect(meta$PTID[sub_negctrl_800],meta$PTID[sub_stim_800])
sub_s=NULL; sub_u=NULL;
for ( i in 1:length(subsub_800)) {
sub_s =c(sub_s,which(meta$PTID[sub_stim_800]==(subsub_800[i])))
sub_u = c(sub_u, which(meta$PTID[sub_negctrl_800]==(subsub_800[i])))
}
sub_s = sub_stim_800[sub_s]; #index for the stim and unstim sample
sub_u = sub_negctrl_800[sub_u];
#cd4
N_s = as.integer(cd4_counts[sub_s])
N_u = as.integer(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]
rm(sub_negctrl_800,subsub_800,sub_stim_800)
###############################################################
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(as.integer(0),dim=c(I,K));
n_u = array(as.integer(0),dim=c(I,K));
d = array(as.integer(0),dim=c(K,M))
count=1;
for (kk in 1:(M-1)) {
set = combs(1:M,kk);
ll = dim(combs(1:M,kk));
for ( lll in 1:ll[1]) {
d[(count-1+lll),set[lll,]] = as.integer(1);
}
#count = count+nChooseK(M,kk);
count = count+choose(M,kk);
}
d[count,]=as.integer(1)
d = cbind(d,as.integer(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] = as.integer(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] = as.integer(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] = as.integer(N_s[i]-sum(n_s[i,1:K1]))
n_u[i,K] = as.integer(N_u[i]-sum(n_u[i,1:K1]))
}
rm(ys,yu,count_s,count_u,count)
#######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)
rm(nu0,ns0,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)<=12);
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[,K] = N_s-as.integer(rowSums(n_s[,1:K1]))
n_u[,K] = N_u-as.integer(rowSums(n_u[,1:K1]))
#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)