-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbio_file.cpp
More file actions
297 lines (270 loc) · 9.33 KB
/
Copy pathbio_file.cpp
File metadata and controls
297 lines (270 loc) · 9.33 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "bio_file.hpp"
#include <iostream>
#include <algorithm>
#include <sys/stat.h>
bio_file::bio_file(std::string filename, open_type type) {
m_type = type;
m_filename = filename;
switch(type) {
case read:
openFile(filename, m_input_stream);
break;
case write:
createFile(filename, m_output_stream);
break;
case none:
// do nothing bc its essentially unset
break;
}
}
bio_file::bio_file() {
m_type = none;
}
std::vector <std::string> bio_file::readBioLine(char delim) { // read one line and return an array of values
char tchar('\0');
std::vector <std::string> line(1, std::string());
if(m_type == write) {
std::cout << "Error : file opened in writing mode" << std::endl;
exit(1);
}
while(tchar != '\n' && remainToRead()) {
m_input_stream.get(tchar);
if(tchar != '\n') {
if(tchar != delim) {
line[line.size() - 1] += tchar;
} else {
line.push_back(std::string());
}
}
}
return line;
}
void bio_file::readWholeFile() {
std::cout << "Reading file " << m_filename << std::endl;
bool warned = false;
int i(0);
while(remainToRead()) {
try {
if(i % 100 == 0) {
std::cout << "Read " << i << " entries\r";
}
m_content.push_back(readLine());
m_indexes[m_content.at(i) -> getChr()].push_back(m_content.at(i).get()); // store a copy of the pointer for easy access
i ++;
} catch(const std::out_of_range& e) {
if(!warned) {
std::cout << "ignored line at position " << getLine() << std::endl;
std::cout << "invalid line: " << e.what() << std::endl;
warned = true;
} else {
std::cout << e.what() << std::endl;
std::cout << "error happened at line " << getLine() << std::endl;
std::cout << "more than one empty line: format is probably incorrect" << std::endl;
throw;
}
} catch(const std::invalid_argument) {
std::cout << "this code was recently changed and before skipped errors: however it also hid start > end errors from bed files" << std::endl;
throw;
}
}
}
void bio_file::writeBioLine(const bio_entry& entry) {
if(m_type != write) {
std::cout << "Error : file opened in reading mode" << std::endl;
exit(1);
}
m_output_stream << entry.getString() << "\n";
}
void bio_file::openFile(std::string filename, std::ifstream& stream) {
struct stat buffer;
if(stat(filename.c_str(), &buffer) != 0) {
std::cout << "file " << filename << " does not exist" << std::endl;
exit(1);
}
stream.open(filename);
} // open the file in reading mode
void bio_file::createFile(std::string filename, std::ofstream& stream) {
stream.open(filename);
} // open the file in writing mode
void bio_file::writeToFile() {
for(const auto& entry: m_content) {
writeBioLine(*entry);
}
}
bool bio_file::remainToRead() const {
return !m_input_stream.eof();
}
std::vector <intersect_results> bio_file::intersect(const bio_file& file, bool stranded, id_status status) {
std::vector <intersect_results> results;
for(const auto& pair: m_indexes) { // pair chr : pointers
try {
const std::vector <bio_entry*> vals = file.getEntriesByChr(pair.first);
int A(0), B(vals.size() - 1);
bool found(false);
int index2(0);
for(const auto& entry: pair.second) {
found = false;
A = 0;
B = vals.size() - 1;
while(A <= B && !found) {
index2 = int((A+B)/2);
intersect_results intersected_entry(entry -> intersect(vals[index2], stranded, status));
if(intersected_entry.result == bio_entry()) {
if((*entry) < *(vals[index2])) {
B = index2 - 1;
} else {
A = index2 + 1;
}
} else { // we got a hit, is it the only hit ?
results.push_back(intersected_entry);
for(int i(index2 - 1); i > 0; i --) {
intersect_results intersected_entry(entry -> intersect(vals[i], stranded, status));
if(intersected_entry.result == bio_entry()) {
break;
} else {
results.push_back(intersected_entry);
}
}
for(int i(index2 + 1); i < vals.size(); i ++) {
intersect_results intersected_entry(entry -> intersect(vals[i], stranded, status));
if(intersected_entry.result == bio_entry()) {
break;
} else {
results.push_back(intersected_entry);
}
}
found = true;
}
}
}
} catch(std::out_of_range) {
std::cout << "no entries for this chr, skipping" << std::endl;
}
}
return results;
}
void bio_file::apply_intersect(bio_file& file, bool stranded, id_status status) {
std::vector <intersect_results> results(intersect(file, stranded, status));
clear(); // ATTENTION ICI ON VIDE LE TABLEAU DONC SOURCE NE POINTE PLUS VERS RIEN
for(const auto& entry: results) {
appendEntry(std::make_unique <bio_entry> (entry.result));
}
}
bool true_sort(const bio_entry* A, const bio_entry* B) {
return *A < *B;
}
std::vector <bio_entry*> bio_file::getEntriesByChr(const std::string chr) const {
std::vector <bio_entry*> results;
try {
results = m_indexes.at(chr);
sort(results.begin(), results.end(), true_sort);
} catch(std::out_of_range) {
std::cout << "No entry matching " + chr << std::endl;
throw;
}
return results;
}
open_type bio_file::getType() const {
return m_type;
}
char bio_file::getChar() {
char returned('\0');
m_input_stream.get(returned);
return returned;
}
bio_entry* bio_file::getEntry(int index) const {
return m_content.at(index).get();
}
int bio_file::getSize() const {
return m_content.size();
}
int bio_file::getLine() {
if(m_type == read) {
int current_pos(m_input_stream.tellg()), number_lines(0);
if(m_input_stream.eof()) {
return -1;
} else if(m_input_stream.fail()) {
std::cout << "issues with the stream, investigate" << std::endl;
return -1;
} else {
char tchar('\0');
m_input_stream.seekg(0, std::ios::beg); // go back to the begining
while(m_input_stream.tellg() < current_pos) {
m_input_stream.get(tchar);
if(tchar == '\n') {
number_lines ++;
}
}
return number_lines;
}
} else {
throw std::invalid_argument("not implemented for file writing");
}
}
void bio_file::writeString(std::string value) {
if(m_type == write) {
m_output_stream << value;
} else {
throw std::domain_error("tried a write operation in a read-only file");
}
}
void bio_file::appendEntry(std::unique_ptr <bio_entry> entry) {
m_content.push_back(std::move(entry));
m_indexes[m_content.back() -> getChr()].push_back(m_content.back().get());
}
void bio_file::typeToWrite(const std::string filename) {
if(m_type == read) {
m_input_stream.close();
}
createFile(filename, m_output_stream);
m_type = write;
m_filename = filename;
}
void bio_file::typeToRead(const std::string filename) {
if(m_type == none) {
if(filename != "") {
m_filename = filename;
} else if(m_filename == "") {
throw std::invalid_argument("forgot to set value for filename, can't set it to inexistant file");
}
}
m_type = read;
openFile(m_filename, m_input_stream);
}
std::vector <std::string> bio_file::getChrs() const {
std::vector <std::string> results;
for(const auto& entry: m_indexes) {
results.push_back(entry.first);
}
return results;
}
char bio_file::peek() {
return m_input_stream.peek();
}
void bio_file::clear() {
m_content.clear();
m_indexes.clear();
}
std::vector <bio_entry*> bio_file::getEntries() const {
std::vector <bio_entry*> results;
for(const auto& entry: m_content) {
results.push_back(entry.get());
}
return results;
}
void bio_file::eraseAndLoadBlock(int amount) {
int amount_loaded(0);
clear();
while(remainToRead() && amount > amount_loaded) {
try {
appendEntry(readLine());
} catch(const std::out_of_range& e) {
std::cout << "ignored line at position " << getLine() << std::endl;
std::cout << "invalid line: " << e.what() << std::endl;
}
amount_loaded ++;
if(amount_loaded % 100 == 0) {
std::cout << "Loaded " << amount_loaded << " entries\r";
}
}
}