This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Extract Snps From Chromosome 1-22 In One Command From *.Bed, *.Bim And *.Fam Files

Hi,

I need to extract chr 1-22 from SNP array data which also contains mtDNA and Y SNP data. Plink does it chromosome by chromosome and in this case I need to extract one chromosome by another and then join it, but I need something by which I can do it in one command.

Alternatively, the way to remove mt and Y SNPs from the main file would also work equally.

Please help if somebody can write the commands.

Thanks

plink snp chromosome

Providing an example of the contents of your file will likely lead to a quick answer.

2 answers

awk '{ if ($1 == 1 || $1 == 22) print $2 }' file.bim > snp.txt

plink --bfile file --extract snp.txt --make-bed --out newfile

this gives me only 1 and 22 numbers of chromosomes while I need 1 to 22! thanks

and you can get all chroms like: awk '($1 ~ /\d+/)' file.bim > snp.txt

^all numbered chroms^

Sorry, I misunderstood your question.

awk '{ if ($1 >= 1 && $1 <= 22) print $2 }' file.bim > snp.txt

thx Baboune. It helped me too.

Assuming it's a bed file, or some other file that has the chromosome name in the first column, it's as simple as doing

perl -ne 'print $_ if /^chr[12]?[0-9]/' yourfile >outfile

As Aaron said, it's difficult to tell you about the others without a sample of the file format.

You really should read up on regular expressions, and the use of grep, awk, sed, or perl to do pattern matching.

Log in to answer this question.