pappsomspp
Library for mass spectrometry
msrundatasettreenode.cpp
Go to the documentation of this file.
1 // GPL 3+
2 // Filippo Rusconi
3 
4 /////////////////////// StdLib includes
5 #include <string>
6 #include <vector>
7 #include <iostream>
8 #include <iomanip>
9 
10 
11 /////////////////////// Qt includes
12 #include <QDebug>
13 
14 
15 /////////////////////// pwiz includes
16 
17 
18 /////////////////////// Local includes
19 #include "msrundatasettreenode.h"
20 #include "../utils.h"
21 
22 
23 namespace pappso
24 {
25 
26 
28 {
29 }
30 
31 
33  QualifiedMassSpectrumCstSPtr mass_spectrum_csp,
34  MsRunDataSetTreeNode *parent_p)
35  : mcsp_massSpectrum(mass_spectrum_csp), mp_parent(parent_p)
36 {
37 }
38 
39 
41  : mcsp_massSpectrum(other.mcsp_massSpectrum), mp_parent(other.mp_parent)
42 {
43  for(auto &&node : other.m_children)
44  m_children.push_back(new MsRunDataSetTreeNode(*node));
45 }
46 
47 
49 {
50  for(auto &&node : m_children)
51  delete node;
52 
53  m_children.clear();
54 }
55 
56 
59 {
60  if(this == &other)
61  return *this;
62 
64  mp_parent = other.mp_parent;
65 
66  for(auto &&node : other.m_children)
67  m_children.push_back(new MsRunDataSetTreeNode(*node));
68 
69  return *this;
70 }
71 
72 void
74  QualifiedMassSpectrumCstSPtr qualified_mass_spectrum_csp)
75 {
76  mcsp_massSpectrum = qualified_mass_spectrum_csp;
77 }
78 
79 
82 {
83  return mcsp_massSpectrum;
84 }
85 
86 
87 void
89 {
90  mp_parent = parent;
91 }
92 
93 
96 {
97  // qDebug();
98 
99  return mp_parent;
100 }
101 
102 
103 bool
105 {
106  // qDebug();
107 
108  if(mp_parent != nullptr)
109  return true;
110  else
111  return false;
112 }
113 
114 
115 void
116 MsRunDataSetTreeNode::size(std::size_t &cumulative_node_count) const
117 {
118 
119  // First account for this node.
120  ++cumulative_node_count;
121 
122  // Then ask for each child to recursively account for themselves and their
123  // children.
124 
125  for(auto &&node : m_children)
126  {
127  node->size(cumulative_node_count);
128  }
129 }
130 
131 
133 MsRunDataSetTreeNode::findNode(std::size_t spectrum_index)
134 {
135  // qDebug();
136 
137  // Finding a node that contains a qualified mass spectrum that has been
138  // acquired at spectrum_index, requires checking if that node is not *this and
139  // if not if it is not one of the children. By essence, this work is
140  // recursive.
141 
142  if(mcsp_massSpectrum->getMassSpectrumId().getSpectrumIndex() ==
143  spectrum_index)
144  {
145  // qDebug() << "The mass spectrum's node is this node.";
146 
147  return this;
148  }
149 
150  // qDebug() << "Need to go searching in the children.";
151 
152  for(auto &node : m_children)
153  {
154  MsRunDataSetTreeNode *iterNode = node->findNode(spectrum_index);
155 
156  if(iterNode != nullptr)
157  {
158  // qDebug() << "Found the mass spectrum's node.";
159 
160  return iterNode;
161  }
162  }
163 
164  return nullptr;
165 }
166 
167 
170 {
171  // qDebug();
172 
173  // Finding a node that contains a qualified mass spectrum requires checking if
174  // that node is not *this and if not if it is not one of the children. By
175  // essence, this work is recursive.
176 
177  if(mass_spectrum_csp == mcsp_massSpectrum)
178  {
179  // qDebug() << "The mass spectrum's node is this node.";
180 
181  return this;
182  }
183 
184  // qDebug() << "Need to go searching in the children.";
185 
186  for(auto &node : m_children)
187  {
188  MsRunDataSetTreeNode *iterNode = node->findNode(mass_spectrum_csp);
189 
190  if(iterNode != nullptr)
191  {
192  // qDebug() << "Found the mass spectrum's node.";
193 
194  return iterNode;
195  }
196  }
197 
198  return nullptr;
199 }
200 
201 
202 void
203 MsRunDataSetTreeNode::flattenedView(std::vector<MsRunDataSetTreeNode *> &nodes,
204  bool with_descendants)
205 {
206 
207  // Do store this.
208 
209  nodes.push_back(this);
210 
211  // And now the descendants.
212 
213  if(with_descendants)
214  {
215  for(auto &&node : m_children)
216  {
217  node->flattenedView(nodes, with_descendants);
218  }
219  }
220  else
221  {
222  }
223 }
224 
225 
226 void
228  std::vector<MsRunDataSetTreeNode *> &nodes, bool with_descendants)
229 {
230 
231  // Do not store this, only this->m_children !
232 
233  for(auto &&node : m_children)
234  node->flattenedView(nodes, with_descendants);
235 }
236 
237 
238 void
240  std::size_t ms_level,
241  std::size_t depth,
242  std::vector<MsRunDataSetTreeNode *> &nodes,
243  bool with_descendants)
244 {
245 
246  if(ms_level == (depth + 1))
247  {
248 
249  // There we are. The ms_level that is asked matches the current depth of
250  // the node we are in.
251 
252  flattenedView(nodes, with_descendants);
253  }
254  else if(ms_level > (depth + 1))
255  {
256  // We still do not have to store the nodes, because what we are
257  // searching is down the tree...
258 
259  for(auto &&node : m_children)
260  {
261  node->flattenedViewMsLevelNodes(
262  ms_level, depth + 1, nodes, with_descendants);
263  }
264  }
265 }
266 
267 
268 std::vector<MsRunDataSetTreeNode *>
270  pappso_double precursor_mz,
271  PrecisionPtr precision_ptr,
272  std::vector<MsRunDataSetTreeNode *> &nodes)
273 {
274  if(precision_ptr == nullptr)
275  qFatal(
276  "Fatal error at %s@%d -- %s(). "
277  "precision_ptr cannot be nullptr."
278  "Program aborted.",
279  __FILE__,
280  __LINE__,
281  __FUNCTION__);
282 
283 
284  // Check if this node matches the requirements.
285 
286  pappso_double mz = mcsp_massSpectrum->getPrecursorMz();
287 
288  if(mz != std::numeric_limits<double>::max())
289  {
290 
291  // Calculate the mz range using the tolerance.
292 
293  pappso_double lower_mz =
294  precursor_mz - (precision_ptr->delta(precursor_mz) / 2);
295  pappso_double upper_mz =
296  precursor_mz + (precision_ptr->delta(precursor_mz) / 2);
297 
298  if(mz >= lower_mz && mz <= upper_mz)
299  {
300  // We are iterating in a node that holds a mass spectrum that was
301  // acquired by fragmenting an ion that matches the searched mz value.
302 
303  nodes.push_back(this);
304  }
305  }
306 
307  // Now handle in the same way, but recursively, all the children of this node.
308 
309  for(auto &&node : m_children)
310  {
311  node->productNodesByPrecursorMz(precursor_mz, precision_ptr, nodes);
312  }
313 
314  return nodes;
315 }
316 
317 
318 std::vector<MsRunDataSetTreeNode *>
320  pappso_double precursor_mz,
321  PrecisionPtr precision_ptr,
322  std::vector<MsRunDataSetTreeNode *> &nodes)
323 {
324  if(precision_ptr == nullptr)
325  qFatal(
326  "Fatal error at %s@%d -- %s(). "
327  "precision_ptr cannot be nullptr."
328  "Program aborted.",
329  __FILE__,
330  __LINE__,
331  __FUNCTION__);
332 
333 
334  // Calculate the mz range using the tolerance.
335  pappso_double lower_mz =
336  precursor_mz - (precision_ptr->delta(precursor_mz) / 2);
337  pappso_double upper_mz =
338  precursor_mz + (precision_ptr->delta(precursor_mz) / 2);
339 
340  // Check if this node matches the requirements.
341 
342  pappso_double mz = mcsp_massSpectrum->getPrecursorMz();
343 
344  if(mz != std::numeric_limits<double>::max())
345  {
346  if(mz >= lower_mz && mz <= upper_mz)
347  {
348  // We are iterating in a node that hold a mass spectrum that was
349  // acquired by fragmenting an ion matching the searched mz value. We
350  // can extract the spectrum index of that precursor mass spectrum and
351  // then get its corresponding node, that we'll store.
352 
353  std::size_t precursor_spectrum_index =
354  mcsp_massSpectrum->getPrecursorSpectrumIndex();
355 
356  MsRunDataSetTreeNode *found_node = findNode(precursor_spectrum_index);
357 
358  if(precursor_spectrum_index !=
359  found_node->mcsp_massSpectrum->getMassSpectrumId()
360  .getSpectrumIndex())
361  qFatal(
362  "Fatal error at %s@%d -- %s(). "
363  "."
364  "Program aborted.",
365  __FILE__,
366  __LINE__,
367  __FUNCTION__);
368 
369  nodes.push_back(found_node);
370  }
371  }
372 
373  // Now handle in the same way, but recursively, all the children of this node.
374 
375  for(auto &&node : m_children)
376  {
377  node->precursorIonNodesByPrecursorMz(precursor_mz, precision_ptr, nodes);
378  }
379 
380  return nodes;
381 }
382 
383 
384 void
386 {
387  // qDebug() << "now calling visitor.visit(*this);";
388 
389  visitor.visit(*this);
390 
391  // qDebug() << "and now calling node->accept(visitor) for each child node.";
392 
393  visitor.setNodesToProcessCount(m_children.size());
394 
395  for(auto &&node : m_children)
396  node->accept(visitor);
397 }
398 
399 
400 std::size_t
401 MsRunDataSetTreeNode::depth(std::size_t depth) const
402 {
403 
404  // qDebug() << "Got depth:" << depth;
405 
406  // If there are no children in this node, that is the end of the tree.
407  // Do not change anything an return.
408 
409  if(!m_children.size())
410  {
411  // qDebug() << "No children, returning" << depth;
412 
413  return depth;
414  }
415 
416  // qDebug() << "There are" << m_children.size() << "children nodes";
417 
418 
419  // At this point we know we can already increment depth by one because
420  // we go down one level by iterating in the m_children vector of nodes.
421 
422  // qDebug() << "Children found, incrementing depth to" << depth + 1;
423 
424  std::size_t local_depth = depth + 1;
425 
426  std::size_t tmp_depth = 0;
427  std::size_t greatest_depth = 0;
428 
429  for(auto &node : m_children)
430  {
431  // qDebug() << "In the children for loop";
432 
433  tmp_depth = node->depth(local_depth);
434 
435  // qDebug() << "Got depth from iterated node:" << tmp_depth;
436 
437  if(tmp_depth > greatest_depth)
438  greatest_depth = tmp_depth;
439  }
440 
441  // qDebug() << "Returning:" << greatest_depth;
442 
443  return greatest_depth;
444 }
445 
446 
447 QString
449 {
450  return QString("mcsp_massSpectrum: %1 ; to string: %2 ; children: %3\n")
452  const_cast<QualifiedMassSpectrum *>(mcsp_massSpectrum.get())))
453  .arg(mcsp_massSpectrum->toString())
454  .arg(m_children.size());
455 }
456 
457 } // namespace pappso
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:67
msrundatasettreenode.h
pappso::MsRunDataSetTreeNode::depth
std::size_t depth(std::size_t depth) const
Definition: msrundatasettreenode.cpp:401
pappso
Definition: aa.cpp:38
pappso::MsRunDataSetTreeNode
Definition: msrundatasettreenode.h:32
pappso::MsRunDataSetTreeNode::m_children
std::vector< MsRunDataSetTreeNode * > m_children
Definition: msrundatasettreenode.h:95
pappso::MsRunDataSetTreeNode::mcsp_massSpectrum
QualifiedMassSpectrumCstSPtr mcsp_massSpectrum
Definition: msrundatasettreenode.h:91
pappso::MsRunDataSetTreeNode::MsRunDataSetTreeNode
MsRunDataSetTreeNode()
Definition: msrundatasettreenode.cpp:27
pappso::MsRunDataSetTreeNodeVisitorInterface::visit
virtual bool visit(const MsRunDataSetTreeNode &node)=0
pappso::MsRunDataSetTreeNodeVisitorInterface::setNodesToProcessCount
virtual void setNodesToProcessCount(std::size_t)=0
pappso::MsRunDataSetTreeNode::flattenedView
void flattenedView(std::vector< MsRunDataSetTreeNode * > &nodes, bool with_descendants=false)
Definition: msrundatasettreenode.cpp:203
pappso::MsRunDataSetTreeNode::hasParent
bool hasParent() const
Definition: msrundatasettreenode.cpp:104
pappso::MsRunDataSetTreeNode::precursorIonNodesByPrecursorMz
std::vector< MsRunDataSetTreeNode * > precursorIonNodesByPrecursorMz(pappso_double precursor_mz, PrecisionPtr precision_ptr, std::vector< MsRunDataSetTreeNode * > &nodes)
Definition: msrundatasettreenode.cpp:319
pappso::MsRunDataSetTreeNode::productNodesByPrecursorMz
std::vector< MsRunDataSetTreeNode * > productNodesByPrecursorMz(pappso_double precursor_mz, PrecisionPtr precision_ptr, std::vector< MsRunDataSetTreeNode * > &nodes)
Definition: msrundatasettreenode.cpp:269
pappso::Utils::pointerToString
static QString pointerToString(const void *const pointer)
Definition: utils.cpp:229
pappso::MsRunDataSetTreeNode::flattenedViewMsLevelNodes
void flattenedViewMsLevelNodes(std::size_t ms_level, std::size_t depth, std::vector< MsRunDataSetTreeNode * > &nodes, bool with_descendants=false)
Definition: msrundatasettreenode.cpp:239
pappso::MsRunDataSetTreeNode::getParent
MsRunDataSetTreeNode * getParent() const
Definition: msrundatasettreenode.cpp:95
pappso::MsRunDataSetTreeNode::~MsRunDataSetTreeNode
virtual ~MsRunDataSetTreeNode()
Definition: msrundatasettreenode.cpp:48
pappso::MsRunDataSetTreeNode::findNode
MsRunDataSetTreeNode * findNode(std::size_t spectrum_index)
Definition: msrundatasettreenode.cpp:133
pappso::QualifiedMassSpectrumCstSPtr
std::shared_ptr< const QualifiedMassSpectrum > QualifiedMassSpectrumCstSPtr
Definition: qualifiedmassspectrum.h:61
pappso::MsRunDataSetTreeNode::toString
QString toString() const
Definition: msrundatasettreenode.cpp:448
pappso::MsRunDataSetTreeNode::accept
void accept(MsRunDataSetTreeNodeVisitorInterface &visitor)
Definition: msrundatasettreenode.cpp:385
pappso::PrecisionPtr
const typedef PrecisionBase * PrecisionPtr
Definition: precision.h:141
pappso::MsRunDataSetTreeNode::mp_parent
MsRunDataSetTreeNode * mp_parent
Definition: msrundatasettreenode.h:93
pappso::MsRunDataSetTreeNodeVisitorInterface
Definition: msrundatasettreevisitor.h:23
pappso::MsRunDataSetTreeNode::setParent
void setParent(MsRunDataSetTreeNode *parent)
Definition: msrundatasettreenode.cpp:88
pappso::MsRunDataSetTreeNode::size
void size(std::size_t &cumulative_node_count) const
Definition: msrundatasettreenode.cpp:116
pappso::MsRunDataSetTreeNode::getQualifiedMassSpectrum
QualifiedMassSpectrumCstSPtr getQualifiedMassSpectrum() const
Definition: msrundatasettreenode.cpp:81
pappso::PrecisionUnit::mz
pappso::MsRunDataSetTreeNode::operator=
MsRunDataSetTreeNode & operator=(const MsRunDataSetTreeNode &other)
Definition: msrundatasettreenode.cpp:58
pappso::MsRunDataSetTreeNode::flattenedViewChildrenOnly
void flattenedViewChildrenOnly(std::vector< MsRunDataSetTreeNode * > &nodes, bool with_descendants=false)
Definition: msrundatasettreenode.cpp:227
pappso::MsRunDataSetTreeNode::setQualifiedMassSpectrum
void setQualifiedMassSpectrum(QualifiedMassSpectrumCstSPtr qualified_mass_spectrum_csp)
Definition: msrundatasettreenode.cpp:73