R/send2ascat.R

'send2ascat' <- function(path=NULL, auto=TRUE, ncores=24)
{
	if (is.null(path)) {
		stop("path to facets output not supplied")
	}
	if (!dir.exists(paste0(path, "/resu/"))) {
		stop("directory 'resu' absent")
	}
	if (!dir.exists(paste0(path, "/resu/mad"))) {
		stop("directory 'mad' absent")
	}
	registerDoMC(ncores)
	if (auto) {
		if (!dir.exists(paste0(path, "/resu/ASCAT/"))) {
			.AscatDirectoryStructure(path=path)
		}
	
		sampleNames = gsub(pattern=".RData", replacement="", x=dir(path=paste0(path, "/resu/mad/"), pattern=".RData", full.names=FALSE), fixed=TRUE)
		ASCAT_Params = data.frame(sample_name=sampleNames,
								  rho=rep(NA,length(sampleNames)),
								  psi=rep(NA,length(sampleNames)),
								  ploidy=rep(NA,length(sampleNames)),
								  purity=rep(NA,length(sampleNames)))
		write.table(ASCAT_Params, file=paste0(path, "/resu/ASCAT/ASCAT_Params/ASCAT_Params.csv"), append=FALSE, quote=FALSE, sep=",", row.names=FALSE, col.names=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, "/resu/mad/", sampleNames[i], ".RData", sep=""))
			for (j in 1:nrow(sCN)) {
				index = which(tCN[,"Chromosome"]==sCN[j,"Chromosome"] & tCN[,"Position"]>=sCN[j,"Start"] & tCN[,"Position"]<=sCN[j,"End"] & tCN[,"Genotype"]==0)
				n = length(index)
				sCN[j,"N"] = sCN[j,"N"] - n
			}
			sCN = subset(sCN, sCN[,"N"]>0)
			tCN = subset(tCN, tCN[,"Genotype"]==1)
			Tumor_LogR = as.numeric(tCN[,"Log2Ratio"])
			Tumor_BAF = as.numeric(tCN[,"BAF"])
			Tumor_LogR_segmented = rep(sCN[,"SegmentedLog2Ratio"], times=sCN[,"N"])
			Tumor_BAF_segmented = rep(sCN[,"SegmentedBAF"], times=sCN[,"N"])
			SNPpos = tCN[,c("Chromosome", "Position"), drop=FALSE]
			names(Tumor_LogR) = names(Tumor_BAF) = names(Tumor_LogR_segmented) = names(Tumor_BAF_segmented) = rownames(SNPpos) = paste0("chr", tCN[,"Chromosome"], ":", tCN[,"Position"])
			colnames(SNPpos) = c("chrs", "pos")
			ch = list()
			for (j in 1:23) {
				index = which(tCN[,"Chromosome"]==j)
				ch[[j]] = index
			}
			chr = ch
			chrs = 1:23
			gender = "2323"
			sexchromosomes = c(23, 24)
			tmp = list(Tumor_LogR=Tumor_LogR,
					   Tumor_BAF=Tumor_BAF,
					   Tumor_LogR_segmented=Tumor_LogR_segmented,
					   Tumor_BAF_segmented=Tumor_BAF_segmented,
					   SNPpos=SNPpos,
					   chromosomes=ch,
					   chrnames=chrs,
					   gender=gender,
					   sexchromosomes=sexchromosomes)
			save(tCN, sCN, tmp, file=paste0(path, "/resu/ASCAT/Segmented/", sampleNames[i], ".RData"))
			return(1)
		}
		close(pb)
		if (sum(unlist(res))==length(sampleNames)) {
			cat("\n")
			res = .RunAscatAuto(path=path)
		}
	} else {
		if (!dir.exists(paste0(path, "/resu/ASCAT/"))) {
			stop("ASCAT directory absent")
		}
		cat("\n")
		res = .RunAscatNoAuto(path=path)
	}
	return(invisible(as.logical(unlist(res))))
}
ndbrown6/CNu documentation built on May 27, 2019, 1:09 p.m.