Skip to content

Instantly share code, notes, and snippets.

@variani
Last active April 2, 2024 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save variani/86c6f18405fd8931fcb213fcdd1ba27a to your computer and use it in GitHub Desktop.
Save variani/86c6f18405fd8931fcb213fcdd1ba27a to your computer and use it in GitHub Desktop.
plink commands
# ------------------
# from bgen to bed
# ------------------
plink2 --bgen myfile.bgen \
# filter by chr or by snps names
--chr 1 --extract {params.snps} \
--make-bed --out myoutput
# 1. plink will create temporary/intermediate files such as *-temporary.{pgen,psam,pvar}
# 2. sample ids in .fam will be `ID1_ID1`
#---------------------
# from bed to vcf
#---------------------
plink --bfile $bfile --chr $chr \
--keep-allele-order --recode vcf-iid --out $chr \
--threads $cores
bgzip -f $chr.vcf
tabix -f $chr.vcf.gz
# Tractor pipeline: https://github.com/eatkinson/Post-QC/blob/master/CohortDataQC_final.sh
# Plink command example to merge two bed files: https://www.biostars.org/p/101191/#101247
# Annotations.
# rsid names from snpdb
# https://hgdownload.cse.ucsc.edu/goldenPath/hg38/database/
GENOME=hg38
SNPBUILD=144
curl -s http://hgdownload.cse.ucsc.edu/goldenPath/$GENOME/database/snp$SNPBUILD.txt.gz | zcat | cut -f 2,3,4,5,6,7,10,16 > dbsnp.$SNPBUILD.$GENOME.bed
# References
# - https://gist.github.com/elowy01/93922762e131d7abd3c7e8e166a74a0b
#---------
# stats
#---------
# number of samples
bcftools query -l $chr.vcf | wc -l
# number of variants
bcftools query -f '%POS\n' $chr.vcfist | wc -l
#----------
# filter
#----------
# filter by variant IDs
# - https://github.com/samtools/bcftools/issues/776
bcftools view -i'ID=@test.snp' my.vcf
# filter by sample IDs
bcftools view -s sample1,sample2 file.vcf > filtered.vcf
bcftools view -S sample_file.txt file.vcf > filtered.vcf
#----------------
# update fields
#----------------
# https://www.biostars.org/p/363263/
# replace variant ID by a custom name 'chr:pos:ref:alt'
bcftools annotate --set-id '%CHROM:%POS:%REF:%FIRST_ALT' input.vcf > output.vcf
#----------
# index
#----------
bgzip -f $chr.vcf
tabix -f $chr.vcf.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment