-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.cpp
240 lines (210 loc) · 5.44 KB
/
cluster.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
227
228
229
230
231
232
233
234
235
236
237
238
#include<cassert>
#include<cmath>
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<cstdarg>
#include<algorithm>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
using namespace std;
ostringstream oMsg;
string sbuf;
#include "defs.h"
#include "union.h"
/*long get_filesize(string filename) {
int file=0;
if((file=open(filename.c_str(),O_RDONLY)) < -1) {
cerr << "File not found: " << filename;
exit(1);
}
struct stat fileStat;
if(fstat(file,&fileStat) < 0) {
cerr << "Can't fstat file: " << filename;
exit(1);
}
return fileStat.st_size;
}
*/
long load_file_to_memory(const char *filename, char **result)
{
long size = get_filesize(filename);
FILE *f = fopen(filename, "rb");
if (f == NULL) {
*result = NULL;
cout << "Failed opening file: " << filename << endl;
exit(1);
}
*result = (char *)malloc(size+1);
if (size != fread(*result, sizeof(char), size, f)) {
cout << "Failed reading file: " << filename << endl;
free(*result);
exit(1);
}
fclose(f);
(*result)[size] = 0;
return size;
}
int tau;
int nthreads;
long numDistinctKmers;
string kmerFile;
string splitsBase;
int kmersize;
int kmerfileLinelen;
char * reads;
class entryClass {
public:
string key;
int id;
string seq;
string count;
string freq;
};
void get_full_kmer(entryClass & x) {
char s[kmersize+1];
long offset = x.id * long(kmerfileLinelen);
strncpy(s, &reads[offset], kmersize);
s[kmersize] = 0;
x.seq = s;
strncpy(s, &reads[offset + kmersize], 6);
s[6] = 0;
x.count = s;
strncpy(s, &reads[offset + kmersize + 6], 9);
s[9] = 0;
x.freq = s;
//cout << x.seq << ":" << x.count << ":" << x.freq << endl;
return;
}
void process_block(unionFindClass * uf, vector<entryClass> & block) {
for (int i = 0; i < block.size(); i++) {
get_full_kmer(block[i]);
}
for (int i = 0; i < block.size(); i++) {
uf->find_set(block[i].id);
for (int j = i + 1; j < block.size(); j++) {
if (hamdist(block[i].seq, block[j].seq, SAME_STRAND, tau ) <= tau) {
uf->unionn(block[i].id, block[j].id);
}
}
}
return;
}
typedef pair<int, unionFindClass *> paramType;
void * onethread(void * params) {
int thread = ((paramType *) params)->first;
unionFindClass * uf = new unionFindClass(numDistinctKmers);
((paramType *) params)->second = uf;
char threadLabel[5];
sprintf(threadLabel, "%02d", thread);
cerr << "Processing split files (" << thread << ")...\n";
ifstream inf;
open_file(inf, splitsBase + "." + threadLabel);
string sbuf;
entryClass last;
vector<entryClass> block;
int counter=0;
while (getline(inf, sbuf)) {
if (++counter % 1000000 == 0) cerr << "Processed (" << thread << ") " << add_commas(counter) << ", ";
istringstream line(sbuf);
entryClass cur;
line >> cur.key >> cur.id;
if (last.key == cur.key) { //add to current reads
block.push_back(cur);
} else {
process_block(uf, block);
block.clear();
block.push_back(cur);
}
last = cur;
}
process_block(uf, block);
inf.close();
cerr << "Finished(" << thread << ") " << endl;
pthread_exit(NULL);
}
void merge(paramType params[]) {
cerr << "Merging union find files...\n";
unionFindClass * ufMaster = new unionFindClass(numDistinctKmers);
vector<string> row;
vector<vector<int> > classes;
for (int i = 0; i < nthreads; i++) {
classes.clear();
unionFindClass * ufSlave = params[i].second;
ufSlave->get_classes(classes);
delete ufSlave;
for (int j = 0; j < classes.size(); j++) {
int first = classes[j][0];
ufMaster->find_set(first);
for (int k = 0; k < classes[j].size(); k++) {
ufMaster->unionn(first, classes[j][k]);
}
}
}
cerr << "Outputting clusters...\n";
classes.clear();
ufMaster->get_classes(classes);
delete ufMaster;
entryClass x;
ofstream outf;
open_file(outf, "reads.uf");
for (int i = 0; i < classes.size(); i++) {
for (int j = 0; j < classes[i].size(); j++) {
x.id = classes[i][j];
get_full_kmer(x);
outf << "ITEM\t" << i << "\t" << x.id << "\t" << x.seq << "\t" << x.count << "\t" << x.freq << endl;
}
outf << endl;
}
outf.close();
}
int main(int argc, char * argv[]) {
tau = atoi(argv[1]);
kmersize = atoi(argv[2]);
kmerFile = argv[3];
splitsBase = argv[4];
long memlim = atol(argv[5]);
string mergeOnly = argv[6];
kmerfileLinelen = kmersize + 16;
nthreads = atoi(argv[7]);
//int nthreads = tau + 1;
//this depends on the implementation of UnionFindClass
long l = get_filesize(kmerFile);
assert (l % kmerfileLinelen == 0);
numDistinctKmers = l / kmerfileLinelen;
//cerr << "numDistnica = " << numDistinctKmers << endl;
//load reads
cerr << "Reading in kmers...\n";
load_file_to_memory(kmerFile.c_str(), &reads);
if (mergeOnly == "1") {
cerr << argv[0] << ": mergeOnly mode not supported, exiting.\n";
exit(1);
}
long maxKmers = (memlim - l - 2*sizeof(int)) / (nthreads * 2*sizeof(int)); //accounting for union find
cerr << argv[0] << ": maxKmers = " << add_commas(maxKmers) << ", numDistinctKmers = " << add_commas(numDistinctKmers) << endl;
if (numDistinctKmers > maxKmers) {
cerr << "Won't fit into memory, don't even try .\n";
exit(1);
}
//start threads
pthread_t thread[nthreads];
paramType params[nthreads];
for (int i = 0; i < nthreads; i++) {
params[i].first = i;
pthread_create(&thread[i], NULL, onethread, (void *) ¶ms[i]);
}
for (int i = 0; i < nthreads; i++) {
pthread_join(thread[i], NULL);
}
merge(params);
delete reads;
return 0;
}