-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeconEngineUtils.cpp
295 lines (264 loc) · 9.07 KB
/
DeconEngineUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Written by Navdeep Jaitly for the Department of Energy (PNNL, Richland, WA)
// Copyright 2006, Battelle Memorial Institute
// E-mail: [email protected]
// Website: http://ncrr.pnl.gov/software
// -------------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0; you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
#include "DeconEngineUtils.h"
#include "Utilities/SavGolSmoother.h"
#include "Utilities/Interpolation.h"
#include "Utilities/Apodization.h"
#include "Utilities/FFT32.h"
namespace DeconEngine
{
void Utils::SetData(std::vector<double> &vectData, float (&data) __gc [])
{
vectData.clear() ;
int numPoints = data->Length ;
for (int ptNum = 0 ; ptNum < numPoints ; ptNum++)
{
vectData.push_back((double)data[ptNum]) ;
}
}
void Utils::SetData(std::vector<float> &vectData, float (&data) __gc [])
{
vectData.clear() ;
int numPoints = data->Length ;
for (int ptNum = 0 ; ptNum < numPoints ; ptNum++)
{
float val = data[ptNum] ;
vectData.push_back(val) ;
}
}
void Utils::GetData(std::vector<float> &vectData, float (&data) __gc [])
{
int numPoints = vectData.size() ;
data = new float __gc [numPoints] ;
for (int ptNum = 0 ; ptNum < numPoints ; ptNum++)
{
data[ptNum] = vectData[ptNum] ;
}
}
void Utils::GetData(std::vector<double> &vectData, float (&data) __gc [])
{
int numPoints = vectData.size() ;
data = new float __gc [numPoints] ;
for (int ptNum = 0 ; ptNum < numPoints ; ptNum++)
{
data[ptNum] = (float) vectData[ptNum] ;
}
}
void Utils::GetStr(System::String *src, char *dest)
{
if (src == 0 || src == "" || src->get_Length() == 0)
{
dest[0] = '\0' ;
return ;
}
int len = src->get_Length() ;
for (int i = 0 ; i < len ; i++)
{
dest[i] = (char) src->Chars[i] ;
}
dest[len] = '\0' ;
}
void Utils::GetStr(const char *src, System::String **dest)
{
if (src == NULL)
{
*dest = NULL ;
return ;
}
if (strlen(src) == 0)
{
*dest = new System::String("") ;
return ;
}
*dest = new System::String(src) ;
return ;
}
double Utils::GetAverage(float (&intensities) __gc [], float maxIntensity)
{
int numPts = intensities->Length ;
if (numPts == 0)
return 0 ;
double backgroundIntensity = 0 ;
int numPtsUsed = 0 ;
for (int i = 0 ; i < numPts ; i++)
{
if (intensities[i] <= maxIntensity && intensities[i] != 0)
{
backgroundIntensity += intensities[i] ;
numPtsUsed++ ;
}
}
return backgroundIntensity / numPtsUsed ;
}
double Utils::GetAverage(std::vector<double> &intensities, float maxIntensity)
{
int numPts = intensities.size() ;
if (numPts == 0)
return 0 ;
double backgroundIntensity = 0 ;
int numPtsUsed = 0 ;
for (int i = 0 ; i < numPts ; i++)
{
if (intensities[i] <= maxIntensity && intensities[i] != 0)
{
backgroundIntensity += intensities[i] ;
numPtsUsed++ ;
}
}
return backgroundIntensity / numPtsUsed ;
}
double Utils::GetTIC(double min_mz, double max_mz, std::vector<double> &mzs, std::vector<double> &intensities, float minIntensity,
double &bpi, double &bp_mz)
{
int numPts = intensities.size() ;
if (numPts == 0)
return 0 ;
double sum = 0 ;
int numPtsUsed = 0 ;
bpi = 0 ;
for (int i = 0 ; i < numPts ; i++)
{
if (mzs[i] >= min_mz && mzs[i] < max_mz && intensities[i] >= minIntensity)
{
sum += intensities[i] ;
if (intensities[i] > bpi)
{
bpi = intensities[i] ;
bp_mz = mzs[i] ;
}
}
}
return sum ;
}
void Utils::ConvertElementTableToFormula(Engine::TheoreticalProfile::AtomicInformation &elemental_isotope_composition, System::Collections::Hashtable* elementCounts,
Engine::TheoreticalProfile::MolecularFormula &formula)
{
char element_char[16] ;
System::Collections::IEnumerator* elements = elementCounts->Keys->GetEnumerator();
formula.Clear() ;
Engine::TheoreticalProfile::AtomicCount atomic_count ;
while(elements->MoveNext())
{
// Get the next element symbol in the table
System::String* element = __try_cast<System::String*>(elements->Current);
DeconEngine::Utils::GetStr(element, element_char) ;
// Put it in a character array
int count = *static_cast<__box int*>(elementCounts->Item[element]);
// find the index of the element in the AtomicInformation
int index = elemental_isotope_composition.GetElementIndex(element_char) ;
if (index == -1)
{
throw new System::ApplicationException(System::String::Concat(S"Unknown element ", element));
}
else
{
atomic_count.mint_index = index ;
atomic_count.mdbl_num_copies = count ;
double mono_mass = elemental_isotope_composition.mvect_elemental_isotopes[index].marr_isotope_mass[0] * count ;
double avg_mass = elemental_isotope_composition.mvect_elemental_isotopes[index].mdbl_average_mass * count ;
formula.AddAtomicCount(atomic_count, mono_mass, avg_mass ) ;
}
}
}
void Utils::SetPeaks(Engine::PeakProcessing::PeakData &pk_data, DeconToolsV2::Peaks::clsPeak* (&peaks) __gc [])
{
Engine::PeakProcessing::Peak enginePeak ;
for (int pkNum = 0 ; pkNum < peaks->Length ; pkNum++)
{
enginePeak.mdbl_FWHM = peaks[pkNum]->mdbl_FWHM ;
enginePeak.mdbl_intensity = peaks[pkNum]->mdbl_intensity ;
enginePeak.mdbl_mz = peaks[pkNum]->mdbl_mz ;
enginePeak.mdbl_SN = peaks[pkNum]->mdbl_SN ;
enginePeak.mint_data_index = peaks[pkNum]->mint_data_index ;
enginePeak.mint_peak_index = peaks[pkNum]->mint_peak_index ;
pk_data.AddPeak(enginePeak) ;
}
pk_data.InitializeUnprocessedPeakData() ;
}
void Utils::SavitzkyGolaySmooth(short num_left, short num_right, short order, float (&mzs) __gc [], float (&intensities) __gc [])
{
Engine::Utilities::SavGolSmoother *sgSmoother = __nogc new Engine::Utilities::SavGolSmoother(num_left, num_right, order) ;
std::vector<double> vectX ;
std::vector<double> vectY ;
int num_pts = mzs->Length ;
for (int pt_num = 0 ; pt_num < num_pts ; pt_num++)
{
vectX.push_back((double)mzs[pt_num]) ;
vectY.push_back((double)intensities[pt_num]) ;
}
sgSmoother->Smooth(&vectX, &vectY) ;
for (int pt_num = 0 ; pt_num < num_pts ; pt_num++)
{
mzs[pt_num] = (float)vectX[pt_num] ;
intensities[pt_num] = (float) vectY[pt_num] ;
}
delete sgSmoother ;
}
int Utils::ZeroFillUnevenData(float (&mzs) __gc [], float (&intensities) __gc [], int maxPtsToAdd)
{
std::vector<float> vectX ;
std::vector<float> vectY ;
int num_pts = mzs->Length ;
for (int pt_num = 0 ; pt_num < num_pts ; pt_num++)
{
float currentMZ = mzs[pt_num] ;
float currentIntensity = intensities[pt_num] ;
vectX.push_back(currentMZ) ;
vectY.push_back(currentIntensity) ;
}
int numPtsX = vectX.size() ;
Engine::Utilities::Interpolation __nogc *interp = __nogc new Engine::Utilities::Interpolation() ;
interp->ZeroFillMissing(vectX, vectY, maxPtsToAdd) ;
int num_pts_new = (int) vectX.size() ;
intensities = new float __gc [num_pts_new] ;
mzs = new float __gc [num_pts_new] ;
for (int i = 0 ; i < num_pts_new ; i++)
{
mzs[i] = vectX[i] ;
intensities[i] = vectY[i] ;
}
delete interp ;
return num_pts_new ;
}
void Utils::Apodize(double minX, double maxX, double sampleRate, int apexPositionPercent, float (&intensities) __gc [], DeconToolsV2::Readers::ApodizationType type)
{
float __nogc *arrIntensities = __nogc new float [intensities->Length] ;
for (int i = 0 ; i < intensities->Length ; i++)
arrIntensities[i] = intensities[i] ;
Engine::Utilities::Apodization::Apodize(minX, maxX, sampleRate, false, (Engine::Utilities::ApodizationType) type, arrIntensities,
intensities->Length, apexPositionPercent) ;
for (int i = 0 ; i < intensities->Length ; i++)
intensities[i] = arrIntensities[i] ;
delete [] arrIntensities ;
}
void Utils::UnApodize(float (&intensities) __gc[], DeconToolsV2::Readers::ApodizationType type)
{
}
void Utils::FourierTransform(float (&intensities) __gc [])
{
float __nogc *arrIntensities = __nogc new float [intensities->Length] ;
for (int i = 0 ; i < intensities->Length ; i++)
arrIntensities[i] = intensities[i] ;
Engine::FFT::realft(intensities->Length, arrIntensities, 1) ;
for (int i = 0 ; i < intensities->Length ; i++)
intensities[i] = arrIntensities[i] ;
delete [] arrIntensities ;
}
void Utils::InverseFourierTransform(float (&intensities) __gc [])
{
float __nogc *arrIntensities = __nogc new float [intensities->Length] ;
for (int i = 0 ; i < intensities->Length ; i++)
arrIntensities[i] = intensities[i] ;
Engine::FFT::realft(intensities->Length, arrIntensities, -1) ;
for (int i = 0 ; i < intensities->Length ; i++)
intensities[i] = arrIntensities[i] ;
delete [] arrIntensities ;
}
}