

library(RColorBrewer)
get_color<-function(mytypes,name="Paired")
{n<-length(mytypes)
if(name=="rainbow")
{ k<-n*1.3
mycolors<-rainbow(k)
mycolors<-mycolors[-as.vector(c(round(k*0.2)+1):(round(k*0.2)+(k-n)))]
mycolors<-setNames(mycolors,mytypes)
}

if(name%in%rownames(brewer.pal.info))
{maxn<-brewer.pal.info[name,"maxcolors"]
if(n<=maxn)
{mycolors<-setNames(brewer.pal(n,name),mytypes)
}else
{if(n<34){
  mycolors<-setNames(brewer.pal(maxn,name),mytypes[1:maxn])
  mycolors<-c(mycolors,
              setNames(c(brewer.pal(12,"Set3"),brewer.pal(8,"Dark2"),
                         brewer.pal(7,"Set2"),brewer.pal(7,"Accent")),
                       mytypes[(maxn+1):length(mytypes)]))
}else
{mycolors<-setNames(rainbow(n),mytypes)
}
}
}
return(mycolors)
}




options(stringsAsFactors = FALSE)

library(ggplot2)
library(reshape2)

outdir<-"C:/test/TCR"

myclone<-read.table(paste0(outdir,"/all_contigs.clone_unique_cell.addCDR3clone.tsv"),sep="\t",header=TRUE,
                    check.names=FALSE)
myclone$CDR3_Clone_ID<-sub("CDR3_","TCR_",myclone$CDR3_Clone_ID)
colors <- c(setNames(c(rainbow(30)[1:30],"white"),c("TCR_1",as.character(2:30),"other TCR")))

myplot1<-myclone[which(myclone$type%in%c("HN","HG")),]
myplot1<-myplot1[which((!is.na(myplot1$sample_name))&(!is.na(myplot1$CDR3_Clone))),]
myplot1$sample_name<-factor(myplot1$sample_name,levels=sort(unique(myplot1$sample_name)))
myplot1$CDR3<-as.character(myplot1$CDR3_Clone_ID)
lines<-which(as.numeric(sub(".+_","",myplot1$CDR3_Clone_ID))>30)
myplot1[lines,"CDR3"]<-"other TCR"
lines<-which(myplot1$CDR3_Clone_ID!="TCR_1")
myplot1[lines,"CDR3"]<-sub(".+_","",myplot1[lines,"CDR3"])
myplot1$CDR3<-factor(myplot1$CDR3,levels=unique(myplot1$CDR3))
myplot1<-table(myplot1[,c("sample_name","CDR3")])
myplot1<-dcast(as.data.frame(myplot1),sample_name~CDR3)
rownames(myplot1)<-myplot1[,1];myplot1<-myplot1[,-1];myplot1<-data.matrix(myplot1)
myplot1<-t(myplot1[,rev(colnames(myplot1))])
png(paste0(outdir,"/CDR3_circos.HNHG.png"),
    width     = 3.25,
    height    = 3.25,
    units     = "in",
    res       = 1200,
    pointsize = 4)
chordDiagram(myplot1, grid.col = c(colors,
                                   get_color(rev(colnames(myplot1)),"Paired")),
             transparency = 0.2,
             link.lwd = 0.5,    # Line width
             link.lty = 1,    # Line type
             link.border = 1)
circos.clear()
dev.off()
svg(paste0(outdir,"/CDR3_circos.HNHG.svg"))
chordDiagram(myplot1, grid.col = c(colors,
                                   get_color(rev(colnames(myplot1)),"Paired")),
             transparency = 0.2,
             link.lwd = 0.5,    # Line width
             link.lty = 1,    # Line type
             link.border = 1)
circos.clear()
dev.off()



myplot1<-myclone[which(myclone$type%in%c("CN","CG")),]
myplot1<-myplot1[which((!is.na(myplot1$sample_name))&(!is.na(myplot1$CDR3_Clone))),]
myplot1$sample_name<-factor(myplot1$sample_name,levels=sort(unique(myplot1$sample_name)))
myplot1$CDR3<-as.character(myplot1$CDR3_Clone_ID)
lines<-which(as.numeric(sub(".+_","",myplot1$CDR3_Clone_ID))>30)
myplot1[lines,"CDR3"]<-"other TCR"
lines<-which(myplot1$CDR3_Clone_ID!="TCR_1")
myplot1[lines,"CDR3"]<-sub(".+_","",myplot1[lines,"CDR3"])
myplot1$CDR3<-factor(myplot1$CDR3,levels=unique(myplot1$CDR3))
myplot1<-table(myplot1[,c("sample_name","CDR3")])
myplot1<-dcast(as.data.frame(myplot1),sample_name~CDR3)
rownames(myplot1)<-myplot1[,1];myplot1<-myplot1[,-1];myplot1<-data.matrix(myplot1)
myplot1<-t(myplot1[,rev(colnames(myplot1))])
png(paste0(outdir,"/CDR3_circos.CNCG.png"),
    width     = 3.25,
    height    = 3.25,
    units     = "in",
    res       = 1200,
    pointsize = 4)
chordDiagram(myplot1, 
             grid.col = c(colors,get_color(rev(colnames(myplot1)),"Paired")),
             transparency = 0.2,
             link.lwd = 0.5,    # Line width
             link.lty = 1,    # Line type
             link.border = 1)
circos.clear()
dev.off()
svg(paste0(outdir,"/CDR3_circos.CNCG.svg"))
chordDiagram(myplot1, grid.col = c(colors,
                                   get_color(rev(colnames(myplot1)),"Paired")),
             transparency = 0.2,
             link.lwd = 0.5,    # Line width
             link.lty = 1,    # Line type
             link.border = 1)
circos.clear()
dev.off()


