forked from marbl/canu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlapReadCache.C
190 lines (125 loc) · 4.13 KB
/
overlapReadCache.C
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
/******************************************************************************
*
* This file is part of canu, a software program that assembles whole-genome
* sequencing reads into contigs.
*
* This software is based on:
* 'Celera Assembler' r4587 (http://wgs-assembler.sourceforge.net)
* the 'kmer package' r1994 (http://kmer.sourceforge.net)
*
* Except as indicated otherwise, this is a 'United States Government Work',
* and is released in the public domain.
*
* File 'README.licenses' in the root directory of this distribution
* contains full conditions and disclaimers.
*/
#include "overlapReadCache.H"
#include <set>
overlapReadCache::overlapReadCache(sqStore *seqStore_, uint64 memLimit) {
seqStore = seqStore_;
nReads = seqStore->sqStore_lastReadID();
readAge = new uint32 [nReads + 1];
readLen = new uint32 [nReads + 1];
memset(readAge, 0, sizeof(uint32) * (nReads + 1));
memset(readLen, 0, sizeof(uint32) * (nReads + 1));
readSeqFwd = new char * [nReads + 1];
memset(readSeqFwd, 0, sizeof(char *) * (nReads + 1));
memoryLimit = memLimit * 1024 * 1024 * 1024;
}
overlapReadCache::~overlapReadCache() {
delete [] readAge;
delete [] readLen;
for (uint32 rr=0; rr<=nReads; rr++)
delete [] readSeqFwd[rr];
delete [] readSeqFwd;
}
void
overlapReadCache::loadRead(uint32 id) {
seqStore->sqStore_getRead(id, &read);
readLen[id] = read.sqRead_length();
readSeqFwd[id] = new char [readLen[id] + 1];
memcpy(readSeqFwd[id], read.sqRead_sequence(), sizeof(char) * readLen[id]);
readSeqFwd[id][readLen[id]] = 0;
}
// Make sure that the reads in 'reads' are in the cache.
// Ideally, these are just the reads we need to load.
void
overlapReadCache::loadReads(std::set<uint32> reads) {
uint32 nn = 0;
uint32 nc = reads.size() / 25;
// For each read in the input set, load it.
//if (reads.size() > 0)
// fprintf(stderr, "loadReads()-- Need to load %u reads.\n", reads.size());
for (auto it=reads.begin(); it != reads.end(); ++it) {
//if ((++nn % nc) == 0)
// fprintf(stderr, "loadReads()-- %6.2f%% finished.\n", 100.0 * nn / reads.size());
if (readLen[*it] != 0)
continue;
loadRead(*it);
}
//fprintf(stderr, "loadReads()-- %6.2f%% finished.\n", 100.0);
// Age all the reads in the cache.
uint32 nLoaded = 0;
for (uint32 id=0; id<nReads; id++) {
if (readLen[id] > 0)
nLoaded++;
readAge[id]++;
}
//fprintf(stderr, "loadReads()-- loaded %u -- %u in cache\n", reads.size(), nLoaded);
}
void
overlapReadCache::markForLoading(std::set<uint32> &reads, uint32 id) {
// Note that it was just used.
readAge[id] = 0;
// Already loaded? Done!
if (readLen[id] != 0)
return;
// Already pending? Done!
if (reads.count(id) != 0)
return;
// Mark it for loading.
reads.insert(id);
}
void
overlapReadCache::loadReads(ovOverlap *ovl, uint32 nOvl) {
std::set<uint32> reads;
for (uint32 oo=0; oo<nOvl; oo++) {
markForLoading(reads, ovl[oo].a_iid);
markForLoading(reads, ovl[oo].b_iid);
}
loadReads(reads);
}
void
overlapReadCache::loadReads(tgTig *tig) {
std::set<uint32> reads;
markForLoading(reads, tig->tigID());
for (uint32 oo=0; oo<tig->numberOfChildren(); oo++)
if (tig->getChild(oo)->isRead() == true)
markForLoading(reads, tig->getChild(oo)->ident());
loadReads(reads);
}
void
overlapReadCache::purgeReads(void) {
uint32 maxAge = 0;
uint64 memoryUsed = 0;
// Find maxAge, and sum memory used
for (uint32 rr=0; rr<=nReads; rr++) {
if (maxAge < readAge[rr])
maxAge = readAge[rr];
memoryUsed += readLen[rr];
}
// Purge oldest until memory is below watermark
while ((memoryLimit < memoryUsed) &&
(maxAge > 1)) {
fprintf(stderr, "purgeReads()-- used " F_U64 "MB limit " F_U64 "MB -- purge age " F_U32 "\n", memoryUsed >> 20, memoryLimit >> 20, maxAge);
for (uint32 rr=0; rr<=nReads; rr++) {
if (maxAge == readAge[rr]) {
memoryUsed -= readLen[rr];
delete [] readSeqFwd[rr]; readSeqFwd[rr] = NULL;
readLen[rr] = 0;
readAge[rr] = 0;
}
}
maxAge--;
}
}