47
|
1 #options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
|
0
|
2
|
|
3 args <- commandArgs(trailingOnly = TRUE)
|
|
4
|
|
5 inFile = args[1]
|
|
6 outFile = args[2]
|
32
|
7 outDir = args[3]
|
42
|
8 clonalType = args[4]
|
0
|
9
|
36
|
10 if (!("gridExtra" %in% rownames(installed.packages()))) {
|
|
11 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
|
32
|
12 }
|
|
13 library(gridExtra)
|
36
|
14 if (!("ggplot2" %in% rownames(installed.packages()))) {
|
|
15 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
|
32
|
16 }
|
23
|
17 require(ggplot2)
|
36
|
18 if (!("plyr" %in% rownames(installed.packages()))) {
|
|
19 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
|
32
|
20 }
|
23
|
21 require(plyr)
|
0
|
22
|
36
|
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
|
|
28
|
38
|
29 test = read.table(inFile, sep="\t", header=TRUE, fill=T)
|
0
|
30
|
39
|
31 test = test[test$Sample != "",]
|
|
32
|
0
|
33 test$Top.V.Gene = gsub("[*]([0-9]+)", "", test$Top.V.Gene)
|
|
34 test$Top.D.Gene = gsub("[*]([0-9]+)", "", test$Top.D.Gene)
|
|
35 test$Top.J.Gene = gsub("[*]([0-9]+)", "", test$Top.J.Gene)
|
|
36
|
42
|
37 #test$VDJCDR3 = do.call(paste, c(test[c("Top.V.Gene", "Top.D.Gene", "Top.J.Gene","CDR3.Seq.DNA")], sep = ":"))
|
|
38 test$VDJCDR3 = do.call(paste, c(test[unlist(strsplit(clonalType, ","))], sep = ":"))
|
0
|
39
|
|
40 PROD = test[test$VDJ.Frame != "In-frame with stop codon" & test$VDJ.Frame != "Out-of-frame" & test$CDR3.Found.How != "NOT_FOUND" , ]
|
|
41
|
|
42 NONPROD = test[test$VDJ.Frame == "In-frame with stop codon" | test$VDJ.Frame == "Out-of-frame" | test$CDR3.Found.How == "NOT_FOUND" , ]
|
|
43
|
36
|
44 #PRODF = PROD[ -1]
|
|
45
|
|
46 PRODF = PROD
|
0
|
47
|
32
|
48 #PRODF = unique(PRODF)
|
|
49 PRODF = PRODF[!duplicated(PRODF$VDJCDR3), ]
|
|
50
|
36
|
51 PRODFV = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.V.Gene")])
|
0
|
52 PRODFV$Length = as.numeric(PRODFV$Length)
|
|
53 Total = 0
|
|
54 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
55 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
56 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
57
|
36
|
58 PRODFD = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.D.Gene")])
|
0
|
59 PRODFD$Length = as.numeric(PRODFD$Length)
|
|
60 Total = 0
|
|
61 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
62 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
63 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
64
|
36
|
65 PRODFJ = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.J.Gene")])
|
0
|
66 PRODFJ$Length = as.numeric(PRODFJ$Length)
|
|
67 Total = 0
|
|
68 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
69 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
70 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
71
|
47
|
72 V = c("v.name\tchr.orderV\nIGHV7-81\t1\nIGHV3-74\t2\nIGHV3-73\t3\nIGHV3-72\t4\nIGHV2-70\t6\nIGHV1-69\t7\nIGHV3-66\t8\nIGHV3-64\t9\nIGHV4-61\t10\nIGHV4-59\t11\nIGHV1-58\t12\nIGHV3-53\t13\nIGHV5-a\t15\nIGHV5-51\t16\nIGHV3-49\t17\nIGHV3-48\t18\nIGHV1-46\t20\nIGHV1-45\t21\nIGHV3-43\t22\nIGHV4-39\t23\nIGHV3-35\t24\nIGHV4-34\t25\nIGHV3-33\t26\nIGHV4-31\t27\nIGHV4-30-4\t28\nIGHV4-30-2\t29\nIGHV3-30-3\t30\nIGHV3-30\t31\nIGHV4-28\t32\nIGHV2-26\t33\nIGHV1-24\t34\nIGHV3-23\t35\nIGHV3-21\t37\nIGHV3-20\t38\nIGHV1-18\t40\nIGHV3-15\t41\nIGHV3-13\t42\nIGHV3-11\t43\nIGHV3-9\t44\nIGHV1-8\t45\nIGHV3-7\t46\nIGHV2-5\t47\nIGHV7-4-1\t48\nIGHV4-4\t49\nIGHV4-b\t50\nIGHV1-3\t51\nIGHV1-2\t52\nIGHV6-1\t53")
|
0
|
73 tcV = textConnection(V)
|
|
74 Vchain = read.table(tcV, sep="\t", header=TRUE)
|
|
75 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
|
|
76 close(tcV)
|
|
77
|
|
78 D = c("v.name\tchr.orderD\nIGHD1-1\t1\nIGHD2-2\t2\nIGHD3-3\t3\nIGHD6-6\t4\nIGHD1-7\t5\nIGHD2-8\t6\nIGHD3-9\t7\nIGHD3-10\t8\nIGHD4-11\t9\nIGHD5-12\t10\nIGHD6-13\t11\nIGHD1-14\t12\nIGHD2-15\t13\nIGHD3-16\t14\nIGHD4-17\t15\nIGHD5-18\t16\nIGHD6-19\t17\nIGHD1-20\t18\nIGHD2-21\t19\nIGHD3-22\t20\nIGHD4-23\t21\nIGHD5-24\t22\nIGHD6-25\t23\nIGHD1-26\t24\nIGHD7-27\t25")
|
|
79 tcD = textConnection(D)
|
|
80 Dchain = read.table(tcD, sep="\t", header=TRUE)
|
|
81 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
|
|
82 close(tcD)
|
|
83
|
|
84
|
|
85 J = c("v.name\tchr.orderJ\nIGHJ1\t1\nIGHJ2\t2\nIGHJ3\t3\nIGHJ4\t4\nIGHJ5\t5\nIGHJ6\t6")
|
|
86 tcJ = textConnection(J)
|
|
87 Jchain = read.table(tcJ, sep="\t", header=TRUE)
|
|
88 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
|
|
89 close(tcJ)
|
|
90
|
32
|
91 setwd(outDir)
|
|
92
|
49
|
93 write.table(PRODF, "allUnique.tsv", sep="\t",quote=F,row.names=T,col.names=T)
|
|
94
|
0
|
95 pV = ggplot(PRODFV)
|
|
96 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))
|
29
|
97 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
|
32
|
98
|
|
99 png("VPlot.png",width = 1280, height = 720)
|
|
100 pV
|
|
101 dev.off();
|
0
|
102
|
|
103 pD = ggplot(PRODFD)
|
|
104 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))
|
29
|
105 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
|
32
|
106
|
|
107 png("DPlot.png",width = 800, height = 600)
|
|
108 pD
|
|
109 dev.off();
|
0
|
110
|
|
111 pJ = ggplot(PRODFJ)
|
|
112 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))
|
32
|
113 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
|
|
114
|
|
115 png("JPlot.png",width = 800, height = 600)
|
|
116 pJ
|
|
117 dev.off();
|
|
118
|
38
|
119 revVchain = Vchain
|
|
120 revDchain = Dchain
|
|
121 revVchain$chr.orderV = rev(revVchain$chr.orderV)
|
|
122 revDchain$chr.orderD = rev(revDchain$chr.orderD)
|
0
|
123
|
32
|
124 plotVD <- function(dat){
|
39
|
125 if(length(dat[,1]) == 0){
|
|
126 return()
|
|
127 }
|
32
|
128 img = ggplot() +
|
38
|
129 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
32
|
130 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
47
|
131 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
38
|
132 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
32
|
133 xlab("D genes") +
|
|
134 ylab("V Genes")
|
|
135
|
|
136 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
137 print(img)
|
|
138 dev.off()
|
|
139 }
|
|
140
|
36
|
141 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
|
38
|
142
|
|
143 VandDCount$l = log(VandDCount$Length)
|
|
144 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
|
|
145 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
|
|
146 VandDCount$relLength = VandDCount$l / VandDCount$max
|
|
147
|
32
|
148 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(test$Sample))
|
|
149
|
|
150 completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
|
38
|
151 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
32
|
152 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
38
|
153 VDList = split(completeVD, f=completeVD[,"Sample"])
|
32
|
154
|
38
|
155 lapply(VDList, FUN=plotVD)
|
32
|
156
|
0
|
157
|
29
|
158
|
32
|
159 plotVJ <- function(dat){
|
39
|
160 if(length(dat[,1]) == 0){
|
|
161 return()
|
|
162 }
|
32
|
163 img = ggplot() +
|
38
|
164 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
32
|
165 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
47
|
166 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
38
|
167 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
32
|
168 xlab("J genes") +
|
|
169 ylab("V Genes")
|
|
170
|
|
171 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
172 print(img)
|
|
173 dev.off()
|
|
174 }
|
|
175
|
36
|
176 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
|
38
|
177
|
|
178 VandJCount$l = log(VandJCount$Length)
|
|
179 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
180 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
181 VandJCount$relLength = VandJCount$l / VandJCount$max
|
|
182
|
32
|
183 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
|
|
184
|
|
185 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
|
38
|
186 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
32
|
187 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
38
|
188 VJList = split(completeVJ, f=completeVJ[,"Sample"])
|
|
189 lapply(VJList, FUN=plotVJ)
|
32
|
190
|
|
191 plotDJ <- function(dat){
|
39
|
192 if(length(dat[,1]) == 0){
|
|
193 return()
|
|
194 }
|
32
|
195 img = ggplot() +
|
38
|
196 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
|
32
|
197 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
47
|
198 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
38
|
199 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
32
|
200 xlab("J genes") +
|
|
201 ylab("D Genes")
|
|
202
|
|
203 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
|
|
204 print(img)
|
|
205 dev.off()
|
|
206 }
|
|
207
|
36
|
208 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
|
38
|
209
|
|
210 DandJCount$l = log(DandJCount$Length)
|
|
211 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
212 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
213 DandJCount$relLength = DandJCount$l / DandJCount$max
|
|
214
|
32
|
215 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
|
|
216
|
|
217 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
|
38
|
218 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
32
|
219 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
38
|
220 DJList = split(completeDJ, f=completeDJ[,"Sample"])
|
|
221 lapply(DJList, FUN=plotDJ)
|
0
|
222
|
|
223
|
32
|
224 sampleFile <- file("samples.txt")
|
|
225 un = unique(test$Sample)
|
|
226 un = paste(un, sep="\n")
|
|
227 writeLines(un, sampleFile)
|
|
228 close(sampleFile)
|
47
|
229
|
|
230
|
|
231 if("Replicate" %in% colnames(test))
|
|
232 {
|
|
233 clonalityFrame = PROD
|
|
234 clonalityFrame$ReplicateConcat = do.call(paste, c(clonalityFrame[c("VDJCDR3", "Sample", "Replicate")], sep = ":"))
|
|
235 clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$ReplicateConcat), ]
|
49
|
236 write.table(clonalityFrame, "clonalityComplete.tsv", sep="\t",quote=F,row.names=T,col.names=T)
|
|
237
|
|
238 ClonalitySampleReplicatePrint <- function(dat){
|
|
239 write.table(dat, paste("clonality_", unique(dat$Sample) , "_", unique(dat$Replicate), ".tsv", sep=""), sep="\t",quote=F,row.names=T,col.names=T)
|
|
240 }
|
|
241
|
|
242 clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,c("Sample", "Replicate")])
|
|
243 lapply(clonalityFrameSplit, FUN=ClonalitySampleReplicatePrint)
|
|
244
|
|
245 ClonalitySamplePrint <- function(dat){
|
|
246 write.table(dat, paste("clonality_", unique(dat$Sample) , ".tsv", sep=""), sep="\t",quote=F,row.names=T,col.names=T)
|
|
247 }
|
|
248
|
|
249 clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,"Sample"])
|
|
250 lapply(clonalityFrameSplit, FUN=ClonalitySamplePrint)
|
|
251
|
47
|
252 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "VDJCDR3")])
|
|
253 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
|
|
254 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
|
|
255 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
|
|
256 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
|
|
257
|
|
258 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
|
|
259 tcct = textConnection(ct)
|
|
260 CT = read.table(tcct, sep="\t", header=TRUE)
|
|
261 close(tcct)
|
|
262 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
|
|
263 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
|
|
264
|
|
265 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "VDJCDR3")])
|
|
266 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
|
|
267 ReplicateReads$squared = ReplicateReads$Reads * ReplicateReads$Reads
|
|
268
|
|
269 ReplicatePrint <- function(dat){
|
|
270 write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
271 }
|
|
272
|
|
273 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
274 lapply(ReplicateSplit, FUN=ReplicatePrint)
|
|
275
|
|
276 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(Reads), ReadsSquaredSum=sum(squared)), by=c("Sample")])
|
|
277 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
|
|
278
|
|
279
|
|
280 ReplicateSumPrint <- function(dat){
|
|
281 write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
282 }
|
|
283
|
|
284 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
285 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
|
|
286
|
|
287 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
|
|
288 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
|
|
289
|
|
290 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
|
|
291 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
|
|
292
|
|
293 ClonalityScorePrint <- function(dat){
|
|
294 write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
295 }
|
|
296
|
|
297 clonalityScore = clonalFreqCount[c("Sample", "Result")]
|
|
298 clonalityScore = unique(clonalityScore)
|
|
299
|
|
300 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
|
|
301 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
|
|
302
|
|
303 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
|
|
304
|
|
305
|
|
306
|
|
307 ClonalityOverviewPrint <- function(dat){
|
|
308 write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
309 }
|
|
310
|
|
311 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
|
|
312 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
|
|
313 }
|