Mass++ mzML IO Plugin v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations
MzmlSpectrum.cpp
1 
12 #include "stdafx.h"
13 #include "MzmlSpectrum.h"
14 
15 #include "MzmlSample.h"
16 #include "MzmlSampleSet.h"
17 #include "MzmlDataHandler.h"
18 
19 #include <list>
20 #include <boost/function.hpp>
21 #include <boost/bind.hpp>
22 #include <xercesc/framework/MemBufInputSource.hpp>
23 
24 
25 using namespace kome::io::mzml;
26 
27 
28 #include <crtdbg.h>
29 #ifdef _DEBUG
30  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
31  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
32 #endif // _DEBUG
33 
34 
35 
36 // constructor
37 MzmlSpectrum::MzmlSpectrum( MzmlSample* file, const char* name )
38  : kome::objects::Spectrum( file, name ) {
39  m_mzmlSample = file;
40  m_offset = -1;
41 }
42 
43 // destructor
45 }
46 
47 // set data offset
48 void MzmlSpectrum::setOffset( const long long offset ) {
49  m_offset = offset;
50 }
51 
52 // get data offset
54  return m_offset;
55 }
56 
57 // parse
58 void MzmlSpectrum::parse( xercesc::SAX2XMLReader* parser ) {
59  // get file
60  MzmlSample* sample = (MzmlSample*)getSample();
61  MzmlSampleSet* sampleSet = sample->getMzmlSampleSet();
62 
63  // parse
64  if( m_offset > 0 ) {
65  // seek
66  FILE* fp = sampleSet->getFile();
67  fileseek( fp, m_offset, SEEK_SET );
68 
69  // read file
70  std::string tag;
71 // >>>>>> @Date:2013/06/03 <Modify> A.Ozaki
72 // 64bit環境で動作させる場合、ポインタアドレスを正しく処理することができなくなります
73 // x86/x64ともに動作させるために、size_tで処理することが勧められているのでいるので、
74 // size_tで処理するように修正します
75  size_t szPos = 0;
76 // <<<<<< @Date:2013/06/03 <Modify> A.Ozaki
77  char buff[ 1024 ];
78  bool loop = true;
79  bool endTag = false;
80  while( loop ) {
81  // read
82  int readSize = fread( buff, 1, 1023, fp );
83 
84  // add to tag
85  if( readSize > 0 ) {
86  buff[ readSize ] = '\0';
87  tag.append( buff );
88  }
89 
90  // check the tag
91 // >>>>>> @Date:2013/06/03 <Modify> A.Ozaki
92  szPos = (size_t)tag.find( "</spectrum>" );
93  if( szPos != (size_t)tag.npos ) {
94  endTag = true;
95  loop = false;
96 
97  tag = tag.substr( 0, szPos );
98  tag.append( "</spectrum>" );
99  }
100 
101  // check the size
102  if( readSize < 1023 ) {
103  loop = false;
104  }
105  }
106 
107  szPos = (size_t)tag.find( "<spectrum" );
108  if( szPos != (size_t)tag.npos ) {
109  tag = tag.substr( szPos );
110  }
111 // <<<<<< @Date:2013/06/03 <Modify> A.Ozaki
112 
113  // parse
114  if( endTag ) {
115  xercesc::MemBufInputSource source( (const XMLByte*)tag.c_str(), tag.size(), "memory_buffer", false );
116  parser->parse( source );
117  }
118  else {
119  LOG_WARN( FMT( "Failed to get the spectrum tag." ) );
120  }
121  }
122  else {
123  // parse file
124  parser->parse( sampleSet->getFilePath() );
125  }
126 }
127 
128 // get data points
130  kome::core::XYData* const xyData,
131  const double minX,
132  const double maxX
133 ) {
134 #pragma omp critical ( getMzmlSpectrumData )
135  {
136  // data points
137  kome::core::DataPoints pt;
138 
139  // parser
140  xercesc::SAX2XMLReader* parser = kome::xml::XercesTool::getParser( NULL );
141  parser->setFeature( xercesc::XMLUni::fgXercesSchema, false );
142  parser->setFeature( xercesc::XMLUni::fgXercesSchemaFullChecking, false );
143 
144  // create handler
145  MzmlDataHandler handler( *m_mzmlSample, getName(), pt );
146 
147  parser->setContentHandler( &handler );
148  parser->setErrorHandler( &handler );
149 
150  // parse
151  try {
152  parse( parser );
153  }
154  catch( const xercesc::XMLException& e ) {
155  LOG_ERROR( FMT( "XML Exception: %s", kome::xml::XercesTool::transcode( e.getMessage() ).c_str() ) );
156  handler.setError( true );
157  }
158  catch( const xercesc::SAXParseException& e ) {
159  LOG_ERROR( FMT( "SAX Parse Exception: %s", kome::xml::XercesTool::transcode( e.getMessage() ).c_str() ) );
160  handler.setError( true );
161  }
162  catch( ... ) {
163  LOG_ERROR( FMT( "Unexpected Exception" ) );
164  handler.setError( true );
165  }
166 
167  // get data
168  int startIdx = 0;
169  if( minX >= 0.0 ) {
170  startIdx = pt.searchIndex( minX );
171  if( startIdx < 0 ) {
172  startIdx = - startIdx - 1;
173  }
174  }
175 
176  int endIdx = (int)pt.getLength() - 1;
177  if( maxX >= 0.0 ) {
178  endIdx = pt.searchIndex( maxX );
179  if( endIdx < 0 ) {
180  endIdx = - endIdx - 2;
181  }
182  }
183 
184  int num = endIdx - startIdx + 1;
185  if( num > 0 ) {
186  xyData->reserve( num );
187  for( int i = startIdx; i <= endIdx; i++ ) {
188  xyData->addPoint( pt.getX( i ), pt.getY( i ) );
189  }
190  }
191  }
192 }
193 
194 // get x range
195 void MzmlSpectrum::onGetXRange( double* minX, double* maxX ) {
196  // get
197  kome::core::DataPoints dps;
198  onGetXYData( &dps, -1.0, -1.0 );
199 
200  // set
201  if( minX != NULL ) {
202  *minX = dps.getMinX();
203  }
204  if( maxX != NULL ) {
205  *maxX = dps.getMaxX();
206  }
207 }
208 
209 // get total intensity
210 double MzmlSpectrum::onGetTotalIntensity( const double minX, const double maxX ) {
211  // get data points
212  kome::core::DataPoints pt;
213  onGetXYData( &pt, minX, maxX );
214 
215  // get total intensity
216  double intensity = 0.0;
217  for( unsigned int i = 0; i < pt.getLength(); i++ ) {
218  intensity += pt.getY( i );
219  }
220 
221  return intensity;
222 }
223 
224 // get total intensity
225 double MzmlSpectrum::onGetMaxIntensity( const double minX, const double maxX ) {
226  // get data points
227  kome::core::DataPoints pt;
228  onGetXYData( &pt, minX, maxX );
229 
230  // get main intensity
231  double intensity = 0.0;
232  for( unsigned int i = 0; i < pt.getLength(); i++ ) {
233  double y = pt.getY( i );
234  intensity = MAX( y, intensity );
235  }
236 
237  return intensity;
238 }
239 
240 // >>>>>> @Date:2013/09/05 <Add> A.Ozaki
241 //
242 // set the flag of request load data (virtual)
244 {
245  return;
246 }
247 
248 // set the flag of request load data (virtual)
250 {
251  return;
252 }
253 
254 // check the flag of request load data (virtual)
256 {
257  return kome::objects::Spectrum::onIsRequestLoadData( );
258 }
259 
260 // set the flag of first access (virtual)
262 {
263  return;
264 }
265 
266 // reset the flag of first access (virtual)
268 {
269  return;
270 }
271 
272 // check the flag of first access (virtual)
274 {
275  return kome::objects::Spectrum::onIsFirstAccess( );
276 }
277 
278 // load data (virtual)
280 {
281  return kome::objects::Spectrum::onLoadData( );
282 }
283 
284 //
285 // <<<<<< @Date:2013/09/05 <Add> A.Ozaki
286 
MzmlSampleSet * getMzmlSampleSet()
gets mzML sample set object
Definition: MzmlSample.cpp:65
virtual ~MzmlSpectrum()
destructor
interfaces of MzmlDataHandler class
virtual void onResetFirstAccess(void)
This method is called by resetFirstAccess method. (abstract method)
virtual void onGetXYData(kome::core::XYData *const xyData, const double minX, const double maxX)
This method is called by getXYData method. (override method)
virtual bool onIsFirstAccess(void)
This method is called by isFirstAccess method. (abstract method)
virtual void onResetRequestLoadData(void)
This method is called by resetRequestLoadData method. (abstract method)
virtual void onGetXRange(double *minX, double *maxX)
This method is called by getMinX or getMaxX method. (override method)
interfaces of MzmlSample class
mzML sample set class
Definition: MzmlSampleSet.h:24
common header file
XML data handler to get spectrum data points.
virtual double onGetTotalIntensity(const double minX, const double maxX)
This method is called by getTotalIntensity method. (override method)
virtual void onSetRequestLoadData(void)
This method is called by setRequestLoadData method. (abstract method)
virtual double onGetMaxIntensity(const double minX, const double maxX)
This method is called by getMaxIntensity method. (override method)
virtual bool onLoadData(void)
This method is called by loadData method. (abstract method)
void parse(xercesc::SAX2XMLReader *parser)
parse XML data
interfaces of MzmlSampleSet class
MzmlSpectrum(MzmlSample *file, const char *name)
constructor
virtual void onSetFirstAccess(void)
This method is called by setFirstAccess method. (abstract method)
virtual bool onIsRequestLoadData(void)
This method is called by isRequestLoadData method. (abstract method)
long long getOffset()
gets data offset
interfaces of MzmlSpectrum class
void setOffset(const long long offset)
sets data offset
FILE * getFile()
gets file descriptor
mzML sample class
Definition: MzmlSample.h:26