ipb-halle / MetFragR

R package for MetFrag
24 stars 14 forks source link

Feature request: Fragmenter #1

Closed meowcat closed 7 years ago

meowcat commented 11 years ago

Hi,

I was toying around in R and Steffen notified me that there actually is a MetFragR package. I had an idea specific to transformation product identification and hoped that I could use some MetFrag features from it - specifically the Fragmenter.

I already tried with R and rJava calls, but up to now haven't had success... It would be really cool if I could use features like this directly.

The .jrcall() calls usually work fine with Java generics, I have used them before with success.

A piece of code to show what I wanted to do:

mf.path <- "C:/Daten/Sourcecode/metfrag/c-ruttkies/MetFrag/bin"
mf.lib.path <- "C:/Daten/Sourcecode/metfrag/c-ruttkies/MetFrag/lib/cdk-1.3.9.jar"

library(rJava)
library(rcdk)
.jaddClassPath(mf.path)
.jaddClassPath(mf.lib.path)

# make a Fragmenter
fref <- .jnew("de/ipbhalle/metfrag/fragmenter/Fragmenter", F, T, F)
fobj <- new( J("de/ipbhalle/metfrag/fragmenter/Fragmenter"), F, T, F)
# load a molecule: Venlafaxine
m <- parse.smiles("CN(C)CC(C1=C=C(C=C1)OC)C2(CCCCC2)O")
m <- m[[1]]
# preprocess as MetFrag would
J("org/openscience/cdk/tools/manipulator/AtomContainerManipulator")$percieveAtomTypesAndConfigureAtoms(m)
convert.implicit.to.explicit(m)

#m.c <- .jcast(m, "org/openscience/cdk/interfaces/IAtomContainer", check=T)
#m.marked <- .jcast(.jrcall(f, "markAllBonds", m), "org/openscience/cdk/interfaces/IAtomContainer")

#all these don't work:
frags <- .jrcall(fref, "generateFragmentsInMemory", m, F, as.integer(1))
# Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
# java.lang.NullPointerException
frags <- fobj$generateFragmentsInMemory(m, F, as.integer(1))
# Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
# java.lang.NullPointerException

# Another try using another Fragmenter constructor with a fake peaklist
spec <- new(J("de/ipbhalle/metfrag/spectrum/WrapperSpectrum"),
            "1\t1",
            as.integer(0),999,T)
peaks <- spec$getPeakList()
fref <- .jnew("de/ipbhalle/metfrag/fragmenter/Fragmenter",
             peaks$clone(), 0.010, 10, as.integer(1), F, T, F, F)
fobj <- new( J("de/ipbhalle/metfrag/fragmenter/Fragmenter"),
          peaks$clone(), 0.010, 10, as.integer(1), F, T, F, F)

frags <- .jrcall(fref, "generateFragmentsInMemory", m, F, as.integer(1))
# Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
# java.lang.NoSuchMethodError: org.openscience.cdk.graph.ConnectivityChecker.partitionIntoMolecules(Lorg/openscience/cdk/interfaces/IAtomContainer;)Lorg/openscience/cdk/interfaces/IMoleculeSet;
frags <- fobj$generateFragmentsInMemory(m, F, as.integer(1))
# Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
# java.lang.NoSuchMethodError: org.openscience.cdk.graph.ConnectivityChecker.partitionIntoMolecules(Lorg/openscience/cdk/interfaces/IAtomContainer;)Lorg/openscience/cdk/interfaces/IMoleculeSet;
meowcat commented 11 years ago

I figured out it's probably a problem arising because rcdk doesn't use the same Cdk version as Metfrag...

In fact I got the Fragmenter with peaks constructor + generateFragmentsInMemory to work when I loaded the metfrag cdk lib before rcdk (but then I can't actually use the rcdk functions anymore, have to make my molecule by hand, and can't display the fragments.)

meowcat commented 10 years ago

Hi,

I hacked MetFrag so it runs rudimentarily with the 1.4.something cdk version from rcdklibs (I can get the fragmenter to work from R; there are possibly many more changes which need to be done but I have no idea.) If you're interested, I can push it to some fork of your repo...

Minimal R example:

mf.path <- "C:/Daten/Sourcecode/metfrag/c-ruttkies/MetFrag/bin"

library(rJava)
#rcdk           "rcdk"           "C:/Daten/R/R-3.0.0"                 "3.2.0.2"       NA           
#rcdklibs       "rcdklibs"       "C:/Daten/R/R-3.0.0"                 "1.5.0.3"       NA 
library(rcdk)
.jaddClassPath(mf.path)
#.jaddClassPath(mf.lib.path)

# make a Fragmenter
fobj <- new( J("de/ipbhalle/metfrag/fragmenter/Fragmenter"), F, T, F)
# load a molecule: Venlafaxine
m <- parse.smiles("CN(C)CC(C1=C=C(C=C1)OC)C2(CCCCC2)O")
m <- m[[1]]
# preprocess as MetFrag would
J("org/openscience/cdk/tools/manipulator/AtomContainerManipulator")$percieveAtomTypesAndConfigureAtoms(m)
convert.implicit.to.explicit(m)

# Even if this is not actually a Vector<Peak> it works. Maybe it's because Java
# generics are implemented strangely or because it's an empty vector - rJava doesn't have
# a way to specify signatures of Java generic classes.
# Omitting to set a peaklist results in NullPointerException during generateFragmentsInMemory
v <- new(J("java/util/Vector"))
fobj$setPeakList(v)
frags <- fobj$generateFragmentsInMemory(m, F, as.integer(3))
frags <- as.list(frags)
view.molecule.2d(frags)
c-ruttkies commented 10 years ago

Hi,

sorry for the delay. I was not really monitoring the progress of this repo the last time. We currently want to start developing the package further and to include your ideas. Nice hacks you pasted here. It is planned to extract the fragments of individual candidate molecules directly. After reading your code I think this is also in your own interest.

Best regards, Christoph