annotate pattern_plots.r @ 39:a24f8c93583a draft

Uploaded
author davidvanzessen
date Thu, 22 Dec 2016 09:39:27 -0500
parents 05c62efdc393
children 1cf60ae234b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1 library(ggplot2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2 library(reshape2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
3 library(scales)
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.file = args[1] #the data that's get turned into the "SHM overview" table in the html report "data_sum.txt"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
8
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
9 plot1.path = args[2]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
10 plot1.png = paste(plot1.path, ".png", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
11 plot1.txt = paste(plot1.path, ".txt", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
12
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
13 plot2.path = args[3]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
14 plot2.png = paste(plot2.path, ".png", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
15 plot2.txt = paste(plot2.path, ".txt", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
16
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
17 plot3.path = args[4]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
18 plot3.png = paste(plot3.path, ".png", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
19 plot3.txt = paste(plot3.path, ".txt", sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
20
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
21 clean.output = args[5]
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
22
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
23 dat = read.table(input.file, header=F, sep=",", quote="", stringsAsFactors=F, fill=T, row.names=1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
24
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
25
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
26
5
012a738edf5a Uploaded
davidvanzessen
parents: 1
diff changeset
27 classes = c("IGA", "IGA1", "IGA2", "IGG", "IGG1", "IGG2", "IGG3", "IGG4", "IGM", "IGE")
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
28 xyz = c("x", "y", "z")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
29 new.names = c(paste(rep(classes, each=3), xyz, sep="."), paste("un", xyz, sep="."), paste("all", xyz, sep="."))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
30
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
31 names(dat) = new.names
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
32
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
33 clean.dat = dat
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
34 clean.dat = clean.dat[,c(paste(rep(classes, each=3), xyz, sep="."), paste("all", xyz, sep="."), paste("un", xyz, sep="."))]
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
35
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
36 write.table(clean.dat, clean.output, quote=F, sep="\t", na="", row.names=T, col.names=NA)
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
37
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
38 dat["RGYW.WRCY",] = colSums(dat[c(13,14),], na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
39 dat["TW.WA",] = colSums(dat[c(15,16),], na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
40
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
41 data1 = dat[c("RGYW.WRCY", "TW.WA"),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
42
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
43 data1 = data1[,names(data1)[grepl(".z", names(data1))]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
44 names(data1) = gsub("\\..*", "", names(data1))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
45
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
46 data1 = melt(t(data1))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
47
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
48 names(data1) = c("Class", "Type", "value")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
49
38
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
50 chk = is.na(data1$value)
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
51 if(any(chk)){
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
52 data1[chk, "value"] = 0
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
53 }
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
54
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
55 data1 = data1[order(data1$Type),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
56
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
57 write.table(data1, plot1.txt, quote=F, sep="\t", na="", row.names=F, col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
58
39
a24f8c93583a Uploaded
davidvanzessen
parents: 38
diff changeset
59 p = ggplot(data1, aes(Class, value)) + geom_bar(aes(fill=Type), stat="identity", position="dodge", colour = "black") + ylab("% of mutations") + guides(fill=guide_legend(title=NULL)) + ggtitle("Percentage of mutations in AID and pol eta motives")
5
012a738edf5a Uploaded
davidvanzessen
parents: 1
diff changeset
60 p = p + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1)) + scale_fill_manual(values=c("RGYW.WRCY" = "white", "TW.WA" = "blue4"))
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
61 #p = p + scale_colour_manual(values=c("RGYW.WRCY" = "black", "TW.WA" = "blue4"))
39
a24f8c93583a Uploaded
davidvanzessen
parents: 38
diff changeset
62 png(filename=plot1.png, width=510, height=300)
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
63 print(p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
64 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
65
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
66 data2 = dat[c(1, 5:8),]
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
67
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
68 data2 = data2[,names(data2)[grepl("\\.x", names(data2))]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
69 names(data2) = gsub(".x", "", names(data2))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
70
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
71 data2["A/T",] = dat["Targeting of A T (%)",names(dat)[grepl("\\.z", names(dat))]]
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
72
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
73 data2["G/C transitions",] = round(data2["Transitions at G C (%)",] / data2["Number of Mutations (%)",] * 100, 1)
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
74
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
75 data2["mutation.at.gc",] = dat["Transitions at G C (%)",names(dat)[grepl("\\.y", names(dat))]]
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
76 data2["G/C transversions",] = round((data2["mutation.at.gc",] - data2["Transitions at G C (%)",]) / data2["Number of Mutations (%)",] * 100, 1)
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
77
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
78 data2["G/C transversions",is.nan(unlist(data2["G/C transversions",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
79 data2["G/C transversions",is.infinite(unlist(data2["G/C transversions",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
80 data2["G/C transitions",is.nan(unlist(data2["G/C transitions",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
81 data2["G/C transitions",is.infinite(unlist(data2["G/C transitions",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
82
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
83 data2 = melt(t(data2[c("A/T","G/C transitions","G/C transversions"),]))
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
84
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
85 names(data2) = c("Class", "Type", "value")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
86
38
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
87 chk = is.na(data2$value)
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
88 if(any(chk)){
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
89 data2[chk, "value"] = 0
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
90 }
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
91
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
92 data2 = data2[order(data2$Type),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
93
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
94 write.table(data2, plot2.txt, quote=F, sep="\t", na="", row.names=F, col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
95
39
a24f8c93583a Uploaded
davidvanzessen
parents: 38
diff changeset
96 p = ggplot(data2, aes(x=Class, y=value, fill=Type)) + geom_bar(position="fill", stat="identity", colour = "black") + scale_y_continuous(labels=percent_format()) + guides(fill=guide_legend(title=NULL)) + ylab("% of mutations") + ggtitle("Relative mutation patterns")
5
012a738edf5a Uploaded
davidvanzessen
parents: 1
diff changeset
97 p = p + theme(panel.background = element_rect(fill = "white", colour="black"), text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1)) + scale_fill_manual(values=c("A/T" = "blue4", "G/C transversions" = "gray74", "G/C transitions" = "white"))
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
98 #p = p + scale_colour_manual(values=c("A/T" = "blue4", "G/C transversions" = "gray74", "G/C transitions" = "black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
99 png(filename=plot2.png, width=480, height=300)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
100 print(p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
101 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
102
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
103 data3 = dat[c(5, 6, 8, 17:20),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
104 data3 = data3[,names(data3)[grepl("\\.x", names(data3))]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
105 names(data3) = gsub(".x", "", names(data3))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
106
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
107 data3["G/C transitions",] = round(data3["Transitions at G C (%)",] / (data3["C",] + data3["G",]) * 100, 1)
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
108
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
109 data3["G/C transversions",] = round((data3["Targeting of G C (%)",] - data3["Transitions at G C (%)",]) / (data3["C",] + data3["G",]) * 100, 1)
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
110
23
81453585dfc3 Uploaded
davidvanzessen
parents: 5
diff changeset
111 data3["A/T",] = round(data3["Targeting of A T (%)",] / (data3["A",] + data3["T",]) * 100, 1)
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
112
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
113 data3["G/C transitions",is.nan(unlist(data3["G/C transitions",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
114 data3["G/C transitions",is.infinite(unlist(data3["G/C transitions",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
115
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
116 data3["G/C transversions",is.nan(unlist(data3["G/C transversions",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
117 data3["G/C transversions",is.infinite(unlist(data3["G/C transversions",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
118
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
119 data3["A/T",is.nan(unlist(data3["A/T",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
120 data3["A/T",is.infinite(unlist(data3["A/T",]))] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
121
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
122 data3 = melt(t(data3[8:10,]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
123 names(data3) = c("Class", "Type", "value")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
124
39
a24f8c93583a Uploaded
davidvanzessen
parents: 38
diff changeset
125 chk = is.na(data3$value)
38
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
126 if(any(chk)){
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
127 data3[chk, "value"] = 0
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
128 }
05c62efdc393 Uploaded
davidvanzessen
parents: 23
diff changeset
129
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
130 data3 = data3[order(data3$Type),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
131
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
132 write.table(data3, plot3.txt, quote=F, sep="\t", na="", row.names=F, col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
133
39
a24f8c93583a Uploaded
davidvanzessen
parents: 38
diff changeset
134 p = ggplot(data3, aes(Class, value)) + geom_bar(aes(fill=Type), stat="identity", position="dodge", colour = "black") + ylab("% of nucleotides") + guides(fill=guide_legend(title=NULL)) + ggtitle("Absolute mutation patterns")
5
012a738edf5a Uploaded
davidvanzessen
parents: 1
diff changeset
135 p = p + theme(panel.background = element_rect(fill = "white", colour="black"), text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1)) + scale_fill_manual(values=c("A/T" = "blue4", "G/C transversions" = "gray74", "G/C transitions" = "white"))
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
136 #p = p + scale_colour_manual(values=c("A/T" = "blue4", "G/C transversions" = "gray74", "G/C transitions" = "black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
137 png(filename=plot3.png, width=480, height=300)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
138 print(p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
139 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
140
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
141
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
142
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
143
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
144
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
145
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
146
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
147
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
148
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
149
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
150
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
151
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
152
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
153
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
154
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
155
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
156
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
157
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
158
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
159
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
160
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
161
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
162
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
163
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
164
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
165
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
166
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
167
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
168
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
169
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
170
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
171