R/map.SNPs.to.plinkfiles.R

Defines functions map.SNPs.to.plinkfiles

map.SNPs.to.plinkfiles <- function(plink.files, pathway){
  
  bim <- plink.files$bim
  cc <- c('NULL', 'character', 'NULL', 'NULL', 'NULL', 'NULL')
  
  nf <- length(bim)
  SNP <- NULL
  iplink <- NULL
  for(i in 1:nf){
    re <- try(b <- load.file(bim[i], header = FALSE, select = cc)[, 1], silent = TRUE)
    if("try-error" %in% class(re)){
      msg <- paste0('Cannot load ', b)
      stop(msg)
    }
    
    b <- intersect(b, pathway$SNP)
    if(length(b) == 0){
      next
    }
    SNP <- c(SNP, b)
    iplink <- c(iplink, rep(i, length(b)))
    
  }
  
  if(length(SNP) == 0){
    msg <- 'No SNP is available in geno.files'
    stop(msg)
  }
  
  sf <- data.frame(SNP, iplink, stringsAsFactors = FALSE)
  sf <- sf[!duplicated(sf$SNP), ]
  rownames(sf) <- NULL
  
  sf
  
}
zhangh12/ARTP3 documentation built on Aug. 16, 2019, 7:39 p.m.