File: misc.h

package info (click to toggle)
phast 1.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,412 kB
  • sloc: ansic: 54,180; makefile: 354; sh: 337; perl: 321
file content (811 lines) | stat: -rw-r--r-- 27,779 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/***************************************************************************
 * PHAST: PHylogenetic Analysis with Space/Time models
 * Copyright (c) 2002-2005 University of California, 2006-2010 Cornell
 * University.  All rights reserved.
 *
 * This source code is distributed under a BSD-style license.  See the
 * file LICENSE.txt for details.
 ***************************************************************************/

/**
   @file misc.h
   Miscellaneous definitions and functions: Argument handling, PDF/CDF, Distribution draws, Codon mapping, etc.
   @ingroup base
*/

#ifndef MISC_H
#define MISC_H

#include <math.h>
#include <stdio.h>
#include <matrix.h>
#include <ctype.h>
#include <lists.h>
#include <time.h>
#include <string.h>
#include <stdint.h>
#include <sys/time.h>
#include <external_libs.h>
struct hash_table;

#define TRUE 1
#define FALSE 0

/** Infinity, for various reasons, it's often useful to represent infinity and
   negative infinity as very large numbers */
#define INFTY 999999999
/** Negative Infinity, for various reasons, it's often useful to represent infinity and negative infinity as very large numbers */
#define NEGINFTY -INFTY

/** Threshold for log_sum function
    @see log_sum
 */
#define SUM_LOG_THRESHOLD -50

/** Shortcut for 2^x */
#define exp2(x) (pow(2,x))
/** Log base 2.  Negative input results in -inf */
#define log2(x) ((x) <= 0 ? NEGINFTY : log(x) / M_LN2)
/** Log base 10.  Negative input results in -inf */
#define log10(x) ((x) <= 0 ? NEGINFTY : log(x) / M_LN10)
/** Logit */
#define logit(x) ( 1 / (1 + exp(-(x))) )

/* CAREFUL: multiple eval! */
#ifndef F2C_INCLUDE
/** Max (if we don't have f2c) */
#define max(x, y) ((x) > (y) ? (x) : (y))
/** Min (if we don't have f2c) */
#define min(x, y) ((x) < (y) ? (x) : (y))
#endif

/** Convert integer to pointer */
#define int_to_ptr(i) ((void*) (intptr_t) (i))
/** Convert pointer to integer */
#define ptr_to_int(p) ((int) (intptr_t) (p))

/** Safe divide, checks for div by 0 so no arithmetic errors are thrown */
#define safediv(x, y) ((y) != 0 ? (x) / (y) : ((x) == 0 ? 0 : ((x) > 0 ? INFTY : NEGINFTY)))

/** Amino Acid alphabet */
#define AA_ALPHABET "ARNDCQEGHILKMFPSTWYV$"

/** Test if a base is Purine */
#define IS_PURINE(b) (toupper(b) == 'A' || toupper(b) == 'G')
/** Test if a base is Pyrimidine */
#define IS_PYRIMIDINE(b) (toupper(b) == 'C' || toupper(b) == 'T')

/** Raise integer to small integral power
    @param x Integer to raise
    @param y Power to raise to
    @result x^y
 */
static PHAST_INLINE
int int_pow(int x, int y) {
  int retval = 1, i;
  for (i = 0; i < y; i++) retval *= x;
  return retval;
}

/** \name Log based calculation functions
\{ */

/** Fast computation of floor(log2(x)), where x is a positive integer
    @param x Integer to take log2 of then floor
*/
static PHAST_INLINE
int log2_int(unsigned x) {
  int i;
  for (i = 0; ; i++) {
    x >>= 1;
    if (x == 0) return i;
  }
}

/** Efficiently compute log of sum of values.
   @param l List of doubles containing values
   @result log of sum of values passed in list
   @note Thanks to David Haussler for showing me this trick.
   @warning Sorts list as side effect.
*/
static PHAST_INLINE
double log_sum(List *l) {
  double maxval, expsum;
  int k;

  if (lst_size(l) > 1)
    lst_qsort_dbl(l, DESCENDING);

  maxval = lst_get_dbl(l, 0);
  expsum = 1;
  k = 1;

  while (k < lst_size(l) && lst_get_dbl(l, k) - maxval > SUM_LOG_THRESHOLD)
    expsum += exp2(lst_get_dbl(l, k++) - maxval);

  return maxval + log2(expsum);
}

/** Efficiently compute log (base e) of sum of values.
   @param l List of doubles containing values
   @result log (base e) of sum of values passed in list
   @note Thanks to David Haussler for showing me this trick.
   @warning Sorts list as side effect.
*/
static PHAST_INLINE
double log_sum_e(List *l) {
  double maxval, expsum;
  int k;

  if (lst_size(l) > 1)
    lst_qsort_dbl(l, DESCENDING);

  maxval = lst_get_dbl(l, 0);
  expsum = 1;
  k = 1;

  while (k < lst_size(l) && lst_get_dbl(l, k) - maxval > SUM_LOG_THRESHOLD)
    expsum += exp(lst_get_dbl(l, k++) - maxval);

  return maxval + log(expsum);
}

/** \} */

/** Compute relative entropy in bits of q with respect to p, both
   probability vectors of dimension d.
   @param q Probability distribution vector to compute relative entry of
   @param p Probability distribution vector with respect of
   @param d Dimension of probability vectors
 */
static PHAST_INLINE
double rel_entropy(double *p, double *q, int d) {
  int i;
  double H = 0;
  for (i = 0; i < d; i++) {
    if (p[i] == 0) continue;
    if (q[i] == 0) return INFTY;
    H += p[i] * (log2(p[i]) - log2(q[i]));
  }
  return H;
}

/** Compute symmetric relative entropy in bits of q with respect to p, both probability vectors of dimension d.
   @param q Probability distribution vector to compute relative entry of
   @param p Probability distribution vector with respect of
   @param d Dimension of probability vectors
 */
static PHAST_INLINE
double sym_rel_entropy(double *p, double *q, int d) {
  double re1 = rel_entropy(p, q, d), re2 = rel_entropy(q, p, d);
  return min(re1, re2);
}
#ifndef RPHAST
#if defined(__MINGW32__)
int random();
void srandom(int seed);
#endif
#endif
/** Specify the Random Number Generator seed number.
    @param seed Starting number for RNG
 */
void set_seed(int seed);

/** \name Combination & Permutation functions
\{ */


/** Return n! */
static PHAST_INLINE
int permutations(int n) {
  int i, retval = 1;
  for (i = 2; i <= n; i++) retval *= i;
  return retval;
}

/** Return n-choose-k */
static PHAST_INLINE
int combinations(int n, int k) {
  int i, retval = 1;
  for (i = 0; i < k; i++) retval *= (n - i);
  return retval / permutations(k);
}


/** Randomly choose k elements from a list of N.
    @param[in,out] selections Result array of size N to be populated with 0 (not chosen) or 1 (chosen). Elements initialized to -1 will be consitered "ineligable" and skipped.
    @param[in] N Number of elements to choose from
    @param[in] k Number of elements to choose
 */
void choose(int *selections, int N, int k);

/** Next combination (used for enumerating combinations).
    Call repeatedly to enumerate combinations.
    @param n Number of elements
    @param k Size of index array
    @param index Array preallocated to size k used to hold output. At end, contains indices in [0, n-1] representing the next element in the series of all possible combinations of n elements. On the first call, set indices[0] = -1 and the array will be initialized appropriately.
   @result TRUE on success, FALSE when no more choices possible
*/
int next_comb(int n, int k, int *index);

/** Produce a random permutation of the designated size.
    @param permutation Result array of size N to be populated with 0 to N-1 in a random order
    @param N Number of elements in the permutation
*/
void permute(int *permutation, int N);

/** \} */

/** Create map from Codons to Amino Acids.
    @param alphabet Possible characters in a sequence (assumed to contain the characters 'A', 'C', 'G',
   and 'T')
    @result Result char array of size (alphabet size)^3 containing codons mapped to corresponding amino acids according to the universal genetic code.
    @note Stop codons will be mapped to '$' characters
    @note Tuples not in {A,C,G,T}^3 will be mapped to null characters ('\0')
    @code
     char *alph = "ACGT";
     get_codon_mapping(alph);


//    Result  K N K N T TTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV$Y$YSSSS$CWCLFLF
//            | | | | | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//    Codons  a a a a a aaaaaaaaaaaccccccccccccccccggggggggggggggggtttttttttttttttt
//            a a a a c cccggggttttaaaaccccggggttttaaaaccccggggttttaaaaccccggggtttt
//            a c g t a cgtacgtacgtacgtacgtacgtacgtacgtacgtacgtacgtacgtacgtacgtacgt
//
//    AAA=K, AAC=N, AAG=K, AAT=N, ACA=T.... etc.

    @endcode
*/
char* get_codon_mapping(char *alphabet);

/** Translate a string tuple (for example "AAC") to an array index (43) for the 'codons' array used in get_codon_mapping.
   @param[in] tuple Three characters to map to array index
   @param[in] inv_alph Inverse alphabet that maps chars (as int) to indices of alphabet array
   @param[in] alph_size Size of the alphabet
   @result Array index used for 'codons' array OR -1 if char not in alphabet encountered
   @note A "digital" indexing scheme is assumed in which the right-most character in
         the tuple is considered the least significant digit.
   @note Inverse of get_tuple_str
   @see get_tuple_str
   @see get_codon_mapping
  */
int tuple_index(char *tuple, int *inv_alph, int alph_size);

/** Translate an array index (for example 43) from the 'codons' array used in get_codon_mapping to a string tuple ("AAC")
   @param[out] tuple_str String corresponding to tuple index
   @param[in] tuple_idx Index of tuple in Codons array
   @param[in] tuple_size Amount of chars in a tuple
   @param[in] alphabet Valid characters in a tuple
   @note Inverse of tuple_index
   @see tuple_index
*/
void get_tuple_str(char *tuple_str, int tuple_idx, int tuple_size,
                   char *alphabet);
/** Read a substitution matrix from the specified file.
   @param F File descriptor of file containing substitution matrix
   @param alph (Optional)  If 'alph' equals the empty string (must be preallocated to adequate
               size), then the sequence of characters that defines the matrix rows
               and columns will be stored in it; otherwise, 'alph' will be taken
               to define the desired alphabet, and the order of rows and columns
               will be rearranged as necessary to be consistent with it
   @note format expected to be that used by BLAST, as output by the NCBI "pam" program
   @note Characters in the file but not in 'alph' will be ignored
   @warning Only minor testing performed.  Check carefully if use
   without predefined alphabet or with alphabets that do not match in
   order

*/
Matrix* read_subst_mat(FILE *F, char *alph);

/** Open a file by filename and get file descriptor.
    @param fname Full path to file
    @param mode Open mode i.e. w, r, r+, w+, etc.
    @result File descriptor
    @note Exits with error message if open unsuccessful
 */
FILE* phast_fopen(const char *fname, const char *mode);

/** Open a file by filename and get file descriptor.
    @param fname Full path to file
    @param mode Open mode i.e. w, r, r+, w+, etc.
    @result File descriptor
 */
FILE* phast_fopen_no_exit(const char *fname, const char *mode);


void phast_fclose(FILE *f);
#ifdef RPHAST
#undef Rf_error
#undef die
#include <R.h>
#define die Rf_error
#define phast_warning Rf_warning
#undef printf
#define printf Rprintf
#undef stdout
#undef stderr
extern FILE *rphast_stdout;
extern FILE *rphast_stderr;
#define stdout rphast_stdout
#define stderr rphast_stderr

/** Write text to file or R stdout/stderr.
    @param f File descriptor of file to write to OR stdout to write to R console OR stderr to write to R error console.
    @param format Format of the string to write like sprintf
    @param ... Parameters like sprintf
    @result Success == 1
  */
int rphast_fprintf(FILE *f, const char *format, ...);
#undef fprintf
#define fprintf rphast_fprintf
#define checkInterrupt() R_CheckUserInterrupt()
#define checkInterruptN(i, n) if ((i)%(n) == 0) R_CheckUserInterrupt()

#else

/** Display a warning on the console.
    @param warnfmt Format of the string to write to console (like printf)
*/
void phast_warning(const char *warnfmt, ...);

/** Display info about unrecoverable error to console and end program.
    @param warnfmt Format of the string to write to console (like printf)
*/
void die(const char *warnfmt, ...);
#define checkInterrupt()
#define checkInterruptN(i,n)
#define unif_rand(void) (1.0*random()/RAND_MAX)
#endif

/** \name Program argument handling functions
\{ */
/** Parse comma separated value string or file reference into list.
   @param arg Comma separated value string or file reference (using the "*" convention)
   @result List of values from arg
   @note: List and all Strings are newly allocated (should be freed externally)
*/
List *get_arg_list(char *arg);

/** Returns remaining command-line arguments as a List of Strings.
  @param argv As passed to main
  @param argc As passed to main
  @param optind Index of first unprocessed argument
  @result List of strings containing unprocessed command line arguments
*/
List *remaining_arg_list(char *argv[], int argc, int optind);

/** Parse comma separated value string or file reference into list of integers.
    @param arg CSV list or file reference
    @result List of integers
*/
List *get_arg_list_int(char *arg);

/** Parse comma separated value string or file reference into list of integers.
    @param arg CSV list or file reference
    @result List of doubles
*/
List *get_arg_list_dbl(char *arg);

/** Argument conversion with error checking (int)
   @param arg String to convert to int
   @result Int parsed from string
 */
int get_arg_int(char *arg);

/** Argument conversion with error checking (double)
   @param arg String to convert to double
   @result Double parsed from string
 */
double get_arg_dbl(char *arg);

/** Argument conversion with error and bounds checking (int)
   @param arg String to convert to int
   @param min Minimum acceptable int
   @param max Maximum acceptable int
   @result Int parsed from string
*/
int get_arg_int_bounds(char *arg, int min, int max);

/** Argument conversion with error and bounds checking (double)
   @param arg String to convert to double
   @param min Minimum acceptable double
   @param max Maximum acceptable dobule
   @result Double parsed from string
*/
double get_arg_dbl_bounds(char *arg, double min, double max);

/** \} */

/** Safe malloc
    @param size Size of memory to allocate
*/
void *smalloc(size_t size);

/** Safe re-malloc
    @param ptr Pointer to memory to reallocate
    @param size New size
*/
void *srealloc(void *ptr, size_t size);

void register_open_file(FILE *F);
void unregister_open_file(FILE *F);

#ifdef USE_PHAST_MEMORY_HANDLER
/** Safe memory free
    @param ptr Pointer to object to free
*/
void sfree(void *ptr);
#else
#define sfree free
#endif

void set_static_var(void **ptr);

/** Copy a string
   @param word String to copy
   @result Word coppied by value
*/
char *copy_charstr(const char *word);

/** Normalize a probability vector.
   @pre All values nonnegative.
   @param p Probability vector to normalize
   @param size Number of elements in vector p
   @result Normalization constant
*/
double normalize_probs(double *p, int size);

/** Test if b1->b2 is a transition.
   @param b1 DNA base to start with
   @param b2 DNA base to end with
   @result 1 if a change from b1 to b2 is a transition, otherwise 0
 */
int is_transition(char b1, char b2);

/** Test if b1->b2 is an indel.
   @param b1 DNA base to start with
   @param b2 DNA base to end with
   @result 1 if a change from b1 to b2 involves an insertion/deletion, otherwise 0
 */
int is_indel(char b1, char b2);


/** Create HashTable from String.
    Parse string defining mapping from old names to new and store as hash
    @param Input String to translate to HashMap e.g. "hg17->human; mm5->mouse; rn3->rat"
    @result HashTable containing data parsed from string
    @note Can use "=" or "->" characters to indicate mapping.
*/
struct hash_table *make_name_hash(char *mapstr);

/** \name PDF/CDF functions
\{ */

/** Evaluate PDF of gamma distribution with parameters a and b
    @param x Gamma distributed variable
    @param a Shape parameter of gamma distribution
    @param b Rate parameter
    @result Evaluation of PDF of Gamma dist.
*/
double gamma_pdf(double x, double a, double b);

/** Evaluate CDF of gamma distribution with parameters a and b
    @param x Gamma distributed variable
    @param a Shape parameter of gamma distribution
    @param b Rate parameter of gamma distribution
    @param lower_tail If == TRUE returns P(X<=x), else returns P(X>=x)
    @result evaluation of CDF of Gamma dist.
*/
double gamma_cdf(double x, double a, double b, int lower_tail);

/** Evaluate pdf of chi-square distribution with dof degrees of freedom.
    @param x Chi-Square distributed variable
    @param dof Degrees of freedom
    @result Evaluation of pdf
*/
double chisq_pdf(double x, double dof);

/** Evaluate cdf of chi-square distribution with dof degrees of freedom.
    @param x Chi-Square distributed variable
    @param dof Degrees of freedom
    @param lower_tail If lower_tail == TRUE returns P(X<=x), else returns P(X>=x)
    @result Evaluation of cdf
*/
double chisq_cdf(double x, double dof, int lower_tail);

/** Evaluate cdf of a 50:50 mixture of a chisq distribution with dof
   degrees of freedom and a point mass at 0.
   @param x Chi-Square distributed variable
   @param dof Degrees of freedom
   @param lower_tail If lower_tail == TRUE returns P(X<=x), else returns P(X>=x)
   @note This function is useful in likelihood ratio tests of bounded parameters.
*/
double half_chisq_cdf(double x, double dof, int lower_tail);

/** \} \name Density evaluation functions
\{ */

/** Evaluate density of Beta distribution.
    @param x Beta distributed variable
    @param a Shape parameter
    @param b Shape parameter
    @result Density of Beta distribution
*/
double d_beta(double x, double a, double b);

/** Evaluate density of poisson distribution.
    @param lambda Expected number of occurrences during a given interval.
    @param k Number of occurrences of an event during a given interval we want to find the probability of
    @result Probability that k occurrences happen in a given interval, when we expect lambda occurrences during that interval
*/
double d_poisson(double lambda, int k);


/** Evaluate density of bivariate normal, p(x, y | mu_x, mu_y, sigma_x, sigma_y, rho).
   @param mu_x Marginal mean
   @param mu_y Marginal mean
   @param sigma_x Marginal standard deviation
   @param sigma_y Marginal standard deviations
   @param rho Correlation coefficient
   @result Estimate of bivariate normal density
*/
double bvn_p(double x, double y, double mu_x, double mu_y, double sigma_x,
             double sigma_y, double rho);

/** \} \name Distribution Draw functions
\{ */

/** Make a draw from a beta distribution with parameters 'a' and 'b'.
    @param a Shape parameter
    @param b Shape parameter
    @result Draw from distribution
*/
double beta_draw(double a, double b);

/** Make a draw from a k-dimensional Dirichlet distribution.
   @param k Dimensionality of distribution
   @param theta Scale of distribution
   @param alpha Distribution parameters given by alpha[0], ..., alpha[k-1].
   @result Draw from distribution
   @note This is accomplished by sampling from gamma
   distributions with parameters alpha[0], ..., alpha[k-1]
   and renormalizing.
 */
void dirichlet_draw(int k, double *alpha, double *theta);


/** Make 'n' draws from a uniform distribution on the interval [min,
   max], optionally with antithetics.
   @pre Call srandom
   @param[in] n Number of draws to make from uniform distribution
   @param[in] min Low end of interval to make draws on
   @param[in] max High end of interval to make draws on
   @param[out] draws Resulting draws
   @param[in] antithetics (Optional) If == 1 use antithetics
   @note Designed for use with real (floating-point) numbers.
 */
void unif_draw(int n, double min, double max, double *draws, int antithetics);

/** Make a draw from a binomial distribution with parameters 'N' and
   'p'.
   @pre Call srandom
   @param N Amount of samples in binomial distribution
   @param p Probability of each sample yielding success
   @result Number of successful samples
   @note Computational complexity is O(N) */
int bn_draw(int N, double p);

/** Make a draw from a binomial distribution with parameters 'n' and
   'pp'.
   @pre Call srandom
   @param N Amount of samples in binomial distribution
   @param pp Probability of occurrence of the event???
   @result Number of successful samples???
   @note Computational complexity is O(N)
   @note This version uses rejection sampling unless n < 25 or n * p <
   1/25.   */
int bn_draw_fast(int n, double pp);

/** Make 'n' draws from a multinomial distribution defined by
   probability vector 'p' with dimension 'd'.
   @pre Call srandom
   @param n Number of draws to make from multinomial distribution
   @param p Probability vector defining multinomial distribution
   @param d Dimensionality of multinomial distribution
   @param counts Number of counts for each category
   @note Sum of elements in 'counts' will equal 'n'.
   @note Probability vector is assumed to be normalized.
 */
void mn_draw(int n, double *p, int d, int *counts);

/** Make a draw from an exponential distribution with parameter
   (expected value) 'b'
   @param b Defines exponential distribution
   @result Draw from exponential distribution
*/
double exp_draw(double b);


/** Make a draw from a gamma distribution with parameters 'a' and 'b'.
   @pre Call srandom
   @param a Shape parameter of gamma distribution; If a == 1, exp_draw is
   called.  If a > 1, Best's (1978) rejection algorithm is used, and
   if a < 1, rejection sampling from the Weibull distribution is
   performed
   @param b Rate parameter of gamma distribution
   @result Draw from distribution
   @note Both algorithms used as described in "Non-Uniform
   Random Variate Generation" by Luc Devroye, available
   online at http://cg.scs.carleton.ca/~luc/rnbookindex.html
*/
double gamma_draw(double a, double b);

/** Given a probability vector, draw an index.
   @pre Call srandom externally
   @param p Probability vector
   @param size Number of elements in probability vector
   @result Index drawn from probability vector
*/
int draw_index(double *p, int size);


/** \} */



/** Incomplete Gamma function.
    @param a Shape of integrand
    @param x End of integral if type='p' or Beginning of integral if type='q'
    @param type 'p' means first half of integral, 'q' means second half (see p. 171)
    @result Integration result
 */
double incomplete_gamma(double a, double x, char type);

/** \name Cumulative distribution probability functions
 \{ */

/** Evaluate P(x <= k | lambda) (first 1/2 of integral)
    Return P(x <= k | lambda), for a variable x that obeys a Poisson
   distribution with parameter lambda
    @param lambda Expected number of occurrences during a given interval.
    @param k Number of occurrences of an event during a given interval we want to find the probability of
    @result Probability that k occurrences happen in or prior to a given interval, when we expect lambda occurrences during that interval
*/
double cum_poisson(double lambda, int k);

/** Evaluate P(x <= k | lambda) (last 1/2 of integral)
    Return P(x <= k | lambda), for a variable x that obeys a Poisson
   distribution with parameter lambda
    @param lambda Expected number of occurrences during a given interval.
    @param k Number of occurrences of an event during a given interval we want to find the probability of
    @result Probability that k occurrences happen in or prior to a given interval, when we expect lambda occurrences during that interval
*/
double cum_poisson_c(double lambda, int k);

/** Return P(x <= a | mu, sigma) for a variable a that obeys a normal ???
   distribution with mean mu and s.d. sigma.
   @param mu Mean of distribution
   @param sigma Standard deviation of distribution
   @param a Number of occurrences in a given interval
   @result Probability that 'a' occurrences will happen within or prior to a given interval
*/
double cum_norm(double mu, double sigma, double a);

/** Return P(x >= a | mu, sigma) for a variable a that obeys a normal ???
   distribution with mean mu and s.d. sigma.
   Use this function instead of 1-cum_norm when a is large (better precision)
   @param mu Mean of distribution
   @param sigma Standard deviation of distribution
   @param a Number of occurrences in a given interval
   @result Probability that 'a' occurrences happen in or after a given interval
*/
double cum_norm_c(double mu, double sigma, double a);

/** Return inverse of standard normal, i.e, inv_cum_norm(p) = a such
   that cum_norm(0, 1, a) = p.
   @param p Probability that 'a' inquiries happen during an interval ???
   @result Number of occurrences that relate to the specified probability
   @note The function is approximated using an algorithm by Peter Acklam
    given at http://home.online.no/~pjacklam/notes/invnorm/.
*/
double inv_cum_norm(double p);
/** \} */


/** Compute min and max of (central) confidence interval.
   @param mu Mean of normal distribution
   @param sigma Standard deviation of normal distribution
   @param interval_size Size of confidence interval (between 0 and 1)
   @param min_x Min of central confidence interval
   @param max_x Max of central confidence interval
  */
void norm_confidence_interval(double mu, double sigma, double interval_size,
                              double *min_x, double *max_x);

/** Save sequence in file as fasta format.
    @param F File descriptor to save sequence with
    @param seq Sequence chars to write out
    @param name Name of the sequence
    @param len Length of the sequence chars
*/
void print_seq_fasta(FILE *F, char *seq, char *name, int len);

/** Return time in seconds since start_time.
    @param timeval Struct used to get current time value
    @param start_time Time in the past that an event started at
    @result Number of seconds since the time defined in start_time
*/
double get_elapsed_time(struct timeval *start_time);

/** Check to see if a file is present and readable on the file system
  @param filename path of file to check for
  @result 1 if exists and readable
*/
int file_exists(char *filename);


/** \name IUPAC mapping functions
\{ */

/** Return a static mapping from IUPAC ambiguity characters to the bases
   that they represent.
   @code
   e.g.
   retval['R'] = "AG";
   retval['Y'] = "CT";
   @endcode
   @result Ambiguity characters -> bases map
*/
char **get_iupac_map();

/** Build an inverse mapping that allows an IUPAC ambiguity character
   to be mapped to an array, alph_size elements, with 1s or 0s
   indicating presence or absence of each character in the set
   associated with the IUPAC character.  This array obeys the indexing
   of the provided inv_states.  For use in computing likelihoods with
   ambiguity characters
   @param inv_states Inverse states array mapping states to unique numbers
   @param alph_size Number of entries in inv_states (size of alphabet)
   @code
   e.g.
      inv_states['A'] = 0
      inv_states['T'] = 1
      inv_states['G'] = 2
      inv_states['C'] = 3

      alph_size = 4
                       ATGC
      retval[(int)'R']  =  '1010'
      retval[(int)'Y']  =  '0101'
   @endcode
*/
int **build_iupac_inv_map(int *inv_states, int alph_size);

/** Free inverse iupac mapping object.
    @param iim Inverse IUPAC mapping object to free.
*/
void free_iupac_inv_map(int **iim);

/** \} \name 'n' Dimensional array functions
\{  */

/** Allocate an 'n' dimensional array.
    @param ndim Number of dimensions in array
    @param dimsize Array of int describing size of each dimension
    @param Size of 1 dimensional array
    @result Pointer to 'n' dimensional array
*/
void *alloc_n_dimensional_array(int ndim, int *dimsize, size_t size);

/** Free an 'n' dimensional array.
    @param Pointer to 'n' dimensional array
    @param ndim Number of dimensions in array
    @param dimsize Array of int describing size of each dimension
 */
void free_n_dimensional_array(void *data, int ndim, int *dimsize);

/** \} */

int get_nlines_in_file(FILE *F);
#endif