File: slow5_press.h

package info (click to toggle)
libslow5lib 0.7.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,092 kB
  • sloc: ansic: 11,825; python: 1,179; sh: 547; makefile: 91; cpp: 40
file content (152 lines) | stat: -rw-r--r-- 6,604 bytes parent folder | download | duplicates (2)
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
/**
 * @file slow5_press.h
 * @brief SLOW5 compression and decompression functions
 * @author Sasha Jenner (jenner.sasha@gmail.com)
 * @date 27/02/2021
 */

/**************************************************************************************************
 ***  Low-level API *******************************************************************************
 **************************************************************************************************/

/*
IMPORTANT: The following low-level APIfunctions  are not yet finalised or documented.
If anyone is interested, please open a GitHub issue, rather than trying to figure out from the code.
Function proptotypes can be changed without notice or completely removed. So do NOT use these functions in your code.
these functions are used by slow5tools and pyslow5 - so any change to a function here means slow5tools and pyslow5 must be fixed
*/

/*
MIT License

Copyright (c) 2020 Sasha Jenner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef SLOW5_PRESS_H
#define SLOW5_PRESS_H

#include <zlib.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* zlib macros */
#define SLOW5_ZLIB_MEM_DEFAULT (8)
#define SLOW5_ZLIB_COMPRESS_CHUNK (131072) /* compression buffer size -- 128KB (2^17) */
#define SLOW5_ZLIB_DEPRESS_CHUNK (262144) /* decompression buffer size -- 256KB (2^18) */

#define SLOW5_ZSTD_COMPRESS_LEVEL (1)

/* (de)compression methods */
enum slow5_press_method {
    SLOW5_COMPRESS_NONE,
    SLOW5_COMPRESS_ZLIB,
    SLOW5_COMPRESS_SVB_ZD, /* streamvbyte zigzag delta */
    SLOW5_COMPRESS_ZSTD,
};
typedef struct{
    enum slow5_press_method record_method;
    enum slow5_press_method signal_method;
} slow5_press_method_t;

/* zlib stream */
struct slow5_zlib_stream {
    z_stream strm_inflate;
    z_stream strm_deflate;
    int flush;
};

/* (de)compression streams */
union slow5_press_stream {
    struct slow5_zlib_stream *zlib;
};

/* (de)compression object */
struct __slow5_press {
    enum slow5_press_method method;
    union slow5_press_stream *stream;
};

typedef struct slow5_press {
    struct __slow5_press *record_press;
    struct __slow5_press *signal_press;
} slow5_press_t;

/* init or free for multiple (de)compress calls */
struct slow5_press *slow5_press_init(slow5_press_method_t method);
struct __slow5_press *__slow5_press_init(enum slow5_press_method method);
void slow5_press_free(struct slow5_press *comp);
void __slow5_press_free(struct __slow5_press *comp);

/* (de)compress ptr */
void *slow5_ptr_compress(struct __slow5_press *comp, const void *ptr, size_t count, size_t *n);
void *slow5_ptr_compress_solo(enum slow5_press_method method, const void *ptr, size_t count, size_t *n);
void *slow5_ptr_depress(struct __slow5_press *comp, const void *ptr, size_t count, size_t *n);
void *slow5_ptr_depress_solo(enum slow5_press_method method, const void *ptr, size_t count, size_t *n);
static inline void *slow5_str_compress(struct __slow5_press *comp, const char *str, size_t *n);

/* (de)compress ptr and write */
ssize_t slow5_fwrite_compress(struct __slow5_press *comp, const void *ptr, size_t size, size_t nmemb, FILE *fp);
ssize_t slow5_fwrite_depress(struct __slow5_press *comp, const void *ptr, size_t size, size_t nmemb, FILE *fp); /* TODO */
static inline ssize_t slow5_fwrite_str_compress(struct __slow5_press *comp, const char *str, FILE *fp);
static inline ssize_t slow5_print_compress(struct __slow5_press *comp, const void *ptr, size_t size, size_t nmemb);
static inline ssize_t slow5_print_depress(struct __slow5_press *comp, const void *ptr, size_t size, size_t nmemb);
static inline ssize_t slow5_print_str_compress(struct __slow5_press *comp, const char *str);
int slow5_fprintf_compress(struct __slow5_press *comp, FILE *fp, const char *format, ...);
int slow5_printf_compress(struct __slow5_press *comp, const char *format, ...);

/* read and (de)compress */
void *slow5_fread_depress(struct __slow5_press *comp, size_t count, FILE *fp, size_t *n);
void *slow5_pread_depress(struct __slow5_press *comp, int fd, size_t count, off_t offset, size_t *n);
void *slow5_pread_depress_solo(enum slow5_press_method method, int fd, size_t count, off_t offset, size_t *n);

/* write compression footer on next compress call */
void slow5_compress_footer_next(struct __slow5_press *comp);

/* static inline function definitions */
static inline void *slow5_str_compress(struct __slow5_press *comp, const char *str, size_t *n) {
    return slow5_ptr_compress(comp, str, strlen(str) + 1, n); /* include '\0' */
}
static inline ssize_t slow5_fwrite_str_compress(struct __slow5_press *comp, const char *str, FILE *fp) {
    return slow5_fwrite_compress(comp, str, sizeof *str, strlen(str), fp); /* don't include '\0' */
}
static inline ssize_t slow5_print_compress(struct __slow5_press *comp, const void *ptr, size_t size, size_t nmemb) {
    return slow5_fwrite_compress(comp, ptr, size, nmemb, stdout);
}
static inline ssize_t slow5_print_depress(struct __slow5_press *comp, const void *ptr, size_t size, size_t nmemb) {
    return slow5_fwrite_depress(comp, ptr, size, nmemb, stdout);
}
static inline ssize_t slow5_print_str_compress(struct __slow5_press *comp, const char *str) {
    return slow5_fwrite_str_compress(comp, str, stdout);
}

uint8_t slow5_encode_record_press(enum slow5_press_method method);
enum slow5_press_method slow5_decode_record_press(uint8_t method);
uint8_t slow5_encode_signal_press(enum slow5_press_method method);
enum slow5_press_method slow5_decode_signal_press(uint8_t method);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* slow5_press.h */