-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackDb.R
175 lines (169 loc) · 5.77 KB
/
trackDb.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(out.rds <- commandArgs(trailingOnly=TRUE))
options(repos="http://cloud.r-project.org")
### Write down what package versions work with your R code, and
### attempt to download and load those packages. The first argument is
### the version of R that you used, e.g. "3.0.2" and then the rest of
### the arguments are package versions. For
### CRAN/Bioconductor/R-Forge/etc packages, write
### e.g. RColorBrewer="1.0.5" and if RColorBrewer is not installed
### then we use install.packages to get the most recent version, and
### warn if the installed version is not the indicated version. For
### GitHub packages, write "user/repo@commit"
### e.g. "tdhock/animint@f877163cd181f390de3ef9a38bb8bdd0396d08a4" and
### we use install_github to get it, if necessary.
works_with_R <- function(Rvers,...){
local.lib <- file.path(
getwd(), paste0(
"library-",
paste(R.version[c("major", "minor")], collapse=".")))
old.path.vec <- .libPaths()
if(! local.lib %in% old.path.vec){
dir.create(local.lib, showWarnings=FALSE, recursive=TRUE)
.libPaths(local.lib)
}
pkg_ok_have <- function(pkg,ok,have){
stopifnot(is.character(ok))
if(!as.character(have) %in% ok){
warning("works with ",pkg," version ",
paste(ok,collapse=" or "),
", have ",have)
}
}
pkg_ok_have("R",Rvers,getRversion())
pkg.vers <- list(...)
for(pkg.i in seq_along(pkg.vers)){
vers <- pkg.vers[[pkg.i]]
pkg <- if(is.null(names(pkg.vers))){
""
}else{
names(pkg.vers)[[pkg.i]]
}
if(pkg == ""){# Then it is from GitHub.
## suppressWarnings is quieter than quiet.
if(!suppressWarnings(require(requireGitHub))){
## If requireGitHub is not available, then install it using
## devtools.
if(!suppressWarnings(require(devtools))){
install.packages("devtools")
require(devtools)
}
install_github("tdhock/requireGitHub")
require(requireGitHub)
}
requireGitHub(vers)
}else{# it is from a CRAN-like repos.
if(!suppressWarnings(require(pkg, character.only=TRUE))){
install.packages(pkg)
}
pkg_ok_have(pkg, vers, packageVersion(pkg))
library(pkg, character.only=TRUE)
}
}
}
works_with_R(
"3.6.0",
microbenchmark="1.4.6",
data.table="1.12.0",
dplyr="0.7.6",
rex="1.1.2",
namedCapture="2019.2.28",
rematch2="2.0.1",
stringr="1.3.1",
stringi="1.2.4",
re2r="0.2.0")
trackDb.txt.gz <- system.file(
"extdata", "trackDb.txt.gz", package="namedCapture")
trackDb.lines <- readLines(trackDb.txt.gz)
if(FALSE){
namedCapture:::variable_args_list(
trackDb.lines,
"track ",
name=trackName.pattern,
"(?:\n[^\n]+)*",
"\\s+bigDataUrl ",
bigDataUrl="[^\n]+")
}
pattern <- "track ((.*?)_(McGill([0-9,]+))(Coverage|Peaks)|[^\n]+)(?:\n[^\n]+)*\\s+bigDataUrl ([^\n]+)"
named.pattern <- "track (?P<trackName>(?P<cellType>.*?)_(?P<sampleName>McGill(?P<sampleID>[0-9,]+))(?P<dataType>Coverage|Peaks)|[^\n]+)(?:\n[^\n]+)*\\s+bigDataUrl (?P<bigDataUrl>[^\n]+)"
timing.dt.list <- list()
for(subject.size in 10^seq(2, 5, by=0.5)){
subject <- rep(trackDb.lines, l=subject.size)
timing <- microbenchmark::microbenchmark("re2r::re2_match_all"={
m <- re2r::re2_match_all(paste(subject, collapse="\n"), named.pattern)[[1]]
data.frame(m[,-1]) %>% mutate(
sampleID=as.integer(sampleID))
}, "stringr::str_match_all"={
m <- stringr::str_match_all(paste(subject, collapse="\n"), pattern)[[1]]
data.frame(m[,-1]) %>% mutate(
sampleID=as.integer(X4))
}, "stringi::stri_match_all"={
m <- stringi::stri_match_all(paste(subject, collapse="\n"), regex=pattern)[[1]]
data.frame(m[,-1]) %>% mutate(
sampleID=as.integer(X4))
}, "base::gregexpr(perl=TRUE)"={
g.res <- gregexpr(
named.pattern, paste(subject, collapse="\n"), perl=TRUE)[[1]]
## }, "base::gregexpr(perl=FALSE)"={
## gregexpr(pattern, paste(subject, collapse="\n"), perl=FALSE)[[1]]
## }, "namedCapture::str_match_all_named"={
## namedCapture::str_match_all_named(paste(subject, collapse="\n"), named.pattern)[[1]]
}, "rematch2::re_match_all"={
tib <- rematch2::re_match_all(paste(subject, collapse="\n"), named.pattern)
}, "rex::re_matches"={
re_matches(paste(subject, collapse="\n"), named.pattern, global=TRUE)[[1]] %>% mutate(
sampleID=as.integer(sampleID))
}, "namedCapture::str_match_all_variable"={
int.pattern <- list("[0-9]+", as.integer)
trackName.pattern <- list(
cellType=".*?",
"_",
sampleName=list(
"McGill",
sampleID=int.pattern),
dataType="Coverage|Peaks",
"|",
"[^\n]+")
match.df <- namedCapture::str_match_all_variable(
subject,
"track ",
trackName=trackName.pattern,
"(?:\n[^\n]+)*",
"\\s+bigDataUrl ",
bigDataUrl="[^\n]+")
},
times=5)
print(timing)
timing.dt.list[[paste(subject.size)]] <- data.table(subject.size, timing)
}
timing.dt <- do.call(rbind, timing.dt.list)
if(FALSE){
timing.dt[, seconds := time/1e9]
stats.dt <- timing.dt[, list(
median=median(seconds),
q25=quantile(seconds, 0.25),
q75=quantile(seconds, 0.75)
), by=list(expr, subject.size)]
library(ggplot2)
gg <- ggplot()+
geom_ribbon(aes(
subject.size, ymin=q25, ymax=q75, fill=expr),
alpha=0.5,
data=stats.dt)+
geom_line(aes(
subject.size, median, color=expr),
data=stats.dt)+
scale_x_log10(limits=c(1e2, 1e5))+
scale_y_log10()
directlabels::direct.label(gg, "last.polygons")
gg <- ggplot()+
geom_ribbon(aes(
subject.size, ymin=q25, ymax=q75, fill=expr),
alpha=0.5,
data=stats.dt)+
geom_line(aes(
subject.size, median, color=expr),
data=stats.dt)+
xlim(0, 1e5)
directlabels::direct.label(gg, "last.polygons")
}
saveRDS(timing.dt, out.rds)