# conda install ipyrad -c bioconda
# conda install toytree -c eaton-lab

import pandas as pd
import toytree

"""
Explanation: <h1><span style="color:gray">ipyrad-analysis toolkit: </span> Astral species trees</h1>

Once you've run treeslider you now have a distribution of gene trees that you can use as input to a multi-locu species tree or network inference program. Here I demonstrate how to input your data to astral.
Required software
End of explanation
"""


# load the tree table from CSV
tree_table = pd.read_csv(
    "./analysis-treeslider/test.tree_table.csv",
    index_col=0,
)

# examine top of table
tree_table.head()

"""
Explanation: Short Tutorial:
End of explanation
"""


outfile = open("trees.nwk", "w")
outfile.write("\n".join(tree_table.tree.tolist()))
outfile.close()

"""
Explanation: Write the trees column to a file
End of explanation
"""


%%bash
wget -q https://github.com/smirarab/ASTRAL/raw/master/Astral.5.6.3.zip 
unzip -qo Astral.5.6.3


"""
Explanation: Get Astral
The following command will download and unzip Astral into the current directory. A binary to call the program astral will be located in the Astral directory.
End of explanation
"""


%%bash
java -jar Astral/astral.5.6.3.jar -i trees.nwk > astral.tre 2>astral.err

"""
Explanation: Run Astral
This will write the results (stdout) to a file called astral.tre, and it will write other information printed to the screen (stderr) to a file called astral.err. By default astral runs 100 bootstrap replicates by gene-tree resampling. To turn on gene and site resampling would require inputting the bootstrap files, which we did not currently save (we could...).
End of explanation
"""


tre = toytree.rtree.coaltree(10)
tre.


tre.rotate_node(names=["r0", "r1", "r2"])

tre.draw(node_labels=True);

tre = toytree.tree("astral.tre").root('reference')
tre.draw(node_labels="support", tip_labels_align=True);

"""
Explanation: Plot astral species tree
End of explanation
"""


