-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilter.cpp
executable file
·221 lines (179 loc) · 7.45 KB
/
filter.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
//
// Created by Nolan Woods on 11/6/2018.
//
#include <thread>
#include <ctime>
#include <seqan/sequence.h>
#include <seqan/seq_io.h>
#include <seqan/basic.h>
#include "BBHashKmerContainer.h"
#include "cmdline.h"
#include "thread_util.h"
#include "FastaRecord.h"
#include "SharedQueue.h"
using Record = FastaRecord;
using Queue = SharedQueue<Record>;
using KmerContainer = BBHashKmerContainer<KmerIterator<seqan::Dna5>, seqan::Dna5>;
const double update_interval = 1; //Seconds
void print_filter_status(unsigned long pending_records, unsigned long output_records, unsigned long total_records,
unsigned long total_output) {
if (total_records)
//Delete previous output
std::cerr << "\033[F\033[K";
std::cerr << "Pending filter: " << std::setw(10) << pending_records << " Pending output: " << std::setw(10) << output_records
<< " Total processed: " << std::setw(10) << total_records
<< " Total output: " << std::setw(10) << total_output << std::endl;
}
//Thread functions
void filter_thread(Queue &pending_records, Queue &output_records, KmerContainer &table, const ParametersFilter ¶ms) {
try {
do {
auto item = pending_records.pop();
seqan::Dna5 *seq_c = toCString(item.seq);
unsigned int match_count{0};
for (unsigned i = 0; i < length(item.seq) - params.kmer_length; i++) {
if (table.contains(seq_c + i) and ++match_count > params.min_kmer_threshhold) goto SKIP_OUTPUT;
}
output_records.push(std::move(item));
SKIP_OUTPUT:
continue;
} while (true);
} catch (Stop &e) {
}
}
void output_thread(Queue &queue, const ParametersFilter ¶ms, unsigned long &total_output) {
seqan::SeqFileOut seqFileOut;
if (params.output_filename == "-")
seqan::open(seqFileOut, std::cout);
else
seqan::open(seqFileOut, params.output_filename.c_str());
try {
do {
auto item = queue.pop();
seqan::writeRecord(seqFileOut, item.id, item.seq); //TODO: add call for fastq sequences
++total_output;
} while (true);
} catch (Stop &e) {
}
}
int filter(ParametersFilter ¶ms) {
//Parse and validate parameters
std::cerr << "Filtering sequence......" << std::endl;
//TODO replace with input parameters
unsigned int queue_limit = 10; //Default soft limit for queue before thread pool increase, TODO replace with memory limit
unsigned long total_records = 0, total_output = 0;
KmerContainer table(params.threads, 2, 100, params.kmer_length); //TODO: properly instantiate and load from table file
//Init thread pool
Queue pending_records, output_records;
std::vector<std::thread> thread_pool;
auto t = thread_pool.emplace(thread_pool.end(), filter_thread, std::ref(pending_records), std::ref(output_records), std::ref(table), std::ref(params));
increment_priority(*t, -1); //Lower priority of filter workers so not to interfere with IO
std::thread output_thread_instance(output_thread, std::ref(output_records), std::ref(params), std::ref(total_output));
//Read in records to queue
seqan::CharString id;
seqan::Dna5QString seq;
seqan::CharString qual;
//Call sequence stream function of seqan to read from the file
seqan::SeqFileIn seqFileIn;
if (params.seq_filename == "-") {
seqan::open(seqFileIn, std::cin);
} else {
seqan::open(seqFileIn, params.seq_filename.c_str());
}
if (params.verbose) print_filter_status(pending_records.size(false), output_records.size(false), total_records, total_output);
std::time_t last_time, now;
//Push record into queue
while (!atEnd(seqFileIn)) { // TODO: readRecord(id, seq, qual, seqStream) for fastq files
try {
// if(norc in args) TODO
reverseComplement(seq);
readRecord(id, seq, qual, seqFileIn);
// else
// readRecord(id, seq, qual, seqFileIn);
} catch (std::exception const &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
//construct a fasta/fastq object
FastaRecord fa(id, seq);
//push to the pending queue
pending_records.push(std::move(fa));
//Check queue size and increase thread pool to desaturate
if (pending_records.size() > queue_limit) {
if (thread_pool.size() < params.threads)
//Increase thread pool by 1
t = thread_pool.emplace(thread_pool.end(), filter_thread, std::ref(pending_records),
std::ref(output_records), std::ref(table), std::ref(params));
increment_priority(*t, -1); //Lower priority of filter workers so not to interfere with IO
//Wait for pending records to desaturate (Non-blocking size check)
while (pending_records.size(false) > queue_limit);
}
now = std::time(nullptr);
if (params.verbose and std::difftime(last_time, now) > update_interval) {
last_time = now;
print_filter_status(pending_records.size(false), output_records.size(false), total_records, total_output);
}
++total_records;
}
//Join thread pool
//Signal threads to exit
pending_records.signal_done();
for (auto &thread : thread_pool) thread.join();
output_records.signal_done();
output_thread_instance.join();
//Output final statistics
if (params.verbose) print_filter_status(pending_records.size(false), output_records.size(false), total_records, total_output);
return 0;
}
/*
int reuse_filter_singlecore(ParametersFilter ¶ms){
//Parse and validate parameters
std::cerr << "Filtering sequence......"<< std::endl;
std::cerr << "Single Core Run" << std::endl;
std::cerr <<"paired? " << (params.seq_mate_filename.length()?"true":"false")<< std::endl;
//TODO replace with input parameters
BBHashKmerContainer<KmerIterator<seqan::Dna5>,seqan::Dna5> table(1,2,100,21); //TODO: properly instantiate and load from table file
table.load(params.index_filename.c_str());
//Read in records to queue
seqan::CharString id;
seqan::Dna5String seq;
seqan::CharString qual;
//Call sequence stream function of seqan to read from the file
seqan::SeqFileIn seqFileIn;
if (params.seq_filename == "-")
seqan::open(seqFileIn, std::cin);
else
seqan::open(seqFileIn, params.seq_filename.c_str());
seqan::SeqFileOut seqFileOut;
if (params.output_filename == "-"){
seqan::open(seqFileOut, std::cout);
}
else{
seqan::open(seqFileOut, params.output_filename.c_str());
}
//Push record into queue
while (!atEnd(seqFileIn)) { // TODO: readRecord(id, seq, qual, seqStream) for fastq files
try {
// if(norc in args) TODO
readRecord(id, seq, qual, seqFileIn);
// else
// readRecord(id, seq, qual, seqFileIn);
} catch (std::exception const & e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
bool flag = false;
seqan::Dna5 *seq_c = toCString(seq);
for(unsigned i = 0; i < length(seq)-params.kmer_length;i++){
if(table.contains(seq_c+i)){
flag = true;
break;
}
}
if(!flag){
writeRecord(seqFileOut,id,seq,qual);
}
}
return 0;
}
*/