R/transcodefiles.R

'transcodefiles' <- function(path=NULL, ncores=24)
{
	if (is.null(path)) {
		stop("path to facets output not supplied")
	}
	if (!dir.exists(paste0(path, "/cncf/"))) {
		stop("directory 'cncf' absent")
	}
	if (!dir.exists(paste0(path, "/resu/"))) {
		dir.create(paste0(path, "/resu/"))
	}
	if (!dir.exists(paste0(path, "/resu/mad"))) {
		dir.create(paste0(path, "/resu/mad"))
	}
	registerDoMC(ncores)
	sampleNames = gsub(pattern=".Rdata", replacement="", x=dir(path=paste0(path, "/cncf/"), pattern=".Rdata", full.names=FALSE), fixed=TRUE)
	pb = txtProgressBar(min=1, max=length(sampleNames), style=3)
	res = foreach (i=1:length(sampleNames)) %dopar% {
		if (length(sampleNames)<=ncores) {
			if (i==length(sampleNames)) {
				setTxtProgressBar(pb, i)
			}
		} else {
			if ((i %% ncores)==0) {
				setTxtProgressBar(pb, i)
			}
		}
		load(paste0(path, "/cncf/", sampleNames[i], ".Rdata"))
		tCN = out2$jointseg[,c("chrom", "maploc", "cnlr", "vafT", "het"),drop=FALSE]
		colnames(tCN) = c("Chromosome", "Position", "Log2Ratio", "BAF", "Genotype")
		sCN = fit$cncf[,c("chrom", "start", "end", "num.mark", "cnlr.median.clust", "mafR.clust"),drop=FALSE]
		for (j in 1:length(unique(fit$cncf[,"segclust"]))) {
			index = which(out2$jointseg[,"segclust"]==unique(fit$cncf[,"segclust"])[j] & out2$jointseg[,"het"]==1)
			if (length(index)!=0) {
				tmp = out2$jointseg[index,"vafT"]
				tmp[tmp<.5] = 1-tmp[tmp<.5]
				sCN[fit$cncf[,"segclust"]==unique(fit$cncf[,"segclust"])[j],"mafR.clust"] = median(tmp, na.rm=TRUE)
			} else {
				sCN[fit$cncf[,"segclust"]==unique(fit$cncf[,"segclust"])[j],"mafR.clust"] = NA
			}
		}
		colnames(sCN) = c("Chromosome", "Start", "End", "N", "SegmentedLog2Ratio", "SegmentedBAF")
		save(tCN, sCN, file=paste0(path, "/resu/mad/", sampleNames[i], ".RData"))
		return(1)
	}
	close(pb)
	if (sum(unlist(res))==length(sampleNames)) {
		cat("\n")
	}
	return(invisible(as.logical(unlist(res))))
}
ndbrown6/CNu documentation built on May 27, 2019, 1:09 p.m.