File: PedigreeTrim.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 (187 lines) | stat: -rw-r--r-- 5,939 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
/*
 *  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"

void Pedigree::ShowTrimHeader(bool & flag)
{
    if (flag)
    {
        printf("Trimming uninformative individuals...\n");
        flag = false;
    }
}

void Pedigree::Trim(bool quiet, int * informative)
{
    int       newCount = 0;
    Person ** newPersons = new Person * [count];

    // This function applies the following filters to reduce complexity
    // of pedigree
    //
    // RULE 1:         Remove all pedigrees no genotype or phenotype data
    // RULE 2:         Remove leaf individuals with no data
    // RULE 3:         Remove founder couples with <2 offspring and no data

    bool     showHeader = true;
    IntArray discardable, offspring, mates, haveData;

    for (int f = 0; f < familyCount; f++)
    {
        Family * fam = families[f];

        // Cache for storing indicators about whether each family member is
        // informative
        haveData.Dimension(fam->count);

        // Check that some data is available in the family
        int hasData = false;
        for (int i = fam->first; i <= fam->last; i++)
            if (informative == NULL)
                hasData |= haveData[persons[i]->traverse] = persons[i]->haveData();
            else
                hasData |= haveData[persons[i]->traverse] = informative[i];

        if (!hasData)
        {
            if (!quiet)
            {
                ShowTrimHeader(showHeader);
                printf("   Removing family %s: No data\n", (const char *) fam->famid);
            }

            for (int i = fam->first; i <= fam->last; i++)
                delete persons[i];

            continue;
        }

        // Assume that we need everyone in the family
        discardable.Dimension(fam->count);
        discardable.Set(0);

        bool trimming = true;

        while (trimming)
        {
            trimming = false;

            // Tally the number of offspring for each individual
            offspring.Dimension(fam->count);
            offspring.Zero();

            // Tally the number of mates for each individual
            mates.Dimension(fam->count);
            mates.Set(-1);

            // In the first round, we count the number of offspring
            // for each individual in the current trimmed version of the
            // pedigree
            for (int i = fam->count - 1; i >= fam->founders; i--)
            {
                if (discardable[i]) continue;

                Person & p = *(persons[fam->path[i]]);

                if (discardable[p.father->traverse])
                    continue;

                if (offspring[i] == 0 && !haveData[p.traverse])
                {
                    trimming = true;
                    discardable[i] = true;
                    continue;
                }

                int father = p.father->traverse;
                int mother = p.mother->traverse;

                if (mates[father] == -1 && mates[mother] == -1)
                {
                    mates[father] = mother,
                                    mates[mother] = father;
                }
                else if (mates[father] != mother)
                {
                    if (mates[father] >= 0)
                        mates[mates[father]] = -2;

                    if (mates[mother] >= 0)
                        mates[mates[mother]] = -2;

                    mates[mother] = -2;
                    mates[father] = -2;
                }

                offspring[father]++;
                offspring[mother]++;
            }

            // In the second pass, we remove individuals with no
            // data who are founders with a single offspring (and
            // no multiple matings) or who have no descendants
            for (int i = fam->count - 1; i >= 0; i--)
            {
                if (discardable[i]) continue;

                Person & p = *(persons[fam->path[i]]);

                if (p.isFounder() || discardable[p.father->traverse])
                {
                    if (mates[i] == -2 ||
                            offspring[i] > 1 ||
                            (mates[i] >= fam->founders &&
                            !discardable[persons[fam->path[mates[i]]]->father->traverse]) ||
                            haveData[p.traverse] ||
                            (mates[i] != -1 && haveData[mates[i]]))
                        continue;

                    trimming = true;
                    discardable[i] = true;
                    continue;
                }
            }
        }

        for (int i = fam->count - 1; i >= 0; i--)
            if (discardable[i])
            {
                if (!quiet)
                {
                    ShowTrimHeader(showHeader);
                    printf("   Removing person %s->%s: No data\n",
                           (const char *) fam->famid,
                           (const char *) persons[fam->path[i]]->pid);
                }
                delete persons[fam->path[i]];
            }
            else
                newPersons[newCount++] = persons[fam->path[i]];
    }

    if (!showHeader)
        printf("\n");

    delete [] persons;

    persons = newPersons;
    count = newCount;
    Sort();
}