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