Skip to content

Commit bcaa4a1

Browse files
committed
Lint codes
1 parent 5d41d00 commit bcaa4a1

File tree

11 files changed

+184
-115
lines changed

11 files changed

+184
-115
lines changed

.lintr

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1-
linters: list(linelen=line_length_linter(80),
2-
abspathlintr=absolute_path_linter(),
3-
assignlintr=assignment_linter(),
4-
notab=no_tab_linter(),
5-
namelinter=object_name_linter(styles = c("snake_case", "camelCase", "symbols")),
6-
brace=brace_linter(),
7-
trailingwhites=trailing_whitespace_linter())
1+
linters: list(
2+
abspathlintr = absolute_path_linter(),
3+
assignlintr = assignment_linter(),
4+
brace = brace_linter(),
5+
comma = commas_linter(),
6+
comment_code = commented_code_linter(),
7+
cyclocomp = cyclocomp_linter(),
8+
equal_na = equals_na_linter(),
9+
left_par = function_left_parentheses_linter(),
10+
infix_spaces = infix_spaces_linter(),
11+
linelen = line_length_linter(80),
12+
notab = whitespace_linter(),
13+
object_length = object_length_linter(),
14+
object_usage = object_usage_linter(),
15+
par_body = paren_body_linter(),
16+
quotes = quotes_linter(),
17+
semicolon = semicolon_linter(),
18+
seq = seq_linter(),
19+
spaces = spaces_inside_linter(),
20+
space_par = spaces_left_parentheses_linter(),
21+
t_f_symbol = T_and_F_symbol_linter(),
22+
blank_lines = trailing_blank_lines_linter(),
23+
trailingwhites= trailing_whitespace_linter(),
24+
vector_logic = vector_logic_linter())
825
exclusions: list("tests/testthat.R",
926
"tests/testthat/test_knn.R",
1027
"tests/testthat/test_mrw.R",
1128
"tests/testthat/test_heat.R",
1229
"tests/testthat/test_util.R",
1330
"inst/doc/diffusr.R",
31+
"vignettes/diffusr.R",
32+
"vignettes/diffusr.Rmd",
1433
"R/RcppExports.R")
1534
exclude: "# Exclude Linting"
1635
exclude_start: "# Begin Exclude Linting"
1736
exclude_end: "# End Exclude Linting"
37+

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export(nearest.neighbors)
88
export(normalize.laplacian)
99
export(normalize.stochastic)
1010
export(random.walk)
11-
import(Rcpp)
1211
importFrom(Rcpp,sourceCpp)
1312
importFrom(checkmate,assert)
1413
importFrom(checkmate,assert_int)

R/diffusr-package.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,29 @@
2929
#' @keywords package
3030
#'
3131
#' @references
32+
#'
3233
#' Tong, H., Faloutsos, C., & Pan, J. Y. (2006),
33-
#' Fast random walk with restart and its applications.\cr \cr
34+
#' Fast random walk with restart and its applications.
35+
#'
3436
#' Koehler, S., Bauer, S., Horn, D., & Robinson, P. N. (2008),
3537
#' Walking the interactome for prioritization of candidate disease genes.
36-
#' \emph{The American Journal of Human Genetics}\cr \cr
38+
#' \emph{The American Journal of Human Genetics}
39+
#'
3740
#' Bonacich, P. (1987),
3841
#' Power and centrality: A family of measures.
39-
#' \emph{American Journal of Sociology}\cr \cr
40-
#' Leiserson, M. D., Vandin, F., Wu, H. T., Dobson, J. R., Eldridge, J. V., Thomas, J. L., ... & Lawrence, M. S. (2015),
41-
#' Pan-cancer network analysis identifies combinations of rare somatic mutations across pathways and protein complexes.
42-
#' \emph{Nature genetics}\cr \cr
43-
#' \url{https://en.wikipedia.org/wiki/Laplacian_matrix} \cr
42+
#' \emph{American Journal of Sociology}
43+
#'
44+
#' Leiserson, M. D., Vandin, F., Wu, H. T., Dobson, J. R., Eldridge, J. V.,
45+
#' Thomas, J. L., ... & Lawrence, M. S. (2015),
46+
#' Pan-cancer network analysis identifies combinations of rare somatic mutations
47+
#' across pathways and protein complexes.
48+
#' \emph{Nature genetics}
49+
#'
50+
#' \url{https://en.wikipedia.org/wiki/Laplacian_matrix}
51+
#'
4452
#' \url{https://en.wikipedia.org/wiki/Heat_equation}
4553
#'
4654
#' @useDynLib diffusr
47-
#' @import Rcpp
55+
#'
56+
#' @importFrom Rcpp sourceCpp
4857
NULL

R/heat_diffusion.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
#'
3737
#' @useDynLib diffusr
3838
#'
39-
#' @importFrom checkmate assert_int assert_integer assert test_matrix test_numeric test_atomic_vector
39+
#' @importFrom checkmate assert_int assert_integer assert test_matrix
40+
#' test_numeric test_atomic_vector
4041
#' @importFrom Rcpp sourceCpp
4142
#'
4243
#' @references
@@ -51,20 +52,22 @@
5152
#' # adjacency matrix (either normalized or not)
5253
#' graph <- matrix(abs(rnorm(n*n)), n, n)
5354
#' # computation of stationary distribution
54-
#' ht <- heat.diffusion(h0, graph)
55+
#' heat <- heat.diffusion(h0, graph)
5556
heat.diffusion <- function(h0, graph, t = 0.5, ...) {
5657
assert_number(t, lower = 0, na.ok = FALSE, finite = TRUE, null.ok = FALSE)
5758
n_elements <- nrow(graph)
5859

5960
# convert h0 if h0 is vector
60-
if (test_numeric(h0, lower = 0, len = n_elements, finite = TRUE, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE) &&
61+
if (test_numeric(h0, lower = 0, len = n_elements, finite = TRUE,
62+
any.missing = FALSE, all.missing = FALSE, null.ok = FALSE) &&
6163
test_atomic_vector(h0, len = n_elements)) {
6264
h0 <- as.matrix(h0)
6365
} else {
6466
assert(
65-
test_matrix(h0, mode = 'numeric', nrows = n_elements, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE),
67+
test_matrix(h0, mode = "numeric", nrows = n_elements, any.missing = FALSE,
68+
all.missing = FALSE, null.ok = FALSE),
6669
any(h0 >= 0),
67-
combine = 'and'
70+
combine = "and"
6871
)
6972
}
7073

@@ -75,12 +78,13 @@ heat.diffusion <- function(h0, graph, t = 0.5, ...) {
7578
# TODO: sparse matrix
7679
} else {
7780
assert(
78-
test_matrix(graph, mode = 'numeric', nrows = n_elements, ncols = n_elements, min.rows = 3, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE),
81+
test_matrix(graph, mode = "numeric", nrows = n_elements,
82+
ncols = n_elements, min.rows = 3, any.missing = FALSE,
83+
all.missing = FALSE, null.ok = FALSE),
7984
any(graph >= 0),
80-
combine = 'and'
85+
combine = "and"
8186
)
8287
heat <- heat_diffusion_(h0, laplacian_(graph), t)
8388
}
8489
return(heat)
8590
}
86-

R/is.dgCMatrix.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @importFrom methods is
99
#'
1010
is.dgCMatrix <- function(mat) {
11-
return(is(mat, 'dgCMatrix'))
11+
return(is(mat, "dgCMatrix"))
1212
}
1313

1414
#' Check if the sparse matrix valid
@@ -22,10 +22,10 @@ assert_dgCMatrix <- function(adj_matrix, non_negative = TRUE) {
2222
if (adj_matrix@Dim[1] != adj_matrix@Dim[2]) {
2323
stop(paste("Error: Assertion on 'adj_matrix' failed: Must have exactly",
2424
adj_matrix@Dim[2], "rows, but has",
25-
adj_matrix@Dim[1] ,"rows."))
25+
adj_matrix@Dim[1], "rows."))
2626
} else if (adj_matrix@Dim[1] < 4) {
27-
stop(paste("Error: Assertion on 'adj_matrix' failed: Must have at least 3 rows, but has",
28-
adj_matrix@Dim[1] ,"rows."))
27+
stop(paste("Error: Assertion on 'adj_matrix' failed: Must have at least",
28+
"3 rows, but has", adj_matrix@Dim[1], "rows."))
2929
} else if (non_negative && any(adj_matrix@x < 0)) {
3030
stop(paste("Error: Assertion on 'adj_matrix' failed: Element",
3131
seq_along(adj_matrix@x)[adj_matrix@x < 0][1], "is not >= 0."))

R/mat_util.R

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@
3232
#' @examples
3333
#' W <- matrix(abs(rnorm(10000)), 100, 100)
3434
#' stoch.W <- normalize.stochastic(W)
35-
normalize.stochastic <- function(obj, ...)
36-
{
35+
normalize.stochastic <- function(obj, ...) {
3736
is_matrix <- FALSE
38-
if (test_numeric(obj, lower = 0, finite = TRUE, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE) &&
37+
if (test_numeric(obj, lower = 0, finite = TRUE, any.missing = FALSE,
38+
all.missing = FALSE, null.ok = FALSE) &&
3939
test_atomic_vector(obj)) {
40-
if (!.equals.double(sum(obj), 1, .001))
41-
{
40+
if (!.equals.double(sum(obj), 1, .001)) {
4241
message("normalizing vector!")
4342
}
4443
} else if (is.dgCMatrix(obj)) {
4544
assert_dgCMatrix(obj)
4645
is_matrix <- TRUE
4746
} else {
4847
assert(
49-
check_matrix(obj, mode = 'numeric', any.missing = FALSE, all.missing = FALSE, null.ok = FALSE),
48+
check_matrix(obj, mode = "numeric", any.missing = FALSE,
49+
all.missing = FALSE, null.ok = FALSE),
5050
any(obj >= 0),
51-
combine = 'and'
51+
combine = "and"
5252
)
5353
is_matrix <- TRUE
5454
}
@@ -81,17 +81,17 @@ normalize.stochastic <- function(obj, ...)
8181
#' @examples
8282
#' W <- matrix(abs(rnorm(10000)), 100, 100)
8383
#' lapl.W <- normalize.laplacian(W)
84-
normalize.laplacian <- function(obj, ...)
85-
{
86-
# UseMethod("normalize.laplacian")
84+
normalize.laplacian <- function(obj, ...) {
8785
if (is.dgCMatrix(obj)) {
8886
assert_dgCMatrix(obj)
8987
# TODO: sparse matrix
9088
} else {
9189
assert(
92-
check_matrix(obj, mode = 'numeric', nrows = ncol(obj), ncols = nrow(obj), min.rows = 3, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE),
90+
check_matrix(obj, mode = "numeric", nrows = ncol(obj), ncols = nrow(obj),
91+
min.rows = 3, any.missing = FALSE, all.missing = FALSE,
92+
null.ok = FALSE),
9393
any(obj >= 0),
94-
combine = 'and'
94+
combine = "and"
9595
)
9696
return(laplacian_(obj))
9797
}
@@ -113,28 +113,27 @@ normalize.laplacian <- function(obj, ...)
113113
#' @examples
114114
#' W <- matrix(abs(rnorm(10000)), 100, 100)
115115
#' cor.hub <- hub.correction(W)
116-
hub.correction <- function(obj)
117-
{
116+
hub.correction <- function(obj) {
118117
n_elements <- nrow(obj)
119118
if (is.dgCMatrix(obj)) {
120119
assert_dgCMatrix(obj)
121120
# TODO: sparse matrix
122121
} else {
123122
assert(
124-
check_matrix(obj, mode = 'numeric', nrows = n_elements, ncols = n_elements, min.rows = 3, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE),
123+
check_matrix(obj, mode = "numeric", min.rows = 3, nrows = n_elements,
124+
ncols = n_elements, any.missing = FALSE, all.missing = FALSE,
125+
null.ok = FALSE),
125126
any(obj >= 0),
126-
combine = 'and'
127+
combine = "and"
127128
)
128129
return(hub_normalize_(obj))
129130
}
130131
}
131132

132133
#' @noRd
133134
#' @importFrom igraph graph_from_adjacency_matrix components
134-
.is.ergodic <- function(obj)
135-
{
136-
adj <- graph_from_adjacency_matrix(
137-
obj, mode="directed", weighted=TRUE)
135+
.is.ergodic <- function(obj) {
136+
adj <- graph_from_adjacency_matrix(obj, mode = "directed", weighted = TRUE)
138137
comps <- components(adj)
139138
ifelse(length(comps$csize) == 1, TRUE, FALSE)
140139
}

R/mrw.R

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
#'
3939
#' @param thresh threshold for breaking the iterative computation of the
4040
#' stationary distribution. If the absolute difference of the distribution at
41-
#' time point $t-1$ and $t$ is less than \code{thresh}, then the algorithm stops.
42-
#' If \code{thresh} is not reached before \code{niter}, then the algorithm stops
43-
#' as well.
41+
#' time point \eqn{t-1} and \eqn{t} is less than \code{thresh}, then the
42+
#' algorithm stops. If \code{thresh} is not reached before \code{niter}, then
43+
#' the algorithm stops as well.
4444
#'
4545
#' @param niter maximal number of iterations for computation of the
4646
#' Markov chain. If \code{thresh} is not reached, then \code{niter} is used as
@@ -62,21 +62,24 @@
6262
#' @return returns a list with the following elements
6363
#' \itemize{
6464
#' \item p.inf the stationary distribution as numeric vector
65-
#' \item transition.matrix the column normalized transition matrix used for the random walk
65+
#' \item transition.matrix the column normalized transition matrix used for
66+
#' the random walk
6667
#' }
6768
#'
6869
#' @references
6970
#' Tong, H., Faloutsos, C., & Pan, J. Y. (2006),
70-
#' Fast random walk with restart and its applications.\cr \cr
71+
#' Fast random walk with restart and its applications.
72+
#'
7173
#' Koehler, S., Bauer, S., Horn, D., & Robinson, P. N. (2008),
7274
#' Walking the interactome for prioritization of candidate disease genes.
73-
#' \emph{The American Journal of Human Genetics}\cr \cr
75+
#' \emph{The American Journal of Human Genetics}
7476
#'
7577
#' @export
7678
#'
7779
#' @useDynLib diffusr
7880
#'
79-
#' @importFrom checkmate assert_number assert_int assert_logical test_numeric assert test_matrix check_numeric
81+
#' @importFrom checkmate assert_number assert_int assert_logical test_numeric
82+
#' assert test_matrix check_numeric
8083
#' @importFrom Rcpp sourceCpp
8184
#'
8285
#' @examples
@@ -90,37 +93,46 @@
9093
#' pt <- random.walk(p0, graph)
9194
#'
9295
random.walk <- function(p0, graph, r = 0.5, niter = 1e4, thresh = 1e-4,
93-
do.analytical = FALSE, correct.for.hubs = FALSE,
94-
ergodic.tolerance = FALSE, return.pt.only = FALSE) {
96+
do.analytical = FALSE, correct.for.hubs = FALSE,
97+
ergodic.tolerance = FALSE, return.pt.only = FALSE) {
9598
## Check the fucking inputs
96-
assert_number(r, lower = 0, upper = 1, na.ok = FALSE, finite = TRUE, null.ok = FALSE)
99+
assert_number(r, lower = 0, upper = 1, na.ok = FALSE, finite = TRUE,
100+
null.ok = FALSE)
97101
assert_int(niter, lower = 2, na.ok = FALSE, coerce = TRUE, null.ok = FALSE)
98-
assert_number(thresh, lower = 0, na.ok = FALSE, finite = TRUE, null.ok = FALSE)
99-
assert_logical(do.analytical, len = 1, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE)
100-
assert_logical(correct.for.hubs, len = 1, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE)
102+
assert_number(thresh, lower = 0, na.ok = FALSE, finite = TRUE,
103+
null.ok = FALSE)
104+
assert_logical(do.analytical, len = 1, any.missing = FALSE,
105+
all.missing = FALSE, null.ok = FALSE)
106+
assert_logical(correct.for.hubs, len = 1, any.missing = FALSE,
107+
all.missing = FALSE, null.ok = FALSE)
101108

102109
# graph must be either matrix or dgCMatrix
103110
n_elements <- nrow(graph)
104111
if (is.dgCMatrix(graph)) {
105112
assert_dgCMatrix(graph)
106113
sparse <- TRUE
114+
# TODO: sparse matrix
107115
} else {
108116
assert(
109-
test_matrix(graph, mode = 'numeric', nrows = n_elements, ncols = n_elements, min.rows = 3, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE),
117+
test_matrix(graph, mode = "numeric", min.rows = 3, nrows = n_elements,
118+
ncols = n_elements, any.missing = FALSE, all.missing = FALSE,
119+
null.ok = FALSE),
110120
any(graph >= 0),
111-
combine = 'and'
121+
combine = "and"
112122
)
113123
sparse <- FALSE
114124
}
115125

116126
# convert p0 if p0 is vector
117-
if (test_numeric(p0, lower = 0, len = n_elements, finite = TRUE, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE)) {
127+
if (test_numeric(p0, lower = 0, len = n_elements, finite = TRUE,
128+
any.missing = FALSE, all.missing = FALSE, null.ok = FALSE)) {
118129
p0 <- as.matrix(p0)
119130
} else {
120131
assert(
121-
test_matrix(p0, mode = 'numeric', nrows = n_elements, any.missing = FALSE, all.missing = FALSE, null.ok = FALSE),
132+
test_matrix(p0, mode = "numeric", nrows = n_elements, any.missing = FALSE,
133+
all.missing = FALSE, null.ok = FALSE),
122134
any(p0 >= 0),
123-
combine = 'and'
135+
combine = "and"
124136
)
125137
}
126138

@@ -136,11 +148,9 @@ random.walk <- function(p0, graph, r = 0.5, niter = 1e4, thresh = 1e-4,
136148
}
137149

138150
l <- mrwr_(normalize.stochastic(p0),
139-
stoch.graph, r, thresh, niter, do.analytical)
151+
stoch.graph, r, thresh, niter, do.analytical)
140152
if (!return.pt.only) {
141-
l <- list(
142-
p.inf=l,
143-
transition.matrix=stoch.graph)
153+
l <- list(p.inf = l, transition.matrix = stoch.graph)
144154
}
145155

146156
return(l)

0 commit comments

Comments
 (0)