Commit 716a1393 authored by mariefbourdon's avatar mariefbourdon
Browse files

add modif to do

parent baf459b0
......@@ -9,7 +9,7 @@
"id" : "C81C94E6",
"lastKnownWriteTime" : 1622623114,
"last_content_update" : 1622623114332,
"path" : "~/stuart_package/stuart/DESCRIPTION",
"path" : "~/Documents/PhD/stuart_R/stuart/DESCRIPTION",
"project_path" : "DESCRIPTION",
"properties" : {
"cursorPosition" : "9,60",
......
{
"cursorPosition" : "22,39",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "8,12",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "16,28",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "23,55",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "9,60",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "84,18",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "135,15",
"last_setup_crc32" : "",
"scrollLine" : "111"
}
\ No newline at end of file
{
"cursorPosition" : "43,27",
"scrollLine" : "0",
"source_window_id" : ""
}
\ No newline at end of file
{
"cursorPosition" : "20,46",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "9,51",
"scrollLine" : "0"
}
\ No newline at end of file
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FDESCRIPTION="A58355B1"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FR%2Fgeno_strains.R="430FE7D7"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FR%2Fmark_allele.R="58D83345"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FR%2Fmark_match.R="DB7C2433"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FR%2Fmark_poly.R="E7505D7B"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FR%2Fmark_prop.R="52B275C1"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FR%2Fstuart_tab-data.R="524B281F"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FR%2Ftab_mark.R="C4894908"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2FR%2Fwrite_rqtl.R="B873B132"
~%2FDocuments%2FPhD%2Fstuart_R%2Fstuart%2Fvignettes%2FstuaRt.Rmd="B92E179B"
~%2Fstuart_package%2Fstuart%2FDESCRIPTION="BEB7232"
~%2Fstuart_package%2Fstuart%2FNAMESPACE="AE613167"
~%2Fstuart_package%2Fstuart%2FR%2Fgeno_strains.R="8F7B714A"
......
#' Data frame with miniMUGA genotyping of F2 individuals and parental strains
#'
#' A dataset containing the genotypes of 176 F2 individuals
#'
#' @format A data frame with 2002493 observations of 11 variables
"genos"
#' @title Create of the summary table for all markers from the genotype data frame
#'
#' @description This function creates a table with all the markers that were genotyped in the array, the alleles for these markers, the number of homozygous and heterozygous animals, as well as the number of non genotyped animals.
#' @param geno data frame with the genotyping results for your cross
#'
#' @import dplyr
#' @import tidyr
#'
#' @export
#'
#### tab_mark function ####
## create table with markers and counts
tab_mark <- function(geno){
#create geno column in geno df
geno <- geno %>% unite(Geno,c("Allele1...Forward","Allele2...Forward"),sep="",remove=FALSE)
#recode genotypes to have all heterozygous encoded the same way (ex: only "AT", no "TA")
geno <- geno %>% mutate(Geno=recode(Geno,
"TA" = "AT",
"GA" = "AG",
"CA" = "AC",
"GT" = "TG",
"CT" = "TC",
"GC" = "CG"))
#create df with counts for each genotype
df_count <- tibble(SNP.Name = as.character(unique(geno$SNP.Name)),
Allele_1 = NA,
Allele_2 = NA,
n_HM1 = NA,
n_HM2 = NA,
n_HT = NA,
n_NA = NA)
## loop to count genotype
for(i in df_count$SNP.Name){
#extract alleles for each marker
Alleles <- geno %>% filter(SNP.Name==i) %>%
select(c(SNP.Name,Sample.ID,Geno,Allele1...Forward,Allele2...Forward)) %>%
pivot_longer(c(Allele1...Forward,Allele2...Forward),names_to="Allele_name",values_to="Allele") %>%
distinct(Allele) %>% filter(Allele != "-")
Alleles <- as.factor(paste(Alleles$Allele))
#sort alleles
Alleles <- factor(Alleles,levels=c("A","T","C","G"))
Alleles <- sort(Alleles)
#add alleles and counts, only for markers with alleles (not markers with no genotyped ind)
if(all(rapportools::is.empty(Alleles))==FALSE){
#add alleles to df_count
df_count <- df_count %>% mutate(Allele_1 = ifelse(SNP.Name == i,
paste(Alleles[1]), Allele_1))
#count for homozygous for allele 1
n1 <- geno %>% filter(SNP.Name==i) %>%
filter(Geno == paste(Alleles[1],Alleles[1],sep="")) %>%
summarise(n=n())
#add count for homozygous for allele 1 to df_count
df_count <- df_count %>% mutate(n_HM1 = ifelse(SNP.Name == i,
n1$n, n_HM1))
}
#if marker not polymorphic
if(is.na(Alleles[2])==TRUE){
#NA as Allele_2
df_count <- df_count %>% mutate(Allele_2 = ifelse(SNP.Name == i,
NA, Allele_2))
#NA as n_HM2
df_count <- df_count %>% mutate(n_HM2 = ifelse(SNP.Name == i,
NA, n_HM2))
#NA as n_HT
df_count <- df_count %>% mutate(n_HT = ifelse(SNP.Name == i,
NA, n_HT))
} else {
#add alleles to df_count
df_count <- df_count %>% mutate(Allele_2 = ifelse(SNP.Name == i,
paste(Alleles[2]), Allele_2))
#count for homozygous for allele 2
n2 <- geno %>% filter(SNP.Name==i) %>%
filter(Geno == paste(Alleles[2],Alleles[2],sep="")) %>%
summarise(n=n())
#add count for homozygous for allele 1 to df_count
df_count <- df_count %>% mutate(n_HM2 = ifelse(SNP.Name == i,
n2$n, n_HM2))
#count for heterozygous
n3 <- geno %>% filter(SNP.Name==i) %>%
filter(Geno == paste(Alleles[1],Alleles[2],sep="")) %>%
summarise(n=n())
#add count for homozygous for allele 1 to df_count
df_count <- df_count %>% mutate(n_HT = ifelse(SNP.Name == i,
n3$n, n_HT))
}
#count for NA
n4 <- geno %>% filter(SNP.Name==i) %>%
filter(Geno == "--" |
Geno == paste(Alleles[1],"-",sep="") | Geno == paste(Alleles[2],"-",sep="") |
Geno == paste("-",Alleles[1],sep="") | Geno == paste("-",Alleles[2],sep="")) %>%
summarise(n=n())
#add count for NA to df_count
df_count <- df_count %>% mutate(n_NA = ifelse(SNP.Name == i,
n4$n, n_NA))
}
#change class of counts as numeric :
df_count$n_HM1 <- df_count$n_HM1 %>% as.numeric()
df_count$n_HM2 <- df_count$n_HM2 %>% as.numeric()
df_count$n_HT <- df_count$n_HT %>% as.numeric()
df_count$n_NA <- df_count$n_NA %>% as.numeric()
#add 0 for null counts
df_count <- df_count %>% mutate_at(.vars=vars(n_HM1,n_HM2,n_HT,n_NA),~replace(., is.na(.), 0))
#return
return(df_count)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment