76
|
1 library(reshape2)
|
|
2
|
|
3 args <- commandArgs(trailingOnly = TRUE)
|
|
4
|
|
5 gene.matches = args[1]
|
|
6 sequence.file = args[2]
|
82
|
7 merged.file = args[3]
|
|
8 outputdir = args[4]
|
|
9 gene.classes = unlist(strsplit(args[5], ","))
|
|
10 hotspot.analysis.sum.file = args[6]
|
81
|
11 NToverview.file = paste(outputdir, "ntoverview.txt", sep="/")
|
|
12 NTsum.file = paste(outputdir, "ntsum.txt", sep="/")
|
76
|
13 main.html = "index.html"
|
|
14
|
|
15 setwd(outputdir)
|
|
16
|
|
17 genes = read.table(gene.matches, header=T, sep="\t", fill=T)
|
|
18 sequences = read.table(sequence.file, header=T, sep="\t", fill=T, stringsAsFactors=F, quote="")
|
82
|
19 merged = read.table(merged.file, header=T, sep="\t", fill=T, stringsAsFactors=F, quote="")
|
|
20 hotspot.analysis.sum = read.table(hotspot.analysis.sum.file, header=F, sep=",", fill=T, stringsAsFactors=F, quote="")
|
76
|
21
|
|
22 dat = merge(sequences, genes, by="Sequence.ID")
|
|
23
|
82
|
24 dat = dat[dat$Sequence.ID %in% merged$Sequence.ID,]
|
|
25
|
76
|
26 dat$seq_conc = paste(dat$CDR1.IMGT, dat$CDR2.IMGT, dat$CDR3.IMGT, dat$FR2.IMGT, dat$FR3.IMGT)
|
|
27
|
78
|
28 IDs = dat[,c("Sequence.ID", "seq_conc", "best_match", "Functionality")]
|
76
|
29 IDs$best_match = as.character(IDs$best_match)
|
|
30
|
|
31 #dat = data.frame(data.table(dat)[, list(freq=.N), by=c("best_match", "seq_conc")])
|
|
32
|
82
|
33 dat = data.frame(table(dat$seq_conc, dat$Functionality))
|
76
|
34
|
|
35 dat = dat[dat$Freq > 1,]
|
|
36
|
82
|
37 names(dat) = c("seq_conc", "Functionality", "Freq")
|
76
|
38
|
|
39 dat$seq_conc = factor(dat$seq_conc)
|
|
40
|
82
|
41 dat = dat[order(as.character(dat$seq_conc)),]
|
76
|
42
|
|
43 #writing html from R...
|
|
44 td = function(val) { paste("<td>", val, "</td>", sep="") }
|
|
45 tr = function(val) { capture.output(cat("<tr>", td(val), "</tr>", sep="")) }
|
|
46 make.link = function(id, clss, val) { paste("<a href='", clss, "_", id, ".html'>", val, "</a>", sep="") }
|
|
47 tbl = function(df) { res = "<table border='1'>"; for(i in 1:nrow(df)){ res = paste(res, tr(df[i,]), sep=""); }; res = paste(res, "</table>"); }
|
|
48
|
|
49 cat("<table border='1'>", file=main.html, append=F)
|
77
|
50 cat("<caption>CDR1+CDR2+CDR3+FR2+FR3 sequences that show up more than once</caption>", file=main.html, append=T)
|
79
|
51 cat("<tr><th>Sequence</th><th>Functionality</th><th>ca1</th><th>ca2</th><th>cg1</th><th>cg2</th><th>cg3</th><th>cg4</th><th>cm</th></tr>", file=main.html, append=T)
|
76
|
52
|
|
53 for(i in 1:nrow(dat)){
|
|
54 ca1 = IDs[IDs$seq_conc == dat[i,c("seq_conc")] & IDs$best_match == "ca1",]
|
|
55 ca2 = IDs[IDs$seq_conc == dat[i,c("seq_conc")] & IDs$best_match == "ca2",]
|
|
56
|
|
57 cg1 = IDs[IDs$seq_conc == dat[i,c("seq_conc")] & IDs$best_match == "cg1",]
|
|
58 cg2 = IDs[IDs$seq_conc == dat[i,c("seq_conc")] & IDs$best_match == "cg2",]
|
|
59 cg3 = IDs[IDs$seq_conc == dat[i,c("seq_conc")] & IDs$best_match == "cg3",]
|
|
60 cg4 = IDs[IDs$seq_conc == dat[i,c("seq_conc")] & IDs$best_match == "cg4",]
|
|
61
|
|
62 cm = IDs[IDs$seq_conc == dat[i,c("seq_conc")] & IDs$best_match == "cm",]
|
|
63
|
82
|
64 classes = c(nrow(ca1), nrow(ca2), nrow(cg1), nrow(cg2), nrow(cg3), nrow(cg4), nrow(cm))
|
|
65
|
|
66 classes.sum = sum(classes)
|
|
67
|
|
68 if(!any(classes != 0 & classes != classes.sum)){
|
|
69 next
|
|
70 }
|
|
71
|
76
|
72 id = as.numeric(dat[i,"seq_conc"])
|
79
|
73
|
|
74 functionality = dat[i,"Functionality"]
|
76
|
75
|
|
76 if(nrow(ca1) > 0){
|
|
77 cat(tbl(ca1), file=paste("ca1_", id, ".html", sep=""))
|
|
78 }
|
|
79
|
|
80 if(nrow(ca2) > 0){
|
|
81 cat(tbl(ca2), file=paste("ca2_", id, ".html", sep=""))
|
|
82 }
|
|
83
|
|
84 if(nrow(cg1) > 0){
|
|
85 cat(tbl(cg1), file=paste("cg1_", id, ".html", sep=""))
|
|
86 }
|
|
87
|
|
88 if(nrow(cg2) > 0){
|
|
89 cat(tbl(cg2), file=paste("cg2_", id, ".html", sep=""))
|
|
90 }
|
|
91
|
|
92 if(nrow(cg3) > 0){
|
|
93 cat(tbl(cg3), file=paste("cg3_", id, ".html", sep=""))
|
|
94 }
|
|
95
|
|
96 if(nrow(cg4) > 0){
|
|
97 cat(tbl(cg4), file=paste("cg4_", id, ".html", sep=""))
|
|
98 }
|
|
99
|
|
100 if(nrow(cm) > 0){
|
|
101 cat(tbl(cm), file=paste("cm_", id, ".html", sep=""))
|
|
102 }
|
|
103
|
|
104 ca1.html = make.link(id, "ca1", nrow(ca1))
|
|
105 ca2.html = make.link(id, "ca2", nrow(ca2))
|
|
106
|
|
107 cg1.html = make.link(id, "cg1", nrow(cg1))
|
|
108 cg2.html = make.link(id, "cg2", nrow(cg2))
|
|
109 cg3.html = make.link(id, "cg3", nrow(cg3))
|
|
110 cg4.html = make.link(id, "cg4", nrow(cg4))
|
|
111
|
|
112 cm.html = make.link(id, "cm", nrow(cm))
|
|
113
|
79
|
114 rw = c(as.character(dat[i,"seq_conc"]), as.character(functionality), ca1.html, ca2.html, cg1.html, cg2.html, cg3.html, cg4.html, cm.html)
|
76
|
115
|
|
116 cat(tr(rw), file=main.html, append=T)
|
|
117 }
|
|
118
|
|
119 cat("</table>", file=main.html, append=T)
|
81
|
120
|
|
121
|
|
122 #ACGT overview
|
|
123
|
|
124
|
|
125
|
|
126 NToverview = genes[,c("Sequence.ID", "best_match")]
|
|
127 sequences$seq = paste(sequences$CDR2.IMGT, sequences$CDR2.IMGT, sequences$FR2.IMGT, sequences$FR3.IMGT, sep="_")
|
|
128
|
|
129 NToverview = merge(NToverview, sequences[,c("Sequence.ID", "seq")], by="Sequence.ID")
|
|
130
|
82
|
131 NToverview = NToverview[NToverview$Sequence.ID %in% merged$Sequence.ID,]
|
|
132
|
81
|
133 NToverview$A = nchar(gsub("[^Aa]", "", NToverview$seq))
|
|
134 NToverview$C = nchar(gsub("[^Cc]", "", NToverview$seq))
|
|
135 NToverview$G = nchar(gsub("[^Gg]", "", NToverview$seq))
|
|
136 NToverview$T = nchar(gsub("[^Tt]", "", NToverview$seq))
|
|
137
|
82
|
138 #Nsum = data.frame(Sequence.ID="-", best_match="Sum", seq="-", A = sum(NToverview$A), C = sum(NToverview$C), G = sum(NToverview$G), T = sum(NToverview$T))
|
|
139
|
|
140 #NToverview = rbind(NToverview, NTsum)
|
|
141
|
|
142 NTresult = data.frame(nt=c("A", "C", "T", "G"))
|
|
143
|
|
144 for(clazz in gene.classes){
|
|
145 NToverview.sub = NToverview[grepl(clazz, NToverview$best_match),]
|
|
146 new.col.x = c(sum(NToverview.sub$A), sum(NToverview.sub$C), sum(NToverview.sub$T), sum(NToverview.sub$G))
|
|
147 new.col.y = sum(new.col.x)
|
|
148 new.col.z = round(new.col.x / new.col.y * 100, 2)
|
|
149
|
|
150 tmp = names(NTresult)
|
|
151 NTresult = cbind(NTresult, data.frame(new.col.x, new.col.y, new.col.z))
|
|
152 names(NTresult) = c(tmp, paste(clazz, c("x", "y", "z"), sep=""))
|
|
153 }
|
81
|
154
|
82
|
155 new.col.x = c(sum(NToverview$A), sum(NToverview$C), sum(NToverview$T), sum(NToverview$G))
|
|
156 new.col.y = sum(new.col.x)
|
|
157 new.col.z = round(new.col.x / new.col.y * 100, 2)
|
|
158
|
|
159 tmp = names(NTresult)
|
|
160 NTresult = cbind(NTresult, data.frame(new.col.x, new.col.y, new.col.z))
|
|
161 names(NTresult) = c(tmp, paste("all", c("x", "y", "z"), sep=""))
|
81
|
162
|
82
|
163 names(hotspot.analysis.sum) = names(NTresult)
|
|
164
|
|
165 hotspot.analysis.sum = rbind(hotspot.analysis.sum, NTresult)
|
|
166
|
|
167 print(hotspot.analysis.sum)
|
|
168
|
|
169 write.table(hotspot.analysis.sum, hotspot.analysis.sum.file, quote=F, sep=",", row.names=F, col.names=F, na="0")
|
|
170
|
81
|
171
|
|
172 write.table(NToverview, NToverview.file, quote=F, sep="\t", row.names=F, col.names=T)
|
|
173
|
|
174
|
|
175
|
|
176
|
|
177
|
|
178
|
|
179
|
|
180
|
|
181
|
|
182
|
|
183
|
|
184
|
|
185
|
|
186
|
|
187
|
|
188
|
|
189
|
|
190
|
|
191
|
|
192
|
|
193
|
|
194
|
|
195
|
|
196
|
|
197
|
|
198
|
|
199
|
|
200
|
|
201
|
|
202
|