annotate report_clonality/RScript.r @ 40:106275b54470 draft

Uploaded
author davidvanzessen
date Tue, 30 May 2017 07:26:33 -0400
parents b6936fb52ab9
children a70dbcfa5d8a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
1 # ---------------------- load/install packages ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
2
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
3 if (!("gridExtra" %in% rownames(installed.packages()))) {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
4 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
5 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
6 library(gridExtra)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
7 if (!("ggplot2" %in% rownames(installed.packages()))) {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
8 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
9 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
10 library(ggplot2)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
11 if (!("plyr" %in% rownames(installed.packages()))) {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
12 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
13 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
14 library(plyr)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
15
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
16 if (!("data.table" %in% rownames(installed.packages()))) {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
17 install.packages("data.table", repos="http://cran.xl-mirror.nl/")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
18 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
19 library(data.table)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
20
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
21 if (!("reshape2" %in% rownames(installed.packages()))) {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
22 install.packages("reshape2", repos="http://cran.xl-mirror.nl/")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
23 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
24 library(reshape2)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
25
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
26 if (!("lymphclon" %in% rownames(installed.packages()))) {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
27 install.packages("lymphclon", repos="http://cran.xl-mirror.nl/")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
28 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
29 library(lymphclon)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
30
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
31 # ---------------------- parameters ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
32
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
33 args <- commandArgs(trailingOnly = TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
34
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
35 infile = args[1] #path to input file
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
36 outfile = args[2] #path to output file
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
37 outdir = args[3] #path to output folder (html/images/data)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
38 clonaltype = args[4] #clonaltype definition, or 'none' for no unique filtering
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
39 ct = unlist(strsplit(clonaltype, ","))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
40 species = args[5] #human or mouse
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
41 locus = args[6] # IGH, IGK, IGL, TRB, TRA, TRG or TRD
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
42 filterproductive = ifelse(args[7] == "yes", T, F) #should unproductive sequences be filtered out? (yes/no)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
43 clonality_method = args[8]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
44
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
45
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
46 # ---------------------- Data preperation ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
47
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
48 print("Report Clonality - Data preperation")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
49
13
d3ebaa2d2fe0 Uploaded
davidvanzessen
parents: 9
diff changeset
50 inputdata = read.table(infile, sep="\t", header=TRUE, fill=T, comment.char="", stringsAsFactors=F)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
51
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
52
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
53 print(paste("nrows: ", nrow(inputdata)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
54
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
55 setwd(outdir)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
56
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
57 # remove weird rows
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
58 inputdata = inputdata[inputdata$Sample != "",]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
59
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
60 print(paste("nrows: ", nrow(inputdata)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
61
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
62 #remove the allele from the V,D and J genes
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
63 inputdata$Top.V.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.V.Gene)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
64 inputdata$Top.D.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.D.Gene)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
65 inputdata$Top.J.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.J.Gene)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
66
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
67 print(paste("nrows: ", nrow(inputdata)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
68
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
69 #filter uniques
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
70 inputdata.removed = inputdata[NULL,]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
71
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
72 print(paste("nrows: ", nrow(inputdata)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
73
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
74 inputdata$clonaltype = 1:nrow(inputdata)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
75
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
76 #keep track of the count of sequences in samples or samples/replicates for the front page overview
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
77 input.sample.count = data.frame(data.table(inputdata)[, list(All=.N), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
78 input.rep.count = data.frame(data.table(inputdata)[, list(All=.N), by=c("Sample", "Replicate")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
79
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
80 PRODF = inputdata
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
81 UNPROD = inputdata
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
82 if(filterproductive){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
83 if("Functionality" %in% colnames(inputdata)) { # "Functionality" is an IMGT column
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
84 #PRODF = inputdata[inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)", ]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
85 PRODF = inputdata[inputdata$Functionality %in% c("productive (see comment)","productive"),]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
86
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
87 PRODF.count = data.frame(data.table(PRODF)[, list(count=.N), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
88
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
89 UNPROD = inputdata[inputdata$Functionality %in% c("unproductive (see comment)","unproductive"), ]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
90 } else {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
91 PRODF = inputdata[inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" , ]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
92 UNPROD = inputdata[!(inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" ), ]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
93 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
94 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
95
13
d3ebaa2d2fe0 Uploaded
davidvanzessen
parents: 9
diff changeset
96 for(i in 1:nrow(UNPROD)){
d3ebaa2d2fe0 Uploaded
davidvanzessen
parents: 9
diff changeset
97 if(!is.numeric(UNPROD[i,"CDR3.Length"])){
d3ebaa2d2fe0 Uploaded
davidvanzessen
parents: 9
diff changeset
98 UNPROD[i,"CDR3.Length"] = 0
d3ebaa2d2fe0 Uploaded
davidvanzessen
parents: 9
diff changeset
99 }
d3ebaa2d2fe0 Uploaded
davidvanzessen
parents: 9
diff changeset
100 }
d3ebaa2d2fe0 Uploaded
davidvanzessen
parents: 9
diff changeset
101
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
102 prod.sample.count = data.frame(data.table(PRODF)[, list(Productive=.N), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
103 prod.rep.count = data.frame(data.table(PRODF)[, list(Productive=.N), by=c("Sample", "Replicate")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
104
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
105 unprod.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive=.N), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
106 unprod.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive=.N), by=c("Sample", "Replicate")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
107
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
108 clonalityFrame = PRODF
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
109
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
110 #remove duplicates based on the clonaltype
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
111 if(clonaltype != "none"){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
112 clonaltype = paste(clonaltype, ",Sample", sep="") #add sample column to clonaltype, unique within samples
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
113 PRODF$clonaltype = do.call(paste, c(PRODF[unlist(strsplit(clonaltype, ","))], sep = ":"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
114 PRODF = PRODF[!duplicated(PRODF$clonaltype), ]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
115
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
116 UNPROD$clonaltype = do.call(paste, c(UNPROD[unlist(strsplit(clonaltype, ","))], sep = ":"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
117 UNPROD = UNPROD[!duplicated(UNPROD$clonaltype), ]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
118
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
119 #again for clonalityFrame but with sample+replicate
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
120 clonalityFrame$clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(clonaltype, ","))], sep = ":"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
121 clonalityFrame$clonality_clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(paste(clonaltype, ",Replicate", sep=""), ","))], sep = ":"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
122 clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$clonality_clonaltype), ]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
123 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
124
40
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
125 if(nrow(PRODF) == 0){
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
126 stop("No sequences left after filtering")
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
127 }
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
128
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
129 prod.unique.sample.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
130 prod.unique.rep.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample", "Replicate")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
131
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
132 unprod.unique.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
133 unprod.unique.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample", "Replicate")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
134
40
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
135
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
136 PRODF$freq = 1
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
137
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
138 if(any(grepl(pattern="_", x=PRODF$ID))){ #the frequency can be stored in the ID with the pattern ".*_freq_.*"
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
139 PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
140 PRODF$freq = gsub("_.*", "", PRODF$freq)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
141 PRODF$freq = as.numeric(PRODF$freq)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
142 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
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
143 PRODF$freq = 1
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
144 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
145 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
146
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
147 #make a names list with sample -> color
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
148 naive.colors = c('blue4', 'darkred', 'olivedrab3', 'red', 'gray74', 'darkviolet', 'lightblue1', 'gold', 'chartreuse2', 'pink', 'Paleturquoise3', 'Chocolate1', 'Yellow', 'Deeppink3', 'Mediumorchid1', 'Darkgreen', 'Blue', 'Gray36', 'Hotpink', 'Yellow4')
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
149 unique.samples = unique(PRODF$Sample)
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
150
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
151 if(length(unique.samples) <= length(naive.colors)){
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
152 sample.colors = naive.colors[1:length(unique.samples)]
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
153 } else {
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
154 sample.colors = rainbow(length(unique.samples))
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
155 }
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
156
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
157 names(sample.colors) = unique.samples
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
158
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
159 print("Sample.colors")
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
160 print(sample.colors)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
161
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
162
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
163 #write the complete dataset that is left over, will be the input if 'none' for clonaltype and 'no' for filterproductive
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
164 write.table(PRODF, "allUnique.txt", sep="\t",quote=F,row.names=F,col.names=T)
18
5d11c9139a55 Uploaded
davidvanzessen
parents: 17
diff changeset
165 #write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
166 write.table(UNPROD, "allUnproductive.txt", sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
167
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
168 print("SAMPLE TABLE:")
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
169 print(table(PRODF$Sample))
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
170
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
171 #write the samples to a file
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
172 sampleFile <- file("samples.txt")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
173 un = unique(inputdata$Sample)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
174 un = paste(un, sep="\n")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
175 writeLines(un, sampleFile)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
176 close(sampleFile)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
177
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
178 # ---------------------- Counting the productive/unproductive and unique sequences ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
179
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
180 print("Report Clonality - counting productive/unproductive/unique")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
181
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
182 #create the table on the overview page with the productive/unique counts per sample/replicate
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
183 #first for sample
40
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
184
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
185 sample.count = merge(input.sample.count, prod.sample.count, by="Sample", all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
186 sample.count$perc_prod = round(sample.count$Productive / sample.count$All * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
187 sample.count = merge(sample.count, prod.unique.sample.count, by="Sample", all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
188 sample.count$perc_prod_un = round(sample.count$Productive_unique / sample.count$All * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
189
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
190 sample.count = merge(sample.count , unprod.sample.count, by="Sample", all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
191 sample.count$perc_unprod = round(sample.count$Unproductive / sample.count$All * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
192 sample.count = merge(sample.count, unprod.unique.sample.count, by="Sample", all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
193 sample.count$perc_unprod_un = round(sample.count$Unproductive_unique / sample.count$All * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
194
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
195 #then sample/replicate
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
196 rep.count = merge(input.rep.count, prod.rep.count, by=c("Sample", "Replicate"), all.x=T)
40
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
197
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
198 print(rep.count)
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
199
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
200 fltr = is.na(rep.count$Productive)
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
201 if(any(fltr)){
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
202 rep.count[fltr,"Productive"] = 0
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
203 }
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
204
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
205 print(rep.count)
106275b54470 Uploaded
davidvanzessen
parents: 38
diff changeset
206
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
207 rep.count$perc_prod = round(rep.count$Productive / rep.count$All * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
208 rep.count = merge(rep.count, prod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
209 rep.count$perc_prod_un = round(rep.count$Productive_unique / rep.count$All * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
210
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
211 rep.count = merge(rep.count, unprod.rep.count, by=c("Sample", "Replicate"), all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
212 rep.count$perc_unprod = round(rep.count$Unproductive / rep.count$All * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
213 rep.count = merge(rep.count, unprod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
214 rep.count$perc_unprod_un = round(rep.count$Unproductive_unique / rep.count$All * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
215
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
216 rep.count$Sample = paste(rep.count$Sample, rep.count$Replicate, sep="_")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
217 rep.count = rep.count[,names(rep.count) != "Replicate"]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
218
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
219 count = rbind(sample.count, rep.count)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
220
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
221
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
222
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
223 write.table(x=count, file="productive_counting.txt", sep=",",quote=F,row.names=F,col.names=F)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
224
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
225 # ---------------------- V+J+CDR3 sequence count ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
226
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
227 VJCDR3.count = data.frame(table(clonalityFrame$Top.V.Gene, clonalityFrame$Top.J.Gene, clonalityFrame$CDR3.Seq.DNA))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
228 names(VJCDR3.count) = c("Top.V.Gene", "Top.J.Gene", "CDR3.Seq.DNA", "Count")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
229
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
230 VJCDR3.count = VJCDR3.count[VJCDR3.count$Count > 0,]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
231 VJCDR3.count = VJCDR3.count[order(-VJCDR3.count$Count),]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
232
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
233 write.table(x=VJCDR3.count, file="VJCDR3_count.txt", sep="\t",quote=F,row.names=F,col.names=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
234
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
235 # ---------------------- Frequency calculation for V, D and J ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
236
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
237 print("Report Clonality - frequency calculation V, D and J")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
238
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
239 PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
240 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
241 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
242 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
243
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
244 PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
245 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
246 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
247 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
248
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
249 PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
250 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
251 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
252 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
253
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
254 # ---------------------- Setting up the gene names for the different species/loci ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
255
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
256 print("Report Clonality - getting genes for species/loci")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
257
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
258 Vchain = ""
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
259 Dchain = ""
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
260 Jchain = ""
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
261
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
262 if(species == "custom"){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
263 print("Custom genes: ")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
264 splt = unlist(strsplit(locus, ";"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
265 print(paste("V:", splt[1]))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
266 print(paste("D:", splt[2]))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
267 print(paste("J:", splt[3]))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
268
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
269 Vchain = unlist(strsplit(splt[1], ","))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
270 Vchain = data.frame(v.name = Vchain, chr.orderV = 1:length(Vchain))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
271
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
272 Dchain = unlist(strsplit(splt[2], ","))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
273 if(length(Dchain) > 0){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
274 Dchain = data.frame(v.name = Dchain, chr.orderD = 1:length(Dchain))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
275 } else {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
276 Dchain = data.frame(v.name = character(0), chr.orderD = numeric(0))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
277 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
278
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
279 Jchain = unlist(strsplit(splt[3], ","))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
280 Jchain = data.frame(v.name = Jchain, chr.orderJ = 1:length(Jchain))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
281
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
282 } else {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
283 genes = read.table("genes.txt", sep="\t", header=TRUE, fill=T, comment.char="")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
284
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
285 Vchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "V",c("IMGT.GENE.DB", "chr.order")]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
286 colnames(Vchain) = c("v.name", "chr.orderV")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
287 Dchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "D",c("IMGT.GENE.DB", "chr.order")]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
288 colnames(Dchain) = c("v.name", "chr.orderD")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
289 Jchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "J",c("IMGT.GENE.DB", "chr.order")]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
290 colnames(Jchain) = c("v.name", "chr.orderJ")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
291 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
292 useD = TRUE
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
293 if(nrow(Dchain) == 0){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
294 useD = FALSE
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
295 cat("No D Genes in this species/locus")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
296 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
297 print(paste(nrow(Vchain), "genes in V"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
298 print(paste(nrow(Dchain), "genes in D"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
299 print(paste(nrow(Jchain), "genes in J"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
300
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
301 # ---------------------- merge with the frequency count ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
302
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
303 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
304
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
305 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
306
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
307 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
308
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
309 # ---------------------- Create the V, D and J frequency plots and write the data.frame for every plot to a file ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
310
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
311 print("Report Clonality - V, D and J frequency plots")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
312
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
313 pV = ggplot(PRODFV)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
314 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))
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
315 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage") + scale_fill_manual(values=sample.colors)
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
316 pV = pV + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
18
5d11c9139a55 Uploaded
davidvanzessen
parents: 17
diff changeset
317 write.table(x=PRODFV, file="VFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
318
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
319 png("VPlot.png",width = 1280, height = 720)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
320 pV
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
321 dev.off()
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
322
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
323 ggsave("VPlot.pdf", pV, width=13, height=7)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
324
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
325 if(useD){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
326 pD = ggplot(PRODFD)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
327 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))
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
328 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage") + scale_fill_manual(values=sample.colors)
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
329 pD = pD + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
18
5d11c9139a55 Uploaded
davidvanzessen
parents: 17
diff changeset
330 write.table(x=PRODFD, file="DFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
331
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
332 png("DPlot.png",width = 800, height = 600)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
333 print(pD)
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
334 dev.off()
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
335
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
336 ggsave("DPlot.pdf", pD, width=10, height=7)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
337 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
338
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
339 pJ = ggplot(PRODFJ)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
340 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))
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
341 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage") + scale_fill_manual(values=sample.colors)
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
342 pJ = pJ + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
18
5d11c9139a55 Uploaded
davidvanzessen
parents: 17
diff changeset
343 write.table(x=PRODFJ, file="JFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
344
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
345 png("JPlot.png",width = 800, height = 600)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
346 pJ
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
347 dev.off()
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
348
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
349 ggsave("JPlot.pdf", pJ)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
350
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
351 # ---------------------- Now the frequency plots of the V, D and J families ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
352
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
353 print("Report Clonality - V, D and J family plots")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
354
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
355 VGenes = PRODF[,c("Sample", "Top.V.Gene")]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
356 VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
357 VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
358 TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
359 VGenes = merge(VGenes, TotalPerSample, by="Sample")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
360 VGenes$Frequency = VGenes$Count * 100 / VGenes$total
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
361 VPlot = ggplot(VGenes)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
362 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)) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
363 ggtitle("Distribution of V gene families") +
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
364 ylab("Percentage of sequences") +
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
365 scale_fill_manual(values=sample.colors) +
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
366 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
367 png("VFPlot.png")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
368 VPlot
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
369 dev.off()
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
370 ggsave("VFPlot.pdf", VPlot)
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
371
18
5d11c9139a55 Uploaded
davidvanzessen
parents: 17
diff changeset
372 write.table(x=VGenes, file="VFFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
373
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
374 if(useD){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
375 DGenes = PRODF[,c("Sample", "Top.D.Gene")]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
376 DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
377 DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
378 TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
379 DGenes = merge(DGenes, TotalPerSample, by="Sample")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
380 DGenes$Frequency = DGenes$Count * 100 / DGenes$total
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
381 DPlot = ggplot(DGenes)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
382 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)) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
383 ggtitle("Distribution of D gene families") +
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
384 ylab("Percentage of sequences") +
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
385 scale_fill_manual(values=sample.colors) +
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
386 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
387 png("DFPlot.png")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
388 print(DPlot)
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
389 dev.off()
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
390
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
391 ggsave("DFPlot.pdf", DPlot)
18
5d11c9139a55 Uploaded
davidvanzessen
parents: 17
diff changeset
392 write.table(x=DGenes, file="DFFrequency.txt", sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
393 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
394
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
395 # ---------------------- Plotting the cdr3 length ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
396
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
397 print("Report Clonality - CDR3 length plot")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
398
9
efa1f5a17b6e Uploaded
davidvanzessen
parents: 8
diff changeset
399 CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length")])
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
400 TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
401 CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
402 CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
403 CDR3LengthPlot = ggplot(CDR3Length)
15
02efa5764a0a Uploaded
davidvanzessen
parents: 14
diff changeset
404 CDR3LengthPlot = CDR3LengthPlot + geom_bar(aes( x = factor(reorder(CDR3.Length, as.numeric(CDR3.Length))), y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
405 ggtitle("Length distribution of CDR3") +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
406 xlab("CDR3 Length") +
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
407 ylab("Percentage of sequences") +
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
408 scale_fill_manual(values=sample.colors) +
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
409 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
410 png("CDR3LengthPlot.png",width = 1280, height = 720)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
411 CDR3LengthPlot
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
412 dev.off()
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
413
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
414 ggsave("CDR3LengthPlot.pdf", CDR3LengthPlot, width=12, height=7)
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
415
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
416 write.table(x=CDR3Length, file="CDR3LengthPlot.txt", sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
417
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
418 # ---------------------- Plot the heatmaps ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
419
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
420 #get the reverse order for the V and D genes
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
421 revVchain = Vchain
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
422 revDchain = Dchain
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
423 revVchain$chr.orderV = rev(revVchain$chr.orderV)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
424 revDchain$chr.orderD = rev(revDchain$chr.orderD)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
425
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
426 if(useD){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
427 print("Report Clonality - Heatmaps VD")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
428 plotVD <- function(dat){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
429 if(length(dat[,1]) == 0){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
430 return()
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
431 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
432
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
433 img = ggplot() +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
434 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
435 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
436 scale_fill_gradient(low="gold", high="blue", na.value="white") +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
437 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
438 xlab("D genes") +
9
efa1f5a17b6e Uploaded
davidvanzessen
parents: 8
diff changeset
439 ylab("V Genes") +
14
15961ca8d9ce Uploaded
davidvanzessen
parents: 13
diff changeset
440 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), panel.grid.major = element_line(colour = "gainsboro"))
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
441
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
442 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
443 print(img)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
444 dev.off()
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
445
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
446 ggsave(paste("HeatmapVD_", unique(dat[3])[1,1] , ".pdf", sep=""), img, height=13, width=8)
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
447
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
448 write.table(x=acast(dat, Top.V.Gene~Top.D.Gene, value.var="Length"), file=paste("HeatmapVD_", unique(dat[3])[1,1], ".txt", sep=""), sep="\t",quote=F,row.names=T,col.names=NA)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
449 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
450
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
451 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
452
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
453 VandDCount$l = log(VandDCount$Length)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
454 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
455 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
456 VandDCount$relLength = VandDCount$l / VandDCount$max
6
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
457 check = is.nan(VandDCount$relLength)
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
458 if(any(check)){
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
459 VandDCount[check,"relLength"] = 0
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
460 }
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
461
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
462 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
463
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
464 completeVD = merge(VandDCount, cartegianProductVD, by.x=c("Top.V.Gene", "Top.D.Gene"), by.y=c("Top.V.Gene", "Top.D.Gene"), all=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
465
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
466 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
467
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
468 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
469
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
470 fltr = is.nan(completeVD$relLength)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
471 if(all(fltr)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
472 completeVD[fltr,"relLength"] = 0
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
473 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
474
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
475 VDList = split(completeVD, f=completeVD[,"Sample"])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
476 lapply(VDList, FUN=plotVD)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
477 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
478
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
479 print("Report Clonality - Heatmaps VJ")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
480
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
481 plotVJ <- function(dat){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
482 if(length(dat[,1]) == 0){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
483 return()
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
484 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
485 cat(paste(unique(dat[3])[1,1]))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
486 img = ggplot() +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
487 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
488 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
489 scale_fill_gradient(low="gold", high="blue", na.value="white") +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
490 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
491 xlab("J genes") +
9
efa1f5a17b6e Uploaded
davidvanzessen
parents: 8
diff changeset
492 ylab("V Genes") +
14
15961ca8d9ce Uploaded
davidvanzessen
parents: 13
diff changeset
493 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), panel.grid.major = element_line(colour = "gainsboro"))
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
494
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
495 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
496 print(img)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
497 dev.off()
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
498
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
499 ggsave(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".pdf", sep=""), img, height=11, width=4)
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
500
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
501 write.table(x=acast(dat, Top.V.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapVJ_", unique(dat[3])[1,1], ".txt", sep=""), sep="\t",quote=F,row.names=T,col.names=NA)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
502 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
503
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
504 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
505
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
506 VandJCount$l = log(VandJCount$Length)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
507 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
508 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
509 VandJCount$relLength = VandJCount$l / VandJCount$max
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
510
6
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
511 check = is.nan(VandJCount$relLength)
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
512 if(any(check)){
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
513 VandJCount[check,"relLength"] = 0
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
514 }
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
515
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
516 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
517
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
518 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
519 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
520 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
521
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
522 fltr = is.nan(completeVJ$relLength)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
523 if(any(fltr)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
524 completeVJ[fltr,"relLength"] = 1
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
525 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
526
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
527 VJList = split(completeVJ, f=completeVJ[,"Sample"])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
528 lapply(VJList, FUN=plotVJ)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
529
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
530
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
531
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
532 if(useD){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
533 print("Report Clonality - Heatmaps DJ")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
534 plotDJ <- function(dat){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
535 if(length(dat[,1]) == 0){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
536 return()
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
537 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
538 img = ggplot() +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
539 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
540 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
541 scale_fill_gradient(low="gold", high="blue", na.value="white") +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
542 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
543 xlab("J genes") +
9
efa1f5a17b6e Uploaded
davidvanzessen
parents: 8
diff changeset
544 ylab("D Genes") +
14
15961ca8d9ce Uploaded
davidvanzessen
parents: 13
diff changeset
545 theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), panel.grid.major = element_line(colour = "gainsboro"))
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
546
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
547 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
548 print(img)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
549 dev.off()
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
550
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
551 ggsave(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".pdf", sep=""), img, width=4, height=7)
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
552
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
553 write.table(x=acast(dat, Top.D.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapDJ_", unique(dat[3])[1,1], ".txt", sep=""), sep="\t",quote=F,row.names=T,col.names=NA)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
554 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
555
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
556
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
557 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
558
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
559 DandJCount$l = log(DandJCount$Length)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
560 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
561 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
562 DandJCount$relLength = DandJCount$l / DandJCount$max
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
563
6
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
564 check = is.nan(DandJCount$relLength)
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
565 if(any(check)){
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
566 DandJCount[check,"relLength"] = 0
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
567 }
d001d0c05dbe Uploaded
davidvanzessen
parents: 5
diff changeset
568
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
569 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
570
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
571 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
572 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
573 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
574
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
575 fltr = is.nan(completeDJ$relLength)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
576 if(any(fltr)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
577 completeDJ[fltr, "relLength"] = 1
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
578 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
579
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
580 DJList = split(completeDJ, f=completeDJ[,"Sample"])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
581 lapply(DJList, FUN=plotDJ)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
582 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
583
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
584
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
585 # ---------------------- output tables for the circos plots ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
586
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
587 print("Report Clonality - Circos data")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
588
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
589 for(smpl in unique(PRODF$Sample)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
590 PRODF.sample = PRODF[PRODF$Sample == smpl,]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
591
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
592 fltr = PRODF.sample$Top.V.Gene == ""
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
593 if(any(fltr, na.rm=T)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
594 PRODF.sample[fltr, "Top.V.Gene"] = "NA"
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
595 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
596
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
597 fltr = PRODF.sample$Top.D.Gene == ""
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
598 if(any(fltr, na.rm=T)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
599 PRODF.sample[fltr, "Top.D.Gene"] = "NA"
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
600 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
601
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
602 fltr = PRODF.sample$Top.J.Gene == ""
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
603 if(any(fltr, na.rm=T)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
604 PRODF.sample[fltr, "Top.J.Gene"] = "NA"
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
605 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
606
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
607 v.d = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.D.Gene)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
608 v.j = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.J.Gene)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
609 d.j = table(PRODF.sample$Top.D.Gene, PRODF.sample$Top.J.Gene)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
610
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
611 write.table(v.d, file=paste(smpl, "_VD_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
612 write.table(v.j, file=paste(smpl, "_VJ_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
613 write.table(d.j, file=paste(smpl, "_DJ_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
614 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
615
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
616 # ---------------------- calculating the clonality score ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
617
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
618 if("Replicate" %in% colnames(inputdata)) #can only calculate clonality score when replicate information is available
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
619 {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
620 print("Report Clonality - Clonality")
18
5d11c9139a55 Uploaded
davidvanzessen
parents: 17
diff changeset
621 write.table(clonalityFrame, "clonalityComplete.txt", sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
622 if(clonality_method == "boyd"){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
623 samples = split(clonalityFrame, clonalityFrame$Sample, drop=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
624
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
625 for (sample in samples){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
626 res = data.frame(paste=character(0))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
627 sample_id = unique(sample$Sample)[[1]]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
628 for(replicate in unique(sample$Replicate)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
629 tmp = sample[sample$Replicate == replicate,]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
630 clone_table = data.frame(table(tmp$clonaltype))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
631 clone_col_name = paste("V", replicate, sep="")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
632 colnames(clone_table) = c("paste", clone_col_name)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
633 res = merge(res, clone_table, by="paste", all=T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
634 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
635
17
da95be204ebc Uploaded
davidvanzessen
parents: 15
diff changeset
636 res[is.na(res)] = 0
da95be204ebc Uploaded
davidvanzessen
parents: 15
diff changeset
637
20
9185c3dfc679 Uploaded
davidvanzessen
parents: 18
diff changeset
638 write.table(res, file=paste("raw_clonality_", sample_id, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=F)
9185c3dfc679 Uploaded
davidvanzessen
parents: 18
diff changeset
639 write.table(as.matrix(res[,2:ncol(res)]), file=paste("raw_clonality2_", sample_id, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=F)
9185c3dfc679 Uploaded
davidvanzessen
parents: 18
diff changeset
640
9185c3dfc679 Uploaded
davidvanzessen
parents: 18
diff changeset
641 res = read.table(paste("raw_clonality_", sample_id, ".txt", sep=""), header=F, sep="\t", quote="", stringsAsFactors=F, fill=T, comment.char="")
17
da95be204ebc Uploaded
davidvanzessen
parents: 15
diff changeset
642
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
643 infer.result = infer.clonality(as.matrix(res[,2:ncol(res)]))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
644
13
d3ebaa2d2fe0 Uploaded
davidvanzessen
parents: 9
diff changeset
645 #print(infer.result)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
646
20
9185c3dfc679 Uploaded
davidvanzessen
parents: 18
diff changeset
647 write.table(data.table(infer.result[[12]]), file=paste("lymphclon_clonality_", sample_id, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=F)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
648
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
649 res$type = rowSums(res[,2:ncol(res)])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
650
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
651 coincidence.table = data.frame(table(res$type))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
652 colnames(coincidence.table) = c("Coincidence Type", "Raw Coincidence Freq")
20
9185c3dfc679 Uploaded
davidvanzessen
parents: 18
diff changeset
653 write.table(coincidence.table, file=paste("lymphclon_coincidences_", sample_id, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
654 }
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
655 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
656 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "clonaltype")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
657
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
658 #write files for every coincidence group of >1
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
659 samples = unique(clonalFreq$Sample)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
660 for(sample in samples){
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
661 clonalFreqSample = clonalFreq[clonalFreq$Sample == sample,]
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
662 if(max(clonalFreqSample$Type) > 1){
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
663 for(i in 2:max(clonalFreqSample$Type)){
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
664 clonalFreqSampleType = clonalFreqSample[clonalFreqSample$Type == i,]
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
665 clonalityFrame.sub = clonalityFrame[clonalityFrame$clonaltype %in% clonalFreqSampleType$clonaltype,]
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
666 clonalityFrame.sub = clonalityFrame.sub[order(clonalityFrame.sub$clonaltype),]
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
667 write.table(clonalityFrame.sub, file=paste("coincidences_", sample, "_", i, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
668 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
669 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
670 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
671
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
672 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
673 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
674 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
675 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
676
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
677 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
678 tcct = textConnection(ct)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
679 CT = read.table(tcct, sep="\t", header=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
680 close(tcct)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
681 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
682 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
683
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
684 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "clonaltype")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
685 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
686 clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
687 ReplicateReads$Reads = as.numeric(ReplicateReads$Reads)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
688 ReplicateReads$squared = as.numeric(ReplicateReads$Reads * ReplicateReads$Reads)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
689
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
690 ReplicatePrint <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
691 write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".txt", sep=""), sep="\t",quote=F,na="-",row.names=F,col.names=F)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
692 }
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
693
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
694 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
695 lapply(ReplicateSplit, FUN=ReplicatePrint)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
696
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
697 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(as.numeric(Reads)), ReadsSquaredSum=sum(as.numeric(squared))), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
698 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
699
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
700 ReplicateSumPrint <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
701 write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".txt", sep=""), sep="\t",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
702 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
703
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
704 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
705 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
706
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
707 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
708 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
709 clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
710 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
711 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
712
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
713 ClonalityScorePrint <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
714 write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".txt", sep=""), sep="\t",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
715 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
716
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
717 clonalityScore = clonalFreqCount[c("Sample", "Result")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
718 clonalityScore = unique(clonalityScore)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
719
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
720 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
721 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
722
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
723 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
724
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
725
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
726
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
727 ClonalityOverviewPrint <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
728 dat = dat[order(dat[,2]),]
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
729 write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".txt", sep=""), sep="\t",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
730 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
731
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
732 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
733 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
734
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
735 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
736
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
737 bak = PRODF
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
738 bakun = UNPROD
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
739
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
740 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")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
741 if(all(imgtcolumns %in% colnames(inputdata)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
742 {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
743 print("found IMGT columns, running junction analysis")
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
744
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
745 #ensure certain columns are in the data (files generated with older versions of IMGT Loader)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
746 col.checks = c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
747 for(col.check in col.checks){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
748 if(!(col.check %in% names(PRODF))){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
749 print(paste(col.check, "not found adding new column"))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
750 if(nrow(PRODF) > 0){ #because R is anoying...
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
751 PRODF[,col.check] = 0
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
752 } else {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
753 PRODF = cbind(PRODF, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
754 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
755 if(nrow(UNPROD) > 0){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
756 UNPROD[,col.check] = 0
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
757 } else {
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
758 UNPROD = cbind(UNPROD, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
759 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
760 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
761 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
762
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
763 PRODF.with.D = PRODF[nchar(PRODF$Top.D.Gene, keepNA=F) > 2,]
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
764 PRODF.no.D = PRODF[nchar(PRODF$Top.D.Gene, keepNA=F) < 4,]
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
765 write.table(PRODF.no.D, "productive_no_D.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=T)
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
766
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
767 UNPROD.with.D = UNPROD[nchar(UNPROD$Top.D.Gene, keepNA=F) > 2,]
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
768 UNPROD.no.D = UNPROD[nchar(UNPROD$Top.D.Gene, keepNA=F) < 4,]
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
769 write.table(UNPROD.no.D, "unproductive_no_D.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=T)
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
770
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
771 num_median = function(x, na.rm=T) { as.numeric(median(x, na.rm=na.rm)) }
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
772
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
773 newData = data.frame(data.table(PRODF.with.D)[,list(unique=.N,
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
774 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
775 P1=mean(.SD$P3V.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
776 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
777 P2=mean(.SD$P5D.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
778 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
779 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
780 P3=mean(.SD$P3D.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
781 N2=mean(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
782 P4=mean(.SD$P5J.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
783 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
784 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
785 Total.N=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
786 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
787 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
788 by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
789 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
790 write.table(newData, "junctionAnalysisProd_mean_wD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
791
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
792 newData = data.frame(data.table(PRODF.with.D)[,list(unique=.N,
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
793 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
794 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
795 N1=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
796 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
797 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
798 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
799 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
800 N2=num_median(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
801 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
802 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
803 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
804 Total.N=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
805 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
806 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
807 by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
808 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
809 write.table(newData, "junctionAnalysisProd_median_wD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
810
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
811 newData = data.frame(data.table(UNPROD.with.D)[,list(unique=.N,
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
812 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
813 P1=mean(.SD$P3V.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
814 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
815 P2=mean(.SD$P5D.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
816 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
817 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
818 P3=mean(.SD$P3D.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
819 N2=mean(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
820 P4=mean(.SD$P5J.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
821 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
822 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
823 Total.N=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
824 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
825 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
826 by=c("Sample")])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
827 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
828 write.table(newData, "junctionAnalysisUnProd_mean_wD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
829
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
830 newData = data.frame(data.table(UNPROD.with.D)[,list(unique=.N,
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
831 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
832 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
833 N1=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
834 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
835 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
836 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
837 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
838 N2=num_median(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
839 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
840 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
841 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
842 Total.N=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
843 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
844 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
845 by=c("Sample")])
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
846 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
847 write.table(newData, "junctionAnalysisUnProd_median_wD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
848
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
849 #---------------- again for no-D
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
850
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
851 newData = data.frame(data.table(PRODF.no.D)[,list(unique=.N,
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
852 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
853 P1=mean(.SD$P3V.nt.nb, na.rm=T),
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
854 N1=mean(.SD$N.REGION.nt.nb, na.rm=T),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
855 P2=mean(.SD$P5J.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
856 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
857 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
858 Total.N=mean(.SD$N.REGION.nt.nb, na.rm=T),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
859 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
860 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
861 by=c("Sample")])
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
862 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
863 write.table(newData, "junctionAnalysisProd_mean_nD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
864
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
865 newData = data.frame(data.table(PRODF.no.D)[,list(unique=.N,
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
866 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
867 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
30
b50965edac24 Uploaded
davidvanzessen
parents: 29
diff changeset
868 N1=num_median(.SD$N.REGION.nt.nb, na.rm=T),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
869 P2=num_median(.SD$P5J.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
870 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
871 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
30
b50965edac24 Uploaded
davidvanzessen
parents: 29
diff changeset
872 Total.N=num_median(.SD$N.REGION.nt.nb, na.rm=T),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
873 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
874 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
875 by=c("Sample")])
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
876 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
877 write.table(newData, "junctionAnalysisProd_median_nD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
878
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
879 newData = data.frame(data.table(UNPROD.no.D)[,list(unique=.N,
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
880 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
881 P1=mean(.SD$P3V.nt.nb, na.rm=T),
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
882 N1=mean(.SD$N.REGION.nt.nb, na.rm=T),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
883 P2=mean(.SD$P5J.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
884 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
885 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
886 Total.N=mean(.SD$N.REGION.nt.nb, na.rm=T),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
887 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
888 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
889 by=c("Sample")])
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
890 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
891 write.table(newData, "junctionAnalysisUnProd_mean_nD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
892
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents: 25
diff changeset
893
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
894 newData = data.frame(data.table(UNPROD.no.D)[,list(unique=.N,
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
895 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
896 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
30
b50965edac24 Uploaded
davidvanzessen
parents: 29
diff changeset
897 N1=num_median(.SD$N.REGION.nt.nb, na.rm=T),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
898 P2=num_median(.SD$P5J.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
899 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
900 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
30
b50965edac24 Uploaded
davidvanzessen
parents: 29
diff changeset
901 Total.N=num_median(.SD$N.REGION.nt.nb, na.rm=T),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
902 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
903 Median.CDR3.l=as.double(median(as.numeric(.SD$CDR3.Length), na.rm=T))),
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
904 by=c("Sample")])
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
905 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
906 write.table(newData, "junctionAnalysisUnProd_median_nD.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
907 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
908
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
909 PRODF = bak
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
910 UNPROD = bakun
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
911
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
912
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
913 # ---------------------- D reading frame ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
914
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
915 D.REGION.reading.frame = PRODF[,c("Sample", "D.REGION.reading.frame")]
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
916
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
917 chck = is.na(D.REGION.reading.frame$D.REGION.reading.frame)
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
918 if(any(chck)){
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
919 D.REGION.reading.frame[chck,"D.REGION.reading.frame"] = "No D"
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
920 }
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
921
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
922 D.REGION.reading.frame.1 = data.frame(data.table(D.REGION.reading.frame)[, list(Freq=.N), by=c("Sample", "D.REGION.reading.frame")])
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
923
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
924 D.REGION.reading.frame.2 = data.frame(data.table(D.REGION.reading.frame)[, list(sample.sum=sum(as.numeric(.SD$D.REGION.reading.frame), na.rm=T)), by=c("Sample")])
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
925
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
926 D.REGION.reading.frame = merge(D.REGION.reading.frame.1, D.REGION.reading.frame.2, by="Sample")
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
927
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
928 D.REGION.reading.frame$percentage = round(D.REGION.reading.frame$Freq / D.REGION.reading.frame$sample.sum * 100, 1)
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
929
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
930 write.table(D.REGION.reading.frame, "DReadingFrame.txt" , sep="\t",quote=F,row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
931
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
932 D.REGION.reading.frame = ggplot(D.REGION.reading.frame)
29
1f83e14f173b Uploaded
davidvanzessen
parents: 26
diff changeset
933 D.REGION.reading.frame = D.REGION.reading.frame + geom_bar(aes( x = D.REGION.reading.frame, y = percentage, fill=Sample), stat='identity', position='dodge' ) + ggtitle("D reading frame") + xlab("Frame") + ylab("Frequency")
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
934 D.REGION.reading.frame = D.REGION.reading.frame + scale_fill_manual(values=sample.colors)
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
935 D.REGION.reading.frame = D.REGION.reading.frame + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
936
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
937 png("DReadingFrame.png")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
938 D.REGION.reading.frame
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
939 dev.off()
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
940
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
941 ggsave("DReadingFrame.pdf", D.REGION.reading.frame)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
942
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
943 # ---------------------- AA composition in CDR3 ----------------------
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
944
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
945 AACDR3 = PRODF[,c("Sample", "CDR3.Seq")]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
946
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
947 TotalPerSample = data.frame(data.table(AACDR3)[, list(total=sum(nchar(as.character(.SD$CDR3.Seq)))), by=Sample])
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
948
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
949 AAfreq = list()
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
950
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
951 for(i in 1:nrow(TotalPerSample)){
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
952 sample = TotalPerSample$Sample[i]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
953 AAfreq[[i]] = data.frame(table(unlist(strsplit(as.character(AACDR3[AACDR3$Sample == sample,c("CDR3.Seq")]), ""))))
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
954 AAfreq[[i]]$Sample = sample
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
955 }
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
956
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
957 AAfreq = ldply(AAfreq, data.frame)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
958 AAfreq = merge(AAfreq, TotalPerSample, by="Sample", all.x = T)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
959 AAfreq$freq_perc = as.numeric(AAfreq$Freq / AAfreq$total * 100)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
960
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
961
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
962 AAorder = read.table(sep="\t", header=TRUE, text="order.aa\tAA\n1\tR\n2\tK\n3\tN\n4\tD\n5\tQ\n6\tE\n7\tH\n8\tP\n9\tY\n10\tW\n11\tS\n12\tT\n13\tG\n14\tA\n15\tM\n16\tC\n17\tF\n18\tL\n19\tV\n20\tI")
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
963 AAfreq = merge(AAfreq, AAorder, by.x='Var1', by.y='AA', all.x=TRUE)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
964
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
965 AAfreq = AAfreq[!is.na(AAfreq$order.aa),]
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
966
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
967 AAfreqplot = ggplot(AAfreq)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
968 AAfreqplot = AAfreqplot + geom_bar(aes( x=factor(reorder(Var1, order.aa)), y = freq_perc, fill = Sample), stat='identity', position='dodge' )
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
969 AAfreqplot = AAfreqplot + annotate("rect", xmin = 0.5, xmax = 2.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
970 AAfreqplot = AAfreqplot + annotate("rect", xmin = 3.5, xmax = 4.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
971 AAfreqplot = AAfreqplot + annotate("rect", xmin = 5.5, xmax = 6.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
972 AAfreqplot = AAfreqplot + annotate("rect", xmin = 6.5, xmax = 7.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
973 AAfreqplot = AAfreqplot + ggtitle("Amino Acid Composition in the CDR3") + xlab("Amino Acid, from Hydrophilic (left) to Hydrophobic (right)") + ylab("Percentage") + scale_fill_manual(values=sample.colors)
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
974 AAfreqplot = AAfreqplot + theme(panel.background = element_rect(fill = "white", colour="black"),text = element_text(size=15, colour="black"), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
975
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
976 png("AAComposition.png",width = 1280, height = 720)
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
977 AAfreqplot
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
978 dev.off()
32
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
979
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
980 ggsave("AAComposition.pdf", AAfreqplot, width=12, height=7)
7c33029fd63d Uploaded
davidvanzessen
parents: 30
diff changeset
981
18
5d11c9139a55 Uploaded
davidvanzessen
parents: 17
diff changeset
982 write.table(AAfreq, "AAComposition.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=T)
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
983
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
984 # ---------------------- AA median CDR3 length ----------------------
5
bcec7bb4e089 Uploaded
davidvanzessen
parents:
diff changeset
985
24
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
986 median.aa.l = data.frame(data.table(PRODF)[, list(median=as.double(median(as.numeric(.SD$CDR3.Length, na.rm=T), na.rm=T))), by=c("Sample")])
d5d203d38c8a Uploaded
davidvanzessen
parents: 20
diff changeset
987 write.table(median.aa.l, "AAMedianBySample.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=F)
8
8cbc1a8d27ae Uploaded
davidvanzessen
parents: 6
diff changeset
988
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
989
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
990 #generate the "Sequences that are present in more than one replicate" dataset
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
991 clonaltype.in.replicates = inputdata
38
b6936fb52ab9 Uploaded
davidvanzessen
parents: 37
diff changeset
992 clonaltype.in.replicates = clonaltype.in.replicates[clonaltype.in.replicates$Functionality %in% c("productive (see comment)","productive"),]
37
f37e072affc0 Uploaded
davidvanzessen
parents: 32
diff changeset
993 clonaltype.in.replicates = na.omit(clonaltype.in.replicates)
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
994 clonaltype = unlist(strsplit(clonaltype, ","))
37
f37e072affc0 Uploaded
davidvanzessen
parents: 32
diff changeset
995
f37e072affc0 Uploaded
davidvanzessen
parents: 32
diff changeset
996 clonaltype.in.replicates$clonaltype = do.call(paste, c(clonaltype.in.replicates[c(clonaltype, "Replicate")], sep = ":"))
f37e072affc0 Uploaded
davidvanzessen
parents: 32
diff changeset
997
f37e072affc0 Uploaded
davidvanzessen
parents: 32
diff changeset
998 clonaltype.in.replicates = clonaltype.in.replicates[!duplicated(clonaltype.in.replicates$clonaltype),]
f37e072affc0 Uploaded
davidvanzessen
parents: 32
diff changeset
999
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1000 clonaltype = clonaltype[-which(clonaltype == "Sample")]
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1001
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1002 clonaltype.in.replicates$clonaltype = do.call(paste, c(clonaltype.in.replicates[clonaltype], sep = ":"))
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1003 clonaltype.in.replicates = clonaltype.in.replicates[,c("clonaltype","Replicate", "ID", "Sequence", "Sample")]
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1004
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1005 clonaltype.counts = data.frame(table(clonaltype.in.replicates$clonaltype))
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1006 names(clonaltype.counts) = c("clonaltype", "coincidence")
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1007
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1008 clonaltype.counts = clonaltype.counts[clonaltype.counts$coincidence > 1,]
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1009
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1010 clonaltype.in.replicates = clonaltype.in.replicates[clonaltype.in.replicates$clonaltype %in% clonaltype.counts$clonaltype,]
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1011 clonaltype.in.replicates = merge(clonaltype.in.replicates, clonaltype.counts, by="clonaltype")
37
f37e072affc0 Uploaded
davidvanzessen
parents: 32
diff changeset
1012 clonaltype.in.replicates = clonaltype.in.replicates[order(-clonaltype.in.replicates$coincidence, clonaltype.in.replicates$clonaltype, clonaltype.in.replicates$Replicate),c("coincidence","clonaltype", "Sample", "Replicate", "ID", "Sequence")]
f37e072affc0 Uploaded
davidvanzessen
parents: 32
diff changeset
1013
25
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1014
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1015 write.table(clonaltype.in.replicates, "clonaltypes_replicates.txt" , sep="\t",quote=F,na="-",row.names=F,col.names=T)
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1016
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1017
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1018
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1019
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1020
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1021
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1022
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1023
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1024
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1025
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1026
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1027
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1028
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1029
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1030
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1031
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1032
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1033
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1034
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1035
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1036
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1037
94765af0db1f Uploaded
davidvanzessen
parents: 24
diff changeset
1038