R/simulateGenotypesHaploids.R

Defines functions sampleHaploids

Documented in sampleHaploids

#' Simulate recombination events in R - output haplotypes
#'
#' This function simulates genotypes within a pedigree, based on centiMorgan map
#' distances. Outputs genotypes as a GenABEL gwaa object.
#' @param haploid.ids vector of IDs to sample haploid genome from
#' @param haploid.parent parent who produced the haploid offspring, "MOTHER" or "FATHER"
#' @param PLINK.prefix default is a randomly generated name.
#' @import GenABEL
#' @export


sampleHaploids <- function(haploid.ids, haploid.parent, PLINK.prefix){
  x       <- read.table(paste0(PLINK.prefix, ".ped"))
  x.map   <- read.table(paste0(PLINK.prefix, ".map"))
  x.pheno <- read.table(paste0(PLINK.prefix, ".pheno"), header = T)

  change.ids <- which(x.pheno$id %in% haploid.ids)

  if(haploid.parent == "MOTHER"){
    for(i in seq(7, ncol(x), 2)){
      x[change.ids,i+1] <- 0
    }
    x.pheno[change.ids, "sex"] <- 0
  }

  if(haploid.parent == "FATHER"){
    for(i in seq(7, ncol(x), 2)){
      x[change.ids,i] <- 0
    }
    x.pheno[change.ids, "sex"] <- 1
  }

  write.table(x      , paste0(PLINK.prefix, ".haplo.ped")  , row.names = F, col.names = F, quote = F)
  write.table(x.map  , paste0(PLINK.prefix, ".haplo.map")  , row.names = F, col.names = F, quote = F)
  write.table(x.pheno, paste0(PLINK.prefix, ".haplo.pheno"), row.names = F, quote = F)


  convert.snp.ped(pedfile = paste0(PLINK.prefix, ".haplo.ped"),
                  mapfile = paste0(PLINK.prefix, ".haplo.map"),
                  outfile = paste0(PLINK.prefix, ".haplo.genabel"),
                  traits = 1, strand = "u",  mapHasHeaderLine = F, bcast = F)


  genabel.geno <- load.gwaa.data(genofile = paste0(PLINK.prefix, ".haplo.genabel"),
                                 phenofile = paste0(PLINK.prefix, ".haplo.pheno"))

  genabel.geno

}
susjoh/pedantry documentation built on Aug. 23, 2019, 11:35 a.m.