This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Extract Random Snps From Whole Genome Data?

Hi All,

I have a dataset containing more than 500K SNPs. Now I need to extract randome 15K SNPs from that. Please help me to do so.

Thanks

snp extraction

How do the answers know that you have bfile (BED) as input?

3 answers

Transform your file in PED format

plink --bfile file1 --recode --out file2

Extract snps column

cut -f 2 file2.map > snps.map

Choose 15k SNPs

shuf -n 15000 snps.map > snps.subset.map

Extract those SNPs from your first file

plink --bfile file1 --extract snps.subset.map --make-bed --out file3

extra step of making a plain text PED file. And not all unix systems have shuf installed. sort -R on the BIM file is all you need.

I think the unix command shuf does the trick (assuming the SNPs are one per line in a text/VCF file)

shuf -n 15000 snps_file.vcf

thanks, I have the file in .bed, .bim, .fam format!

use PLINK to create a VCF file and follow Pablo's suggestion. Or use the PLINK R interface to do the same.

plink --bfile file1 --recode --out file2
cut -f 2 file2.map > snps.map
shuf -n 15000 snps.map > snps.subset.map
plink --bfile file1 --extract snps.subset.map --make-bed --out file3

plink --bfile file1 --recode --out file2

cut -f 2 file2.map > snps.map

shuf -n 15000 snps.map > snps.subset.map

plink --bfile file1 --extract snps.subset.map --make-bed --out file3

the command shuf is not found when I try to run this on terminal in OSX.

you can also just use the UNIX sort to randomly grab lines out of your BIM file...

sort -R yourdata.bim | head -15000 | awk '{print$2}' > random15k.snps
plink --file yourdata --extract random15k.snps --make-bed --out random15k

this avoids the time and disk space to convert your file to a plain-text PED file and keeps it all binary for speed and disk friendliness =)

thanks. this also worked perfectly.

Log in to answer this question.