libpappsomspp
Library for mass spectrometry
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 "../../pappsoexception.h"
34 #include "../../massspectrum/massspectrum.h"
35 
36 namespace 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 
47 MsRunXicExtractorDisk::MsRunXicExtractorDisk(const MsRunXicExtractorDisk &other)
48  : pappso::MsRunXicExtractor(other)
49 {
50 
51  m_temporaryDirectory = other.m_temporaryDirectory;
52  mpa_temporaryDirectory = new QTemporaryDir(
53  QString("%1/msrun_%2_")
54  .arg(m_temporaryDirectory)
55  .arg(msp_msrun_reader.get()->getMsRunId().get()->getXmlId()));
56 }
57 
59 {
60  if(mpa_temporaryDirectory != nullptr)
61  {
63  }
64 }
65 
66 void
68 {
69  QString filename(msp_msrun_reader.get()->getMsRunId().get()->getFileName());
70  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
71  << filename;
72  try
73  {
74  QByteArray byte_array = filename.toUtf8();
75  std::string res = "";
76  for(char c : byte_array)
77  {
78  res += c;
79  }
81  // msp_msrun_reader = nullptr;
82  }
83  catch(pappso::PappsoException &errora)
84  {
85  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
86  throw pappso::PappsoException(QObject::tr("Error reading file (%1) : %2")
87  .arg(filename)
88  .arg(errora.qwhat()));
89  }
90  catch(std::exception &error)
91  {
92  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
94  QObject::tr("Error reading file (%1) using : %2")
95  .arg(filename)
96  .arg(error.what()));
97  }
98 }
99 
101 MsRunXicExtractorDisk::getXicCstSPtr(const MzRange &mz_range,
102  pappso::pappso_double rt_begin,
103  pappso::pappso_double rt_end)
104 {
105  std::shared_ptr<Xic> msrunxic_sp = std::make_shared<Xic>(Xic());
106 
107  std::vector<MsRunSliceSPtr> slice_list;
108  slice_list = acquireSlices(mz_range);
109 
110  if(slice_list.size() == 0)
111  {
113  QObject::tr("Error getMsRunXicSp slice_list.size() == 0"));
114  }
115 
116  for(std::size_t i = 0; i < m_retentionTimeList.size(); i++)
117  {
118 
119  DataPoint xic_element;
120  xic_element.x = m_retentionTimeList[i];
121  xic_element.y = 0;
122  if((xic_element.x < rt_begin) || (xic_element.x > rt_end))
123  continue;
124 
125  for(auto &&msrun_slice : slice_list)
126  {
127  const MassSpectrum &spectrum = msrun_slice.get()->getSpectrum(i);
128  for(auto &&peak : spectrum)
129  {
130  if(mz_range.contains(peak.x))
131  {
133  {
134  xic_element.y += peak.y;
135  }
136  else
137  {
138  if(xic_element.y < peak.y)
139  {
140  xic_element.y = peak.y;
141  }
142  }
143  }
144  }
145  }
146  msrunxic_sp.get()->push_back(xic_element);
147  }
148 
149  return (msrunxic_sp);
150 }
151 
152 std::vector<XicCstSPtr>
154  const std::vector<MzRange> &mz_range_list)
155 {
156 
157  std::vector<XicCstSPtr> xic_list_return;
158  for(auto &range : mz_range_list)
159  {
160  xic_list_return.push_back(getXicCstSPtr(range, 0, 40000000));
161  }
162  return xic_list_return;
163 }
164 
165 void
167 {
168  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
169  m_minMz = 5000;
170  m_maxMz = 0;
171 
172  unsigned int slice_number;
173  std::map<unsigned int, MassSpectrum> spectrum_map;
174 
175  /*
176  const pwiz::msdata::SpectrumList *p_spectrum_list =
177  p_msdatafile->run.spectrumListPtr.get();
178 
179  std::size_t spectrum_list_size = p_spectrum_list->size();
180  pwiz::msdata::SpectrumPtr pwiz_spectrum;
181  */
182 
183  m_rtSize = m_msrun_points.size();
184 
185 
186  MassSpectrumCstSPtr spectrum;
187  for(auto &&msrun_point : m_msrun_points)
188  {
189 
190  spectrum_map.clear();
191 
192  m_retentionTimeList.push_back(msrun_point.rt);
193 
194  spectrum =
195  msp_msrun_reader.get()->massSpectrumCstSPtr(msrun_point.spectrum_index);
196 
197  const MassSpectrum *p_spectrum = spectrum.get();
198  if(p_spectrum->size() > 0)
199  {
200  if(p_spectrum->begin()->x < m_minMz)
201  {
202  m_minMz = p_spectrum->begin()->x;
203  }
204  // iterate through the m/z-intensity pairs
205 
206  if(p_spectrum->back().x > m_maxMz)
207  {
208  m_maxMz = p_spectrum->back().x;
209  }
210 
211  for(auto &peak : *p_spectrum)
212  {
213 
214  slice_number = peak.x;
215 
216  std::pair<std::map<unsigned int, MassSpectrum>::iterator, bool>
217  ret = spectrum_map.insert(std::pair<unsigned int, MassSpectrum>(
218  slice_number, MassSpectrum()));
219 
220  ret.first->second.push_back(peak);
221  // auto ret = spectrum_map.insert(std::pair<unsigned int,
222  // MassSpectrum>(slice_number,MassSpectrum()));
223  // ret.first->second.push_back(peak);
224  }
225 
226  // slices are ready for this retention time
227  storeSlices(spectrum_map, m_retentionTimeList.size() - 1);
228  }
229  }
230 
231  endPwizRead();
232  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
233 }
234 
235 
236 void
238  std::map<unsigned int, MassSpectrum> &spectrum_map, std::size_t ipos)
239 {
240  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
241 
242  for(auto &&spectrum_pair : spectrum_map)
243  {
244  appendSliceOnDisk(spectrum_pair.first, spectrum_pair.second, ipos);
245  }
246 
247  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
248 }
249 
250 void
251 MsRunXicExtractorDisk::appendSliceOnDisk(unsigned int slice_number,
252  MassSpectrum &spectrum,
253  std::size_t ipos)
254 {
255  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
256  QFile slice_file(
257  QString("%1/%2").arg(mpa_temporaryDirectory->path()).arg(slice_number));
258  bool new_file = false;
259  if(!slice_file.exists())
260  {
261  new_file = true;
262  }
263  if(!slice_file.open(QIODevice::WriteOnly | QIODevice::Append))
264  {
266  QObject::tr("unable to open file %1").arg(slice_file.fileName()));
267  }
268  QDataStream stream(&slice_file);
269 
270  if(new_file)
271  {
272  stream << (quint32)slice_number;
273  stream << (quint32)m_rtSize;
274  }
275 
276  stream << (quint32)ipos;
277  stream << spectrum;
278 
279  slice_file.flush();
280  slice_file.close();
281  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
282 }
283 
285 MsRunXicExtractorDisk::unserializeSlice(unsigned int slice_number)
286 {
287  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
288  try
289  {
290  std::shared_ptr<MsRunSlice> msrun_slice_sp =
291  std::make_shared<MsRunSlice>(MsRunSlice());
292 
293  QFile slice_file(
294  QString("%1/%2").arg(mpa_temporaryDirectory->path()).arg(slice_number));
295  if(!slice_file.exists())
296  {
297  msrun_slice_sp.get()->setSize(m_rtSize);
298  msrun_slice_sp.get()->setSliceNumber(slice_number);
299  return msrun_slice_sp;
300  }
301  if(!slice_file.open(QIODevice::ReadOnly))
302  {
304  QObject::tr("unable to open file %1 in readonly")
305  .arg(slice_file.fileName()));
306  }
307  QDataStream stream(&slice_file);
308 
309  stream >> *(msrun_slice_sp.get());
310 
311  slice_file.close();
312 
313  return msrun_slice_sp;
314  }
315  catch(pappso::PappsoException &error)
316  {
318  QObject::tr("error unserializing slice %1:\n%2")
319  .arg(slice_number)
320  .arg(error.qwhat()));
321  }
322  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
323 }
324 
325 std::vector<MsRunSliceSPtr>
326 MsRunXicExtractorDisk::acquireSlices(const MzRange &mz_range)
327 {
328  QMutexLocker lock(&m_mutex);
329  std::vector<MsRunSliceSPtr> slice_list;
330  for(unsigned int i = mz_range.lower(); i <= mz_range.upper(); i++)
331  {
332  auto it = std::find_if(m_msRunSliceListCache.begin(),
333  m_msRunSliceListCache.end(),
334  [i](const MsRunSliceSPtr &slice_sp) {
335  return slice_sp.get()->getSliceNumber() == i;
336  });
337  if(it != m_msRunSliceListCache.end())
338  {
339  slice_list.push_back(*it);
340  m_msRunSliceListCache.push_back(*it);
341  }
342  else
343  {
344  MsRunSliceSPtr slice_sp = unserializeSlice(i);
345  slice_list.push_back(slice_sp);
346  m_msRunSliceListCache.push_back(slice_sp);
347  }
348  }
349 
350  if(m_msRunSliceListCache.size() > 20)
351  {
352  m_msRunSliceListCache.pop_front();
353  }
354  return slice_list;
355 }
356 
357 
358 void
360 {
361 }
362 } // namespace pappso
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:69
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:76
pappso::MsRunXicExtractorDisk::acquireSlices
std::vector< MsRunSliceSPtr > acquireSlices(const MzRange &mz_range)
retrieve all the slices corresponding to a given mz_range
Definition: msrunxicextractordisk.cpp:347
pappso
Definition: aa.cpp:38
pappso::MsRunXicExtractorDisk::mpa_temporaryDirectory
QTemporaryDir * mpa_temporaryDirectory
Definition: msrunxicextractordisk.h:99
pappso::MassSpectrum
Class to represent a mass spectrum.
Definition: massspectrum.h:91
pappso::MsRunReaderSPtr
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition: msrunreader.h:172
pappso::MsRunXicExtractorDisk::m_msRunSliceListCache
std::deque< MsRunSliceSPtr > m_msRunSliceListCache
Definition: msrunxicextractordisk.h:107
pappso::MsRunXicExtractorDisk::m_mutex
QMutex m_mutex
Definition: msrunxicextractordisk.h:109
pappso::MsRunXicExtractorDisk::m_rtSize
std::size_t m_rtSize
Definition: msrunxicextractordisk.h:105
pappso::MsRunXicExtractorDisk::m_retentionTimeList
std::vector< pappso::pappso_double > m_retentionTimeList
Definition: msrunxicextractordisk.h:100
pappso::MsRunXicExtractorDisk::m_minMz
pappso::pappso_double m_minMz
Definition: msrunxicextractordisk.h:103
pappso::PrecisionUnit::res
@ res
msrunxicextractordisk.h
MsRunReader based XIC extractor featuring disk cache.
pappso::MsRunXicExtractorInterface::m_xicExtractMethod
XicExtractMethod m_xicExtractMethod
Definition: msrunxicextractorinterface.h:106
pappso::MsRunXicExtractorDisk::m_maxMz
pappso::pappso_double m_maxMz
Definition: msrunxicextractordisk.h:102
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:258
pappso::PeptideIonNter::c
@ c
pappso::XicCstSPtr
std::shared_ptr< const Xic > XicCstSPtr
Definition: xic.h:58
pappso::MsRunXicExtractorInterface::msp_msrun_reader
MsRunReaderSPtr msp_msrun_reader
Definition: msrunxicextractorinterface.h:105
pappso::MsRunXicExtractorDisk::prepareExtractor
void prepareExtractor()
Definition: msrunxicextractordisk.cpp:88
pappso::XicExtractMethod::sum
@ sum
sum of intensities
pappso::PappsoException::qwhat
virtual const QString & qwhat() const
Definition: pappsoexception.h:108
pappso::MsRunXicExtractorDisk::~MsRunXicExtractorDisk
virtual ~MsRunXicExtractorDisk()
Definition: msrunxicextractordisk.cpp:79
pappso::MsRunSliceSPtr
std::shared_ptr< const MsRunSlice > MsRunSliceSPtr
Definition: msrunslice.h:60
pappso::MsRunXicExtractorDisk::serializeMsRun
void serializeMsRun()
Definition: msrunxicextractordisk.cpp:187
pappso::MsRunXicExtractorDisk::getXicCstSPtrList
virtual std::vector< XicCstSPtr > getXicCstSPtrList(const std::vector< MzRange > &mz_range_list) override
extract a list of XIC given a list of mass to extract
Definition: msrunxicextractordisk.cpp:174
pappso::MsRunXicExtractorDisk::getXicCstSPtr
virtual XicCstSPtr getXicCstSPtr(const MzRange &mz_range, pappso::pappso_double rt_begin, pappso::pappso_double rt_end) override
get a XIC on this MsRun at the given mass range
Definition: msrunxicextractordisk.cpp:122
pappso::MsRunXicExtractorDisk::unserializeSlice
MsRunSliceSPtr unserializeSlice(unsigned int slice_number)
get one slice from disk by her slice number (dalton)
Definition: msrunxicextractordisk.cpp:306
pappso::MsRunXicExtractor::m_msrun_points
std::vector< MsRunXicExtractorPoints > m_msrun_points
Definition: msrunxicextractor.h:46
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:272
pappso::MsRunXicExtractorDisk::endPwizRead
virtual void endPwizRead()
Definition: msrunxicextractordisk.cpp:380
pappso::MsRunXicExtractorDisk::MsRunXicExtractorDisk
MsRunXicExtractorDisk(MsRunReaderSPtr &msrun_reader)
pappso::PappsoException
Definition: pappsoexception.h:62