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