changeset 7:a9053212a462 draft

Uploaded
author davidvanzessen
date Mon, 05 Jan 2015 09:30:08 -0500
parents 8b46fca04595
children 043fd6613fd9
files RScript.r RScript_b.r RScript_t.r asc.gif bg.gif complete.sh complete_immunerepertoire.xml desc.gif imgt_loader.py jquery.tablesorter.min.js r_wrapper.sh r_wrapper_b.sh r_wrapper_t.sh style.css
diffstat 14 files changed, 754 insertions(+), 1337 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RScript.r	Mon Jan 05 09:30:08 2015 -0500
@@ -0,0 +1,580 @@
+# ---------------------- load/install packages ----------------------
+
+if (!("gridExtra" %in% rownames(installed.packages()))) {
+  install.packages("gridExtra", repos="http://cran.xl-mirror.nl/") 
+}
+library(gridExtra)
+if (!("ggplot2" %in% rownames(installed.packages()))) {
+  install.packages("ggplot2", repos="http://cran.xl-mirror.nl/") 
+}
+library(ggplot2)
+if (!("plyr" %in% rownames(installed.packages()))) {
+  install.packages("plyr", repos="http://cran.xl-mirror.nl/") 
+}			
+library(plyr)
+
+if (!("data.table" %in% rownames(installed.packages()))) {
+  install.packages("data.table", repos="http://cran.xl-mirror.nl/") 
+}
+library(data.table)
+
+if (!("reshape2" %in% rownames(installed.packages()))) {
+  install.packages("reshape2", repos="http://cran.xl-mirror.nl/") 
+}
+library(reshape2)
+
+# ---------------------- parameters ----------------------
+
+args <- commandArgs(trailingOnly = TRUE)
+
+infile = args[1] #path to input file
+outfile = args[2] #path to output file
+outdir = args[3] #path to output folder (html/images/data)
+clonaltype = args[4] #clonaltype definition, or 'none' for no unique filtering
+species = args[5] #human or mouse
+locus = args[6] # IGH, IGK, IGL, TRB, TRA, TRG or TRD
+filterproductive = ifelse(args[7] == "yes", T, F) #should unproductive sequences be filtered out? (yes/no)
+
+# ---------------------- Data preperation ----------------------
+
+inputdata = read.table(infile, sep="\t", header=TRUE, fill=T, comment.char="")
+
+setwd(outdir)
+
+# remove weird rows
+inputdata = inputdata[inputdata$Sample != "",] 
+
+#remove the allele from the V,D and J genes
+inputdata$Top.V.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.V.Gene)
+inputdata$Top.D.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.D.Gene)
+inputdata$Top.J.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.J.Gene)
+inputdata$clonaltype = 1:nrow(inputdata)
+PRODF = inputdata
+if(filterproductive){
+  if("Functionality" %in% colnames(inputdata)) { # "Functionality" is an IMGT column
+    PRODF = inputdata[inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)", ]
+  } else {
+    PRODF = inputdata[inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" , ]
+  }
+}
+
+#remove duplicates based on the clonaltype
+if(clonaltype != "none"){
+  PRODF$clonaltype = do.call(paste, c(PRODF[unlist(strsplit(clonaltype, ","))], sep = ":"))
+  PRODF = PRODF[!duplicated(PRODF$clonaltype), ]
+}
+
+PRODF$freq = 1
+
+if(any(grepl(pattern="_", x=PRODF$ID))){ #the frequency can be stored in the ID with the pattern ".*_freq_.*"
+  PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
+  PRODF$freq = gsub("_.*", "", PRODF$freq)
+  PRODF$freq = as.numeric(PRODF$freq)
+  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
+    PRODF$freq = 1
+  }
+}
+
+
+
+#write the complete dataset that is left over, will be the input if 'none' for clonaltype and 'no' for filterproductive
+write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
+
+#write the samples to a file
+sampleFile <- file("samples.txt")
+un = unique(inputdata$Sample)
+un = paste(un, sep="\n")
+writeLines(un, sampleFile)
+close(sampleFile)
+
+# ---------------------- Counting the productive/unproductive and unique sequences ----------------------
+
+inputdata.dt = data.table(inputdata) #for speed
+
+ct = unlist(strsplit(clonaltype, ","))
+if(clonaltype == "none"){
+	ct = c("ID")
+}
+
+inputdata.dt$samples_replicates = paste(inputdata.dt$Sample, inputdata.dt$Replicate, sep="_")
+samples_replicates = c(unique(inputdata.dt$samples_replicates), unique(as.character(inputdata.dt$Sample)))
+frequency_table = data.frame(ID = samples_replicates[order(samples_replicates)])
+
+
+sample_productive_count = inputdata.dt[, list(All=.N, 
+                                              Productive = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",]), 
+                                              perc_prod = 1,
+                                              Productive_unique = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",list(count=.N),by=ct]), 
+                                              perc_prod_un = 1,
+                                              Unproductive= nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",]),
+                                              perc_unprod = 1,
+                                              Unproductive_unique =nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",list(count=.N),by=ct]),
+                                              perc_unprod_un = 1),
+                                       by=c("Sample")]
+
+sample_productive_count$perc_prod = round(sample_productive_count$Productive / sample_productive_count$All * 100)
+sample_productive_count$perc_prod_un = round(sample_productive_count$Productive_unique / sample_productive_count$All * 100)
+
+sample_productive_count$perc_unprod = round(sample_productive_count$Unproductive / sample_productive_count$All * 100)
+sample_productive_count$perc_unprod_un = round(sample_productive_count$Unproductive_unique / sample_productive_count$All * 100)
+
+
+sample_replicate_productive_count = inputdata.dt[, list(All=.N, 
+                                                        Productive = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",]), 
+                                                        perc_prod = 1,
+                                                        Productive_unique = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",list(count=.N),by=ct]), 
+                                                        perc_prod_un = 1,
+                                                        Unproductive= nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",]),
+                                                        perc_unprod = 1,
+                                                        Unproductive_unique =nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",list(count=.N),by=ct]),
+                                                        perc_unprod_un = 1),
+                                                 by=c("samples_replicates")]
+
+sample_replicate_productive_count$perc_prod = round(sample_replicate_productive_count$Productive / sample_replicate_productive_count$All * 100)
+sample_replicate_productive_count$perc_prod_un = round(sample_replicate_productive_count$Productive_unique / sample_replicate_productive_count$All * 100)
+
+sample_replicate_productive_count$perc_unprod = round(sample_replicate_productive_count$Unproductive / sample_replicate_productive_count$All * 100)
+sample_replicate_productive_count$perc_unprod_un = round(sample_replicate_productive_count$Unproductive_unique / sample_replicate_productive_count$All * 100)
+
+setnames(sample_replicate_productive_count, colnames(sample_productive_count))
+
+counts = rbind(sample_replicate_productive_count, sample_productive_count)
+counts = counts[order(counts$Sample),]
+
+write.table(x=counts, file="productive_counting.txt", sep=",",quote=F,row.names=F,col.names=F)
+
+# ---------------------- Frequency calculation for V, D and J ----------------------
+
+PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
+Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
+PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
+PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
+
+PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
+Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
+PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
+PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
+
+PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
+Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
+PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
+PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
+
+# ---------------------- Setting up the gene names for the different T/B, human/mouse and locus ----------------------
+
+V = c("v.name\tchr.orderV\n")
+D = c("v.name\tchr.orderD\n")  
+J = c("v.name\tchr.orderJ\n")
+
+if(species == "human"){
+  if(locus == "trb"){		
+    V = c("v.name\tchr.orderV\nTRBV2\t1\nTRBV3-1\t2\nTRBV4-1\t3\nTRBV5-1\t4\nTRBV6-1\t5\nTRBV4-2\t6\nTRBV6-2\t7\nTRBV4-3\t8\nTRBV6-3\t9\nTRBV7-2\t10\nTRBV6-4\t11\nTRBV7-3\t12\nTRBV9\t13\nTRBV10-1\t14\nTRBV11-1\t15\nTRBV10-2\t16\nTRBV11-2\t17\nTRBV6-5\t18\nTRBV7-4\t19\nTRBV5-4\t20\nTRBV6-6\t21\nTRBV5-5\t22\nTRBV7-6\t23\nTRBV5-6\t24\nTRBV6-8\t25\nTRBV7-7\t26\nTRBV6-9\t27\nTRBV7-8\t28\nTRBV5-8\t29\nTRBV7-9\t30\nTRBV13\t31\nTRBV10-3\t32\nTRBV11-3\t33\nTRBV12-3\t34\nTRBV12-4\t35\nTRBV12-5\t36\nTRBV14\t37\nTRBV15\t38\nTRBV16\t39\nTRBV18\t40\nTRBV19\t41\nTRBV20-1\t42\nTRBV24-1\t43\nTRBV25-1\t44\nTRBV27\t45\nTRBV28\t46\nTRBV29-1\t47\nTRBV30\t48")
+    D = c("v.name\tchr.orderD\nTRBD1\t1\nTRBD2\t2\n")	
+    J = c("v.name\tchr.orderJ\nTRBJ1-1\t1\nTRBJ1-2\t2\nTRBJ1-3\t3\nTRBJ1-4\t4\nTRBJ1-5\t5\nTRBJ1-6\t6\nTRBJ2-1\t7\nTRBJ2-2\t8\nTRBJ2-3\t9\nTRBJ2-4\t10\nTRBJ2-5\t11\nTRBJ2-6\t12\nTRBJ2-7\t13")
+  } else if (locus == "tra"){
+    V = c("v.name\tchr.orderVTRAV1-1\t1\nTRAV1-2\t2\nTRAV2\t3\nTRAV3\t4\nTRAV4\t5\nTRAV5\t6\nTRAV6\t7\nTRAV7\t8\nTRAV8-1\t9\nTRAV9-1\t10\nTRAV10\t11\nTRAV12-1\t12\nTRAV8-2\t13\nTRAV8-3\t14\nTRAV13-1\t15\nTRAV12-2\t16\nTRAV8-4\t17\nTRAV13-2\t18\nTRAV14/DV4\t19\nTRAV9-2\t20\nTRAV12-3\t21\nTRAV8-6\t22\nTRAV16\t23\nTRAV17\t24\nTRAV18\t25\nTRAV19\t26\nTRAV20\t27\nTRAV21\t28\nTRAV22\t29\nTRAV23/DV6\t30\nTRAV24\t31\nTRAV25\t32\nTRAV26-1\t33\nTRAV27\t34\nTRAV29/DV5\t35\nTRAV30\t36\nTRAV26-2\t37\nTRAV34\t38\nTRAV35\t39\nTRAV36/DV7\t40\nTRAV38-1\t41\nTRAV38-2/DV8\t42\nTRAV39\t43\nTRAV40\t44\nTRAV41\t45\n")
+    D = c("v.name\tchr.orderD\n")	
+    J = c("v.name\tchr.orderJ\nTRAJ57\t1\nTRAJ56\t2\nTRAJ54\t3\nTRAJ53\t4\nTRAJ52\t5\nTRAJ50\t6\nTRAJ49\t7\nTRAJ48\t8\nTRAJ47\t9\nTRAJ46\t10\nTRAJ45\t11\nTRAJ44\t12\nTRAJ43\t13\nTRAJ42\t14\nTRAJ41\t15\nTRAJ40\t16\nTRAJ39\t17\nTRAJ38\t18\nTRAJ37\t19\nTRAJ36\t20\nTRAJ34\t21\nTRAJ33\t22\nTRAJ32\t23\nTRAJ31\t24\nTRAJ30\t25\nTRAJ29\t26\nTRAJ28\t27\nTRAJ27\t28\nTRAJ26\t29\nTRAJ24\t30\nTRAJ23\t31\nTRAJ22\t32\nTRAJ21\t33\nTRAJ20\t34\nTRAJ18\t35\nTRAJ17\t36\nTRAJ16\t37\nTRAJ15\t38\nTRAJ14\t39\nTRAJ13\t40\nTRAJ12\t41\nTRAJ11\t42\nTRAJ10\t43\nTRAJ9\t44\nTRAJ8\t45\nTRAJ7\t46\nTRAJ6\t47\nTRAJ5\t48\nTRAJ4\t49\nTRAJ3\t50")
+  } else if (locus == "trg"){
+    V = c("v.name\tchr.orderV\nTRGV9\t1\nTRGV8\t2\nTRGV5\t3\nTRGV4\t4\nTRGV3\t5\nTRGV2\t6")
+    D = c("v.name\tchr.orderD\n")	
+    J = c("v.name\tchr.orderJ\nTRGJ2\t1\nTRGJP2\t2\nTRGJ1\t3\nTRGJP1\t4")
+  } else if (locus == "trd"){
+    V = c("v.name\tchr.orderV\nTRDV1\t1\nTRDV2\t2\nTRDV3\t3")
+    D = c("v.name\tchr.orderD\nTRDD1\t1\nTRDD2\t2\nTRDD3\t3")	
+    J = c("v.name\tchr.orderJ\nTRDJ1\t1\nTRDJ4\t2\nTRDJ2\t3\nTRDJ3\t4")
+  } else if(locus == "igh"){
+    V = c("v.name\tchr.orderV\nIGHV3-74\t1\nIGHV3-73\t2\nIGHV3-72\t3\nIGHV2-70\t4\nIGHV1-69D\t5\nIGHV1-69-2\t6\nIGHV2-70D\t7\nIGHV1-69\t8\nIGHV3-66\t9\nIGHV3-64\t10\nIGHV4-61\t11\nIGHV4-59\t12\nIGHV1-58\t13\nIGHV3-53\t14\nIGHV5-51\t15\nIGHV3-49\t16\nIGHV3-48\t17\nIGHV1-46\t18\nIGHV1-45\t19\nIGHV3-43\t20\nIGHV4-39\t21\nIGHV3-43D\t22\nIGHV4-38-2\t23\nIGHV4-34\t24\nIGHV3-33\t25\nIGHV4-31\t26\nIGHV3-30-5\t27\nIGHV4-30-4\t28\nIGHV3-30-3\t29\nIGHV4-30-2\t30\nIGHV4-30-1\t31\nIGHV3-30\t32\nIGHV4-28\t33\nIGHV2-26\t34\nIGHV1-24\t35\nIGHV3-23D\t36\nIGHV3-23\t37\nIGHV3-21\t38\nIGHV3-20\t39\nIGHV1-18\t40\nIGHV3-15\t41\nIGHV3-13\t42\nIGHV3-11\t43\nIGHV5-10-1\t44\nIGHV3-9\t45\nIGHV1-8\t46\nIGHV3-64D\t47\nIGHV3-7\t48\nIGHV2-5\t49\nIGHV7-4-1\t50\nIGHV4-4\t51\nIGHV1-3\t52\nIGHV1-2\t53\nIGHV6-1\t54")
+    D = c("v.name\tchr.orderD\nIGHD1-7\t1\nIGHD2-8\t2\nIGHD3-9\t3\nIGHD3-10\t4\nIGHD5-12\t5\nIGHD6-13\t6\nIGHD2-15\t7\nIGHD3-16\t8\nIGHD4-17\t9\nIGHD5-18\t10\nIGHD6-19\t11\nIGHD1-20\t12\nIGHD2-21\t13\nIGHD3-22\t14\nIGHD5-24\t15\nIGHD6-25\t16\nIGHD1-26\t17\nIGHD7-27\t18")
+    J = c("v.name\tchr.orderJ\nIGHJ1\t1\nIGHJ2\t2\nIGHJ3\t3\nIGHJ4\t4\nIGHJ5\t5\nIGHJ6\t6")
+  } else if (locus == "igk"){
+    V = c("v.name\tchr.orderV\nIGKV3D-7\t1\nIGKV1D-8\t2\nIGKV1D-43\t3\nIGKV3D-11\t4\nIGKV1D-12\t5\nIGKV1D-13\t6\nIGKV3D-15\t7\nIGKV1D-16\t8\nIGKV1D-17\t9\nIGKV3D-20\t10\nIGKV2D-26\t11\nIGKV2D-28\t12\nIGKV2D-29\t13\nIGKV2D-30\t14\nIGKV1D-33\t15\nIGKV1D-39\t16\nIGKV2D-40\t17\nIGKV2-40\t18\nIGKV1-39\t19\nIGKV1-33\t20\nIGKV2-30\t21\nIGKV2-29\t22\nIGKV2-28\t23\nIGKV1-27\t24\nIGKV2-24\t25\nIGKV3-20\t26\nIGKV1-17\t27\nIGKV1-16\t28\nIGKV3-15\t29\nIGKV1-13\t30\nIGKV1-12\t31\nIGKV3-11\t32\nIGKV1-9\t33\nIGKV1-8\t34\nIGKV1-6\t35\nIGKV1-5\t36\nIGKV5-2\t37\nIGKV4-1\t38")
+    D = c("v.name\tchr.orderD\n")
+    J = c("v.name\tchr.orderJ\nIGKJ1\t1\nIGKJ2\t2\nIGKJ3\t3\nIGKJ4\t4\nIGKJ5\t5")
+  } else if (locus == "igl"){
+    V = c("v.name\tchr.orderV\nIGLV4-69\t1\nIGLV8-61\t2\nIGLV4-60\t3\nIGLV6-57\t4\nIGLV5-52\t5\nIGLV1-51\t6\nIGLV9-49\t7\nIGLV1-47\t8\nIGLV7-46\t9\nIGLV5-45\t10\nIGLV1-44\t11\nIGLV7-43\t12\nIGLV1-41\t13\nIGLV1-40\t14\nIGLV5-39\t15\nIGLV5-37\t16\nIGLV1-36\t17\nIGLV3-27\t18\nIGLV3-25\t19\nIGLV2-23\t20\nIGLV3-22\t21\nIGLV3-21\t22\nIGLV3-19\t23\nIGLV2-18\t24\nIGLV3-16\t25\nIGLV2-14\t26\nIGLV3-12\t27\nIGLV2-11\t28\nIGLV3-10\t29\nIGLV3-9\t30\nIGLV2-8\t31\nIGLV4-3\t32\nIGLV3-1\t33")
+    D = c("v.name\tchr.orderD\n")
+    J = c("v.name\tchr.orderJ\nIGLJ1\t1\nIGLJ2\t2\nIGLJ3\t3\nIGLJ6\t4\nIGLJ7\t5")
+  }
+} else if (species == "mouse"){
+  if(locus == "trb"){
+    V = c("v.name\tchr.orderV\nTRBV1\t1\nTRBV2\t2\nTRBV3\t3\nTRBV4\t4\nTRBV5\t5\nTRBV12-1\t6\nTRBV13-1\t7\nTRBV12-2\t8\nTRBV13-2\t9\nTRBV13-3\t10\nTRBV14\t11\nTRBV15\t12\nTRBV16\t13\nTRBV17\t14\nTRBV19\t15\nTRBV20\t16\nTRBV23\t17\nTRBV24\t18\nTRBV26\t19\nTRBV29\t20\nTRBV30\t21\nTRBV31\t22")
+    D = c("v.name\tchr.orderD\nTRBD1\t1\nTRBD2\t2")
+    J = c("v.name\tchr.orderJ\nTRBJ1-1\t1\nTRBJ1-2\t2\nTRBJ1-3\t3\nTRBJ1-4\t4\nTRBJ1-5\t5\nTRBJ2-1\t6\nTRBJ2-2\t7\nTRBJ2-3\t8\nTRBJ2-4\t9\nTRBJ2-5\t10\nTRBJ2-6\t11\nTRBJ2-7\t12")
+  } else if (locus == "tra"){
+    cat("mouse tra not yet implemented")
+  } else if (locus == "trg"){
+    cat("mouse trg not yet implemented")
+  } else if (locus == "trd"){
+    cat("mouse trd not yet implemented")
+  } else if(locus == "igh"){
+    cat("mouse igh not yet implemented")
+  } else if (locus == "igk"){
+    cat("mouse igk not yet implemented")
+  } else if (locus == "igl"){
+    cat("mouse igl not yet implemented")
+  }
+}
+
+useD = TRUE
+if(species == "human" && locus == "tra"){
+  useD = FALSE
+  cat("No D Genes in this species/locus")
+}
+
+# ---------------------- load the gene names into a data.frame and merge with the frequency count ----------------------
+
+tcV = textConnection(V)
+Vchain = read.table(tcV, sep="\t", header=TRUE)
+PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
+close(tcV)
+
+tcD = textConnection(D)
+Dchain = read.table(tcD, sep="\t", header=TRUE)
+PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
+close(tcD)
+
+tcJ = textConnection(J)
+Jchain = read.table(tcJ, sep="\t", header=TRUE)
+PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
+close(tcJ)
+
+# ---------------------- Create the V, D and J frequency plots and write the data.frame for every plot to a file ----------------------
+
+pV = ggplot(PRODFV)
+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))
+pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
+write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
+
+png("VPlot.png",width = 1280, height = 720)
+pV
+dev.off();
+
+if(useD){
+  pD = ggplot(PRODFD)
+  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))
+  pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
+  write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
+  
+  png("DPlot.png",width = 800, height = 600)
+  print(pD)
+  dev.off();
+}
+
+pJ = ggplot(PRODFJ)
+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))
+pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
+write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
+
+png("JPlot.png",width = 800, height = 600)
+pJ
+dev.off();
+
+pJ = ggplot(PRODFJ)
+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))
+pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
+write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
+
+png("JPlot.png",width = 800, height = 600)
+pJ
+dev.off();
+
+# ---------------------- Now the frequency plots of the V, D and J families ----------------------
+
+VGenes = PRODF[,c("Sample", "Top.V.Gene")]
+VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
+VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
+TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
+VGenes = merge(VGenes, TotalPerSample, by="Sample")
+VGenes$Frequency = VGenes$Count * 100 / VGenes$total
+VPlot = ggplot(VGenes)
+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)) + 
+  ggtitle("Distribution of V gene families") + 
+  ylab("Percentage of sequences")
+png("VFPlot.png")
+VPlot
+dev.off();
+write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
+
+if(useD){
+  DGenes = PRODF[,c("Sample", "Top.D.Gene")]
+  DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
+  DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
+  TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
+  DGenes = merge(DGenes, TotalPerSample, by="Sample")
+  DGenes$Frequency = DGenes$Count * 100 / DGenes$total
+  DPlot = ggplot(DGenes)
+  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)) + 
+    ggtitle("Distribution of D gene families") + 
+    ylab("Percentage of sequences")
+  png("DFPlot.png")
+  print(DPlot)
+  dev.off();
+  write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
+}
+
+JGenes = PRODF[,c("Sample", "Top.J.Gene")]
+JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
+JGenes = data.frame(data.table(JGenes)[, list(Count=.N), by=c("Sample", "Top.J.Gene")])
+TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
+JGenes = merge(JGenes, TotalPerSample, by="Sample")
+JGenes$Frequency = JGenes$Count * 100 / JGenes$total
+JPlot = ggplot(JGenes)
+JPlot = JPlot + geom_bar(aes( x = Top.J.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
+  ggtitle("Distribution of J gene families") + 
+  ylab("Percentage of sequences")
+png("JFPlot.png")
+JPlot
+dev.off();
+write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
+
+# ---------------------- Plotting the cdr3 length ----------------------
+
+CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length.DNA")])
+TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
+CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
+CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
+CDR3LengthPlot = ggplot(CDR3Length)
+CDR3LengthPlot = CDR3LengthPlot + geom_bar(aes( x = CDR3.Length.DNA, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
+  ggtitle("Length distribution of CDR3") + 
+  xlab("CDR3 Length") + 
+  ylab("Percentage of sequences")
+png("CDR3LengthPlot.png",width = 1280, height = 720)
+CDR3LengthPlot
+dev.off()
+write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
+
+# ---------------------- Plot the heatmaps ----------------------
+
+
+#get the reverse order for the V and D genes
+revVchain = Vchain
+revDchain = Dchain
+revVchain$chr.orderV = rev(revVchain$chr.orderV)
+revDchain$chr.orderD = rev(revDchain$chr.orderD)
+
+if(useD){
+  plotVD <- function(dat){
+    if(length(dat[,1]) == 0){
+      return()
+    }
+    img = ggplot() + 
+      geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) + 
+      theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
+      scale_fill_gradient(low="gold", high="blue", na.value="white") + 
+      ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
+      xlab("D genes") + 
+      ylab("V Genes")
+    
+    png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
+    print(img)
+    dev.off()
+    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)
+  }
+  
+  VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
+  
+  VandDCount$l = log(VandDCount$Length)
+  maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
+  VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
+  VandDCount$relLength = VandDCount$l / VandDCount$max
+  
+  cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(inputdata$Sample))
+  
+  completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
+  completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
+  completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
+  VDList = split(completeVD, f=completeVD[,"Sample"])
+  
+  lapply(VDList, FUN=plotVD)
+}
+
+plotVJ <- function(dat){
+  if(length(dat[,1]) == 0){
+    return()
+  }
+  cat(paste(unique(dat[3])[1,1]))
+  img = ggplot() + 
+    geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) + 
+    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
+    scale_fill_gradient(low="gold", high="blue", na.value="white") + 
+    ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
+    xlab("J genes") + 
+    ylab("V Genes")
+  
+  png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
+  print(img)
+  dev.off()
+  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)
+}
+
+VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
+
+VandJCount$l = log(VandJCount$Length)
+maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
+VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
+VandJCount$relLength = VandJCount$l / VandJCount$max
+
+cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(inputdata$Sample))
+
+completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
+completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
+completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
+VJList = split(completeVJ, f=completeVJ[,"Sample"])
+lapply(VJList, FUN=plotVJ)
+
+if(useD){
+  plotDJ <- function(dat){
+    if(length(dat[,1]) == 0){
+      return()
+    }
+    img = ggplot() + 
+      geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) + 
+      theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
+      scale_fill_gradient(low="gold", high="blue", na.value="white") + 
+      ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
+      xlab("J genes") + 
+      ylab("D Genes")
+    
+    png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
+    print(img)
+    dev.off()
+    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)
+  }
+  
+  
+  DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
+  
+  DandJCount$l = log(DandJCount$Length)
+  maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
+  DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
+  DandJCount$relLength = DandJCount$l / DandJCount$max
+  
+  cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(inputdata$Sample))
+  
+  completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
+  completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
+  completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
+  DJList = split(completeDJ, f=completeDJ[,"Sample"])
+  lapply(DJList, FUN=plotDJ)
+}
+
+
+# ---------------------- calculating the clonality score ----------------------
+
+if("Replicate" %in% colnames(inputdata)) #can only calculate clonality score when replicate information is available
+{
+  clonalityFrame = inputdata
+  if(clonaltype != "none"){
+    clonalityFrame$ReplicateConcat = paste(clonalityFrame$clonaltype, clonalityFrame$Sample, clonalityFrame$Replicate, sep = ":")
+    clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$ReplicateConcat), ]
+  }
+  write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
+  
+  ClonalitySampleReplicatePrint <- function(dat){
+    write.table(dat, paste("clonality_", unique(inputdata$Sample) , "_", unique(dat$Replicate), ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
+  }
+  
+  clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,c("Sample", "Replicate")])
+  #lapply(clonalityFrameSplit, FUN=ClonalitySampleReplicatePrint)
+  
+  ClonalitySamplePrint <- function(dat){
+    write.table(dat, paste("clonality_", unique(inputdata$Sample) , ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
+  }
+  
+  clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,"Sample"])
+  #lapply(clonalityFrameSplit, FUN=ClonalitySamplePrint)
+  
+  clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "clonaltype")])
+  clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
+  clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
+  clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
+  clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
+  
+  ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
+  tcct = textConnection(ct)
+  CT  = read.table(tcct, sep="\t", header=TRUE)
+  close(tcct)
+  clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
+  clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
+  
+  ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "clonaltype")])
+  ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
+  clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
+  ReplicateReads$squared = ReplicateReads$Reads * ReplicateReads$Reads
+  
+  ReplicatePrint <- function(dat){
+    write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
+  }
+  
+  ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
+  lapply(ReplicateSplit, FUN=ReplicatePrint)
+  
+  ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(Reads), ReadsSquaredSum=sum(squared)), by=c("Sample")])
+  clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
+  
+  
+  ReplicateSumPrint <- function(dat){
+    write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
+  }
+  
+  ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
+  lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
+  
+  clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
+  clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
+  clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
+  clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
+  clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
+  
+  ClonalityScorePrint <- function(dat){
+    write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
+  }
+  
+  clonalityScore = clonalFreqCount[c("Sample", "Result")]
+  clonalityScore = unique(clonalityScore)
+  
+  clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
+  lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
+  
+  clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
+  
+  
+  
+  ClonalityOverviewPrint <- function(dat){
+    write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
+  }
+  
+  clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
+  lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
+}
+
+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")
+if(all(imgtcolumns %in% colnames(inputdata)))
+{
+  newData = data.frame(data.table(inputdata)[,list(unique=.N, 
+                                              VH.DEL=mean(X3V.REGION.trimmed.nt.nb, na.rm=T),
+                                              P1=mean(P3V.nt.nb, na.rm=T),
+                                              N1=mean(N1.REGION.nt.nb, na.rm=T),
+                                              P2=mean(P5D.nt.nb, na.rm=T),
+                                              DEL.DH=mean(X5D.REGION.trimmed.nt.nb, na.rm=T),
+                                              DH.DEL=mean(X3D.REGION.trimmed.nt.nb, na.rm=T),
+                                              P3=mean(P3D.nt.nb, na.rm=T),
+                                              N2=mean(N2.REGION.nt.nb, na.rm=T),
+                                              P4=mean(P5J.nt.nb, na.rm=T),
+                                              DEL.JH=mean(X5J.REGION.trimmed.nt.nb, na.rm=T),
+                                              Total.Del=(	mean(X3V.REGION.trimmed.nt.nb, na.rm=T) + 
+                                                            mean(X5D.REGION.trimmed.nt.nb, na.rm=T) + 
+                                                            mean(X3D.REGION.trimmed.nt.nb, na.rm=T) +
+                                                            mean(X5J.REGION.trimmed.nt.nb, na.rm=T)),
+                                              
+                                              Total.N=(	mean(N1.REGION.nt.nb, na.rm=T) +
+                                                          mean(N2.REGION.nt.nb, na.rm=T)),
+                                              
+                                              Total.P=(	mean(P3V.nt.nb, na.rm=T) +
+                                                          mean(P5D.nt.nb, na.rm=T) +
+                                                          mean(P3D.nt.nb, na.rm=T) +
+                                                          mean(P5J.nt.nb, na.rm=T))),
+                                        by=c("Sample")])
+  write.table(newData, "junctionAnalysis.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
+}
--- a/RScript_b.r	Mon Sep 08 04:24:04 2014 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,462 +0,0 @@
-#options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
-
-args <- commandArgs(trailingOnly = TRUE)
-
-inFile = args[1]
-outFile = args[2]
-outDir = args[3]
-clonalType = args[4]
-species = args[5]
-locus = args[6]
-selection = args[7]
-
-
-
-if (!("gridExtra" %in% rownames(installed.packages()))) {
-	install.packages("gridExtra", repos="http://cran.xl-mirror.nl/") 
-}
-library(gridExtra)
-if (!("ggplot2" %in% rownames(installed.packages()))) {
-	install.packages("ggplot2", repos="http://cran.xl-mirror.nl/") 
-}
-library(ggplot2)
-if (!("plyr" %in% rownames(installed.packages()))) {
-	install.packages("plyr", repos="http://cran.xl-mirror.nl/") 
-}			
-library(plyr)
-
-if (!("data.table" %in% rownames(installed.packages()))) {
-	install.packages("data.table", repos="http://cran.xl-mirror.nl/") 
-}
-library(data.table)
-
-if (!("reshape2" %in% rownames(installed.packages()))) {
-	install.packages("reshape2", repos="http://cran.xl-mirror.nl/") 
-}
-library(reshape2)
-
-
-test = read.table(inFile, sep="\t", header=TRUE, fill=T, comment.char="")
-
-test = test[test$Sample != "",]
-
-test$Top.V.Gene = gsub("[*]([0-9]+)", "", test$Top.V.Gene)
-test$Top.D.Gene = gsub("[*]([0-9]+)", "", test$Top.D.Gene)
-test$Top.J.Gene = gsub("[*]([0-9]+)", "", test$Top.J.Gene)
-
-#test$VDJCDR3 = do.call(paste, c(test[c("Top.V.Gene", "Top.D.Gene", "Top.J.Gene","CDR3.Seq.DNA")], sep = ":"))
-test$VDJCDR3 = do.call(paste, c(test[unlist(strsplit(clonalType, ","))], sep = ":"))
-
-PROD = test[test$VDJ.Frame != "In-frame with stop codon" & test$VDJ.Frame != "Out-of-frame" & test$CDR3.Found.How != "NOT_FOUND" , ]
-if("Functionality" %in% colnames(test)) {
-	PROD = test[test$Functionality == "productive" | test$Functionality == "productive (see comment)", ]
-}
-
-NONPROD = test[test$VDJ.Frame == "In-frame with stop codon" | test$VDJ.Frame == "Out-of-frame" | test$CDR3.Found.How == "NOT_FOUND" , ]
-
-#PRODF = PROD[ -1]
-
-PRODF = PROD
-
-#PRODF = unique(PRODF)
-
-
-
-if(selection == "unique"){
-	PRODF = PRODF[!duplicated(PRODF$VDJCDR3), ]
-}
-
-PRODFV = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.V.Gene")])
-PRODFV$Length = as.numeric(PRODFV$Length)
-Total = 0
-Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
-PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
-PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
-
-PRODFD = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.D.Gene")])
-PRODFD$Length = as.numeric(PRODFD$Length)
-Total = 0
-Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
-PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
-PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
-
-PRODFJ = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Sample", "Top.J.Gene")])
-PRODFJ$Length = as.numeric(PRODFJ$Length)
-Total = 0
-Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
-PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
-PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
-
-V = c("v.name\tchr.orderV")
-D = c("v.name\tchr.orderD")
-J = c("v.name\tchr.orderJ")
-
-if(species == "human"){
-	if(locus == "igh"){		
-		V = c("v.name\tchr.orderV\nIGHV3-74\t1\nIGHV3-73\t2\nIGHV3-72\t3\nIGHV2-70\t4\nIGHV1-69D\t5\nIGHV1-69-2\t6\nIGHV2-70D\t7\nIGHV1-69\t8\nIGHV3-66\t9\nIGHV3-64\t10\nIGHV4-61\t11\nIGHV4-59\t12\nIGHV1-58\t13\nIGHV3-53\t14\nIGHV5-51\t15\nIGHV3-49\t16\nIGHV3-48\t17\nIGHV1-46\t18\nIGHV1-45\t19\nIGHV3-43\t20\nIGHV4-39\t21\nIGHV3-43D\t22\nIGHV4-38-2\t23\nIGHV4-34\t24\nIGHV3-33\t25\nIGHV4-31\t26\nIGHV3-30-5\t27\nIGHV4-30-4\t28\nIGHV3-30-3\t29\nIGHV4-30-2\t30\nIGHV4-30-1\t31\nIGHV3-30\t32\nIGHV4-28\t33\nIGHV2-26\t34\nIGHV1-24\t35\nIGHV3-23D\t36\nIGHV3-23\t37\nIGHV3-21\t38\nIGHV3-20\t39\nIGHV1-18\t40\nIGHV3-15\t41\nIGHV3-13\t42\nIGHV3-11\t43\nIGHV5-10-1\t44\nIGHV3-9\t45\nIGHV1-8\t46\nIGHV3-64D\t47\nIGHV3-7\t48\nIGHV2-5\t49\nIGHV7-4-1\t50\nIGHV4-4\t51\nIGHV1-3\t52\nIGHV1-2\t53\nIGHV6-1\t54")
-		D = c("v.name\tchr.orderD\nIGHD1-7\t1\nIGHD2-8\t2\nIGHD3-9\t3\nIGHD3-10\t4\nIGHD5-12\t5\nIGHD6-13\t6\nIGHD2-15\t7\nIGHD3-16\t8\nIGHD4-17\t9\nIGHD5-18\t10\nIGHD6-19\t11\nIGHD1-20\t12\nIGHD2-21\t13\nIGHD3-22\t14\nIGHD5-24\t15\nIGHD6-25\t16\nIGHD1-26\t17\nIGHD7-27\t18")
-		J = c("v.name\tchr.orderJ\nIGHJ1\t1\nIGHJ2\t2\nIGHJ3\t3\nIGHJ4\t4\nIGHJ5\t5\nIGHJ6\t6")
-	} else if (locus == "igk"){
-		V = c("v.name\tchr.orderV\nIGKV3D-7\t1\nIGKV1D-8\t2\nIGKV1D-43\t3\nIGKV3D-11\t4\nIGKV1D-12\t5\nIGKV1D-13\t6\nIGKV3D-15\t7\nIGKV1D-16\t8\nIGKV1D-17\t9\nIGKV3D-20\t10\nIGKV2D-26\t11\nIGKV2D-28\t12\nIGKV2D-29\t13\nIGKV2D-30\t14\nIGKV1D-33\t15\nIGKV1D-39\t16\nIGKV2D-40\t17\nIGKV2-40\t18\nIGKV1-39\t19\nIGKV1-33\t20\nIGKV2-30\t21\nIGKV2-29\t22\nIGKV2-28\t23\nIGKV1-27\t24\nIGKV2-24\t25\nIGKV3-20\t26\nIGKV1-17\t27\nIGKV1-16\t28\nIGKV3-15\t29\nIGKV1-13\t30\nIGKV1-12\t31\nIGKV3-11\t32\nIGKV1-9\t33\nIGKV1-8\t34\nIGKV1-6\t35\nIGKV1-5\t36\nIGKV5-2\t37\nIGKV4-1\t38")
-		D = c("v.name\tchr.orderD\n")
-		J = c("v.name\tchr.orderJ\nIGKJ1\t1\nIGKJ2\t2\nIGKJ3\t3\nIGKJ4\t4\nIGKJ5\t5")
-	} else if (locus == "igl"){
-		V = c("v.name\tchr.orderV\nIGLV4-69\t1\nIGLV8-61\t2\nIGLV4-60\t3\nIGLV6-57\t4\nIGLV5-52\t5\nIGLV1-51\t6\nIGLV9-49\t7\nIGLV1-47\t8\nIGLV7-46\t9\nIGLV5-45\t10\nIGLV1-44\t11\nIGLV7-43\t12\nIGLV1-41\t13\nIGLV1-40\t14\nIGLV5-39\t15\nIGLV5-37\t16\nIGLV1-36\t17\nIGLV3-27\t18\nIGLV3-25\t19\nIGLV2-23\t20\nIGLV3-22\t21\nIGLV3-21\t22\nIGLV3-19\t23\nIGLV2-18\t24\nIGLV3-16\t25\nIGLV2-14\t26\nIGLV3-12\t27\nIGLV2-11\t28\nIGLV3-10\t29\nIGLV3-9\t30\nIGLV2-8\t31\nIGLV4-3\t32\nIGLV3-1\t33")
-		D = c("v.name\tchr.orderD\n")
-		J = c("v.name\tchr.orderJ\nIGLJ1\t1\nIGLJ2\t2\nIGLJ3\t3\nIGLJ6\t4\nIGLJ7\t5")
-	}
-} else if (species == "mouse"){
-	if(locus == "igh"){
-		cat("mouse igh not yet implemented")
-	} else if (locus == "igk"){
-		cat("mouse igk not yet implemented")
-	} else if (locus == "igl"){
-		cat("mouse igl not yet implemented")
-	}
-}
-
-useD = TRUE
-if(species == "human" && (locus == "igk" || locus == "igl")){
-	useD = FALSE
-}
-
-tcV = textConnection(V)
-Vchain = read.table(tcV, sep="\t", header=TRUE)
-PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
-close(tcV)
-
-tcD = textConnection(D)
-Dchain = read.table(tcD, sep="\t", header=TRUE)
-PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
-close(tcD)
-
-tcJ = textConnection(J)
-Jchain = read.table(tcJ, sep="\t", header=TRUE)
-PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
-close(tcJ)
-
-setwd(outDir)
-
-write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-pV = ggplot(PRODFV)
-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))
-pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
-write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-png("VPlot.png",width = 1280, height = 720)
-pV
-dev.off();
-
-if(useD){
-	pD = ggplot(PRODFD)
-	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))
-	pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
-	write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-	png("DPlot.png",width = 800, height = 600)
-	print(pD)
-	dev.off();
-}
-
-pJ = ggplot(PRODFJ)
-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))
-pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
-write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-png("JPlot.png",width = 800, height = 600)
-pJ
-dev.off();
-
-VGenes = PRODF[,c("Sample", "Top.V.Gene")]
-VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
-VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
-TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
-VGenes = merge(VGenes, TotalPerSample, by="Sample")
-VGenes$Frequency = VGenes$Count * 100 / VGenes$total
-VPlot = ggplot(VGenes)
-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)) + 
-				ggtitle("Distribution of V gene families") + 
-				ylab("Percentage of sequences")
-png("VFPlot.png")
-VPlot
-dev.off();
-write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-if(useD){
-	DGenes = PRODF[,c("Sample", "Top.D.Gene")]
-	DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
-	DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
-	TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
-	DGenes = merge(DGenes, TotalPerSample, by="Sample")
-	DGenes$Frequency = DGenes$Count * 100 / DGenes$total
-	DPlot = ggplot(DGenes)
-	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)) + 
-					ggtitle("Distribution of D gene families") + 
-					ylab("Percentage of sequences")
-	png("DFPlot.png")
-	print(DPlot)
-	dev.off();
-	write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-}
-
-JGenes = PRODF[,c("Sample", "Top.J.Gene")]
-JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
-JGenes = data.frame(data.table(JGenes)[, list(Count=.N), by=c("Sample", "Top.J.Gene")])
-TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
-JGenes = merge(JGenes, TotalPerSample, by="Sample")
-JGenes$Frequency = JGenes$Count * 100 / JGenes$total
-JPlot = ggplot(JGenes)
-JPlot = JPlot + geom_bar(aes( x = Top.J.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-				ggtitle("Distribution of J gene families") + 
-				ylab("Percentage of sequences")
-png("JFPlot.png")
-JPlot
-dev.off();
-write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length.DNA")])
-TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
-CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
-CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
-CDR3LengthPlot = ggplot(CDR3Length)
-CDR3LengthPlot = CDR3LengthPlot + geom_bar(aes( x = CDR3.Length.DNA, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-				ggtitle("Length distribution of CDR3") + 
-				xlab("CDR3 Length") + 
-				ylab("Percentage of sequences")
-png("CDR3LengthPlot.png",width = 1280, height = 720)
-CDR3LengthPlot
-dev.off()
-write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-revVchain = Vchain
-revDchain = Dchain
-revVchain$chr.orderV = rev(revVchain$chr.orderV)
-revDchain$chr.orderD = rev(revDchain$chr.orderD)
-
-if(useD){
-	plotVD <- function(dat){
-		if(length(dat[,1]) == 0){
-			return()
-		}
-		img = ggplot() + 
-		geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) + 
-		theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-		scale_fill_gradient(low="gold", high="blue", na.value="white") + 
-		ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
-		xlab("D genes") + 
-		ylab("V Genes")
-		
-		png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
-		print(img)
-		dev.off()
-		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)
-	}
-
-	VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
-
-	VandDCount$l = log(VandDCount$Length)
-	maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
-	VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
-	VandDCount$relLength = VandDCount$l / VandDCount$max
-
-	cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(test$Sample))
-
-	completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
-	completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
-	completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
-	VDList = split(completeVD, f=completeVD[,"Sample"])
-
-	lapply(VDList, FUN=plotVD)
-}
-
-plotVJ <- function(dat){
-	if(length(dat[,1]) == 0){
-		return()
-	}
-	cat(paste(unique(dat[3])[1,1]))
-	img = ggplot() + 
-	geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) + 
-	theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-	scale_fill_gradient(low="gold", high="blue", na.value="white") + 
-	ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
-	xlab("J genes") + 
-	ylab("V Genes")
-	
-	png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
-	print(img)
-	dev.off()
-	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)
-}
-
-VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
-
-VandJCount$l = log(VandJCount$Length)
-maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
-VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
-VandJCount$relLength = VandJCount$l / VandJCount$max
-
-cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
-
-completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
-completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
-completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
-VJList = split(completeVJ, f=completeVJ[,"Sample"])
-lapply(VJList, FUN=plotVJ)
-
-if(useD){
-	plotDJ <- function(dat){
-		if(length(dat[,1]) == 0){
-			return()
-		}
-		img = ggplot() + 
-		geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) + 
-		theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-		scale_fill_gradient(low="gold", high="blue", na.value="white") + 
-		ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
-		xlab("J genes") + 
-		ylab("D Genes")
-		
-		png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
-		print(img)
-		dev.off()
-		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)
-	}
-
-
-	DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
-
-	DandJCount$l = log(DandJCount$Length)
-	maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
-	DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
-	DandJCount$relLength = DandJCount$l / DandJCount$max
-
-	cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
-
-	completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
-	completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
-	completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
-	DJList = split(completeDJ, f=completeDJ[,"Sample"])
-	lapply(DJList, FUN=plotDJ)
-}
-
-sampleFile <- file("samples.txt")
-un = unique(test$Sample)
-un = paste(un, sep="\n")
-writeLines(un, sampleFile)
-close(sampleFile)
-
-
-if("Replicate" %in% colnames(test))
-{
-	clonalityFrame = PROD
-	clonalityFrame$ReplicateConcat = do.call(paste, c(clonalityFrame[c("VDJCDR3", "Sample", "Replicate")], sep = ":"))
-	clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$ReplicateConcat), ]
-	write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-	ClonalitySampleReplicatePrint <- function(dat){
-	    write.table(dat, paste("clonality_", unique(dat$Sample) , "_", unique(dat$Replicate), ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
-	}
-
-    clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,c("Sample", "Replicate")])
-    #lapply(clonalityFrameSplit, FUN=ClonalitySampleReplicatePrint)
-
-    ClonalitySamplePrint <- function(dat){
-	    write.table(dat, paste("clonality_", unique(dat$Sample) , ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
-	}
-
-    clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,"Sample"])
-    #lapply(clonalityFrameSplit, FUN=ClonalitySamplePrint)
-
-	clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "VDJCDR3")])
-	clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
-	clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
-	clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
-	clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
-
-	ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
-	tcct = textConnection(ct)
-	CT  = read.table(tcct, sep="\t", header=TRUE)
-	close(tcct)
-	clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
-	clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
-
-	ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "VDJCDR3")])
-	ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
-	clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
-	ReplicateReads$squared = ReplicateReads$Reads * ReplicateReads$Reads
-
-	ReplicatePrint <- function(dat){
-		write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
-	}
-
-	ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
-	lapply(ReplicateSplit, FUN=ReplicatePrint)
-
-	ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(Reads), ReadsSquaredSum=sum(squared)), by=c("Sample")])
-	clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
-
-
-	ReplicateSumPrint <- function(dat){
-		write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
-	}
-
-	ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
-	lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
-
-	clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
-	clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
-	clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
-	clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
-	clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
-
-	ClonalityScorePrint <- function(dat){
-		write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
-	}
-
-	clonalityScore = clonalFreqCount[c("Sample", "Result")]
-	clonalityScore = unique(clonalityScore)
-
-	clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
-	lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
-
-	clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
-
-
-
-	ClonalityOverviewPrint <- function(dat){
-		write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
-	}
-
-	clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
-	lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
-}
-
-if("Functionality" %in% colnames(test))
-{
-	newData = data.frame(data.table(PROD)[,list(unique=.N, 
-				VH.DEL=mean(X3V.REGION.trimmed.nt.nb),
-				P1=mean(P3V.nt.nb),
-				N1=mean(N1.REGION.nt.nb),
-				P2=mean(P5D.nt.nb),
-				DEL.DH=mean(X5D.REGION.trimmed.nt.nb),
-				DH.DEL=mean(X3D.REGION.trimmed.nt.nb),
-				P3=mean(P3D.nt.nb),
-				N2=mean(N2.REGION.nt.nb),
-				P4=mean(P5J.nt.nb),
-				DEL.JH=mean(X5J.REGION.trimmed.nt.nb),
-				Total.Del=(	mean(X3V.REGION.trimmed.nt.nb) + 
-							mean(X5D.REGION.trimmed.nt.nb) + 
-							mean(X3D.REGION.trimmed.nt.nb) +
-							mean(X5J.REGION.trimmed.nt.nb)),
-							
-				Total.N=(	mean(N1.REGION.nt.nb) +
-							mean(N2.REGION.nt.nb)),
-							
-				Total.P=(	mean(P3V.nt.nb) +
-							mean(P5D.nt.nb) +
-							mean(P3D.nt.nb) +
-							mean(P5J.nt.nb))),
-				by=c("Sample")])
-	write.table(newData, "junctionAnalysis.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
-}
--- a/RScript_t.r	Mon Sep 08 04:24:04 2014 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,499 +0,0 @@
-#options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
-
-args <- commandArgs(trailingOnly = TRUE)
-
-inFile = args[1]
-outFile = args[2]
-outDir = args[3]
-clonalType = args[4]
-species = args[5]
-locus = args[6]
-selection = args[7]
-
-if (!("gridExtra" %in% rownames(installed.packages()))) {
-	install.packages("gridExtra", repos="http://cran.xl-mirror.nl/") 
-}
-library(gridExtra)
-if (!("ggplot2" %in% rownames(installed.packages()))) {
-	install.packages("ggplot2", repos="http://cran.xl-mirror.nl/") 
-}
-library(ggplot2)
-if (!("plyr" %in% rownames(installed.packages()))) {
-	install.packages("plyr", repos="http://cran.xl-mirror.nl/") 
-}			
-library(plyr)
-
-if (!("data.table" %in% rownames(installed.packages()))) {
-	install.packages("data.table", repos="http://cran.xl-mirror.nl/") 
-}
-library(data.table)
-
-if (!("reshape2" %in% rownames(installed.packages()))) {
-	install.packages("reshape2", repos="http://cran.xl-mirror.nl/") 
-}
-library(reshape2)
-
-
-test = read.table(inFile, sep="\t", header=TRUE, fill=T, comment.char="")
-
-test = test[test$Sample != "",]
-
-print("test1\n")
-
-test$Top.V.Gene = gsub("[*]([0-9]+)", "", test$Top.V.Gene)
-test$Top.D.Gene = gsub("[*]([0-9]+)", "", test$Top.D.Gene)
-test$Top.J.Gene = gsub("[*]([0-9]+)", "", test$Top.J.Gene)
-
-#test$VDJCDR3 = do.call(paste, c(test[c("Top.V.Gene", "Top.D.Gene", "Top.J.Gene","CDR3.Seq.DNA")], sep = ":"))
-test$VDJCDR3 = do.call(paste, c(test[unlist(strsplit(clonalType, ","))], sep = ":"))
-
-PROD = test[test$VDJ.Frame != "In-frame with stop codon" & test$VDJ.Frame != "Out-of-frame" & test$CDR3.Found.How != "NOT_FOUND" , ]
-if("Functionality" %in% colnames(test)) {
-	PROD = test[test$Functionality == "productive" | test$Functionality == "productive (see comment)", ]
-}
-
-NONPROD = test[test$VDJ.Frame == "In-frame with stop codon" | test$VDJ.Frame == "Out-of-frame" | test$CDR3.Found.How == "NOT_FOUND" , ]
-
-#PRODF = PROD[ -1]
-
-PRODF = PROD
-print("test2\n")
-#PRODF = unique(PRODF)
-if(any(grepl(pattern="_", x=PRODF$ID))){ #dumb and way to simple
-	PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
-	PRODF$freq = gsub("_.*", "", PRODF$freq)
-	PRODF$freq = as.numeric(PRODF$freq)
-	if(any(is.na(PRODF$freq))){ #fix the dumbness if it fails
-		PRODF$freq = 1
-		if(selection == "unique"){
-			PRODF = PRODF[!duplicated(PRODF$VDJCDR3), ]
-		}
-	}
-} else {
-	PRODF$freq = 1
-	if(selection == "unique"){
-		PRODF = PRODF[!duplicated(PRODF$VDJCDR3), ]
-	}
-}
-
-PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
-PRODFV$Length = as.numeric(PRODFV$Length)
-Total = 0
-Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
-PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
-PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
-
-PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
-PRODFD$Length = as.numeric(PRODFD$Length)
-Total = 0
-Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
-PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
-PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
-
-PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
-PRODFJ$Length = as.numeric(PRODFJ$Length)
-Total = 0
-Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
-PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
-PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
-
-V = c("v.name\tchr.orderV\n")
-D = c("v.name\tchr.orderD\n")	
-J = c("v.name\tchr.orderJ\n")
-
-print("test3\n")
-
-if(species == "human"){
-	if(locus == "trb"){		
-		V = c("v.name\tchr.orderV\nTRBV2\t1\nTRBV3-1\t2\nTRBV4-1\t3\nTRBV5-1\t4\nTRBV6-1\t5\nTRBV4-2\t6\nTRBV6-2\t7\nTRBV4-3\t8\nTRBV6-3\t9\nTRBV7-2\t10\nTRBV6-4\t11\nTRBV7-3\t12\nTRBV9\t13\nTRBV10-1\t14\nTRBV11-1\t15\nTRBV10-2\t16\nTRBV11-2\t17\nTRBV6-5\t18\nTRBV7-4\t19\nTRBV5-4\t20\nTRBV6-6\t21\nTRBV5-5\t22\nTRBV7-6\t23\nTRBV5-6\t24\nTRBV6-8\t25\nTRBV7-7\t26\nTRBV6-9\t27\nTRBV7-8\t28\nTRBV5-8\t29\nTRBV7-9\t30\nTRBV13\t31\nTRBV10-3\t32\nTRBV11-3\t33\nTRBV12-3\t34\nTRBV12-4\t35\nTRBV12-5\t36\nTRBV14\t37\nTRBV15\t38\nTRBV16\t39\nTRBV18\t40\nTRBV19\t41\nTRBV20-1\t42\nTRBV24-1\t43\nTRBV25-1\t44\nTRBV27\t45\nTRBV28\t46\nTRBV29-1\t47\nTRBV30\t48")
-		D = c("v.name\tchr.orderD\nTRBD1\t1\nTRBD2\t2\n")	
-		J = c("v.name\tchr.orderJ\nTRBJ1-1\t1\nTRBJ1-2\t2\nTRBJ1-3\t3\nTRBJ1-4\t4\nTRBJ1-5\t5\nTRBJ1-6\t6\nTRBJ2-1\t7\nTRBJ2-2\t8\nTRBJ2-3\t9\nTRBJ2-4\t10\nTRBJ2-5\t11\nTRBJ2-6\t12\nTRBJ2-7\t13")
-	} else if (locus == "tra"){
-		V = c("v.name\tchr.orderVTRAV1-1\t1\nTRAV1-2\t2\nTRAV2\t3\nTRAV3\t4\nTRAV4\t5\nTRAV5\t6\nTRAV6\t7\nTRAV7\t8\nTRAV8-1\t9\nTRAV9-1\t10\nTRAV10\t11\nTRAV12-1\t12\nTRAV8-2\t13\nTRAV8-3\t14\nTRAV13-1\t15\nTRAV12-2\t16\nTRAV8-4\t17\nTRAV13-2\t18\nTRAV14/DV4\t19\nTRAV9-2\t20\nTRAV12-3\t21\nTRAV8-6\t22\nTRAV16\t23\nTRAV17\t24\nTRAV18\t25\nTRAV19\t26\nTRAV20\t27\nTRAV21\t28\nTRAV22\t29\nTRAV23/DV6\t30\nTRAV24\t31\nTRAV25\t32\nTRAV26-1\t33\nTRAV27\t34\nTRAV29/DV5\t35\nTRAV30\t36\nTRAV26-2\t37\nTRAV34\t38\nTRAV35\t39\nTRAV36/DV7\t40\nTRAV38-1\t41\nTRAV38-2/DV8\t42\nTRAV39\t43\nTRAV40\t44\nTRAV41\t45\n")
-		D = c("v.name\tchr.orderD\n")	
-		J = c("v.name\tchr.orderJ\nTRAJ57\t1\nTRAJ56\t2\nTRAJ54\t3\nTRAJ53\t4\nTRAJ52\t5\nTRAJ50\t6\nTRAJ49\t7\nTRAJ48\t8\nTRAJ47\t9\nTRAJ46\t10\nTRAJ45\t11\nTRAJ44\t12\nTRAJ43\t13\nTRAJ42\t14\nTRAJ41\t15\nTRAJ40\t16\nTRAJ39\t17\nTRAJ38\t18\nTRAJ37\t19\nTRAJ36\t20\nTRAJ34\t21\nTRAJ33\t22\nTRAJ32\t23\nTRAJ31\t24\nTRAJ30\t25\nTRAJ29\t26\nTRAJ28\t27\nTRAJ27\t28\nTRAJ26\t29\nTRAJ24\t30\nTRAJ23\t31\nTRAJ22\t32\nTRAJ21\t33\nTRAJ20\t34\nTRAJ18\t35\nTRAJ17\t36\nTRAJ16\t37\nTRAJ15\t38\nTRAJ14\t39\nTRAJ13\t40\nTRAJ12\t41\nTRAJ11\t42\nTRAJ10\t43\nTRAJ9\t44\nTRAJ8\t45\nTRAJ7\t46\nTRAJ6\t47\nTRAJ5\t48\nTRAJ4\t49\nTRAJ3\t50")
-	} else if (locus == "trg"){
-		V = c("v.name\tchr.orderV\nTRGV9\t1\nTRGV8\t2\nTRGV5\t3\nTRGV4\t4\nTRGV3\t5\nTRGV2\t6")
-		D = c("v.name\tchr.orderD\n")	
-		J = c("v.name\tchr.orderJ\nTRGJ2\t1\nTRGJP2\t2\nTRGJ1\t3\nTRGJP1\t4")
-	} else if (locus == "trd"){
-		V = c("v.name\tchr.orderV\nTRDV1\t1\nTRDV2\t2\nTRDV3\t3")
-		D = c("v.name\tchr.orderD\nTRDD1\t1\nTRDD2\t2\nTRDD3\t3")	
-		J = c("v.name\tchr.orderJ\nTRDJ1\t1\nTRDJ4\t2\nTRDJ2\t3\nTRDJ3\t4")
-	}
-} else if (species == "mouse"){
-	if(locus == "trb"){		
-		cat("mouse trb not yet implemented")
-	} else if (locus == "tra"){
-		cat("mouse tra not yet implemented")
-	} else if (locus == "trg"){
-		cat("mouse trg not yet implemented")
-	} else if (locus == "trd"){
-		cat("mouse trd not yet implemented")
-	}
-}
-useD = TRUE
-if(species == "human" && locus == "tra"){
-	useD = FALSE
-	cat("No D Genes in this species/locus")
-}
-
-print("test4\n")
-
-tcV = textConnection(V)
-Vchain = read.table(tcV, sep="\t", header=TRUE)
-PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
-close(tcV)
-
-
-tcD = textConnection(D)
-Dchain = read.table(tcD, sep="\t", header=TRUE)
-PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
-close(tcD)
-
-
-
-tcJ = textConnection(J)
-Jchain = read.table(tcJ, sep="\t", header=TRUE)
-PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
-close(tcJ)
-
-setwd(outDir)
-
-write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-pV = ggplot(PRODFV)
-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))
-pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
-write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-png("VPlot.png",width = 1280, height = 720)
-pV
-dev.off();
-
-pD = ggplot(PRODFD)
-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))
-pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
-write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-png("DPlot.png",width = 800, height = 600)
-pD
-dev.off();
-
-pJ = ggplot(PRODFJ)
-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))
-pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
-write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-png("JPlot.png",width = 800, height = 600)
-pJ
-dev.off();
-
-print("test5\n")
-
-VGenes = PRODF[,c("Sample", "Top.V.Gene", "freq")]
-VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
-VGenes = data.frame(data.table(VGenes)[, list(Count=sum(freq)), by=c("Sample", "Top.V.Gene")])
-TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
-VGenes = merge(VGenes, TotalPerSample, by="Sample")
-VGenes$Frequency = VGenes$Count * 100 / VGenes$total
-VPlot = ggplot(VGenes)
-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)) + 
-				ggtitle("Distribution of V gene families") + 
-				ylab("Percentage of sequences")
-png("VFPlot.png")
-VPlot
-dev.off();
-write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-DGenes = PRODF[,c("Sample", "Top.D.Gene", "freq")]
-DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
-DGenes = data.frame(data.table(DGenes)[, list(Count=sum(freq)), by=c("Sample", "Top.D.Gene")])
-TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
-DGenes = merge(DGenes, TotalPerSample, by="Sample")
-DGenes$Frequency = DGenes$Count * 100 / DGenes$total
-DPlot = ggplot(DGenes)
-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)) + 
-				ggtitle("Distribution of D gene families") + 
-				ylab("Percentage of sequences")
-png("DFPlot.png")
-DPlot
-dev.off();
-write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-JGenes = PRODF[,c("Sample", "Top.J.Gene", "freq")]
-JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
-JGenes = data.frame(data.table(JGenes)[, list(Count=sum(freq)), by=c("Sample", "Top.J.Gene")])
-TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
-JGenes = merge(JGenes, TotalPerSample, by="Sample")
-JGenes$Frequency = JGenes$Count * 100 / JGenes$total
-JPlot = ggplot(JGenes)
-JPlot = JPlot + geom_bar(aes( x = Top.J.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-				ggtitle("Distribution of J gene families") + 
-				ylab("Percentage of sequences")
-png("JFPlot.png")
-JPlot
-dev.off();
-write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-CDR3Length = data.frame(data.table(PRODF)[, list(Count=sum(freq)), by=c("Sample", "CDR3.Length.DNA")])
-TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
-CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
-CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
-CDR3LengthPlot = ggplot(CDR3Length)
-CDR3LengthPlot = CDR3LengthPlot + geom_bar(aes( x = CDR3.Length.DNA, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-				ggtitle("Length distribution of CDR3") + 
-				xlab("CDR3 Length") + 
-				ylab("Percentage of sequences")
-png("CDR3LengthPlot.png",width = 1280, height = 720)
-CDR3LengthPlot
-dev.off()
-write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-revVchain = Vchain
-revDchain = Dchain
-revVchain$chr.orderV = rev(revVchain$chr.orderV)
-revDchain$chr.orderD = rev(revDchain$chr.orderD)
-
-print("test6\n")
-
-plotVD <- function(dat){
-	if(length(dat[,1]) == 0){
-		return()
-	}
-	img = ggplot() + 
-	geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) + 
-	theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-	scale_fill_gradient(low="gold", high="blue", na.value="white") + 
-	ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
-	xlab("D genes") + 
-	ylab("V Genes")
-	
-	png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
-	print(img)
-	
-	dev.off()
-	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)
-}
-
-VandDCount = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
-
-VandDCount$l = log(VandDCount$Length)
-maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
-VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
-VandDCount$relLength = VandDCount$l / VandDCount$max
-
-cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(test$Sample))
-
-completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
-completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
-completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
-VDList = split(completeVD, f=completeVD[,"Sample"])
-
-lapply(VDList, FUN=plotVD)
-
-plotVJ <- function(dat){
-	if(length(dat[,1]) == 0){
-		return()
-	}
-	img = ggplot() + 
-	geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) + 
-	theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-	scale_fill_gradient(low="gold", high="blue", na.value="white") + 
-	ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
-	xlab("J genes") + 
-	ylab("V Genes")
-	
-	png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
-	print(img)
-	dev.off()
-	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)
-}
-
-VandJCount = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
-
-VandJCount$l = log(VandJCount$Length)
-maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
-VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
-VandJCount$relLength = VandJCount$l / VandJCount$max
-
-cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
-
-completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
-completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
-completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
-VJList = split(completeVJ, f=completeVJ[,"Sample"])
-lapply(VJList, FUN=plotVJ)
-
-plotDJ <- function(dat){
-	if(length(dat[,1]) == 0){
-		return()
-	}
-	img = ggplot() + 
-	geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) + 
-	theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
-	scale_fill_gradient(low="gold", high="blue", na.value="white") + 
-	ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) + 
-	xlab("J genes") + 
-	ylab("D Genes")
-	
-	png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
-	print(img)
-	dev.off()
-	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)
-}
-
-DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
-
-DandJCount$l = log(DandJCount$Length)
-maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
-DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
-DandJCount$relLength = DandJCount$l / DandJCount$max
-
-cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
-
-completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
-completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
-completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
-DJList = split(completeDJ, f=completeDJ[,"Sample"])
-lapply(DJList, FUN=plotDJ)
-
-sampleFile <- file("samples.txt")
-un = unique(test$Sample)
-un = paste(un, sep="\n")
-writeLines(un, sampleFile)
-close(sampleFile)
-
-print("test7\n")
-
-if("Replicate" %in% colnames(test))
-{
-	clonalityFrame = PROD
-	clonalityFrame$ReplicateConcat = do.call(paste, c(clonalityFrame[c("VDJCDR3", "Sample", "Replicate")], sep = ":"))
-	clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$ReplicateConcat), ]
-	write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
-
-	ClonalitySampleReplicatePrint <- function(dat){
-	    write.table(dat, paste("clonality_", unique(dat$Sample) , "_", unique(dat$Replicate), ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
-	}
-
-	clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,c("Sample", "Replicate")])
-	#lapply(clonalityFrameSplit, FUN=ClonalitySampleReplicatePrint)
-
-	ClonalitySamplePrint <- function(dat){
-	    write.table(dat, paste("clonality_", unique(dat$Sample) , ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
-	}
-
-	clonalityFrameSplit = split(clonalityFrame, f=clonalityFrame[,"Sample"])
-	#lapply(clonalityFrameSplit, FUN=ClonalitySamplePrint)
-
-	clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "VDJCDR3")])
-	clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
-	clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
-	clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
-	clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
-
-	ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
-	tcct = textConnection(ct)
-	CT  = read.table(tcct, sep="\t", header=TRUE)
-	close(tcct)
-	clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
-	clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
-
-	ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "VDJCDR3")])
-	ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
-	clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
-	ReplicateReads$squared = ReplicateReads$Reads * ReplicateReads$Reads
-
-	ReplicatePrint <- function(dat){
-		write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
-	}
-
-	ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
-	lapply(ReplicateSplit, FUN=ReplicatePrint)
-
-	ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(Reads), ReadsSquaredSum=sum(squared)), by=c("Sample")])
-	clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
-
-
-	ReplicateSumPrint <- function(dat){
-		write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
-	}
-
-	ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
-	lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
-	
-	writeClonalitySequences <- function(dat){
-		for(i in c(2,3,4,5,6)){
-			fltr = dat[dat$Type == i,]
-			if(length(fltr[,1]) == 0){
-				next
-			}
-			tmp = clonalityFrame[clonalityFrame$Sample == fltr$Sample[1] & clonalityFrame$VDJCDR3 %in% fltr$VDJCDR3,]
-			tmp = tmp[order(tmp$VDJCDR3),]
-			write.table(tmp, paste("ClonalitySequences_", unique(dat[1])[1,1] , "_", i, ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=T)
-		}
-	}
-	freqsplt = split(clonalFreq[clonalFreq$Type > 1,], clonalFreq[clonalFreq$Type > 1,]$Sample)
-	lapply(freqsplt, FUN=writeClonalitySequences)
-
-	clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
-	clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
-	clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
-	clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
-	clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
-
-	ClonalityScorePrint <- function(dat){
-		write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
-	}
-
-	clonalityScore = clonalFreqCount[c("Sample", "Result")]
-	clonalityScore = unique(clonalityScore)
-
-	clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
-	lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
-
-	clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
-
-
-
-	ClonalityOverviewPrint <- function(dat){
-		write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
-	}
-
-	clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
-	lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
-}
-
-print("test8\n")
-
-if("Functionality" %in% colnames(test))
-{
-	newData = data.frame(data.table(PROD)[,list(unique=.N, 
-				VH.DEL=mean(X3V.REGION.trimmed.nt.nb),
-				P1=mean(P3V.nt.nb),
-				N1=mean(N1.REGION.nt.nb),
-				P2=mean(P5D.nt.nb),
-				DEL.DH=mean(X5D.REGION.trimmed.nt.nb),
-				DH.DEL=mean(X3D.REGION.trimmed.nt.nb),
-				P3=mean(P3D.nt.nb),
-				N2=mean(N2.REGION.nt.nb),
-				P4=mean(P5J.nt.nb),
-				DEL.JH=mean(X5J.REGION.trimmed.nt.nb),
-				Total.Del=(	mean(X3V.REGION.trimmed.nt.nb) + 
-							mean(X5D.REGION.trimmed.nt.nb) + 
-							mean(X3D.REGION.trimmed.nt.nb) +
-							mean(X5J.REGION.trimmed.nt.nb)),
-							
-				Total.N=(	mean(N1.REGION.nt.nb) +
-							mean(N2.REGION.nt.nb)),
-							
-				Total.P=(	mean(P3V.nt.nb) +
-							mean(P5D.nt.nb) +
-							mean(P3D.nt.nb) +
-							mean(P5J.nt.nb))),
-				by=c("Sample")])
-	write.table(newData, "junctionAnalysis.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
-}
-
-print("test9\n")
Binary file asc.gif has changed
Binary file bg.gif has changed
--- a/complete.sh	Mon Sep 08 04:24:04 2014 -0400
+++ b/complete.sh	Mon Jan 05 09:30:08 2015 -0500
@@ -6,7 +6,7 @@
 clonalType=$4
 species=$5
 locus=$6
-selection=$7
+filterproductive=$7
 
 html=$2
 dir="$(cd "$(dirname "$0")" && pwd)"
@@ -23,7 +23,6 @@
 	echo "igblastn -germline_db_V $PWD/igblastdatabase/database/human_gl_V -germline_db_J $PWD/igblastdatabase/database/human_gl_J -germline_db_D $PWD/igblastdatabase/database/human_gl_D -domain_system imgt -query $1 -auxiliary_data $PWD/igblastdatabase/optional_file/human_gl.aux -show_translation -outfmt 3 > $PWD/$4"
 	/home/galaxy/galaxy/igblast/igblastn -germline_db_V $PWD/igblastdatabase/database/human_gl_V -germline_db_J $PWD/igblastdatabase/database/human_gl_J -germline_db_D $PWD/igblastdatabase/database/human_gl_D -domain_system imgt -query $1 -auxiliary_data $PWD/igblastdatabase/optional_file/human_gl.aux -show_translation -outfmt 3 > $PWD/$4
 	echo "<tr><td>Finished blast of sample $3 of patient $2</td></tr>" >> $html
-
 	echo "<tr><td>Starting parse of sample $3 of patient $2</td></tr>" >> $html
 	perl $dir/igparse.pl $PWD/$4 0 | grep -v "D:" | cut -f2- > "$5"
 	echo "<tr><td>Finished parse of sample $3 of patient $2</td></tr>" >> $html
@@ -33,7 +32,6 @@
 	echo "<tr><td>Starting imgt convert of sample $3 of patient $2</td></tr>" >> $html
 	bash $dir/imgt_loader.sh $1 $4 $5
 	echo "<tr><td>Finished conversion of sample $3 of patient $2</td></tr>" >> $html
-	
 }
 
 id=""
@@ -82,17 +80,5 @@
 
 echo "after ED"
 
-if [ "$locus" == "igh" ] || [ "$locus" == "igk" ] || [ "$locus" == "igl" ]; then
-	bash $dir/r_wrapper_b.sh $PWD/merged.txt $2 $outputDir $clonalType $species $locus $selection
-else
-	bash $dir/r_wrapper_t.sh $PWD/merged.txt $2 $outputDir $clonalType $species $locus $selection
-fi
-
+$dir/r_wrapper.sh $PWD/merged.txt $2 $outputDir $clonalType $species $locus $filterproductive
 
-
-
-
-
-
-
-
--- a/complete_immunerepertoire.xml	Mon Sep 08 04:24:04 2014 -0400
+++ b/complete_immunerepertoire.xml	Mon Jan 05 09:30:08 2015 -0500
@@ -8,7 +8,7 @@
  ${g.sample}
             #end for
 		#end for
-" $out_file $out_file.files_path "$clonaltype_select" $species $locus $selection
+" $out_file $out_file.files_path "$clonaltype" $species $locus $filterproductive
 	</command>
 	<inputs>
 		<repeat name="patients" title="Patients" min="1" default="1">
@@ -17,7 +17,8 @@
 			</repeat>
 			<param name="id" type="text" label="ID" />
 		</repeat>
-		<param name="clonaltype_select" type="select" label="Clonal Type Definition">
+		<param name="clonaltype" type="select" label="Clonal Type Definition">
+			<option value="none">Don't remove duplicates based on clonaltype</option>
 			<option value="Top.V.Gene,CDR3.Seq">Top.V.Gene, CDR3.Seq</option>
 			<option value="Top.V.Gene,CDR3.Seq.DNA">Top.V.Gene, CDR3.Seq.DNA</option>
 			<option value="Top.V.Gene,Top.J.Gene,CDR3.Seq">Top.V.Gene, Top.J.Gene, CDR3.Seq</option>
@@ -39,10 +40,10 @@
 			<option value="trg">TRG</option>
 			<option value="trd">TRD</option>
 		</param>
-
-		<param name="selection" type="select" label="Selection">
-			<option value="unique">Unique (Based on clonaltype)</option>
-			<option value="all">All</option>
+		
+		<param name="filterproductive" type="select" label="Filter out the unproductive sequences">
+			<option value="yes">Yes</option>
+			<option value="no">No</option>
 		</param>
 	</inputs>
 	<outputs>
@@ -52,4 +53,3 @@
 		The entire Immune Repertoire pipeline as a single tool, input several FASTA files, give them an ID and it will BLAST, parse, merge and plot them.
 	</help>
 </tool>
-
Binary file desc.gif has changed
--- a/imgt_loader.py	Mon Sep 08 04:24:04 2014 -0400
+++ b/imgt_loader.py	Mon Jan 05 09:30:08 2015 -0500
@@ -128,7 +128,6 @@
 outFrame["Top D Gene"] = outFrame["Top D Gene"].apply(lambda x: filterGenes(x, dPattern))
 outFrame["Top J Gene"] = outFrame["Top J Gene"].apply(lambda x: filterGenes(x, jPattern))
 
-print outFrame
 
 tmp = outFrame["VDJ Frame"]
 tmp = tmp.replace("in-frame", "In-frame")
@@ -137,6 +136,6 @@
 outFrame["VDJ Frame"] = tmp
 outFrame["CDR3 Length DNA"] = outFrame["CDR3 Seq DNA"].map(str).map(len)
 safeLength = lambda x: len(x) if type(x) == str else 0
-outFrame = outFrame[(outFrame["CDR3 Seq DNA"].map(safeLength) > 0) & (outFrame["Top V Gene"] != "NA") & (outFrame["Top J Gene"] != "NA")] #filter out weird rows?
+#outFrame = outFrame[(outFrame["CDR3 Seq DNA"].map(safeLength) > 0) & (outFrame["Top V Gene"] != "NA") & (outFrame["Top J Gene"] != "NA")] #filter out weird rows?
 #outFrame = outFrame[(outFrame["CDR3 Seq DNA"].map(safeLength) > 0) & (outFrame["Top V Gene"] != "NA") & (outFrame["Top D Gene"] != "NA") & (outFrame["Top J Gene"] != "NA")] #filter out weird rows?
 outFrame.to_csv(outFile, sep="\t", index=False, index_label="index")
--- a/jquery.tablesorter.min.js	Mon Sep 08 04:24:04 2014 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-
-(function($){$.extend({tablesorter:new
-function(){var parsers=[],widgets=[];this.defaults={cssHeader:"header",cssAsc:"headerSortUp",cssDesc:"headerSortDown",cssChildRow:"expand-child",sortInitialOrder:"asc",sortMultiSortKey:"shiftKey",sortForce:null,sortAppend:null,sortLocaleCompare:true,textExtraction:"simple",parsers:{},widgets:[],widgetZebra:{css:["even","odd"]},headers:{},widthFixed:false,cancelSelection:true,sortList:[],headerList:[],dateFormat:"us",decimal:'/\.|\,/g',onRenderHeader:null,selectorHeaders:'thead th',debug:false};function benchmark(s,d){log(s+","+(new Date().getTime()-d.getTime())+"ms");}this.benchmark=benchmark;function log(s){if(typeof console!="undefined"&&typeof console.debug!="undefined"){console.log(s);}else{alert(s);}}function buildParserCache(table,$headers){if(table.config.debug){var parsersDebug="";}if(table.tBodies.length==0)return;var rows=table.tBodies[0].rows;if(rows[0]){var list=[],cells=rows[0].cells,l=cells.length;for(var i=0;i<l;i++){var p=false;if($.metadata&&($($headers[i]).metadata()&&$($headers[i]).metadata().sorter)){p=getParserById($($headers[i]).metadata().sorter);}else if((table.config.headers[i]&&table.config.headers[i].sorter)){p=getParserById(table.config.headers[i].sorter);}if(!p){p=detectParserForColumn(table,rows,-1,i);}if(table.config.debug){parsersDebug+="column:"+i+" parser:"+p.id+"\n";}list.push(p);}}if(table.config.debug){log(parsersDebug);}return list;};function detectParserForColumn(table,rows,rowIndex,cellIndex){var l=parsers.length,node=false,nodeValue=false,keepLooking=true;while(nodeValue==''&&keepLooking){rowIndex++;if(rows[rowIndex]){node=getNodeFromRowAndCellIndex(rows,rowIndex,cellIndex);nodeValue=trimAndGetNodeText(table.config,node);if(table.config.debug){log('Checking if value was empty on row:'+rowIndex);}}else{keepLooking=false;}}for(var i=1;i<l;i++){if(parsers[i].is(nodeValue,table,node)){return parsers[i];}}return parsers[0];}function getNodeFromRowAndCellIndex(rows,rowIndex,cellIndex){return rows[rowIndex].cells[cellIndex];}function trimAndGetNodeText(config,node){return $.trim(getElementText(config,node));}function getParserById(name){var l=parsers.length;for(var i=0;i<l;i++){if(parsers[i].id.toLowerCase()==name.toLowerCase()){return parsers[i];}}return false;}function buildCache(table){if(table.config.debug){var cacheTime=new Date();}var totalRows=(table.tBodies[0]&&table.tBodies[0].rows.length)||0,totalCells=(table.tBodies[0].rows[0]&&table.tBodies[0].rows[0].cells.length)||0,parsers=table.config.parsers,cache={row:[],normalized:[]};for(var i=0;i<totalRows;++i){var c=$(table.tBodies[0].rows[i]),cols=[];if(c.hasClass(table.config.cssChildRow)){cache.row[cache.row.length-1]=cache.row[cache.row.length-1].add(c);continue;}cache.row.push(c);for(var j=0;j<totalCells;++j){cols.push(parsers[j].format(getElementText(table.config,c[0].cells[j]),table,c[0].cells[j]));}cols.push(cache.normalized.length);cache.normalized.push(cols);cols=null;};if(table.config.debug){benchmark("Building cache for "+totalRows+" rows:",cacheTime);}return cache;};function getElementText(config,node){var text="";if(!node)return"";if(!config.supportsTextContent)config.supportsTextContent=node.textContent||false;if(config.textExtraction=="simple"){if(config.supportsTextContent){text=node.textContent;}else{if(node.childNodes[0]&&node.childNodes[0].hasChildNodes()){text=node.childNodes[0].innerHTML;}else{text=node.innerHTML;}}}else{if(typeof(config.textExtraction)=="function"){text=config.textExtraction(node);}else{text=$(node).text();}}return text;}function appendToTable(table,cache){if(table.config.debug){var appendTime=new Date()}var c=cache,r=c.row,n=c.normalized,totalRows=n.length,checkCell=(n[0].length-1),tableBody=$(table.tBodies[0]),rows=[];for(var i=0;i<totalRows;i++){var pos=n[i][checkCell];rows.push(r[pos]);if(!table.config.appender){var l=r[pos].length;for(var j=0;j<l;j++){tableBody[0].appendChild(r[pos][j]);}}}if(table.config.appender){table.config.appender(table,rows);}rows=null;if(table.config.debug){benchmark("Rebuilt table:",appendTime);}applyWidget(table);setTimeout(function(){$(table).trigger("sortEnd");},0);};function buildHeaders(table){if(table.config.debug){var time=new Date();}var meta=($.metadata)?true:false;var header_index=computeTableHeaderCellIndexes(table);$tableHeaders=$(table.config.selectorHeaders,table).each(function(index){this.column=header_index[this.parentNode.rowIndex+"-"+this.cellIndex];this.order=formatSortingOrder(table.config.sortInitialOrder);this.count=this.order;if(checkHeaderMetadata(this)||checkHeaderOptions(table,index))this.sortDisabled=true;if(checkHeaderOptionsSortingLocked(table,index))this.order=this.lockedOrder=checkHeaderOptionsSortingLocked(table,index);if(!this.sortDisabled){var $th=$(this).addClass(table.config.cssHeader);if(table.config.onRenderHeader)table.config.onRenderHeader.apply($th);}table.config.headerList[index]=this;});if(table.config.debug){benchmark("Built headers:",time);log($tableHeaders);}return $tableHeaders;};function computeTableHeaderCellIndexes(t){var matrix=[];var lookup={};var thead=t.getElementsByTagName('THEAD')[0];var trs=thead.getElementsByTagName('TR');for(var i=0;i<trs.length;i++){var cells=trs[i].cells;for(var j=0;j<cells.length;j++){var c=cells[j];var rowIndex=c.parentNode.rowIndex;var cellId=rowIndex+"-"+c.cellIndex;var rowSpan=c.rowSpan||1;var colSpan=c.colSpan||1
-var firstAvailCol;if(typeof(matrix[rowIndex])=="undefined"){matrix[rowIndex]=[];}for(var k=0;k<matrix[rowIndex].length+1;k++){if(typeof(matrix[rowIndex][k])=="undefined"){firstAvailCol=k;break;}}lookup[cellId]=firstAvailCol;for(var k=rowIndex;k<rowIndex+rowSpan;k++){if(typeof(matrix[k])=="undefined"){matrix[k]=[];}var matrixrow=matrix[k];for(var l=firstAvailCol;l<firstAvailCol+colSpan;l++){matrixrow[l]="x";}}}}return lookup;}function checkCellColSpan(table,rows,row){var arr=[],r=table.tHead.rows,c=r[row].cells;for(var i=0;i<c.length;i++){var cell=c[i];if(cell.colSpan>1){arr=arr.concat(checkCellColSpan(table,headerArr,row++));}else{if(table.tHead.length==1||(cell.rowSpan>1||!r[row+1])){arr.push(cell);}}}return arr;};function checkHeaderMetadata(cell){if(($.metadata)&&($(cell).metadata().sorter===false)){return true;};return false;}function checkHeaderOptions(table,i){if((table.config.headers[i])&&(table.config.headers[i].sorter===false)){return true;};return false;}function checkHeaderOptionsSortingLocked(table,i){if((table.config.headers[i])&&(table.config.headers[i].lockedOrder))return table.config.headers[i].lockedOrder;return false;}function applyWidget(table){var c=table.config.widgets;var l=c.length;for(var i=0;i<l;i++){getWidgetById(c[i]).format(table);}}function getWidgetById(name){var l=widgets.length;for(var i=0;i<l;i++){if(widgets[i].id.toLowerCase()==name.toLowerCase()){return widgets[i];}}};function formatSortingOrder(v){if(typeof(v)!="Number"){return(v.toLowerCase()=="desc")?1:0;}else{return(v==1)?1:0;}}function isValueInArray(v,a){var l=a.length;for(var i=0;i<l;i++){if(a[i][0]==v){return true;}}return false;}function setHeadersCss(table,$headers,list,css){$headers.removeClass(css[0]).removeClass(css[1]);var h=[];$headers.each(function(offset){if(!this.sortDisabled){h[this.column]=$(this);}});var l=list.length;for(var i=0;i<l;i++){h[list[i][0]].addClass(css[list[i][1]]);}}function fixColumnWidth(table,$headers){var c=table.config;if(c.widthFixed){var colgroup=$('<colgroup>');$("tr:first td",table.tBodies[0]).each(function(){colgroup.append($('<col>').css('width',$(this).width()));});$(table).prepend(colgroup);};}function updateHeaderSortCount(table,sortList){var c=table.config,l=sortList.length;for(var i=0;i<l;i++){var s=sortList[i],o=c.headerList[s[0]];o.count=s[1];o.count++;}}function multisort(table,sortList,cache){if(table.config.debug){var sortTime=new Date();}var dynamicExp="var sortWrapper = function(a,b) {",l=sortList.length;for(var i=0;i<l;i++){var c=sortList[i][0];var order=sortList[i][1];var s=(table.config.parsers[c].type=="text")?((order==0)?makeSortFunction("text","asc",c):makeSortFunction("text","desc",c)):((order==0)?makeSortFunction("numeric","asc",c):makeSortFunction("numeric","desc",c));var e="e"+i;dynamicExp+="var "+e+" = "+s;dynamicExp+="if("+e+") { return "+e+"; } ";dynamicExp+="else { ";}var orgOrderCol=cache.normalized[0].length-1;dynamicExp+="return a["+orgOrderCol+"]-b["+orgOrderCol+"];";for(var i=0;i<l;i++){dynamicExp+="}; ";}dynamicExp+="return 0; ";dynamicExp+="}; ";if(table.config.debug){benchmark("Evaling expression:"+dynamicExp,new Date());}eval(dynamicExp);cache.normalized.sort(sortWrapper);if(table.config.debug){benchmark("Sorting on "+sortList.toString()+" and dir "+order+" time:",sortTime);}return cache;};function makeSortFunction(type,direction,index){var a="a["+index+"]",b="b["+index+"]";if(type=='text'&&direction=='asc'){return"("+a+" == "+b+" ? 0 : ("+a+" === null ? Number.POSITIVE_INFINITY : ("+b+" === null ? Number.NEGATIVE_INFINITY : ("+a+" < "+b+") ? -1 : 1 )));";}else if(type=='text'&&direction=='desc'){return"("+a+" == "+b+" ? 0 : ("+a+" === null ? Number.POSITIVE_INFINITY : ("+b+" === null ? Number.NEGATIVE_INFINITY : ("+b+" < "+a+") ? -1 : 1 )));";}else if(type=='numeric'&&direction=='asc'){return"("+a+" === null && "+b+" === null) ? 0 :("+a+" === null ? Number.POSITIVE_INFINITY : ("+b+" === null ? Number.NEGATIVE_INFINITY : "+a+" - "+b+"));";}else if(type=='numeric'&&direction=='desc'){return"("+a+" === null && "+b+" === null) ? 0 :("+a+" === null ? Number.POSITIVE_INFINITY : ("+b+" === null ? Number.NEGATIVE_INFINITY : "+b+" - "+a+"));";}};function makeSortText(i){return"((a["+i+"] < b["+i+"]) ? -1 : ((a["+i+"] > b["+i+"]) ? 1 : 0));";};function makeSortTextDesc(i){return"((b["+i+"] < a["+i+"]) ? -1 : ((b["+i+"] > a["+i+"]) ? 1 : 0));";};function makeSortNumeric(i){return"a["+i+"]-b["+i+"];";};function makeSortNumericDesc(i){return"b["+i+"]-a["+i+"];";};function sortText(a,b){if(table.config.sortLocaleCompare)return a.localeCompare(b);return((a<b)?-1:((a>b)?1:0));};function sortTextDesc(a,b){if(table.config.sortLocaleCompare)return b.localeCompare(a);return((b<a)?-1:((b>a)?1:0));};function sortNumeric(a,b){return a-b;};function sortNumericDesc(a,b){return b-a;};function getCachedSortType(parsers,i){return parsers[i].type;};this.construct=function(settings){return this.each(function(){if(!this.tHead||!this.tBodies)return;var $this,$document,$headers,cache,config,shiftDown=0,sortOrder;this.config={};config=$.extend(this.config,$.tablesorter.defaults,settings);$this=$(this);$.data(this,"tablesorter",config);$headers=buildHeaders(this);this.config.parsers=buildParserCache(this,$headers);cache=buildCache(this);var sortCSS=[config.cssDesc,config.cssAsc];fixColumnWidth(this);$headers.click(function(e){var totalRows=($this[0].tBodies[0]&&$this[0].tBodies[0].rows.length)||0;if(!this.sortDisabled&&totalRows>0){$this.trigger("sortStart");var $cell=$(this);var i=this.column;this.order=this.count++%2;if(this.lockedOrder)this.order=this.lockedOrder;if(!e[config.sortMultiSortKey]){config.sortList=[];if(config.sortForce!=null){var a=config.sortForce;for(var j=0;j<a.length;j++){if(a[j][0]!=i){config.sortList.push(a[j]);}}}config.sortList.push([i,this.order]);}else{if(isValueInArray(i,config.sortList)){for(var j=0;j<config.sortList.length;j++){var s=config.sortList[j],o=config.headerList[s[0]];if(s[0]==i){o.count=s[1];o.count++;s[1]=o.count%2;}}}else{config.sortList.push([i,this.order]);}};setTimeout(function(){setHeadersCss($this[0],$headers,config.sortList,sortCSS);appendToTable($this[0],multisort($this[0],config.sortList,cache));},1);return false;}}).mousedown(function(){if(config.cancelSelection){this.onselectstart=function(){return false};return false;}});$this.bind("update",function(){var me=this;setTimeout(function(){me.config.parsers=buildParserCache(me,$headers);cache=buildCache(me);},1);}).bind("updateCell",function(e,cell){var config=this.config;var pos=[(cell.parentNode.rowIndex-1),cell.cellIndex];cache.normalized[pos[0]][pos[1]]=config.parsers[pos[1]].format(getElementText(config,cell),cell);}).bind("sorton",function(e,list){$(this).trigger("sortStart");config.sortList=list;var sortList=config.sortList;updateHeaderSortCount(this,sortList);setHeadersCss(this,$headers,sortList,sortCSS);appendToTable(this,multisort(this,sortList,cache));}).bind("appendCache",function(){appendToTable(this,cache);}).bind("applyWidgetId",function(e,id){getWidgetById(id).format(this);}).bind("applyWidgets",function(){applyWidget(this);});if($.metadata&&($(this).metadata()&&$(this).metadata().sortlist)){config.sortList=$(this).metadata().sortlist;}if(config.sortList.length>0){$this.trigger("sorton",[config.sortList]);}applyWidget(this);});};this.addParser=function(parser){var l=parsers.length,a=true;for(var i=0;i<l;i++){if(parsers[i].id.toLowerCase()==parser.id.toLowerCase()){a=false;}}if(a){parsers.push(parser);};};this.addWidget=function(widget){widgets.push(widget);};this.formatFloat=function(s){var i=parseFloat(s);return(isNaN(i))?0:i;};this.formatInt=function(s){var i=parseInt(s);return(isNaN(i))?0:i;};this.isDigit=function(s,config){return/^[-+]?\d*$/.test($.trim(s.replace(/[,.']/g,'')));};this.clearTableBody=function(table){if($.browser.msie){function empty(){while(this.firstChild)this.removeChild(this.firstChild);}empty.apply(table.tBodies[0]);}else{table.tBodies[0].innerHTML="";}};}});$.fn.extend({tablesorter:$.tablesorter.construct});var ts=$.tablesorter;ts.addParser({id:"text",is:function(s){return true;},format:function(s){return $.trim(s.toLocaleLowerCase());},type:"text"});ts.addParser({id:"digit",is:function(s,table){var c=table.config;return $.tablesorter.isDigit(s,c);},format:function(s){return $.tablesorter.formatFloat(s);},type:"numeric"});ts.addParser({id:"currency",is:function(s){return/^[£$€?.]/.test(s);},format:function(s){return $.tablesorter.formatFloat(s.replace(new RegExp(/[£$€]/g),""));},type:"numeric"});ts.addParser({id:"ipAddress",is:function(s){return/^\d{2,3}[\.]\d{2,3}[\.]\d{2,3}[\.]\d{2,3}$/.test(s);},format:function(s){var a=s.split("."),r="",l=a.length;for(var i=0;i<l;i++){var item=a[i];if(item.length==2){r+="0"+item;}else{r+=item;}}return $.tablesorter.formatFloat(r);},type:"numeric"});ts.addParser({id:"url",is:function(s){return/^(https?|ftp|file):\/\/$/.test(s);},format:function(s){return jQuery.trim(s.replace(new RegExp(/(https?|ftp|file):\/\//),''));},type:"text"});ts.addParser({id:"isoDate",is:function(s){return/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(s);},format:function(s){return $.tablesorter.formatFloat((s!="")?new Date(s.replace(new RegExp(/-/g),"/")).getTime():"0");},type:"numeric"});ts.addParser({id:"percent",is:function(s){return/\%$/.test($.trim(s));},format:function(s){return $.tablesorter.formatFloat(s.replace(new RegExp(/%/g),""));},type:"numeric"});ts.addParser({id:"usLongDate",is:function(s){return s.match(new RegExp(/^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/));},format:function(s){return $.tablesorter.formatFloat(new Date(s).getTime());},type:"numeric"});ts.addParser({id:"shortDate",is:function(s){return/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/.test(s);},format:function(s,table){var c=table.config;s=s.replace(/\-/g,"/");if(c.dateFormat=="us"){s=s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/,"$3/$1/$2");}else if(c.dateFormat=="uk"){s=s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/,"$3/$2/$1");}else if(c.dateFormat=="dd/mm/yy"||c.dateFormat=="dd-mm-yy"){s=s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3");}return $.tablesorter.formatFloat(new Date(s).getTime());},type:"numeric"});ts.addParser({id:"time",is:function(s){return/^(([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(am|pm)))$/.test(s);},format:function(s){return $.tablesorter.formatFloat(new Date("2000/01/01 "+s).getTime());},type:"numeric"});ts.addParser({id:"metadata",is:function(s){return false;},format:function(s,table,cell){var c=table.config,p=(!c.parserMetadataName)?'sortValue':c.parserMetadataName;return $(cell).metadata()[p];},type:"numeric"});ts.addWidget({id:"zebra",format:function(table){if(table.config.debug){var time=new Date();}var $tr,row=-1,odd;$("tr:visible",table.tBodies[0]).each(function(i){$tr=$(this);if(!$tr.hasClass(table.config.cssChildRow))row++;odd=(row%2==0);$tr.removeClass(table.config.widgetZebra.css[odd?0:1]).addClass(table.config.widgetZebra.css[odd?1:0])});if(table.config.debug){$.tablesorter.benchmark("Applying Zebra widget",time);}}});})(jQuery);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/r_wrapper.sh	Mon Jan 05 09:30:08 2015 -0500
@@ -0,0 +1,164 @@
+#!/bin/bash
+
+inputFile=$1
+outputDir=$3
+outputFile=$3/index.html #$2
+clonalType=$4
+species=$5
+locus=$6
+filterproductive=$7
+useD="false"
+if [[ "$species" == "human" && "$locus" = "igh" ]] ; then
+	useD="true"
+fi
+dir="$(cd "$(dirname "$0")" && pwd)"
+mkdir $3
+Rscript --verbose $dir/RScript.r $inputFile $outputDir $outputDir $clonalType $species $locus $filterproductive 2>&1
+cp $dir/tabber.js $outputDir
+cp $dir/style.css $outputDir
+cp $dir/script.js $outputDir
+cp $dir/jquery-1.11.0.min.js $outputDir
+samples=`cat $outputDir/samples.txt`
+echo "<html><center><h1><a href='index.html'>Click here for the results</a></h1>Tip: Open it in a new tab (middle mouse button or right mouse button -> 'open in new tab' on the link above)<br />" > $2
+echo "<table border = 1>" >> $2
+echo "<thead><tr><th>Sample/Replicate</th><th>All</th><th>Productive</th><th>Unique Productive</th><th>Unproductive</th><th>Unique Unproductive</th></tr></thead>" >> $2
+while IFS=, read sample all productive perc_prod productive_unique perc_prod_un unproductive perc_unprod unproductive_unique perc_unprod_un
+	do
+		echo "<tr><td>$sample</td>" >> $2
+		echo "<td>$all</td>" >> $2
+		echo "<td>$productive (${perc_prod}%)</td>" >> $2
+		echo "<td>$productive_unique (${perc_prod_un}%)</td>" >> $2
+		echo "<td>$unproductive (${perc_unprod}%)</td>" >> $2
+		echo "<td>$unproductive_unique (${perc_unprod_un}%)</td></tr>" >> $2
+done < $outputDir/productive_counting.txt
+echo "</table border></center></html>" >> $2
+
+echo "productive_counting.txt"
+echo "<html><head><title>Report on:" >> $outputFile
+for sample in $samples; do
+	echo " $sample" >> $outputFile
+done
+echo "</title><script type='text/javascript' src='jquery-1.11.0.min.js'></script>" >> $outputFile
+echo "<script type='text/javascript' src='tabber.js'></script>" >> $outputFile
+echo "<script type='text/javascript' src='script.js'></script>" >> $outputFile
+echo "<link rel='stylesheet' type='text/css' href='style.css'></head>" >> $outputFile
+echo "<div class='tabber'><div class='tabbertab' title='Gene frequencies'>" >> $outputFile
+
+echo "<img src='CDR3LengthPlot.png'/><br />" >> $outputFile
+echo "<img src='VFPlot.png'/>" >> $outputFile
+if [[ "$useD" == "true" ]] ; then
+	echo "<img src='DFPlot.png'/>" >> $outputFile
+fi
+echo "<img src='JFPlot.png'/>" >> $outputFile
+echo "<img src='VPlot.png'/>" >> $outputFile
+if [[ "$useD" == "true" ]] ; then
+	echo "<img src='DPlot.png'/>" >> $outputFile
+fi
+echo "<img src='JPlot.png'/></div>" >> $outputFile
+
+count=1
+echo "<div class='tabbertab' title='Heatmaps'><div class='tabber'>" >> $outputFile
+for sample in $samples; do
+	echo "<div class='tabbertab' title='$sample'><table border='1'><tr>" >> $outputFile
+	if [[ "$useD" == "true" ]] ; then
+		echo "<td><img src='HeatmapVD_$sample.png'/></td>" >> $outputFile
+	fi
+	echo "<td><img src='HeatmapVJ_$sample.png'/></td>" >> $outputFile
+	if [[ "$useD" == "true" ]] ; then
+		echo "<td><img src='HeatmapDJ_$sample.png'/></td>" >> $outputFile
+	fi
+	echo "</tr></table></div>" >> $outputFile
+	count=$((count+1))
+done
+echo "</div></div>" >> $outputFile
+
+#echo "<div class='tabbertab' title='Interactive'><svg class='chart'></svg><script src='http://d3js.org/d3.v3.min.js'></script></div>" >> $outputFile
+
+hasReplicateColumn="$(if head -n 1 $inputFile | grep -q 'Replicate'; then echo 'Yes'; else echo 'No'; fi)"
+echo "$hasReplicateColumn"
+#if its a 'new' merged file with replicate info
+if [[ "$hasReplicateColumn" == "Yes" ]] ; then
+	echo "<div class='tabbertab' title='Clonality'><div class='tabber'>" >> $outputFile
+	for sample in $samples; do
+		clonalityScore="$(cat $outputDir/ClonalityScore_$sample.csv)"
+		echo "<div class='tabbertab' title='$sample'><table border='1'>" >> $outputFile
+		echo "<tr><td colspan='4'>Clonality Score: $clonalityScore</td></tr>" >> $outputFile
+
+		#replicate,reads,squared
+		echo "<tr><td>Replicate ID</td><td>Number of Reads</td><td>Reads Squared</td><td></td></tr>" >> $outputFile
+		while IFS=, read replicate reads squared
+		do
+			
+			echo "<tr><td>$replicate</td><td>$reads</td><td>$squared</td><td></td></tr>" >> $outputFile
+		done < $outputDir/ReplicateReads_$sample.csv
+		
+		#sum of reads and reads squared
+		while IFS=, read readsSum squaredSum
+			do
+				echo "<tr><td>Sum</td><td>$readsSum</td><td>$squaredSum</td></tr>" >> $outputFile
+		done < $outputDir/ReplicateSumReads_$sample.csv
+		
+		#overview
+		echo "<tr><td>Coincidence Type</td><td>Raw Coincidence Freq</td><td>Coincidence Weight</td><td>Coincidences, Weighted</td></tr>" >> $outputFile
+		while IFS=, read type count weight weightedCount
+		do
+			echo "<tr><td>$type</td><td>$count</td><td>$weight</td><td>$weightedCount</td></tr>" >> $outputFile
+		done < $outputDir/ClonalityOverView_$sample.csv
+		echo "</table></div>" >> $outputFile
+	done
+	echo "</div></div>" >> $outputFile
+fi
+
+hasJunctionData="$(if head -n 1 $inputFile | grep -q '3V-REGION trimmed-nt nb'; then echo 'Yes'; else echo 'No'; fi)"
+
+if [[ "$hasJunctionData" == "Yes" ]] ; then
+	echo "<div class='tabbertab' title='Junction Analysis'><table border='1' id='junction_table'><thead><tr><th>Sample</th><th>unique</th><th>VH.DEL</th><th>P1</th><th>N1</th><th>P2</th><th>DEL.DH</th><th>DH.DEL</th><th>P3</th><th>N2</th><th>P4</th><th>DEL.JH</th><th>Total.Del</th><th>Total.N</th><th>Total.P</th><thead></tr><tbody>" >> $outputFile
+	while IFS=, read Sample unique VHDEL P1 N1 P2 DELDH DHDEL P3 N2 P4 DELJH TotalDel TotalN TotalP
+	do
+		echo "<tr><td>$Sample</td><td>$unique</td><td>$VHDEL</td><td>$P1</td><td>$N1</td><td>$P2</td><td>$DELDH</td><td>$DHDEL</td><td>$P3</td><td>$N2</td><td>$P4</td><td>$DELJH</td><td>$TotalDel</td><td>$TotalN</td><td>$TotalP</td></tr>" >> $outputFile
+	done < $outputDir/junctionAnalysis.csv
+	echo "</tbody></table></div>" >> $outputFile
+fi
+
+echo "<div class='tabbertab' title='Comparison'><table border='1'><tr><th>ID</th><th>Include</th></tr>" >> $outputFile
+for sample in $samples; do
+	echo "<tr><td>$sample</td><td><input type='checkbox' onchange=\"javascript:compareAdd('$sample')\" id='compare_checkbox_$sample'/></td></tr>" >> $outputFile
+done
+echo "</table><div name='comparisonarea'>" >> $outputFile
+echo "<table><tr id='comparison_table_vd'></tr></table>" >> $outputFile
+echo "<table><tr id='comparison_table_vj'></tr></table>" >> $outputFile
+echo "<table><tr id='comparison_table_dj'></tr></table>" >> $outputFile
+echo "</div></div>" >> $outputFile
+
+echo "<div class='tabbertab' title='Downloads'>" >> $outputFile
+echo "<table border='1'>" >> $outputFile
+echo "<tr><th>Description</th><th>Link</th></tr>" >> $outputFile
+echo "<tr><td>The dataset used to generate the frequency graphs and the heatmaps (Unique based on clonaltype, $clonalType)</td><td><a href='allUnique.csv'>Download</a></td></tr>" >> $outputFile
+echo "<tr><td>The dataset used to calculate clonality score (Unique based on clonaltype, $clonalType)</td><td><a href='clonalityComplete.csv'>Download</a></td></tr>" >> $outputFile
+
+echo "<tr><td>The dataset used to generate the CDR3 length frequency graph</td><td><a href='CDR3LengthPlot.csv'>Download</a></td></tr>" >> $outputFile
+
+echo "<tr><td>The dataset used to generate the V gene family frequency graph</td><td><a href='VFFrequency.csv'>Download</a></td></tr>" >> $outputFile
+if [[ "$useD" == "true" ]] ; then
+	echo "<tr><td>The dataset used to generate the D gene family frequency graph</td><td><a href='DFFrequency.csv'>Download</a></td></tr>" >> $outputFile
+fi
+echo "<tr><td>The dataset used to generate the J gene family frequency graph</td><td><a href='JFFrequency.csv'>Download</a></td></tr>" >> $outputFile
+
+echo "<tr><td>The dataset used to generate the V gene frequency graph</td><td><a href='VFrequency.csv'>Download</a></td></tr>" >> $outputFile
+if [[ "$useD" == "true" ]] ; then
+	echo "<tr><td>The dataset used to generate the D gene frequency graph</td><td><a href='DFrequency.csv'>Download</a></td></tr>" >> $outputFile
+fi
+echo "<tr><td>The dataset used to generate the J gene frequency graph</td><td><a href='JFrequency.csv'>Download</a></td></tr>" >> $outputFile
+
+for sample in $samples; do
+	if [[ "$useD" == "true" ]] ; then
+		echo "<tr><td>The data used to generate the VD heatmap for $sample.</td><td><a href='HeatmapVD_$sample.csv'>Download</a></td></tr>" >> $outputFile
+	fi
+	echo "<tr><td>The data used to generate the VJ heatmap for $sample.</td><td><a href='HeatmapVJ_$sample.csv'>Download</a></td></tr>" >> $outputFile
+	if [[ "$useD" == "true" ]] ; then
+		echo "<tr><td>The data used to generate the DJ heatmap for $sample.</td><td><a href='HeatmapDJ_$sample.csv'>Download</a></td></tr>" >> $outputFile
+	fi
+done
+
+echo "</table>" >> $outputFile
+echo "</div></html>" >> $outputFile
--- a/r_wrapper_b.sh	Mon Sep 08 04:24:04 2014 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,156 +0,0 @@
-#!/bin/bash
-
-inputFile=$1
-outputDir=$3
-outputFile=$3/index.html #$2
-clonalType=$4
-species=$5
-locus=$6
-selection=$7
-
-useD="false"
-if [[ "$species" == "human" && "$locus" = "igh" ]] ; then
-	useD="true"
-fi
-dir="$(cd "$(dirname "$0")" && pwd)"
-mkdir $3
-Rscript --verbose $dir/RScript_b.r $inputFile $outputDir $outputDir $clonalType $species $locus $selection 2>&1
-cp $dir/tabber.js $outputDir
-cp $dir/style.css $outputDir
-cp $dir/script.js $outputDir
-cp $dir/jquery-1.11.0.min.js $outputDir
-cp $dir/jquery.tablesorter.min.js $outputDir
-cp $dir/asc.gif $outputDir
-cp $dir/desc.gif $outputDir
-cp $dir/bg.gif $outputDir
-samples=`cat $outputDir/samples.txt`
-echo "<html><center><h1><a href='index.html'>Click here for the results</a></h1>Tip: Open it in a new tab (middle mouse button or right mouse button -> 'open in new tab' on the link above)</center></html>" > $2
-echo "<html><head><title>Report on:" >> $outputFile
-for sample in $samples; do
-	echo " $sample" >> $outputFile
-done
-echo "</title><script type='text/javascript' src='jquery-1.11.0.min.js'></script>" >> $outputFile
-echo "<script type='text/javascript' src='tabber.js'></script>" >> $outputFile
-echo "<script type='text/javascript' src='script.js'></script>" >> $outputFile
-echo "<script type='text/javascript' src='jquery.tablesorter.min.js'></script>" >> $outputFile
-echo "<link rel='stylesheet' type='text/css' href='style.css'></head>" >> $outputFile
-echo "<div class='tabber'><div class='tabbertab' title='Gene frequencies'>" >> $outputFile
-
-echo "<img src='CDR3LengthPlot.png'/><br />" >> $outputFile
-echo "<img src='VFPlot.png'/>" >> $outputFile
-if [[ "$useD" == "true" ]] ; then
-	echo "<img src='DFPlot.png'/>" >> $outputFile
-fi
-echo "<img src='JFPlot.png'/>" >> $outputFile
-echo "<img src='VPlot.png'/>" >> $outputFile
-if [[ "$useD" == "true" ]] ; then
-	echo "<img src='DPlot.png'/>" >> $outputFile
-fi
-echo "<img src='JPlot.png'/></div>" >> $outputFile
-
-count=1
-echo "<div class='tabbertab' title='Heatmaps'><div class='tabber'>" >> $outputFile
-for sample in $samples; do
-	echo "<div class='tabbertab' title='$sample'><table border='1'><tr>" >> $outputFile
-	if [[ "$useD" == "true" ]] ; then
-		echo "<td><img src='HeatmapVD_$sample.png'/></td>" >> $outputFile
-	fi
-	echo "<td><img src='HeatmapVJ_$sample.png'/></td>" >> $outputFile
-	if [[ "$useD" == "true" ]] ; then
-		echo "<td><img src='HeatmapDJ_$sample.png'/></td>" >> $outputFile
-	fi
-	echo "</tr></table></div>" >> $outputFile
-	count=$((count+1))
-done
-echo "</div></div>" >> $outputFile
-
-#echo "<div class='tabbertab' title='Interactive'><svg class='chart'></svg><script src='http://d3js.org/d3.v3.min.js'></script></div>" >> $outputFile
-
-hasReplicateColumn="$(if head -n 1 $inputFile | grep -q 'Replicate'; then echo 'Yes'; else echo 'No'; fi)"
-echo "$hasReplicateColumn"
-#if its a 'new' merged file with replicate info
-if [[ "$hasReplicateColumn" == "Yes" ]] ; then
-	echo "<div class='tabbertab' title='Clonality'><div class='tabber'>" >> $outputFile
-	for sample in $samples; do
-		clonalityScore="$(cat $outputDir/ClonalityScore_$sample.csv)"
-		echo "<div class='tabbertab' title='$sample'><table border='1'>" >> $outputFile
-		echo "<tr><td colspan='4'>Clonality Score: $clonalityScore</td></tr>" >> $outputFile
-
-		#replicate,reads,squared
-		echo "<tr><td>Replicate ID</td><td>Number of Reads</td><td>Reads Squared</td><td></td></tr>" >> $outputFile
-		while IFS=, read replicate reads squared
-		do
-			
-			echo "<tr><td>$replicate</td><td>$reads</td><td>$squared</td><td></td></tr>" >> $outputFile
-		done < $outputDir/ReplicateReads_$sample.csv
-		
-		#sum of reads and reads squared
-		while IFS=, read readsSum squaredSum
-			do
-				echo "<tr><td>Sum</td><td>$readsSum</td><td>$squaredSum</td></tr>" >> $outputFile
-		done < $outputDir/ReplicateSumReads_$sample.csv
-		
-		#overview
-		echo "<tr><td>Coincidence Type</td><td>Raw Coincidence Freq</td><td>Coincidence Weight</td><td>Coincidences, Weighted</td></tr>" >> $outputFile
-		while IFS=, read type count weight weightedCount
-		do
-			echo "<tr><td>$type</td><td>$count</td><td>$weight</td><td>$weightedCount</td></tr>" >> $outputFile
-		done < $outputDir/ClonalityOverView_$sample.csv
-		echo "</table></div>" >> $outputFile
-	done
-	echo "</div></div>" >> $outputFile
-fi
-
-hasJunctionData="$(if head -n 1 $inputFile | grep -q '3V-REGION trimmed-nt nb'; then echo 'Yes'; else echo 'No'; fi)"
-
-if [[ "$hasJunctionData" == "Yes" ]] ; then
-	echo "<div class='tabbertab' title='Junction Analysis'><table border='1' id='junction_table'  class='tablesorter'><thead><tr><th>Sample</th><th>unique</th><th>VH.DEL</th><th>P1</th><th>N1</th><th>P2</th><th>DEL.DH</th><th>DH.DEL</th><th>P3</th><th>N2</th><th>P4</th><th>DEL.JH</th><th>Total.Del</th><th>Total.N</th><th>Total.P</th><thead></tr><tbody>" >> $outputFile
-	while IFS=, read Sample unique VHDEL P1 N1 P2 DELDH DHDEL P3 N2 P4 DELJH TotalDel TotalN TotalP
-	do
-		echo "<tr><td>$Sample</td><td>$unique</td><td>$VHDEL</td><td>$P1</td><td>$N1</td><td>$P2</td><td>$DELDH</td><td>$DHDEL</td><td>$P3</td><td>$N2</td><td>$P4</td><td>$DELJH</td><td>$TotalDel</td><td>$TotalN</td><td>$TotalP</td></tr>" >> $outputFile
-	done < $outputDir/junctionAnalysis.csv
-	echo "</tbody></table></div>" >> $outputFile
-fi
-
-echo "<div class='tabbertab' title='Comparison'><table border='1'><tr><th>ID</th><th>Include</th></tr>" >> $outputFile
-for sample in $samples; do
-	echo "<tr><td>$sample</td><td><input type='checkbox' onchange=\"javascript:compareAdd('$sample')\" id='compare_checkbox_$sample'/></td></tr>" >> $outputFile
-done
-echo "</table><div name='comparisonarea'>" >> $outputFile
-echo "<table><tr id='comparison_table_vd'></tr></table>" >> $outputFile
-echo "<table><tr id='comparison_table_vj'></tr></table>" >> $outputFile
-echo "<table><tr id='comparison_table_dj'></tr></table>" >> $outputFile
-echo "</div></div>" >> $outputFile
-
-echo "<div class='tabbertab' title='Downloads'>" >> $outputFile
-echo "<table border='1'>" >> $outputFile
-echo "<tr><th>Description</th><th>Link</th></tr>" >> $outputFile
-echo "<tr><td>The dataset used to generate the frequency graphs and the heatmaps (Unique based on clonaltype, $clonalType)</td><td><a href='allUnique.csv'>Download</a></td></tr>" >> $outputFile
-echo "<tr><td>The dataset used to calculate clonality score (Unique based on clonaltype, $clonalType)</td><td><a href='clonalityComplete.csv'>Download</a></td></tr>" >> $outputFile
-
-echo "<tr><td>The dataset used to generate the CDR3 length frequency graph</td><td><a href='CDR3LengthPlot.csv'>Download</a></td></tr>" >> $outputFile
-
-echo "<tr><td>The dataset used to generate the V gene family frequency graph</td><td><a href='VFFrequency.csv'>Download</a></td></tr>" >> $outputFile
-if [[ "$useD" == "true" ]] ; then
-	echo "<tr><td>The dataset used to generate the D gene family frequency graph</td><td><a href='DFFrequency.csv'>Download</a></td></tr>" >> $outputFile
-fi
-echo "<tr><td>The dataset used to generate the J gene family frequency graph</td><td><a href='JFFrequency.csv'>Download</a></td></tr>" >> $outputFile
-
-echo "<tr><td>The dataset used to generate the V gene frequency graph</td><td><a href='VFrequency.csv'>Download</a></td></tr>" >> $outputFile
-if [[ "$useD" == "true" ]] ; then
-	echo "<tr><td>The dataset used to generate the D gene frequency graph</td><td><a href='DFrequency.csv'>Download</a></td></tr>" >> $outputFile
-fi
-echo "<tr><td>The dataset used to generate the J gene frequency graph</td><td><a href='JFrequency.csv'>Download</a></td></tr>" >> $outputFile
-
-for sample in $samples; do
-	if [[ "$useD" == "true" ]] ; then
-		echo "<tr><td>The data used to generate the VD heatmap for $sample.</td><td><a href='HeatmapVD_$sample.csv'>Download</a></td></tr>" >> $outputFile
-	fi
-	echo "<tr><td>The data used to generate the VJ heatmap for $sample.</td><td><a href='HeatmapVJ_$sample.csv'>Download</a></td></tr>" >> $outputFile
-	if [[ "$useD" == "true" ]] ; then
-		echo "<tr><td>The data used to generate the DJ heatmap for $sample.</td><td><a href='HeatmapDJ_$sample.csv'>Download</a></td></tr>" >> $outputFile
-	fi
-done
-
-echo "</table>" >> $outputFile
-echo "</div></html>" >> $outputFile
--- a/r_wrapper_t.sh	Mon Sep 08 04:24:04 2014 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,151 +0,0 @@
-#!/bin/bash
-
-inputFile=$1
-outputDir=$3
-outputFile=$3/index.html #$2
-clonalType=$4
-species=$5
-locus=$6
-selection=$7
-useD="true"
-if [[ "$species" == "human" && "$locus" = "tra" ]] ; then
-	useD="false"
-fi
-dir="$(cd "$(dirname "$0")" && pwd)"
-mkdir $3
-Rscript --verbose $dir/RScript_t.r $inputFile $outputDir $outputDir $clonalType $species $locus $selection 2>&1
-cp $dir/tabber.js $outputDir
-cp $dir/style.css $outputDir
-cp $dir/script.js $outputDir
-cp $dir/jquery-1.11.0.min.js $outputDir
-cp $dir/jquery.tablesorter.min.js $outputDir
-cp $dir/asc.gif $outputDir
-cp $dir/desc.gif $outputDir
-cp $dir/bg.gif $outputDir
-echo "<html><center><h1><a href='index.html'>Click here for the results</a></h1>Tip: Open it in a new tab (middle mouse button or right mouse button -> 'open in new tab' on the link above)</center></html>" > $2
-echo "<html<head>" >> $outputFile
-echo "<script type='text/javascript' src='jquery-1.11.0.min.js'></script>" >> $outputFile
-echo "<script type='text/javascript' src='tabber.js'></script>" >> $outputFile
-echo "<script type='text/javascript' src='script.js'></script>" >> $outputFile
-echo "<script type='text/javascript' src='jquery.tablesorter.min.js'></script>" >> $outputFile
-echo "<link rel='stylesheet' type='text/css' href='style.css'></head>" >> $outputFile
-echo "<div class='tabber'><div class='tabbertab' title='Gene frequencies'>" >> $outputFile
-
-echo "<img src='CDR3LengthPlot.png'/><br />" >> $outputFile
-echo "<img src='VFPlot.png'/>" >> $outputFile
-if [[ "$useD" == "true" ]] ; then
-	echo "<img src='DFPlot.png'/>" >> $outputFile
-fi
-echo "<img src='JFPlot.png'/>" >> $outputFile
-echo "<img src='VPlot.png'/>" >> $outputFile
-if [[ "$useD" == "true" ]] ; then
-	echo "<img src='DPlot.png'/>" >> $outputFile
-fi
-echo "<img src='JPlot.png'/></div>" >> $outputFile
-
-samples=`cat $outputDir/samples.txt`
-count=1
-echo "<div class='tabbertab' title='Heatmaps'><div class='tabber'>" >> $outputFile
-for sample in $samples; do
-	echo "<div class='tabbertab' title='$sample'><table border='1'><tr>" >> $outputFile
-	if [[ "$useD" == "true" ]] ; then
-		echo "<td><img src='HeatmapVD_$sample.png'/></td>" >> $outputFile
-	fi
-	echo "<td><img src='HeatmapVJ_$sample.png'/></td>" >> $outputFile
-	if [[ "$useD" == "true" ]] ; then
-		echo "<td><img src='HeatmapDJ_$sample.png'/></td>" >> $outputFile
-	fi
-	echo "</tr></table></div>" >> $outputFile
-	count=$((count+1))
-done
-echo "</div></div>" >> $outputFile
-
-
-hasReplicateColumn="$(if head -n 1 $inputFile | grep -q 'Replicate'; then echo 'Yes'; else echo 'No'; fi)"
-echo "$hasReplicateColumn"
-#if its a 'new' merged file with replicate info
-if [[ "$hasReplicateColumn" == "Yes" ]] ; then
-	echo "<div class='tabbertab' title='Clonality'><div class='tabber'>" >> $outputFile
-	for sample in $samples; do
-		clonalityScore="$(cat $outputDir/ClonalityScore_$sample.csv)"
-		echo "<div class='tabbertab' title='$sample'><table border='1'>" >> $outputFile
-		echo "<tr><td colspan='4'>Clonality Score: $clonalityScore</td></tr>" >> $outputFile
-
-		#replicate,reads,squared
-		echo "<tr><td>Replicate ID</td><td>Number of Reads</td><td>Reads Squared</td><td></td></tr>" >> $outputFile
-		while IFS=, read replicate reads squared
-		do
-			
-			echo "<tr><td>$replicate</td><td>$reads</td><td>$squared</td><td></td></tr>" >> $outputFile
-		done < $outputDir/ReplicateReads_$sample.csv
-		
-		#sum of reads and reads squared
-		while IFS=, read readsSum squaredSum
-			do
-				echo "<tr><td>Sum</td><td>$readsSum</td><td>$squaredSum</td></tr>" >> $outputFile
-		done < $outputDir/ReplicateSumReads_$sample.csv
-		
-		#overview
-		echo "<tr><td>Coincidence Type</td><td>Raw Coincidence Freq</td><td>Coincidence Weight</td><td>Coincidences, Weighted</td></tr>" >> $outputFile
-		while IFS=, read type count weight weightedCount
-		do
-			echo "<tr><td><a href='ClonalitySequences_${sample}_${type}.csv'>$type</a></td><td>$count</td><td>$weight</td><td>$weightedCount</td></tr>" >> $outputFile
-		done < $outputDir/ClonalityOverView_$sample.csv
-		echo "</table></div>" >> $outputFile
-	done
-	echo "</div></div>" >> $outputFile
-fi
-
-hasJunctionData="$(if head -n 1 $inputFile | grep -q '3V-REGION trimmed-nt nb'; then echo 'Yes'; else echo 'No'; fi)"
-
-if [[ "$hasJunctionData" == "Yes" ]] ; then
-	echo "<div class='tabbertab' title='Junction Analysis'><table border='1' id='junction_table'  class='tablesorter'><thead><tr><th>Sample</th><th>unique</th><th>VH.DEL</th><th>P1</th><th>N1</th><th>P2</th><th>DEL.DH</th><th>DH.DEL</th><th>P3</th><th>N2</th><th>P4</th><th>DEL.JH</th><th>Total.Del</th><th>Total.N</th><th>Total.P</th><thead></tr><tbody>" >> $outputFile
-	while IFS=, read Sample unique VHDEL P1 N1 P2 DELDH DHDEL P3 N2 P4 DELJH TotalDel TotalN TotalP
-	do
-		echo "<tr><td>$Sample</td><td>$unique</td><td>$VHDEL</td><td>$P1</td><td>$N1</td><td>$P2</td><td>$DELDH</td><td>$DHDEL</td><td>$P3</td><td>$N2</td><td>$P4</td><td>$DELJH</td><td>$TotalDel</td><td>$TotalN</td><td>$TotalP</td></tr>" >> $outputFile
-	done < $outputDir/junctionAnalysis.csv
-	echo "</tbody></table></div>" >> $outputFile
-fi
-
-echo "<div class='tabbertab' title='Comparison'><table border='1'><tr><th>ID</th><th>Include</th></tr>" >> $outputFile
-for sample in $samples; do
-	echo "<tr><td>$sample</td><td><input type='checkbox' onchange=\"javascript:compareAdd('$sample')\" id='compare_checkbox_$sample'/></td></tr>" >> $outputFile
-done
-echo "</table><div name='comparisonarea'>" >> $outputFile
-echo "<table><tr id='comparison_table_vd'></tr></table>" >> $outputFile
-echo "<table><tr id='comparison_table_vj'></tr></table>" >> $outputFile
-echo "<table><tr id='comparison_table_dj'></tr></table>" >> $outputFile
-echo "</div></div>" >> $outputFile
-
-echo "<div class='tabbertab' title='Downloads'>" >> $outputFile
-echo "<table border='1'>" >> $outputFile
-echo "<tr><th>Description</th><th>Link</th></tr>" >> $outputFile
-echo "<tr><td>The dataset used to generate the frequency graphs and the heatmaps (Unique based on clonaltype, $clonalType)</td><td><a href='allUnique.csv'>Download</a></td></tr>" >> $outputFile
-echo "<tr><td>The dataset used to calculate clonality score (Unique based on clonaltype, $clonalType)</td><td><a href='clonalityComplete.csv'>Download</a></td></tr>" >> $outputFile
-
-echo "<tr><td>The dataset used to generate the CDR3 length frequency graph</td><td><a href='CDR3LengthPlot.csv'>Download</a></td></tr>" >> $outputFile
-
-echo "<tr><td>The dataset used to generate the V gene family frequency graph</td><td><a href='VFFrequency.csv'>Download</a></td></tr>" >> $outputFile
-if [[ "$useD" == "true" ]] ; then
-	echo "<tr><td>The dataset used to generate the D gene family frequency graph</td><td><a href='DFFrequency.csv'>Download</a></td></tr>" >> $outputFile
-fi
-echo "<tr><td>The dataset used to generate the J gene family frequency graph</td><td><a href='JFFrequency.csv'>Download</a></td></tr>" >> $outputFile
-
-echo "<tr><td>The dataset used to generate the V gene frequency graph</td><td><a href='VFrequency.csv'>Download</a></td></tr>" >> $outputFile
-if [[ "$useD" == "true" ]] ; then
-	echo "<tr><td>The dataset used to generate the D gene frequency graph</td><td><a href='DFrequency.csv'>Download</a></td></tr>" >> $outputFile
-fi
-echo "<tr><td>The dataset used to generate the J gene frequency graph</td><td><a href='JFrequency.csv'>Download</a></td></tr>" >> $outputFile
-
-for sample in $samples; do
-	if [[ "$useD" == "true" ]] ; then
-		echo "<tr><td>The data used to generate the VD heatmap for $sample.</td><td><a href='HeatmapVD_$sample.csv'>Download</a></td></tr>" >> $outputFile
-	fi
-	echo "<tr><td>The data used to generate the VJ heatmap for $sample.</td><td><a href='HeatmapVJ_$sample.csv'>Download</a></td></tr>" >> $outputFile
-	if [[ "$useD" == "true" ]] ; then
-		echo "<tr><td>The data used to generate the DJ heatmap for $sample.</td><td><a href='HeatmapDJ_$sample.csv'>Download</a></td></tr>" >> $outputFile
-	fi
-done
-
-echo "</table>" >> $outputFile
-echo "</div></html>" >> $outputFile
--- a/style.css	Mon Sep 08 04:24:04 2014 -0400
+++ b/style.css	Mon Jan 05 09:30:08 2015 -0500
@@ -108,43 +108,3 @@
  overflow:auto;
 }
 
-/* tables */
-table.tablesorter {
-	font-family:arial;
-	background-color: #CDCDCD;
-	margin:10px 0pt 15px;
-	font-size: 8pt;
-	width: 100%;
-	text-align: left;
-}
-table.tablesorter thead tr th, table.tablesorter tfoot tr th {
-	background-color: #e6EEEE;
-	border: 1px solid #FFF;
-	font-size: 8pt;
-	padding: 4px;
-}
-table.tablesorter thead tr .header {
-	background-image: url(bg.gif);
-	background-repeat: no-repeat;
-	background-position: center right;
-	cursor: pointer;
-}
-table.tablesorter tbody td {
-	color: #3D3D3D;
-	padding: 4px;
-	background-color: #FFF;
-	vertical-align: top;
-}
-table.tablesorter tbody tr.odd td {
-	background-color:#F0F0F6;
-}
-table.tablesorter thead tr .headerSortUp {
-	background-image: url(asc.gif);
-}
-table.tablesorter thead tr .headerSortDown {
-	background-image: url(desc.gif);
-}
-table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {
-background-color: #8dbdd8;
-}
-