Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
AveragedSpectrum.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "AveragedSpectrum.h"
14 
15 #include "Sample.h"
16 #include "DataGroupNode.h"
17 
18 #include <math.h>
19 #include <list>
20 #include <utility>
21 
22 
23 using namespace kome::objects;
24 
25 
26 #include <crtdbg.h>
27 #ifdef _DEBUG
28  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
29  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
30 #endif // _DEBUG
31 
32 
33 
34 // constructor
36  : Spectrum( ( group == NULL ? NULL : group->getSample() ), "" ) {
37  m_d = 0.05;
38  setGroup( group );
39 }
40 
41 // destructor
43 }
44 
45 // add spectrum
47  // update stage
48  if( m_spectra.size() == 0 ) {
49  setMsStage( spectrum->getMsStage() );
50  }
51  else if( spectrum->getMsStage() != getMsStage() ) {
52  setMsStage( -1 );
53  }
54 
55  // update RT
56  double startRt = getStartRt();
57  if( spectrum->getStartRt() >= 0.0
58  && ( spectrum->getStartRt() < startRt || startRt < 0.0 ) ) {
59  setStartRt( spectrum->getStartRt() );
60  }
61  double endRt = getEndRt();
62  if( spectrum->getEndRt() >= 0.0
63  && ( spectrum->getEndRt() > endRt || endRt < 0.0 ) ) {
64  setEndRt( spectrum->getEndRt() );
65  }
66 
67  // TIC, BPC, BPI
68  if( m_spectra.size() == 0 ) {
69  setTotalIntensity( spectrum->getTotalIntensity() );
70  setMaxIntensity( spectrum->getMaxIntensity() );
71  setBasePeakMass( spectrum->getBasePeakMass() );
72  }
73  else {
74  // TIC
75  double tic = getTotalIntensity() * (double)m_spectra.size();
76  tic += spectrum->getTotalIntensity();
77  tic /= (double)( m_spectra.size() + 1 );
78  setTotalIntensity( tic );
79 
80  // BPC, BPI
81  if( spectrum->getMaxIntensity() > getMaxIntensity() ) {
82  setMaxIntensity( spectrum->getMaxIntensity() );
83  setBasePeakMass( spectrum->getBasePeakMass() );
84  }
85  }
86 
87  // precursor
88  double precursor = 0.0;
89  int precCnt = 0;
90  for( unsigned int i = 0; i < m_spectra.size(); i++ ) {
91  double tmpPrec = m_spectra[ i ]->getPrecursor();
92  if( tmpPrec > 0.0 ) {
93  precursor += tmpPrec;
94  precCnt++;
95  }
96  }
97  if( precCnt > 0 ) {
98  precursor /= (double)precCnt;
99  setPrecursor( precursor );
100  }
101 
102  // add spectrum to array
103  m_spectra.push_back( spectrum );
104 
105  // update title @date 2011.02.07 <Add> M.Izumi
106  updateTitle();
107 }
108 
109 // get the number of spectra
111  return m_spectra.size();
112 }
113 
114 // get spectrum
115 Spectrum* AveragedSpectrum::getSpectrum( const unsigned int idx ) {
116  if( idx >= m_spectra.size() ) {
117  return NULL;
118  }
119  return m_spectra[ idx ];
120 }
121 
122 // set merge distance
123 void AveragedSpectrum::setMergeDistance( const double d ) {
124  m_d = std::max( d, 0.0 );
125 }
126 
127 // get merge distance
129  return m_d;
130 }
131 
132 // update title
134  // name
135  std::string name = "Average Spectrum";
136  if( getStartRt() >= 0.0 && getEndRt() >= 0.0 ) {
137  name += FMT( " [%.4f - %.4f]", getStartRt(), getEndRt() );
138  }
139  setName( name.c_str() );
140 
141  // title
142  name += FMT( " %d spectra", m_spectra.size() );
143  setTitle( name.c_str() );
144 }
145 
146 // get xy data
148  kome::core::XYData* const xyData,
149  const double minX,
150  const double maxX
151 ) {
152  // check spectra
153  if( m_spectra.size() == 0 ) {
154  return;
155  }
156 
157  bool isProfile = false;;
158  for( unsigned int i = 0; i < m_spectra.size() && !isProfile; i++ ) {
159  if( !m_spectra[ i ]->isCentroidMode() ) {
160  isProfile = true;
161  }
162  }
163 
164  setCentroidMode( !isProfile );
165 
166 
167  // data points
169  if( m_spectra.size() > 0 ) {
170  getAveragedDataPoints( &( m_spectra[ 0 ] ), m_spectra.size(), dps );
171 
172  for( unsigned int i = 0; i < dps.getLength(); i++ ) {
173  const double x = dps.getX( i );
174  const double y = dps.getY( i ) / (double)m_spectra.size();
175 
176  if( ( minX < 0.0 || x >= minX ) && ( maxX < 0.0 || x <= maxX ) ) {
177  xyData->addPoint( x, y );
178  }
179  }
180  }
181 }
182 
183 // get x range
184 void AveragedSpectrum::onGetXRange( double* minX, double* maxX ) {
185  double minXX = double();
186  double maxXX = double();
187 
188  // get x range
189  if( m_spectra.size() > 0 ) {
190  minXX = m_spectra[ 0 ]->getMinX();
191  maxXX = m_spectra[ 0 ]->getMaxX();
192 
193  for( unsigned int i = 1; i < m_spectra.size(); i++ ) {
194  double tmpMinX = m_spectra[ i ]->getMinX();
195  double tmpMaxX = m_spectra[ i ]->getMaxX();
196 
197  minXX = MIN( minXX, tmpMinX );
198  maxXX = MAX( maxXX, tmpMaxX );
199  }
200  }
201 
202  // store
203  *minX = minXX;
204  *maxX = maxXX;
205 }
206 
207 // get total intensity
208 double AveragedSpectrum::onGetTotalIntensity( const double minX, const double maxX ) {
209  // get total intensity
210  double intensity = 0.0;
211  for( unsigned int i = 0; i < m_spectra.size(); i++ ) {
212  intensity += m_spectra[ i ]->getTotalIntensity( minX, maxX );
213  }
214  intensity /= (double)m_spectra.size();
215 
216  return intensity;
217 }
218 
219 // get max intensity
220 double AveragedSpectrum::onGetMaxIntensity( const double minX, const double maxX ) {
221  // get max intensity
222  double intensity = 0.0;
223  for( unsigned int i = 0; i < m_spectra.size(); i++ ) {
224  double y = m_spectra[ i ]->getMaxIntensity( minX, maxX );
225  if( y > intensity ) {
226  intensity = y;
227  }
228  }
229 
230  return intensity;
231 }
232 
233 // get the averaged data points
234 void AveragedSpectrum::getAveragedDataPoints( kome::objects::Spectrum** spectra, const unsigned int num, kome::core::XYData& xyData ) {
235  // check parameters
236  if( spectra == NULL || num == 0 ) {
237  return;
238  }
239 
240  // get data points
241  if( num == 1 ) {
242  spectra[ 0 ]->getXYData( &xyData, -1.0, -1.0, false );
243  }
244  else {
245  unsigned int mid = num / 2;
246 
248  getAveragedDataPoints( spectra, mid, dps0 );
249 
251  getAveragedDataPoints( spectra + mid, num - mid, dps1 );
252 
253  mergeDataPoints( dps0, dps1, xyData );
254  }
255 }
256 
257 // merge data points
259  // points
260  class Point {
261  public:
262  Point() {};
263  virtual ~Point() {};
264  public:
265  double x;
266  double y;
267  int spec;
268  public:
269  static bool lessPoint( Point& p0, Point& p1 ) {
270  return ( p0.x < p1.x );
271  };
272  };
273  std::vector< Point > points;
274 
275  double d = 0.05;
276  double prevX = -1.0;
277 
278  // first data points
279  const unsigned int len0 = src0.getLength();
280  for( unsigned int i = 0; i < len0; i++ ) {
281  const double x = src0.getX( i );
282  const double y = src0.getY( i );
283  if( y > 0.0 ) {
284  points.resize( points.size() + 1 );
285  points.back().x = x;
286  points.back().y = y;
287  points.back().spec = 0;
288 
289  if( prevX > 0.0 ) {
290  double tmpD = fabs( x - prevX );
291  if( tmpD < d ) {
292  d = tmpD;
293  }
294  }
295  prevX = x;
296  }
297  }
298 
299  // second data points
300  const unsigned int len1 = src1.getLength();
301  for( unsigned int i = 0; i < len1; i++ ) {
302  const double x = src1.getX( i );
303  const double y = src1.getY( i );
304  if( y > 0.0 ) {
305  points.resize( points.size() + 1 );
306  points.back().x = x;
307  points.back().y = y;
308  points.back().spec = 1;
309 
310  if( prevX > 0.0 ) {
311  double tmpD = fabs( x - prevX );
312  if( tmpD < d ) {
313  d = tmpD;
314  }
315  }
316  prevX = x;
317  }
318  }
319 
320  // sort
321  std::sort( points.begin(), points.end(), Point::lessPoint );
322 
323  // add point
324  std::set< int > specSet;
325  double sumX = 0.0;
326  double sumY = 0.0;
327  for( unsigned int i = 0; i < points.size(); i++ ) {
328  const double x = points[ i ].x;
329  const double y = points[ i ].y;
330  const int spec = points[ i ].spec;
331 
332  if( !specSet.empty() ) {
333  double tmpX = sumX / sumY;
334  if( specSet.find( spec ) != specSet.end() || tmpX + d < x ) {
335  double tmpY = sumY;
336  dst.addPoint( tmpX, tmpY );
337  specSet.clear();
338  }
339  }
340 
341  if( specSet.empty() ) {
342  sumX = x * y;
343  sumY = y;
344  }
345  else {
346  sumX = sumX + x * y;
347  sumY = sumY + y;
348  }
349 
350  specSet.insert( spec );
351  }
352 
353  if( !specSet.empty() ) {
354  double tmpX = sumX / sumY;
355  double tmpY = sumY;
356  dst.addPoint( tmpX, tmpY );
357  }
358 }
double getStartRt()
gets the start of retention time
Definition: Spectrum.cpp:191
abstraction class of two dimention coordinate data
Definition: XYData.h:34
void setTotalIntensity(const double intensity)
sets total intensity of spectrum
Definition: Spectrum.cpp:505
void setMaxIntensity(const double intensity)
sets max intensity of spectrum
Definition: Spectrum.cpp:537
Spectrum * getSpectrum(const unsigned int idx)
gets spectrum
group of spectrum management class
Definition: DataGroupNode.h:33
static void mergeDataPoints(kome::core::XYData &src0, kome::core::XYData &src1, kome::core::XYData &dst)
merges data points
data points data of profile management class
Definition: DataPoints.h:25
std::vector< Spectrum * > m_spectra
static void getAveragedDataPoints(kome::objects::Spectrum **spectra, const unsigned int num, kome::core::XYData &xyData)
gets the averaged data points
double getX(const unsigned int index)
gets x coordinate
Definition: XYData.cpp:224
void setPrecursor(const int stage, const double precursor)
sets precursor
Definition: Spectrum.cpp:640
virtual double onGetMaxIntensity(const double minX, const double maxX)
This method is called by getMaxIntensity method. (override method)
void setEndRt(const double rt)
sets end retention time
Definition: Spectrum.cpp:172
void setName(const char *name)
sets spectrum name
Definition: Spectrum.cpp:113
virtual ~AveragedSpectrum()
destructor
void setCentroidMode(const bool centroidMode)
sets centroid mode or not
Definition: Spectrum.cpp:935
virtual double onGetTotalIntensity(const double minX, const double maxX)
This method is called by getTotalIntensity method. (override method)
virtual void onGetXRange(double *minX, double *maxX)
This method is called by getMinX or getMaxX method. (override method)
double getY(const unsigned int index)
gets y coordinate
Definition: XYData.cpp:243
virtual void onGetXYData(kome::core::XYData *const xyData, const double minX, const double maxX)
This method is called by getXYData method. (override method)
interfaces of AveragedSpectrum class
interfaces of DataGroupNode class
int getMsStage()
gets ms stage
Definition: Spectrum.cpp:634
interfaces of Sample class
void setMergeDistance(const double d)
sets the merge disntace
#define MIN(x, y)
Definition: CoreMacros.h:30
void addSpectrum(Spectrum *spectrum)
add spectrum to array
#define NULL
Definition: CoreMacros.h:18
double getTotalIntensity(const double minX=-1.0, const double maxX=-1.0)
gets total intensity in specified range
Definition: Spectrum.cpp:516
AveragedSpectrum(DataGroupNode *group)
constructor
kome::core::XYData * getXYData()
gets xy data from data manager
Definition: Spectrum.cpp:279
#define MAX(x, y)
Definition: CoreMacros.h:27
void setMsStage(const int stage)
sets ms stage
Definition: Spectrum.cpp:587
void setStartRt(const double rt)
sets start retention time
Definition: Spectrum.cpp:160
void setBasePeakMass(const double mass)
sets base peak mass
Definition: Spectrum.cpp:569
double getMergeDistance()
gets the merge distance
spectrum information management class
Definition: Spectrum.h:30
unsigned int getNumberOfSpectra()
gets the number of spectra
double getEndRt()
gets the end of retention time
Definition: Spectrum.cpp:197
void addPoint(const double x, const double y)
adds point
Definition: XYData.cpp:149
void setGroup(DataGroupNode *group)
sets spectrum group
Definition: Spectrum.cpp:893
void setTitle(const char *title)
sets spectrum title
Definition: Spectrum.cpp:215
bool isCentroidMode()
judget wheter this spectrum is centroid mode
Definition: Spectrum.cpp:941
double getMaxIntensity(const double minX=-1.0, const double maxX=-1.0)
gets max intensity in specified range
Definition: Spectrum.cpp:548
double getBasePeakMass()
gets base peak mass
Definition: Spectrum.cpp:581
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216