Skip to content

Commit

Permalink
Started adding documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hechth committed Jul 29, 2024
1 parent 9fa9225 commit 1dfb73f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Generated by roxygen2: do not edit by hand

export(adaptive.bin)
export(add_feature_ids)
export(adjust.time)
export(aggregate_by_rt)
export(as_feature_sample_table)
export(bigauss.esti)
export(bigauss.esti.EM)
export(bigauss.mix)
export(check_files)
export(clean_data_matrix)
export(comb)
export(compute_boundaries)
Expand Down Expand Up @@ -41,6 +44,7 @@ export(compute_template)
export(compute_template_adjusted_rt)
export(compute_uniq_grp)
export(correct_time)
export(correct_time_v2)
export(count_peaks)
export(create_aligned_feature_table)
export(create_features_from_cluster)
Expand All @@ -63,6 +67,7 @@ export(get_features_in_rt_range)
export(get_mzrange_bound_indices)
export(get_num_workers)
export(get_rt_region_indices)
export(get_sample_name)
export(get_single_occurrence_mask)
export(get_times_to_use)
export(hybrid)
Expand Down
33 changes: 27 additions & 6 deletions R/adjust.time.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
NULL
#> NULL

#' Combine template and sample features
#' @param template_features Tibble Template feature table (mz, rt, cluster, sample_id).
#' @param features Tibble Sample feature table (mz, rt, cluster, sample_id).
#' @return Tibble Combined feature table (rbind).
#' @export
compute_comb <- function(template_features, features) {
combined <- dplyr::bind_rows(
Expand All @@ -11,6 +15,12 @@ compute_comb <- function(template_features, features) {
return(combined)
}

#' Select features to use for retention time alignment
#' @description This function selects features present in both the sample
#' feature table and template feature table given they have the same cluster,
#' are adjacent in the combined table.
#' @param combined Tibble Table with (mz, rt, cluster, sample_id).
#' @return List of bool Returns list of bools with TRUE at each index where this condition is met.
#' @export
compute_sel <- function(combined) {
l <- nrow(combined)
Expand All @@ -19,6 +29,11 @@ compute_sel <- function(combined) {
return(sel)
}

#' Create two column table with paired sample and template retention times.
#' @param combined Tibble Table with features from sample and template.
#' @param sel list of bools List of bools indiciating which features to pair.
#' See 'compute_sel'.
#' @param j string Template sample_id.
#' @export
compute_template_adjusted_rt <- function(combined, sel, j) {
all_features <- cbind(combined$rt[sel], combined$rt[sel + 1])
Expand Down Expand Up @@ -59,29 +74,34 @@ compute_corrected_features_v2 <- function(features, template_rt, delta_rt) {
return(features |> dplyr::arrange_at(c("mz", "rt")))
}

#' Correct the rt in feature table based on paired feature rts and differences.
#' @param features Tibble The feature table for which to correct rts.
#' @param template_rt List of floats Template retention times for the paired features.
#' @param delta_rt List of floats Differences between the paired rts.
#' @return Tibble A table with corrected retention times.
#' @export
compute_corrected_features <- function(features, delta_rt, avg_time) {
compute_corrected_features <- function(features, template_rt, delta_rt) {
features <- features |> dplyr::arrange_at(c("rt", "mz"))

corrected <- features$rt
original <- features$rt

idx <- dplyr::between(original, min(delta_rt), max(delta_rt))
idx <- dplyr::between(original, min(template_rt), max(template_rt))
to_correct <- original[idx]
this.smooth <- ksmooth(
template_rt,
delta_rt,
avg_time,
kernel = "normal",
bandwidth = (max(delta_rt) - min(delta_rt)) / 5,
bandwidth = (max(template_rt) - min(template_rt)) / 5,
x.points = to_correct
)

corrected[idx] <- this.smooth$y + to_correct
lower_bound_adjustment <- mean(this.smooth$y[this.smooth$x == min(this.smooth$x)])
upper_bound_adjustment <- mean(this.smooth$y[this.smooth$x == max(this.smooth$x)])

idx_lower <- original < min(delta_rt)
idx_upper <- original > max(delta_rt)
idx_lower <- original < min(template_rt)
idx_upper <- original > max(template_rt)

corrected[idx_lower] <- corrected[idx_lower] + lower_bound_adjustment
corrected[idx_upper] <- corrected[idx_upper] + upper_bound_adjustment
Expand Down Expand Up @@ -149,6 +169,7 @@ compute_template <- function(extracted_features) {
return(tibble::as_tibble(template_features))
}

#' @export
correct_time_v2 <- function(features, template) {
if (unique(features$sample_id) == unique(template$sample_id))
return(tibble::as_tibble(features))
Expand Down
14 changes: 14 additions & 0 deletions R/unsupervised.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
NULL
#> NULL

#' Read the metadata table, retention time data matrix and intensity data matrix
#' and combine them into a single table
#' @param metadata Tibble Feature metadata table with information concerning the peaks.
#' @param rt_crosstab Tibble Data matrix with features on rows and samples on columns holding rt data.
#' @param int_crosstab Tibble Data matrix with features on rows and samples on columns holding intensity data.
#' @return Tibble A merged table containing all information.
#' @export
as_feature_sample_table <- function(metadata, rt_crosstab, int_crosstab) {
feature_names <- as.character(rt_crosstab$id)
sample_names <- colnames(metadata)[-c(1:8)]
Expand All @@ -27,6 +34,9 @@ as_feature_sample_table <- function(metadata, rt_crosstab, int_crosstab) {
return(data)
}

#' Check files whether they exist.
#' @param filenames list of filenames Filenames to check whether they exist.
#' @export
check_files <- function(filenames) {
missing <- !file.exists(filenames)
missing_filenames <- paste0('\t', filenames[missing], collapse = '\n')
Expand All @@ -36,6 +46,10 @@ check_files <- function(filenames) {
}
}

#' Get the sample name as basename of the file.
#' @param filename string Name of the file.
#' @return string Sample name.
#' @export
get_sample_name <- function(filename) {
tools::file_path_sans_ext(basename(filename))
}
Expand Down
5 changes: 4 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ register_functions_to_cluster <- function(cluster) {
'get_mzrange_bound_indices',
'compute_mass_density',
'l2normalize',
'compute_peaks_and_valleys'
'compute_peaks_and_valleys',
'as_feature_sample_table',
'check_files',
'get_sample_name'
))
snow::clusterEvalQ(cluster, library("dplyr"))
snow::clusterEvalQ(cluster, library("stringr"))
Expand Down

0 comments on commit 1dfb73f

Please sign in to comment.