Circlize plot - Does not draw clear lines

445 views Asked by At

I have this script, which results in this really pretty circlize plot.

install.packages('circlize',  repos = "http://cran.rstudio.com/" )  
install.packages('data.table', repos = "http://cran.rstudio.com/")

library(circlize)
library(data.table)




karyo <- fread(colClasses = c("character", "numeric",
                              "numeric", "character", "character"),
              "ssa01  0          45670927   1p    gneg
               ssa01  45670927   102921759  1qa   gpos50
               ssa01  102921759  158994226  1qb   gneg
               ssa02  0          37194884   2p    gpos50
               ssa02  37194884   72887026   2q    gneg
               ssa03  0          53325515   3p    gpos50
               ssa03  53325515   92451775   3q    gneg
               ssa04  0          24769547   4p    gpos50
               ssa04  24769547   82368206   4q    gneg
               ssa05  0          44366638   5p    gpos50
               ssa05  44366638   80452451   5q    gneg
               ssa06  0          44681795   6p    gpos50
               ssa06  44681795   87037992   6q    gneg
               ssa07  0          29822415   7p    gpos50
               ssa07  29822415   58755209   7q    gneg
               ssa08  -15000000  0          rDNA  gpos50
               ssa08  0          26442203   8q    gneg
               ssa09  0          48560009   9qa   gpos50
               ssa09  48560009   92800824   9qb   gneg
               ssa09  92800824   141678924  9qc   gpos50
               ssa10  0          50712303   10qa  gneg
               ssa10  50712303   116119932  10qb  gpos50
               ssa11  0          44252459   11qa  gneg
               ssa11  44252459   93835980   11qb  gpos50
               ssa12  0          31366442   12qa  gneg
               ssa12  31366442   91841354   12qb  gpos50
               ssa13  0          46825690   13qa  gneg
               ssa13  46825690   107755956  13qb  gpos50
               ssa14  0          45561258   14qa  gneg
               ssa14  45561258   93876043   14qb  gpos50
               ssa15  0          53413608   15qa  gneg
               ssa15  53413608   103934464  15qb  gpos50
               ssa16  0          56974611   16qa  gneg
               ssa16  56974611   87752276   16qb  gpos50
               ssa17  0          31608156   17qa  gneg
               ssa17  31608156   57636407   17qb  gpos50
               ssa18  0          35154609   18qa  gneg
               ssa18  35154609   70656513   18qb  gpos50
               ssa19  0          38868197   19qa  gneg
               ssa19  38868197   82958981   19qb  gpos50
               ssa20  0          40962200   20qa  gneg
               ssa20  40962200   86771154   20qb  gpos50
               ssa21  0  58011450  21  gneg
               ssa22  0  63437620  22  gneg
               ssa23  0  49935705  23  gneg
               ssa24  0  48640078  24  gneg
               ssa25  0  51465665  25  gneg
               ssa26  0  47887414  26  gneg
               ssa27  0  43932925  27  gneg
               ssa28  0  39590257  28  gneg
               ssa29  0  42472244  29  gneg")

cells <- fread("ssa01  0          6843616    ssa09  48497608   43657580   cigene_21_21
                ssa01  6946687    8594997    ssa09  4749099    6657810    cigene_21_21
               ssa01  8761342    9088168    ssa09  270912     155778     cigene_21_21
               ssa01  9158929    44448014   ssa09  6779314    43613554   cigene_21_21
               ssa01  44464323   45490398   ssa09  4717661    3644783    cigene_21_21
               ssa01  45851456   71976385   ssa18  2845971    28112522   cigene_21_21
               ssa01  72879911   73557255   ssa28  34194749   34694334   cigene_21_21
               ssa01  73558102   88357101   ssa28  13189995   24991882   cigene_21_21
               ssa01  88393814   90591948   ssa18  28120583   30993608   cigene_21_21
               ssa01  90719401   99584790   ssa28  25023445   33200917   cigene_21_21
               ssa01  106258728  106674403  ssa13  101000184  100406704  cigene_21_21
               ssa01  108338976  115563273  ssa13  98799652   90773398   cigene_21_21
               ssa01  116910054  121177528  ssa13  90772566   85845771   cigene_21_21
               ssa01  121201942  142747918  ssa11  72697822   49568288   cigene_21_21
               ssa01  144877136  145895090  ssa11  45581503   44384062   cigene_21_21
               ssa01  146151174  149925087  ssa11  46202668   49405861   cigene_21_21
               ssa01  153001283  156331869  ssa13  101054432  107355605  cigene_21_21
               ssa01  156444877  158990959  ssa13  107565689  103272042  cigene_21_21
               ssa02  0          37183267   ssa05  80452451   44387533   cigene_21_3
               ssa02  37206501   72887026   ssa12  31357447   0          cigene_21_3
               ssa03  364935     2747440    ssa14  47030385   45149425   cigene_21_5
               ssa03  1122363    4195588    ssa14  50783037   48525974   cigene_21_5
               ssa03  5212593    44547960   ssa14  4919797    44703263   cigene_21_5
               ssa03  44841249   47252771   ssa13  16080251   13167425   cigene_21_5
               ssa03  47268596   48018298   ssa05  18588680   19685350   cigene_21_5
               ssa03  48048600   52186527   ssa23  780875     3112448    cigene_21_5
               ssa03  52649577   53313933   ssa23  0          757933     cigene_21_5
               ssa03  53337097   92451775   ssa06  44497429   0          cigene_21_5
               ssa04  0          15341445   ssa08  26055991   7829984    cigene_21_7
               ssa04  15621004   24719742   ssa08  0          7829984    cigene_21_7
               ssa04  24819351   28652971   ssa10  53060574   52329613   cigene_21_7
               ssa04  29656081   31102993   ssa10  58161834   59807071   cigene_21_7
               ssa04  31198629   38035923   ssa11  84682864   75019162   cigene_21_7
               ssa04  38138247   39124546   ssa11  74095227   72787793   cigene_21_7
               ssa04  39186468   77325076   ssa13  85759651   46939698   cigene_21_7
               ssa04  77398345   77760129   ssa10  60384152   59876005   cigene_21_7
               ssa04  77892210   82302510   ssa11  84885734   93658279   cigene_21_7
               ssa05  0          16783642   ssa09  91697818   76066628   cigene_21_8
               ssa05  19797082   44345743   ssa09  48622409   74756512   cigene_21_8
               ssa06  44866160   63240934   ssa15  20326123   38376064   cigene_21_9
               ssa06  63349385   64012747   ssa15  50808339   50100071   cigene_21_9
               ssa06  64037873   79441812   ssa15  54460690   41235676   cigene_21_9
               ssa06  79475283   86773188   ssa15  20195818   10834079   cigene_21_9
               ssa07  0          3169069    ssa18  70509147   67091998   cigene_21_10
               ssa07  4717043    12776862   ssa18  39315610   48836242   cigene_21_10
               ssa07  12782626   13557017   ssa18  49636752   48855526   cigene_21_10
               ssa07  13559138   29545108   ssa18  49668386   66913352   cigene_21_10
               ssa07  30099722   58321216   ssa17  31965773   57540086   cigene_21_10
               ssa09  372600     2523774    ssa11  4568887    112654     cigene_21_11
               ssa09  91740478   92636663   ssa20  1005209    0          cigene_21_11
               ssa09  92964984   138116665  ssa20  40968350   80791454   cigene_21_11
               ssa10  6516854    9083984    ssa16  55080990   51872091   cigene_21_12
               ssa10  9912722    19185602   ssa16  49756067   37311992   cigene_21_12
               ssa10  19292640   49094993   ssa23  33285371   3126275    cigene_21_12
               ssa10  64886930   73893160   ssa23  43112254   33350463   cigene_21_12
               ssa10  73919063   111715556  ssa16  37287700   0          cigene_21_12
               ssa10  111895177  116088268  ssa23  43217075   48795685   cigene_21_12
               ssa11  4847201    10220648   ssa26  4517045    10259102   cigene_21_13
               ssa11  10245832   14795494   ssa26  15137018   10259113   cigene_21_13
               ssa11  14804709   44120855   ssa26  15138210   47884620   cigene_21_13
               ssa12  31375436   35524288   ssa22  4152549    0          cigene_21_14
               ssa12  35542504   83373635   ssa22  51996806   4158499    cigene_21_14
               ssa12  83746172   91723600   ssa22  52043634   63200112   cigene_21_14
               ssa13  0          7174925    ssa15  103591384  95503623   cigene_21_15
               ssa13  7175841    10333631   ssa15  70707775   73414810   cigene_21_15
               ssa13  16120805   22268064   ssa15  56018876   62832409   cigene_21_15
               ssa13  22331525   26381128   ssa15  79477647   75176327   cigene_21_15
               ssa13  26404080   28835918   ssa15  65671872   63102592   cigene_21_15
               ssa13  28929493   31820350   ssa15  68453835   66050303   cigene_21_15
               ssa13  31824704   43526833   ssa15  94392728   79480752   cigene_21_15
               ssa13  43530010   44484971   ssa15  95236934   95503623   cigene_21_15
               ssa13  44567360   46711682   ssa15  70671711   68607378   cigene_21_15
               ssa14  47114391   48433466   ssa27  5232718    3582537    cigene_21_17
               ssa14  51604585   54683245   ssa27  0          3582218    cigene_21_17
               ssa14  54767228   71598820   ssa27  5665886    22486246   cigene_21_17
               ssa14  71685703   85987446   ssa27  36363992   22551628   cigene_21_17
               ssa14  86001684   93798132   ssa27  36387980   43151030   cigene_21_17
               ssa15  0          1777952    ssa24  1793564    468390     cigene_21_18
               ssa15  2568097    10380129   ssa24  40547260   47817636   cigene_21_18
               ssa16  58868231   87752276   ssa17  1168292    31250539   cigene_21_2
               ssa18  514148     2792680    ssa19  66054142   63968520   cigene_21_1
               ssa19  0          4398723    ssa19  55763317   51509249   cigene_21_4
               ssa19  8209966    14853065   ssa29  27579285   36466443   cigene_21_4
               ssa19  16742039   32839049   ssa29  0          16247061   cigene_21_4
               ssa19  32865538   38853352   ssa28  39589923   34718551   cigene_21_4
               ssa19  38883042   51480644   ssa28  13108997   0          cigene_21_4
               ssa19  60868480   61508995   ssa28  7219775    7725975    cigene_21_4
               ssa19  62735943   63378768   ssa19  4901716    7586712    cigene_21_4
               ssa19  66256876   78280992   ssa29  27576310   16856802   cigene_21_4
               ssa19  78320578   82952330   ssa29  36474059   42376689   cigene_21_4
               ssa20  1080524    4216238    ssa24  4661034    1836998    cigene_21_6
               ssa20  5246663    29618716   ssa24  40253535   14198356   cigene_21_6
               ssa20  29625403   30290372   ssa24  13580940   14165236   cigene_21_6
               ssa20  30290972   40956049   ssa24  13547900   4663342    cigene_21_6
               ssa21  0          5534565    ssa25  4156252    0          grey
               ssa21  5593276    17047359   ssa25  4658816    14846060   grey
               ssa21  17069345   51519898   ssa25  47016726   14853562   grey
               ssa21  51921340   58011450   ssa25  47405282   51465665   grey")
names(cells) <- c("chr1", "start1", "end1", "chr2", "start2", "end2", "col")
cells <- data.frame(cells)

regions_annot <- fread("ssa01  0          10000000   ssa01  region_telomeric
                      ssa01  10000000   148994226  ssa01  region_normal
                      ssa01  148994226  158994226  ssa01  region_telomeric
                      ssa02  0          12000000   ssa02  region_collapsed
                      ssa02  12000001   51206500   ssa02  region_highid
                      ssa02  51206500   72887026   ssa02  region_collapsed
                      ssa03  0          10000000   ssa03  region_telomeric
                      ssa03  10000000   50048600   ssa03  region_normal
                      ssa03  50048600   69337097   ssa03  region_highid
                      ssa03  69337097   92451775   ssa03  region_collapsed
                      ssa04  0          11000000   ssa04  region_collapsed
                      ssa04  11000001   25621004   ssa04  region_highid
                      ssa04  25621004   72368206   ssa04  region_normal
                      ssa04  72368206   82368206   ssa04  region_telomeric
                      ssa05  0          8000000    ssa05  region_highid
                      ssa05  8000000    43452452   ssa05  region_normal
                      ssa05  43452452   69452452   ssa05  region_highid
                      ssa05  69452452   80452451   ssa05  region_collapsed
                      ssa06  0          27497429   ssa06  region_collapsed
                      ssa06  27497429   44866161   ssa06  region_highid
                      ssa06  44866161   77037992   ssa06  region_normal
                      ssa06  77037992   87037992   ssa06  region_telomeric
                      ssa07  0          10000000   ssa07  region_telomeric
                      ssa07  10000000   31099723   ssa07  region_normal
                      ssa07  31099723   47099723   ssa07  region_highid
                      ssa07  47099723   58755209   ssa07  region_collapsed
                      ssa08  0          16055992   ssa08  region_highid
                      ssa08  16055992   26442203   ssa08  region_collapsed
                      ssa09  0          84697819   ssa09  region_normal
                      ssa09  84697819   141678924  ssa09  region_highid
                      ssa10  0          106119932  ssa10  region_normal
                      ssa10  106119932  116119932  ssa10  region_telomeric
                      ssa11  0          6847202    ssa11  region_normal
                      ssa11  6847202    28804710   ssa11  region_highid
                      ssa11  28804710   44804709   ssa11  region_collapsed
                      ssa11  44804709   83835980   ssa11  region_normal
                      ssa11  83835980   93835980   ssa11  region_telomeric
                      ssa12  0          16357447   ssa12  region_collapsed
                      ssa12  16357447   30357447   ssa12  region_highid
                      ssa12  30357447   81841354   ssa12  region_normal
                      ssa12  81841354   91841354   ssa12  region_telomeric
                      ssa13  0          97755956   ssa13  region_normal
                      ssa13  97755956   107755956  ssa13  region_telomeric
                      ssa14  0          83876043   ssa14  region_normal
                      ssa14  83876043   93876043   ssa14  region_telomeric
                      ssa15  0          93934464   ssa15  region_normal
                      ssa15  93934464   103934464  ssa15  region_telomeric
                      ssa16  0          58868232   ssa16  region_normal
                      ssa16  58868232   73868231   ssa16  region_highid
                      ssa16  73868231   87752276   ssa16  region_collapsed
                      ssa17  0          16168292   ssa17  region_highid
                      ssa17  16168292   24168292   ssa17  region_collapsed
                      ssa17  24168293   48965773   ssa17  region_highid
                      ssa17  48965773   57636407   ssa17  region_collapsed
                      ssa18  0          60656513   ssa18  region_normal
                      ssa18  60656513   70656513   ssa18  region_telomeric
                      ssa19  0          72958981   ssa19  region_normal
                      ssa19  72958981   82958981   ssa19  region_telomeric
                      ssa20  0          40968350   ssa20  region_normal
                      ssa20  40968350   86771154   ssa20  region_highid
                      ssa21  0          48011450   ssa21  region_normal
                      ssa21  48011450   58011450   ssa21  region_telomeric
                      ssa22  0          53437620   ssa22  region_normal
                      ssa22  53437620   63437620   ssa22  region_telomeric
                      ssa23  0          39935705   ssa23  region_normal
                      ssa23  39935705   49935705   ssa23  region_telomeric
                      ssa24  0          38640078   ssa24  region_normal
                      ssa24  38640078   48640078   ssa24  region_telomeric
                      ssa25  0          41465665   ssa25  region_normal
                      ssa25  41465665   51465665   ssa25  region_telomeric
                      ssa26  0          6517046    ssa26  region_normal
                      ssa26  6517046    29138210   ssa26  region_highid
                      ssa26  29138210   47887414   ssa26  region_collapsed
                      ssa27  0          33932925   ssa27  region_normal
                      ssa27  33932925   43932925   ssa27  region_telomeric
                      ssa28  0          29590257   ssa28  region_normal
                      ssa28  29590257   39590257   ssa28  region_telomeric
                      ssa29  0          32472244   ssa29  region_normal
                      ssa29  32472244   42472244   ssa29  region_telomeric")
names(regions_annot) <- c("chr", "start", "end", "value", "col")

getCol <- function(r, g, b, alpha=0.8) {
    rgb(r/255, g/255, b/255, alpha=alpha)
}

getColName <- function(col.in, alpha=0.8) {
    co = as.numeric(col2rgb(col.in))
    rgb(red=co[1]/255, green=co[2]/255, blue=co[3]/255, alpha=alpha)
}

getColFromList <- function(color) {
    if (!is.function(get(color))) { return(getColName(color)) } # "grey", "black"
    else { return(getColName(color)) } #cigene_21_1"
}

alpha <- 0.8
cigene_21_1 <- getCol(119,17,85,alpha)
cigene_21_2 <- getCol(170,68,136,alpha)
cigene_21_3 <- getCol(204,153,187,alpha)
cigene_21_4 <- getCol(17,68,119,alpha)
cigene_21_5 <- getCol(68,119,170,alpha)
cigene_21_6 <- getCol(119,170,221,alpha)
cigene_21_7 <- getCol(17,119,119,alpha)
cigene_21_8 <- getCol(68,170,170,alpha)
cigene_21_9 <- getCol(119,204,204,alpha)
cigene_21_10 <- getCol(17,119,68,alpha)
cigene_21_11 <- getCol(68,170,119,alpha)
cigene_21_12 <- getCol(136,204,170,alpha)
cigene_21_13 <- getCol(119,119,17,alpha)
cigene_21_14 <- getCol(170,170,68,alpha)
cigene_21_15 <- getCol(221,221,119,alpha)
cigene_21_16 <- getCol(119,68,17,alpha)
cigene_21_17 <- getCol(170,119,68,alpha)
cigene_21_18 <- getCol(221,170,119,alpha)
cigene_21_29 <- getCol(119,17,34,alpha)
cigene_21_20 <- getCol(170,68,85,alpha)
cigene_21_21 <- getCol(221,119,136,alpha)

alpha <- 1
region_telomeric <- getCol(255,255,191,alpha)
region_normal <- getCol(102,194,165,alpha)
region_collapsed <- getCol(213,62,79,alpha)
region_highid <- getCol(244,109,67,alpha)


regions_annot.bed <- data.frame(regions_annot)[1:3]
regions_annot.bed$value1 <- rep(1, nrow(regions_annot.bed))
regions_annot.bed$value2 <- rep(0, nrow(regions_annot.bed))
regions_annot.bed$col <- as.character(unlist(mget(regions_annot$col, ifnotfound = list(function(x) getColFromList(x)))))

cells$col <- as.character(unlist(mget(cells$col, ifnotfound = list(function(x) getColFromList(x)))))

par(mar = c(1, 1, 1, 1))
circos.par(start.degree = 90)
circos.initializeWithIdeogram(data.frame(karyo), track.height=0.05, ideogram.height = 0.03, chromosome.index = sort(unique(karyo$V1)))
circos.genomicTrackPlotRegion(regions_annot.bed, track.height=0.02, panel.fun = function(region,  value,  ...) {
        circos.genomicRect(region, value, col = value$col, border = NA, ...)
})
circos.genomicLink(cells[,1:3], cells[,4:6], col = cells$col, border = NA)

I replaced what is in the cells variable with more observations, which are in this file. However, I think due to too many observations I get this pretty crappy plot.

I want the plot drawn with the data from the file (more observations) to more resemble the one in the first image. I would like that the lines in the plot don't turn out like this, because of lines that are overlapping with each other.

Does anyone have any suggestions on this?

1

There are 1 answers

1
Zuguang Gu On BEST ANSWER

The reason is the width of each link is too small compared to the width of contigs (or chromosomes), which makes the link too thin. Links are actually drawn as polygons and a polygon has a border and filled area. Borders are lines with certain line width in R. In some graphic device (like png), when you set border = NA to remove the borders, the remaining filled area can be even thinner and can be broken and that is why you see there are so many segments.

To solve this problem, you can do either:

  1. set the end positions to be the same as the start positions so that each link is plotted as lines while not polygons
  2. set border = cells$col to keep the borders.

Following is generated from the second method:

enter image description here