-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenSubseqSeedsGraph.cpp
280 lines (230 loc) · 6.72 KB
/
genSubseqSeedsGraph.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Given a set of reads, extract their FracSubseqHash seeds as vertices.
Two seeds a and b are connected by a directed edge if they are adjacent
seeds (in this order) on some read.
Each read is therefore represented by a path in the resulting graph.
After all reads are processed, remove nodes (seeds) that only appear
in one read.
Output the graph in dot format.
By: Ke@PSU
Last edited: 03/06/2023
*/
#include "util.h"
#include "SeedsGraph.hpp"
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <thread>
#include <mutex>
#include <queue>
#include <condition_variable>
#include <functional>
using namespace std;
#define NUMTHREADS 15
#define EXPECTEDVALUE ((1lu<<30)+(1lu<<29))
#define THRESHOLDFACTOR 0.785
//from brewer color scheme spectral11
const char* COLORS[] = {"#910142", "#5e4fa2", "#f46d43",
"#66c2a5", "#fee08b", "#e6f598", "#3288bd",
"#d53e4f", "#abdda4", "#fdae61", "#ffffbf"};
typedef SeedsGraph<kmer> Graph;
typedef Graph::Node Node;
string kmerToString(const kmer& x, int k, char* buf){
decode(x, k, buf);
return string(buf);
}
struct ReadPath{
size_t read_idx;
Node *head, *tail;
ReadPath(size_t read_idx):read_idx(read_idx),
head(nullptr), tail(nullptr){};
};
struct Read{
string seq;
size_t idx;
Read(string&& s, size_t i): seq(move(s)), idx(i) {};
Read(Read&& o): seq(move(o.seq)), idx(exchange(o.idx, 0)) {};
};
class SeedFactory{
const int n;
const int k;
const RandTableCell* table;
const double threshold;
Graph &graph;
queue<Read> jobs;
vector<thread> minions;
bool done;
mutex door;
condition_variable trumpet;
Node* storeSeedWithPosInGraph(const kmer seed, const size_t read_idx,
const size_t cur_pos, size_t* prev_pos,
Node* prev, Graph& g);
double getScoreFromDPTable(const int n, const int k, const DPCell* dp);
void getAndSaveSubseqSeeds(const Read &r);
void atWork(int x);
public:
SeedFactory(const int n, const int k, const RandTableCell* table,
const double threshold, Graph& g):
n(n), k(k), table(table), threshold(threshold),
graph(g), done(false){
minions.reserve(NUMTHREADS);
for(int i=0; i<NUMTHREADS; ++i){
minions.emplace_back(bind(&SeedFactory::atWork, this, i));
}
}
~SeedFactory(){
unique_lock<mutex> lock(door);
done = true;
lock.unlock();
trumpet.notify_all();
for(auto& x : minions){
x.join();
}
}
void addJob(string&& r, size_t idx){
unique_lock<mutex> lock(door);
jobs.emplace(move(r), idx);
trumpet.notify_one();
}
};
int main(int argc, const char * argv[])
{
if(argc != 5){
printf("usage: genSubseqGraph.out readFile n k randTableFile\n");
return 1;
}
int n = atoi(argv[2]);
int k = atoi(argv[3]);
double threshold = THRESHOLDFACTOR * EXPECTEDVALUE * k;
//load table
RandTableCell table[k*ALPHABETSIZE];
const char* table_filename = argv[4];
struct stat test_table;
if(stat(table_filename, &test_table) == 0){//file exists
loadRandTable(table_filename, k, table);
}else{
initRandTable(k, table);
saveRandTable(table_filename, k, table);
}
//input reads and process
ifstream fin(argv[1], ifstream::in);
Graph g;
//vector<ReadPath> paths;
SeedFactory factory(n, k, table, threshold, g);
string read;
size_t read_idx = 0;
while(fin.get() == '>'){
//fin >> read_idx;
//skip the header
fin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(fin, read);
++ read_idx;
factory.addJob(move(read), read_idx);
}
//only keep reads that appear on multiple distinct reads
g.removeUniqSeeds();
//output
char output_filename[200];
int output_len = strstr(argv[1], ".efa") - argv[1];
int tablename_st = strlen(table_filename) - 1;
for(; tablename_st>=0; --tablename_st){
if(table_filename[tablename_st] == '/'){
break;
}
}
++tablename_st;
output_len = sprintf(output_filename, "%.*s-%s-t%f.graph",
output_len, argv[1],
table_filename+tablename_st,
THRESHOLDFACTOR);
g.saveGraph(output_filename);
return 0;
}
/*** implementation of SeedFactory functions ***/
inline Node* SeedFactory::storeSeedWithPosInGraph(
kmer seed, const size_t read_idx, const size_t cur_pos,
size_t* prev_pos, Node* prev, Graph& g){
//avoid self loops
if(prev && prev->seed == seed) return prev;
Node* cur = g.addNode(seed);
if(prev){
prev->addNext(read_idx, *prev_pos, cur);
cur->addPrev(read_idx, cur_pos, prev);
}
*prev_pos = cur_pos;
return cur;
}
inline double SeedFactory::getScoreFromDPTable(const int n, const int k,
const DPCell* dp){
int q = access2d(k+1, n, k);
double score = fabs(dp[q].min);
if(score < dp[q].max) return dp[q].max;
else return score;
}
void SeedFactory::atWork(int x){
unique_lock<mutex> lock(door, defer_lock);
while(true){
lock.lock();
while(!done && jobs.empty()){
trumpet.wait(lock);
}
if(!jobs.empty()){
Read r = move(jobs.front());
jobs.pop();
lock.unlock();
getAndSaveSubseqSeeds(r);
}else{
return;
}
}
}
void SeedFactory::getAndSaveSubseqSeeds(const Read &r){
int len = r.seq.length();
int i;
char cur[n+1];
kmer seed;
double score;
Node *prev = nullptr;
size_t prev_pos;
//calculate an extra column, can skip next position if score at
//[n+1][k] does not reach threshold; otherwise does not need recalculation
//if backtrack from [n+1][k] does not use first char
DPCell dp[(n+2)*(k+1)];
for(i=0; i<len-n; i+=1){
r.seq.copy(cur, n+1, i);
fillDPTable(cur, n+1, k, table, dp);
//printf("called at %d\n", i);
//get seed from pos i
score = getScoreFromDPTable(n, k, dp);
if(score >= threshold){
backtrackDPTable(cur, n, k, dp, &seed);
//add to graph
prev = storeSeedWithPosInGraph(seed, r.idx, i,
&prev_pos, prev, graph);
}
score = getScoreFromDPTable(n+1, k, dp);
if(score >= threshold){
if(!backtrackDPTable(cur, n+1, k, dp, &seed)){//first char not used
++i; //skip recalculation of next position
//add to graph
prev = storeSeedWithPosInGraph(seed, r.idx, i,
&prev_pos, prev, graph);
}
}else{//score less than threshold even with extra column, actual score can only be lower
++i;
}
}
//handle last seed, either it's never calculated or the previous
//iteration did not work (i.e., score >= threshold but used 1st char)
if(i == len - n){
r.seq.copy(cur, n, i);
fillDPTable(cur, n, k, table, dp);
//printf("called at %d\n", i);
score = getScoreFromDPTable(n, k, dp);
if(score >= threshold){
backtrackDPTable(cur, n, k, dp, &seed);
prev = storeSeedWithPosInGraph(seed, r.idx, i,
&prev_pos, prev, graph);
}
}
}