... | ... |
@@ -3,7 +3,9 @@ CHANGES IN VERSION 1.5.1 |
3 | 3 |
|
4 | 4 |
SIGNIFICANT USER-LEVEL CHANGES |
5 | 5 |
|
6 |
- o Column 'peakScores' is now bounded between 0 and 1000 and approximately uniformly distributed. |
|
6 |
+ o New column 'maxPostInPeak' containing the maximum posterior within each peak. |
|
7 |
+ |
|
8 |
+ o Score in exported BED files is calculated as -10*log10(maxPostInPeak). |
|
7 | 9 |
|
8 | 10 |
o 'changeFDR()' was renamed to 'adjustSensitivity()'. |
9 | 11 |
|
... | ... |
@@ -75,7 +75,11 @@ adjustSensitivity.multivariate <- function(model, sensitivity, invert=FALSE) { |
75 | 75 |
if (is(model, class.multivariate.hmm)) { |
76 | 76 |
## Calculate states |
77 | 77 |
ptm <- startTimedMessage("Calculating states from maximum-posterior in each peak ...") |
78 |
- p <- getMaxPostInPeaks(model$bins$state, model$bins$posteriors) |
|
78 |
+ if (is.null(model$bins$maxPostInPeak)) { |
|
79 |
+ p <- getMaxPostInPeaks(model$bins$state, model$bins$posteriors) |
|
80 |
+ } else { |
|
81 |
+ p <- model$bins$maxPostInPeak |
|
82 |
+ } |
|
79 | 83 |
p.thresholded <- matrix(FALSE, ncol=ncol(p), nrow=nrow(p)) |
80 | 84 |
for (icol in 1:ncol(p)) { |
81 | 85 |
if (!invert) { |
... | ... |
@@ -92,25 +96,23 @@ adjustSensitivity.multivariate <- function(model, sensitivity, invert=FALSE) { |
92 | 96 |
model$bins$combination <- factor(mapping[as.character(model$bins$state)], levels=mapping[as.character(levels(model$bins$state))]) |
93 | 97 |
} |
94 | 98 |
stopTimedMessage(ptm) |
95 |
- ## Redo peakScores |
|
96 |
- ptm <- startTimedMessage("Re-estimating peakScores ...") |
|
97 |
- p <- getMaxPostInPeaks(model$bins$state, model$bins$posteriors) |
|
98 |
- model$bins$peakScores <- calculatePeakScores(p) |
|
99 |
- rm(p) |
|
99 |
+ ## Redo maxPostInPeak |
|
100 |
+ ptm <- startTimedMessage("Re-estimating maximum posterior in peaks ...") |
|
101 |
+ model$bins$maxPostInPeak <- getMaxPostInPeaks(model$bins$state, model$bins$posteriors) |
|
100 | 102 |
stopTimedMessage(ptm) |
101 | 103 |
## Redo segmentation |
102 | 104 |
model$segments <- multivariateSegmentation(model$bins, column2collapseBy='state') |
103 | 105 |
## Redo peaks |
104 | 106 |
ptm <- startTimedMessage("Recalculating peaks ...") |
105 | 107 |
model$peaks <- list() |
106 |
- for (i1 in 1:ncol(model$segments$peakScores)) { |
|
107 |
- mask <- model$segments$peakScores[,i1] > 0 |
|
108 |
+ for (i1 in 1:ncol(model$segments$maxPostInPeak)) { |
|
109 |
+ mask <- model$segments$maxPostInPeak[,i1] > 0 |
|
108 | 110 |
peaks <- model$segments[mask] |
109 | 111 |
mcols(peaks) <- NULL |
110 |
- peaks$peakScores <- model$segments$peakScores[mask,i1] |
|
112 |
+ peaks$maxPostInPeak <- model$segments$maxPostInPeak[mask,i1] |
|
111 | 113 |
model$peaks[[i1]] <- peaks |
112 | 114 |
} |
113 |
- names(model$peaks) <- colnames(model$segments$peakScores) |
|
115 |
+ names(model$peaks) <- colnames(model$segments$maxPostInPeak) |
|
114 | 116 |
stopTimedMessage(ptm) |
115 | 117 |
|
116 | 118 |
} else if (is(model, class.combined.multivariate.hmm)) { |
... | ... |
@@ -118,7 +120,11 @@ adjustSensitivity.multivariate <- function(model, sensitivity, invert=FALSE) { |
118 | 120 |
mapping.df <- stateBrewer(model$info[,setdiff(names(model$info), 'ID')], mode='full') |
119 | 121 |
mapping <- mapping.df$combination |
120 | 122 |
names(mapping) <- mapping.df$state |
121 |
- p <- getMaxPostInPeaks(model$bins$state, model$bins$posteriors) |
|
123 |
+ if (is.null(model$bins$maxPostInPeak)) { |
|
124 |
+ p <- getMaxPostInPeaks(model$bins$state, model$bins$posteriors) |
|
125 |
+ } else { |
|
126 |
+ p <- model$bins$maxPostInPeak |
|
127 |
+ } |
|
122 | 128 |
p.thresholded <- matrix(FALSE, ncol=ncol(p), nrow=nrow(p)) |
123 | 129 |
for (icol in 1:ncol(p)) { |
124 | 130 |
if (!invert) { |
... | ... |
@@ -137,11 +143,9 @@ adjustSensitivity.multivariate <- function(model, sensitivity, invert=FALSE) { |
137 | 143 |
multiHMM$bins$combination <- mapping[as.character(states)] |
138 | 144 |
multiHMM$bins$state <- factor(states) |
139 | 145 |
multiHMM$bins$posteriors <- model$bins$posteriors |
140 |
- ## Redo peakScores |
|
141 |
- ptm <- startTimedMessage("Re-estimating peakScores ...") |
|
142 |
- p <- getMaxPostInPeaks(multiHMM$bins$state, multiHMM$bins$posteriors) |
|
143 |
- multiHMM$bins$peakScores <- calculatePeakScores(p) |
|
144 |
- rm(p) |
|
146 |
+ ## Redo maxPostInPeak |
|
147 |
+ ptm <- startTimedMessage("Re-estimating maximum posterior in peaks ...") |
|
148 |
+ multiHMM$bins$maxPostInPeak <- getMaxPostInPeaks(multiHMM$bins$state, multiHMM$bins$posteriors) |
|
145 | 149 |
stopTimedMessage(ptm) |
146 | 150 |
multiHMM$mapping <- mapping |
147 | 151 |
multiHMM$peaks <- model$peaks |
... | ... |
@@ -149,14 +153,14 @@ adjustSensitivity.multivariate <- function(model, sensitivity, invert=FALSE) { |
149 | 153 |
## Redo peaks |
150 | 154 |
ptm <- startTimedMessage("Recalculating peaks ...") |
151 | 155 |
model$peaks <- list() |
152 |
- for (i1 in 1:ncol(model$segments$peakScores)) { |
|
153 |
- mask <- model$segments$peakScores[,i1] > 0 |
|
156 |
+ for (i1 in 1:ncol(model$segments$maxPostInPeak)) { |
|
157 |
+ mask <- model$segments$maxPostInPeak[,i1] > 0 |
|
154 | 158 |
peaks <- model$segments[mask] |
155 | 159 |
mcols(peaks) <- NULL |
156 |
- peaks$peakScores <- model$segments$peakScores[mask,i1] |
|
160 |
+ peaks$maxPostInPeak <- model$segments$maxPostInPeak[mask,i1] |
|
157 | 161 |
model$peaks[[i1]] <- peaks |
158 | 162 |
} |
159 |
- names(model$peaks) <- colnames(model$segments$peakScores) |
|
163 |
+ names(model$peaks) <- colnames(model$segments$maxPostInPeak) |
|
160 | 164 |
stopTimedMessage(ptm) |
161 | 165 |
} else { |
162 | 166 |
stop("Supply either a uniHMM, multiHMM or combinedMultiHMM object.") |
... | ... |
@@ -187,7 +191,9 @@ adjustSensitivity.univariate <- function(model, sensitivity, invert=FALSE) { |
187 | 191 |
if (is.null(model$bins$posterior.modified)) stop("Cannot recalculate states because column 'posterior.modified' is missing.") |
188 | 192 |
## Calculate states |
189 | 193 |
ptm <- startTimedMessage("Calculating states from maximum-posterior in each peak ...") |
190 |
- model$bins$maxPostInPeak <- getMaxPostInPeaks.univariate(model$bins$state, model$bins$posterior.modified) |
|
194 |
+ if (is.null(model$bins$maxPostInPeak)) { |
|
195 |
+ model$bins$maxPostInPeak <- getMaxPostInPeaks.univariate(model$bins$state, model$bins$posterior.modified) |
|
196 |
+ } |
|
191 | 197 |
states <- model$bins$state |
192 | 198 |
states[model$bins$state == 'modified'] <- 'unmodified' |
193 | 199 |
if (!invert) { |
... | ... |
@@ -198,17 +204,15 @@ adjustSensitivity.univariate <- function(model, sensitivity, invert=FALSE) { |
198 | 204 |
model$bins$state <- states |
199 | 205 |
model$bins$maxPostInPeak <- NULL |
200 | 206 |
stopTimedMessage(ptm) |
201 |
- ## Redo peakScores |
|
202 |
- ptm <- startTimedMessage("Re-estimating peakScores ...") |
|
207 |
+ ## Redo maxPostInPeak |
|
208 |
+ ptm <- startTimedMessage("Re-estimating maximum posterior in peaks ...") |
|
203 | 209 |
model$bins$maxPostInPeak <- getMaxPostInPeaks.univariate(model$bins$state, model$bins$posterior.modified) |
204 |
- model$bins$peakScores <- calculatePeakScores.univariate(model$bins$maxPostInPeak) |
|
205 |
- model$bins$maxPostInPeak <- NULL |
|
206 | 210 |
stopTimedMessage(ptm) |
207 | 211 |
## Redo segmentation |
208 | 212 |
ptm <- startTimedMessage("Making segmentation ...") |
209 | 213 |
gr <- model$bins |
210 | 214 |
df <- as.data.frame(gr) |
211 |
- red.df <- suppressMessages(collapseBins(df, column2collapseBy='state', columns2average=c('score'), columns2drop=c('width',grep('posteriors', names(df), value=TRUE), 'counts', 'counts.rpkm', 'posterior.modified'))) |
|
215 |
+ red.df <- suppressMessages(collapseBins(df, column2collapseBy='state', columns2drop=c('width',grep('posteriors', names(df), value=TRUE), 'counts', 'counts.rpkm', 'posterior.modified'))) |
|
212 | 216 |
model$segments <- methods::as(red.df, 'GRanges') |
213 | 217 |
seqlengths(model$segments) <- seqlengths(model$bins)[seqlevels(model$segments)] |
214 | 218 |
## Redo peaks |
... | ... |
@@ -317,25 +321,23 @@ changePostCutoff.multivariate <- function(model, post.cutoff) { |
317 | 321 |
model$bins$combination <- factor(mapping[as.character(model$bins$state)], levels=mapping[as.character(levels(model$bins$state))]) |
318 | 322 |
} |
319 | 323 |
stopTimedMessage(ptm) |
320 |
- ## Redo peakScores |
|
321 |
- ptm <- startTimedMessage("Re-estimating peakScores ...") |
|
322 |
- p <- getMaxPostInPeaks(model$bins$state, model$bins$posteriors) |
|
323 |
- model$bins$peakScores <- calculatePeakScores(p) |
|
324 |
- rm(p) |
|
324 |
+ ## Redo maxPostInPeak |
|
325 |
+ ptm <- startTimedMessage("Re-estimating maximum posterior in peaks ...") |
|
326 |
+ model$bins$maxPostInPeak <- getMaxPostInPeaks(model$bins$state, model$bins$posteriors) |
|
325 | 327 |
stopTimedMessage(ptm) |
326 | 328 |
## Redo segmentation |
327 | 329 |
model$segments <- multivariateSegmentation(model$bins, column2collapseBy='state') |
328 | 330 |
## Redo peaks |
329 | 331 |
ptm <- startTimedMessage("Recalculating peaks ...") |
330 | 332 |
model$peaks <- list() |
331 |
- for (i1 in 1:ncol(model$segments$peakScores)) { |
|
332 |
- mask <- model$segments$peakScores[,i1] > 0 |
|
333 |
+ for (i1 in 1:ncol(model$segments$maxPostInPeak)) { |
|
334 |
+ mask <- model$segments$maxPostInPeak[,i1] > 0 |
|
333 | 335 |
peaks <- model$segments[mask] |
334 | 336 |
mcols(peaks) <- NULL |
335 |
- peaks$peakScores <- model$segments$peakScores[mask,i1] |
|
337 |
+ peaks$maxPostInPeak <- model$segments$maxPostInPeak[mask,i1] |
|
336 | 338 |
model$peaks[[i1]] <- peaks |
337 | 339 |
} |
338 |
- names(model$peaks) <- colnames(model$segments$peakScores) |
|
340 |
+ names(model$peaks) <- colnames(model$segments$maxPostInPeak) |
|
339 | 341 |
stopTimedMessage(ptm) |
340 | 342 |
} else if (is(model, class.combined.multivariate.hmm)) { |
341 | 343 |
ptm <- startTimedMessage("Calculating states from posteriors ...") |
... | ... |
@@ -357,11 +359,9 @@ changePostCutoff.multivariate <- function(model, post.cutoff) { |
357 | 359 |
multiHMM$bins$combination <- mapping[as.character(states)] |
358 | 360 |
multiHMM$bins$state <- factor(states) |
359 | 361 |
multiHMM$bins$posteriors <- post |
360 |
- ## Redo peakScores |
|
361 |
- ptm <- startTimedMessage("Re-estimating peakScores ...") |
|
362 |
- p <- getMaxPostInPeaks(multiHMM$bins$state, multiHMM$bins$posteriors) |
|
363 |
- multiHMM$bins$peakScores <- calculatePeakScores(p) |
|
364 |
- rm(p) |
|
362 |
+ ## Redo maxPostInPeak |
|
363 |
+ ptm <- startTimedMessage("Re-estimating maximum posterior in peaks ...") |
|
364 |
+ multiHMM$bins$maxPostInPeak <- getMaxPostInPeaks(multiHMM$bins$state, multiHMM$bins$posteriors) |
|
365 | 365 |
stopTimedMessage(ptm) |
366 | 366 |
multiHMM$mapping <- mapping |
367 | 367 |
multiHMM$peaks <- model$peaks |
... | ... |
@@ -369,14 +369,14 @@ changePostCutoff.multivariate <- function(model, post.cutoff) { |
369 | 369 |
## Redo peaks |
370 | 370 |
ptm <- startTimedMessage("Recalculating peaks ...") |
371 | 371 |
model$peaks <- list() |
372 |
- for (i1 in 1:ncol(model$segments$peakScores)) { |
|
373 |
- mask <- model$segments$peakScores[,i1] > 0 |
|
372 |
+ for (i1 in 1:ncol(model$segments$maxPostInPeak)) { |
|
373 |
+ mask <- model$segments$maxPostInPeak[,i1] > 0 |
|
374 | 374 |
peaks <- model$segments[mask] |
375 | 375 |
mcols(peaks) <- NULL |
376 |
- peaks$peakScores <- model$segments$peakScores[mask,i1] |
|
376 |
+ peaks$maxPostInPeak <- model$segments$maxPostInPeak[mask,i1] |
|
377 | 377 |
model$peaks[[i1]] <- peaks |
378 | 378 |
} |
379 |
- names(model$peaks) <- colnames(model$segments$peakScores) |
|
379 |
+ names(model$peaks) <- colnames(model$segments$maxPostInPeak) |
|
380 | 380 |
stopTimedMessage(ptm) |
381 | 381 |
} else { |
382 | 382 |
stop("Supply either a uniHMM, multiHMM or combinedMultiHMM object.") |
... | ... |
@@ -416,11 +416,9 @@ changePostCutoff.univariate <- function(model, post.cutoff) { |
416 | 416 |
states <- state.labels[states] |
417 | 417 |
model$bins$state <- states |
418 | 418 |
stopTimedMessage(ptm) |
419 |
- ## Redo peakScores |
|
420 |
- ptm <- startTimedMessage("Re-estimating peakScores ...") |
|
419 |
+ ## Redo maxPostInPeak |
|
420 |
+ ptm <- startTimedMessage("Re-estimating maximum posterior in peaks ...") |
|
421 | 421 |
model$bins$maxPostInPeak <- getMaxPostInPeaks.univariate(model$bins$state, model$bins$posterior.modified) |
422 |
- model$bins$peakScores <- calculatePeakScores.univariate(model$bins$maxPostInPeak) |
|
423 |
- model$bins$maxPostInPeak <- NULL |
|
424 | 422 |
stopTimedMessage(ptm) |
425 | 423 |
## Redo segmentation |
426 | 424 |
ptm <- startTimedMessage("Making segmentation ...") |
... | ... |
@@ -498,11 +498,9 @@ runMultivariate <- function(binned.data, stepbins, info, comb.states, use.states |
498 | 498 |
stopTimedMessage(ptm) |
499 | 499 |
|
500 | 500 |
if (get.posteriors) { |
501 |
- ptm <- startTimedMessage("Calculating peak scores ...") |
|
501 |
+ ptm <- startTimedMessage("Getting maximum posterior in peaks ...") |
|
502 | 502 |
result$bins$maxPostInPeak <- getMaxPostInPeaks(result$bins$state, result$bins$posteriors) |
503 |
- result$bins$peakScores <- calculatePeakScores(result$bins$maxPostInPeak) |
|
504 |
- result$bins$maxPostInPeak <- NULL |
|
505 |
- result$bins$differential.score <- differentialScoreSum(result$bins$peakScores, result$info) |
|
503 |
+ result$bins$differential.score <- differentialScoreSum(result$bins$maxPostInPeak, result$info) |
|
506 | 504 |
stopTimedMessage(ptm) |
507 | 505 |
} |
508 | 506 |
if (!keep.posteriors) { |
... | ... |
@@ -527,14 +525,14 @@ runMultivariate <- function(binned.data, stepbins, info, comb.states, use.states |
527 | 525 |
## Peaks ## |
528 | 526 |
ptm <- startTimedMessage("Obtaining peaks ...") |
529 | 527 |
result$peaks <- list() |
530 |
- for (i1 in 1:ncol(result$segments$peakScores)) { |
|
531 |
- mask <- result$segments$peakScores[,i1] > 0 |
|
528 |
+ for (i1 in 1:ncol(result$segments$maxPostInPeak)) { |
|
529 |
+ mask <- result$segments$maxPostInPeak[,i1] > 0 |
|
532 | 530 |
peaks <- result$segments[mask] |
533 | 531 |
mcols(peaks) <- NULL |
534 |
- peaks$peakScores <- result$segments$peakScores[mask,i1] |
|
532 |
+ peaks$maxPostInPeak <- result$segments$maxPostInPeak[mask,i1] |
|
535 | 533 |
result$peaks[[i1]] <- peaks |
536 | 534 |
} |
537 |
- names(result$peaks) <- colnames(result$segments$peakScores) |
|
535 |
+ names(result$peaks) <- colnames(result$segments$maxPostInPeak) |
|
538 | 536 |
stopTimedMessage(ptm) |
539 | 537 |
|
540 | 538 |
return(result) |
... | ... |
@@ -560,8 +560,6 @@ callPeaksUnivariateAllChr <- function(binned.data, input.data=NULL, eps=0.01, in |
560 | 560 |
|
561 | 561 |
## Peak score as maximum posterior in that peak ## |
562 | 562 |
result$bins$maxPostInPeak <- getMaxPostInPeaks.univariate(result$bins$state, result$bins$posterior.modified) |
563 |
- result$bins$peakScores <- calculatePeakScores.univariate(result$bins$maxPostInPeak) |
|
564 |
- result$bins$maxPostInPeak <- NULL |
|
565 | 563 |
|
566 | 564 |
## Segmentation ## |
567 | 565 |
ptm <- startTimedMessage("Making segmentation ...") |
... | ... |
@@ -92,8 +92,8 @@ combineMultivariates <- function(hmms, mode) { |
92 | 92 |
counts[[1]] <- hmm$bins$counts.rpkm |
93 | 93 |
posteriors <- list() |
94 | 94 |
posteriors[[1]] <- hmm$bins$posteriors |
95 |
- peakScores <- list() |
|
96 |
- peakScores[[1]] <- hmm$bins$peakScores |
|
95 |
+ maxPostInPeak <- list() |
|
96 |
+ maxPostInPeak[[1]] <- hmm$bins$maxPostInPeak |
|
97 | 97 |
peaks <- list() |
98 | 98 |
peaks[[1]] <- hmm$peaks |
99 | 99 |
binstates <- list() |
... | ... |
@@ -108,7 +108,7 @@ combineMultivariates <- function(hmms, mode) { |
108 | 108 |
combs[[i1]] <- hmm$bins$combination |
109 | 109 |
counts[[i1]] <- hmm$bins$counts.rpkm |
110 | 110 |
posteriors[[i1]] <- hmm$bins$posteriors |
111 |
- peakScores[[i1]] <- hmm$bins$peakScores |
|
111 |
+ maxPostInPeak[[i1]] <- hmm$bins$maxPostInPeak |
|
112 | 112 |
peaks[[i1]] <- hmm$peaks |
113 | 113 |
binstates[[i1]] <- dec2bin(hmm$bins$state, colnames=hmm$info$ID) |
114 | 114 |
stopTimedMessage(ptm) |
... | ... |
@@ -116,7 +116,7 @@ combineMultivariates <- function(hmms, mode) { |
116 | 116 |
} |
117 | 117 |
ptm <- startTimedMessage("Concatenating conditions ...") |
118 | 118 |
posteriors <- do.call(cbind, posteriors) |
119 |
- peakScores <- do.call(cbind, peakScores) |
|
119 |
+ maxPostInPeak <- do.call(cbind, maxPostInPeak) |
|
120 | 120 |
infos <- do.call(rbind, infos) |
121 | 121 |
conditions <- unique(infos$condition) |
122 | 122 |
states <- factor(bin2dec(do.call(cbind, binstates))) |
... | ... |
@@ -131,7 +131,7 @@ combineMultivariates <- function(hmms, mode) { |
131 | 131 |
infos <- list() |
132 | 132 |
counts <- list() |
133 | 133 |
posteriors <- list() |
134 |
- peakScores <- list() |
|
134 |
+ maxPostInPeak <- list() |
|
135 | 135 |
peaks <- list() |
136 | 136 |
binstates <- list() |
137 | 137 |
for (i1 in 1:length(hmms)) { |
... | ... |
@@ -140,7 +140,7 @@ combineMultivariates <- function(hmms, mode) { |
140 | 140 |
infos[[i1]] <- hmm$info |
141 | 141 |
counts[[i1]] <- hmm$bins$counts.rpkm |
142 | 142 |
posteriors[[i1]] <- hmm$bins$posteriors |
143 |
- peakScores[[i1]] <- hmm$bins$peakScores |
|
143 |
+ maxPostInPeak[[i1]] <- hmm$bins$maxPostInPeak |
|
144 | 144 |
peaks[[i1]] <- hmm$peaks |
145 | 145 |
binstates[[i1]] <- dec2bin(hmm$bins$state, colnames=hmm$info$ID) |
146 | 146 |
stopTimedMessage(ptm) |
... | ... |
@@ -158,8 +158,8 @@ combineMultivariates <- function(hmms, mode) { |
158 | 158 |
counts <- counts[,infos$ID] |
159 | 159 |
posteriors <- do.call(cbind, posteriors) |
160 | 160 |
posteriors <- posteriors[,infos$ID] |
161 |
- peakScores <- do.call(cbind, peakScores) |
|
162 |
- peakScores <- peakScores[,infos$ID] |
|
161 |
+ maxPostInPeak <- do.call(cbind, maxPostInPeak) |
|
162 |
+ maxPostInPeak <- maxPostInPeak[,infos$ID] |
|
163 | 163 |
binstates <- do.call(cbind, binstates) |
164 | 164 |
binstates <- binstates[,infos$ID] |
165 | 165 |
states <- factor(bin2dec(binstates)) |
... | ... |
@@ -199,7 +199,7 @@ combineMultivariates <- function(hmms, mode) { |
199 | 199 |
conditions <- unique(infos$condition) |
200 | 200 |
counts <- hmm$bins$counts.rpkm |
201 | 201 |
posteriors <- hmm$bins$posteriors |
202 |
- peakScores <- hmm$bins$peakScores |
|
202 |
+ maxPostInPeak <- hmm$bins$maxPostInPeak |
|
203 | 203 |
peaks <- hmm$peaks |
204 | 204 |
states <- hmm$bins$state |
205 | 205 |
combs <- list() |
... | ... |
@@ -227,8 +227,8 @@ combineMultivariates <- function(hmms, mode) { |
227 | 227 |
counts[[1]] <- hmm$bins$counts.rpkm |
228 | 228 |
posteriors <- list() |
229 | 229 |
posteriors[[1]] <- hmm$bins$posteriors |
230 |
- peakScores <- list() |
|
231 |
- peakScores[[1]] <- hmm$bins$peakScores |
|
230 |
+ maxPostInPeak <- list() |
|
231 |
+ maxPostInPeak[[1]] <- hmm$bins$maxPostInPeak |
|
232 | 232 |
peaks <- list() |
233 | 233 |
peaks[[1]] <- hmm$peaks |
234 | 234 |
binstates <- list() |
... | ... |
@@ -242,7 +242,7 @@ combineMultivariates <- function(hmms, mode) { |
242 | 242 |
infos[[i1]] <- hmm$info |
243 | 243 |
counts[[i1]] <- hmm$bins$counts.rpkm |
244 | 244 |
posteriors[[i1]] <- hmm$bins$posteriors |
245 |
- peakScores[[i1]] <- hmm$bins$peakScores |
|
245 |
+ maxPostInPeak[[i1]] <- hmm$bins$maxPostInPeak |
|
246 | 246 |
peaks[[i1]] <- hmm$peaks |
247 | 247 |
binstates[[i1]] <- dec2bin(hmm$bins$state, colnames=hmm$info$ID) |
248 | 248 |
stopTimedMessage(ptm) |
... | ... |
@@ -251,7 +251,7 @@ combineMultivariates <- function(hmms, mode) { |
251 | 251 |
ptm <- startTimedMessage("Concatenating mark-conditions ...") |
252 | 252 |
counts <- do.call(cbind, counts) |
253 | 253 |
posteriors <- do.call(cbind, posteriors) |
254 |
- peakScores <- do.call(cbind, peakScores) |
|
254 |
+ maxPostInPeak <- do.call(cbind, maxPostInPeak) |
|
255 | 255 |
binstates <- do.call(cbind, binstates) |
256 | 256 |
infos <- do.call(rbind, infos) |
257 | 257 |
conditions <- unique(infos$condition) |
... | ... |
@@ -297,12 +297,12 @@ combineMultivariates <- function(hmms, mode) { |
297 | 297 |
ptm <- startTimedMessage("Transferring counts and posteriors ...") |
298 | 298 |
bins$counts.rpkm <- counts |
299 | 299 |
bins$posteriors <- posteriors |
300 |
- bins$peakScores <- peakScores |
|
300 |
+ bins$maxPostInPeak <- maxPostInPeak |
|
301 | 301 |
stopTimedMessage(ptm) |
302 | 302 |
|
303 | 303 |
## Add differential score ## |
304 | 304 |
ptm <- startTimedMessage("Adding differential score ...") |
305 |
- bins$differential.score <- differentialScoreSum(bins$peakScores, infos) |
|
305 |
+ bins$differential.score <- differentialScoreSum(bins$maxPostInPeak, infos) |
|
306 | 306 |
stopTimedMessage(ptm) |
307 | 307 |
|
308 | 308 |
### Redo the segmentation for all conditions combined |
... | ... |
@@ -193,8 +193,11 @@ exportUnivariatePeaks <- function(hmm.list, filename, header=TRUE, separate.file |
193 | 193 |
} |
194 | 194 |
cat(paste0("track name=\"",trackname.string,"\" description=\"",trackname.string,"\" visibility=1 itemRgb=On priority=",priority,"\n"), file=filename.gz, append=TRUE) |
195 | 195 |
} |
196 |
- if (is.null(peaks$peakScores)) { |
|
196 |
+ if (is.null(peaks$maxPostInPeak)) { |
|
197 | 197 |
peaks$peakScores <- 0 |
198 |
+ } else { |
|
199 |
+ peaks$peakScores <- suppressWarnings( -10*log10(1-peaks$maxPostInPeak) ) |
|
200 |
+ peaks$peakScores[is.nan(peaks$peakScores) | peaks$peakScores > 1000] <- 1000 |
|
198 | 201 |
} |
199 | 202 |
df <- as.data.frame(peaks) |
200 | 203 |
df$peakNumber <- paste0('peak_', 1:nrow(df)) |
... | ... |
@@ -434,12 +437,19 @@ exportMultivariatePeaks <- function(hmm, filename, trackname=NULL, header=TRUE, |
434 | 437 |
cat("", file=filename.gz) |
435 | 438 |
} |
436 | 439 |
|
437 |
- ## Export peaks on a per bin basis to obtain proper posterior scores |
|
440 |
+ ## Export peaks |
|
438 | 441 |
for (imod in 1:length(hmm$info$ID)) { |
439 | 442 |
ID <- hmm$info$ID[imod] |
440 | 443 |
|
441 |
- ## Select only segments with peaks |
|
444 |
+ ## Add peak score |
|
442 | 445 |
peaks <- insertchr(hmm$peaks[[ID]]) |
446 |
+ if (is.null(peaks$maxPostInPeak)) { |
|
447 |
+ peaks$peakScores <- 0 |
|
448 |
+ } else { |
|
449 |
+ peaks$peakScores <- suppressWarnings( -10*log10(1-peaks$maxPostInPeak) ) |
|
450 |
+ peaks$peakScores[is.nan(peaks$peakScores) | peaks$peakScores > 1000] <- 1000 |
|
451 |
+ } |
|
452 |
+ |
|
443 | 453 |
peaks.df <- as.data.frame(peaks) |
444 | 454 |
peaks.df$peakNumber <- paste0('peak_', 1:nrow(peaks.df)) |
445 | 455 |
|
... | ... |
@@ -606,12 +616,20 @@ exportCombinedMultivariatePeaks <- function(hmm, filename, trackname=NULL, heade |
606 | 616 |
cat("", file=filename.gz) |
607 | 617 |
} |
608 | 618 |
|
619 |
+ ## Export peaks |
|
609 | 620 |
for (imod in 1:length(hmm$info$ID)) { |
610 | 621 |
ID <- hmm$info$ID[imod] |
611 | 622 |
cond <- hmm$info$condition[imod] |
612 | 623 |
|
613 |
- ## Select only segments with peaks |
|
624 |
+ ## Add peak score |
|
614 | 625 |
peaks <- insertchr(hmm$peaks[[ID]]) |
626 |
+ if (is.null(peaks$maxPostInPeak)) { |
|
627 |
+ peaks$peakScores <- 0 |
|
628 |
+ } else { |
|
629 |
+ peaks$peakScores <- suppressWarnings( -10*log10(1-peaks$maxPostInPeak) ) |
|
630 |
+ peaks$peakScores[is.nan(peaks$peakScores) | peaks$peakScores > 1000] <- 1000 |
|
631 |
+ } |
|
632 |
+ |
|
615 | 633 |
peaks.df <- as.data.frame(peaks) |
616 | 634 |
peaks.df$peakNumber <- paste0('peak_', 1:nrow(peaks.df)) |
617 | 635 |
|
... | ... |
@@ -2,7 +2,7 @@ getMaxPostInPeaks <- function(states, posteriors) { |
2 | 2 |
|
3 | 3 |
binstates <- dec2bin(states, colnames = colnames(posteriors)) |
4 | 4 |
rownames(binstates) <- NULL |
5 |
- peakScores <- matrix(NA, nrow=nrow(binstates), ncol=ncol(binstates), dimnames=list(NULL, colnames(binstates))) |
|
5 |
+ maxPostInPeak <- matrix(NA, nrow=nrow(binstates), ncol=ncol(binstates), dimnames=list(NULL, colnames(binstates))) |
|
6 | 6 |
for (icol in 1:ncol(binstates)) { |
7 | 7 |
r.bin <- rle(binstates[,icol]) |
8 | 8 |
r <- r.bin |
... | ... |
@@ -16,9 +16,9 @@ getMaxPostInPeaks <- function(states, posteriors) { |
16 | 16 |
r <- r.bin |
17 | 17 |
r$values[r$values == TRUE] <- df$x |
18 | 18 |
r$values[r$values == FALSE] <- 0 |
19 |
- peakScores[,icol] <- inverse.rle(r) |
|
19 |
+ maxPostInPeak[,icol] <- inverse.rle(r) |
|
20 | 20 |
} |
21 |
- return(peakScores) |
|
21 |
+ return(maxPostInPeak) |
|
22 | 22 |
|
23 | 23 |
} |
24 | 24 |
|
... | ... |
@@ -76,21 +76,21 @@ removeCondition <- function(model, conditions) { |
76 | 76 |
} |
77 | 77 |
model$bins$posteriors <- posteriors |
78 | 78 |
} |
79 |
- if (!is.null(model$bins$peakScores)) { |
|
80 |
- peakScores <- model$bins$peakScores |
|
79 |
+ if (!is.null(model$bins$maxPostInPeak)) { |
|
80 |
+ maxPostInPeak <- model$bins$maxPostInPeak |
|
81 | 81 |
removeconds <- paste0(paste0('-', conditions, '-'), collapse='|') |
82 |
- keepconds <- grep(removeconds, colnames(peakScores), invert=TRUE, value=TRUE) |
|
83 |
- peakScores <- peakScores[,keepconds] |
|
84 |
- if (class(peakScores) != "matrix") { |
|
85 |
- peakScores <- matrix(peakScores, ncol=1, dimnames=list(NULL, keepconds)) |
|
82 |
+ keepconds <- grep(removeconds, colnames(maxPostInPeak), invert=TRUE, value=TRUE) |
|
83 |
+ maxPostInPeak <- maxPostInPeak[,keepconds] |
|
84 |
+ if (class(maxPostInPeak) != "matrix") { |
|
85 |
+ maxPostInPeak <- matrix(maxPostInPeak, ncol=1, dimnames=list(NULL, keepconds)) |
|
86 | 86 |
} |
87 |
- model$bins$peakScores <- peakScores |
|
87 |
+ model$bins$maxPostInPeak <- maxPostInPeak |
|
88 | 88 |
} |
89 | 89 |
stopTimedMessage(ptm) |
90 | 90 |
# Redo differential score |
91 | 91 |
if (!is.null(model$bins$differential.score)) { |
92 | 92 |
ptm <- startTimedMessage("Differential score ...") |
93 |
- model$bins$differential.score <- differentialScoreSum(model$bins$peakScores, model$info) |
|
93 |
+ model$bins$differential.score <- differentialScoreSum(model$bins$maxPostInPeak, model$info) |
|
94 | 94 |
stopTimedMessage(ptm) |
95 | 95 |
} |
96 | 96 |
# Redo transition frequencies and groups |
... | ... |
@@ -12,16 +12,16 @@ multivariateSegmentation <- function(bins, column2collapseBy='state') { |
12 | 12 |
df <- as.data.frame(bins) |
13 | 13 |
ind.readcols <- grep('^counts', names(df)) |
14 | 14 |
ind.postcols <- grep('^posteriors', names(df)) |
15 |
- ind.peakcols <- grep('^peakScores', names(df)) |
|
15 |
+ ind.peakcols <- grep('^maxPostInPeak', names(df)) |
|
16 | 16 |
ind.widthcol <- grep('width', names(df)) |
17 | 17 |
ind.scorecol <- grep('differential.score', names(df)) |
18 | 18 |
red.df <- suppressMessages(collapseBins(df, column2collapseBy=column2collapseBy, columns2average=c(ind.scorecol), columns2drop=c(ind.readcols, ind.widthcol, ind.postcols))) |
19 | 19 |
names(red.df) <- sub('^mean.','', names(red.df)) |
20 |
- red.peakscores <- as.matrix(red.df[grep('^peakScores', names(red.df))]) |
|
21 |
- red.df[grep('^peakScores', names(red.df))] <- NULL |
|
20 |
+ red.maxPostInPeak <- as.matrix(red.df[grep('^maxPostInPeak', names(red.df))]) |
|
21 |
+ red.df[grep('^maxPostInPeak', names(red.df))] <- NULL |
|
22 | 22 |
segments <- methods::as(red.df, 'GRanges') |
23 |
- segments$peakScores <- red.peakscores |
|
24 |
- colnames(segments$peakScores) <- colnames(bins$peakScores) |
|
23 |
+ segments$maxPostInPeak <- red.maxPostInPeak |
|
24 |
+ colnames(segments$maxPostInPeak) <- colnames(bins$maxPostInPeak) |
|
25 | 25 |
## Reorder properly to match the order in bins |
26 | 26 |
diffscore.temp <- segments$differential.score |
27 | 27 |
segments$differential.score <- NULL |