Legofit
infers population history from nucleotide site patterns.
misc.h
1#ifndef GPTREE_MISC_H
2# define GPTREE_MISC_H
3
4# include "typedefs.h"
5# include "binary.h"
6# include "lblndx.h"
7# include <stdio.h>
8# include <math.h>
9# include <float.h>
10# include <assert.h>
11# include <gsl/gsl_rng.h>
12
13static inline int Dbl_near(double x, double y);
14int LDbl_near(long double x, long double y);
15static inline int Dbl_equals_allowNonfinite(double x, double y);
16int compareInts(const void *void_x, const void *void_y);
17int compareLongs(const void *void_x, const void *void_y);
18int compareDoubles(const void *void_x, const void *void_y);
19int getNumCores(void);
20void dostacktrace(const char *file, int line, FILE * ofp);
21double perturb_ratio_w(double x, double w, gsl_rng * rng);
22double perturb_ratio(double x, gsl_rng * rng);
23long double perturb_interval(long double x, long double lo, long double hi,
24 gsl_rng * rng);
25int removeZeroes(int dim, unsigned x[dim]);
26void eprintf(const char *fmt, ...);
27FILE *efopen(const char *restrict name, const char *restrict mode);
28void printBranchTab(double tab[3][3], FILE * fp);
29void unitTstResult(const char *facility, const char *result);
30void tellopt(const char *opt, const char *description);
31unsigned Dbl_first_geq(double val, unsigned len, double v[len]);
32long long_first_geq(long val, long *v, long len);
33long long_last_leq(long val, long *v, long len);
34int strCountSetChunks(const char *str, const char *sep);
35int strchrcnt(const char *s, int c);
36void *memdup(const void *p, size_t n);
37double KLdiverg(int n, const double o[n], const double e[n]);
38double sumDbl(int n, const double x[n]);
39double reflect(double x, double lo, double hi);
40char *strlowercase(char *s);
41#ifdef __clang__
42unsigned long strhash(const char *ss) __attribute__((no_sanitize("integer")));
43#else
44unsigned long strhash(const char *ss);
45#endif
46int stripchr(char *s, int c);
47char *stripWhiteSpace(char *buff);
48char *stripInternalWhiteSpace(char *buff);
49char *nextWhitesepToken(char **str);
50int tokenize(int dim, char *token[dim], char *s, const char *delim);
51void strReplaceChr(char *s, int a, int b);
52double parseDbl(char *token);
53char *strltrunc(char *s, int n);
54void hdr(const char *msg);
55char *strcenter(const char *text, unsigned width,
56 char *buff, size_t buffsize);
57int readline(int dim, char buff[dim], FILE *fp);
58const char *mybasename(const char *name);
59int legalName(const char *name);
60int strnncopy(size_t n, char dst[n], size_t m, const char src[m]);
61void collapse_whitespace(char * buff);
62void stutter(char c, int n, FILE * fp);
63static inline double survival(double t, double twoN);
64
65# define ERR(code, msg) do{ \
66 char buff[50]; \
67 strerror_r((code), buff, sizeof(buff)); \
68 fprintf(stderr,"%s:%s:%d: %s: %d (%s)\n", \
69 __FILE__,__func__,__LINE__, \
70 (msg), (code), buff); \
71 exit(1); \
72 }while(0)
73
74# define DIE(msg) do{ \
75 fprintf(stderr,"%s:%s:%d: %s\n", \
76 __FILE__,__func__,__LINE__, (msg)); \
77 exit(EXIT_FAILURE); \
78 }while(0)
79
80# define CHECKMEM(x) do { \
81 if((x)==NULL) { \
82 fprintf(stderr, "%s:%s:%d: NULL pointer\n", \
83 __FILE__,__func__,__LINE__); \
84 exit(EXIT_FAILURE); \
85 } \
86 } while(0)
87
88# define REQUIRE(x, file, line) do { \
89 if (!(x)) { \
90 dostacktrace(__FILE__,__LINE__,stderr); \
91 fprintf(stderr,"ERR@%s:%d->%s:%d: Sanity check FAIL\n", \
92 (file), (line), __FILE__,__LINE__); \
93 exit(EXIT_FAILURE); \
94 } \
95 } while(0)
96
97// If PTR!=NULL, add offset OSET to pointer PTR (if SIGN>0) or
98// subtract it (otherwise). Units are sizeof(char) rather than the
99// size of the object to which PTR refers.
100#define SHIFT_PTR(PTR,OSET,SIGN) do{ \
101 if((PTR) != NULL) { \
102 if((SIGN) > 0) \
103 (PTR) = (void *) (((size_t) (PTR)) + ((size_t) (OSET))); \
104 else \
105 (PTR) = (void *) (((size_t) (PTR)) - ((size_t) (OSET))); \
106 } \
107 }while(0);
108
111static inline int Dbl_near(double x, double y) {
112 double tol = fmax(fabs(x), fabs(y)) * 8.0 * DBL_EPSILON;
113 tol = fmax(tol, DBL_EPSILON);
114 return fabs(x - y) <= tol;
115}
116
120static inline int Dbl_equals_allowNonfinite(double x, double y) {
121 if(x == y)
122 return 1;
123
124 int xtype = fpclassify(x);
125 int ytype = fpclassify(y);
126
127 if(xtype==FP_NAN && ytype==FP_NAN)
128 return 1;
129 if(xtype==FP_INFINITE && ytype==FP_INFINITE) {
130 if(x>0.0 && y>0.0)
131 return 1;
132 if(x<0.0 && y<0.0)
133 return 1;
134 }
135 return 0;
136}
137
139static inline double survival(double t, double twoN) {
140 assert(t >= 0.0);
141 assert(twoN > 0.0);
142 return exp(-t / twoN);
143}
144
145#endif
void * memdup(const void *p, size_t n)
duplicate memory block
Definition: misc.c:314
double KLdiverg(int n, const double o[n], const double e[n])
Return Kullback-Leibler divergence of o relative to e.
Definition: misc.c:377
void strReplaceChr(char *s, int a, int b)
In string s, replace instances of character a with character b.
Definition: misc.c:548
void eprintf(const char *fmt,...)
eprintf: print error message and exit.
Definition: misc.c:101
int removeZeroes(int dim, unsigned x[dim])
Remove zeroes from an array of unsigned ints by sliding positive entries to the left.
Definition: misc.c:652
int compareInts(const void *void_x, const void *void_y)
Compare two ints.
Definition: misc.c:352
double perturb_ratio(double x, gsl_rng *rng)
Uniform perturbation on log scale.
Definition: misc.c:153
double perturb_ratio_w(double x, double w, gsl_rng *rng)
Uniform perturbation on log scale.
Definition: misc.c:141
double reflect(double x, double lo, double hi)
Fold x back and forth across the boundaries "lo" and "hi" to obtain a value y such that lo <= y <= hi...
Definition: misc.c:408
FILE * efopen(const char *restrict name, const char *restrict mode)
Open file.
Definition: misc.c:119
char * stripWhiteSpace(char *buff)
Remove white space from beginning and end of a string.
Definition: misc.c:474
char * strlowercase(char *s)
Convert NULL-terminated string to lower case.
Definition: misc.c:428
int strCountSetChunks(const char *str, const char *sep)
In string str, count the number of contiguous chunks of characters belonging to set.
Definition: misc.c:296
long long_last_leq(long val, long *v, long len)
Vector v must be sorted in ascending order before this function is called.
Definition: misc.c:257
char * strcenter(const char *text, unsigned width, char *buff, size_t buffsize)
Center string "text" in a field of width "width".
Definition: misc.c:625
int getNumCores(void)
An almost platform-independent function that returns the number of CPU cores on the current machine.
Definition: misc.c:69
void dostacktrace(const char *file, int line, FILE *ofp)
Print call stack.
Definition: misc.c:40
long long_first_geq(long val, long *v, long len)
Vector v must be sorted in ascending order before this function is called.
Definition: misc.c:223
int compareDoubles(const void *void_x, const void *void_y)
Compare two doubles.
Definition: misc.c:369
void tellopt(const char *opt, const char *description)
Describe an option.
Definition: misc.c:56
long double perturb_interval(long double x, long double lo, long double hi, gsl_rng *rng)
Uniform perturbation within interval.
Definition: misc.c:160
double sumDbl(int n, const double x[n])
Sum an array of doubles.
Definition: misc.c:393
double parseDbl(char *token)
Parse token as a double.
Definition: misc.c:561
void unitTstResult(const char *facility, const char *result)
Print result of a unit test.
Definition: misc.c:288
int compareLongs(const void *void_x, const void *void_y)
Compare two long ints.
Definition: misc.c:335
int readline(int dim, char buff[dim], FILE *fp)
Read a line of input into buff.
Definition: misc.c:679
char * strltrunc(char *s, int n)
Truncate string to n characters (plus terminating '\0') by removing characters from the left.
Definition: misc.c:587
int legalName(const char *name)
Return nonzero if name is legal; zero otherwise.
Definition: misc.c:714
unsigned long strhash(const char *ss)
Hash a character string using algorithm of Daniel J. Boorstein.
Definition: misc.c:440
char * nextWhitesepToken(char **str)
Return tokens separated by 1 or more spaces and/or tabs.
Definition: misc.c:519
void stutter(char c, int n, FILE *fp)
Print character c n times on output fp.
Definition: misc.c:798
int tokenize(int dim, char *token[dim], char *s, const char *delim)
Separate string s into tokens, separated by any character in string delim.
Definition: misc.c:532
const char * mybasename(const char *pathname)
Strip leading pathname components; return a pointer to the filename.
Definition: misc.c:703
int stripchr(char *s, int c)
Remove character c from string s. Return length of string.
Definition: misc.c:455
unsigned Dbl_first_geq(double val, unsigned len, double v[len])
Vector v must be sorted in ascending order before this function is called.
Definition: misc.c:189
int LDbl_near(long double x, long double y)
Return 1 if the relative difference between x and y is less than or equal to 8*DBL_EPSILON.
Definition: misc.c:791