-
Notifications
You must be signed in to change notification settings - Fork 37
/
selectSolution.R
148 lines (132 loc) · 6.08 KB
/
selectSolution.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#' selectSolutions.R
#' author: Gavin Ha
#' Dana-Farber Cancer Institute
#' Broad Institute
#' contact: <[email protected]> or <[email protected]>
#' date: May 14, 2017
library(optparse)
option_list <- list(
make_option(c("--ploidyRun2"), type = "character", help = "Directory containing TitanCNA results for initialized ploidy=2. [Required]"),
make_option(c("--ploidyRun3"), type = "character", default = 0, help = "Directory containing TitanCNA results for initialized ploidy=3. [Required if --ploidyRun4 not given]"),
make_option(c("--ploidyRun4"), type = "character", default = 0, help = "Directory containing TitanCNA results for initialized ploidy=4. [Required if --ploidyRun3 not given]"),
make_option(c("--threshold"), type = "numeric", default=0.05, help = "Proportion ploidyRun2 likelihood greater than than ploidyRun3/4 by at least this value. [Default %default]"),
make_option(c("-o","--outFile"), type = "character", help = "Output file containing a list of solutions chosen for all samples. [Required]")
)
parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)
library(stringr)
library(data.table)
phi2Dir <- opt$ploidyRun2
phi3Dir <- opt$ploidyRun3
phi4Dir <- opt$ploidyRun4
threshold <- opt$threshold
#outDir <- args[5]
outFile <- opt$outFile
#outLink <- args[6]
homd.snp.thres <- 1500
homd.snp.prop <- 0.02
phi2Files <- list.files(phi2Dir, pattern="params.txt", full.names=T)
if (!is.null(phi3Dir) && phi3Dir != "NULL"){
phi3Files <- list.files(phi3Dir, pattern="params.txt", full.names=T)
}else{
phi3Files <- NULL
}
if (!is.null(phi4Dir) && phi4Dir != "NULL"){
phi4Files <- list.files(phi4Dir, pattern="params.txt", full.names=T)
}else{
phi4Files <- NULL
}
if (is.null(phi3Files) && is.null(phi4Files)){
stop("phi3Dir or phi4Dir must be provided.")
}
samples <- gsub(".params.txt", "", basename(phi2Files))
numSamples <- length(samples)
patients <- unique(str_match(samples, "(.+)_cluster")[,2])
numPatients <- length(patients)
########################################################
##### FUNCTION TO FORMAT PARAMS FOR OUTPUT #############
########################################################
formatParams <- function(params){
id <- colnames(params)
barcode <- strsplit(id, "_cluster")[[1]][1]
cellPrev <- strsplit(params[grepl("Clonal cluster cellular prevalence",
rownames(params)), 1], " ")[[1]]
numClust <- length(cellPrev)
cellPrev <- paste0(format(cellPrev, digits=4), collapse=",")
norm <- as.numeric(params[grepl("Normal contamination estimate", rownames(params)), 1])
purity <- 1 - norm
ploidy <- as.numeric(params[grepl("Average tumour ploidy estimate", rownames(params)), 1])
loglik <- as.numeric(params[grepl("likelihood", rownames(params)), 1])
sdbw <- as.numeric(params[grepl("S_Dbw validity index \\(Both\\)", rownames(params)), 1])
return(list(id=id, barcode=barcode, numClust=numClust, cellPrev=cellPrev,
purity=purity, norm=norm, ploidy=ploidy, loglik=loglik, sdbw=sdbw))
}
#save.image()
getParamAllClusters <- function(phiSamples, phiStr = "2"){
phiParams <- NULL#data.frame(id=NA, barcode=NA, numClust=NA,
#cellPrev=NA, purity=NA, norm=NA, ploidy=NA, loglik=NA, sdbw=NA)
for (j in 1:length(phiSamples)){
phi <- read.delim(phiSamples[j], header=F, row.names=1, stringsAsFactors=F, sep="\t")
colnames(phi) <- gsub(".params.txt", "", basename(phiSamples[j]))
param.df <- data.frame(formatParams(phi), stringsAsFactors=F)
phisegs <- fread(gsub(".params.txt", ".segs.txt", phiSamples[j]))
stateDist <- phisegs[, sum(Length.snp.), by = TITAN_call]
# HOMD total proportion greater than homd.snp.prop
if ("HOMD" %in% stateDist$TITAN_call){
ind <- stateDist[TITAN_call == "HOMD", V1] / sum(stateDist[, V1]) > homd.snp.prop
# HOMD seg larger than homd.snp.thres
ind <- ind | sum(phisegs[TITAN_call == "HOMD", Length.snp.] > homd.snp.thres) > 0
if (ind){
param.df[, "loglik"] <- -Inf
param.df[, "sdbw"] <- Inf
}
}
phiParams <- rbind(phiParams, param.df )
}
#phiParams <- na.omit(phiParams)
phiParams <- cbind(Phi=phiStr, phiParams, path = gsub(".params.txt", "", phiSamples))
return(phiParams)
}
########################################################
################### MAIN FUNCTION ######################
########################################################
#optSolution <- matrix(NA, ncol = 9, nrow = numSamples, dimnames = list(c(), c("SampleID_cluster", "SampleID_Barcode", "NumClonalClusters", "CellularPrevalence", "Purity", "NormalContam", "Ploidy", "LogLik", "Selected.Solution")))
#outLink <- paste0(basename(outDir), ".sh")
#fc <- file(outLink, "w+")
optSolutionAll <- NULL
for (i in 1:numPatients){
id <- paste(patients[i], "_cluster[0-9]+\\.params\\.txt$", sep="")
phi2Samples <- grep(id, phi2Files, value=T)
if (length(phi2Samples) > 0){
phi2Params <- getParamAllClusters(phi2Samples, "2")
}
phi3Params <- NULL
phi3Params$loglik <- NA
if (!is.null(phi3Files)){
phi3Samples <- grep(id, phi3Files, value=T)
if (length(phi3Samples) > 0){
phi3Params <- getParamAllClusters(phi3Samples, "3")
}
}
phi4Params <- NULL
phi4Params$loglik <- NA
if (!is.null(phi4Files)){
phi4Samples <- grep(id, phi4Files, value=T)
if (length(phi4Samples) > 0){
phi4Params <- getParamAllClusters(phi4Samples, "4")
}
}
## select ploidy based on which set of ploidy solutions has consistently lower loglik
maxInd <- apply(cbind(phi2Params$loglik + (abs(phi2Params$loglik)*threshold), phi3Params$loglik, phi4Params$loglik), 1, which.max)
ploidySolInd <- as.numeric(names(which.max(table(maxInd))))
optPloidy <- switch(ploidySolInd, phi2Params, phi3Params, phi4Params)
optSolution <- optPloidy[which.min(optPloidy$sdbw), ]
optSolutionAll <- rbind(optSolutionAll, optSolution)
#lnCmd <- paste0("cp -r ", optSolution$path, "* ", outDir, "/")
## apply link command ##
#write.table(lnCmd, file = fc, col.names=F, row.names=F, quote=F, sep="\t", append=T)
}
#outFile <- paste0(basename(outDir), ".txt")
write.table(optSolutionAll, file = outFile, col.names = T, row.names = F, quote = F, sep = "\t")
#close(fc)