libpappsomspp
Library for mass spectrometry
Toggle main menu visibility
Loading...
Searching...
No Matches
msrunxicextractordisk.cpp
Go to the documentation of this file.
1
/**
2
* \file pappsomspp/xicextractor/private/msrunxicextractordisk.cpp
3
* \date 12/05/2018
4
* \author Olivier Langella
5
* \brief proteowizard based XIC extractor featuring disk cache
6
*/
7
8
/*******************************************************************************
9
* Copyright (c) 2018 Olivier Langella <Olivier.Langella@u-psud.fr>.
10
*
11
* This file is part of the PAPPSOms++ library.
12
*
13
* PAPPSOms++ is free software: you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation, either version 3 of the License, or
16
* (at your option) any later version.
17
*
18
* PAPPSOms++ is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License
24
* along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
25
*
26
* Contributors:
27
* Olivier Langella <Olivier.Langella@u-psud.fr> - initial API and
28
*implementation
29
******************************************************************************/
30
31
#include "
msrunxicextractordisk.h
"
32
#include <QDebug>
33
#include "
pappsomspp/core/pappsoexception.h
"
34
#include "
pappsomspp/core/massspectrum/massspectrum.h
"
35
36
namespace
pappso
37
{
38
39
MsRunXicExtractorDisk::MsRunXicExtractorDisk
(
MsRunReaderSPtr
&msrun_reader,
40
const
QDir &temporary_dir)
41
:
pappso
::
MsRunXicExtractor
(msrun_reader)
42
{
43
mpa_temporaryDirectory
=
nullptr
;
44
m_temporaryDirectory
= temporary_dir.absolutePath();
45
}
46
47
MsRunXicExtractorDisk::MsRunXicExtractorDisk
(
const
MsRunXicExtractorDisk
&other)
48
:
pappso
::
MsRunXicExtractor
(other)
49
{
50
51
m_temporaryDirectory
= other.
m_temporaryDirectory
;
52
mpa_temporaryDirectory
=
53
new
QTemporaryDir(QString(
"%1/msrun_%2_"
)
54
.arg(
m_temporaryDirectory
)
55
.arg(
msp_msrun_reader
.get()->getMsRunId().get()->getXmlId()));
56
}
57
58
MsRunXicExtractorDisk::~MsRunXicExtractorDisk
()
59
{
60
if
(
mpa_temporaryDirectory
!=
nullptr
)
61
{
62
delete
mpa_temporaryDirectory
;
63
}
64
}
65
66
void
67
MsRunXicExtractorDisk::prepareExtractor
()
68
{
69
qDebug();
70
try
71
{
72
serializeMsRun
();
73
// msp_msrun_reader = nullptr;
74
}
75
catch
(
pappso::PappsoException
&errora)
76
{
77
qDebug();
78
throw
pappso::PappsoException
(QObject::tr(
"Error reading file (%1) : %2"
)
79
.arg(
msp_msrun_reader
.get()->getMsRunId().get()->toString())
80
.arg(errora.
qwhat
()));
81
}
82
catch
(std::exception &error)
83
{
84
qDebug();
85
throw
pappso::PappsoException
(QObject::tr(
"Error reading file (%1) using : %2"
)
86
.arg(
msp_msrun_reader
.get()->getMsRunId().get()->toString())
87
.arg(error.what()));
88
}
89
}
90
91
92
void
93
MsRunXicExtractorDisk::protectedExtractXicCoordSPtrList
(
94
UiMonitorInterface
&monitor,
95
std::vector<XicCoordSPtr>::iterator it_xic_coord_list_begin,
96
std::vector<XicCoordSPtr>::iterator it_xic_coord_list_end)
97
{
98
// sort xic by mz:
99
std::sort(it_xic_coord_list_begin, it_xic_coord_list_end, [](
XicCoordSPtr
&
a
,
XicCoordSPtr
&
b
) {
100
return
a
.get()->mzRange.getMz() <
b
.get()->mzRange.getMz();
101
});
102
103
for
(
auto
it = it_xic_coord_list_begin; it != it_xic_coord_list_end; it++)
104
{
105
extractOneXicCoord
(*(it->get()));
106
monitor.
count
();
107
}
108
}
109
110
void
111
MsRunXicExtractorDisk::extractOneXicCoord
(
XicCoord
&xic_coord)
112
{
113
std::shared_ptr<Xic> msrunxic_sp = xic_coord.
xicSptr
;
114
115
double
rt_begin = xic_coord.
rtTarget
-
m_retentionTimeAroundTarget
;
116
double
rt_end = xic_coord.
rtTarget
+
m_retentionTimeAroundTarget
;
117
118
119
std::vector<MsRunSliceSPtr> slice_list;
120
slice_list =
acquireSlices
(xic_coord.
mzRange
);
121
122
if
(slice_list.size() == 0)
123
{
124
throw
pappso::PappsoException
(QObject::tr(
"Error getMsRunXicSp slice_list.size() == 0"
));
125
}
126
127
for
(std::size_t i = 0; i <
m_retentionTimeList
.size(); i++)
128
{
129
130
DataPoint
xic_element;
131
xic_element.
x
=
m_retentionTimeList
[i];
132
xic_element.
y
= 0;
133
if
((xic_element.
x
< rt_begin) || (xic_element.
x
> rt_end))
134
continue
;
135
136
for
(
auto
&&msrun_slice : slice_list)
137
{
138
const
MassSpectrum
&spectrum = msrun_slice.get()->getSpectrum(i);
139
for
(
auto
&&peak : spectrum)
140
{
141
if
(xic_coord.
mzRange
.
contains
(peak.x))
142
{
143
if
(
m_xicExtractMethod
==
Enums::XicExtractMethod::sum
)
144
{
145
xic_element.
y
+= peak.y;
146
}
147
else
148
{
149
if
(xic_element.
y
< peak.y)
150
{
151
xic_element.
y
= peak.y;
152
}
153
}
154
}
155
}
156
}
157
msrunxic_sp.get()->push_back(xic_element);
158
}
159
}
160
161
void
162
MsRunXicExtractorDisk::serializeMsRun
()
163
{
164
qDebug();
165
m_minMz
= 5000;
166
m_maxMz
= 0;
167
168
unsigned
int
slice_number;
169
std::map<unsigned int, MassSpectrum> spectrum_map;
170
171
/*
172
const pwiz::msdata::SpectrumList *p_spectrum_list =
173
p_msdatafile->run.spectrumListPtr.get();
174
175
std::size_t spectrum_list_size = p_spectrum_list->size();
176
pwiz::msdata::SpectrumPtr pwiz_spectrum;
177
*/
178
179
m_rtSize
=
m_msrun_points
.size();
180
181
182
MassSpectrumCstSPtr
spectrum;
183
for
(
auto
&&msrun_point :
m_msrun_points
)
184
{
185
186
spectrum_map.clear();
187
188
m_retentionTimeList
.push_back(msrun_point.rt);
189
190
spectrum =
msp_msrun_reader
.get()->massSpectrumCstSPtr(msrun_point.spectrum_index);
191
192
const
MassSpectrum
*p_spectrum = spectrum.get();
193
if
(p_spectrum->size() > 0)
194
{
195
if
(p_spectrum->begin()->x <
m_minMz
)
196
{
197
m_minMz
= p_spectrum->begin()->x;
198
}
199
// iterate through the m/z-intensity pairs
200
201
if
(p_spectrum->back().x >
m_maxMz
)
202
{
203
m_maxMz
= p_spectrum->back().x;
204
}
205
206
for
(
auto
&peak : *p_spectrum)
207
{
208
209
slice_number = peak.x;
210
211
std::pair<std::map<unsigned int, MassSpectrum>::iterator,
bool
> ret =
212
spectrum_map.insert(
213
std::pair<unsigned int, MassSpectrum>(slice_number,
MassSpectrum
()));
214
215
ret.first->second.push_back(peak);
216
// auto ret = spectrum_map.insert(std::pair<unsigned int,
217
// MassSpectrum>(slice_number,MassSpectrum()));
218
// ret.first->second.push_back(peak);
219
}
220
221
// slices are ready for this retention time
222
storeSlices
(spectrum_map,
m_retentionTimeList
.size() - 1);
223
}
224
}
225
226
endPwizRead
();
227
qDebug();
228
}
229
230
231
void
232
MsRunXicExtractorDisk::storeSlices
(std::map<unsigned int, MassSpectrum> &spectrum_map,
233
std::size_t ipos)
234
{
235
// qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
236
237
for
(
auto
&&spectrum_pair : spectrum_map)
238
{
239
appendSliceOnDisk
(spectrum_pair.first, spectrum_pair.second, ipos);
240
}
241
242
// qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
243
}
244
245
void
246
MsRunXicExtractorDisk::appendSliceOnDisk
(
unsigned
int
slice_number,
247
MassSpectrum
&spectrum,
248
std::size_t ipos)
249
{
250
// qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
251
QFile slice_file(QString(
"%1/%2"
).arg(
mpa_temporaryDirectory
->path()).arg(slice_number));
252
bool
new_file =
false
;
253
if
(!slice_file.exists())
254
{
255
new_file =
true
;
256
}
257
if
(!slice_file.open(QIODevice::WriteOnly | QIODevice::Append))
258
{
259
throw
pappso::PappsoException
(
260
QObject::tr(
"unable to open file %1"
).arg(slice_file.fileName()));
261
}
262
QDataStream stream(&slice_file);
263
264
if
(new_file)
265
{
266
stream << (quint32)slice_number;
267
stream << (quint32)
m_rtSize
;
268
}
269
270
stream << (quint32)ipos;
271
stream << spectrum;
272
273
slice_file.flush();
274
slice_file.close();
275
// qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
276
}
277
278
MsRunSliceSPtr
279
MsRunXicExtractorDisk::unserializeSlice
(
unsigned
int
slice_number)
280
{
281
qDebug();
282
try
283
{
284
std::shared_ptr<MsRunSlice> msrun_slice_sp = std::make_shared<MsRunSlice>(
MsRunSlice
());
285
286
QFile slice_file(QString(
"%1/%2"
).arg(
mpa_temporaryDirectory
->path()).arg(slice_number));
287
if
(!slice_file.exists())
288
{
289
msrun_slice_sp.get()->setSize(
m_rtSize
);
290
msrun_slice_sp.get()->setSliceNumber(slice_number);
291
return
msrun_slice_sp;
292
}
293
if
(!slice_file.open(QIODevice::ReadOnly))
294
{
295
throw
pappso::PappsoException
(
296
QObject::tr(
"unable to open file %1 in readonly"
).arg(slice_file.fileName()));
297
}
298
QDataStream stream(&slice_file);
299
300
stream >> *(msrun_slice_sp.get());
301
302
slice_file.close();
303
304
return
msrun_slice_sp;
305
}
306
catch
(
pappso::PappsoException
&error)
307
{
308
throw
pappso::PappsoException
(
309
QObject::tr(
"error unserializing slice %1:\n%2"
).arg(slice_number).arg(error.
qwhat
()));
310
}
311
qDebug();
312
}
313
314
std::vector<MsRunSliceSPtr>
315
MsRunXicExtractorDisk::acquireSlices
(
const
MzRange
&mz_range)
316
{
317
QMutexLocker lock(&
m_mutex
);
318
std::vector<MsRunSliceSPtr> slice_list;
319
for
(
unsigned
int
i = mz_range.
lower
(); i <= mz_range.
upper
(); i++)
320
{
321
auto
it = std::find_if(
322
m_msRunSliceListCache
.begin(),
323
m_msRunSliceListCache
.end(),
324
[i](
const
MsRunSliceSPtr
&slice_sp) { return slice_sp.get()->getSliceNumber() == i; });
325
if
(it !=
m_msRunSliceListCache
.end())
326
{
327
slice_list.push_back(*it);
328
m_msRunSliceListCache
.push_back(*it);
329
}
330
else
331
{
332
MsRunSliceSPtr
slice_sp =
unserializeSlice
(i);
333
slice_list.push_back(slice_sp);
334
m_msRunSliceListCache
.push_back(slice_sp);
335
}
336
}
337
338
if
(
m_msRunSliceListCache
.size() > 20)
339
{
340
m_msRunSliceListCache
.pop_front();
341
}
342
return
slice_list;
343
}
344
345
346
void
347
MsRunXicExtractorDisk::endPwizRead
()
348
{
349
msp_msrun_reader
.get()->releaseDevice();
350
}
351
}
// namespace pappso
pappso::MassSpectrum
Class to represent a mass spectrum.
Definition
massspectrum.h:70
pappso::MsRunSlice
Definition
msrunslice.h:43
pappso::MsRunXicExtractorDisk::endPwizRead
virtual void endPwizRead()
Definition
msrunxicextractordisk.cpp:347
pappso::MsRunXicExtractorDisk::acquireSlices
std::vector< MsRunSliceSPtr > acquireSlices(const MzRange &mz_range)
retrieve all the slices corresponding to a given mz_range
Definition
msrunxicextractordisk.cpp:315
pappso::MsRunXicExtractorDisk::prepareExtractor
void prepareExtractor()
Definition
msrunxicextractordisk.cpp:67
pappso::MsRunXicExtractorDisk::m_retentionTimeList
std::vector< pappso::pappso_double > m_retentionTimeList
Definition
msrunxicextractordisk.h:101
pappso::MsRunXicExtractorDisk::~MsRunXicExtractorDisk
virtual ~MsRunXicExtractorDisk()
Definition
msrunxicextractordisk.cpp:58
pappso::MsRunXicExtractorDisk::MsRunXicExtractorDisk
MsRunXicExtractorDisk(MsRunReaderSPtr &msrun_reader)
pappso::MsRunXicExtractorDisk::m_msRunSliceListCache
std::deque< MsRunSliceSPtr > m_msRunSliceListCache
Definition
msrunxicextractordisk.h:108
pappso::MsRunXicExtractorDisk::serializeMsRun
void serializeMsRun()
Definition
msrunxicextractordisk.cpp:162
pappso::MsRunXicExtractorDisk::storeSlices
virtual void storeSlices(std::map< unsigned int, MassSpectrum > &slice_vector, std::size_t ipos)
store MassSpectrum slices (by daltons) for a given retention time
Definition
msrunxicextractordisk.cpp:232
pappso::MsRunXicExtractorDisk::m_maxMz
pappso::pappso_double m_maxMz
Definition
msrunxicextractordisk.h:103
pappso::MsRunXicExtractorDisk::m_rtSize
std::size_t m_rtSize
Definition
msrunxicextractordisk.h:106
pappso::MsRunXicExtractorDisk::extractOneXicCoord
void extractOneXicCoord(XicCoord &xic_coord)
Definition
msrunxicextractordisk.cpp:111
pappso::MsRunXicExtractorDisk::m_minMz
pappso::pappso_double m_minMz
Definition
msrunxicextractordisk.h:104
pappso::MsRunXicExtractorDisk::appendSliceOnDisk
void appendSliceOnDisk(unsigned int slice_number, MassSpectrum &spectrum, std::size_t ipos)
append a slice on disk (in a file)
Definition
msrunxicextractordisk.cpp:246
pappso::MsRunXicExtractorDisk::protectedExtractXicCoordSPtrList
virtual void protectedExtractXicCoordSPtrList(UiMonitorInterface &monitor, std::vector< XicCoordSPtr >::iterator it_xic_coord_list_begin, std::vector< XicCoordSPtr >::iterator it_xic_coord_list_end) override
Definition
msrunxicextractordisk.cpp:93
pappso::MsRunXicExtractorDisk::mpa_temporaryDirectory
QTemporaryDir * mpa_temporaryDirectory
Definition
msrunxicextractordisk.h:100
pappso::MsRunXicExtractorDisk::m_mutex
QMutex m_mutex
Definition
msrunxicextractordisk.h:110
pappso::MsRunXicExtractorDisk::unserializeSlice
MsRunSliceSPtr unserializeSlice(unsigned int slice_number)
get one slice from disk by her slice number (dalton)
Definition
msrunxicextractordisk.cpp:279
pappso::MsRunXicExtractorDisk::m_temporaryDirectory
QString m_temporaryDirectory
Definition
msrunxicextractordisk.h:99
pappso::MsRunXicExtractorInterface::m_retentionTimeAroundTarget
double m_retentionTimeAroundTarget
Definition
msrunxicextractorinterface.h:120
pappso::MsRunXicExtractorInterface::m_xicExtractMethod
Enums::XicExtractMethod m_xicExtractMethod
Definition
msrunxicextractorinterface.h:119
pappso::MsRunXicExtractorInterface::msp_msrun_reader
MsRunReaderSPtr msp_msrun_reader
Definition
msrunxicextractorinterface.h:118
pappso::MsRunXicExtractor::MsRunXicExtractor
MsRunXicExtractor(const MsRunXicExtractor &other)
Definition
msrunxicextractor.cpp:73
pappso::MsRunXicExtractor::m_msrun_points
std::vector< MsRunXicExtractorPoints > m_msrun_points
Definition
msrunxicextractor.h:99
pappso::MzRange
Definition
mzrange.h:46
pappso::MzRange::lower
pappso_double lower() const
Definition
mzrange.h:71
pappso::MzRange::upper
pappso_double upper() const
Definition
mzrange.h:77
pappso::MzRange::contains
bool contains(pappso_double) const
Definition
mzrange.cpp:115
pappso::PappsoException
Definition
pappsoexception.h:41
pappso::PappsoException::qwhat
virtual const QString & qwhat() const
Definition
pappsoexception.h:66
pappso::UiMonitorInterface
Definition
uimonitorinterface.h:50
pappso::UiMonitorInterface::count
virtual void count()=0
count steps report when a step is computed in an algorithm
massspectrum.h
msrunxicextractordisk.h
pappso::Enums::XicExtractMethod::sum
@ sum
sum of intensities
Definition
types.h:265
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition
aa.cpp:39
pappso::MsRunReaderSPtr
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition
msrunreader.h:57
pappso::PeptideIonNter::a
@ a
Definition
peptide.h:77
pappso::PeptideIonNter::b
@ b
Definition
peptide.h:74
pappso::MsRunSliceSPtr
std::shared_ptr< const MsRunSlice > MsRunSliceSPtr
Definition
msrunslice.h:40
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition
massspectrum.h:54
pappso::XicCoordSPtr
std::shared_ptr< XicCoord > XicCoordSPtr
Definition
xiccoord.h:44
pappsoexception.h
pappso::DataPoint
Definition
datapoint.h:22
pappso::DataPoint::x
pappso_double x
Definition
datapoint.h:24
pappso::DataPoint::y
pappso_double y
Definition
datapoint.h:25
pappso::XicCoord
coordinates of the XIC to extract and the resulting XIC after extraction
Definition
xiccoord.h:68
pappso::XicCoord::xicSptr
XicSPtr xicSptr
extracted xic
Definition
xiccoord.h:135
pappso::XicCoord::rtTarget
double rtTarget
the targeted retention time to extract around intended in seconds, and related to one msrun....
Definition
xiccoord.h:131
pappso::XicCoord::mzRange
MzRange mzRange
the mass to extract
Definition
xiccoord.h:125
pappsomspp
core
xicextractor
private
msrunxicextractordisk.cpp
Generated on
for libpappsomspp by
1.17.0