Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jasenfinch/mzAnnotation
Browse files Browse the repository at this point in the history
  • Loading branch information
jasenfinch committed Apr 29, 2021
2 parents 5913cc6 + 8806aee commit 8efd211
Show file tree
Hide file tree
Showing 50 changed files with 177 additions and 168 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
# - {os: windows-latest, r: 'release'}
# - {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}

Expand Down
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: mzAnnotation
Title: Signal Annotation Tools for High Resolution Metabolomics
Version: 1.7.3
Version: 1.7.4
Authors@R:
person(given = "Jasen",
family = "Finch",
Expand Down Expand Up @@ -39,7 +39,6 @@ Collate: allGenerics.R
calcAdducts.R
calcM.R
calcMZ.R
calculateMs.R
checks.R
convert.R
data.R
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ importFrom(dplyr,contains)
importFrom(dplyr,everything)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,inner_join)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,rename)
Expand All @@ -62,6 +63,7 @@ importFrom(purrr,map_chr)
importFrom(purrr,map_dbl)
importFrom(purrr,map_df)
importFrom(purrr,map_int)
importFrom(stats,setNames)
importFrom(stringr,str_c)
importFrom(stringr,str_extract)
importFrom(stringr,str_replace)
Expand All @@ -71,7 +73,10 @@ importFrom(tibble,as_tibble)
importFrom(tibble,deframe)
importFrom(tibble,rowid_to_column)
importFrom(tibble,tibble)
importFrom(tidyr,drop_na)
importFrom(tidyr,expand_grid)
importFrom(tidyr,gather)
importFrom(tidyr,spread)
importFrom(utils,combn)
importFrom(utils,getFromNamespace)
useDynLib(mzAnnotation)
22 changes: 0 additions & 22 deletions R/calculateMs.R

This file was deleted.

2 changes: 1 addition & 1 deletion R/ionisationProducts.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param adductTable table of adduct rules. Defaults to adducts()
#' @examples
#' ionisationProducts(aminoAcids$SMILES[1])
#' @importFrom dplyr ungroup
#' @importFrom dplyr ungroup rowwise
#' @export

ionisationProducts <- function(SMILES,adductTable = adducts()){
Expand Down
3 changes: 2 additions & 1 deletion R/mzAnnotation.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ globalVariables(c('Name','ID','MF','Accurate Mass','Smile 1','Adduct',
'Theoretical m/z','True','desc','m/z','Adducts',
'Negative_Charge','Positive_Charge','SMILES','Total_Charge',
'NHH','COO','ACCESSION_ID','Accurate_Mass','Measured m/z',
'HBA1','TPSA','.','Possible','Rule','x'
'HBA1','TPSA','.','Possible','Rule','x','Transformation','ID1',
'ID2','M','M1','M2','m/z1','m/z2','fill'
))
142 changes: 97 additions & 45 deletions R/relationshipCalculator.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,113 @@
#' @description adduct, isotope and biotransfromation calculation.
#' @param mz numeric \code{vector} of accurate m/z
#' @param limit limit of deviation for thresholding associations. Defaults to 0.001
#' @param modes character vector of ionisation modes of input mz
#' @param adducts character vector of adducts to use. If \code{NULL} all available adducts will be used. If \code{modes} is not \code{NULL}, this should be named list, the names denoting the adduct aquisition modes.
#' @param adducts character vector of adducts names to use
#' @param isotopes character vector of isotopes to use.
#' @param transformations character vector of transformations to use
#' @param adductTable table containing adduct formation rules. Defaults to \code{\link{adducts}()}.
#' @param isotopeTable table containing isotope rules. Defaults to \code{\link{isotopes}()}.
#' @param transformationTable table containing transformation rules. Defaults to \code{\link{transformations}()}.
#' @examples
#' relationshipCalculator(c(132.03023,168.00691))
#' @author Jasen Finch
#' @export
#' @importFrom utils combn
#' @importFrom dplyr left_join contains
#' @importFrom dplyr left_join contains inner_join
#' @importFrom stringr str_c
#' @examples
#' relationshipCalculator(c(132.03023,168.00691))
#'
#' ## with modes declared
#' relationshipCalculator(c(132.03023,172.00067),
#' modes = c('n','p'),
#' adducts = list(n = c("[M-H]1-","[M+Cl]1-","[M+K-2H]1-"),
#' p = c('[M+H]1+','[M+K]1+','[M+Na]1+')))
#' @importFrom tidyr expand_grid spread drop_na
#' @importFrom tibble rowid_to_column tibble
#' @importFrom stats setNames

relationshipCalculator <- function(mz, limit = 0.001, adducts = c("[M-H]1-","[M+Cl]1-","[M+K-2H]1-"), isotopes = NA, transformations = NA, adductTable = adducts(), isotopeTable = isotopes(), transformationTable = transformations()){

Ms <- expand_grid(`m/z` = mz,
Adduct = adducts,
Isotope = isotopes,
Transformation = transformations) %>%
mutate(M = calculateMs(`m/z`,Adduct,Isotope,Transformation)) %>%
rowid_to_column(var = 'ID')

relationships <- expand_grid(ID1 = Ms$ID,
ID2 = Ms$ID) %>%
filter(ID1 != ID2) %>%
left_join(Ms %>%
select(ID1 = ID,M1 = M),
by = 'ID1') %>%
left_join(Ms %>%
select(ID2 = ID,M2 = M),
by = 'ID2') %>%
mutate(Error = abs(M1 - M2)) %>%
filter(Error <= limit) %>%
left_join(Ms %>%
setNames(str_c(names(.),'1')),
by = c("ID1", "M1")) %>%
left_join(Ms %>%
setNames(str_c(names(.),'2')),
by = c("ID2", "M2")) %>%
filter(`m/z1` != `m/z2`) %>%
select(contains('ID'),
contains('m/z'),
contains('Adduct'),
contains('Isotope'),
contains('Transformation'),Error)

unique_rel <- relationships %>%
select(contains('ID')) %>%
mutate(fill = 1) %>%
spread(ID2,fill) %>%
{
id1 <- select(., ID1)
. <- select(.,-ID1)

.[lower.tri(.)] <- NA
. <- bind_cols(.,id1)
.
} %>%
gather(ID2,fill,-ID1) %>%
drop_na() %>%
select(-fill) %>%
mutate(ID2 = as.numeric(ID2))

relationships <- relationships %>%
inner_join(unique_rel,
by = c("ID1", "ID2")) %>%
select(-contains('ID'))

return(relationships)
}

relationshipCalculator <- function(mz, limit = 0.001, modes = NULL, adducts = c("[M-H]1-","[M+Cl]1-","[M+K-2H]1-"), isotopes = NULL, transformations = NULL, adductTable = adducts(), isotopeTable = isotopes(), transformationTable = transformations()){
if (is.null(adducts)) {
adducts <- adductTable$Name
}
if (!is.null(modes) & is.list(adducts)) {
A1 <- adducts[[modes[1]]]
A2 <- adducts[[modes[2]]]
} else {
A1 <- adducts
A2 <- adducts
calculateMs <- function(mzs, adducts, isotopes, transformations, adductTable = adducts(), isotopeTable = isotopes(), transformationTable = transformations()){

if (!identical(length(mzs),length(adducts),length(isotopes),length(transformations))){
stop('Arguments mzs, adducts, isotopes, transformations should be vectors of the same length',
call. = FALSE)
}
isotopes <- c('NA',isotopes)
transformations <- c('NA',transformations)

combinations <- combn(mz,2)

combinations <- apply(combinations,2,function(x){
M1 <- calculateMs(x[1],A1,isotopes,transformations,adductTable,isotopeTable,transformationTable)
M2 <- calculateMs(x[2],A2,isotopes,transformations,adductTable,isotopeTable,transformationTable)

coms <- expand.grid(M1$ID,M2$ID)
colnames(coms) <- c('ID1','ID2')
coms <- left_join(coms,M1,by = c('ID1' = 'ID'))
colnames(coms)[3:ncol(coms)] <- str_c(colnames(coms)[3:ncol(coms)],'1')
coms <- left_join(coms,M2,by = c('ID2' = 'ID'))
colnames(coms)[8:ncol(coms)] <- str_c(colnames(coms)[8:ncol(coms)],'2')

coms <- mutate(coms,Error = abs(M1 - M2)) %>%
filter(Error <= limit) %>%
select(contains('m/z'),contains('Adduct'),contains('Isotope'),contains('Transformation'),Error)
return(coms)
})
combinations <- bind_rows(combinations) %>%
as_tibble()
return(combinations)

addRules <- adducts %>%
tibble(Adduct = .) %>%
left_join(adductTable ,
by = c('Adduct' = 'Name'))

isoRules <- isotopes %>%
tibble(Isotope = .) %>%
left_join(isotopeTable, by = "Isotope") %>%
{
.$`Mass Difference`[is.na(.$`Mass Difference`)] <- 0
.
}

transRules <- transformations %>%
tibble(Transformation = .) %>%
left_join(transformationTable, by = c("Transformation" = "MF Change")) %>%
{
.$Difference[is.na(.$Difference)] <- 0
.
}

M <- ((mzs - addRules$Add) * addRules$Charge)
M <- (M - isoRules$`Mass Difference`)
M <- M / addRules$xM
M <- M - transRules$Difference

return(round(M,5))
}
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ res <- relationshipCalculator(c(132.03023,168.00691))

res
#> # A tibble: 1 x 9
#> `m/z1` `m/z2` Adduct1 Adduct2 Isotope1 Isotope2 Transformation1
#> <dbl> <dbl> <chr> <chr> <lgl> <lgl> <lgl>
#> 1 132. 168. [M-H]1- [M+Cl] NA NA NA
#> `m/z1` `m/z2` Adduct1 Adduct2 Isotope1 Isotope2 Transformation1
#> <dbl> <dbl> <chr> <chr> <lgl> <lgl> <lgl>
#> 1 132. 168. [M-H]1- [M+Cl]1- NA NA NA
#> # … with 2 more variables: Transformation2 <lgl>, Error <dbl>
```

Expand Down Expand Up @@ -82,9 +82,9 @@ res <- PIPsearch(db = db,
isotope = NA)
res
#> # A tibble: 1 x 12
#> ID NAME InChI InChIKey SMILES MF Accurate_Mass Isotope Adduct
#> <int> <chr> <chr> <chr> <chr> <chr> <dbl> <lgl> <chr>
#> 1 4 L-As… InCh… CKLJMWT… C([C@… C4H7… 133. NA [M-H]…
#> # … with 3 more variables: `Measured m/z` <dbl>, `Theoretical m/z` <dbl>, `PPM
#> # Error` <dbl>
#> ID NAME InChI InChIKey SMILES MF Accurate_Mass Isotope Adduct
#> <int> <chr> <chr> <chr> <chr> <chr> <dbl> <lgl> <chr>
#> 1 4 L-Asp… InChI=1S/C… CKLJMWTZI… C([C@@… C4H7… 133. NA [M-H]…
#> # … with 3 more variables: Measured m/z <dbl>, Theoretical m/z <dbl>,
#> # PPM Error <dbl>
```
2 changes: 1 addition & 1 deletion docs/404.html

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

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

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

2 changes: 1 addition & 1 deletion docs/authors.html

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

18 changes: 9 additions & 9 deletions docs/index.html

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

2 changes: 1 addition & 1 deletion docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pandoc: 2.11.2
pkgdown: 1.6.1
pkgdown_sha: ~
articles: {}
last_built: 2021-02-17T23:51Z
last_built: 2021-04-29T17:14Z
urls:
reference: https://jasenfinch.github.io/mzAnnotation//reference
article: https://jasenfinch.github.io/mzAnnotation//articles
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/MetaboliteDatabase-class.html

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

2 changes: 1 addition & 1 deletion docs/reference/PIPsearch.html

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

2 changes: 1 addition & 1 deletion docs/reference/adductTransformMF.html

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

Loading

0 comments on commit 8efd211

Please sign in to comment.