Browse code

better argument cheking in various functions

git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/bsseq@80445 bc3139a8-67e5-0310-9ffc-ced21a209358

Kasper D. Hansen authored on 14/09/2013 15:06:17
Showing5 changed files

... ...
@@ -1,5 +1,5 @@
1 1
 Package: bsseq
2
-Version: 0.9.9
2
+Version: 0.9.10
3 3
 Title: Analyze, manage and store bisulfite sequencing data
4 4
 Description: Tools for analyzing and visualizing bisulfite sequencing data
5 5
 Author: Kasper Daniel Hansen
... ...
@@ -43,6 +43,9 @@ BSmooth.tstat <- function(BSseq, group1, group2, estimate.var = c("same", "paire
43 43
         stopifnot(min(group2) >= 1 & max(group2) <= ncol(BSseq))
44 44
     } else stop("problems with argument 'group2'")
45 45
     stopifnot(length(intersect(group1, group2)) == 0)
46
+    stopifnot(length(group1) > 0)
47
+    stopifnot(length(group2) > 0)
48
+    stopifnot(length(group1) + length(group2) >= 3)
46 49
     if(estimate.var == "paired")
47 50
         stopifnot(length(group1) == length(group2))
48 51
     
... ...
@@ -51,6 +54,8 @@ BSmooth.tstat <- function(BSseq, group1, group2, estimate.var = c("same", "paire
51 54
     
52 55
     if(is.null(maxGap))
53 56
         maxGap <- BSseq@parameters$maxGap
57
+    if(is.null(maxGap))
58
+        stop("need to set argument 'maxGap'")
54 59
     
55 60
     if(verbose) cat("[BSmooth.tstat] preprocessing ... ")
56 61
     ptime1 <- proc.time()
... ...
@@ -65,20 +65,21 @@ plot.BSseqTstat <- function(x, y, ...) {
65 65
     }
66 66
 }
67 67
 
68
-getStats <- function(BSseqTstat, regions = NULL, column = "tstat.corrected") {
68
+getStats <- function(BSseqTstat, regions = NULL, stat = "tstat.corrected") {
69 69
     stopifnot(is(BSseqTstat, "BSseqTstat"))
70
-    stopifnot(column %in% colnames(BSseqTstat@stats))
71
-    if(class(regions) == "data.frame")
72
-        regions <- data.frame2GRanges(regions)
73 70
     if(is.null(regions))
74 71
         return(BSseqTstat@stats)
72
+    if(class(regions) == "data.frame")
73
+        regions <- data.frame2GRanges(regions)
74
+    stopifnot(stat %in% colnames(BSseqTstat@stats))
75
+    stopifnot(length(stat) == 1)
75 76
     stopifnot(is(regions, "GenomicRanges"))
76 77
     ov <- findOverlaps(BSseqTstat, regions)
77 78
     ov.sp <- split(queryHits(ov), subjectHits(ov))
78 79
     getRegionStats <- function(idx) {
79 80
         mat <- BSseqTstat@stats[idx,, drop=FALSE]
80
-        areaStat <- sum(mat[, column])
81
-        maxStat <- max(mat[, column])
81
+        areaStat <- sum(mat[, stat])
82
+        maxStat <- max(mat[, stat])
82 83
         c(areaStat, maxStat)
83 84
     }
84 85
     stats <- matrix(NA, ncol = 2, nrow = length(regions))
... ...
@@ -86,7 +87,7 @@ getStats <- function(BSseqTstat, regions = NULL, column = "tstat.corrected") {
86 87
     tmp <- lapply(ov.sp, getRegionStats)
87 88
     stats[as.integer(names(tmp)),] <- do.call(rbind, tmp)
88 89
     out <- as.data.frame(stats)
89
-    if(! column %in% c("tstat.corrected", "tstat"))
90
+    if(! stat %in% c("tstat.corrected", "tstat"))
90 91
         return(out)
91 92
     getRegionStats_ttest <- function(idx) {
92 93
         mat <- BSseqTstat@stats[idx,, drop=FALSE]
... ...
@@ -14,12 +14,14 @@ dmrFinder <- function(BSseqTstat, cutoff = NULL, qcutoff = c(0.025, 0.975),
14 14
     positions <- start(BSseqTstat)
15 15
     regions <- bsseq:::regionFinder3(direction, chr = chrs, pos = positions,
16 16
                                      maxGap = maxGap, verbose = subverbose)
17
+    if(is.null(regions$down) && is.null(regions$up))
18
+        return(NULL)
17 19
     if(verbose) cat("[dmrFinder] creating dmr data.frame\n")
18 20
     regions <- do.call(rbind, regions)
19 21
     rownames(regions) <- NULL
20 22
     regions$width <- regions$end - regions$start + 1
21 23
     regions$invdensity <- regions$width / regions$n
22
-    stats <- getStats(BSseqTstat, regions, column = stat)
24
+    stats <- getStats(BSseqTstat, regions, stat = stat)
23 25
     regions <- cbind(regions, stats)
24 26
     if(stat %in% c("tstat.corrected", "tstat")) {
25 27
         regions$direction <- ifelse(regions$meanDiff > 0, "hyper", "hypo")
... ...
@@ -21,6 +21,7 @@
21 21
     \item Allowed user specification of maxk, with a default of 10,000
22 22
     in BSmooth.
23 23
     \item Many bugfixes made necessary by the new class representation.
24
+    \item Better argument checking in BSmooth.tstat.
24 25
   }
25 26
 }
26 27