-
Notifications
You must be signed in to change notification settings - Fork 29
Function `baltic.loadJSON`
Barney Potter edited this page Oct 14, 2024
·
1 revision
The loadJSON()
function in BALTIC is used to load a Nextstrain JSON file and create a BALTIC tree object. This function can handle both local JSON files and URLs to Nextstrain JSONs, and provides options for attribute translation and tree processing.
def loadJSON(json_object, json_translation={'name':'name','absoluteTime':'num_date'},
verbose=False, sort=True, stats=True)
-
json_object
(str or dict): The path to the JSON file, a URL to a Nextstrain JSON, or a JSON object. -
json_translation
(dict): A dictionary that translates JSON attributes to tree attributes. Default is{'name': 'name', 'absoluteTime': 'num_date'}
. -
verbose
(bool): If True, prints verbose output during the process. Default is False. -
sort
(bool): If True, sorts the branches of the tree after loading. Default is True. -
stats
(bool): If True, calculates tree statistics after loading. Default is True.
- tuple: A tuple containing the BALTIC tree object created from the JSON and the metadata from the JSON.
- Loads the JSON data from a file, URL, or object.
- Extracts metadata and tree structure from the JSON.
- Calls
make_treeJSON()
to create a BALTIC tree object from the JSON tree data. - Translates JSON attributes to BALTIC tree attributes based on the
json_translation
dictionary. - Processes node attributes and traits.
- Calculates branch lengths based on specified attributes (e.g., 'height' or 'absoluteTime').
- Optionally sorts branches and calculates tree statistics.
- Extracts and assigns color mappings from the JSON metadata.
- Returns the processed BALTIC tree object and the JSON metadata.
- Loading Nextstrain JSON files into BALTIC for analysis or visualization.
- Importing trees and associated metadata from web-based phylogenetic resources.
- Processing trees with custom JSON formats by specifying appropriate attribute translations.
- Extracting both tree structure and associated metadata from a single JSON source.
import baltic as bt
import matplotlib.pyplot as plt
# Load a Nextstrain JSON (local file or URL)
tree, metadata = bt.loadJSON("https://nextstrain.org/charon/getDataset?prefix=/zika", verbose=True)
# Print basic tree information
print(f"Number of tips: {len(tree.getExternal())}")
print(f"Number of internal nodes: {len(tree.getInternal())}")
# Check available traits
sample_node = tree.Objects[0]
print("Available traits:", list(sample_node.traits.keys()))
# Visualize the tree
fig, ax = plt.subplots(figsize=(12, 8))
tree.plotTree(ax)
tree.addText(ax, x_attr=lambda n: n.x + 0.001)
plt.title("Phylogenetic Tree")
plt.show()
# Plot a time-scaled tree if absoluteTime is available
if hasattr(tree.getExternal()[0], 'absoluteTime'):
fig, ax = plt.subplots(figsize=(12, 8))
tree.plotTree(ax)
tree.addText(ax, x_attr=lambda n: n.absoluteTime)
ax.set_xlabel("Time")
plt.title("Time-scaled Phylogenetic Tree")
plt.show()
# Print some metadata information
print("\nMetadata:")
for key in metadata:
print(f" {key}")
# If color mappings are available, print an example
if hasattr(tree, 'cmap') and tree.cmap:
print("\nColor mapping example:")
example_trait = next(iter(tree.cmap))
print(f"Trait: {example_trait}")
for category, color in list(tree.cmap[example_trait].items())[:5]:
print(f" {category}: {color}")
- The function can handle JSON data from local files, URLs (specifically Nextstrain URLs), or pre-loaded JSON objects.
- The
json_translation
dictionary is crucial for mapping JSON attributes to BALTIC tree attributes. Modify this if your JSON uses different key names. - Ensure that the
json_translation
dictionary includes mappings for 'name' and at least one of 'absoluteTime', 'length', or 'height'. - The function extracts color mappings from the JSON metadata, which can be useful for consistent coloring in visualizations.
- When loading from a Nextstrain URL, the function uses the
requests
library to fetch the data. - The function assumes a specific structure for Nextstrain JSONs. If working with custom JSON formats, you may need to modify the function or preprocess your JSON.
- The
sort
andstats
options allow for immediate processing of the tree after loading, which can be useful for quick analyses or visualizations. - This function is particularly useful for working with Nextstrain data or other phylogenetic data in a similar JSON format.
Wiki written with the assistance of claude.ai 3.5 "Sonnet".
- Core
baltic
classes:-
Class
tree
- Tree Construction and Manipulation methods
- Tree Analysis methods
- Tree Conversion and Output methods
- Tree Visualization
- Utility Methods
- Class
leaf
- Class
node
- Class
clade
- Class
reticulation
-
Class