andrewparkermorgan / popcorn

MIT License
6 stars 0 forks source link

width of migration edge #3

Closed imaa9 closed 1 year ago

imaa9 commented 1 year ago

Hey Andrew, I'm plotting a treemix plot with popcorn (it's so great!) and I want to change the width of the lines -- both the lines making up the tree (to something like linewidth = 1) and the migration edge curve (this I would like to modify according to the weight, similar to color, something like aes(linewidth =weight *5)). I can't quite figure out how to implement these modification, would you be able to make any suggestions regarding this?

Thanks a heap, Inbar

andrewparkermorgan commented 1 year ago

The output of plot_treemix() is tricky to customize because the data comes in two parts, the nodes of the tree (both internal nodes and tips) and the edges (both the backbone of the tree and the migration edges). I just pushed a small modification that adds a parameter branch.width that allows to change the width of the lines making up the tree, and also returns the tips and edges of the tree silently as attributes of the plot.

To change the appearance of the migration edges you will need to re-create them yourself but this is not too hard. Read the TreeMix output and create the plot, but without migration edges.

tmx <- read_treemix("input_file")
p <- plot_treemix(tmx, plot.migration = FALSE)

Now grab the edges of the tree from the output.

edges <- attr(p, "edges")
head(edges)

You will get something like this:

# A tibble: 6 × 6
  from.x from.y   to.x  to.y type    weight
   <dbl>  <dbl>  <dbl> <dbl> <fct>    <dbl>
1 0.0588  0.455 0.0733 0.409 NOT_MIG      1
2 0.0499  0.773 0.0572 0.795 NOT_MIG      1
3 0.0499  0.773 0.0530 0.545     MIG    0.3
4 0.0530  0.545 0.0555 0.591 NOT_MIG      1
5 0.0530  0.545 0.0530 0.5   NOT_MIG      1
6 0.0530  0.5   0.0588 0.455 NOT_MIG      1

Now you can manually add the migration edges with the desired appearance.

p + 
    geom_curve(data = subset(edges, type == "MIG"),
               aes(x = from.x, y = from.y, xend = to.x, yend = to.y, linewidth = weight),
               curvature = 0.25,
               arrow = grid::arrow(length = grid::unit(6, "points"), type = "closed"))

The code for plot_treemix() is just an adaptation of plotting code in the original TreeMix pacakage to work with ggplot2. It would have been nice to instead use an existing package for plotting phylogenetic trees (ape, phytools, ggtree etc), but dealing with migration edges would take some work and I never had time to tackle it.

imaa9 commented 1 year ago

Hey Andrew,

Thanks so much for the quick and very helpful reply! When I try to get the newest version of popcorn it says ✔ checking for file 'C:\Users\inbar\AppData\Local\Temp\RtmpIlJeq2\remotes7786fb23001\andrewparkermorgan-popcorn-85d41ff/DESCRIPTION' ... ─ preparing 'popcorn' ✔ checking DESCRIPTION meta-information ... Warning: newline within quoted string at plot_treemix.Rd:30 Error in parse_Rd ("C:/Users/inbar/AppData/Local/Temp/RtmpM1mXPr/Rbuild60145541506b/popcorn/man/plot_treemix.Rd", : Unexpected end of input (in " quoted string opened at plot_treemix.Rd:30:22) Execution halted Error: Failed to install 'popcorn' from GitHub: ! System command 'Rcmd.exe' failed

Is this a problem on my end or is there something up with the update? Sorry for this rather basic question.

Thanks, Inbar

Inbar Maayan PhD Candidate | she/her Department of Organismic and Evolutionary Biology Harvard University @.*** www.inbarmaayan.com

On Tue, Jul 4, 2023 at 1:11 PM Andrew Parker Morgan < @.***> wrote:

The output of plot_treemix() is tricky to customize because the data comes in two parts, the nodes of the tree (both internal nodes and tips) and the edges (both the backbone of the tree and the migration edges). I just pushed a small modification that adds a parameter branch.width that allows to change the width of the lines making up the tree, and also returns the tips and edges of the tree silently as attributes of the plot.

To change the appearance of the migration edges you will need to re-create them yourself but this is not too hard. Read the TreeMix output and create the plot, but without migration edges.

tmx <- read_treemix("input_file") p <- plot_treemix(tmx, plot.migration = FALSE)

Now grab the edges of the tree from the output.

edges <- attr(p, "edges") head(edges)

You will get something like this:

A tibble: 6 × 6

from.x from.y to.x to.y type weight

1 0.0588 0.455 0.0733 0.409 NOT_MIG 1 2 0.0499 0.773 0.0572 0.795 NOT_MIG 1 3 0.0499 0.773 0.0530 0.545 MIG 0.3 4 0.0530 0.545 0.0555 0.591 NOT_MIG 1 5 0.0530 0.545 0.0530 0.5 NOT_MIG 1 6 0.0530 0.5 0.0588 0.455 NOT_MIG 1 Now you can manually add the migration edges with the desired appearance. p + geom_curve(data = subset(edges, type == "MIG"), aes(x = from.x, y = from.y, xend = to.x, yend = to.y, linewidth = weight), curvature = 0.25, arrow = grid::arrow(length = grid::unit(6, "points"), type = "closed")) The code for plot_treemix() is just an adaptation of plotting code in the original TreeMix pacakage to work with ggplot2. It would have been nice to instead use an existing package for plotting phylogenetic trees (ape, phytools, ggtree etc), but dealing with migration edges would take some work and I never had time to tackle it. — Reply to this email directly, view it on GitHub , or unsubscribe . You are receiving this because you authored the thread.Message ID: ***@***.***>
andrewparkermorgan commented 1 year ago

It looks like a problem in parsing the documentation files. I regenerated them and pushed a new commit; maybe that will fix it?

imaa9 commented 1 year ago

Cool the install worked! and branch.width works beautifully, thanks a heap!

Unfortunately it doesn't look like my ggplot object has "edges" as an attribute: image

the object that I use to create it does seem to have the edges attribute, but it has seven columns titled V1 through V7, and I can't quite tell if the column names you have in your dataframe correspond to mine (this is the dataframe for two migration edges):

d_grh2$edges V1 V2 V3 V4 V5 V6 V7 1 136 135 1.484310e-03 1.000000 NOT_MIG 0 1.000000 2 304 412 1.531620e-04 1.000000 NOT_MIG 0 1.000000 3 472 76 5.376060e-04 1.000000 NOT_MIG 0 0.456275 4 604 603 9.712070e-04 1.000000 NOT_MIG 0 1.000000 5 212 1004 2.694460e-04 1.000000 NOT_MIG 0 1.000000 6 1004 211 1.037520e-03 1.000000 NOT_MIG 0 1.000000 7 1004 1096 2.515700e-04 1.000000 NOT_MIG 0 1.000000 8 1096 1003 3.378730e-04 1.000000 NOT_MIG 0 1.000000 9 1096 1095 3.700890e-04 1.000000 NOT_MIG 0 1.000000 10 412 1192 2.950490e-04 1.000000 NOT_MIG 0 1.000000 11 1192 411 4.598850e-04 1.000000 NOT_MIG 0 1.000000 12 1192 1191 4.596340e-04 1.000000 NOT_MIG 0 1.000000 13 256 472 1.429340e-04 1.000000 NOT_MIG 0 0.456275 14 604 1616 2.279310e-04 1.000000 NOT_MIG 0 1.000000 15 1616 303 4.475810e-04 1.000000 NOT_MIG 0 1.000000 16 1616 1615 5.285400e-04 1.000000 NOT_MIG 0 1.000000 17 916 1852 4.927290e-04 1.000000 NOT_MIG 0 1.000000 18 1396 1976 2.450740e-04 1.000000 NOT_MIG 0 1.000000 19 136 2372 1.682020e-04 1.000000 NOT_MIG 0 1.000000 20 2372 1396 2.710750e-04 1.000000 NOT_MIG 0 1.000000 21 2372 2371 1.455540e-03 1.000000 NOT_MIG 0 1.000000 22 1852 2512 3.413280e-04 1.000000 NOT_MIG 0 1.000000 23 2512 104 1.293120e-04 1.000000 NOT_MIG 0 1.000000 24 212 2656 2.607870e-04 1.000000 NOT_MIG 0 1.000000 25 304 2804 2.696530e-04 1.000000 NOT_MIG 0 1.000000 26 2804 604 3.922560e-04 1.000000 NOT_MIG 0 1.000000 27 2804 2803 0.000000e+00 1.000000 NOT_MIG 0 1.000000 28 2956 2235 5.648930e-04 1.000000 NOT_MIG 0 1.000000 29 1976 3112 9.723940e-06 1.000000 NOT_MIG 0 1.000000 30 1396 1975 1.435540e-03 1.000000 NOT_MIG 0 1.000000 31 412 3272 3.579020e-04 1.000000 NOT_MIG 0 1.000000 32 3272 16 4.469120e-04 1.000000 NOT_MIG 0 1.000000 33 3604 2104 1.910150e-04 1.000000 NOT_MIG 0 0.456275 34 3776 304 6.638530e-04 1.000000 NOT_MIG 0 1.000000 35 3776 3775 1.172780e-03 1.000000 NOT_MIG 0 1.000000 36 3952 212 1.079110e-03 1.000000 NOT_MIG 0 1.000000 37 4316 355 6.499760e-04 1.000000 NOT_MIG 0 1.000000 38 16 3271 7.526430e-05 1.000000 NOT_MIG 0 1.000000 39 3272 916 3.722900e-04 1.000000 NOT_MIG 0 1.000000 40 76 75 6.436390e-04 1.000000 NOT_MIG 0 0.456275 41 4696 1395 4.036360e-04 1.000000 NOT_MIG 0 1.000000 42 4696 4695 1.468330e-03 1.000000 NOT_MIG 0 1.000000 43 2236 4892 1.189240e-04 1.000000 NOT_MIG 0 1.000000 44 4892 2956 6.151710e-04 1.000000 NOT_MIG 0 1.000000 45 1852 5296 5.336460e-04 1.000000 NOT_MIG 0 1.000000 46 5296 1851 2.036010e-03 1.000000 NOT_MIG 0 1.000000 47 5296 5295 1.510020e-03 1.000000 NOT_MIG 0 1.000000 48 356 5504 6.308400e-04 1.000000 NOT_MIG 0 1.000000 49 76 3 6.644550e-04 1.000000 NOT_MIG 0 0.456275 50 472 471 9.355620e-04 1.000000 NOT_MIG 0 0.456275 51 4504 4503 2.409710e-03 1.000000 NOT_MIG 0 0.456275 52 5716 4132 8.760120e-04 1.000000 NOT_MIG 0 1.000000 53 5716 5715 3.395390e-03 1.000000 NOT_MIG 0 1.000000 54 1504 3776 9.569240e-03 1.000000 NOT_MIG 0 1.000000 55 3112 4696 3.572770e-04 1.000000 NOT_MIG 0 1.000000 56 16 6152 4.185260e-04 1.000000 NOT_MIG 0 1.000000 57 6152 1 9.019670e-04 1.000000 NOT_MIG 0 1.000000 58 6152 6151 2.050600e-03 1.000000 NOT_MIG 0 1.000000 59 4132 6604 2.307520e-04 1.000000 NOT_MIG 0 1.000000 60 6604 136 4.328070e-04 1.000000 NOT_MIG 0 1.000000 61 6604 6603 8.066070e-04 1.000000 NOT_MIG 0 1.000000 62 2104 1732 1.761650e-04 1.000000 NOT_MIG 0 0.456275 63 916 6836 2.358950e-04 1.000000 NOT_MIG 0 1.000000 64 6836 915 1.196600e-04 1.000000 NOT_MIG 0 1.000000 65 6836 6835 3.153470e-04 1.000000 NOT_MIG 0 1.000000 66 32 1731 4.754380e-04 1.000000 NOT_MIG 0 0.456275 67 2656 7312 2.196850e-04 1.000000 NOT_MIG 0 1.000000 68 7312 7311 3.782260e-04 1.000000 NOT_MIG 0 1.000000 69 2104 2103 2.726410e-04 1.000000 NOT_MIG 0 0.456275 70 3604 3603 5.798710e-04 1.000000 NOT_MIG 0 0.456275 71 7556 4 1.583200e-03 1.000000 NOT_MIG 0 0.456275 72 7556 3604 6.136230e-04 1.000000 NOT_MIG 0 0.456275 73 5504 3952 3.287730e-04 1.000000 NOT_MIG 0 1.000000 74 7072 7071 5.274070e-04 1.000000 NOT_MIG 0 0.456275 75 7804 8312 0.000000e+00 1.000000 NOT_MIG 0 0.456275 76 8312 7803 1.028800e-04 1.000000 NOT_MIG 0 0.456275 77 4316 8572 5.772120e-04 1.000000 NOT_MIG 0 1.000000 78 8572 4315 1.670600e-03 1.000000 NOT_MIG 0 1.000000 79 8572 8571 8.358330e-04 1.000000 NOT_MIG 0 1.000000 80 3952 4316 3.622170e-04 1.000000 NOT_MIG 0 1.000000 81 5504 5503 2.226120e-03 1.000000 NOT_MIG 0 1.000000 82 104 536 0.000000e+00 1.000000 NOT_MIG 0 1.000000 83 2512 2511 1.523360e-03 1.000000 NOT_MIG 0 1.000000 84 2956 8836 4.059610e-04 1.000000 NOT_MIG 0 1.000000 85 8836 2955 5.474450e-04 1.000000 NOT_MIG 0 1.000000 86 8836 8835 2.240930e-03 1.000000 NOT_MIG 0 1.000000 87 4892 103 8.297560e-04 1.000000 NOT_MIG 0 1.000000 88 2236 4891 5.254160e-04 1.000000 NOT_MIG 0 1.000000 89 8312 7555 1.842930e-03 1.000000 NOT_MIG 0 0.456275 90 9104 4131 1.548560e-03 1.000000 NOT_MIG 0 1.000000 91 9104 9103 9.450860e-04 1.000000 NOT_MIG 0 1.000000 92 536 535 8.674210e-04 1.000000 NOT_MIG 0 1.000000 93 3112 3111 7.889400e-05 1.000000 NOT_MIG 0 1.000000 94 1976 751 1.151970e-03 1.000000 NOT_MIG 0 1.000000 95 3436 3435 1.761890e-05 1.000000 NOT_MIG 0 1.000000 96 4132 9376 1.175780e-03 1.000000 NOT_MIG 0 1.000000 97 9376 9104 2.462510e-04 1.000000 NOT_MIG 0 1.000000 98 9376 9375 3.517170e-03 1.000000 NOT_MIG 0 1.000000 99 7312 171 1.301670e-03 1.000000 NOT_MIG 0 1.000000 100 2656 2655 1.815260e-04 1.000000 NOT_MIG 0 1.000000 101 536 2236 3.050530e-04 1.000000 NOT_MIG 0 1.000000 102 104 15 4.277030e-04 1.000000 NOT_MIG 0 1.000000 103 0 1504 1.593220e-02 1.000000 NOT_MIG 0 1.000000 104 1504 3436 7.834720e-03 1.000000 NOT_MIG 0 1.000000 105 9377 1503 8.000000e-03 1.000000 NOT_MIG 0 1.000000 106 9377 0 8.000000e-03 1.000000 NOT_MIG 0 1.000000 107 256 255 6.817780e-04 1.000000 NOT_MIG 0 0.456275 108 4504 256 1.773970e-04 1.000000 NOT_MIG 0 0.456275 109 0 676 3.938041e-02 0.675481 NOT_MIG 0 0.456275 110 32 4504 8.209070e-05 1.000000 NOT_MIG 0 0.456275 111 3436 832 8.754200e-04 1.000000 NOT_MIG 0 1.000000 112 9394 8055 0.000000e+00 1.000000 NOT_MIG 1 1.000000 113 9394 676 0.000000e+00 0.324519 MIG 1 0.000000 114 832 6375 9.550620e-04 1.000000 NOT_MIG 0 1.000000 115 832 8056 1.091440e-04 1.000000 NOT_MIG 0 1.000000 116 8056 831 2.839690e-03 1.000000 NOT_MIG 0 1.000000 117 8056 6376 8.344060e-04 1.000000 NOT_MIG 0 1.000000 118 6376 51 5.694570e-04 1.000000 NOT_MIG 0 1.000000 119 6376 752 1.394010e-03 1.000000 NOT_MIG 0 1.000000 120 172 3951 7.048340e-04 1.000000 NOT_MIG 0 1.000000 121 172 5092 2.672400e-03 1.000000 NOT_MIG 0 1.000000 122 5092 5931 8.633440e-04 1.000000 NOT_MIG 0 1.000000 123 5092 5932 2.196100e-03 1.000000 NOT_MIG 0 1.000000 124 5932 9394 9.724060e-03 1.000000 NOT_MIG 0 1.000000 125 752 5716 9.754150e-04 1.000000 NOT_MIG 0 1.000000 126 752 356 1.983640e-04 1.000000 NOT_MIG 0 1.000000 127 356 172 1.870890e-03 1.000000 NOT_MIG 0 1.000000 128 5932 9494 0.000000e+00 1.000000 NOT_MIG 0 1.000000 129 9494 5091 0.000000e+00 1.000000 NOT_MIG NaN 1.000000 130 9494 675 0.000000e+00 0.402721 MIG NaN 0.000000 131 1292 2 3.477870e-04 1.000000 NOT_MIG 0 0.456275 132 676 7556 6.158170e-04 1.000000 NOT_MIG 0 0.456275 133 676 1292 6.766610e-04 1.000000 NOT_MIG 0 0.456275 134 1292 675 0.000000e+00 0.597279 NOT_MIG 0 0.162773 135 2 8311 0.000000e+00 1.000000 NOT_MIG 0 0.456275 136 2 7804 3.396830e-04 1.000000 NOT_MIG 0 0.456275 137 7804 1291 1.081680e-03 1.000000 NOT_MIG 0 0.456275 138 1732 32 3.232710e-04 1.000000 NOT_MIG 0 0.456275 139 1732 7072 0.000000e+00 1.000000 NOT_MIG 0 0.456275 140 7072 31 9.654110e-04 1.000000 NOT_MIG 0 0.456275

I would be glad to hear any suggestions you have for a workaround with geom_curve()

andrewparkermorgan commented 1 year ago

The ggplot2 object is a list whose elements can be retrieved by using $, as you show, but to get its attributes (in R lingo) you have to call attr(p_grh2, "edges") as I show in the example a few steps up this thread

imaa9 commented 1 year ago

Ah cool, it might have been an issue with updating popcorn because beforehand when I called this command and then called the edges object it just said "NULL" (this is why I looked for another way to potentially retrieve the info). But now when I use your command it works great! thanks so very much :)