File: mzParser.h

package info (click to toggle)
libmstoolkit 82-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 776 kB
  • sloc: cpp: 10,999; ansic: 109; makefile: 86; sh: 20
file content (1529 lines) | stat: -rw-r--r-- 46,899 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
/*
  mzParser - This distribution contains novel code
  and publicly available open source utilities. The novel code is
  open source under the FreeBSD License, please see LICENSE file
  for detailed information.

  Copyright (C) 2011, Mike Hoopmann, Institute for Systems Biology
  Version 1.0, January 4, 2011.
  Version 1.1, March 14, 2012.

  Additional code/libraries obtained from:

  X!Tandem 2010.12.01: http://thegpm.org/

  Copyright and license information for these free utilities are
  provided in the LICENSE file. Note that many changes were made
  to the code adapted from X!Tandem.
*/

#ifndef _MZPARSER_H
#define _MZPARSER_H

//------------------------------------------------
// Standard libraries
//------------------------------------------------
#include <vector>
#include <map>
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include "expat.h"
#include "zlib.h"
#include "MSNumpress.hpp"

#ifdef MZP_MZ5
#include "hdf5.h"
#include "H5Cpp.h"
#endif

using namespace std;

#ifdef MZP_MZ5
using namespace H5;
#endif

//------------------------------------------------
// MACROS
//------------------------------------------------

//#define OSX    // to compile with cc on Macintosh OSX
//#define OSX_TIGER // use with OSX if OSX Tiger is being used
//#define OSX_INTEL // compile with cc on Macintosh OSX on Intel-style processors
//#define MINGW    // compiling with MinGW gcc toolset
// The following should be defined by the compiler already
//#define __GNUC__  // compiling with gcc (or g++)
//#define _MSC_VER  // compiling with Microsoft Studio

#define XMLCLASS    
#ifndef XML_STATIC
#define XML_STATIC  // to statically link the expat libraries
#endif

//For Windows
#ifdef _MSC_VER
#define __inline__ __inline
typedef _int64  __int64_t;
typedef unsigned _int32 uint32_t;
typedef unsigned _int64 uint64_t;
typedef __int64 f_off;
#define mzpfseek(h,p,o) _fseeki64(h,p,o)
#define mzpftell(h) _ftelli64(h)
#define mzpatoi64(h) _atoi64(h)
#endif

//For MinGW toolset, which lacks the ftello, fseeko, etc functions
#ifdef MINGW
//typedef __int64 f_off;
//#define __int64_t int64_t
#define mzpfseek(h,p,o) fseeko64(h,p,o)
#define mzpftell(h) ftello64(h)
#define mzpatoi64(h) _atoi64(h)
//#include <stdexcept>
#endif

#if defined(GCC) || defined(__LINUX__)
#include <stdint.h>
#include <stdexcept>
#ifndef _LARGEFILE_SOURCE
#error "need to define _LARGEFILE_SOURCE!!"
#endif    /* end _LARGEFILE_SOURCE */
#if _FILE_OFFSET_BITS<64
#error "need to define _FILE_OFFSET_BITS=64"
#endif

typedef off_t f_off;
#define mzpfseek(h,p,o) fseeko(h,p,o)
#define mzpftell(h) ftello(h)
#define mzpatoi64(h) atoll(h)
#endif

#ifdef OSX
#define __inline__ inline
#ifndef OSX_TIGER
#define __int64_t int64_t
#endif
#endif

// this define for the INTEL-based OSX platform is untested and may not work
#ifdef OSX_INTEL
#define __inline__ inline
#endif

// this define should work for most LINUX and UNIX platforms



//------------------------------------------------
// mzMLParser structures, definitions, and enums
//------------------------------------------------
//specDP (Spectrum Data Point) is the basic content element of a mass spectrum.
typedef struct specDP{
  double mz;
  double intensity;
} specDP;

typedef struct TimeIntensityPair{
  double time;
  double intensity;
} TimeIntensityPair;

enum enumActivation {
  none=0,
  CID=1,
  HCD=2,
  ETD=3,
  ETDSA=4,
  ECD=5,
};



//------------------------------------------------
// mzMLParser classes
//------------------------------------------------
//For holding mzML and mzXML indexes
class cindex  {
public:

  static int compare (const void* a, const void* b) {
    if( *(int*)a < *(int*)b ) return -1;
    if( *(int*)a > *(int*)b ) return 1;
    return 0;
  }

  int scanNum;
  string idRef;
  f_off offset;
};



//For instrument information
class instrumentInfo {
public:
  string analyzer;
  string detector;
  string id;
  string ionization;
  string manufacturer;
  string model;
  instrumentInfo(){
    analyzer="";
    detector="";
    id="";
    ionization="";
    manufacturer="";
    model="";
  }
  void clear(){
    analyzer="";
    detector="";
    id="";
    ionization="";
    manufacturer="";
    model="";
  }
};

typedef struct sPrecursorIon{
  double intensity;
  double isoLowerMZ; //lower offset of the isolation window
  double isoMZ;      //the mz of the isolation window
  double isoUpperMZ;  //upper offset of the isolation window
  double mz;         //is this always redundant with isoMZ?
  double monoMZ;
  vector<int>* possibleCharges;
  int charge;
  sPrecursorIon(){
    intensity=0;
    isoLowerMZ=0;
    isoMZ=0;
    isoUpperMZ=0;
    mz=0;
    monoMZ=0;
    possibleCharges=new vector<int>;
    charge=0;
  }
  sPrecursorIon(const sPrecursorIon& p){
    intensity=p.intensity;
    isoLowerMZ = p.isoLowerMZ;
    isoMZ = p.isoMZ;
    isoUpperMZ = p.isoUpperMZ;
    mz=p.mz;
    monoMZ=p.monoMZ;
    possibleCharges=new vector<int>;
    for(size_t i=0;i<p.possibleCharges->size();i++) possibleCharges->push_back(p.possibleCharges->at(i));
    charge=p.charge;
  }
  ~sPrecursorIon(){
    delete possibleCharges;
  }
  sPrecursorIon& operator=(const sPrecursorIon& p){
    if(this!=&p){
      intensity=p.intensity;
      isoLowerMZ = p.isoLowerMZ;
      isoMZ = p.isoMZ;
      isoUpperMZ = p.isoUpperMZ;
      mz=p.mz;
      monoMZ=p.monoMZ;
      delete possibleCharges;
      possibleCharges=new vector<int>;
      for(size_t i=0;i<p.possibleCharges->size();i++) possibleCharges->push_back(p.possibleCharges->at(i));
      charge=p.charge;
    }
    return *this;
  }
  void clear(){
    intensity=0;
    isoLowerMZ = 0;
    isoMZ = 0;
    isoUpperMZ = 0;
    mz=0;
    monoMZ=0;
    possibleCharges->clear();
    charge=0;
  }
} sPrecursorIon;

class BasicSpectrum  {
public:

  //Constructors & Destructors
  BasicSpectrum();
  BasicSpectrum(const BasicSpectrum& s);
  ~BasicSpectrum();

  //Operator overloads
  BasicSpectrum& operator=(const BasicSpectrum& s);
  specDP& operator[ ](const unsigned int index);

  //Modifiers
  void addDP(specDP dp);
  void clear();
  void setActivation(int a);
  void setBasePeakIntensity(double d);
  void setBasePeakMZ(double d);
  void setCentroid(bool b);
  void setCollisionEnergy(double d);
  void setCompensationVoltage(double d);
  void setFilterLine(char* str);
  void setHighMZ(double d);
  void setIDString(char* str);
  void setIonInjectionTime(double d);
  void setLowMZ(double d);
  void setMSLevel(int level);
  void setPeaksCount(int i);
  void setPositiveScan(bool b);
  void setPrecursorIon(sPrecursorIon& pi);
  void setPrecursorIon(double mz, double monoMZ, double intensity, int charge, int possibleChargeCount, int* possibleCharges);
  void setPrecursorScanNum(int i);
  void setRTime(float t);
  void setScanIndex(int num);
  void setScanNum(int num);
  void setTotalIonCurrent(double d);

  //Accessors
  int           getActivation();
  double        getBasePeakIntensity();
  double        getBasePeakMZ();
  bool          getCentroid();
  double        getCollisionEnergy();
  double        getCompensationVoltage();
  int           getFilterLine(char* str);
  double        getHighMZ();
  int           getIDString(char* str);
  double        getIonInjectionTime();
  double        getLowMZ();
  int           getMSLevel();
  int           getPeaksCount();
  bool          getPositiveScan();
  sPrecursorIon getPrecursorIon(int i=0);
  int           getPrecursorIonCount();
  int           getPrecursorCharge(int i=0);
  int           getPrecursorChargeCount(int i=0);
  double        getPrecursorIntensity(int i=0);
  double        getPrecursorMonoMZ(int i=0);
  double        getPrecursorMZ(int i=0);
  int           getPrecursorScanNum();
  float         getRTime(bool min=true);
  int           getScanIndex();
  int           getScanNum();
  double        getTotalIonCurrent();
  size_t        size();

protected:

  //Data Members (protected)
  int             activation;
  double          basePeakIntensity;
  double          basePeakMZ;
  bool            centroid;
  double          collisionEnergy;
  double          compensationVoltage;  //FAIMS compensation voltage
  char            filterLine[128];
  double          highMZ;
  char            idString[128];
  double          ionInjectionTime;
  double          lowMZ;
  int             msLevel;
  int             peaksCount;
  bool            positiveScan;
  int             precursorScanNum;      //Precursor scan number; 0 if no precursor or unknown
  float           rTime;                //always stored in minutes
  int             scanIndex;            //when scan numbers aren't enough, there are indexes (start at 1)
  int             scanNum;              //identifying scan number
  double          totalIonCurrent;
  vector<specDP>* vData;                //Spectrum data points
  vector<sPrecursorIon>* vPrecursor;
     
};

class BasicChromatogram  {
public:

  //Constructors & Destructors
  BasicChromatogram();
  BasicChromatogram(const BasicChromatogram& c);
  ~BasicChromatogram();

  //Operator overloads
  BasicChromatogram& operator=(const BasicChromatogram& c);
  TimeIntensityPair& operator[ ](const unsigned int index);

  //Modifiers
  void addTIP(TimeIntensityPair tip);
  void clear();
  void setIDString(char* str);
  void setPrecursor(double mz, int z, double offLow, double offHigh);
  void setProduct(double mz, double offLow, double offHigh);

  //Accessors
  int                         getCharge();
  vector<TimeIntensityPair>&  getData();
  int                         getIDString(char* str);
  double                      getPreMZ();
  double                      getPreOffsetLower();
  double                      getPreOffsetUpper();
  double                      getProdMZ();
  double                      getProdOffsetLower();
  double                      getProdOffsetUpper();
  size_t                      size();

protected:

  //Data Members (protected)
  int                         charge;
  char                        idString[128];
  double                      precursorMZ;
  double                      precursorOffsetLower;
  double                      precursorOffsetUpper;
  double                      productMZ;
  double                      productOffsetLower;
  double                      productOffsetUpper;
  vector<TimeIntensityPair>   vData;          //Chromatogram data points
     
};

//------------------------------------------------
// Random access gz (zran from zlib source code)
//------------------------------------------------
#define SPAN 1048576L       // desired distance between access points
#define WINSIZE 32768U      // sliding window size
#define CHUNK 32768         // file input buffer size
#define READCHUNK 16384

// access point entry 
typedef struct point {
  f_off out;          // corresponding offset in uncompressed data 
  f_off in;           // offset in input file of first full byte
  int bits;           // number of bits (1-7) from byte at in - 1, or 0
  unsigned char window[WINSIZE];  // preceding 32K of uncompressed data
} point;

// access point list 
typedef struct gz_access {
  int have;           // number of list entries filled in 
  int size;           // number of list entries allocated 
  point *list; // allocated list
} gz_access;

class Czran{
public:

  Czran();
  ~Czran();

  void free_index();
  gz_access *addpoint(int bits, f_off in, f_off out, unsigned left, unsigned char *window);
  int build_index(FILE *in, f_off span);
  int build_index(FILE *in, f_off span, gz_access **built);
  int extract(FILE *in, f_off offset, unsigned char *buf, int len);
  int extract(FILE *in, f_off offset);
  f_off getfilesize();

protected:
private:
  gz_access* index;
  
  unsigned char* buffer;
  f_off bufferOffset;
  int bufferLen;

  unsigned char* lastBuffer;
  f_off lastBufferOffset;
  int lastBufferLen;

  f_off fileSize;

};



//------------------------------------------------
// X!Tandem borrowed headers
//------------------------------------------------

int b64_decode_mio (char *dest, char *src, size_t size);
int b64_encode (char *dest, const char *src, int len);

class mzpSAXHandler{
public:

  //  SAXHandler constructors and destructors
  mzpSAXHandler();
  virtual ~mzpSAXHandler();

  //  SAXHandler eXpat event handlers
  virtual void startElement(const XML_Char *el, const XML_Char **attr);
  virtual void endElement(const XML_Char *el);
  virtual void characters(const XML_Char *s, int len);

  //  SAXHandler Parsing functions.
  bool open(const char* fileName);
  bool parse();
  bool parseOffset(f_off offset);
  void setGZCompression(bool b);

  inline void setFileName(const char* fileName) {
    m_strFileName = fileName;
  }

  //  SAXHandler helper functions
  inline bool isElement(const char *n1, const XML_Char *n2)
  {  return (strcmp(n1, n2) == 0); }

  inline bool isAttr(const char *n1, const XML_Char *n2)
  {  return (strcmp(n1, n2) == 0); }

  inline const char* getAttrValue(const char* name, const XML_Char **attr) {
    for (int i = 0; attr[i]; i += 2) {
      if (isAttr(name, attr[i])) return attr[i + 1];
    }
    return "";
  }

protected:

  XML_Parser m_parser;
  string  m_strFileName;
  bool m_bStopParse;
  bool m_bGZCompression;

  FILE* fptr;
  Czran gzObj;

};

class mzpSAXMzmlHandler : public mzpSAXHandler {
public:
  mzpSAXMzmlHandler(BasicSpectrum* bs);
  mzpSAXMzmlHandler(BasicSpectrum* bs, BasicChromatogram* bc);
  ~mzpSAXMzmlHandler();

  //  Overrides of SAXHandler functions
  void startElement(const XML_Char *el, const XML_Char **attr);
  void endElement(const XML_Char *el);
  void characters(const XML_Char *s, int len);

  //  SAXMzmlHandler public functions
  vector<cindex>*         getChromatIndex();
  f_off                   getIndexOffset();
  vector<instrumentInfo>* getInstrument();
  int                     getPeaksCount();
  vector<cindex>*         getSpecIndex();
  int                     highChromat();
  int                     highScan();
  bool                    load(const char* fileName);
  int                     lowScan();
  bool                    readChromatogram(int num=-1);
  bool                    readHeader(int num=-1);
  bool                    readSpectrum(int num=-1);
  
protected:

private:

  //  mzpSAXMzmlHandler subclasses
  class cvParam  {
  public:
    string refGroupName;
    string name;
    string accession;
    string value;
    string unitAccession;
    string unitName;
  };

  //  mzpSAXMzmlHandler private functions
  void  processData();
  void  processCVParam(const char* name, const char* accession, const char* value, const char* unitName="0", const char* unitAccession="0");
  void  pushChromatogram();
  void  pushSpectrum();  // Load current data into pvSpec, may have to guess charge
  f_off readIndexOffset();
  void  stopParser();

  //  mzpSAXMzmlHandler Base64 conversion functions
  void decode(vector<double>& d);
  //void decode32(vector<double>& d);
  //void decode64(vector<double>& d);
  //void decompress32(vector<double>& d);
  //void decompress64(vector<double>& d);
  unsigned long dtohl(uint32_t l, bool bNet);
  uint64_t dtohl(uint64_t l, bool bNet);

  //  mzpSAXMzmlHandler Flags indicating parser is inside a particular tag.
  bool m_bInIndexedMzML;
  bool m_bInRefGroup;
  bool m_bInmzArrayBinary;
  bool m_bInintenArrayBinary;
  bool m_bInSpectrumList;
  bool m_bInChromatogramList;
  bool m_bInIndexList;
  bool m_bInProduct;

  //  mzpSAXMzmlHandler procedural flags.
  bool m_bChromatogramIndex;
  bool m_bHeaderOnly;
  bool m_bLowPrecision;
  bool m_bNetworkData;  // i.e. big endian
  bool m_bNumpressLinear;
  bool m_bNumpressPic;
  bool m_bNumpressSlof;
  bool m_bNoIndex;
  bool m_bSpectrumIndex;
  bool m_bZlib;
  int  m_iDataType;   //0=unspecified, 1=32-bit float, 2=64-bit float
  bool m_bIndexSorted;
  //  mzpSAXMzmlHandler index data members.
  vector<cindex>    m_vIndex;
  cindex            curIndex;
  int               posIndex;
  f_off             indexOffset;

  vector<cindex>    m_vChromatIndex;
  cindex            curChromatIndex;
  int               posChromatIndex;

  //  mzpSAXMzmlHandler data members.
  BasicChromatogram*      chromat;
  string                  m_ccurrentRefGroupName;
  long                    m_encodedLen;            // For compressed data
  instrumentInfo          m_instrument;
  sPrecursorIon           m_precursorIon;
  int                     m_peaksCount;            // Count of peaks in spectrum
  vector<cvParam>         m_refGroupCvParams;
  int                     m_scanSPECCount;
  int                     m_scanIDXCount;
  int                     m_scanPRECCount;
  double                  m_startTime;            //in minutes
  double                  m_stopTime;              //in minutes
  string                  m_strData;              // For collecting character data.
  vector<instrumentInfo>  m_vInstrument;
  BasicSpectrum*          spec;
  vector<double>          vdI;
  vector<double>          vdM;                    // Peak list vectors (masses and charges)

};

class mzpSAXMzxmlHandler : public mzpSAXHandler {
public:
  mzpSAXMzxmlHandler(BasicSpectrum* bs);
  mzpSAXMzxmlHandler(BasicSpectrum* bs, BasicChromatogram* bc);
  ~mzpSAXMzxmlHandler();

  //  Overrides of SAXHandler functions
  void startElement(const XML_Char *el, const XML_Char **attr);
  void endElement(const XML_Char *el);
  void characters(const XML_Char *s, int len);

  //  mzpSAXMzxmlHandler public functions
  vector<cindex>* getIndex();
  f_off           getIndexOffset();
  instrumentInfo  getInstrument();
  int             getPeaksCount();
  int             highScan();
  bool            load(const char* fileName);
  int             lowScan();
  bool            readChromat(int num=-1);
  bool            readHeader(int num=-1);
  bool            readSpectrum(int num=-1);
  
protected:

private:

  //  mzpSAXMzxmlHandler private functions
  void  pushSpectrum();  // Load current data into pvSpec, may have to guess charge
  f_off readIndexOffset();
  void  stopParser();

  //  mzpSAXMzxmlHandler Base64 conversion functions
  void decode32();
  void decode64();
  void decompress32();
  void decompress64();
  unsigned long dtohl(uint32_t l, bool bNet);
  uint64_t dtohl(uint64_t l, bool bNet);

  //  mzpSAXMzxmlHandler Flags indicating parser is inside a particular tag.
  bool m_bInDataProcessing;
  bool m_bInIndex;
  bool m_bInMsInstrument;
  bool m_bInMsRun;
  bool m_bInPeaks;
  bool m_bInPrecursorMz;
  bool m_bInScan;

  //  mzpSAXMzxmlHandler procedural flags.
  bool m_bCompressedData;
  bool m_bHeaderOnly;
  bool m_bLowPrecision;
  bool m_bNetworkData;  // i.e. big endian
  bool m_bNoIndex;
  bool m_bScanIndex;
  bool m_bIndexSorted;
  //  mzpSAXMzxmlHandler index data members.
  vector<cindex>    m_vIndex;
  cindex            curIndex;
  int               posIndex;
  f_off             indexOffset;

  //  mzpSAXMzxmlHandler data members.
  uLong                   m_compressLen;  // For compressed data
  instrumentInfo          m_instrument;
  int                     m_peaksCount;    // Count of peaks in spectrum
  sPrecursorIon           m_precursorIon;
  string                  m_strData;      // For collecting character data.
  vector<instrumentInfo>  m_vInstrument;
  BasicSpectrum*          spec;
  vector<double>          vdI;
  vector<double>          vdM;            // Peak list vectors (masses and charges)

};

//------------------------------------------------
// mz5 Support
//------------------------------------------------

#ifdef MZP_MZ5

//mz5 constants
#define CVL 128
#define USRVL 128
#define USRNL 256
#define USRTL 64

static unsigned short MZ5_FILE_MAJOR_VERSION = 0;
static unsigned short MZ5_FILE_MINOR_VERSION = 9;

//forward declarations
class mzpMz5Config;

enum MZ5DataSets {
  ControlledVocabulary,
  FileContent,
  Contact,
  CVReference,
  CVParam,
  UserParam,
  RefParam,
  ParamGroups,
  SourceFiles,
  Samples,
  Software,
  ScanSetting,
  InstrumentConfiguration,
  DataProcessing,
  Run,
  SpectrumMetaData,
  SpectrumBinaryMetaData,
  SpectrumIndex,
  SpectrumMZ,
  SpectrumIntensity,
  ChromatogramMetaData,
  ChromatogramBinaryMetaData,
  ChromatogramIndex,
  ChromatogramTime,
  ChromatogramIntensity,
  FileInformation
};

enum SpectrumLoadPolicy {
  SLP_InitializeAllOnCreation,
  SLP_InitializeAllOnFirstCall
};

enum ChromatogramLoadPolicy {
  CLP_InitializeAllOnCreation,
  CLP_InitializeAllOnFirstCall
};

struct FileInformationMZ5Data  {
  unsigned short majorVersion;
  unsigned short minorVersion;
  unsigned short didFiltering;
  unsigned short deltaMZ;
  unsigned short translateInten;
};
  
struct FileInformationMZ5: public FileInformationMZ5Data {
  FileInformationMZ5();
  FileInformationMZ5(const FileInformationMZ5&);
  FileInformationMZ5(const mzpMz5Config&);
  ~FileInformationMZ5();
  FileInformationMZ5& operator=(const FileInformationMZ5&);
  void init(const unsigned short majorVersion, const unsigned short minorVersion, const unsigned didFiltering, const unsigned deltaMZ, const unsigned translateInten);
  static CompType getType();
};

struct ContVocabMZ5Data  {
  char* uri;
  char* fullname;
  char* id;
  char* version;
};

struct ContVocabMZ5: public ContVocabMZ5Data {
  ContVocabMZ5();
  ContVocabMZ5(const string& uri, const string& fullname, const string& id, const string& version);
  ContVocabMZ5(const char* uri, const char* fullname, const char* id, const char* version);
  ContVocabMZ5(const ContVocabMZ5&);
  ContVocabMZ5& operator=(const ContVocabMZ5&);
  ~ContVocabMZ5();
  void init(const string&, const string&, const string&, const string&);
  static CompType getType();
};

struct CVRefMZ5Data {
  char* name;
  char* prefix;
  unsigned long accession;
};

struct CVRefMZ5: public CVRefMZ5Data {
  CVRefMZ5();
  CVRefMZ5(const CVRefMZ5&);
  CVRefMZ5& operator=(const CVRefMZ5&);
  ~CVRefMZ5();
  void init(const char* name, const char* prefix,  const unsigned long accession);
  static CompType getType();
};

struct UserParamMZ5Data {
  char name[USRNL];
  char value[USRVL];
  char type[USRTL];
  unsigned long unitCVRefID;
};

struct UserParamMZ5: public UserParamMZ5Data {
  UserParamMZ5();
  UserParamMZ5(const UserParamMZ5&);
  UserParamMZ5& operator=(const UserParamMZ5&);
  ~UserParamMZ5();
  void init(const char* name, const char* value, const char* type, const unsigned long urefid);
  static CompType getType();
};

struct CVParamMZ5Data {
  char value[CVL];
  unsigned long typeCVRefID;
  unsigned long unitCVRefID;
};

struct CVParamMZ5: public CVParamMZ5Data {
  CVParamMZ5();
  CVParamMZ5(const CVParamMZ5&);
  CVParamMZ5& operator=(const CVParamMZ5&);
  ~CVParamMZ5();
  void init(const char* value, const unsigned long& cvrefid, const unsigned long& urefid);
  static CompType getType();
};

struct RefMZ5Data {
  unsigned long refID;
};

struct RefMZ5: public RefMZ5Data {
  RefMZ5();
  RefMZ5(const RefMZ5&);
  RefMZ5& operator=(const RefMZ5&);
  ~RefMZ5();
  static CompType getType();
};

struct RefListMZ5Data {
  size_t len;
  RefMZ5* list;
};

struct RefListMZ5: RefListMZ5Data {
  RefListMZ5();
  RefListMZ5(const RefListMZ5&);
  RefListMZ5& operator=(const RefListMZ5&);
  ~RefListMZ5();
  void init(const RefMZ5* list, const size_t len);
  static VarLenType getType();
};

struct ParamListMZ5Data {
  unsigned long cvParamStartID;
  unsigned long cvParamEndID;
  unsigned long userParamStartID;
  unsigned long userParamEndID;
  unsigned long refParamGroupStartID;
  unsigned long refParamGroupEndID;
};

struct ParamListMZ5: ParamListMZ5Data {
  ParamListMZ5();
  ParamListMZ5(const ParamListMZ5&);
  ParamListMZ5& operator=(const ParamListMZ5&);
  ~ParamListMZ5();
  void init(const unsigned long cvstart, const unsigned long cvend, const unsigned long usrstart, const unsigned long usrend, const unsigned long refstart, const unsigned long refend);
  static CompType getType();
};

struct ParamGroupMZ5 {
  char* id;
  ParamListMZ5 paramList;
  ParamGroupMZ5();
  ParamGroupMZ5(const ParamGroupMZ5&);
  ParamGroupMZ5& operator=(const ParamGroupMZ5&);
  ~ParamGroupMZ5();
  void init(const ParamListMZ5& params, const char* id);
  static CompType getType();
};

struct SourceFileMZ5 {
  char* id;
  char* location;
  char* name;
  ParamListMZ5 paramList;
  SourceFileMZ5();
  SourceFileMZ5(const SourceFileMZ5&);
  SourceFileMZ5& operator=(const SourceFileMZ5&);
  ~SourceFileMZ5();
  void init(const ParamListMZ5& params, const char* id, const char* location, const char* name);
  static CompType getType();
};

struct SampleMZ5 {
  char* id;
  char* name;
  ParamListMZ5 paramList;
  SampleMZ5();
  SampleMZ5(const SampleMZ5&);
  SampleMZ5& operator=(const SampleMZ5&);
  ~SampleMZ5();
  void init(const ParamListMZ5& params, const char* id, const char* name);
  static CompType getType();
};

struct SoftwareMZ5 {
  char* id;
  char* version;
  ParamListMZ5 paramList;
  SoftwareMZ5();
  SoftwareMZ5(const SoftwareMZ5&);
  SoftwareMZ5& operator=(const SoftwareMZ5&);
  ~SoftwareMZ5();
  void init(const ParamListMZ5& params, const char* id, const char* version);
  static CompType getType();
};

struct ParamListsMZ5 {
  size_t len;
  ParamListMZ5* lists;
  ParamListsMZ5();
  ParamListsMZ5(const ParamListsMZ5&);
  ParamListsMZ5& operator=(const ParamListsMZ5&);
  ~ParamListsMZ5();
  void init(const ParamListMZ5* list, const size_t len);
  static VarLenType getType();
};

struct ScanSettingMZ5 {
  char* id;
  ParamListMZ5 paramList;
  RefListMZ5 sourceFileIDs;
  ParamListsMZ5 targetList;
  ScanSettingMZ5();
  ScanSettingMZ5(const ScanSettingMZ5&);
  ScanSettingMZ5& operator=(const ScanSettingMZ5&);
  ~ScanSettingMZ5();
  void init(const ParamListMZ5& params, const RefListMZ5& refSourceFiles, const ParamListsMZ5 targets, const char* id);
  static CompType getType();
};

struct ComponentMZ5 {
  ParamListMZ5 paramList;
  unsigned long order;
  ComponentMZ5();
  ComponentMZ5(const ComponentMZ5&);
  ComponentMZ5& operator=(const ComponentMZ5&);
  ~ComponentMZ5();
  void init(const ParamListMZ5&, const unsigned long order);
  static CompType getType();
};

struct ComponentListMZ5 {
  size_t len;
  ComponentMZ5* list;
  ComponentListMZ5();
  ComponentListMZ5(const ComponentListMZ5&);
  ComponentListMZ5(const vector<ComponentMZ5>&);
  ComponentListMZ5& operator=(const ComponentListMZ5&);
  ~ComponentListMZ5();
  void init(const ComponentMZ5*, const size_t&);
  static VarLenType getType();
};

struct ComponentsMZ5 {
  ComponentListMZ5 sources;
  ComponentListMZ5 analyzers;
  ComponentListMZ5 detectors;
  ComponentsMZ5();
  ComponentsMZ5(const ComponentsMZ5&);
  ComponentsMZ5& operator=(const ComponentsMZ5&);
  ~ComponentsMZ5();
  void init(const ComponentListMZ5& sources, const ComponentListMZ5& analyzers,  const ComponentListMZ5& detectors);
  static CompType getType();
};

struct InstrumentConfigurationMZ5 {
  char* id;
  ParamListMZ5 paramList;
  ComponentsMZ5 components;
  RefMZ5 scanSettingRefID;
  RefMZ5 softwareRefID;
  InstrumentConfigurationMZ5();
  InstrumentConfigurationMZ5(const InstrumentConfigurationMZ5&);
  InstrumentConfigurationMZ5& operator=(const InstrumentConfigurationMZ5&);
  ~InstrumentConfigurationMZ5();
  void init(const ParamListMZ5& params, const ComponentsMZ5& components, const RefMZ5& refScanSetting, const RefMZ5& refSoftware, const char* id);
  static CompType getType();
};

struct ProcessingMethodMZ5 {
  ParamListMZ5 paramList;
  RefMZ5 softwareRefID;
  unsigned long order;
  ProcessingMethodMZ5();
  ProcessingMethodMZ5(const ProcessingMethodMZ5&);
  ProcessingMethodMZ5& operator=(const ProcessingMethodMZ5&);
  ~ProcessingMethodMZ5();
  void init(const ParamListMZ5& params, const RefMZ5& refSoftware, const unsigned long order);
  static CompType getType();
};

struct ProcessingMethodListMZ5 {
  size_t len;
  ProcessingMethodMZ5* list;
  ProcessingMethodListMZ5();
  ProcessingMethodListMZ5(const ProcessingMethodListMZ5&);
  ProcessingMethodListMZ5& operator=(const ProcessingMethodListMZ5&);
  ~ProcessingMethodListMZ5();
  void init(const ProcessingMethodMZ5* list, const size_t len);
  static VarLenType getType();
};

struct DataProcessingMZ5  {
  char* id;
  ProcessingMethodListMZ5 processingMethodList;
  DataProcessingMZ5();
  DataProcessingMZ5(const DataProcessingMZ5&);
  DataProcessingMZ5& operator=(const DataProcessingMZ5&);
  ~DataProcessingMZ5();
  void init(const ProcessingMethodListMZ5&, const char* id);
  static CompType getType();
};

struct PrecursorMZ5  {
  char* externalSpectrumId;
  ParamListMZ5 activation;
  ParamListMZ5 isolationWindow;
  ParamListsMZ5 selectedIonList;
  RefMZ5 spectrumRefID;
  RefMZ5 sourceFileRefID;
  PrecursorMZ5();
  PrecursorMZ5(const PrecursorMZ5&);
  PrecursorMZ5& operator=(const PrecursorMZ5&);
  ~PrecursorMZ5();
  void init(const ParamListMZ5& activation,  const ParamListMZ5& isolationWindow, const ParamListsMZ5 selectedIonList, const RefMZ5& refSpectrum, const RefMZ5& refSourceFile, const char* externalSpectrumId);
  static CompType getType();
};

struct PrecursorListMZ5 {
  size_t len;
  PrecursorMZ5* list;
  PrecursorListMZ5();
  PrecursorListMZ5(const PrecursorListMZ5&);
  PrecursorListMZ5& operator=(const PrecursorListMZ5&);
  ~PrecursorListMZ5();
  void init(const PrecursorMZ5*, const size_t len);
  static VarLenType getType();
};

struct ChromatogramMZ5 {
  char* id;
  ParamListMZ5 paramList;
  PrecursorMZ5 precursor;
  ParamListMZ5 productIsolationWindow;
  RefMZ5 dataProcessingRefID;
  unsigned long index;
  ChromatogramMZ5();
  ChromatogramMZ5(const ChromatogramMZ5&);
  ChromatogramMZ5& operator=(const ChromatogramMZ5&);
  ~ChromatogramMZ5();
  void init(const ParamListMZ5& params, const PrecursorMZ5& precursor, const ParamListMZ5& productIsolationWindow, const RefMZ5& refDataProcessing, const unsigned long index, const char* id);
  static CompType getType();
};

struct ScanMZ5 {
  char* externalSpectrumID;
  ParamListMZ5 paramList;
  ParamListsMZ5 scanWindowList;
  RefMZ5 instrumentConfigurationRefID;
  RefMZ5 sourceFileRefID;
  RefMZ5 spectrumRefID;
  ScanMZ5();
  ScanMZ5(const ScanMZ5&);
  ScanMZ5& operator=(const ScanMZ5&);
  ~ScanMZ5();
  void init(const ParamListMZ5& params, const ParamListsMZ5& scanWindowList, const RefMZ5& refInstrument, const RefMZ5& refSourceFile, const RefMZ5& refSpectrum, const char* externalSpectrumID);
  static CompType getType();
};

struct ScanListMZ5 {
  size_t len;
  ScanMZ5* list;
  ScanListMZ5();
  ScanListMZ5(const ScanListMZ5&);
  ScanListMZ5(const vector<ScanMZ5>&);
  ScanListMZ5& operator=(const ScanListMZ5&);
  ~ScanListMZ5();
  void init(const ScanMZ5* list, const size_t len);
  static VarLenType getType();
};

struct ScansMZ5 {
  ParamListMZ5 paramList;
  ScanListMZ5 scanList;
  ScansMZ5();
  ScansMZ5(const ScansMZ5&);
  ScansMZ5& operator=(const ScansMZ5&);
  ~ScansMZ5();
  void init(const ParamListMZ5& params, const ScanListMZ5& scanList);
  static CompType getType();
};

struct SpectrumMZ5 {
  char* id;
  char* spotID;
  ParamListMZ5 paramList;
  ScansMZ5 scanList;
  PrecursorListMZ5 precursorList;
  ParamListsMZ5 productList;
  RefMZ5 dataProcessingRefID;
  RefMZ5 sourceFileRefID;
  unsigned int index;
  SpectrumMZ5();
  SpectrumMZ5(const SpectrumMZ5&);
  SpectrumMZ5& operator=(const SpectrumMZ5&);
  ~SpectrumMZ5();
  void init(const ParamListMZ5& params, const ScansMZ5& scanList, const PrecursorListMZ5& precursors, const ParamListsMZ5& productIonIsolationWindows, const RefMZ5& refDataProcessing, const RefMZ5& refSourceFile, const unsigned long index, const char* id, const char* spotID);
  static CompType getType();
};

struct RunMZ5 {
  char* id;
  char* startTimeStamp;
  char* fid;
  char* facc;
  ParamListMZ5 paramList;
  RefMZ5 defaultSpectrumDataProcessingRefID;
  RefMZ5 defaultChromatogramDataProcessingRefID;
  RefMZ5 defaultInstrumentConfigurationRefID;
  RefMZ5 sourceFileRefID;
  RefMZ5 sampleRefID;
  RunMZ5();
  RunMZ5(const RunMZ5&);
  RunMZ5& operator=(const RunMZ5&);
  ~RunMZ5();
  void init(const ParamListMZ5& params, const RefMZ5& refSpectrumDP, const RefMZ5& refChromatogramDP, const RefMZ5& refDefaultInstrument, const RefMZ5& refSourceFile, const RefMZ5& refSample, const char* id, const char* startTimeStamp, const char* fid, const char* facc);
  static CompType getType();
};

struct BinaryDataMZ5 {
  ParamListMZ5 xParamList;
  ParamListMZ5 yParamList;
  RefMZ5 xDataProcessingRefID;
  RefMZ5 yDataProcessingRefID;
  BinaryDataMZ5();
  BinaryDataMZ5(const BinaryDataMZ5&);
  BinaryDataMZ5& operator=(const BinaryDataMZ5&);
  ~BinaryDataMZ5();
  void init(const ParamListMZ5& xParams, const ParamListMZ5& yParams, const RefMZ5& refDPx, const RefMZ5& refDPy);
  static CompType getType();
};

struct CVRefItem {
  int group; //0=MS, 1=UO, don't know what others there are.
  int ref;
};

class mzpMz5Config{
public:
  mzpMz5Config();
  ~mzpMz5Config();

  static bool PRINT_HDF5_EXCEPTIONS;

  const bool      doFiltering() const;
  const bool      doTranslating() const;
  const size_t    getBufferInB();
  const DataType& getDataTypeFor(const MZ5DataSets v);
  const string&    getNameFor(const MZ5DataSets v);
  const size_t&    getRdccSlots();
  MZ5DataSets      getVariableFor(const string& name);
  void            setFiltering(const bool flag) const;
  void            setTranslating(const bool flag) const;

protected:

private:

  size_t                      bufferInMB_;
  ChromatogramLoadPolicy      chromatogramLoadPolicy_;
  int                          deflateLvl_;
  mutable bool                doFiltering_;
  mutable bool                doTranslating_;
  size_t                      rdccSolts_;
  SpectrumLoadPolicy          spectrumLoadPolicy_;
  map<MZ5DataSets, size_t>    variableBufferSizes_;
  map<MZ5DataSets, hsize_t>    variableChunkSizes_;
  map<MZ5DataSets, string>    variableNames_;
  map<MZ5DataSets, DataType>  variableTypes_;
  map<string, MZ5DataSets>    variableVariables_; //Really? variableVariables? Was this written by Donald Rumsfeld?

  void init(const bool filter, const bool deltamz, const bool translateinten);

};

class cMz5Index : public cindex  {
public:
  unsigned long cvStart;
  unsigned long cvLen;
};

class mzpMz5Handler{
public:
  mzpMz5Handler(mzpMz5Config* c, BasicSpectrum* s);
  mzpMz5Handler(mzpMz5Config* c, BasicSpectrum* s, BasicChromatogram* bc);
  ~mzpMz5Handler();

  void                            clean(const MZ5DataSets v, void* data, const size_t dsend);
  vector<cMz5Index>*              getChromatIndex();
  void                            getData(vector<double>& data, const MZ5DataSets v, const hsize_t start, const hsize_t end);
  const map<MZ5DataSets, size_t>&  getFields();
  vector<cMz5Index>*              getSpecIndex();
  int                              highChromat();
  int                              highScan();
  int                              lowScan();
  void                            processCVParams(unsigned long index);
  bool                            readChromatogram(int num=-1);
  void*                            readDataSet(const MZ5DataSets v, size_t& dsend, void* ptr=0);
  bool                            readFile(const string filename);
  bool                            readHeader(int num=-1);
  bool                            readSpectrum(int num=-1);

protected:

private:

  //  mzpMz5Handler index data members.
  cMz5Index          curIndex;
  f_off              indexOffset;
  int                m_scanIDXCount;
  vector<cMz5Index>  m_vIndex;
  int                posIndex;

  cMz5Index          curChromatIndex;
  vector<cMz5Index>  m_vChromatIndex;
  int                posChromatIndex;

  map<MZ5DataSets, DataSet> bufferMap_;
  BasicChromatogram*        chromat;
  bool                      closed_;
  mzpMz5Config*              config_;
  vector<CVRefItem>          cvRef;
  vector<CVParamMZ5>        cvParams_;
  map<MZ5DataSets, size_t>  fields_;
  H5File*                    file_;
  BasicSpectrum*            spec;
};

#endif

//------------------------------------------------
// RAMP API
//------------------------------------------------
#define INSTRUMENT_LENGTH 2000
#define SCANTYPE_LENGTH 32
#define CHARGEARRAY_LENGTH 128
#define PRECURSORARRAY_LENGTH 512

typedef double RAMPREAL; 
typedef f_off ramp_fileoffset_t;

typedef struct RAMPFILE{
  BasicSpectrum* bs;
  mzpSAXMzmlHandler* mzML;
  mzpSAXMzxmlHandler* mzXML;
  #ifdef MZP_MZ5
  mzpMz5Config* mz5Config;
  mzpMz5Handler* mz5;
  #endif
  int fileType;
  int bIsMzData;
  RAMPFILE(){
    bs=NULL;
    mzML=NULL;
    mzXML=NULL;
    #ifdef MZP_MZ5
    mz5=NULL;
    mz5Config=NULL;
    #endif
    fileType=0;
    bIsMzData=0;
  }
  ~RAMPFILE(){
    if(bs!=NULL) delete bs;
    if(mzML!=NULL) delete mzML;
    if(mzXML!=NULL) delete mzXML;
    bs=NULL;
    mzML=NULL;
    mzXML=NULL;
    #ifdef MZP_MZ5
    if(mz5!=NULL) delete mz5;
    if(mz5Config!=NULL) delete mz5Config;
    mz5=NULL;
    mz5Config=NULL;
    #endif
  }
} RAMPFILE;

static vector<const char *> data_Ext;

struct ScanHeaderStruct {
   
  int    acquisitionNum;            // scan number as declared in File (may be gaps)
  int    mergedScan;                // only if MS level > 1 
  int    mergedResultScanNum;       // scan number of the resultant merged scan 
  int    mergedResultStartScanNum;  // smallest scan number of the scanOrigin for merged scan 
  int    mergedResultEndScanNum;    // largest scan number of the scanOrigin for merged scan 
  int    msLevel;
  int    numPossibleCharges;
  int    peaksCount;
  int    precursorCharge;           // only if MS level > 1 
  int    precursorCount;   
  int    precursorScanNum;          // only if MS level > 1 
  int    scanIndex;                 //a sequential index for non-sequential scan numbers (1-based)
  int    seqNum;                    //number in sequence observed file (1-based)
   
  double basePeakIntensity;
  double basePeakMZ;
  double collisionEnergy;
  double compensationVoltage;   // only if MS level > 1
  double highMZ;
  double ionInjectionTime;
  double ionisationEnergy;
  double lowMZ;
  double precursorIntensity;    // only if MS level > 1
  double precursorMonoMZ;
  double precursorMZ;           //only if MS level > 1 
  double retentionTime;         //in seconds
  double selectionWindowLower;  //the range of ions acquired
  double selectionWindowUpper;  //in DDA, for example, +/-1 Da around precursor
  double totIonCurrent;
   
  char   activationMethod[SCANTYPE_LENGTH];
  char   additionalPrecursors[PRECURSORARRAY_LENGTH];
  char   filterLine[CHARGEARRAY_LENGTH];
  char   idString[CHARGEARRAY_LENGTH];
  char   possibleCharges[SCANTYPE_LENGTH];
  char   scanType[SCANTYPE_LENGTH];
        
  bool   centroid; //true if spectrum is centroided
  bool   possibleChargesArray[CHARGEARRAY_LENGTH]; /* NOTE: does NOT include "precursorCharge" information; only from "possibleCharges" */
   
  ramp_fileoffset_t    filePosition; /* where in the file is this header? */
};

struct RunHeaderStruct {
  int     scanCount;

  double  dEndTime;
  double  dStartTime;
  double  endMZ;
  double  highMZ;
  double  lowMZ;
  double  startMZ;
};

typedef struct InstrumentStruct {
   char manufacturer[INSTRUMENT_LENGTH];
   char model[INSTRUMENT_LENGTH];
   char ionisation[INSTRUMENT_LENGTH];
   char analyzer[INSTRUMENT_LENGTH];
   char detector[INSTRUMENT_LENGTH];
} InstrumentStruct;

struct ScanCacheStruct {
  int seqNumStart;    // scan at which the cache starts
  int size;           // number of scans in the cache
  struct ScanHeaderStruct *headers;
  RAMPREAL **peaks;
};

int                 checkFileType(const char* fname);
ramp_fileoffset_t   getIndexOffset(RAMPFILE *pFI);
InstrumentStruct*   getInstrumentStruct(RAMPFILE *pFI);
void                getPrecursor(const struct ScanHeaderStruct *scanHeader, int index, double &mz, double &monoMZ, double &intensity, int &charge, int &possibleCharges, int *&possibleChargeArray);
void                getScanSpanRange(const struct ScanHeaderStruct *scanHeader, int *startScanNum, int *endScanNum);
void                rampCloseFile(RAMPFILE *pFI);
string              rampConstructInputFileName(const string &basename);
char*               rampConstructInputFileName(char *buf,int buflen,const char *basename);
char*               rampConstructInputPath(char *buf, int inbuflen, const char *dir_in, const char *basename);
const char**        rampListSupportedFileTypes();
RAMPFILE*           rampOpenFile(const char *filename);
char*               rampValidFileType(const char *buf);
void                readHeader(RAMPFILE *pFI, ramp_fileoffset_t lScanIndex, struct ScanHeaderStruct *scanHeader);
void                readHeader(RAMPFILE *pFI, ramp_fileoffset_t lScanIndex, struct ScanHeaderStruct *scanHeader, unsigned long scanI);
ramp_fileoffset_t*  readIndex(RAMPFILE *pFI, ramp_fileoffset_t indexOffset, int *iLastScan);
int                 readMsLevel(RAMPFILE *pFI, ramp_fileoffset_t lScanIndex);
void                readMSRun(RAMPFILE *pFI, struct RunHeaderStruct *runHeader);
RAMPREAL*           readPeaks(RAMPFILE *pFI, ramp_fileoffset_t lScanIndex);
RAMPREAL*           readPeaks(RAMPFILE* pFI, ramp_fileoffset_t lScanIndex, unsigned long scanI);
int                 readPeaksCount(RAMPFILE *pFI, ramp_fileoffset_t lScanIndex);
int                 readPeaksCount(RAMPFILE *pFI, ramp_fileoffset_t lScanIndex, unsigned long scanI);
void                readRunHeader(RAMPFILE *pFI, ramp_fileoffset_t *pScanIndex, struct RunHeaderStruct *runHeader, int iLastScan);

//MH:Cached RAMP functions
void                            clearScanCache(struct ScanCacheStruct* cache);
void                            freeScanCache(struct ScanCacheStruct* cache);
int                              getCacheIndex(struct ScanCacheStruct* cache, int seqNum);
struct ScanCacheStruct*          getScanCache(int size);
const struct ScanHeaderStruct*  readHeaderCached(struct ScanCacheStruct* cache, int seqNum, RAMPFILE* pFI, ramp_fileoffset_t lScanIndex);
int                              readMsLevelCached(struct ScanCacheStruct* cache, int seqNum, RAMPFILE* pFI, ramp_fileoffset_t lScanIndex);
const RAMPREAL*                  readPeaksCached(struct ScanCacheStruct* cache, int seqNum, RAMPFILE* pFI, ramp_fileoffset_t lScanIndex);
void                            shiftScanCache(struct ScanCacheStruct* cache, int nScans);

//MH:Unimplimented functions. These just bark cerr when used.
int                  isScanAveraged(struct ScanHeaderStruct *scanHeader);
int                  isScanMergedResult(struct ScanHeaderStruct *scanHeader);
int                  rampSelfTest(char *filename);
char*                rampTrimBaseName(char *buf);
int                  rampValidateOrDeriveInputFilename(char *inbuf, int inbuflen, char *spectrumName);
double              readStartMz(RAMPFILE *pFI, ramp_fileoffset_t lScanIndex);
double              readEndMz(RAMPFILE *pFI, ramp_fileoffset_t lScanIndex);
void                setRampOption(long option);


//------------------------------------------------
// PWiz API
//------------------------------------------------
class Chromatogram{
public:
  Chromatogram();
  ~Chromatogram();

  BasicChromatogram*  bc;
  string              id;

  void getTimeIntensityPairs(vector<TimeIntensityPair>& v);
};
typedef class Chromatogram* ChromatogramPtr;

class ChromatogramList{
public:
  ChromatogramList();
  ChromatogramList(mzpSAXMzmlHandler* ml, void* m5, BasicChromatogram* bc);
  ~ChromatogramList();

  ChromatogramPtr    chromatogram(int index, bool binaryData = false);
  bool              get();
  size_t            size();

  vector<cindex>*      vChromatIndex;
  #ifdef MZP_MZ5
  vector<cMz5Index>*  vMz5Index;
  #endif

private:
  mzpSAXMzmlHandler*  mzML;
  #ifdef MZP_MZ5
  mzpMz5Handler*      mz5;
  #endif
  ChromatogramPtr      chromat;
};
typedef class ChromatogramList* ChromatogramListPtr;

class PwizRun{
public:
  PwizRun();
  PwizRun(mzpSAXMzmlHandler* ml, void* m5, BasicChromatogram* b);
  ~PwizRun();

  ChromatogramListPtr chromatogramListPtr;

  void set(mzpSAXMzmlHandler* ml, void* m5, BasicChromatogram* b);

private:
  mzpSAXMzmlHandler*  mzML;
  #ifdef MZP_MZ5
  mzpMz5Handler*      mz5;
  #endif
  BasicChromatogram*  bc;
};

class MSDataFile{
public:
  MSDataFile(string s);
  ~MSDataFile();

  PwizRun run;

private:
  BasicSpectrum*      bs;
  BasicChromatogram*  bc;
  mzpSAXMzmlHandler*  mzML;
  #ifdef MZP_MZ5
  mzpMz5Config*        mz5Config;
  mzpMz5Handler*      mz5;
  #endif
};

//------------------------------------------------
// MzParser Interface
//------------------------------------------------
class MzParser {
public:
  //Constructors and Destructors
  MzParser(BasicSpectrum* s);
  MzParser(BasicSpectrum* s, BasicChromatogram* c);
  ~MzParser();

  //User functions
  vector<cindex>*  getChromatIndex();
  int   highChromat();
  int   highScan();
  bool  load(char* fname);
  int   lowScan();
  bool  readChromatogram(int num=-1);
  bool  readSpectrum(int num=-1);
  bool  readSpectrumHeader(int num=-1);

protected:
  mzpSAXMzmlHandler*    mzML;
  mzpSAXMzxmlHandler*   mzXML;
  #ifdef MZP_MZ5
  mzpMz5Handler*        mz5;
  mzpMz5Config*         mz5Config;
  #endif

private:
  //private functions
  int checkFileType(char* fname);

  //private data members
  BasicChromatogram*  chromat;
  int                 fileType;
  BasicSpectrum*      spec;

};

#endif