1
|
1 library(data.table)
|
|
2 library(ggplot2)
|
|
3 library(reshape2)
|
|
4
|
|
5 args <- commandArgs(trailingOnly = TRUE)
|
|
6
|
|
7 input = args[1]
|
|
8 genes = unlist(strsplit(args[2], ","))
|
|
9 outputdir = args[3]
|
|
10 empty.region.filter = args[4]
|
|
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))
|
23
|
18 row.names(result) = c("Number of Mutations (%)", "Transition (%)", "Transversions (%)", "Transitions at G C (%)", "Targeting of G C (%)")
|
1
|
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
|
|
27 write.table(x=transitionTable, file="transitions.txt", sep=",",quote=F,row.names=T,col.names=NA)
|
|
28 cat("0", file="n.txt")
|
|
29 stop("No data")
|
|
30 }
|
|
31
|
|
32 cleanup_columns = c("FR1.IMGT.c.a",
|
|
33 "FR2.IMGT.g.t",
|
|
34 "CDR1.IMGT.Nb.of.nucleotides",
|
|
35 "CDR2.IMGT.t.a",
|
|
36 "FR1.IMGT.c.g",
|
|
37 "CDR1.IMGT.c.t",
|
|
38 "FR2.IMGT.a.c",
|
|
39 "FR2.IMGT.Nb.of.mutations",
|
|
40 "FR2.IMGT.g.c",
|
|
41 "FR2.IMGT.a.g",
|
|
42 "FR3.IMGT.t.a",
|
|
43 "FR3.IMGT.t.c",
|
|
44 "FR2.IMGT.g.a",
|
|
45 "FR3.IMGT.c.g",
|
|
46 "FR1.IMGT.Nb.of.mutations",
|
|
47 "CDR1.IMGT.g.a",
|
|
48 "CDR1.IMGT.t.g",
|
|
49 "CDR1.IMGT.g.c",
|
|
50 "CDR2.IMGT.Nb.of.nucleotides",
|
|
51 "FR2.IMGT.a.t",
|
|
52 "CDR1.IMGT.Nb.of.mutations",
|
|
53 "CDR3.IMGT.Nb.of.nucleotides",
|
|
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 print("Cleaning up columns")
|
|
110
|
|
111 for(col in cleanup_columns){
|
|
112 dat[,col] = gsub("\\(.*\\)", "", dat[,col])
|
|
113 #dat[dat[,col] == "",] = "0"
|
|
114 dat[,col] = as.numeric(dat[,col])
|
|
115 dat[is.na(dat[,col]),col] = 0
|
|
116 }
|
|
117
|
|
118 regions = c("FR1", "CDR1", "FR2", "CDR2", "FR3")
|
|
119 if(empty.region.filter == "FR1") {
|
|
120 regions = c("CDR1", "FR2", "CDR2", "FR3")
|
|
121 } else if (empty.region.filter == "CDR1") {
|
7
|
122 regions = c("FR2", "CDR2", "FR3")
|
1
|
123 } else if (empty.region.filter == "FR2") {
|
7
|
124 regions = c("CDR2", "FR3")
|
1
|
125 }
|
|
126
|
|
127 sum_by_row = function(x, columns) { sum(as.numeric(x[columns]), na.rm=T) }
|
|
128
|
|
129 print("aggregating data into new columns")
|
|
130
|
|
131 VRegionMutations_columns = paste(regions, ".IMGT.Nb.of.mutations", sep="")
|
|
132 dat$VRegionMutations = apply(dat, FUN=sum_by_row, 1, columns=VRegionMutations_columns)
|
|
133
|
|
134 VRegionNucleotides_columns = paste(regions, ".IMGT.Nb.of.nucleotides", sep="")
|
|
135 dat$FR3.IMGT.Nb.of.nucleotides = nchar(dat$FR3.IMGT.seq)
|
|
136 dat$VRegionNucleotides = apply(dat, FUN=sum_by_row, 1, columns=VRegionNucleotides_columns)
|
|
137
|
|
138 transitionMutations_columns = paste(rep(regions, each=4), c(".IMGT.a.g", ".IMGT.g.a", ".IMGT.c.t", ".IMGT.t.c"), sep="")
|
|
139 dat$transitionMutations = apply(dat, FUN=sum_by_row, 1, columns=transitionMutations_columns)
|
|
140
|
|
141 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="")
|
|
142 dat$transversionMutations = apply(dat, FUN=sum_by_row, 1, columns=transversionMutations_columns)
|
|
143
|
|
144 transitionMutationsAtGC_columns = paste(rep(regions, each=2), c(".IMGT.g.a",".IMGT.c.t"), sep="")
|
|
145 dat$transitionMutationsAtGC = apply(dat, FUN=sum_by_row, 1, columns=transitionMutationsAtGC_columns)
|
|
146
|
|
147 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="")
|
|
148 #totalMutationsAtGC_columns = paste(rep(regions, each=6), c(".IMGT.g.a",".IMGT.c.t",".IMGT.c.a",".IMGT.c.g",".IMGT.g.t"), sep="")
|
|
149 dat$totalMutationsAtGC = apply(dat, FUN=sum_by_row, 1, columns=totalMutationsAtGC_columns)
|
|
150
|
|
151 transitionMutationsAtAT_columns = paste(rep(regions, each=2), c(".IMGT.a.g",".IMGT.t.c"), sep="")
|
|
152 dat$transitionMutationsAtAT = apply(dat, FUN=sum_by_row, 1, columns=transitionMutationsAtAT_columns)
|
|
153
|
|
154 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="")
|
|
155 #totalMutationsAtAT_columns = paste(rep(regions, each=5), c(".IMGT.a.g",".IMGT.t.c",".IMGT.a.c",".IMGT.g.c",".IMGT.t.g"), sep="")
|
|
156 dat$totalMutationsAtAT = apply(dat, FUN=sum_by_row, 1, columns=totalMutationsAtAT_columns)
|
|
157
|
|
158 FRRegions = regions[grepl("FR", regions)]
|
|
159 CDRRegions = regions[grepl("CDR", regions)]
|
|
160
|
|
161 FR_silentMutations_columns = paste(FRRegions, ".IMGT.Nb.of.silent.mutations", sep="")
|
|
162 dat$silentMutationsFR = apply(dat, FUN=sum_by_row, 1, columns=FR_silentMutations_columns)
|
|
163
|
|
164 CDR_silentMutations_columns = paste(CDRRegions, ".IMGT.Nb.of.silent.mutations", sep="")
|
|
165 dat$silentMutationsCDR = apply(dat, FUN=sum_by_row, 1, columns=CDR_silentMutations_columns)
|
|
166
|
|
167 FR_nonSilentMutations_columns = paste(FRRegions, ".IMGT.Nb.of.nonsilent.mutations", sep="")
|
|
168 dat$nonSilentMutationsFR = apply(dat, FUN=sum_by_row, 1, columns=FR_nonSilentMutations_columns)
|
|
169
|
|
170 CDR_nonSilentMutations_columns = paste(CDRRegions, ".IMGT.Nb.of.nonsilent.mutations", sep="")
|
|
171 dat$nonSilentMutationsCDR = apply(dat, FUN=sum_by_row, 1, columns=CDR_nonSilentMutations_columns)
|
|
172
|
|
173 mutation.sum.columns = c("Sequence.ID", "VRegionMutations", "VRegionNucleotides", "transitionMutations", "transversionMutations", "transitionMutationsAtGC", "transitionMutationsAtAT", "silentMutationsFR", "nonSilentMutationsFR", "silentMutationsCDR", "nonSilentMutationsCDR")
|
|
174 write.table(dat[,mutation.sum.columns], "mutation_by_id.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
|
175
|
|
176 setwd(outputdir)
|
|
177
|
8
|
178 write.table(dat, input, sep="\t",quote=F,row.names=F,col.names=T)
|
|
179
|
24
|
180 base.order.x = data.frame(base=c("A", "C", "G", "T"), order.x=1:4)
|
|
181 base.order.y = data.frame(base=c("T", "G", "C", "A"), order.y=1:4)
|
1
|
182
|
|
183 calculate_result = function(i, gene, dat, matrx, f, fname, name){
|
|
184 tmp = dat[grepl(paste("^", gene, ".*", sep=""), dat$best_match),]
|
|
185
|
|
186 j = i - 1
|
|
187 x = (j * 3) + 1
|
|
188 y = (j * 3) + 2
|
|
189 z = (j * 3) + 3
|
|
190
|
|
191 if(nrow(tmp) > 0){
|
|
192 if(fname == "sum"){
|
|
193 matrx[1,x] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
|
|
194 matrx[1,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
|
|
195 matrx[1,z] = round(f(matrx[1,x] / matrx[1,y]) * 100, digits=1)
|
|
196 } else {
|
|
197 matrx[1,x] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
|
|
198 matrx[1,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
|
|
199 matrx[1,z] = round(f(tmp$VRegionMutations / tmp$VRegionNucleotides) * 100, digits=1)
|
|
200 }
|
|
201
|
|
202 matrx[2,x] = round(f(tmp$transitionMutations, na.rm=T), digits=1)
|
|
203 matrx[2,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
|
|
204 matrx[2,z] = round(matrx[2,x] / matrx[2,y] * 100, digits=1)
|
|
205
|
|
206 matrx[3,x] = round(f(tmp$transversionMutations, na.rm=T), digits=1)
|
|
207 matrx[3,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
|
|
208 matrx[3,z] = round(matrx[3,x] / matrx[3,y] * 100, digits=1)
|
|
209
|
|
210 matrx[4,x] = round(f(tmp$transitionMutationsAtGC, na.rm=T), digits=1)
|
|
211 matrx[4,y] = round(f(tmp$totalMutationsAtGC, na.rm=T), digits=1)
|
|
212 matrx[4,z] = round(matrx[4,x] / matrx[4,y] * 100, digits=1)
|
|
213
|
|
214 matrx[5,x] = round(f(tmp$totalMutationsAtGC, na.rm=T), digits=1)
|
|
215 matrx[5,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
|
|
216 matrx[5,z] = round(matrx[5,x] / matrx[5,y] * 100, digits=1)
|
|
217
|
|
218 matrx[6,x] = round(f(tmp$transitionMutationsAtAT, na.rm=T), digits=1)
|
|
219 matrx[6,y] = round(f(tmp$totalMutationsAtAT, na.rm=T), digits=1)
|
|
220 matrx[6,z] = round(matrx[6,x] / matrx[6,y] * 100, digits=1)
|
|
221
|
|
222 matrx[7,x] = round(f(tmp$totalMutationsAtAT, na.rm=T), digits=1)
|
|
223 matrx[7,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
|
|
224 matrx[7,z] = round(matrx[7,x] / matrx[7,y] * 100, digits=1)
|
|
225
|
|
226 matrx[8,x] = round(f(tmp$nonSilentMutationsFR, na.rm=T), digits=1)
|
|
227 matrx[8,y] = round(f(tmp$silentMutationsFR, na.rm=T), digits=1)
|
|
228 matrx[8,z] = round(matrx[8,x] / matrx[8,y], digits=1)
|
|
229
|
|
230 matrx[9,x] = round(f(tmp$nonSilentMutationsCDR, na.rm=T), digits=1)
|
|
231 matrx[9,y] = round(f(tmp$silentMutationsCDR, na.rm=T), digits=1)
|
|
232 matrx[9,z] = round(matrx[9,x] / matrx[9,y], digits=1)
|
|
233
|
|
234 if(fname == "sum"){
|
7
|
235
|
|
236 regions.fr = regions[grepl("FR", regions)]
|
|
237 regions.fr = paste(regions.fr, ".IMGT.Nb.of.nucleotides", sep="")
|
|
238 regions.cdr = regions[grepl("CDR", regions)]
|
|
239 regions.cdr = paste(regions.cdr, ".IMGT.Nb.of.nucleotides", sep="")
|
|
240
|
|
241 if(length(regions.fr) > 1){ #in case there is only on FR region (rowSums needs >1 column)
|
|
242 matrx[10,x] = round(f(rowSums(tmp[,regions.fr], na.rm=T)), digits=1)
|
|
243 } else {
|
|
244 matrx[10,x] = round(f(tmp[,regions.fr], na.rm=T), digits=1)
|
|
245 }
|
1
|
246 matrx[10,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
|
|
247 matrx[10,z] = round(matrx[10,x] / matrx[10,y] * 100, digits=1)
|
|
248
|
7
|
249 if(length(regions.cdr) > 1){ #in case there is only on CDR region
|
|
250 matrx[11,x] = round(f(rowSums(tmp[,regions.cdr], na.rm=T)), digits=1)
|
|
251 } else {
|
|
252 matrx[11,x] = round(f(tmp[,regions.cdr], na.rm=T), digits=1)
|
|
253 }
|
1
|
254 matrx[11,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
|
|
255 matrx[11,z] = round(matrx[11,x] / matrx[11,y] * 100, digits=1)
|
|
256 }
|
|
257 }
|
|
258
|
|
259 transitionTable = data.frame(A=zeros,C=zeros,G=zeros,T=zeros)
|
|
260 row.names(transitionTable) = c("A", "C", "G", "T")
|
|
261 transitionTable["A","A"] = NA
|
|
262 transitionTable["C","C"] = NA
|
|
263 transitionTable["G","G"] = NA
|
|
264 transitionTable["T","T"] = NA
|
|
265
|
|
266 if(nrow(tmp) > 0){
|
|
267 for(nt1 in nts){
|
|
268 for(nt2 in nts){
|
|
269 if(nt1 == nt2){
|
|
270 next
|
|
271 }
|
|
272 NT1 = LETTERS[letters == nt1]
|
|
273 NT2 = LETTERS[letters == nt2]
|
|
274 FR1 = paste("FR1.IMGT.", nt1, ".", nt2, sep="")
|
|
275 CDR1 = paste("CDR1.IMGT.", nt1, ".", nt2, sep="")
|
|
276 FR2 = paste("FR2.IMGT.", nt1, ".", nt2, sep="")
|
|
277 CDR2 = paste("CDR2.IMGT.", nt1, ".", nt2, sep="")
|
|
278 FR3 = paste("FR3.IMGT.", nt1, ".", nt2, sep="")
|
|
279 if (empty.region.filter == "leader"){
|
|
280 transitionTable[NT1,NT2] = sum(tmp[,c(FR1, CDR1, FR2, CDR2, FR3)])
|
|
281 } else if (empty.region.filter == "FR1") {
|
|
282 transitionTable[NT1,NT2] = sum(tmp[,c(CDR1, FR2, CDR2, FR3)])
|
|
283 } else if (empty.region.filter == "CDR1") {
|
|
284 transitionTable[NT1,NT2] = sum(tmp[,c(FR2, CDR2, FR3)])
|
|
285 } else if (empty.region.filter == "FR2") {
|
|
286 transitionTable[NT1,NT2] = sum(tmp[,c(CDR2, FR3)])
|
|
287 }
|
|
288 }
|
|
289 }
|
|
290 transition = transitionTable
|
|
291 transition$id = names(transition)
|
|
292
|
|
293 transition2 = melt(transition, id.vars="id")
|
|
294
|
24
|
295 transition2 = merge(transition2, base.order.x, by.x="id", by.y="base")
|
1
|
296
|
24
|
297 transition2 = merge(transition2, base.order.y, by.x="variable", by.y="base")
|
1
|
298
|
|
299 transition2[is.na(transition2$value),]$value = 0
|
23
|
300
|
24
|
301 if(any(transition2$value != 0)){ #having a transition table filled with 0 is bad
|
23
|
302 print("Plotting heatmap and transition")
|
1
|
303 png(filename=paste("transitions_stacked_", name, ".png", sep=""))
|
|
304 p = ggplot(transition2, aes(factor(reorder(id, order.x)), y=value, fill=factor(reorder(variable, order.y)))) + geom_bar(position="fill", stat="identity", colour="black") #stacked bar
|
39
|
305 p = p + xlab("From base") + ylab("") + ggtitle("Bargraph transition information") + guides(fill=guide_legend(title=NULL))
|
5
|
306 p = p + theme(panel.background = element_rect(fill = "white", colour="black"), text = element_text(size=16, colour="black")) + scale_fill_manual(values=c("A" = "blue4", "G" = "lightblue1", "C" = "olivedrab3", "T" = "olivedrab4"))
|
1
|
307 #p = p + scale_colour_manual(values=c("A" = "black", "G" = "black", "C" = "black", "T" = "black"))
|
|
308 print(p)
|
|
309 dev.off()
|
42
|
310
|
|
311 ggsave(paste("transitions_stacked_", name, ".pdf", sep=""))
|
|
312
|
1
|
313 png(filename=paste("transitions_heatmap_", name, ".png", sep=""))
|
24
|
314 p = ggplot(transition2, aes(factor(reorder(variable, -order.y)), factor(reorder(id, -order.x)))) + geom_tile(aes(fill = value)) + scale_fill_gradient(low="white", high="steelblue") #heatmap
|
39
|
315 p = p + xlab("To base") + ylab("From Base") + ggtitle("Heatmap transition information") + theme(panel.background = element_rect(fill = "white", colour="black"), text = element_text(size=16, colour="black"))
|
1
|
316 print(p)
|
|
317 dev.off()
|
42
|
318
|
|
319 ggsave(paste("transitions_heatmap_", name, ".pdf", sep=""))
|
1
|
320 } else {
|
23
|
321 #print("No data to plot")
|
1
|
322 }
|
|
323 }
|
|
324
|
|
325 #print(paste("writing value file: ", name, "_", fname, "_value.txt" ,sep=""))
|
|
326 write.table(x=transitionTable, file=paste("transitions_", name ,"_", fname, ".txt", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
|
|
327 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)
|
|
328 cat(matrx[1,x], file=paste(name, "_", fname, "_value.txt" ,sep=""))
|
|
329 cat(nrow(tmp), file=paste(name, "_", fname, "_n.txt" ,sep=""))
|
|
330 #print(paste(fname, name, nrow(tmp)))
|
|
331 matrx
|
|
332 }
|
|
333 nts = c("a", "c", "g", "t")
|
|
334 zeros=rep(0, 4)
|
|
335 funcs = c(median, sum, mean)
|
|
336 fnames = c("median", "sum", "mean")
|
|
337
|
|
338 print("Creating result tables")
|
|
339
|
|
340 for(i in 1:length(funcs)){
|
|
341 func = funcs[[i]]
|
|
342 fname = fnames[[i]]
|
|
343
|
23
|
344 print(paste("Creating table for", fname))
|
|
345
|
1
|
346 rows = 9
|
|
347 if(fname == "sum"){
|
|
348 rows = 11
|
|
349 }
|
|
350 matrx = matrix(data = 0, ncol=((length(genes) + 1) * 3),nrow=rows)
|
|
351 for(i in 1:length(genes)){
|
|
352 matrx = calculate_result(i, genes[i], dat, matrx, func, fname, genes[i])
|
|
353 }
|
|
354 matrx = calculate_result(i + 1, ".*", dat[!grepl("unmatched", dat$best_match),], matrx, func, fname, name="all")
|
|
355
|
|
356 result = data.frame(matrx)
|
|
357 if(fname == "sum"){
|
23
|
358 row.names(result) = c("Number of Mutations (%)", "Transitions (%)", "Transversions (%)", "Transitions at G C (%)", "Targeting of G C (%)", "Transitions at A T (%)", "Targeting of A T (%)", "FR R/S (ratio)", "CDR R/S (ratio)", "nt in FR", "nt in CDR")
|
1
|
359 } else {
|
23
|
360 row.names(result) = c("Number of Mutations (%)", "Transitions (%)", "Transversions (%)", "Transitions at G C (%)", "Targeting of G C (%)", "Transitions at A T (%)", "Targeting of A T (%)", "FR R/S (ratio)", "CDR R/S (ratio)")
|
1
|
361 }
|
|
362 write.table(x=result, file=paste("mutations_", fname, ".txt", sep=""), sep=",",quote=F,row.names=T,col.names=F)
|
|
363 }
|
|
364
|
|
365 print("Adding median number of mutations to sum table")
|
|
366 sum.table = read.table("mutations_sum.txt", sep=",", header=F)
|
|
367 median.table = read.table("mutations_median.txt", sep=",", header=F)
|
|
368
|
|
369 new.table = sum.table[1,]
|
|
370 new.table[2,] = median.table[1,]
|
|
371 new.table[3:12,] = sum.table[2:11,]
|
|
372 new.table[,1] = as.character(new.table[,1])
|
|
373 new.table[2,1] = "Median of Number of Mutations (%)"
|
|
374
|
23
|
375 #sum.table = sum.table[c("Number of Mutations (%)", "Median of Number of Mutations (%)", "Transition (%)", "Transversions (%)", "Transitions at G C (%)", "Targeting of G C (%)", "Transitions at A T (%)", "Targeting of A T (%)", "FR R/S (ratio)", "CDR R/S (ratio)", "nt in FR", "nt in CDR"),]
|
1
|
376
|
|
377 write.table(x=new.table, file="mutations_sum.txt", sep=",",quote=F,row.names=F,col.names=F)
|
|
378
|
|
379 print("Plotting IGA piechart")
|
|
380
|
|
381 dat = dat[!grepl("^unmatched", dat$best_match),]
|
|
382
|
|
383 #blegh
|
|
384
|
|
385 genesForPlot = dat[grepl("IGA", dat$best_match),]$best_match
|
|
386
|
|
387 if(length(genesForPlot) > 0){
|
|
388 genesForPlot = data.frame(table(genesForPlot))
|
|
389 colnames(genesForPlot) = c("Gene","Freq")
|
|
390 genesForPlot$label = paste(genesForPlot$Gene, "-", genesForPlot$Freq)
|
|
391
|
|
392 pc = ggplot(genesForPlot, aes(x = factor(1), y=Freq, fill=Gene))
|
|
393 pc = pc + geom_bar(width = 1, stat = "identity") + scale_fill_manual(labels=genesForPlot$label, values=c("IGA1" = "lightblue1", "IGA2" = "blue4"))
|
|
394 pc = pc + coord_polar(theta="y") + scale_y_continuous(breaks=NULL)
|
5
|
395 pc = pc + theme(panel.background = element_rect(fill = "white", colour="black"), text = element_text(size=16, colour="black"), axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank())
|
39
|
396 pc = pc + xlab(" ") + ylab(" ") + ggtitle(paste("IGA subclass distribution", "( n =", sum(genesForPlot$Freq), ")"))
|
2
|
397 write.table(genesForPlot, "IGA_pie.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
1
|
398
|
|
399 png(filename="IGA.png")
|
|
400 print(pc)
|
|
401 dev.off()
|
42
|
402
|
|
403 ggsave("IGA.pdf", pc)
|
1
|
404 }
|
|
405
|
|
406 print("Plotting IGG piechart")
|
|
407
|
|
408 genesForPlot = dat[grepl("IGG", dat$best_match),]$best_match
|
|
409
|
|
410 if(length(genesForPlot) > 0){
|
|
411 genesForPlot = data.frame(table(genesForPlot))
|
|
412 colnames(genesForPlot) = c("Gene","Freq")
|
|
413 genesForPlot$label = paste(genesForPlot$Gene, "-", genesForPlot$Freq)
|
|
414
|
|
415 pc = ggplot(genesForPlot, aes(x = factor(1), y=Freq, fill=Gene))
|
|
416 pc = pc + geom_bar(width = 1, stat = "identity") + scale_fill_manual(labels=genesForPlot$label, values=c("IGG1" = "olivedrab3", "IGG2" = "red", "IGG3" = "gold", "IGG4" = "darkred"))
|
|
417 pc = pc + coord_polar(theta="y") + scale_y_continuous(breaks=NULL)
|
5
|
418 pc = pc + theme(panel.background = element_rect(fill = "white", colour="black"), text = element_text(size=16, colour="black"), axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank())
|
39
|
419 pc = pc + xlab(" ") + ylab(" ") + ggtitle(paste("IGG subclass distribution", "( n =", sum(genesForPlot$Freq), ")"))
|
2
|
420 write.table(genesForPlot, "IGG_pie.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
1
|
421
|
|
422 png(filename="IGG.png")
|
|
423 print(pc)
|
|
424 dev.off()
|
42
|
425
|
|
426 ggsave("IGG.pdf", pc)
|
1
|
427 }
|
|
428
|
|
429 print("Plotting scatterplot")
|
|
430
|
|
431 dat$percentage_mutations = round(dat$VRegionMutations / dat$VRegionNucleotides * 100, 2)
|
|
432 dat.clss = dat
|
|
433
|
|
434 dat.clss$best_match = substr(dat.clss$best_match, 0, 3)
|
|
435
|
|
436 dat.clss = rbind(dat, dat.clss)
|
|
437
|
|
438 p = ggplot(dat.clss, aes(best_match, percentage_mutations))
|
|
439 p = p + geom_point(aes(colour=best_match), position="jitter") + geom_boxplot(aes(middle=mean(percentage_mutations)), alpha=0.1, outlier.shape = NA)
|
5
|
440 p = p + xlab("Subclass") + ylab("Frequency") + ggtitle("Frequency scatter plot") + theme(panel.background = element_rect(fill = "white", colour="black"), text = element_text(size=16, colour="black"))
|
9
|
441 p = p + scale_fill_manual(values=c("IGA" = "blue4", "IGA1" = "lightblue1", "IGA2" = "blue4", "IGG" = "olivedrab3", "IGG1" = "olivedrab3", "IGG2" = "red", "IGG3" = "gold", "IGG4" = "darkred", "IGM" = "darkviolet", "IGE" = "darkorange", "all" = "blue4"))
|
39
|
442 p = p + scale_colour_manual(guide = guide_legend(title = "Subclass"), values=c("IGA" = "blue4", "IGA1" = "lightblue1", "IGA2" = "blue4", "IGG" = "olivedrab3", "IGG1" = "olivedrab3", "IGG2" = "red", "IGG3" = "gold", "IGG4" = "darkred", "IGM" = "darkviolet", "IGE" = "darkorange", "all" = "blue4"))
|
1
|
443
|
|
444 png(filename="scatter.png")
|
|
445 print(p)
|
|
446 dev.off()
|
|
447
|
42
|
448 ggsave("scatter.pdf", p)
|
|
449
|
1
|
450 write.table(dat[,c("Sequence.ID", "best_match", "VRegionMutations", "VRegionNucleotides", "percentage_mutations")], "scatter.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
|
451
|
|
452 print("Plotting frequency ranges plot")
|
|
453
|
|
454 dat$best_match_class = substr(dat$best_match, 0, 3)
|
|
455 freq_labels = c("0", "0-2", "2-5", "5-10", "10-15", "15-20", "20")
|
|
456 dat$frequency_bins = cut(dat$percentage_mutations, breaks=c(-Inf, 0, 2,5,10,15,20, Inf), labels=freq_labels)
|
|
457
|
|
458 frequency_bins_sum = data.frame(data.table(dat)[, list(class_sum=sum(.N)), by=c("best_match_class")])
|
|
459
|
|
460 frequency_bins_data = data.frame(data.table(dat)[, list(frequency_count=.N), by=c("best_match_class", "frequency_bins")])
|
|
461
|
|
462 frequency_bins_data = merge(frequency_bins_data, frequency_bins_sum, by="best_match_class")
|
|
463
|
|
464 frequency_bins_data$frequency = round(frequency_bins_data$frequency_count / frequency_bins_data$class_sum * 100, 2)
|
|
465
|
|
466 p = ggplot(frequency_bins_data, aes(frequency_bins, frequency))
|
5
|
467 p = p + geom_bar(aes(fill=best_match_class), stat="identity", position="dodge") + theme(panel.background = element_rect(fill = "white", colour="black"), text = element_text(size=16, colour="black"))
|
39
|
468 p = p + xlab("Frequency ranges") + ylab("Frequency") + ggtitle("Mutation Frequencies by class") + scale_fill_manual(guide = guide_legend(title = "Class"), values=c("IGA" = "blue4", "IGG" = "olivedrab3", "IGM" = "darkviolet", "IGE" = "darkorange", "all" = "blue4"))
|
1
|
469
|
|
470 png(filename="frequency_ranges.png")
|
|
471 print(p)
|
|
472 dev.off()
|
|
473
|
42
|
474 ggsave("frequency_ranges.pdf", p)
|
|
475
|
1
|
476 frequency_bins_data_by_class = frequency_bins_data
|
|
477
|
25
|
478 frequency_bins_data_by_class = frequency_bins_data_by_class[order(frequency_bins_data_by_class$best_match_class, frequency_bins_data_by_class$frequency_bins),]
|
|
479
|
|
480 frequency_bins_data_by_class$frequency_bins = gsub("-", " to ", frequency_bins_data_by_class$frequency_bins)
|
|
481 frequency_bins_data_by_class[frequency_bins_data_by_class$frequency_bins == "20", c("frequency_bins")] = "20 or higher"
|
|
482 frequency_bins_data_by_class[frequency_bins_data_by_class$frequency_bins == "0", c("frequency_bins")] = "0 or lower"
|
|
483
|
1
|
484 write.table(frequency_bins_data_by_class, "frequency_ranges_classes.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
|
485
|
|
486 frequency_bins_data = data.frame(data.table(dat)[, list(frequency_count=.N), by=c("best_match", "best_match_class", "frequency_bins")])
|
|
487
|
23
|
488 frequency_bins_sum = data.frame(data.table(dat)[, list(class_sum=sum(.N)), by=c("best_match")])
|
|
489
|
|
490 frequency_bins_data = merge(frequency_bins_data, frequency_bins_sum, by="best_match")
|
1
|
491
|
|
492 frequency_bins_data$frequency = round(frequency_bins_data$frequency_count / frequency_bins_data$class_sum * 100, 2)
|
|
493
|
25
|
494 frequency_bins_data = frequency_bins_data[order(frequency_bins_data$best_match, frequency_bins_data$frequency_bins),]
|
|
495 frequency_bins_data$frequency_bins = gsub("-", " to ", frequency_bins_data$frequency_bins)
|
|
496 frequency_bins_data[frequency_bins_data$frequency_bins == "20", c("frequency_bins")] = "20 or higher"
|
|
497 frequency_bins_data[frequency_bins_data$frequency_bins == "0", c("frequency_bins")] = "0 or lower"
|
|
498
|
1
|
499 write.table(frequency_bins_data, "frequency_ranges_subclasses.txt", sep="\t",quote=F,row.names=F,col.names=T)
|
|
500
|
|
501
|
|
502
|
|
503
|
|
504
|
|
505
|
|
506
|
|
507
|
|
508
|
|
509
|
|
510
|
|
511
|
|
512
|
|
513
|
|
514
|
|
515
|
|
516
|
|
517
|
|
518
|
|
519
|
|
520
|
|
521
|
|
522
|
|
523
|
|
524
|
|
525
|
|
526
|
|
527
|
|
528
|
|
529
|
|
530
|
|
531
|
|
532
|
|
533
|
|
534
|
|
535
|
|
536
|
|
537
|
|
538
|
|
539
|
|
540
|
|
541
|
|
542
|
|
543
|
|
544
|
|
545
|
|
546
|
|
547
|
|
548
|
|
549
|
|
550
|
|
551
|
|
552
|
|
553
|
|
554
|
|
555
|