Skip to content

Instantly share code, notes, and snippets.

@meowcat
Created November 17, 2014 13:20
Show Gist options
  • Save meowcat/befb649eb993b397bab3 to your computer and use it in GitHub Desktop.
Save meowcat/befb649eb993b397bab3 to your computer and use it in GitHub Desktop.
Rcdk generates formulae from off-center mass?
library(rcdk)
limits <- list(
c("C", 0, 20),
c("H", 0, 24),
c("N", 0, 1),
c("O", 0,4))
f <- generate.formula(55.05423, 0.001, elements=limits, charge=1, validation=FALSE)
# generates the correct formula:
print(f)
rm(f)
try(f <- generate.formula(55.05423, 0.0005, elements=limits, charge=1, validation=FALSE))
# error
f <- generate.formula(55.05427, 0.0005, elements=limits, charge=1, validation=FALSE) # fail
f <- generate.formula(55.05428, 0.0005, elements=limits, charge=1, validation=FALSE) # works
try(f <- generate.formula(55.054275, 0.0005, elements=limits, charge=1, validation=FALSE)) # fail
f <- generate.formula(55.054276, 0.0005, elements=limits, charge=1, validation=FALSE) # works
f <- generate.formula(55.05453, 0.0005, elements=limits, charge=1, validation=FALSE)
# generates the correct formula, even though the actual formula mass is 0.5423 and outside
# of the bounds 55.05428 to 55.05478 (if they are two-sided)
print(f)
rm(f)
f <- generate.formula(55.05473, 0.0005, elements=limits, charge=1, validation=FALSE)
# even this gives the correct formula!
print(f)
# all the way up to:
rm(f)
f <- generate.formula(55.05523, 0.0005, elements=limits, charge=1, validation=FALSE)
print(f)
rm(f)
# Generate formulae from a matrix of input masses and windows
gf.string <- function(mass, limit) tryCatch(
generate.formula(mass,limit, elements=limits, charge=1, validation=FALSE)[[1]]@string, error=function(e) NA)
gf.mass <- function(mass, limit) tryCatch(
generate.formula(mass,limit, elements=limits, charge=1, validation=FALSE)[[1]]@mass, error=function(e) NA)
masses <- 55.05423 +seq(-0.001, 0.001, 0.0001)
mlimits <- c(0.0001, 0.0002, 0.0005, 0.001)
#tabulated <-as.data.frame(do.call(rbind, lapply(masses, function(m) unlist(lapply(mlimits, function(l) gf.string(m,l))))))
tabulated <-as.data.frame(do.call(rbind, lapply(masses, function(m) unlist(lapply(mlimits, function(l) gf.mass(m,l))))))
rownames(tabulated) <- masses
colnames(tabulated) <- mlimits
print(tabulated)
# -> generate.formula calculates formulas ignoring the charge=1 parameter (but returns the mass of the charged formula)!
# The former behaviour was to generate formulas from the charged mass (taking the charge into account).
# i.e. generate.formula(55.05423, window=0.0001, charge=1) would return C4H7 at 55.05423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment