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