Legofit
infers population history from nucleotide site patterns.
Data Structures | Functions
branchtab.c File Reference

Hash table associating key (an unsigned int encoding a site pattern) and value (a double representing the length of the ascending branch) More...

#include "branchtab.h"
#include "misc.h"
#include <assert.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

Data Structures

struct  BranchTab
 

Functions

BranchTabBranchTab_new (unsigned nsamples)
 Create a new BranchTab with room for site patterns associated with data in which the number of samples is "nsamples".
 
BranchTabBranchTab_dup (const BranchTab *old)
 
int BranchTab_equals (const BranchTab *lhs, const BranchTab *rhs)
 Return 1 if two BranchTab objects are equal; 0 otherwise.
 
void BranchTab_free (BranchTab *self)
 Destructor for BranchTab.
 
int BranchTab_hasSingletons (BranchTab *self)
 Return 1 if BranchTab includes singleton site patterns.
 
long double BranchTab_get (BranchTab *self, tipId_t key)
 Return value corresponding to key.
 
void BranchTab_add (BranchTab *self, tipId_t key, long double value)
 Add a value to table.
 
unsigned BranchTab_size (BranchTab *self)
 Return the number of elements in the BranchTab.
 
void BranchTab_sanityCheck (BranchTab *self, const char *file, int line)
 
int BranchTab_divideBy (BranchTab *self, long double denom)
 Divide all values by denom. Return 0 on success, or 1 on failure.
 
void BranchTab_print (const BranchTab *self, FILE *fp)
 Print a BranchTab to standard output.
 
void BranchTab_plusEquals (BranchTab *lhs, BranchTab *rhs)
 Add each entry in table rhs to table lhs.
 
void BranchTab_minusEquals (BranchTab *lhs, BranchTab *rhs)
 Subtract each entry in table rhs from table lhs.
 
void BranchTab_toArrays (BranchTab *self, unsigned n, tipId_t key[n], long double value[n])
 Fill arrays key and value with values in BranchTab. More...
 
BranchTabBranchTab_collapse (BranchTab *old, tipId_t collapse)
 Return a pointer to a newly-allocated BranchTab, which is like "old", except that two or more of the populations in "old" are mapped into a single population in the new BranchTab.
 
BranchTabBranchTab_rmPops (BranchTab *old, tipId_t remove)
 Return pointer to newly-allocated BranchTab, which is like old, except that it lacks the populations indicated by the bits in "remove".
 
long double BranchTab_sum (const BranchTab *self)
 Return sum of values in BranchTab.
 
long double BranchTab_entropy (const BranchTab *self)
 Return negative of sum of p*ln(p)
 
int BranchTab_normalize (BranchTab *self)
 Divide all values by their sum. More...
 
BranchTabBranchTab_from_StrDblQueue (StrDblQueue *queue, LblNdx *lndx)
 Construct a BranchTab from a StrDblQueue. More...
 
long double BranchTab_KLdiverg (const BranchTab *obs, const BranchTab *expt)
 Calculate KL divergence from two BranchTab objects, which should be normalized before entering this function. More...
 
long double BranchTab_negLnL (const BranchTab *obs, const BranchTab *expt)
 Negative log likelihood. More...
 
void make_collapse_map (size_t n, tipId_t map[n], tipId_t collapse)
 Make map, an array whose i'th entry is an unsigned integer with one bit on and the rest off. More...
 
void make_rm_map (size_t n, tipId_t map[n], tipId_t remove)
 Make map, an array whose i'th entry is an unsigned integer with one bit on and the rest off. More...
 
tipId_t remap_bits (size_t n, tipId_t map[n], tipId_t old)
 

Detailed Description

Hash table associating key (an unsigned int encoding a site pattern) and value (a double representing the length of the ascending branch)

Author
Alan R. Rogers

Function Documentation

◆ BranchTab_from_StrDblQueue()

BranchTab * BranchTab_from_StrDblQueue ( StrDblQueue queue,
LblNdx lndx 
)

Construct a BranchTab from a StrDblQueue.

The LblNdx translates site patterns from string representation to integer representation. Function returns a newly-allocated BranchTab. If queue is NULL, function returns NULL.

References BranchTab_add(), BranchTab_new(), LblNdx_getTipId(), LblNdx_print(), and LblNdx_size().

◆ BranchTab_KLdiverg()

long double BranchTab_KLdiverg ( const BranchTab obs,
const BranchTab expt 
)

Calculate KL divergence from two BranchTab objects, which should be normalized before entering this function.

Use BranchTab_normalize to normalize. Function returns HUGE_VAL if there are observed values without corresponding values in expt.

References BranchTab_sum(), and LDbl_near().

Referenced by costFun().

◆ BranchTab_negLnL()

long double BranchTab_negLnL ( const BranchTab obs,
const BranchTab expt 
)

Negative log likelihood.

Multinomial model. lnL is sum across site patterns of x*log(p), where x is an observed site pattern count and p its probability.

References BranchTab_sum(), and LDbl_near().

Referenced by costFun().

◆ BranchTab_normalize()

int BranchTab_normalize ( BranchTab self)

Divide all values by their sum.

Return 0 on success, or 1 on failure.

References BranchTab_sum().

Referenced by costFun().

◆ BranchTab_toArrays()

void BranchTab_toArrays ( BranchTab self,
unsigned  n,
tipId_t  key[n],
long double  value[n] 
)

Fill arrays key and value with values in BranchTab.

On return, key[i] is the id of the i'th site pattern, and value[i] is the total branch length associated with that site pattern.

◆ make_collapse_map()

void make_collapse_map ( size_t  n,
tipId_t  map[n],
tipId_t  collapse 
)

Make map, an array whose i'th entry is an unsigned integer with one bit on and the rest off.

The on bit indicates the position in the new id of the i'th bit in the old id.

All bits equal to 1 in collapse are mapped to the minimum bit in collapse. All bits with positions below that of this mininum bit are mapped to their original position. Bits that are above the minumum bit and are off in collapse are shifted right by "shift" places, where shift is one less than the number of on bits in collapse that are to the right of the bit in question.

On entry, n equals the number of samples, map is an array of n tipId_t values, and collapse is an integer whose "on" bits represent the set of samples that will be collapsed into a single sample.

◆ make_rm_map()

void make_rm_map ( size_t  n,
tipId_t  map[n],
tipId_t  remove 
)

Make map, an array whose i'th entry is an unsigned integer with one bit on and the rest off.

The on bit indicates the position in the new id of the i'th bit in the old id.

All samples corresponding to an "on" bit in "remove" are deleted. Other samples (those not removed) have their bits right-shifted by a number of positions equal to the number of lower-order populations that have been removed.

On entry, n equals the number of samples, map is an array of n tipId_t values, and remove is an integer whose "on" bits represent the set of samples that will be removed.