This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R programming: genotype concordance

Hi guys,

I have an R programming question. I want to compare the genotype (.GT columns) with the given alleles (.allele columns) in dataframe df1 and see if they are concordant or not. The rule is that if there is only one allele, it should be 0/0. If there are two alleles (for example GA), the genotype should be 0/1 and that is why I have mismatch in the concordance column for GA. So, A, T, G or C individually is 0/0 and in pair combination is 0/1. Based on this information, I want to add new concordance column next to every two columns that are compared with and have match or mismatch result in concordance column. This concordance column should be cbind next to every compared pair of column and get Result. Could you please help me get this done. Thank you.

df1

1.allele  1.GT  2.allele  2.GT
A         0/0   A         0/0
GA        0/0   CT        0/1
C         0/0   G         0/0

Result

1.allele  1.GT  1.Concordance  2.allele  2.GT  2.Concordance
A         0/0   match          A         0/0   match
GA        0/0   mismatch       CT        0/1   match
C         0/0   match          G         0/0   match
r

What is the second "Concordance" column for?

second concordance is for 2. columns, and since there is no mismatch it's left blank.

Are there also homozygous SNPs, such as GG or TT?

No, there are not, Thank you.

2 answers

Data:

df <- data.frame(allele1=c("A","AT","C"),GT1=c("0/0","0/0","0/0"),
allele2=c("AT","G","CG"),GT2=c("0/0","0/0","0/0"))

First I create a vector of correct translation. But I am assuming that there is always a 0/1, never a 1/0:

library(stringr)
translation <- function(x) ifelse(str_length(df[,x])>1,"0/1","0/0")
correct <- sapply(c(1,3),translation)

Finally, mis/match:

match.fun <- function(x) ifelse(df[,x] == correct[,(x/2)] , "match" , "mismatch"  )
comparison <- sapply(c(2,4),match.fun)
cbind(df[,1:2],"Concordance1"=comparison[,1],df[,3:4],"Concordance2"=comparison[,2])

This is awesome, Thank you!

The following should do what you need, assuming that your data.frame is called dat.

dat$concordance = ifelse(dat[,2]==dat[,5],"","mismatch")

Sorry, looks like you did not understand the question. Could you please check the updated question. Thanks

Log in to answer this question.