7
|
1 # ---------------------- load/install packages ----------------------
|
|
2
|
|
3 if (!("gridExtra" %in% rownames(installed.packages()))) {
|
|
4 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
|
|
5 }
|
|
6 library(gridExtra)
|
|
7 if (!("ggplot2" %in% rownames(installed.packages()))) {
|
|
8 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
|
|
9 }
|
|
10 library(ggplot2)
|
|
11 if (!("plyr" %in% rownames(installed.packages()))) {
|
|
12 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
|
|
13 }
|
|
14 library(plyr)
|
|
15
|
|
16 if (!("data.table" %in% rownames(installed.packages()))) {
|
|
17 install.packages("data.table", repos="http://cran.xl-mirror.nl/")
|
|
18 }
|
|
19 library(data.table)
|
|
20
|
|
21 if (!("reshape2" %in% rownames(installed.packages()))) {
|
|
22 install.packages("reshape2", repos="http://cran.xl-mirror.nl/")
|
|
23 }
|
|
24 library(reshape2)
|
|
25
|
|
26 # ---------------------- parameters ----------------------
|
|
27
|
|
28 args <- commandArgs(trailingOnly = TRUE)
|
|
29
|
|
30 infile = args[1] #path to input file
|
|
31 outfile = args[2] #path to output file
|
|
32 outdir = args[3] #path to output folder (html/images/data)
|
|
33 clonaltype = args[4] #clonaltype definition, or 'none' for no unique filtering
|
10
|
34 ct = unlist(strsplit(clonaltype, ","))
|
7
|
35 species = args[5] #human or mouse
|
|
36 locus = args[6] # IGH, IGK, IGL, TRB, TRA, TRG or TRD
|
|
37 filterproductive = ifelse(args[7] == "yes", T, F) #should unproductive sequences be filtered out? (yes/no)
|
|
38
|
|
39 # ---------------------- Data preperation ----------------------
|
|
40
|
|
41 inputdata = read.table(infile, sep="\t", header=TRUE, fill=T, comment.char="")
|
|
42
|
|
43 setwd(outdir)
|
|
44
|
|
45 # remove weird rows
|
10
|
46 inputdata = inputdata[inputdata$Sample != "",]
|
7
|
47
|
|
48 #remove the allele from the V,D and J genes
|
|
49 inputdata$Top.V.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.V.Gene)
|
|
50 inputdata$Top.D.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.D.Gene)
|
|
51 inputdata$Top.J.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.J.Gene)
|
10
|
52
|
7
|
53 inputdata$clonaltype = 1:nrow(inputdata)
|
10
|
54
|
7
|
55 PRODF = inputdata
|
9
|
56 UNPROD = inputdata
|
7
|
57 if(filterproductive){
|
|
58 if("Functionality" %in% colnames(inputdata)) { # "Functionality" is an IMGT column
|
|
59 PRODF = inputdata[inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)", ]
|
9
|
60 UNPROD = inputdata[!(inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)"), ]
|
7
|
61 } else {
|
|
62 PRODF = inputdata[inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" , ]
|
9
|
63 UNPROD = inputdata[!(inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" ), ]
|
7
|
64 }
|
|
65 }
|
|
66
|
|
67 #remove duplicates based on the clonaltype
|
|
68 if(clonaltype != "none"){
|
10
|
69 clonaltype = paste(clonaltype, ",Sample", sep="") #add sample column to clonaltype, unique within samples
|
7
|
70 PRODF$clonaltype = do.call(paste, c(PRODF[unlist(strsplit(clonaltype, ","))], sep = ":"))
|
|
71 PRODF = PRODF[!duplicated(PRODF$clonaltype), ]
|
|
72 }
|
|
73
|
|
74 PRODF$freq = 1
|
|
75
|
|
76 if(any(grepl(pattern="_", x=PRODF$ID))){ #the frequency can be stored in the ID with the pattern ".*_freq_.*"
|
|
77 PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
|
|
78 PRODF$freq = gsub("_.*", "", PRODF$freq)
|
|
79 PRODF$freq = as.numeric(PRODF$freq)
|
|
80 if(any(is.na(PRODF$freq))){ #if there was an "_" in the ID, but not the frequency, go back to frequency of 1 for every sequence
|
|
81 PRODF$freq = 1
|
|
82 }
|
|
83 }
|
|
84
|
|
85
|
|
86
|
|
87 #write the complete dataset that is left over, will be the input if 'none' for clonaltype and 'no' for filterproductive
|
|
88 write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
89
|
|
90 #write the samples to a file
|
|
91 sampleFile <- file("samples.txt")
|
|
92 un = unique(inputdata$Sample)
|
|
93 un = paste(un, sep="\n")
|
|
94 writeLines(un, sampleFile)
|
|
95 close(sampleFile)
|
|
96
|
|
97 # ---------------------- Counting the productive/unproductive and unique sequences ----------------------
|
|
98
|
|
99 inputdata.dt = data.table(inputdata) #for speed
|
|
100
|
|
101 if(clonaltype == "none"){
|
10
|
102 ct = c("clonaltype")
|
7
|
103 }
|
|
104
|
|
105 inputdata.dt$samples_replicates = paste(inputdata.dt$Sample, inputdata.dt$Replicate, sep="_")
|
|
106 samples_replicates = c(unique(inputdata.dt$samples_replicates), unique(as.character(inputdata.dt$Sample)))
|
|
107 frequency_table = data.frame(ID = samples_replicates[order(samples_replicates)])
|
|
108
|
|
109
|
|
110 sample_productive_count = inputdata.dt[, list(All=.N,
|
|
111 Productive = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",]),
|
|
112 perc_prod = 1,
|
|
113 Productive_unique = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",list(count=.N),by=ct]),
|
|
114 perc_prod_un = 1,
|
|
115 Unproductive= nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",]),
|
|
116 perc_unprod = 1,
|
|
117 Unproductive_unique =nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",list(count=.N),by=ct]),
|
|
118 perc_unprod_un = 1),
|
|
119 by=c("Sample")]
|
|
120
|
|
121 sample_productive_count$perc_prod = round(sample_productive_count$Productive / sample_productive_count$All * 100)
|
|
122 sample_productive_count$perc_prod_un = round(sample_productive_count$Productive_unique / sample_productive_count$All * 100)
|
|
123
|
|
124 sample_productive_count$perc_unprod = round(sample_productive_count$Unproductive / sample_productive_count$All * 100)
|
|
125 sample_productive_count$perc_unprod_un = round(sample_productive_count$Unproductive_unique / sample_productive_count$All * 100)
|
|
126
|
|
127
|
|
128 sample_replicate_productive_count = inputdata.dt[, list(All=.N,
|
|
129 Productive = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",]),
|
|
130 perc_prod = 1,
|
|
131 Productive_unique = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",list(count=.N),by=ct]),
|
|
132 perc_prod_un = 1,
|
|
133 Unproductive= nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",]),
|
|
134 perc_unprod = 1,
|
|
135 Unproductive_unique =nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",list(count=.N),by=ct]),
|
|
136 perc_unprod_un = 1),
|
|
137 by=c("samples_replicates")]
|
|
138
|
|
139 sample_replicate_productive_count$perc_prod = round(sample_replicate_productive_count$Productive / sample_replicate_productive_count$All * 100)
|
|
140 sample_replicate_productive_count$perc_prod_un = round(sample_replicate_productive_count$Productive_unique / sample_replicate_productive_count$All * 100)
|
|
141
|
|
142 sample_replicate_productive_count$perc_unprod = round(sample_replicate_productive_count$Unproductive / sample_replicate_productive_count$All * 100)
|
|
143 sample_replicate_productive_count$perc_unprod_un = round(sample_replicate_productive_count$Unproductive_unique / sample_replicate_productive_count$All * 100)
|
|
144
|
|
145 setnames(sample_replicate_productive_count, colnames(sample_productive_count))
|
|
146
|
|
147 counts = rbind(sample_replicate_productive_count, sample_productive_count)
|
|
148 counts = counts[order(counts$Sample),]
|
|
149
|
|
150 write.table(x=counts, file="productive_counting.txt", sep=",",quote=F,row.names=F,col.names=F)
|
|
151
|
|
152 # ---------------------- Frequency calculation for V, D and J ----------------------
|
|
153
|
|
154 PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
|
|
155 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
156 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
157 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
158
|
|
159 PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
|
|
160 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
161 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
162 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
163
|
|
164 PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
|
|
165 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
166 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
167 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
168
|
|
169 # ---------------------- Setting up the gene names for the different T/B, human/mouse and locus ----------------------
|
|
170
|
|
171 V = c("v.name\tchr.orderV\n")
|
|
172 D = c("v.name\tchr.orderD\n")
|
|
173 J = c("v.name\tchr.orderJ\n")
|
|
174
|
|
175 if(species == "human"){
|
|
176 if(locus == "trb"){
|
|
177 V = c("v.name\tchr.orderV\nTRBV2\t1\nTRBV3-1\t2\nTRBV4-1\t3\nTRBV5-1\t4\nTRBV6-1\t5\nTRBV4-2\t6\nTRBV6-2\t7\nTRBV4-3\t8\nTRBV6-3\t9\nTRBV7-2\t10\nTRBV6-4\t11\nTRBV7-3\t12\nTRBV9\t13\nTRBV10-1\t14\nTRBV11-1\t15\nTRBV10-2\t16\nTRBV11-2\t17\nTRBV6-5\t18\nTRBV7-4\t19\nTRBV5-4\t20\nTRBV6-6\t21\nTRBV5-5\t22\nTRBV7-6\t23\nTRBV5-6\t24\nTRBV6-8\t25\nTRBV7-7\t26\nTRBV6-9\t27\nTRBV7-8\t28\nTRBV5-8\t29\nTRBV7-9\t30\nTRBV13\t31\nTRBV10-3\t32\nTRBV11-3\t33\nTRBV12-3\t34\nTRBV12-4\t35\nTRBV12-5\t36\nTRBV14\t37\nTRBV15\t38\nTRBV16\t39\nTRBV18\t40\nTRBV19\t41\nTRBV20-1\t42\nTRBV24-1\t43\nTRBV25-1\t44\nTRBV27\t45\nTRBV28\t46\nTRBV29-1\t47\nTRBV30\t48")
|
|
178 D = c("v.name\tchr.orderD\nTRBD1\t1\nTRBD2\t2\n")
|
|
179 J = c("v.name\tchr.orderJ\nTRBJ1-1\t1\nTRBJ1-2\t2\nTRBJ1-3\t3\nTRBJ1-4\t4\nTRBJ1-5\t5\nTRBJ1-6\t6\nTRBJ2-1\t7\nTRBJ2-2\t8\nTRBJ2-3\t9\nTRBJ2-4\t10\nTRBJ2-5\t11\nTRBJ2-6\t12\nTRBJ2-7\t13")
|
|
180 } else if (locus == "tra"){
|
|
181 V = c("v.name\tchr.orderVTRAV1-1\t1\nTRAV1-2\t2\nTRAV2\t3\nTRAV3\t4\nTRAV4\t5\nTRAV5\t6\nTRAV6\t7\nTRAV7\t8\nTRAV8-1\t9\nTRAV9-1\t10\nTRAV10\t11\nTRAV12-1\t12\nTRAV8-2\t13\nTRAV8-3\t14\nTRAV13-1\t15\nTRAV12-2\t16\nTRAV8-4\t17\nTRAV13-2\t18\nTRAV14/DV4\t19\nTRAV9-2\t20\nTRAV12-3\t21\nTRAV8-6\t22\nTRAV16\t23\nTRAV17\t24\nTRAV18\t25\nTRAV19\t26\nTRAV20\t27\nTRAV21\t28\nTRAV22\t29\nTRAV23/DV6\t30\nTRAV24\t31\nTRAV25\t32\nTRAV26-1\t33\nTRAV27\t34\nTRAV29/DV5\t35\nTRAV30\t36\nTRAV26-2\t37\nTRAV34\t38\nTRAV35\t39\nTRAV36/DV7\t40\nTRAV38-1\t41\nTRAV38-2/DV8\t42\nTRAV39\t43\nTRAV40\t44\nTRAV41\t45\n")
|
|
182 D = c("v.name\tchr.orderD\n")
|
|
183 J = c("v.name\tchr.orderJ\nTRAJ57\t1\nTRAJ56\t2\nTRAJ54\t3\nTRAJ53\t4\nTRAJ52\t5\nTRAJ50\t6\nTRAJ49\t7\nTRAJ48\t8\nTRAJ47\t9\nTRAJ46\t10\nTRAJ45\t11\nTRAJ44\t12\nTRAJ43\t13\nTRAJ42\t14\nTRAJ41\t15\nTRAJ40\t16\nTRAJ39\t17\nTRAJ38\t18\nTRAJ37\t19\nTRAJ36\t20\nTRAJ34\t21\nTRAJ33\t22\nTRAJ32\t23\nTRAJ31\t24\nTRAJ30\t25\nTRAJ29\t26\nTRAJ28\t27\nTRAJ27\t28\nTRAJ26\t29\nTRAJ24\t30\nTRAJ23\t31\nTRAJ22\t32\nTRAJ21\t33\nTRAJ20\t34\nTRAJ18\t35\nTRAJ17\t36\nTRAJ16\t37\nTRAJ15\t38\nTRAJ14\t39\nTRAJ13\t40\nTRAJ12\t41\nTRAJ11\t42\nTRAJ10\t43\nTRAJ9\t44\nTRAJ8\t45\nTRAJ7\t46\nTRAJ6\t47\nTRAJ5\t48\nTRAJ4\t49\nTRAJ3\t50")
|
|
184 } else if (locus == "trg"){
|
|
185 V = c("v.name\tchr.orderV\nTRGV9\t1\nTRGV8\t2\nTRGV5\t3\nTRGV4\t4\nTRGV3\t5\nTRGV2\t6")
|
|
186 D = c("v.name\tchr.orderD\n")
|
|
187 J = c("v.name\tchr.orderJ\nTRGJ2\t1\nTRGJP2\t2\nTRGJ1\t3\nTRGJP1\t4")
|
|
188 } else if (locus == "trd"){
|
|
189 V = c("v.name\tchr.orderV\nTRDV1\t1\nTRDV2\t2\nTRDV3\t3")
|
|
190 D = c("v.name\tchr.orderD\nTRDD1\t1\nTRDD2\t2\nTRDD3\t3")
|
|
191 J = c("v.name\tchr.orderJ\nTRDJ1\t1\nTRDJ4\t2\nTRDJ2\t3\nTRDJ3\t4")
|
|
192 } else if(locus == "igh"){
|
|
193 V = c("v.name\tchr.orderV\nIGHV3-74\t1\nIGHV3-73\t2\nIGHV3-72\t3\nIGHV2-70\t4\nIGHV1-69D\t5\nIGHV1-69-2\t6\nIGHV2-70D\t7\nIGHV1-69\t8\nIGHV3-66\t9\nIGHV3-64\t10\nIGHV4-61\t11\nIGHV4-59\t12\nIGHV1-58\t13\nIGHV3-53\t14\nIGHV5-51\t15\nIGHV3-49\t16\nIGHV3-48\t17\nIGHV1-46\t18\nIGHV1-45\t19\nIGHV3-43\t20\nIGHV4-39\t21\nIGHV3-43D\t22\nIGHV4-38-2\t23\nIGHV4-34\t24\nIGHV3-33\t25\nIGHV4-31\t26\nIGHV3-30-5\t27\nIGHV4-30-4\t28\nIGHV3-30-3\t29\nIGHV4-30-2\t30\nIGHV4-30-1\t31\nIGHV3-30\t32\nIGHV4-28\t33\nIGHV2-26\t34\nIGHV1-24\t35\nIGHV3-23D\t36\nIGHV3-23\t37\nIGHV3-21\t38\nIGHV3-20\t39\nIGHV1-18\t40\nIGHV3-15\t41\nIGHV3-13\t42\nIGHV3-11\t43\nIGHV5-10-1\t44\nIGHV3-9\t45\nIGHV1-8\t46\nIGHV3-64D\t47\nIGHV3-7\t48\nIGHV2-5\t49\nIGHV7-4-1\t50\nIGHV4-4\t51\nIGHV1-3\t52\nIGHV1-2\t53\nIGHV6-1\t54")
|
|
194 D = c("v.name\tchr.orderD\nIGHD1-7\t1\nIGHD2-8\t2\nIGHD3-9\t3\nIGHD3-10\t4\nIGHD5-12\t5\nIGHD6-13\t6\nIGHD2-15\t7\nIGHD3-16\t8\nIGHD4-17\t9\nIGHD5-18\t10\nIGHD6-19\t11\nIGHD1-20\t12\nIGHD2-21\t13\nIGHD3-22\t14\nIGHD5-24\t15\nIGHD6-25\t16\nIGHD1-26\t17\nIGHD7-27\t18")
|
|
195 J = c("v.name\tchr.orderJ\nIGHJ1\t1\nIGHJ2\t2\nIGHJ3\t3\nIGHJ4\t4\nIGHJ5\t5\nIGHJ6\t6")
|
|
196 } else if (locus == "igk"){
|
|
197 V = c("v.name\tchr.orderV\nIGKV3D-7\t1\nIGKV1D-8\t2\nIGKV1D-43\t3\nIGKV3D-11\t4\nIGKV1D-12\t5\nIGKV1D-13\t6\nIGKV3D-15\t7\nIGKV1D-16\t8\nIGKV1D-17\t9\nIGKV3D-20\t10\nIGKV2D-26\t11\nIGKV2D-28\t12\nIGKV2D-29\t13\nIGKV2D-30\t14\nIGKV1D-33\t15\nIGKV1D-39\t16\nIGKV2D-40\t17\nIGKV2-40\t18\nIGKV1-39\t19\nIGKV1-33\t20\nIGKV2-30\t21\nIGKV2-29\t22\nIGKV2-28\t23\nIGKV1-27\t24\nIGKV2-24\t25\nIGKV3-20\t26\nIGKV1-17\t27\nIGKV1-16\t28\nIGKV3-15\t29\nIGKV1-13\t30\nIGKV1-12\t31\nIGKV3-11\t32\nIGKV1-9\t33\nIGKV1-8\t34\nIGKV1-6\t35\nIGKV1-5\t36\nIGKV5-2\t37\nIGKV4-1\t38")
|
|
198 D = c("v.name\tchr.orderD\n")
|
|
199 J = c("v.name\tchr.orderJ\nIGKJ1\t1\nIGKJ2\t2\nIGKJ3\t3\nIGKJ4\t4\nIGKJ5\t5")
|
|
200 } else if (locus == "igl"){
|
|
201 V = c("v.name\tchr.orderV\nIGLV4-69\t1\nIGLV8-61\t2\nIGLV4-60\t3\nIGLV6-57\t4\nIGLV5-52\t5\nIGLV1-51\t6\nIGLV9-49\t7\nIGLV1-47\t8\nIGLV7-46\t9\nIGLV5-45\t10\nIGLV1-44\t11\nIGLV7-43\t12\nIGLV1-41\t13\nIGLV1-40\t14\nIGLV5-39\t15\nIGLV5-37\t16\nIGLV1-36\t17\nIGLV3-27\t18\nIGLV3-25\t19\nIGLV2-23\t20\nIGLV3-22\t21\nIGLV3-21\t22\nIGLV3-19\t23\nIGLV2-18\t24\nIGLV3-16\t25\nIGLV2-14\t26\nIGLV3-12\t27\nIGLV2-11\t28\nIGLV3-10\t29\nIGLV3-9\t30\nIGLV2-8\t31\nIGLV4-3\t32\nIGLV3-1\t33")
|
|
202 D = c("v.name\tchr.orderD\n")
|
|
203 J = c("v.name\tchr.orderJ\nIGLJ1\t1\nIGLJ2\t2\nIGLJ3\t3\nIGLJ6\t4\nIGLJ7\t5")
|
|
204 }
|
|
205 } else if (species == "mouse"){
|
|
206 if(locus == "trb"){
|
|
207 V = c("v.name\tchr.orderV\nTRBV1\t1\nTRBV2\t2\nTRBV3\t3\nTRBV4\t4\nTRBV5\t5\nTRBV12-1\t6\nTRBV13-1\t7\nTRBV12-2\t8\nTRBV13-2\t9\nTRBV13-3\t10\nTRBV14\t11\nTRBV15\t12\nTRBV16\t13\nTRBV17\t14\nTRBV19\t15\nTRBV20\t16\nTRBV23\t17\nTRBV24\t18\nTRBV26\t19\nTRBV29\t20\nTRBV30\t21\nTRBV31\t22")
|
|
208 D = c("v.name\tchr.orderD\nTRBD1\t1\nTRBD2\t2")
|
|
209 J = c("v.name\tchr.orderJ\nTRBJ1-1\t1\nTRBJ1-2\t2\nTRBJ1-3\t3\nTRBJ1-4\t4\nTRBJ1-5\t5\nTRBJ2-1\t6\nTRBJ2-2\t7\nTRBJ2-3\t8\nTRBJ2-4\t9\nTRBJ2-5\t10\nTRBJ2-6\t11\nTRBJ2-7\t12")
|
|
210 } else if (locus == "tra"){
|
|
211 cat("mouse tra not yet implemented")
|
|
212 } else if (locus == "trg"){
|
|
213 cat("mouse trg not yet implemented")
|
|
214 } else if (locus == "trd"){
|
|
215 cat("mouse trd not yet implemented")
|
|
216 } else if(locus == "igh"){
|
|
217 cat("mouse igh not yet implemented")
|
|
218 } else if (locus == "igk"){
|
|
219 cat("mouse igk not yet implemented")
|
|
220 } else if (locus == "igl"){
|
|
221 cat("mouse igl not yet implemented")
|
|
222 }
|
|
223 }
|
|
224
|
|
225 useD = TRUE
|
|
226 if(species == "human" && locus == "tra"){
|
|
227 useD = FALSE
|
|
228 cat("No D Genes in this species/locus")
|
|
229 }
|
|
230
|
|
231 # ---------------------- load the gene names into a data.frame and merge with the frequency count ----------------------
|
|
232
|
|
233 tcV = textConnection(V)
|
|
234 Vchain = read.table(tcV, sep="\t", header=TRUE)
|
|
235 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
|
|
236 close(tcV)
|
|
237
|
|
238 tcD = textConnection(D)
|
|
239 Dchain = read.table(tcD, sep="\t", header=TRUE)
|
|
240 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
|
|
241 close(tcD)
|
|
242
|
|
243 tcJ = textConnection(J)
|
|
244 Jchain = read.table(tcJ, sep="\t", header=TRUE)
|
|
245 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
|
|
246 close(tcJ)
|
|
247
|
|
248 # ---------------------- Create the V, D and J frequency plots and write the data.frame for every plot to a file ----------------------
|
|
249
|
|
250 pV = ggplot(PRODFV)
|
|
251 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))
|
|
252 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
|
|
253 write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
254
|
|
255 png("VPlot.png",width = 1280, height = 720)
|
|
256 pV
|
|
257 dev.off();
|
|
258
|
|
259 if(useD){
|
|
260 pD = ggplot(PRODFD)
|
|
261 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))
|
|
262 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
|
|
263 write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
264
|
|
265 png("DPlot.png",width = 800, height = 600)
|
|
266 print(pD)
|
|
267 dev.off();
|
|
268 }
|
|
269
|
|
270 pJ = ggplot(PRODFJ)
|
|
271 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))
|
|
272 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
|
|
273 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
274
|
|
275 png("JPlot.png",width = 800, height = 600)
|
|
276 pJ
|
|
277 dev.off();
|
|
278
|
|
279 pJ = ggplot(PRODFJ)
|
|
280 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))
|
|
281 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
|
|
282 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
283
|
|
284 png("JPlot.png",width = 800, height = 600)
|
|
285 pJ
|
|
286 dev.off();
|
|
287
|
|
288 # ---------------------- Now the frequency plots of the V, D and J families ----------------------
|
|
289
|
|
290 VGenes = PRODF[,c("Sample", "Top.V.Gene")]
|
|
291 VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
|
|
292 VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
|
|
293 TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
294 VGenes = merge(VGenes, TotalPerSample, by="Sample")
|
|
295 VGenes$Frequency = VGenes$Count * 100 / VGenes$total
|
|
296 VPlot = ggplot(VGenes)
|
|
297 VPlot = VPlot + geom_bar(aes( x = Top.V.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
298 ggtitle("Distribution of V gene families") +
|
|
299 ylab("Percentage of sequences")
|
|
300 png("VFPlot.png")
|
|
301 VPlot
|
|
302 dev.off();
|
|
303 write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
304
|
|
305 if(useD){
|
|
306 DGenes = PRODF[,c("Sample", "Top.D.Gene")]
|
|
307 DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
|
|
308 DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
|
|
309 TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
310 DGenes = merge(DGenes, TotalPerSample, by="Sample")
|
|
311 DGenes$Frequency = DGenes$Count * 100 / DGenes$total
|
|
312 DPlot = ggplot(DGenes)
|
|
313 DPlot = DPlot + geom_bar(aes( x = Top.D.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
314 ggtitle("Distribution of D gene families") +
|
|
315 ylab("Percentage of sequences")
|
|
316 png("DFPlot.png")
|
|
317 print(DPlot)
|
|
318 dev.off();
|
|
319 write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
320 }
|
|
321
|
|
322 JGenes = PRODF[,c("Sample", "Top.J.Gene")]
|
|
323 JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
|
|
324 JGenes = data.frame(data.table(JGenes)[, list(Count=.N), by=c("Sample", "Top.J.Gene")])
|
|
325 TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
|
|
326 JGenes = merge(JGenes, TotalPerSample, by="Sample")
|
|
327 JGenes$Frequency = JGenes$Count * 100 / JGenes$total
|
|
328 JPlot = ggplot(JGenes)
|
|
329 JPlot = JPlot + geom_bar(aes( x = Top.J.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
330 ggtitle("Distribution of J gene families") +
|
|
331 ylab("Percentage of sequences")
|
|
332 png("JFPlot.png")
|
|
333 JPlot
|
|
334 dev.off();
|
|
335 write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
336
|
|
337 # ---------------------- Plotting the cdr3 length ----------------------
|
|
338
|
|
339 CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length.DNA")])
|
|
340 TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
|
|
341 CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
|
|
342 CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
|
|
343 CDR3LengthPlot = ggplot(CDR3Length)
|
|
344 CDR3LengthPlot = CDR3LengthPlot + geom_bar(aes( x = CDR3.Length.DNA, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
345 ggtitle("Length distribution of CDR3") +
|
|
346 xlab("CDR3 Length") +
|
|
347 ylab("Percentage of sequences")
|
|
348 png("CDR3LengthPlot.png",width = 1280, height = 720)
|
|
349 CDR3LengthPlot
|
|
350 dev.off()
|
|
351 write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
352
|
|
353 # ---------------------- Plot the heatmaps ----------------------
|
|
354
|
|
355
|
|
356 #get the reverse order for the V and D genes
|
|
357 revVchain = Vchain
|
|
358 revDchain = Dchain
|
|
359 revVchain$chr.orderV = rev(revVchain$chr.orderV)
|
|
360 revDchain$chr.orderD = rev(revDchain$chr.orderD)
|
|
361
|
|
362 if(useD){
|
|
363 plotVD <- function(dat){
|
|
364 if(length(dat[,1]) == 0){
|
|
365 return()
|
|
366 }
|
|
367 img = ggplot() +
|
|
368 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
|
369 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
370 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
371 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
372 xlab("D genes") +
|
|
373 ylab("V Genes")
|
|
374
|
|
375 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
376 print(img)
|
|
377 dev.off()
|
|
378 write.table(x=acast(dat, Top.V.Gene~Top.D.Gene, value.var="Length"), file=paste("HeatmapVD_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
|
|
379 }
|
|
380
|
|
381 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
|
|
382
|
|
383 VandDCount$l = log(VandDCount$Length)
|
|
384 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
|
|
385 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
|
|
386 VandDCount$relLength = VandDCount$l / VandDCount$max
|
|
387
|
|
388 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(inputdata$Sample))
|
|
389
|
|
390 completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
|
|
391 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
392 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
393 VDList = split(completeVD, f=completeVD[,"Sample"])
|
|
394
|
|
395 lapply(VDList, FUN=plotVD)
|
|
396 }
|
|
397
|
|
398 plotVJ <- function(dat){
|
|
399 if(length(dat[,1]) == 0){
|
|
400 return()
|
|
401 }
|
|
402 cat(paste(unique(dat[3])[1,1]))
|
|
403 img = ggplot() +
|
|
404 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
|
|
405 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
406 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
407 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
408 xlab("J genes") +
|
|
409 ylab("V Genes")
|
|
410
|
|
411 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
412 print(img)
|
|
413 dev.off()
|
|
414 write.table(x=acast(dat, Top.V.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapVJ_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
|
|
415 }
|
|
416
|
|
417 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
|
|
418
|
|
419 VandJCount$l = log(VandJCount$Length)
|
|
420 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
421 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
422 VandJCount$relLength = VandJCount$l / VandJCount$max
|
|
423
|
|
424 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(inputdata$Sample))
|
|
425
|
|
426 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
|
|
427 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
428 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
429 VJList = split(completeVJ, f=completeVJ[,"Sample"])
|
|
430 lapply(VJList, FUN=plotVJ)
|
|
431
|
|
432 if(useD){
|
|
433 plotDJ <- function(dat){
|
|
434 if(length(dat[,1]) == 0){
|
|
435 return()
|
|
436 }
|
|
437 img = ggplot() +
|
|
438 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
|
|
439 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
440 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
441 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
|
|
442 xlab("J genes") +
|
|
443 ylab("D Genes")
|
|
444
|
|
445 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
|
|
446 print(img)
|
|
447 dev.off()
|
|
448 write.table(x=acast(dat, Top.D.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapDJ_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
|
|
449 }
|
|
450
|
|
451
|
|
452 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
|
|
453
|
|
454 DandJCount$l = log(DandJCount$Length)
|
|
455 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
|
|
456 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
|
|
457 DandJCount$relLength = DandJCount$l / DandJCount$max
|
|
458
|
|
459 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(inputdata$Sample))
|
|
460
|
|
461 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
|
|
462 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
463 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
464 DJList = split(completeDJ, f=completeDJ[,"Sample"])
|
|
465 lapply(DJList, FUN=plotDJ)
|
|
466 }
|
|
467
|
|
468
|
|
469 # ---------------------- calculating the clonality score ----------------------
|
|
470
|
|
471 if("Replicate" %in% colnames(inputdata)) #can only calculate clonality score when replicate information is available
|
|
472 {
|
|
473 clonalityFrame = inputdata
|
|
474 if(clonaltype != "none"){
|
10
|
475 clonalityFrame$clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(clonaltype, ","))], sep = ":"))
|
7
|
476 clonalityFrame$ReplicateConcat = paste(clonalityFrame$clonaltype, clonalityFrame$Sample, clonalityFrame$Replicate, sep = ":")
|
|
477 clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$ReplicateConcat), ]
|
|
478 }
|
|
479 write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
|
|
480
|
|
481 ClonalitySampleReplicatePrint <- function(dat){
|
|
482 write.table(dat, paste("clonality_", unique(inputdata$Sample) , "_", unique(dat$Replicate), ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
|
|
483 }
|
|
484
|
|
485 clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,c("Sample", "Replicate")])
|
|
486 #lapply(clonalityFrameSplit, FUN=ClonalitySampleReplicatePrint)
|
|
487
|
|
488 ClonalitySamplePrint <- function(dat){
|
|
489 write.table(dat, paste("clonality_", unique(inputdata$Sample) , ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
|
|
490 }
|
|
491
|
|
492 clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,"Sample"])
|
|
493 #lapply(clonalityFrameSplit, FUN=ClonalitySamplePrint)
|
|
494
|
|
495 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "clonaltype")])
|
|
496 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
|
|
497 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
|
|
498 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
|
|
499 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
|
|
500
|
|
501 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
|
|
502 tcct = textConnection(ct)
|
|
503 CT = read.table(tcct, sep="\t", header=TRUE)
|
|
504 close(tcct)
|
|
505 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
|
|
506 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
|
|
507
|
|
508 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "clonaltype")])
|
|
509 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
|
|
510 clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
|
|
511 ReplicateReads$squared = ReplicateReads$Reads * ReplicateReads$Reads
|
|
512
|
|
513 ReplicatePrint <- function(dat){
|
|
514 write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
515 }
|
|
516
|
|
517 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
518 lapply(ReplicateSplit, FUN=ReplicatePrint)
|
|
519
|
10
|
520 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(as.numeric(Reads)), ReadsSquaredSum=sum(as.numeric(squared))), by=c("Sample")])
|
7
|
521 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
|
|
522
|
|
523
|
|
524 ReplicateSumPrint <- function(dat){
|
|
525 write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
526 }
|
|
527
|
|
528 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
|
|
529 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
|
|
530
|
|
531 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
|
|
532 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
|
|
533 clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
|
|
534 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
|
|
535 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
|
|
536
|
|
537 ClonalityScorePrint <- function(dat){
|
|
538 write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
539 }
|
|
540
|
|
541 clonalityScore = clonalFreqCount[c("Sample", "Result")]
|
|
542 clonalityScore = unique(clonalityScore)
|
|
543
|
|
544 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
|
|
545 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
|
|
546
|
|
547 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
|
|
548
|
|
549
|
|
550
|
|
551 ClonalityOverviewPrint <- function(dat){
|
|
552 write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
553 }
|
|
554
|
|
555 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
|
|
556 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
|
|
557 }
|
|
558
|
|
559 imgtcolumns = c("X3V.REGION.trimmed.nt.nb","P3V.nt.nb", "N1.REGION.nt.nb", "P5D.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "P3D.nt.nb", "N2.REGION.nt.nb", "P5J.nt.nb", "X5J.REGION.trimmed.nt.nb", "X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb")
|
|
560 if(all(imgtcolumns %in% colnames(inputdata)))
|
|
561 {
|
8
|
562 newData = data.frame(data.table(PRODF)[,list(unique=.N,
|
10
|
563 VH.DEL=mean(X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
564 P1=mean(P3V.nt.nb, na.rm=T),
|
|
565 N1=mean(N1.REGION.nt.nb, na.rm=T),
|
|
566 P2=mean(P5D.nt.nb, na.rm=T),
|
|
567 DEL.DH=mean(X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
568 DH.DEL=mean(X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
569 P3=mean(P3D.nt.nb, na.rm=T),
|
|
570 N2=mean(N2.REGION.nt.nb, na.rm=T),
|
|
571 P4=mean(P5J.nt.nb, na.rm=T),
|
|
572 DEL.JH=mean(X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
573 Total.Del=( mean(X3V.REGION.trimmed.nt.nb, na.rm=T) +
|
|
574 mean(X5D.REGION.trimmed.nt.nb, na.rm=T) +
|
|
575 mean(X3D.REGION.trimmed.nt.nb, na.rm=T) +
|
|
576 mean(X5J.REGION.trimmed.nt.nb, na.rm=T)),
|
|
577
|
|
578 Total.N=( mean(N1.REGION.nt.nb, na.rm=T) +
|
|
579 mean(N2.REGION.nt.nb, na.rm=T)),
|
|
580
|
|
581 Total.P=( mean(P3V.nt.nb, na.rm=T) +
|
|
582 mean(P5D.nt.nb, na.rm=T) +
|
|
583 mean(P3D.nt.nb, na.rm=T) +
|
|
584 mean(P5J.nt.nb, na.rm=T))),
|
|
585 by=c("Sample")])
|
9
|
586 write.table(newData, "junctionAnalysisProd.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
|
|
587
|
10
|
588 newData = data.frame(data.table(UNPROD)[,list(unique=.N,
|
|
589 VH.DEL=mean(X3V.REGION.trimmed.nt.nb, na.rm=T),
|
|
590 P1=mean(P3V.nt.nb, na.rm=T),
|
|
591 N1=mean(N1.REGION.nt.nb, na.rm=T),
|
|
592 P2=mean(P5D.nt.nb, na.rm=T),
|
|
593 DEL.DH=mean(X5D.REGION.trimmed.nt.nb, na.rm=T),
|
|
594 DH.DEL=mean(X3D.REGION.trimmed.nt.nb, na.rm=T),
|
|
595 P3=mean(P3D.nt.nb, na.rm=T),
|
|
596 N2=mean(N2.REGION.nt.nb, na.rm=T),
|
|
597 P4=mean(P5J.nt.nb, na.rm=T),
|
|
598 DEL.JH=mean(X5J.REGION.trimmed.nt.nb, na.rm=T),
|
|
599 Total.Del=( mean(X3V.REGION.trimmed.nt.nb, na.rm=T) +
|
|
600 mean(X5D.REGION.trimmed.nt.nb, na.rm=T) +
|
|
601 mean(X3D.REGION.trimmed.nt.nb, na.rm=T) +
|
|
602 mean(X5J.REGION.trimmed.nt.nb, na.rm=T)),
|
|
603
|
|
604 Total.N=( mean(N1.REGION.nt.nb, na.rm=T) +
|
|
605 mean(N2.REGION.nt.nb, na.rm=T)),
|
|
606
|
|
607 Total.P=( mean(P3V.nt.nb, na.rm=T) +
|
|
608 mean(P5D.nt.nb, na.rm=T) +
|
|
609 mean(P3D.nt.nb, na.rm=T) +
|
|
610 mean(P5J.nt.nb, na.rm=T))),
|
|
611 by=c("Sample")])
|
9
|
612 write.table(newData, "junctionAnalysisUnProd.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
|
7
|
613 }
|