Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Sample.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "Sample.h"
14 
15 #include "SampleSet.h"
16 #include "DataGroupNode.h"
17 #include "DataManager.h"
18 #include "ActiveObjectsManager.h"
19 #include "Chromatogram.h"
20 #include "Spectrum.h"
21 #include "Peaks.h"
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 // static member
34 std::vector< std::string > Sample::m_commonProps;
35 int Sample::m_optSampleId = 0;
36 
37 // constructor
38 Sample::Sample( SampleSet* sampleSet ) {
39  // initialize
40  m_type = "MS";
41  m_sampleSet = sampleSet;
42  m_sampleIdx = -1;
43  m_specId = 0;
44  m_opened = false;
45  m_edited = false;
46  m_root = NULL;
47  m_parallelReadable = false;
48  m_supportedApiPeaks = false;
49 
50  // issue sample id
52 }
53 
54 // destructor
56  // close
57  DataManager::closeSample( this, true );
58 
59  // delete spectra
60  if( m_root != NULL ) {
61  delete m_root;
62  m_root = NULL;
63  m_opened = false;
64  m_edited = false;
65  }
66 
67  // delete hidden objects
68  for( unsigned int i = 0; i < m_hiddenSet.getNumberOfSpectra(); i++ ) {
69  delete m_hiddenSet.getSpectrum( i );
70  }
71  for( unsigned int i = 0; i < m_hiddenSet.getNumberOfChromatograms(); i++ ) {
72  delete m_hiddenSet.getChromatogram( i );
73  }
74 }
75 
76 // get sample set
78  return m_sampleSet;
79 }
80 
81 // set sample index
82 void Sample::setSampleIndex( const int index ) {
83  m_sampleIdx = index;
84 }
85 
86 // get sample index
88  return m_sampleIdx;
89 }
90 
91 // set sample name
92 void Sample::setName( const char* name ) {
93  m_name = NVL( name, "" );
94 
95  // property
96  if( m_root != NULL ) {
97  m_root->getProperties().setValue( "Sample Name", m_name.c_str() );
98  }
99 }
100 
101 // get sample name
102 const char* Sample::getName() {
103  return m_name.c_str();
104 }
105 
106 // set sample type
107 void Sample::setType( const char* type ) {
108  m_type = NVL( type, "" );
109 
110  // property
111  if( m_root != NULL ) {
112  m_root->getProperties().setValue( "Sample Type", m_type.c_str() );
113  }
114 }
115 
116 // get sample type
117 const char* Sample::getType() {
118  return m_type.c_str();
119 }
120 
121 // set instrument name
122 void Sample::setInstrument( const char* instrument ) {
123  // clear
124  m_instrument.clear();
125  if( instrument == NULL ) {
126  return;
127  }
128 
129  // instrument name
130  std::string instStr = trimstring( instrument );
131  bool prevSpace = false;
132  for( unsigned int i = 0; i < instStr.size(); i++ ) {
133  char c = instStr[ i ];
134  if( isalpha( c ) || isdigit( c ) ) {
135  prevSpace = false;
136  c = (char)toupper( c );
137  m_instrument.push_back( c );
138  }
139  else if( !prevSpace ) {
140  prevSpace = true;
141  m_instrument.push_back( '-' );
142  }
143  }
144 
145  // set property
146  if( m_root != NULL ) {
147  m_root->getProperties().setValue( "Instrument", m_instrument.c_str() );
148  }
149 }
150 
151 // get instrument name
152 const char* Sample::getInstrument() {
153  return m_instrument.c_str();
154 }
155 
156 // set MS company
157 void Sample::setMsCompany( const char* company ) {
158  m_company = NVL( company, "" );
159 
160  if( m_root != NULL ) {
161  m_root->getProperties().setValue( "Manufacturer", m_company.c_str() );
162  }
163 }
164 
165 // get MS company
166 const char* Sample::getMsCompany() {
167  return m_company.c_str();
168 }
169 
170 // set software name
171 void Sample::setSoftwareName( const char* name ) {
172  m_softwareName = NVL( name, "" );
173 
174  if( m_root != NULL ) {
175  m_root->getProperties().setValue( "Software Name", m_softwareName.c_str() );
176  }
177 }
178 
179 // get software namee
180 const char* Sample::getSoftwareName() {
181  return m_softwareName.c_str();
182 }
183 
184 // set software version
185 void Sample::setSoftwareVersion( const char* version ) {
186  m_softwareVersion = NVL( version, "" );
187 
188  if( m_root != NULL ) {
189  m_root->getProperties().setValue( "Software Version", m_softwareVersion.c_str() );
190  }
191 }
192 
193 // get software version
195  return m_softwareVersion.c_str();
196 }
197 
198 // set opened flag value
199 void Sample::setOpened( const bool opened ) {
200  m_opened = opened;
201 }
202 
203 // judges whether this sample is opened or not
205  return m_opened;
206 }
207 
208 // set edited flag value
209 void Sample::setEdited( const bool edited ) {
210  m_edited = edited;
211 }
212 
213 // judges whether this sample is edited or not
215  return m_edited;
216 }
217 
218 // spectrum group
220  return m_root;
221 }
222 
223 // hiddent data set
225  return &m_hiddenSet;
226 }
227 
228 // get the number of spectrum groups
230  return m_groups.size();
231 }
232 
233 // get spectrum group
234 DataGroupNode* Sample::getGroup( const unsigned int index ) {
235  if( index >= m_groups.size() ) {
236  return NULL;
237  }
238  return m_groups[ index ];
239 }
240 
241 // get groups
242 void Sample::getGroups( DataGroupNode* parentGroup ) {
243  if( parentGroup == NULL ) { // all groups
244  // initialize
245  m_groups.clear();
246 
247  if( m_root != NULL ) {
248  getGroups( m_root );
249  }
250  }
251  else {
252  // add
253  if( parentGroup->getNumberOfChildren() == 0 ) { // leaf
254  // set index
255  parentGroup->setGroupIndex( (int)m_groups.size() );
256  m_groups.push_back( parentGroup );
257  }
258 
259  // add children
260  for( unsigned int i = 0; i < parentGroup->getNumberOfChildren(); i++ ) {
261  // child
262  DataGroupNode* child = parentGroup->getChild( i );
263  if( child != NULL ) {
264  getGroups( child );
265  }
266  }
267  }
268 }
269 
270 // set parallel readable
271 void Sample::setParallelReadable( const bool readable ) {
272  m_parallelReadable = readable;
273 }
274 
275 // get parallel readable
277  return m_parallelReadable;
278 }
279 
280 // open sample
282  if( m_opened ) { // already opened
283  return true;
284  }
285 
286  // active objects manager
288 
289  // create root group
290  if( m_root != NULL ) {
291  delete m_root;
292  m_groups.clear();
293  }
294  std::string groupName = m_name;
295 
296  std::string fileName;
297  if( m_sampleSet != NULL ) {
298  fileName = m_sampleSet->getFileName();
299  }
300 
301  if( !fileName.empty() ) {
302  if( !groupName.empty() ) {
303  groupName.append( " : " );
304  }
305  groupName.append( fileName );
306  }
307 
308  if( m_sampleSet != NULL && m_sampleSet->getNumberOfSamples() > 1 ) {
309  groupName.append( FMT( "(%d)", this->m_sampleIdx ) );
310  }
311 
312  m_root = new DataGroupNode( this, groupName.c_str() );
313 
314  // sample set
316  if( m_sampleSet != NULL ) {
317  m_sampleSet->onOpenSample( this, progress ); // add param @date 2014.07.08 <Mod> M.Izumi
318  }
319 
320  // open
321  LOG_INFO( FMT( "Open Sample [%s]", m_name.c_str() ) );
322  bool ret = onOpenSample( m_root, progress );
323 
324  // add sort @date 2011.10.31 <Add> M.Izumi
325  m_root->sortSpectra();
326 
328 
329  // properties
330  if( m_sampleSet != NULL ) {
331  m_root->getProperties().setValue( "File Path", m_sampleSet->getFilePath() );
332  m_root->getProperties().setValue( "File Name", m_sampleSet->getFileName() );
333  }
334  m_root->getProperties().setValue( "Instrument", m_instrument.c_str() );
335  m_root->getProperties().setValue( "Manufacturer", m_instrument.c_str() );
336  m_root->getProperties().setValue( "Sample Name", m_name.c_str() );
337  m_root->getProperties().setValue( "Sample Type", m_type.c_str() );
338  m_root->getProperties().setValue( "Software Name", m_softwareName.c_str() );
339  m_root->getProperties().setValue( "Software Version", m_softwareVersion.c_str() );
340 
341  if( ret ) { // success
342  m_opened = true;
343  getGroups( NULL );
344 
345  DataManager::openSample( this );
346  }
347  else { // failed
348  LOG_ERROR( FMT( "Failed to open the sample.[%s]", m_name.c_str() ) );
349  closeSample();
350  }
351 
352 
353  return ret;
354 }
355 
356 // close sample
358  if( !m_opened ) {
359  return true;
360  }
361 
362  // close
363  bool ret = onCloseSample();
364  DataManager::closeSample( this, false );
365  m_opened = false;
366 
367  // delete spectra
368  if( m_root != NULL ) {
369  delete m_root;
370  m_root = NULL;
371  m_groups.clear();
372  }
373 
374  // sample set
375  if( m_sampleSet != NULL ) {
376  m_sampleSet->onCloseSample( this );
377  }
378  m_specId = 0; // @date 2013.09.05 <Add> M.Izumi
379 
380  return ret;
381 }
382 
383 // detect peaks by API
384 void Sample::detectPeaksByAPI( Spectrum* spec, Peaks* peaks ) {
385  onDetectPeaksByAPI( spec, peaks );
386 }
387 
388 // check the property key
389 bool Sample::isCommonProperty( const char* key ) {
390  // keys
391  const char* keys[] = {
392  "File Path",
393  "File Name",
394  "Instrument",
395  "Manufacturer",
396  "Sample Name",
397  "Sample Type",
398  "Software Name",
399  "Software Version"
400  };
401 
402  // create array
403  if( m_commonProps.size() == 0 ) {
404  const unsigned int num = sizeof( keys ) / sizeof( const char* );
405 
406  for( unsigned int i = 0; i < num; i++ ) {
407  m_commonProps.push_back( keys[ i ] );
408  }
409  }
410 
411  // check the key
412  if( key == NULL ) {
413  return false;
414  }
415 
416  for( unsigned int i = 0; i < m_commonProps.size(); i++ ) {
417  if( m_commonProps[ i ].compare( key ) == 0 ) {
418  return true;
419  }
420  }
421 
422  return false;
423 }
424 
425 // issue sample id
428  int inum = aoMgr.getNumberOfOpenedSamples();
429  int id = m_optSampleId;
430  if( inum > 0 ){
431  id++;
432  }
433  return id;
434 }
435 
436 // set sample id
437 void Sample::setSampleId( int id ){
438  m_optSampleId = id;
439 }
440 
441 // get sample id
443  return m_optSampleId;
444 }
445 
446 // get sample by id
449 
450  int inum = aoMgr.getNumberOfOpenedSamples();
451  for( int i=0; i < inum; i++ ){
452  Sample* sample = aoMgr.getOpenedSample( i );
453  if( m_optSampleId == id ){
454  return sample;
455  }
456  }
457 
458  return NULL;
459 }
460 
461 // issue spectrum id
463  int specId = m_specId;
464  m_specId++;
465 
466  return specId;
467 }
468 
469 // get spectrum by id
471  // gets all spectra
472  kome::objects::DataSet spectra;
473 
475  if( root == NULL ) {
476  return NULL;
477  }
478  root->getDataSet( &spectra );
479 
480  // search spectrum
482  for( unsigned int i = 0; i < spectra.getNumberOfSpectra() && spec == NULL; i++ ) {
483  kome::objects::Spectrum* tmp = spectra.getSpectrum( i );
484  if( tmp->getId() == id ) {
485  spec = tmp;
486  }
487  }
488 
489  return spec;
490 }
491 
492 // issue chromatogram id
494  static int chromId = 0;
495  chromId++;
496 
497  return chromId;
498 }
499 
500 // get chromatogram by id
502  // @date 2013.09.03 <Mod> M.Izumi ->
503  // gets all spectra
504  kome::objects::DataSet chroms;
505 
507  if( root == NULL ) {
508  return NULL;
509  }
510  root->getDataSet( &chroms );
511 
512  // search spectrum
514  for( unsigned int i = 0; i < chroms.getNumberOfChromatograms() && chrom == NULL; i++ ) {
515  kome::objects::Chromatogram* tmp = chroms.getChromatogram( i );
516  if( tmp->getId() == id ) {
517  chrom = tmp;
518  }
519  }
520 
521  return chrom;
522  // @date 2013.09.03 <Mod> M.Izumi <-
523 }
524 
525 // issue group id
527  static int groupId = 0;
528 
529  groupId++;
530  return groupId;
531 }
532 
533 // get group by id
535  int inum = getNumberOfGroups();
536  for( int i=0; i < inum; i++ ){
537  DataGroupNode* group = getGroup(i);
538  if( group->getId() == id ){
539  return group;
540  }
541  }
542  return NULL;
543 }
544 
545 // get API peak detection is supported.
547  return m_supportedApiPeaks;
548 }
549 
550 // set API peak detection is supported
551 void Sample::setSupportedAPIPeaks( const bool supported ) {
552  m_supportedApiPeaks = supported;
553 }
554 
555 // detect peaks using API
557  if( !m_supportedApiPeaks ) {
558  LOG_WARN( FMT( "This file type does not support API peak detection." ) );
559  }
560 }
561 
562 // reset id
564  m_optSampleId = 0;
565 }
bool m_parallelReadable
Definition: Sample.h:89
int issueSpecId()
to issue the spectrum id
Definition: Sample.cpp:462
interfaces of ActiveObjectsManager class
group of spectrum management class
Definition: DataGroupNode.h:33
bool isEdited()
judges whther this sample is edited or not
Definition: Sample.cpp:214
virtual bool onOpenSample(Sample *sample, kome::core::Progress *progress=NULL)=0
This method is called by openSample method. (abstract method)
void getDataSet(DataSet *dataSet)
gets spectra that contains this group. (getSpectrum method cannot get spectra that belong to child gr...
virtual bool onCloseSample()=0
This method is called by closeSample method. (abstract method)
sample set information management class
Definition: SampleSet.h:29
void setName(const char *name)
sets sample name
Definition: Sample.cpp:92
interfaces of Chromatogram class
SampleSet * getSampleSet()
gets sample set object
Definition: Sample.cpp:77
int getId()
gets spectrum id
Definition: Spectrum.cpp:1136
DataGroupNode * getChild(const unsigned int index)
gets child group
virtual void onDetectPeaksByAPI(Spectrum *spec, Peaks *peaks)
This methos is called by detectPeakByAPI method.
Definition: Sample.cpp:556
sample information management class
Definition: Sample.h:34
Chromatogram * getChromatogramById(int id)
A schromatogram is acquired by id.
Definition: Sample.cpp:501
DataGroupNode * getGroup(const unsigned int index)
gets spectrum group
Definition: Sample.cpp:234
std::vector< DataGroupNode * > m_groups
Definition: Sample.h:86
static void openSample(Sample *sample)
This method is called when a sample is opened.
Definition: DataManager.cpp:56
int getId()
gets chromatogram id
void setInstrument(const char *instrument)
sets instrument name
Definition: Sample.cpp:122
static ActiveObjectsManager & getInstance()
get active object manager object (This is the only object.)
active object management class
static void closeSample(Sample *sample, const bool deleting)
This method is called when a sample is closed.
Definition: DataManager.cpp:64
static std::vector< std::string > m_commonProps
Definition: Sample.h:102
std::string m_company
Definition: Sample.h:68
void setValue(const char *key, const char *value)
sets parameter value
Definition: Properties.cpp:39
Spectrum * getSpectrumById(int id)
A spectrum is acquired by id.
Definition: Sample.cpp:470
interfaces of SampleSet class
const char * getFileName()
gets file name
Definition: SampleSet.cpp:74
unsigned int getNumberOfSpectra()
gets the number of spectra
Definition: DataSet.cpp:128
DataSet m_hiddenSet
Definition: Sample.h:92
std::string m_softwareName
Definition: Sample.h:71
std::string trimstring(const char *s)
remove white spaces from both ends of specified string
interfaces of Spectrum class
static void stopLoadingTimer()
stops loading timer
Definition: SampleSet.cpp:188
kome::core::Properties & getProperties()
gets properties
const char * getName()
gets sample name
Definition: Sample.cpp:102
progress display abstract class
Definition: Progress.h:31
Spectrum * getSpectrum(const unsigned int index)
gets the number of spectra
Definition: DataSet.cpp:133
interfaces of DataGroupNode class
static void resetId()
Reset the ID of the sample.
Definition: Sample.cpp:563
interfaces of Sample class
interfaces of Peaks class
void setMsCompany(const char *company)
sets MS company
Definition: Sample.cpp:157
const char * getType()
gets sample type
Definition: Sample.cpp:117
void setParallelReadable(const bool readable)
sets parallel readable or not
Definition: Sample.cpp:271
DataGroupNode * getGroupById(int id)
A DataGroupNode is qcquired by id.
Definition: Sample.cpp:534
Chromatogram * getChromatogram(const unsigned int index)
gets chroamtogram
Definition: DataSet.cpp:146
void detectPeaksByAPI(Spectrum *spec, Peaks *peaks)
detect peaks using Application Progreamming Interface
Definition: Sample.cpp:384
std::string m_type
Definition: Sample.h:62
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
unsigned int getNumberOfOpenedSamples()
gets the number of opened samples
Sample(SampleSet *sampleSet)
constructor
Definition: Sample.cpp:38
#define NULL
Definition: CoreMacros.h:18
SampleSet * m_sampleSet
Definition: Sample.h:50
const char * getInstrument()
gets instrument name
Definition: Sample.cpp:152
std::string m_softwareVersion
Definition: Sample.h:74
bool m_supportedApiPeaks
Definition: Sample.h:95
const char * getSoftwareName()
gets software name
Definition: Sample.cpp:180
bool closeSample()
closes sample
Definition: Sample.cpp:357
static int issueSampleId()
to issue the sample id
Definition: Sample.cpp:426
unsigned int getNumberOfChromatograms()
gets the number of chromatograms
Definition: DataSet.cpp:141
bool isParallelReadable()
gets whether this sample is parallel readable or not
Definition: Sample.cpp:276
void setSampleId(int id)
set sample id
Definition: Sample.cpp:437
void setEdited(const bool edited)
sets edited flag
Definition: Sample.cpp:209
const char * getMsCompany()
gets MS company
Definition: Sample.cpp:166
int getSampleIndex()
gets sample index
Definition: Sample.cpp:87
std::string m_name
Definition: Sample.h:59
unsigned int getNumberOfSamples()
gets the nubmer of samples
Definition: SampleSet.cpp:115
bool isSupportedAPIPeaks()
judges whether API peak detection is supported on this sample.
Definition: Sample.cpp:546
DataGroupNode * getRootDataGroupNode()
gets root spectrum group
Definition: Sample.cpp:219
void setType(const char *type)
sets sample type
Definition: Sample.cpp:107
unsigned int getNumberOfGroups()
gets the number of spectrum groups
Definition: Sample.cpp:229
bool openSample(kome::core::Progress *progress=NULL)
opens sample [out] progress progressbar to display
Definition: Sample.cpp:281
void setSampleIndex(const int index)
sets sample index
Definition: Sample.cpp:82
spectrum information management class
Definition: Spectrum.h:30
static bool isCommonProperty(const char *key)
check whther the specified property key is common property or not
Definition: Sample.cpp:389
void getGroups(DataGroupNode *parentGroup=NULL)
gets spectrum groups
Definition: Sample.cpp:242
one or more spectra management class
Definition: DataSet.h:31
virtual bool onOpenSample(DataGroupNode *rootGroup, kome::core::Progress *progress)=0
This method is called by openSample method. (abstract method)
int getSampleId()
get sample id
Definition: Sample.cpp:442
std::string m_instrument
Definition: Sample.h:65
void setSupportedAPIPeaks(const bool supported)
sets API peak dtection is supported on this sample.
Definition: Sample.cpp:551
peaks information class
Definition: Peaks.h:35
void setSoftwareVersion(const char *version)
sets software version
Definition: Sample.cpp:185
static Sample * getSampleById(int id)
A sample is acquired by id.
Definition: Sample.cpp:447
int issueChromId(Sample *sample)
to issue the chromatogram id
Definition: Sample.cpp:493
Sample * getOpenedSample(const unsigned int idx)
gets opened sample
int issueGroupId(Sample *sample)
to issue the group id
Definition: Sample.cpp:526
chromatogram information management class
Definition: Chromatogram.h:33
const char * getSoftwareVersion()
gets software version
Definition: Sample.cpp:194
static int m_optSampleId
Definition: Sample.h:98
virtual ~Sample()
destructor
Definition: Sample.cpp:55
static void startLoadingTimer()
starts loading timer
Definition: SampleSet.cpp:183
interfaces of DataManager class
bool isOpened()
judges whther this sample is opened or not
Definition: Sample.cpp:204
void setSoftwareName(const char *name)
sets software name
Definition: Sample.cpp:171
virtual void sortSpectra()
sorts spectra in retention time order (override method)
const char * getFilePath()
gets file path
Definition: SampleSet.cpp:69
DataSet * getHiddenDataSet()
gets the hidden data set
Definition: Sample.cpp:224
void setOpened(const bool opened)
sets opened flag
Definition: Sample.cpp:199
virtual bool onCloseSample(Sample *sample)=0
This method is called by closeSample method. (abstract method)
unsigned int getNumberOfChildren()
gets the number of children
DataGroupNode * m_root
Definition: Sample.h:77
void setGroupIndex(const int index)
sets group index