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