Skip to content

Commit

Permalink
group to utils library
Browse files Browse the repository at this point in the history
  • Loading branch information
zwdzwd committed Dec 19, 2016
1 parent facd372 commit 10d466c
Show file tree
Hide file tree
Showing 9 changed files with 1,694 additions and 0 deletions.
629 changes: 629 additions & 0 deletions encode.h

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef _WZ_STATS_H_
#define _WZ_STATS_H_

#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#define MAXIT 100
#define EPS 3.0e-7
#define FPMIN 1.0e-30

typedef enum{HOMOREF = 0, HET = 1, HOMOVAR = 2} Genotype;

double binom_pval(int s, int n, double p);

double pval2qual(double pval);

double varcall_pval(int kr, int kv, double error, double mu, double contam);

double genotype_lnlik(Genotype genotype, int kr, int kv, double error, double contam);

double somatic_lnlik(int kr, int kv, double error);

double fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);

/* double ln_beta_incdiff_kernel(double p1, double p2, int a, int b); */
double inconsist_score(int kr_tumor, int kv_tumor, int kr_normal, int kv_normal, double mu, double error);

double ln_sum2(double ln1, double ln2);

double ln_sum3(double ln1, double ln2, double ln3);

double ln_substract(double ln1, double ln2);

double ln_binom_kernel(double p, int a, int b);

double ln_beta_incdiff_kernel(double p1, double p2, int a, int b);

double somatic_posterior(int kr_t, int kv_t, int kr_n, int kv_n, double error, double mu, double mu_somatic, double contam);

double beta_binomial(int k, int n, double a, double b);

#endif
90 changes: 90 additions & 0 deletions wqueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef _WQUEUE_H_
#define _WQUEUE_H_

#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>

/*
by [email protected]
block get when empty
block put when full
example usage:
*/

#define DEFINE_WQUEUE(name, _wqueue_elem_t) \
typedef struct { \
uint32_t size; \
_wqueue_elem_t *data; \
uint32_t head, tail; \
int full, empty; \
pthread_mutex_t mut; \
pthread_cond_t not_full, not_empty; \
} wqueue_##name##_t; \
static inline wqueue_##name##_t *wqueue_init_##name(uint32_t size) { \
wqueue_##name##_t *q = calloc(1, sizeof(wqueue_##name##_t)); \
q->empty = 1; \
q->size = size; \
q->data = calloc(size, sizeof(_wqueue_elem_t)); \
pthread_mutex_init(&q->mut, NULL); \
pthread_cond_init(&q->not_full, NULL); \
pthread_cond_init(&q->not_empty, NULL); \
return q; \
} \
static inline void wqueue_destroy_##name(wqueue_##name##_t *q) { \
free(q->data); \
pthread_mutex_destroy(&q->mut); \
pthread_cond_destroy(&q->not_full); \
pthread_cond_destroy(&q->not_empty); \
free(q); \
} \
static inline void wqueue_get_##name(wqueue_##name##_t *q, \
_wqueue_elem_t *e) { \
pthread_mutex_lock(&q->mut); \
while(q->empty) pthread_cond_wait(&q->not_empty, &q->mut); \
*e = q->data[q->head++]; \
if (q->head == q->size) q->head = 0; \
if (q->head == q->tail) q->empty = 1; \
q->full = 0; \
pthread_mutex_unlock(&q->mut); \
pthread_cond_signal(&q->not_full); \
} \
static inline void wqueue_put_##name(wqueue_##name##_t *q, \
_wqueue_elem_t *e) { \
pthread_mutex_lock(&q->mut); \
while(q->full) pthread_cond_wait(&q->not_full, &q->mut); \
q->data[q->tail++] = *e; \
if (q->tail == q->size) q->tail = 0; \
if (q->head == q->tail) q->full = 1; \
q->empty = 0; \
pthread_mutex_unlock(&q->mut); \
pthread_cond_signal(&q->not_empty); \
} \
static inline void wqueue_put2_##name(wqueue_##name##_t *q, \
_wqueue_elem_t e) { \
pthread_mutex_lock(&q->mut); \
while(q->full) pthread_cond_wait(&q->not_full, &q->mut); \
q->data[q->tail++] = e; \
if (q->tail == q->size) q->tail = 0; \
if (q->head == q->tail) q->full = 1; \
q->empty = 0; \
pthread_mutex_unlock(&q->mut); \
pthread_cond_signal(&q->not_empty); \
}

#define wqueue_t(name) wqueue_##name##_t

#define wqueue_init(name, size) wqueue_init_##name(size)

#define wqueue_destroy(name, q) wqueue_destroy_##name(q)

#define wqueue_get(name, q, e) wqueue_get_##name(q, e)

#define wqueue_put(name, q, e) wqueue_put_##name(q, e)

#define wqueue_put2(name, q, e) wqueue_put2_##name(q, e)

#endif
51 changes: 51 additions & 0 deletions wstr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#ifndef _WZ_STR_H_
#define _WZ_STR_H_

#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include "kstring.h"

#define kstring_init(s) kstring_t (s); (s).s=0; (s).m=(s).l=0;

#define _WZ_STRLEN 100

static inline char*
wasprintf(const char *fmt, ...) {

char *s = malloc(_WZ_STRLEN); /* default length */

va_list args;
va_start(args, fmt);
int l = vsnprintf(s, _WZ_STRLEN, fmt, args); /* if fail, l is the number characters that would've been written */
if (l >= _WZ_STRLEN) {
s = realloc(s, l+1);
va_start(args, fmt);
vsnprintf(s, l+1, fmt, args);
}
va_end(args);

return s;
}

static inline int strcount_char(char *s, char c) {
int i, n=0;
for (i=0; s[i]; ++i)
if (s[i] == c)
++n;
return n;
}

static inline void ensure_number(char *s) {
int i;
for (i=0;s[i];++i) {
if (!isdigit(s[i]) && s[i]!='.') {
fprintf(stderr, "[%s:%d] Trying to convert nondigit string to number: %s\n", __func__, __LINE__, s);
fflush(stderr);
exit(1);
}
}
}

#endif /* _WZ_STR_H_ */
Loading

0 comments on commit 10d466c

Please sign in to comment.