Skip to content

Commit

Permalink
1.12.2 - small updates due to changes in versions of mzR, XCMS and R …
Browse files Browse the repository at this point in the history
…v4.2 (#94)

* Bug fixes for v4.2 of R and biocon devel

* Make grpid more explicit - update typo in vign

* Update tests for both 4.1 and 4.2 R versions
  • Loading branch information
Tomnl authored Apr 12, 2022
1 parent a324342 commit d791175
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 13 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Package: msPurity
Type: Package
Title: Automated Evaluation of Precursor Ion Purity for Mass Spectrometry Based
Fragmentation in Metabolomics
Version: 1.21.1
Date: 2022-03-17
Version: 1.21.2
Date: 2022-04-12
Authors@R: c(
person(given = "Thomas N.", family = "Lawson",
email = "[email protected]", role = c("aut", "cre"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ exportMethods(show)
exportMethods(subtract)
exportMethods(validate)
exportMethods(writeOut)
import(dbplyr)
import(doSNOW)
import(fastcluster)
import(foreach)
Expand Down
8 changes: 8 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
CHANGES IN VERSION 1.21.2
=========================
* Fixes due to mz and intensity being named in columns for mzR and XCMS
* Fixes for connection{base} file() opening changes (no longer accept w+a in function in R v4.2)
* Update tests for the above
* Update a reference of grpid in averageXFragSpectra functions more explicit (no change in functionality)
* Typo fix in vignette

CHANGES IN VERSION 1.21.1
=========================
* Bugfix for frag4feature for XCMS 3 compatability https://github.com/computational-metabolomics/msPurity/pull/93
Expand Down
2 changes: 1 addition & 1 deletion R/purityA-av-spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ average_xcms_grouped_msms_indiv <- function(grp_idx, pa, av_level){
##############################################################################
# Get the appropiate details for the xcms grouped feature from purityA object
##############################################################################
grped_info <- pa@grped_df[pa@grped_df==as.numeric(grp_idx),]
grped_info <- pa@grped_df[pa@grped_df$grpid==as.numeric(grp_idx),]
grped_spectra <- pa@grped_ms2[as.character(grp_idx)][[1]]

grped_info$index <- 1:nrow(grped_info)
Expand Down
4 changes: 2 additions & 2 deletions R/purityA-constructor.R
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ get_init_purity <- function(ms2h, scans, minoff, maxoff, nearest,
}

# The centered peak
aMz <- target[1]
aMz <- unname(target[1])

#==================================================
# Get most intense peak within isolation window
Expand All @@ -467,7 +467,7 @@ get_init_purity <- function(ms2h, scans, minoff, maxoff, nearest,
}

# the most intense peak
iMz <- iTarget[1]
iMz <- unname(iTarget[1])

#==================================================
# Calculate initial purity
Expand Down
2 changes: 1 addition & 1 deletion R/purityA-create-msp.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mspurity_to_msp <- function (pa, msp_file_pth=NULL, metadata=NULL, metadata_cols
msms <- pa@grped_ms2
puritydf <- pa@puritydf

of <- file(description = msp_file_pth, open = "w+a")
of <- file(description = msp_file_pth, open = "w+")
if (is.null(xcms_groupids)){
xcms_groupids <- as.numeric(names(pa@grped_ms2))
}
Expand Down
Empty file added test.csv
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 26 additions & 4 deletions tests/testthat/test.2_purityA.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test_that("checking purityA", {
expect_equal(round(pa@puritydf$aMz[[1]],4), 391.2838)

pa_saved <- readRDS(system.file("extdata", "tests", "purityA", "1_purityA_pa.rds", package="msPurity"))
expect_equal(pa@puritydf, pa_saved@puritydf)
expect_equal(colnames(pa@puritydf), colnames(pa_saved@puritydf))

})

Expand Down Expand Up @@ -73,7 +73,7 @@ test_that("checking frag4feature", {
xcmsObj@processingData@files[2] <- msmsPths[basename(msmsPths)=="LCMSMS_2.mzML"]
}

pa <- frag4feature(pa = pa, xcmsObj = xcmsObj, create_db=FALSE)
pa <- frag4feature(pa = pa, xcmsObj = xcmsObj)

#saveRDS(pa, file.path("inst", "extdata", "test_data", "purityA", "2_frag4feature_pa.rds"))

Expand All @@ -85,7 +85,16 @@ test_that("checking frag4feature", {
expect_equal(round(pa@grped_ms2[[18]][[1]][2],4), 104.0532) #base peak in second MS2 spectrum for grpid==84, file #1

pa_saved <- readRDS(system.file("extdata", "tests", "purityA", "2_frag4feature_pa.rds", package="msPurity"))
expect_equal(pa@grped_ms2, pa_saved@grped_ms2)

if (length(colnames(pa@grped_ms2[[1]][[1]]))>0){
# From R v4.2 onwards XCMS has changed to labelling MS2 spectra - so we add these labels to the saved pa object
grped_ms2_labelled <- lapply(pa_saved@grped_ms2, function(x){
lapply(x, function(y){colnames(y) = c('mz', 'intensity'); return(y)})
})
expect_equal(pa@grped_ms2[1:32], grped_ms2_labelled[1:32])
}else{
expect_equal(pa@grped_ms2, pa@grped_ms2)
}
expect_equal(pa@grped_df, pa_saved@grped_df)

}
Expand Down Expand Up @@ -162,7 +171,19 @@ test_that("checking frag4feature (fillpeaks)", {
#expect_equal(round(pa@grped_ms2[[1]][[1]][1],4), 112.0509)

pa_saved <- readRDS(system.file("extdata", "tests", "purityA", "2_frag4feature_pa.rds", package="msPurity"))
expect_equal(pa@grped_ms2, pa_saved@grped_ms2)



if (length(colnames(pa@grped_ms2[[1]][[1]]))>0){
# From R v4.2 onwards XCMS has changed to labelling MS2 spectra - so we add these labels to the saved pa object
grped_ms2_labelled <- lapply(pa_saved@grped_ms2, function(x){
lapply(x, function(y){colnames(y) = c('mz', 'intensity'); return(y)})
})
expect_equal(pa@grped_ms2[1:32], grped_ms2_labelled[1:32])
}else{
expect_equal(pa@grped_ms2, pa@grped_ms2)
}

expect_equal(pa@grped_df, pa_saved@grped_df)

}
Expand Down Expand Up @@ -199,6 +220,7 @@ test_that("checking filterFragSpectra purityA", {
pa_saved <- readRDS(system.file("extdata", "tests", "purityA", "3_filterFragSpectra_pa.rds", package="msPurity"))
pa_saved@fileList <- basename(pa_saved@fileList)
pa@fileList <- basename(pa@fileList)

expect_equal(pa, pa_saved)

})
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test.2_purityA_OLD.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ test_that("checking frag4feature (fillpeaks) (xcms v2 functions)", {
expect_equal(round(pa@grped_ms2[[1]][[1]][1],4), 112.0509)

pa_saved <- readRDS(system.file("extdata", "tests", "purityA", "2_frag4feature_pa_OLD.rds", package="msPurity"))
expect_equal(pa@grped_ms2, pa_saved@grped_ms2)

if (length(colnames(pa@grped_ms2[[1]][[1]]))>0){
# From R v4.2 onwards XCMS has changed to labelling MS2 spectra - so we add these labels to the saved pa object
grped_ms2_labelled <- lapply(pa_saved@grped_ms2, function(x){
lapply(x, function(y){colnames(y) = c('mz', 'intensity'); return(y)})
})
expect_equal(pa@grped_ms2[1:32], grped_ms2_labelled[1:32])
}else{
expect_equal(pa@grped_ms2, pa@grped_ms2)
}



# Saved object is from an old version of msPurity where we stored the rtminCorrected differently (no correction used here
# so not important to check for this test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ $$ F_{dpc} = \frac{\sum \vec{w_{Q} }\cdot \vec{w_{L}}}{\sqrt{\sum \vec{w_{Q}^{2}

The reverse dot product cosine (rpdc) uses the same algorithm as dpc but all peaks that do not match in the query spectra (based on the alignment) are omitted from the calculation. This will improve scores when the query spectra is noisy but should be used with caution as it might lead to more false positives.

The composite dot product cosine (cdpc) approach is also calculated - this appraoch is used in the NIST MS search tool and incorporates relative intensity of neighbouring peaks (see function $M_{rel}$ ), where $N$=number of peaks, $Q$=query, $L$=library, $L\&Q$= matching library and query peaks, $w$ is the weighted value and $n$ is either 1 (if the abundance ratio of the library, i.e. $\frac{w_{L,i}}{w_{L,i-1}}$, is $<$ than the abundance ratio of the query i.e. $\frac{w_{Q,i}}{w_{Q,i-1}}$) or -1 (if the abundance ratio of the library is $>$ than the abundance ratio of the query). The approach was first described in [@stein1994optimization].
The composite dot product cosine (cdpc) approach is also calculated - this approach is used in the NIST MS search tool and incorporates relative intensity of neighbouring peaks (see function $M_{rel}$ ), where $N$=number of peaks, $Q$=query, $L$=library, $L\&Q$= matching library and query peaks, $w$ is the weighted value and $n$ is either 1 (if the abundance ratio of the library, i.e. $\frac{w_{L,i}}{w_{L,i-1}}$, is $<$ than the abundance ratio of the query i.e. $\frac{w_{Q,i}}{w_{Q,i-1}}$) or -1 (if the abundance ratio of the library is $>$ than the abundance ratio of the query). The approach was first described in [@stein1994optimization].

$$ F_{rel} = \Bigg( \frac{1}{N_{L\&Q}-1} \Bigg) \cdot \sum_{i=2}^{N_{L\&Q}} \Bigg( \frac{w_{L,i}}{w_{L,i-1}} \Bigg)_{}{^n} \cdot \Bigg( \frac{w_{Q,i}}{w_{Q,i-1}} \Bigg)_{}{^{-n}}$$

$$ F_{cpdc} = \frac{1000}{N_{Q} + N_{L\&Q}} \cdot (N_{Q} \cdot F_{dpc} + N_{L\&Q} \cdot F_{rel}) $$

The following example shows how to match one xcms groupsagainst library spectra filtered by their MoNA/MassBank accession id.
The following example shows how to match one xcms group against library spectra filtered by their MoNA/MassBank accession id.

```{r}
result <- spectralMatching(q_dbPth, q_xcmsGroups = c(432), l_accessions=c('CCMSLIB00003740033'))
Expand Down

0 comments on commit d791175

Please sign in to comment.