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

A single segment of a population tree. More...

#include "binary.h"
#include "branchtab.h"
#include "comb.h"
#include "error.h"
#include "idset.h"
#include "idsetset.h"
#include "matcoal.h"
#include "misc.h"
#include "parstore.h"
#include "ptrlst.h"
#include "ptrptrmap.h"
#include "segment.h"
#include "setpart.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

Data Structures

struct  CombDat
 
struct  MigDat
 
struct  SetPartDat
 
struct  Segment
 

Functions

static IdSetSet ** get_descendants1 (int wdim, IdSetSet **w, int nsamples, tipId_t *sample, int *newdim)
 Return a newly-allocated array of IdSetSet objects. More...
 
static IdSetSet ** get_descendants2 (int dim0, IdSetSet **w0, int dim1, IdSetSet **w1, int nsamples, tipId_t *sample, int *newdim)
 
static void mv_idsets_to_parent (Segment *self, int ipar, PtrLst **a)
 
static void coalescent_interval_length (int n, long double elen[n - 1], long double eig[n], long double v)
 
static void project (int n, long double pr[n], long double eig[n - 1])
 
int visitComb (int d, int ndx[d], void *data)
 Visit a combination.
 
int visitSetPart (unsigned n, unsigned a[n], void *data)
 Visit a set partition. More...
 
int visitMig (int nmig, int *migndx, void *data)
 Visit a combination defining migrants. More...
 
static void unlink_child (Segment *child, Segment *parent)
 Remove child from parent.
 
static int Segment_coalesceFinite (Segment *self, int dosing, BranchTab *branchtab, long unsigned *event_counter)
 
static int Segment_coalesceInfinite (Segment *self, long double v, int dosing, BranchTab *branchtab)
 
static void Segment_duplicate_nodes (Segment *old, PtrPtrMap *ppm)
 Traverse tree, making a duplicate of each node, and putting the duplicates into a hash map (called ppm) in which the old node is the key and the new duplicate is the value associated with that key.
 
static int Segment_equals_r (Segment *a, Segment *b)
 
static int self_ndx (Segment *self, Segment *parent)
 
static int w_isempty (int dim, IdSetSet **w)
 Return 1 if each PtrLst in array w is empty; return 0 otherwise.
 
static void migrate (PtrLst *migrants, PtrLst *natives, PtrLst *sets, int nmig, int *migndx, int nnat, int *natndx, MigDat *mdat)
 On entry, "sets" is a vector of IdSet objects, which will be copied, and the copies allocated among migrants and natives. More...
 
static void mv_to_waiting_room (Segment *self, PtrLst *src, int ipar, int nlin)
 Move IdSet objects from list "src" into the relevant waiting room of parent ipar. More...
 
void Segment_print_d (Segment *self, const char *func, int line)
 
static int tipidcmp (const void *vx, const void *vy)
 This sorts tipId_t values into numerical order, unlike the more complex comparison function, compare_tipId, which is defined in lblndx.c.
 
static void Segment_print_r (Segment *self, FILE *fp, int indent)
 
void * Segment_new (int twoN_i, int start_i, ParStore *ps, const char *label)
 
void Segment_free (Segment *self)
 
void Segment_prune (Segment *self)
 Traverse network, removing segments with no children and no samples. More...
 
void Segment_update (Segment *self, ParStore *ps)
 
void Segment_unvisit (Segment *self)
 Set all "visited" flags to false.
 
void Segment_newSample (Segment *self, unsigned ndx)
 Add a new sample to a Segment. More...
 
int Segment_addChild (void *vparent, void *vchild)
 
void Segment_sanityCheck (Segment *self, const char *file, int lineno)
 Check sanity of Segment.
 
int Segment_mix (void *vchild, int mix_i, void *vintrogressor, void *vnative, ParStore *ps)
 
void * Segment_root (void *vself)
 Find root of population network, starting from given node.
 
int Segment_feasible (const Segment *self, Bounds bnd, int verbose)
 Return 1 if parameters satisfy inequality constraints, or 0 otherwise.
 
SegmentSegment_dup (Segment *old_root, PtrPtrMap *ppm)
 Duplicate a network of nodes, returning a pointer to the root of the duplicate network. More...
 
void Segment_print (void *vroot, FILE *fp, int indent)
 
int Segment_equals (Segment *a, Segment *b)
 
int Segment_coalesce (Segment *self, int dosing, BranchTab *branchtab, long unsigned *event_counter)
 
static void coalescent_interval_length (int n, long double elen[n], long double eig[n - 1], long double v)
 
void Segment_clear (Segment *self)
 Remove all references to samples from tree of populations. More...
 
int Segment_isClear (const Segment *self)
 Return 1 if Segment tree is empty of samples.
 

Variables

tipId_t union_all_samples
 
long double improbable
 
long double pr_ignored
 

Detailed Description

A single segment of a population tree.

Author
Alan R. Rogers

Function Documentation

◆ get_descendants1()

static IdSetSet ** get_descendants1 ( int  wdim,
IdSetSet **  w,
int  nsamples,
tipId_t *  sample,
int *  newdim 
)
static

Return a newly-allocated array of IdSetSet objects.

The i'th object contains all the IdSet entries in w[i], with each entry augmented by the entries (if any) in vector "sample". If there are no entries in any of the w[i], the returned array contains a single non-empty entry, with the entries of "samples". If "w" and "samples" are both empty, *newdim will equal 0 and the function returns NULL. Otherwise, *newdim is the dimension of the returned array.

On entry, wdim is the dimension of array w, and nsamples is the dimension of array sample. On return *newdim is the dimension of the newly-allocated array returned by the function.

◆ migrate()

static void migrate ( PtrLst migrants,
PtrLst natives,
PtrLst sets,
int  nmig,
int *  migndx,
int  nnat,
int *  natndx,
MigDat mdat 
)
static

On entry, "sets" is a vector of IdSet objects, which will be copied, and the copies allocated among migrants and natives.

Each copy will acquire a MigOutcome label, which is constructed using mig_event, mig_outcome, and mig_pr. Each set within "sets" should have the same length (number of tipId_t values), and that length should correspond to the indices in arrays "migndx" and "natndx". The first of these arrays holds the indices of the migrant tipId_t values within each set, and the second holds indices of natives. On return, "migrants" and "natives" contain IdSet objects of migrants and natives.

◆ mv_to_waiting_room()

static void mv_to_waiting_room ( Segment self,
PtrLst src,
int  ipar,
int  nlin 
)
static

Move IdSet objects from list "src" into the relevant waiting room of parent ipar.

"nlin" is the number of lineages in each IdSet object within "src". It also serves as the index into the parental waiting room. On return, "src" is empty, and self->parent[ipar]->w[iself][nlin] contains the IdSet objects that were originally in "src". Here, "iself" is the index of "self" in the parent's list of children.

◆ Segment_clear()

void Segment_clear ( Segment self)

Remove all references to samples from tree of populations.

Sets "visited" to 0 in every node.

◆ Segment_dup()

Segment * Segment_dup ( Segment old_root,
PtrPtrMap *  ppm 
)

Duplicate a network of nodes, returning a pointer to the root of the duplicate network.

On entry, ppm should be an empty hashmap.

References Segment_duplicate_nodes(), and Segment_unvisit().

◆ Segment_newSample()

void Segment_newSample ( Segment self,
unsigned  ndx 
)

Add a new sample to a Segment.

This is called once for each of the samples specified in the .lgo file.

◆ Segment_prune()

void Segment_prune ( Segment self)

Traverse network, removing segments with no children and no samples.

These do not contribute to the calculation.

References Segment_prune(), and unlink_child().

Referenced by Segment_prune().

◆ visitMig()

int visitMig ( int  nmig,
int *  migndx,
void *  data 
)

Visit a combination defining migrants.

migndx has the 0-based indices of the current set of migrants. Its length is nmig, which may be zero.

◆ visitSetPart()

int visitSetPart ( unsigned  n,
unsigned  a[n],
void *  data 
)

Visit a set partition.

n is the number of descendants, a[i] is the index of the ancestor of the i'th descendant.