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