Mercurial > repos > prog > lcmsmatching
comparison chem.R @ 0:e66bb061af06 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 3529b25417f8e1a5836474c9adec4b696d35099d-dirty
author | prog |
---|---|
date | Tue, 12 Jul 2016 12:02:37 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e66bb061af06 |
---|---|
1 if ( ! exists('load.sdf')) { # Do not load again if already loaded | |
2 | |
3 ############# | |
4 # CONSTANTS # | |
5 ############# | |
6 | |
7 R.LIB.CHEM.FILE.PATH <- parent.frame(2)$ofile | |
8 | |
9 CARBOXYL.GROUP <- "carboxyl" | |
10 | |
11 ################## | |
12 # LOAD JAVA CHEM # | |
13 ################## | |
14 | |
15 load.java.chem <- function() { | |
16 library(rJava) | |
17 .jinit() | |
18 .jcall('java/lang/System', 'S', 'setProperty', "rJava.debug", "1") # DEBUG/VERBOSE mode --> TODO does not work | |
19 cmd <- c("mvn", "-f", file.path(dirname(R.LIB.CHEM.FILE.PATH), '..', 'java-chem'), "org.apache.maven.plugins:maven-dependency-plugin:2.10:build-classpath") | |
20 classpath <- system(paste(cmd, collapse = " "), intern = TRUE) | |
21 classpath <- grep("^\\[INFO]", classpath, invert = TRUE, value = TRUE) | |
22 classpath <- strsplit(classpath, split = ':')[[1]] # TODO make it portable (classpath under Windows use ';' instead of ':') | |
23 .jaddClassPath(classpath) | |
24 .jaddClassPath(file.path(dirname(R.LIB.CHEM.FILE.PATH), '..', 'java-chem', 'target', 'java-chem-1.0.jar')) | |
25 } | |
26 | |
27 ############# | |
28 # GET INCHI # | |
29 ############# | |
30 | |
31 get.inchi <- function(mol) { | |
32 load.java.chem() | |
33 cdkhlp <- .jnew('org/openscience/chem/CdkHelper') | |
34 inchi <- .jcall(cdkhlp, 'S', 'getInchi', mol) | |
35 return(inchi) | |
36 } | |
37 | |
38 ######################### | |
39 # CONTAINS SUBSTRUCTURE # | |
40 ######################### | |
41 | |
42 contains.substructure <- function(inchi, group) { | |
43 | |
44 load.java.chem() | |
45 cdkhlp <- .jnew('org/openscience/chem/CdkHelper') | |
46 | |
47 # Search for substructure | |
48 contains <- .jcall(cdkhlp, '[Z', 'containFunctionalGroup', inchi, toupper(group)) | |
49 | |
50 return(contains) | |
51 } | |
52 | |
53 ############ | |
54 # LOAD SDF # | |
55 ############ | |
56 | |
57 load.sdf <- function(file, silent = FALSE) { | |
58 | |
59 library(stringr) | |
60 | |
61 # Valid file ? | |
62 if ( ! file.exists(file)) { | |
63 if ( ! silent) | |
64 warning(paste0("SDF File \"", file, "\" does not exist.")) | |
65 return(NULL) | |
66 } | |
67 | |
68 info <- data.frame() | |
69 | |
70 # Read file line by line | |
71 con <- file(file) | |
72 open(con) | |
73 imol <- 1 # Index of molecule inside the file | |
74 field.name <- NA_character_ | |
75 while (TRUE) { | |
76 | |
77 # Read one line | |
78 line <- readLines(con, n = 1) | |
79 if (length(line) == 0) | |
80 break | |
81 | |
82 # Field value | |
83 if ( ! is.na(field.name)) { | |
84 info[imol, field.name] <- line | |
85 field.name <- NA_character_ | |
86 next | |
87 } | |
88 | |
89 # Empty line | |
90 if (line == "") { | |
91 field.name <- NA_character_ | |
92 next | |
93 } | |
94 | |
95 # End of molecule | |
96 if (substring(line, 1, 4) == "$$$$") { | |
97 field.name <- NA_character_ | |
98 imol <- imol + 1 | |
99 next | |
100 } | |
101 | |
102 # Metadata field | |
103 g <- str_match(line, "^> <(.*)>$") | |
104 if ( ! is.na(g[1,2])) { | |
105 field.name <- g[1,2] | |
106 next | |
107 } | |
108 } | |
109 close(con) | |
110 | |
111 # Load molecule structures | |
112 load.java.chem() | |
113 cdkhlp <- .jnew('org/openscience/chem/CdkHelper') | |
114 struct <- .jcall(cdkhlp, '[Lorg/openscience/cdk/interfaces/IAtomContainer;', 'loadSdf', file) | |
115 | |
116 return(list(struct = struct, info = info)) | |
117 } | |
118 | |
119 } # end of load safe guard |