-
Notifications
You must be signed in to change notification settings - Fork 2
/
Examples.cpp
219 lines (199 loc) · 7.5 KB
/
Examples.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
#include "io/FastxStream.h"
#include "io/FastxChunk.h"
#include <string>
#include <iostream>
#include "thirdparty/CLI11.hpp"
#include "io/DataQueue.h"
#include <thread>
#include "io/Formater.h"
#include <sys/time.h>
double get_time(){
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
}
typedef rabbit::core::TDataQueue<rabbit::fq::FastqDataPairChunk> FqChunkQueue;
typedef int pfunc(int, char**);
int count_line(rabbit::fq::FastqChunk *fqchunk) { return 1000; }
int producer_pe_fastq_task(std::string file, std::string file2, rabbit::fq::FastqDataPool &fastqPool, FqChunkQueue &dq) {
rabbit::fq::FastqFileReader *fqFileReader;
fqFileReader = new rabbit::fq::FastqFileReader(file, fastqPool, file2, false);
int n_chunks = 0;
int line_sum = 0;
while (true) {
rabbit::fq::FastqDataPairChunk *fqdatachunk = new rabbit::fq::FastqDataPairChunk;
fqdatachunk = fqFileReader->readNextPairChunk();
if (fqdatachunk == NULL) break;
n_chunks++;
//std::cout << "readed chunk: " << n_chunks << std::endl;
dq.Push(n_chunks, fqdatachunk);
}
dq.SetCompleted();
delete fqFileReader;
std::cout << "file " << file << " has " << n_chunks << " chunks" << std::endl;
return 0;
}
void consumer_pe_fastq_task(rabbit::fq::FastqDataPool &fastqPool, FqChunkQueue &dq) {
long line_sum = 0;
rabbit::int64 id = 0;
std::vector<neoReference> data;
rabbit::fq::FastqDataPairChunk *fqdatachunk = new rabbit::fq::FastqDataPairChunk;
data.resize(10000);
while (dq.Pop(id, fqdatachunk)) {
line_sum += rabbit::fq::chunkFormat((rabbit::fq::FastqDataChunk*)(fqdatachunk->left_part), data, true);
line_sum += rabbit::fq::chunkFormat((rabbit::fq::FastqDataChunk*)(fqdatachunk->right_part), data, true);
fastqPool.Release(fqdatachunk->left_part);
fastqPool.Release(fqdatachunk->right_part);
}
}
int producer_fastq_task(std::string file, rabbit::fq::FastqDataPool& fastqPool, rabbit::core::TDataQueue<rabbit::fq::FastqDataChunk> &dq){
rabbit::fq::FastqFileReader fqFileReader(file, fastqPool);
rabbit::int64 n_chunks = 0;
while(true){
rabbit::fq::FastqDataChunk* fqdatachunk;// = new rabbit::fq::FastqDataChunk;
fqdatachunk = fqFileReader.readNextChunk();
if (fqdatachunk == NULL) break;
n_chunks++;
//std::cout << "readed chunk: " << n_chunks << std::endl;
dq.Push(n_chunks, fqdatachunk);
}
dq.SetCompleted();
std::cout << "file " << file << " has " << n_chunks << " chunks" << std::endl;
return 0;
}
void consumer_fastq_task(rabbit::fq::FastqDataPool& fastqPool, rabbit::core::TDataQueue<rabbit::fq::FastqDataChunk> &dq){
long line_sum = 0;
rabbit::int64 id = 0;
std::vector<neoReference> data;
rabbit::fq::FastqDataChunk* fqdatachunk;// = new rabbit::fq::FastqDataChunk;
data.resize(10000);
while(dq.Pop(id, fqdatachunk)){
line_sum += rabbit::fq::chunkFormat(fqdatachunk, data, true);
fastqPool.Release(fqdatachunk);
}
std::cout << "line_sum: " << line_sum << std::endl;
}
void print_chunk(rabbit::fa::FastaDataChunk *chunk) {
std::cout << "chunk size: " << chunk->size << std::endl;
std::cout << "chunk head: " << std::string((char *)chunk->data.Pointer(), 100) << std::endl;
}
void print_fachunkpart_info(rabbit::fa::FastaChunk *fachunk) {
std::cout << "------------------chunk info-----------------" << std::endl;
print_chunk(fachunk->chunk);
rabbit::fa::FastaDataChunk *current_chunk = fachunk->chunk;
while (current_chunk->next != NULL) {
std::cout << "next" << std::endl;
current_chunk = current_chunk->next;
print_chunk(current_chunk);
}
}
int producer_fasta_task(std::string file) {
rabbit::fa::FastaDataPool fastaPool(256, 1 << 22);
rabbit::fa::FastaFileReader faFileReader(file, fastaPool, false);
int n_chunks = 0;
int line_sum = 0;
while (true) {
rabbit::fa::FastaChunk *fachunk = new rabbit::fa::FastaChunk;
fachunk = faFileReader.readNextChunkList();
//fachunk = faFileReader->readNextChunk();
if (fachunk == NULL) break;
n_chunks++;
//std::cout << n_chunks << std::endl;
// line_sum += count_line(fqchunk);
//std::vector<Reference> data;
//print_fachunkpart_info(fachunk);
//-----relaease
rabbit::fa::FastaDataChunk *tmp = fachunk->chunk;
do {
fastaPool.Release(tmp);
tmp = tmp->next;
} while (tmp != NULL);
// line_sum += rabbit::fa::chunkFormat(*fachunk, data);
}
std::cout << "file " << file << " has " << line_sum << " lines" << std::endl;
return 0;
}
int test_fastq_pe(int argc, char **argv) {
std::string file1 = "/home/old_home/haoz/ncbi/public/sra/fastv_test/SRX7918726.sra_1.fastq";
std::string file2 = "/home/old_home/haoz/ncbi/public/sra/fastv_test/SRX7918726.sra_2.fastq";
int th = 20; // thread number
rabbit::fq::FastqDataPool fastqPool(256, 1 << 22);
FqChunkQueue queue1(128, 1);
std::thread producer(producer_pe_fastq_task, file1, file2, std::ref(fastqPool), std::ref(queue1));
std::thread **threads = new std::thread *[th];
for (int t = 0; t < th; t++) {
threads[t] = new std::thread(std::bind(consumer_pe_fastq_task, std::ref(fastqPool), std::ref(queue1)));
}
producer.join();
for (int t = 0; t < th; t++) {
threads[t]->join();
}
//delete fastqPool;
for (int t = 0; t < th; t++) {
delete threads[t];
}
return 0;
}
// test SE
int test_fastq_se(int argc, char** argv){
std::string file = "/home/old_home/haoz/workspace/QC/fastp_dsrc/out_1.fq";
//std::string file = "/home/old_home/haoz/workspace/data/hg19/hg19.fa";
//---------------cmd parser----------------
CLI::App app{"Wellcome to RabbitIO"};
CLI::Option* opt;
std::string filename ;
int th;
app.add_option("-f, --file", filename, "input file name")
->default_val(file);
app.add_option("-t, --threads", th, "worktreads")
->default_val(2);
//----------------------------------------
CLI11_PARSE(app, argc, argv);
if(app.count("-f"))
std::cout << "filename: " << filename << std::endl;
else{
std::cout << "-f not find, use default: " << filename << std::endl;
}
rabbit::fq::FastqDataPool fastqPool(32, 1<<22);
rabbit::core::TDataQueue<rabbit::fq::FastqDataChunk> queue1(64, 1);
std::thread producer(producer_fastq_task, filename, std::ref(fastqPool), std::ref(queue1));
vector<std::thread> threads;
for(int t = 0; t < th; t++){
threads.emplace_back(thread(consumer_fastq_task, std::ref(fastqPool), std::ref(queue1)));
}
producer.join();
for(int t = 0; t < th; t++){
threads[t].join();
}
return 0;
}
int test_fasta(int argc, char** argv){
producer_fasta_task("/home/old_home/haoz/workspace/data/hg19/hg19.fa");
return 0;
}
inline void check_sucess(pfunc func, int argc, char** argv, std::string desc){
std::cout << "runing " << desc << std::endl;
double start_time = get_time();
if(func(argc, argv) == 0){
std::cout << "["<< desc << "] runing sucess!";
}else{
std::cout << "["<< desc << "] not sucess!";
}
double end_time = get_time();
std::cout << " time: " << end_time - start_time << " s" << std::endl;
}
int main(int argc, char** argv){
check_sucess(test_fastq_pe, argc, argv, "test fastq pair end");
check_sucess(test_fastq_se, argc, argv, "test fastq single end");
check_sucess(test_fasta, argc, argv, "test fasta process");
}
/*
FastqReader processer( );
io::data::chunk<FastqChunk> fqchunk = processer.get_chunk(file);
//or
io::data::seq<FastqSeq> fqseqs = processer.get_formated_seq(file);
processer.process_seq(worker_num, func, param);
thread_pool.process(producer_task, file);
thread_pool.process(consumer_task, file);
return 0;
}*/