在foreach%dopar%loop

时间:2017-03-28 20:59:03

标签: r doparallel parallel-foreach

我有这个代码,我想要并行,但我似乎无法让它工作。 这个想法是,对于每个值chr,snp_sel(geno_data,k,bl)给我一个k列的矩阵,这些列随后逐个写入文件。 我怎么能在这个循环中%dopar%?

foreach(chr=1:length(chrs_raw)) %dopar% 
{
    start = Sys.time() 
    print(start) 

    print(chr) 

    # get .raw file name + path 
    rawfile = paste(RAWfolder, chrs_raw[chr],sep="/") 
    # get .bim file name + path 
    bimfile = paste(RAWfolder, chrs_bim[chr],sep="/") 

    # Read in genetype data in raw format 
    geno_data = fread(rawfile, data.table=FALSE, showProgress = FALSE) 
    # Remove first 7 columns 
    geno_data = as.matrix(geno_data[,c(7:ncol(geno_data))]) 

    # Apply LD subsetting function Lubke et al 2012 
    LDsubset = snp_sel(geno_data,k, bl) 
    rm(geno_data) 

    snp_data = fread(bimfile, data.table=FALSE, showProgress = FALSE) 

    for(subsets in 1:ncol(LDsubset)) 
    { 
            dataout = snp_data[LDsubset[,subsets][LDsubset[,subsets] != 0],2] 
            outfile = paste(gsub(".bim","",basename(chrs_bim[chr])), "S",subsets, sep="") 
            pathout = paste("folderOut/Data/Subsets/",outfile, sep="") 

            write.table(dataout, pathout, col.names=FALSE, row.names=F) 
    } 
    rm(snp_data); rm(LDsubset) 
    stop = Sys.time() 
    print(stop-start) 
}

1 个答案:

答案 0 :(得分:0)

该行:

pathout = paste(folderOut/Data/Subsets/",outfile, sep="") 

缺少开头双引号。它应该是:

pathout = paste("folderOut/Data/Subsets/", outfile, sep="") 

我不确定这是否完全解决了您的问题,但这应该会有所帮助。