Browse code

Merge pull request #366 from lima1/issue_40

Issue 40

Markus Riester authored on 24/04/2024 19:15:01 • GitHub committed on 24/04/2024 19:15:01
Showing 7 changed files

... ...
@@ -69,4 +69,4 @@ biocViews: CopyNumberVariation, Software, Sequencing,
69 69
     VariantAnnotation, VariantDetection, Coverage, ImmunoOncology
70 70
 NeedsCompilation: no
71 71
 ByteCompile: yes
72
-RoxygenNote: 7.2.3.9000
72
+RoxygenNote: 7.3.1
... ...
@@ -1,5 +1,6 @@
1 1
 # Generated by roxygen2: do not edit by hand
2 2
 
3
+export(adjustLogRatio)
3 4
 export(annotateTargets)
4 5
 export(bootstrapResults)
5 6
 export(calculateBamCoverageByInterval)
... ...
@@ -1,6 +1,12 @@
1 1
 Changes in version 2.10.0
2 2
 -------------------------
3 3
 
4
+NEW FEATURES
5
+    o adjustLogRatio function for adjusting a tumor vs normal coverage
6
+      ratio for purity and ploidy. Useful for downstream tools that
7
+      expect ratios instead of absolute copy numbers such as GISTIC.
8
+      Thanks @tedtoal (#40).
9
+
4 10
 SIGNIFICANT USER-VISIBLE CHANGES
5 11
 
6 12
     o Provide interval-level likelihood scores in runAbsoluteCN return
7 13
new file mode 100644
... ...
@@ -0,0 +1,40 @@
1
+#' Adjust tumor vs. normal coverage log ratio for tumor purity and ploidy
2
+#' 
3
+#' This function can be used to adjust the log ratio for tumor purity and
4
+#' ploidy for downstream tools that expect a log2 ratio (for example GISTIC).
5
+#' 
6
+#' 
7
+#' @param ratio Vector of log2 tumor vs normal coverage ratios. 
8
+#' @param purity Purity of sample.
9
+#' @param ploidy Ploidy of sample.
10
+#' @param is.log2 \code{log.ratio} is \code{log2} transformed. 
11
+#' @param min.ratio Minimum (non-log2-transformed) ratio. Set to approx -8
12
+#' \code{log2} adjusted.
13
+#' @return \code{numeric(length(log.ratio))}, \code{log.ratio} adjusted 
14
+#' for \code{purity} and \code{ploidy} 
15
+#' @author Markus Riester
16
+#' @references
17
+#   * Zack et al. (2012), Pan-cancer patterns of somatic copy number alteration 
18
+#'    Nature Biotechnology.
19
+#'  * Toal (2018), https://github.com/lima1/PureCN/issues/40
20
+#' 
21
+#' @examples
22
+#' 
23
+#' normal.coverage.file <- system.file("extdata", "example_normal.txt.gz", 
24
+#'     package = "PureCN")
25
+#' tumor.coverage.file <- system.file("extdata", "example_tumor.txt.gz", 
26
+#'     package = "PureCN")
27
+#' normal <- readCoverageFile(normal.coverage.file)
28
+#' tumor <- readCoverageFile(tumor.coverage.file)
29
+#' log.ratio <- calculateLogRatio(normal, tumor)
30
+#' log.ratio.adjusted <- adjustLogRatio(log.ratio, 0.65, 1.73)
31
+#' 
32
+#' @export adjustLogRatio
33
+adjustLogRatio <- function(ratio, purity, ploidy, is.log2 = TRUE, min.ratio = 2^-8) {
34
+    if (is.log2) ratio <- 2^ratio
35
+    adjusted <- (purity * ploidy * ratio + 2 * (1 - purity) * ratio - 2 * (1 - purity)) / (purity * ploidy)
36
+    adjusted <- pmax(min.ratio, adjusted)
37
+    if (is.log2) adjusted <- log2(adjusted)
38
+    return(adjusted)
39
+}
40
+
0 41
new file mode 100644
... ...
@@ -0,0 +1,47 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/adjustLogRatio.R
3
+\name{adjustLogRatio}
4
+\alias{adjustLogRatio}
5
+\title{Adjust tumor vs. normal coverage log ratio for tumor purity and ploidy}
6
+\usage{
7
+adjustLogRatio(ratio, purity, ploidy, is.log2 = TRUE, min.ratio = 2^-8)
8
+}
9
+\arguments{
10
+\item{ratio}{Vector of log2 tumor vs normal coverage ratios.}
11
+
12
+\item{purity}{Purity of sample.}
13
+
14
+\item{ploidy}{Ploidy of sample.}
15
+
16
+\item{is.log2}{\code{log.ratio} is \code{log2} transformed.}
17
+
18
+\item{min.ratio}{Minimum (non-log2-transformed) ratio. Set to approx -8
19
+\code{log2} adjusted.}
20
+}
21
+\value{
22
+\code{numeric(length(log.ratio))}, \code{log.ratio} adjusted 
23
+for \code{purity} and \code{ploidy}
24
+}
25
+\description{
26
+This function can be used to adjust the log ratio for tumor purity and
27
+ploidy for downstream tools that expect a log2 ratio (for example GISTIC).
28
+}
29
+\examples{
30
+
31
+normal.coverage.file <- system.file("extdata", "example_normal.txt.gz", 
32
+    package = "PureCN")
33
+tumor.coverage.file <- system.file("extdata", "example_tumor.txt.gz", 
34
+    package = "PureCN")
35
+normal <- readCoverageFile(normal.coverage.file)
36
+tumor <- readCoverageFile(tumor.coverage.file)
37
+log.ratio <- calculateLogRatio(normal, tumor)
38
+log.ratio.adjusted <- adjustLogRatio(log.ratio, 0.65, 1.73)
39
+
40
+}
41
+\references{
42
+Nature Biotechnology.
43
+ * Toal (2018), https://github.com/lima1/PureCN/issues/40
44
+}
45
+\author{
46
+Markus Riester
47
+}
... ...
@@ -7,9 +7,9 @@
7 7
 filterVcfMuTect2(
8 8
   vcf,
9 9
   tumor.id.in.vcf = NULL,
10
-  ignore = c("clustered_events", "t_lod", "str_contraction", "read_position",
11
-    "position", "fragment_length", "multiallelic", "clipping", "strand_artifact",
12
-    "strand_bias", "slippage", "weak_evidence", "orientation", "haplotype"),
10
+  ignore = c("clustered_events", "t_lod", "str_contraction", "read_position", "position",
11
+    "fragment_length", "multiallelic", "clipping", "strand_artifact", "strand_bias",
12
+    "slippage", "weak_evidence", "orientation", "haplotype"),
13 13
   ...
14 14
 )
15 15
 }
16 16
new file mode 100644
... ...
@@ -0,0 +1,16 @@
1
+context("adjustLogRatio")
2
+
3
+test_that("Function returns expected values for example coverage", {
4
+     data(purecn.example.output)
5
+     log.ratio <- purecn.example.output$results[[1]]$seg$seg.mean   
6
+     purity <- purecn.example.output$results[[1]]$purity
7
+     ploidy <- purecn.example.output$results[[1]]$ploidy
8
+     log.ratio.adjusted <- adjustLogRatio(log.ratio, purity, ploidy)
9
+     total.ploidy <- 1.73
10
+     p <- 1
11
+     log.ratio.offset <- 0
12
+     opt.C <- (2^(log.ratio.adjusted + log.ratio.offset) *  total.ploidy)/p - ((2 * (1 - p))/p)
13
+     expect_lt(abs(min(log.ratio.adjusted, na.rm=TRUE) + 8), 0.001)
14
+     expect_lt(median(abs(opt.C - purecn.example.output$results[[1]]$seg$C)), 0.1)  
15
+})
16
+