Skip to content

Assembly I: Assembling contigs

image

All work for this exercise will occur in the 2.assembly/ directory.


The standard input files for SPAdes

SPAdes has preferences for how sequence data is provided to it. To begin, we will look at the types of data accepted by SPAdes:

Navigate to working directory

cd /nesi/nobackup/nesi02659/MGSS_U/<YOUR FOLDER>/2.assembly

Remember to update <YOUR FOLDER> to your own folder

Load SPAdes and check parameters

# Load module
module purge
module load SPAdes/4.0.0-foss-2023a-Python-3.11.6

# Check parameters
spades.py -h
SPAdes parameters
...
Input data:
  --12 <filename>             file with interlaced forward and reverse paired-end reads
  -1 <filename>               file with forward paired-end reads
  -2 <filename>               file with reverse paired-end reads
  -s <filename>               file with unpaired reads
  --merged <filename>         file with merged forward and reverse paired-end reads
  --pe-12 <#> <filename>      file with interlaced reads for paired-end library number <#>.
                              Older deprecated syntax is -pe<#>-12 <filename>
  --pe-1 <#> <filename>       file with forward reads for paired-end library number <#>.
                              Older deprecated syntax is -pe<#>-1 <filename>
  --pe-2 <#> <filename>       file with reverse reads for paired-end library number <#>.
                              Older deprecated syntax is -pe<#>-2 <filename>
  --pe-s <#> <filename>       file with unpaired reads for paired-end library number <#>.
                              Older deprecated syntax is -pe<#>-s <filename>
  --pe-m <#> <filename>       file with merged reads for paired-end library number <#>.
                              Older deprecated syntax is -pe<#>-m <filename>
  --pe-or <#> <or>            orientation of reads for paired-end library number <#> 
                              (<or> = fr, rf, ff).
                              Older deprecated syntax is -pe<#>-<or>
  --s <#> <filename>          file with unpaired reads for single reads library number <#>.
                              Older deprecated syntax is --s<#> <filename>
  --mp-12 <#> <filename>      file with interlaced reads for mate-pair library number <#>.
                              Older deprecated syntax is -mp<#>-12 <filename>
  --mp-1 <#> <filename>       file with forward reads for mate-pair library number <#>.
                              Older deprecated syntax is -mp<#>-1 <filename>
  --mp-2 <#> <filename>       file with reverse reads for mate-pair library number <#>.
                              Older deprecated syntax is -mp<#>-2 <filename>
  --mp-s <#> <filename>       file with unpaired reads for mate-pair library number <#>.
                              Older deprecated syntax is -mp<#>-s <filename>
  --mp-or <#> <or>            orientation of reads for mate-pair library number <#> 
                              (<or> = fr, rf, ff).
                              Older deprecated syntax is -mp<#>-<or>
  --hqmp-12 <#> <filename>    file with interlaced reads for high-quality mate-pair library number <#>.
                              Older deprecated syntax is -hqmp<#>-12 <filename>
  --hqmp-1 <#> <filename>     file with forward reads for high-quality mate-pair library number <#>.
                              Older deprecated syntax is -hqmp<#>-1 <filename>
  --hqmp-2 <#> <filename>     file with reverse reads for high-quality mate-pair library number <#>.
                              Older deprecated syntax is -hqmp<#>-2 <filename>
  --hqmp-s <#> <filename>     file with unpaired reads for high-quality mate-pair library number <#>.
                              Older deprecated syntax is -hqmp<#>-s <filename>
  --hqmp-or <#> <or>          orientation of reads for high-quality mate-pair library number <#> 
                              (<or> = fr, rf, ff).
                              Older deprecated syntax is -hqmp<#>-<or>
  --sanger <filename>         file with Sanger reads
  --pacbio <filename>         file with PacBio reads
  --nanopore <filename>       file with Nanopore reads
  --trusted-contigs <filename>
                              file with trusted contigs
  --untrusted-contigs <filename>
                              file with untrusted contigs
...

At a glance, you could provide any of the following data types to SPAdes and have it perform an assembly:

  1. Illumina paired-end sequencing data
  2. Sanger sequences
  3. PacBio reads
  4. Oxford Nanopore reads
  5. Pre-assembled scaffolds for guiding the assembly

Awkwardly, while SPAdes accepts multiple input libraries (i.e. samples) in a single assembly, this behaviour does not work with the -meta flag enabled, which is needed in our example to activate metagenome assembly mode. We therefore need to concatenate our four individual samples together ready for sequencing.

Concatenate samples by read direction

cat sample1_R1.fastq.gz sample2_R1.fastq.gz sample3_R1.fastq.gz sample4_R1.fastq.gz > for_spades_R1.fq.gz
cat sample1_R2.fastq.gz sample2_R2.fastq.gz sample3_R2.fastq.gz sample4_R2.fastq.gz > for_spades_R2.fq.gz

Note that these FASTQ files are compressed, yet we can concatenate them together with the cat command regardless. This is a nice feature of .gz files that is handy to remember.


Basic assembly parameters

For any assembler, there are a lot of parameters that can be fine-tuned depending on your data. As no two data sets are the same, it is almost impossible to predict which parameter combinations will yield the best outcome for your dataset. That said, an assembly can be quite a resource-intensive process and it is generally not practical to test every permutation of parameter values with your data. In genomics, the saying goes that the best assembly is the one that answers your question. As long as the data you are receiving is meaningful to the hypothesis you are seeking to address, then your assembly is as good as it needs to be.

Generally speaking, assemblers are developed in a way where they run with default parameters that have been empirically demonstrated to produce the best outcome on average across multiple data sets. For most purposes, there is not a lot of need to change these, but some parameters that we would always want to look at include:

  1. k-mer sizes to be assembled over, and step size if using a range
  2. Number of threads to use during assembly
  3. Memory limit to prevent the assembler from using up all available RAM and forcing the computer to use its swap space

Setting the k-mer size

Depending on which assembler you are using, the commands for choosing the k-mer sizes for the assembly vary slightly, but they are recognisable between programs. In SPAdes, you can set the k-mer size using either

Terminal

spades.py -k 21,33,55,77,99,121 ...

spades.py -k auto ...

The first command lets us specify the k-mers ourselves, or we are letting SPAdes automatically pick the most appropriate size.

Specifying the number of threads

Terminal

spades.py --threads 20 ...

If no thread count is specified by the user, SPAdes will assemble with 16 threads.

Setting a memory limit

By far, the worst feature of SPAdes is the high memory requirement for performing an assembly (see our Bonus material on assembling with IDBA-UD instead). In the absence of monitoring, SPAdes will request more and more memory as it proceeds. If this requires more memory than is available on your computer, your system will start to store memory to disk space. This is an extremely slow operation and can render your computer effectively unusable. As the default memory limit is only 250GB you also need to provide a specific limit when running a job on NeSI with more allocated memory. In any case, if SPAdes exceeds the default or set memory limit during its run, it will just terminate. It will not try to stay under the limit.

Terminal

spades.py --memory 400GB ...

No such parameter exists in IDBA-UD, but it requires far less RAM than SPAdes, so you are less likely to need it.


Preparing an assembly job for slurm

NeSI does not allow users to execute large jobs interactively on the terminal. Instead, the node that we have logged into has only a small fraction of the computing resources that NeSI houses. It is used to write small command scripts, which are then deployed to the large compute nodes by a system called slurm. The ins and outs of working in slurm are well beyond the scope of this workshop (and may not be relevant if your institution uses a different resource allocation system). In this workshop, we will therefore only be showing you how to write minimal slurm scripts sufficient to achieve our goals. By the end of the workshop, you should have built up a small collection of slurm scripts for performing the necessary stages of our workflow and with experience you will be able to modify these to suit your own needs.

Submitting a SPAdes job to NeSI using slurm

To begin, we need to open a text file using the nano text editor.

Create script named spades_assembly.sl using nano

nano spades_assembly.sl

Into this file, either write or copy/paste the following commands:

Warning

Paste or type in the following. Remember to update <YOUR FOLDER> to your own directory.

slurm script

#!/bin/bash -e

#SBATCH --account       nesi02659
#SBATCH --job-name      spades_assembly
#SBATCH --partition     milan
#SBATCH --time          00:30:00
#SBATCH --mem           10G
#SBATCH --cpus-per-task 12
#SBATCH --error         %x_%j.err
#SBATCH --output        %x_%j.out

# Performance fix required when submitted from OnDemand (e.g. JupyterLab)
OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK

# Load modules
module purge
module load SPAdes/4.0.0-foss-2023a-Python-3.11.6

# Working directory
cd /nesi/nobackup/nesi02659/MGSS_U/<YOUR FOLDER>/2.assembly

# Run SPAdes
spades.py --threads $SLURM_CPUS_PER_TASK \
          --memory $((SLURM_MEM_PER_NODE/1024)) \
          --tmp-dir "$TMPDIR" \
          --meta -k 33,55,77,99,121 \
          -1 for_spades_R1.fq.gz \
          -2 for_spades_R2.fq.gz \
          -o spades_assembly/

Save your script by pressing Ctrl + o then Enter ; exit the editor by pressing Ctrl + x . Going through those lines one by one:

Slurm parameters and their functions
Parameter Description
#!/bin/bash -e Header for the file, letting NeSI know how to interpret the following commands. The -e flag means that the slurm run will halt at the first failed command (rather than pushing through and trying to execute subsequent ones)
--account The name of the project account to run the job under. You are provided with this when you create a project on NeSI
--job-name The name of the job, to display when using the squeue command
--partition This is a parameter that enables us to access the milan nodes (think of a node as a single computer within the cluster) where resources are reserved for this workshop
--time Maximum run time for the job before it is killed
--mem Amount of server memory to allocate to the job. If this is exceeded, the job will be terminated
--cpus-per-task number of processing cores to assign to the job. This should match with the number used by your assembler
--error File to log the standard error stream of the program.
This is typically used to prove error reports, or to just inform the user of job progress
--output File to log the standard output stream of the program.
This is typically used to inform the user of job progress and supply messages

The module load command needs to be invoked within your slurm script. It is also a good idea to explicitly set the path to your files within the job so that

  1. There is no chance of having the job fail immediately because it cannot find the relevant files
  2. When looking back through your slurm logs, you know where the data is meant to be

SPAdes parameters of note

Parameter Description
--threads Number of threads (see above). Here, we use a special slurm environment variable: $SLURM_CPUS_PER_TASK to tell the programme to use the same number of threads allocated for this job via --cpus-per-task.
--tmp-dir Improve performance by using RAM for temporary files
--memory Memory limit (see above). Here, we use the special slurm environment variable $SLURM_MEM_PER_NODE. As this value is given in MB and SPAdes expects GB, we have to devide by 1024.
--meta Activate metagenome assembly mode. Default is to assemble your metagenome using single genome assembly assumptions
-k k-mer sizes for assembly. These choices will provide the output we will use in the Binning session, but feel free to experiment with these to see if you can improve the assembly
-1 Forward reads, matched to their reverse partners
-2 Reverse reads, matched to their forward partners
-o Output directory for all files

It is a good idea to match your number of threads request in the slurm script with what you intend to use with SPAdes because your project usage is calculated based off what you request in your slurm scripts rather than what you actually use. Requesting many unused threads simply drives your project down the priority queue. By contrast, requesting fewer threads than you attempt to use in the program (i.e., request 10 in slurm, set thread count to 30 in SPAdes) will result in reduced performance, as your SPAdes job will divide up jobs as though it has 30 threads, but only 10 will be provided.

Once you are happy with your slurm script, execute the job by navigating to the location of your script and entering the command

Submitting the slurm script for SPAdes assembly

sbatch spades_assembly.sl

You will receive a message telling you the job identifier for your assembly. Record this number, as we will use it in the next exercise.

Monitoring job progress

You can view the status of your current jobs using the command

Terminal

squeue --me

Terminal output

     JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
   9996013     milan spades_a    drost PD       0:00      1 (Priority)

We can see here that the job has not yet begun (status ST is PD = pending and run TIME is zero), as NeSI is waiting for resources to come available. When they do, the output will change to

Terminal output

     JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
   9996013     milan spades_a    drost  R       0:16      1 mc064

Which allows us to track how far into our run we are.

When your job starts running, files with suffixes .err and .out will be created in the directory from where you submitted your job. These files will have have your job name and job identification number as file names.