Before starting make sure to install miniconda, bwa-mem2, samtools, and all dependencies. Obtain reference genome and resequencing data. I suggest copying data to a new drive and working from said drive. A huge amount of storage space will be needed for all of the .bam files. Check quality of genome see "Genomes" tutorial. Check quality of resequence data.
conda activate bwa-mem2
First create a new working directory to place all .fq.gz files for all species.
Index the genome to be used as a reference.
nohup bwa-mem2 index -p species1 {PWD}/reference.fasta &
This must be done for each individual including both the forward and reverse Illumina/DNBseq reads. Thus, a .bam file will be created for each individual. This will be done using the loop found in the script bwa_align.sh. Use nano to double check the script. The creation of all of the .bam files took several days.
nohup ./bwa_align.sh &
This will also be done using a loop. Ideally all mapping % is above 90%.
./samtools_flagstat.sh
Summarize the mapping results in a table
./make_flagstat_table.sh
Place all .bam files in a subdirectory. With the option -Ov this will create an uncompressed vcf file. The option -q 30 specifies the minimum mapping quality for an alignment to be used. The option -Q specifies the minimum quality for a base to be used.
nohup bcftools mpileup --threads 20 --skip-indels -q 30 -Q 20 -f {PWD}/reference.fasta -Oz -o species_pileup_date.vcf.gz {PWD}/alignments/*.bam &
nohup bcftools call -m -Oz -f GQ -o species_pileup_date_called.vcf.gz species_pileup_date.vcf.gz &
nohup bcftools filter -Oz -o species_pileup_date_called_filtered.vcf.gz -i 'DP>= 4 &MQ >= 40 & QUAL >= 30' species_pileup_date_called.vcf.gz &
nohup bcftools filter -e 'F_MISSING > 0.5' -Oz -o species_pileup_date_called_filtered_fmiss50.vcf.gz species_pileup_date_called_filtered.vcf.gz &
This is an alternative to the previous three steps if you have multiple vcfs that you want to process. Place all vcfs in a subdirectory and run the following script within the subdirectory to automatically process all files.
nohup ./process_vcf.sh &
nohup bcftools filter -i 'MAC >= 1' -Oz -o species_pileup_date_called_filtered_fmiss50_variant.vcf.gz species_pileup_date_called_filtered_fmiss50.vcf.gz &
nohup bcftools filter -e 'MAF > 0.00' -Oz -o species_date_called_filtered_fmiss50_invariant.vcf.gz species_pileup_date_called_filtered_fmiss50.vcf.gz
Index using tabix which is from samtools, found in bcftools env.
tabix species_pileup_date_called_filtered_fmiss50_variant.vcf.gz
tabix species_date_called_filtered_fmiss50_invariant.vcf.gz
bcftools concat --allow-overlaps species_date_called_filtered_fmiss50_invariant.vcf.gz species_pileup_date_called_filtered_fmiss50_variant.vcf.gz -O z -o species_date_called_filtered_fmiss50_allsites.vcf.gz
For this, you need a series of files: .ped, .pedind, .map. The .ped file gives your genotypes, the .pedind file gives information about the sample and the population assignment, and the .map file gives information about the position of the loci in the genome. The pedind file is very light and useful for viewing down the line. These files can be generated from your vcf and a chrom-map.
nohup bcftools view -h species_pileup_date_called_filtered_fmiss50_variant.vcf.gz | cut -f 1 | uniq | awk '{print $0"\t"$0}' > species_chrom-map.txt &
nohup vcftools --gzvcf species_pileup_date_called_filtered_fmiss50_variant.vcf.gz --plink --chrom-map species_chrom-map.txt --out species_pileup_date_called_filtered_fmiss50_variant.ped &
This will also create a *.map file with the snp location information
cat species_pileup_date_called_filtered_fmiss50_variant.ped | cut -f1-6 > tmp
cat species_pileup_date_called_filtered_fmiss50_variant.ped | cut -c 1,2 | paste tmp > species_pileup_date_called_filtered_fmiss50_variant.pedind
The pedind file can be further parsed using sed or awk to modify sample names. Eigensoft does not like long sample names. If you do modify the sample names in the .pedind make sure to also change them in the .ped file. The last column of the pedind file should be 1 and the second to last column gives the population or species assignment. Modify accordingly.
The map file should have been created when you created the ped file. Parse the map file so that column 1 is not 0. You can set column 1 equal to 1.
awk -vOFS='\t' '{$1 = "1"; print}' species_pileup_date_called_filtered_fmiss50_variant.map > species_pileup_date_called_filtered_fmiss50_variant_modified.map
Alternatively name chromosomes:
awk '{ if ($2 ~ /chrm1/) $1 = "1" ; print }' your_file.map > your_new_file.map
Also remove special characters like semicolons from scaffold names
sed -i 's|;|_|g' your_new_file.map
Then, sort the map file
sort -k1,1n -k4,4n your_new_file.map > your_new_sorted_file.map
To make a pca of your variant data, first create a parameter file to tell the program what to do. There is an example parameter file given called smartpca.par. Input the names of your files for the .ped, .pedind, and .map input options. Name your output options keeping the extensions the same.
Eigensoft must be previously installed. You can use conda to install.
smartpca -p smartpca.par
Then export results and visualize in R with smartpca.R script.
Activate eigensoft environment. Modify the example convertf parameter file given here. The files .geno, .snp, and .ind should have resulted from running smartpca. The subsequent files are output files, name appropriately. Then run convertf:
convertf -p convertf_example.par
This should create the .bed file that is needed to run admixture. Use the script given called run_admixture.sh to run for k 1 - 10. Update section called define base command with location where admixture is installed and where your .bed file is.
./run_admixture.sh