### Initialize TidyTree with Newick String
Source: https://github.com/cdcgov/tidytree/blob/master/test/example.html
This example shows how to initialize the TidyTree library. It first applies basic CSS for page layout and then creates a TidyTree instance using a Newick formatted string and an options object specifying the parent element for rendering.
```javascript
html, body { height: 100%; width: 100%; padding: 0; margin: 0; overflow: hidden; }
let newick = "(A:0.1,B:0.2,(C:0.3,D:0.4):0.5);";
let tree = new TidyTree(newick, { parent: "body" });
```
--------------------------------
### Context Menu Initialization
Source: https://github.com/cdcgov/tidytree/blob/master/app/index.html
Initializes a context menu function. The provided snippet is incomplete, but it suggests the start of a context menu handler that receives data 'd' and event 'e'. Further implementation details are missing.
```javascript
function contextMenu(d) {
let e = d3.event;
if (!e) retu
```
--------------------------------
### Quick Start: Initialize TidyTree with Newick Data
Source: https://github.com/cdcgov/tidytree/blob/master/README.md
This snippet demonstrates how to quickly integrate the TidyTree library into an HTML page. It requires d3.js as a dependency and initializes a phylogenetic tree from a Newick string, rendering it within the specified parent element.
```html
```
```javascript
let newick = "(A:0.1,B:0.2,(C:0.3,D:0.4):0.5);";
let tree = new TidyTree(newick, { parent: "body" });
```
--------------------------------
### Node GUID Retrieval
Source: https://github.com/cdcgov/tidytree/blob/master/docs/README.md
Retrieves unique identifiers (GUIDs) for nodes within the TidyTree, with options to filter for leaf nodes or based on a predicate.
```APIDOC
getNodeGUIDs(leavesOnly, predicate)
- Retrieves the GUIDs of the nodes in the TidyTree instance.
- Parameters:
- leavesOnly: [boolean] Whether to retrieve GUIDs only for leaf nodes.
- predicate: [function] A function that returns true if the node should be included.
- Returns: [Array] An array of GUIDs of the nodes.
```
--------------------------------
### Get All Leaf Nodes
Source: https://github.com/cdcgov/tidytree/blob/master/docs/README.md
Retrieves all descendant nodes that are considered leaves from a given node in the tree. Optionally, the starting node itself can be included in the result.
```APIDOC
getAllLeaves(node, includeSelf)
Returns an array of all the child leaf nodes of the given node in a tree.
Parameters:
node [Object][75]: A node of the tree.
includeSelf [boolean][77]: Whether to include the given node itself as a leaf node. Defaults to false.
Returns:
[Array][80]: An array of leaf nodes.
```
--------------------------------
### Document Ready and Tree Initialization
Source: https://github.com/cdcgov/tidytree/blob/master/app/index.html
Initializes the TidyTree application when the DOM is ready. It sets up Material Design components, parses URL parameters to load a tree file, and falls back to loading a default 'life.nwk' file if no URL is provided or loading fails.
```javascript
$(document).ready(function() {
$("body").bootstrapMaterialDesign();
const params = new URLSearchParams(window.location.search);
const fileUrl = params.get("file");
let treeFetched;
if (fileUrl) {
d3.select("#tree-url").property("value", fileUrl);
treeFetched = fetchAndLoadTree(fileUrl);
}
if (!fileUrl || !treeFetched) {
fetch("life.nwk").then(response => response.text().then(buildTree));
}
});
```
--------------------------------
### TidyTree Class and Constructor
Source: https://github.com/cdcgov/tidytree/blob/master/docs/README.md
Initializes a TidyTree object. The constructor accepts data, options, events, and a Newick string to set up the tree visualization.
```APIDOC
TidyTree(data, options, events, newick)
- Creates a TidyTree object.
- Parameters:
- data: The initial data structure for the tree.
- options [Object]: Configuration options for setting up the tree.
- events: Event handlers for tree interactions.
- newick [String]: A valid Newick string representing the phylogenetic tree.
```
--------------------------------
### Available Layouts, Types, and Modes
Source: https://github.com/cdcgov/tidytree/blob/master/docs/README.md
Provides lists of valid configuration options for rendering trees, branches, and nodes.
```APIDOC
validLayouts
- The available layouts for rendering trees.
- Type: [Array]
validTypes
- The available types for rendering branches.
- Type: [Array]
validModes
- The available modes for rendering branches.
- Type: [Array]
validNodeColorModes
- The available color modes for rendering nodes.
- Type: [Array]
validBranchColorModes
- The available color modes for rendering branches.
- Type: [Array]
```
--------------------------------
### Fetch and Load Tree from URL
Source: https://github.com/cdcgov/tidytree/blob/master/app/index.html
Asynchronously fetches tree data from a given URL. It checks for HTTP errors, parses the response text, calls buildTree to render the tree, and updates the URL. Provides user feedback on failure.
```javascript
async function fetchAndLoadTree(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const text = await response.text();
buildTree(text);
updateQueryParam(url);
return true;
} catch (error) {
alert("Failed to load the Newick tree. Check the URL and CORS settings.");
}
return false;
}
```
--------------------------------
### TidyTree Appearance and Styling Methods
Source: https://github.com/cdcgov/tidytree/blob/master/docs/README.md
Methods for customizing the visual appearance of the tree, including colors, stretching, and node/label styling.
```APIDOC
setColorOptions(newColorOptions)
- Sets the tree's color options.
- Parameters:
- newColorOptions [Object]: An object containing new color configuration.
- Returns: The TidyTree object.
setHStretch(proportion)
- Adjusts the horizontal stretching of the tree.
- Parameters:
- proportion [Number]: The proportion by which to stretch the tree horizontally.
- Returns: The TidyTree object.
setVStretch(proportion)
- Adjusts the vertical stretching of the tree.
- Parameters:
- proportion [Number]: The proportion by which to stretch the tree vertically.
- Returns: The TidyTree object.
setBranchNodes(nodes)
- Sets the branch nodes.
- Parameters:
- nodes: The branch nodes to set.
- Returns: The TidyTree object.
setBranchLabels(labels)
- Sets the branch labels.
- Parameters:
- labels: The branch labels to set.
- Returns: The TidyTree object.
setBranchDistances(distances)
- Sets the branch distances.
- Parameters:
- distances: The branch distances to set.
- Returns: The TidyTree object.
setLeafNodes(nodes)
- Sets the leaf nodes.
- Parameters:
- nodes: The leaf nodes to set.
- Returns: The TidyTree object.
setEquidistantLeaves(equidistant)
- Configures leaves to be displayed equidistantly.
- Parameters:
- equidistant [Boolean]: True to enable equidistant leaves, false otherwise.
- Returns: The TidyTree object.
setLeafLabels(labels)
- Sets the leaf labels.
- Parameters:
- labels: The leaf labels to set.
- Returns: The TidyTree object.
setRuler(ruler)
- Sets the ruler for the tree.
- Parameters:
- ruler: The ruler configuration.
- Returns: The TidyTree object.
```
--------------------------------
### Load Tree from URL Button Handler
Source: https://github.com/cdcgov/tidytree/blob/master/app/index.html
Handles the click event for a 'Load URL' button. It retrieves the URL from an input field and calls fetchAndLoadTree to load the tree data.
```javascript
d3.select("#load-url").on("click", async function() {
const url = d3.select("#tree-url").property("value").trim();
if (url) {
fetchAndLoadTree(url);
} else {
alert("Please enter a valid URL.");
}
});
```
--------------------------------
### TidyTree Rendering and Layout Methods
Source: https://github.com/cdcgov/tidytree/blob/master/docs/README.md
Methods related to drawing, redrawing, and controlling the visual layout of the phylogenetic tree.
```APIDOC
draw(selector)
- Draws the phylogenetic tree on the element specified by the CSS selector.
- Parameters:
- selector [String]: A CSS selector for the target element.
- Returns: The TidyTree object.
redraw()
- Redraws the tree links and relocates nodes accordingly.
- Returns: The TidyTree object.
recenter()
- Centers the tree within the view.
- Returns: The TidyTree object.
setLayout(newLayout)
- Sets the tree's layout.
- Parameters:
- newLayout [String]: The name of the new layout to apply.
- Returns: The TidyTree object.
```
--------------------------------
### Build Tree Function
Source: https://github.com/cdcgov/tidytree/blob/master/app/index.html
Core function to create or update the TidyTree visualization. It instantiates the TidyTree class with Newick data and various configuration options derived from DOM elements. It also accepts optional callbacks for context menus and tooltips.
```javascript
function buildTree(newick) {
tree = new TidyTree(
newick ? newick : tree.data.clone(),
{
parent: "#tree",
layout: d3.select("#layout").node().value,
mode: d3.select("#mode").node().value,
type: d3.select("#type").node().value,
colorOptions: {
nodeColorMode: "list",
branchColorMode: "monophyletic",
defaultNodeColor: d3.select("#defaultNodeColor").node().value,
defaultBranchColor: d3.select("#defaultBranchColor").node().value,
highlightColor: d3.select("#highlightColor").node().value,
},
leafNodes: d3.select("#leafNodes").node().checked,
branchNodes: d3.select("#branchNodes").node().checked,
leafLabels: d3.select("#leafLabels").node().checked,
equidistantLeaves: d3.select("#equidistantLeaves").node().checked,
branchLabels: d3.select("#branchLabels").node().checked,
branchDistances: d3.select("#branchDistances").node().checked,
ruler: d3.select("#ruler").node().checked,
animation: parseFloat(d3.select("#animation").node().value),
margin: [10, 10, 70, 30]
},
{
contextmenu: contextMenu,
showtooltip: showTooltip,
hidetooltip: hideTooltip
}
);
}
```
--------------------------------
### File Input Handler
Source: https://github.com/cdcgov/tidytree/blob/master/app/index.html
Handles file uploads via an input element. When a file is selected, it uses FileReader to read the file content as text and then calls buildTree to render the tree.
```javascript
d3.select("#source").on("input", function() {
let reader = new FileReader();
reader.onload = () => buildTree(reader.result);
reader.readAsText(d3.event.srcElement.files[0]);
});
```
--------------------------------
### Populate Select Options
Source: https://github.com/cdcgov/tidytree/blob/master/app/index.html
Dynamically populates dropdown (