Browse code

Merge pull request #19 from mjhelf/master

Addressing issue with minElements in decomposeMass()/decomposeIsotopes()

Steffen Neumann authored on 02/05/2019 19:46:54 • GitHub committed on 02/05/2019 19:46:54
Showing2 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,104 @@
1
+
2
+#default settings for minElements and maxElements
3
+test.minmaxElements1 <- function() {
4
+
5
+  elem <-  initializeElements(c("C","H","N","O","P"))
6
+  testmass <- 269.2431
7
+  
8
+  res <- decomposeMass(testmass, ppm = 5, mzabs = 0, elements = elem)   
9
+  
10
+  checkEquals(res$formula[order(abs(res$exactmass - testmass))],c("C6H34N6O3P",
11
+                                                                        "C2H37N7O3P2",
12
+                                                                        "C3H43O8P2",
13
+                                                                        "C10H31N5O3",
14
+                                                                        "C3H48NOP5",
15
+                                                                        "C12H33N2O4",
16
+                                                                        "C7H45OP4",
17
+                                                                        "CH41N3O7P2",
18
+                                                                        "H35N10O2P2",
19
+                                                                        "C8H36N3O4P"))
20
+}
21
+
22
+#must filter out formulas that do not contain C
23
+test.minmaxElements2 <- function() {
24
+  
25
+  elem <-  initializeElements(c("C","H","N","O","P"))
26
+  testmass <- 269.2431
27
+  
28
+  res <- decomposeMass(testmass, ppm = 5, mzabs = 0, elements = elem, minElements = "C1")   
29
+  
30
+  checkEquals(res$formula[order(abs(res$exactmass - testmass))],c("C6H34N6O3P",
31
+                                                                        "C2H37N7O3P2",
32
+                                                                        "C3H43O8P2",
33
+                                                                        "C10H31N5O3",
34
+                                                                        "C3H48NOP5",
35
+                                                                        "C12H33N2O4",
36
+                                                                        "C7H45OP4",
37
+                                                                        "CH41N3O7P2",
38
+                                                                        "C8H36N3O4P"))
39
+}
40
+
41
+#set maxElements
42
+test.minmaxElements3 <- function() {
43
+  
44
+  elem <-  initializeElements(c("C","H","N","O","P"))
45
+  testmass <- 269.2431
46
+  
47
+  res <- decomposeMass(testmass, ppm = 5,  mzabs = 0, elements = elem, minElements = "C1", maxElements = "C6")   
48
+  
49
+  checkEquals(res$formula[order(abs(res$exactmass - testmass))],c("C6H34N6O3P",
50
+                                                                       "C2H37N7O3P2",
51
+                                                                       "C3H43O8P2",
52
+                                                                       "C3H48NOP5",
53
+                                                                       "CH41N3O7P2"))
54
+}
55
+
56
+
57
+#equal minElements and maxElements
58
+test.minmaxElements4 <- function() {
59
+  
60
+  elem <-  initializeElements(c("C","H","N","O","P"))
61
+  testmass <- 269.2431
62
+  
63
+  res <- decomposeMass(testmass, ppm = 5,  mzabs = 0, elements = elem, minElements = "C6", maxElements = "C6")   
64
+  
65
+  checkEquals(res$formula[order(abs(res$exactmass - testmass))],c("C6H34N6O3P"
66
+                                                                 ))
67
+}
68
+
69
+#maxElements > minElements
70
+test.minmaxElements5 <- function() {
71
+  
72
+  elem <-  initializeElements(c("C","H","N","O","P"))
73
+  testmass <- 269.2431
74
+  
75
+  res <- decomposeMass(testmass, ppm = 5,  mzabs = 0, elements = elem, minElements = "C6", maxElements = "C5")   
76
+  
77
+  checkTrue(is.null(res))
78
+}
79
+
80
+
81
+#Zeros in maxElements are ignored and do not cause an error
82
+test.minmaxElements6 <- function() {
83
+  
84
+  elem <-  initializeElements(c("C","H","N","O","P"))
85
+  testmass <- 269.2431
86
+  
87
+  res <- decomposeMass(testmass, ppm = 5,  mzabs = 0, elements = elem, minElements = "C6", maxElements = "P0")   
88
+  
89
+  checkEquals(res$formula[order(abs(res$exactmass - testmass))],c("C6H34N6O3P", "C10H31N5O3", "C12H33N2O4", "C7H45OP4", "C8H36N3O4P"))
90
+}
91
+
92
+#more complex exammple:
93
+test.minmaxElements7 <- function() {
94
+  
95
+  elem <- initializeCHNOPS()       
96
+  testmass <- 347.0630844422
97
+  
98
+  res <- decomposeMass(testmass, ppm = 5,  mzabs = 0, elements = elem, minElements = "C10O6PN", maxElements = "O10P1S3")   
99
+  
100
+  checkEquals(res$formula[order(abs(res$exactmass - testmass))],c("C10H14N5O7P",  "C10H22NO6PS2", "C12H16N2O8P"))
101
+}
102
+
103
+
104
+
... ...
@@ -119,22 +119,32 @@ bool isValidMyNitrogenRule(const ComposedElement& molecule, int z) {
119 119
 bool isWithinElementRange(const ComposedElement& molecule, const ComposedElement& minElements, const ComposedElement& maxElements) {
120 120
 // {{{
121 121
 
122
-  for (ComposedElement::container::const_iterator it = molecule.getElements().begin(); 
123
-       it != molecule.getElements().end(); ++it) {
124
-    
122
+//iterating over Elements present in minElements
123
+  for (ComposedElement::container::const_iterator it = minElements.getElements().begin();
124
+       it != minElements.getElements().end(); ++it) {
125
+
125 126
     int mincount = static_cast<int>(minElements.getElementAbundance((it->first).getName()));
126
-    int maxcount = static_cast<int>(maxElements.getElementAbundance((it->first).getName()));
127 127
 
128 128
     int count = static_cast<int>(molecule.getElementAbundance((it->first).getName()));
129
-      
129
+
130 130
       if (count < mincount) {
131 131
 	return false;
132 132
       }
133 133
 
134
-      // TODO: Fails e.g. for "C2N0" 
135
-      if (maxcount>0 && count > maxcount) {
136
-	return false;
137
-      }
134
+  }
135
+  
136
+  //must iterate over Elements present in molecule and ignore cases of Elements not defined in maxElements
137
+  for (ComposedElement::container::const_iterator it = maxElements.getElements().begin(); 
138
+       it != maxElements.getElements().end(); ++it) {
139
+    
140
+    int maxcount = static_cast<int>(maxElements.getElementAbundance((it->first).getName()));
141
+    
142
+    int count = static_cast<int>(molecule.getElementAbundance((it->first).getName()));
143
+    
144
+    // TODO: Fails e.g. for "C2N0" 
145
+    if (maxcount>0 && count > maxcount) {
146
+      return false;
147
+    }
138 148
   }
139 149
 
140 150
   return true;