annotate shm_csr.r @ 0:c33d93683a09 draft

Uploaded
author davidvanzessen
date Thu, 13 Oct 2016 10:52:24 -0400
parents
children faae21ba5c63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1 library(data.table)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2 library(ggplot2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
3 library(reshape2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
4
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
5 args <- commandArgs(trailingOnly = TRUE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
6
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
7 input = args[1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
8 genes = unlist(strsplit(args[2], ","))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
9 outputdir = args[3]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
10 include_fr1 = ifelse(args[4] == "yes", T, F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
11 setwd(outputdir)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
12
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
13 dat = read.table(input, header=T, sep="\t", fill=T, stringsAsFactors=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
14
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
15 if(length(dat$Sequence.ID) == 0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
16 setwd(outputdir)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
17 result = data.frame(x = rep(0, 5), y = rep(0, 5), z = rep(NA, 5))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
18 row.names(result) = c("Number of Mutations (%)", "Transition (%)", "Transversions (%)", "Transitions at G C (%)", "Targeting of C G (%)")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
19 write.table(x=result, file="mutations.txt", sep=",",quote=F,row.names=T,col.names=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
20 transitionTable = data.frame(A=rep(0, 4),C=rep(0, 4),G=rep(0, 4),T=rep(0, 4))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
21 row.names(transitionTable) = c("A", "C", "G", "T")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
22 transitionTable["A","A"] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
23 transitionTable["C","C"] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
24 transitionTable["G","G"] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
25 transitionTable["T","T"] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
26 write.table(x=transitionTable, file="transitions.txt", sep=",",quote=F,row.names=T,col.names=NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
27 cat("0", file="n.txt")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
28 stop("No data")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
29 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
30
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
31 cleanup_columns = c("FR1.IMGT.c.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
32 "FR2.IMGT.g.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
33 "CDR1.IMGT.Nb.of.nucleotides",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
34 "CDR2.IMGT.t.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
35 "FR1.IMGT.c.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
36 "CDR1.IMGT.c.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
37 "FR2.IMGT.a.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
38 "FR2.IMGT.Nb.of.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
39 "FR2.IMGT.g.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
40 "FR2.IMGT.a.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
41 "FR3.IMGT.t.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
42 "FR3.IMGT.t.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
43 "FR2.IMGT.g.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
44 "FR3.IMGT.c.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
45 "FR1.IMGT.Nb.of.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
46 "CDR1.IMGT.g.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
47 "CDR1.IMGT.t.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
48 "CDR1.IMGT.g.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
49 "CDR2.IMGT.Nb.of.nucleotides",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
50 "FR2.IMGT.a.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
51 "CDR1.IMGT.Nb.of.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
52 "CDR3.IMGT.Nb.of.nucleotides",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
53 "CDR1.IMGT.a.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
54 "FR3.IMGT.a.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
55 "FR1.IMGT.g.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
56 "FR3.IMGT.a.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
57 "FR1.IMGT.a.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
58 "CDR2.IMGT.a.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
59 "CDR2.IMGT.Nb.of.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
60 "CDR2.IMGT.g.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
61 "CDR2.IMGT.a.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
62 "CDR1.IMGT.t.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
63 "FR3.IMGT.g.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
64 "FR1.IMGT.g.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
65 "FR3.IMGT.g.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
66 "CDR1.IMGT.a.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
67 "FR1.IMGT.a.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
68 "FR3.IMGT.a.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
69 "FR3.IMGT.Nb.of.nucleotides",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
70 "FR2.IMGT.t.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
71 "CDR2.IMGT.g.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
72 "FR2.IMGT.t.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
73 "CDR1.IMGT.t.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
74 "FR2.IMGT.t.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
75 "FR3.IMGT.t.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
76 "FR2.IMGT.Nb.of.nucleotides",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
77 "FR1.IMGT.t.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
78 "FR1.IMGT.t.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
79 "FR3.IMGT.c.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
80 "FR1.IMGT.t.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
81 "CDR2.IMGT.a.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
82 "FR2.IMGT.c.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
83 "CDR1.IMGT.g.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
84 "CDR2.IMGT.t.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
85 "FR1.IMGT.Nb.of.nucleotides",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
86 "CDR1.IMGT.c.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
87 "CDR2.IMGT.t.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
88 "FR3.IMGT.g.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
89 "CDR1.IMGT.a.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
90 "FR2.IMGT.c.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
91 "FR3.IMGT.Nb.of.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
92 "FR2.IMGT.c.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
93 "CDR2.IMGT.g.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
94 "FR1.IMGT.g.c",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
95 "CDR2.IMGT.c.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
96 "FR3.IMGT.c.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
97 "CDR1.IMGT.c.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
98 "CDR2.IMGT.c.g",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
99 "CDR2.IMGT.c.a",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
100 "FR1.IMGT.c.t",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
101 "FR1.IMGT.Nb.of.silent.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
102 "FR2.IMGT.Nb.of.silent.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
103 "FR3.IMGT.Nb.of.silent.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
104 "FR1.IMGT.Nb.of.nonsilent.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
105 "FR2.IMGT.Nb.of.nonsilent.mutations",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
106 "FR3.IMGT.Nb.of.nonsilent.mutations")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
107
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
108
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
109 print("Cleaning up columns")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
110 for(col in cleanup_columns){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
111 dat[,col] = gsub("\\(.*\\)", "", dat[,col])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
112 #dat[dat[,col] == "",] = "0"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
113 dat[,col] = as.numeric(dat[,col])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
114 dat[is.na(dat[,col]),col] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
115 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
116
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
117 regions = c("FR1", "CDR1", "FR2", "CDR2", "FR3")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
118 if(!include_fr1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
119 regions = c("CDR1", "FR2", "CDR2", "FR3")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
120 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
121
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
122 sum_by_row = function(x, columns) { sum(as.numeric(x[columns]), na.rm=T) }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
123
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
124 print("aggregating data into new columns")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
125
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
126 VRegionMutations_columns = paste(regions, ".IMGT.Nb.of.mutations", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
127 dat$VRegionMutations = apply(dat, FUN=sum_by_row, 1, columns=VRegionMutations_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
128
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
129 VRegionNucleotides_columns = paste(regions, ".IMGT.Nb.of.nucleotides", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
130 dat$FR3.IMGT.Nb.of.nucleotides = nchar(dat$FR3.IMGT.seq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
131 dat$VRegionNucleotides = apply(dat, FUN=sum_by_row, 1, columns=VRegionNucleotides_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
132
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
133 transitionMutations_columns = paste(rep(regions, each=4), c(".IMGT.a.g", ".IMGT.g.a", ".IMGT.c.t", ".IMGT.t.c"), sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
134 dat$transitionMutations = apply(dat, FUN=sum_by_row, 1, columns=transitionMutations_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
135
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
136 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="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
137 dat$transversionMutations = apply(dat, FUN=sum_by_row, 1, columns=transversionMutations_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
138
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
139
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
140 transitionMutationsAtGC_columns = paste(rep(regions, each=2), c(".IMGT.g.a",".IMGT.c.t"), sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
141 dat$transitionMutationsAtGC = apply(dat, FUN=sum_by_row, 1, columns=transitionMutationsAtGC_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
142
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
143
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
144 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="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
145 #totalMutationsAtGC_columns = paste(rep(regions, each=6), c(".IMGT.g.a",".IMGT.c.t",".IMGT.c.a",".IMGT.c.g",".IMGT.g.t"), sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
146 dat$totalMutationsAtGC = apply(dat, FUN=sum_by_row, 1, columns=totalMutationsAtGC_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
147
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
148 transitionMutationsAtAT_columns = paste(rep(regions, each=2), c(".IMGT.a.g",".IMGT.t.c"), sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
149 dat$transitionMutationsAtAT = apply(dat, FUN=sum_by_row, 1, columns=transitionMutationsAtAT_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
150
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
151 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="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
152 #totalMutationsAtAT_columns = paste(rep(regions, each=5), c(".IMGT.a.g",".IMGT.t.c",".IMGT.a.c",".IMGT.g.c",".IMGT.t.g"), sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
153 dat$totalMutationsAtAT = apply(dat, FUN=sum_by_row, 1, columns=totalMutationsAtAT_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
154
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
155
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
156 FRRegions = regions[grepl("FR", regions)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
157 CDRRegions = regions[grepl("CDR", regions)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
158
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
159 FR_silentMutations_columns = paste(FRRegions, ".IMGT.Nb.of.silent.mutations", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
160 dat$silentMutationsFR = apply(dat, FUN=sum_by_row, 1, columns=FR_silentMutations_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
161
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
162 CDR_silentMutations_columns = paste(CDRRegions, ".IMGT.Nb.of.silent.mutations", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
163 dat$silentMutationsCDR = apply(dat, FUN=sum_by_row, 1, columns=CDR_silentMutations_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
164
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
165 FR_nonSilentMutations_columns = paste(FRRegions, ".IMGT.Nb.of.nonsilent.mutations", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
166 dat$nonSilentMutationsFR = apply(dat, FUN=sum_by_row, 1, columns=FR_nonSilentMutations_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
167
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
168 CDR_nonSilentMutations_columns = paste(CDRRegions, ".IMGT.Nb.of.nonsilent.mutations", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
169 dat$nonSilentMutationsCDR = apply(dat, FUN=sum_by_row, 1, columns=CDR_nonSilentMutations_columns)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
170
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
171 mutation.sum.columns = c("Sequence.ID", "VRegionMutations", "VRegionNucleotides", "transitionMutations", "transversionMutations", "transitionMutationsAtGC", "transitionMutationsAtAT", "silentMutationsFR", "nonSilentMutationsFR", "silentMutationsCDR", "nonSilentMutationsCDR")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
172
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
173 write.table(dat[,mutation.sum.columns], "mutation_by_id.txt", sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
174
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
175 setwd(outputdir)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
176
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
177 base.order = data.frame(base=c("A", "T", "C", "G"), order=1:4)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
178
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
179 calculate_result = function(i, gene, dat, matrx, f, fname, name){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
180 tmp = dat[grepl(paste("^", gene, ".*", sep=""), dat$best_match),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
181
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
182 j = i - 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
183 x = (j * 3) + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
184 y = (j * 3) + 2
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
185 z = (j * 3) + 3
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
186
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
187 if(nrow(tmp) > 0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
188
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
189 if(fname == "sum"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
190 matrx[1,x] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
191 matrx[1,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
192 matrx[1,z] = round(f(matrx[1,x] / matrx[1,y]) * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
193 } else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
194 matrx[1,x] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
195 matrx[1,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
196 matrx[1,z] = round(f(tmp$VRegionMutations / tmp$VRegionNucleotides) * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
197 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
198
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
199 matrx[2,x] = round(f(tmp$transitionMutations, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
200 matrx[2,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
201 matrx[2,z] = round(matrx[2,x] / matrx[2,y] * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
202
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
203 matrx[3,x] = round(f(tmp$transversionMutations, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
204 matrx[3,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
205 matrx[3,z] = round(matrx[3,x] / matrx[3,y] * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
206
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
207 matrx[4,x] = round(f(tmp$transitionMutationsAtGC, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
208 matrx[4,y] = round(f(tmp$totalMutationsAtGC, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
209 matrx[4,z] = round(matrx[4,x] / matrx[4,y] * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
210
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
211 matrx[5,x] = round(f(tmp$totalMutationsAtGC, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
212 matrx[5,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
213 matrx[5,z] = round(matrx[5,x] / matrx[5,y] * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
214
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
215 matrx[6,x] = round(f(tmp$transitionMutationsAtAT, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
216 matrx[6,y] = round(f(tmp$totalMutationsAtAT, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
217 matrx[6,z] = round(matrx[6,x] / matrx[6,y] * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
218
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
219 matrx[7,x] = round(f(tmp$totalMutationsAtAT, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
220 matrx[7,y] = round(f(tmp$VRegionMutations, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
221 matrx[7,z] = round(matrx[7,x] / matrx[7,y] * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
222
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
223 matrx[8,x] = round(f(tmp$nonSilentMutationsFR, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
224 matrx[8,y] = round(f(tmp$silentMutationsFR, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
225 matrx[8,z] = round(matrx[8,x] / matrx[8,y], digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
226
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
227 matrx[9,x] = round(f(tmp$nonSilentMutationsCDR, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
228 matrx[9,y] = round(f(tmp$silentMutationsCDR, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
229 matrx[9,z] = round(matrx[9,x] / matrx[9,y], digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
230
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
231 if(fname == "sum"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
232 matrx[10,x] = round(f(rowSums(tmp[,c("FR2.IMGT.Nb.of.nucleotides", "FR3.IMGT.Nb.of.nucleotides")], na.rm=T)), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
233 matrx[10,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
234 matrx[10,z] = round(matrx[10,x] / matrx[10,y] * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
235
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
236 matrx[11,x] = round(f(rowSums(tmp[,c("CDR1.IMGT.Nb.of.nucleotides", "CDR2.IMGT.Nb.of.nucleotides")], na.rm=T)), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
237 matrx[11,y] = round(f(tmp$VRegionNucleotides, na.rm=T), digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
238 matrx[11,z] = round(matrx[11,x] / matrx[11,y] * 100, digits=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
239 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
240 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
241
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
242 transitionTable = data.frame(A=zeros,C=zeros,G=zeros,T=zeros)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
243 row.names(transitionTable) = c("A", "C", "G", "T")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
244 transitionTable["A","A"] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
245 transitionTable["C","C"] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
246 transitionTable["G","G"] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
247 transitionTable["T","T"] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
248
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
249 if(nrow(tmp) > 0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
250 for(nt1 in nts){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
251 for(nt2 in nts){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
252 if(nt1 == nt2){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
253 next
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
254 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
255 NT1 = LETTERS[letters == nt1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
256 NT2 = LETTERS[letters == nt2]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
257 FR1 = paste("FR1.IMGT.", nt1, ".", nt2, sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
258 CDR1 = paste("CDR1.IMGT.", nt1, ".", nt2, sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
259 FR2 = paste("FR2.IMGT.", nt1, ".", nt2, sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
260 CDR2 = paste("CDR2.IMGT.", nt1, ".", nt2, sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
261 FR3 = paste("FR3.IMGT.", nt1, ".", nt2, sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
262 if(include_fr1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
263 transitionTable[NT1,NT2] = sum(tmp[,c(FR1, CDR1, FR2, CDR2, FR3)])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
264 } else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
265 transitionTable[NT1,NT2] = sum(tmp[,c(CDR1, FR2, CDR2, FR3)])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
266 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
267 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
268 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
269 transition = transitionTable
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
270 transition$id = names(transition)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
271
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
272 transition2 = melt(transition, id.vars="id")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
273
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
274 transition2 = merge(transition2, base.order, by.x="id", by.y="base")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
275 transition2 = merge(transition2, base.order, by.x="variable", by.y="base")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
276
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
277 transition2[is.na(transition2$value),]$value = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
278
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
279 if(!all(transition2$value == 0)){ #having rows of data but a transition table filled with 0 is bad
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
280
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
281 print("Plotting stacked transition")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
282
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
283 png(filename=paste("transitions_stacked_", name, ".png", sep=""))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
284 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
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
285 p = p + xlab("From base") + ylab("To base") + ggtitle("Mutations frequency from base to base") + guides(fill=guide_legend(title=NULL))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
286 p = p + theme(panel.background = element_rect(fill = "white", colour="black")) + scale_fill_manual(values=c("A" = "blue4", "G" = "lightblue1", "C" = "olivedrab3", "T" = "olivedrab4"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
287 #p = p + scale_colour_manual(values=c("A" = "black", "G" = "black", "C" = "black", "T" = "black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
288 print(p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
289 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
290
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
291 print("Plotting heatmap transition")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
292
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
293 png(filename=paste("transitions_heatmap_", name, ".png", sep=""))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
294 p = ggplot(transition2, aes(factor(reorder(id, order.x)), factor(reorder(variable, order.y)))) + geom_tile(aes(fill = value)) + scale_fill_gradient(low="white", high="steelblue") #heatmap
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
295 p = p + xlab("From base") + ylab("To base") + ggtitle("Mutations frequency from base to base") + theme(panel.background = element_rect(fill = "white", colour="black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
296 print(p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
297 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
298 } else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
299 print("No data to plot")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
300 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
301 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
302
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
303 #print(paste("writing value file: ", name, "_", fname, "_value.txt" ,sep=""))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
304
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
305 write.table(x=transitionTable, file=paste("transitions_", name ,"_", fname, ".txt", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
306 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)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
307
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
308 cat(matrx[1,x], file=paste(name, "_", fname, "_value.txt" ,sep=""))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
309 cat(nrow(tmp), file=paste(name, "_", fname, "_n.txt" ,sep=""))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
310
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
311 #print(paste(fname, name, nrow(tmp)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
312
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
313 matrx
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
314 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
315
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
316 nts = c("a", "c", "g", "t")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
317 zeros=rep(0, 4)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
318
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
319 funcs = c(median, sum, mean)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
320 fnames = c("median", "sum", "mean")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
321
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
322 print("Creating result tables")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
323
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
324 for(i in 1:length(funcs)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
325 func = funcs[[i]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
326 fname = fnames[[i]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
327
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
328 rows = 9
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
329 if(fname == "sum"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
330 rows = 11
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
331 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
332 matrx = matrix(data = 0, ncol=((length(genes) + 1) * 3),nrow=rows)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
333
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
334 for(i in 1:length(genes)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
335 print(paste("Creating table for", fname, genes[i]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
336 matrx = calculate_result(i, genes[i], dat, matrx, func, fname, genes[i])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
337 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
338
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
339 matrx = calculate_result(i + 1, ".*", dat[!grepl("unmatched", dat$best_match),], matrx, func, fname, name="all")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
340
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
341 result = data.frame(matrx)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
342 if(fname == "sum"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
343 row.names(result) = c("Number of Mutations (%)", "Transitions (%)", "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)", "nt in FR", "nt in CDR")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
344 } else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
345 row.names(result) = c("Number of Mutations (%)", "Transitions (%)", "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)")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
346 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
347
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
348 write.table(x=result, file=paste("mutations_", fname, ".txt", sep=""), sep=",",quote=F,row.names=T,col.names=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
349 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
350
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
351 print("Adding median number of mutations to sum table")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
352
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
353 sum.table = read.table("mutations_sum.txt", sep=",", header=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
354 median.table = read.table("mutations_median.txt", sep=",", header=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
355
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
356 new.table = sum.table[1,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
357 new.table[2,] = median.table[1,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
358 new.table[3:12,] = sum.table[2:11,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
359 new.table[,1] = as.character(new.table[,1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
360 new.table[2,1] = "Median of Number of Mutations (%)"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
361
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
362 #sum.table = sum.table[c("Number of Mutations (%)", "Median of 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)", "nt in FR", "nt in CDR"),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
363
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
364 write.table(x=new.table, file="mutations_sum.txt", sep=",",quote=F,row.names=F,col.names=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
365
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
366
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
367 print("Plotting IGA piechart")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
368
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
369 dat = dat[!grepl("^unmatched", dat$best_match),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
370
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
371 #blegh
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
372 genesForPlot = dat[grepl("IGA", dat$best_match),]$best_match
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
373 if(length(genesForPlot) > 0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
374 genesForPlot = data.frame(table(genesForPlot))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
375 colnames(genesForPlot) = c("Gene","Freq")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
376 genesForPlot$label = paste(genesForPlot$Gene, "-", genesForPlot$Freq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
377
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
378 pc = ggplot(genesForPlot, aes(x = factor(1), y=Freq, fill=Gene))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
379 pc = pc + geom_bar(width = 1, stat = "identity") + scale_fill_manual(labels=genesForPlot$label, values=c("IGA1" = "lightblue1", "IGA2" = "blue4"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
380 pc = pc + coord_polar(theta="y")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
381 pc = pc + theme(panel.background = element_rect(fill = "white", colour="black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
382 pc = pc + xlab(" ") + ylab(" ") + ggtitle(paste("IGA subclasses", "( n =", sum(genesForPlot$Freq), ")"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
383 write.table(genesForPlot, "IGA.txt", sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
384
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
385 png(filename="IGA.png")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
386 print(pc)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
387 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
388 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
389
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
390 print("Plotting IGG piechart")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
391
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
392 genesForPlot = dat[grepl("IGG", dat$best_match),]$best_match
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
393 if(length(genesForPlot) > 0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
394 genesForPlot = data.frame(table(genesForPlot))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
395 colnames(genesForPlot) = c("Gene","Freq")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
396 genesForPlot$label = paste(genesForPlot$Gene, "-", genesForPlot$Freq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
397
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
398 pc = ggplot(genesForPlot, aes(x = factor(1), y=Freq, fill=Gene))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
399 pc = pc + geom_bar(width = 1, stat = "identity") + scale_fill_manual(labels=genesForPlot$label, values=c("IGG1" = "olivedrab3", "IGG2" = "red", "IGG3" = "gold", "IGG4" = "darkred"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
400 pc = pc + coord_polar(theta="y")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
401 pc = pc + theme(panel.background = element_rect(fill = "white", colour="black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
402 pc = pc + xlab(" ") + ylab(" ") + ggtitle(paste("IGG subclasses", "( n =", sum(genesForPlot$Freq), ")"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
403 write.table(genesForPlot, "IGG.txt", sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
404
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
405 png(filename="IGG.png")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
406 print(pc)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
407 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
408 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
409
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
410
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
411 print("Plotting scatterplot")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
412
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
413 dat$percentage_mutations = round(dat$VRegionMutations / dat$VRegionNucleotides * 100, 2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
414
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
415 p = ggplot(dat, aes(best_match, percentage_mutations))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
416 p = p + geom_point(aes(colour=best_match), position="jitter") + geom_boxplot(aes(middle=mean(percentage_mutations)), alpha=0.1, outlier.shape = NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
417 p = p + xlab("Subclass") + ylab("Frequency") + ggtitle("Frequency scatter plot") + theme(panel.background = element_rect(fill = "white", colour="black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
418 p = p + scale_fill_manual(values=c("IGA1" = "lightblue1", "IGA2" = "blue4", "IGG1" = "olivedrab3", "IGG2" = "red", "IGG3" = "gold", "IGG4" = "darkred", "IGM" = "black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
419 p = p + scale_colour_manual(values=c("IGA1" = "lightblue1", "IGA2" = "blue4", "IGG1" = "olivedrab3", "IGG2" = "red", "IGG3" = "gold", "IGG4" = "darkred", "IGM" = "black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
420
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
421 png(filename="scatter.png")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
422 print(p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
423 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
424
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
425 write.table(dat[,c("Sequence.ID", "best_match", "VRegionMutations", "VRegionNucleotides", "percentage_mutations")], "scatter.txt", sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
426
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
427 write.table(dat, input, sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
428
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
429
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
430 print("Plotting frequency ranges plot")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
431
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
432 dat$best_match_class = substr(dat$best_match, 0, 3)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
433 freq_labels = c("0", "0-2", "2-5", "5-10", "10-15", "15-20", "20")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
434 dat$frequency_bins = cut(dat$percentage_mutations, breaks=c(-Inf, 0, 2,5,10,15,20, Inf), labels=freq_labels)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
435
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
436 frequency_bins_sum = data.frame(data.table(dat)[, list(class_sum=sum(.N)), by=c("best_match_class")])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
437
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
438 frequency_bins_data = data.frame(data.table(dat)[, list(frequency_count=.N), by=c("best_match_class", "frequency_bins")])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
439
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
440 frequency_bins_data = merge(frequency_bins_data, frequency_bins_sum, by="best_match_class")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
441
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
442 frequency_bins_data$frequency = round(frequency_bins_data$frequency_count / frequency_bins_data$class_sum * 100, 2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
443
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
444 p = ggplot(frequency_bins_data, aes(frequency_bins, frequency))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
445 p = p + geom_bar(aes(fill=best_match_class), stat="identity", position="dodge") + theme(panel.background = element_rect(fill = "white", colour="black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
446 p = p + xlab("Frequency ranges") + ylab("Frequency") + ggtitle("Mutation Frequencies by class") + scale_fill_manual(values=c("IGA" = "blue4", "IGG" = "olivedrab3", "IGM" = "black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
447
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
448 png(filename="frequency_ranges.png")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
449 print(p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
450 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
451
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
452 frequency_bins_data_by_class = frequency_bins_data
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
453
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
454 write.table(frequency_bins_data_by_class, "frequency_ranges_classes.txt", sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
455
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
456 frequency_bins_data = data.frame(data.table(dat)[, list(frequency_count=.N), by=c("best_match", "best_match_class", "frequency_bins")])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
457
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
458 frequency_bins_data = merge(frequency_bins_data, frequency_bins_sum, by="best_match_class")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
459
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
460 frequency_bins_data$frequency = round(frequency_bins_data$frequency_count / frequency_bins_data$class_sum * 100, 2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
461
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
462 write.table(frequency_bins_data, "frequency_ranges_subclasses.txt", sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
463
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
464
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
465 #frequency_bins_data_by_class
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
466 #frequency_ranges_subclasses.txt
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
467
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
468
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
469
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
470
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
471
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
472
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
473
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
474
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
475
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
476
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
477
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
478
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
479
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
480
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
481
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
482
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
483
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
484
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
485
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
486
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
487
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
488
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
489
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
490
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
491
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
492
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
493