-
Notifications
You must be signed in to change notification settings - Fork 0
/
meanKmerCoverage.cpp
226 lines (213 loc) · 5.51 KB
/
meanKmerCoverage.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include <cstring>
#include <string.h>
#include <ctpl_stl.h>
#include <thread>
using namespace std;
int32_t kmer_len, threadN;
vector<int32_t> mul;
std::vector<std::string> wigout;
ifstream wigfile;
void print_info(void)
{
std::cout << "Find mean unique k-mer coverage for a given k-mer size at each genomic position ver. 1.0\n"
<< "\nUsage:\nmeanKmerCoverage [options] <k-mer> <mapping_wig>\n"
<< "Parameters:\n"
<< "<k-mer> - k-mer size\n"
<< "<mul_wig> - Minimum unique k-mer length (left-anchored) wiggle file\n"
<< "Options:\n"
<< "-t<value> - Number of additional threads to use\n";
}
bool help(int argc, char** argv)
{
const std::string help = "--help";
for (int i = 1; i < argc; ++i)
{
if (argv[i] == help)
return true;
}
return false;
}
bool load_wiggle_chrom(string &chrom, int32_t &end)
{
string cur, cur0, field, new_chrom="";
int32_t pos=0, split, bufsize, start, counter=1000000;
end=0;
mul.clear();
while (getline(wigfile, cur))
{
if (cur[0] == 'f')
{
pos = end;
cur0 = cur;
bufsize = cur0.size() + 1;
while (cur0.length() > 0)
{
split = cur0.find(" ");
if (split == std::string::npos)
split = cur0.length();
field = cur0.substr(0, split);
if (split == cur.length())
cur0.erase(0, split);
else
cur0.erase(0, split + 1);
if (field.find("chrom") == 0)
{
new_chrom = field.substr(field.find("=") + 1, field.length());
if (chrom == "") chrom = new_chrom;
}
else if (field.find("start") == 0)
{
start = stoi(field.substr(field.find("=") + 1, field.length())) - 1;
if (start > pos)
for (int32_t i=pos; i<start;i++) mul.push_back(0);
pos = start;
}
}
if (new_chrom != chrom)
{
wigfile.seekg(-bufsize, ios_base::cur);
break;
}
end = pos;
} else {
mul.push_back(stoi(cur));
end++;
if (end >= counter)
{
fprintf(stderr, "\rRead %iMbp of %s", counter / 1000000, chrom.c_str());
fflush(stderr);
counter += 1000000;
}
}
}
fprintf(stderr, "\r \r");
fprintf(stderr, "%s\t%i\n", chrom.c_str(), end);
fflush(stderr);
if (new_chrom == "")
return false;
else
return true;
}
std::string rtrim(const std::string &s)
{
int32_t end = s.find_last_not_of("0");
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
void find_coverage(const uint id, const string chrom, const int32_t index,
const int32_t maxlen)
{
int32_t start=index*1000000, end=(index+1)*1000000;
end = std::min(end, maxlen);
int32_t count=0, upos=start-kmer_len;
bool header=true;
float fkmer_len = kmer_len;
std::string outline="", val;
for (int32_t i=std::max(0, upos); i<start; i++)
if ((mul[i] > 0) & (mul[i] <= kmer_len)) count++;
for (int32_t i=start; i<end; i++)
{
if (upos >= 0)
if ((mul[upos] > 0) & (mul[upos] <= kmer_len)) count -= 1;
if ((mul[i] > 0) & (mul[i] <= kmer_len)) count += 1;
if (count == 0) header = true;
else
{
if (header)
{
outline += "fixedStep chrom=";
outline += chrom;
outline += " start=";
outline += std::to_string(i + 1);
outline += " step=1\n";
header = false;
}
val = std::to_string(count / fkmer_len);
val = rtrim(val);
outline += val;
outline += "\n";
}
upos++;
}
if (outline.length() == 0) outline += "x";
wigout[index] = outline;
}
int main(int argc, char *argv[])
{
fprintf(stderr, "\r \r");
fflush(stderr);
if (argc <= 2 || help(argc, argv))
{
print_info();
return 1;
}
int32_t apos;
threadN = 1;
for(apos = 1; apos < argc; ++apos)
if(argv[apos][0] == '-')
{
if (strncmp(argv[apos], "-t", 2) == 0)
threadN = atoi(&argv[apos][2]);
} else
break;
if(argc - apos < 2)
{
print_info();
return 0;
}
kmer_len = stoi(argv[apos++]);
// Create thread pool
threadN = std::min(std::thread::hardware_concurrency(), \
static_cast<unsigned>(threadN));
ctpl::thread_pool pool(threadN);
// Begin loading wiggle and submitting jobs
wigfile.open(argv[apos]);
string chrom="";
int32_t chrom_end;
while (load_wiggle_chrom(chrom, chrom_end))
{
// Submit jobs
int32_t num_jobs = (chrom_end - 1) / 1000000 + 1;
wigout.clear();
for (int32_t j=0; j<num_jobs; j++)
{
wigout.push_back("");
pool.push(find_coverage, chrom, j, chrom_end);
}
// Wait for jobs to finish
time_t current_time, new_time;
time(¤t_time);
int32_t outpos=0;
current_time -= 1;
while (outpos < num_jobs)
{
if (wigout[outpos].length() > 0)
{
if (wigout[outpos] != "x")
{
fputs(wigout[outpos].c_str(), stdout);
fflush(stdout);
}
wigout[outpos] = "";
outpos ++;
}
time(&new_time);
if (new_time != current_time)
{
std::cerr << "\rScoring " << chrom << " job " << outpos
<< " of " << num_jobs << "\n";
current_time = new_time;
}
}
chrom = "";
std::cerr << "\r \r";
}
wigfile.close();
return EXIT_SUCCESS;
}