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 available and takes about <10 minutes to run completely, but can be run on even a laptop in about less than an hour.
We will use the Lagonosticta and Vidua data set from DaCosta & Sorenson 2016. This data set is composed of single-end 101bp reads from a ddRAD-seq library prepared with the SbfI and EcoRI enzymes and is available on NCBI by its study accession SRP059199. At the end of this notebook we also demonstrate the use of ipyrad.analysis tools to run downstream analyses on this data set.
The figure below from this paper shows the general workflow in which two fairly distinct clades were sequenced together but then analyzed separately.
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 tutorial 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 [1]:
## conda install ipyrad -c ipyrad
## conda install toytree -c eaton-lab
## conda install entrez-direct -c bioconda
## conda install sratools -c bioconda
In [8]:
## imports
import ipyrad as ip
import ipyrad.analysis as ipa
import ipyparallel as ipp
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.
In [9]:
## ipcluster start --n=20
In [11]:
## connect to cluster
ipyclient = ipp.Client()
ipyclient.ids
Out[11]:
These data are archived on the NCBI sequence read archive (SRA) under accession id SRP059199. For convenience, the data are also hosted at a public Dropbox link which is a bit easier to access. Run the code below to download and decompress the fastq data files, which will save them into a directory called fastqs-Finches/.
In [3]:
## download the Pedicularis data set from NCBI
sra = ipa.sratools(accession="SRP059199", workdir="fastqs-Finches")
sra.run(force=True, ipyclient=ipyclient)
In [4]:
## you must provide a name for the Assembly
data = ip.Assembly("Finches")
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 [6]:
## set parameters
data.set_params("project_dir", "analysis-ipyrad/Finches")
data.set_params("sorted_fastq_path", "fastqs-Finches/*.fastq.gz")
data.set_params("datatype", "ddrad")
data.set_params("restriction_overhang", ("CCTGCAGG", "AATTC"))
data.set_params("clust_threshold", "0.85")
data.set_params("filter_adapters", "2")
data.set_params("max_Hs_consens", (5, 5))
data.set_params("output_formats", "psvnkua")
## see/print all parameters
data.get_params()
In [7]:
## run steps 1 & 2 of the assembly
data.run("12")
In [8]:
## access the stats of the assembly (so far) from the .stats attribute
data.stats
Out[8]:
In [9]:
## run steps 3-5 (within-sample steps) of the assembly
data.run("345")
In [11]:
## create data set with only Vidua samples + outgroup
subs = [i for i in data.samples if "Vidua" in i] +\
[i for i in data.samples if "Anomalo" in i]
vidua = data.branch("vidua", subsamples=subs)
## create data set with only Lagonostica sampes + outgroup
subs = [i for i in data.samples if "Lagon" in i] +\
[i for i in data.samples if "Clyto" in i]
lagon = data.branch("lagon", subsamples=subs)
In [15]:
vidua.run("6")
In [16]:
lagon.run("6")
In [17]:
## iterate over data set and parameters
for assembly in [vidua, lagon]:
for min_sample in [4, 10]:
## create new assembly, apply new name and parameters
newname = "{}_min{}".format(assembly.name, min_sample)
newdata = assembly.branch(newname)
newdata.set_params("min_samples_locus", min_sample)
## run step 7
newdata.run("7")
In [3]:
vm4 = ip.load_json("analysis-ipyrad/Finches/vidua_min4.json")
vm4.stats
Out[3]:
In [4]:
lm4 = ip.load_json("analysis-ipyrad/Finches/lagon_min4.json")
lm4.stats
Out[4]:
In [5]:
## or read the full stats file as a bash command (cat)
!cat $vm4.stats_files.s7
In [6]:
## the same full stats for lagon
!cat $lm4.stats_files.s7
Thee is 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. You should still read the full tutorial of the software you are using to understand the full scope of the parameters involved and their impacts, but once you understand that, the ipyrad analysis tools provide an easy way to setup up scripts to sample different distributions of SNPs and to run many replicates in parallel.
In [13]:
import ipyrad.analysis as ipa
In [14]:
## you can re-load assemblies at a later time from their JSON file
min4 = ip.load_json("analysis-ipyrad/Finches/vidua_min4.json")
min10 = ip.load_json("analysis-ipyrad/Finches/vidua_min10.json")
In [29]:
## conda install raxml -c bioconda
## conda install toytree -c eaton-lab
In [48]:
## create a raxml analysis object for the min13 data sets
rax = ipa.raxml(
name=min10.name,
data=min10.outfiles.phy,
workdir="analysis-raxml",
T=20,
N=100,
o=[i for i in min10.samples if "Ano" in i],
)
In [49]:
## print the raxml command and call it
print rax.command
rax.run(force=True)
In [50]:
## access the resulting tree files
rax.trees
Out[50]:
In [51]:
## plot a tree in the notebook with toytree
import toytree
tre = toytree.tree(rax.trees.bipartitions)
tre.root(wildcard="Ano")
tre.draw(
width=350,
height=400,
node_labels=tre.get_node_values("support"),
#use_edge_lengths=True,
);
In [39]:
## create a tetrad analysis object
tet = ipa.tetrad(
name=min4.name,
seqfile=min4.outfiles.snpsphy,
mapfile=min4.outfiles.snpsmap,
nboots=100,
)
In [40]:
## run tree inference
tet.run(ipyclient)
In [41]:
## access tree files
tet.trees
Out[41]:
In [112]:
## plot results (just like above, but unrooted by default)
import toytree
qtre = toytree.tree(tet.trees.nhx)
qtre.root(wildcard="Ano")
qtre.draw(
width=350,
height=400,
node_labels=tre.get_node_values("support"),
);
In [54]:
## draw a cloud-tree to see variation among bootstrap trees
## note that the trees are UNROOTED here, but tips are in the
## same order in all trees.
boots = toytree.multitree(tet.trees.boots, fixed_order=tre.get_tip_labels())
boots.draw_cloudtree(orient='right', edge_style={"opacity": 0.05});