# conda init
# conda create -n treemix
# conda activate treemix
# conda install treemix -c bioconda -c conda-forge
# conda install ipyrad -c bioconda -c conda-forge
# conda install jupyter -c conda-forge
# conda install toytree -c eaton-lab -c conda-forge
# jupyter-notebook

import ipyrad.analysis as ipa
import toytree
import toyplot

print('ipyrad', ipa.__version__)
print('toytree', toytree.__version__)
! treemix --version | grep 'TreeMix v. '

"""
Explanation: <span style="color:gray">ipyrad-analysis toolkit:</span> treemix
View as notebook
The program TreeMix by Pickrell & Pritchard (2012)  is used to infer population splits and admixture from allele frequency data. From the TreeMix documentation: "In the underlying model, the modern-day populations in a species are related to a common ancestor via a graph of ancestral populations. We use the allele frequencies in the modern populations to infer the structure of this graph."
Required software
A minor detail of the treemix conda installation is that it installs a bunch of junk alongside it, including R and openblas, into your conda environment. It's a ton of bloat that in my experience has a high chance of causing incompatibilities with other packages in your conda installation eventually. For this reason I recommend installing it into a separate conda environment.  
The installation instructions below can be used to setup a new environment called treemix that you can use for this analyis. This will avoid the installation from conflicting with any of your other software. When you are done you can switch back to your default environment.
End of explanation
"""


# the path to your HDF5 formatted snps file
data = "/home/deren/Downloads/ref_pop2.snps.hdf5"

"""
Explanation: Required input data files
Your input data should be a .snps.hdf database file produced by ipyrad. If you do not have this you can generate it from any VCF file following the vcf2hdf5 tool tutorial. The database file contains the genotype calls information as well as linkage information that is used for subsampling unlinked SNPs and bootstrap resampling.
End of explanation
"""


# group individuals into populations
imap = {
    "virg": ["TXWV2", "LALC2", "SCCU3", "FLSF33", "FLBA140"],
    "mini": ["FLSF47", "FLMO62", "FLSA185", "FLCK216"],
    "gemi": ["FLCK18", "FLSF54", "FLWO6", "FLAB109"],
    "bran": ["BJSL25", "BJSB3", "BJVL19"],
    "fusi": ["MXED8", "MXGT4", "TXGR3", "TXMD3"],
    "sagr": ["CUVN10", "CUCA4", "CUSV6", "CUMM5"],
    "oleo": ["CRL0030", "HNDA09", "BZBB1", "MXSA3017"],
}

# minimum % of samples that must be present in each SNP from each group
minmap = {i: 0.5 for i in imap}

# init a treemix analysis object with some param arguments
tmx = ipa.treemix(
    data=data, 
    imap=imap,
    minmap=minmap, 
    seed=123456,
    root="bran,fusi",
    m=2,
)

# print the command string that will be called and run it
print(tmx.command)
tmx.run()

# draw the resulting tree
tmx.draw_tree();

# draw the covariance matrix
tmx.draw_cov();

"""
Explanation: Short Tutorial:
If you entered population information during data assembly then you may have already produced a .treemix.gz output file that can be used as input to the treemix command line program. Alternatively, you can run treemix using the ipyrad tool here which offers some additional flexibility for filtering SNP data, and for running treemix programatically over many parameter settings. 
The key features offered by ipa.treemix include: 

Filter unlinked SNPs (1 per locus) many times for replicate analyses.
Filter by sample or populations coverage.
Plotting functions. 
Easy to write for-loops
End of explanation
"""


# init a treemix analysis object with some param arguments
tmx = ipa.treemix(
    data=data, 
    imap=imap,
    minmap=minmap, 
    seed=1234,
    root="bran,fusi",
)

tests = {}
nadmix = [0, 1, 2, 3, 4, 5]

# iterate over n admixture edges and store results in a dictionary
for adm in nadmix:
    tmx.params.m = adm
    tmx.run()
    tests[adm] = tmx.results.llik

# plot the likelihood for different values of m
toyplot.plot(
    nadmix,
    [tests[i] for i in nadmix],
    width=350, 
    height=275,
    stroke_width=3,
    xlabel="n admixture edges",
    ylabel="ln(likelihood)",
);

"""
Explanation: 1. Finding the best value for m
As with structure plots there is no True best value, but you can use model selection methods to decide whether one is a statistically better fit to your data than another. Adding additional admixture edges will always improve the likelihood score, but with diminishing returns as you add additional edges that explain little variation in the data. You can look at the log likelihood score of each model fit by running a for-loop like below. You may want to run this within another for-loop that iterates over different subsampled SNPs.
End of explanation
"""


# a gridded canvas to plot trees on 
canvas = toyplot.Canvas(width=600, height=700)

# iterate over multiple set of SNPs
for i in range(9):
    
    # init a treemix analysis object with a random (no) seed
    tmx = ipa.treemix(
        data=data, 
        imap=imap,
        minmap=minmap,
        root="bran,fusi",
        global_=True,
        m=2,
        quiet=True
    )
    
    # run model fit
    tmx.run()
    
    # select a plot grid axis and add tree to axes
    axes = canvas.cartesian(grid=(3, 3, i))
    tmx.draw_tree(axes)

# a gridded canvas to plot trees on 
canvas = toyplot.Canvas(width=600, height=700)

# iterate over multiple set of SNPs
for i in range(9):
    
    # init a treemix analysis object with a random (no) seed
    tmx = ipa.treemix(
        data=data, 
        imap=imap,
        minmap=minmap,
        root="bran,fusi",
        global_=True,
        m=3,
        quiet=True
    )
    
    # run model fit
    tmx.run()
    
    # create a grid axis and add tree to axes
    axes = canvas.cartesian(grid=(3, 3, i))
    tmx.draw_tree(axes)

"""
Explanation: 2. Iterate over different subsamples of SNPs
The treemix tool randomly subsamples 1 SNP per locus to reduce the effect of linkage on the results. However, depending on the size of your data set, and the strength of the signal, subsampling may yield slightly different results in different iterations. You can check over different subsampled iterations by re-initing the treemix tool with a different (or no) random seed. Below I plot the results of 9 iterations for m=2. I also use the global_=True option here which performs a more thorough (but slower) search.
End of explanation
"""


import toyplot.pdf
toyplot.pdf.render(canvas, "treemix-m3.pdf")

"""
Explanation: 3. Save the plot to pdf
End of explanation
"""


