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