jjm
jjm

Reputation: 21

write to file in foreach %dopar% loop

I have this code which I want to make parallel however I can't seem to get it to work. The idea is that for each value chr, snp_sel(geno_data,k, bl) gives me back a matrix of k columns these columns are subsequently written one by one to a file. How can I %dopar% this loop?

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) 
}

Upvotes: 0

Views: 1954

Answers (1)

Jaguar
Jaguar

Reputation: 204

The line:

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

is missing an opening double quote. It should be:

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

I'm not sure if that fixes your problem entirely but that should help.

Upvotes: 1

Related Questions