IsoSpec  1.95
cwrapper.cpp
1 /*
2  * Copyright (C) 2015-2018 Mateusz Łącki and Michał Startek.
3  *
4  * This file is part of IsoSpec.
5  *
6  * IsoSpec is free software: you can redistribute it and/or modify
7  * it under the terms of the Simplified ("2-clause") BSD licence.
8  *
9  * IsoSpec is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the Simplified BSD Licence
14  * along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
15  */
16 
17 
18 #include <tuple>
19 #include <string.h>
20 #include <iostream>
21 #include <algorithm>
22 #include <stdexcept>
23 #include "cwrapper.h"
24 #include "misc.h"
25 #include "marginalTrek++.h"
26 #include "isoSpec++.h"
27 #include "tabulator.h"
28 
29 using namespace IsoSpec;
30 
31 extern "C"
32 {
33 void * setupIso(int dimNumber,
34  const int* isotopeNumbers,
35  const int* atomCounts,
36  const double* isotopeMasses,
37  const double* isotopeProbabilities)
38 {
39  const double** IM = new const double*[dimNumber];
40  const double** IP = new const double*[dimNumber];
41  int idx = 0;
42  for(int i=0; i<dimNumber; i++)
43  {
44  IM[i] = &isotopeMasses[idx];
45  IP[i] = &isotopeProbabilities[idx];
46  idx += isotopeNumbers[i];
47  }
48  //TODO in place (maybe pass a numpy matrix??)
49 
50  Iso* iso = new Iso(dimNumber, isotopeNumbers, atomCounts, IM, IP);
51 
52  delete[] IM;
53  delete[] IP;
54 
55  return reinterpret_cast<void*>(iso);
56 }
57 
58 void deleteIso(void* iso)
59 {
60  delete reinterpret_cast<Iso*>(iso);
61 }
62 
63 
64 #define ISOSPEC_C_FN_CODE(generatorType, dataType, method)\
65 dataType method##generatorType(void* generator){ return reinterpret_cast<generatorType*>(generator)->method(); }
66 
67 #define ISOSPEC_C_FN_CODE_GET_CONF_SIGNATURE(generatorType)\
68 void get_conf_signature##generatorType(void* generator, int* space)\
69 { reinterpret_cast<generatorType*>(generator)->get_conf_signature(space); }
70 
71 
72 #define ISOSPEC_C_FN_DELETE(generatorType) void delete##generatorType(void* generator){ delete reinterpret_cast<generatorType*>(generator); }
73 
74 #define ISOSPEC_C_FN_CODES(generatorType)\
75 ISOSPEC_C_FN_CODE(generatorType, double, mass) \
76 ISOSPEC_C_FN_CODE(generatorType, double, lprob) \
77 ISOSPEC_C_FN_CODE(generatorType, double, prob) \
78 ISOSPEC_C_FN_CODE_GET_CONF_SIGNATURE(generatorType) \
79 ISOSPEC_C_FN_CODE(generatorType, bool, advanceToNextConfiguration) \
80 ISOSPEC_C_FN_DELETE(generatorType)
81 
82 
83 
84 //______________________________________________________THRESHOLD GENERATOR
85 void* setupIsoThresholdGenerator(void* iso,
86  double threshold,
87  bool _absolute,
88  int _tabSize,
89  int _hashSize)
90 {
92  std::move(*reinterpret_cast<Iso*>(iso)),
93  threshold,
94  _absolute,
95  _tabSize,
96  _hashSize);
97 
98  return reinterpret_cast<void*>(iso_tmp);
99 }
100 ISOSPEC_C_FN_CODES(IsoThresholdGenerator)
101 
102 
103 //______________________________________________________LAYERED GENERATOR
104 void* setupIsoLayeredGenerator(void* iso,
105  double _target_coverage,
106  double _percentage_to_expand,
107  int _tabSize,
108  int _hashSize,
109  bool _do_trim)
110 {
112  std::move(*reinterpret_cast<Iso*>(iso)),
113  _target_coverage,
114  _percentage_to_expand,
115  _tabSize,
116  _hashSize,
117  _do_trim);
118 
119  return reinterpret_cast<void*>(iso_tmp);
120 }
121 ISOSPEC_C_FN_CODES(IsoLayeredGenerator)
122 
123 
124 //______________________________________________________ORDERED GENERATOR
125 void* setupIsoOrderedGenerator(void* iso,
126  int _tabSize,
127  int _hashSize)
128 {
130  std::move(*reinterpret_cast<Iso*>(iso)),
131  _tabSize,
132  _hashSize);
133 
134  return reinterpret_cast<void*>(iso_tmp);
135 }
136 ISOSPEC_C_FN_CODES(IsoOrderedGenerator)
137 
138 //______________________________________________________ Threshold Tabulator
139 
140 void* setupThresholdTabulator(void* generator,
141  bool get_masses,
142  bool get_probs,
143  bool get_lprobs,
144  bool get_confs)
145 {
146  Tabulator<IsoThresholdGenerator>* tabulator = new Tabulator<IsoThresholdGenerator>(reinterpret_cast<IsoThresholdGenerator*>(generator),
147  get_masses,
148  get_probs,
149  get_lprobs,
150  get_confs);
151 
152  return reinterpret_cast<void*>(tabulator);
153 }
154 
155 void deleteThresholdTabulator(void* t)
156 {
157  delete reinterpret_cast<Tabulator<IsoThresholdGenerator>*>(t);
158 }
159 
160 const double* massesThresholdTabulator(void* tabulator)
161 {
162  return reinterpret_cast<Tabulator<IsoThresholdGenerator>*>(tabulator)->masses(true);
163 }
164 
165 const double* lprobsThresholdTabulator(void* tabulator)
166 {
167  return reinterpret_cast<Tabulator<IsoThresholdGenerator>*>(tabulator)->lprobs(true);
168 }
169 
170 const double* probsThresholdTabulator(void* tabulator)
171 {
172  return reinterpret_cast<Tabulator<IsoThresholdGenerator>*>(tabulator)->probs(true);
173 }
174 
175 const int* confsThresholdTabulator(void* tabulator)
176 {
177  return reinterpret_cast<Tabulator<IsoThresholdGenerator>*>(tabulator)->confs(true);
178 }
179 
180 int confs_noThresholdTabulator(void* tabulator)
181 {
182  return reinterpret_cast<Tabulator<IsoThresholdGenerator>*>(tabulator)->confs_no();
183 }
184 
185 
186 //______________________________________________________ Layered Tabulator
187 
188 void* setupLayeredTabulator(void* generator,
189  bool get_masses,
190  bool get_probs,
191  bool get_lprobs,
192  bool get_confs)
193 {
194  Tabulator<IsoLayeredGenerator>* tabulator = new Tabulator<IsoLayeredGenerator>(reinterpret_cast<IsoLayeredGenerator*>(generator),
195  get_masses,
196  get_probs,
197  get_lprobs,
198  get_confs);
199 
200  return reinterpret_cast<void*>(tabulator);
201 }
202 
203 void deleteLayeredTabulator(void* t)
204 {
205  delete reinterpret_cast<Tabulator<IsoLayeredGenerator>*>(t);
206 }
207 
208 const double* massesLayeredTabulator(void* tabulator)
209 {
210  return reinterpret_cast<Tabulator<IsoLayeredGenerator>*>(tabulator)->masses(true);
211 }
212 
213 const double* lprobsLayeredTabulator(void* tabulator)
214 {
215  return reinterpret_cast<Tabulator<IsoLayeredGenerator>*>(tabulator)->lprobs(true);
216 }
217 
218 const double* probsLayeredTabulator(void* tabulator)
219 {
220  return reinterpret_cast<Tabulator<IsoLayeredGenerator>*>(tabulator)->probs(true);
221 }
222 
223 const int* confsLayeredTabulator(void* tabulator)
224 {
225  return reinterpret_cast<Tabulator<IsoLayeredGenerator>*>(tabulator)->confs(true);
226 }
227 
228 int confs_noLayeredTabulator(void* tabulator)
229 {
230  return reinterpret_cast<Tabulator<IsoLayeredGenerator>*>(tabulator)->confs_no();
231 }
232 
233 void freeReleasedArray(void* array)
234 {
235  free(array);
236 }
237 
238 } //extern "C" ends here
IsoSpec::IsoOrderedGenerator
The generator of isotopologues sorted by their probability of occurrence.
Definition: isoSpec++.h:180
IsoSpec
Definition: allocator.cpp:22
IsoSpec::Iso
The Iso class for the calculation of the isotopic distribution.
Definition: isoSpec++.h:52
IsoSpec::IsoThresholdGenerator
The generator of isotopologues above a given threshold value.
Definition: isoSpec++.h:236
IsoSpec::Tabulator
Definition: tabulator.h:13
IsoSpec::IsoLayeredGenerator
The class that represents isotopologues above a given joint probability value.
Definition: isoSpec++.h:384