Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Adafede committed Jul 22, 2023
1 parent 481b161 commit 44c283f
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 96 deletions.
7 changes: 4 additions & 3 deletions R/create_edges_parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ create_edges_parallel <- function(index,
FUN = create_edges_progress,
query = index,
s1 = cbind(mz = frags[[index]][, 1], intensity = frags[[index]][, 2]),
fragments = frags,
precursors = precs
)
frags = frags,
precs = precs
) |>
tidytable::bind_rows()
return(list)
}
40 changes: 20 additions & 20 deletions R/create_edges_progress.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library(MsCoreUtils)

utils::globalVariables(
c(
"params"
Expand All @@ -12,8 +14,8 @@ utils::globalVariables(
#' @param target Indices of target spectra
#' @param query Index of query spectra
#' @param s1 Query spectrum
#' @param fragments Fragments
#' @param precursors Precursors
#' @param frags Fragments
#' @param precs Precursors
#'
#' @return NULL
#'
Expand All @@ -23,30 +25,28 @@ utils::globalVariables(
create_edges_progress <- function(target,
query,
s1,
fragments,
precursors) {
frags,
precs) {
s2 <-
cbind(mz = fragments[[target]][, 1], intensity = fragments[[target]][, 2])
cbind(mz = frags[[target]][, 1], intensity = frags[[target]][, 2])
map <-
MsCoreUtils::join_gnps(
join_gnps(
x = s1[, 1],
y = s2[, 1],
xPrecursorMz = precursors[query],
yPrecursorMz = precursors[target],
xPrecursorMz = precs[query],
yPrecursorMz = precs[target],
tolerance = params$ms$tolerances$mass$dalton$ms2,
ppm = params$ms$tolerances$mass$ppm$ms2
)
score <- MsCoreUtils::gnps(s1[map$x, ], s2[map$y, ])
matched_peaks_count <-
length((map$x * map$y)[!is.na(map$x * map$y)])
presence_ratio <- matched_peaks_count / length(map$y)
return(
data.frame(
"feature_id" = query,
"target_id" = target,
"score" = score,
"matched_peaks_count" = matched_peaks_count,
"presence_ratio" = presence_ratio
matched_peaks_count <- length((map$x * map$y)[!is.na(map$x * map$y)])
presence_ratio <-
return(
data.frame(
"feature_id" = query,
"target_id" = target,
"score" = gnps(s1[map$x, ], s2[map$y, ]),
"matched_peaks_count" = matched_peaks_count,
"presence_ratio" = matched_peaks_count / length(map$y)
)
)
)
}
23 changes: 9 additions & 14 deletions R/create_edges_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,26 @@ create_edges_spectra <- function(
Spectra::addProcessing(normalize_peaks()) |>
Spectra::applyProcessing()

log_debug("Extracting fragments and precursors...")
fragments_norm <- spectra@backend@peaksData
precursors <- spectra$precursorMz
nspe <- length(spectra)

log_debug("Performing spectral comparison")
log_debug(
"As we do not bin the spectra,
nor limit the precursors delta,
expect a long processing time."
)
log_debug("Take yourself a break, you deserve it.")

nspecz <- length(spectra)
precz <- spectra$precursorMz
fragz <- spectra@backend@peaksData
## Originally written with future but too slow...TODO investigate
matches_sim <- pbmcapply::pbmclapply(
X = 1:(nspe - 1),
edges <- pbmcapply::pbmclapply(
X = 1:(nspecz - 1),
mc.cores = parallelly::availableCores(),
ignore.interactive = TRUE,
FUN = create_edges_parallel,
frags = fragments_norm,
precs = precursors,
nspecs = nspe
)

edges <- matches_sim |>
frags = fragz,
precs = precz,
nspecs = nspecz
) |>
tidytable::bind_rows()

## old version, keep in case
Expand Down
Loading

0 comments on commit 44c283f

Please sign in to comment.