-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.h
188 lines (136 loc) · 6.41 KB
/
io.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
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
#ifndef __IO__
#define __IO__
#include <htslib/bgzf.h>
#include <htslib/kstring.h>
#include <htslib/tbx.h>
#include <zlib.h>
#include "argStruct.h"
struct pairStruct;
typedef struct outfile_t outfile_t;
struct outfile_t {
uint8_t ctype; // output file compression type (e.g. none, gz, bgz. see PROGRAM_OUTFILE_CTYPE_*
char* fn; // full output filename (prefix + suffix + extension + cextension)
kstring_t kbuf; // kstring buffer for output file content
FILE* fp; // used if ctype==PROGRAM_OUTFILE_CTYPE_NONE
gzFile gzfp; // used if ctype==PROGRAM_OUTFILE_CTYPE_GZ
BGZF* bgzfp; // used if ctype==PROGRAM_OUTFILE_CTYPE_BGZ
};
outfile_t* outfile_init(const char* suffix, const char* extension, const uint8_t ctype);
void outfile_destroy(outfile_t* outfile);
void outfile_write(outfile_t* outfile);
//********************************************************************************
//***************************** IO *********************************************
//********************************************************************************
namespace IO {
/// @brief validateString - check if string is valid
void validateString(const char* str);
/// @brief verbose - check if verbose level meets the threshold
///
/// @param verbose_threshold - threshold to check against the verbose arg value
/// @return 1 if verbose level meets the threshold, 0 otherwise
///
/// @example
/// if `-v 3` is used; (sets the bit at index 2 (3-1==2)
/// verbose(2) will check if a bit with index 1 (2-1==1) or higher is set
/// if(verbose(2)) will run;
///
/// if `-v 1` is used; (sets the bit at index 0 (1-1==0)
/// verbose(2) will check if a bit with index 1 (2-1==1) or higher is set
/// if(verbose(2)) will not run;
int verbose(const int verbose_threshold);
// TODO use a global count for count write warnings to log for x amount of sites like in angsd
// void vprint_count(const char* format, ...);
/// @brief vprint - verbose print: print to stderr if verbose > 0
/// @example vprint("Hello %s", "World");
void vprint(const char* format, ...);
/// @brief <o> verbose print with threshold
/// @param verbose_threshold threshold for printing the specified message
/// @example vprint(1, "Hello %s", "World"); // will print if verbose >= 1
void vprint(const int verbose_threshold, const char* format, ...);
/// @brief <o> verbose print with threshold and file pointer
/// @param fp file pointer to print to
/// @param verbose_threshold threshold for printing the specified message
/// @example vprint(1, "Hello %s", "World"); // will print to fp if verbose >= 1
void vprint(FILE* fp, const int verbose_threshold, const char* format, ...);
hts_idx_t* load_bcf_csi_idx(const char* fn);
tbx_t* load_vcf_tabix_idx(const char* fn);
void requireArgFile(const char* fn, const char* requiredArg, const char* requiredFor);
void requireArgFile(const char* fn, const char* requiredArg);
void requireArgStr(const char* str, const char* requiredArg, const char* requiredFor);
void requireArgStr(const char* str, const char* requiredArg);
extern const char* FILE_EXTENSIONS[];
int fileExists(const char* fn);
char* setFileName(const char* a, const char* b);
char* setFileName(const char* fn, const char* suffix, const char* fc_ext);
const char* getFileExtension(const char* fn);
FILE* getFile(const char* fname, const char* mode);
gzFile getGzFile(const char* fname, const char* mode);
FILE* openFileW(const char* a, const char* b);
FILE* openFileW(char* c);
gzFile openGzFileW(const char* a, const char* b);
gzFile openGzFileW(char* c);
bool isGzFile(const char* fn);
namespace readFile {
char* getFirstLine(FILE* fp);
char* getLine(FILE* fp);
char* readToBuffer(const char* fn);
}; // namespace readFile
namespace readGzFile {
char* getFirstLine(char* fn);
char* getFirstLine(gzFile fp);
int readToBuffer(char* fn, char** buffer_p, size_t* buf_size_p);
}; // namespace readGzFile
namespace inspectFile {
int count_nCols(const char* line, const char* delims);
int count_nRows(char* fn, int HAS_COLNAMES);
int count_nRows(FILE* fp, int HAS_COLNAMES);
}; // namespace inspectFile
/// @brief outputStruct - struct for output files
///
/// @param kbuf - kstring buffer for output file contents
/// -> if kbuf will only be used once,
/// memory is allocated and deallocated inplace
/// (inside the specific writing function)
/// e.g. see amova_t::print_as_csv()
/// -> if kbuf will be used multiple times
/// i.e. appending to kbuf from different functions
/// memory is allocated and deallocated in the constructor
/// and destructor respectively
/// e.g. see out_args_fs
/// for thread safety, use a separate kstring_t within each thread
/// and append to the main kbuf after joining the threads
///
typedef struct outputStruct {
char* fn = NULL;
OUTFC fc;
FILE* fp = NULL;
gzFile gzfp = NULL;
BGZF* bgzfp = NULL;
kstring_t* kbuf = NULL;
outputStruct(const char* fn_, const char* suffix, int fc_);
~outputStruct();
void flush();
void* get_fp();
void write(const char* buf);
void write(kstring_t* kbuf);
void kbuf_write(); // write internal kbuf
void kbuf_destroy_buffer();
} outputStruct;
// typedef struct outFilesStruct {
// outputStruct* out_args_fs = NULL;
// outputStruct* out_dm_fs = NULL;
// outputStruct* out_amova_fs = NULL;
// outputStruct* out_dev_fs = NULL;
// outputStruct* out_jgcd_fs = NULL;
// outputStruct* out_dxy_fs = NULL;
// outputStruct* out_nj_fs = NULL;
// outputStruct* out_blockstab_fs = NULL;
// outputStruct* out_v_bootstrapRep_fs = NULL;
// } outFilesStruct;
// void outFilesStruct_init(outFilesStruct* ofs);
// void outFilesStruct_destroy(outFilesStruct* ofs);
} // namespace IO
// extern IO::outFilesStruct* outFiles;
kstring_t* kbuf_init();
void kbuf_destroy(kstring_t* kbuf);
#endif // __IO__