-
Notifications
You must be signed in to change notification settings - Fork 137
/
mer_file.i
191 lines (171 loc) · 5.84 KB
/
mer_file.i
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
/******************************/
/* Query output of jellyfish. */
/******************************/
%{
class QueryMerFile {
std::unique_ptr<jellyfish::mer_dna_bloom_filter> bf;
jellyfish::mapped_file binary_map;
std::unique_ptr<binary_query> jf;
public:
QueryMerFile(const char* path) {
std::ifstream in(path);
if(!in.good())
throw std::runtime_error(std::string("Can't open file '") + path + "'");
jellyfish::file_header header(in);
jellyfish::mer_dna::k(header.key_len() / 2);
if(header.format() == "bloomcounter") {
jellyfish::hash_pair<jellyfish::mer_dna> fns(header.matrix(1), header.matrix(2));
bf.reset(new jellyfish::mer_dna_bloom_filter(header.size(), header.nb_hashes(), in, fns));
if(!in.good())
throw std::runtime_error("Bloom filter file is truncated");
} else if(header.format() == "binary/sorted") {
binary_map.map(path);
jf.reset(new binary_query(binary_map.base() + header.offset(), header.key_len(), header.counter_len(), header.matrix(),
header.size() - 1, binary_map.length() - header.offset()));
} else {
throw std::runtime_error(std::string("Unsupported format '") + header.format() + "'");
}
}
#ifdef SWIGPERL
unsigned int get(const MerDNA& m) { return jf ? jf->check(m) : bf->check(m); }
#else
unsigned int __getitem__(const MerDNA& m) { return jf ? jf->check(m) : bf->check(m); }
#endif
};
%}
%feature("autodoc", "Give random access to a Jellyfish database. Given a mer, it returns the count associated with that mer");
class QueryMerFile {
public:
%feature("autodoc", "Open the jellyfish database");
QueryMerFile(const char* path) throw(std::runtime_error);
%feature("autodoc", "Get the count for the mer m");
#ifdef SWIGPERL
unsigned int get(const MerDNA& m);
#else
unsigned int __getitem__(const MerDNA& m);
#endif
};
/****************************/
/* Read output of jellyfish */
/****************************/
#ifdef SWIGRUBY
%mixin ReadMerFile "Enumerable";
#endif
#ifdef SWIGPYTHON
// For python iteratable, throw StopIteration if at end of iterator
%exception __next__ {
$action;
if(!result.first) {
PyErr_SetString(PyExc_StopIteration, "Done");
SWIG_fail;
}
}
%exception next {
$action;
if(!result.first) {
PyErr_SetString(PyExc_StopIteration, "Done");
SWIG_fail;
}
}
%typemap(out) std::pair<const MerDNA*, uint64_t> {
SWIG_Object m = SWIG_NewPointerObj(const_cast<MerDNA*>(($1).first), SWIGTYPE_p_MerDNA, 0);
SWIG_Object c = SWIG_From(unsigned long)(($1).second);
%append_output(m);
%append_output(c);
}
#endif
#ifdef SWIGPERL
// For perl, return an empty array at end of iterator
%typemap(out) std::pair<const MerDNA*, uint64_t> {
if(($1).first) {
SWIG_Object m = SWIG_NewPointerObj(const_cast<MerDNA*>(($1).first), SWIGTYPE_p_MerDNA, 0);
SWIG_Object c = SWIG_From(unsigned long)(($1).second);
%append_output(m);
%append_output(c);
}
}
#endif
%{
class ReadMerFile {
std::ifstream in;
std::unique_ptr<binary_reader> binary;
std::unique_ptr<text_reader> text;
std::pair<const MerDNA*, uint64_t> next_mer__() {
std::pair<const MerDNA*, uint64_t> res((const MerDNA*)0, 0);
if(next_mer()) {
res.first = mer();
res.second = count();
}
return res;
}
public:
ReadMerFile(const char* path) :
in(path)
{
if(!in.good())
throw std::runtime_error(std::string("Can't open file '") + path + "'");
jellyfish::file_header header(in);
jellyfish::mer_dna::k(header.key_len() / 2);
if(header.format() == binary_dumper::format)
binary.reset(new binary_reader(in, &header));
else if(header.format() == text_dumper::format)
text.reset(new text_reader(in, &header));
else
throw std::runtime_error(std::string("Unsupported format '") + header.format() + "'");
}
bool next_mer() {
if(binary) {
if(binary->next()) return true;
binary.reset();
} else if(text) {
if(text->next()) return true;
text.reset();
}
return false;
}
const MerDNA* mer() const { return static_cast<const MerDNA*>(binary ? &binary->key() : &text->key()); }
unsigned long count() const { return binary ? binary->val() : text->val(); }
#ifdef SWIGRUBY
void each() {
if(!rb_block_given_p()) return;
while(next_mer()) {
auto m = SWIG_NewPointerObj(const_cast<MerDNA*>(mer()), SWIGTYPE_p_MerDNA, 0);
auto c = SWIG_From_unsigned_SS_long(count());
rb_yield(rb_ary_new3(2, m, c));
}
}
#endif
#ifdef SWIGPERL
std::pair<const MerDNA*, uint64_t> each() { return next_mer__(); }
#endif
#ifdef SWIGPYTHON
ReadMerFile* __iter__() { return this; }
std::pair<const MerDNA*, uint64_t> __next__() { return next_mer__(); }
std::pair<const MerDNA*, uint64_t> next() { return next_mer__(); }
#endif
};
%}
%feature("autodoc", "Read a Jellyfish database sequentially");
class ReadMerFile {
public:
%feature("autodoc", "Open the jellyfish database");
ReadMerFile(const char* path) throw(std::runtime_error);
%feature("autodoc", "Move to the next mer in the file. Returns false if no mers left, true otherwise");
bool next_mer();
%feature("autodoc", "Returns current mer");
const MerDNA* mer() const;
%feature("autodoc", "Returns the count of the current mer");
unsigned long count() const;
%feature("autodoc", "Iterate through all the mers in the file, passing two values: a mer and its count");
#ifdef SWIGRUBY
void each();
#endif
#ifdef SWIGPERL
std::pair<const MerDNA*, uint64_t> each();
#endif
#ifdef SWIGPYTHON
ReadMerFile* __iter__();
std::pair<const MerDNA*, uint64_t> __next__();
std::pair<const MerDNA*, uint64_t> next() { return __next__(); }
#endif
};