-
Notifications
You must be signed in to change notification settings - Fork 0
/
im_clam.c
568 lines (481 loc) · 20.8 KB
/
im_clam.c
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
//im_clam.c --
// A. Kern 3/20/15
//
// Composite Likelihood estimation of canonical IM models via AFS
//going to use NLopt for optimization
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <nlopt.h>
#include "AFS.h"
#include "adkGSL.h"
#include "cs.h"
#include "time.h"
#include <unistd.h>
#include "adkCSparse.h"
#include "AFS_ctmc.h"
#include <slepcmfn.h>
#include "AFS_ctmc_petsc.h"
#include "im_clam.h"
void import2DSFSData(const char *fileName, gsl_matrix *obsData);
PetscInt n1, n2;
afsStateSpace *stateSpace, *reducedStateSpace;
clam_lik_params *currentParams, *nextParams;
gsl_matrix *transMat;
double *res;
double lowerBounds[5] = {0.01,0.01,0.0,0.0,0.001};
double upperBounds[5] = {10.0,10.0,20.0,20.0,10.0};
PetscBool vbse = PETSC_FALSE;
static char help[] = "im_clam\n\
Example: mpiexec -n <np> ./im_clam -s <stateSpace file> -m <mats file> -d <data file> \n\n\toptions:\n\
\t-exp expected value mode (requires -x flag too)\n\
\t-GIM uncertainty estimation mode (requires -x flag too)\n\
\t-mo multiple optimizations from different start points\n\
\t-global multi-level optimization (MLSL algo.)\n\
\t-x <theta_2, theta_A, mig12, mig21, t_div> parameter starting values\n\
\t-obs (prints out observed AFS as well as that expected from MLE params)\n\
\t-u mutation rate per base pair per generation (only used to unscale parameters; default 1e-8)\n\
\t-g generation time (gens/year; default 20)\n\
\t-seqLen sequence length scanned for polymorphisms (used to unscale parameter)\n\
\t-put upper bound for optimization of thetas\n\
\t-pum upper bound for optimization of migration rates\n\
\t-pudt upper bound for optimization of divergence time\n\
\t-r randomSeed\n\
\t-v verbose output\n";
int main(int argc, char **argv){
PetscInt i,j, N,runMode;
clock_t time1, time2;
PetscInt nnz;
PetscInt seed;
char filename[PETSC_MAX_PATH_LEN], filename2[PETSC_MAX_PATH_LEN], filename3[PETSC_MAX_PATH_LEN] ;
double lik = 0.0;
double mle[5] = {0.1,2,1,1,5};
const gsl_rng_type * T;
gsl_rng * r;
PetscErrorCode ierr;
PetscMPIInt rank,size;
PetscBool flg,obsFlag;
// Vec tmpVec;
PetscInt dim=5;
double snpNumber, pi_est, p, N0;
double u=1e-8;
double genPerYear=20;
double put = 10.0;
double pum = 20.0;
double pudt=10.0;
double propSnp;
PetscInt seqLen;
gsl_matrix *fi,*gi;
FILE *infile;
PetscInt testInt=0;
PetscScalar one = 1.0;
cs *ident;
///////////////////
///// PETSC / Slepc library version of this code
SlepcInitialize(&argc,&argv,(char*)0,help);
ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr);
ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size);CHKERRQ(ierr);
if(rank==0){
printf("\n.___ _____ .__\n| | / \\ ____ | | _____ _____\n| |/ \\ / \\ _/ ___\\| | \\__ \\ / \\\n| / Y \\ \\ \\___| |__/ __ \\| Y Y \\\n|___\\____|__ /____\\___ >____(____ /__|_| /\n \\/_____/ \\/ \\/ \\/\n\n\n");
printf("im_clam -- Isolation with Migration Composite Likelihood Analysis using Markov chains\n");
//printf("A.D. Kern 2015\n///////////////////\n");
printf("\n\n");
}
if(argc<2){
printf("%s",help);
exit(666);
}
time1=clock();
ierr = PetscOptionsGetString(NULL,"-s",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetString(NULL,"-m",filename2,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetString(NULL,"-d",filename3,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetInt(NULL,"-r",&seed,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetReal(NULL,"-u",&u,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetReal(NULL,"-g",&genPerYear,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetInt(NULL,"-seqLen",&seqLen,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetReal(NULL,"-put",&put,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetReal(NULL,"-pum",&pum,&flg);CHKERRQ(ierr);
ierr = PetscOptionsGetReal(NULL,"-pudt",&pudt,&flg);CHKERRQ(ierr);
//set upper bounds
upperBounds[0]=put;upperBounds[1]=put;upperBounds[2]=pum;upperBounds[3]=pum;upperBounds[4]=pudt;
if(!flg) seed=time(NULL);
//printf("rank:%d %d\n",rank,seed);
MPI_Bcast(&seed,1,MPI_INT,0,PETSC_COMM_WORLD);
//printf("rank:%d %d\n",rank,seed);
runMode = 1;
obsFlag=PETSC_FALSE;
//setup RNG for starting point for optimization
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
gsl_rng_set(r,seed);
for(i=0;i<dim;i++){
mle[i] = gsl_ran_flat(r,lowerBounds[i], upperBounds[i]);
}
PetscOptionsHasName(NULL,"-v",&flg); if(flg) vbse=PETSC_TRUE;
PetscOptionsHasName(NULL,"-obs",&flg); if(flg) obsFlag=PETSC_TRUE;
PetscOptionsHasName(NULL,"-exp",&flg); if(flg) runMode=2;
ierr = PetscOptionsGetRealArray(NULL,"-x",mle,&dim,&flg);CHKERRQ(ierr);
//double check we are set for exp mode
if(runMode == 2 && !flg){
printf("for exp runmode need to set parameter values using -x flag\n");
exit(1);
}
PetscOptionsHasName(NULL,"-mo",&flg); if(flg) runMode=3;
PetscOptionsHasName(NULL,"-global",&flg); if(flg) runMode=4;
PetscOptionsHasName(NULL,"-GIM",&flg); if(flg) runMode=5;
//import state space
stateSpace = afsStateSpaceImportFromFile(filename);
N = stateSpace->nstates;
//quick peak at the mats file to set nnz
//open file
infile = fopen(filename2, "r");
if (infile == NULL){
fprintf(stderr,"Error opening mats file! ARRRRR!!!!\n");
exit(1);
}
testInt=fscanf(infile,"nnz: %d", &nnz);
fclose(infile);
//got nnz, time for allocs
//setup currentParams struct; alloc all arrays
currentParams = malloc(sizeof(clam_lik_params));
currentParams->rng = r;
currentParams->n1 = n1 = stateSpace->states[0]->popMats[0]->size1 - 1;
currentParams->n2 = n2 = stateSpace->states[0]->popMats[1]->size2 - 1;
currentParams->stateSpace = stateSpace;
currentParams->rank = rank;
// currentParams->snpNumber = snpNumber;
currentParams->expAFS = gsl_matrix_alloc(n1+1,n2+1);
currentParams->expAFS2 = gsl_matrix_alloc(n1+1,n2+1);
currentParams->map = malloc(sizeof(PetscInt)*N);
currentParams->reverseMap = malloc(sizeof(PetscInt)*N);
currentParams->rates = gsl_vector_alloc(stateSpace->nstates);
currentParams->stateVec = gsl_vector_alloc(stateSpace->nstates);
currentParams->resVec = gsl_vector_alloc(stateSpace->nstates);
gsl_vector_set_zero(currentParams->resVec);
// currentParams->paramVector = gsl_vector_alloc(5);
// currentParams->paramCIUpper = gsl_vector_alloc(5);
// currentParams->paramCILower = gsl_vector_alloc(5);
// currentParams->mlParams = gsl_vector_alloc(5);
currentParams->top = malloc(sizeof(double) * nnz);
currentParams->topA = malloc(sizeof(double) * nnz);
currentParams->move = malloc(sizeof(PetscInt) * nnz);
currentParams->moveA = malloc(sizeof(PetscInt) * nnz);
// printf("allocated moveType...\n");
for(i=0;i< (nnz);i++){
currentParams->move[i]=0;
currentParams->top[i]=0;
currentParams->moveA[i]=0;
currentParams->topA[i]=0;
}
currentParams->dim1 = malloc(sizeof(PetscInt) * nnz);
currentParams->dim2 = malloc(sizeof(PetscInt) * nnz);
currentParams->dim1A = malloc(sizeof(PetscInt) * N * 10);
currentParams->dim2A = malloc(sizeof(PetscInt) * N * 10);
currentParams->b = malloc(N*sizeof(double));
currentParams->expoArray = malloc(N*sizeof(double));
currentParams->st = malloc(N*sizeof(double));
currentParams->paramVector = gsl_vector_alloc(5);
gsl_vector_set_all(currentParams->paramVector,1.0);
currentParams->fEvals=0;
currentParams->triplet = cs_spalloc(N, N, nnz + N , 1, 1); //alloc sparse mat with extra space for nonzero identity mats
ident = cs_spalloc(N,N,N,1,1);
for(i=0;i<N;i++) cs_entry(ident,i,i,1);
currentParams->eye = cs_compress(ident);
cs_spfree(ident);
//set up some petsc matrices//////////////////////////////////////////////////////////////
//
//
ierr = MatCreate(PETSC_COMM_WORLD,¤tParams->C);CHKERRQ(ierr);
// MatSetType(C,MATMPIAIJ);
ierr = MatSetSizes(currentParams->C, PETSC_DECIDE, PETSC_DECIDE,N,N);CHKERRQ(ierr);
ierr = MatSetFromOptions(currentParams->C);CHKERRQ(ierr);
// MatSetOption(currentParams->C, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
ierr = MatSetUp(currentParams->C);CHKERRQ(ierr);
ierr = MatCreateDense(PETSC_COMM_WORLD,PETSC_DECIDE, PETSC_DECIDE, N, N, NULL, ¤tParams->denseMat1);CHKERRQ(ierr);
ierr = MatSetFromOptions(currentParams->denseMat1);CHKERRQ(ierr);
MatAssemblyBegin(currentParams->denseMat1,MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(currentParams->denseMat1,MAT_FINAL_ASSEMBLY);
///////// Set up MFN ////////////////////////////////////////////////////////////////////////////////////////
ierr = MFNCreate(PETSC_COMM_WORLD,¤tParams->mfn);CHKERRQ(ierr);
ierr = MFNSetFunction(currentParams->mfn,SLEPC_FUNCTION_EXP);CHKERRQ(ierr);
ierr = MFNSetTolerances(currentParams->mfn,1e-07,PETSC_DEFAULT);CHKERRQ(ierr);
//mapping stateSpace to reducedSpace
currentParams->reducedStateSpace = afsStateSpaceNew();
afsStateSpaceMapAndReducePopn(currentParams->stateSpace, currentParams->map, currentParams->reducedStateSpace, currentParams->reverseMap);
currentParams->Na=currentParams->reducedStateSpace->nstates;
VecCreate(PETSC_COMM_WORLD,¤tParams->ancStateVec);
VecSetSizes(currentParams->ancStateVec,PETSC_DECIDE,currentParams->Na);
VecSetFromOptions(currentParams->ancStateVec);
VecDuplicate(currentParams->ancStateVec,¤tParams->ancResVec);
//set up more matrices
ierr = MatCreate(PETSC_COMM_WORLD,¤tParams->C2);CHKERRQ(ierr);
MatSetType(currentParams->C2,MATMPIAIJ);
ierr = MatSetSizes(currentParams->C2, PETSC_DECIDE, PETSC_DECIDE,currentParams->Na,currentParams->Na);CHKERRQ(ierr);
ierr = MatSetFromOptions(currentParams->C2);CHKERRQ(ierr);
ierr = MatSetUp(currentParams->C2);CHKERRQ(ierr);
ierr = MatCreateDense(PETSC_COMM_WORLD,PETSC_DECIDE, PETSC_DECIDE, currentParams->Na, currentParams->Na, NULL, ¤tParams->denseMat2);CHKERRQ(ierr);
ident = cs_spalloc(currentParams->Na,currentParams->Na,currentParams->Na,1,1);
for(i=0;i<currentParams->Na;i++) cs_entry(ident,i,i,1);
currentParams->eyeAnc = cs_compress(ident);
cs_spfree(ident);
//DTMC with PETSC /////////////
// ierr = MatCreate(PETSC_COMM_WORLD,¤tParams->D);CHKERRQ(ierr);
// ierr = MatSetSizes(currentParams->D, PETSC_DECIDE, PETSC_DECIDE,N,N);CHKERRQ(ierr);
// ierr = MatSetFromOptions(currentParams->D);CHKERRQ(ierr);
// ierr = MatSetUp(currentParams->D);CHKERRQ(ierr);
// ierr = MatCreate(PETSC_COMM_WORLD,¤tParams->D_copy);CHKERRQ(ierr);
// ierr = MatSetSizes(currentParams->D_copy, PETSC_DECIDE, PETSC_DECIDE,N,N);CHKERRQ(ierr);
// ierr = MatSetFromOptions(currentParams->D_copy);CHKERRQ(ierr);
// ierr = MatSetUp(currentParams->D_copy);CHKERRQ(ierr);
//sparse identity mat
// ierr = MatCreate(PETSC_COMM_WORLD,¤tParams->ident);CHKERRQ(ierr);
// ierr = MatSetSizes(currentParams->ident, PETSC_DECIDE, PETSC_DECIDE,N,N);CHKERRQ(ierr);
// ierr = MatSetFromOptions(currentParams->ident);CHKERRQ(ierr);
// ierr = MatSetUp(currentParams->ident);CHKERRQ(ierr);
// MatAssemblyBegin(currentParams->ident,MAT_FINAL_ASSEMBLY);
// MatAssemblyEnd(currentParams->ident,MAT_FINAL_ASSEMBLY);
// MatShift(currentParams->ident,one);
//dense identity mat
// ierr = MatCreateDense(PETSC_COMM_WORLD,PETSC_DECIDE, PETSC_DECIDE, N, N, NULL, ¤tParams->denseIdent);CHKERRQ(ierr);
// ierr = MatSetFromOptions(currentParams->denseIdent);CHKERRQ(ierr);
// MatAssemblyBegin(currentParams->denseIdent,MAT_FINAL_ASSEMBLY);
// MatAssemblyEnd(currentParams->denseIdent,MAT_FINAL_ASSEMBLY);
// MatShift(currentParams->denseIdent,one);
// VecCreate(PETSC_COMM_WORLD,¤tParams->xInv);
// VecSetSizes(currentParams->xInv,PETSC_DECIDE,N);
// VecSetFromOptions(currentParams->xInv);
// VecDuplicate(currentParams->xInv,¤tParams->bInv);
///////////////////////////////////////
//////////////////////////////
// setup done! ///////////////
////////////////////////////////////
//import mats
mcMatsImportFromFile(filename2, &nnz, currentParams->top, currentParams->move, currentParams->dim1, currentParams->dim2);
currentParams->nnz = nnz;
//init snpNumber to avoid compiler warning
snpNumber=0;
if(runMode != 2){
//alloc data matrix and import data; estimate pi and N1
currentParams->obsData = gsl_matrix_alloc(n1+1,n2+1);
import2DSFSData(filename3, currentParams->obsData);
snpNumber = gsl_matrix_sum(currentParams->obsData);
propSnp = (float) snpNumber / (float) seqLen;
// pi_est = 0.0;
// for(i=1;i<n1;i++){
// for(j=0;j<n2+1;j++){
// p = (float) i / n1;
// pi_est += (2 * p * (1.0 - p)) * gsl_matrix_get(currentParams->obsData,i,j);
// }
// }
// pi_est *= n1 / (n1 - 1) / snpNumber;
// N0=pi_est / u / 4;
}
////////////////////////////
time2=clock();
if(rank==0)printf("setup time:%f secs\n\n",(double) (time2-time1)/CLOCKS_PER_SEC);
time1=clock();
//expected AFS
//printf("runMode = %d\n",runMode);
switch(runMode){
case 1:
if(rank==0){
printf("\nParameter estimation run mode\n\n");
printf("now optimizing....\n\n");
printf("initial parameter guess:\n");
for(i=0;i<5;i++)printf("%f ",mle[i]);
printf("\n\n");
}
maximizeLikNLOpt(&lik, currentParams, mle);
fi = getGodambeInfoMatrix(mle, lik, currentParams);
//get N0 estimate
N0 = propSnp / currentParams->meanTreeLength / 4.0 / u;
if(rank == 0){
printf("for scaling:\nu: %e gen: %lf N0:%lf meanTreeLength:%lf seqLen:%d\n",u,genPerYear,N0,currentParams->meanTreeLength,seqLen);
printf("Composite Likelihood estimates of IM params (scaled by 1/theta_pop1):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<5;i++)printf("%f\t",(float)mle[i]);
printf("\n\nComposite Likelihood estimates of IM params (unscaled):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<2;i++)printf("%f\t",(float)mle[i]*N0);
for(i=2;i<4;i++)printf("%f\t",(float)mle[i]/N0/4);
printf("%f\t",(float)mle[i] * N0 * 4.0/ (float)genPerYear);
printf("\n\nUncertainty estimates of IM params (scaled by 1/theta_pop1):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<5;i++)printf("%f\t",(float) sqrt(gsl_matrix_get(fi,i,i)));
printf("\n\nUncertainty estimates of IM params (unscaled):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<2;i++)printf("%f\t",(float) sqrt(gsl_matrix_get(fi,i,i))*N0);
for(i=2;i<4;i++)printf("%f\t",(float) sqrt(gsl_matrix_get(fi,i,i))/N0/4);
printf("%f\t",(float)sqrt(gsl_matrix_get(fi,i,i)) * N0 * 4.0/ (float)genPerYear);
printf("\n");
printf("\n\nlikelihood: %lf\n",-lik);
currentParams->nnz = nnz;
printf("\nExpected AFS:\n");
gsl_matrix_prettyPrint(currentParams->expAFS);
}
break;
case 2:
if(rank == 0)printf("expected value run mode\n");
for(i=0;i<5;i++){
gsl_vector_set(currentParams->paramVector,i,mle[i]);
}
calcLogAFS_IM(currentParams);
currentParams->nnz = nnz;
if(rank == 0){
printf("Expected AFS:\n");
gsl_matrix_prettyPrint(currentParams->expAFS);
printf("parameter values used:\n");
for(i=0;i<dim;i++)printf("%f\t",gsl_vector_get(currentParams->paramVector,i));
printf("\n\n");
}
time2=clock();
//lik = calcLikNLOpt(5,mle,NULL,currentParams);
//
//printf("Likelihood: %g\n",lik);
//if(rank==0)printf("w/ CSPARSE time:%f secs\n Liklihood Func. Evals: %d\n",(double) (time2-time1)/CLOCKS_PER_SEC,currentParams->fEvals);
time1=clock();
break;
case 3:
if(rank==0){
printf("\nParameter estimation run mode with multiple optimizations\n\n");
printf("now optimizing....\n\n");
printf("initial parameter guess:\n");
for(i=0;i<5;i++)printf("%f ",mle[i]);
printf("\n\n");
}
for(j = 0; j < 3; j++){
for(i=0;i<5;i++)mle[i] = gsl_ran_flat(r,lowerBounds[i], upperBounds[i]);
printf("optimization %d initial parameter guess:\n",j);
for(i=0;i<5;i++)printf("%f ",mle[i]);
maximizeLikNLOpt(&lik, currentParams, mle);
if(rank == 0){
printf("Composite Likelihood estimates of IM params (scaled by 1/theta_pop1):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<5;i++)printf("%f\t",(float)mle[i]);
printf("\n\nlikelihood: %lf\n",-lik);
currentParams->nnz = nnz;
printf("\nExpected AFS:\n");
gsl_matrix_prettyPrint(currentParams->expAFS);
}
}
break;
case 4:
if(rank==0){
printf("\nParameter estimation run mode\n\n");
printf("now optimizing....\n\n");
printf("initial parameter guess:\n");
for(i=0;i<5;i++)printf("%f ",mle[i]);
printf("\n\n");
}
maximizeLikNLOpt_MLSL(&lik, currentParams, mle);
fi = getGodambeInfoMatrix(mle, lik, currentParams);
if(rank == 0){
N0 = propSnp / currentParams->meanTreeLength / 4.0 / u;
printf("Composite Likelihood estimates of IM params (scaled by 1/theta_pop1):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<5;i++)printf("%f\t",(float)mle[i]);
printf("\n\nComposite Likelihood estimates of IM params (unscaled):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<2;i++)printf("%f\t",(float)mle[i]*N0);
for(i=2;i<4;i++)printf("%f\t",(float)mle[i]/N0/4);
printf("%f\t",(float)mle[i] * N0 * 4.0/ (float)genPerYear);
printf("\n\nUncertainty estimates of IM params (scaled by 1/theta_pop1):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<5;i++)printf("%f\t",(float) sqrt(gsl_matrix_get(fi,i,i)));
printf("\n\nUncertainty estimates of IM params (unscaled):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<2;i++)printf("%f\t",(float) sqrt(gsl_matrix_get(fi,i,i))*N0);
for(i=2;i<4;i++)printf("%f\t",(float) sqrt(gsl_matrix_get(fi,i,i))/N0/4);
printf("%f\t",(float)sqrt(gsl_matrix_get(fi,i,i)) * N0 * 4.0/ (float)genPerYear);
printf("\n");
printf("\n\nlikelihood: %lf\n",-lik);
currentParams->nnz = nnz;
printf("\nExpected AFS:\n");
gsl_matrix_prettyPrint(currentParams->expAFS);
}
break;
case 5:
for(i=0;i<5;i++){
gsl_vector_set(currentParams->paramVector,i,mle[i]);
}
if(rank==0){
printf("\nUncertainty estimation (via Godambe Information) run mode\n\n");
printf("MLE parameters:\n");
for(i=0;i<5;i++)printf("%f ",mle[i]);
printf("\n");
}
lik = calcLikNLOpt(5,mle,NULL,currentParams);
gi = getGodambeInfoMatrix(mle, lik, currentParams);
N0 = propSnp / currentParams->meanTreeLength / 4.0 / u;
// if(rank==0){
// printf("FIM:\n");
// gsl_matrix_prettyPrint(fi);
// printf("\nGIM:\n");
// gsl_matrix_prettyPrint(gi);
// printf("\n");
// }
if(rank==0){
printf("\nlikelihood: %lf\n",-lik);
printf("Composite Likelihood estimates of IM params (scaled by 1/theta_pop1):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<5;i++)printf("%f\t",(float)mle[i]);
printf("\n\nComposite Likelihood estimates of IM params (unscaled):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<4;i++)printf("%f\t",(float)mle[i]*N0);
printf("%f\t",(float)mle[i] * N0 * 4.0/ (float)genPerYear);
printf("\n\nUncertainty estimates of IM params (scaled by 1/theta_pop1):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<5;i++)printf("%f\t",(float) sqrt(gsl_matrix_get(gi,i,i)));
printf("\n\nUncertainty estimates of IM params (unscaled):\n");
printf("theta_pop2\ttheta_anc\tmig_1->2\tmig_2->1\tt_div\n");
for(i=0;i<4;i++)printf("%f\t",(float) sqrt(gsl_matrix_get(gi,i,i))*N0);
printf("%f\t",(float)sqrt(gsl_matrix_get(gi,i,i)) * N0 * 4.0/ (float)genPerYear);
printf("\n");
}
break;
}
if(obsFlag && rank==0){
gsl_matrix_scale(currentParams->obsData,1.0/snpNumber);
printf("%f SNPs in dataset\n",snpNumber);
printf("Observed AFS:\n");
gsl_matrix_prettyPrint(currentParams->obsData);
gsl_matrix_scale(currentParams->obsData,snpNumber);
}
MatDestroy(¤tParams->denseMat1);
MatDestroy(¤tParams->denseMat2);
MatDestroy(¤tParams->C);
MatDestroy(¤tParams->C2);
MatDestroy(¤tParams->D);
MatDestroy(¤tParams->D_copy);
MatDestroy(¤tParams->denseIdent);
MatDestroy(¤tParams->ident);
MFNDestroy(¤tParams->mfn);
VecDestroy(¤tParams->ancStateVec);
VecDestroy(¤tParams->ancResVec);
VecDestroy(¤tParams->xInv);
VecDestroy(¤tParams->bInv);
gsl_rng_free (r);
afsStateSpaceFree(currentParams->reducedStateSpace);
time2=clock();
if(rank==0)printf("total run time:%f secs\n Liklihood Func. Evals: %d\n",(double) (time2-time1)/CLOCKS_PER_SEC,currentParams->fEvals);
ierr = PetscFinalize();
return(0);
}
//import2DSFSData -- imports a matrix from fileName and stuffs it in prealloc'd obsData
void import2DSFSData(const char *fileName, gsl_matrix *obsData){
FILE *infile;
//open file
infile = fopen(fileName, "r");
if (infile == NULL){
fprintf(stderr,"Error opening data file! ARRRRR!!!!\n");
exit(1);
}
gsl_matrix_fscanf(infile,obsData);
fclose(infile);
}