BGenFile.h 3.72 KB
Newer Older
zhanxw's avatar
zhanxw committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef _BGENFILE_H_
#define _BGENFILE_H_

#include <stdint.h>  // uint32_t
#include <set>
#include <string>
#include <vector>

#include "third/zlib/zlib.h"
#include "third/zstd/lib/zstd.h"

#include "BGenIndex.h"
#include "BGenVariant.h"

zhanxw's avatar
zhanxw committed
15
16
17
18
19
// copied from libVcf/VCFConstant.h
#define MISSING_GENOTYPE -9
#define PLINK_MALE 1
#define PLINK_FEMALE 2

zhanxw's avatar
zhanxw committed
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
class RangeList;

class BGenFile {
 public:
  enum SNP_COMPRESSION { NO_COMPRESSION = 0, GZIP = 1, ZSTD = 2 };
  enum LAYOUT { UNSUPPORTED = 0, LAYOUT1 = 1, LAYOUT2 = 2 };
  enum SAMPLE_IDENTIFIER {
    NO_SAMPLE_IDENTIFIER = 0,
    HAS_SAMPLE_IDENTIFIER = 1
  };
  typedef enum {
    BGEN_LINE_MODE,  // read by line
    BGEN_RANGE_MODE  // read by range
  } Mode;

  BGenFile(const std::string& fn);
  /**
   * @return true: if a valid record is read
   */
  bool readRecord();

  //////////////////////////////////////////////////
  // Sample inclusion/exclusion
  void includePeople(const std::string& s);
  void includePeople(const std::vector<std::string>& v);
  void includePeopleFromFile(const char* fn);
  void includeAllPeople();
  void excludePeople(const std::string& s);
  void excludePeople(const std::vector<std::string>& v);
  void excludePeopleFromFile(const char* fn);
  void excludeAllPeople();
  //////////////////////////////////////////////////
  // Adjust range collections
  void enableAutoMerge();
  void disableAutoMerge();
  // void clearRange();
  void setRangeFile(const char* fn);
  // @param l is a string of range(s)
  void setRange(const char* chrom, int begin, int end);
  void setRange(const RangeList& rl);
  void setRangeList(const std::string& l);
  // this function the entry point for all function add/change region list
  void setRangeList(const RangeList& rl);

  // which single-base chromosomal sites are allowed to read
  int setSiteFile(const std::string& fn);
  // void setRangeMode();

 public:
  int getNumMarker() const { return M; }
  int getNumSample() const { return N; }
zhanxw's avatar
zhanxw committed
71
  int getNumEffectiveSample() const;
zhanxw's avatar
zhanxw committed
72
73
74
  const std::vector<std::string>& getSampleIdentifier() const {
    return sampleIdentifier;
  }
zhanxw's avatar
zhanxw committed
75
  void getIncludedSampleName(std::vector<std::string>* p) const;
zhanxw's avatar
zhanxw committed
76
  const BGenVariant& getVariant() const { return var; }
zhanxw's avatar
zhanxw committed
77
  int getEffectiveIndex(int idx) const;
zhanxw's avatar
zhanxw committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

 private:
  BGenFile(const BGenFile&);
  BGenFile& operator=(const BGenFile&);

 private:
  bool parseLayout1();
  bool parseLayout2();

  void parseString(FILE* fp, int lenByte, std::string* out);
  void parseUint32(FILE* fp, uint32_t* value);
  void parseUint16(FILE* fp, uint16_t* value);
  int choose(int n, int m);

  bool isFileEnd(FILE* fp);
  static long getFileSize(const std::string& fn);

  // sample inclusion/exclusion related
  void setPeopleMask(const std::string& s, bool b);
  void setPeopleMaskFromFile(const char* fn, bool b);
  void setRangeMode();
  // range list related
zhanxw's avatar
zhanxw committed
100
101
  void buildEffectiveIndex();

zhanxw's avatar
zhanxw committed
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
 private:
  std::string bgenFileName;
  FILE* fp;
  // first 4 bytes
  uint32_t offset;
  // header block
  uint32_t LH;
  uint32_t M;
  uint32_t N;
  uint8_t magic[4];
  std::vector<uint8_t> freeData;
  uint32_t flag;
  SNP_COMPRESSION snpCompression;
  LAYOUT layout;
  SAMPLE_IDENTIFIER flagSampleIdentifier;
  // sample identifier block
  std::vector<std::string> sampleIdentifier;

  long fileSize;
  std::vector<uint8_t> compressedBuf;
  uint32_t C;  // number of compressed bytes
  std::vector<uint8_t> buf;
  uint32_t D;  // number of bytes before compression

  BGenVariant var;
  BGenIndex index;

  bool autoMergeRange;
  Mode mode;                     /// read consecutively or read by index
  std::vector<bool> sampleMask;  // true means exclusion
zhanxw's avatar
zhanxw committed
132
  std::vector<int> effectiveIndex;
zhanxw's avatar
zhanxw committed
133
134
135
136
137
  // allow chromosomal sites
  std::set<std::string> allowedSite;
};  // class BGenFile

#endif /* _BGENFILE_H_ */