R/send2gap.R

'send2gap' <- 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/GAP/"))) {
			.GapDirectoryStructure(path=path)
		}
		
		sampleNames = gsub(pattern=".RData", replacement="", x=dir(path=paste0(path, "/resu/mad/"), pattern=".RData", full.names=FALSE), fixed=TRUE)
		GAP_Params = data.frame(sample_name=sampleNames,
								p_BAF=rep(NA, length(sampleNames)),
								q_LRR=rep(NA, length(sampleNames)),
								X2copy_LRR=rep(NA, length(sampleNames)),
								Chromosome_Count=rep(NA, length(sampleNames)),
								Centromere_Count=rep(NA, length(sampleNames)),
								DNA_Index=rep(NA, length(sampleNames)),
								Ploidy=rep(NA, length(sampleNames)))
		write.table(GAP_Params, file=paste0(path, "/resu/GAP/GAP_Params/GAP_Params.csv"), append=FALSE, quote=FALSE, sep=",", row.names=FALSE, col.names=TRUE)
		data(CytoBand)
		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=""))
			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"])
			cen_pos = NULL
			for (j in 1:23) {
				cen_pos = c(cen_pos, max(CytoBand[CytoBand[,1]==j & CytoBand[,2]=="p",4]))
			}
			Index = 1:nrow(tCN)
			Chr_N = tCN[,"Chromosome"]
			Chr_arm = vector(mode="numeric", length=nrow(tCN))
			for (j in 1:23) {
				index = which(tCN[,"Chromosome"]==j & tCN[,"Position"]<=cen_pos[j])
				Chr_arm[index] = 1
				index = which(tCN[,"Chromosome"]==j & tCN[,"Position"]>cen_pos[j])
				Chr_arm[index] = 2
			}
			SNP_annot = data.frame(Index=Index, Chr_N=Chr_N, Chr_arm=Chr_arm, Position=tCN[,"Position"], row.names=paste0("chr", tCN[,"Chromosome"], ":", tCN[,"Position"]))
			save(Tumor_LogR_segmented, Tumor_BAF_segmented, SNP_annot, file=paste0(path, "/resu/GAP/Segmented/", sampleNames[i], ".RData"))
			save(Tumor_LogR, Tumor_BAF, SNP_annot, file=paste0(path, "/resu/GAP/Raw/", sampleNames[i], ".RData"))
			return(1)
		}
		close(pb)
		if (sum(unlist(res))==length(sampleNames)) {
			cat("\n")
			res = .RunGapAuto(path=path)
		}
	} else {
		if (!dir.exists(paste0(path, "/resu/GAP/"))) {
			stop("GAP directory absent")
		}
		cat("\n")
		res = .RunGapNoAuto(path=path)
	}
	return(invisible(as.logical(unlist(res))))
}
ndbrown6/CNu documentation built on May 27, 2019, 1:09 p.m.