26 int t = 0;
int s = 0;
int p = 0;
int x = 0;
27 int a = 0;
int d = 0;
int e = 0;
int ss = 0;
30 tree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK);
33 if (!tree)
error(
"Unable to parse input file");
39 node = mxmlGetParent(mxmlFindPath(tree,
"shibaInput/time"));
40 Times = atoi(mxmlElementGetAttr(node,
"n"));
41 if (!
Times)
error(
"time/@n not found in input");
43 node = mxmlGetParent(mxmlFindPath(tree,
"shibaInput/space"));
44 Spaces = atoi(mxmlElementGetAttr(node,
"n"));
47 node = mxmlGetParent(mxmlFindPath(tree,
"shibaInput/phylo"));
48 Phylos = atoi(mxmlElementGetAttr(node,
"n"));
51 node = mxmlGetParent(mxmlFindPath(tree,
"shibaInput/taxa"));
52 Taxa = atoi(mxmlElementGetAttr(node,
"n"));
53 if (!
Taxa)
error(
"taxa/@n not found in input");
57 node = mxmlGetParent(mxmlFindPath(tree,
"*/config/nStartSpaces"));
58 Cfg.nStartSpaces = atoi(mxmlGetOpaque(mxmlGetFirstChild(node)));
59 if (!
Cfg.nStartSpaces)
error(
"//config/nStartSpaces not found in input");
61 node = mxmlGetParent(mxmlFindPath(tree,
"*/config/maxRuns"));
62 Cfg.maxRuns = atoi(mxmlGetOpaque(mxmlGetFirstChild(node)));
63 if (!
Cfg.maxRuns)
error(
"//config/maxRuns not found in input");
66 node = mxmlGetParent(mxmlFindPath(tree,
"*/config/stopAfterSuccess"));
67 Cfg.stopAfterSuccess = atoi(mxmlGetOpaque(mxmlGetFirstChild(node)));
68 if (!
Cfg.stopAfterSuccess)
error(
"//stopAfterSuccess not found in input");
70 if (
Cfg.probSurvA == -1.0) {
71 node = mxmlGetParent(mxmlFindPath(tree,
"*/config/probSurvA"));
72 Cfg.probSurvA = atof(mxmlGetOpaque(mxmlGetFirstChild(node)));
73 if (!
Cfg.probSurvA)
error(
"//config/probSurvA not found in input");
76 if (
Cfg.probDispA == -1.0) {
77 node = mxmlGetParent(mxmlFindPath(tree,
"*/config/probDispA"));
78 Cfg.probDispA = atof(mxmlGetOpaque(mxmlGetFirstChild(node)));
79 if (!
Cfg.probDispA)
error(
"//config/probDispA not found in input");
82 node = mxmlGetParent(mxmlFindPath(tree,
"*/config/probSurvB"));
83 Cfg.probSurvB = atof(mxmlGetOpaque(mxmlGetFirstChild(node)));
84 if (!
Cfg.probSurvB)
error(
"//config/probSurvB not found in input");
86 node = mxmlGetParent(mxmlFindPath(tree,
"*/config/probDispB"));
87 Cfg.probDispB = atof(mxmlGetOpaque(mxmlGetFirstChild(node)));
88 if (!
Cfg.probDispB)
error(
"//config/probDispB not found in input");
110 if (mxmlGetElement(node))
113 if (!strcmp(mxmlGetElement(node),
"periodStart"))
115 if (t >=
Times)
error(
"Too many periodStart elements");
116 if (!mxmlGetOpaque(mxmlGetFirstChild(node))) \
117 error(
"A periodStart element is missing");
118 else RealTime[t] = atof(mxmlGetOpaque(mxmlGetFirstChild(node)));
127 error(
"Time sequence is not sorted in declining age");
129 if (!mxmlElementGetAttr(node,
"id"))
130 error(
"An id attr is missing in periodStart");
131 else asprintf(&TimeLabel[t],
"%s",
132 mxmlElementGetAttr(node,
"id"));
137 else if (!strcmp(mxmlGetElement(node),
"spaceName"))
139 if (s >=
Spaces)
error(
"Too many spaceName elements");
140 if (!mxmlGetOpaque(mxmlGetFirstChild(node)))
141 error(
"A spaceName datum is missing");
143 mxmlGetOpaque(mxmlGetFirstChild(node)));
144 if (!mxmlElementGetAttr(node,
"id"))
145 error(
"An id attr is missing in spaceName");
146 else asprintf(&SpaceLabel[s],
"%s",
147 mxmlElementGetAttr(node,
"id"));
152 else if (!strcmp(mxmlGetElement(node),
"newick"))
154 if (p >=
Phylos)
error(
"Too many newick elements");
155 if (!mxmlGetOpaque(mxmlGetFirstChild(node)))
156 error(
"A newick datum is missing");
160 char *tmp;
int tmpn = 0;
162 mxmlGetOpaque(mxmlGetFirstChild(node)));
163 for (
int i = 0; i < strlen(tmp); i++)
164 if ((tmp[i] != 32) &&
169 asprintf(&
Phylo[p],
"%c", tmp[i]); tmpn++;
176 if (!mxmlElementGetAttr(node,
"id"))
177 error(
"An id attr is missing in newick");
178 else asprintf(&PhyloLabel[p],
"%s",
179 mxmlElementGetAttr(node,
"id"));
184 else if (!strcmp(mxmlGetElement(node),
"taxon"))
186 if (x >=
Taxa)
error(
"Too many taxon elements");
187 if (!mxmlGetOpaque(mxmlGetFirstChild(node))) \
188 error(
"A taxon datum is missing");
189 else asprintf(&
Taxon[x],
"%s", \
190 mxmlGetOpaque(mxmlGetFirstChild(node)));
191 if (!mxmlElementGetAttr(node,
"id")) \
192 error(
"An id attr is missing in taxon");
193 else asprintf(&TaxonLabel[x],
"%s", \
194 mxmlElementGetAttr(node,
"id"));
199 else if (!strcmp(mxmlGetElement(node),
"area"))
204 if (!mxmlGetOpaque(mxmlGetFirstChild(node)))
205 error(
"An area datum is missing");
207 if (!mxmlElementGetAttr(node,
"time")) \
208 error(
"A time attr is missing in area");
212 for (
int i = 0; i <
Times; i++)
214 if (!strcmp(mxmlElementGetAttr(node,
"time"),
215 TimeLabel[i])) a_t = i;
220 "XML parse error: //area/@time '%s' not IDREF\n",
221 mxmlElementGetAttr(node,
"time"));
226 if (!mxmlElementGetAttr(node,
"space")) {
227 error(
"A space attr is missing in area");
230 for (
int i = 0; i <
Spaces; i++)
231 if (!strcmp(mxmlElementGetAttr(node,
"space"),
232 SpaceLabel[i])) a_s = i;
235 "XML parse error: //area/@space '%s' not IDREF\n",
236 mxmlElementGetAttr(node,
"space"));
241 Area[a_t][a_s] = atof(mxmlGetOpaque(mxmlGetFirstChild(node)));
246 else if (!strcmp(mxmlGetElement(node),
"dist"))
248 int d_t;
int d_s1;
int d_s2;
250 error(
"Too many dist elements");
252 if (!mxmlGetOpaque(mxmlGetFirstChild(node)))
253 error(
"An dist datum is missing");
255 if (!mxmlElementGetAttr(node,
"time")) \
256 error(
"A time attr is missing in dist");
260 for (
int i = 0; i <
Times; i++)
262 if (!strcmp(mxmlElementGetAttr(node,
"time"),
263 TimeLabel[i])) d_t = i;
268 "XML parse error: //dist/@time '%s' not IDREF\n",
269 mxmlElementGetAttr(node,
"time"));
274 if (!mxmlElementGetAttr(node,
"from"))
275 error(
"A from attr is missing in dist");
279 for (
int i = 0; i <
Spaces; i++)
281 if (!strcmp(mxmlElementGetAttr(node,
"from"),
282 SpaceLabel[i])) d_s1 = i;
287 "XML parse error: //dist/@from '%s' not IDREF\n",
288 mxmlElementGetAttr(node,
"from"));
293 if (!mxmlElementGetAttr(node,
"to"))
294 error(
"A to attr is missing in dist");
298 for (
int i = 0; i <
Spaces; i++)
300 if (!strcmp(mxmlElementGetAttr(node,
"to"),
301 SpaceLabel[i])) d_s2 = i;
306 "XML parse error: //dist/@to '%s' not IDREF\n",
307 mxmlElementGetAttr(node,
"to"));
312 Dist[d_t][d_s1][d_s2] =
313 atof(mxmlGetOpaque(mxmlGetFirstChild(node)));
314 Dist[d_t][d_s2][d_s1] =
315 atof(mxmlGetOpaque(mxmlGetFirstChild(node)));
320 else if (!strcmp(mxmlGetElement(node),
"extant"))
322 int e_x;
int e_t;
int e_s;
326 if (!mxmlElementGetAttr(node,
"taxon"))
327 error(
"A taxon attr is missing in extant");
331 for (
int i = 0; i <
Taxa; i++)
333 if (!strcmp(mxmlElementGetAttr(node,
"taxon"),
334 TaxonLabel[i])) e_x = i;
339 "XML parse error: //extant/@taxon '%s' not IDREF\n",
340 mxmlElementGetAttr(node,
"taxon"));
345 if (!mxmlElementGetAttr(node,
"time")) \
346 error(
"A time attr is missing in extant");
350 for (
int i = 0; i <
Times; i++)
352 if (!strcmp(mxmlElementGetAttr(node,
"time"),
353 TimeLabel[i])) e_t = i;
358 "XML parse error: //extant/@time '%s' not IDREF\n",
359 mxmlElementGetAttr(node,
"time"));
364 if (!mxmlElementGetAttr(node,
"space"))
365 error(
"A space attr is missing in extant");
369 for (
int i = 0; i <
Spaces; i++)
371 if (!strcmp(mxmlElementGetAttr(node,
"space"),
372 SpaceLabel[i])) e_s = i;
377 "XML parse error: //extant/@space '%s' not IDREF\n",
378 mxmlElementGetAttr(node,
"space"));
383 Extant[e_x][e_t][e_s] = 1;
390 else if (!strcmp(mxmlGetElement(node),
"allowed"))
393 if (ss >=
Spaces)
error(
"Too many startSpaces/allowed elements");
395 if (!mxmlElementGetAttr(node,
"space"))
396 error(
"A space attr is missing in allowed");
400 for (
int i = 0; i <
Spaces; i++)
402 if (!strcmp(mxmlElementGetAttr(node,
"space"),
403 SpaceLabel[i])) ss_s = i;
408 "XML parse error: //allowed/@space '%s' not IDREF\n",
409 mxmlElementGetAttr(node,
"space"));
418 node = mxmlWalkNext(node, tree, MXML_DESCEND);
423 if (t !=
Times)
error(
"Wrong number of periodStart elements");
424 if (s !=
Spaces)
error(
"Wrong number of spaceName elements");
425 if (p !=
Phylos)
error(
"Wrong number of newick elements");
426 if (x !=
Taxa)
error(
"Wrong number of taxon elements");
430 error(
"Wrong number of dist elements");
431 if (e < 1)
error(
"Need at least one extant taxon");
448 printf(
"# Data as read from %s\n",
DataFile);
450 printf(
"\n## Spaces:\n");
451 for (
int i = 0; i <
Spaces; i++) printf(
" %3d: %s\n", i,
SpaceName[i]);
453 printf(
"\n## Times:\n");
454 for (
int i = 0; i <
Times; i++) printf(
" %3d: %7.2f\n", i,
RealTime[i]);
456 printf(
"\n## Areas:\n");
458 for (
int i = 0; i <
Spaces; i++) printf(
" %6d", i);
460 for (
int i = 0; i <
Times; i++)
462 printf(
" Time %3d:", i);
463 for (
int j = 0; j <
Spaces; j++) printf(
" %6.2f",
Area[i][j]);
467 printf(
"\n## Distances:\n");
468 for (
int t = 0; t <
Times; t++)
470 printf(
" Time: %3d\n", t);
472 for (
int i = 0; i <
Spaces; i++) printf(
" %6d", i);
474 for (
int i = 0; i <
Spaces; i++)
476 printf(
" To %3d:", i);
477 for (
int j = 0; j <
Spaces; j++) printf(
" %6.1f",
Dist[t][i][j]);
482 printf(
"\n## Taxa:\n");
483 for (
int i = 0; i <
Taxa; i++) printf(
" %d: %s\n", i,
Taxon[i]);
485 printf(
"\n## Phylos:\n");
486 for (
int i = 0; i <
Phylos; i++) printf(
" %d: %s\n", i,
Phylo[i]);
488 printf(
"\n## LineagePeriods:\n");
489 printf(
" Lineage : ");
492 if ((i % 5) == 0) printf(
"|");
496 for (
int t = 0; t <
Times; t++)
498 printf(
" Period %3d: ", t);
506 printf(
"\n## LineageDaughters:\n");
507 for (
int i = 0; i <
Lineages; i++) {
508 printf(
" Lineage %3d: ", i);
515 printf(
"\n## ExtantTaxa:\n");
517 for (
int i = 0; i <
Spaces; i++) printf(
" %3d", i);
519 for (
int t = 0; t <
Times; t++)
521 for (
int i = 0; i <
Taxa; i++)
524 for (
int j = 0; j <
Spaces; j++)
525 if (
Extant[i][t][j]) isOne++;
528 printf(
" Time %3d, Taxon %3d:", t, i);
529 for (
int j = 0; j <
Spaces; j++) printf(
" %3d",
Extant[i][t][j]);
535 printf(
"\n## LineageExtant:\n");
537 for (
int i = 0; i <
Spaces; i++) printf(
" %3d", i);
539 for (
int t = 0; t <
Times; t++)
544 for (
int j = 0; j <
Spaces; j++)
548 printf(
" Time %3d, Lineage %3d:", t, i);
555 printf(
"\n## StartSpaces:\n");
557 for (
int i = 0; i <
Spaces; i++) printf(
" %3d", i);
559 printf(
" Allowed :");
563 printf(
"\n## Dispersal Function:\n");
564 printf(
" 0.0 0.5 1.0\n");
566 " +----+----+----+----+----+----+----+----+----+----+-> y (pDisp)\n");
567 for (
int i = 0; i <= 10; i++) {
568 double d = pDispersal((
double) i / 10.0);
569 if (!(i % 2)) printf(
" %3.1f +", (
double) i / 10.0);
571 for (
int j = 0; j < ((int) (d * 50.0)); j++) printf(
"*");
575 printf(
" v x (proportion of max distance)\n");
578 printf(
"\n## Survival Function:\n");
579 printf(
" 0.0 0.5 1.0\n");
581 " +----+----+----+----+----+----+----+----+----+----+-> y (pSurv)\n");
582 for (
int i = 0; i <= 10; i++) {
583 double s = pSurvival((
double) i / 10.0 );
584 if (!(i % 2)) printf(
" %3.1f +", (
double) i / 10.0);
586 for (
int j = 0; j < ((int) (s * 50.0)); j++) printf(
"*");
590 printf(
" v x (proportion of total land area)\n");
593 printf(
"\n## Config:\n");
594 printf(
" nStartSpaces : %10d\n",
Cfg.nStartSpaces);
595 printf(
" maxRuns : %10d\n",
Cfg.maxRuns);
596 printf(
" stopAfterSuccess : %10d\n",
Cfg.stopAfterSuccess);
597 printf(
" probSurvA : %10.2f\n",
Cfg.probSurvA);
598 printf(
" probSurvB : %10.2f\n",
Cfg.probSurvB);
599 printf(
" probDispA : %10.2f\n",
Cfg.probDispA);
600 printf(
" probDispB : %10.2f\n",
Cfg.probDispB);
614 printf(
"ERROR:\n %s\n", error_msg);
631 ptr = calloc(dimx,
sizeof(
double));
632 if (!ptr)
error(
"allocation failure in mem1d_d()");
646 ptr = calloc(dimx,
sizeof(
int));
647 if (!ptr)
error(
"allocation failure in mem1d_i()");
661 ptr = calloc(dimx,
sizeof(
int *));
662 if (!ptr)
error(
"allocation failure in mem2d1_i()");
672 for(
int i = 0; i < dimx; i++)
688 ptr = calloc(dimx,
sizeof(
char *));
689 if (!ptr)
error(
"allocation failure in mem2d1_c()");
702 for(
int i = 0; i < dimx; i++)
715 ptr = calloc(dimx,
sizeof(
int *));
716 if (!ptr)
error(
"allocation failure in mem2d_i() pass 1");
717 for(
int i = 0; i < dimx; i++)
719 ptr[i] = calloc(dimy,
sizeof(
int));
720 if (!ptr[i])
error(
"allocation failure in mem2d_i() pass 2");
725 void free2d_i(
int **ptr,
int dimx)
727 for(
int i = 0; i < dimx; i++)
739 ptr = calloc(dimx,
sizeof(
double *));
740 if (!ptr)
error(
"allocation failure in mem2d_d() pass 1");
741 for(
int i = 0; i < dimx; i++)
743 ptr[i] = calloc(dimy,
sizeof(
double));
744 if (!ptr[i])
error(
"allocation failure in mem2d_d() pass 2");
749 void free2d_d(
double **ptr,
int dimx)
751 for(
int i = 0; i < dimx; i++)
760 double***
mem3d_d(
int dimx,
int dimy,
int dimz)
763 ptr = calloc(dimx,
sizeof(
double **));
764 if (!ptr)
error(
"allocation failure in mem3d_d() pass 1");
765 for(
int i = 0; i < dimx; i++)
767 ptr[i] = calloc(dimy,
sizeof(
double *));
768 if (!ptr[i])
error(
"allocation failure in mem3d_d() pass 2");
769 for(
int j = 0; j < dimy; j++)
771 ptr[i][j] = calloc(dimz,
sizeof(
double));
772 if (!ptr[i][j])
error(
"allocation failure in mem3d_d() pass 3");
778 void free3d_d(
double ***ptr,
int dimx,
int dimy)
780 for(
int i = 0; i < dimx; i++)
782 for(
int j = 0; j < dimy; j++)
796 ptr = calloc(dimx,
sizeof(
int **));
797 if (!ptr)
error(
"allocation failure in mem3d_d() pass 1");
798 for(
int i = 0; i < dimx; i++)
800 ptr[i] = calloc(dimy,
sizeof(
int *));
801 if (!ptr[i])
error(
"allocation failure in mem3d_d() pass 2");
802 for(
int j = 0; j < dimy; j++)
804 ptr[i][j] = calloc(dimz,
sizeof(
int));
805 if (!ptr[i][j])
error(
"allocation failure in mem3d_d() pass 3");
811 void free3d_i(
int ***ptr,
int dimx,
int dimy)
813 for(
int i = 0; i < dimx; i++)
815 for(
int j = 0; j < dimy; j++)
826 char *version = VERSION;
827 printf(
"\n SHIBA (Simulated Historical Island Biogeography Analysis)\n");
828 printf(
" (c) Campbell Webb 2013; Version %s\n", version);
829 printf(
" Usage: shiba [ -dfhlpsv ]\n");
830 printf(
" Options:\n");
831 printf(
" -d NUM Use this value as the prob. of dispersal (probDispA)\n");
832 printf(
" -f FILE Use this file as input. Default file: shibaInput.xml\n");
833 printf(
" -h Print this help list\n");
834 printf(
" -l Print input data summary\n");
835 printf(
" -p INT Use this phylogeny (0...n). Default: 0\n");
836 printf(
" -s NUM Use this value as the prob. of survival (probSurvA)\n");
837 printf(
" -v Print highly verbose event descriptions\n\n");