-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_file.c
732 lines (647 loc) · 19 KB
/
write_file.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/**
* @file write_file.c
* @author Arun Sethuraman
* @author Karin Dorman, kdorman@iastate.edu
* @date Wed Dec 5 20:44:34 CST 2012
*
* Functions to write results to output files.
*/
#include "multiclust.h"
/**
* Write data to file. Presumably, you call this function to write
* simulated data, and simulated data is in _data::ILM, so set last
* argument non-zero.
*
* @param opt options ojbect
* @param dat data object
* @param use_ILM write from dat->ILM (not dat->IL)
* @return error status
*/
int write_data(options *opt, data *dat, char const *outfile, int use_ILM)
{
int i, j, l, m, msum, r, r2;
int haplotype_no;
char *filename = NULL;
FILE *fp;
if (!outfile) {
int len = strlen(opt->path) + 7;
r2 = r = rand();
while (r2) {
len++;
r2 /= 10;
}
MAKE_1ARRAY(filename, len);
sprintf(filename, "%sbs%d.str", opt->path, r);
if ((fp = fopen(filename, "w")) == NULL) {
FREE_1ARRAY(filename);
return message(stderr, __FILE__, __func__, __LINE__, ERROR_MSG,
FILE_OPEN_ERROR, filename);
}
FREE_1ARRAY(filename);
} else if ((fp = fopen(outfile, "w")) == NULL) {
return message(stderr, __FILE__, __func__, __LINE__, ERROR_MSG,
FILE_OPEN_ERROR, outfile);
}
/* header line */
if (opt->output_format == STRUCTURE) {
for (l = 0; l < dat->L; l++)
fprintf(fp, "%sloc%d", l ? " " : "", l + 1);
fprintf(fp, "\n");
}
if (opt->output_format == PED)
opt->write_plus_one = 1;
if (!use_ILM) {
if (opt->output_format == STRUCTURE) {
for (i = 0; i < dat->I; ++i) {
haplotype_no = dat->ploidy * i;
for (j = 0; j < dat->ploidy; j++) {
if (dat->idv)
fprintf(fp, "%s %s",
dat->idv[i].name,
dat->pops[dat->idv[i].locale]);
else
fprintf(fp, "%d %d", i, i);
for (l = 0; l < dat->L; l++)
fprintf(fp, " %d",
opt->write_plus_one
? dat->IL[haplotype_no + j][l] + 1
: dat->IL[haplotype_no + j][l]);
fprintf(fp, "\n");
}
}
} else {
for (i = 0; i < dat->I; ++i ) {
haplotype_no = dat->ploidy * i;
if (dat->idv)
fprintf(fp, "%s %s 0 0 0 -9",
dat->idv[i].name,
dat->idv[i].name);
else
fprintf(fp, "%d %d 0 0 0 -9", i, i);
for (l = 0; l < dat->L; ++l) {
for (j = 0; j < dat->ploidy; ++j) {
fprintf(fp, " %d",
opt->write_plus_one
? dat->IL[haplotype_no + j][l] + 1
: dat->IL[haplotype_no + j][l]);
}
}
fprintf(fp, "\n");
}
}
} else {
for (i = 0; i < dat->I; i++) { /* individual */
for (j = 1; j <= dat->ploidy; j++) { /* haplotype */
fprintf(fp, "%s %s", dat->idv[i].name,
dat->pops[dat->idv[i].locale]);
for (l = 0; l < dat->L; l++) { /* locus */
/* skip ahead to next allele to print */
m = 0;
msum = dat->ILM[i][l][m];
while (msum < j)
msum += dat->ILM[i][l][++m];
if (dat->L_alleles)
fprintf(fp, " %d", opt->write_plus_one ?
dat->L_alleles[l][m] + 1 : dat->L_alleles[l][m]);
else
fprintf(fp, " %d", m);
}
fprintf(fp, "\n");
}
}
}
fclose(fp);
if (filename)
free(filename);
return NO_ERROR;
} /* End of write_data(). */
/**
* Write maximum likelihood parameter estimates to file.
*
* @param outfile name of output file
* @param opt options object
* @param dat data object
* @param mod model object
* @return error status
*/
int write_file(char* outfile, options* opt, data* dat, model* mod)
{
FILE* fp;
int i, k, l, m;
if ((fp = fopen(outfile, "w")) == NULL)
return message(stderr, __FILE__, __func__, __LINE__,
ERROR_MSG, FILE_OPEN_ERROR, outfile);
/* mixing proportions eta */
if (!opt->admixture || opt->eta_constrained) {
/* etak */
for (k = 0; k < mod->K; k++)
#ifndef OLDWAY
fprintf(fp, "%f ", mod->vetak[mod->pindex][k]);
#else
fprintf(fp, "%f ", mod->etak[k]);
#endif
fprintf(fp, "\n\n");
}
else {
/* etaik */
for (i = 0; i < dat->I; i++) {
for (k = 0; k < mod->K; k++)
#ifndef OLDWAY
fprintf(fp, "%f ", mod->vetaik[mod->pindex][i][k]);
#else
fprintf(fp, "%f ", mod->etaik[i][k]);
#endif
fprintf(fp, "\n");
}
fprintf(fp, "\n\n");
}
/* allele probabilities pKLM */
for (k = 0; k < mod->K; k++) {
for (l = 0; l < dat->L; l++) {
for (m = 0; m < dat->uniquealleles[l]; m++)
#ifndef OLDWAY
fprintf(fp, "%f ", mod->vpklm[mod->pindex][k][l][m]);
#else
fprintf(fp, "%f ", mod->pKLM[k][l][m]);
#endif
fprintf(fp, "\n");
}
fprintf(fp, "\n");
}
fclose(fp);
return NO_ERROR;
} /* End of write_file(). */
/**
* Write detailed output about fitted admixture model.
*
* @param opt options object
* @param dat data object
* @param mod model object
* @return error status
*/
int write_file_detail(options* opt, data* dat, model* mod)
{
FILE* fp;
int i, k, l, m;
int len; /* length of outfile name */
char* outfile = NULL; /* outfile name */
/* print maximum log likelihood, AIC, BIC, and partition counts */
if (opt->outfile_name != NULL) {
len = strlen(opt->outfile_name) + (int)(log(mod->K) / log(10)) + 21;
MAKE_1ARRAY(outfile, len);
sprintf(outfile, "%s.%s.K=%d.out.txt", opt->outfile_name,
opt->admixture ? "admix" : "mix", mod->K);
}
else {
/*
size_t base_idx = strlen(opt->filename);
while (base_idx-- > 0 && opt->filename[base_idx] != '/' && opt->filename[base_idx] != '\\');
if (base_idx || opt->filename[base_idx] == '/' || opt->filename[base_idx] == '\\')
++base_idx;
*/
/* compute length of output filename */
len = strlen(opt->path) + strlen(opt->filename_file) + //[base_idx]) +
+ (opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\')
+ (int)(log(mod->K) / log(10)) + 21;
MAKE_1ARRAY(outfile, len);
sprintf(outfile, "%s%s%s.%s.K=%d.out.txt", opt->path,
opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\' ? "/" : "",
opt->filename_file, //[base_idx],
opt->admixture ? "admix" : "mix", mod->K);
}
if ((fp = fopen(outfile, "w")) == NULL) {
int err_no = message(stderr, __FILE__, __func__, __LINE__, ERROR_MSG,
FILE_OPEN_ERROR, outfile);
FREE_1ARRAY(outfile);
return err_no;
}
fprintf(fp, "logL = %f (%s)\n", mod->logL,
mod->converged ? "converged" : "not converged");
fprintf(fp, "AIC = %f\n", aic(mod));
fprintf(fp, "BIC = %f\n\n", bic(dat, mod));
fprintf(fp, "count.K\n");
for (k = 0; k < mod->K; k++)
fprintf(fp, "%d ", mod->count_K[k]);
fprintf(fp, "\n\n");
fclose(fp);
/* print etaik */
if (!opt->admixture || opt->eta_constrained) {
if (opt->outfile_name != NULL) {
sprintf(outfile, "%s.%s.K=%d.etak.txt", opt->outfile_name,
opt->admixture ? "admix" : "mix", mod->K);
}
else {
sprintf(outfile, "%s%s%s.%s.K=%d.etak.txt", opt->path, //opt->filename
opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\' ? "/" : "",
opt->filename_file,
opt->admixture ? "admix" : "mix", mod->K);
}
if ((fp = fopen(outfile, "w")) == NULL)
return message(stderr, __FILE__, __func__, __LINE__,
ERROR_MSG, FILE_OPEN_ERROR, outfile);
fprintf(fp, "i\tk\tetak\n");
for (k = 0; k < mod->K; k++)
#ifndef OLDWAY
fprintf(fp, "%d\t%f\n", k, mod->vetak[mod->pindex][k]);
#else
fprintf(fp, "%d\t%f\n", k, mod->etak[k]);
#endif
fprintf(fp, "\n");
}
else {
if (opt->outfile_name != NULL) {
sprintf(outfile, "%s.%s.K=%d.etaik.txt", opt->outfile_name,
opt->admixture ? "admix" : "mix", mod->K);
}
else {
sprintf(outfile, "%s%s%s.%s.K=%d.etaik.txt", opt->path, //opt->filename,
opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\' ? "/" : "",
opt->filename_file,
opt->admixture ? "admix" : "mix", mod->K);
}
if ((fp = fopen(outfile, "w")) == NULL)
return message(stderr, __FILE__, __func__, __LINE__,
ERROR_MSG, FILE_OPEN_ERROR, outfile);
fprintf(fp, "i\tk\tetaik\n");
for (i = 0; i < dat->I; i++)
for (k = 0; k < mod->K; k++)
#ifndef OLDWAY
fprintf(fp, "%d\t%d\t%f\n", i, k, mod->vetaik[mod->pindex][i][k]);
#else
fprintf(fp, "%d\t%d\t%f\n", i, k, mod->etaik[i][k]);
#endif
fprintf(fp, "\n");
}
fclose(fp);
/* print KLM */
if (opt->outfile_name != NULL) {
sprintf(outfile, "%s.%s.K=%d.pklm.txt", opt->outfile_name,
opt->admixture ? "admix" : "mix", mod->K);
}
else {
sprintf(outfile, "%s%s%s.%s.K=%d.pklm.txt", opt->path, //opt->filename,
opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\' ? "/" : "",
opt->filename_file,
opt->admixture ? "admix" : "mix", mod->K);
}
if ((fp = fopen(outfile, "w")) == NULL)
return message(stderr, __FILE__, __func__, __LINE__,
ERROR_MSG, FILE_OPEN_ERROR, outfile);
fprintf(fp, "k\tl\tm\tKLM\n");
for (k = 0; k < mod->K; k++)
for (l = 0; l < dat->L; l++)
for (m = 0; m < dat->uniquealleles[l]; m++)
fprintf(fp, "%d\t%d\t%d\t%f\n", k, l, m,
#ifndef OLDWAY
mod->vpklm[mod->pindex][k][l][m]);
#else
mod->pKLM[k][l][m]);
#endif
fprintf(fp, "\n");
fclose(fp);
FREE_1ARRAY(outfile);
return NO_ERROR;
} /* End of write_file_detail(). */
/**
* Assign each individual to a posteriori most likely subpopulation. The
* admixture model allows alleles to arise from different subpopulations, so
* the assignment here is based on which subpopulation is the a posterior most
* probable source of the most alleles.
*
* was BUG: was doing a priori assignments based on estimated etaik
*
* @param dat data object
* @param mod model object
* @return void
*/
void partition_admixture(data* dat, model* mod)
{
int i, k, l, m, I_K;
double map;
double dik[mod->K];
for (k = 0; k < mod->K; k++)
mod->count_K[k] = 0;
for (i = 0; i < dat->I; i++) {
/* compute expected number of alleles in subpopulation k */
for (k = 0; k < mod->K; k++)
dik[k] = 0;
for (l = 0; l < dat->L; l++)
for (m = 0; m < dat->uniquealleles[l]; m++)
for (k = 0; k < mod->K; k++)
dik[k] += mod->diklm[i][k][l][m];
/* assign individual to subpopulation source of most alleles */
I_K = 0;
map = dik[0];
for (k = 1; k < mod->K; k++)
if (dik[k] > map) {
map = dik[k];
I_K = k;
}
/* assign to subpopulation I_K; increment I_K subpop. cnt */
dat->I_K[i] = I_K;
mod->count_K[I_K]++;
}
} /* End of partition_admixture(). */
/**
* Print POPQ file. This function prints out the POPQ file, as used by
* DISTRUCT/CLUMPP, under the admixture model.
*
* [TODO] Sorry, error handling is a really awkward aspect of array.h.
*
* @param opt options object
* @param dat data object
* @param mod model object
* @return error status
*/
#undef MAKE_1ARRAY
#define MAKE_1ARRAY MAKE_1ARRAY_ERR
int popq_admix(options* opt, data* dat, model* mod)
{
int i, k, l, m, n;
int len;
char* outfile = NULL;
FILE* fp;
double** vik_p = NULL;
int err = NO_ERROR;
k = mod->K;
while (k) {
len++;
k /= 10;
}
/* open file to write */
if (opt->outfile_name != NULL) {
len = strlen(opt->outfile_name) + 19 + (int) log10(mod->K);
MAKE_1ARRAY(outfile, len);
if ((err = errno))
goto FREE_AND_RETURN;
sprintf(outfile, "%s_admix_popq_%d.popq", opt->outfile_name,
mod->K);
}
else {
len = strlen(opt->path) + strlen(opt->filename_file) + 19 + (int) log10(mod->K);
MAKE_1ARRAY(outfile, len);
if ((err = errno))
goto FREE_AND_RETURN;
sprintf(outfile, "%s%s%s_admix_popq_%d.popq", opt->path,
opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\' ? "/" : "",
opt->filename_file, mod->K);
}
if ((fp = fopen(outfile, "w")) == NULL) {
err = message(stderr, __FILE__, __func__, __LINE__, ERROR_MSG,
FILE_OPEN_ERROR, outfile);
goto FREE_AND_RETURN;
}
/* compute post. prob. that locale n allele is in pop. k */
MAKE_2ARRAY(vik_p, dat->numpops, mod->K);
if ((err = errno))
goto FREE_AND_RETURN;
for (n = 0; n < dat->numpops; n++)
for (k = 0; k < mod->K; k++)
vik_p[n][k] = 0;
for (k = 0; k < mod->K; k++) {
for (i = 0; i < dat->I; i++)
for (l = 0; l < dat->L; l++)
/* was BUG: skipped missing data, but
* - missing data has info, and
* - missing alleles used to normalize below
*/
for (m = 0; m < dat->uniquealleles[l]; m++)
vik_p[dat->idv[i].locale][k]
+= mod->diklm[i][k][l][m];
for (n = 0; n < dat->numpops; n++)
vik_p[n][k] = vik_p[n][k]
/ (dat->ploidy * dat->L * dat->i_p[n]);
}
for (n = 0; n < dat->numpops; n++) {
fprintf(fp, "%s:\t", dat->pops[n]);
for (k = 0; k < mod->K; k++)
fprintf(fp, "%lf\t", vik_p[n][k]);
fprintf(fp, "%d\n", dat->i_p[n]);
}
fclose(fp);
FREE_AND_RETURN:
FREE_2ARRAY(vik_p);
FREE_1ARRAY(outfile);
return err;
} /* end of popq_admix(). */
#undef MAKE_1ARRAY
#define MAKE_1ARRAY MAKE_1ARRAY_RETURN
/**
* Print out INDIVQ file, as used by DISTRUCT/CLUMPP, under the admixture model.
*
* [TODO] Sorry, error handling is a really awkward aspect of array.h.
*
* @param opt options object
* @param dat data object
* @param mod model object
* @return error status
*/
#undef MAKE_1ARRAY
#define MAKE_1ARRAY MAKE_1ARRAY_ERR
int indivq_admix(options* opt, data* dat, model* mod)
{
int i, k, l, m;
int len;
char* outfile = NULL;
double** vik = NULL;
FILE* fp;
int err = NO_ERROR;
if (opt->outfile_name != NULL) {
len = strlen(opt->outfile_name) + 23 + (int) log10(mod->K);
MAKE_1ARRAY(outfile, len);
if ((err = errno))
goto FREE_AND_RETURN;
sprintf(outfile, "%s_admix_indivq_%d.indivq", opt->outfile_name, mod->K);
}
else {
len = strlen(opt->path) + strlen(opt->filename_file) + 23 + (int) log10(mod->K);
MAKE_1ARRAY(outfile, len);
if ((err = errno))
goto FREE_AND_RETURN;
sprintf(outfile, "%s%s%s_admix_indivq_%d.indivq", opt->path,
opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\' ? "/" : "",
opt->filename_file, mod->K);
}
if ((fp = fopen(outfile, "w")) == NULL) {
err = message(stderr, __FILE__, __func__, __LINE__, ERROR_MSG,
FILE_OPEN_ERROR, outfile);
goto FREE_AND_RETURN;
}
if (opt->eta_constrained || dat->missing_data) {
/* compute probability random allele from i from subpopn k */
MAKE_2ARRAY(vik, dat->I, mod->K);
if ((err = errno))
goto FREE_AND_RETURN;
for (k = 0; k < mod->K; k++)
for (i = 0; i < dat->I; i++)
vik[i][k] = 0;
for (k = 0; k < mod->K; k++)
for (i = 0; i < dat->I; i++) {
for (l = 0; l < dat->L; l++)
/* was BUG ignored missing as in popq_admix() */
for (m = 0; m < dat->uniquealleles[l]; m++)
vik[i][k] += mod->diklm[i][k][l][m];
vik[i][k] = vik[i][k] / (dat->ploidy * dat->L);
}
}
else {
#ifndef OLDWAY
vik = mod->vetaik[mod->pindex];
#else
vik = mod->etaik;
#endif
}
for (i = 0; i < dat->I; i++) {
fprintf(fp, "%d\t%s\t(x)\t%s\t:", i, dat->idv[i].name,
dat->pops[dat->idv[i].locale]);
for (k = 0; k < mod->K; k++)
fprintf(fp, "\t%f", vik[i][k]);
fprintf(fp, "\n");
}
fclose(fp);
FREE_AND_RETURN:
if (opt->eta_constrained || dat->missing_data)
FREE_2ARRAY(vik);
FREE_1ARRAY(outfile);
return err;
} /* end of indivq_admix(). */
#undef MAKE_1ARRAY
#define MAKE_1ARRAY MAKE_1ARRAY_RETURN
/**
* Assign individuals to subpopulations. Assign individuals to subpopulation
* based on Maximum A Posteriori (MAP) estimate, assuming flat prior.
*
* @param dat data object
* @param mod model object
* @return void
*/
void partition_mixture(data* dat, model* mod)
{
int i, k;
double tmp_vik;
for (k = 0; k < mod->K; k++)
mod->count_K[k] = 0;
for (i = 0; i < dat->I; i++) {
dat->I_K[i] = 0;
tmp_vik = mod->vik[i][0];
for (k = 1; k < mod->K; k++)
if (mod->vik[i][k] > tmp_vik) {
tmp_vik = mod->vik[i][k];
dat->I_K[i] = k;
}
mod->count_K[dat->I_K[i]]++;
}
} /* End of partition_mixture(). */
/**
* Print a popq file. Print data in a popq-style file under the mixture model
* as required by DISTRUCT/CLUMPP with sufficient statistics.
*
* [TODO] Sorry, error handling is a really awkward aspect of array.h.
*
* @param opt options object
* @param dat data object
* @param mod model object
* @return error status
*/
#undef MAKE_1ARRAY
#define MAKE_1ARRAY MAKE_1ARRAY_ERR
int popq_mix(options* opt, data* dat, model* mod)
{
int i, k, n, len;
char* outfile = NULL;
double** vik_p = NULL;
FILE* fp;
int err = NO_ERROR;
/* write popq file */
if (opt->outfile_name != NULL) {
/* compute length of output filename */
len = strlen(opt->outfile_name) + 15;
MAKE_1ARRAY(outfile, len);
if ((err = errno))
goto FREE_AND_RETURN;
sprintf(outfile, "%s_mix_popq.popq", opt->outfile_name);
}
else {
/* compute length of output filename */
len = strlen(opt->path) + strlen(opt->filename_file) + 15;
MAKE_1ARRAY(outfile, len);
if ((err = errno))
goto FREE_AND_RETURN;
sprintf(outfile, "%s%s%s_mix_popq.popq", opt->path,
opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\' ? "/" : "",
opt->filename_file);
}
if ((fp = fopen(outfile, "w")) == NULL) {
err = message(stderr, __FILE__, __func__, __LINE__,
ERROR_MSG, FILE_OPEN_ERROR, outfile);
goto FREE_AND_RETURN;
}
MAKE_2ARRAY(vik_p, dat->numpops, mod->K);
if ((err = errno))
goto FREE_AND_RETURN;
for (n = 0; n < dat->numpops; n++)
for (k = 0; k < mod->K; k++)
vik_p[n][k] = 0;
for (k = 0; k < mod->K; k++) /* population */
/* was BUG: access past end of array */
for (i = 0; i < dat->I; i++) /* individual */
vik_p[dat->idv[i].locale][k] += mod->vik[i][k];
/* normalize by the number of individuals in each locale */
for (n = 0; n < dat->numpops; n++)
for (k = 0; k < mod->K; k++)
vik_p[n][k] = vik_p[n][k] / dat->i_p[n];
for (n = 0; n < dat->numpops; n++) {
fprintf(fp, "%s:\t", dat->pops[n]);
for (k = 0; k < mod->K; k++)
fprintf(fp, "%lf\t", vik_p[n][k]);
fprintf(fp, "%d\n", dat->i_p[n]);
}
fclose(fp);
FREE_AND_RETURN:
FREE_2ARRAY(vik_p);
FREE_1ARRAY(outfile);
return err;
} /* end of popq_mix() */
#undef MAKE_1ARRAY
#define MAKE_1ARRAY MAKE_1ARRAY_RETURN
/**
* Print results in INDIVQ suitable for DISTRUCT/CLUMPP.
*
* @param opt options object
* @param dat data object
* @param mod model object
* @return error status
* @return error status
*/
int indivq_mix(options* opt, data* dat, model* mod)
{
int i, k, len;
char* outfile = NULL;
FILE* fp;
if (opt->outfile_name != NULL) {
len = strlen(opt->outfile_name) + 16 + (int) log10(mod->K);
MAKE_1ARRAY(outfile, len);
sprintf(outfile, "%s.mix.K=%d.indivq", opt->outfile_name, mod->K);
} else {
len = strlen(opt->path) + strlen(opt->filename_file) + 16 + (int) log10(mod->K);
MAKE_1ARRAY(outfile, len);
sprintf(outfile, "%s%s%s.mix.K=%d.indivq", opt->path,
opt->path && opt->path[strlen(opt->path) - 1] != '/' && opt->path[strlen(opt->path) - 1] != '\\' ? "/" : "",
opt->filename_file, mod->K);
}
if ((fp = fopen(outfile, "w")) == NULL) {
FREE_1ARRAY(outfile);
return message(stderr, __FILE__, __func__, __LINE__, ERROR_MSG,
FILE_OPEN_ERROR, outfile);
}
for (i = 0; i < dat->I; i++) {
fprintf(fp, "%d\t%s\t(x)\t%s\t:", i, dat->idv[i].name,
dat->pops[dat->idv[i].locale]);
for (k = 0; k < mod->K; k++)
fprintf(fp, "\t%f", mod->vik[i][k]);
fprintf(fp, "\n");
}
fclose(fp);
FREE_1ARRAY(outfile);
return NO_ERROR;
} /* end of indivq_mix() */