Here we demonstrate a denovo assembly for an empirical RAD data set using the ipyrad Python API. This example was run on a workstation with 20 cores and takes about 10 minutes to assemble, but you should be able to run it on a 4-core laptop in ~30-60 minutes.

For our example data we will use the 13 taxa Pedicularis data set from Eaton and Ree (2013) (Open Access). This data set is composed of single-end 75bp reads from a RAD-seq library prepared with the PstI enzyme. The data set also serves as an example for several of our analysis cookbooks that demonstrate methods for analyzing RAD-seq data files. At the end of this notebook there are also several examples of how to use the ipyrad analysis tools to run downstream analyses in parallel.

The figure below shows the ingroup taxa from this study and their sampling locations. The study includes all species within a small monophyletic clade of Pedicularis, including multiple individuals from 5 species and several subspecies, as well as an outgroup species. The sampling essentially spans from population-level variation where species boundaries are unclear, to higher-level divergence where species boundaries are quite distinct. This is a common scale at which RAD-seq data are often very useful.

If you haven't done so yet, start by installing ipyrad using conda (see ipyrad installation instructions) as well as the packages in the cell below. This is easiest to do in a terminal. Then open a jupyter-notebook, like this one, and follow along with the tuturial by copying and executing the code in the cells, and adding your own documentation between them using markdown. Feel free to modify parameters to see their effects on the downstream results.

In contrast to the ipyrad CLI, the ipyrad API gives users much more fine-scale control over the parallelization of their analysis, but this also requires learning a little bit about the library that we use to do this, called ipyparallel. This library is designed for use with jupyter-notebooks to allow massive-scale multi-processing while working interactively.

Understanding the nuts and bolts of it might take a little while, but it is fairly easy to get started using it, especially in the way it is integrated with ipyrad. To start a parallel client to you must run the command-line program 'ipcluster'. This will essentially start a number of independent Python processes (kernels) which we can then send bits of work to do. The cluster can be stopped and restarted independently of this notebook, which is convenient for working on a cluster where connecting to many cores is not always immediately available.

Open a terminal and type the following command to start an ipcluster instance with N engines.

These data are archived on the NCBI sequence read archive (SRA) under accession id SRP021469. As part of the ipyrad analysis tools we have a wrapper around the SRAtools software that can be used to query NCBI and download sequence data based on accession IDs. Run the code below to download the fastq data files associated with this study. The data will be saved the specified directory which will be created if it does not already exist. The compressed file size of the data is a little over 1GB. If you pass your ipyclient to the .run() command below then the download will be parallelized.

In [5]:

## download the Pedicularis data set from NCBIsra=ipa.sratools(accession="SRP021469",workdir="fastqs-Ped")sra.run(force=True,ipyclient=ipyclient)

This object stores the parameters of the assembly and the organization of data files.

In [6]:

## you must provide a name for the Assemblydata=ip.Assembly("pedicularis")

New Assembly: pedicularis

Set parameters for the Assembly. This will raise an error if any of the parameters are not allowed because they are the wrong type, or out of the allowed range.

In [10]:

## set parametersdata.set_params("project_dir","analysis-ipyrad")data.set_params("sorted_fastq_path","fastqs-Ped/*.fastq.gz")data.set_params("clust_threshold","0.90")data.set_params("filter_adapters","2")data.set_params("max_Hs_consens",(5,5))data.set_params("trim_loci",(0,5,0,0))data.set_params("output_formats","psvnkua")## see/print all parametersdata.get_params()

Branch to create several final data sets with different parameter settings¶

In [11]:

## create a branch for outputs with min_samples = 4 (lots of missing data)min4=data.branch("min4")min4.set_params("min_samples_locus",4)min4.run("7")## create a branch for outputs with min_samples = 13 (no missing data)min13=data.branch("min13")min13.set_params("min_samples_locus",13)min13.run("7")## create a branch with no missing data for ingroups, but allow## missing data in the outgroups by setting population assignments.## The population min-sample values overrule the min-samples-locus parampops=data.branch("min11-pops")pops.populations={"ingroup":(11,[iforiinpops.samplesif"prz"notini]),"outgroup":(0,[iforiinpops.samplesif"prz"ini]),}pops.run("7")## create a branch with no missing data and with outgroups removednouts=data.branch("nouts_min11",subsamples=[iforiinpops.samplesif"prz"notini])nouts.set_params("min_samples_locus",11)nouts.run("7")

We have a lot more information about analysis tools in the ipyrad documentation. But here I'll show just a quick example of how you can easily access the data files for these assemblies and use them in downstream analysis software. The ipyrad analysis tools include convenient wrappers to make it easier to parallelize analyses of RAD-seq data. Please see the full documentation for the ipyrad.analysis tools in the ipyrad documentation for more details.

In [6]:

importipyradasipimportipyrad.analysisasipa

In [7]:

## you can re-load assemblies at a later time from their JSON filemin4=ip.load_json("analysis-ipyrad/min4.json")min13=ip.load_json("analysis-ipyrad/min13.json")nouts=ip.load_json("analysis-ipyrad/nouts_min11.json")

## create a structure analysis object for the no-outgroup data setstruct=ipa.structure(name=nouts.name,data=nouts.outfiles.str,mapfile=nouts.outfiles.snpsmap,)## set params for analysis (should be longer in real analyses)struct.mainparams.burnin=1000struct.mainparams.numreps=8000

## group taxa into 'populations'imap={"prz":["32082_przewalskii","33588_przewalskii"],"cys":["41478_cyathophylloides","41954_cyathophylloides"],"cya":["30686_cyathophylla"],"sup":["29154_superba"],"cup":["33413_thamno"],"tha":["30556_thamno"],"rck":["35236_rex"],"rex":["35855_rex","40578_rex"],"lip":["39618_rex","38362_rex"],}## optional: loci will be filtered if they do not have data for at## least N samples in each species. Minimums cannot be <1.minmap={"prz":2,"cys":2,"cya":1,"sup":1,"cup":1,"tha":1,"rck":1,"rex":2,"lip":2,}## sets a random number seedimportnumpynumpy.random.seed(12349876)## create a treemix analysis objecttmix=ipa.treemix(name=min13.name,data=min13.outfiles.snpsphy,mapfile=min13.outfiles.snpsmap,imap=imap,minmap=minmap,)## you can set additional parameter args heretmix.params.root="prz"tmix.params.global_=1## print the full paramstmix.params

## a dictionary for storing treemix objectstdict={}## iterate over values of mforrepinxrange(4):formiginxrange(4):## create new treemix object copyname="mig-{}-rep-{}".format(mig,rep)tmp=tmix.copy(name)## set params on new objecttmp.params.m=mig## run treemix analysistmp.run()## store the treemix objecttdict[name]=tmp

In [43]:

importtoyplot## select a single resulttmp=tdict["mig-1-rep-1"]## draw the tree similar to the Treemix plotting R code## this code is rather new and will be expanded in the future.canvas=toyplot.Canvas(width=350,height=350)axes=canvas.cartesian(padding=25,margin=75)axes=tmp.draw(axes)

## a dictionary mapping sample names to 'species' namesimap={"prz":["32082_przewalskii","33588_przewalskii"],"cys":["41478_cyathophylloides","41954_cyathophylloides"],"cya":["30686_cyathophylla"],"sup":["29154_superba"],"cup":["33413_thamno"],"tha":["30556_thamno"],"rck":["35236_rex"],"rex":["35855_rex","40578_rex"],"lip":["39618_rex","38362_rex"],}## optional: loci will be filtered if they do not have data for at## least N samples/individuals in each species.minmap={"prz":2,"cys":2,"cya":1,"sup":1,"cup":1,"tha":1,"rck":1,"rex":2,"lip":2,}## a tree hypothesis (guidetree) (here based on tetrad results)## for the 'species' we've collapsed samples into.newick="((((((rex, lip), rck), tha), cup), (cys, (cya, sup))), prz);"

## set some optional params, leaving others at their defaults## you should definitely run these longer for real analysesb.params.burnin=1000b.params.nsample=2000b.params.sampfreq=20## print paramsb.params

You can either call 'write_bpp_files()' to write input files for this data set to be run in BPP, and then call BPP on those files, or you can use the '.run()' command to run the data files directly, and in parallel on the cluster. If you specify multiple reps then a different random sample of loci will be selected, and different random seeds applied to each replicate.