Mercurial > repos > davidvanzessen > test_plotting_merged
comparison RScript.r @ 0:0e6f33e21c60 draft
Uploaded
author | davidvanzessen |
---|---|
date | Wed, 09 Oct 2013 04:31:48 -0400 |
parents | |
children | c43c63f4ef80 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0e6f33e21c60 |
---|---|
1 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } ) | |
2 | |
3 args <- commandArgs(trailingOnly = TRUE) | |
4 | |
5 inFile = args[1] | |
6 outFile = args[2] | |
7 outDir = args[3] | |
8 | |
9 if (!require("gridExtra")) { | |
10 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/") | |
11 } | |
12 library (gridExtra) | |
13 if (!require("ggplot2")) { | |
14 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/") | |
15 } | |
16 require(ggplot2) | |
17 if (!require("plyr")) { | |
18 install.packages("plyr", repos="http://cran.xl-mirror.nl/") | |
19 } | |
20 require(plyr) | |
21 | |
22 test = read.table(inFile, sep="\t", header=TRUE) | |
23 | |
24 test$Top.V.Gene = gsub("[*]([0-9]+)", "", test$Top.V.Gene) | |
25 test$Top.D.Gene = gsub("[*]([0-9]+)", "", test$Top.D.Gene) | |
26 test$Top.J.Gene = gsub("[*]([0-9]+)", "", test$Top.J.Gene) | |
27 | |
28 test$VDJCDR3 = do.call(paste, c(test[c("Top.V.Gene", "Top.D.Gene", "Top.J.Gene","CDR3.Seq.DNA")], sep = ":")) | |
29 | |
30 PROD = test[test$VDJ.Frame != "In-frame with stop codon" & test$VDJ.Frame != "Out-of-frame" & test$CDR3.Found.How != "NOT_FOUND" , ] | |
31 | |
32 NONPROD = test[test$VDJ.Frame == "In-frame with stop codon" | test$VDJ.Frame == "Out-of-frame" | test$CDR3.Found.How == "NOT_FOUND" , ] | |
33 | |
34 PRODF = PROD[ -1] | |
35 | |
36 #unique(PRODF[duplicated(PRODF),]) | |
37 #length(row.names(PRODF[duplicated(PRODF),])) | |
38 | |
39 #length(row.names(PRODF)) | |
40 PRODF = unique(PRODF) | |
41 #length(row.names(PRODF)) | |
42 | |
43 PRODFV = ddply(PRODF, c("Sample", "Top.V.Gene"), function(x) summary(x$VDJCDR3)) | |
44 PRODFV$Length = as.numeric(PRODFV$Length) | |
45 Total = 0 | |
46 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length))) | |
47 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE) | |
48 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total)) | |
49 | |
50 PRODFD = ddply(PRODF, c("Sample", "Top.D.Gene"), function(x) summary(x$VDJCDR3)) | |
51 PRODFD$Length = as.numeric(PRODFD$Length) | |
52 Total = 0 | |
53 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length))) | |
54 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE) | |
55 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total)) | |
56 | |
57 PRODFJ = ddply(PRODF, c("Sample", "Top.J.Gene"), function(x) summary(x$VDJCDR3)) | |
58 PRODFJ$Length = as.numeric(PRODFJ$Length) | |
59 Total = 0 | |
60 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length))) | |
61 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE) | |
62 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total)) | |
63 | |
64 V = c("v.name\tchr.orderV\nIGHV7-81\t1\nIGHV3-74\t2\nIGHV3-73\t3\nIGHV3-72\t4\nIGHV3-71\t5\nIGHV2-70\t6\nIGHV1-69\t7\nIGHV3-66\t8\nIGHV3-64\t9\nIGHV4-61\t10\nIGHV4-59\t11\nIGHV1-58\t12\nIGHV3-53\t13\nIGHV3-52\t14\nIGHV5-a\t15\nIGHV5-51\t16\nIGHV3-49\t17\nIGHV3-48\t18\nIGHV3-47\t19\nIGHV1-46\t20\nIGHV1-45\t21\nIGHV3-43\t22\nIGHV4-39\t23\nIGHV3-35\t24\nIGHV4-34\t25\nIGHV3-33\t26\nIGHV4-31\t27\nIGHV4-30-4\t28\nIGHV4-30-2\t29\nIGHV3-30-3\t30\nIGHV3-30\t31\nIGHV4-28\t32\nIGHV2-26\t33\nIGHV1-24\t34\nIGHV3-23\t35\nIGHV3-22\t36\nIGHV3-21\t37\nIGHV3-20\t38\nIGHV3-19\t39\nIGHV1-18\t40\nIGHV3-15\t41\nIGHV3-13\t42\nIGHV3-11\t43\nIGHV3-9\t44\nIGHV1-8\t45\nIGHV3-7\t46\nIGHV2-5\t47\nIGHV7-4-1\t48\nIGHV4-4\t49\nIGHV4-b\t50\nIGHV1-3\t51\nIGHV1-2\t52\nIGHV6-1\t53") | |
65 tcV = textConnection(V) | |
66 Vchain = read.table(tcV, sep="\t", header=TRUE) | |
67 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE) | |
68 close(tcV) | |
69 | |
70 D = c("v.name\tchr.orderD\nIGHD1-1\t1\nIGHD2-2\t2\nIGHD3-3\t3\nIGHD6-6\t4\nIGHD1-7\t5\nIGHD2-8\t6\nIGHD3-9\t7\nIGHD3-10\t8\nIGHD4-11\t9\nIGHD5-12\t10\nIGHD6-13\t11\nIGHD1-14\t12\nIGHD2-15\t13\nIGHD3-16\t14\nIGHD4-17\t15\nIGHD5-18\t16\nIGHD6-19\t17\nIGHD1-20\t18\nIGHD2-21\t19\nIGHD3-22\t20\nIGHD4-23\t21\nIGHD5-24\t22\nIGHD6-25\t23\nIGHD1-26\t24\nIGHD7-27\t25") | |
71 tcD = textConnection(D) | |
72 Dchain = read.table(tcD, sep="\t", header=TRUE) | |
73 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE) | |
74 close(tcD) | |
75 | |
76 | |
77 J = c("v.name\tchr.orderJ\nIGHJ1\t1\nIGHJ2\t2\nIGHJ3\t3\nIGHJ4\t4\nIGHJ5\t5\nIGHJ6\t6") | |
78 tcJ = textConnection(J) | |
79 Jchain = read.table(tcJ, sep="\t", header=TRUE) | |
80 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE) | |
81 close(tcJ) | |
82 | |
83 setwd(outDir) | |
84 | |
85 pV = ggplot(PRODFV) | |
86 pV = pV + geom_bar( aes( x=factor(reorder(Top.V.Gene, chr.orderV)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
87 | |
88 png("VPlot.png",width = 1280, height = 720) | |
89 pV | |
90 dev.off(); | |
91 | |
92 pD = ggplot(PRODFD) | |
93 pD = pD + geom_bar( aes( x=factor(reorder(Top.D.Gene, chr.orderD)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
94 | |
95 png("DPlot.png",width = 800, height = 600) | |
96 pD | |
97 dev.off(); | |
98 | |
99 pJ = ggplot(PRODFJ) | |
100 pJ = pJ + geom_bar( aes( x=factor(reorder(Top.J.Gene, chr.orderJ)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
101 | |
102 png("JPlot.png",width = 800, height = 600) | |
103 pJ | |
104 dev.off(); | |
105 | |
106 | |
107 plotVD <- function(dat){ | |
108 ggplot() + | |
109 geom_tile(data=dat, aes(x=factor(Top.V.Gene), y=factor(Top.D.Gene), fill=log)) + | |
110 theme(axis.text.x = element_text(angle = 90, hjust = 1)) + | |
111 scale_fill_gradient(low="white", high="red") + | |
112 ggtitle(unique(dat$Sample)) | |
113 } | |
114 | |
115 | |
116 VandDCount = ddply(PRODF, c("Top.V.Gene", "Top.D.Gene", "Sample"), function(x) summary(x$VDJCDR3)) | |
117 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(test$Sample)) | |
118 | |
119 completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE) | |
120 completeVD$Length = as.numeric(completeVD$Length) | |
121 completeVD$log = log(completeVD$Length) | |
122 completeVD$log[is.na(completeVD$log)] = 0 | |
123 l = split(completeVD, f=completeVD[,"Sample"]) | |
124 png("HeatmapVD%d.png") | |
125 lapply(l, FUN=plotVD) | |
126 dev.off() | |
127 | |
128 | |
129 plotVJ <- function(dat){ | |
130 ggplot() + | |
131 geom_tile(data=dat, aes(x=factor(Top.V.Gene), y=factor(Top.J.Gene), fill=log)) + | |
132 theme(axis.text.x = element_text(angle = 90, hjust = 1)) + | |
133 scale_fill_gradient(low="white", high="red") + | |
134 ggtitle(unique(dat$Sample)) | |
135 } | |
136 | |
137 VandJCount = ddply(PRODF, c("Top.V.Gene", "Top.J.Gene", "Sample"), function(x) summary(x$VDJCDR3)) | |
138 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample)) | |
139 | |
140 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE) | |
141 completeVJ$Length = as.numeric(completeVJ$Length) | |
142 completeVJ$log = log(completeVJ$Length) | |
143 completeVJ$log[is.na(completeVJ$log)] = 0 | |
144 l = split(completeVJ, f=completeVJ[,"Sample"]) | |
145 png("HeatmapVJ%d.png") | |
146 lapply(l, FUN=plotVJ) | |
147 dev.off() | |
148 | |
149 plotDJ <- function(dat){ | |
150 ggplot() + | |
151 geom_tile(data=dat, aes(x=factor(Top.D.Gene), y=factor(Top.J.Gene), fill=log)) + | |
152 theme(axis.text.x = element_text(angle = 90, hjust = 1)) + | |
153 scale_fill_gradient(low="white", high="red") + | |
154 ggtitle(unique(dat$Sample)) | |
155 } | |
156 | |
157 DandJCount = ddply(PRODF, c("Top.D.Gene", "Top.J.Gene", "Sample"), function(x) summary(x$VDJCDR3)) | |
158 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample)) | |
159 | |
160 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE) | |
161 completeDJ$Length = as.numeric(completeDJ$Length) | |
162 completeDJ$log = log(completeDJ$Length) | |
163 completeDJ$log[is.na(completeDJ$log)] = 0 | |
164 l = split(completeDJ, f=completeDJ[,"Sample"]) | |
165 png("HeatmapDJ%d.png") | |
166 lapply(l, FUN=plotDJ) | |
167 dev.off() | |
168 | |
169 | |
170 sampleFile <- file("samples.txt") | |
171 un = unique(test$Sample) | |
172 un = paste(un, sep="\n") | |
173 writeLines(un, sampleFile) | |
174 close(sampleFile) |