annotate report_clonality/RScript.r.old @ 27:b539aeb75980 draft

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