annotate pattern_plots.r @ 0:8a5a2abbb870 draft default tip

Uploaded
author davidvanzessen
date Mon, 29 Aug 2016 05:36:10 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
1 library(ggplot2)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
2 library(reshape2)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
3 library(scales)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
4
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
5 args <- commandArgs(trailingOnly = TRUE)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
6
8a5a2abbb870 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"
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
8
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
9 plot1.path = args[2]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
10 plot1.png = paste(plot1.path, ".png", sep="")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
11 plot1.txt = paste(plot1.path, ".txt", sep="")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
12
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
13 plot2.path = args[3]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
14 plot2.png = paste(plot2.path, ".png", sep="")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
15 plot2.txt = paste(plot2.path, ".txt", sep="")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
16
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
17 plot3.path = args[4]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
18 plot3.png = paste(plot3.path, ".png", sep="")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
19 plot3.txt = paste(plot3.path, ".txt", sep="")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
20
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
21 dat = read.table(input.file, header=F, sep=",", quote="", stringsAsFactors=F, fill=T, row.names=1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
22
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
23
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
24
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
25 classes = c("ca", "ca1", "ca2", "cg", "cg1", "cg2", "cg3", "cg4", "cm")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
26 xyz = c("x", "y", "z")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
27 new.names = c(paste(rep(classes, each=3), xyz, sep="."), paste("un", xyz, sep="."), paste("all", xyz, sep="."))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
28
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
29 names(dat) = new.names
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
30
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
31 dat["RGYW.WRCY",] = colSums(dat[c(13,14),])
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
32 dat["TW.WA",] = colSums(dat[c(15,16),])
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
33
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
34 data1 = dat[c("RGYW.WRCY", "TW.WA"),]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
35
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
36 data1 = data1[,names(data1)[grepl(".z", names(data1))]]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
37 names(data1) = gsub("\\..*", "", names(data1))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
38
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
39 data1 = melt(t(data1))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
40
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
41 names(data1) = c("Class", "Type", "value")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
42
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
43 write.table(data1, plot1.txt, quote=F, sep="\t", na="", row.names=F, col.names=T)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
44
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
45 p = ggplot(data1, aes(Class, value)) + geom_bar(aes(fill=Type), stat="identity", position="dodge") + ylab("% of mutations") + guides(fill=guide_legend(title=NULL))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
46 png(filename=plot1.png)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
47 print(p)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
48 dev.off()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
49
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
50 data2 = dat[5:8,]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
51
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
52 data2["sum",] = colSums(data2)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
53
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
54 data2 = data2[,names(data2)[grepl("\\.x", names(data2))]]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
55 names(data2) = gsub(".x", "", names(data2))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
56
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
57 data2["A/T",] = round(colSums(data2[3:4,]) / data2["sum",] * 100, 1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
58 data2["A/T",is.nan(unlist(data2["A/T",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
59
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
60 data2["G/C transversions",] = round(data2[2,] / data2["sum",] * 100, 1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
61 data2["G/C transitions",] = round(data2[1,] / data2["sum",] * 100, 1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
62
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
63
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
64 data2["G/C transversions",is.nan(unlist(data2["G/C transversions",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
65 data2["G/C transversions",is.infinite(unlist(data2["G/C transversions",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
66 data2["G/C transitions",is.nan(unlist(data2["G/C transitions",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
67 data2["G/C transitions",is.infinite(unlist(data2["G/C transitions",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
68
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
69 data2 = melt(t(data2[6:8,]))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
70
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
71 names(data2) = c("Class", "Type", "value")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
72
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
73 write.table(data2, plot2.txt, quote=F, sep="\t", na="", row.names=F, col.names=T)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
74
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
75 p = ggplot(data2, aes(x=Class, y=value, fill=Type)) + geom_bar(position="fill", stat="identity") + scale_y_continuous(labels=percent_format()) + guides(fill=guide_legend(title=NULL)) + ylab("% of mutations")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
76 png(filename=plot2.png)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
77 print(p)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
78 dev.off()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
79
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
80 data3 = dat[c(5, 6, 8, 17:20),]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
81 data3 = data3[,names(data3)[grepl("\\.x", names(data3))]]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
82 names(data3) = gsub(".x", "", names(data3))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
83
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
84 data3["G/C transitions",] = round(data3[1,] / (data3[5,] + data3[7,]) * 100, 1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
85
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
86 data3["G/C transversions",] = round(data3[2,] / (data3[5,] + data3[7,]) * 100, 1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
87
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
88 data3["A/T",] = round(data3[3,] / (data3[4,] + data3[6,]) * 100, 1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
89
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
90 data3["G/C transitions",is.nan(unlist(data3["G/C transitions",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
91 data3["G/C transitions",is.infinite(unlist(data3["G/C transitions",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
92
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
93 data3["G/C transversions",is.nan(unlist(data3["G/C transversions",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
94 data3["G/C transversions",is.infinite(unlist(data3["G/C transversions",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
95
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
96 data3["A/T",is.nan(unlist(data3["A/T",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
97 data3["A/T",is.infinite(unlist(data3["A/T",]))] = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
98
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
99 data3 = melt(t(data3[8:10,]))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
100 names(data3) = c("Class", "Type", "value")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
101
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
102 write.table(data3, plot3.txt, quote=F, sep="\t", na="", row.names=F, col.names=T)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
103
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
104 p = ggplot(data3, aes(Class, value)) + geom_bar(aes(fill=Type), stat="identity", position="dodge") + ylab("% of nucleotides") + guides(fill=guide_legend(title=NULL))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
105 png(filename=plot3.png)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
106 print(p)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
107 dev.off()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
108
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
109
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
110
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
111
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
112
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
113
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
114
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
115
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
116
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
117
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
118
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
119
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
120
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
121
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
122
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
123
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
124
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
125
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
126
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
127
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
128
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
129
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
130
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
131
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
132
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
133
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
134
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
135
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
136
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
137
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
138
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
139