Skip to content

Commit

Permalink
improve code redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
Adafede committed Jul 7, 2023
1 parent 6948c1a commit b3c0ce0
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 94 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Collate:
'annotate_spectra.R'
'clean_bio.R'
'clean_chemo.R'
'clean_collapse.R'
'complement_metadata_structures.R'
'create_adducts_neg.R'
'create_adducts_pos.R'
Expand All @@ -86,6 +87,7 @@ Collate:
'export_spectra.R'
'export_spectra_2.R'
'extract_spectra.R'
'get_example_sirius.R'
'get_file.R'
'get_gnps_tables.R'
'get_last_version_from_zenodo.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(annotate_masses)
export(annotate_spectra)
export(clean_bio)
export(clean_chemo)
export(clean_collapse)
export(complement_metadata_structures)
export(create_adducts_neg)
export(create_adducts_pos)
Expand All @@ -22,6 +23,7 @@ export(export_params)
export(export_spectra)
export(export_spectra_2)
export(extract_spectra)
export(get_example_sirius)
export(get_file)
export(get_gnps_tables)
export(get_last_version_from_zenodo)
Expand Down
24 changes: 24 additions & 0 deletions R/clean_collapse.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' @title Clean collapse
#'
#' @description This function collapses a grouped dataframe and trims it
#'
#' @param grouped_df Grouped dataframe
#'
#' @return NULL
#'
#' @export
#'
#' @examples NULL
clean_collapse <- function(grouped_df) {
clean_collapse_df <- grouped_df |>
dplyr::summarize(dplyr::across(
dplyr::everything(),
.fns = function(x) {
x <- list(paste(unique(x[!is.na(x)]), collapse = " $ "))
}
)) |>
dplyr::ungroup() |>
dplyr::mutate(dplyr::across(dplyr::everything(), trimws))

return(clean_collapse_df)
}
25 changes: 25 additions & 0 deletions R/get_example_sirius.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' @title Get example sirius
#'
#' @description This function gets example SIRIUS annotations
#'
#' @param url URL where the example is accessible
#' @param export Path where to save the example
#'
#' @return NULL
#'
#' @export
#'
#' @examples NULL
get_example_sirius <-
function(url = paths$urls$examples$sirius,
export = paths$data$interim$annotations$example_sirius) {
get_file(
url = url,
export = export
)
message("Unzipping")
utils::unzip(
zipfile = export,
exdir = dirname(export)
)
}
12 changes: 2 additions & 10 deletions R/split_tables_sop.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ split_tables_sop <- function(table) {
structure_inchikey,
structure_smiles
) |>
dplyr::summarize(dplyr::across(dplyr::everything(), .fns = function(x) {
x <- list(paste(unique(x[!is.na(x)]), collapse = " $ "))
})) |>
dplyr::ungroup() |>
dplyr::mutate(dplyr::across(dplyr::everything(), trimws))
clean_collapse()

table_structures_taxonomy_npc <- table |>
dplyr::filter(!is.na(structure_smiles_2D)) |>
Expand All @@ -145,11 +141,7 @@ split_tables_sop <- function(table) {
) |>
tidytable::distinct() |>
dplyr::group_by(structure_smiles_2D) |>
dplyr::summarize(dplyr::across(dplyr::everything(), .fns = function(x) {
x <- list(paste(unique(x[!is.na(x)]), collapse = " $ "))
})) |>
dplyr::ungroup() |>
dplyr::mutate(dplyr::across(dplyr::everything(), trimws)) |>
clean_collapse() |>
dplyr::mutate(dplyr::across(
dplyr::matches("taxonomy.*_0"),
.fns = function(x) {
Expand Down
70 changes: 6 additions & 64 deletions R/weight_annotations.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,81 +204,23 @@ weight_annotations <-

log_debug(x = "performing taxonomically informed scoring")
annotation_table_weighted_bio <-
weight_bio(
annotationTable = annotation_table_taxed,
structureOrganismPairsTable = structure_organism_pairs_table,
weightSpectral = weight_spectral,
weightBiological = weight_biological,
scoreBiologicalDomain = score_biological_domain,
scoreBiologicalKingdom = score_biological_kingdom,
scoreBiologicalPhylum = score_biological_phylum,
scoreBiologicalClass = score_biological_class,
scoreBiologicalOrder = score_biological_order,
scoreBiologicalFamily = score_biological_family,
scoreBiologicalTribe = score_biological_tribe,
scoreBiologicalGenus = score_biological_genus,
scoreBiologicalSpecies = score_biological_species,
scoreBiologicalVariety = score_biological_variety
)
weight_bio()

annotation_table_weighted_bio |>
decorate_bio(
sc_kin = score_biological_kingdom,
sc_phy = score_biological_phylum,
sc_cla = score_biological_class,
sc_ord = score_biological_order,
sc_fam = score_biological_family,
sc_gen = score_biological_genus,
sc_spe = score_biological_species,
sc_var = score_biological_variety
)
decorate_bio()

log_debug(x = "cleaning taxonomically informed results and preparing for chemically informed scoring")
annotation_table_weighted_bio_cleaned <- clean_bio(
annotationTableWeightedBio = annotation_table_weighted_bio,
edgesTable = edges_table,
candidatesInitial = candidates_initial,
minimalMs1Bio = minimal_ms1_bio
)
annotation_table_weighted_bio_cleaned <- clean_bio()

log_debug(x = "performing chemically informed scoring")
annotation_table_weighted_chemo <-
weight_chemo(
annotationTableWeightedBioCleaned = annotation_table_weighted_bio_cleaned,
weightSpectral = weight_spectral,
weightBiological = weight_biological,
weightChemical = weight_chemical,
scoreChemicalClaKingdom = score_chemical_cla_kingdom,
scoreChemicalClaSuperclass = score_chemical_cla_superclass,
scoreChemicalClaClass = score_chemical_cla_class,
scoreChemicalClaParent = score_chemical_cla_parent,
scoreChemicalNpcPathway = score_chemical_npc_pathway,
scoreChemicalNpcSuperclass = score_chemical_npc_superclass,
scoreChemicalNpcClass = score_chemical_npc_class
)
weight_chemo()

annotation_table_weighted_chemo |>
decorate_chemo(
sc_cla_kin = score_chemical_cla_kingdom,
sc_cla_sup = score_chemical_cla_superclass,
sc_cla_cla = score_chemical_cla_class,
sc_cla_par = score_chemical_cla_parent,
sc_npc_pat = score_chemical_npc_pathway,
sc_npc_sup = score_chemical_npc_superclass,
sc_npc_cla = score_chemical_npc_class
)
decorate_chemo()

log_debug(x = "cleaning for export")
results <- clean_chemo(
annotationTableWeightedChemo = annotation_table_weighted_chemo,
componentsTable = components_table,
featuresTable = features_table,
structureOrganismPairsTable = structure_organism_pairs_table,
candidatesFinal = candidates_final,
minimalMs1Bio = minimal_ms1_bio,
minimalMs1Chemo = minimal_ms1_chemo,
summarize = summarise
)
results <- clean_chemo()

log_debug(x = "Exporting ...")
time <- format(Sys.time(), "%y%m%d_%H%M%OS")
Expand Down
10 changes: 1 addition & 9 deletions inst/scripts/get_example_sirius.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ log_debug(
log_debug("Authors: ", crayon::green("AR"), "\n")
log_debug("Contributors: ...")

get_file(
url = paths$urls$examples$sirius,
export = paths$data$interim$annotations$example_sirius
)
message("Unzipping")
utils::unzip(
zipfile = paths$data$interim$annotations$example_sirius,
exdir = dirname(paths$data$interim$annotations$example_sirius)
)
get_example_sirius()

end <- Sys.time()

Expand Down
17 changes: 17 additions & 0 deletions man/clean_collapse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/get_example_sirius.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions tests/testthat/test_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,7 @@ testthat::test_that("Whole process", {

#### SIRIUS
## mini version for tests
sirius_mini <- paths$data$interim$annotations$example_sirius |>
gsub(pattern = ".zip", replacement = "_mini.zip")
get_file(
url = paths$urls$examples$sirius,
export = paths$data$interim$annotations$example_sirius
)
message("Unzipping")
utils::unzip(
zipfile = paths$data$interim$annotations$example_sirius,
exdir = dirname(paths$data$interim$annotations$example_sirius)
)
get_example_sirius()

### Libraries
#### ECMDB
Expand Down

0 comments on commit b3c0ce0

Please sign in to comment.