This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Convert Genotype Matrix Into Plink Format

I have a genotype matrix (700 000 rows of SNPs and 2000 columns of samples). It's coded as 0/1/2 or NA. I want to convert this into plink format ped and map files. What's the best way to do this?

Thanks for the help!

It looks like:

         Sample1      Sample2        Sample3     Sample N
  SNP1     0            1              0            2  
  SNP2     0            NA             0            0  
  SNP3     0            0              0            0  
  SNP4     0            NA             0            0  
  SNP5     0            1              0            2  
  SNP6     0            NA             0            0  
  SNP7     2            1              0            2  
  SNP8     NA            NA             NA            NA
plink

Can you show us sample of the file?

can you tell me, How can i do it in R? I have a genotype matrix (near 3000 animal with 50 000 SNP in columns). It's coded as 0/1/2 or NA. I want to convert this into plink format in form allelic format for example 0 to 0 0, 1 to 1 1 and 2 to 2 2. this is a format for PLINK for quaity control my data, What's the best way to do this in R?

1 answer

Based on your example data named raw.txt, you can make TPED format files, then use plink to convert to pedmap format:

#make tped
awk 'NR != 1 {print 1,$1,0,NR}' raw.txt > temp_snp.txt
cut -f2- raw.txt | sed '1,1d' | sed 's/0/A A/g' | sed 's/1/A B/g' | sed 's/2/B B/g' | sed 's/NA/0 0/g' > temp_geno.txt
paste temp_snp.txt temp_geno.txt > plink.tped

#make tfam
head -n1 raw.txt | tr '\t' '\n' | sed '1,1d' | awk '{print $1,$1,0,0,1,1}' > plink.tfam

#convert to PedMap
plink --noweb \
--tfile plink \
--recode \
--out plink

Or I would just use R for analysis.

Log in to answer this question.