0
|
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]
|
32
|
7 outDir = args[3]
|
42
|
8 clonalType = args[4]
|
0
|
9
|
36
|
10 if (!("gridExtra" %in% rownames(installed.packages()))) {
|
|
11 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
|
32
|
12 }
|
|
13 library(gridExtra)
|
36
|
14 if (!("ggplot2" %in% rownames(installed.packages()))) {
|
|
15 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
|
32
|
16 }
|
23
|
17 require(ggplot2)
|
36
|
18 if (!("plyr" %in% rownames(installed.packages()))) {
|
|
19 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
|
32
|
20 }
|
23
|
21 require(plyr)
|
0
|
22
|
36
|
23 if (!("data.table" %in% rownames(installed.packages()))) {
|
|
24 install.packages("data.table", repos="http://cran.xl-mirror.nl/")
|
|
25 }
|
|
26 library(data.table)
|
|
27
|
|
28
|
38
|
29 test = read.table(inFile, sep="\t", header=TRUE, fill=T)
|
0
|
30
|
39
|
31 test = test[test$Sample != "",]
|
|
32
|
45
|
33 if("Replicate" %in% colnames(test))
|
|
34 {
|
46
|
35 test$SRID = do.call(paste, c(test[c("Sample", "Replicate")], sep = "-"))
|
45
|
36 }
|
|
37
|
0
|
38 test$Top.V.Gene = gsub("[*]([0-9]+)", "", test$Top.V.Gene)
|
|
39 test$Top.D.Gene = gsub("[*]([0-9]+)", "", test$Top.D.Gene)
|
|
40 test$Top.J.Gene = gsub("[*]([0-9]+)", "", test$Top.J.Gene)
|
|
41
|
42
|
42 #test$VDJCDR3 = do.call(paste, c(test[c("Top.V.Gene", "Top.D.Gene", "Top.J.Gene","CDR3.Seq.DNA")], sep = ":"))
|
|
43 test$VDJCDR3 = do.call(paste, c(test[unlist(strsplit(clonalType, ","))], sep = ":"))
|
0
|
44
|
|
45 PROD = test[test$VDJ.Frame != "In-frame with stop codon" & test$VDJ.Frame != "Out-of-frame" & test$CDR3.Found.How != "NOT_FOUND" , ]
|
|
46
|
|
47 NONPROD = test[test$VDJ.Frame == "In-frame with stop codon" | test$VDJ.Frame == "Out-of-frame" | test$CDR3.Found.How == "NOT_FOUND" , ]
|
|
48
|
36
|
49 #PRODF = PROD[ -1]
|
|
50
|
|
51 PRODF = PROD
|
0
|
52
|
32
|
53 #PRODF = unique(PRODF)
|
|
54 PRODF = PRODF[!duplicated(PRODF$VDJCDR3), ]
|
|
55
|
36
|
56 PRODFV = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.V.Gene")])
|
0
|
57 PRODFV$Length = as.numeric(PRODFV$Length)
|
|
58 Total = 0
|
|
59 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
60 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
61 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
62
|
36
|
63 PRODFD = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.D.Gene")])
|
0
|
64 PRODFD$Length = as.numeric(PRODFD$Length)
|
|
65 Total = 0
|
|
66 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
67 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
68 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
69
|
36
|
70 PRODFJ = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.J.Gene")])
|
0
|
71 PRODFJ$Length = as.numeric(PRODFJ$Length)
|
|
72 Total = 0
|
|
73 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
74 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
75 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
76
|
|
77 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")
|
|
78 tcV = textConnection(V)
|
|
79 Vchain = read.table(tcV, sep="\t", header=TRUE)
|
|
80 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
|
|
81 close(tcV)
|
|
82
|
|
83 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")
|
|
84 tcD = textConnection(D)
|
|
85 Dchain = read.table(tcD, sep="\t", header=TRUE)
|
|
86 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
|
|
87 close(tcD)
|
|
88
|
|
89
|
|
90 J = c("v.name\tchr.orderJ\nIGHJ1\t1\nIGHJ2\t2\nIGHJ3\t3\nIGHJ4\t4\nIGHJ5\t5\nIGHJ6\t6")
|
|
91 tcJ = textConnection(J)
|
|
92 Jchain = read.table(tcJ, sep="\t", header=TRUE)
|
|
93 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
|
|
94 close(tcJ)
|
|
95
|
32
|
96 setwd(outDir)
|
|
97
|
0
|
98 pV = ggplot(PRODFV)
|
|
99 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))
|
29
|
100 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
|
32
|
101
|
|
102 png("VPlot.png",width = 1280, height = 720)
|
|
103 pV
|
|
104 dev.off();
|
0
|
105
|
|
106 pD = ggplot(PRODFD)
|
|
107 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))
|
29
|
108 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
|
32
|
109
|
|
110 png("DPlot.png",width = 800, height = 600)
|
|
111 pD
|
|
112 dev.off();
|
0
|
113
|
|
114 pJ = ggplot(PRODFJ)
|
|
115 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))
|
32
|
116 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
|
|
117
|
|
118 png("JPlot.png",width = 800, height = 600)
|
|
119 pJ
|
|
120 dev.off();
|
|
121
|
38
|
122 revVchain = Vchain
|
|
123 revDchain = Dchain
|
|
124 revVchain$chr.orderV = rev(revVchain$chr.orderV)
|
|
125 revDchain$chr.orderD = rev(revDchain$chr.orderD)
|
0
|
126
|
32
|
127 plotVD <- function(dat){
|
39
|
128 if(length(dat[,1]) == 0){
|
|
129 return()
|
|
130 }
|
32
|
131 img = ggplot() +
|
38
|
132 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
32
|
133 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
45
|
134 scale_fill_gradient(low="gold", high="blue", na.value="white", limits=c(0,1)) +
|
38
|
135 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
32
|
136 xlab("D genes") +
|
|
137 ylab("V Genes")
|
|
138
|
|
139 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
140 print(img)
|
|
141 dev.off()
|
|
142 }
|
|
143
|
36
|
144 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
|
38
|
145
|
|
146 VandDCount$l = log(VandDCount$Length)
|
|
147 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
|
|
148 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
|
|
149 VandDCount$relLength = VandDCount$l / VandDCount$max
|
|
150
|
32
|
151 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(test$Sample))
|
|
152
|
|
153 completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
|
38
|
154 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
32
|
155 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
38
|
156 VDList = split(completeVD, f=completeVD[,"Sample"])
|
32
|
157
|
38
|
158 lapply(VDList, FUN=plotVD)
|
32
|
159
|
0
|
160
|
29
|
161
|
32
|
162 plotVJ <- function(dat){
|
39
|
163 if(length(dat[,1]) == 0){
|
|
164 return()
|
|
165 }
|
32
|
166 img = ggplot() +
|
38
|
167 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
32
|
168 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
45
|
169 scale_fill_gradient(low="gold", high="blue", na.value="white", limits=c(0,1)) +
|
38
|
170 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
32
|
171 xlab("J genes") +
|
|
172 ylab("V Genes")
|
|
173
|
|
174 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
175 print(img)
|
|
176 dev.off()
|
|
177 }
|
|
178
|
36
|
179 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
|
38
|
180
|
|
181 VandJCount$l = log(VandJCount$Length)
|
|
182 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
183 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
184 VandJCount$relLength = VandJCount$l / VandJCount$max
|
|
185
|
32
|
186 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
|
|
187
|
|
188 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
|
38
|
189 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
32
|
190 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
38
|
191 VJList = split(completeVJ, f=completeVJ[,"Sample"])
|
|
192 lapply(VJList, FUN=plotVJ)
|
32
|
193
|
|
194 plotDJ <- function(dat){
|
39
|
195 if(length(dat[,1]) == 0){
|
|
196 return()
|
|
197 }
|
32
|
198 img = ggplot() +
|
38
|
199 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
|
32
|
200 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
45
|
201 scale_fill_gradient(low="gold", high="blue", na.value="white", limits=c(0,1)) +
|
38
|
202 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
32
|
203 xlab("J genes") +
|
|
204 ylab("D Genes")
|
|
205
|
|
206 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
|
|
207 print(img)
|
|
208 dev.off()
|
|
209 }
|
|
210
|
36
|
211 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
|
38
|
212
|
|
213 DandJCount$l = log(DandJCount$Length)
|
|
214 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
215 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
216 DandJCount$relLength = DandJCount$l / DandJCount$max
|
|
217
|
32
|
218 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
|
|
219
|
|
220 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
|
38
|
221 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
32
|
222 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
38
|
223 DJList = split(completeDJ, f=completeDJ[,"Sample"])
|
|
224 lapply(DJList, FUN=plotDJ)
|
0
|
225
|
|
226
|
32
|
227 sampleFile <- file("samples.txt")
|
|
228 un = unique(test$Sample)
|
|
229 un = paste(un, sep="\n")
|
|
230 writeLines(un, sampleFile)
|
|
231 close(sampleFile)
|