Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply the same scale bar / scaling to multiple phylograms #41

Open
Elodie-Wells opened this issue Aug 29, 2024 · 1 comment
Open

Apply the same scale bar / scaling to multiple phylograms #41

Elodie-Wells opened this issue Aug 29, 2024 · 1 comment

Comments

@Elodie-Wells
Copy link

I have several unrooted phylograms that I would like to present in the same figure, all with the same scaling/scale bar. Is this possible? At the moment I have several phylograms, each with a different scale bar.

Many thanks!

@arklumpus
Copy link
Owner

Hi! This is possible with a short custom script.

  • First of all, open the parameters for the Radial Coordinates module and make sure that the Preserve aspect ratio check box is enabled.
  • Then, click on the Edit... button next to Custom script; in the code editor window, enter the following code:
Click to expand source code
using PhyloTree;
using System.Collections.Generic;
using TreeViewer;
using VectSharp;

namespace ab61f97f546704f82b5be4f3ffcd77665
{
    //Do not change class name
    public static class CustomCoordinates
    {
        //Do not change method signature
        public static void GetCoordinates(TreeNode tree, ref Dictionary<string, Point> coordinates)
        {
            // This parameter determines the final size of the tree. Make it bigger/smaller as you see fit.
            double targetScale = 10;
            
            
            // You should not need to change anything further.

            // The current scaling factor for converting branch length units to graphics units.
            double currentScale = coordinates["95b61284-b870-48b9-b51c-3276f7d89df1"].X;

            // Create a new dictionary to hold the rescaled coordinates.
            Dictionary<string, Point> scaledCoordinates = new Dictionary<string, Point>();

            // Cycle through all the coordinates.
            foreach (KeyValuePair<string, Point> kvp in coordinates)
            {
                // Rescale the coordinates of every point by a factor of targetScale / currentScale.
                scaledCoordinates[kvp.Key] = new Point(kvp.Value.X / currentScale * targetScale, kvp.Value.Y / currentScale * targetScale);
            }

            // Use the new scaledCoordinates dictionary in place of the previous coordinates.
            coordinates = scaledCoordinates;
        }
    }
}

You should follow these steps for all the trees you want to plot, and this will ensure that they all have the same scaling. You can then use the Scale bar Plot action module only on one of them.

To determine the actual size of the trees, you can change the value of the targetScale variable (line 15 in the script); this is the scaling factor that transforms branch lengths in graphics units (e.g., if targetScale = 10, a branch that has Length = 2 will be 20 "pixels" long). Just make sure you use the same value for all trees!

I hope this works, let me know if you have any more questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants