-
Notifications
You must be signed in to change notification settings - Fork 11
/
readdef.cc
1210 lines (1117 loc) · 46.2 KB
/
readdef.cc
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ped-sim: pedigree simulation tool
//
// This program is distributed under the terms of the GNU General Public License
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include "readdef.h"
// TODO: only use sexConstraints array when there are sex-specific maps?
// TODO: make branchNumSpouses positive
// Reads in the pedigree formats from the def file, including the type of the
// pedigree (full, half, or double) and the number of samples to produce in
// every generation
void readDef(vector<SimDetails> &simDetails, char *defFile) {
// open def file:
FILE *in = fopen(defFile, "r");
if (!in) {
printf("ERROR: could not open def file %s!\n", defFile);
perror("open");
exit(1);
}
// def file gives the number of samples to print; we store this in a 2d array
// with the first index being generation number and the second index the
// branch number
int **curNumSampsToPrint = NULL;
// Have variable number of branches in each generation
int *curNumBranches = NULL;
// Who are the parents of each branch in each generation?
// Contains <curNumGen> rows, and <2*curNumBranches[gen]> columns on each row.
// Stores the generation and branch numbers of the two parents.
// Negative branch values correspond to founders that are stored in the same
// branch number as the other parent.
Parent **curBranchParents = NULL;
// Gives numerical values indicating dependencies of sex assignments for each
// branch. For example, if the person in branch 1 has children with the
// individual in branch 2 and 3, branch 2 and 3 must have the same sex.
// Also stores the sexes of the i1 individuals when these are specified in
// the def file.
SexConstraint **curSexConstraints = NULL;
// If all i1 individuals are to have the same sex, the following gives its
// value. A value of -1 corresponds to random assignment.
// Branch-specific i1 specifications override this
int curI1Sex = -1;
// Counts of number of non-founder spouses for each generation/branch
int **curBranchNumSpouses = NULL;
int curNumGen = 0;
// for ensuring generations are in increasing order. This requirement arises
// from the fact that we assign the number of branches in each generation to
// be equal to the previous generation (except generation 2), so we need to
// know which generation we've assigned the generation numbers to and update
// branch counts for any generations that aren't explicitly listed.
int lastReadGen = -1;
// Tracks whether there has been an explicit assignment of the parents of
// each branch to avoid double assignments and giving default assignments.
vector<bool> branchParentsAssigned;
// Stores sets of individuals that are required to have the same and/or
// opposite sex assignments by virtue of their being spouses.
// Also, for each set, stores the sex (either 0 or 1 for male or female) of
// the set if this is specified for any member of a set (or their partners)
// in the def file
// (this value is temporary: its contents get put into <curSexConstraints>
// during processing)
vector< pair<set<Parent,ParentComp>,int8_t>* > spouseDependencies;
bool warningGiven = false;
size_t bytesRead = 1024;
char *buffer = (char *) malloc(bytesRead + 1);
if (buffer == NULL) {
printf("ERROR: out of memory");
exit(5);
}
const char *delim = " \t\n";
int line = 0;
while (getline(&buffer, &bytesRead, in) >= 0) {
line++;
char *token, *saveptr, *endptr;
token = strtok_r(buffer, delim, &saveptr);
if (token == NULL || token[0] == '#') {
// blank line or comment -- skip
continue;
}
if (strcmp(token, "def") == 0) {
if (spouseDependencies.size() > 0) {
// do some bookkeeping to finalize the previously read pedigree
int lastNumGen = curNumGen;
finishLastDef(lastNumGen, curSexConstraints, spouseDependencies);
}
/////////////////////////////////////////////////////////////////////////
// parse new pedigree description
char *name = strtok_r(NULL, delim, &saveptr);
char *numRepsStr = strtok_r(NULL, delim, &saveptr);
char *numGenStr = strtok_r(NULL, delim, &saveptr);
char *i1SexStr = strtok_r(NULL, delim, &saveptr);
// Note: leaving i1SexStr out of the next conditional because it can be
// NULL or non-NULL
if (name == NULL || numRepsStr == NULL || numGenStr == NULL ||
strtok_r(NULL, delim, &saveptr) != NULL) {
fprintf(stderr, "ERROR: line %d in def: expect four or five fields for pedigree definition:\n",
line);
fprintf(stderr, " def [name] [numReps] [numGen] <sex of i1>\n");
exit(5);
}
errno = 0; // initially
int curNumReps = strtol(numRepsStr, &endptr, 10);
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: expected number of replicates to simulate as second token\n",
line);
if (errno != 0)
perror("strtol");
exit(2);
}
curNumGen = strtol(numGenStr, &endptr, 10);
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: expected number of generations to simulate as third",
line);
fprintf(stderr, " token\n");
if (errno != 0)
perror("strtol");
exit(2);
}
if (i1SexStr == NULL)
curI1Sex = -1;
else {
if (strcmp(i1SexStr, "F") == 0) {
curI1Sex = 1;
}
else if (strcmp(i1SexStr, "M") == 0) {
curI1Sex = 0;
}
else {
fprintf(stderr, "ERROR: line %d in def: allowed values for sex of i1 field are 'M' and 'F'\n",
line);
fprintf(stderr, " got %s\n", i1SexStr);
exit(7);
}
}
// TODO: slow linear search to ensure lack of repetition of the pedigree
// names; probably fast enough
for(auto it = simDetails.begin(); it != simDetails.end(); it++) {
if (strcmp(it->name, name) == 0) {
fprintf(stderr, "ERROR: line %d in def: name of pedigree is same as previous pedigree\n",
line);
exit(5);
}
}
curNumSampsToPrint = new int*[curNumGen];
curNumBranches = new int[curNumGen];
curBranchParents = new Parent*[curNumGen];
curSexConstraints = new SexConstraint*[curNumGen];
curBranchNumSpouses = new int*[curNumGen];
if (curNumSampsToPrint == NULL || curNumBranches == NULL ||
curBranchParents == NULL || curSexConstraints == NULL ||
curBranchNumSpouses == NULL) {
printf("ERROR: out of memory");
exit(5);
}
if (lastReadGen >= 0)
lastReadGen = -1; // reset
for(int gen = 0; gen < curNumGen; gen++) {
// initially
curNumSampsToPrint[gen] = NULL;
// set to -1 initially so we know these are unassigned; will update
// later
curNumBranches[gen] = -1;
curBranchParents[gen] = NULL;
curSexConstraints[gen] = NULL;
curBranchNumSpouses[gen] = NULL;
}
simDetails.emplace_back(curNumReps, curNumGen, curNumSampsToPrint,
curNumBranches, curBranchParents,
curSexConstraints, curI1Sex,
curBranchNumSpouses, name);
continue;
}
///////////////////////////////////////////////////////////////////////////
// parse line with information about a generation in the current pedigree
// is there a current pedigree?
if (curNumSampsToPrint == NULL) {
fprintf(stderr, "ERROR: line %d in def: expect four or five fields for pedigree definition:\n",
line);
fprintf(stderr, " def [name] [numReps] [numGen] <sex of i1>\n");
exit(5);
}
char *genNumStr = token;
char *numSampsStr = strtok_r(NULL, delim, &saveptr);
char *branchStr = strtok_r(NULL, delim, &saveptr);
errno = 0; // initially
int generation = strtol(genNumStr, &endptr, 10);
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: expected generation number or \"def\" as first token\n",
line);
if (errno != 0)
perror("strtol");
exit(2);
}
if (numSampsStr == NULL) {
printf("ERROR: improper line number %d in def file: expected at least two fields\n",
line);
exit(5);
}
int numSamps = strtol(numSampsStr, &endptr, 10);
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: expected number of samples to print as second token\n",
line);
if (errno != 0)
perror("strtol");
exit(2);
}
if (generation < 1 || generation > curNumGen) {
fprintf(stderr, "ERROR: line %d in def: generation %d below 1 or above %d (max number\n",
line, generation, curNumGen);
fprintf(stderr, " of generations)\n");
exit(1);
}
if (numSamps < 0) {
fprintf(stderr, "ERROR: line %d in def: in generation %d, number of samples to print\n",
line, generation);
fprintf(stderr, " below 0\n");
exit(2);
}
if (generation == 1 && numSamps > 1) {
fprintf(stderr, "ERROR: line %d in def: in generation 1, if founders are to be printed must\n",
line);
fprintf(stderr, " list 1 as the number to be printed (others invalid)\n");
exit(2);
}
if (generation <= lastReadGen) {
fprintf(stderr, "ERROR: line %d in def: generation numbers must be in increasing order\n",
line);
exit(7);
}
// if <curNumBranches> != -1, have prior definition for generation.
// subtract 1 from <generation> because array is 0 based
if (curNumBranches[generation - 1] != -1) {
fprintf(stderr, "ERROR: line %d in def: multiple entries for generation %d\n",
line, generation);
exit(2);
}
// Will assign <numSamps> to each branch of this generation below -- first
// need to know how many branches are in this generation
// Assign number of branches (and parents) for generations that are not
// explicitly listed. In general the number of branches is equal to the
// number in the previous generation. The exceptions are generation 1 which
// defaults to 1 branch, and generation 2 which defaults to 2 branches when
// generation 1 has only 1 branch (otherwise it's assigned the same as the
// previous generation)
for(int i = lastReadGen + 1; i < generation - 1; i++) {
if (i == 0)
curNumBranches[0] = 1;
else if (i == 1 && curNumBranches[0] == 1)
curNumBranches[1] = 2;
else
curNumBranches[i] = curNumBranches[i-1];
// assign default parents for each branch:
if (i > 0)
assignDefaultBranchParents(curNumBranches[i-1], curNumBranches[i],
&(curBranchParents[i]), /*prevGen=*/i-1);
// assign default of 0 samples to print
curNumSampsToPrint[i] = new int[ curNumBranches[i] ];
if (curNumSampsToPrint[i] == NULL) {
printf("ERROR: out of memory");
exit(5);
}
for (int b = 0; b < curNumBranches[i]; b++)
curNumSampsToPrint[i][b] = 0;
}
int thisGenNumBranches;
if (branchStr != NULL) {
thisGenNumBranches = strtol(branchStr, &endptr, 10);
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: optional third token must be numerical value giving\n",
line);
fprintf(stderr, " number of branches\n");
if (errno != 0)
perror("strtol");
exit(2);
}
if (thisGenNumBranches <= 0) {
fprintf(stderr, "ERROR: line %d in def: in generation %d, branch number zero or below\n",
line, generation);
exit(2);
}
else {
curNumBranches[generation - 1] = thisGenNumBranches;
}
}
else {
if (generation - 1 == 0)
thisGenNumBranches = 1;
else if (generation - 1 == 1 && curNumBranches[0] == 1)
thisGenNumBranches = 2;
else
thisGenNumBranches = curNumBranches[generation-2];
curNumBranches[generation - 1] = thisGenNumBranches;
}
curNumSampsToPrint[generation - 1] = new int[thisGenNumBranches];
if (curNumSampsToPrint[generation - 1] == NULL) {
printf("ERROR: out of memory");
exit(5);
}
for(int b = 0; b < thisGenNumBranches; b++) {
curNumSampsToPrint[generation - 1][b] = numSamps;
}
lastReadGen = generation - 1;
// now read in and assign (if only using the defaults) the branch parents
// for this generation. Note that in the first generation, all individuals
// are necessarily founders so there should not be any specification.
if (generation - 1 > 0) {
bool warning = readBranchSpec(curNumBranches,
&curBranchParents[generation - 1],
curNumSampsToPrint[generation - 1],
/*curGen=*/generation - 1, curSexConstraints,
/*prevSpouseNum=*/&curBranchNumSpouses[generation - 2],
branchParentsAssigned, spouseDependencies, curI1Sex,
delim, saveptr, endptr, line);
warningGiven = warningGiven || warning;
}
else {
bool warning = readBranchSpec(curNumBranches,
/*thisGenBranchParents=NA=*/NULL,
curNumSampsToPrint[generation - 1],
/*curGen=*/generation - 1,
curSexConstraints,
/*prevGenSpouseNum=NA=*/NULL,
branchParentsAssigned, spouseDependencies,
curI1Sex, delim, saveptr, endptr, line);
warningGiven = warningGiven || warning;
}
}
// do some bookkeeping to finalize the previously read pedigree
int lastNumGen = curNumGen;
finishLastDef(lastNumGen, curSexConstraints, spouseDependencies);
for(auto it = simDetails.begin(); it != simDetails.end(); it++) {
bool someBranchToPrint = false;
bool anyNoPrint = false;
int lastGenNumBranches = it->numBranches[ it->numGen - 1 ];
for(int b = 0; b < lastGenNumBranches; b++) {
if (it->numSampsToPrint[ it->numGen - 1 ][b] == 0)
anyNoPrint = true;
else
someBranchToPrint = true;
}
if (!someBranchToPrint) {
fprintf(stderr, "ERROR: request to simulate pedigree \"%s\" with %d generations\n",
it->name, it->numGen);
fprintf(stderr, " but no request to print any samples from last generation (number %d)\n",
it->numGen);
exit(4);
}
else if (anyNoPrint) {
fprintf(stderr, "Warning: no-print branches in last generation of pedigree %s:\n",
it->name);
fprintf(stderr, " can omit these branches and possibly reduce number of founders needed\n");
}
}
if (simDetails.size() == 0) {
fprintf(stderr, "ERROR: def file does not contain pedigree definitions;\n");
fprintf(stderr, " nothing to simulate\n");
exit(3);
}
free(buffer);
fclose(in);
if (warningGiven)
fprintf(stderr, "\n");
}
void finishLastDef(int numGen, SexConstraint **&sexConstraints,
vector< pair<set<Parent,ParentComp>,int8_t>* > &spouseDependencies) {
// need to assign sex from spouse dependency sets to <sexConstraints>; also
// don't need to store the spouse dependency sets anymore:
for(unsigned int i = 0; i < spouseDependencies.size(); i++) {
if (spouseDependencies[i] != NULL) {
int8_t theSex = spouseDependencies[i]->second;
if (theSex >= 0) {
// assign the sexes of all samples in the spouseDependencies set to
// <theSex>
set<Parent,ParentComp> &toUpdate = spouseDependencies[i]->first;
for(auto it = toUpdate.begin(); it != toUpdate.end(); it++) {
assert(sexConstraints[ it->gen ][ it->branch ].theSex == -1 ||
sexConstraints[ it->gen ][ it->branch ].theSex == theSex);
sexConstraints[ it->gen ][ it->branch ].theSex = theSex;
}
}
delete spouseDependencies[i];
spouseDependencies[i] = NULL;
}
}
// TODO: can speed things by doing this, though note that it means that the
// results for a given random seed will differ relative to an earlier version
// of Ped-sim; therefore, will not do this, but may combine it with another
// feature that has this effect
// (Also, if this gets put in place, can remove the assignment of NULL just
// above.)
//spouseDependencies.clear();
}
// Gives the default parent assignment for any branches that have not had
// their parents explicitly specified.
void assignDefaultBranchParents(int prevGenNumBranches, int thisGenNumBranches,
Parent **thisGenBranchParents, int prevGen,
int *prevGenSpouseNum,
vector<bool> *branchParentsAssigned) {
// how many new branches is each previous branch the parent of?
int multFactor = thisGenNumBranches / prevGenNumBranches;
if (multFactor == 0)
multFactor = 1; // for branches that survive, map prev branch i to cur i
// allocate space to store the parents of each branch
if (*thisGenBranchParents == NULL) {
*thisGenBranchParents = new Parent[2 * thisGenNumBranches];
if (*thisGenBranchParents == NULL) {
printf("ERROR: out of memory");
exit(5);
}
}
for(int prevB = 0; prevB < prevGenNumBranches; prevB++) {
if (prevB >= thisGenNumBranches)
break; // defined all the branches for this generation
// same founder spouse for all <multFactor> branches that <prevB>
// is the parent of
int spouseNum = -1;
bool spouseNumDefined = false;
for(int multB = 0; multB < multFactor; multB++) {
int curBranch = prevB * multFactor + multB;
if (branchParentsAssigned && (*branchParentsAssigned)[curBranch])
continue; // skip assignment of branches that were assigned previously
(*thisGenBranchParents)[curBranch*2].gen = prevGen;
(*thisGenBranchParents)[curBranch*2].branch = prevB;
if (!spouseNumDefined) {
if (prevGenSpouseNum) {
prevGenSpouseNum[ prevB ]--;
spouseNum = prevGenSpouseNum[ prevB ];
}
else
spouseNum = -1;
spouseNumDefined = true;
}
(*thisGenBranchParents)[curBranch*2 + 1].gen = prevGen;
(*thisGenBranchParents)[curBranch*2 + 1].branch = spouseNum;
}
}
// For any branches in this generation that are not an exact multiple of
// the number of branches in the previous generation, make them brand new
// founders. They will contain exactly one person regardless of the number
// of samples to print
for(int newB = prevGenNumBranches * multFactor; newB < thisGenNumBranches;
newB++) {
if (branchParentsAssigned && (*branchParentsAssigned)[newB])
continue; // skip assignment of branches that were assigned previously
// undefined parents for excess branches: new founders
(*thisGenBranchParents)[newB*2].gen = prevGen;
(*thisGenBranchParents)[newB*2].branch =
(*thisGenBranchParents)[newB*2 + 1].branch = -1;
}
}
// Reads in and performs state changes for branch specifications including
// both parent assignments and no-print directives
// Returns true iff a warning has been printed
bool readBranchSpec(int *numBranches, Parent **thisGenBranchParents,
int *thisGenNumSampsToPrint, int curGen,
SexConstraint **sexConstraints, int **prevGenSpouseNum,
vector<bool> &branchParentsAssigned,
vector< pair<set<Parent,ParentComp>,int8_t>* > &spouseDependencies,
const int i1Sex, const char *delim, char *&saveptr,
char *&endptr, int line) {
bool warningGiven = false;
int prevGen = curGen - 1;
assert(curGen == 0 || *prevGenSpouseNum == NULL);
if (curGen > 0) {
*prevGenSpouseNum = new int[numBranches[prevGen]];
if (*prevGenSpouseNum == NULL) {
printf("ERROR: out of memory\n");
exit(5);
}
for(int b = 0; b < numBranches[prevGen]; b++)
// What number have we assigned through for founder spouses of
// individuals in the previous generation? Note that founders have an id
// (in the code for the purposes of <curBranchParents>) that are always
// negative and that by default we assign one founder spouse to marry one
// person in each branch in the previous generation (see
// assignDefaultBranchParents())
(*prevGenSpouseNum)[b] = 0;
*thisGenBranchParents = new Parent[ 2 * numBranches[curGen] ];
if (*thisGenBranchParents == NULL) {
printf("ERROR: out of memory\n");
exit(5);
}
if (sexConstraints[prevGen] == NULL) {
// if the previous generation didn't have any branch-specific sex
// assignments, need to allocate space for sex constraints (i.e., based
// on which branch i1 individual has children with which other branch i1)
sexConstraints[prevGen] = new SexConstraint[numBranches[prevGen]];
if (sexConstraints[prevGen] == NULL) {
printf("ERROR: out of memory\n");
exit(5);
}
initSexConstraints(sexConstraints[prevGen], numBranches[prevGen]);
}
// so far, all the branches in the current generation are assigned default
// parents; track which branches get explicitly assigned and throw an error
// if the same branch is assigned more than once
branchParentsAssigned.clear();
for(int i = 0; i < numBranches[curGen]; i++)
branchParentsAssigned.push_back(false);
}
while (char *assignToken = strtok_r(NULL, delim, &saveptr)) {
char *assignBranches = assignToken; // will add '\0' at ':'
char *fullAssignPar = NULL;
int i;
Parent pars[2];
// split on ':', 'n', or 's' to get the parent assignments/sexes on the
// right and the branches on the left
for(i = 0; assignToken[i] != ':' && assignToken[i] != 'n' &&
assignToken[i] != 's' && assignToken[i] != '\0'; i++);
// ':' gives parents (after the ':'); n means the preceding branches
// should not have their members printed; s means the following character
// ('M' or 'F') gives the sex of the i1 individual in the branches
bool parentAssign = false;
bool noPrint = false;
bool sexAssign = false;
int sexToAssign = -1;
if (assignToken[i] == ':')
parentAssign = true;
else if (assignToken[i] == 'n')
noPrint = true;
else if (assignToken[i] == 's')
sexAssign = true;
else {
fprintf(stderr, "ERROR: line %d in def: improperly formatted parent assignment, sex assignment\n",
line);
fprintf(stderr, " or no-print field %s\n", assignToken);
exit(8);
}
assignToken[i] = '\0';
if (curGen == 0 && parentAssign) {
fprintf(stderr, "ERROR: line %d in def: first generation cannot have parent specifications\n",
line);
exit(8);
}
// should have only one of these options:
assert(parentAssign || noPrint || sexAssign);
if (parentAssign) {
// Get the one or two parents
char *assignPar[2];
assignPar[0] = &(assignToken[i+1]); // will add '\0' at '_' if present
assignPar[1] = NULL; // initially; updated just below
readParents(numBranches, prevGen, sexConstraints, prevGenSpouseNum,
spouseDependencies, assignBranches, assignPar, pars,
fullAssignPar, i1Sex, endptr, line);
}
else if (noPrint) {
// expect a space after the 'n': check this
if (assignToken[i+1] != '\0') {
assignToken[i] = 'n';
fprintf(stderr, "ERROR: line %d in def: improperly formatted no-print field \"%s\":\n",
line, assignToken);
fprintf(stderr, " no-print character 'n' should be followed by white space\n");
exit(8);
}
}
else {
bool badField = false;
if (assignToken[i+1] == 'M')
sexToAssign = 0;
else if (assignToken[i+1] == 'F')
sexToAssign = 1;
else
badField = true;
if (badField || assignToken[i+2] != '\0') {
assignToken[i] = 's';
fprintf(stderr, "ERROR: line %d in def: improperly formatted sex assignment field \"%s\":\n",
line, assignToken);
fprintf(stderr, " character 's' should be followed either 'M' or 'F' and then white space\n");
exit(10);
}
}
// either we're not assigning a sex or <sexToAssign> is 0 or 1:
assert(!sexAssign || sexToAssign == 0 || sexToAssign == 1);
// process the branches
// if <parentAssign>, these will be assigned <pars> as parent OR
// if <noPrint>, these will not be printed OR
// if <sexAssign>, these will be assigned <sexToAssign> as their sex
bool done = false;
// the starting branch for a range (delimited by '-'); see below
char *startBranch = NULL;
while (!done) {
for(i = 0; assignBranches[i] != ',' && assignBranches[i] != '-' &&
assignBranches[i] != '\0'; i++);
if (assignBranches[i] == '-') { // have a range; just passed over start:
assignBranches[i] = '\0';
if (startBranch != NULL) {
fprintf(stderr, "ERROR: line %d in def: improperly formatted branch range \"%s-%s-\"\n",
line, startBranch, assignBranches);
exit(5);
}
startBranch = assignBranches;
assignBranches = &(assignBranches[i+1]); // go through next loop
}
else if (assignBranches[i] == ',' || assignBranches[i] == '\0') {
if (assignBranches[i] == '\0') {
done = true;
if (i == 0)
break; // early termination, will get error below
}
else
assignBranches[i] = '\0';
int curBranch = strtol(assignBranches, &endptr, 10) - 1; // 0 indexed
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: unable to parse branch %s to ",
line, assignBranches);
if (parentAssign)
fprintf(stderr, "assign parent %s to\n", fullAssignPar);
else if (noPrint)
fprintf(stderr, "set as no-print\n");
else // sexAssign
fprintf(stderr, "assign sex %c to\n",
(sexToAssign == 0) ? 'M' : 'F');
if (errno != 0)
perror("strtol");
exit(2);
}
if (startBranch) {
int rangeEnd = curBranch;
int rangeStart = strtol(startBranch, &endptr, 10) - 1; // 0 indexed
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: unable to parse branch %s to ",
line, startBranch);
if (parentAssign)
fprintf(stderr, "assign parent %s to\n", fullAssignPar);
else if (noPrint)
fprintf(stderr, "set as no-print\n");
else // sexAssign
fprintf(stderr, "assign sex %c to\n",
(sexToAssign == 0) ? 'M' : 'F');
if (errno != 0)
perror("strtol");
exit(2);
}
startBranch = NULL; // parsed: reset this variable
if (rangeStart >= rangeEnd) {
fprintf(stderr, "ERROR: line %d in def: non-increasing branch range %d-%d to\n",
line, rangeStart, rangeEnd);
if (parentAssign)
fprintf(stderr, " assign parent %s to\n", fullAssignPar);
else if (noPrint)
fprintf(stderr, " set as no-print\n");
else // sexAssign
fprintf(stderr, " assign sex %c to\n",
(sexToAssign == 0) ? 'M' : 'F');
exit(8);
}
if (rangeEnd >= numBranches[curGen]) {
fprintf(stderr, "ERROR: line %d in def: request to assign a branch greater than %d, the total\n",
line, numBranches[curGen]);
fprintf(stderr, " number of branches in generation %d\n", curGen + 1);
exit(11);
}
for(int branch = rangeStart; branch <= rangeEnd; branch++) {
assignBranch(parentAssign, noPrint, sexToAssign, curGen, branch,
sexConstraints, thisGenBranchParents,
thisGenNumSampsToPrint, numBranches[curGen],
branchParentsAssigned, pars, line, warningGiven);
}
}
else {
if (curBranch >= numBranches[curGen]) {
fprintf(stderr, "ERROR: line %d in def: request to assign a branch greater than %d, the total\n",
line, numBranches[curGen]);
fprintf(stderr, " number of branches in generation %d\n", curGen + 1);
exit(11);
}
assignBranch(parentAssign, noPrint, sexToAssign, curGen, curBranch,
sexConstraints, thisGenBranchParents,
thisGenNumSampsToPrint, numBranches[curGen],
branchParentsAssigned, pars, line, warningGiven);
}
assignBranches = &(assignBranches[i+1]); // go through next in loop
}
}
if (startBranch != NULL) {
fprintf(stderr, "ERROR: line %d in def: range of branches ", line);
if (parentAssign)
fprintf(stderr, "to assign parents ");
else if (noPrint)
fprintf(stderr, "set as no-print ");
else // sexAssign
fprintf(stderr, "assign sex %c to\n", (sexToAssign == 0) ? 'M' : 'F');
fprintf(stderr, "does not terminate\n");
exit(8);
}
}
// TODO: As a space optimization, could compress the spouseDependencies
// vector more. When combining two sets, the indexes of one of the set
// are no longer used, but the size of the spouseDependencies list
// still accounts for them.
assert(spouseDependencies.size() % 2 == 0);
if (curGen > 0)
assignDefaultBranchParents(numBranches[prevGen], numBranches[curGen],
thisGenBranchParents, prevGen, *prevGenSpouseNum,
&branchParentsAssigned);
return warningGiven;
}
void assignBranch(bool parentAssign, bool noPrint, int sexToAssign, int curGen,
int branch, SexConstraint **sexConstraints,
Parent **thisGenBranchParents, int *thisGenNumSampsToPrint,
int thisGenNumBranches, vector<bool> &branchParentsAssigned,
Parent pars[2], int line, bool &warningGiven) {
if (parentAssign) {
if (branchParentsAssigned[branch]) {
fprintf(stderr, "ERROR: line %d in def: parents of branch number %d assigned multiple times\n",
line, branch+1);
exit(8);
}
branchParentsAssigned[branch] = true;
for(int p = 0; p < 2; p++)
(*thisGenBranchParents)[branch*2 + p] = pars[p];
}
else if (noPrint) {
// print 0 samples for <branch>
if (thisGenNumSampsToPrint[branch] > 1) {
fprintf(stderr, "Warning: line %d in def: generation %d would print %d individuals, now set to 0\n",
line, curGen + 1, thisGenNumSampsToPrint[branch]);
warningGiven = true;
}
else if (thisGenNumSampsToPrint[branch] == 0) {
fprintf(stderr, "Warning: line %d in def: generation %d branch %d, no-print is redundant\n",
line, curGen + 1, branch + 1);
warningGiven = true;
}
thisGenNumSampsToPrint[branch] = 0;
}
else { // sexAssign
if (sexConstraints[curGen] == NULL) {
// make space for sex assignments
sexConstraints[curGen] = new SexConstraint[thisGenNumBranches];
if (sexConstraints[curGen] == NULL) {
printf("ERROR: out of memory\n");
exit(5);
}
initSexConstraints(sexConstraints[curGen], thisGenNumBranches);
}
else if (sexConstraints[curGen][branch].theSex != -1) {
fprintf(stderr, "ERROR: line %d in def: sex of branch number %d assigned multiple times\n",
line, branch+1);
exit(8);
}
sexConstraints[curGen][branch].theSex = sexToAssign;
}
}
// In the branch specifications, read the parent assignments
void readParents(int *numBranches, int prevGen, SexConstraint **sexConstraints,
int **prevGenSpouseNum,
vector< pair<set<Parent,ParentComp>,int8_t>* > &spouseDependencies,
char *assignBranches, char *assignPar[2], Parent pars[2],
char *&fullAssignPar, const int i1Sex, char *&endptr,
int line) {
int i;
errno = 0; // initially
// Find the second parent if present
for(i = 0; assignPar[0][i] != '_' && assignPar[0][i] != '\0'; i++);
if (assignPar[0][i] == '_') {
assignPar[0][i] = '\0';
assignPar[1] = &(assignPar[0][i+1]);
}
for(int p = 0; p < 2; p++) {
pars[p].branch = -1;
pars[p].gen = prevGen;
}
for(int p = 0; p < 2 && assignPar[p] != NULL && assignPar[p][0] != '\0'; p++){
// Check for generation number
char *genNumStr = NULL;
for(i = 0; assignPar[p][i] != '^' && assignPar[p][i] != '\0'; i++);
if (assignPar[p][i] == '^') {
// Have a generation number
if (p == 0) {
fprintf(stderr, "ERROR: line %d in def: parent assignment for branches %s gives generation\n",
line, assignBranches);
fprintf(stderr, " number for the first parent, but this is only allowed for the second\n");
fprintf(stderr, " parent; for example, 2:1_3^1 has branch 1 from previous generation\n");
fprintf(stderr, " married to branch 3 from generation 1\n");
exit(3);
}
assignPar[p][i] = '\0';
genNumStr = &(assignPar[p][i+1]);
pars[p].gen = strtol(genNumStr, &endptr, 10) - 1; // 0 indexed => -1
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: unable to parse parent assignment for branches %s\n",
line, assignBranches);
fprintf(stderr, " malformed generation number string for second parent: %s\n",
genNumStr);
if (errno != 0)
perror("strtol");
exit(5);
}
if (pars[p].gen > prevGen) {
fprintf(stderr, "ERROR: line %d in def: unable to parse parent assignment for branches %s\n",
line, assignBranches);
fprintf(stderr, " generation number %s for second parent is after previous generation\n",
genNumStr);
exit(-7);
}
else if (pars[p].gen < 0) {
fprintf(stderr, "ERROR: line %d in def: unable to parse parent assignment for branches %s\n",
line, assignBranches);
fprintf(stderr, " generation number %s for second parent is before first generation\n",
genNumStr);
exit(5);
}
}
pars[p].branch = strtol(assignPar[p], &endptr, 10) - 1; // 0 indexed => -1
if (errno != 0 || *endptr != '\0') {
fprintf(stderr, "ERROR: line %d in def: unable to parse parent assignment for branches %s\n",
line, assignBranches);
if (errno != 0)
perror("strtol");
exit(2);
}
if (pars[p].branch < 0) {
fprintf(stderr, "ERROR: line %d in def: parent assignments must be of positive branch numbers\n",
line);
exit(8);
}
else if (pars[p].branch >= numBranches[ pars[p].gen ]) {
fprintf(stderr, "ERROR: line %d in def: parent branch number %d is more than the number of\n",
line, pars[p].branch+1);
fprintf(stderr, " branches (%d) in generation %d\n",
numBranches[ pars[p].gen ], pars[p].gen+1);
exit(8);
}
// so that we can print the parent assignment in case of errors below
if (genNumStr != NULL)
genNumStr[-1] = '^';
}
if (pars[0].branch == -1) {
// new founder
assert(pars[1].branch == -1);
}
else if (pars[1].branch == -1) {
// Have not yet assigned the numerical id of parent 1. Because the def
// file doesn't specify this, it is a founder, and one that hasn't been
// assigned before. As such, we'll get a unique number associated with a
// spouse of pars[0].branch. Negative values correspond to founders, so
// we decrement <prevGenSpouseNum>. (It is initialized to 0 above)
(*prevGenSpouseNum)[ pars[0].branch ]--;
pars[1].branch = (*prevGenSpouseNum)[ pars[0].branch ];
}
else {
if (pars[0].branch == pars[1].branch && pars[0].gen == pars[1].gen) {
fprintf(stderr, "ERROR: line %d in def: cannot have both parents be from same branch\n",
line);
exit(8);
}
if (i1Sex >= 0) {
fprintf(stderr, "ERROR: line %d in def: cannot have fixed sex for i1 samples and marriages\n",
line);
fprintf(stderr, " between branches -- i1's will have the same sex and cannot reproduce.\n");
fprintf(stderr, " consider assigning sexes to individual branches\n");
exit(9);
}
updateSexConstraints(sexConstraints, pars, numBranches, spouseDependencies,
line);
}
// so that we can print the parent assignment in case of errors below
if (assignPar[1] != NULL)
assignPar[1][-1] = '_';
fullAssignPar = assignPar[0];
}
// Given the branch indexes of two parents, adds constraints and error checks
// to ensure that this couple does not violate the requirement that parents
// must have opposite sex.
// Also stores (in <spouseDependencies>) the specified or inferred sexes of the
// branches; sexes explicitly specified in the def file are currently stored in
// <sexConstraints>. This function infers the sexes of those branches these
// individuals have children with, and transitively to the partners of those
// branches, etc.
void updateSexConstraints(SexConstraint **sexConstraints, Parent pars[2],
int *numBranches,
vector< pair< set<Parent,ParentComp>, int8_t>* >
&spouseDependencies,
int line) {
// we check these things in the caller, but just to be sure:
for(int p = 0; p < 2; p++) {
assert(pars[p].branch >= 0);
assert(pars[p].branch < numBranches[ pars[p].gen ]);
}
if (sexConstraints[ pars[0].gen ][ pars[0].branch ].set == -1 &&
sexConstraints[ pars[1].gen ][ pars[1].branch ].set == -1) {
// neither is a member of a spouse set: create and add to
// <spouseDependencies>
pair<set<Parent,ParentComp>,int8_t> *sets[2];
for(int p = 0; p < 2; p++) {
sets[p] = new pair<set<Parent,ParentComp>,int8_t>();
if (sets[p] == NULL) {
printf("ERROR: out of memory");
exit(5);
}
sets[p]->first.insert( pars[p] );
// which index in spouseDependencies is the set corresponding to this
// parent stored in? As we're about to add it just below this, the
// current size will be the index
sexConstraints[ pars[p].gen ][ pars[p].branch ].set =
spouseDependencies.size();
if (sexConstraints[ pars[p].gen ][ pars[p].branch ].theSex != -1)
sets[p]->second = sexConstraints[pars[p].gen][pars[p].branch].theSex;
else
sets[p]->second = -1;
spouseDependencies.push_back( sets[p] );
}
// any assigned sexes? ensure consistency:
if (sets[0]->second >= 0 || sets[1]->second >= 0) {
for(int p = 0; p < 2; p++)
if (sets[p]->second < 0)
// infer sex that is unassigned: is opposite (^ 1) the assigned one
sets[p]->second = sets[ p^1 ]->second ^ 1;
if (sets[0]->second != (sets[1]->second ^ 1)) {
fprintf(stderr, "ERROR: line %d in def: assigning branch %d from generation %d and branch %d from\n",
line, pars[0].branch+1, pars[0].gen+1, pars[1].branch+1);
fprintf(stderr, " generation %d as parents is impossible: they are assigned the same sex\n",
pars[1].gen+1);
exit(3);
}
}
}
else if (sexConstraints[ pars[0].gen ][ pars[0].branch ].set == -1 ||