auspice json io#

auspice-json-io

Code#

## Export a nexus MCC tree to auspice JSON and view it on https://auspice.us
## Drag and drop the output file (mers-json.json) onto auspice.us to explore the tree
## interactively, e.g. colour by 'type' (host) or 'posterior'. The image for this example
## is a screenshot of that view rather than a matplotlib figure.

import baltic as bt
import json

ll = bt.io.load_nexus('./../MERS-CoV/MERS.mcc.tree', 'time') ## import nexus MCC tree
ll.treeStats()

ll.sort_branches()
auspiceJSON = ll.to_auspice_json() ## convert to auspice JSON

with open('mers-json.json', 'w') as outFile:
    json.dump(auspiceJSON, fp=outFile, indent=4) ## save JSON to file - open this on https://auspice.us

##########
## baltic can also read auspice JSONs back in
ll, meta = bt.io.load_JSON('mers-json.json', 'time') ## note that importing auspice JSONs results in a tuple - the baltic representation of the tree and a meta dict
ll.treeStats() ## report stats about tree