
rm(list = ls())
library(rstudioapi)
###########################
# Reading the data
###########################

setwd(dirname(getActiveDocumentContext()$path ))
data <- read.csv("Figure 2-source data.csv", header=TRUE,stringsAsFactors=FALSE)

IDs = unique(data[,"ID"])
ytypes = unique(data[,"ytype_def"])
pchID = c(21,22,23,24,25,21,22,23,24,25,21,22,23,24,25,21,22,23,24,25,21,22)

coloresID = c("steelblue4","slateblue3","navy","lightsteelblue4","dodgerblue2", 
              "salmon4","indianred3","brown2","coral1","chocolate2",
              "aquamarine3","darkolivegreen3","cadetblue4","chartreuse3","forestgreen","seagreen3",
              "palegreen3","springgreen4","darkseagreen4","green4","greenyellow","lightgreen")

tstart=0
tend=27


nrows = 3
ncolumns =3

par(mfrow = c(nrows,ncolumns),     
    oma = c(0, 0, 0, 0), # 
    mar = c(4.0, 5.0, 0.0, 0.5), 
    mgp = c(3.0, 1, 0),    
    xpd = FALSE)

i=1

Slope_CCR5p= c()
Diff_CCR5p =c()

CCR5pwk =c()
CCR5nwk =c()
CD8wk =c()

for(ID in IDs){
  ##########################
  # 1. Reading T cell counts
  ##########################
  
  i = which(IDs %in% ID)
  
  #Cd4+CCR5+
  CD4CCR5pd = 10^as.numeric(data[which(ID==data[,"ID"]& data[,"ytype_def"]=="cd4+ccr5+"),"y"])
  tCD4CCR5pd = as.numeric(data[which(ID==data[,"ID"] & data[,"ytype_def"]=="cd4+ccr5+"),"time"])
  
 
  ##########################
  # 2. Plotting CCR5+
  ##########################
  
  xlabel=""
  
  if (i==1 ){
    ylabel = expression(paste("CD4"^"+"~"CCR5"^"+"~"T cells/",mu,"L"))
  }
  else{
    ylabel=""
  }
  
  # Plotting
  if (i==1 || i==6 || i==11){
    plot(tCD4CCR5pd/7,CD4CCR5pd,
         xlab=xlabel,ylim = range(10^0,15000),xlim =  c(tstart,tend),
         ylab=ylabel,bty="n",log="y",
         main="",yaxt="n",xaxt="n",
         cex.main=1.7,cex.axis=1.7,cex.lab=1.7,
         type="o",pch=pchID[i],bg=coloresID[i],
         lwd=1,cex=1.2)

    if (i==1) text(12,6500,"Control group",cex=1.6, font = 2)
    if (i==6) text(12,6500,"WT group",cex=1.6, font = 2)
    if (i==11) text(12,6500,expression(bold(Delta~"CCR5 group")),cex=1.6, font = 2)
    
    axis(1,at=axTicks(1),labels=as.character(axTicks(1)),cex.axis=1.4,font=2,lwd=2,las=1)
    axis(2,at=c(1,10,100,1000,10000),labels=expression(bold("1"),bold("10"),bold("10"^"2"),bold("10"^"3"),bold("10"^"4")),
         cex.axis=1.4,font=2,lwd=2)#,las=1)
    

  }
  else{
    points(tCD4CCR5pd/7,CD4CCR5pd,
           type="o",pch=pchID[i],bg=coloresID[i],
           lwd=1,cex=1.2)
    
    }
  
  ########################
  # Levels at week ~ 10
  ########################
  index0 = which.min(abs(tCD4CCR5pd-0))
  if(max(tCD4CCR5pd)>=(9*7)){
    index10 = which.min(abs(tCD4CCR5pd-10*7))}
  else{
    index10 = NA
  }
  
  #######################
  # Levels at week ~ 25
  ########################
  
  if(max(tCD4CCR5pd)>=(20*7) ){
    index25 = which.min(abs(tCD4CCR5pd-25*7))
  }
  else{
    index25 = NA
  }
  CCR5pwk = rbind(CCR5pwk,c(CD4CCR5pd[c(index0,index10,index25)]))

  
  
  ###############
  # upslopes
  ##############

  # Computing the slope For WT and Delta-CCR5 groups
  if (i>=6){
    changes = CD4CCR5pd[c(2:length(CD4CCR5pd))]-CD4CCR5pd[c(1:(length(CD4CCR5pd)-1))]
    for (kk in 1:length(changes)){
      if ( sum(changes[c(kk:length(changes))]) <=0){
        index2=kk
        break
      }
    }
    Slope_CCR5p =c(Slope_CCR5p,lm(log(CD4CCR5pd[1:index2]) ~ tCD4CCR5pd[1:index2])$coeff[[2]])
  }
}

  
  
Slope_CCR5n=c()
for(ID in IDs){
  ##########################
  # 1. Reading T cell counts
  ##########################
  
  i = which(IDs %in% ID)

  #Cd4+CCR5-
  CD4CCR5nd = 10^as.numeric(data[which(ID==data[,"ID"]& data[,"ytype_def"]=="cd4+ccr5-"),"y"])
  tCD4CCR5nd = as.numeric(data[which(ID==data[,"ID"] & data[,"ytype_def"]=="cd4+ccr5-"),"time"])

  
  ##########################
  # 2. Plotting CCR5-
  ##########################
  
  xlabel=""
  if (i==1 ){
    ylabel = expression(paste("CD4"^"+"~"CCR5"^"-"~"T cells/",mu,"L"))
  }
  else{
    ylabel=""
  }
  
  
  if (i==1 || i==6 || i==11){
    plot(tCD4CCR5nd/7,CD4CCR5nd,
         xlab=xlabel,ylim = range(10^0,15000),xlim =  c(tstart,tend),
         ylab=ylabel,bty="n",log="y",
         main="",yaxt="n",xaxt="n",
         
         cex.main=1.7,cex.axis=1.7,cex.lab=1.7,
         type="o",pch=pchID[i],bg=coloresID[i],
         lwd=1,cex=1.2)
    
    axis(2,at=c(1,10,100,1000,10000),labels=expression(bold("1"),bold("10"),bold("10"^"2"),bold("10"^"3"),bold("10"^"4")),
         cex.axis=1.4,font=2,lwd=2)#,las=1)
    axis(1,at=axTicks(1),labels=as.character(axTicks(1)),cex.axis=1.4,font=2,lwd=2,las=1)
    
  }
  else{
    points(tCD4CCR5nd/7,CD4CCR5nd,
           type="o",pch=pchID[i],bg=coloresID[i],
           lwd=1,cex=1.2)
  }
  
  ###############
  # upslopes
  ##############
  # 
  if (i>=6){
    Slope_CCR5n=c(Slope_CCR5n,lm(log(CD4CCR5nd[which(tCD4CCR5pd<=(25*7))]) ~ tCD4CCR5nd[which(tCD4CCR5pd<=(25*7))])$coeff[[2]])
  }
  
  index0 = which.min(abs(tCD4CCR5nd-0))
  
  ########################
  # Levels at week ~ 10
  ########################
  
  if(max(tCD4CCR5nd)>(9*7)){
    index10 = which.min(abs(tCD4CCR5nd-10*7))}
  else{
    index10 = NA
  }
  
  ########################
  # Levels at week ~ 25
  ########################
  
  if(max(tCD4CCR5nd)>(20*7) ){
    index25 = which.min(abs(tCD4CCR5nd-25*7))
  }
  else{
    index25 = NA
  }
  CCR5nwk = rbind(CCR5nwk,c(CD4CCR5nd[c(index0,index10,index25)]))
}

  
Slope_CD8= c()
Diff_CD8M =c()
Diff_CD8E =c()
for(ID in IDs){
  ##########################
  # 1. Reading T cell counts
  ##########################
  
  i = which(IDs %in% ID)
  
  #Cd8+
  CD8d = 10^as.numeric(data[which(ID==data[,"ID"]& data[,"ytype_def"]=="cd8+"),"y"])
  tCD8d = as.numeric(data[which(ID==data[,"ID"] & data[,"ytype_def"]=="cd8+"),"time"])
 
  ##########################
  # 2. Plotting CD8+
  ##########################
  
  xlabel=expression("weeks after transplant")

  if (i==1 ){
    ylabel = expression(paste("CD8"^"+"~"T cells/",mu,"L"))
  }
  else{
    ylabel=""
  }
  
  
  if (i==1 || i==6 || i==11){
    plot(tCD8d/7,CD8d,
         xlab=xlabel,ylim = range(10^0,15000),xlim =  c(tstart,tend),
         ylab=ylabel,bty="n",log="y",
         main="",yaxt="n",xaxt="n",
         cex.main=1.7,cex.axis=1.7,cex.lab=1.7,
         type="o",pch=pchID[i],bg=coloresID[i],
         lwd=1,cex=1.2)
    
    aty <- axTicks(2)
    labels <- sapply(aty,function(k){
      if (log10(k)%%1==0){
        toString(k)
      }
      else{
        paste("")
      }}
    )
    
    axis(2,at=c(1,10,100,1000,10000),labels=expression(bold("1"),bold("10"),bold("10"^"2"),bold("10"^"3"),bold("10"^"4")),
         cex.axis=1.4,font=2,lwd=2)#,las=1)
    axis(1,at=axTicks(1),labels=as.character(axTicks(1)),cex.axis=1.4,font=2,lwd=2,las=1)
    
  }
  else{
    points(tCD8d/7,CD8d,
           type="o",pch=pchID[i],bg=coloresID[i],
           lwd=1,cex=1.2)
    }
  

  ###############
  # upslopes
  ##############
  if (i>=6){

    index2=which.min(abs(tCD8d-5*7))
    Slope_CD8 =c(Slope_CD8,lm(log(CD8d[1:index2]) ~ tCD8d[1:index2])$coeff[[2]])
  
  }
  ########################
  # Levels at week ~ 10
  ########################
  index0 = which.min(abs(tCD8d-0))
  if(max(tCD8d)>(9*7)){
    index10 = which.min(abs(tCD8d-10*7))}
  else{
    index10 = NA
  }
  
  ########################
  # Levels at week ~ 25
  ########################
  
  if(max(tCD8d)>(20*7) ){
    index25 = which.min(abs(tCD8d-25*7))
  }
  else{
    index25 = NA
  }
  CD8wk = rbind(CD8wk,c(CD8d[c(index0,index10,index25)]))
}
  
  ################
  # Boxplots
  ################
  
  nrows = 1
  ncolumns =4
  
  par(mfrow = c(nrows,ncolumns),     
      oma = c(0, 0, 0, 0), # 
      mar = c(4.0, 4.9, 0.0, 0.5), 
      mgp = c(2.9, 1, 0),    
      xpd = FALSE)
  
 

  ####################
  # CCR5+ for groups
  ####################

  
  boxplot(CCR5pwk[1:5,1],
          CCR5pwk[6:10,1],
          CCR5pwk[11:22,1],
          
          CCR5pwk[1:5,2],
          CCR5pwk[6:10,2],
          CCR5pwk[11:22,2],
          
          CCR5pwk[1:5,3],
          CCR5pwk[6:10,3],
          CCR5pwk[11:22,3],
          
          ylim=c(1,1500),frame=F,log="y",boxwex=0.5,
          boxlty = 1, whisklty = 1, staplelwd = 1, outpch = 3, outcex = 0, medcol = "gray1",
          xlab ="Weeks after transplant", ylab=expression(paste("CD4"^"+"~"\nCCR5"^"+"," T cells/",mu,"L")),
          xaxt = 'n',yaxt="n",bty="n",

          col=c("steelblue","indianred1","seagreen3",
                "steelblue","indianred1","seagreen3",
                "steelblue","indianred1","seagreen3"),

          #col="white",
          outline = FALSE,
          cex.main=1.4,cex.axis=1.4,cex.lab=1.4)#,
  abline(v=c(3.5,6.5),lty=2,col="gray35")
  
  axis(2,at=c(1,10,100,1000),labels=c("1","10","100","1000"),cex.axis=1.4,lwd=2,font=2)
  axis(1,at=c(2,5,8),labels=c("0","10", "25"),cex.axis=1.4,lwd=2,font=2)
  
  
  # MW Tests with Bonferroni correction
  T0C = pairwise.wilcox.test(c(CCR5pwk[1:5,1], CCR5pwk[6:10,1],CCR5pwk[11:22,1]),
                             c(rep(1,5),rep(2,5),rep(3,12)), 
                             p.adjust.method = "bonferroni")
  text(2,5,"*",cex=1.2) # T0C[2,1]<0.05
  text(3,16,"***",cex=1.2)# T0C[3,1]<0.001
  
  T10C = pairwise.wilcox.test(c(CCR5pwk[1:5,2], CCR5pwk[6:10,2],CCR5pwk[11:22,2]),
                             c(rep(1,5),rep(2,5),rep(3,12)), 
                             p.adjust.method = "bonferroni")
  
  T25C = pairwise.wilcox.test(c(CCR5pwk[1:5,3], CCR5pwk[6:10,3],CCR5pwk[11:22,3]),
                             c(rep(1,5),rep(2,5),rep(3,12)), 
                             p.adjust.method = "bonferroni")

  
  ####################
  # CCR5- for groups
  ####################

  boxplot(CCR5nwk[1:5,1],
          CCR5nwk[6:10,1],
          CCR5nwk[11:22,1],
          
          CCR5nwk[1:5,2],
          CCR5nwk[6:10,2],
          CCR5nwk[11:22,2],
          
          CCR5nwk[1:5,3],
          CCR5nwk[6:10,3],
          CCR5nwk[11:22,3],
          
          ylim=c(10,1500),frame=F,log="y",boxwex=0.5,
          boxlty = 1, whisklty = 1, staplelwd = 1, outpch = 3, outcex = 1, medcol = "gray1",
          xlab ="Weeks after transplant", ylab=expression(paste("CD4"^"+"~"\nCCR5"^"-"," T cells/",mu,"L")),
          xaxt = 'n',yaxt="n",bty="n",
          
          col=c("steelblue","indianred1","seagreen3",
                "steelblue","indianred1","seagreen3",
                "steelblue","indianred1","seagreen3"),
          #col="white",
          outline = FALSE,
          cex.main=1.4,cex.axis=1.4,cex.lab=1.4)#,
  abline(v=c(3.5,6.5),lty=2,col="gray35")
  axis(2,at=c(10,100,1000),labels=c("10","100","1000"),cex.axis=1.4,lwd=2,font=2)
  axis(1,at=c(2,5,8),labels=c("0","10", "25"),cex.axis=1.4,lwd=2,font=2)
  
  # MW Tests with Bonferroni correction
  T0Cn = pairwise.wilcox.test(c(CCR5nwk[1:5,1], CCR5nwk[6:10,1],CCR5nwk[11:22,1]),
                             c(rep(1,5),rep(2,5),rep(3,12)), 
                             p.adjust.method = "bonferroni")
  text(2,160,"*",cex=1.2)#T0Cn[2,1]<0.05
  text(3,120,"***",cex=1.2)#T0Cn[3,1]<0.001
  
  
  T10Cn = pairwise.wilcox.test(c(CCR5nwk[1:5,2], CCR5nwk[6:10,2],CCR5nwk[11:22,2]),
                              c(rep(1,5),rep(2,5),rep(3,12)), 
                              p.adjust.method = "bonferroni")
  text(5,270,"*",cex=1.2) #T10Cn[2,1]<0.05
  text(6,300,"**",cex=1.2)#T10Cn[3,1]<0.01
  
  T25Cn = pairwise.wilcox.test(c(CCR5nwk[1:5,3], CCR5nwk[6:10,3],CCR5nwk[11:22,3]),
                              c(rep(1,5),rep(2,5),rep(3,12)), 
                              p.adjust.method = "bonferroni")
 
  
  ####################
  # CD8+ for groups
  ####################
  
  boxplot(CD8wk[1:5,1],
          CD8wk[6:10,1],
          CD8wk[11:22,1],
          
          CD8wk[1:5,2],
          CD8wk[6:10,2],
          CD8wk[11:22,2],
          
          CD8wk[1:5,3],
          CD8wk[6:10,3],
          CD8wk[11:22,3],
          
          ylim=c(10,1500),frame=F,log="y",boxwex=0.5,
          boxlty = 1, whisklty = 1, staplelwd = 1, outpch = 3, outcex = 1, medcol = "gray1",
          xlab ="Weeks after transplant", ylab=expression(paste("CD8"^"+"," T cells/",mu,"L")),
          xaxt = 'n',yaxt="n",bty="n",
          
          col=c("steelblue","indianred1","seagreen3",
                "steelblue","indianred1","seagreen3",
                "steelblue","indianred1","seagreen3"),
          outline = FALSE,
          cex.main=1.4,cex.axis=1.4,cex.lab=1.4)#,
  abline(v=c(3.5,6.5),lty=2,col="gray35")

  
  axis(2,at=c(10,100,1000),labels=c("10","100","1000"),cex.axis=1.4,lwd=2,font=2)
  axis(1,at=c(2,5,8),labels=c("0","10", "25"),cex.axis=1.4,lwd=2,font=2)
  

  # MW Tests with Bonferroni correction
  T0C8 = pairwise.wilcox.test(c(CD8wk[1:5,1], CD8wk[6:10,1],CD8wk[11:22,1]),
                              c(rep(1,5),rep(2,5),rep(3,12)), 
                              p.adjust.method = "bonferroni")
  text(2,135,"*",cex=1.2) # T0C8[2,1]<0.05
  text(3,95,"***",cex=1.2)# T0C8[3,1]<0.001
  
  T10C8 = pairwise.wilcox.test(c(CD8wk[1:5,2], CD8wk[6:10,2],CD8wk[11:22,2]),
                               c(rep(1,5),rep(2,5),rep(3,12)), 
                               p.adjust.method = "bonferroni")
  text(6,1100,"***",cex=1.2) # T10C8[3,1]<0.01
  
  T25C8 = pairwise.wilcox.test(c(CD8wk[1:5,3], CD8wk[6:10,3],CD8wk[11:22,3]),
                               c(rep(1,5),rep(2,5),rep(3,12)), 
                               p.adjust.method = "bonferroni")
  
  ################
  # Upslopes
  ################
  
  
  boxplot(Slope_CCR5p[1:5],
          Slope_CCR5p[6:17],
          Slope_CCR5n[1:5],
          Slope_CCR5n[6:17],
          Slope_CD8[1:5],
          Slope_CD8[6:17],
          ylim=c(0.004,0.6),frame=F,boxwex=0.5,log="y",
          outline = FALSE,
          boxlty = 1, whisklty = 1, staplelwd = 1, outpch = 3, outcex = 1, medcol = "gray1",
          col=c("indianred1","seagreen3","indianred1","seagreen3","indianred1","seagreen3"),
          xlab ="", ylab=expression("Expansion rate (1/day)"),#expression(paste(Delta,"log(cells/",mu,"L)/",Delta,"day")),
          xaxt = 'n',yaxt="n",bty="n",
          cex.main=1.4,cex.axis=1.4,cex.lab=1.4)#,
  axis(2,at=c(0.004,0.01,0.03,0.1,0.3),labels=c("0.004","0.01","0.03","0.1","0.3"),cex.axis=1.4,lwd=2,font=2)
  axis(1,at=c(1.5,3.5,5.5),labels=expression(bold("CCR5"^"+"),bold("CCR5"^"-"),bold("CD8"^"+")),cex.axis=1.4,lwd=2)
  

  text(3.5,0.4,"T cell upslope \nin transplant groups",cex=1.2,font=2)
  mtext(expression(bold("CD4"^"+")), side=1, line=2.4, at=1.5,cex=0.9)
  mtext(expression(bold("CD4"^"+")), side=1, line=2.4, at=3.5,cex=0.9)
  
  SlopesALL = pairwise.wilcox.test(c(Slope_CCR5p, Slope_CCR5n,Slope_CD8),
                                  c(rep(1,18),rep(2,18),rep(3,18)), 
                                  p.adjust.method = "bonferroni",paired = TRUE)
  
  SlopesWT = pairwise.wilcox.test(c(Slope_CCR5p[1:6], Slope_CCR5n[1:6],Slope_CD8[1:6]),
                              c(rep(1,6),rep(2,6),rep(3,6)), 
                              p.adjust.method = "bonferroni",paired = TRUE)
  
  SlopesCCR5 = pairwise.wilcox.test(c(Slope_CCR5p[6:17], Slope_CCR5n[6:17],Slope_CD8[6:17]),
                                  c(rep(1,12),rep(2,12),rep(3,12)), 
                                  p.adjust.method = "bonferroni",paired = TRUE)
  
  