Skip to content

Commit

Permalink
Moving slow5_deg.* to existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
sashajenner committed Aug 16, 2024
1 parent f233f57 commit 62a0393
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 128 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ OBJ = $(BUILD_DIR)/slow5.o \
$(BUILD_DIR)/slow5_misc.o \
$(BUILD_DIR)/slow5_press.o \
$(BUILD_DIR)/slow5_mt.o \
$(BUILD_DIR)/slow5_deg.o \

PREFIX = /usr/local
VERSION = `git describe --tags`
Expand Down Expand Up @@ -71,9 +70,6 @@ $(BUILD_DIR)/slow5_press.o: src/slow5_press.c include/slow5/slow5_press.h src/sl
$(BUILD_DIR)/slow5_mt.o: src/slow5_mt.c include/slow5/slow5_mt.h $(SLOW5_H)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -fpic -o $@

$(BUILD_DIR)/slow5_deg.o: src/slow5_deg.c src/slow5_deg.h include/slow5/slow5.h include/slow5/slow5_error.h
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -fpic -o $@

clean:
rm -rf $(OBJ) $(STATICLIB) $(SHAREDLIB) $(SHAREDLIBV)
make -C $(SVB) clean
Expand Down
81 changes: 0 additions & 81 deletions src/slow5_deg.c

This file was deleted.

43 changes: 0 additions & 43 deletions src/slow5_deg.h

This file was deleted.

25 changes: 25 additions & 0 deletions src/slow5_extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define SLOW5_EXTRA_H

#include <dirent.h>
#include <stdint.h>
#include <slow5/slow5.h>
#include <slow5/slow5_error.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -19,6 +21,9 @@ Function prototypes can be changed without notice or completely removed. So do N
these functions are used by slow5tools and pyslow5 - so any change to a function here means slow5tools and pyslow5 must be fixed.
*/

extern enum slow5_log_level_opt slow5_log_level;
extern enum slow5_exit_condition_opt slow5_exit_condition;

// slow5 file
slow5_file_t *slow5_init(FILE *fp, const char *pathname, enum slow5_fmt format);
slow5_file_t *slow5_init_empty(FILE *fp, const char *pathname, enum slow5_fmt format);
Expand Down Expand Up @@ -142,6 +147,26 @@ const char *slow5_format_get_str(enum slow5_format format);
//const uint8_t *str_get_slow5_version(const char *str);
*/

/* Irreversible signal degradation (lossy compression) API */

/*
* Return a suggestion for the number of bits to use with qts degradation given
* a slow5 file. Return 0 on error and set slow5_errno.
*/
int8_t slow5_suggest_qts(const struct slow5_file *p);

/*
* For array a with length n, zero b LSB of each integer by rounding them to the
* nearest power of 2. Set slow5_errno on error.
*/
void slow5_arr_qts_round(int16_t *a, uint64_t n, uint8_t b);

/*
* For the signal array in a slow5 record, zero b LSB of each integer by
* rounding them to the nearest power of 2. Set slow5_errno on error.
*/
void slow5_rec_qts_round(struct slow5_rec *r, uint8_t b);

#ifdef __cplusplus
}
#endif
Expand Down
76 changes: 76 additions & 0 deletions src/slow5_press.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifdef SLOW5_USE_ZSTD
#include <zstd.h>
#endif /* SLOW5_USE_ZSTD */
#include "slow5_extra.h"
#include "slow5_misc.h"

extern enum slow5_log_level_opt slow5_log_level;
Expand Down Expand Up @@ -46,6 +47,7 @@ static int16_t *ptr_depress_ex_zd(const uint8_t *ptr, size_t count, size_t *n);

/* other */
static int vfprintf_compress(struct __slow5_press *comp, FILE *fp, const char *format, va_list ap);
static int round_to_power_of_2(int number, int number_of_bits);


/* Convert the press method back an forth from the library format to spec format
Expand Down Expand Up @@ -1956,3 +1958,77 @@ unsigned char *z_inflate_buf(const char *comp_str, size_t *n) {
return written;
}
*/

/* Irreversible signal degradation (lossy compression) */

/*
* Return a suggestion for the number of bits to use with qts degradation given
* a slow5 file. Return 0 on error and set slow5_errno.
*/
int8_t slow5_suggest_qts(const struct slow5_file *p)
{
if (!p) {
SLOW5_ERROR("Argument '%s' cannot be NULL.", SLOW5_TO_STR(p));
slow5_errno = SLOW5_ERR_ARG;
return 0;
}

return 0;
}

/*
* Zero number_of_bits LSB of number by rounding it to the nearest power of 2.
* number_of_bits must be >= 1. Return the rounded number.
* Taken from https://github.com/hasindu2008/sigtk src/qts.c.
*/
static int round_to_power_of_2(int number, int number_of_bits) {
//create a binary mask with the specified number of bits
int bit_mask = (1 << number_of_bits) - 1;

//extract out the value of the LSBs considered
int lsb_bits = number & bit_mask;


int round_threshold = (1 << (number_of_bits - 1));

//check if the least significant bits are closer to 0 or 2^n
if (lsb_bits < round_threshold) {
return (number & ~bit_mask) + 0; //round down to the nearest power of 2
} else {
return (number & ~bit_mask) + (1 << number_of_bits); //round up to the nearest power of 2
}
}

/*
* For array a with length n, zero b LSB of each integer by rounding them to the
* nearest power of 2. Set slow5_errno on error.
*/
void slow5_arr_qts_round(int16_t *a, uint64_t n, uint8_t b)
{
uint64_t i;

if (!b)
return;
if (!a) {
SLOW5_ERROR("Argument '%s' cannot be NULL.", SLOW5_TO_STR(a));
slow5_errno = SLOW5_ERR_ARG;
return;
}

for (i = 0; i < n; i++)
a[i] = round_to_power_of_2(a[i], (int) b);
}

/*
* For the signal array in a slow5 record, zero b LSB of each integer by
* rounding them to the nearest power of 2. Set slow5_errno on error.
*/
void slow5_rec_qts_round(struct slow5_rec *r, uint8_t b)
{
if (!r) {
SLOW5_ERROR("Argument '%s' cannot be NULL.", SLOW5_TO_STR(r));
slow5_errno = SLOW5_ERR_ARG;
} else {
slow5_arr_qts_round(r->raw_signal, r->len_raw_signal, b);
}
}

0 comments on commit 62a0393

Please sign in to comment.