From ce5849f38e31f62a6f34fbf15c4bbc4e336843fc Mon Sep 17 00:00:00 2001 From: xtYao Date: Mon, 25 Jun 2018 16:54:35 -0400 Subject: [PATCH 1/2] safer seg2gr --- R/gUtils.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/gUtils.R b/R/gUtils.R index 6a469d6..50ade8d 100644 --- a/R/gUtils.R +++ b/R/gUtils.R @@ -2350,6 +2350,10 @@ seg2gr = function(segs, seqlengths = NULL, seqinfo = Seqinfo()) segs$pos1 = as.numeric(segs$pos1) segs$pos2 = as.numeric(segs$pos2) + ## only convert valid ranges + valid.ix = which(!is.na(segs$pos1) & !is.na(segs$pos2)) + segs = segs[valid.ix, , drop=FALSE] + out = GRanges(seqnames = segs$chr, ranges = IRanges(segs$pos1, segs$pos2),strand = segs$strand, seqlengths = seqlengths) } else{ From 842dd582298b7c3644343fcf34b11bf6a7adb46b Mon Sep 17 00:00:00 2001 From: xtYao Date: Thu, 6 Sep 2018 16:34:02 -0400 Subject: [PATCH 2/2] in gr.string added colon between end and strand to be compatible with GenomicRanges --- R/gUtils.R | 11 ++++------- tests/testthat/test_rangeops.R | 14 +++++++------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/R/gUtils.R b/R/gUtils.R index 9738ca5..c2472e6 100644 --- a/R/gUtils.R +++ b/R/gUtils.R @@ -1031,23 +1031,20 @@ gr.string = function(gr, add.chr = FALSE, mb = FALSE, round = 3, other.cols = c( str = ifelse(as.logical(strand(gr)!='*'), as.character(strand(gr)), '') - + ## XT debug: the lastest GRanges requires an extra ":" in between end and strand if (mb){ - return(paste(sn, ':', round(start(gr)/1e6, round), '-', round(end(gr)/1e6, round), str, other.str, sep = '')) + return(paste(sn, ':', round(start(gr)/1e6, round), '-', round(end(gr)/1e6, round), ":", str, other.str, sep = '')) } else{ if (pretty){ - return(paste(sn, ':', stringr::str_trim(format(start(gr), big.mark = ',')), '-', stringr::str_trim(format(end(gr), big.mark = ',')), str, other.str, sep = '')) + return(paste(sn, ':', stringr::str_trim(format(start(gr), big.mark = ',')), '-', stringr::str_trim(format(end(gr), big.mark = ',')), ":", str, other.str, sep = '')) } else{ - return(paste(sn, ':', start(gr), '-', end(gr), str, other.str, sep = '')) + return(paste(sn, ':', start(gr), '-', end(gr), ":", str, other.str, sep = '')) } } } - - - #' @name grl.reduce #' @title grl.reduce #' @description diff --git a/tests/testthat/test_rangeops.R b/tests/testthat/test_rangeops.R index 4f5ca02..9ad70d2 100644 --- a/tests/testthat/test_rangeops.R +++ b/tests/testthat/test_rangeops.R @@ -351,15 +351,15 @@ test_that("gr.string", { expect_that(grepl("(+|-)", gr.string(example_genes)[1]), is_true()) ## check 'if (length(gr)==0){' ## add.chr - expect_equal(gr.string(example_genes, add.chr=TRUE)[1], 'chr1:69090-70008+') + expect_equal(gr.string(example_genes, add.chr=TRUE)[1], 'chr1:69090-70008:+') ## mb - expect_equal(gr.string(example_genes[1], mb = TRUE), '1:0.069-0.07+') + expect_equal(gr.string(example_genes[1], mb = TRUE), '1:0.069-0.07:+') ## round - expect_equal(gr.string(example_genes[1], round = 1, mb=TRUE), '1:0.1-0.1+') + expect_equal(gr.string(example_genes[1], round = 1, mb=TRUE), '1:0.1-0.1:+') ## other.cols - expect_equal(gr.string(example_genes[1], other.cols='name'), '1:69090-70008+ OR4F5') + expect_equal(gr.string(example_genes[1], other.cols='name'), '1:69090-70008:+ OR4F5') ## pretty - expect_equal(gr.string(example_genes, pretty=TRUE)[1], '1:69,090-70,008+') + expect_equal(gr.string(example_genes, pretty=TRUE)[1], '1:69,090-70,008:+') }) @@ -387,12 +387,12 @@ test_that("grl.string", { expect_that(nchar(names(grl.string(grl.hiC[1:5])[1])) > 0, is_true()) expect_that(grepl(",", grl.string(grl.hiC[1:5])[1]), is_true()) ## check 'if (class(grl) == "GRanges"){' - expect_equal(grl.string(example_genes[1]), '1:69090-70008+') + expect_equal(grl.string(example_genes[1]), '1:69090-70008:+') ## check 'error handling' expect_error(grl.string('foo')) ## Error: Input must be GRangesList (or GRanges, which is sent to gr.string) ## check 'else{ nm = 1:length(grl) ! 1138 }' names(grl1) = NULL - expect_equal(as.character(grl.string(grl1[2])), '9:140100229-140100229-,19:24309057-24309057-') + expect_equal(as.character(grl.string(grl1[2])), '9:140100229-140100229:-,19:24309057-24309057:-') })