{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ipyrad-analysis toolkit: STRUCTURE\n",
"\n",
"Structure v.2.3.4 is a standard tool for examining population genetic structure based on allele frequencies within and among populations. Although many new implementations of the structure algorithm have been developed in recent years offering improvements to speed, the classic tool offers a number of useful options that keep it relevant to this day. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Required software"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# conda install ipyrad -c bioconda\n",
"# conda install -c bioconda -c ipyrad structure clumpp\n",
"# conda install toyplot -c eaton-lab"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import ipyrad.analysis as ipa\n",
"import toyplot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Required input data files\n",
"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](https://ipyrad.readthedocs.io/en/latest/API-analysis/cookbook-vcf2hdf5.html). The database file contains the genotype calls information as well as linkage information that is used for subsampling unlinked SNPs and bootstrap resampling."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# the path to your .snps.hdf5 database file\n",
"data = \"/home/deren/Downloads/ref_pop2.snps.hdf5\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### Note: missing data in STRUCTURE analyses:\n",
"\n",
"Structure infers the values of missing data while it runs the MCMC chain. No imputation is required, but it will perform more accurately if there is less missing data and when base calls are more accurate. I recommend not imputing data and simply filtering fairly stringently. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Approximate run times\n",
"This example data set should probably be run for a longer burnin and number of reps if it were to be used in a publication. For reference, this data set takes about 2.5 hours to run 12 jobs on a 4 core laptop for a data set with 27 samples and ~125K SNPs. If your data set has more samples or SNPs then it will take longer. If you have 2X as many cores then it will run 2X faster. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Input data file and population assignments\n",
"If you are using the \"sample\" input method then population assignments (imap dictionary) are used for for filtering, color coding plots, and for imputation. If you are using the \"kmeans\" imputing method then population assignments are only used for filtering and color coding plots."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# group individuals into populations\n",
"imap = {\n",
" \"virg\": [\"TXWV2\", \"LALC2\", \"SCCU3\", \"FLSF33\", \"FLBA140\"],\n",
" \"mini\": [\"FLSF47\", \"FLMO62\", \"FLSA185\", \"FLCK216\"],\n",
" \"gemi\": [\"FLCK18\", \"FLSF54\", \"FLWO6\", \"FLAB109\"],\n",
" \"sagr\": [\"CUVN10\", \"CUCA4\", \"CUSV6\"],\n",
" \"oleo\": [\"CRL0030\", \"HNDA09\", \"BZBB1\", \"MXSA3017\"],\n",
" \"fusi\": [\"MXED8\", \"MXGT4\", \"TXGR3\", \"TXMD3\"],\n",
" \"bran\": [\"BJSL25\", \"BJSB3\", \"BJVL19\"],\n",
"}\n",
"\n",
"# require that 50% of samples have data in each group\n",
"minmap = {i: 0.5 for i in imap}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Enter data file and params\n",
"The `struct` analysis object takes input data as the *.snps.hdf5* file produced by ipyrad. All other parameters are optional. The **imap** dictionary groups individuals into populations and **minmap** can be used to filter SNPs to only include those that have data for at least some proportion of samples in every group. The **mincov** option works similarly, it filters SNPs that are shared across less than some proportion of all samples (in contrast to minmap this does not use imap groupings). \n",
"\n",
"When you init the object it will load the data and apply filtering. The printed output tells you how many SNPs were removed by each filter and the remaining amount of missing data after filtering. These remaining missing values are the ones that will be filled with imputation. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Samples: 27\n",
"Sites before filtering: 349914\n",
"Filtered (indels): 0\n",
"Filtered (bi-allel): 13001\n",
"Filtered (mincov): 222081\n",
"Filtered (minmap): 112898\n",
"Filtered (combined): 226418\n",
"Sites after filtering: 123496\n",
"Sites containing missing values: 96001 (77.74%)\n",
"Missing values in SNP matrix: 142017 (4.26%)\n"
]
}
],
"source": [
"# init analysis object with input data and (optional) parameter options\n",
"struct = ipa.structure(\n",
" name=\"test\",\n",
" data=data,\n",
" imap=imap,\n",
" minmap=minmap,\n",
" mincov=0.9,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Run STRUCTURE and plot results. \n",
"The `burnin` and `numreps` parameters determine the length of the run. For analyses with many samples and with larger values of K you should use much larger values than these. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"struct.mainparams.burnin = 5000\n",
"struct.mainparams.numreps = 10000"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Parallel connection | oud: 4 cores\n",
"[####################] 100% 2:26:57 | running 12 structure jobs \n"
]
}
],
"source": [
"struct.run(nreps=3, kpop=[2, 3, 4, 5], auto=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Analyze results: Choosing K"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"You can continue an analysis with the same `name` and `workdir` by setting additional replicates or values of K and calling `.run()` again. Here I will increase the number of replicates per K value from 3 to 5, and run one additional K value. Be sure to use all of the same parameter and filtering values that you used in the previous run or you might cause unexpected problems. \n",
"\n",
"Here because we already finished 3 replicates for K=2,3,4,5 it will run 2 more for each of those, and it will run 5 replicates for K=6 since we do not have any finished replicates of those yet. You can see which result files exist for a named analysis object by accessing the `.result_files` attribute, or by looking in the working directory. To overwrite existing files instead of adding more replicates you can use `force=True` in the run command. You could also simply create a new object with a different name. "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12 previous results loaded for run [test]\n",
"Samples: 27\n",
"Sites before filtering: 349914\n",
"Filtered (indels): 0\n",
"Filtered (bi-allel): 13001\n",
"Filtered (mincov): 222081\n",
"Filtered (minmap): 112898\n",
"Filtered (combined): 226418\n",
"Sites after filtering: 123496\n",
"Sites containing missing values: 96001 (77.74%)\n",
"Missing values in SNP matrix: 142017 (4.26%)\n",
"Parallel connection | oud: 4 cores\n",
"[####################] 100% 3:39:43 | running 13 structure jobs \n"
]
}
],
"source": [
"# init analysis object with same params as previously\n",
"struct = ipa.structure(\n",
" name=\"test\",\n",
" data=data,\n",
" imap=imap,\n",
" minmap=minmap,\n",
" mincov=0.9,\n",
")\n",
"\n",
"# use the same params as before \n",
"struct.mainparams.burnin = 5000\n",
"struct.mainparams.numreps = 10000\n",
"\n",
"# call run for all K values you want to have 5 finished replicates\n",
"struct.run(nreps=5, kpop=[2, 3, 4, 5, 6], auto=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"