Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
SpectraChromatogram.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "SpectraChromatogram.h"
14 
15 #include "PointsManager.h"
16 #include "PeaksManager.h"
17 #include "Spectrum.h"
18 #include "DataSet.h"
19 #include "DataGroupNode.h"
20 #include "XYDataOperation.h"
21 
22 #include <math.h>
23 #include <set>
24 
25 
26 using namespace kome::objects;
27 
28 
29 #include <crtdbg.h>
30 #ifdef _DEBUG
31  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
32  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
33 #endif // _DEBUG
34 
35 
36 
37 // static member
38 const unsigned char SpectraChromatogram::m_msFilters[] = {
39  0x01,
40  0x02,
41  0x04,
42  0x08,
43  0x10,
44  0x20,
45  0x40,
46  0x80
47 };
48 
49 // constructor
51  : Chromatogram( dataSet.getGroup() == NULL ? NULL : dataSet.getGroup()->getSample() ) {
52  // initialize
53  m_minMz = -1.0;
54  m_maxMz = -1.0;
55  m_minRt = -1.0;
56  m_maxRt = -1.0;
57  m_filter = 0xff;
58  m_tic = true;
59 
60  m_proc = bProces; // add flg @date 2012.10.03 <Add> M.Izumi
61 
62  m_spectra = &dataSet;
63 
64  m_group = dataSet.getGroup();
65  setTitle();
66 }
67 
68 // destructor
70 }
71 
72 // get spectra
74  return *m_spectra;
75 }
76 
77 // set m/z range
78 void SpectraChromatogram::setMzRange( const double minMz, const double maxMz ) {
79  setMinMz( minMz );
80  setMaxMz( maxMz );
81 }
82 
83 // set min m/z
84 void SpectraChromatogram::setMinMz( const double mz ) {
85  if( mz != m_minMz ) {
86  deleteXYData();
87  m_minMz = mz;
88  }
89  setTitle();
90 }
91 
92 // set max m/z
93 void SpectraChromatogram::setMaxMz( const double mz ) {
94  if( mz != m_maxMz ) {
95  deleteXYData();
96  m_maxMz = mz;
97  }
98  setTitle();
99 }
100 
101 // get min m/z
103  return m_minMz;
104 }
105 
106 // get max m/z
108  return m_maxMz;
109 }
110 
111 // set RT range
112 void SpectraChromatogram::setRtRange( const double minRt, const double maxRt ) {
113  setMinRt( minRt );
114  setMaxRt( maxRt );
115 }
116 
117 // set min RT
118 void SpectraChromatogram::setMinRt( const double rt ) {
119  if( rt != m_minRt ) {
120  deleteXYData();
121  m_minRt = rt;
122  }
123 }
124 
125 // set max RT
126 void SpectraChromatogram::setMaxRt( const double rt ) {
127  if( rt != m_maxRt ) {
128  deleteXYData();
129  m_maxRt = rt;
130  }
131 }
132 
133 // get min RT
135  return m_minRt;
136 }
137 
138 // get max RT
140  return m_maxRt;
141 }
142 
143 // set filter
144 void SpectraChromatogram::setFilter( const unsigned char filter ) {
145  if( m_filter != filter ) {
146  deleteXYData();
147  m_filter = filter;
148  }
149 }
150 
151 // get filter
153  return m_filter;
154 }
155 
156 // set TIC mode
158  if( !m_tic ) {
159  deleteXYData();
160  m_tic = true;
161  }
162  setTitle();
163 }
164 
165 // set BPC mode
167  if( m_tic ) {
168  deleteXYData();
169  m_tic = false;
170  }
171  setTitle();
172 }
173 
174 // get TIC mode
176  return m_tic;
177 }
178 
179 // get BPC mode
181  return !m_tic;
182 }
183 
184 // get spectra
185 void SpectraChromatogram::getMemberSpectra( std::vector< Spectrum* >& spectra ) {
186  // get xy data
187  for( unsigned int i = 0; i < m_spectra->getNumberOfSpectra(); i++ ) {
188  // sepectrum
189  Spectrum* spec = m_spectra->getSpectrum( i );
190 
191  // check
192  if( spec->hasChromatogram() ) {
193  const unsigned char stage = m_msFilters[ CLAMP( spec->getMsStage(), 1, 8 ) - 1 ];
194  if( m_filter & stage ) {
195  spectra.push_back( spec );
196  }
197  }
198  }
199 }
200 
201 // search spectrum
202 int SpectraChromatogram::searchSpectrumIndex( std::vector< Spectrum* >& spectra, const double rt, SearchType search ) {
203  // search
204  int idx = -1;
205  double diff = 99999999999.9;
206  for( int i = 0; i < (int)spectra.size(); i++ ) {
207  // spectrum
208  Spectrum* spec = spectra[ i ];
209 
210  //
211  if( search == SEARCH_PREV ) {
212  if( spec->getRt() <= rt ) {
213  idx = i;
214  }
215  }
216  else if( search == SEARCH_NEXT ) {
217  if( spec->getRt() >= rt && idx < 0 ) {
218  idx = i;
219  }
220  }
221  else {
222  double d = fabs( spec->getRt() - rt );
223  if( d < diff ) {
224  idx = i;
225  diff = d;
226  }
227  }
228  }
229 
230  return idx;
231 }
232 
233 // set title
235  // title
236  std::string title;
237 
238  if( !m_proc ){
239  // chromatogram type
240  if( m_tic ) {
241  if( m_minMz < 0.0 && m_maxMz < 0.0 ) {
242  title = "TIC";
243  m_icon = "TIC";
244  }
245  else {
246  title = "MC";
247  m_icon = "MC";
248  }
249 
250  }else{
251  title = "BPC";
252  m_icon = "BPC";
253  }
254  }else{
255  title = "Processed MC";
256  // chromatogram type
257  if( m_tic ) {
258  m_icon = "MC";
259  }else{
260  m_icon = "BPC";
261  }
262  }
263 
264  // range
265  if( m_minMz >= 0.0 && m_maxMz >= 0.0 ) {
266  title.append( FMT( "[%.2f-%.2f]", m_minMz, m_maxMz ) );
267  }
268  else if( m_minMz >= 0.0 ) {
269  title.append( FMT( "[%.2f- ]", m_minMz ) );
270  }
271  else if( m_maxMz >= 0.0 ) {
272  title.append( FMT( "[ -%.2f]", m_maxMz ) );
273  }
274 
275  // name
276  if( m_name.empty() ) {
277  m_title = title;
278  }
279  else {
280  if( m_group != NULL && m_group->getParentGroup() != NULL ) {
281  m_title = FMT( "%s (%s) : %s", m_name.c_str(), m_group->getName(), title.c_str() );
282  }
283  else {
284  m_title = FMT( "%s : %s", m_name.c_str(), title.c_str() );
285  }
286  }
287 }
288 
289 // on get xy data
291  // get spectra
292  std::vector< Spectrum* > spectra;
293  std::set< Spectrum* > specSet;
294  getMemberSpectra( spectra );
295  for( unsigned int i = 0; i < spectra.size(); i++ ) {
296  specSet.insert( spectra[ i ] );
297  }
298 
299  // get xy data
300  for( unsigned int i = 0; i < m_spectra->getNumberOfSpectra(); i++ ) {
301  // spectrum
302  Spectrum* spec = m_spectra->getSpectrum( i );
303 
304  // RT
305  double rt = spec->getRt();
306 
307  // add point
308  if( ( m_minRt < 0.0 || rt >= m_minRt ) && ( m_maxRt < 0.0 || rt <= m_maxRt )
309  && specSet.find( spec ) != specSet.end() ) {
310  // intensity
311  double intensity = 0.0;
312 
313  // @date 2012.10.03 <Mod> M.Izumi ->
314  if( !m_proc ){
315  if( m_tic ) {
316  intensity = spec->getTotalIntensity( m_minMz, m_maxMz );
317  }
318  else {
319  intensity = spec->getMaxIntensity( m_minMz, m_maxMz );
320  }
321  }else{ // Use data processing
323  spec->getXYData( &dps, true );
324 
325  for( unsigned int i=0; i < dps.getLength(); i++ ){
326  if( m_minMz < dps.getX(i) || dps.getX(i) >= m_maxMz ){
327  if( m_tic ){
328  intensity += dps.getY(i); // TIC
329  }else{
330  intensity = std::max( intensity, dps.getY(i) ); // BPC
331  }
332  }
333  }
334 
335  }
336  // @date 2012.10.03 <Mod> M.Izumi <-
337 
338  // add
339  xyData->addPoint( rt, intensity );
340  }
341  }
342 }
343 
344 // on get spectra
346  DataSet& spectra,
347  const double startRt,
348  const double endRt,
349  const SearchType startSearch,
350  const SearchType endSearch
351 ) {
352  // get spectra
353  std::vector< Spectrum* > specArray;
354  getMemberSpectra( specArray );
355 
356  // search index
357  int startIdx = std::max( 0, searchSpectrumIndex( specArray, startRt, startSearch ) );
358  int endIdx = std::min( (int)specArray.size() - 1, searchSpectrumIndex( specArray, endRt, endSearch ) );
359 
360  // get spectra
361  for( int i = startIdx; i <= endIdx; i++ ) {
362  spectra.addSpectrum( specArray[ i ] );
363  }
364 }
365 
366 // on get mass
367 double SpectraChromatogram::onGetMass( const unsigned int index ) {
368  // get spectrum
369  if( index >= m_spectra->getNumberOfSpectra() ) {
370  return -1.0;
371  }
372  Spectrum* spec = m_spectra->getSpectrum( index );
373  if( spec == NULL ) {
374  return -1.0;
375  }
376 
377  return spec->getBasePeakMass();
378 }
379 
380 // on get MS stage
381 int SpectraChromatogram::onGetMsStage( const unsigned int index ) {
382  // get spectrum
383  if( index >= m_spectra->getNumberOfSpectra() ) {
384  return -1;
385  }
386  Spectrum* spec = m_spectra->getSpectrum( index );
387  if( spec == NULL ) {
388  return -1;
389  }
390 
391  return spec->getMsStage();
392 }
393 
394 // on get precursor
395 double SpectraChromatogram::onGetPrecursor( const unsigned int index ) {
396  // get spectrum
397  if( index >= m_spectra->getNumberOfSpectra() ) {
398  return -1.0;
399  }
400  Spectrum* spec = m_spectra->getSpectrum( index );
401  if( spec == NULL ) {
402  return -1.0;
403  }
404 
405  return spec->getPrecursor();
406 }
abstraction class of two dimention coordinate data
Definition: XYData.h:34
bool isTIC()
judges whether chromatogram mode is TIC (Total Ion SpectraChromatogram)
SpectraChromatogram(DataSet &dataSet, bool bProces=false)
constructor
data points data of profile management class
Definition: DataPoints.h:25
interfaces of PointsManager class
double getPrecursor(const int stage)
gets precursor
Definition: Spectrum.cpp:683
double getRt()
gets retention time
Definition: Spectrum.cpp:184
interfaces of PeaksManager class
double getX(const unsigned int index)
gets x coordinate
Definition: XYData.cpp:224
const char * getName()
gets group name
bool isBPC()
judges whether chromatogram mode is BPC (Base Peak SpectraChromatogram)
void setTIC()
sets Total Ion SpectraChromatogram mode
double getMaxMz()
gets maximum value of m/z range
unsigned int getNumberOfSpectra()
gets the number of spectra
Definition: DataSet.cpp:128
double getY(const unsigned int index)
gets y coordinate
Definition: XYData.cpp:243
interfaces of Spectrum class
static const unsigned char m_msFilters[]
void setMaxRt(const double rt)
sets maximum value of RT range
interfaces of XYDataOperation class
DataSet & getMemberSpectra()
gets spectra
Spectrum * getSpectrum(const unsigned int index)
gets the number of spectra
Definition: DataSet.cpp:133
interfaces of DataGroupNode class
virtual void onGetSpectra(DataSet &spectra, const double startRt, const double endRt, const SearchType startSearch, const SearchType endSearch)
This method is called by searchSpectrum or getDataSet method (overriede method)
int getMsStage()
gets ms stage
Definition: Spectrum.cpp:634
double getMinMz()
gets minimum value of m/z range
virtual double onGetPrecursor(const unsigned int index)
This method is called by getPrecursor method (override method)
void setMinMz(const double mz)
sets minimum value of m/z range
interfaces of DataSet class
DataGroupNode * getParentGroup()
get parent spectrum group
interfaces of SpectraChromatogram class
#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
void setMaxMz(const double mz)
sets maximum value of m/z range
void setMzRange(const double minMz, const double maxMz)
sets m/z range
kome::core::XYData * getXYData()
gets xy data from data manager
Definition: Spectrum.cpp:279
SearchType
search type
void setRtRange(const double minRt, const double maxRt)
sets RT range
virtual double onGetMass(const unsigned int index)
This method is called by getMass method (override method)
#define CLAMP(x, low, high)
Definition: CoreMacros.h:35
virtual void onGetXYData(kome::core::XYData *const xyData)
This method is called by getXYData method (override method)
DataGroupNode * getGroup()
gets spectrum group
Definition: DataSet.cpp:65
void setFilter(const unsigned char filter)
sets filter
void deleteXYData()
deletes xy data of data manager
spectrum information management class
Definition: Spectrum.h:30
unsigned char getFilter()
gets filter
one or more spectra management class
Definition: DataSet.h:31
void addPoint(const double x, const double y)
adds point
Definition: XYData.cpp:149
double getMaxRt()
gets maximum value of RT range
double getMinRt()
gets minimum value of RT range
chromatogram information management class
Definition: Chromatogram.h:33
void setMinRt(const double rt)
sets minimum value of RT range
void setBPC()
sets Base Peak SpectraChromatogram mode
virtual int onGetMsStage(const unsigned int index)
This method is called by getMsStage method (override method)
int searchSpectrumIndex(std::vector< Spectrum * > &spectra, const double rt, SearchType search)
searches spectrum
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
virtual void addSpectrum(Spectrum *spec)
adds spectrum to group
Definition: DataSet.cpp:167
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216
bool hasChromatogram()
judges whether this spectrum has chromatogram
Definition: Spectrum.cpp:888