Debian Codegraph @

libmeryl/libmeryl.H

raw
1 #ifndef LIBMERYL_H
2 #define LIBMERYL_H
3
4 #include "bio++.H"
5
6 // A merStream reader/writer for meryl mercount data.
7 //
8 // merSize is used to check that the meryl file is the correct size.
9 // If it isn't the code fails.
10 //
11 // The reader returns mers in lexicographic order. No random access.
12 // The writer assumes that mers come in sorted increasingly.
13 //
14 // numUnique the total number of mers with count of one
15 // numDistinct the total number of distinct mers in this file
16 // numTotal the total number of mers in this file
17
18
19 class merylStreamReader {
20 public:
21 merylStreamReader(const char *fn, uint32 ms=0);
22 ~merylStreamReader();
23
24 kMer &theFMer(void) { return(_thisMer); };
25 uint64 theCount(void) { return(_thisMerCount); };
26
27 bool hasPositions(void) { return(_POS != 0L); };
28 uint32 *thePositions(void) { return(_thisMerPositions); };
29 uint32 getPosition(uint32 i) { return(((_POS) && (i < _thisMerCount)) ? _thisMerPositions[i] : ~uint32ZERO); };
30
31 uint32 merSize(void) { return(_merSizeInBits >> 1); };
32 uint32 merCompression(void) { return(_merCompression); };
33
34 uint32 prefixSize(void) { return(_prefixSize); };
35
36 uint64 numberOfUniqueMers(void) { return(_numUnique); };
37 uint64 numberOfDistinctMers(void) { return(_numDistinct); };
38 uint64 numberOfTotalMers(void) { return(_numTotal); };
39
40 uint64 histogram(uint32 i) { return((i < _histogramLen) ? _histogram[i] : ~uint64ZERO); };
41 uint64 histogramLength(void) { return(_histogramLen); };
42 uint64 histogramHuge(void) { return(_histogramHuge); };
43 uint64 histogramMaximumCount(void) { return(_histogramMaxValue); };
44
45 bool nextMer(void);
46 bool validMer(void) { return(_validMer); };
47 private:
48 bitPackedFile *_IDX;
49 bitPackedFile *_DAT;
50 bitPackedFile *_POS;
51
52 uint64 getIDXnumber(void) {
53 uint64 n = 1;
54
55 if (_idxIsPacked)
56 n = _IDX->getNumber();
57 else
58 n = _IDX->getBits(32);
59
60 return(n);
61 };
62 uint64 getDATnumber(void) {
63 uint64 n = 1;
64
65 if (_datIsPacked) {
66 if (_DAT->getBits(1))
67 n = _DAT->getNumber() + 2;
68 } else {
69 n = _DAT->getBits(32);
70 }
71
72 return(n);
73 };
74
75 // Why not bool? Seems like the bitPackedFile is incompatible
76 // with bools.
77 uint32 _idxIsPacked;
78 uint32 _datIsPacked;
79 uint32 _posIsPacked;
80
81 uint32 _merSizeInBits;
82 uint32 _merCompression;
83 uint32 _prefixSize;
84 uint32 _merDataSize;
85 uint64 _thisBucket;
86 uint64 _thisBucketSize;
87 uint64 _numBuckets;
88
89 kMer _thisMer;
90 uint64 _thisMerCount;
91
92 uint32 _thisMerPositionsMax;
93 uint32 *_thisMerPositions;
94
95 uint64 _numUnique;
96 uint64 _numDistinct;
97 uint64 _numTotal;
98
99 uint64 _histogramHuge; // number that are bigger than Len
100 uint64 _histogramLen; // number of entries in the histo
101 uint64 _histogramMaxValue; // highest count ever seen
102 uint64 *_histogram;
103
104 bool _validMer;
105 };
106
107
108 class merylStreamWriter {
109 public:
110 merylStreamWriter(const char *filePrefix,
111 uint32 merSize, // In bases
112 uint32 merComp, // A length, bases
113 uint32 prefixSize, // In bits
114 bool positionsEnabled);
115 ~merylStreamWriter();
116
117 void addMer(kMer &mer, uint32 count=1, uint32 *positions=0L);
118 void addMer(uint64 prefix, uint32 prefixBits,
119 uint64 mer, uint32 merBits,
120 uint32 count=1,
121 uint32 *positions=0L);
122
123 private:
124 void writeMer(void);
125
126 void setIDXnumber(uint64 n) {
127 if (_idxIsPacked)
128 _IDX->putNumber(n);
129 else
130 _IDX->putBits(n, 32);
131 };
132 void setDATnumber(uint64 n) {
133 if (_datIsPacked) {
134 if (n == 1) {
135 _DAT->putBits(uint64ZERO, 1);
136 } else {
137 _DAT->putBits(uint64ONE, 1);
138 _DAT->putNumber(n-2);
139 }
140 } else {
141 _DAT->putBits(n, 32);
142 }
143 };
144
145
146 bitPackedFile *_IDX;
147 bitPackedFile *_DAT;
148 bitPackedFile *_POS;
149
150 uint32 _idxIsPacked;
151 uint32 _datIsPacked;
152 uint32 _posIsPacked;
153
154 uint32 _merSizeInBits;
155 uint32 _merCompression;
156 uint32 _prefixSize;
157 uint32 _merDataSize;
158 uint64 _thisBucket;
159 uint64 _thisBucketSize;
160 uint64 _numBuckets;
161
162 uint64 _numUnique;
163 uint64 _numDistinct;
164 uint64 _numTotal;
165
166 uint64 _histogramHuge; // number that are bigger than Len
167 uint64 _histogramLen; // number of entries in the histo
168 uint64 _histogramMaxValue; // highest count ever seen
169 uint64 *_histogram;
170
171 bool _thisMerIsBits;
172 bool _thisMerIskMer;
173
174 kMer _thisMer;
175
176 uint64 _thisMerPre;
177 uint64 _thisMerMer;
178
179 uint32 _thisMerPreSize;
180 uint32 _thisMerMerSize;
181
182 uint64 _thisMerCount;
183 };
184
185 #endif // LIBMERYL_H