Mass++ mzML IO Plugin v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations
MzmlChromatogram.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "MzmlChromatogram.h"
14 #include "MzmlSample.h"
15 #include "MzmlSampleSet.h"
16 #include "MzmlDataHandler.h"
17 
18 #include <list>
19 #include <boost/function.hpp>
20 #include <boost/bind.hpp>
21 #include <xercesc/framework/MemBufInputSource.hpp>
22 
23 
24 using namespace kome::io::mzml;
25 
26 
27 #include <crtdbg.h>
28 #ifdef _DEBUG
29  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
30  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
31 #endif // _DEBUG
32 
33 
34 
35 // constructor
37  : kome::objects::Chromatogram( file ) {
38  m_offset = -1;
39  m_mzmlSample = file;
40 }
41 
42 // destructor
44 }
45 
46 // set offset
47 void MzmlChromatogram::setOffset( const long long offset ) {
48  m_offset = offset;
49 }
50 
51 // get offset
53  return m_offset;
54 }
55 
56 // parse XML data
57 void MzmlChromatogram::parse( xercesc::SAX2XMLReader* parser ) {
58  // get file
59  MzmlSample* sample = (MzmlSample*)getSample();
60  MzmlSampleSet* sampleSet = sample->getMzmlSampleSet();
61 
62  // parse
63  if( m_offset > 0 ) {
64  // seek
65  FILE* fp = sampleSet->getFile();
66  fileseek( fp, m_offset, SEEK_SET );
67 
68  // read file
69  std::string tag;
70  char buff[ 1024 ];
71  bool loop = true;
72  bool endTag = false;
73  while( loop ) {
74  // read
75  int readSize = fread( buff, 1, 1023, fp );
76 
77  // add to tag
78  if( readSize > 0 ) {
79  buff[ readSize ] = '\0';
80  tag.append( buff );
81  }
82 
83  // check the tag
84  unsigned int pos = tag.find( "</chromatogram>" );
85  if( pos != (unsigned int)tag.npos ) {
86  endTag = true;
87  loop = false;
88 
89  tag = tag.substr( 0, pos );
90  tag.append( "</chromatogram>" );
91  }
92 
93  // check the size
94  if( readSize < 1023 ) {
95  loop = false;
96  }
97  }
98 
99  unsigned int pos = tag.find( "<chromatogram" );
100  if( pos != (unsigned int)tag.npos ) {
101  tag = tag.substr( pos );
102  }
103 
104  // parse
105  if( endTag ) {
106  xercesc::MemBufInputSource source( (const XMLByte*)tag.c_str(), tag.size(), "memory_buffer", false );
107  parser->parse( source );
108  }
109  else {
110  LOG_WARN( FMT( "Failed to get the chromatogram tag." ) );
111  }
112  }
113  else {
114  // parse file
115  parser->parse( sampleSet->getFilePath() );
116  }
117 }
118 
119 // on get xy data
120 void MzmlChromatogram::onGetXYData( kome::core::XYData* const xyData ) {
121  // check the parameter
122  if( xyData == NULL ) {
123  return;
124  }
125 
126  // data points
127  kome::core::DataPoints dps;
128 
129  // parser
130  xercesc::SAX2XMLReader* parser = kome::xml::XercesTool::getParser( NULL );
131  parser->setFeature( xercesc::XMLUni::fgXercesSchema, false );
132  parser->setFeature( xercesc::XMLUni::fgXercesSchemaFullChecking, false );
133 
134  // create handler
135  MzmlDataHandler handler( *m_mzmlSample, getName(), dps );
136 
137  parser->setContentHandler( &handler );
138  parser->setErrorHandler( &handler );
139 
140  // parse
141  try {
142  parse( parser );
143  for( unsigned int i = 0; i < dps.getLength(); i++ ) {
144  xyData->addPoint( dps.getX( i ), dps.getY( i ) );
145  }
146  }
147  catch( const xercesc::XMLException& e ) {
148  LOG_ERROR( FMT( "XML Exception: %s", kome::xml::XercesTool::transcode( e.getMessage() ).c_str() ) );
149  handler.setError( true );
150  }
151  catch( const xercesc::SAXParseException& e ) {
152  LOG_ERROR( FMT( "SAX Parse Exception: %s", kome::xml::XercesTool::transcode( e.getMessage() ).c_str() ) );
153  handler.setError( true );
154  }
155  catch( ... ) {
156  LOG_ERROR( FMT( "Unexpected Exception" ) );
157  handler.setError( true );
158  }
159 }
160 
161 // on get spectra
163  kome::objects::DataSet& dataSet,
164  const double startRt,
165  const double endRt,
166  const kome::objects::SearchType startSearch,
167  const kome::objects::SearchType endSearch
168 ) {
169 }
170 
171 // on get mass
172 double MzmlChromatogram::onGetMass( const unsigned int index ) {
173  return -1.0;
174 }
175 
176 // on get ms stage
177 int MzmlChromatogram::onGetMsStage( const unsigned int index ) {
178  return -1;
179 }
180 
181 // on get precursor
182 double MzmlChromatogram::onGetPrecursor( const unsigned int index ) {
183  return -1.0;
184 }
MzmlSampleSet * getMzmlSampleSet()
gets mzML sample set object
Definition: MzmlSample.cpp:65
interfaces of MzmlDataHandler class
virtual void onGetXYData(kome::core::XYData *const xyData)
This method is called by getXYData method (override method)
interfaces of MzmlSample class
mzML sample set class
Definition: MzmlSampleSet.h:24
void setOffset(const long long offset)
sets data offset
common header file
virtual double onGetMass(const unsigned int index)
This method is called by getMass method (override method)
XML data handler to get spectrum data points.
virtual int onGetMsStage(const unsigned int index)
This method is called by getMsStage method (override method)
virtual ~MzmlChromatogram()
destructor
interfaces of MzmlChromatogram class
long long getOffset()
gets data offset
interfaces of MzmlSampleSet class
void parse(xercesc::SAX2XMLReader *parser)
parse XML data
FILE * getFile()
gets file descriptor
virtual double onGetPrecursor(const unsigned int index)
This method is called by getPrecursor method (override method)
MzmlChromatogram(MzmlSample *file)
constructor
virtual void onGetSpectra(kome::objects::DataSet &dataSet, const double startRt, const double endRt, const kome::objects::SearchType startSearch, const kome::objects::SearchType endSearch)
This method is called by searchSpectrum or getSpectra method (overriede method)
mzML sample class
Definition: MzmlSample.h:26