-
Notifications
You must be signed in to change notification settings - Fork 1
Exercises Series 1
Before diving into the exercises, please read the General Guidelines
Context
We will start by aligning reads to the reference assembly using the BWA mem algorithm, which is arguably the standard aligner for short reads.
Task
Implement a rule to align reads to the already indexed assembly using BWA.
Details
- The reference assembly file is
data/genome.fa - For now, your rule should only align the existing reads file:
data/samples/A.fastq - The command to align reads to an indexed assembly with BWA is:
bwa mem <assembly_file> <reads_file> - BWA mem outputs alignments in the SAM format; usually, it is advised to use the BAM format (a compressed binary version of SAM). SAM files can be converted to BAM with the software Samtools; in this case, we can pipe the output of BWA to samtools to obtain a BAM file with the following command:
bwa mem <assembly_file> <reads_file> | samtools view -b > <output_file>. By default, the results ofsamtools vieware output tostdout, we need to redirect it to the actual output file with> - The output file will have the extension
.bam(don't forget to specify this in the output directive!).
Relevant Wiki section
Context
We have implemented a rule to align reads to a reference, but this rule only works for the reads file data/samples/A.fastq. We would like to align another reads file data/samples/B.fastq, and maybe even more reads files in the future.
Task
Generalize the alignment rule to handle any reads file present in data. From now on, rules should always be written to handle any relevant input from previous rules.
Relevant Wiki section
Hint
Click to expand
Use a wildcard to specify the reads file name.
Don't forget the output file extension: '.bam'
Context
We aligned the reads to the reference and generated BAM files. Many analyses require that BAM files are sorted by genomic coordinates, because sorted BAM files can be processed much more easily than unsorted ones.
Task
Implement a rule to sort the BAM files with Samtools.
Details
- The command to sort BAM files with Samtools is
samtools sort -O bam <alignment_file> > <output_file> - The -O parameter specify the output format. By default, the results are output to
stdout, which we redirect to the output file with>. - The output file will have the extension
.sorted.bam(to differentiate them from the alignment bam files).
Context
Just like we did with the reference assembly, it is common to index alignment files to speed up access to specific regions in the alignment.
Task
Implement a rule to index the sorted BAM files with Samtools.
Details
- The command to index BAM files with Samtools is
samtools index <alignment_file> - For this rule, you don't have to specify the output file in the command, as it will be automatically generated. You'll still need to specify the output in the
outputdirective! The index file will have the extension.bam.bai.
Context
We aligned reads from two samples (A and B) to the reference assembly, and obtained two sorted and indexed alignment files. We will now call variants from these two files together; the output will be a VCF file.
Task
Implement a rule to call genomic variants from the alignment files using BCFtools.
Details
- The command to generate the VCF file is:
bcftools mpileup -f <assembly_file> <alignment_files> | bcftools call -mv - > <output_file> - Variant calling processes all alignment files together; use concepts from this section to do that. Your rule should also make sure that the alignment files are indexed
- The -v flag in bcftools call is used to output only variant sites. The -m flags specify to use a specific variant calling model (multiallelic and rare variants caller)
- The output file will have the extension
.vcf
Relevant Wiki section
Hints
Process all alignment files
- Use the expand syntax to collect all alignment files.
- Add a list of samples to your Snakefile for convenience.
Ensure alignment files are indexed
Add the output of the indexing rule as input to the variant calling rule.
Context
BCF tools calls variants using a probabilistic model. An important parameter of this model is the expected substitution rate. We would like to add a parameter to the variant calling rule to easily specify this substitution rate.
Task
Add a non-file parameter to the variant calling rule to control prior substitution rate.
Details
- The prior substitution rate is given to
bcftools callwith the parameter -P. - The default value is 0.001
Relevant Wiki section
Context
We now have a VCF file containing all variants for our samples. We would now like to quantify each type of substitution (e.g. A->T, C->A, ...) to detect potential biases.
We provide the external script create_substitution_table.py at the root of the workflow folder.
If you are ahead, you can have fun writing the Python script yourself.
This script should parse the output of the variant calling rule and output a table of substitution counts for each combination of nucleotides. If you decide to implement the script yourself:
- Only consider SNPs, i.e. single nucleotide substitutions, ignore insertions and deletions
- A suggestion of final output would be a tabulated file (tsv) containing a matrix with the following structure:
| A | T | G | C | |
|---|---|---|---|---|
| A | - | x | x | x |
| T | x | - | x | x |
| G | x | x | - | x |
| C | x | x | x | - |
Task
Implement a rule to quantify SNP substitutions from the VCF file generated by the variant calling rule.
Details
- Call the external Python script
create_substitution_table.pyfrom the rule using thescriptdirective.
Relevant Wiki section
Hints
Parsing of VCF file
VCF files are complex and not entirely standardized. Fortunately, for this exercise, we only need very little information that is easily accessible: the reference nucleotide and the variant nucleotide. The first lines of the file are comments, starting with "##". You can skip these lines when parsing the file. Then, there is a header line starting with a single "#", that you can skip as well (you could skip all lines starting with "#").
Then, for each line in the file, we want to split the line with the separator TAB ("\t"); the reference allele will be in the 3rd field and the variant allele will be in the 4th field (0-based indexing).
To save the results while processing the file, you could store the counts for each substitution in a dictionary, or even a Defaultdict if you know how to use them.
- Defining rules
- Rule dependencies
- Wildcards
- Executing workflows
- The expand syntax
- Non-file rule parameters
- Executing Python code