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