writecmd <- function(data.C , data.T , dis_Chr , data.L , data.N , colorChr , tra_Margin , labels_inf = NULL , labelChr , tra_hmap_typcolhmap , tra_border = "" , tra_heatcol_dis , tra_heat_heatcoldiscus ,
                    trackChr , tratype ,  chr_height , datatypeChr , heightTra , source_data , tra_poi_poisiz , heatmapcols , tra_bgcol , gap.width , tra_yaxis,
                    tra_hmap_poslines , tra_hmap_poslinhei , tra_hmap_cellbord = "" , tra_hmap_cellbord_col , tra_hmap_heatmapcol , plotsize , outergap , rect_gra_3Col = c("red","blue","green") ,
                    tra_rect_rectcol , tra_trct_colrect , tra_rect_rectcoldis , tra_rect_rectcoldiscus , tra_transparency , tra_coltype , tra_colcol ,
                    tra_colorcus , tra_line_fillarea = "" , tra_poipch , tra_colorline , tra_baseline , outAxis , outAxis_size , labelChr_size , tra_bar_direction ,
                    tra_bar_Boundary , tra_bar_coldir1 , tra_bar_coldir2 , tra_line_selrea , tra_bar_borderarea , colformatLinks , colorLinks ,
                    selcolorLinks , gracolinks , transparencyLinks , legendpos , addlegend , hlt_data , midplot , trapos , trac_index , 
                    chrfil = "",  trafil = "" ,  labfil = "" , linfil = ""
                    ){
  dir.create("./script")
  setwd("./script")
  dir.create("./chromosome")
  dir.create("./track")
  dir.create("./label")
  dir.create("./links")
  cat('## Thank you for using shinyCircos- v2.0 (https://venyao.xyz/shinyCircos/)',file="code.R",append=TRUE,sep="\n")
  cat('## This file is automatically generated by shinyCircos-V2.0. It is not recommended that you change it without understanding it',file="code.R",append=TRUE,sep="\n")
  cat("",file="code.R",append=TRUE,sep="\n")
  cat("",file="code.R",append=TRUE,sep="\n")
  cat('## Please put your chromsome data at "./chromosome/", unless they are empty',file="code.R",append=TRUE,sep="\n")
  cat('## Please put your chromsome data at "./chromosome/", unless they are empty',file="code.R",append=TRUE,sep="\n")
  cat('## Please put your track data at "./track/", unless they are empty',file="code.R",append=TRUE,sep="\n")
  cat('## Please put your label data at "./label/", unless they are empty',file="code.R",append=TRUE,sep="\n")
  cat('## Please put your links data at "./links/", unless they are empty',file="code.R",append=TRUE,sep="\n")
  cat('## Please make sure that the folder you specified contains the following four folders, unless they are empty',file="code.R",append=TRUE,sep="\n")
  cat('## Please donâ€™t add or subtract the number of files at will, otherwise there may be problems',file="code.R",append=TRUE,sep="\n")
  cat("",file="code.R",append=TRUE,sep="\n")
  cat("",file="code.R",append=TRUE,sep="\n")
  cat("",file="code.R",append=TRUE,sep="\n")
  cat("",file="code.R",append=TRUE,sep="\n")
  
  cat('setwd("D:/script/") #Please specify the folder!!!',file="code.R",append=TRUE,sep="\n")
  cat("
  options(warn=-1)
  library(circlize)
  library(RColorBrewer)
  library(data.table)
  library(gridBase)
  library(ComplexHeatmap)
  library(randomcoloR)",file="code.R",append=TRUE,sep="\n")  
  cat("",file="code.R",append=TRUE,sep="\n")
  
  cat("",file="code.R",append=TRUE,sep="\n")
  
  
  
  if(!is.null(data.C)){
    
    if("add" %in% data.C[,1]){
      data.C <<- data.C[-which(data.C[,1] == "add"),]
    }
    fwrite(data.C,"./chromosome/data.C.csv")
  }
  if(!is.null(data.T)){
    tratype <- unlist(tratype)
    ordertrapos <- order(trapos)
    for (i in 1:length(data.T)) {
      data.TT <- data.T[[ordertrapos[i]]]
      fwrite(data.TT,paste0("./track/data.T",i,".csv"))
    }
  }else{
    cat("
  data.T <- NULL",file="code.R",append=TRUE,sep="\n")
  }
  if(!is.null(data.N)){
    labpos <- labels_inf[,1]
    orderlabpos <- order(labpos)
    for (i in 1:length(data.N)) {
      data.NN <- data.N[[orderlabpos[i]]]
      fwrite(data.NN,paste0("./label/data.N",i,".csv"))
    }
  }else{
    cat("
  data.N <- NULL",file="code.R",append=TRUE,sep="\n")
  }
  if(!is.null(data.L)){
    fwrite(data.L,"./links/data.L.csv")
  }else{
    cat("
  data.L <- NULL",file="code.R",append=TRUE,sep="\n")
  }
  
  
  cat(
    paste0(
      '
  data.C <- as.data.frame(fread("./chromosome/data.C.csv", stringsAsFactors = F,data.table = FALSE))
  filenameT <- list.files("./track/")
  if(length(filenameT) != 0){
    data.T <- lapply(1:length(filenameT), function(x) {
                as.data.frame(fread(paste0("./track/",filenameT[x])), stringsAsFactors = F)
              })
  }
  filenameL <- list.files("./links/")
  if(length(filenameL) != 0){
    data.L <- as.data.frame(fread("./links/data.L.csv", stringsAsFactors = F))
  }
  filenameN <- list.files("./label/")
  if(length(filenameN) != 0){
    data.N <- lapply(1:length(filenameN), function(x) {
                as.data.frame(fread(paste0("./label/",filenameN[x])), stringsAsFactors = F)
              })
  }
  dis_Chr <- ',dis_Chr,'
  
  colorChr <- ',paste0("c('",paste0(colorChr,collapse = "','"),"')"),'
  tra_Margin <- ',paste0("c(",paste0(unlist(tra_Margin),collapse = ","),")"),'
  '
    ),file="code.R",append=TRUE,sep="\n"
  )
  if(!is.null(labels_inf)){
    cat(
      paste0(
        '
  labels_inf <- ',paste0("data.frame(col1=c(",paste0(labels_inf[,1],collapse = ","),"),col2 =c('",paste0(labels_inf[,2],collapse = "','"),"'),col3 =c(",paste0(labels_inf[,3],collapse = ","),"),col4 =c('",paste0(labels_inf[,4],collapse = "','"),"'),col5 =c(",paste0(labels_inf[,5],collapse = ","),"),col6 =c(",paste0(labels_inf[,6],collapse = ","),"),col7 =c(",paste0(labels_inf[,7],collapse = ","),"))"),'
  labels_inf[,c(1,3,5,6,7)] <- apply(labels_inf[,c(1,3,5,6,7)],c(1,2),as.numeric)
  '
      ),file="code.R",append=TRUE,sep="\n"
    )
  }
  cat(
    paste0(
      '
  labelChr <- ',labelChr,'
  tra_hmap_typcolhmap <- ',paste0("c('",paste0(unlist(tra_hmap_typcolhmap),collapse = "','"),"')"),'
  tra_border <- ',paste0("c('",paste0(unlist(tra_border),collapse = "','"),"')"),'
  tra_heatcol_dis <- ',paste0("c('",paste0(unlist(tra_heatcol_dis),collapse = "','"),"')"),'
  tra_heat_heatcoldiscus <- ',paste0("c('",paste0(unlist(tra_heat_heatcoldiscus),collapse = "','"),"')"),'
  trackChr <- ',paste0('"',trackChr,'"'),'
  tratype <- ',paste0("c('",paste0(unlist(tratype),collapse = "','"),"')"),'
  chr_height <- ',chr_height,'
  datatypeChr <- ',datatypeChr,'
  heightTra <- ',paste0("c(",paste0(unlist(heightTra),collapse = ","),")"),'
  source_data <- "',paste0(source_data,'"'),'
  tra_poi_poisiz <- ',paste0("c(",paste0(tra_poi_poisiz,collapse = ","),")"),'
  heatmapcols <- ',paste0("c('",paste0(unlist(heatmapcols),collapse = "','"),"')"),'
  tra_bgcol <- ',paste0("c('",paste0(unlist(tra_bgcol),collapse = "','"),"')"),'
  gap.width <- ',paste0("c(",paste0(gap.width,collapse = ","),")"),'
  tra_yaxis <- ',paste0("c(",paste0(tra_yaxis,collapse = ","),")"),'
  tra_hmap_poslines <- ',paste0("c('",paste0(unlist(tra_hmap_poslines),collapse = "','"),"')"),'
  tra_hmap_poslinhei <- ',paste0("c(",paste0(unlist(tra_hmap_poslinhei),collapse = ","),")"),'
  tra_hmap_cellbord <- ',paste0("c('",paste0(unlist(tra_hmap_cellbord),collapse = "','"),"')"),'
  tra_hmap_cellbord_col <- ',paste0("c('",paste0(unlist(tra_hmap_cellbord_col),collapse = "','"),"')"),'
  tra_hmap_heatmapcol <- ',paste0("c(",paste0(unlist(tra_hmap_heatmapcol),collapse = ","),")"),'
  plotsize <- ',paste0("c(",paste0(plotsize,collapse = ","),")"),'
  outergap <- ',outergap,'
  rect_gra_3Col <- ',paste0("c('",paste0(unlist(rect_gra_3Col),collapse = "','"),"')"),'#####
  tra_rect_rectcol <- ',paste0("c(",paste0(unlist(tra_rect_rectcol),collapse = ","),")"),'
  tra_trct_colrect <- ',paste0("c('",paste0(unlist(tra_trct_colrect),collapse = "','"),"')"),'
  tra_rect_rectcoldis <- ',paste0("c('",paste0(unlist(tra_rect_rectcoldis),collapse = "','"),"')"),'
  tra_rect_rectcoldiscus <- ',paste0("c('",paste0(unlist(tra_rect_rectcoldiscus),collapse = "','"),"')"),'
  tra_transparency <- ',paste0("c(",paste0(unlist(tra_transparency),collapse = ","),")"),'
  tra_coltype <- ',paste0("c('",paste0(unlist(tra_coltype),collapse = "','"),"')"),'
  tra_colcol <- ',paste0("c('",paste0(unlist(tra_colcol),collapse = "','"),"')"),'
  tra_colorcus <- ',paste0("c('",paste0(unlist(tra_colorcus),collapse = "','"),"')"),'
  tra_line_fillarea <- ',paste0("c('",paste0(unlist(tra_line_fillarea),collapse = "','"),"')"),'
  tra_poipch <- ',paste0("c('",paste0(unlist(tra_poipch),collapse = "','"),"')"),'
  tra_colorline <- ',paste0("c('",paste0(unlist(tra_colorline),collapse = "','"),"')"),'
  tra_baseline <- ',paste0("c('",paste0(unlist(tra_baseline),collapse = "','"),"')"),'
  outAxis <- ',outAxis,'
  outAxis_size <- ',outAxis_size,'
  labelChr_size <- ',labelChr_size,'
  tra_bar_direction <- ',paste0("c(",paste0(unlist(tra_bar_direction),collapse = ","),")"),'
  tra_bar_Boundary <- ',paste0("c('",paste0(unlist(tra_bar_Boundary),collapse = "','"),"')"),'
  tra_bar_coldir1 <- ',paste0("c('",paste0(unlist(tra_bar_coldir1),collapse = "','"),"')"),'
  tra_bar_coldir2 <- ',paste0("c('",paste0(unlist(tra_bar_coldir2),collapse = "','"),"')"),'
  tra_line_selrea <- ',paste0("c(",paste0(unlist(tra_line_selrea),collapse = ","),")"),'
  tra_bar_borderarea <- ',paste0("c('",paste0(unlist(tra_bar_borderarea),collapse = "','"),"')"),'
  colformatLinks <- ',colformatLinks,'#########
  colorLinks <- ',colorLinks,'
  selcolorLinks <- ',paste0("c('",paste0(unlist(selcolorLinks),collapse = "','"),"')"),'
  gracolinks <- ',paste0("c('",paste0(unlist(gracolinks),collapse = "','"),"')"),'
  transparencyLinks <- ',transparencyLinks,'
  legendpos <- ',paste0("c('",paste0(unlist(legendpos),collapse = "','"),"')"),'
  addlegend <- ',paste0("c('",paste0(unlist(addlegend),collapse = "','"),"')"),'
  '
    ),file="code.R",append=TRUE,sep="\n"
  )
  if(!is.null(hlt_data)){
    cat(
      paste0(
        '
  hlt_data <- ',paste0("data.frame(chr=c('",paste0(hlt_data[,1],collapse = "','"),"'),start =c(",paste0(hlt_data[,2],collapse = ","),"),start =c(",paste0(hlt_data[,3],collapse = ","),"),start =c('",paste0(hlt_data[,4],collapse = "','"),"'))"),' #########data.frame(chr=hlt_data[,1],start=hlt_data[,2],end=hlt_data[,3],color=hlt_data[,4])
  '
      ),file="code.R",append=TRUE,sep="\n"
    )
  }
  cat(
    paste0(
      '
  midplot <- ',midplot,'
  trapos <- ',paste0("c(",paste0(unlist(trapos),collapse = ","),")"),'
  trac_index <- ',paste0("c('",paste0(unlist(trac_index),collapse = "','"),"')"),'
      '
    ),file="code.R",append=TRUE,sep="\n"
  )
  
  
  cat(
    paste0(
      '
pdf("./plot.pdf", width = ',plotsize[1],'/72, height = ',plotsize[2],'/72,onefile = FALSE)
convert_unit_in_canvas_coordinate = function(x, unit = c("mm", "cm", "inches")) {
  pin = par("pin")
  usr = par("usr")
  unit = match.arg(unit)
  pt_per_inche1 = (usr[2] - usr[1])/pin[1]
  pt_per_inche2 = (usr[4] - usr[3])/pin[2]
  if(abs(pt_per_inche1 - pt_per_inche2) > 1e-3) {
    warning_wrap("`convert_unit_in_data_coordinate()` only works when aspect of the coordinate is 1.")
  }
  inche_per_mm = 0.0393700787401575
  # length in the data coordinate
  if(unit == "inches") {
    len = x * pt_per_inche1
  } else if(unit == "mm") {
    len = x * pt_per_inche1 * inche_per_mm
  } else if(unit == "cm") {
    len = x * pt_per_inche1 * inche_per_mm * 10
  }
  return(len)
}
circos.genomicInitialize.new = function(
    data, 
    sector.names = NULL, 
    major.by = NULL,
    plotType = c("axis", "labels"), 
    tickLabelsStartFromZero = TRUE,
    axis.labels.cex = 0.4*par("cex"), 
    labels.cex = 0.8*par("cex"), 
    track.height = NULL, 
    labels_inf_chr =NULL,
    data.CN = NULL,
    outergap = 1,
    plotother = 0,
    ...) {
  outergap <- outergap*0.01
  if(is.factor(data[[1]])) {
    fa = levels(data[[1]])
  } else {
    fa = unique(data[[1]])
  }
  if(!is.null(sector.names)) {
    if(length(sector.names) != length(fa)) {
      stop_wrap("length of `sector.names` and length of sectors differ.")
    }
  } else {
    sector.names = fa
  }
  names(sector.names) = fa
  # calculate xlim
  x1 = tapply(data[[2]], data[[1]], min)[fa]
  x2 = tapply(data[[3]], data[[1]], max)[fa]
  
  op = circos.par("cell.padding")
  ow = circos.par("points.overflow.warning")
  circos.par(cell.padding = c(0, 0, 0, 0), points.overflow.warning = FALSE)
  circos.initialize(factor(fa, levels = fa), xlim = cbind(x1, x2), ...)
  
  if(circos.par$ring) {
    op = c(op[1], 0, op[3], 0)
    ow = FALSE
  }
  if(is.null(track.height)) {
    if(all(c("axis", "labels") %in% plotType)) {
      track.height = convert_unit_in_canvas_coordinate(1.5, "mm") + strheight("0", cex = axis.labels.cex) + 
        convert_unit_in_canvas_coordinate(0.5, "mm") + strheight("chr", cex = labels.cex) + strheight("chr", cex = 1)*outergap
    } else if("labels" %in% plotType) {
      track.height = strheight("chr", cex = 1)*outergap + strheight("chr", cex = labels.cex)
    } else if("axis" %in% plotType) {
      track.height = convert_unit_in_canvas_coordinate(1.5, "mm") + strheight("0", cex = axis.labels.cex)
    } else {
      track.height = convert_height(3, "mm")
    }
  }
  #label
  if(!is.null(labels_inf_chr)){
    if(labels_inf_chr[[2]]=="outside"){
      if(ncol(data.CN) == 4){
        circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex = (((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = labels_inf_chr[[4]] , padding = 0 , track.margin = c(0,0), side = "outside")
      }else{
        circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex = (((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = data.CN[,5] , padding = 0 , track.margin = c(0,0), side = "outside")
      }
    }
  }
  # axis and chromosome names
  if(any(plotType %in% c("axis", "labels"))) {
    circos.genomicTrackPlotRegion(data, ylim = c(0, 1), bg.border = NA, track.height = track.height,
                                  panel.fun = function(region, value, ...) {
                                    sector.index = get.cell.meta.data("sector.index")
                                    xlim = get.cell.meta.data("xlim")
                                    
                                    if(all(c("axis", "labels") %in% plotType)) {
                                      circos.genomicAxis(h = "bottom", major.by = major.by, tickLabelsStartFromZero = tickLabelsStartFromZero, labels.cex = axis.labels.cex)
                                      circos.text(mean(xlim), convert_y(1.5, "mm") + convert_y(strheight("chr", cex = 1), "canvas")*outergap + convert_y(strheight("chr", cex = axis.labels.cex), "canvas") + convert_y(0.5, "mm"), 
                                                  labels = sector.names[sector.index], cex = labels.cex, adj = c(0.5, 0), niceFacing = TRUE)
                                    } else if("labels" %in% plotType) {
                                      circos.text(mean(xlim), convert_y(strheight("chr", cex = 1), "canvas")*outergap , labels = sector.names[sector.index], cex = labels.cex, adj = c(0.5, 0), niceFacing = TRUE)
                                    } else if("axis" %in% plotType) {
                                      circos.genomicAxis(h = "bottom", major.by = major.by, tickLabelsStartFromZero = tickLabelsStartFromZero,labels.cex = axis.labels.cex)
                                    }
                                  }
    )
    if(plotother != 0){
      circos.updatePlotRegion(
        sector.index = data[,1][nrow(data)],
        track.index = get.current.track.index(),
        bg.border = NA,
        bg.col = NA
      )
    }
  }
  circos.par("cell.padding" = op, "points.overflow.warning" = ow)
  return(invisible(NULL))
}


legendplot <- function(tktype,data.TT,data.TT_old,i,legendpos,tkcolor,addlegend){
  if(addlegend =="Yes"){
    if(tktype == "heatmap-gradual"){
      break1 <- min(as.numeric(as.matrix(data.TT[,-c(1:3)])))
      break2 <- max(as.numeric(as.matrix(data.TT[,-c(1:3)])))
      midpoint <- (break1+break2)/2
      hmapcols <- unlist(hmapcols)
      f <- colorRamp2(breaks = c(break1, midpoint, break2), colors = hmapcols)
      if(legendpos == "Right"){
        return(
          Legend(
            col_fun = f,
            title = paste0("track",i)
          )
        )
      }else{
        return(
          Legend(
            col_fun = f,
            title = paste0("track",i),
            direction = "horizontal"
          )
        )
      }
    }else if(tktype == "rect-gradual"){
      f <- colorRamp2(breaks = c(min(data.TT[,4]), mean(data.TT[,4]), max(data.TT[,4])), colors = rectcols)
      if(legendpos == "Right"){
        return(
          Legend(
            col_fun = f,
            title = paste0("track",i)
          )
        )
      }else{
        return(
          Legend(
            col_fun = f,
            title = paste0("track",i),
            direction = "horizontal"
          )
        )
      }
    }else if(tktype == "rect-discrete"){
      lgdcol <- cbind(unique(data.TT_old[,4]),unique(data.TT[,4]))
      lgdcol <- lgdcol[order(lgdcol[,1]),]
      if(legendpos == "Right"){
        return(
          Legend(
            at = lgdcol[,1],
            legend_gp = gpar(fill = lgdcol[,2]),
            nrow = 6,
            title = paste0("track",i)
          )
        )
      }else{
        return(
          Legend(
            at = lgdcol[,1],
            legend_gp = gpar(fill = lgdcol[,2]),
            ncol = 4,
            by_row = TRUE,
            title = paste0("track",i),
            direction = "horizontal"
          )
        )
      }
    }else if(tktype == "heatmap-discrete"){
      lgdcol <- cbind(unique(c(t(data.TT_old[,4:length(data.TT_old)]))),unique(c(t(data.TT))))
      lgdcol <- lgdcol[order(lgdcol[,1]),]
      if(legendpos == "Right"){
        return(
          Legend(
            at = lgdcol[,1],
            legend_gp = gpar(fill = lgdcol[,2]),
            nrow = 6,
            title = paste0("track",i)
          )
        )
      }else{
        return(
          Legend(
            at = lgdcol[,1],
            legend_gp = gpar(fill = lgdcol[,2]),
            ncol = 4,
            by_row = TRUE,
            title = paste0("track",i),
            direction = "horizontal"
          )
        )
      }
      #}else if("stack" %in% colnames(data.TT_old)){
    }else if(tktype == "stack-point" | tktype == "stack-line"){
      lgdcol <- cbind(unique(data.TT_old[,4],fromLast = TRUE),tkcolor)
      lgdcol <- lgdcol[order(lgdcol[,1]),]
      if(legendpos == "Right"){
        return(
          Legend(
            at = lgdcol[,1],
            legend_gp = gpar(fill = lgdcol[,2]),
            nrow = 6,
            title = paste0("track",i)
          )
        )
      }else{
        Legend(
          at = lgdcol[,1],
          legend_gp = gpar(fill = lgdcol[,2]),
          ncol = 4,
          by_row = TRUE,
          title = paste0("track",i),
          direction = "horizontal"
        )
      }
    }
  }
}
   
heilab <- 0
if(!is.null(data.N)){
  heilab <- sum(as.numeric(labels_inf[,3]))
}
heihat <- 0
tramar <- 0
if(!is.null(data.T)){
  for (k in 1:length(data.T)) {
    if(tra_hmap_poslines[k] == "1"){
      if(tratype[k] == "heatmap-gradual"|tratype[k] =="heatmap-discrete"){
        heihat <- heihat + tra_hmap_poslinhei[k]
      }
    }
    tramar <- tramar + tra_Margin[k]
    
  }
}else{
  heightTra <- NULL
}
if(outAxis == 1 && labelChr == 1){
  plotTypes <- c("labels","axis")
}else if(outAxis == 1 && labelChr == 2){
  plotTypes <- "axis"
}else if(outAxis == 2 && labelChr == 1){
  plotTypes <- "labels"
}else{
  plotTypes <- NULL
}
allheight <- sum(heihat , tramar , dis_Chr , heilab , as.numeric(chr_height) , sum(as.numeric(unlist(heightTra))))
if(!is.null(data.L)){
  if(allheight > 0.7){
    if(!is.null(data.T)) {
      for (k in 1:length(data.T)) {
        if(tratype[k] == "heatmap-gradual"|tratype[k] =="heatmap-discrete"){
          tra_hmap_poslinhei[k] <- 0.7*tra_hmap_poslinhei[k]/allheight
        }
        heightTra[k] <- 0.7*heightTra[k]/allheight
        tra_Margin[k] <- 0.7*tra_Margin[k]/allheight
      }
    }
    dis_Chr <- 0.7*dis_Chr/allheight
    if(!is.null(data.N)){
      for (k in 1:length(data.N)){
        labels_inf[k,3] <- 0.7* as.numeric(labels_inf[k,3])/allheight
      }
    }
    chr_height <- 0.7*chr_height/allheight
  }
}else{
  if(allheight > 0.8){
    if(!is.null(data.T)){
      for (k in 1:length(data.T)) {
        if(tratype[k] == "heatmap-gradual"|tratype[k] =="heatmap-discrete"){
          tra_hmap_poslinhei[k] <- 0.8*tra_hmap_poslinhei[k]/allheight
        }
        heightTra[k] <- 0.8*heightTra[k]/allheight
        tra_Margin[k] <- 0.8*tra_Margin[k]/allheight
      }
    }
    dis_Chr <- 0.8*dis_Chr/allheight
    if(!is.null(data.N)){
      for (k in 1:length(data.N)){
        labels_inf[k,3] <- 0.8*as.numeric(labels_inf[k,3])/allheight
      }
    }
    chr_height <- 0.8*chr_height/allheight
  }
}
  
  
  
if(!is.null(data.L)){
  if(ncol(data.L)==6 | ncol(data.L)==7){
    data.L1 <- data.L[,1:3]
    data.L2 <- data.L[,4:6]
    data.L1[,2] <- as.numeric(data.L1[,2])
    data.L1[,3] <- as.numeric(data.L1[,3])
    data.L2[,2] <- as.numeric(data.L2[,2])
    data.L2[,3] <- as.numeric(data.L2[,3])	  
    data.L1$num <- 1:nrow(data.L1)
    data.L2$num <- 1:nrow(data.L2)
    rownames(data.L1) <- data.L1$num
    rownames(data.L2) <- data.L2$num
  }
}
grid.newpage()
lgdplot <- list()
if(addlegend == "Yes"){
  if(legendpos == "Right"){
      circle_size = unit(1, "snpc")
      pushViewport(
        viewport(
          x = 0, 
          y = 0.5,
          width = circle_size, 
          height = circle_size,
          just = c("left", "center")
        )
      )
      par(omi = gridOMI(),  new = TRUE)
    
  }else{
    
      circle_size = unit(1, "snpc")
      pushViewport(
        viewport(
          x = 0.5,
          y = 1,
          width = circle_size, 
          height = circle_size,
          just = c("center", "top")
        )
      )
      par(omi = gridOMI(),  new = TRUE)
    
  }
}else{
    circle_size = unit(1, "snpc")
    pushViewport(
      viewport(
        x = 0, 
        y = 0.5,
        width = circle_size, 
        height = circle_size,
        just = c("left", "center")
      )
    )
    par(omi = gridOMI(),  new = TRUE)
}
data.C[,2] <- as.numeric(data.C[,2])
data.C[,3] <- as.numeric(data.C[,3])
if(1 %in% unlist(tra_yaxis) | trac_index == "Yes"){
  if(ncol(data.C) == 3){
    print(sum(data.C[,3]))
    data.C <<- rbind(data.C,c("add",1,round(0.025*sum(data.C[,3]))))
  }else{
    kk <- tapply(data.C[,3],data.C[,1],max)
    data.C <<- rbind(data.C,c("add",1,round(0.025*sum(kk)),"1","1"))
  }
}
circos.clear()
    
if(!is.null(data.N)){
  ccc <- NULL
  for (k in 1:nrow(labels_inf)) {
    kkkk <- labels_inf[k,]
    kkkk[,c(1,3,5,6,7)] <- as.numeric(kkkk[,c(1,3,5,6,7)])
    if(kkkk[6] == 1){
      kkkk[7] = 1
    }else{
      kkkk[7] = kkkk[7]*0.01
    }
    ccc <- rbind(ccc,kkkk) 
  }
  labels_inf <- ccc
  
  
  
  
  
  if(0 %in% labels_inf[,1]){
    lsb_tra_index <- labels_inf[,1] %in% 0
    labels_inf_chr <- labels_inf[lsb_tra_index,]
    labels_inf_tra <- labels_inf[!lsb_tra_index,]
    data.CN <- data.N[[as.numeric(labels_inf_chr[[5]])]]
  }else{
    labels_inf_tra <- labels_inf
    labels_inf_chr <- NULL
  }
  lab_inf <- (1:length(data.T)) %in% labels_inf_tra[,1]
}else{
  labels_inf_chr <- NULL
}
    
## *** The gap width ***
repnumgap <- round(length(unique(data.C[,1]))/length(gap.width))+1
gap.width <- rep(gap.width, repnumgap)[1:length(unique(data.C[,1]))]
gap.width <- as.numeric(gap.width)
if(1 %in% unlist(tra_yaxis) | trac_index == "Yes"){
  if(1 %in% unlist(tra_yaxis) & trac_index == "Yes"){
    gap.width[(length(unique(data.C[,1]))-1):length(unique(data.C[,1]))] <- c(1,2)
    rotation <- 8
  }else if(1 %in% unlist(tra_yaxis) & trac_index != "Yes"){
    gap.width[(length(unique(data.C[,1]))-1):length(unique(data.C[,1]))] <- c(0.5,0.5)
    rotation <- 5
  }else{
    gap.width[(length(unique(data.C[,1]))-1):length(unique(data.C[,1]))] <- c(0.5,0.5)
    rotation <- 5
  }
}else{
  rotation <- gap.width[length(gap.width)]/2
}
repnumcol <- round(length(unique(data.C[,1]))/length(colorChr))+1
colorChr <- rep(colorChr, repnumcol)[1:length(unique(data.C[,1]))]



if(1 %in% unlist(tra_yaxis) | trac_index == "Yes"){
  if(1 %in% unlist(tra_yaxis) & trac_index != "Yes"){
    plotother <- 2.1
  }else if(!(1 %in% unlist(tra_yaxis)) & trac_index == "Yes"){
    plotother <- 1.2
  }else{
    plotother <- 2.2
  }
}else{
  plotother <- 0
}
if(source_data == "a"){ # upload
  
  if(datatypeChr == "1"){ # general
    if(trackChr == "track"){ #Chromosome band show
      #trackChr : "Show" = "track", "Hide" = ""
      
      
      circos.par("start.degree" = 90 - rotation , "gap.degree" = gap.width , cell.padding=c(0,0,0,0) , track.margin=c(0,0))
      circos.genomicInitialize.new(data.C,plotType = plotTypes , axis.labels.cex = outAxis_size , labels.cex = labelChr_size,data.CN = data.CN,labels_inf_chr = labels_inf_chr , outergap = outergap , plotother = plotother)
      if("labels" %in% plotTypes && !("axis" %in% plotTypes)){
        gapgap <- 0.02
      }else{
        gapgap <- 0
      }
  
      circos.genomicTrackPlotRegion(ylim = c(0, 1) , bg.col = colorChr, bg.border = NA , track.height = chr_height , track.margin = c(dis_Chr,gapgap))
  
      if(plotother != 0){
        circos.updatePlotRegion(
          sector.index = data.C[,1][nrow(data.C)],
          track.index = get.current.track.index(),
          bg.border = NA,
          bg.col = NA
        )
        if(plotother == 2.2){
          traxlim <- get.cell.meta.data("xlim", sector.index = get.current.sector.index(), track.index = get.current.track.index())
          circos.text(
            sector.index = get.current.sector.index(),
            track.index = get.current.track.index(),
            x = round((traxlim[2]-traxlim[1])/3),
            y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
            labels = 0,
            facing = "downward"
          )
        }else if(plotother == 1.2){
          circos.text(
            sector.index = get.current.sector.index(),
            track.index = get.current.track.index(),
            x = get.cell.meta.data("xcenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
            y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
            labels = 0,
            facing = "downward"
          )
        }
      }
  
      if(!is.null(labels_inf_chr)){
        if(labels_inf_chr[[2]]=="inside"){
          if(ncol(data.CN) == 4){
            circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex = (((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = labels_inf_chr[[4]] , padding = 0 , track.margin = c(0,0), side = "inside")
          }else{
            circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex = (((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = data.CN[,5] , padding = 0 , track.margin = c(0,0), side = "inside")
          }
          
        }
      }
    }else if(trackChr!="track"){
      circos.par("start.degree"=90-rotation, "gap.degree"=gap.width, cell.padding=c(0,0,0,0), track.margin=c(0,0))
      circos.genomicInitialize.new(data.C,plotType = plotTypes , axis.labels.cex = outAxis_size , labels.cex = labelChr_size,data.CN = data.CN,labels_inf_chr = labels_inf_chr , outergap = outergap , plotother = plotother)
      if(!is.null(labels_inf_chr)){
        if(labels_inf_chr[[2]]=="inside"){
          if(ncol(data.CN) == 4){
            circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex =(((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = labels_inf_chr[[4]] , padding = 0 , track.margin = c(0,0), side = "inside")
          }else{
            circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex =(((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = data.CN[,5] , padding = 0 , track.margin = c(0,0), side = "inside")
          }
        }
      }
    }
  }else if(datatypeChr == "2"){# cytoband
    circos.par("start.degree"=90-rotation, "gap.degree"=gap.width, cell.padding=c(0,0,0,0), track.margin=c(0,0))
    circos.genomicInitialize.new(data.C,plotType = plotTypes , axis.labels.cex = outAxis_size , labels.cex = labelChr_size,data.CN = data.CN,labels_inf_chr = labels_inf_chr , outergap = outergap , plotother = plotother)
    if("labels" %in% plotTypes && !("axis" %in% plotTypes)){
      gapgap <- 0.02
    }else{
      gapgap <- 0
    }
    circos.genomicTrackPlotRegion(data.C, ylim = c(0, 1), bg.border = NA, track.height = chr_height, track.margin = c(dis_Chr,gapgap) , panel.fun = function(region, value, ...){
      col = cytoband.col(value[[2]])
      circos.genomicRect(region, value, ybottom = 0, ytop = 1, col = col, border = NA, ...)
      xlim = get.cell.meta.data("xlim")
      circos.rect(xlim[1], 0, xlim[2], 1, border = "#000000")
    }, cell.padding = c(0, 0, 0, 0))
    if(plotother != 0){
      circos.updatePlotRegion(
        sector.index = data.C[,1][nrow(data.C)],
        track.index = get.current.track.index(),
        bg.border = NA,
        bg.col = NA
      )
      if(plotother == 2.2){
        traxlim <- get.cell.meta.data("xlim", sector.index = get.current.sector.index(), track.index = get.current.track.index())
        circos.text(
          sector.index = get.current.sector.index(),
          track.index = get.current.track.index(),
          x = round((traxlim[2]-traxlim[1])/3),
          y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          labels = 0,
          facing = "downward"
        )
      }else if(plotother == 1.2){
        circos.text(
          sector.index = get.current.sector.index(),
          track.index = get.current.track.index(),
          x = get.cell.meta.data("xcenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          labels = 0,
          facing = "downward"
        )
      }
    }
    if(!is.null(labels_inf_chr)){
      if(labels_inf_chr[[2]]=="inside"){
        if(ncol(data.CN) == 4){
          circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex =(((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = labels_inf_chr[[4]] , padding = 0 , track.margin = c(0,0), side = "inside")
        }else{
          circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex =(((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = data.CN[,5] , padding = 0 , track.margin = c(0,0), side = "inside")
        }
      }
    }
    
  }
}else if(source_data == "b"){ #sample
  if(datatypeChr == "1"){
    circos.par("start.degree" = 90 - rotation , "gap.degree" = gap.width , cell.padding=c(0,0,0,0) , track.margin=c(0,0))
    circos.genomicInitialize.new(data.C,plotType = plotTypes , axis.labels.cex = outAxis_size , labels.cex = labelChr_size,data.CN = data.CN,labels_inf_chr = labels_inf_chr , outergap = outergap , plotother = plotother)
    if("labels" %in% plotTypes && !("axis" %in% plotTypes)){
      gapgap <- 0.02
    }else{
      gapgap <- 0
    }
    
    circos.genomicTrackPlotRegion(ylim = c(0, 1) , bg.col = colorChr, bg.border = NA , track.height = chr_height , track.margin = c(dis_Chr,gapgap))
    
    if(plotother != 0){
      circos.updatePlotRegion(
        sector.index = data.C[,1][nrow(data.C)],
        track.index = get.current.track.index(),
        bg.border = NA,
        bg.col = NA
      )
      if(plotother == 2.2){
        traxlim <- get.cell.meta.data("xlim", sector.index = get.current.sector.index(), track.index = get.current.track.index())
        circos.text(
          sector.index = get.current.sector.index(),
          track.index = get.current.track.index(),
          x = round((traxlim[2]-traxlim[1])/3),
          y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          labels = 0,
          facing = "downward"
        )
      }else if(plotother == 1.2){
        circos.text(
          sector.index = get.current.sector.index(),
          track.index = get.current.track.index(),
          x = get.cell.meta.data("xcenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          labels = 0,
          facing = "downward"
        )
      }
    }
    
    if(!is.null(labels_inf_chr)){
      if(labels_inf_chr[[2]]=="inside"){
        if(ncol(data.CN) == 4){
          circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex = (((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = labels_inf_chr[[4]] , padding = 0 , track.margin = c(0,0), side = "inside")
        }else{
          circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex = (((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = data.CN[,5] , padding = 0 , track.margin = c(0,0), side = "inside")
        }
        
      }
    }
  }else{
    circos.par("start.degree"=90-rotation, "gap.degree"=gap.width, cell.padding=c(0,0,0,0), track.margin=c(0,0))
    circos.genomicInitialize.new(data.C,plotType = plotTypes , axis.labels.cex = outAxis_size , labels.cex = labelChr_size,data.CN = data.CN,labels_inf_chr = labels_inf_chr , outergap = outergap , plotother = plotother)
    if("labels" %in% plotTypes && !("axis" %in% plotTypes)){
      gapgap <- 0.02
    }else{
      gapgap <- 0
    }
    circos.genomicTrackPlotRegion(data.C, ylim = c(0, 1), bg.border = NA, track.height = chr_height, track.margin = c(dis_Chr,gapgap) , panel.fun = function(region, value, ...){
      col = cytoband.col(value[[2]])
      circos.genomicRect(region, value, ybottom = 0, ytop = 1, col = col, border = NA, ...)
      xlim = get.cell.meta.data("xlim")
      circos.rect(xlim[1], 0, xlim[2], 1, border = "#000000")
    }, cell.padding = c(0, 0, 0, 0))
    if(plotother != 0){
      circos.updatePlotRegion(
        sector.index = data.C[,1][nrow(data.C)],
        track.index = get.current.track.index(),
        bg.border = NA,
        bg.col = NA
      )
      if(plotother == 2.2){
        traxlim <- get.cell.meta.data("xlim", sector.index = get.current.sector.index(), track.index = get.current.track.index())
        circos.text(
          sector.index = get.current.sector.index(),
          track.index = get.current.track.index(),
          x = round((traxlim[2]-traxlim[1])/3),
          y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          labels = 0,
          facing = "downward"
        )
      }else if(plotother == 1.2){
        circos.text(
          sector.index = get.current.sector.index(),
          track.index = get.current.track.index(),
          x = get.cell.meta.data("xcenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
          labels = 0,
          facing = "downward"
        )
      }
    }
    if(!is.null(labels_inf_chr)){
      if(labels_inf_chr[[2]]=="inside"){
        if(ncol(data.CN) == 4){
          circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex =(((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = labels_inf_chr[[4]] , padding = 0 , track.margin = c(0,0), side = "inside")
        }else{
          circos.genomicLabels(data.CN, labels.column = 4, connection_height = labels_inf_chr[[3]]/4, labels_height = (labels_inf_chr[[3]]/4)*3 , cex =(((((as.numeric(labels_inf_chr[[3]]))*4)/5)/max(strwidth(data.CN[,4])))-0.15)*labels_inf_chr[[7]] , line_col = "#000000" , col = data.CN[,5] , padding = 0 , track.margin = c(0,0), side = "inside")
        }
      }
    }
    
  }
}
if(!is.null(unlist(data.T))){
  for(i in 1:length(data.T)){
    if(!is.null(data.N)){
      if(lab_inf[i]){
        labels_inf <- labels_inf_tra[labels_inf_tra[,1] %in% i,]
        data.NN <- data.N[[as.numeric(labels_inf[[5]])]]
        labelsize <- (((((as.numeric(labels_inf[[3]]))*4)/5)/max(strwidth(data.NN[,4])))-0.15)*labels_inf[[7]]
        lab_height <- as.numeric(labels_inf[[3]])
      }
    }else{
      lab_inf <- rep(0, times=length(data.T))
    }
    ordertrapos <- order(trapos)
    data.TT <- data.T[[ordertrapos[i]]]
    data.TT_old <- data.T[[ordertrapos[i]]]
    tktype <- tratype[i]
    data.TT[,2] <- as.numeric(data.TT[,2])
    data.TT[,3] <- as.numeric(data.TT[,3])
    
    ## *** The fill color for track ***
    if(tktype!="rect-discrete" && tktype!="rect-gradual" && tktype!="heatmap-gradual" && tktype!="heatmap-discrete"  && tktype!="ideogram"){
      data.TT$num <- 1:nrow(data.TT)
      data.TTC <- NULL
      coltypeTrack <- as.numeric(tra_coltype[i]) 
      if(tktype == "stack-point" | tktype == "stack-line"){
        names(data.TT)[4] <- "stack"
        names(data.TT_old) <- c("chr","start","end","stack")
        if(coltypeTrack==1){
          selcols <- distinctColorPalette(999)
          #selcols <- c("blue", "red", "green", "cyan", "purple", "pink", "orange", "yellow", "navy", "seagreen", "maroon", "burlywood3", "magenta2")
          tkcolor <- sample(selcols,length(unique(data.TT$stack)))
          tkcolor <- data.frame(group=unique(data.TT$stack),cols=tkcolor,stringsAsFactors=F)
          colname <- colnames(data.TT)
          data.TTC <- merge(data.TT,tkcolor,by.x="stack",by.y="group",all.x=T)
          data.TTC <- data.TTC[,c(colname,"cols")]
          data.TTC$cols[is.na(data.TTC$cols)] <- "grey"
            tkcolor <- unique(data.TTC$cols)
            data.TT <- data.TT[,1:4]
        }else if(coltypeTrack == 2){
          tkcolor <- tra_colcol[i]
        }else if(coltypeTrack==3){
          tkcolor <- tra_colorcus[i]
          tkcolor <- unlist(strsplit(tkcolor,";"))
          tkcolor <- data.frame(id=tkcolor,stringsAsFactors=F)
          tkcolor$group <- gsub("\\\\:.*","",tkcolor$id)
          tkcolor$cols <- gsub(".*\\\\:","",tkcolor$id)
          tkcolor$group <- gsub(" ","",tkcolor$group)
          tkcolor$cols <- gsub(" ","",tkcolor$cols)
          colname <- colnames(data.TT)
          if("color" %in% colnames(data.TT)){
            data.TTC <- merge(data.TT,tkcolor,by.x="color",by.y="group",all.x=T)
          }else if(colnames(data.TT)[4]=="stack"){
            data.TTC <- merge(data.TT,tkcolor,by.x="stack",by.y="group",all.x=T)
          }
          data.TTC <- data.TTC[c(colname,"cols")]
          data.TTC$cols[is.na(data.TTC$cols)] <- "grey"
            tkcolor <- unique(data.TTC$cols,fromLast = TRUE)
            data.TT <- data.TT[,1:4]
        }
      }else{
        if(coltypeTrack == 2){
          tkcolor <- tra_colcol[i]
          tkcolor <- gsub("\\\\s","",strsplit(tkcolor,",")[[1]])
          tkcolor <- gsub("\\\\\'","",tkcolor)
          tkcolor <- gsub("0x","#", tkcolor)
        }else if((coltypeTrack==3 && ("color" %in% colnames(data.TT)))){
          tkcolor <- tra_colorcus[i]
          tkcolor <- unlist(strsplit(tkcolor,";"))
          tkcolor <- data.frame(id=tkcolor,stringsAsFactors=F)
          tkcolor$group <- gsub("\\\\:.*","",tkcolor$id)
          tkcolor$cols <- gsub(".*\\\\:","",tkcolor$id)
          tkcolor$group <- gsub(" ","",tkcolor$group)
          tkcolor$cols <- gsub(" ","",tkcolor$cols)
          colname <- colnames(data.TT)
          if("color" %in% colnames(data.TT)){
            data.TTC <- merge(data.TT,tkcolor,by.x="color",by.y="group",all.x=T)
          }else if(colnames(data.TT)[4]=="stack"){
            data.TTC <- merge(data.TT,tkcolor,by.x="stack",by.y="group",all.x=T)
          }
          data.TTC <- data.TTC[c(colname,"cols")]
          data.TTC$cols[is.na(data.TTC$cols)] <- "grey"
            tkcolor <- unique(data.TTC$cols,fromLast = TRUE)
            data.TT <- data.TT[,1:4]
        }else if(coltypeTrack==1 && ("color" %in% colnames(data.TT))){
          selcols <- distinctColorPalette(999)
          tkcolor <- sample(selcols,length(unique(data.TT$color)))
          tkcolor <- data.frame(group=unique(data.TT$color),cols=tkcolor,stringsAsFactors=F)
          colname <- colnames(data.TT)
          data.TTC <- merge(data.TT,tkcolor,by.x="color",by.y="group",all.x=T)
          data.TTC <- data.TTC[c(colname,"cols")]
          data.TTC$cols[is.na(data.TTC$cols)] <- "grey"
            tkcolor <- unique(data.TTC$cols)
            data.TT <- data.TT[,1:4]
        }else{
          selcols <- distinctColorPalette(999)
          tkcolor <- sample(selcols,ncol(data.TT_old)-3)
        }
      }
      
      
      if(!is.null(data.TTC)){
        data.TTC <- data.TTC[order(data.TTC$num),]
        rownames(data.TTC) <- NULL
        data.TTC$num <- NULL
      }
      data.TT$num <- NULL
      if(c(ncol(data.TT)==5 | ncol(data.TT)==6 | ncol(data.TT)==7) && ("color" %in% colnames(data.TT))){
        data.TT <- data.TT[,1:4]
      }else if(c(ncol(data.TT)==5 | ncol(data.TT)==6 | ncol(data.TT)==7) && ("pch" %in% colnames(data.TT)) && !("color" %in% colnames(data.TT))){
        data.TT <- data.TT[,1:4]
        tkcolor <- tkcolor[1]
      }
    }
    ## *** The backgroud color for track ***
    tkbgcol <- tra_bgcol[i]
    if(!is.null(tkbgcol)){
      tkbgcol <- gsub("\\\\s","",strsplit(tkbgcol,",")[[1]])
      tkbgcol <- gsub("\\\\\'","",tkbgcol)
      tkbgcol <- gsub("0x","#", tkbgcol)
      repnumcol <- round(length(unique(data.C[,1]))/length(tkbgcol))+1
      tkbgcol <- rep(tkbgcol, repnumcol)[1:length(unique(data.C[,1]))]
    }
    ## *** The track margin ***
    tkmargin <- tra_Margin[i]
    tkmargin <- as.numeric(tkmargin)
    ## *** The track height ***
    tkheight <- heightTra[i]
    if(!is.null(tkheight)){
      tkheight <- as.numeric(tkheight)
    }
    ## *** The y coordinates of baselines ***
    tklinecoord <- tra_baseline[i]
    if(!is.null(tklinecoord)){
      tklinecoord <- as.numeric(unlist(strsplit(tklinecoord,",")))
    }else{
      tklinecoord <- c(0.25,0.75)
    }
    ## *** The baselines color ***
    tklinecolor <- tra_colorline[i]
    if(!is.null(tklinecolor)){
      tklinecolor <- gsub("\\\\\'","",tklinecolor)
      tklinecolor <- gsub("0x","#", tklinecolor)
      tklinecolor <- unlist(strsplit(tklinecolor,","))
      tklinecolor <- rep(tklinecolor, length(tklinecoord))[1:length(tklinecoord)]
    }
    if(ncol(data.TT_old)==4 && colnames(data.TT_old)[4]=="stack"){
      tklinecol <- gsub("\\\\\'","",tklinecolor)
      tklinecol <- gsub("0x","#", tklinecol)
      tklinecol <- unlist(strsplit(tklinecol,","))
      tklinecol <- rep(tklinecol, length(unique(data.TT_old[,4])))[1:length(unique(data.TT_old[,4]))]
    }
    ## *** Add connection ***
    if(!is.null(tra_hmap_poslines[i])){
      if(tra_hmap_poslines[i] == "1"){
        heightlines <- tra_hmap_poslinhei[i]
      }
    }
    ## *** Add border ***
    tkborder <- tra_border[i]
    
    if(!is.null(tra_hmap_cellbord[i])){
      if(tra_hmap_cellbord[i] == "add"){
        tkbordercol <- tra_hmap_cellbord_col[i]
      }else{
        tkbordercol <- NA
      }
    }
    
    if(!is.null(tra_hmap_heatmapcol[i])){
      if(tra_hmap_heatmapcol[i] == 1){ #heatmap color type, 1 is "typical"
        hmapcols <- gsub("\\\\\'","",tra_hmap_typcolhmap[i])
      }else{
        hmapcols <- heatmapcols[i]
      }
      hmapcols <- unlist(strsplit(hmapcols,"\\\\."))
    }
    #
    ##
    ## *** The bar direction ***
    tkbardir <- tra_bar_direction[i]
    if(!is.null(tkbardir)){
      if(tkbardir==2){
        tkbarvalue <- tra_bar_Boundary[i]
        tkbarcol1 <- tra_bar_coldir1[i]
        tkbarcol2 <- tra_bar_coldir2[i]
        tktransparency <- tra_transparency[i]
        tkbarcol1 <- adjustcolor(tkbarcol1, alpha.f = tktransparency)
        tkbarcol2 <- adjustcolor(tkbarcol2, alpha.f = tktransparency)
      }
    }
    
    if(tktype == "heatmap-discrete"){
      tra_heatcol_dis_tr <- tra_heatcol_dis[i]
      #tra_heatcol_dis,tra_heat_heatcoldiscus
      
      data.TT_hat_dis <- data.TT
      for(li in 4:length(data.TT)){
        data.TT_hat_dis[,li] <- as.numeric(as.factor(data.TT[,li]))
      }
      if(tra_heatcol_dis_tr == 2){
        heat_dis_cols <- tra_heat_heatcoldiscus[i]
        heat_dis_cols <- unlist(strsplit(heat_dis_cols,";"))
        heat_dis_cols <- data.frame(id=heat_dis_cols,stringsAsFactors=F)
        heat_dis_cols$group <- gsub("\\\\:.*","",heat_dis_cols$id)
        heat_dis_cols$cols <- gsub(".*\\\\:","",heat_dis_cols$id)
        heat_dis_cols$group <- gsub(" ","",heat_dis_cols$group)
        heat_dis_cols$cols <- gsub(" ","",heat_dis_cols$cols)
        heat_dis_cols[,2] <- as.numeric(as.factor(heat_dis_cols[,2]))
        data_TT_col <- apply(data.TT_hat_dis[,4:length(data.TT_hat_dis)], MARGIN = c(1,2), function(x){
          if(length(which(heat_dis_cols[,2] == x)) != 0){
            heat_dis_cols[which(heat_dis_cols[,2] == x),3]
          }else{
            "#FFFFFF"
          }
        })
      }else{
        cols <- c(brewer.pal(11,"Set3"),brewer.pal(9,"Set1")[c(-1,-3,-6)],brewer.pal(8,"Dark2"),"chartreuse","aquamarine","cornflowerblue","blue","cyan","bisque1","darkorchid2","firebrick1","gold1","magenta1","olivedrab1","navy","maroon1","tan","yellow3","black","bisque4","seagreen3","plum2","yellow1","springgreen","slateblue1","lightsteelblue1","lightseagreen","limegreen")
        selcol <- sample(cols,length(unique(unlist(data.TT_hat_dis[,4:length(data.TT_hat_dis)]))),replace = TRUE)
        data_ht_col <- data.TT_hat_dis
        for(ki in 4:length(data.TT_hat_dis)){
          data_ht_col[,ki] <- selcol[data.TT_hat_dis[,ki]]
        }
        
        data_TT_col <- data_ht_col[,4:length(data_ht_col)]
      }
      data_TT_col <- as.matrix(data_TT_col)
    }
    ## *** Two kinds of rect  plot type color ***
    selrectcol <- tra_rect_rectcol[i]
    if(tktype == "rect-gradual"){
      if(selrectcol==1){
        rectcol <- tra_trct_colrect[i]
        if(rectcol=="blue"){
          rectcols <- c("#EDEDFD","#6969F5","#00008B")
        }else if(rectcol=="red"){
          rectcols <- c("#FDEDED","#F56969","#8B0000")
        }else if(rectcol=="green"){
          rectcols <- c("#EDFBED","#69E169","#008B00")
        }else if(rectcol=="cyan"){
          rectcols <- c("#EDFBFB","#69E1E1","#008B8B")
        }else if(rectcol=="purple"){
          rectcols <- c("#F6F0FB","#B27FE1","#551A8B")
        }else if(rectcol=="pink"){
          rectcols <- c("#FBEEF5","#E172AE","#8B1076")
        }else if(rectcol=="orange"){
          rectcols <- c("#FDF5ED","#F5AE69","#8B4500")
        }else if(rectcol=="yellow"){
          rectcols <- c("#FDFDED","#EFEF1A","#8B8B00")
        }else if(rectcol=="navy"){
          rectcols <- c("#EDEDF6","#7272B8","#000080")
        }else if(rectcol=="seagreen"){
          rectcols <- c("#F2FBF6","#4EEE94","#2E8B57")
        }else if(rectcol=="maroon"){
          rectcols <- c("#FFF4FB","#FF69C7","#8B1C62")
        }else if(rectcol=="olivedrab"){
          rectcols <- c("#FBFFF4","#C6FF52","#698B22")
        }else if(rectcol=="gold"){
          rectcols <- c("#FFFCF1","#FFDD28","#8B7500")
        }else if(rectcol=="lightblue"){
          rectcols <- c("#EFF5F7","#AFCDD7","#68838B")
        }else if(rectcol=="navy.yellow"){
          rectcols <- c("#000080","#7B7B41","#FFFF00")
        }else if(rectcol=="purple.seagreen"){
          rectcols <- c("#551A8B","#548994","#54FF9F")
        }else if(rectcol=="navy.orange"){
          rectcols <- c("#000080","#7B5041","#FFA500")
        }else if(rectcol=="navy.cyan"){
          rectcols <- c("#000080","#007BBD","#00FFFF")
        }else if(rectcol=="blue.red"){
          rectcols <- c("#0000FF","#730083","#EE0000")
        }else if(rectcol=="green.red"){
          rectcols <- c("#00EE00","#757800","#EE0000")
        }
      }else if(selrectcol==2){
        rectcols <- rect_gra_3Col[i,]
      }
    }else if(tktype == "rect-discrete"){
      if(selrectcol==2){
        rectcols <- tra_rect_rectcoldis[i]
        data.TT[,4] <- rectcols
      }else if(selrectcol==3){
        rectcols <- tra_rect_rectcoldiscus[i]
        rectcols <- unlist(strsplit(rectcols,";"))
        rectcols <- data.frame(id=rectcols,stringsAsFactors=F)
        rectcols$group <- gsub("\\\\:.*","",rectcols$id)
        rectcols$cols <- gsub(".*\\\\:","",rectcols$id)
        rectcols$group <- gsub(" ","",rectcols$group)
        rectcols$cols <- gsub(" ","",rectcols$cols)
        colname <- colnames(data.TT)[1:3]
        data.TT <- merge(data.TT,rectcols,by.x=colnames(data.TT)[4],by.y="group",all.x=T)
        data.TT <- data.TT[c(colname,"cols")]
      }
    }
    ## *** The transparency of color ***
    
    tktransparency <- tra_transparency[i]
    if((tktype!="rect-gradual" && tktype!="rect-discrete" && tktype!="heatmap-gradual" && tktype!="heatmap-discrete" && tktype!="ideogram") | (tktype=="line" && tra_line_fillarea[i]!="add")){
      tkcolor <- adjustcolor(tkcolor, alpha.f = tktransparency)
    }
    # data.TTT <- data.TT
    # data.TTT$id <- paste(data.TTT[,1],data.TTT[,2],data.TTT[,3],sep="")
    # data.TTT$num <- 1:nrow(data.TTT)
    ## *** The track border
    if(!is.null(tkborder)){
      if(tkborder=="add"){
        tkborder <- "grey"
      }else{
        tkborder <- NA
      }
    }
    ## *** The symbol type & point size***
    if(tktype == "point" & !("pch" %in% colnames(data.TT))){
      if(!is.null(tra_poipch[i])){
        symboltype <- as.numeric(tra_poipch[i])
        if(!is.null(symboltype)){
          symboltype <- rep(symboltype, length(unique(data.TT_old[,4])))[1:length(unique(data.TT_old[,4]))]
        }
      }
    }
    if(tktype == "point" & !("cex" %in% colnames(data.TT))){
      if(!is.null(tra_poi_poisiz[i])){
        pointsize <- as.numeric(tra_poi_poisiz[i])
      }
    }
    if(tktype == "stack-point"){
      if(!is.null(tra_poipch[i])){
        symboltype <- tra_poipch[i]
        if(!is.null(symboltype)){
          symboltype <- as.numeric(unlist(strsplit(symboltype,",")))
          symboltype <- rep(symboltype, length(unique(data.TT_old[,4])))[1:length(unique(data.TT_old[,4]))]
        }
        
      }
      if(!is.null(tra_poi_poisiz[i])){
        pointsize <- as.numeric(tra_poi_poisiz[i])
      }
    }
    columns <- c(1:ncol(data.TT))[-c(1:3)]
    
    
    
    if(lab_inf[i]){
      if(labels_inf[[2]]=="outside"){
        if(ncol(data.NN) == 4){
          circos.genomicLabels(data.NN, labels.column = 4, connection_height = lab_height/4, labels_height = (lab_height/4)*3 , cex =labelsize , line_col = "#000000" , col = labels_inf[[4]] , padding = 0 , track.margin = c(0,0), side = "outside")
        }else{
          circos.genomicLabels(data.NN, labels.column = 4, connection_height = lab_height/4, labels_height = (lab_height/4)*3 , cex =labelsize , line_col = "#000000" , col = data.NN[,5] , padding = 0 , track.margin = c(0,0), side = "outside")
        }
      }
    }
    
    
    
    
    
    
    
    
    if(tktype == "line"){
      ## *** Fill the area ***
      if(tra_line_fillarea[i]!="add"){
        area <- FALSE
        borderset <- NA
        lwdnum <- 1
      }else if(tra_line_fillarea[i]=="add" && tra_line_selrea[i]==1){
        area <- TRUE
        lwdnum <- 0.2
      }else if(tra_line_fillarea[i]=="add" && tra_line_selrea[i]==2){
        area <- TRUE
        borderset <- NA
        if(nchar(tra_bar_borderarea[i])!=0){
          borderset <- adjustcolor(tra_bar_borderarea[i],alpha.f = tktransparency)
        }
        lwdnum <- 0.2
      }
      data.TT[,ncol(data.TT)] <- as.numeric(data.TT[,ncol(data.TT)])
      circos.genomicTrackPlotRegion(data.TT, track.height = tkheight, track.margin = c(tkmargin,0) , bg.col = tkbgcol , bg.border = tkborder, panel.fun = function(region,value,...){
        if(nchar(tklinecolor[1])!=0){
          xlim <- get.cell.meta.data("xlim")
          ylim <- get.cell.meta.data("ylim")
          for(k in 1:length(tklinecoord)){
            y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
            circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
          }
        }
        if((coltypeTrack==1 && !("color" %in% colnames(data.TT_old))) | coltypeTrack==2){
          if(length(columns)==1){
            tkcolor <- tkcolor[1]
          }else{
            tkcolor <- c(tkcolor,rep("grey",length(columns)))
            tkcolor <- tkcolor[1:length(columns)]
          }
          if(tra_line_selrea[i]==1 | tra_line_fillarea[i]!="add"){
            borderset <- adjustcolor(tkcolor,alpha.f = tktransparency)
          }
          circos.genomicLines(region, value, numeric.column=columns-3, col=borderset, area=area, border=tkcolor, lwd=lwdnum, lty=1, ...)
        }
      })
      takindx <- get.current.track.index()
      if(coltypeTrack==3 && ncol(data.TTC)>=6 && ("cols" %in% colnames(data.TTC))){
        data.TTC$id <- paste(data.TTC[,1],data.TTC[,2],data.TTC[,3],sep="")
        data.TTC$num <- 1:nrow(data.TTC)
        lapply(unique(data.TTC[,1]),function(x){
          circos.updatePlotRegion(sector.index = x, track.index=takindx , bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
          if(nchar(tklinecolor[1])!=0){
            xlim <- get.cell.meta.data("xlim")
            ylim <- get.cell.meta.data("ylim")
            for(k in 1:length(tklinecoord)){
              y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
              circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
            }
          }
          dat <- data.TTC[data.TTC[,1] %in% x,]
          lapply(unique(dat$cols),function(m){
            datt <- dat[dat$cols %in% m,]
            ind <- which(data.TTC$id %in% datt$id)
            datt.fil <- na.omit(unique(data.TTC[ind,]))
            datt.fil <- datt.fil[datt.fil[,1] %in% x,]
            rownum <- datt.fil$num
            rownumdif <- diff(rownum)
            indx <- which(rownumdif != 1)
            indx1 <- c(0,indx)
            rownumdif1 <- c(1,rownumdif)
            if(length(indx)==0){
              rownumdif1 <- 1
            }else{
              for(k in 1:length(which(rownumdif!=1))){
                rownumdif1[(indx1[k]+1):indx[k]] <- k
                if(k==length(which(rownumdif!=1))){
                  rownumdif1[(indx[k]+1):length(rownumdif1)] <- k+1
                }
              }
            }
            datt.fil$indx <- rownumdif1
            lapply(unique(rownumdif1),function(h){
              datt.fill <- datt.fil[datt.fil$indx == h,]
              minnum <- min(datt.fill$num)
              if(sum(dat$num==(minnum-1)) != 0){
                datt.fill <- rbind(datt.fill,as.character(c(dat[dat$num==(minnum-1),],h)))
              }
              
              datt.fill[,2] <- as.numeric(datt.fill[,2])
              datt.fill[,3] <- as.numeric(datt.fill[,3])
              datt.fill[,4] <- as.numeric(datt.fill[,4])
              datt.fill$indx <- as.numeric(datt.fill$indx)
              datt.fill <- datt.fill[!is.na(datt.fill[,2]),]
              datt.fill <- datt.fill[order(datt.fill[,2],datt.fill[,3]),]
              if(tra_line_selrea[i]==1 | tra_line_fillarea[i]!="add"){
                borderset <- adjustcolor(m,alpha.f = tktransparency)
              }
              circos.lines((datt.fill[,2]+datt.fill[,3])/2,datt.fill[,4], col=borderset, area=area, border=m, lwd=lwdnum, lty=1)
            })
          })
        })
      }else if(coltypeTrack==1 && ("color" %in% colnames(data.TT_old)) && ("cols" %in% colnames(data.TTC))){
        data.TTC$id <- paste(data.TTC[,1],data.TTC[,2],data.TTC[,3],sep="")
        data.TTC$num <- 1:nrow(data.TTC)
        lapply(unique(data.TTC[,1]),function(x){
          circos.updatePlotRegion(sector.index = x, track.index = takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
          if(nchar(tklinecolor[1])!=0){
            xlim <- get.cell.meta.data("xlim")
            ylim <- get.cell.meta.data("ylim")
            for(k in 1:length(tklinecoord)){
              y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
              circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
            }
          }
          dat <- data.TTC[data.TTC[,1] %in% x,]
          lapply(unique(dat$cols),function(m){
            datt <- dat[dat$cols %in% m,]
            ind <- which(data.TTC$id %in% datt$id)
            datt.fil <- na.omit(unique(data.TTC[ind,]))
            datt.fil <- datt.fil[datt.fil[,1] %in% x,]
            rownum <- datt.fil$num
            rownumdif <- diff(rownum)
            indx <- which(rownumdif != 1)
            indx1 <- c(0,indx)
            rownumdif1 <- c(1,rownumdif)
            if(length(indx)==0){
              rownumdif1 <- 1
            }else{
              for(k in 1:length(which(rownumdif!=1))){
                rownumdif1[(indx1[k]+1):indx[k]] <- k
                if(k==length(which(rownumdif!=1))){
                  rownumdif1[(indx[k]+1):length(rownumdif1)] <- k+1
                }
              }
            }
            datt.fil$indx <- rownumdif1
            lapply(unique(rownumdif1),function(h){
              datt.fill <- datt.fil[datt.fil$indx == h,]
              minnum <- min(datt.fill$num)
              if(sum(dat$num==(minnum-1)) != 0){
                
                datt.fill <- rbind(datt.fill,as.character(c(dat[dat$num==(minnum-1),],h)))
                
              }
              
              datt.fill[,2] <- as.numeric(datt.fill[,2])
              datt.fill[,3] <- as.numeric(datt.fill[,3])
              datt.fill[,4] <- as.numeric(datt.fill[,4])
              datt.fill$indx <- as.numeric(datt.fill$indx)
              datt.fill <- datt.fill[!is.na(datt.fill[,2]),]
              datt.fill <- datt.fill[order(datt.fill[,2],datt.fill[,3]),]
              if(tra_line_selrea[i]==1 | tra_line_fillarea[i]!="add"){
                borderset <- adjustcolor(m,alpha.f = tktransparency)
              }
              circos.lines((datt.fill[,2]+datt.fill[,3])/2,datt.fill[,4], col=borderset, area=area, border=m, lwd=lwdnum, lty=1)
            })
          })
        })
      }
    }else if(tktype == "stack-line"){
      bed_list <- lapply(unique(data.TT_old[,4],fromLast = TRUE),function(x){
        
        if(coltypeTrack==2){
          data.TT[data.TT[,4] %in% x,1:3]
        }else{
          data.TTC[data.TTC[,4] %in% x,1:3]
        }
      })
      circos.genomicTrackPlotRegion(bed_list, stack = TRUE, track.height = tkheight, track.margin = c(tkmargin,0),bg.col = tkbgcol, bg.border = tkborder, panel.fun = function(region, value, ...){
        ii = getI(...)
        if(coltypeTrack==1){
          circos.genomicLines(region, value, col=tkcolor[ii], lty=1, ...)
        }else if(coltypeTrack==2){
          circos.genomicLines(region, value, col=tkcolor, lty=1, ...)
        }else if(coltypeTrack==3){
          circos.genomicLines(region, value, col=tkcolor[ii], lty=1, ...)
        }
      })
    }else if(tktype == "stack-point"){
      bed_list <- lapply(unique(data.TT_old[,4],fromLast = TRUE),function(x){
        if(coltypeTrack==2){
          data.TT[data.TT[,4] %in% x,1:3]
        }else{
          data.TTC[data.TTC[,4] %in% x,1:3]
        }
      })
      circos.genomicTrackPlotRegion(bed_list, stack = TRUE, track.height = tkheight, track.margin = c(tkmargin,0), bg.col = tkbgcol , bg.border = tkborder, panel.fun = function(region, value, ...){
        ii = getI(...)
        if(coltypeTrack==1){
          circos.lines(CELL_META$cell.xlim, c(ii, ii), lty = 2, col = tklinecol[ii])
          circos.genomicPoints(region, value, pch = symboltype[ii], cex = pointsize, col = tkcolor[ii],...)
        }else if(coltypeTrack==2){
          circos.lines(CELL_META$cell.xlim, c(ii, ii), lty = 2, col = tklinecol[ii])
          circos.genomicPoints(region, value, pch = symboltype[ii], cex = pointsize, col = tkcolor,...)
        }else if(coltypeTrack==3){
          circos.lines(CELL_META$cell.xlim, c(ii, ii), lty = 2, col = tklinecol[ii]) #  "#808080" "#808080"
          circos.genomicPoints(region, value, pch = symboltype[ii], cex = pointsize, col = tkcolor[ii],...)
        }
      })
    }else if(tktype == "point"){
      
      data.TT[,ncol(data.TT)] <- as.numeric(data.TT[,ncol(data.TT)])
      circos.genomicTrack(data.TT , track.height = tkheight , track.margin = c(tkmargin,0) , bg.col = tkbgcol , bg.border = tkborder , panel.fun = function(region, value, ...) {
        if(nchar(tklinecolor[1])!=0){
          xlim <- get.cell.meta.data("xlim")
          ylim <- get.cell.meta.data("ylim")
          for(k in 1:length(tklinecoord)){
            y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
            circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
          }
        }
        if(!("cex" %in% colnames(data.TT_old)) && !("pch" %in% colnames(data.TT_old)) && ((coltypeTrack==1 && !("color" %in% colnames(data.TT_old))) | coltypeTrack==2)){
          if(length(columns)==1){
            tkcolor <- tkcolor[1]
          }else{
            tkcolor <- c(tkcolor,rep("grey",length(columns)))
            tkcolor <- tkcolor[1:length(columns)]
          }
          circos.genomicPoints(region, value, numeric.column=columns-3, col= tkcolor, cex= pointsize, pch= symboltype[1], ...)
        }
      })
      takindx <- get.current.track.index()
      if(!("cex" %in% colnames(data.TT_old))){
        if(coltypeTrack==3 && ("pch" %in% colnames(data.TTC)) && ("cols" %in% colnames(data.TTC))){
          lapply(unique(data.TTC[,1]),function(x){
            circos.updatePlotRegion(sector.index = x, track.index = takindx , bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TTC[data.TTC[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=adjustcolor(dat$cols,alpha.f = tktransparency), cex=0.6, pch=dat$pch)
          })
        }else if(coltypeTrack==3 && ncol(data.TTC)>=6 && !("pch" %in% colnames(data.TTC)) && ("cols" %in% colnames(data.TTC))){
          lapply(unique(data.TTC[,1]),function(x){
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TTC[data.TTC[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=adjustcolor(dat$cols,alpha.f = tktransparency), cex=0.6, pch=16)
          })
        }else if(coltypeTrack!=3 && ("pch" %in% colnames(data.TT_old)) && ("color" %in% colnames(data.TT_old))){
          lapply(unique(data.TT_old[,1]),function(x){
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TT_old[data.TT_old[,1] %in% x,]
            if(coltypeTrack==1){
              tkcols <- data.TTC$cols[data.TTC[,1] %in% x]
              circos.points((dat[,2]+dat[,3])/2,dat[,4], col=adjustcolor(tkcols,alpha.f = tktransparency), cex=0.6, pch=dat$pch)
            }else{
              circos.points((dat[,2]+dat[,3])/2,dat[,4], col=tkcolor[1], cex=0.6, pch=dat$pch)
            }
          })
        }else if(("pch" %in% colnames(data.TT_old)) && !("color" %in% colnames(data.TT_old))){
          lapply(unique(data.TT_old[,1]),function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TT_old[data.TT_old[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=tkcolor, cex=0.6, pch=dat$pch)
          })
        }else if(coltypeTrack==1 && ("color" %in% colnames(data.TT_old)) && !("pch" %in% colnames(data.TTC)) && ("cols" %in% colnames(data.TTC))){
          lapply(unique(data.TTC[,1]),function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TTC[data.TTC[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=adjustcolor(dat$cols,alpha.f = tktransparency), cex=0.6, pch=16)
          })
        }
      }else if("cex" %in% colnames(data.TT_old)){
        
        if(coltypeTrack==3 && ("pch" %in% colnames(data.TTC)) && ("cols" %in% colnames(data.TTC))){
          lapply(unique(data.TTC[,1]),function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TTC[data.TTC[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=adjustcolor(dat$cols,alpha.f = tktransparency), cex=dat$cex, pch=dat$pch)
          })
        }else if(coltypeTrack==3 && ncol(data.TTC)>=6 && !("pch" %in% colnames(data.TTC)) && ("cols" %in% colnames(data.TTC))){
          lapply(unique(data.TTC[,1]),function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TTC[data.TTC[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=adjustcolor(dat$cols,alpha.f = tktransparency), cex=dat$cex, pch=16)
          })
        }else if(coltypeTrack!=3 && ("pch" %in% colnames(data.TT_old)) && ("color" %in% colnames(data.TT_old))){
          lapply(unique(data.TT_old[,1]),function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TT_old[data.TT_old[,1] %in% x,]
            if(coltypeTrack==1){
              tkcols <- data.TTC$cols[data.TTC[,1] %in% x]
              circos.points((dat[,2]+dat[,3])/2,dat[,4], col=adjustcolor(tkcols,alpha.f = tktransparency), cex=dat$cex, pch=dat$pch)
            }else{
              circos.points((dat[,2]+dat[,3])/2,dat[,4], col=tkcolor[1], cex=dat$cex, pch=dat$pch)
            }
          })
        }else if(("pch" %in% colnames(data.TT_old)) && !("color" %in% colnames(data.TT_old))){
          lapply(unique(data.TT_old[,1]),function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TT_old[data.TT_old[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=tkcolor, cex=dat$cex, pch=dat$pch)
          })
        }else if(coltypeTrack==1 && ("color" %in% colnames(data.TT_old)) && !("pch" %in% colnames(data.TTC)) && ("cols" %in% colnames(data.TTC))){
          lapply(unique(data.TTC[,1]),function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TTC[data.TTC[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=adjustcolor(dat$cols,alpha.f = tktransparency), cex=dat$cex, pch=16)
          })
        }else if(!("color" %in% colnames(data.TT_old)) && !("pch" %in% colnames(data.TTC))){
          lapply(unique(data.TT_old[,1]),function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TT_old[data.TT_old[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=tkcolor[1], cex=dat$cex, pch=16)
          })
        }else if(coltypeTrack==2 && ("color" %in% colnames(data.TT_old)) && !("pch" %in% colnames(data.TT_old))){
          lapply(data.TT_old,function(x){
            
            circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
            
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            dat <- data.TT_old[data.TT_old[,1] %in% x,]
            circos.points((dat[,2]+dat[,3])/2,dat[,4], col=tkcolor[1], cex=dat$cex, pch=16)
          })
        }
      }
    }else if(tktype == "rect-discrete" | tktype == "rect-gradual"){
      if(tktype== "rect-discrete"){
        if(selrectcol==1){
          data.TT[,4] <- as.numeric(as.factor(data.TT[,4]))
          cols <- c(brewer.pal(11,"Set3"),brewer.pal(9,"Set1")[c(-1,-3,-6)],brewer.pal(8,"Dark2"),"chartreuse","aquamarine","cornflowerblue","blue","cyan","bisque1","darkorchid2","firebrick1","gold1","magenta1","olivedrab1","navy","maroon1","tan","yellow3","black","bisque4","seagreen3","plum2","yellow1","springgreen","slateblue1","lightsteelblue1","lightseagreen","limegreen")
          selcol <- sample(cols,length(unique(data.TT[,4])),replace = TRUE)
          #tkcolor.export[[i]] <- selcol
          data.TT[,4] <- selcol[data.TT[,4]]
          circos.genomicTrackPlotRegion(data.TT, ylim=c(0,1),track.height = tkheight, track.margin = c(tkmargin,0), bg.col = tkbgcol, bg.border = tkborder, panel.fun = function(region,value,...){
            circos.genomicRect(region, value, col=adjustcolor(value[[1]],alpha.f = tktransparency), border = NA, ...)
          })
        }else{
          circos.genomicTrackPlotRegion(data.TT, ylim=c(0,1),track.height = tkheight, track.margin = c(tkmargin,0), bg.col = tkbgcol, bg.border = tkborder, panel.fun = function(region,value,...){
            circos.genomicRect(region, value, col=adjustcolor(value[[1]],alpha.f = tktransparency), border = NA, ...)
          })
        }
      }else{
        f <- colorRamp2(breaks = c(min(data.TT[,4]), mean(data.TT[,4]), max(data.TT[,4])), colors = rectcols)
        circos.genomicTrackPlotRegion(data.TT, ylim=c(0,1),track.height = tkheight, track.margin = c(tkmargin,0), bg.col = tkbgcol, bg.border = tkborder, panel.fun = function(region,value,...){
          circos.genomicRect(region, value, col=adjustcolor(f(value[[1]]),alpha.f = tktransparency), border = NA, ...)
        })
      }
    }else if(tktype == "heatmap-gradual"){
      data.TT$num <- NULL
      break1 <- min(as.numeric(as.matrix(data.TT[,-c(1:3)])))
      break2 <- max(as.numeric(as.matrix(data.TT[,-c(1:3)])))
      midpoint <- (break1+break2)/2
      f <- colorRamp2(breaks = c(break1, midpoint, break2), colors = hmapcols)
      if(is.na(tkbordercol)){ #no cell borders
        if(tra_hmap_poslines[i] == "2"){ #no position lines
          circos.genomicHeatmap(
            data.TT, col = f, side = "inside" , heatmap_height = tkheight , numeric.column = 4:length(data.TT) ,  track.margin = c(tkmargin,0) , connection_height = NULL
          )
        }else{
          circos.genomicHeatmap(
            data.TT, col = f, side = "inside" , heatmap_height = tkheight , numeric.column = 4:length(data.TT) ,  track.margin = c(tkmargin,0) , connection_height = heightlines
          )
        }
      }else{
        if(tra_hmap_poslines[i] == "2"){
          circos.genomicHeatmap(
            data.TT, col = f, side = "inside" , heatmap_height = tkheight , numeric.column = 4:length(data.TT) ,  track.margin = c(tkmargin,0) , connection_height = NULL , border = tkbordercol , border_lwd = 0.1
          )
        }else{
          circos.genomicHeatmap(
            data.TT, col = f, side = "inside" , heatmap_height = tkheight , numeric.column = 4:length(data.TT) ,  track.margin = c(tkmargin,0) , connection_height = heightlines , border = tkbordercol , border_lwd = 0.1
          )
        }
      }
    }else if(tktype == "heatmap-discrete"){
      data.TT$num <- NULL
      #no cell borders
      if(is.na(tkbordercol)){
        if(tra_hmap_poslines[i] == "2"){ #no position lines
          circos.genomicHeatmap(
            data.TT_hat_dis, col = data_TT_col, side = "inside" , heatmap_height = tkheight , numeric.column = 4:length(data.TT_hat_dis) ,  track.margin = c(tkmargin,0) , connection_height = NULL
          )
        }else{
          circos.genomicHeatmap(
            data.TT_hat_dis, col = data_TT_col, side = "inside" , heatmap_height = tkheight , numeric.column = 4:length(data.TT_hat_dis) ,  track.margin = c(tkmargin,0) , connection_height = heightlines
          )
        }
      } else {
        
        if(tra_hmap_poslines[i] == "2"){ #no position lines
          
          
          circos.genomicHeatmap(
            data.TT_hat_dis, col = data_TT_col, side = "inside" , heatmap_height = tkheight , numeric.column = 4:length(data.TT_hat_dis) ,  track.margin = c(tkmargin,0) , connection_height = NULL , border = tkbordercol , border_lwd = 0.1
          )
        }else{
          circos.genomicHeatmap(
            data.TT_hat_dis, col = data_TT_col, side = "inside" , heatmap_height = tkheight , numeric.column = 4:length(data.TT_hat_dis) ,  track.margin = c(tkmargin,0) , connection_height = heightlines , border = tkbordercol , border_lwd = 0.1
          )
        }
      }
      
      data.TT <- data_TT_col
    }else if(tktype == "ideogram"){
      circos.genomicIdeogram(data.TT,track.height = tkheight, track.margin = c(tkmargin,0))
    }else if(tktype == "bar"){
      data.TT[,ncol(data.TT)] <- as.numeric(data.TT[,ncol(data.TT)])
      circos.genomicTrackPlotRegion(data.TT, track.height = tkheight, track.margin = c(tkmargin,0),bg.col = tkbgcol , bg.border = tkborder , panel.fun = function(region,value,...){
        if(!("color" %in% colnames(data.TT_old)) && !("cols" %in% colnames(data.TTC))){
          if(length(columns)==1 && tkbardir==1){
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            if(coltypeTrack==1){
              circos.genomicRect(region, value, numeric.column=columns-3, ytop.column = 1, ybottom = min(data.TT[,4]), col = tkcolor, border = NA, ...)
            }
          }else if(length(columns)==1 && tkbardir==2){
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            tkbarvalue <- as.numeric(tkbarvalue)
            indx <- value[,1] > tkbarvalue
            if(length(value[indx,])!=0 && length(value[!indx,])!=0){
              circos.genomicRect(region[indx,], value[indx,], ytop.column = 1, ybottom = tkbarvalue, col=tkbarcol1, border = NA, ...)
              circos.genomicRect(region[!indx,], value[!indx,], ytop.column = 1, ybottom =  tkbarvalue, col=tkbarcol2, border = NA, ...)
            }else if(length(value[indx,])!=0 && length(value[!indx,])==0){
              circos.genomicRect(region[indx,], value[indx,], ytop.column = 1, ybottom = tkbarvalue, col=tkbarcol1, border = NA, ...)
            }else if(length(value[indx,])==0 && length(value[!indx,])!=0){
              circos.genomicRect(region[!indx,], value[!indx,], ytop.column = 1, ybottom =  tkbarvalue, col=tkbarcol2, border = NA, ...)
            }
          }else if(length(columns)==2 && tkbardir==1){
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            if(coltypeTrack!=3){
              tkcolor <- c(tkcolor,rep("grey",length(columns)))
              tkcolor <- tkcolor[1:length(columns)]
              circos.genomicRect(region, value, ytop.column = 1, ybottom = min(c(data.TT[,4],data.TT[,5])), col=tkcolor[1], border = NA, ...)
              circos.genomicRect(region, value, ytop.column = 2, ybottom = min(c(data.TT[,4],data.TT[,5])), col=tkcolor[2], border = NA, ...)
            }
          }else if(length(columns)==2 && tkbardir==2){
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            circos.genomicRect(region, value, ytop.column = 1, ybottom = min(c(data.TT[,4],data.TT[,5])), col=tkbarcol1, border = NA, ...)
            circos.genomicRect(region, value, ytop.column = 2, ybottom = min(c(data.TT[,4],data.TT[,5])), col=tkbarcol2, border = NA, ...)
          }
        }
        if(("color" %in% colnames(data.TT_old)) && ("cols" %in% colnames(data.TTC))){
          if(length(columns)==2 && tkbardir==2){
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            circos.genomicRect(region, value, ytop.column = 1, ybottom = min(c(data.TT[,4],data.TT[,5])), col=tkbarcol1, border = NA, ...)
            circos.genomicRect(region, value, ytop.column = 2, ybottom = min(c(data.TT[,4],data.TT[,5])), col=tkbarcol2, border = NA, ...)
          }else if(length(columns)==1 && tkbardir==2){
            if(nchar(tklinecolor[1])!=0){
              xlim <- get.cell.meta.data("xlim")
              ylim <- get.cell.meta.data("ylim")
              for(k in 1:length(tklinecoord)){
                y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
                circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
              }
            }
            tkbarvalue <- as.numeric(tkbarvalue)
            indx <- value[,1] > tkbarvalue
            if(length(value[indx,])!=0 && length(value[!indx,])!=0){
              circos.genomicRect(region[indx,], value[indx,], ytop.column = 1, ybottom = tkbarvalue, col=tkbarcol1, border = NA, ...)
              circos.genomicRect(region[!indx,], value[!indx,], ytop.column = 1, ybottom =  tkbarvalue, col=tkbarcol2, border = NA, ...)
            }else if(length(value[indx,])!=0 && length(value[!indx,])==0){
              circos.genomicRect(region[indx,], value[indx,], ytop.column = 1, ybottom = tkbarvalue, col=tkbarcol1, border = NA, ...)
            }else if(length(value[indx,])==0 && length(value[!indx,])!=0){
              circos.genomicRect(region[!indx,], value[!indx,], ytop.column = 1, ybottom =  tkbarvalue, col=tkbarcol2, border = NA, ...)
            }
          }
        }
        if(length(columns)==1 && tkbardir==1 && coltypeTrack==2){
          if(nchar(tklinecolor[1])!=0){
            xlim <- get.cell.meta.data("xlim")
            ylim <- get.cell.meta.data("ylim")
            for(k in 1:length(tklinecoord)){
              y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
              circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
            }
          }
          circos.genomicRect(region, value, numeric.column=columns-3, ytop.column = 1, ybottom = min(data.TT[,4]), col=tkcolor[1], border = NA, ...)
        }
      })
      takindx <- get.current.track.index()
      if(length(columns)==1 && tkbardir==1 && coltypeTrack==3 && ncol(data.TTC)>=6 && ("cols" %in% colnames(data.TTC))){
        lapply(unique(data.TTC[,1]),function(x){
          circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
          if(nchar(tklinecolor[1])!=0){
            xlim <- get.cell.meta.data("xlim")
            ylim <- get.cell.meta.data("ylim")
            for(k in 1:length(tklinecoord)){
              y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
              circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
            }
          }
          dat <- data.TTC[data.TTC[,1] %in% x,]
          circos.rect(xleft=dat[,2], xright=dat[,3],ytop=dat[,4],ybottom=rep(get.cell.meta.data("ylim")[1],nrow(dat)), col=adjustcolor(dat$cols,alpha.f = tktransparency), border = NA)
        })
      }else if(length(columns)==1 && tkbardir==1 && coltypeTrack==1 && ncol(data.TTC)>=6 && ("cols" %in% colnames(data.TTC))){
        lapply(unique(data.TTC[,1]),function(x){
          circos.updatePlotRegion(sector.index = x, track.index=takindx, bg.col = tkbgcol[which(unique(data.C[,1])==x)], bg.border = tkborder)
          if(nchar(tklinecolor[1])!=0){
            xlim <- get.cell.meta.data("xlim")
            ylim <- get.cell.meta.data("ylim")
            for(k in 1:length(tklinecoord)){
              y1 <- as.numeric(quantile(ylim,probs=tklinecoord[k]))
              circos.lines(x=xlim,y=c(y1,y1), col=tklinecolor[k], lwd=0.1)
            }
          }
          dat <- data.TTC[data.TTC[,1] %in% x,]
          circos.rect(xleft=dat[,2], xright=dat[,3],ytop=dat[,4],ybottom=rep(get.cell.meta.data("ylim")[1],nrow(dat)), col=adjustcolor(dat$cols,alpha.f = tktransparency), border = NA)
        })
      }
    }
    
    if(1 %in% unlist(tra_yaxis) | trac_index == "Yes"){
      circos.updatePlotRegion(
        sector.index = data.C[,1][nrow(data.C)],
        track.index = get.current.track.index(),
        bg.border = NA,
        bg.col = NA
      )
      fanwei <- get.cell.meta.data("ylim",sector.index = get.current.sector.index(), track.index = get.current.track.index())
      fanwei[fanwei < 0] <- ceiling(fanwei[fanwei < 0]*100)/100
      fanwei[fanwei > 0] <- floor(fanwei[fanwei > 0 ]*100)/100
      if(tra_yaxis[i]==1){
        circos.yaxis(
          side = "left",
          tick = TRUE,
          at = fanwei,
          sector.index = get.all.sector.index()[1],
          labels.niceFacing = FALSE,
          labels.cex = 0.5
        )
      }
      if(trac_index == "Yes"){
        if(1 %in% unlist(tra_yaxis) & trac_index == "Yes"){
          traxlim <- get.cell.meta.data("xlim", sector.index = get.current.sector.index(), track.index = get.current.track.index())
          circos.text(
            sector.index = get.current.sector.index(),
            track.index = get.current.track.index(),
            x = round((traxlim[2]-traxlim[1])/3),
            y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
            labels = as.character(i),
            facing = "downward"
          )
        }else{
          circos.text(
            sector.index = get.current.sector.index(),
            track.index = get.current.track.index(),
            x = get.cell.meta.data("xcenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
            y = get.cell.meta.data("ycenter", sector.index = get.current.sector.index(), track.index = get.current.track.index()),
            labels = as.character(i),
            facing = "downward"
          )
        }
      }
    }
    if(lab_inf[i]){
      if(labels_inf[[2]]=="inside"){
        if(ncol(data.NN) == 4){
          circos.genomicLabels(data.NN, labels.column = 4, connection_height = lab_height/4, labels_height = (lab_height/4)*3 , cex =labelsize  , line_col = "#000000" , col = labels_inf[[4]] , padding = 0 , track.margin = c(0,0), side = "inside")
        }else{
          circos.genomicLabels(data.NN, labels.column = 4, connection_height = lab_height/4, labels_height = (lab_height/4)*3 , cex =labelsize  , line_col = "#000000" , col = data.NN[,5] , padding = 0 , track.margin = c(0,0), side = "inside")
        }
      }
    }
    lgdplot[[i]] <- legendplot(tktype = tktype,data.TT = data.TT,data.TT_old = data.TT_old,i=i,legendpos = legendpos,tkcolor = tkcolor , addlegend = addlegend)
  }
}

if(!is.null(data.L)){
  
  if(ncol(data.L)==7){
    names(data.L)[7] <- "color"
  }
  rou <- get_most_inside_radius()
  # rou <- rou[1]+0.05
  rou <- rou[1]
  if(colformatLinks!=3){ #not gradual color
    if(colorLinks==2){ #specific color
      splitcol <- ":" %in% unlist(strsplit(selcolorLinks,""))
      if(ncol(data.L)==7 && splitcol){
        #if(ncol(data.L)==7 && colnames(data.L)[7]=="color" && splitcol){
        data.L$num <- 1:nrow(data.L)
        
        selcolorLinks <- unlist(strsplit(selcolorLinks,";"))
        selcolorLinks <- data.frame(id=selcolorLinks,stringsAsFactors=F)
        selcolorLinks$group <- gsub("\\\\:.*","",selcolorLinks$id)
        selcolorLinks$cols <- gsub(".*\\\\:","",selcolorLinks$id)
        selcolorLinks$group <- gsub(" ","",selcolorLinks$group)
        selcolorLinks$cols <- gsub(" ","",selcolorLinks$cols)
        data.LC <- merge(data.L,selcolorLinks,by.x="color",by.y="group",all.x=T)
        data.LC$cols[is.na(data.LC$cols)] <- "grey"
          data.LC <- data.LC[order(data.LC$num),]
          data.LC$num <- NULL
          rownames(data.LC) <- NULL
          data.L$num <- NULL
          colLinks <- adjustcolor(data.LC$cols, alpha.f = transparencyLinks)
          lgdcol <- data.LC[,c("color","cols")]
          lgdcol <- lgdcol[order(lgdcol[,1]),]
          if(addlegend == "Yes"){
            if(legendpos == "Right"){
              lgdplot[[length(lgdplot)+1]] <- Legend(
                at = unique(lgdcol[,1]),
                legend_gp = gpar(fill = unique(lgdcol[,2])),
                nrow = 6,
                title = paste0("links")
              )
            }else{
              lgdplot[[length(lgdplot)+1]] <- Legend(
                at = unique(lgdcol[,1]),
                legend_gp = gpar(fill = unique(lgdcol[,2])),
                ncol = 4,
                by_row = TRUE,
                title = paste0("links"),
                direction = "horizontal"
              )
            }
          }
      }else if((ncol(data.L)==6 | ncol(data.L)==7) && !splitcol){
        colLinks <- adjustcolor(selcolorLinks[1], alpha.f = transparencyLinks)
      }
      data.L1 <- data.L[,c(1:3)]
      data.L2 <- data.L[,c(4:6)]
      circos.genomicLink(data.L1, data.L2, rou = rou, col = colLinks, border = NA , reduce_to_mid_line = midplot)
    }else{
      if(ncol(data.L)==7 && colnames(data.L)[7]=="color"){
        groupnum <- length(unique(data.L[,7]))
        linkscolor.export <- rand_color(groupnum)
        randcolorLinks <- data.frame(group=unique(data.L[,7]), cols=linkscolor.export, stringsAsFactors=F)
        names(randcolorLinks)[1] <- "group"
        data.LC <- merge(data.L,randcolorLinks,by.x="color",by.y="group",all.x=T)
        colLinks <- adjustcolor(data.LC$cols, alpha.f = transparencyLinks)
      }
      data.L1 <- data.L1[,c(1:3)]
      data.L2 <- data.L2[,c(1:3)]
      if(ncol(data.L)==7 && colnames(data.L)[7]=="color"){
        circos.genomicLink(data.L1, data.L2, rou = rou, col = colLinks, border = NA , reduce_to_mid_line = midplot)
        lgdcol <- data.LC[,c("color","cols")]
        lgdcol <- lgdcol[order(lgdcol[,1]),]
        if(colformatLinks == 2){
          if(addlegend == "Yes"){
            if(legendpos == "Right"){
              lgdplot[[length(lgdplot)+1]] <- Legend(
                at = unique(lgdcol[,1]),
                legend_gp = gpar(fill = unique(lgdcol[,2])),
                nrow = 6,
                title = paste0("links")
              )
            }else{
              lgdplot[[length(lgdplot)+1]] <- Legend(
                at = unique(lgdcol[,1]),
                legend_gp = gpar(fill = unique(lgdcol[,2])),
                ncol = 4,
                by_row = TRUE,
                title = paste0("links"),
                direction = "horizontal"
              )
            }
          }
        }
      }else{
        linkscolor.export <- rand_color(nrow(data.L1), transparency = 1-transparencyLinks)
        circos.genomicLink(data.L1, data.L2, rou = rou, col = linkscolor.export, border = NA , reduce_to_mid_line = midplot)
      }
      
    }
  }else{ #gradual color
    if(ncol(data.L)==7 && colnames(data.L)[7]=="color"){
      break1 <- min(as.numeric(as.matrix(data.L[,-c(1:6)])))
      break2 <- max(as.numeric(as.matrix(data.L[,-c(1:6)])))
      midpoint <- (break1+break2)/2
      f <- colorRamp2(breaks = c(break1, midpoint, break2), colors = gracolinks)
      colLinks <- f(data.L$color)
    }else if((ncol(data.L)==6 | ncol(data.L)==7)){
      linkscolor.export <- rand_color(1)
      colLinks <- linkscolor.export
    }
    
    if(!(ncol(data.L)==6)){
      data.L1 <- data.L[,c(1:3)]
      data.L2 <- data.L[,c(4:6)]
      circos.genomicLink(data.L1, data.L2, rou = rou, col = colLinks, border = NA , reduce_to_mid_line = midplot)
    }
    if(addlegend == "Yes"){
      if(legendpos == "Right"){
        lgdplot[[length(lgdplot)+1]] <- Legend(
          col_fun = f,
          title = paste0("links")
        )
      }else{
        lgdplot[[length(lgdplot)+1]] <- Legend(
          col_fun = f,
          title = paste0("links"),
          direction = "horizontal"
        )
      }
    }
  }
}

lgdplot_cache <- NULL
if(!is.null(unlist(lgdplot))){ #remove empty values
  for (k in 1:length(lgdplot)){
    if(is.null(lgdplot[[k]])){
      lgdplot_cache <- c(lgdplot_cache,k)
    }
  }
  if(!is.null(lgdplot_cache)){
    lgdplot <- lgdplot[-c(lgdplot_cache)]
  }
}

if(exists("hlt_data")){
  if(length(hlt_data) != 0){
    lapply(1:length(hlt_data[,1]), function(k){
      kdata <- hlt_data[k,]
      draw.sector(
        start.degree = circlize(as.numeric(kdata[2]), 0, sector.index = kdata[1], track.index = 1)[1],
        end.degree = circlize(as.numeric(kdata[3]), 0, sector.index = kdata[1], track.index = 1)[1],
        rou1 = 1,
        col =  as.character(kdata[4])
      )
    })
  }
}
circos.clear()
if(addlegend == "Yes"){
  if(legendpos == "Right"){
      upViewport()
      h <- dev.size()[2]
      if(length(lgdplot) != 0){
        draw(
          packLegend(
            list = lgdplot,
            max_height = unit(0.9*h,"inch")
          ),
          x = circle_size,
          just = "left"
        )
      }
  }else{
      upViewport()
      h <- dev.size()[1]
      if(length(lgdplot) != 0){
        draw(
          packLegend(
            list = lgdplot,
            max_width = unit(0.9*h,"inch"),
            direction = "horizontal"
          ),
          y = unit(1,"npc")-circle_size,
          just = "top"
        )
      }
  }
}else{
    upViewport()
}
dev.off()
    '
  ),file="code.R",append=TRUE,sep="\n"
)
  setwd("../")
  zip::zip(zipfile = "./script.zip",files = "script",recurse = TRUE)
}