-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyper.R
210 lines (176 loc) · 6.29 KB
/
typer.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
################################################################################
#
# Copyright (C) Patrick Cahan 2007-2009
#
# Contact: pcahan@wustl.edu
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#################################################################################
# compute CNVr genotypes for supplied samples
# Determines the optimal clustering of CNV-region as determined by
# the silhouette and agreement between clustering and segment calls
# returns list of:
# (1) cnvr: cnvr coordinates, score, etc
# (2) genotype: clustering assigned labels
# (3) coding: i.e. -1, 0, 1 for losses, normal, gains, respectively
# (4) mean_sig: mean log2(ratio) of each sample in cnvr
# Each element can be accessed by usual R naming.So, x[['cnvr']], will return a table of CNVRs.
w_genotype<-function(snames,cnvBag,cnvrs,nclusts=2:7){
cnvrs<-cnvrs[order(cnvrs$chr, cnvrs$str),];
cnvrsOut<-data.frame();
genotypes<-data.frame();
coding<-data.frame();
mean_sigs<-data.frame();
chrs<-unique(as.vector(cnvrs$chr));
for(chr in chrs){
x<-genotypeChr(snames,cnvBag,cnvrs,chr,nclusts);
cnvrsOut<-rbind(cnvrsOut, x$cnvrs);
genotypes<-rbind(genotypes, x$genotypes);
coding<-rbind(coding, x$coding);
mean_sigs<-rbind(mean_sigs, x$mean_sigs);
}
list(cnvrs=cnvrsOut, genotypes=genotypes, coding=coding, mean_sigs=mean_sigs);
}
#### HELPER fucntions #####
# in: ngData, cnvs, nclusts
genotypeCNVR<-function(ngData,cnvr,cnvs,nclusts=2:7){
array_ids<-as.vector(rownames(ngData));
if(max(nclusts)> (length(array_ids)-1)){
nclusts<-2:(length(array_ids)-1);
}
aveWidths<-vector(length=length(nclusts));
goodnesses<-vector(length=length(nclusts));
genos<-list();
meanSigs<-list();
i<-1;
for(nclust in nclusts){
x<-pam(ngData, nclust);
labels<-data.frame(array_id=array_ids, label=x$clustering);
aveWidths[i]<-x$silinfo$avg.width;
meanSigs[[i]]<-getMeanSigs(ngData, labels);
refGrp<-detRefGroup(labels$label,meanSigs[[i]]);
genos[[i]]<-makeCNVrGeno(labels,cnvs,refGrp);
goodnesses[i]<-agreement(cnvs,genos[[i]],refGrp);
i<-i+1;
}
# select the best fit
best<-which.max(goodnesses*aveWidths);
mean_sigs<-meanSigs[[best]];
genotypes<-getGenotypes(mean_sigs, genos[[best]]$label, array_ids);
list(ave_width=aveWidths[[best]],
agreement=goodnesses[[best]],
nclusts=nclusts[best],
genotypes=genotypes);
}
# compute CNVr genotypes for supplied samples and chr
genotypeChr<-function(snames,cnvBag,cnvrs,chr,nclusts=2:7){
cnvrs<-cnvrs[cnvrs$chr==chr,];
genotypes<-data.frame();
coding<-data.frame();
mean_sigs<-data.frame();
cnvrsOut<-data.frame();
ng<-multiSample(snames,chr);
for( i in seq(nrow(cnvrs))){
cnvr<-cnvrs[i,];
x<-sliceNG(ng,cnvr);
cnvs<-cnvBag[[cnvr$cnvrid]];
genos<-genotypeCNVR(x,cnvr,cnvs,nclusts);
ave_width<-genos$ave_width;
cnvr<-cbind(cnvr, ave_width=ave_width);
cnvrsOut<-rbind(cnvrsOut,cnvr);
genotypes<-rbind(genotypes, data.frame(t(genos$genotypes$genos)));
coding<-rbind(coding, data.frame(t(genos$genotypes$coding)));
mean_sigs<-rbind(mean_sigs,data.frame(t(genos$genotypes$meanSigs)));
}
genotypes<-cbind(data.frame(cnvrid=cnvrs$cnvrid), genotypes);
colnames(genotypes)<-c('cnvrid',snames);
coding<-cbind(data.frame(cnvrid=cnvrs$cnvrid), coding);
colnames(coding)<-c('cnvrid',snames);
mean_sigs<-cbind(data.frame(cnvrid=cnvrs$cnvrid), mean_sigs);
colnames(mean_sigs)<-c('cnvrid',snames);
list(cnvrs=cnvrsOut, genotypes=genotypes, coding=coding, mean_sigs=mean_sigs);
}
sliceNG<-function(ng, loc){
thisNG<-ng[ng$position>=loc$str & ng$position<=loc$stp,];
x<-t(thisNG[,c(2:ncol(thisNG))]);
colnames(x)<-thisNG$position;
x;
}
# return the cluster label corresponding to the inds with the log2ratio closest to zero
# assume correct ordering
detRefGroup<-function(clusterLabels, meanSigs){
x<-which.min(abs(meanSigs));
clusterLabels[x];
}
agreement<-function(hcalls, labels, refGrp){
hcs<-hcalls$name;
x<-intersect(hcs, labels[labels$label==refGrp,]$array_id);
1-(length(x)/nrow(hcalls));
}
getMeanSigs<-function(ngData, labels){
means<-vector(length=nrow(labels));
for(i in seq(nrow(labels))){
means[i]<-mean(ngData[i,])
}
means;
}
# disallows the formation of de novo genotype groups
makeCNVrGeno<-function(labels,hCalls,refGrp){
grps<-unique(labels$label);
for(grp in grps){
if(grp!=refGrp){
wx<-which(labels$label==grp);
x<-labels[wx,];
ol<-length(intersect(x$array_id, hCalls$name));
if(ol==0){
labels[wx,]$label<-refGrp;
}
}
}
labels;
}
# assumes only one 'normal' genotype
getGenotypes<-function(meanSigs, genos, array_ids){
alleles<-unique(genos);
df<-data.frame(allele=alleles,
gMeans=vector(length=length(alleles)),
coding=vector(length=length(alleles)));
for(i in seq(length(alleles))){
df$gMeans[i]<-mean(meanSigs[which(genos==alleles[i])]);
}
# "normal"
norm<-which.min(abs(df$gMeans));
df$coding[norm]<-0;
losses<-df[setdiff(which(df$gMeans<0),norm),];
if(nrow(losses)>0){
vals<- -1*order(abs(losses$gMeans));
df[rownames(losses),]$coding<-vals;
}
gains<-df[setdiff(which(df$gMeans>0),norm),];
if(nrow(gains)>0){
vals<- order(abs(gains$gMeans));
df[rownames(gains),]$coding<-vals;
}
ans<-data.frame(array_id=array_ids,
meanSigs=meanSigs,
genos=genos,
coding=vector(length=length(array_ids)));
for(i in seq(nrow(ans))){
ans$coding[i]<-df[which(df$allele==genos[i]),]$coding;
}
ans;
}