-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcpu_utils.hpp
executable file
·625 lines (551 loc) · 25.1 KB
/
cpu_utils.hpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#ifndef __dba_cpu_utils_included
#define __dba_cpu_utils_included
#include "cuda_utils.hpp"
#include "exit_codes.hpp"
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cerrno>
#if SLOW5_SUPPORTED == 1
#include "submodules/slow5lib/include/slow5/slow5.h"
#endif
#if HDF5_SUPPORTED == 1
extern "C"{
#include "hdf5.h"
}
#endif
#define CONCAT2(a,b) (std::string(a)+std::string(b))
#define CONCAT3(a,b,c) (std::string(a)+std::string(b)+std::string(c))
#define CONCAT4(a,b,c,d) (std::string(a)+std::string(b)+std::string(c)+std::string((d)))
unsigned int FileRead( std::istream & is, std::vector <char> & buff ) {
is.read( &buff[0], buff.size() );
return is.gcount();
}
unsigned int CountLines( const std::vector <char> & buff, int sz ) {
int newlines = 0;
const char * p = &buff[0];
for ( int i = 0; i < sz; i++ ) {
if ( p[i] == '\n' ) {
newlines++;
}
}
return newlines;
}
// Function that converts a short to the given template value
// data - the short buffer to be converted
// data_length - the length of the buffer passed in
// returns a new buffer that is of type template with the short data stored in it
template <class T>
short* templateToShort(T* data, size_t data_length){
short* return_data;
return_data = (short*)malloc(sizeof(short)*data_length);
std::transform(data, data + data_length, return_data, [](T s){ return (short)s; });
return return_data;
}
template <typename T>
int
read_binary_data(const char *binary_file_name, T **output_vals, size_t *num_output_vals, bool is_short=false){
// See how big the file is, so we can allocate the appropriate buffer
std::ifstream ifs(binary_file_name, std::ios::binary);
std::streampos n;
if(ifs){
ifs.seekg(0, ifs.end);
n = ifs.tellg();
*num_output_vals = n/(is_short?2:sizeof(T)); // special case where shorts will be converted to floats for Z-normalized computation
}
else{
return 1;
}
T *out = 0;
cudaMallocManaged(&out, sizeof(T)*(*num_output_vals)); CUERR("Cannot allocate CPU memory for reading sequence from file");
ifs.seekg(0, std::ios::beg);
ifs.read((char *) out, n);
if(is_short){ // in place expansion from short -> float
for(std::streamoff i = (std::streamoff) n/sizeof(short); i >= 0; i--){
out[i] = (float) *(((short*) out)+i);
}
}
// Only set the output if all the data was succesfully read in.
*output_vals = out;
return 0;
}
template <typename T>
int
read_text_data(const char *text_file_name, T **output_vals, size_t *num_output_vals){
// Count the number of lines in the file (buffering 1MB on read for speed) so we know how much space to allocate for output_vals
// std::ios::sync_with_stdio(false); //optimization
const int SZ = 1024 * 1024;
std::vector <char> read_buffer( SZ );
std::ifstream ifs(text_file_name);
if(!ifs){
std::cerr << "Error reading in file " << text_file_name << ", skipping" << std::endl;
return 1;
}
int n = 0;
while(int sz = FileRead(ifs, read_buffer)) {
n += CountLines(read_buffer, sz);
}
*num_output_vals = n;
if(n == 0){
std::cerr << "File " << text_file_name << " is empty or not properly formatted. Skipping." << std::endl;
ifs.close();
return 1;
}
cudaMallocManaged(output_vals, sizeof(T)*n); CUERR("Cannot allocate CPU memory for reading sequence from text file");
// Read the actual values
ifs.clear(); // get rid of EOF error state
ifs.seekg(0, std::ios::beg);
std::stringstream in; // Make a stream for the line itself
std::string line;
int i = 0;
while(n--){ // Read line by line
std::getline(ifs, line); in.str(line);
in >> (*output_vals)[i++]; // Read the first whitespace-separated token
in.clear(); // to reuse the stringatream parser
}
// Only set the output if all the data was succesfully read in.
// *output_vals = out;
ifs.close();
return 0;
}
#if SLOW5_SUPPORTED == 1
int
scan_slow5_data(const char *slow5_file_name, size_t *num_sequences){
slow5_file_t *sp = slow5_open(slow5_file_name,"r");
if(sp==NULL){
std::cerr << "Could not open SLOW5 file " << slow5_file_name << std::endl;
return SLOW5_FILE_UNREADABLE;
}
int ret=0;
ret = slow5_idx_load(sp);
if(ret<0){
std::cerr << "Could not open SLOW5 file index for " << slow5_file_name << std::endl;
return SLOW5_FILE_UNREADABLE;
}
uint64_t num_reads = 0;
char **read_ids = slow5_get_rids(sp, &num_reads);
if(read_ids==NULL){
std::cerr << "Could not get number of reads from SLOW5 file " << slow5_file_name << std::endl;
return SLOW5_FILE_UNREADABLE;
}
slow5_idx_unload(sp);
slow5_close(sp);
*num_sequences = num_reads;
return 0;
}
#endif
#if HDF5_SUPPORTED == 1
int
scan_fast5_data(const char *fast5_file_name, size_t *num_sequences){
hsize_t num_read_objects;
/* Open an existing file. */
hid_t file_id = H5Fopen(fast5_file_name, H5F_ACC_RDONLY, H5P_DEFAULT);
if(file_id < 0){
std::cerr << "Could not open HDF5 file " << fast5_file_name << std::endl;
return FAST5_FILE_UNREADABLE;
}
H5Eset_auto1(NULL, NULL);
// Old format, one read per file
hid_t read_group = H5Gopen(file_id, "/Raw/Reads", H5P_DEFAULT);
if(read_group < 0){ // New formst, multiple reads per file
read_group = H5Gopen(file_id, "/", H5P_DEFAULT);
}
if(read_group < 0 || H5Gget_num_objs(read_group, &num_read_objects)){
std::cerr << "Could not get read groups from FAST5 (HDF5) file " << fast5_file_name << " so skipping" << std::endl;
H5Gclose(read_group);
H5Fclose(file_id);
return FAST5_FILE_CONTENTS_UNRECOGNIZED;
}
hsize_t num_read_objects_rejected = 0;
char read_subgroup_name[6]; // only care about the first five letters for this check, plus null string terminator
for(int j = 0; j < num_read_objects; ++j){
// See if the object looks like a read based on its name
size_t name_size = H5Gget_objname_by_idx(read_group, j, read_subgroup_name, 6);
// Should have the form Read_# (old) or read_????? (new)
if(name_size < 5 || (read_subgroup_name[0] != 'R' && read_subgroup_name[0] != 'r')
|| read_subgroup_name[1] != 'e'
|| read_subgroup_name[2] != 'a'
|| read_subgroup_name[3] != 'd'
|| read_subgroup_name[4] != '_'){
std::cout << "Skipping unexpected HDF5 object " << read_subgroup_name << std::endl;
num_read_objects_rejected++;
continue;
}
}
H5Gclose(read_group);
H5Fclose(file_id);
*num_sequences = (size_t) (num_read_objects - num_read_objects_rejected);
return 0;
}
#endif
int
scan_tsv_data(const char *tsv_file_name, size_t *num_sequences){
// Count the number of lines in the file (buffering 1MB on read for speed) so we know how much space to allocate for sequence pointers
// std::ios::sync_with_stdio(false); // optimization
const int SZ = 1024 * 1024;
std::vector <char> read_buffer( SZ );
std::ifstream ifs(tsv_file_name, std::ios::binary); // Don't bother translating EOL as we are counting only, so using binary mode (PC + *NIX)
if(!ifs){
return 1;
}
int n = 0;
while(int sz = FileRead(ifs, read_buffer)) {
n += CountLines(read_buffer, sz);
}
*num_sequences = n;
return 0;
}
#if SLOW5_SUPPORTED == 1
template <typename T>
int
read_slow5_data(const char *slow5_file_name, T **sequences, char **sequence_names, size_t *sequence_lengths){
int local_seq_count_so_far = 0;
slow5_file_t *sp = slow5_open(slow5_file_name,"r");
if(sp==NULL){ // No message, assume scan function called earlier provided these
return 0;
}
slow5_rec_t *rec = NULL;
int ret=0;
while((ret = slow5_get_next(&rec,sp)) >= 0){
ssize_t read_length = rec->len_raw_signal;
ssize_t name_size = strlen(rec->read_id)+1;
T *t_seq = 0;
cudaMallocManaged(&t_seq, sizeof(T)*read_length); CUERR("Cannot allocate managed memory for SLOW5 signal");
// Convert the SLOW5 raw shorts to the desired datatype from the template
for(int j = 0; j < read_length; j++){
t_seq[j] = (T) rec->raw_signal[j];
}
sequences[local_seq_count_so_far] = t_seq;
sequence_lengths[local_seq_count_so_far] = read_length;
cudaMallocHost(&sequence_names[local_seq_count_so_far], name_size); CUERR("Cannot allocate CPU memory for reading sequence name from SLOW5 file");
memcpy(sequence_names[local_seq_count_so_far], rec->read_id, name_size);
local_seq_count_so_far++;
}
if(ret != SLOW5_ERR_EOF){ //check if proper end of file has been reached
std::cerr << "Error in slow5_get_next: Error code " << ret << std::endl;
return SLOW5_FILE_UNREADABLE;
}
slow5_rec_free(rec);
slow5_close(sp);
return local_seq_count_so_far;
}
#endif
#if HDF5_SUPPORTED == 1
template <typename T>
int
read_fast5_data(const char *fast5_file_name, T **sequences, char **sequence_names, size_t *sequence_lengths){
int local_seq_count_so_far = 0;
hid_t file_id = H5Fopen(fast5_file_name, H5F_ACC_RDONLY, H5P_DEFAULT);
if(file_id < 0){ // No message, assume scan function called earlier provided these
return 0;
}
bool old_format = true;
H5Eset_auto1(NULL, NULL);
// Old format, one read per file
hid_t read_group = H5Gopen(file_id, "/Raw/Reads", H5P_DEFAULT);
if(read_group < 0){ // New formst, multiple reads per file
read_group = H5Gopen(file_id, "/", H5P_DEFAULT);
old_format = false;
}
hsize_t num_read_objects = 0;
if(read_group < 0 || H5Gget_num_objs(read_group, &num_read_objects)){
H5Gclose(read_group);
H5Fclose(file_id);
return 0;
}
char *read_subgroup_name = NULL;
for(int i = 0; i < num_read_objects; ++i){
ssize_t name_size = H5Gget_objname_by_idx(read_group, i, NULL, 0);
if(name_size < 5){
continue; // Too short to be "[Rr]ead_..."
}
name_size++; // add space for NULL termination
errno = 0;
read_subgroup_name = (char *) (read_subgroup_name == NULL ? std::malloc(name_size) : std::realloc(read_subgroup_name, name_size));
if(errno || (read_subgroup_name == NULL)){
std::cerr << "Error in malloc/realloc for HDF5 read group name: " << strerror(errno) << std::endl;
exit(FAST5_CANNOT_MALLOC_READNAME);
}
name_size = H5Gget_objname_by_idx(read_group, i, read_subgroup_name, name_size);
// Should have the form Read_# (old) or read_????? (new)
if(name_size < 5 || (read_subgroup_name[0] != 'R' && read_subgroup_name[0] != 'r') || read_subgroup_name[1] != 'e' || read_subgroup_name[2] != 'a' || read_subgroup_name[3] != 'd' || read_subgroup_name[4] != '_'){
std::cerr << "Skipping " << read_subgroup_name << " as it does not follow the naming convention" << std::endl;
continue;
}
hid_t signal_dataset_id = 0;
if(old_format){
signal_dataset_id = H5Dopen(file_id, (CONCAT3("/Raw/Reads/",read_subgroup_name,"/Signal")).c_str(), H5P_DEFAULT);
}
else{
signal_dataset_id = H5Dopen(file_id, (CONCAT3("/",read_subgroup_name,"/Raw/Signal")).c_str(), H5P_DEFAULT);
}
if(signal_dataset_id < 0){
std::cerr << "Skipping " << read_subgroup_name << " Signal, H5DOpen failed" << std::endl;
continue;
}
hid_t signal_dataspace_id = H5Dget_space(signal_dataset_id);
if(signal_dataspace_id < 0){
std::cerr << "Skipping " << read_subgroup_name << " Signal, cannot get the data space" << std::endl;
continue;
}
const hsize_t read_length = H5Sget_simple_extent_npoints(signal_dataspace_id);
if(read_length < 1){
std::cerr << "Skipping " << read_subgroup_name << " with reported Signal length " << read_length << std::endl;
continue;
}
hid_t memSpace = H5Screate_simple(1, &read_length, NULL);
if(memSpace < 0){
std::cerr << "Failed to create a simple memory space specification in the HDF5 API, please report to the software author(s)." << std::endl;
exit(FAST5_HDF5_API_ERROR);
}
short *sequence_buffer = (short *) std::malloc(sizeof(short)*read_length);
if(H5Dread(signal_dataset_id, H5T_STD_I16LE, memSpace, signal_dataspace_id, H5P_DEFAULT, sequence_buffer) < 0){
std::cerr << "Skipping " << read_subgroup_name << ", could not get " << read_length << " Signal from bulk FAST5 (HDF5) file '" << fast5_file_name << "'" << std::endl;
continue;
}
T *t_seq = 0;
cudaMallocManaged(&t_seq, sizeof(T)*read_length); CUERR("Cannot allocate managed memory for FAST5 signal");
// Convert the FAST5 raw shorts to the desired datatype from the template
for(int j = 0; j < read_length; j++){
t_seq[j] = (T) sequence_buffer[j];
}
free(sequence_buffer);
sequences[i] = t_seq;
sequence_lengths[i] = read_length;
cudaMallocHost(&sequence_names[local_seq_count_so_far], name_size); CUERR("Cannot allocate CPU memory for reading sequence name from FAST5 file");
memcpy(sequence_names[local_seq_count_so_far], read_subgroup_name, name_size-1); // -1 as not ASCIIZ, we'll put that in manually
sequence_names[local_seq_count_so_far][name_size-1] = '\0';
H5Dclose(signal_dataset_id);
local_seq_count_so_far++;
}
if(read_subgroup_name != NULL) free(read_subgroup_name);
H5Gclose(read_group);
H5Fclose(file_id);
return local_seq_count_so_far;
}
#endif
template <typename T>
int
read_tsv_data(const char *tsv_file_name, T **sequences, char **sequence_names, size_t *sequence_lengths){
int local_seq_count_so_far = 0;
// One sequence per line, values tab separated.
std::ifstream ifs(tsv_file_name);
if(!ifs){
return 0;
}
for(std::string line; std::getline(ifs, line); ){
// Assumption is that the first value is the sequence name (or in the case of the UCR time series archive, the sequence class identifier).
int numDataColumns = std::count(line.begin(), line.end(), '\t');
sequence_lengths[local_seq_count_so_far] = numDataColumns;
T *this_seq;
cudaMallocManaged(&this_seq, sizeof(T)*numDataColumns); CUERR("Cannot allocate managed memory for reading sequence from TSV file");
sequences[local_seq_count_so_far] = this_seq;
std::istringstream iss(line);
std::string seq_name;
iss >> seq_name;
cudaMallocHost(&sequence_names[local_seq_count_so_far], seq_name.length()+1); CUERR("Cannot allocate CPU memory for reading sequence name from TSV file");
memcpy(sequence_names[local_seq_count_so_far], seq_name.c_str(), seq_name.length()+1);
int element_count = 0;
while(iss.good()){
iss >> this_seq[element_count++]; // automatically does string -> numeric value conversion
}
local_seq_count_so_far++;
}
return local_seq_count_so_far;
}
template<typename T>
int readSequenceTSVFiles(char **filenames, int num_files, T ***sequences, char ***sequence_names, size_t **sequence_lengths){
std::cerr << "Step 1 of 3: Loading " << num_files << (num_files == 1 ? " TSV data file" : " TSV data files");
// Need two passes: 1st figure out how many sequences there are, then in the 2nd we read the sequences into memory.
size_t total_seq_count = 0;
for(int i = 0; i < num_files; ++i){
size_t seq_count_this_file = 0;
scan_tsv_data(filenames[i], &seq_count_this_file);
total_seq_count += seq_count_this_file;
}
std::cerr << ", total sequence count " << total_seq_count << std::endl;
std::cerr << "0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << std::endl;
cudaMallocManaged(sequences, sizeof(T *)*total_seq_count); CUERR("Allocating managed memory for sequence pointers");
cudaMallocHost(sequence_names, sizeof(char *)*total_seq_count); CUERR("Allocating CPU memory for sequence lengths");
cudaMallocManaged(sequence_lengths, sizeof(size_t)*total_seq_count); CUERR("Allocating managed memory for sequence lengths");
int dotsPrinted = 0;
char spinner[4] = { '|', '/', '-', '\\'};
int actual_count = 0;
for(int i = 0; i < num_files; ++i){
int newDotTotal = 100*((float) i/(num_files-1));
if(newDotTotal > dotsPrinted){
for(; dotsPrinted < newDotTotal; dotsPrinted++){
std::cerr << "\b.|";
}
}
else{
std::cerr << "\b" << spinner[i%4];
}
size_t num_seqs_this_file = read_tsv_data<T>(filenames[i], (*sequences) + actual_count, (*sequence_names) + actual_count, (*sequence_lengths) + actual_count);
if(num_seqs_this_file < 1){
std::cerr << "Error reading in TSV file " << filenames[i] << ", skipping" << std::endl;
}
else{
actual_count += num_seqs_this_file;
}
}
if(dotsPrinted < 100){while(dotsPrinted++ < 99){std::cerr << ".";} std::cerr << "|";}
std::cerr << std::endl;
return actual_count;
}
#if SLOW5_SUPPORTED == 1
template<typename T>
int readSequenceSLOW5Files(char **filenames, int num_files, T ***sequences, char ***sequence_names, size_t **sequence_lengths){
std::cerr << "Step 1 of 3: Loading " << num_files << (num_files == 1 ? " S/BLOW5 file" : " S/BLOW5 files");
// Need two passes: 1st figure out how many sequences there are, then in the 2nd we read the sequences into memory.
size_t total_seq_count = 0;
for(int i = 0; i < num_files; ++i){
size_t seq_count_this_file = 0;
scan_slow5_data(filenames[i], &seq_count_this_file);
total_seq_count += seq_count_this_file;
}
std::cerr << ", total sequence count " << total_seq_count << std::endl;
std::cerr << "0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << std::endl;
cudaMallocManaged(sequences, sizeof(T *)*total_seq_count); CUERR("Allocating managed memory for sequence pointers");
cudaMallocHost(sequence_names, sizeof(char *)*total_seq_count); CUERR("Allocating CPU memory for sequence lengths");
cudaMallocManaged(sequence_lengths, sizeof(size_t)*total_seq_count); CUERR("Allocating managed memory for sequence lengths");
int dotsPrinted = 0;
char spinner[4] = { '|', '/', '-', '\\'};
int actual_count = 0;
for(int i = 0; i < num_files; ++i){
int newDotTotal = 100*((float) i/(num_files-1));
if(newDotTotal > dotsPrinted){
for(; dotsPrinted < newDotTotal; dotsPrinted++){
std::cerr << "\b.|";
}
}
else{
std::cerr << "\b" << spinner[i%4];
}
size_t num_seqs_this_file = read_slow5_data<T>(filenames[i], (*sequences) + actual_count, (*sequence_names) + actual_count, (*sequence_lengths) + actual_count);
if(num_seqs_this_file < 1){
std::cerr << "Error reading in SLOW5 file " << filenames[i] << ", skipping" << std::endl;
}
else{
actual_count += num_seqs_this_file;
}
}
if(dotsPrinted < 100){while(dotsPrinted++ < 99){std::cerr << ".";} std::cerr << "|";}
std::cerr << std::endl;
return actual_count;
}
#endif
#if HDF5_SUPPORTED == 1
template<typename T>
int readSequenceFAST5Files(char **filenames, int num_files, T ***sequences, char ***sequence_names, size_t **sequence_lengths){
std::cerr << "Step 1 of 3: Loading " << num_files << (num_files == 1 ? " FAST5 data file" : " FAST5 data files");
// Need two passes: 1st figure out how many sequences there are, then in the 2nd we read the sequences into memory.
size_t total_seq_count = 0;
for(int i = 0; i < num_files; ++i){
size_t seq_count_this_file = 0;
scan_fast5_data(filenames[i], &seq_count_this_file);
total_seq_count += seq_count_this_file;
}
std::cerr << ", total sequence count " << total_seq_count << std::endl;
std::cerr << "0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << std::endl;
cudaMallocManaged(sequences, sizeof(T *)*total_seq_count); CUERR("Allocating managed memory for sequence pointers");
cudaMallocHost(sequence_names, sizeof(char *)*total_seq_count); CUERR("Allocating CPU memory for sequence names");
cudaMallocManaged(sequence_lengths, sizeof(size_t)*total_seq_count); CUERR("Allocating managed memory for sequence lengths");
int dotsPrinted = 0;
char spinner[4] = { '|', '/', '-', '\\'};
int actual_count = 0;
for(int i = 0; i < num_files; ++i){
int newDotTotal = 100*((float) i/(num_files-1));
if(newDotTotal > dotsPrinted){
for(; dotsPrinted < newDotTotal; dotsPrinted++){
std::cerr << "\b.|";
}
}
else{
std::cerr << "\b" << spinner[i%4];
}
size_t num_seqs_this_file = read_fast5_data<T>(filenames[i], (*sequences) + actual_count, (*sequence_names) + actual_count, (*sequence_lengths) + actual_count);
if(num_seqs_this_file < 1){
std::cerr << "No reads in FAST5 file " << filenames[i] << ", skipping" << std::endl;
}
else{
actual_count += num_seqs_this_file;
}
}
if(dotsPrinted < 100){while(dotsPrinted++ < 99){std::cerr << ".";} std::cerr << "|";}
std::cerr << std::endl;
return actual_count;
}
#endif
template<typename T>
int readSequenceTextFiles(char **filenames, int num_files, T ***sequences, char ***sequence_names, size_t **sequence_lengths){
cudaMallocManaged(sequences, sizeof(T *)*num_files); CUERR("Allocating managed memory for sequence pointers from text files");
cudaMallocHost(sequence_names, sizeof(char *)*num_files); CUERR("Allocating host memory for sequence names from text files");
cudaMallocManaged(sequence_lengths, sizeof(size_t)*num_files); CUERR("Allocating managed memory for sequence lengths from text files");
int dotsPrinted = 0;
std::cerr << "Step 1 of 3: Loading " << num_files << (num_files == 1 ? " text data file" : " text data files") << ", total sequence count " << num_files << std::endl;
std::cerr << "0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << std::endl;
char spinner[4] = { '|', '/', '-', '\\'};
int actual_count = 0;
for(int i = 0; i < num_files; ++i){
int newDotTotal = 100*((float) i/(num_files-1));
if(newDotTotal > dotsPrinted){
for(; dotsPrinted < newDotTotal; dotsPrinted++){
std::cerr << "\b.|";
}
}
else{
std::cerr << "\b" << spinner[i%4];
}
if(read_text_data<T>(filenames[i], (*sequences) + actual_count, (*sequence_lengths) + actual_count)){
std::cerr << "Error reading in text file " << filenames[i] << ", skipping" << std::endl;
}
else{
actual_count++;
}
cudaMallocHost(*sequence_names+i, sizeof(char)*(strlen(filenames[i])+1)); CUERR("Allocating managed memory for a sequence name from text file");
strcpy((*sequence_names)[i], filenames[i]);
}
if(dotsPrinted < 100){while(dotsPrinted++ < 99){std::cerr << ".";} std::cerr << "|";}
std::cerr << std::endl;
return actual_count;
}
template<typename T>
int readSequenceBinaryFiles(char **filenames, int num_files, T ***sequences, char ***sequence_names, size_t **sequence_lengths, bool is_short=false){
cudaMallocManaged(sequences, sizeof(T *)*num_files); CUERR("Allocating CPU memory for sequence pointers from binary files");
cudaMallocHost(sequence_names, sizeof(char *)*num_files); CUERR("Allocating host memory for sequence names from binary files");
cudaMallocManaged(sequence_lengths, sizeof(size_t)*num_files); CUERR("Allocating CPU memory for sequence lengths from binary files");
int dotsPrinted = 0;
std::cerr << "Step 1 of 3: Loading " << num_files << (num_files == 1 ? " binary data file" : " binary data files") << ", total sequence count " << num_files << std::endl;
std::cerr << "0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << std::endl;
char spinner[4] = { '|', '/', '-', '\\'};
int actual_count = 0;
for(int i = 0; i < num_files; ++i){
int newDotTotal = 100*((float) i/(num_files-1));
if(newDotTotal > dotsPrinted){
for(; dotsPrinted < newDotTotal; dotsPrinted++){
std::cerr << "\b.|";
}
}
else{
std::cerr << "\b" << spinner[i%4];
}
if(read_binary_data<T>(filenames[i], (*sequences) + actual_count, (*sequence_lengths) + actual_count, is_short)){
std::cerr << "Error reading in binary file " << filenames[i] << ", skipping" << std::endl;
}
else{
actual_count++;
}
cudaMallocHost(*sequence_names+i, sizeof(char)*(strlen(filenames[i])+1)); CUERR("Allocating managed memory for a sequence name from text file");
strcpy((*sequence_names)[i], filenames[i]);
}
if(dotsPrinted < 100){while(dotsPrinted++ < 99){std::cerr << ".";} std::cerr << "|";}
std::cerr << std::endl;
return actual_count;
}
#endif