0
|
1 #options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
|
|
2
|
|
3 args <- commandArgs(trailingOnly = TRUE)
|
|
4
|
|
5 inFile = args[1]
|
|
6 outFile = args[2]
|
|
7 outDir = args[3]
|
|
8 clonalType = args[4]
|
|
9
|
|
10 if (!("gridExtra" %in% rownames(installed.packages()))) {
|
|
11 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
|
|
12 }
|
|
13 library(gridExtra)
|
|
14 if (!("ggplot2" %in% rownames(installed.packages()))) {
|
|
15 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
|
|
16 }
|
|
17 require(ggplot2)
|
|
18 if (!("plyr" %in% rownames(installed.packages()))) {
|
|
19 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
|
|
20 }
|
|
21 require(plyr)
|
|
22
|
|
23 if (!("data.table" %in% rownames(installed.packages()))) {
|
|
24 install.packages("data.table", repos="http://cran.xl-mirror.nl/")
|
|
25 }
|
|
26 library(data.table)
|
|
27
|
2
|
28 if (!("reshape2" %in% rownames(installed.packages()))) {
|
|
29 install.packages("reshape2", repos="http://cran.xl-mirror.nl/")
|
|
30 }
|
|
31 library(reshape2)
|
|
32
|
0
|
33
|
|
34 test = read.table(inFile, sep="\t", header=TRUE, fill=T, comment.char="")
|
|
35
|
|
36 test = test[test$Sample != "",]
|
|
37
|
2
|
38
|
|
39
|
0
|
40 test$Top.V.Gene = gsub("[*]([0-9]+)", "", test$Top.V.Gene)
|
|
41 test$Top.D.Gene = gsub("[*]([0-9]+)", "", test$Top.D.Gene)
|
|
42 test$Top.J.Gene = gsub("[*]([0-9]+)", "", test$Top.J.Gene)
|
|
43
|
|
44 #test$VDJCDR3 = do.call(paste, c(test[c("Top.V.Gene", "Top.D.Gene", "Top.J.Gene","CDR3.Seq.DNA")], sep = ":"))
|
|
45 test$VDJCDR3 = do.call(paste, c(test[unlist(strsplit(clonalType, ","))], sep = ":"))
|
|
46
|
|
47 PROD = test[test$VDJ.Frame != "In-frame with stop codon" & test$VDJ.Frame != "Out-of-frame" & test$CDR3.Found.How != "NOT_FOUND" , ]
|
|
48 if("Functionality" %in% colnames(test)) {
|
|
49 PROD = test[test$Functionality == "productive" | test$Functionality == "productive (see comment)", ]
|
|
50 }
|
|
51
|
|
52 NONPROD = test[test$VDJ.Frame == "In-frame with stop codon" | test$VDJ.Frame == "Out-of-frame" | test$CDR3.Found.How == "NOT_FOUND" , ]
|
|
53
|
|
54 #PRODF = PROD[ -1]
|
|
55
|
|
56 PRODF = PROD
|
|
57
|
|
58 #PRODF = unique(PRODF)
|
2
|
59 if(any(grepl(pattern="_", x=PRODF$ID))){
|
0
|
60
|
2
|
61 PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
|
|
62 PRODF$freq = gsub("_.*", "", PRODF$freq)
|
|
63 PRODF$freq = as.numeric(PRODF$freq)
|
|
64 } else {
|
|
65 PRODF$freq = 1
|
|
66 PRODF = PRODF[!duplicated(PRODF$VDJCDR3), ]
|
|
67 }
|
|
68
|
|
69 PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
|
0
|
70 PRODFV$Length = as.numeric(PRODFV$Length)
|
|
71 Total = 0
|
|
72 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
73 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
74 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
75
|
2
|
76 PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
|
0
|
77 PRODFD$Length = as.numeric(PRODFD$Length)
|
|
78 Total = 0
|
|
79 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
80 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
81 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
82
|
2
|
83 PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
|
0
|
84 PRODFJ$Length = as.numeric(PRODFJ$Length)
|
|
85 Total = 0
|
|
86 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
87 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
88 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
89
|
|
90 V = c("v.name\tchr.orderV\nTRBV1\t1\nTRBV2\t2\nTRBV3\t3\nTRBV4\t4\nTRBV5\t5\nTRBV12-1\t6\nTRBV13-1\t7\nTRBV12-2\t8\nTRBV13-2\t9\nTRBV13-3\t10\nTRBV14\t11\nTRBV15\t12\nTRBV16\t13\nTRBV17\t14\nTRBV19\t15\nTRBV20\t16\nTRBV23\t17\nTRBV24\t18\nTRBV26\t19\nTRBV29\t20\nTRBV30\t21\nTRBV31\t22\n")
|
|
91 tcV = textConnection(V)
|
|
92 Vchain = read.table(tcV, sep="\t", header=TRUE)
|
|
93 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
|
|
94 close(tcV)
|
|
95
|
|
96 D = c("v.name\tchr.orderD\nTRBD1\t1\nTRBD2\t2\n")
|
|
97 tcD = textConnection(D)
|
|
98 Dchain = read.table(tcD, sep="\t", header=TRUE)
|
|
99 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
|
|
100 close(tcD)
|
|
101
|
|
102
|
|
103 J = c("v.name\tchr.orderJ\nTRBJ1-1\t1\nTRBJ1-2\t2\nTRBJ1-3\t3\nTRBJ1-4\t4\nTRBJ1-5\t5\nTRBJ2-1\t6\nTRBJ2-2\t7\nTRBJ2-3\t8\nTRBJ2-4\t9\nTRBJ2-5\t10\nTRBJ2-6\t11\nTRBJ2-7\t12\n")
|
|
104 tcJ = textConnection(J)
|
|
105 Jchain = read.table(tcJ, sep="\t", header=TRUE)
|
|
106 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
|
|
107 close(tcJ)
|
|
108
|
|
109 setwd(outDir)
|
|
110
|
2
|
111 write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
|
0
|
112
|
|
113 pV = ggplot(PRODFV)
|
|
114 pV = pV + geom_bar( aes( x=factor(reorder(Top.V.Gene, chr.orderV)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
|
|
115 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
|
2
|
116 write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
0
|
117
|
|
118 png("VPlot.png",width = 1280, height = 720)
|
|
119 pV
|
|
120 dev.off();
|
|
121
|
|
122 pD = ggplot(PRODFD)
|
|
123 pD = pD + geom_bar( aes( x=factor(reorder(Top.D.Gene, chr.orderD)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
|
|
124 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
|
2
|
125 write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
0
|
126
|
|
127 png("DPlot.png",width = 800, height = 600)
|
|
128 pD
|
|
129 dev.off();
|
|
130
|
|
131 pJ = ggplot(PRODFJ)
|
|
132 pJ = pJ + geom_bar( aes( x=factor(reorder(Top.J.Gene, chr.orderJ)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
|
|
133 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
|
2
|
134 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
0
|
135
|
|
136 png("JPlot.png",width = 800, height = 600)
|
|
137 pJ
|
|
138 dev.off();
|
|
139
|
2
|
140 VGenes = PRODF[,c("Sample", "Top.V.Gene", "freq")]
|
|
141 VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
|
|
142 VGenes = data.frame(data.table(VGenes)[, list(Count=sum(freq)), by=c("Sample", "Top.V.Gene")])
|
|
143 TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
144 VGenes = merge(VGenes, TotalPerSample, by="Sample")
|
|
145 VGenes$Frequency = VGenes$Count * 100 / VGenes$total
|
|
146 VPlot = ggplot(VGenes)
|
|
147 VPlot = VPlot + geom_bar(aes( x = Top.V.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
148 ggtitle("Distribution of V gene families") +
|
|
149 ylab("Percentage of sequences")
|
|
150 png("VFPlot.png")
|
|
151 VPlot
|
|
152 dev.off();
|
|
153 write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
154
|
|
155 DGenes = PRODF[,c("Sample", "Top.D.Gene", "freq")]
|
|
156 DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
|
|
157 DGenes = data.frame(data.table(DGenes)[, list(Count=sum(freq)), by=c("Sample", "Top.D.Gene")])
|
|
158 TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
159 DGenes = merge(DGenes, TotalPerSample, by="Sample")
|
|
160 DGenes$Frequency = DGenes$Count * 100 / DGenes$total
|
|
161 DPlot = ggplot(DGenes)
|
|
162 DPlot = DPlot + geom_bar(aes( x = Top.D.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
163 ggtitle("Distribution of D gene families") +
|
|
164 ylab("Percentage of sequences")
|
|
165 png("DFPlot.png")
|
|
166 DPlot
|
|
167 dev.off();
|
|
168 write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
169
|
|
170 JGenes = PRODF[,c("Sample", "Top.J.Gene", "freq")]
|
|
171 JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
|
|
172 JGenes = data.frame(data.table(JGenes)[, list(Count=sum(freq)), by=c("Sample", "Top.J.Gene")])
|
|
173 TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
174 JGenes = merge(JGenes, TotalPerSample, by="Sample")
|
|
175 JGenes$Frequency = JGenes$Count * 100 / JGenes$total
|
|
176 JPlot = ggplot(JGenes)
|
|
177 JPlot = JPlot + geom_bar(aes( x = Top.J.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
178 ggtitle("Distribution of J gene families") +
|
|
179 ylab("Percentage of sequences")
|
|
180 png("JFPlot.png")
|
|
181 JPlot
|
|
182 dev.off();
|
|
183 write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
184
|
|
185 CDR3Length = data.frame(data.table(PRODF)[, list(Count=sum(freq)), by=c("Sample", "CDR3.Length.DNA")])
|
|
186 TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
|
|
187 CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
|
|
188 CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
|
|
189 CDR3LengthPlot = ggplot(CDR3Length)
|
|
190 CDR3LengthPlot = CDR3LengthPlot + geom_bar(aes( x = CDR3.Length.DNA, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
191 ggtitle("Length distribution of CDR3") +
|
|
192 xlab("CDR3 Length") +
|
|
193 ylab("Percentage of sequences")
|
|
194 png("CDR3LengthPlot.png",width = 1280, height = 720)
|
|
195 CDR3LengthPlot
|
|
196 dev.off()
|
|
197 write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
198
|
0
|
199 revVchain = Vchain
|
|
200 revDchain = Dchain
|
|
201 revVchain$chr.orderV = rev(revVchain$chr.orderV)
|
|
202 revDchain$chr.orderD = rev(revDchain$chr.orderD)
|
|
203
|
|
204 plotVD <- function(dat){
|
|
205 if(length(dat[,1]) == 0){
|
|
206 return()
|
|
207 }
|
|
208 img = ggplot() +
|
|
209 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
|
210 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
211 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
212 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
213 xlab("D genes") +
|
|
214 ylab("V Genes")
|
|
215
|
|
216 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
217 print(img)
|
2
|
218
|
0
|
219 dev.off()
|
2
|
220 write.table(x=acast(dat, Top.V.Gene~Top.D.Gene, value.var="Length"), file=paste("HeatmapVD_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
|
0
|
221 }
|
|
222
|
2
|
223 VandDCount = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
|
0
|
224
|
|
225 VandDCount$l = log(VandDCount$Length)
|
|
226 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
|
|
227 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
|
|
228 VandDCount$relLength = VandDCount$l / VandDCount$max
|
|
229
|
|
230 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(test$Sample))
|
|
231
|
|
232 completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
|
|
233 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
234 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
235 VDList = split(completeVD, f=completeVD[,"Sample"])
|
|
236
|
|
237 lapply(VDList, FUN=plotVD)
|
|
238
|
|
239
|
|
240
|
|
241 plotVJ <- function(dat){
|
|
242 if(length(dat[,1]) == 0){
|
|
243 return()
|
|
244 }
|
|
245 img = ggplot() +
|
|
246 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
|
247 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
248 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
249 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
250 xlab("J genes") +
|
|
251 ylab("V Genes")
|
|
252
|
|
253 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
254 print(img)
|
|
255 dev.off()
|
2
|
256 write.table(x=acast(dat, Top.V.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapVJ_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
|
0
|
257 }
|
|
258
|
2
|
259 VandJCount = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
|
0
|
260
|
|
261 VandJCount$l = log(VandJCount$Length)
|
|
262 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
263 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
264 VandJCount$relLength = VandJCount$l / VandJCount$max
|
|
265
|
|
266 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
|
|
267
|
|
268 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
|
|
269 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
270 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
271 VJList = split(completeVJ, f=completeVJ[,"Sample"])
|
|
272 lapply(VJList, FUN=plotVJ)
|
|
273
|
|
274 plotDJ <- function(dat){
|
|
275 if(length(dat[,1]) == 0){
|
|
276 return()
|
|
277 }
|
|
278 img = ggplot() +
|
|
279 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
|
|
280 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
281 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
282 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
283 xlab("J genes") +
|
|
284 ylab("D Genes")
|
|
285
|
|
286 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
|
|
287 print(img)
|
|
288 dev.off()
|
2
|
289 write.table(x=acast(dat, Top.D.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapDJ_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
|
0
|
290 }
|
|
291
|
|
292 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
|
|
293
|
|
294 DandJCount$l = log(DandJCount$Length)
|
|
295 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
296 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
297 DandJCount$relLength = DandJCount$l / DandJCount$max
|
|
298
|
|
299 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
|
|
300
|
|
301 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
|
|
302 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
303 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
304 DJList = split(completeDJ, f=completeDJ[,"Sample"])
|
|
305 lapply(DJList, FUN=plotDJ)
|
|
306
|
|
307
|
|
308 sampleFile <- file("samples.txt")
|
|
309 un = unique(test$Sample)
|
|
310 un = paste(un, sep="\n")
|
|
311 writeLines(un, sampleFile)
|
|
312 close(sampleFile)
|
|
313
|
|
314
|
|
315 if("Replicate" %in% colnames(test))
|
|
316 {
|
|
317 clonalityFrame = PROD
|
|
318 clonalityFrame$ReplicateConcat = do.call(paste, c(clonalityFrame[c("VDJCDR3", "Sample", "Replicate")], sep = ":"))
|
|
319 clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$ReplicateConcat), ]
|
2
|
320 write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
|
0
|
321
|
|
322 ClonalitySampleReplicatePrint <- function(dat){
|
2
|
323 write.table(dat, paste("clonality_", unique(dat$Sample) , "_", unique(dat$Replicate), ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
|
0
|
324 }
|
|
325
|
|
326 clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,c("Sample", "Replicate")])
|
2
|
327 #lapply(clonalityFrameSplit, FUN=ClonalitySampleReplicatePrint)
|
0
|
328
|
|
329 ClonalitySamplePrint <- function(dat){
|
2
|
330 write.table(dat, paste("clonality_", unique(dat$Sample) , ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
|
0
|
331 }
|
|
332
|
|
333 clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,"Sample"])
|
2
|
334 #lapply(clonalityFrameSplit, FUN=ClonalitySamplePrint)
|
0
|
335
|
|
336 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "VDJCDR3")])
|
|
337 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
|
|
338 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
|
|
339 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
|
|
340 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
|
|
341
|
|
342 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
|
|
343 tcct = textConnection(ct)
|
|
344 CT = read.table(tcct, sep="\t", header=TRUE)
|
|
345 close(tcct)
|
|
346 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
|
|
347 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
|
|
348
|
|
349 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "VDJCDR3")])
|
|
350 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
|
|
351 clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
|
|
352 ReplicateReads$squared = ReplicateReads$Reads * ReplicateReads$Reads
|
|
353
|
|
354 ReplicatePrint <- function(dat){
|
|
355 write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
356 }
|
|
357
|
|
358 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
359 lapply(ReplicateSplit, FUN=ReplicatePrint)
|
|
360
|
|
361 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(Reads), ReadsSquaredSum=sum(squared)), by=c("Sample")])
|
|
362 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
|
|
363
|
|
364
|
|
365 ReplicateSumPrint <- function(dat){
|
|
366 write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
367 }
|
|
368
|
|
369 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
370 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
|
|
371
|
|
372 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
|
|
373 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
|
|
374 clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
|
|
375 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
|
|
376 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
|
|
377
|
|
378 ClonalityScorePrint <- function(dat){
|
|
379 write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
380 }
|
|
381
|
|
382 clonalityScore = clonalFreqCount[c("Sample", "Result")]
|
|
383 clonalityScore = unique(clonalityScore)
|
|
384
|
|
385 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
|
|
386 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
|
|
387
|
|
388 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
|
|
389
|
|
390
|
|
391
|
|
392 ClonalityOverviewPrint <- function(dat){
|
|
393 write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
394 }
|
|
395
|
|
396 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
|
|
397 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
|
|
398 }
|
|
399
|
|
400 if("Functionality" %in% colnames(test))
|
|
401 {
|
|
402 newData = data.frame(data.table(PROD)[,list(unique=.N,
|
|
403 VH.DEL=mean(X3V.REGION.trimmed.nt.nb),
|
|
404 P1=mean(P3V.nt.nb),
|
|
405 N1=mean(N1.REGION.nt.nb),
|
|
406 P2=mean(P5D.nt.nb),
|
|
407 DEL.DH=mean(X5D.REGION.trimmed.nt.nb),
|
|
408 DH.DEL=mean(X3D.REGION.trimmed.nt.nb),
|
|
409 P3=mean(P3D.nt.nb),
|
|
410 N2=mean(N2.REGION.nt.nb),
|
|
411 P4=mean(P5J.nt.nb),
|
|
412 DEL.JH=mean(X5J.REGION.trimmed.nt.nb),
|
|
413 Total.Del=( mean(X3V.REGION.trimmed.nt.nb) +
|
|
414 mean(X5D.REGION.trimmed.nt.nb) +
|
|
415 mean(X3D.REGION.trimmed.nt.nb) +
|
|
416 mean(X5J.REGION.trimmed.nt.nb)),
|
|
417
|
|
418 Total.N=( mean(N1.REGION.nt.nb) +
|
|
419 mean(N2.REGION.nt.nb)),
|
|
420
|
|
421 Total.P=( mean(P3V.nt.nb) +
|
|
422 mean(P5D.nt.nb) +
|
|
423 mean(P3D.nt.nb) +
|
|
424 mean(P5J.nt.nb))),
|
|
425 by=c("Sample")])
|
|
426 write.table(newData, "junctionAnalysis.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
427 }
|