write to file foreach() not working

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

write to file foreach() not working

This post was updated on .

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 chromosome chr != chrnumber read in due to sorting of data ie chr10 comes before chr2
        # 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)
        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()

Best wishes,