r - How to find homozygous recessive values -
stemming earlier question had trying check whether parent correct parent given child's genotype (see checking whether "string" expression in specified variable contained in several other variables)
now, i'm trying see whether if genotype of child recessive genotype (having 2 of same recessive values [alleles] gene). in case, child affected disease while parents not (the child proband). have tried figure out whether parents , child homozygous, , kinda worked out whether child matches parent's genotype.... these 2 pieces of information can't seem to work out whether child homozygous recessive...
here have far (following similar answer above):
homo <- read.table("/.../family1a/family1a_vcf.txt", sep="\t", header=t) d <- data.frame(list(mom = homo[c(1)], dad = homo[c(2)], child = homo[c(3)] ), stringsasfactors = false) check_homo <- function(x) { #homo m1 <- sapply(strsplit(as.character(d[,2]),"/"),function(x) x[1]) m2 <- sapply(strsplit(as.character(d[,2]),"/"),function(x) x[2]) d1 <- sapply(strsplit(as.character(d[,1]),"/"),function(x) x[1]) d2 <- sapply(strsplit(as.character(d[,1]),"/"),function(x) x[2]) c1 <- sapply(strsplit(as.character(d[,3]),"/"),function(x) x[1]) c2 <- sapply(strsplit(as.character(d[,3]),"/"),function(x) x[2]) mom_homo <- m1 == m2 dad_homo <- d1 == d2 child_homo <- c1 == c2 homo_matrix_d <- matrix(c(dad_homo,child_homo), ncol=2, byrow=true) homo_matrix_m <- matrix(c(mom_homo,child_homo), ncol=2, byrow=true) homo_match_dc <- rowsums(homo_matrix_d) homo_match_mc <- rowsums(homo_matrix_m) #which ones equal parents fam <- strsplit(as.character(d[c(1, 2, 3)]), "/") names(fam) <- c("mom", "dad", "child") mom_query <- fam[["child"]] == fam[["mom"]] dad_query <- fam[["child"]] == fam[["dad"]] fam_matrix <- matrix(c(mom=mom_query, dad=dad_query), nrow=2) child_match_parents <- rowsums(fam_matrix) #if child doesn't match parents , child_homo = recessive #if child equal parents,if homo_parent , homo_child child = dominant child_rec <- ifelse((child_match_parents < 1 & child_homo == "true"), "recessive", "other") child_dom <- ifelse((child_match_parents != 0 & child_homo == "true") & (mom_homo == "true" | dad_homo == "true"), "dominant", "other") } child_recessive_tmp <- data.frame(apply(x, 1, check_homo))
it last 2 lines in loop aren't working. whole thing wrong don't mind frustrated responses. in all, want 1 variable saying whether child's genotype homozygous recessive or not.
edit:
example of data: there 1 snp per row.
mom dad child rs1 a/a g/g a/g rs2 t/c t/c t/t rs3 a/a c/a a/a . . . rs100 g/c a/g c/a
the problem code: function returns nothing, regardless of whether want, not have output (i suggest use if()
else if()
, else
below).
also: although apply
function data frame, function not seem use input whatsoever. first few lines operate on original data frame -- not input.
and highly recommend learn how use debug
step through malfunctioning function.
here more succinct way implement function:
sample_dat <- data.frame(mom = c("a/a", "t/c", "a/a", "g/c"), dad = c("g/g", "t/c", "c/a", "a/g"), child = c("a/g", "t/t", "a/a", "c/a")) check_homo <- function(d) { fam <- sapply(d, function(.) strsplit(as.character(.), "/")) homozygous <- unlist(lapply(fam, function(x) identical(x[[1]], x[[2]]) ) ) if (homozygous[3] & !duplicated(fam)[3]) "recessive" else if (sum(homozygous[1:2]) >= 1 & homozygous[3] & duplicated(fam)[3]) "dominant" else "other" } apply(sample_dat, 1, check_homo) # [1] "other" "recessive" "dominant" "other"
you might use debug
step through check_homo
, understand purpose of each line.
Comments
Post a Comment