标签: Flapjack

  • HMP转Flapjack基因型和图谱文件

    【功能】

    将HMP文件转换为Flapjack的genotype和map文件。

    【测试】

    Null

    【更新历史】

    2020年2月23日

    • 自动完成转换,并在输入文件相同目录下生成【flapjackGeno.txt】文件和【flapjackmap.txt】。

    【使用方法】

    在Rstudio中运行下列代码,选择要转换的hmp文件。


    # Hmp converted to flapjack genotype file for import
    rm(list=ls())
    
    ### function section ###
    # the function of Read files
    readFiles <- function(header=TRUE,choose=FALSE,fname){
      if(!"readr" %in% installed.packages()) {
        install.packages("readr")
        library(readr)
      }else{
        library(readr)
      }
      if (choose==TRUE){
        myFileName <- file.choose()
      }else{
        myFileName <- fname
      }
      if (grepl("\\.csv$",myFileName)){
        if (header==TRUE){
          myFile <- read_csv(myFileName, col_names = TRUE)
        }else{
          myFile <- read_csv(myFileName, col_names = FALSE)
        }
      }else{
        if (header==TRUE){
          myFile <- read_tsv(myFileName, col_names = TRUE)
        }else{
          myFile <- read_tsv(myFileName, col_names = FALSE)
        }
      }
      return(myFile)
    }
    
    # the function of replace the letters of hybrids
    repHybrids <- function(myVector){
      myVector <- sub("R","A/G",myVector)
      myVector <- sub("Y","C/T",myVector)
      myVector <- sub("S","C/G",myVector)
      myVector <- sub("W","A/T",myVector)
      myVector <- sub("K","G/T",myVector)
      myVector <- sub("M","A/C",myVector)
      myVector <- sub("N","-",myVector)
      return(myVector)
    }
    ### function section end ###
    
    ### read file ###  
    # choose a file of HMP
    fn <- choose.files()
    
    # set workspace
    setwd(dirname(fn))
    
    # read a file of HMP
    hmpFile <- readFiles(header=T,fname(basename(fn)))
    
    ### read file end ###
    
    ### genotype ###
    # remove useless columns
    myHmpFile <- as.matrix(hmpFile[,c(1,12:ncol(hmpFile))])
    
    # replace the hybrids
    system.time(newHmpFile <- apply(myHmpFile,2,repHybrids))
    
    # check consistancy
    all(myHmpFile[,1] == newHmpFile[,1])
    all(colnames(myHmpFile) == colnames(newHmpFile))
    
    # Transposing
    resultImprotFile <- t(newHmpFile)
    resultImprotFile <- cbind(rownames(resultImprotFile),resultImprotFile)
    resultImprotFile[,1] <- sub("rs#","",rownames(resultImprotFile))
    resultImprotFile <- rbind("",resultImprotFile)
    resultImprotFile[1,1] <- "# fjFile = GENOTYPE"
    
    # output file
    write.table(resultImprotFile,"flapjackGeno.txt",quote = FALSE,sep="\t",row.names=FALSE,col.names = FALSE)
    ### genotype end ###
    
    ### map ###
    # map file
    myMapFile <- as.matrix(hmpFile[,c(1,3,4)])
    myMapFile[,3] <- as.numeric(myMapFile[,3])
    myMapFile <- rbind("",myMapFile)
    myMapFile[1,1] <- "# fjFile = MAP"
    # output file
    write.table(myMapFile,"flapjackMap.txt",quote = FALSE,sep="\t",row.names=FALSE,col.names = FALSE)
    ### map end ###