# PCA on genotype matrix with multiple alleles

Consider an m x n genotype matrix of m haploid samples and n SNPs where each value is an allele encoded by an integer (0,1,2,3).

Is there a good/standard way to encode the alleles in order to perform a PCA on this matrix to investigate population structure ?

I have seen this with matrices of diploid samples where each SNP is encoded as 0/1/2 to represent the number of non-reference allele, but is there a way to consider more than 2 alleles ?

e.g. difference between alleles 1 and 3 should be equal to difference between alleles 1 and 2

You can make a 'dummy variable' for each allele. That means that you don't have info per SNP, but for SNPs with more alleles, the allele is present (1) or not (0).

I recommend the Hamming distance and doing a multidimensional scaling (a procedure similar to PCA but for distances), that way you don't create new variables for the same position.

The distance function can be defined as

hamming_dist <- function(x, y) {
if (x != y) {
1
} else {
0
}


Or you can use a package:

library(e1071)
H <- hamming.distance(as.matrix(X))


Or as you yourself pointed other implementations.

You can use them to calculate the distance between all samples and then use it for the multidimensional analysis (cmdscale in R)

plot(cmdscale(H))

• Although I accepted the other answer as it addresses PCA directly, this is a really good alternative! However, the implementation of the Hamming distance in e1071 relies on a nested loop, making it unusable on large SNP matrices. This blog provides a (much) more efficient implementation (in R as well) using matrix multiplication: johanndejong.wordpress.com/2015/10/02/… – cmdoret Apr 11 '18 at 11:41
• I add your finding to the answer (the comment might get deleted). Glad you find it useful. – llrs Apr 11 '18 at 14:54
{"ft_lang_label":"__label__en","ft_lang_prob":0.9306938,"math_prob":0.9564849,"size":539,"snap":"2020-34-2020-40","text_gpt3_token_len":127,"char_repetition_ratio":0.12897196,"word_repetition_ratio":0.020833334,"special_character_ratio":0.23376623,"punctuation_ratio":0.07826087,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.957989,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-30T03:26:54Z\",\"WARC-Record-ID\":\"<urn:uuid:887254d6-724e-4d1b-a3c1-ff578d9a8ea4>\",\"Content-Length\":\"153498\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ef227387-3c4e-4877-829c-20bc753ce09a>\",\"WARC-Concurrent-To\":\"<urn:uuid:bbf10501-033c-49f3-a26d-219bfe92e45b>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://bioinformatics.stackexchange.com/questions/4030/pca-on-genotype-matrix-with-multiple-alleles\",\"WARC-Payload-Digest\":\"sha1:B3RYHOQKM4YXMSE7OM7XMGWU6L5F5HDQ\",\"WARC-Block-Digest\":\"sha1:MONCJVO4O6GAO5EUUSMEDSYR4QWEQTQJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600402101163.62_warc_CC-MAIN-20200930013009-20200930043009-00211.warc.gz\"}"}