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