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