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
|
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2008 Shaun Purcell //
// //
// This file is distributed under the GNU General Public //
// License, Version 2. Please see the file COPYING for more //
// details //
// //
//////////////////////////////////////////////////////////////////
#include <iostream>
#include "plink.h"
#include "options.h"
#include <cmath>
bool isAncestorOf(Individual *indx, Individual * f);
int mCount(Individual * indx, Individual *f);
void listAllAncestors(Individual * a, set<Individual*> & anclist, int d)
{
anclist.insert( a );
if ( a->pm == NULL && a->pp == NULL )
return;
if ( a->pp )
{
anclist.insert( a->pp );
listAllAncestors( a->pp , anclist , d+1 );
}
if ( a->pm )
{
anclist.insert( a->pm );
listAllAncestors( a->pm , anclist , d+1 );
}
return;
}
double genrel(Individual * a, Individual * b)
{
// cout << "GR for " << a->iid << " and " << b->iid << "\n";
double g = 0;
// Same person?
if ( a == b )
return 1;
// Are both individuals founders or in different families?
if ( a->fid != b->fid )
return 0;
if ( a->founder && b->founder )
return 0;
// Assuming no inbreeding, find the nearest common ancestor
// For each possible individual, store the number of meioses that
// separate a from k and b from k (in an int2, -1 for not a common
// ancestor)
map<Individual*,int2> nca;
// Start with ancestors of A
set<Individual*> ancestorsA;
set<Individual*> ancestorsB;
listAllAncestors(a,ancestorsA,0);
listAllAncestors(b,ancestorsB,0);
multiset<Individual*> commonAncestors;
set<Individual*>::iterator i = ancestorsA.begin();
while( i != ancestorsA.end() )
{
commonAncestors.insert( *i );
++i;
}
i = ancestorsB.begin();
while( i != ancestorsB.end() )
{
commonAncestors.insert( *i );
++i;
}
// Any individuals represented twice?
set<Individual*> mrca;
multiset<Individual*>::iterator j = commonAncestors.begin();
while ( j != commonAncestors.end() )
{
if ( commonAncestors.count( *j ) == 2 )
mrca.insert( *j );
++j;
}
// cout << "sizes = " << ancestorsA.size() << " " << ancestorsB.size() << "\n";
// cout << "MRCA for " << a->fid << " " << a->iid << " / " << b->iid << "\n";
// i = mrca.begin();
// while( i != mrca.end() )
// {
// cout << (*i)->fid << " " << (*i)->iid << "\n";
// ++i;
// }
// cout << "\n";
// Iterate through common ancestors, finding # of
// meioses back to the two founders individuals
i = mrca.begin();
while( i != mrca.end() )
{
int2 m( (*i)->countMeioses(a) , (*i)->countMeioses(b) );
nca.insert( make_pair( *i, m ) );
++i;
}
int kmin = 9999;
i = mrca.begin();
while( i != mrca.end() )
{
int2 m( (*i)->countMeioses(a) , (*i)->countMeioses(b) );
nca.insert( make_pair( *i, m ) );
if ( m.p1 + m.p2 < kmin )
kmin = m.p1 + m.p2;
++i;
}
//////////////////////
// Calculate 'g'
map<Individual*,int2>::iterator k = nca.begin();
while( k != nca.end() )
{
int2 m = k->second;
int m2 = m.p1 + m.p2;
if ( m2 == kmin )
{
g += pow(0.5,m2);
//cout << "adding " << k->first->iid << " : " << m.p1 << " + " << m.p2 << "\n";
}
++k;
}
return g;
}
int Individual::countMeioses(Individual *f)
{
if ( isAncestorOf(this,f ) )
return mCount(this,f);
else if ( isAncestorOf( f,this ) )
return mCount(f,this);
else
return 0;
}
int mCount(Individual * indx, Individual *f)
{
vector<Individual*> inds;
vector<bool> checked;
bool finished = false;
int nm = 0;
// Add self to list
inds.push_back(indx);
checked.push_back(false);
while (!finished)
{
// Check list for a match
// needs changing if inbreeding
for (int i = 0 ; i < inds.size() ; i++)
{
if (inds[i] == f) return nm;
}
// Increment meioses counter
nm++;
// Add children of unchecked inds
int already = inds.size();
for (int i = 0 ; i < already ; i++)
{
if (!checked[i])
{
for (int j = 0 ; j < inds[i]->kids.size() ; j++)
{
inds.push_back(inds[i]->kids[j]);
checked.push_back(false);
}
checked[i] = true;
}
}
// All done?
finished = true;
for (int i = 0 ; i < inds.size() ; i++)
if ( checked[i] == false ) finished = false;
// loop back
}
return nm;
}
bool isAncestorOf(Individual *indx, Individual * f)
{
vector<Individual*> inds;
vector<bool> checked;
bool finished = false;
int nm = 0;
// Add self to list
inds.push_back(indx);
checked.push_back(false);
while (!finished)
{
// Check list for a match
for (int i = 0 ; i < inds.size() ; i++)
if (inds[i] == f) return true;
// Increment meioses counter
nm++;
// Add children of unchecked inds
int already = inds.size();
for (int i = 0 ; i < already ; i++)
{
if (!checked[i])
{
for (int j = 0 ; j < inds[i]->kids.size() ; j++)
{
inds.push_back( inds[i]->kids[j] );
checked.push_back(false);
}
checked[i] = true;
}
}
// All done?
finished = true;
for (int i = 0 ; i < inds.size() ; i++)
if ( checked[i] == false ) finished = false;
// loop back
}
return false;
}
|