write to file foreach() not working

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
jjm
Reply | Threaded
Open this post in threaded view
|

write to file foreach() not working

jjm
This post was updated on .
Hi,

I have this code which I want to make parallel however I can't seem to get it to work.
I get the impression that dopar has problems with writing something to a file is this true? and if so what do I need to change?

foreach(chr=1:length(chrs_raw)) %dopar%
{
        start = Sys.time()
        print(start)
        # print chromosome chr != chrnumber read in due to sorting of data ie chr10 comes before chr2
        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 = cbind(snp_data[LDsubset[,subsets][LDsubset[,subsets] != 0],2])
                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)
}

Best wishes,
J