Skip to content

Commit

Permalink
Add a lintr configuration file and apply it to the package
Browse files Browse the repository at this point in the history
  • Loading branch information
aursiber committed Jul 31, 2024
1 parent 319d2a1 commit 3523be2
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 466 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
LICENSE.md
_pkgdown.yml
^\.github$
.lintr
7 changes: 7 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
linters: linters_with_defaults(
line_length_linter(250),
commented_code_linter = NULL,
trailing_whitespace_linter(allow_empty_lines = TRUE),
object_name_linter = NULL
)
encoding: "UTF-8"
46 changes: 23 additions & 23 deletions R/build_Lifemap.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' The dataframe can contain any number of additional columns defining traits/characters/values associated to
#' each taxid.
#' @param basemap The chosen basemap for Lifemap ("fr", "ncbi", "base" or "virus").
#' @param verbose If TRUE (the default), the function will print detailed information to the console.
#' @param verbose If TRUE (the default), the function will print detailed information to the console.
#' If FALSE, it will run silently.
#'
#' @return A list of class lifemap_obj containing:
Expand All @@ -16,7 +16,7 @@
#' - A list of its ascendants (ascend)
#' - Its type ("requested" or "ancestor")
#' - Its direct ancestor
#' - Its type (type), i.e. whether the taxid was
#' - Its type (type), i.e. whether the taxid was
#' requested by the user ("requested") or if it is the anecestor of a requested taxid ("ancestor")
#' - basemap : the basemap used to get taxa's details
#'
Expand All @@ -34,16 +34,16 @@
#' LM <- build_Lifemap(eukaryotes_80, "fr")
#' }
build_Lifemap <- function(df, basemap = c("ncbi", "base", "fr", "virus"), verbose = TRUE) {

basemap <- match.arg(arg = basemap, choices = basemap)
if (is.null(df$taxid)) {
stop('The dataframe must at least contain a "taxid" column')
}
# create a new "environment" to store the full data
if (!exists("lifemap_basemap_envir", where = .GlobalEnv)) {
if (!exists("lifemap_basemap_envir", where = .GlobalEnv)) {
lifemap_basemap_envir <- new.env()
}

## SET DATASETS ASDRESSES
# getting the right URL depending on the basemap wanted
if (basemap == "ncbi") {
Expand All @@ -59,7 +59,7 @@ build_Lifemap <- function(df, basemap = c("ncbi", "base", "fr", "virus"), verbos
else {
stop(sprintf('%s is not a working basemap, try c("base", "fr", "ncbi" or "virus")', basemap))
}

y <- tryCatch({
load(url(basemap_url), envir = lifemap_basemap_envir)
},
Expand All @@ -72,61 +72,61 @@ build_Lifemap <- function(df, basemap = c("ncbi", "base", "fr", "virus"), verbos
return(NA)
}
)

if (!is.na(y)) {
# download full data for chosen basemap
if (verbose) {
cat("Downloading basemap coordinates...\n")
}
load(url(basemap_url), envir = lifemap_basemap_envir)

# add LUCA
LUCA <- data.frame("taxid" = 0, "lon" = 0, "lat" = -4.226497, "sci_name" = "Luca", "zoom" = 5)
lifemap_basemap_envir$DF <- dplyr::bind_rows(lifemap_basemap_envir$DF, LUCA)

# get info for unique taxids (then we work with df_distinct, not df anymore)
df_distinct <- dplyr::distinct(df, .data$taxid, .keep_all = TRUE)
if (nrow(df_distinct) != nrow(df)) {
warning(sprintf("%s duplicated TaxIDs were removed \n", nrow(df) - nrow(df_distinct)))
warning(sprintf("%s duplicated TaxIDs were removed \n", nrow(df) - nrow(df_distinct)))
}

# get data
if (verbose) {
cat("Getting info for requested taxids...\n")
}

# get index of requested taxids
indexes <- fastmatch::fmatch(df_distinct$taxid, lifemap_basemap_envir$DF$taxid)
if (sum(is.na(indexes)) > 0) {
warning(sprintf("%s TaxID(s) could not be found: %s \n",
sum(is.na(indexes)),
paste(df_distinct$taxid[is.na(indexes)], sep = ",")))
warning(sprintf("%s TaxID(s) could not be found: %s \n",
sum(is.na(indexes)),
paste(df_distinct$taxid[is.na(indexes)], sep = ",")))
}

# create new df with only existing taxids
df_exists <- df_distinct[!is.na(indexes), ]
DATA0 <- lifemap_basemap_envir$DF[indexes[!is.na(indexes)], ]

# get ancestors
unique_ancestors <- unique(unlist(DATA0$ascend))
real_ancestors <- setdiff(unique_ancestors, df_exists$taxid)
ANCESTORS <- lifemap_basemap_envir$DF[fastmatch::fmatch(real_ancestors, lifemap_basemap_envir$DF$taxid), ]

# add type
DATA0$type <- "requested"
ANCESTORS$type <- "ancestor"
# bind all
DATA1 <- rbind(DATA0, ANCESTORS)
# merge

# merge
DATA2 <- merge(DATA1, df_exists, by = "taxid", all = TRUE)

# replace the column 'ascend' by simply the direct ancestor
DATA2$ancestor <- unlist(lapply(DATA2$ascend, function(x) ifelse(!is.null(x), x[1], NA)))

lm_obj <- list(df = DATA2, basemap = basemap)
class(lm_obj) <- c("lifemap_obj", "list")

return(lm_obj)
} else {
return(NA)
Expand Down
19 changes: 10 additions & 9 deletions R/create_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#'
#' create_matrix(LM_eukaryotes$df, c("GC.", "Genes"))
create_matrix <- function(df, cols) {
a <- sapply(1:nrow(df),
function(x,y) {
a <- sapply(seq_len(nrow(df)),
function(x, y) {
cbind(y$taxid[x], c(y$taxid[x], y$ascend[x][[1]]))
},
},
y = df)
a <- a[-length(a)]
B <- do.call(rbind, a)
Expand All @@ -30,7 +30,7 @@ create_matrix <- function(df, cols) {
new_df <- dplyr::full_join(new_df, df[, c("taxid", var)], by = dplyr::join_by("descendant" == "taxid"))
}
}
return (new_df)
return(new_df)
}

#' Infer numerical values to nodes.
Expand All @@ -49,9 +49,10 @@ create_matrix <- function(df, cols) {
#'
#' inferred_values <- pass_infos(M = infos, FUN = mean, value = "GC.")
pass_infos <- function(M, FUN, value) {
inferred_values <- tapply(M[[value]], M$ancestor, function(x) {
iv <- tapply(M[[value]], M$ancestor, function(x) {
x <- x[!is.na(x)]
FUN(x)})
return(iv)
}


Expand All @@ -74,15 +75,15 @@ pass_infos <- function(M, FUN, value) {
#'
#' inferred_values <- pass_infos_discret(M = infos, value = "Status")
pass_infos_discret <- function(M, value) {
bind_values <- M |>
bind_values <- M |>
dplyr::select(.data$ancestor, dplyr::all_of(value)) |>
stats::na.omit() |>
dplyr::group_by(.data[[value]], .data$ancestor) |>
dplyr::count() |>
dplyr::count() |>
tidyr::pivot_wider(names_from = dplyr::all_of(value), values_from = .data$n, values_fill = 0) |>
as.data.frame() |>
dplyr::rename("taxid" = "ancestor") |>
dplyr::rename("taxid" = "ancestor") |>
dplyr::arrange(.data$taxid)

return(bind_values)
}
13 changes: 6 additions & 7 deletions R/display_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@
#' display_map()
display_map <- function(df = NULL, basemap = c("fr", "ncbi", "base", "virus")) {
basemap <- match.arg(basemap)
if (basemap == "fr"){
if (basemap == "fr") {
display <- "http://lifemap-fr.univ-lyon1.fr/osm_tiles/{z}/{x}/{y}.png"
} else if (basemap == "ncbi"){
} else if (basemap == "ncbi") {
display <- "http://lifemap-ncbi.univ-lyon1.fr/osm_tiles/{z}/{x}/{y}.png"
} else if (basemap == "base"){
} else if (basemap == "base") {
display <- "http://lifemap.univ-lyon1.fr/osm_tiles/{z}/{x}/{y}.png"
} else if (basemap == "virus"){
} else if (basemap == "virus") {
display <- "https://virusmap.univ-lyon1.fr/osm_tiles/{z}/{x}/{y}.png"
}
url2check <- strsplit(display, "osm_tiles")[[1]][1]


m <- tryCatch({
leaflet::leaflet(df) |>
leaflet::addTiles(display, options = leaflet::providerTileOptions(minZoom = 5, maxZoom = 50))
Expand All @@ -43,7 +42,7 @@ display_map <- function(df = NULL, basemap = c("fr", "ncbi", "base", "virus")) {
return(NA)
}
)

if (!all(is.na(m))) {
return(m)
} else {
Expand Down
Loading

0 comments on commit 3523be2

Please sign in to comment.