shiba
 All Data Structures Files Functions Variables Macros Pages
shiba.h
Go to the documentation of this file.
1 // This software is distributed under the terms of the BSD 2-Clause License
2 // (see bottom of this file for full text)
3 
8 #define _GNU_SOURCE
9 
10 
13 #define Sasprintf(write_to, ...) { \
14  char *tmp_string_for_extend = (write_to); \
15  asprintf(&(write_to), __VA_ARGS__); \
16  free(tmp_string_for_extend); \
17  }
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h> // for getopt()
22 #include <ctype.h> // for isprint()
23 #include <math.h>
24 #include <pthread.h>
25 #include <string.h>
26 #include <time.h> // for time()
27 #include "version.h"
28 
29 // ------------------------ STRUCTURES ---------------------------
30 
32 typedef struct {
34  int *startSpace;
35  int nStartSpaces; //<! The number of starting spaces
36  int maxRuns;
37  int stopAfterSuccess;
38  double probSurvA;
39  double probSurvB;
40  double probDispA;
41  double probDispB;
42  int verbose;
43 } config;
44 
46 typedef struct {
47  int nnodes;
48  int *parent;
49  int *ndaughter;
50  int *depth;
51  double *bl;
52  int ultra;
53  double *age;
54 
55  char **taxon;
56 } phylo;
57 
58 // ------------------------ VARIABLES ---------------------------
59 
62 
63 int Times;
64 double *RealTime;
65 
66 int Spaces;
67 char **SpaceName;
68 
69 int Phylos;
70 char **Phylo;
71 
72 int Taxa;
73 char **Taxon;
74 
75 double **Area ;
76 double ***Dist ;
77 double **PSurv;
78 double ***PDisp;
79 int ***Extant;
80 
81 
82 
83 
84 int Lineages;
88 int ***LineageExtant;
90 
91 char *DataFile;
93 int PrintData;
94 
95 // #### these variables should be renamed and their scope restricted:
96 // The spatial location of each lineage at each time 1 or 0
97 //int ***locn; //[MAXLINEAGES][TIME][SPACE]; DELETE ME
98 int **record; // [MAXLINEAGES][SPACE]; remember this records node position - not final position
99 long int success;
100 long int topresent;
101 double maxdist;
102 double maxarea;
103 pthread_mutex_t mymutex;
104 int RUN_BATCH ;
105 
106 
107 // ------------------------ FUNCTIONS ---------------------------
108 // (documentation precedes the function definitions)
109 
110 void readArgs(int argc, char **argv);
111 void readXML();
112 void printIndata();
113 phylo parseNewick(char *in);
114 void phyloToLineage(phylo p);
115 void help();
116 void error(char *a);
117 
118 int* mem1d_i(int dimx);
119 double* mem1d_d(int dim);
120 
121 int** mem2d_i(int dimx, int dimy);
122 void free2d_i(int **ptr, int dimx);
123 int** mem2d1_i(int dimx);
124 void free2d1_i(int **ptr, int dimx);
125 double** mem2d_d(int dimx, int dimy);
126 void free2d_d(double **ptr, int dimx);
127 char** mem2d1_c(int dimx);
128 void free2d1_c(char **ptr, int dimx);
129 
130 double*** mem3d_d(int dimx, int dimy, int dimz);
131 void free3d_d(double ***ptr, int dimx, int dimy);
132 int*** mem3d_i(int dimx, int dimy, int dimz);
133 void free3d_i(int ***ptr, int dimx, int dimy);
134 
135 void shiba(phylo p);
136 double findMaxArea();
137 double findMaxDist();
138 void *biogeo();
139 void printSuccessAll(phylo p);
140 double pDispersal(double x);
141 double pSurvival(double x);
142 void printArray(int t);
143 
144 /* TODO: Allow speciation and dying out that still results in correct censored
145  * tree
146  * TODO: Test no of simulated occurrences, and adjust pDisp and pMort
147  * to optimize
148  */
149 
150 
151 /* Copyright (c) 2013, Campbell Webb
152  All rights reserved.
153 
154  Redistribution and use in source and binary forms, with or without
155  modification, are permitted provided that the following conditions
156  are met:
157 
158  * Redistributions of source code must retain the above copyright
159  notice, this list of conditions and the following disclaimer.
160  * Redistributions in binary form must reproduce the above copyright
161  notice, this list of conditions and the following disclaimer in
162  the documentation and/or other materials provided with the
163  distribution.
164 
165  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
167  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
168  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
169  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
170  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
171  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
172  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
173  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
174  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
175  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
176  OF THE POSSIBILITY OF SUCH DAMAGE.
177  */