libpappsomspp
Library for mass spectrometry
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
32#include <QDebug>
35
36namespace pappso
37{
38
40 const QDir &temporary_dir)
41 : pappso::MsRunXicExtractor(msrun_reader)
42{
43 mpa_temporaryDirectory = nullptr;
44 m_temporaryDirectory = temporary_dir.absolutePath();
45}
46
49{
50
53 new QTemporaryDir(QString("%1/msrun_%2_")
55 .arg(msp_msrun_reader.get()->getMsRunId().get()->getXmlId()));
56}
57
65
66void
68{
69 qDebug();
70 try
71 {
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
92void
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
110void
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 {
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
161void
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
231void
232MsRunXicExtractorDisk::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
245void
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 {
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
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 {
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 {
309 QObject::tr("error unserializing slice %1:\n%2").arg(slice_number).arg(error.qwhat()));
310 }
311 qDebug();
312}
313
314std::vector<MsRunSliceSPtr>
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(),
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
346void
348{
349 msp_msrun_reader.get()->releaseDevice();
350}
351} // namespace pappso
Class to represent a mass spectrum.
std::vector< MsRunSliceSPtr > acquireSlices(const MzRange &mz_range)
retrieve all the slices corresponding to a given mz_range
std::vector< pappso::pappso_double > m_retentionTimeList
MsRunXicExtractorDisk(MsRunReaderSPtr &msrun_reader)
std::deque< MsRunSliceSPtr > m_msRunSliceListCache
virtual void storeSlices(std::map< unsigned int, MassSpectrum > &slice_vector, std::size_t ipos)
store MassSpectrum slices (by daltons) for a given retention time
void extractOneXicCoord(XicCoord &xic_coord)
void appendSliceOnDisk(unsigned int slice_number, MassSpectrum &spectrum, std::size_t ipos)
append a slice on disk (in a file)
virtual void protectedExtractXicCoordSPtrList(UiMonitorInterface &monitor, std::vector< XicCoordSPtr >::iterator it_xic_coord_list_begin, std::vector< XicCoordSPtr >::iterator it_xic_coord_list_end) override
MsRunSliceSPtr unserializeSlice(unsigned int slice_number)
get one slice from disk by her slice number (dalton)
MsRunXicExtractor(const MsRunXicExtractor &other)
std::vector< MsRunXicExtractorPoints > m_msrun_points
pappso_double lower() const
Definition mzrange.h:71
pappso_double upper() const
Definition mzrange.h:77
bool contains(pappso_double) const
Definition mzrange.cpp:115
virtual const QString & qwhat() const
virtual void count()=0
count steps report when a step is computed in an algorithm
@ sum
sum of intensities
Definition types.h:279
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition msrunreader.h:57
std::shared_ptr< const MsRunSlice > MsRunSliceSPtr
Definition msrunslice.h:40
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
std::shared_ptr< XicCoord > XicCoordSPtr
Definition xiccoord.h:44
pappso_double x
Definition datapoint.h:24
pappso_double y
Definition datapoint.h:25
coordinates of the XIC to extract and the resulting XIC after extraction
Definition xiccoord.h:68
XicSPtr xicSptr
extracted xic
Definition xiccoord.h:135
double rtTarget
the targeted retention time to extract around intended in seconds, and related to one msrun....
Definition xiccoord.h:131
MzRange mzRange
the mass to extract
Definition xiccoord.h:125