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