5
|
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
|
13
|
50 inputdata = read.table(infile, sep="\t", header=TRUE, fill=T, comment.char="", stringsAsFactors=F)
|
5
|
51
|
24
|
52
|
5
|
53 print(paste("nrows: ", nrow(inputdata)))
|
|
54
|
|
55 setwd(outdir)
|
|
56
|
|
57 # remove weird rows
|
|
58 inputdata = inputdata[inputdata$Sample != "",]
|
|
59
|
|
60 print(paste("nrows: ", nrow(inputdata)))
|
|
61
|
|
62 #remove the allele from the V,D and J genes
|
|
63 inputdata$Top.V.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.V.Gene)
|
|
64 inputdata$Top.D.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.D.Gene)
|
|
65 inputdata$Top.J.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.J.Gene)
|
|
66
|
|
67 print(paste("nrows: ", nrow(inputdata)))
|
|
68
|
|
69 #filter uniques
|
|
70 inputdata.removed = inputdata[NULL,]
|
|
71
|
|
72 print(paste("nrows: ", nrow(inputdata)))
|
|
73
|
|
74 inputdata$clonaltype = 1:nrow(inputdata)
|
|
75
|
|
76 #keep track of the count of sequences in samples or samples/replicates for the front page overview
|
|
77 input.sample.count = data.frame(data.table(inputdata)[, list(All=.N), by=c("Sample")])
|
|
78 input.rep.count = data.frame(data.table(inputdata)[, list(All=.N), by=c("Sample", "Replicate")])
|
|
79
|
|
80 PRODF = inputdata
|
|
81 UNPROD = inputdata
|
|
82 if(filterproductive){
|
|
83 if("Functionality" %in% colnames(inputdata)) { # "Functionality" is an IMGT column
|
|
84 #PRODF = inputdata[inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)", ]
|
|
85 PRODF = inputdata[inputdata$Functionality %in% c("productive (see comment)","productive"),]
|
|
86
|
|
87 PRODF.count = data.frame(data.table(PRODF)[, list(count=.N), by=c("Sample")])
|
|
88
|
|
89 UNPROD = inputdata[inputdata$Functionality %in% c("unproductive (see comment)","unproductive"), ]
|
|
90 } else {
|
|
91 PRODF = inputdata[inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" , ]
|
|
92 UNPROD = inputdata[!(inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" ), ]
|
|
93 }
|
|
94 }
|
|
95
|
13
|
96 for(i in 1:nrow(UNPROD)){
|
|
97 if(!is.numeric(UNPROD[i,"CDR3.Length"])){
|
|
98 UNPROD[i,"CDR3.Length"] = 0
|
|
99 }
|
|
100 }
|
|
101
|
5
|
102 prod.sample.count = data.frame(data.table(PRODF)[, list(Productive=.N), by=c("Sample")])
|
|
103 prod.rep.count = data.frame(data.table(PRODF)[, list(Productive=.N), by=c("Sample", "Replicate")])
|
|
104
|
|
105 unprod.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive=.N), by=c("Sample")])
|
|
106 unprod.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive=.N), by=c("Sample", "Replicate")])
|
|
107
|
|
108 clonalityFrame = PRODF
|
|
109
|
|
110 #remove duplicates based on the clonaltype
|
|
111 if(clonaltype != "none"){
|
|
112 clonaltype = paste(clonaltype, ",Sample", sep="") #add sample column to clonaltype, unique within samples
|
|
113 PRODF$clonaltype = do.call(paste, c(PRODF[unlist(strsplit(clonaltype, ","))], sep = ":"))
|
|
114 PRODF = PRODF[!duplicated(PRODF$clonaltype), ]
|
|
115
|
|
116 UNPROD$clonaltype = do.call(paste, c(UNPROD[unlist(strsplit(clonaltype, ","))], sep = ":"))
|
|
117 UNPROD = UNPROD[!duplicated(UNPROD$clonaltype), ]
|
|
118
|
|
119 #again for clonalityFrame but with sample+replicate
|
|
120 clonalityFrame$clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(clonaltype, ","))], sep = ":"))
|
|
121 clonalityFrame$clonality_clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(paste(clonaltype, ",Replicate", sep=""), ","))], sep = ":"))
|
|
122 clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$clonality_clonaltype), ]
|
|
123 }
|
|
124
|
40
|
125 if(nrow(PRODF) == 0){
|
|
126 stop("No sequences left after filtering")
|
|
127 }
|
|
128
|
5
|
129 prod.unique.sample.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample")])
|
|
130 prod.unique.rep.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample", "Replicate")])
|
|
131
|
|
132 unprod.unique.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample")])
|
|
133 unprod.unique.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample", "Replicate")])
|
|
134
|
40
|
135
|
5
|
136 PRODF$freq = 1
|
|
137
|
|
138 if(any(grepl(pattern="_", x=PRODF$ID))){ #the frequency can be stored in the ID with the pattern ".*_freq_.*"
|
|
139 PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
|
|
140 PRODF$freq = gsub("_.*", "", PRODF$freq)
|
|
141 PRODF$freq = as.numeric(PRODF$freq)
|
|
142 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
|
|
143 PRODF$freq = 1
|
|
144 }
|
|
145 }
|
|
146
|
8
|
147 #make a names list with sample -> color
|
|
148 naive.colors = c('blue4', 'darkred', 'olivedrab3', 'red', 'gray74', 'darkviolet', 'lightblue1', 'gold', 'chartreuse2', 'pink', 'Paleturquoise3', 'Chocolate1', 'Yellow', 'Deeppink3', 'Mediumorchid1', 'Darkgreen', 'Blue', 'Gray36', 'Hotpink', 'Yellow4')
|
|
149 unique.samples = unique(PRODF$Sample)
|
|
150
|
|
151 if(length(unique.samples) <= length(naive.colors)){
|
|
152 sample.colors = naive.colors[1:length(unique.samples)]
|
|
153 } else {
|
|
154 sample.colors = rainbow(length(unique.samples))
|
|
155 }
|
|
156
|
|
157 names(sample.colors) = unique.samples
|
|
158
|
|
159 print("Sample.colors")
|
|
160 print(sample.colors)
|
5
|
161
|
|
162
|
|
163 #write the complete dataset that is left over, will be the input if 'none' for clonaltype and 'no' for filterproductive
|
|
164 write.table(PRODF, "allUnique.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
18
|
165 #write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
|
25
|
166 write.table(UNPROD, "allUnproductive.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
167
|
24
|
168 print("SAMPLE TABLE:")
|
|
169 print(table(PRODF$Sample))
|
|
170
|
5
|
171 #write the samples to a file
|
|
172 sampleFile <- file("samples.txt")
|
|
173 un = unique(inputdata$Sample)
|
|
174 un = paste(un, sep="\n")
|
|
175 writeLines(un, sampleFile)
|
|
176 close(sampleFile)
|
|
177
|
|
178 # ---------------------- Counting the productive/unproductive and unique sequences ----------------------
|
|
179
|
|
180 print("Report Clonality - counting productive/unproductive/unique")
|
|
181
|
|
182 #create the table on the overview page with the productive/unique counts per sample/replicate
|
|
183 #first for sample
|
40
|
184
|
5
|
185 sample.count = merge(input.sample.count, prod.sample.count, by="Sample", all.x=T)
|
|
186 sample.count$perc_prod = round(sample.count$Productive / sample.count$All * 100)
|
|
187 sample.count = merge(sample.count, prod.unique.sample.count, by="Sample", all.x=T)
|
|
188 sample.count$perc_prod_un = round(sample.count$Productive_unique / sample.count$All * 100)
|
|
189
|
|
190 sample.count = merge(sample.count , unprod.sample.count, by="Sample", all.x=T)
|
|
191 sample.count$perc_unprod = round(sample.count$Unproductive / sample.count$All * 100)
|
|
192 sample.count = merge(sample.count, unprod.unique.sample.count, by="Sample", all.x=T)
|
|
193 sample.count$perc_unprod_un = round(sample.count$Unproductive_unique / sample.count$All * 100)
|
|
194
|
|
195 #then sample/replicate
|
|
196 rep.count = merge(input.rep.count, prod.rep.count, by=c("Sample", "Replicate"), all.x=T)
|
40
|
197
|
|
198 print(rep.count)
|
|
199
|
|
200 fltr = is.na(rep.count$Productive)
|
|
201 if(any(fltr)){
|
|
202 rep.count[fltr,"Productive"] = 0
|
|
203 }
|
|
204
|
|
205 print(rep.count)
|
|
206
|
5
|
207 rep.count$perc_prod = round(rep.count$Productive / rep.count$All * 100)
|
|
208 rep.count = merge(rep.count, prod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
|
|
209 rep.count$perc_prod_un = round(rep.count$Productive_unique / rep.count$All * 100)
|
|
210
|
|
211 rep.count = merge(rep.count, unprod.rep.count, by=c("Sample", "Replicate"), all.x=T)
|
|
212 rep.count$perc_unprod = round(rep.count$Unproductive / rep.count$All * 100)
|
|
213 rep.count = merge(rep.count, unprod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
|
|
214 rep.count$perc_unprod_un = round(rep.count$Unproductive_unique / rep.count$All * 100)
|
|
215
|
|
216 rep.count$Sample = paste(rep.count$Sample, rep.count$Replicate, sep="_")
|
|
217 rep.count = rep.count[,names(rep.count) != "Replicate"]
|
|
218
|
|
219 count = rbind(sample.count, rep.count)
|
|
220
|
|
221
|
|
222
|
|
223 write.table(x=count, file="productive_counting.txt", sep=",",quote=F,row.names=F,col.names=F)
|
|
224
|
|
225 # ---------------------- V+J+CDR3 sequence count ----------------------
|
|
226
|
|
227 VJCDR3.count = data.frame(table(clonalityFrame$Top.V.Gene, clonalityFrame$Top.J.Gene, clonalityFrame$CDR3.Seq.DNA))
|
|
228 names(VJCDR3.count) = c("Top.V.Gene", "Top.J.Gene", "CDR3.Seq.DNA", "Count")
|
|
229
|
|
230 VJCDR3.count = VJCDR3.count[VJCDR3.count$Count > 0,]
|
|
231 VJCDR3.count = VJCDR3.count[order(-VJCDR3.count$Count),]
|
|
232
|
|
233 write.table(x=VJCDR3.count, file="VJCDR3_count.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
|
234
|
|
235 # ---------------------- Frequency calculation for V, D and J ----------------------
|
|
236
|
|
237 print("Report Clonality - frequency calculation V, D and J")
|
|
238
|
|
239 PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
|
|
240 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
241 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
242 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
243
|
|
244 PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
|
|
245 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
246 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
247 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
248
|
|
249 PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
|
|
250 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
251 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
252 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
253
|
|
254 # ---------------------- Setting up the gene names for the different species/loci ----------------------
|
|
255
|
|
256 print("Report Clonality - getting genes for species/loci")
|
|
257
|
|
258 Vchain = ""
|
|
259 Dchain = ""
|
|
260 Jchain = ""
|
|
261
|
|
262 if(species == "custom"){
|
|
263 print("Custom genes: ")
|
|
264 splt = unlist(strsplit(locus, ";"))
|
|
265 print(paste("V:", splt[1]))
|
|
266 print(paste("D:", splt[2]))
|
|
267 print(paste("J:", splt[3]))
|
|
268
|
|
269 Vchain = unlist(strsplit(splt[1], ","))
|
|
270 Vchain = data.frame(v.name = Vchain, chr.orderV = 1:length(Vchain))
|
|
271
|
|
272 Dchain = unlist(strsplit(splt[2], ","))
|
|
273 if(length(Dchain) > 0){
|
|
274 Dchain = data.frame(v.name = Dchain, chr.orderD = 1:length(Dchain))
|
|
275 } else {
|
|
276 Dchain = data.frame(v.name = character(0), chr.orderD = numeric(0))
|
|
277 }
|
|
278
|
|
279 Jchain = unlist(strsplit(splt[3], ","))
|
|
280 Jchain = data.frame(v.name = Jchain, chr.orderJ = 1:length(Jchain))
|
|
281
|
|
282 } else {
|
|
283 genes = read.table("genes.txt", sep="\t", header=TRUE, fill=T, comment.char="")
|
|
284
|
|
285 Vchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "V",c("IMGT.GENE.DB", "chr.order")]
|
|
286 colnames(Vchain) = c("v.name", "chr.orderV")
|
|
287 Dchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "D",c("IMGT.GENE.DB", "chr.order")]
|
|
288 colnames(Dchain) = c("v.name", "chr.orderD")
|
|
289 Jchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "J",c("IMGT.GENE.DB", "chr.order")]
|
|
290 colnames(Jchain) = c("v.name", "chr.orderJ")
|
|
291 }
|
|
292 useD = TRUE
|
|
293 if(nrow(Dchain) == 0){
|
|
294 useD = FALSE
|
|
295 cat("No D Genes in this species/locus")
|
|
296 }
|
|
297 print(paste(nrow(Vchain), "genes in V"))
|
|
298 print(paste(nrow(Dchain), "genes in D"))
|
|
299 print(paste(nrow(Jchain), "genes in J"))
|
|
300
|
|
301 # ---------------------- merge with the frequency count ----------------------
|
|
302
|
|
303 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
|
|
304
|
|
305 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
|
|
306
|
|
307 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
|
|
308
|
|
309 # ---------------------- Create the V, D and J frequency plots and write the data.frame for every plot to a file ----------------------
|
|
310
|
|
311 print("Report Clonality - V, D and J frequency plots")
|
|
312
|
|
313 pV = ggplot(PRODFV)
|
|
314 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))
|
8
|
315 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage") + scale_fill_manual(values=sample.colors)
|
|
316 pV = pV + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
|
18
|
317 write.table(x=PRODFV, file="VFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
318
|
|
319 png("VPlot.png",width = 1280, height = 720)
|
|
320 pV
|
32
|
321 dev.off()
|
|
322
|
|
323 ggsave("VPlot.pdf", pV, width=13, height=7)
|
5
|
324
|
|
325 if(useD){
|
|
326 pD = ggplot(PRODFD)
|
|
327 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))
|
8
|
328 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage") + scale_fill_manual(values=sample.colors)
|
|
329 pD = pD + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
|
18
|
330 write.table(x=PRODFD, file="DFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
331
|
|
332 png("DPlot.png",width = 800, height = 600)
|
|
333 print(pD)
|
32
|
334 dev.off()
|
|
335
|
|
336 ggsave("DPlot.pdf", pD, width=10, height=7)
|
5
|
337 }
|
|
338
|
|
339 pJ = ggplot(PRODFJ)
|
|
340 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))
|
8
|
341 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage") + scale_fill_manual(values=sample.colors)
|
|
342 pJ = pJ + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
|
18
|
343 write.table(x=PRODFJ, file="JFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
344
|
|
345 png("JPlot.png",width = 800, height = 600)
|
|
346 pJ
|
32
|
347 dev.off()
|
|
348
|
|
349 ggsave("JPlot.pdf", pJ)
|
5
|
350
|
|
351 # ---------------------- Now the frequency plots of the V, D and J families ----------------------
|
|
352
|
|
353 print("Report Clonality - V, D and J family plots")
|
|
354
|
|
355 VGenes = PRODF[,c("Sample", "Top.V.Gene")]
|
|
356 VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
|
|
357 VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
|
|
358 TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
359 VGenes = merge(VGenes, TotalPerSample, by="Sample")
|
|
360 VGenes$Frequency = VGenes$Count * 100 / VGenes$total
|
|
361 VPlot = ggplot(VGenes)
|
|
362 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)) +
|
|
363 ggtitle("Distribution of V gene families") +
|
8
|
364 ylab("Percentage of sequences") +
|
|
365 scale_fill_manual(values=sample.colors) +
|
|
366 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
|
5
|
367 png("VFPlot.png")
|
|
368 VPlot
|
32
|
369 dev.off()
|
|
370 ggsave("VFPlot.pdf", VPlot)
|
|
371
|
18
|
372 write.table(x=VGenes, file="VFFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
373
|
|
374 if(useD){
|
|
375 DGenes = PRODF[,c("Sample", "Top.D.Gene")]
|
|
376 DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
|
|
377 DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
|
|
378 TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
379 DGenes = merge(DGenes, TotalPerSample, by="Sample")
|
|
380 DGenes$Frequency = DGenes$Count * 100 / DGenes$total
|
|
381 DPlot = ggplot(DGenes)
|
|
382 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)) +
|
|
383 ggtitle("Distribution of D gene families") +
|
8
|
384 ylab("Percentage of sequences") +
|
|
385 scale_fill_manual(values=sample.colors) +
|
|
386 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
|
5
|
387 png("DFPlot.png")
|
|
388 print(DPlot)
|
32
|
389 dev.off()
|
|
390
|
|
391 ggsave("DFPlot.pdf", DPlot)
|
18
|
392 write.table(x=DGenes, file="DFFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
393 }
|
|
394
|
|
395 # ---------------------- Plotting the cdr3 length ----------------------
|
|
396
|
|
397 print("Report Clonality - CDR3 length plot")
|
|
398
|
9
|
399 CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length")])
|
5
|
400 TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
|
|
401 CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
|
|
402 CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
|
|
403 CDR3LengthPlot = ggplot(CDR3Length)
|
15
|
404 CDR3LengthPlot = CDR3LengthPlot + geom_bar(aes( x = factor(reorder(CDR3.Length, as.numeric(CDR3.Length))), y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
5
|
405 ggtitle("Length distribution of CDR3") +
|
|
406 xlab("CDR3 Length") +
|
8
|
407 ylab("Percentage of sequences") +
|
|
408 scale_fill_manual(values=sample.colors) +
|
|
409 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
|
5
|
410 png("CDR3LengthPlot.png",width = 1280, height = 720)
|
|
411 CDR3LengthPlot
|
|
412 dev.off()
|
32
|
413
|
|
414 ggsave("CDR3LengthPlot.pdf", CDR3LengthPlot, width=12, height=7)
|
|
415
|
24
|
416 write.table(x=CDR3Length, file="CDR3LengthPlot.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
417
|
|
418 # ---------------------- Plot the heatmaps ----------------------
|
|
419
|
|
420 #get the reverse order for the V and D genes
|
|
421 revVchain = Vchain
|
|
422 revDchain = Dchain
|
|
423 revVchain$chr.orderV = rev(revVchain$chr.orderV)
|
|
424 revDchain$chr.orderD = rev(revDchain$chr.orderD)
|
|
425
|
|
426 if(useD){
|
|
427 print("Report Clonality - Heatmaps VD")
|
|
428 plotVD <- function(dat){
|
|
429 if(length(dat[,1]) == 0){
|
|
430 return()
|
|
431 }
|
|
432
|
|
433 img = ggplot() +
|
|
434 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), 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("D genes") +
|
9
|
439 ylab("V Genes") +
|
14
|
440 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), panel.grid.major = element_line(colour = "gainsboro"))
|
5
|
441
|
|
442 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
443 print(img)
|
|
444 dev.off()
|
32
|
445
|
|
446 ggsave(paste("HeatmapVD_", unique(dat[3])[1,1] , ".pdf", sep=""), img, height=13, width=8)
|
|
447
|
24
|
448 write.table(x=acast(dat, Top.V.Gene~Top.D.Gene, value.var="Length"), file=paste("HeatmapVD_", unique(dat[3])[1,1], ".txt", sep=""), sep="\t",quote=F,row.names=T,col.names=NA)
|
5
|
449 }
|
|
450
|
|
451 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
|
|
452
|
|
453 VandDCount$l = log(VandDCount$Length)
|
|
454 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
|
|
455 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
|
|
456 VandDCount$relLength = VandDCount$l / VandDCount$max
|
6
|
457 check = is.nan(VandDCount$relLength)
|
|
458 if(any(check)){
|
|
459 VandDCount[check,"relLength"] = 0
|
|
460 }
|
5
|
461
|
|
462 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name)
|
|
463
|
|
464 completeVD = merge(VandDCount, cartegianProductVD, by.x=c("Top.V.Gene", "Top.D.Gene"), by.y=c("Top.V.Gene", "Top.D.Gene"), all=TRUE)
|
|
465
|
|
466 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
467
|
|
468 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
469
|
|
470 fltr = is.nan(completeVD$relLength)
|
|
471 if(all(fltr)){
|
|
472 completeVD[fltr,"relLength"] = 0
|
|
473 }
|
|
474
|
|
475 VDList = split(completeVD, f=completeVD[,"Sample"])
|
|
476 lapply(VDList, FUN=plotVD)
|
|
477 }
|
|
478
|
|
479 print("Report Clonality - Heatmaps VJ")
|
|
480
|
|
481 plotVJ <- function(dat){
|
|
482 if(length(dat[,1]) == 0){
|
|
483 return()
|
|
484 }
|
|
485 cat(paste(unique(dat[3])[1,1]))
|
|
486 img = ggplot() +
|
|
487 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
|
488 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
489 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
490 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
491 xlab("J genes") +
|
9
|
492 ylab("V Genes") +
|
14
|
493 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), panel.grid.major = element_line(colour = "gainsboro"))
|
5
|
494
|
|
495 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
496 print(img)
|
|
497 dev.off()
|
32
|
498
|
|
499 ggsave(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".pdf", sep=""), img, height=11, width=4)
|
|
500
|
24
|
501 write.table(x=acast(dat, Top.V.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapVJ_", unique(dat[3])[1,1], ".txt", sep=""), sep="\t",quote=F,row.names=T,col.names=NA)
|
5
|
502 }
|
|
503
|
|
504 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
|
|
505
|
|
506 VandJCount$l = log(VandJCount$Length)
|
|
507 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
508 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
509 VandJCount$relLength = VandJCount$l / VandJCount$max
|
|
510
|
6
|
511 check = is.nan(VandJCount$relLength)
|
|
512 if(any(check)){
|
|
513 VandJCount[check,"relLength"] = 0
|
|
514 }
|
|
515
|
5
|
516 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name)
|
|
517
|
|
518 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
|
|
519 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
520 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
521
|
|
522 fltr = is.nan(completeVJ$relLength)
|
|
523 if(any(fltr)){
|
|
524 completeVJ[fltr,"relLength"] = 1
|
|
525 }
|
|
526
|
|
527 VJList = split(completeVJ, f=completeVJ[,"Sample"])
|
|
528 lapply(VJList, FUN=plotVJ)
|
|
529
|
|
530
|
|
531
|
|
532 if(useD){
|
|
533 print("Report Clonality - Heatmaps DJ")
|
|
534 plotDJ <- function(dat){
|
|
535 if(length(dat[,1]) == 0){
|
|
536 return()
|
|
537 }
|
|
538 img = ggplot() +
|
|
539 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
|
|
540 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
541 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
542 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
543 xlab("J genes") +
|
9
|
544 ylab("D Genes") +
|
14
|
545 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), panel.grid.major = element_line(colour = "gainsboro"))
|
5
|
546
|
|
547 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
|
|
548 print(img)
|
|
549 dev.off()
|
32
|
550
|
|
551 ggsave(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".pdf", sep=""), img, width=4, height=7)
|
|
552
|
24
|
553 write.table(x=acast(dat, Top.D.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapDJ_", unique(dat[3])[1,1], ".txt", sep=""), sep="\t",quote=F,row.names=T,col.names=NA)
|
5
|
554 }
|
|
555
|
|
556
|
|
557 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
|
|
558
|
|
559 DandJCount$l = log(DandJCount$Length)
|
|
560 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
561 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
562 DandJCount$relLength = DandJCount$l / DandJCount$max
|
|
563
|
6
|
564 check = is.nan(DandJCount$relLength)
|
|
565 if(any(check)){
|
|
566 DandJCount[check,"relLength"] = 0
|
|
567 }
|
|
568
|
5
|
569 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name)
|
|
570
|
|
571 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
|
|
572 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
573 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
574
|
|
575 fltr = is.nan(completeDJ$relLength)
|
|
576 if(any(fltr)){
|
|
577 completeDJ[fltr, "relLength"] = 1
|
|
578 }
|
|
579
|
|
580 DJList = split(completeDJ, f=completeDJ[,"Sample"])
|
|
581 lapply(DJList, FUN=plotDJ)
|
|
582 }
|
|
583
|
|
584
|
|
585 # ---------------------- output tables for the circos plots ----------------------
|
|
586
|
|
587 print("Report Clonality - Circos data")
|
|
588
|
|
589 for(smpl in unique(PRODF$Sample)){
|
|
590 PRODF.sample = PRODF[PRODF$Sample == smpl,]
|
|
591
|
|
592 fltr = PRODF.sample$Top.V.Gene == ""
|
|
593 if(any(fltr, na.rm=T)){
|
|
594 PRODF.sample[fltr, "Top.V.Gene"] = "NA"
|
|
595 }
|
|
596
|
|
597 fltr = PRODF.sample$Top.D.Gene == ""
|
|
598 if(any(fltr, na.rm=T)){
|
|
599 PRODF.sample[fltr, "Top.D.Gene"] = "NA"
|
|
600 }
|
|
601
|
|
602 fltr = PRODF.sample$Top.J.Gene == ""
|
|
603 if(any(fltr, na.rm=T)){
|
|
604 PRODF.sample[fltr, "Top.J.Gene"] = "NA"
|
|
605 }
|
|
606
|
|
607 v.d = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.D.Gene)
|
|
608 v.j = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.J.Gene)
|
|
609 d.j = table(PRODF.sample$Top.D.Gene, PRODF.sample$Top.J.Gene)
|
|
610
|
|
611 write.table(v.d, file=paste(smpl, "_VD_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
|
|
612 write.table(v.j, file=paste(smpl, "_VJ_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
|
|
613 write.table(d.j, file=paste(smpl, "_DJ_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
|
|
614 }
|
|
615
|
|
616 # ---------------------- calculating the clonality score ----------------------
|
|
617
|
|
618 if("Replicate" %in% colnames(inputdata)) #can only calculate clonality score when replicate information is available
|
|
619 {
|
|
620 print("Report Clonality - Clonality")
|
18
|
621 write.table(clonalityFrame, "clonalityComplete.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
622 if(clonality_method == "boyd"){
|
|
623 samples = split(clonalityFrame, clonalityFrame$Sample, drop=T)
|
|
624
|
|
625 for (sample in samples){
|
|
626 res = data.frame(paste=character(0))
|
|
627 sample_id = unique(sample$Sample)[[1]]
|
|
628 for(replicate in unique(sample$Replicate)){
|
|
629 tmp = sample[sample$Replicate == replicate,]
|
|
630 clone_table = data.frame(table(tmp$clonaltype))
|
|
631 clone_col_name = paste("V", replicate, sep="")
|
|
632 colnames(clone_table) = c("paste", clone_col_name)
|
|
633 res = merge(res, clone_table, by="paste", all=T)
|
|
634 }
|
|
635
|
17
|
636 res[is.na(res)] = 0
|
|
637
|
20
|
638 write.table(res, file=paste("raw_clonality_", sample_id, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=F)
|
|
639 write.table(as.matrix(res[,2:ncol(res)]), file=paste("raw_clonality2_", sample_id, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=F)
|
|
640
|
|
641 res = read.table(paste("raw_clonality_", sample_id, ".txt", sep=""), header=F, sep="\t", quote="", stringsAsFactors=F, fill=T, comment.char="")
|
17
|
642
|
5
|
643 infer.result = infer.clonality(as.matrix(res[,2:ncol(res)]))
|
|
644
|
13
|
645 #print(infer.result)
|
5
|
646
|
20
|
647 write.table(data.table(infer.result[[12]]), file=paste("lymphclon_clonality_", sample_id, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=F)
|
5
|
648
|
|
649 res$type = rowSums(res[,2:ncol(res)])
|
|
650
|
|
651 coincidence.table = data.frame(table(res$type))
|
|
652 colnames(coincidence.table) = c("Coincidence Type", "Raw Coincidence Freq")
|
20
|
653 write.table(coincidence.table, file=paste("lymphclon_coincidences_", sample_id, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
654 }
|
26
|
655 }
|
|
656 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "clonaltype")])
|
|
657
|
|
658 #write files for every coincidence group of >1
|
|
659 samples = unique(clonalFreq$Sample)
|
|
660 for(sample in samples){
|
|
661 clonalFreqSample = clonalFreq[clonalFreq$Sample == sample,]
|
|
662 if(max(clonalFreqSample$Type) > 1){
|
|
663 for(i in 2:max(clonalFreqSample$Type)){
|
|
664 clonalFreqSampleType = clonalFreqSample[clonalFreqSample$Type == i,]
|
|
665 clonalityFrame.sub = clonalityFrame[clonalityFrame$clonaltype %in% clonalFreqSampleType$clonaltype,]
|
|
666 clonalityFrame.sub = clonalityFrame.sub[order(clonalityFrame.sub$clonaltype),]
|
|
667 write.table(clonalityFrame.sub, file=paste("coincidences_", sample, "_", i, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
|
|
668 }
|
|
669 }
|
|
670 }
|
|
671
|
|
672 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
|
|
673 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
|
|
674 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
|
|
675 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
|
|
676
|
|
677 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
|
|
678 tcct = textConnection(ct)
|
|
679 CT = read.table(tcct, sep="\t", header=TRUE)
|
|
680 close(tcct)
|
|
681 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
|
|
682 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
|
|
683
|
|
684 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "clonaltype")])
|
|
685 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
|
|
686 clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
|
|
687 ReplicateReads$Reads = as.numeric(ReplicateReads$Reads)
|
|
688 ReplicateReads$squared = as.numeric(ReplicateReads$Reads * ReplicateReads$Reads)
|
|
689
|
|
690 ReplicatePrint <- function(dat){
|
|
691 write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".txt", sep=""), sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
5
|
692 }
|
26
|
693
|
|
694 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
695 lapply(ReplicateSplit, FUN=ReplicatePrint)
|
|
696
|
|
697 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(as.numeric(Reads)), ReadsSquaredSum=sum(as.numeric(squared))), by=c("Sample")])
|
|
698 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
|
|
699
|
|
700 ReplicateSumPrint <- function(dat){
|
|
701 write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".txt", sep=""), sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
|
702 }
|
|
703
|
|
704 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
705 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
|
|
706
|
|
707 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
|
|
708 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
|
|
709 clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
|
|
710 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
|
|
711 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
|
|
712
|
|
713 ClonalityScorePrint <- function(dat){
|
|
714 write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".txt", sep=""), sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
|
715 }
|
|
716
|
|
717 clonalityScore = clonalFreqCount[c("Sample", "Result")]
|
|
718 clonalityScore = unique(clonalityScore)
|
|
719
|
|
720 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
|
|
721 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
|
|
722
|
|
723 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
|
|
724
|
|
725
|
|
726
|
|
727 ClonalityOverviewPrint <- function(dat){
|
|
728 dat = dat[order(dat[,2]),]
|
|
729 write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".txt", sep=""), sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
|
730 }
|
|
731
|
|
732 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
|
|
733 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
|
|
734
|
5
|
735 }
|
|
736
|
|
737 bak = PRODF
|
25
|
738 bakun = UNPROD
|
5
|
739
|
|
740 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")
|
|
741 if(all(imgtcolumns %in% colnames(inputdata)))
|
|
742 {
|
|
743 print("found IMGT columns, running junction analysis")
|
24
|
744
|
5
|
745 #ensure certain columns are in the data (files generated with older versions of IMGT Loader)
|
|
746 col.checks = c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb")
|
|
747 for(col.check in col.checks){
|
|
748 if(!(col.check %in% names(PRODF))){
|
|
749 print(paste(col.check, "not found adding new column"))
|
|
750 if(nrow(PRODF) > 0){ #because R is anoying...
|
|
751 PRODF[,col.check] = 0
|
|
752 } else {
|
|
753 PRODF = cbind(PRODF, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
|
|
754 }
|
|
755 if(nrow(UNPROD) > 0){
|
|
756 UNPROD[,col.check] = 0
|
|
757 } else {
|
|
758 UNPROD = cbind(UNPROD, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
|
|
759 }
|
|
760 }
|
|
761 }
|
|
762
|
24
|
763 PRODF.with.D = PRODF[nchar(PRODF$Top.D.Gene, keepNA=F) > 2,]
|
|
764 PRODF.no.D = PRODF[nchar(PRODF$Top.D.Gene, keepNA=F) < 4,]
|
26
|
765 write.table(PRODF.no.D, "productive_no_D.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=T)
|
24
|
766
|
25
|
767 UNPROD.with.D = UNPROD[nchar(UNPROD$Top.D.Gene, keepNA=F) > 2,]
|
|
768 UNPROD.no.D = UNPROD[nchar(UNPROD$Top.D.Gene, keepNA=F) < 4,]
|
26
|
769 write.table(UNPROD.no.D, "unproductive_no_D.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=T)
|
25
|
770
|
5
|
771 num_median = function(x, na.rm=T) { as.numeric(median(x, na.rm=na.rm)) }
|
25
|
772
|
24
|
773 newData = data.frame(data.table(PRODF.with.D)[,list(unique=.N,
|
5
|
774 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
775 P1=mean(.SD$P3V.nt.nb, na.rm=T),
|
|
776 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
|
|
777 P2=mean(.SD$P5D.nt.nb, na.rm=T),
|
|
778 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
779 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
780 P3=mean(.SD$P3D.nt.nb, na.rm=T),
|
|
781 N2=mean(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
|
782 P4=mean(.SD$P5J.nt.nb, na.rm=T),
|
|
783 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
784 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)),
|
|
785 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)),
|
|
786 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
38
|
787 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
|
5
|
788 by=c("Sample")])
|
|
789 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
24
|
790 write.table(newData, "junctionAnalysisProd_mean_wD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
5
|
791
|
24
|
792 newData = data.frame(data.table(PRODF.with.D)[,list(unique=.N,
|
5
|
793 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
794 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
|
|
795 N1=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
|
|
796 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
|
|
797 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
798 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
799 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
|
|
800 N2=num_median(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
|
801 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
|
|
802 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
803 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)),
|
|
804 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)),
|
|
805 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
38
|
806 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
|
5
|
807 by=c("Sample")])
|
|
808 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
24
|
809 write.table(newData, "junctionAnalysisProd_median_wD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
5
|
810
|
25
|
811 newData = data.frame(data.table(UNPROD.with.D)[,list(unique=.N,
|
5
|
812 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
813 P1=mean(.SD$P3V.nt.nb, na.rm=T),
|
|
814 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
|
|
815 P2=mean(.SD$P5D.nt.nb, na.rm=T),
|
|
816 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
817 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
818 P3=mean(.SD$P3D.nt.nb, na.rm=T),
|
|
819 N2=mean(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
|
820 P4=mean(.SD$P5J.nt.nb, na.rm=T),
|
|
821 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
822 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)),
|
|
823 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)),
|
|
824 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
38
|
825 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
|
5
|
826 by=c("Sample")])
|
|
827 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
24
|
828 write.table(newData, "junctionAnalysisUnProd_mean_wD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
5
|
829
|
25
|
830 newData = data.frame(data.table(UNPROD.with.D)[,list(unique=.N,
|
5
|
831 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
832 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
|
|
833 N1=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
|
|
834 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
|
|
835 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
836 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
837 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
|
|
838 N2=num_median(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
|
|
839 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
|
|
840 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
841 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)),
|
|
842 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)),
|
|
843 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
38
|
844 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
|
5
|
845 by=c("Sample")])
|
24
|
846 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
|
847 write.table(newData, "junctionAnalysisUnProd_median_wD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
|
848
|
|
849 #---------------- again for no-D
|
|
850
|
|
851 newData = data.frame(data.table(PRODF.no.D)[,list(unique=.N,
|
|
852 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
853 P1=mean(.SD$P3V.nt.nb, na.rm=T),
|
26
|
854 N1=mean(.SD$N.REGION.nt.nb, na.rm=T),
|
24
|
855 P2=mean(.SD$P5J.nt.nb, na.rm=T),
|
|
856 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
857 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
|
26
|
858 Total.N=mean(.SD$N.REGION.nt.nb, na.rm=T),
|
24
|
859 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
38
|
860 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
|
24
|
861 by=c("Sample")])
|
5
|
862 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
24
|
863 write.table(newData, "junctionAnalysisProd_mean_nD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
|
864
|
|
865 newData = data.frame(data.table(PRODF.no.D)[,list(unique=.N,
|
|
866 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
867 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
|
30
|
868 N1=num_median(.SD$N.REGION.nt.nb, na.rm=T),
|
24
|
869 P2=num_median(.SD$P5J.nt.nb, na.rm=T),
|
|
870 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
871 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
|
30
|
872 Total.N=num_median(.SD$N.REGION.nt.nb, na.rm=T),
|
24
|
873 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
38
|
874 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
|
24
|
875 by=c("Sample")])
|
|
876 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
|
877 write.table(newData, "junctionAnalysisProd_median_nD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
|
878
|
25
|
879 newData = data.frame(data.table(UNPROD.no.D)[,list(unique=.N,
|
24
|
880 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
881 P1=mean(.SD$P3V.nt.nb, na.rm=T),
|
26
|
882 N1=mean(.SD$N.REGION.nt.nb, na.rm=T),
|
24
|
883 P2=mean(.SD$P5J.nt.nb, na.rm=T),
|
|
884 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
885 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
|
26
|
886 Total.N=mean(.SD$N.REGION.nt.nb, na.rm=T),
|
24
|
887 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
38
|
888 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
|
24
|
889 by=c("Sample")])
|
|
890 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
|
891 write.table(newData, "junctionAnalysisUnProd_mean_nD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
|
892
|
26
|
893
|
25
|
894 newData = data.frame(data.table(UNPROD.no.D)[,list(unique=.N,
|
24
|
895 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
896 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
|
30
|
897 N1=num_median(.SD$N.REGION.nt.nb, na.rm=T),
|
24
|
898 P2=num_median(.SD$P5J.nt.nb, na.rm=T),
|
|
899 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
900 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
|
30
|
901 Total.N=num_median(.SD$N.REGION.nt.nb, na.rm=T),
|
24
|
902 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
|
38
|
903 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
|
24
|
904 by=c("Sample")])
|
|
905 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
|
|
906 write.table(newData, "junctionAnalysisUnProd_median_nD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
5
|
907 }
|
|
908
|
|
909 PRODF = bak
|
25
|
910 UNPROD = bakun
|
5
|
911
|
|
912
|
|
913 # ---------------------- D reading frame ----------------------
|
|
914
|
8
|
915 D.REGION.reading.frame = PRODF[,c("Sample", "D.REGION.reading.frame")]
|
5
|
916
|
8
|
917 chck = is.na(D.REGION.reading.frame$D.REGION.reading.frame)
|
|
918 if(any(chck)){
|
|
919 D.REGION.reading.frame[chck,"D.REGION.reading.frame"] = "No D"
|
|
920 }
|
5
|
921
|
24
|
922 D.REGION.reading.frame.1 = data.frame(data.table(D.REGION.reading.frame)[, list(Freq=.N), by=c("Sample", "D.REGION.reading.frame")])
|
|
923
|
|
924 D.REGION.reading.frame.2 = data.frame(data.table(D.REGION.reading.frame)[, list(sample.sum=sum(as.numeric(.SD$D.REGION.reading.frame), na.rm=T)), by=c("Sample")])
|
5
|
925
|
24
|
926 D.REGION.reading.frame = merge(D.REGION.reading.frame.1, D.REGION.reading.frame.2, by="Sample")
|
|
927
|
|
928 D.REGION.reading.frame$percentage = round(D.REGION.reading.frame$Freq / D.REGION.reading.frame$sample.sum * 100, 1)
|
|
929
|
|
930 write.table(D.REGION.reading.frame, "DReadingFrame.txt" , sep="\t",quote=F,row.names=F,col.names=T)
|
5
|
931
|
|
932 D.REGION.reading.frame = ggplot(D.REGION.reading.frame)
|
29
|
933 D.REGION.reading.frame = D.REGION.reading.frame + geom_bar(aes( x = D.REGION.reading.frame, y = percentage, fill=Sample), stat='identity', position='dodge' ) + ggtitle("D reading frame") + xlab("Frame") + ylab("Frequency")
|
8
|
934 D.REGION.reading.frame = D.REGION.reading.frame + scale_fill_manual(values=sample.colors)
|
|
935 D.REGION.reading.frame = D.REGION.reading.frame + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
|
5
|
936
|
|
937 png("DReadingFrame.png")
|
|
938 D.REGION.reading.frame
|
|
939 dev.off()
|
|
940
|
32
|
941 ggsave("DReadingFrame.pdf", D.REGION.reading.frame)
|
5
|
942
|
|
943 # ---------------------- AA composition in CDR3 ----------------------
|
|
944
|
|
945 AACDR3 = PRODF[,c("Sample", "CDR3.Seq")]
|
|
946
|
|
947 TotalPerSample = data.frame(data.table(AACDR3)[, list(total=sum(nchar(as.character(.SD$CDR3.Seq)))), by=Sample])
|
|
948
|
|
949 AAfreq = list()
|
|
950
|
|
951 for(i in 1:nrow(TotalPerSample)){
|
|
952 sample = TotalPerSample$Sample[i]
|
|
953 AAfreq[[i]] = data.frame(table(unlist(strsplit(as.character(AACDR3[AACDR3$Sample == sample,c("CDR3.Seq")]), ""))))
|
|
954 AAfreq[[i]]$Sample = sample
|
|
955 }
|
|
956
|
|
957 AAfreq = ldply(AAfreq, data.frame)
|
|
958 AAfreq = merge(AAfreq, TotalPerSample, by="Sample", all.x = T)
|
|
959 AAfreq$freq_perc = as.numeric(AAfreq$Freq / AAfreq$total * 100)
|
|
960
|
|
961
|
|
962 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")
|
|
963 AAfreq = merge(AAfreq, AAorder, by.x='Var1', by.y='AA', all.x=TRUE)
|
|
964
|
|
965 AAfreq = AAfreq[!is.na(AAfreq$order.aa),]
|
|
966
|
|
967 AAfreqplot = ggplot(AAfreq)
|
|
968 AAfreqplot = AAfreqplot + geom_bar(aes( x=factor(reorder(Var1, order.aa)), y = freq_perc, fill = Sample), stat='identity', position='dodge' )
|
|
969 AAfreqplot = AAfreqplot + annotate("rect", xmin = 0.5, xmax = 2.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
|
|
970 AAfreqplot = AAfreqplot + annotate("rect", xmin = 3.5, xmax = 4.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
|
|
971 AAfreqplot = AAfreqplot + annotate("rect", xmin = 5.5, xmax = 6.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
|
|
972 AAfreqplot = AAfreqplot + annotate("rect", xmin = 6.5, xmax = 7.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
|
8
|
973 AAfreqplot = AAfreqplot + ggtitle("Amino Acid Composition in the CDR3") + xlab("Amino Acid, from Hydrophilic (left) to Hydrophobic (right)") + ylab("Percentage") + scale_fill_manual(values=sample.colors)
|
32
|
974 AAfreqplot = AAfreqplot + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
|
5
|
975
|
|
976 png("AAComposition.png",width = 1280, height = 720)
|
|
977 AAfreqplot
|
|
978 dev.off()
|
32
|
979
|
|
980 ggsave("AAComposition.pdf", AAfreqplot, width=12, height=7)
|
|
981
|
18
|
982 write.table(AAfreq, "AAComposition.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=T)
|
5
|
983
|
8
|
984 # ---------------------- AA median CDR3 length ----------------------
|
5
|
985
|
24
|
986 median.aa.l = data.frame(data.table(PRODF)[, list(median=as.double(median(as.numeric(.SD$CDR3.Length, na.rm=T), na.rm=T))), by=c("Sample")])
|
|
987 write.table(median.aa.l, "AAMedianBySample.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
|
8
|
988
|
25
|
989
|
|
990 #generate the "Sequences that are present in more than one replicate" dataset
|
|
991 clonaltype.in.replicates = inputdata
|
38
|
992 clonaltype.in.replicates = clonaltype.in.replicates[clonaltype.in.replicates$Functionality %in% c("productive (see comment)","productive"),]
|
37
|
993 clonaltype.in.replicates = na.omit(clonaltype.in.replicates)
|
25
|
994 clonaltype = unlist(strsplit(clonaltype, ","))
|
37
|
995
|
|
996 clonaltype.in.replicates$clonaltype = do.call(paste, c(clonaltype.in.replicates[c(clonaltype, "Replicate")], sep = ":"))
|
|
997
|
|
998 clonaltype.in.replicates = clonaltype.in.replicates[!duplicated(clonaltype.in.replicates$clonaltype),]
|
|
999
|
25
|
1000 clonaltype = clonaltype[-which(clonaltype == "Sample")]
|
|
1001
|
|
1002 clonaltype.in.replicates$clonaltype = do.call(paste, c(clonaltype.in.replicates[clonaltype], sep = ":"))
|
|
1003 clonaltype.in.replicates = clonaltype.in.replicates[,c("clonaltype","Replicate", "ID", "Sequence", "Sample")]
|
|
1004
|
|
1005 clonaltype.counts = data.frame(table(clonaltype.in.replicates$clonaltype))
|
|
1006 names(clonaltype.counts) = c("clonaltype", "coincidence")
|
|
1007
|
|
1008 clonaltype.counts = clonaltype.counts[clonaltype.counts$coincidence > 1,]
|
|
1009
|
|
1010 clonaltype.in.replicates = clonaltype.in.replicates[clonaltype.in.replicates$clonaltype %in% clonaltype.counts$clonaltype,]
|
|
1011 clonaltype.in.replicates = merge(clonaltype.in.replicates, clonaltype.counts, by="clonaltype")
|
37
|
1012 clonaltype.in.replicates = clonaltype.in.replicates[order(-clonaltype.in.replicates$coincidence, clonaltype.in.replicates$clonaltype, clonaltype.in.replicates$Replicate),c("coincidence","clonaltype", "Sample", "Replicate", "ID", "Sequence")]
|
|
1013
|
25
|
1014
|
|
1015 write.table(clonaltype.in.replicates, "clonaltypes_replicates.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=T)
|
|
1016
|
|
1017
|
|
1018
|
|
1019
|
|
1020
|
|
1021
|
|
1022
|
|
1023
|
|
1024
|
|
1025
|
|
1026
|
|
1027
|
|
1028
|
|
1029
|
|
1030
|
|
1031
|
|
1032
|
|
1033
|
|
1034
|
|
1035
|
|
1036
|
|
1037
|
|
1038
|