geno_strains.R 2.49 KB
Newer Older
Marie Bourdon's avatar
Marie Bourdon committed
1
#' @title Create haplotype for a new mouse strain into a dataframe
Marie  BOURDON's avatar
Marie BOURDON committed
2
3
4
5
#'
#' @description This functions adds columns for parental strains used in the cross in the annotation data frame, from the genotype data frame in which one or several animal of the parental strains were genotyped.
#' If several animals of one strain were genotyped, a consensus is created from these animals.
#' The consensus is created as follow : if the indivuals carry the same allele, this allele is kept, otherwise, the allele is noted as "N". If individuals show residual heterozygosity, it is encoded as "H".
6
#' @param ref data frame with the annotation of the arrray used
Marie  BOURDON's avatar
Marie BOURDON committed
7
8
9
10
11
12
13
14
15
16
17
18
#' @param geno data frame with the genotyping results for your cross from miniMUGA array
#' @param par1 first parental strain used in the cross, the name must be written as in the geno data frame
#' @param par2 second parental strain used in the cross, the name must be written as in the geno data frame
#' @param name1 name of the first parental strain to use as the column name in the ref data frame
#' @param name2 name of the second parental strain to use as the column name in the ref data frame
#'
#' @import dplyr
#' @import tidyr
#'
#' @export
#'
geno_strains <- function(ref,geno,par1,par2,name1,name2){
Marie Bourdon's avatar
Marie Bourdon committed
19
20
21
22
23
24
25
  #rename df columns
  geno <- geno %>% rename("marker"=1,
                          "id"=2,
                          "allele_1"=3,
                          "allele_2"=4)


Marie  BOURDON's avatar
Marie BOURDON committed
26
27
  #recode genotypes from 2 alleles to 1
  geno <- geno %>% mutate_all(as.character)
Marie Bourdon's avatar
Marie Bourdon committed
28
29
30
31
  geno <- geno %>% filter(id %in% c(par1,par2))
  geno <- geno %>% mutate(Geno=case_when(allele_1 == "-" | allele_2 == "-" ~ "N",
                                         allele_1 == allele_2 ~ allele_1,
                                         allele_1 %in% c("A","T","G","C") & allele_2 %in% c("A","T","G","C") ~ "H"))
Marie  BOURDON's avatar
Marie BOURDON committed
32

Marie Bourdon's avatar
Marie Bourdon committed
33
  geno <- geno %>% select(marker,id,Geno) %>% pivot_wider(names_from = id, values_from = Geno)
Marie  BOURDON's avatar
Marie BOURDON committed
34
35
36


  #create consensus
37
  geno <- geno %>% mutate(parent1 = !!sym(par1[1]))
Marie  BOURDON's avatar
Marie BOURDON committed
38
  if(length(par1)!=1){
39
40
41
    for(i in 2:length(par1)){
      geno <- geno %>% mutate(parent1 = ifelse(parent1==!!sym(par1[i]),parent1,"N"))
    }
Marie  BOURDON's avatar
Marie BOURDON committed
42
43
  }

44
  geno <- geno %>% mutate(parent2 = !!sym(par2[1]))
Marie  BOURDON's avatar
Marie BOURDON committed
45
  if(length(par2)!=1){
46
47
48
    for(i in 2:length(par2)){
      geno <- geno %>% mutate(parent2 = ifelse(parent2==!!sym(par2[i]),parent2,"N"))
    }
Marie  BOURDON's avatar
Marie BOURDON committed
49
50
  }

Marie Bourdon's avatar
Marie Bourdon committed
51
52
  geno <- geno %>% select(marker,parent1,parent2)
  colnames(geno) <- c("marker",name1,name2)
Marie  BOURDON's avatar
Marie BOURDON committed
53
54

  #merge with ref file
Marie Bourdon's avatar
Marie Bourdon committed
55
  ref <- full_join(ref,geno,by=c("marker"="marker"))
Marie  BOURDON's avatar
Marie BOURDON committed
56
57
  return(ref)
}