forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence_manager.h
142 lines (122 loc) · 4.26 KB
/
sequence_manager.h
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
/*
* MEGAHIT
* Copyright (C) 2014 - 2015 The University of Hong Kong
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* contact: Dinghua Li <[email protected]> */
#ifndef SEQUENCE_MANAGER_H__
#define SEQUENCE_MANAGER_H__
#include <string>
#include <vector>
#include <zlib.h>
#include "definitions.h"
#include "kseq.h"
#include "sequence_package.h"
#include "edge_io.h"
#ifndef KSEQ_INITED
#define KSEQ_INITED
KSEQ_INIT(gzFile, gzread)
#endif
/**
* @brief manage the reading of a set of fastx reads/binary reads/contigs/edges
* @details providing functions like reading contigs/single-end reads/paired reads/interleaved reads from fastx/binary format
* and writing sequences into binary formats
*/
struct SequenceManager {
enum FileType {
kFastxReads,
kBinaryReads,
kMegahitContigs,
kMegahitEdges,
kSortedEdges,
} f_type;
enum ReadLibType {
kPaired,
kInterleaved,
kSingle,
kOthers,
} r_type;
SequencePackage *package_;
std::vector<multi_t> *multi_; // edges or contigs' multiplicity
std::vector<float> *float_multi_;
std::vector<gzFile> files_; // for reading sorted edges
std::vector<kseq_t *> kseq_readers_; // for paired reading
EdgeReader edge_reader_;
bool edge_reader_inited_;
std::vector<uint32_t> buf_; // for reading binary reads
int min_len_;
int k_from_, k_to_;
SequenceManager(SequencePackage *package = NULL): package_(package) {
multi_ = NULL;
float_multi_ = NULL;
files_.clear();
kseq_readers_.clear();
edge_reader_.destroy();
edge_reader_inited_ = false;
min_len_ = 0;
k_from_ = 1;
k_to_ = 1;
};
~SequenceManager() {
clear();
}
void clear() {
for (auto iter = kseq_readers_.begin(); iter != kseq_readers_.end(); ++iter) {
kseq_destroy(*iter);
}
for (auto iter = files_.begin(); iter != files_.end(); ++iter) {
gzclose(*iter);
}
files_.clear();
kseq_readers_.clear();
if (edge_reader_inited_) {
edge_reader_.destroy();
edge_reader_inited_ = false;
}
}
void set_file_type(FileType f_type) {
this->f_type = f_type;
}
void set_readlib_type(ReadLibType r_type) {
this->r_type = r_type;
}
void set_package(SequencePackage *package) {
package_ = package;
}
void set_multiplicity_vector(std::vector<multi_t> *v) {
multi_ = v;
}
void set_float_multiplicity_vector(std::vector<float> *v) {
float_multi_ = v;
}
void set_file(const std::string &file_name);
void set_pe_files(const std::string &file_name1, const std::string &file_name2);
void set_edge_files(const std::string &file_prefix);
void set_kmer_size(int kmer_from, int kmer_to) {
k_from_ = kmer_from; // only apply to reading megahit contigs
k_to_ = kmer_to;
}
void set_min_len(int min_len) {
min_len_ = min_len; // only apply to megahit contigs
}
int64_t ReadShortReads(int64_t max_num, int64_t max_num_bases, bool append, bool reverse, bool trimN = false, std::string file_name = "");
int64_t ReadEdges(int64_t max_num, bool append);
int64_t ReadEdgesWithFixedLen(int64_t max_num, bool append);
int64_t ReadShortedEdges(int64_t max_num, int kmer_size, bool append);
int64_t ReadMegahitContigs(int64_t max_num, int64_t max_num_bases, bool append, bool reverse,
int discard_flag, bool extend_loop, bool calc_depth);
void WriteBinarySequences(FILE *file, bool reverse, int64_t from = 0, int64_t to = -1);
};
#endif