0
|
1 # ---------------------- load/install packages ----------------------
|
|
2
|
|
3 if (!("gridExtra" %in% rownames(installed.packages()))) {
|
|
4 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
|
|
5 }
|
|
6 library(gridExtra)
|
|
7 if (!("ggplot2" %in% rownames(installed.packages()))) {
|
|
8 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
|
|
9 }
|
|
10 library(ggplot2)
|
|
11 if (!("plyr" %in% rownames(installed.packages()))) {
|
|
12 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
|
3
|
13 }
|
0
|
14 library(plyr)
|
|
15
|
|
16 if (!("data.table" %in% rownames(installed.packages()))) {
|
|
17 install.packages("data.table", repos="http://cran.xl-mirror.nl/")
|
|
18 }
|
|
19 library(data.table)
|
|
20
|
|
21 if (!("reshape2" %in% rownames(installed.packages()))) {
|
|
22 install.packages("reshape2", repos="http://cran.xl-mirror.nl/")
|
|
23 }
|
|
24 library(reshape2)
|
|
25
|
|
26 if (!("lymphclon" %in% rownames(installed.packages()))) {
|
|
27 install.packages("lymphclon", repos="http://cran.xl-mirror.nl/")
|
|
28 }
|
|
29 library(lymphclon)
|
|
30
|
|
31 # ---------------------- parameters ----------------------
|
|
32
|
|
33 args <- commandArgs(trailingOnly = TRUE)
|
|
34
|
|
35 infile = args[1] #path to input file
|
|
36 outfile = args[2] #path to output file
|
|
37 outdir = args[3] #path to output folder (html/images/data)
|
|
38 clonaltype = args[4] #clonaltype definition, or 'none' for no unique filtering
|
|
39 ct = unlist(strsplit(clonaltype, ","))
|
|
40 species = args[5] #human or mouse
|
|
41 locus = args[6] # IGH, IGK, IGL, TRB, TRA, TRG or TRD
|
|
42 filterproductive = ifelse(args[7] == "yes", T, F) #should unproductive sequences be filtered out? (yes/no)
|
|
43 clonality_method = args[8]
|
|
44
|
|
45
|
|
46 # ---------------------- Data preperation ----------------------
|
|
47
|
|
48 print("Report Clonality - Data preperation")
|
|
49
|
|
50 inputdata = read.table(infile, sep="\t", header=TRUE, fill=T, comment.char="")
|
|
51
|
|
52 print(paste("nrows: ", nrow(inputdata)))
|
|
53
|
|
54 setwd(outdir)
|
|
55
|
|
56 # remove weird rows
|
|
57 inputdata = inputdata[inputdata$Sample != "",]
|
|
58
|
|
59 print(paste("nrows: ", nrow(inputdata)))
|
|
60
|
|
61 #remove the allele from the V,D and J genes
|
|
62 inputdata$Top.V.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.V.Gene)
|
|
63 inputdata$Top.D.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.D.Gene)
|
|
64 inputdata$Top.J.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.J.Gene)
|
|
65
|
|
66 print(paste("nrows: ", nrow(inputdata)))
|
|
67
|
|
68 #filter uniques
|
|
69 inputdata.removed = inputdata[NULL,]
|
|
70
|
|
71 print(paste("nrows: ", nrow(inputdata)))
|
|
72
|
|
73 inputdata$clonaltype = 1:nrow(inputdata)
|
|
74
|
|
75 #keep track of the count of sequences in samples or samples/replicates for the front page overview
|
|
76 input.sample.count = data.frame(data.table(inputdata)[, list(All=.N), by=c("Sample")])
|
|
77 input.rep.count = data.frame(data.table(inputdata)[, list(All=.N), by=c("Sample", "Replicate")])
|
|
78
|
|
79 PRODF = inputdata
|
|
80 UNPROD = inputdata
|
|
81 if(filterproductive){
|
|
82 if("Functionality" %in% colnames(inputdata)) { # "Functionality" is an IMGT column
|
|
83 #PRODF = inputdata[inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)", ]
|
|
84 PRODF = inputdata[inputdata$Functionality %in% c("productive (see comment)","productive"),]
|
|
85
|
|
86 PRODF.count = data.frame(data.table(PRODF)[, list(count=.N), by=c("Sample")])
|
|
87
|
|
88 UNPROD = inputdata[inputdata$Functionality %in% c("unproductive (see comment)","unproductive"), ]
|
|
89 } else {
|
|
90 PRODF = inputdata[inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" , ]
|
|
91 UNPROD = inputdata[!(inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" ), ]
|
|
92 }
|
|
93 }
|
|
94
|
|
95 prod.sample.count = data.frame(data.table(PRODF)[, list(Productive=.N), by=c("Sample")])
|
|
96 prod.rep.count = data.frame(data.table(PRODF)[, list(Productive=.N), by=c("Sample", "Replicate")])
|
|
97
|
|
98 unprod.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive=.N), by=c("Sample")])
|
|
99 unprod.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive=.N), by=c("Sample", "Replicate")])
|
|
100
|
|
101 clonalityFrame = PRODF
|
|
102
|
|
103 #remove duplicates based on the clonaltype
|
|
104 if(clonaltype != "none"){
|
|
105 clonaltype = paste(clonaltype, ",Sample", sep="") #add sample column to clonaltype, unique within samples
|
|
106 PRODF$clonaltype = do.call(paste, c(PRODF[unlist(strsplit(clonaltype, ","))], sep = ":"))
|
|
107 PRODF = PRODF[!duplicated(PRODF$clonaltype), ]
|
|
108
|
|
109 UNPROD$clonaltype = do.call(paste, c(UNPROD[unlist(strsplit(clonaltype, ","))], sep = ":"))
|
|
110 UNPROD = UNPROD[!duplicated(UNPROD$clonaltype), ]
|
|
111
|
|
112 #again for clonalityFrame but with sample+replicate
|
|
113 clonalityFrame$clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(clonaltype, ","))], sep = ":"))
|
|
114 clonalityFrame$clonality_clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(paste(clonaltype, ",Replicate", sep=""), ","))], sep = ":"))
|
|
115 clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$clonality_clonaltype), ]
|
|
116 }
|
|
117
|
1
|
118 print("SAMPLE TABLE:")
|
|
119 print(table(PRODF$Sample))
|
|
120
|
0
|
121 prod.unique.sample.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample")])
|
|
122 prod.unique.rep.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample", "Replicate")])
|
|
123
|
|
124 unprod.unique.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample")])
|
|
125 unprod.unique.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample", "Replicate")])
|
|
126
|
|
127 PRODF$freq = 1
|
|
128
|
|
129 if(any(grepl(pattern="_", x=PRODF$ID))){ #the frequency can be stored in the ID with the pattern ".*_freq_.*"
|
|
130 PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
|
|
131 PRODF$freq = gsub("_.*", "", PRODF$freq)
|
|
132 PRODF$freq = as.numeric(PRODF$freq)
|
|
133 if(any(is.na(PRODF$freq))){ #if there was an "_" in the ID, but not the frequency, go back to frequency of 1 for every sequence
|
|
134 PRODF$freq = 1
|
|
135 }
|
|
136 }
|
|
137
|
|
138
|
|
139
|
|
140 #write the complete dataset that is left over, will be the input if 'none' for clonaltype and 'no' for filterproductive
|
|
141 write.table(PRODF, "allUnique.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
|
142 write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
143 write.table(UNPROD, "allUnproductive.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
144
|
|
145 #write the samples to a file
|
|
146 sampleFile <- file("samples.txt")
|
|
147 un = unique(inputdata$Sample)
|
|
148 un = paste(un, sep="\n")
|
|
149 writeLines(un, sampleFile)
|
|
150 close(sampleFile)
|
|
151
|
|
152 # ---------------------- Counting the productive/unproductive and unique sequences ----------------------
|
|
153
|
|
154 print("Report Clonality - counting productive/unproductive/unique")
|
|
155
|
|
156 #create the table on the overview page with the productive/unique counts per sample/replicate
|
|
157 #first for sample
|
|
158 sample.count = merge(input.sample.count, prod.sample.count, by="Sample", all.x=T)
|
|
159 sample.count$perc_prod = round(sample.count$Productive / sample.count$All * 100)
|
|
160 sample.count = merge(sample.count, prod.unique.sample.count, by="Sample", all.x=T)
|
|
161 sample.count$perc_prod_un = round(sample.count$Productive_unique / sample.count$All * 100)
|
|
162
|
|
163 sample.count = merge(sample.count , unprod.sample.count, by="Sample", all.x=T)
|
|
164 sample.count$perc_unprod = round(sample.count$Unproductive / sample.count$All * 100)
|
|
165 sample.count = merge(sample.count, unprod.unique.sample.count, by="Sample", all.x=T)
|
|
166 sample.count$perc_unprod_un = round(sample.count$Unproductive_unique / sample.count$All * 100)
|
|
167
|
|
168 #then sample/replicate
|
|
169 rep.count = merge(input.rep.count, prod.rep.count, by=c("Sample", "Replicate"), all.x=T)
|
|
170 rep.count$perc_prod = round(rep.count$Productive / rep.count$All * 100)
|
|
171 rep.count = merge(rep.count, prod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
|
|
172 rep.count$perc_prod_un = round(rep.count$Productive_unique / rep.count$All * 100)
|
|
173
|
|
174 rep.count = merge(rep.count, unprod.rep.count, by=c("Sample", "Replicate"), all.x=T)
|
|
175 rep.count$perc_unprod = round(rep.count$Unproductive / rep.count$All * 100)
|
|
176 rep.count = merge(rep.count, unprod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
|
|
177 rep.count$perc_unprod_un = round(rep.count$Unproductive_unique / rep.count$All * 100)
|
|
178
|
|
179 rep.count$Sample = paste(rep.count$Sample, rep.count$Replicate, sep="_")
|
|
180 rep.count = rep.count[,names(rep.count) != "Replicate"]
|
|
181
|
|
182 count = rbind(sample.count, rep.count)
|
|
183
|
|
184
|
|
185
|
|
186 write.table(x=count, file="productive_counting.txt", sep=",",quote=F,row.names=F,col.names=F)
|
|
187
|
|
188 # ---------------------- V+J+CDR3 sequence count ----------------------
|
|
189
|
|
190 VJCDR3.count = data.frame(table(clonalityFrame$Top.V.Gene, clonalityFrame$Top.J.Gene, clonalityFrame$CDR3.Seq.DNA))
|
|
191 names(VJCDR3.count) = c("Top.V.Gene", "Top.J.Gene", "CDR3.Seq.DNA", "Count")
|
|
192
|
|
193 VJCDR3.count = VJCDR3.count[VJCDR3.count$Count > 0,]
|
|
194 VJCDR3.count = VJCDR3.count[order(-VJCDR3.count$Count),]
|
|
195
|
|
196 write.table(x=VJCDR3.count, file="VJCDR3_count.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
|
197
|
|
198 # ---------------------- Frequency calculation for V, D and J ----------------------
|
|
199
|
|
200 print("Report Clonality - frequency calculation V, D and J")
|
|
201
|
|
202 PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
|
|
203 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
204 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
205 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
206
|
|
207 PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
|
|
208 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
209 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
210 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
211
|
|
212 PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
|
|
213 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
214 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
215 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
216
|
|
217 # ---------------------- Setting up the gene names for the different species/loci ----------------------
|
|
218
|
|
219 print("Report Clonality - getting genes for species/loci")
|
|
220
|
|
221 Vchain = ""
|
|
222 Dchain = ""
|
|
223 Jchain = ""
|
|
224
|
|
225 if(species == "custom"){
|
|
226 print("Custom genes: ")
|
|
227 splt = unlist(strsplit(locus, ";"))
|
|
228 print(paste("V:", splt[1]))
|
|
229 print(paste("D:", splt[2]))
|
|
230 print(paste("J:", splt[3]))
|
|
231
|
|
232 Vchain = unlist(strsplit(splt[1], ","))
|
|
233 Vchain = data.frame(v.name = Vchain, chr.orderV = 1:length(Vchain))
|
|
234
|
|
235 Dchain = unlist(strsplit(splt[2], ","))
|
|
236 if(length(Dchain) > 0){
|
|
237 Dchain = data.frame(v.name = Dchain, chr.orderD = 1:length(Dchain))
|
|
238 } else {
|
|
239 Dchain = data.frame(v.name = character(0), chr.orderD = numeric(0))
|
|
240 }
|
|
241
|
|
242 Jchain = unlist(strsplit(splt[3], ","))
|
|
243 Jchain = data.frame(v.name = Jchain, chr.orderJ = 1:length(Jchain))
|
|
244
|
|
245 } else {
|
|
246 genes = read.table("genes.txt", sep="\t", header=TRUE, fill=T, comment.char="")
|
|
247
|
|
248 Vchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "V",c("IMGT.GENE.DB", "chr.order")]
|
|
249 colnames(Vchain) = c("v.name", "chr.orderV")
|
|
250 Dchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "D",c("IMGT.GENE.DB", "chr.order")]
|
|
251 colnames(Dchain) = c("v.name", "chr.orderD")
|
|
252 Jchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "J",c("IMGT.GENE.DB", "chr.order")]
|
|
253 colnames(Jchain) = c("v.name", "chr.orderJ")
|
|
254 }
|
|
255 useD = TRUE
|
|
256 if(nrow(Dchain) == 0){
|
|
257 useD = FALSE
|
|
258 cat("No D Genes in this species/locus")
|
|
259 }
|
|
260 print(paste(nrow(Vchain), "genes in V"))
|
|
261 print(paste(nrow(Dchain), "genes in D"))
|
|
262 print(paste(nrow(Jchain), "genes in J"))
|
|
263
|
|
264 # ---------------------- merge with the frequency count ----------------------
|
|
265
|
|
266 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
|
|
267
|
|
268 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
|
|
269
|
|
270 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
|
|
271
|
|
272 # ---------------------- Create the V, D and J frequency plots and write the data.frame for every plot to a file ----------------------
|
|
273
|
|
274 print("Report Clonality - V, D and J frequency plots")
|
|
275
|
|
276 pV = ggplot(PRODFV)
|
|
277 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))
|
|
278 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
|
|
279 write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
280
|
|
281 png("VPlot.png",width = 1280, height = 720)
|
|
282 pV
|
|
283 dev.off();
|
|
284
|
|
285 if(useD){
|
|
286 pD = ggplot(PRODFD)
|
|
287 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))
|
|
288 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
|
|
289 write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
290
|
|
291 png("DPlot.png",width = 800, height = 600)
|
|
292 print(pD)
|
|
293 dev.off();
|
|
294 }
|
|
295
|
|
296 pJ = ggplot(PRODFJ)
|
|
297 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))
|
|
298 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
|
|
299 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
300
|
|
301 png("JPlot.png",width = 800, height = 600)
|
|
302 pJ
|
|
303 dev.off();
|
|
304
|
|
305 pJ = ggplot(PRODFJ)
|
|
306 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))
|
|
307 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
|
|
308 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
309
|
|
310 png("JPlot.png",width = 800, height = 600)
|
|
311 pJ
|
|
312 dev.off();
|
|
313
|
|
314 # ---------------------- Now the frequency plots of the V, D and J families ----------------------
|
|
315
|
|
316 print("Report Clonality - V, D and J family plots")
|
|
317
|
|
318 VGenes = PRODF[,c("Sample", "Top.V.Gene")]
|
|
319 VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
|
|
320 VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
|
|
321 TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
322 VGenes = merge(VGenes, TotalPerSample, by="Sample")
|
|
323 VGenes$Frequency = VGenes$Count * 100 / VGenes$total
|
|
324 VPlot = ggplot(VGenes)
|
|
325 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)) +
|
|
326 ggtitle("Distribution of V gene families") +
|
|
327 ylab("Percentage of sequences")
|
|
328 png("VFPlot.png")
|
|
329 VPlot
|
|
330 dev.off();
|
|
331 write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
332
|
|
333 if(useD){
|
|
334 DGenes = PRODF[,c("Sample", "Top.D.Gene")]
|
|
335 DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
|
|
336 DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
|
|
337 TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
338 DGenes = merge(DGenes, TotalPerSample, by="Sample")
|
|
339 DGenes$Frequency = DGenes$Count * 100 / DGenes$total
|
|
340 DPlot = ggplot(DGenes)
|
|
341 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)) +
|
|
342 ggtitle("Distribution of D gene families") +
|
|
343 ylab("Percentage of sequences")
|
|
344 png("DFPlot.png")
|
|
345 print(DPlot)
|
|
346 dev.off();
|
|
347 write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
348 }
|
|
349
|
|
350 JGenes = PRODF[,c("Sample", "Top.J.Gene")]
|
|
351 JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
|
|
352 JGenes = data.frame(data.table(JGenes)[, list(Count=.N), by=c("Sample", "Top.J.Gene")])
|
|
353 TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
354 JGenes = merge(JGenes, TotalPerSample, by="Sample")
|
|
355 JGenes$Frequency = JGenes$Count * 100 / JGenes$total
|
|
356 JPlot = ggplot(JGenes)
|
|
357 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)) +
|
|
358 ggtitle("Distribution of J gene families") +
|
|
359 ylab("Percentage of sequences")
|
|
360 png("JFPlot.png")
|
|
361 JPlot
|
|
362 dev.off();
|
|
363 write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
364
|
|
365 # ---------------------- Plotting the cdr3 length ----------------------
|
|
366
|
|
367 print("Report Clonality - CDR3 length plot")
|
|
368
|
|
369 CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length.DNA")])
|
|
370 TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
|
|
371 CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
|
|
372 CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
|
|
373 CDR3LengthPlot = ggplot(CDR3Length)
|
|
374 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)) +
|
|
375 ggtitle("Length distribution of CDR3") +
|
|
376 xlab("CDR3 Length") +
|
|
377 ylab("Percentage of sequences")
|
|
378 png("CDR3LengthPlot.png",width = 1280, height = 720)
|
|
379 CDR3LengthPlot
|
|
380 dev.off()
|
|
381 write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
382
|
|
383 # ---------------------- Plot the heatmaps ----------------------
|
|
384
|
|
385 #get the reverse order for the V and D genes
|
|
386 revVchain = Vchain
|
|
387 revDchain = Dchain
|
|
388 revVchain$chr.orderV = rev(revVchain$chr.orderV)
|
|
389 revDchain$chr.orderD = rev(revDchain$chr.orderD)
|
|
390
|
|
391 if(useD){
|
|
392 print("Report Clonality - Heatmaps VD")
|
|
393 plotVD <- function(dat){
|
|
394 if(length(dat[,1]) == 0){
|
|
395 return()
|
|
396 }
|
|
397
|
|
398 img = ggplot() +
|
|
399 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
|
400 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
401 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
402 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
403 xlab("D genes") +
|
|
404 ylab("V Genes")
|
|
405
|
|
406 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
407 print(img)
|
|
408 dev.off()
|
|
409 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)
|
|
410 }
|
|
411
|
|
412 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
|
|
413
|
|
414 VandDCount$l = log(VandDCount$Length)
|
|
415 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
|
|
416 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
|
|
417 VandDCount$relLength = VandDCount$l / VandDCount$max
|
|
418
|
|
419 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name)
|
|
420
|
|
421 completeVD = merge(VandDCount, cartegianProductVD, by.x=c("Top.V.Gene", "Top.D.Gene"), by.y=c("Top.V.Gene", "Top.D.Gene"), all=TRUE)
|
|
422
|
|
423 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
424
|
|
425 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
426
|
|
427 fltr = is.nan(completeVD$relLength)
|
|
428 if(all(fltr)){
|
|
429 completeVD[fltr,"relLength"] = 0
|
|
430 }
|
|
431
|
|
432 VDList = split(completeVD, f=completeVD[,"Sample"])
|
|
433 lapply(VDList, FUN=plotVD)
|
|
434 }
|
|
435
|
|
436 print("Report Clonality - Heatmaps VJ")
|
|
437
|
|
438 plotVJ <- function(dat){
|
|
439 if(length(dat[,1]) == 0){
|
|
440 return()
|
|
441 }
|
|
442 cat(paste(unique(dat[3])[1,1]))
|
|
443 img = ggplot() +
|
|
444 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
|
445 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
446 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
447 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
448 xlab("J genes") +
|
|
449 ylab("V Genes")
|
|
450
|
|
451 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
452 print(img)
|
|
453 dev.off()
|
|
454 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)
|
|
455 }
|
|
456
|
|
457 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
|
|
458
|
|
459 VandJCount$l = log(VandJCount$Length)
|
|
460 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
461 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
462 VandJCount$relLength = VandJCount$l / VandJCount$max
|
|
463
|
|
464 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name)
|
|
465
|
|
466 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
|
|
467 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
468 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
469
|
|
470 fltr = is.nan(completeVJ$relLength)
|
|
471 if(any(fltr)){
|
|
472 completeVJ[fltr,"relLength"] = 1
|
|
473 }
|
|
474
|
|
475 VJList = split(completeVJ, f=completeVJ[,"Sample"])
|
|
476 lapply(VJList, FUN=plotVJ)
|
|
477
|
|
478
|
|
479
|
|
480 if(useD){
|
|
481 print("Report Clonality - Heatmaps DJ")
|
|
482 plotDJ <- function(dat){
|
|
483 if(length(dat[,1]) == 0){
|
|
484 return()
|
|
485 }
|
|
486 img = ggplot() +
|
|
487 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
|
|
488 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
489 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
490 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
491 xlab("J genes") +
|
|
492 ylab("D Genes")
|
|
493
|
|
494 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
|
|
495 print(img)
|
|
496 dev.off()
|
|
497 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)
|
|
498 }
|
|
499
|
|
500
|
|
501 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
|
|
502
|
|
503 DandJCount$l = log(DandJCount$Length)
|
|
504 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
505 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
506 DandJCount$relLength = DandJCount$l / DandJCount$max
|
|
507
|
|
508 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name)
|
|
509
|
|
510 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
|
|
511 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
512 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
513
|
|
514 fltr = is.nan(completeDJ$relLength)
|
|
515 if(any(fltr)){
|
|
516 completeDJ[fltr, "relLength"] = 1
|
|
517 }
|
|
518
|
|
519 DJList = split(completeDJ, f=completeDJ[,"Sample"])
|
|
520 lapply(DJList, FUN=plotDJ)
|
|
521 }
|
|
522
|
|
523
|
|
524 # ---------------------- output tables for the circos plots ----------------------
|
|
525
|
|
526 print("Report Clonality - Circos data")
|
|
527
|
|
528 for(smpl in unique(PRODF$Sample)){
|
|
529 PRODF.sample = PRODF[PRODF$Sample == smpl,]
|
|
530
|
|
531 fltr = PRODF.sample$Top.V.Gene == ""
|
|
532 if(any(fltr, na.rm=T)){
|
|
533 PRODF.sample[fltr, "Top.V.Gene"] = "NA"
|
|
534 }
|
|
535
|
|
536 fltr = PRODF.sample$Top.D.Gene == ""
|
|
537 if(any(fltr, na.rm=T)){
|
|
538 PRODF.sample[fltr, "Top.D.Gene"] = "NA"
|
|
539 }
|
|
540
|
|
541 fltr = PRODF.sample$Top.J.Gene == ""
|
|
542 if(any(fltr, na.rm=T)){
|
|
543 PRODF.sample[fltr, "Top.J.Gene"] = "NA"
|
|
544 }
|
|
545
|
|
546 v.d = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.D.Gene)
|
|
547 v.j = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.J.Gene)
|
|
548 d.j = table(PRODF.sample$Top.D.Gene, PRODF.sample$Top.J.Gene)
|
|
549
|
|
550 write.table(v.d, file=paste(smpl, "_VD_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
|
|
551 write.table(v.j, file=paste(smpl, "_VJ_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
|
|
552 write.table(d.j, file=paste(smpl, "_DJ_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
|
|
553 }
|
|
554
|
|
555 # ---------------------- calculating the clonality score ----------------------
|
|
556
|
|
557 if("Replicate" %in% colnames(inputdata)) #can only calculate clonality score when replicate information is available
|
|
558 {
|
|
559 print("Report Clonality - Clonality")
|
|
560 write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
561 if(clonality_method == "boyd"){
|
|
562 samples = split(clonalityFrame, clonalityFrame$Sample, drop=T)
|
|
563
|
|
564 for (sample in samples){
|
|
565 res = data.frame(paste=character(0))
|
|
566 sample_id = unique(sample$Sample)[[1]]
|
|
567 for(replicate in unique(sample$Replicate)){
|
|
568 tmp = sample[sample$Replicate == replicate,]
|
|
569 clone_table = data.frame(table(tmp$clonaltype))
|
|
570 clone_col_name = paste("V", replicate, sep="")
|
|
571 colnames(clone_table) = c("paste", clone_col_name)
|
|
572 res = merge(res, clone_table, by="paste", all=T)
|
|
573 }
|
|
574
|
|
575 res[is.na(res)] = 0
|
|
576 infer.result = infer.clonality(as.matrix(res[,2:ncol(res)]))
|
|
577
|
|
578 print(infer.result)
|
|
579
|
|
580 write.table(data.table(infer.result[[12]]), file=paste("lymphclon_clonality_", sample_id, ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=F)
|
|
581
|
|
582 res$type = rowSums(res[,2:ncol(res)])
|
|
583
|
|
584 coincidence.table = data.frame(table(res$type))
|
|
585 colnames(coincidence.table) = c("Coincidence Type", "Raw Coincidence Freq")
|
|
586 write.table(coincidence.table, file=paste("lymphclon_coincidences_", sample_id, ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
|
|
587 }
|
|
588 } else {
|
|
589 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "clonaltype")])
|
|
590
|
|
591 #write files for every coincidence group of >1
|
|
592 samples = unique(clonalFreq$Sample)
|
|
593 for(sample in samples){
|
|
594 clonalFreqSample = clonalFreq[clonalFreq$Sample == sample,]
|
|
595 if(max(clonalFreqSample$Type) > 1){
|
|
596 for(i in 2:max(clonalFreqSample$Type)){
|
|
597 clonalFreqSampleType = clonalFreqSample[clonalFreqSample$Type == i,]
|
|
598 clonalityFrame.sub = clonalityFrame[clonalityFrame$clonaltype %in% clonalFreqSampleType$clonaltype,]
|
|
599 clonalityFrame.sub = clonalityFrame.sub[order(clonalityFrame.sub$clonaltype),]
|
|
600 write.table(clonalityFrame.sub, file=paste("coincidences_", sample, "_", i, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
|
|
601 }
|
|
602 }
|
|
603 }
|
|
604
|
|
605 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
|
|
606 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
|
|
607 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
|
|
608 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
|
|
609
|
|
610 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
|
|
611 tcct = textConnection(ct)
|
|
612 CT = read.table(tcct, sep="\t", header=TRUE)
|
|
613 close(tcct)
|
|
614 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
|
|
615 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
|
|
616
|
|
617 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "clonaltype")])
|
|
618 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
|
|
619 clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
|
|
620 ReplicateReads$Reads = as.numeric(ReplicateReads$Reads)
|
|
621 ReplicateReads$squared = as.numeric(ReplicateReads$Reads * ReplicateReads$Reads)
|
|
622
|
|
623 ReplicatePrint <- function(dat){
|
|
624 write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
625 }
|
|
626
|
|
627 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
628 lapply(ReplicateSplit, FUN=ReplicatePrint)
|
|
629
|
|
630 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(as.numeric(Reads)), ReadsSquaredSum=sum(as.numeric(squared))), by=c("Sample")])
|
|
631 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
|
|
632
|
|
633 ReplicateSumPrint <- function(dat){
|
|
634 write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
635 }
|
|
636
|
|
637 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
638 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
|
|
639
|
|
640 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
|
|
641 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
|
|
642 clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
|
|
643 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
|
|
644 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
|
|
645
|
|
646 ClonalityScorePrint <- function(dat){
|
|
647 write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
648 }
|
|
649
|
|
650 clonalityScore = clonalFreqCount[c("Sample", "Result")]
|
|
651 clonalityScore = unique(clonalityScore)
|
|
652
|
|
653 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
|
|
654 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
|
|
655
|
|
656 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
|
|
657
|
|
658
|
|
659
|
|
660 ClonalityOverviewPrint <- function(dat){
|
|
661 dat = dat[order(dat[,2]),]
|
|
662 write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
663 }
|
|
664
|
|
665 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
|
|
666 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
|
|
667 }
|
|
668 }
|
|
669
|
|
670 bak = PRODF
|
|
671
|
|
672 imgtcolumns = c("X3V.REGION.trimmed.nt.nb","P3V.nt.nb", "N1.REGION.nt.nb", "P5D.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "P3D.nt.nb", "N2.REGION.nt.nb", "P5J.nt.nb", "X5J.REGION.trimmed.nt.nb", "X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb")
|
|
673 if(all(imgtcolumns %in% colnames(inputdata)))
|
|
674 {
|
|
675 print("found IMGT columns, running junction analysis")
|
|
676
|
|
677 if(locus %in% c("IGK","IGL", "TRA", "TRG")){
|
|
678 print("VJ recombination, no filtering on absent D")
|
|
679 } else {
|
|
680 print("VDJ recombination, using N column for junction analysis")
|
|
681 fltr = nchar(PRODF$Top.D.Gene) < 4
|
|
682 print(paste("Removing", sum(fltr), "sequences without a identified D"))
|
|
683 PRODF = PRODF[!fltr,]
|
|
684 }
|
|
685
|
|
686
|
|
687 #ensure certain columns are in the data (files generated with older versions of IMGT Loader)
|
|
688 col.checks = c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb")
|
|
689 for(col.check in col.checks){
|
|
690 if(!(col.check %in% names(PRODF))){
|
|
691 print(paste(col.check, "not found adding new column"))
|
|
692 if(nrow(PRODF) > 0){ #because R is anoying...
|
|
693 PRODF[,col.check] = 0
|
|
694 } else {
|
|
695 PRODF = cbind(PRODF, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
|
|
696 }
|
|
697 if(nrow(UNPROD) > 0){
|
|
698 UNPROD[,col.check] = 0
|
|
699 } else {
|
|
700 UNPROD = cbind(UNPROD, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
|
|
701 }
|
|
702 }
|
|
703 }
|
|
704
|
|
705 num_median = function(x, na.rm=T) { as.numeric(median(x, na.rm=na.rm)) }
|
|
706
|
|
707 newData = data.frame(data.table(PRODF)[,list(unique=.N,
|
|
708 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
709 P1=mean(.SD$P3V.nt.nb, na.rm=T),
|
|
710 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
|
|
711 P2=mean(.SD$P5D.nt.nb, na.rm=T),
|
|
712 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
713 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
714 P3=mean(.SD$P3D.nt.nb, na.rm=T),
|
|
715 N2=mean(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
|
716 P4=mean(.SD$P5J.nt.nb, na.rm=T),
|
|
717 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
718 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
|
|
719 Total.N=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
3
|
720 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
|
721 Median.CDR3.l=median(.SD$CDR3.Length.DNA)),
|
0
|
722 by=c("Sample")])
|
|
723 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
|
724 write.table(newData, "junctionAnalysisProd_mean.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
725
|
|
726 newData = data.frame(data.table(PRODF)[,list(unique=.N,
|
|
727 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
728 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
|
|
729 N1=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
|
|
730 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
|
|
731 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
732 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
733 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
|
|
734 N2=num_median(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
|
735 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
|
|
736 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
737 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
|
|
738 Total.N=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
3
|
739 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
|
740 Median.CDR3.l=median(.SD$CDR3.Length.DNA)),
|
0
|
741 by=c("Sample")])
|
|
742 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
|
743 write.table(newData, "junctionAnalysisProd_median.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
744
|
|
745 newData = data.frame(data.table(UNPROD)[,list(unique=.N,
|
|
746 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
747 P1=mean(.SD$P3V.nt.nb, na.rm=T),
|
|
748 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
|
|
749 P2=mean(.SD$P5D.nt.nb, na.rm=T),
|
|
750 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
751 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
752 P3=mean(.SD$P3D.nt.nb, na.rm=T),
|
|
753 N2=mean(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
|
754 P4=mean(.SD$P5J.nt.nb, na.rm=T),
|
|
755 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
756 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
|
|
757 Total.N=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
3
|
758 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
|
759 Median.CDR3.l=median(.SD$CDR3.Length.DNA)),
|
0
|
760 by=c("Sample")])
|
|
761 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
|
762 write.table(newData, "junctionAnalysisUnProd_mean.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
763
|
|
764 newData = data.frame(data.table(UNPROD)[,list(unique=.N,
|
|
765 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
766 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
|
|
767 N1=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
|
|
768 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
|
|
769 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
770 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
771 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
|
|
772 N2=num_median(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
|
773 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
|
|
774 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
775 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
|
|
776 Total.N=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
3
|
777 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
|
778 Median.CDR3.l=median(.SD$CDR3.Length.DNA)),
|
0
|
779 by=c("Sample")])
|
|
780
|
|
781 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
|
782 write.table(newData, "junctionAnalysisUnProd_median.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
783 }
|
|
784
|
|
785 PRODF = bak
|
|
786
|
|
787
|
|
788 # ---------------------- D reading frame ----------------------
|
|
789
|
|
790 D.REGION.reading.frame = PRODF$D.REGION.reading.frame
|
|
791
|
|
792 D.REGION.reading.frame[is.na(D.REGION.reading.frame)] = "No D"
|
|
793
|
|
794 D.REGION.reading.frame = data.frame(table(D.REGION.reading.frame))
|
|
795
|
|
796 write.table(D.REGION.reading.frame, "DReadingFrame.csv" , sep="\t",quote=F,row.names=F,col.names=T)
|
|
797
|
|
798 D.REGION.reading.frame = ggplot(D.REGION.reading.frame)
|
|
799 D.REGION.reading.frame = D.REGION.reading.frame + geom_bar(aes( x = D.REGION.reading.frame, y = Freq), stat='identity', position='dodge' ) + ggtitle("D reading frame") + xlab("Frequency") + ylab("Frame")
|
|
800
|
|
801 png("DReadingFrame.png")
|
|
802 D.REGION.reading.frame
|
|
803 dev.off()
|
|
804
|
|
805
|
|
806
|
|
807
|
|
808 # ---------------------- AA composition in CDR3 ----------------------
|
|
809
|
|
810 AACDR3 = PRODF[,c("Sample", "CDR3.Seq")]
|
|
811
|
|
812 TotalPerSample = data.frame(data.table(AACDR3)[, list(total=sum(nchar(as.character(.SD$CDR3.Seq)))), by=Sample])
|
|
813
|
|
814 AAfreq = list()
|
|
815
|
|
816 for(i in 1:nrow(TotalPerSample)){
|
|
817 sample = TotalPerSample$Sample[i]
|
|
818 AAfreq[[i]] = data.frame(table(unlist(strsplit(as.character(AACDR3[AACDR3$Sample == sample,c("CDR3.Seq")]), ""))))
|
|
819 AAfreq[[i]]$Sample = sample
|
|
820 }
|
|
821
|
|
822 AAfreq = ldply(AAfreq, data.frame)
|
|
823 AAfreq = merge(AAfreq, TotalPerSample, by="Sample", all.x = T)
|
|
824 AAfreq$freq_perc = as.numeric(AAfreq$Freq / AAfreq$total * 100)
|
|
825
|
|
826
|
|
827 AAorder = read.table(sep="\t", header=TRUE, text="order.aa\tAA\n1\tR\n2\tK\n3\tN\n4\tD\n5\tQ\n6\tE\n7\tH\n8\tP\n9\tY\n10\tW\n11\tS\n12\tT\n13\tG\n14\tA\n15\tM\n16\tC\n17\tF\n18\tL\n19\tV\n20\tI")
|
|
828 AAfreq = merge(AAfreq, AAorder, by.x='Var1', by.y='AA', all.x=TRUE)
|
|
829
|
|
830 AAfreq = AAfreq[!is.na(AAfreq$order.aa),]
|
|
831
|
|
832 AAfreqplot = ggplot(AAfreq)
|
|
833 AAfreqplot = AAfreqplot + geom_bar(aes( x=factor(reorder(Var1, order.aa)), y = freq_perc, fill = Sample), stat='identity', position='dodge' )
|
|
834 AAfreqplot = AAfreqplot + annotate("rect", xmin = 0.5, xmax = 2.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
|
|
835 AAfreqplot = AAfreqplot + annotate("rect", xmin = 3.5, xmax = 4.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
|
|
836 AAfreqplot = AAfreqplot + annotate("rect", xmin = 5.5, xmax = 6.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
|
|
837 AAfreqplot = AAfreqplot + annotate("rect", xmin = 6.5, xmax = 7.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
|
|
838 AAfreqplot = AAfreqplot + ggtitle("Amino Acid Composition in the CDR3") + xlab("Amino Acid, from Hydrophilic (left) to Hydrophobic (right)") + ylab("Percentage")
|
|
839
|
|
840 png("AAComposition.png",width = 1280, height = 720)
|
|
841 AAfreqplot
|
|
842 dev.off()
|
|
843 write.table(AAfreq, "AAComposition.csv" , sep=",",quote=F,na="-",row.names=F,col.names=T)
|
|
844
|
|
845
|