comparison mutation_analysis.r @ 57:16c7fc1c4bf8 draft

Uploaded
author davidvanzessen
date Fri, 18 Mar 2016 07:50:34 -0400
parents
children
comparison
equal deleted inserted replaced
56:2eb94c08e550 57:16c7fc1c4bf8
1 library(data.table)
2 library(ggplot2)
3
4 args <- commandArgs(trailingOnly = TRUE)
5
6 input = args[1]
7 genes = unlist(strsplit(args[2], ","))
8 outputdir = args[3]
9 print(args[4])
10 include_fr1 = ifelse(args[4] == "yes", T, F)
11 setwd(outputdir)
12
13 dat = read.table(input, header=T, sep="\t", fill=T, stringsAsFactors=F)
14
15 if(length(dat$Sequence.ID) == 0){
16 setwd(outputdir)
17 result = data.frame(x = rep(0, 5), y = rep(0, 5), z = rep(NA, 5))
18 row.names(result) = c("Number of Mutations (%)", "Transition (%)", "Transversions (%)", "Transitions at G C (%)", "Targeting of C G (%)")
19 write.table(x=result, file="mutations.txt", sep=",",quote=F,row.names=T,col.names=F)
20 transitionTable = data.frame(A=rep(0, 4),C=rep(0, 4),G=rep(0, 4),T=rep(0, 4))
21 row.names(transitionTable) = c("A", "C", "G", "T")
22 transitionTable["A","A"] = NA
23 transitionTable["C","C"] = NA
24 transitionTable["G","G"] = NA
25 transitionTable["T","T"] = NA
26 write.table(x=transitionTable, file="transitions.txt", sep=",",quote=F,row.names=T,col.names=NA)
27 cat("0", file="n.txt")
28 stop("No data")
29 }
30
31
32
33 cleanup_columns = c("FR1.IMGT.c.a",
34 "FR2.IMGT.g.t",
35 "CDR1.IMGT.Nb.of.nucleotides",
36 "CDR2.IMGT.t.a",
37 "FR1.IMGT.c.g",
38 "CDR1.IMGT.c.t",
39 "FR2.IMGT.a.c",
40 "FR2.IMGT.Nb.of.mutations",
41 "FR2.IMGT.g.c",
42 "FR2.IMGT.a.g",
43 "FR3.IMGT.t.a",
44 "FR3.IMGT.t.c",
45 "FR2.IMGT.g.a",
46 "FR3.IMGT.c.g",
47 "FR1.IMGT.Nb.of.mutations",
48 "CDR1.IMGT.g.a",
49 "CDR1.IMGT.t.g",
50 "CDR1.IMGT.g.c",
51 "CDR2.IMGT.Nb.of.nucleotides",
52 "FR2.IMGT.a.t",
53 "CDR1.IMGT.Nb.of.mutations",
54 "CDR1.IMGT.a.g",
55 "FR3.IMGT.a.c",
56 "FR1.IMGT.g.a",
57 "FR3.IMGT.a.g",
58 "FR1.IMGT.a.t",
59 "CDR2.IMGT.a.g",
60 "CDR2.IMGT.Nb.of.mutations",
61 "CDR2.IMGT.g.t",
62 "CDR2.IMGT.a.c",
63 "CDR1.IMGT.t.c",
64 "FR3.IMGT.g.c",
65 "FR1.IMGT.g.t",
66 "FR3.IMGT.g.t",
67 "CDR1.IMGT.a.t",
68 "FR1.IMGT.a.g",
69 "FR3.IMGT.a.t",
70 "FR3.IMGT.Nb.of.nucleotides",
71 "FR2.IMGT.t.c",
72 "CDR2.IMGT.g.a",
73 "FR2.IMGT.t.a",
74 "CDR1.IMGT.t.a",
75 "FR2.IMGT.t.g",
76 "FR3.IMGT.t.g",
77 "FR2.IMGT.Nb.of.nucleotides",
78 "FR1.IMGT.t.a",
79 "FR1.IMGT.t.g",
80 "FR3.IMGT.c.t",
81 "FR1.IMGT.t.c",
82 "CDR2.IMGT.a.t",
83 "FR2.IMGT.c.t",
84 "CDR1.IMGT.g.t",
85 "CDR2.IMGT.t.g",
86 "FR1.IMGT.Nb.of.nucleotides",
87 "CDR1.IMGT.c.g",
88 "CDR2.IMGT.t.c",
89 "FR3.IMGT.g.a",
90 "CDR1.IMGT.a.c",
91 "FR2.IMGT.c.a",
92 "FR3.IMGT.Nb.of.mutations",
93 "FR2.IMGT.c.g",
94 "CDR2.IMGT.g.c",
95 "FR1.IMGT.g.c",
96 "CDR2.IMGT.c.t",
97 "FR3.IMGT.c.a",
98 "CDR1.IMGT.c.a",
99 "CDR2.IMGT.c.g",
100 "CDR2.IMGT.c.a",
101 "FR1.IMGT.c.t",
102 "FR1.IMGT.Nb.of.silent.mutations",
103 "FR2.IMGT.Nb.of.silent.mutations",
104 "FR3.IMGT.Nb.of.silent.mutations",
105 "FR1.IMGT.Nb.of.nonsilent.mutations",
106 "FR2.IMGT.Nb.of.nonsilent.mutations",
107 "FR3.IMGT.Nb.of.nonsilent.mutations")
108
109 for(col in cleanup_columns){
110 dat[,col] = gsub("\\(.*\\)", "", dat[,col])
111 #dat[dat[,col] == "",] = "0"
112 dat[,col] = as.numeric(dat[,col])
113 dat[is.na(dat[,col]),] = 0
114 }
115
116 regions = c("FR1", "CDR1", "FR2", "CDR2", "FR3")
117 if(!include_fr1){
118 regions = c("CDR1", "FR2", "CDR2", "FR3")
119 }
120
121 sum_by_row = function(x, columns) { sum(as.numeric(x[columns]), na.rm=T) }
122
123 VRegionMutations_columns = paste(regions, ".IMGT.Nb.of.mutations", sep="")
124 dat$VRegionMutations = apply(dat, FUN=sum_by_row, 1, columns=VRegionMutations_columns)
125
126 VRegionNucleotides_columns = paste(regions, ".IMGT.Nb.of.nucleotides", sep="")
127 dat$VRegionNucleotides = apply(dat, FUN=sum_by_row, 1, columns=VRegionNucleotides_columns)
128
129 transitionMutations_columns = paste(rep(regions, each=4), c(".IMGT.a.g", ".IMGT.g.a", ".IMGT.c.t", ".IMGT.t.c"), sep="")
130 dat$transitionMutations = apply(dat, FUN=sum_by_row, 1, columns=transitionMutations_columns)
131
132 transversionMutations_columns = paste(rep(regions, each=8), c(".IMGT.a.c",".IMGT.c.a",".IMGT.a.t",".IMGT.t.a",".IMGT.g.c",".IMGT.c.g",".IMGT.g.t",".IMGT.t.g"), sep="")
133 dat$transversionMutations = apply(dat, FUN=sum_by_row, 1, columns=transversionMutations_columns)
134
135
136 transitionMutationsAtGC_columns = paste(rep(regions, each=2), c(".IMGT.g.a",".IMGT.c.t"), sep="")
137 dat$transitionMutationsAtGC = apply(dat, FUN=sum_by_row, 1, columns=transitionMutationsAtGC_columns)
138
139
140 totalMutationsAtGC_columns = paste(rep(regions, each=6), c(".IMGT.c.g",".IMGT.c.t",".IMGT.c.a",".IMGT.g.c",".IMGT.g.a",".IMGT.g.t"), sep="")
141 #totalMutationsAtGC_columns = paste(rep(regions, each=6), c(".IMGT.g.a",".IMGT.c.t",".IMGT.c.a",".IMGT.c.g",".IMGT.g.t"), sep="")
142 dat$totalMutationsAtGC = apply(dat, FUN=sum_by_row, 1, columns=totalMutationsAtGC_columns)
143
144 transitionMutationsAtAT_columns = paste(rep(regions, each=2), c(".IMGT.a.g",".IMGT.t.c"), sep="")
145 dat$transitionMutationsAtAT = apply(dat, FUN=sum_by_row, 1, columns=transitionMutationsAtAT_columns)
146
147 totalMutationsAtAT_columns = paste(rep(regions, each=6), c(".IMGT.a.g",".IMGT.a.c",".IMGT.a.t",".IMGT.t.g",".IMGT.t.c",".IMGT.t.a"), sep="")
148 #totalMutationsAtAT_columns = paste(rep(regions, each=5), c(".IMGT.a.g",".IMGT.t.c",".IMGT.a.c",".IMGT.g.c",".IMGT.t.g"), sep="")
149 dat$totalMutationsAtAT = apply(dat, FUN=sum_by_row, 1, columns=totalMutationsAtAT_columns)
150
151
152 FRRegions = regions[grepl("FR", regions)]
153 CDRRegions = regions[grepl("CDR", regions)]
154
155 FR_silentMutations_columns = paste(FRRegions, ".IMGT.Nb.of.silent.mutations", sep="")
156 dat$silentMutationsFR = apply(dat, FUN=sum_by_row, 1, columns=FR_silentMutations_columns)
157
158 CDR_silentMutations_columns = paste(CDRRegions, ".IMGT.Nb.of.silent.mutations", sep="")
159 dat$silentMutationsCDR = apply(dat, FUN=sum_by_row, 1, columns=CDR_silentMutations_columns)
160
161 FR_nonSilentMutations_columns = paste(FRRegions, ".IMGT.Nb.of.nonsilent.mutations", sep="")
162 dat$nonSilentMutationsFR = apply(dat, FUN=sum_by_row, 1, columns=FR_nonSilentMutations_columns)
163
164 CDR_nonSilentMutations_columns = paste(CDRRegions, ".IMGT.Nb.of.nonsilent.mutations", sep="")
165 dat$nonSilentMutationsCDR = apply(dat, FUN=sum_by_row, 1, columns=CDR_nonSilentMutations_columns)
166
167 mutation.sum.columns = c("Sequence.ID", "VRegionMutations", "VRegionNucleotides", "transitionMutations", "transversionMutations", "transitionMutationsAtGC", "transitionMutationsAtAT", "silentMutationsFR", "nonSilentMutationsFR", "silentMutationsCDR", "nonSilentMutationsCDR")
168
169 write.table(dat[,mutation.sum.columns], "mutation_by_id.txt", sep="\t",quote=F,row.names=F,col.names=T)
170
171 setwd(outputdir)
172
173
174 calculate_result = function(i, gene, dat, matrx, f, fname, name){
175 tmp = dat[grepl(paste(".*", gene, ".*", sep=""), dat$best_match),]
176
177 j = i - 1
178 x = (j * 3) + 1
179 y = (j * 3) + 2
180 z = (j * 3) + 3
181
182 if(nrow(tmp) > 0){
183
184 matrx[1,x] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
185 matrx[1,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
186 matrx[1,z] = round(matrx[1,x] / matrx[1,y] * 100, digits=1)
187
188 matrx[2,x] = round(f(tmp$transitionMutations, na.rm=T), digits=1)
189 matrx[2,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
190 matrx[2,z] = round(matrx[2,x] / matrx[2,y] * 100, digits=1)
191
192 matrx[3,x] = round(f(tmp$transversionMutations, na.rm=T), digits=1)
193 matrx[3,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
194 matrx[3,z] = round(matrx[3,x] / matrx[3,y] * 100, digits=1)
195
196 matrx[4,x] = round(f(tmp$transitionMutationsAtGC, na.rm=T), digits=1)
197 matrx[4,y] = round(f(tmp$totalMutationsAtGC, na.rm=T), digits=1)
198 matrx[4,z] = round(matrx[4,x] / matrx[4,y] * 100, digits=1)
199
200 matrx[5,x] = round(f(tmp$totalMutationsAtGC, na.rm=T), digits=1)
201 matrx[5,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
202 matrx[5,z] = round(matrx[5,x] / matrx[5,y] * 100, digits=1)
203
204 matrx[6,x] = round(f(tmp$transitionMutationsAtAT, na.rm=T), digits=1)
205 matrx[6,y] = round(f(tmp$totalMutationsAtAT, na.rm=T), digits=1)
206 matrx[6,z] = round(matrx[6,x] / matrx[6,y] * 100, digits=1)
207
208 matrx[7,x] = round(f(tmp$totalMutationsAtAT, na.rm=T), digits=1)
209 matrx[7,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
210 matrx[7,z] = round(matrx[7,x] / matrx[7,y] * 100, digits=1)
211
212 matrx[8,x] = round(f(tmp$nonSilentMutationsFR, na.rm=T), digits=1)
213 matrx[8,y] = round(f(tmp$silentMutationsFR, na.rm=T), digits=1)
214 matrx[8,z] = round(matrx[8,x] / matrx[8,y], digits=1)
215
216 matrx[9,x] = round(f(tmp$nonSilentMutationsCDR, na.rm=T), digits=1)
217 matrx[9,y] = round(f(tmp$silentMutationsCDR, na.rm=T), digits=1)
218 matrx[9,z] = round(matrx[9,x] / matrx[9,y], digits=1)
219 }
220
221 transitionTable = data.frame(A=zeros,C=zeros,G=zeros,T=zeros)
222 row.names(transitionTable) = c("A", "C", "G", "T")
223 transitionTable["A","A"] = NA
224 transitionTable["C","C"] = NA
225 transitionTable["G","G"] = NA
226 transitionTable["T","T"] = NA
227
228 if(nrow(tmp) > 0){
229 for(nt1 in nts){
230 for(nt2 in nts){
231 if(nt1 == nt2){
232 next
233 }
234 NT1 = LETTERS[letters == nt1]
235 NT2 = LETTERS[letters == nt2]
236 FR1 = paste("FR1.IMGT.", nt1, ".", nt2, sep="")
237 CDR1 = paste("CDR1.IMGT.", nt1, ".", nt2, sep="")
238 FR2 = paste("FR2.IMGT.", nt1, ".", nt2, sep="")
239 CDR2 = paste("CDR2.IMGT.", nt1, ".", nt2, sep="")
240 FR3 = paste("FR3.IMGT.", nt1, ".", nt2, sep="")
241 if(include_fr1){
242 transitionTable[NT1,NT2] = sum(tmp[,c(FR1, CDR1, FR2, CDR2, FR3)])
243 } else {
244 transitionTable[NT1,NT2] = sum(tmp[,c(CDR1, FR2, CDR2, FR3)])
245 }
246 }
247 }
248 }
249
250
251 print(paste("writing value file: ", name, "_", fname, "_value.txt" ,sep=""))
252
253 write.table(x=transitionTable, file=paste("transitions_", name ,"_", fname, ".txt", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
254 write.table(x=tmp[,c("Sequence.ID", "best_match", "chunk_hit_percentage", "nt_hit_percentage", "start_locations")], file=paste("matched_", name , "_", fname, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
255
256 cat(matrx[1,x], file=paste(name, "_", fname, "_value.txt" ,sep=""))
257 cat(length(tmp$Sequence.ID), file=paste(name, "_", fname, "_n.txt" ,sep=""))
258
259 matrx
260 }
261
262 nts = c("a", "c", "g", "t")
263 zeros=rep(0, 4)
264
265 funcs = c(median, sum, mean)
266 fnames = c("median", "sum", "mean")
267
268 for(i in 1:length(funcs)){
269 func = funcs[[i]]
270 fname = fnames[[i]]
271
272 matrx = matrix(data = 0, ncol=((length(genes) + 1) * 3),nrow=9)
273
274 for(i in 1:length(genes)){
275 matrx = calculate_result(i, genes[i], dat, matrx, func, fname, genes[i])
276 }
277
278 matrx = calculate_result(i + 1, ".*", dat, matrx, func, fname, name="all")
279
280 result = data.frame(matrx)
281 row.names(result) = c("Number of Mutations (%)", "Transition (%)", "Transversions (%)", "Transitions at G C (%)", "Targeting of C G (%)", "Transitions at A T (%)", "Targeting of A T (%)", "FR R/S (ratio)", "CDR R/S (ratio)")
282
283 write.table(x=result, file=paste("mutations_", fname, ".txt", sep=""), sep=",",quote=F,row.names=T,col.names=F)
284 }
285
286
287 if (!("ggplot2" %in% rownames(installed.packages()))) {
288 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
289 }
290
291
292 genesForPlot = gsub("[0-9]", "", dat$best_match)
293 genesForPlot = data.frame(table(genesForPlot))
294 colnames(genesForPlot) = c("Gene","Freq")
295 genesForPlot$label = paste(genesForPlot$Gene, "-", genesForPlot$Freq)
296 write.table(genesForPlot, "all.txt", sep="\t",quote=F,row.names=F,col.names=T)
297
298
299 pc = ggplot(genesForPlot, aes(x = factor(1), y=Freq, fill=label))
300 pc = pc + geom_bar(width = 1, stat = "identity")
301 pc = pc + coord_polar(theta="y")
302 pc = pc + xlab(" ") + ylab(" ") + ggtitle(paste("Classes", "( n =", sum(genesForPlot$Freq), ")"))
303
304 png(filename="all.png")
305 pc
306 dev.off()
307
308
309 #blegh
310 genesForPlot = dat[grepl("ca", dat$best_match),]$best_match
311 if(length(genesForPlot) > 0){
312 genesForPlot = data.frame(table(genesForPlot))
313 colnames(genesForPlot) = c("Gene","Freq")
314 genesForPlot$label = paste(genesForPlot$Gene, "-", genesForPlot$Freq)
315
316 pc = ggplot(genesForPlot, aes(x = factor(1), y=Freq, fill=label))
317 pc = pc + geom_bar(width = 1, stat = "identity")
318 pc = pc + coord_polar(theta="y")
319 pc = pc + xlab(" ") + ylab(" ") + ggtitle(paste("IgA subclasses", "( n =", sum(genesForPlot$Freq), ")"))
320 write.table(genesForPlot, "ca.txt", sep="\t",quote=F,row.names=F,col.names=T)
321
322 png(filename="ca.png")
323 print(pc)
324 dev.off()
325 }
326
327 genesForPlot = dat[grepl("cg", dat$best_match),]$best_match
328 if(length(genesForPlot) > 0){
329 genesForPlot = data.frame(table(genesForPlot))
330 colnames(genesForPlot) = c("Gene","Freq")
331 genesForPlot$label = paste(genesForPlot$Gene, "-", genesForPlot$Freq)
332
333 pc = ggplot(genesForPlot, aes(x = factor(1), y=Freq, fill=label))
334 pc = pc + geom_bar(width = 1, stat = "identity")
335 pc = pc + coord_polar(theta="y")
336 pc = pc + xlab(" ") + ylab(" ") + ggtitle(paste("IgG subclasses", "( n =", sum(genesForPlot$Freq), ")"))
337 write.table(genesForPlot, "cg.txt", sep="\t",quote=F,row.names=F,col.names=T)
338
339 png(filename="cg.png")
340 print(pc)
341 dev.off()
342 }
343
344 dat$percentage_mutations = round(dat$VRegionMutations / dat$VRegionNucleotides * 100, 2)
345
346 p = ggplot(dat, aes(best_match, percentage_mutations))
347 p = p + geom_point(aes(colour=best_match), position="jitter") + geom_boxplot(aes(middle=mean(percentage_mutations)), alpha=0.1, outlier.shape = NA)
348 p = p + xlab("Subclass") + ylab("Frequency") + ggtitle("Frequency scatter plot")
349
350 png(filename="scatter.png")
351 print(p)
352 dev.off()
353
354 write.table(dat[,c("Sequence.ID", "best_match", "VRegionMutations", "VRegionNucleotides", "percentage_mutations")], "scatter.txt", sep="\t",quote=F,row.names=F,col.names=T)
355
356 write.table(dat, input, sep="\t",quote=F,row.names=F,col.names=T)
357
358
359
360
361
362
363 dat$best_match_class = substr(dat$best_match, 0, 2)
364 freq_labels = c("0", "0-2", "2-5", "5-10", "10-15", "15-20", "20")
365 dat$frequency_bins = cut(dat$percentage_mutations, breaks=c(-Inf, 0, 2,5,10,15,20, Inf), labels=freq_labels)
366
367 frequency_bins_data = data.frame(data.table(dat)[, list(frequency_count=.N), by=c("best_match_class", "frequency_bins")])
368
369 p = ggplot(frequency_bins_data, aes(frequency_bins, frequency_count))
370 p = p + geom_bar(aes(fill=best_match_class), stat="identity", position="dodge")
371 p = p + xlab("Frequency ranges") + ylab("Frequency") + ggtitle("Mutation Frequencies by class")
372
373 png(filename="frequency_ranges.png")
374 print(p)
375 dev.off()
376
377 frequency_bins_data_by_class = frequency_bins_data
378
379 write.table(frequency_bins_data_by_class, "frequency_ranges_classes.txt", sep="\t",quote=F,row.names=F,col.names=T)
380
381 frequency_bins_data = data.frame(data.table(dat)[, list(frequency_count=.N), by=c("best_match", "frequency_bins")])
382
383 write.table(frequency_bins_data, "frequency_ranges_subclasses.txt", sep="\t",quote=F,row.names=F,col.names=T)
384
385
386 #frequency_bins_data_by_class
387 #frequency_ranges_subclasses.txt
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414