libpappsomspp
Library for mass spectrometry
filterchargedeconvolution.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/processing/filters/filtertdfcorrectpeak.cpp
3  * \date 30/09/2020
4  * \author Thomas Renne
5  * \brief Sum peaks and transform mz to fit charge = 1
6  */
7 
8 /*******************************************************************************
9  * Copyright (c) 2020 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  ******************************************************************************/
28 #include <QDebug>
29 #include "../../exception/exceptionnotrecognized.h"
30 
31 using namespace pappso;
32 
34  const QString &strBuildParams)
35 {
36  buildFilterFromString(strBuildParams);
37  // qInfo() << "ChargeDeconvolution created";
38 }
39 
40 
42  : m_precisionPtrZ1(precision_ptr)
43 {
46  m_precisionPtrZ1 = precision_ptr;
48  m_precisionPtrZ1, 0.5);
49 
50  // pappso::PrecisionFactory::getPrecisionDividedBy(m_precisionPtrZ1, 2):
51 
52  // qInfo() << m_precisionPtrZ2->getNominal();
53 }
54 
56  const FilterChargeDeconvolution &other)
57  : m_precisionPtrZ1(other.m_precisionPtrZ1),
58  m_precisionPtrZ2(other.m_precisionPtrZ2)
59 {
62 }
63 
65 {
66  qInfo() << "ChargeDeconvolution destroyed";
67 }
68 
69 
70 void
72  const QString &strBuildParams)
73 {
74  //"chargeDeconvolution|0.02dalton"
75  qDebug();
76  if(strBuildParams.startsWith("chargeDeconvolution|"))
77  {
78  QStringList params =
79  strBuildParams.split("|").back().split(";", QString::SkipEmptyParts);
80 
81  QString precision = params.at(0);
82  m_precisionPtrZ1 =
83  PrecisionFactory::fromString(precision.replace("dalton", " dalton")
84  .replace("ppm", " ppm")
85  .replace("res", " res"));
86  qDebug();
87  m_precisionPtrZ2 =
89  m_precisionPtrZ1, 0.5);
90 
91 
92  m_diffC12C13_z1 = DIFFC12C13;
93  m_diffC12C13_z2 = DIFFC12C13 / 2;
94  }
95  else
96  {
98  QString("building chargeDeconvolution from string %1 is not possible")
99  .arg(strBuildParams));
100  }
101  qDebug();
102 }
103 
104 
105 QString
107 {
108  QString strCode =
109  QString("chargeDeconvolution|%1").arg(m_precisionPtrZ1->toString());
110  strCode.replace(" ", "");
111 
112  return strCode;
113 }
114 
115 Trace &
117 {
118  qDebug();
119  std::vector<FilterChargeDeconvolution::DataPointInfoSp> data_points_info;
120  data_points.sortY();
121  qDebug() << data_points.size();
122  Trace new_trace;
123 
124  for(auto &data_point : data_points)
125  {
126  addDataPointToList(data_points_info, data_point);
127  }
128  computeBestChargeOfDataPoint(data_points_info);
129 
130  // qDebug() << data_points_info.size();
131  computeIsotopeDeconvolution(data_points_info);
132  // qDebug() << data_points_info.size();
133  transformToMonoChargedForAllDataPoint(data_points_info);
134  for(DataPointInfoSp &dpi : data_points_info)
135  {
136  // qDebug() << dpi->new_mono_charge_data_point.x << dpi->z_charge;
137  new_trace.push_back(dpi->new_mono_charge_data_point);
138  }
139 
140  new_trace.sortX();
141  data_points = std::move(new_trace);
142  qDebug() << data_points.size();
143  qDebug();
144  return data_points;
145 }
146 
147 void
149  std::vector<FilterChargeDeconvolution::DataPointInfoSp> &points,
150  pappso::DataPoint &data_point) const
151 {
152  DataPointInfoSp new_dpi(std::make_shared<DataPointInfo>());
153 
154  new_dpi->data_point = data_point;
155  MzRange range1(data_point.x + m_diffC12C13_z1, m_precisionPtrZ1);
156  new_dpi->z1_range = std::pair<double, double>(range1.lower(), range1.upper());
157  MzRange range2(data_point.x + m_diffC12C13_z2, m_precisionPtrZ2);
158  new_dpi->z2_range = std::pair<double, double>(range2.lower(), range2.upper());
159  addDataPointRefByExclusion(points, new_dpi);
160  points.push_back(new_dpi);
161 }
162 
163 void
165  std::vector<FilterChargeDeconvolution::DataPointInfoSp> &points,
167 {
168  // add datapoint which match the mz_range = 1 to z1_list
169  auto i_z1 = points.begin(), end = points.end();
170  while(i_z1 != end)
171  {
172  // get the datapoint which match the range
173  i_z1 = std::find_if(i_z1, end, [&new_dpi](DataPointInfoSp dpi) {
174  return (new_dpi->data_point.x >= dpi->z1_range.first &&
175  new_dpi->data_point.x <= dpi->z1_range.second);
176  });
177  if(i_z1 != end)
178  {
179  // add the datapoint to the list and add the parent pointer
180  i_z1->get()->z1_vect.push_back(new_dpi);
181  new_dpi->parent = *i_z1;
182  DataPointInfoSp parent_z1 = i_z1->get()->parent.lock();
183  while(parent_z1 != nullptr)
184  {
185  parent_z1.get()->z1_vect.push_back(new_dpi);
186  parent_z1 = parent_z1->parent.lock();
187  }
188  i_z1++;
189  }
190  }
191 
192  // add datapoint which match the mz_range = 2 to z2_list
193  auto i_z2 = points.begin();
194  while(i_z2 != end)
195  {
196  // get the datapoint which match the range
197  i_z2 = std::find_if(i_z2, end, [&new_dpi](DataPointInfoSp dpi) {
198  return (new_dpi->data_point.x >= dpi->z2_range.first &&
199  new_dpi->data_point.x <= dpi->z2_range.second);
200  });
201  if(i_z2 != end)
202  {
203  // add the datapoint to the list and add the parent pointer
204  i_z2->get()->z2_vect.push_back(new_dpi);
205  new_dpi->parent = *i_z2;
206  DataPointInfoSp parent_z2 = i_z2->get()->parent.lock();
207  while(parent_z2 != nullptr)
208  {
209  parent_z2.get()->z2_vect.push_back(new_dpi);
210  parent_z2 = parent_z2->parent.lock();
211  }
212  i_z2++;
213  }
214  }
215 }
216 
217 void
219  std::vector<FilterChargeDeconvolution::DataPointInfoSp> &data_points_info)
220  const
221 {
222  for(DataPointInfoSp &data_point_info : data_points_info)
223  {
224  if(data_point_info.get()->z1_vect.size() >= 1 &&
225  data_point_info.get()->z2_vect.size() == 0)
226  {
227  for(std::weak_ptr<DataPointInfo> other :
228  data_point_info.get()->z1_vect)
229  {
230  other.lock()->z_charge = 1;
231  }
232  data_point_info.get()->z_charge = 1;
233  }
234  else if(data_point_info.get()->z1_vect.size() == 0 &&
235  data_point_info.get()->z2_vect.size() >= 1)
236  {
237  for(std::weak_ptr<DataPointInfo> other :
238  data_point_info.get()->z2_vect)
239  {
240  other.lock()->z_charge = 2;
241  }
242  data_point_info.get()->z_charge = 2;
243  }
244  else if(data_point_info.get()->z1_vect.size() >= 1 &&
245  data_point_info.get()->z2_vect.size() >= 1)
246  {
247  for(std::weak_ptr<DataPointInfo> other :
248  data_point_info.get()->z2_vect)
249  {
250  other.lock()->z_charge = 2;
251  }
252  data_point_info.get()->z_charge = 2;
253  }
254  else
255  {
256  if(data_point_info.get()->z_charge == -1)
257  {
258  data_point_info.get()->z_charge = 0;
259  }
260  }
261  }
262 }
263 
264 void
266  std::vector<FilterChargeDeconvolution::DataPointInfoSp> &data_points_info)
267  const
268 {
269  std::vector<FilterChargeDeconvolution::DataPointInfoSp>
270  deconvoluted_points_info;
271 
272  for(DataPointInfoSp &data_point_info : data_points_info)
273  {
274  if(data_point_info->parent.lock() == nullptr)
275  {
276  DataPointInfoSp deconvoluted_point(std::make_shared<DataPointInfo>());
277 
278  deconvoluted_point->z_charge = data_point_info->z_charge;
279  deconvoluted_point->new_mono_charge_data_point =
280  data_point_info->data_point;
281 
282  if(data_point_info->z_charge == 1)
283  {
284 
285  for(std::weak_ptr<DataPointInfo> data : data_point_info->z1_vect)
286  {
287  deconvoluted_point->new_mono_charge_data_point.y +=
288  data.lock()->data_point.y;
289  }
290  }
291  else if(data_point_info->z_charge == 2)
292  {
293  for(std::weak_ptr<DataPointInfo> data : data_point_info->z2_vect)
294  {
295  deconvoluted_point->new_mono_charge_data_point.y +=
296  data.lock()->data_point.y;
297  }
298  }
299  else // if z.charge == 0
300  {
301  deconvoluted_point->new_mono_charge_data_point =
302  data_point_info->data_point;
303  }
304  deconvoluted_points_info.push_back(deconvoluted_point);
305  }
306  }
307  data_points_info = deconvoluted_points_info;
308 }
309 
310 void
312  std::vector<FilterChargeDeconvolution::DataPointInfoSp> &data_points_info)
313  const
314 {
315  for(DataPointInfoSp &dpi : data_points_info)
316  {
317  if(dpi->z_charge == 2)
318  {
319  dpi->new_mono_charge_data_point.x +=
320  dpi->new_mono_charge_data_point.x - MHPLUS;
321  }
322  }
323 }
excetion to use when an item type is not recognized
void addDataPointRefByExclusion(std::vector< FilterChargeDeconvolution::DataPointInfoSp > &points, FilterChargeDeconvolution::DataPointInfoSp &new_dpi) const
For each datapointInfo add the datapoint to the lists by their exclusion range.
void buildFilterFromString(const QString &strBuildParams) override
build this filer using a string
std::shared_ptr< DataPointInfo > DataPointInfoSp
void computeBestChargeOfDataPoint(std::vector< FilterChargeDeconvolution::DataPointInfoSp > &data_points_info) const
Compare both list (z1 and z2) and add the right level of charge.
FilterChargeDeconvolution(PrecisionPtr precision_ptr)
void transformToMonoChargedForAllDataPoint(std::vector< FilterChargeDeconvolution::DataPointInfoSp > &data_points_info) const
For eache datapointInfo with a charge = 2 transform the peak to a charge = 1 by multiplying the mz by...
void addDataPointToList(std::vector< FilterChargeDeconvolution::DataPointInfoSp > &points, DataPoint &data_point) const
Add each datapoint to a vector of structure describe above.
Trace & filter(Trace &data_points) const override
get all the datapoints and remove different isotope and add their intensity and change to charge = 1 ...
void computeIsotopeDeconvolution(std::vector< FilterChargeDeconvolution::DataPointInfoSp > &data_points_info) const
For eache datapointInfo whith no parent copy info in new vector with the intensity of the monoistipic...
pappso_double lower() const
Definition: mzrange.h:72
pappso_double upper() const
Definition: mzrange.h:78
static PrecisionPtr fromString(const QString &str)
get a precision pointer from a string
Definition: precision.cpp:71
static PrecisionPtr getPrecisionPtrFractionInstance(PrecisionPtr origin, double fraction)
get the fraction of an existing precision pointer
Definition: precision.cpp:202
A simple container of DataPoint instances.
Definition: trace.h:132
void sortY()
Definition: trace.cpp:886
void sortX()
Definition: trace.cpp:878
Sum peaks and transform mz to fit charge = 1.
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
const pappso_double MHPLUS(1.007276466879)
const pappso_double DIFFC12C13(1.0033548378)
pappso_double x
Definition: datapoint.h:22