File: PedigreeFamily.cpp

package info (click to toggle)
libstatgen 1.0.15-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,588 kB
  • sloc: cpp: 49,624; ansic: 1,408; makefile: 320; sh: 60
file content (295 lines) | stat: -rw-r--r-- 8,610 bytes parent folder | download | duplicates (4)
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
/*
 *  Copyright (C) 2010  Regents of the University of Michigan
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Pedigree.h"
#include "Constant.h"
#include "MathConstant.h"
#include "Error.h"

#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>

Family::Family(Pedigree & pedigree, int _first, int _last, int _serial) :
        ped(pedigree)
{
    serial = _serial;
    first = _first;
    last = _last;
    count = last - first + 1;
    path = new int [count];
    famid = ped[first].famid;

    founders = mzTwins = 0;

    for (int i=first; i<=last; i++)
        if (ped[i].isFounder())
        {
            ped[i].traverse = founders;
            path[founders++] = ped[i].serial;
        }
        else
        {
            ped[i].traverse = -1;
            if (ped[i].isMzTwin(ped[i]))
                for (int j = first; j < i; j++)
                    if (ped[i].isMzTwin(ped[j]))
                    {
                        mzTwins++;
                        break;
                    }
        }

    nonFounders = count - founders;
    generations = nonFounders == 0 ? 1 : 2;

    int next = founders;
    while (next < count)
    {
        bool check = false;

        // Create traversal where path ancestors precede their offspring
        for (int i=first; i<=last; i++)
            if (ped[i].traverse == -1)
            {
                int fatherSerial = ped[i].father->traverse;
                int motherSerial = ped[i].mother->traverse;

                if (fatherSerial >= 0 && motherSerial >= 0)
                {
                    check = true;

                    ped[i].traverse = next;
                    path[next++] = i;

                    if (fatherSerial >= founders || motherSerial >= founders)
                        generations = 3;

                    // If this individual is part of a set of MZ twins
                    if (ped[i].zygosity & 1)
                        for (int j = 0; j < ped[i].sibCount; j++)
                        {
                            Person & sib = *ped[i].sibs[j];

                            // Insert all co-twins at the same position in traversal
                            // order
                            if (sib.traverse == -1 && ped[i].zygosity == sib.zygosity)
                            {
                                sib.traverse = next;
                                path[next++] = sib.serial;
                            }
                        }
                }
            }

        if (!check) ShowInvalidCycles();
    }
}

Family::~Family()
{
    delete [] path;
}

void Family::ShowInvalidCycles()
{
    // Try and identify key individuals responsible for
    // pedigree mess-up ... when this function is called
    // pedigree has been traversed top-down and individuals
    // that are correctly specified have IDs of >= 0.

    // This routine traverses the pedigree bottom up to
    // identify a subset of individuals likely to be causing
    // the problem
    IntArray descendants(ped.count);
    descendants.Zero();

    for (int i = first; i <= last; i++)
        if (ped[i].traverse == -1)
        {
            descendants[ped[i].father->serial]++;
            descendants[ped[i].mother->serial]++;
        }

    IntArray stack;

    for (int i = first; i <= last; i++)
        if (ped[i].traverse == -1 && descendants[i] == 0)
        {
            stack.Push(i);

            do
            {
                int j = stack.Pop();

                if (ped[j].traverse != -1) continue;

                ped[j].traverse = 9999;

                if (--descendants[ped[j].father->serial] == 0)
                    stack.Push(ped[j].father->serial);
                if (--descendants[ped[j].mother->serial] == 0)
                    stack.Push(ped[j].mother->serial);
            }
            while (stack.Length());
        }

    printf("The structure of family %s requires\n"
           "an individual to be his own ancestor.\n\n"
           "To identify the problem(s), examine the\n"
           "following key individuals:\n\n",
           (const char *) famid);

    for (int i = first; i <= last; i++)
        if (ped[i].traverse == -1)
            printf("Problem Person: %s\n", (const char *) ped[i].pid);

    error("Invalid pedigree structure.");
}

int Family::ConnectedGroups(IntArray * groupMembership)
{
    IntArray groups(count);

    // Use the quick union algorithm to identify connected groups
    groups.SetSequence(0, 1);
    for (int i = count - 1; i >= founders; i--)
    {
        // Lookup parents
        int group0 = i;
        int group1 = ped[path[i]].father->traverse;
        int group2 = ped[path[i]].mother->traverse;

        // Identify their corresponding groupings
        while (groups[group0] != group0) group0 = groups[group0];
        while (groups[group1] != group1) group1 = groups[group1];
        while (groups[group2] != group2) group2 = groups[group2];

        int group = group1 < group2 ? group1 : group2;
        if (group0 < group) group = group0;

        groups[group0] = groups[group1] = groups[group2] = group;
    }

    // Count groupings
    int groupCount = 0;
    for (int i = 0; i < founders; i++)
        if (groups[i] == i)
            groupCount++;

    if (groupMembership == NULL)
        return groupCount;

    // Flatten tree so all items point to root
    for (int i = 1; i < count; i++)
        groups[i] = groups[groups[i]];

    // Update group membership info
    int group = 0;
    groupMembership->Dimension(count);
    for (int i = 0; i < count; i++)
        if (groups[i] == i)
            (*groupMembership)[i] = ++group;
        else
            (*groupMembership)[i] = (*groupMembership)[groups[i]];

#if 0
    // This stretch of code outputs family structure and group membership
    // And should usually be commented out!
    for (int j = first; j <= last; j++)
        printf("%s %s %s %s %d %d\n",
               (const char *) famid, (const char *) ped[j].pid,
               (const char *) ped[j].fatid, (const char *) ped[j].motid,
               ped[j].sex, groups[ped[j].traverse]);
#endif

    return groupCount;
}

/*
int Family::ConnectedGroups(IntArray * groupMembership)
   {
   IntArray * stack = new IntArray[count];
   IntArray groups(count);

   groups.Zero();

   int group = 0;
   int seed = count - 1;

   // Search for connected sets of individuals until everyone is accounted for
   while (true)
      {
      while ((seed >= 0) && (groups[seed] != 0))
         seed--;

      if (seed == -1)
         break;

      Mark(seed, ++group, stack, groups);

      for (int j = seed; j >= founders; j--)
         if (groups[j] == 0)
            {
            int fat_j = ped[path[j]].father->traverse;
            int mot_j = ped[path[j]].mother->traverse;

            if (groups[fat_j] == group || groups[mot_j] == group)
               Mark(j, group, stack, groups);
            else
               stack[mot_j].Push(j),
               stack[fat_j].Push(j);
            }

      for (int j = 0; j < count; j++)
         stack[j].Clear();
      }

   if (groupMembership != NULL)
      (*groupMembership) = groups;

   // This stretch of code outputs family structure and group membership
   // And should usually be commented out!
#if 0
   for (int j = first; j <= last; j++)
      printf("%s %s %s %s %d %d\n",
         (const char *) famid, (const char *) ped[j].pid,
         (const char *) ped[j].fatid, (const char *) ped[j].motid,
         ped[j].sex, groups[ped[j].traverse]);
#endif

   delete [] stack;

   return group;
   }

void Family::Mark(int j, int group, IntArray * stack, IntArray & groups)
   {
   if (groups[j] == group) return;

   groups[j] = group;

   while (stack[j].Length())
      Mark(stack[j].Pop(), group, stack, groups);

   if (j < founders) return;

   Mark(ped[path[j]].father->traverse, group, stack, groups);
   Mark(ped[path[j]].mother->traverse, group, stack, groups);
   }
*/