IsoSpec  1.95
cwrapper.cpp
1 /*
2  * Copyright (C) 2015-2019 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 "fixedEnvelopes.h"
28 
29 using namespace IsoSpec;
30 
31 
32 extern "C"
33 {
34 void * setupIso(int dimNumber,
35  const int* isotopeNumbers,
36  const int* atomCounts,
37  const double* isotopeMasses,
38  const double* isotopeProbabilities)
39 {
40  const double** IM = new const double*[dimNumber];
41  const double** IP = new const double*[dimNumber];
42  int idx = 0;
43  for(int i=0; i<dimNumber; i++)
44  {
45  IM[i] = &isotopeMasses[idx];
46  IP[i] = &isotopeProbabilities[idx];
47  idx += isotopeNumbers[i];
48  }
49  //TODO in place (maybe pass a numpy matrix??)
50 
51  Iso* iso = new Iso(dimNumber, isotopeNumbers, atomCounts, IM, IP);
52 
53  delete[] IM;
54  delete[] IP;
55 
56  return reinterpret_cast<void*>(iso);
57 }
58 
59 void deleteIso(void* iso)
60 {
61  delete reinterpret_cast<Iso*>(iso);
62 }
63 
64 double getLightestPeakMassIso(void* iso)
65 {
66  return reinterpret_cast<Iso*>(iso)->getLightestPeakMass();
67 }
68 
69 double getHeaviestPeakMassIso(void* iso)
70 {
71  return reinterpret_cast<Iso*>(iso)->getHeaviestPeakMass();
72 }
73 
74 double getMonoisotopicPeakMassIso(void* iso)
75 {
76  return reinterpret_cast<Iso*>(iso)->getMonoisotopicPeakMass();
77 }
78 
79 double getModeLProbIso(void* iso)
80 {
81  return reinterpret_cast<Iso*>(iso)->getModeLProb();
82 }
83 
84 double getModeMassIso(void* iso)
85 {
86  return reinterpret_cast<Iso*>(iso)->getModeMass();
87 }
88 
89 double getTheoreticalAverageMassIso(void* iso)
90 {
91  return reinterpret_cast<Iso*>(iso)->getTheoreticalAverageMass();
92 }
93 
94 
95 
96 #define ISOSPEC_C_FN_CODE(generatorType, dataType, method)\
97 dataType method##generatorType(void* generator){ return reinterpret_cast<generatorType*>(generator)->method(); }
98 
99 #define ISOSPEC_C_FN_CODE_GET_CONF_SIGNATURE(generatorType)\
100 void get_conf_signature##generatorType(void* generator, int* space)\
101 { reinterpret_cast<generatorType*>(generator)->get_conf_signature(space); }
102 
103 
104 #define ISOSPEC_C_FN_DELETE(generatorType) void delete##generatorType(void* generator){ delete reinterpret_cast<generatorType*>(generator); }
105 
106 #define ISOSPEC_C_FN_CODES(generatorType)\
107 ISOSPEC_C_FN_CODE(generatorType, double, mass) \
108 ISOSPEC_C_FN_CODE(generatorType, double, lprob) \
109 ISOSPEC_C_FN_CODE(generatorType, double, prob) \
110 ISOSPEC_C_FN_CODE_GET_CONF_SIGNATURE(generatorType) \
111 ISOSPEC_C_FN_CODE(generatorType, bool, advanceToNextConfiguration) \
112 ISOSPEC_C_FN_DELETE(generatorType)
113 
114 
115 
116 //______________________________________________________THRESHOLD GENERATOR
117 void* setupIsoThresholdGenerator(void* iso,
118  double threshold,
119  bool _absolute,
120  int _tabSize,
121  int _hashSize)
122 {
124  std::move(*reinterpret_cast<Iso*>(iso)),
125  threshold,
126  _absolute,
127  _tabSize,
128  _hashSize);
129 
130  return reinterpret_cast<void*>(iso_tmp);
131 }
132 ISOSPEC_C_FN_CODES(IsoThresholdGenerator)
133 
134 
135 //______________________________________________________LAYERED GENERATOR
136 void* setupIsoLayeredGenerator(void* iso,
137  int _tabSize,
138  int _hashSize
139  )
140 {
142  std::move(*reinterpret_cast<Iso*>(iso)),
143  _tabSize,
144  _hashSize
145  );
146 
147  return reinterpret_cast<void*>(iso_tmp);
148 }
149 ISOSPEC_C_FN_CODES(IsoLayeredGenerator)
150 
151 
152 //______________________________________________________ORDERED GENERATOR
153 void* setupIsoOrderedGenerator(void* iso,
154  int _tabSize,
155  int _hashSize)
156 {
158  std::move(*reinterpret_cast<Iso*>(iso)),
159  _tabSize,
160  _hashSize);
161 
162  return reinterpret_cast<void*>(iso_tmp);
163 }
164 ISOSPEC_C_FN_CODES(IsoOrderedGenerator)
165 
166 //______________________________________________________ Threshold FixedEnvelope
167 
168 void* setupThresholdFixedEnvelope(void* iso,
169  double threshold,
170  bool absolute,
171  bool get_confs,
172  bool get_lprobs,
173  bool get_masses,
174  bool get_probs)
175 {
176  ThresholdFixedEnvelope* tabulator = new ThresholdFixedEnvelope(*reinterpret_cast<const Iso*>(iso),
177  threshold,
178  absolute,
179  get_confs,
180  get_lprobs,
181  get_masses,
182  get_probs);
183 
184  return reinterpret_cast<void*>(tabulator);
185 }
186 
187 void deleteThresholdFixedEnvelope(void* t)
188 {
189  delete reinterpret_cast<ThresholdFixedEnvelope*>(t);
190 }
191 
192 const double* massesThresholdFixedEnvelope(void* tabulator)
193 {
194  return reinterpret_cast<ThresholdFixedEnvelope*>(tabulator)->release_masses();
195 }
196 
197 const double* lprobsThresholdFixedEnvelope(void* tabulator)
198 {
199  return reinterpret_cast<ThresholdFixedEnvelope*>(tabulator)->release_lprobs();
200 }
201 
202 const double* probsThresholdFixedEnvelope(void* tabulator)
203 {
204  return reinterpret_cast<ThresholdFixedEnvelope*>(tabulator)->release_probs();
205 }
206 
207 const int* confsThresholdFixedEnvelope(void* tabulator)
208 {
209  return reinterpret_cast<ThresholdFixedEnvelope*>(tabulator)->release_confs();
210 }
211 
212 int confs_noThresholdFixedEnvelope(void* tabulator)
213 {
214  return reinterpret_cast<ThresholdFixedEnvelope*>(tabulator)->confs_no();
215 }
216 
217 
218 //______________________________________________________ Layered FixedEnvelope
219 
220 void* setupTotalProbFixedEnvelope(void* iso,
221  double target_coverage,
222  bool optimize,
223  bool get_confs,
224  bool get_lprobs,
225  bool get_masses,
226  bool get_probs)
227 {
228  TotalProbFixedEnvelope* tabulator = new TotalProbFixedEnvelope(*reinterpret_cast<const Iso*>(iso),
229  target_coverage,
230  optimize,
231  get_confs,
232  get_lprobs,
233  get_masses,
234  get_probs);
235 
236  return reinterpret_cast<void*>(tabulator);
237 }
238 
239 void deleteTotalProbFixedEnvelope(void* t)
240 {
241  delete reinterpret_cast<TotalProbFixedEnvelope*>(t);
242 }
243 
244 const double* massesTotalProbFixedEnvelope(void* tabulator)
245 {
246  return reinterpret_cast<TotalProbFixedEnvelope*>(tabulator)->release_masses();
247 }
248 
249 const double* lprobsTotalProbFixedEnvelope(void* tabulator)
250 {
251  return reinterpret_cast<TotalProbFixedEnvelope*>(tabulator)->release_lprobs();
252 }
253 
254 const double* probsTotalProbFixedEnvelope(void* tabulator)
255 {
256  return reinterpret_cast<TotalProbFixedEnvelope*>(tabulator)->release_probs();
257 }
258 
259 const int* confsTotalProbFixedEnvelope(void* tabulator)
260 {
261  return reinterpret_cast<TotalProbFixedEnvelope*>(tabulator)->release_confs();
262 }
263 
264 int confs_noTotalProbFixedEnvelope(void* tabulator)
265 {
266  return reinterpret_cast<TotalProbFixedEnvelope*>(tabulator)->confs_no();
267 }
268 
269 void freeReleasedArray(void* array)
270 {
271  free(array);
272 }
273 } //extern "C" ends here
IsoSpec::ThresholdFixedEnvelope
Definition: fixedEnvelopes.h:93
IsoSpec::IsoOrderedGenerator
The generator of isotopologues sorted by their probability of occurrence.
Definition: isoSpec++.h:207
IsoSpec::TotalProbFixedEnvelope
Definition: fixedEnvelopes.h:118
IsoSpec
Definition: allocator.cpp:21
IsoSpec::Iso
The Iso class for the calculation of the isotopic distribution.
Definition: isoSpec++.h:53
IsoSpec::IsoThresholdGenerator
The generator of isotopologues above a given threshold value.
Definition: isoSpec++.h:263
IsoSpec::IsoLayeredGenerator
Definition: isoSpec++.h:393