-
Notifications
You must be signed in to change notification settings - Fork 3
/
facets.R
154 lines (129 loc) · 5.75 KB
/
facets.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
147
148
149
150
151
152
153
154
library('ggplot2')
library('facets')
args = commandArgs(trailingOnly=TRUE)
pair_name = args[1]
pileup = args[2]
min_normal_depth = as.numeric(args[3])
cval = as.numeric(args[4])
maxiter = as.numeric(args[5])
seed_initial = as.numeric(args[6])
seed_iterations = as.numeric(args[7])
genome_build = "hg19"
if (seed_iterations <= 0) {
seed_iterations = 1
}
run_facets = function(seed, pileup, min_normal_depth, cval, genome_build, maxiter) {
set.seed(seed)
rcmat = readSnpMatrix(pileup)
xx = preProcSample(rcmat, ndepth = min_normal_depth, cval = cval, gbuild = genome_build)
oo = procSample(xx, cval = cval)
fit = emcncf(oo, maxiter = maxiter)
return(list(seed_oo=oo, seed_fit=fit))
}
plot_facets_iterations = function(pair_name, seeds_dataframe, median_purity, median_ploidy) {
title = paste('Stability of FACETS outputs across ', seed_iterations, ' seeds,\n', pair_name, sep='')
p = ggplot(seeds_dataframe, aes(x=ploidy, y=purity)) + xlim(0, 8) + ylim(0, 1) +
geom_hline(yintercept=median_purity, linetype='dashed', size=1, alpha=0.2) +
geom_vline(xintercept=median_ploidy, linetype='dashed', size=1, alpha=0.2) +
geom_jitter(size = 6, color='#E69F00', alpha=0.5) +
ggtitle(title) +
theme(plot.title = element_text(size=16, hjust=0.5)) +
theme(axis.title.x = element_text(size=16)) +
theme(axis.title.y = element_text(size=16))
return(p)
}
seeds = seed_initial:(seed_initial+seed_iterations-1)
seeds_dataframe = data.frame()
for (seed in seeds) {
seed_list = run_facets(seed, pileup, min_normal_depth, cval, genome_build, maxiter)
seed_oo = seed_list$seed_oo
seed_fit = seed_list$seed_fit
dip_log_r = seed_oo$dipLogR
purity = as.numeric(signif(seed_fit$purity, 3))
ploidy = as.numeric(signif(seed_fit$ploidy, 3))
seed_dataframe = data.frame(seed, purity, ploidy, dip_log_r)
names(seed_dataframe) <- c("seed", "purity", "ploidy", "dip_log_r")
seeds_dataframe = rbind(seeds_dataframe, seed_dataframe)
}
seeds_dataframe_filename = paste(pair_name, '.facets_iterations.txt', sep='')
write.table(seeds_dataframe, seeds_dataframe_filename, sep='\t', quote=FALSE, row.names=FALSE)
idx_na_purity = is.na(seeds_dataframe$purity)
n_iteratons_na_purity = sum(as.numeric(idx_na_purity))
median_purity = median(seeds_dataframe[!idx_na_purity, ]$purity, na.rm=TRUE)
median_ploidy = median(seeds_dataframe[!idx_na_purity, ]$ploidy, na.rm=TRUE)
plot_iterations_filename = paste(pair_name, '.facets_iterations.pdf', sep='')
pdf(plot_iterations_filename, height=10, width=7.5)
plot_facets_iterations(pair_name, seeds_dataframe, median_purity, median_ploidy)
dev.off()
if (!is.na(median_purity)) {
delta_median_purity = abs(seeds_dataframe$purity - median_purity)
min_delta_purity = min(delta_median_purity, na.rm=TRUE)
median_purity_seed = as.numeric(seeds_dataframe[delta_median_purity %in% min_delta_purity,]$seed)
if (length(median_purity_seed) > 1) {median_purity_seed = median_purity_seed[1]}
used_seed = as.numeric(median_purity_seed)
} else {
used_seed = seed_initial
}
facets_list = run_facets(used_seed, pileup, min_normal_depth, cval, genome_build, maxiter)
oo = facets_list$seed_oo
fit = facets_list$seed_fit
purity = as.numeric(signif(fit$purity, 3))
ploidy = as.numeric(signif(fit$ploidy, 3))
log_likelihood = fit$loglik
cncf = fit$cncf
dip_log_r = oo$dipLogR
flags = oo$flags
emflags = fit$emflags
number_segments = NROW(cncf)
number_segments_NA_LCN = sum(is.na(cncf$lcn.em))
genome_segments_filename = paste(pair_name, '.genome_segments.pdf', sep='')
diagnostic_plot_filename = paste(pair_name, '.diagnostic_plot.pdf', sep='')
cncf_dataframe_filename = paste(pair_name, '.facets_cncf.txt', sep='')
summary_dataframe_filename = paste(pair_name, '.facets_output.txt', sep='')
flags_filename = paste(pair_name, '.facets_flags.txt', sep='')
emflags_filename = paste(pair_name, '.facets_emflags.txt', sep='')
if(!is.na(purity)) {
pdf(genome_segments_filename, height=10, width=7.5)
plotSample(x=oo, emfit=fit)
dev.off()
pdf(diagnostic_plot_filename, height=10, width=7.5)
logRlogORspider(oo$out, oo$dipLogR)
dev.off()
} else {
write.table(NA, genome_segments_filename)
write.table(NA, diagnostic_plot_filename)
}
df = data.frame(pair_name=pair_name, purity=purity, ploidy=ploidy, digLogR=dip_log_r)
df_flags = data.frame(flags=flags)
df_emflags = data.frame(emflags=emflags)
write.table(cncf, cncf_dataframe_filename, sep='\t', quote=FALSE, row.names=FALSE)
write.table(df, summary_dataframe_filename, sep='\t', quote=FALSE, row.names=FALSE)
write.table(df_flags, flags_filename, sep='\t', quote=FALSE, row.names=FALSE)
write.table(df_emflags, emflags_filename, sep='\t', quote=FALSE, row.names=FALSE)
write(purity, 'purity.txt')
write(ploidy, 'ploidy.txt')
write(log_likelihood, 'log_likelihood.txt')
write(dip_log_r, 'dip_log_r.txt')
write(used_seed, 'seed_used.txt')
write(n_iteratons_na_purity, 'number_iterations_with_na_purity.txt')
write(number_segments, 'number_segments.txt')
write(number_segments_NA_LCN, 'number_segments_NA_LCN.txt')
# save RData object needed for add_ccf_to_maf_config method, part of Phylogic preprocessing
formatSegmentOutput = function(out, sampID) {
seg = list()
seg$ID = rep(sampID, nrow(out$out))
seg$chrom = out$out$chrom
seg$loc.start = rep(NA, length(seg$ID))
seg$loc.end = seg$loc.start
seg$num.mark = out$out$num.mark
seg$seg.mean = out$out$cnlr.median
for(i in 1:nrow(out$out)) {
lims = range(out$jointseg$maploc[(out$jointseg$chrom == out$out$chrom[i] & out$jointseg$seg == out$out$seg[i])], na.rm=T)
seg$loc.start[i] = lims[1]
seg$loc.end[i] = lims[2]
}
as.data.frame(seg)
}
out = oo
out$IGV <- formatSegmentOutput(out, pair_name)
save(fit, out, file=paste(pair_name, '.RData', sep=''))