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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
|
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2009 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 <iomanip>
#include <fstream>
#include <sstream>
#include <set>
#include <algorithm>
#include <cmath>
#include "plink.h"
#include "helper.h"
#include "options.h"
#include "perm.h"
#include "stats.h"
using namespace std;
extern ofstream LOG;
// Helper function: find the maximum distance between two clusters
double cldist(vector<vector<double> > &, vector<int> &, vector<int> &);
// Helper function: group average link
double groupAvgLink(vector<vector<double> > &, vector<int> &, vector<int> &);
// Helper function: are two clusters phenotypically homogeneous?
bool homogeneous_clusters(Plink &, vector<int> &, vector<int> &);
// Do two clusters conform to any --mcc Ncase Ncontrol specification?
bool spec_clusters(Plink &, vector<int> &, vector<int> &);
// Any members of the clusters that can't be matched?
bool pairable_cluster(vector<vector<bool> > & , vector<int>&, vector<int>&);
// Have we already picked somebody from this category?
bool selcon_inds(Plink&, vector<int>&, vector<int>&, set<int>&);
class Neighbour
{
public:
double dist;
Individual * neighbour;
bool operator< (const Neighbour & s2) const
{ return (dist < s2.dist); }
};
// Complete-linkage clustering based on average IBS distance
// Extra constraints:
// --pmerge P do not merge clusters containing two individuals who differ at this level
// --mc N do not let clusters contain more than N individuals
// --cc do not merge phenotypically identical clusters
// --mcc N1 N2 do not let cluster contain more than N1 cases and N2 controls
// --match external categorical matching criteria
// --match-type positive or negative matches
// --qmatch external quantitative threshold based
// --qt define thresholds for QT matching
// --pick1 only select one individual from each covariate group
// --ibm X identity-by-missingness threshold
void Plink::buildCluster()
{
///////////////////////////////////////
// This is an individual-mode analysis
if (par::SNP_major)
SNP2Ind();
//////////////////////////////////////
// Force an initial cluster solution?
// Initially, # of clusters = # of people, unless
// we are forcing a starting solution
int ni = n;
vector<vector<int> > cl;
if ( par::force_initial_cluster )
{
if (!readClusterFile())
error("Problem reading --within {file}");
printLOG("Forcing an initial starting solution from [ " + par::include_cluster_filename + " ]\n");
set<int> added;
for (int k=0;k<nk;k++)
{
vector<int> t;
for (int i=0;i<ni;i++)
{
if ( sample[i]->sol == k )
{
t.push_back(i);
added.insert(i);
}
}
cl.push_back(t);
}
// And now add any remain individuals, in their own clusters,
// starting from cluster nk onwards
for (int i=0;i<ni;i++)
{
if ( added.find(i) == added.end() )
{
vector<int> t(1);
t[0] = i;
cl.push_back(t);
}
}
}
else
{
for (int i=0;i<ni;i++)
{
vector<int> t(1);
t[0] = i;
cl.push_back(t);
}
}
// T/F matrix (lower diagonal) for whether
// two people can be matched, based on p-value
// constraint and any external criteria
// pairable[i][j] (default = T)
vector<vector<bool> > pairable(n);
for (int i=0; i<n; i++)
{
vector<bool> tmp(n,true);
pairable[i] = tmp;
}
///////////////////////////////
// External matching criteria
// Determine, in advance, potential pairwise matching
if (par::bmatch)
{
printLOG("Applying categorical matching criteria...\n");
// Read in each covariate one at a time,
// and determine matching
// we can use the covariate file, as the cluster
// routine exits after clustering (i.e. so covariates
// never used)
// Has the user specified a match-type file? If not,
// assume all are positive matches.
vector<bool> btype(0);
if (par::bmatch_usertype)
{
checkFileExists(par::bmatch_direction_filename);
ifstream BT(par::bmatch_direction_filename.c_str(), ios::in);
while (!BT.eof())
{
string tmp;
BT >> tmp;
if(BT.eof()) break;
if (tmp=="+" || tmp=="1")
btype.push_back(true);
else
btype.push_back(false);
}
BT.close();
printLOG(int2str(btype.size())+" match-type definitions read from [ "+
par::bmatch_direction_filename+" ]\n");
}
int c=0;
// Swap b-match filename as cluster/within filename
par::include_cluster_filename = par::bmatch_filename;
while (1)
{
par::mult_clst = ++c;
if (!readClusterFile()) break;
if (!par::bmatch_usertype)
btype.push_back(true);
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++)
{
// ->missing means missing on covariate in this context
// Simple matching (no usertypes or +-match)
if ( btype[c-1] )
{
// +/match
if (sample[i]->sol != sample[j]->sol &&
(!sample[i]->missing ) &&
(!sample[j]->missing ) )
pairable[i][j] = pairable[j][i] = false;
}
else
{
// -/match
if (sample[i]->sol == sample[j]->sol &&
(!sample[i]->missing ) &&
(!sample[j]->missing ) )
pairable[i][j] = pairable[j][i] = false;
}
}
}
printLOG("Matched on "+int2str(c-1)+
" variables from [ "+par::bmatch_filename+" ]\n");
}
if (par::qmatch)
{
printLOG("Applying quantitative matching criteria...\n");
vector<double> qt; // number of thresholds specified
checkFileExists(par::qmatch_threshold_filename);
ifstream QT(par::qmatch_threshold_filename.c_str(), ios::in);
while (!QT.eof())
{
double tmp;
QT >> tmp;
if(QT.eof()) break;
qt.push_back(tmp);
}
QT.close();
printLOG(int2str(qt.size())+" q-match thresholds read from [ "+
par::qmatch_threshold_filename+" ]\n");
// Swap q-match filename as covariate file
par::covar_filename = par::qmatch_filename;
int c=0; // counter for number of fields in qmatch file
for (int z=1; z<=qt.size(); z++)
{
par::mult_covar = z;
if (!readCovariateFile()) break;
c++;
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++)
{
if ( abs( sample[i]->covar - sample[j]->covar ) > qt[c-1] &&
(!sample[i]->missing) &&
(!sample[j]->missing) )
pairable[i][j] = pairable[j][i] = false;
}
}
printLOG("Matched on "+
int2str(c)+" quantitative covariates from [ "
+par::qmatch_filename +" ]\n");
}
if (par::cluster_missing)
{
printLOG("Clustering individuals based on genome-wide IBM\n");
}
else
{
printLOG("Clustering individuals based on genome-wide IBS\n");
stringstream s2;
s2 << "Merge distance p-value constraint = " << par::merge_p << "\n";
printLOG(s2.str());
}
if (par::outlier_detection)
printLOG("Outlier detection based on neighbours "+int2str(par::min_neighbour)+
" to "+int2str(par::max_neighbour)+"\n");
/////////////////////////////////////////////////////////
// Also, if --pick1 is in effect, we need to read a list from which
// we can pick only 1 individual
if (par::cluster_selcon)
{
// Swap pick1 filename as covariate file
par::include_cluster_filename = par::cluster_selcon_file;
par::mult_clst = 1;
if (!readClusterFile())
error("Problem reading for --pick1 option");
}
// Keep track of what has been selected already
set<int> selcon;
/////////////////////////////
// Set up distance matrices
// Lower diagonal structure, requires that i > j
mdist.resize(n);
for (int j=0;j<n;j++)
mdist[j].resize(j);
//////////////////////////////////////////
// Genome-wide IBS for each pair
// Either calculate, or re-read from file
vector<double> prop_sig_diff(n);
// Calculate...
if (!par::ibd_read)
{
int c=0;
int c2=0;
for (int i1=0; i1<n-1; i1++)
for (int i2=i1+1; i2<n; i2++)
{
// Only update message every 100 iterations
if (c==c2 || c==np)
{
if (par::cluster_missing)
{
cout << "IBM calculation: "
<< c++ << " of " << np
<< " \r";
cout.flush();
}
else
{
cout << "IBS(g) calculation: "
<< c++ << " of " << np
<< " \r";
cout.flush();
}
c2+=100;
}
else
++c;
Z IBSg;
if (par::cluster_missing)
calcGenomeIBM(sample[i1],sample[i2]);
else
{
// Also calculate IBM as a constraint?
if (par::cluster_ibm_constraint)
{
calcGenomeIBM(sample[i1],sample[i2]);
if ( dst < par::cluster_ibm_constraint_value )
pairable[i1][i2] = pairable[i2][i1] = false;
}
// IBS distance (stored in dst)
IBSg = calcGenomeIBS(sample[i1],sample[i2]);
}
mdist[i2][i1]=dst;
//////////////////////////
// Is this pair pairable?
if (pv < par::merge_p && realnum(pv))
{
// record pair as unpairable
pairable[i1][i2] = pairable[i2][i1] = false;
// record for both individuals a IBS-based mismatch
prop_sig_diff[i1]++;
prop_sig_diff[i2]++;
}
}
}
else // ... read IBS information from .genome file
{
checkFileExists(par::ibd_file);
if ( par::ibd_read_minimal )
printLOG("Reading IBS estimates (minimal format) from [ "
+par::ibd_file+" ] \n");
else
printLOG("Reading genome-wide IBS estimates from [ "
+par::ibd_file+" ] \n");
if ( compressed( par::ibd_file ) )
par::compress_genome = true;
ZInput ZINC( par::ibd_file , par::compress_genome );
map<string,int> mperson;
for (int i=0; i<n; i++)
mperson.insert(make_pair( sample[i]->fid+"_"+sample[i]->iid , i ));
map<Individual*,int> mcode;
for (int i=0; i<n; i++)
mcode.insert(make_pair( sample[i] , i ));
vector<Individual*> peeps;
if ( par::ibd_read_minimal )
{
// read in list of people here
while ( 1 )
{
vector<string> ids = ZINC.tokenizeLine();
if ( ids.size() != 2 )
{
string emsg = "Problem with line in [ " + par::ibd_file + " ]\n";
for (int i=0;i<ids.size();i++)
emsg += ids[i] + " ";
error(emsg);
}
string fid = ids[0];
string iid = ids[1];
if ( fid == "__END" )
break;
// Find this person
string pcode = fid+"_"+iid;
map<string,int>::iterator p = mperson.find(pcode);
// Add NULL if this person actually not in
// the current file -- in this case, they
// will be ignored -- but remember we have
// to check for NULLs below and skip those
// numbers in that case...
if ( p == mperson.end() )
peeps.push_back( NULL );
else
peeps.push_back( sample[p->second] );
// Just in case we have a malformed file
if ( ZINC.endOfFile() )
error("Problem with premature stop in file [ " + par::ibd_file + " ]\n");
}
//////////////////////////////////////////////////////
// Now read the actual IBS/PPC values for these peeps
if ( peeps.size() != sample.size() )
printLOG("Warning -- a different number of people in .genome.min that dataset\n");
int size = peeps.size();
int p1 = 0, p2 = 1;
while ( 1 )
{
double mydst, pv, ibd;
vector<string> val = ZINC.tokenizeLine();
if ( ZINC.endOfFile() )
{
// Check that p1,p2 counts are as should be...
break;
}
if ( val.size() != 3 )
{
string emsg = "Problem with line in [ " + par::ibd_file + " ]\n";
for (int i=0;i<val.size();i++)
emsg += val[i] + " ";
error(emsg);
}
if ( !from_string<double>( mydst, val[0], std::dec ) )
mydst = 0;
if ( !from_string<double>( pv, val[1], std::dec ) )
pv = 0;
if ( !from_string<double>( ibd, val[2], std::dec ) )
ibd = 0;
Individual * person1 = peeps[p1];
Individual * person2 = peeps[p2];
int pn1 = mcode.find( person1 )->second;
int pn2 = mcode.find( person2 )->second;
if ( person1 == NULL || person2 == NULL || person1 == person2 )
{
// Advance to next pair
++p2;
if ( p2 == n )
{
++p1;
p2=p1+1;
}
if ( p1==n )
break;
continue;
}
// cout << "found " << pn1 << " and " << pn2 << " is "
// << person1->fid << " " << person1->iid << " x "
// << person2->fid << " " << person2->iid << "\t"
// << " with "
// << mydst << " " << pv << "\n";
// Record IBS distance
if ( pn1 > pn2 )
mdist[pn1][pn2] = mydst;
else
mdist[pn2][pn1] = mydst;
//////////////////////////
// Is this pair pairable?
if (pv < par::merge_p && realnum(pv))
{
// record pair as unpairable
pairable[pn1][pn2] = false;
pairable[pn2][pn1] = false;
// record for both individuals a IBS-based mismatch
prop_sig_diff[pn1]++;
prop_sig_diff[pn2]++;
}
// Also calculate IBM as a constraint?
if (par::cluster_ibm_constraint)
{
calcGenomeIBM(person1,person2);
if ( dst < par::cluster_ibm_constraint_value )
{
pairable[pn1][pn2] = false;
pairable[pn2][pn1] = false;
}
}
// Advance to next peep-pair
++p2;
if ( p2 == n )
{
++p1;
p2=p1+1;
}
// Finished?
if ( p1==n )
break;
}
}
else
{
// Read in .genome file in verbose mode
// We only want FID1,IID1,FID2,IID2 (always first four)
// DST and PPC
// Get field codes from header
int ppc_code = -1;
int dst_code = -1;
int col_length = 0;
double mydst;
vector<string> tokens = ZINC.tokenizeLine();
col_length = tokens.size();
if ( tokens.size() < 4 ||
tokens[0] != "FID1" ||
tokens[1] != "IID1" ||
tokens[2] != "FID2" ||
tokens[3] != "IID2" )
error("Problem with header row of .genome file");
for ( int i = 4; i<tokens.size(); i++)
{
if ( tokens[i] == "PPC" )
ppc_code = i;
if ( tokens[i] == "P" )
ppc_code = i;
if ( tokens[i] == "DST" )
dst_code = i;
}
if ( ppc_code == -1 || dst_code == -1 )
error("Could not find PPC or DST fields in .genome file");
// Read each pair at a time
while ( ! ZINC.endOfFile() )
{
vector<string> tokens = ZINC.tokenizeLine();
if ( tokens.size() == 0 )
continue;
if ( col_length != tokens.size() )
{
string strmsg = "";
for (int i=0;i<tokens.size();i++)
strmsg += tokens[i] + " ";
error("Problem reading line in .genome file:\n"+strmsg+"\n");
}
string fid1 = tokens[0];
string iid1 = tokens[1];
string fid2 = tokens[2];
string iid2 = tokens[3];
string ipv = tokens[ppc_code];
string idst = tokens[dst_code];
// Skip any blank rows, or additional header rows
if (fid1=="") continue;
if (fid1=="FID1") continue;
// if ( ! ( from_string<double>( ibs0 , i0 , std::dec) &&
// from_string<double>( ibs1 , i1 , std::dec) &&
// from_string<double>( ibs2 , i2 , std::dec) ) )
// {
// error("Problem with line in .genome file, IBS estimates: \n"
// +i0+" "+i1+" "+i2+" "+ipv+"\n");
// }
if ( ! from_string<double>( mydst , idst , std::dec) )
mydst = 0;
if ( ! from_string<double>( pv , ipv , std::dec) )
pv = 1;
// Calculate proportion IBS matching
// if (par::cluster_euclidean)
// mydst = (ibs2*2+ibs1*0.5)/(ibs2*2+ibs1+ibs0);
// else
// mydst = (ibs2+ibs1*0.5)/(ibs2+ibs1+ibs0);
map<string,int>::iterator person1 = mperson.find(fid1+"_"+iid1);
map<string,int>::iterator person2 = mperson.find(fid2+"_"+iid2);
if ( person1 == mperson.end() || person2 == mperson.end() || person1 == person2 )
continue;
// Record IBS distance
if ( person1->second > person2->second )
mdist[person1->second][person2->second] = mydst;
else
mdist[person2->second][person1->second] = mydst;
//////////////////////////
// Is this pair pairable?
if (pv < par::merge_p && pv==pv)
{
// record pair as unpairable
pairable[person1->second][person2->second] = false;
pairable[person2->second][person1->second] = false;
// record for both individuals a IBS-based mismatch
prop_sig_diff[person1->second]++;
prop_sig_diff[person2->second]++;
}
// Also calculate IBM as a constraint?
if (par::cluster_ibm_constraint)
{
calcGenomeIBM(sample[person1->second],sample[person2->second]);
if ( dst < par::cluster_ibm_constraint_value )
{
pairable[person1->second][person2->second] = false;
pairable[person2->second][person1->second] = false;
}
}
} // Read next line in .genome
}
ZINC.close();
/////////////////////////////////////////////
// Check that every pair in the dataset has
// actually been assigned a value -- i.e. check
// for 0 IBS codes, etc.
}
///////////////////////////////////
// IBS permutation test
if ( par::ibs_test )
{
// If we were called by permutationIBSTest(),
// now it is time to return
return;
}
///////////////////////////////////
// Display matrix of IBS distances
if (par::matrix)
{
string f;
if (par::cluster_missing)
f = par::output_file_name+ ".mdist.missing";
else if (par::distance_matrix)
f = par::output_file_name+ ".mdist";
else
f = par::output_file_name+ ".mibs";
if (!par::cluster_missing)
{
if (par::distance_matrix)
printLOG("Writing IBS distance matrix to [ "+f + " ]\n");
else
printLOG("Writing IBS similarity matrix to [ "+f + " ]\n");
}
else
printLOG("Writing IBM distance matrix to [ "+f + " ]\n");
ofstream MAT(f.c_str(),ios::out);
MAT.clear();
for (int i=0;i<mdist.size();i++)
{
for (int j=0;j<mdist.size();j++)
{
if ( par::distance_matrix )
{
// Distances
if (i>j)
MAT << 1 - mdist[i][j] << " ";
else if (i==j)
MAT << 0 << " ";
else
MAT << 1 - mdist[j][i] << " ";
}
else
{
// Similarities
if (i>j)
MAT << mdist[i][j] << " ";
else if (i==j)
MAT << 1 << " ";
else
MAT << mdist[j][i] << " ";
}
}
MAT << "\n";
}
MAT.close();
}
////////////////////////////////////
// Determine how many pairable pairs
// we have now
if (!par::cluster_missing)
{
int paircount = 0;
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++)
if (pairable[i][j]) paircount++;
printLOG("Of these, "+int2str(paircount)+" are pairable based on constraints\n");
}
//////////////////////////
// Outlier detection
if (par::outlier_detection)
{
printLOG("Writing individual neighbour/outlier statatistics to [ " +
par::output_file_name + ".nearest ]\n");
vector<vector<double> > min_dst(n);
vector<vector<double> > zmin_dst(n);
vector<vector<Individual*> > min_ind(n);
if (par::max_neighbour > n-1)
error("Nearest neighbour range specified as [ "+int2str(par::max_neighbour)
+" ] but only [ "+int2str(n)+" ] individuals in sample.");
for (int k=par::min_neighbour;k<=par::max_neighbour;k++)
{
// Consider each person
for (int i=0;i<n;i++)
{
vector<Neighbour> ibs(n-1);
int c=0;
for (int j=0;j<n;j++)
if (i!=j)
{
if ( i>j )
ibs[c].dist = mdist[i][j];
else
ibs[c].dist = mdist[j][i];
ibs[c].neighbour = sample[j];
c++;
}
sort(ibs.begin(),ibs.end());
min_dst[i].push_back(ibs[ibs.size() - k].dist);
min_ind[i].push_back(ibs[ibs.size() - k].neighbour);
}
// Calculate mean and variance of min_dst to
// give Z-scores
double mean = 0;
double var = 0;
for (int i=0; i<n; i++)
mean += min_dst[i][min_dst[i].size()-1];
mean /= (double)n;
for (int i=0; i<n; i++)
var += (min_dst[i][min_dst[i].size()-1]-mean)*(min_dst[i][min_dst[i].size()-1]-mean);
var /= (double)(n-1);
for (int i=0; i<n; i++)
zmin_dst[i].push_back( ( min_dst[i][min_dst[i].size()-1] - mean ) / sqrt(var) ) ;
}
// Second measure: based on significance test
// Proportion of rest of sample with whom significant difference at 'pmerge' threshold
// pv might be NaN, but only if very small # of markers is used -- ignore, as
// all values will be meaningless in any case
if (!par::cluster_missing)
for (int i=0;i<n;i++)
prop_sig_diff[i] /= (double)(n-1);
// And output to a file
ofstream MD((par::output_file_name+".nearest").c_str(),ios::out);
MD.clear();
MD.precision(4);
MD << setw(12) << "FID" << " "
<< setw(12) << "IID" << " "
<< setw(6) << "NN" << " "
<< setw(12) << "MIN_DST" << " "
<< setw(12) << "Z" << " "
<< setw(12) << "FID2" << " "
<< setw(12) << "IID2" << " ";
if (!par::cluster_missing)
MD << setw(12) << "PROP_DIFF" << " ";
MD << "\n";
for (int i=0; i<n; i++)
for (int k=0;k<min_dst[0].size();k++)
{
MD << setw(12) << sample[i]->fid << " "
<< setw(12) << sample[i]->iid << " "
<< setw(6) << par::min_neighbour+k << " "
<< setw(12) << min_dst[i][k] << " "
<< setw(12) << zmin_dst[i][k] << " "
<< setw(12) << min_ind[i][k]->fid << " "
<< setw(12) << min_ind[i][k]->iid << " ";
if (!par::cluster_missing)
MD << setw(12) << prop_sig_diff[i] << " ";
MD << "\n";
}
MD.close();
}
//////////////////////////
// Cluster analysis
if ( par::cluster )
{
int c=1;
bool done=false;
// Matrix of solutions
vector< vector<int> > sol(ni);
for (int i=0;i<ni;i++) sol[i].resize(ni);
vector<double> hist(1);
// Build solution
for (int i=0; i<cl.size(); i++)
for (int j=0; j<cl[i].size(); j++)
sol[cl[i][j]][0] = i;
printLOG("Writing cluster progress to [ "+par::output_file_name + ".cluster0 ]\n");
ofstream CLST((par::output_file_name+".cluster0").c_str(),ios::out);
CLST.clear();
while(!done)
{
double dmin = -999;
int imin=-1;
int jmin=-1;
// 1. Find min/max distance between pairable clusters
for (int i=0; i<cl.size()-1; i++)
for (int j=i+1; j<cl.size(); j++)
{
// Cluster on IBS: group average link or complete linkage?
double d = par::cluster_group_avg ? groupAvgLink(mdist,cl[i],cl[j]) : cldist(mdist,cl[i],cl[j]);
// Are these individuals/clusters more similar AND pairable?
if ( d>dmin && pairable_cluster(pairable,cl[i],cl[j]) )
{
// And will the max cluster size requirement be fulfilled?
if (par::max_cluster_size==0 ||
(( cl[i].size()+cl[j].size()) <= par::max_cluster_size) )
{
// And will the basic phenotypic matching requirement be fulfilled?
if ( (!par::cluster_on_phenotype)
|| (!homogeneous_clusters((*this),cl[i],cl[j])))
{
// What about the --mcc clustering
if ( (!par::cluster_on_mcc) || spec_clusters( (*this),cl[i],cl[j]) )
{
// And what about pick1 constrains? (this must be final constraint)
if ( (!par::cluster_selcon) || selcon_inds( (*this),cl[i],cl[j],selcon))
{
imin=i;
jmin=j;
dmin=d;
}
}
}
}
}
}
// Did we get a merge?
if (imin==-1) {
done=true;
//printLOG("Cannot make clusters that satisfy constraints at step "+int2str(c)+"\n");
goto done_making_clusters;
}
// Save merge distance
hist.push_back(dmin);
// Add to list of selected categories
if (par::cluster_selcon)
{
if (cl[imin].size() == 1 )
selcon.insert( sample[cl[imin][0]]->sol );
if (cl[jmin].size() == 1 )
selcon.insert( sample[cl[jmin][0]]->sol );
}
// 2. Join these clusters
for(int j=0;j<cl[jmin].size();j++)
cl[imin].push_back(cl[jmin][j]);
cl.erase(cl.begin()+jmin);
if (cl.size()==1 || cl.size()==par::max_cluster_N) done=true;
// List entire sample
CLST << "Merge step " << c << "\t" << hist[c];
// Build solution
for (int i=0; i<cl.size(); i++)
for (int j=0; j<cl[i].size(); j++)
{
sol[cl[i][j]][c] = i;
}
// Calculate average within/between cluster distances
double between = 0, within = 0;
int withinN = 0, betweenN = 0;
for (int j1=0; j1<sol.size(); j1++)
for (int j2=0; j2<sol.size(); j2++)
{
if (j1 < j2)
{
if(sol[j1][c] == sol[j2][c])
{
within += mdist[j2][j1];
withinN++;
}
else
{
between += mdist[j2][j1];
betweenN++;
}
}
}
CLST << "\t" << between/(double)betweenN
<< "\t" << within/(double)withinN
<< "\t" << ( between/(double)betweenN ) / ( within/(double)withinN )
<< "\n";
// Next merge
c++;
}
done_making_clusters:
CLST.close();
//////////////////////////////////
// Best solution is final solution
int best = hist.size()-1;
if (!par::cluster_missing)
{
printLOG("Writing cluster solution (1) [ "
+ par::output_file_name + ".cluster1 ]\n");
CLST.open((par::output_file_name+".cluster1").c_str(),ios::out);
CLST.clear();
for (int i=0; i<cl.size(); i++)
{
CLST << "SOL-" << i << "\t";
for (int j=0; j<cl[i].size(); j++)
{
CLST << " "
<< sample[cl[i][j]]->fid << "_"
<< sample[cl[i][j]]->iid;
if (par::cluster_on_phenotype || par::cluster_on_mcc)
CLST << "("
<< (int)sample[cl[i][j]]->phenotype
<< ")";
}
CLST << "\n";
}
CLST.close();
printLOG("Writing cluster solution (2) [ "
+ par::output_file_name + ".cluster2 ]\n");
CLST.open((par::output_file_name+".cluster2").c_str(),ios::out);
CLST.clear();
for (int j=0; j<sol.size(); j++)
{
// Display...
CLST << sample[j]->fid << " "
<< sample[j]->iid << "\t"
<< sol[j][best] << "\n";
// Keep track of this (might be needed if MDS plot done)
sample[j]->sol = sol[j][best];
}
CLST.close();
}
if (!par::cluster_missing)
{
printLOG("Writing cluster solution (3) [ "
+ par::output_file_name + ".cluster3 ]\n");
CLST.open((par::output_file_name+".cluster3").c_str(),ios::out);
}
else
{
printLOG("Writing cluster solution (3) [ "
+ par::output_file_name + ".cluster3.missing ]\n");
CLST.open((par::output_file_name+".cluster3.missing").c_str(),ios::out);
}
CLST.clear();
for (int j=0; j<sol.size(); j++)
{
// Display...
CLST << sample[j]->fid << " "
<< sample[j]->iid << "\t";
for (int i=0; i<sol[0].size(); i++)
CLST << sol[j][i] << " ";
CLST << "\n";
}
CLST << "\n";
CLST.close();
}
//////////////////////////////////////////////////////////
// Produce MDS plot?
if ( par::cluster_plot )
generateMDS();
// Shutdown now
shutdown();
}
double cldist(vector<vector<double> > & d,
vector<int> & a,
vector<int> & b)
{
// Compare based on first metric, but also return paired second
double l;
l = a[0]>b[0] ? d[a[0]][b[0]] : d[b[0]][a[0]];
for (int i=0; i<a.size(); i++)
for (int j=0; j<b.size(); j++)
{
if ( a[i] > b[j] )
{
if ( d[a[i]][b[j]] < l ) l = d[a[i]][b[j]];
}
else
{
if ( d[b[j]][a[i]] < l ) l = d[b[j]][a[i]];
}
}
return l;
}
double groupAvgLink(vector<vector<double> > & d,
vector<int> & a,
vector<int> & b)
{
double s = 0;
for (int i=0; i<a.size(); i++)
for (int j=0; j<b.size(); j++)
{
if ( a[i] > b[j] )
{
s += d[a[i]][b[j]];
}
else
{
s += d[b[j]][a[i]];
}
}
return 1.0 / ( a.size() * b.size() ) * s ;
}
bool homogeneous_clusters(Plink & P, vector<int> & a, vector<int> & b)
{
// Determine how to handle missing phenotypes?
bool homogeneous = true;
for (int i=0; i<a.size(); i++)
for (int j=0; j<b.size(); j++)
{
if ( (P.sample[a[i]]->phenotype != P.sample[b[j]]->phenotype)
&& (!P.sample[a[i]]->missing) && (!P.sample[b[j]]->missing) )
homogeneous = false;
}
return homogeneous;
}
bool spec_clusters(Plink & P, vector<int> & a, vector<int> & b)
{
// Missing individuals will be treated as unaffected
int ncase = 0, ncontrol = 0;
for (int i=0; i<a.size(); i++)
if (P.sample[a[i]]->aff) ncase++;
else ncontrol++;
for (int j=0; j<b.size(); j++)
if (P.sample[b[j]]->aff) ncase++;
else ncontrol++;
if (ncase <= par::max_cluster_case &&
ncontrol <= par::max_cluster_control)
return true;
else
return false;
}
bool pairable_cluster(vector<vector<bool> > & pairable, vector<int> & a, vector<int> & b)
{
for (int i=0; i<a.size(); i++)
for (int j=0; j<b.size(); j++)
if (!pairable[a[i]][b[j]]) return false;
return true;
}
bool selcon_inds(Plink & P, vector<int> & a, vector<int> & b, set<int> & inc)
{
// Only need to check for singletons (i.e. once somebody is in a cluster,
// they must have already passed this test)
if ( a.size() == 1 )
{
// Individual already in?
if ( inc.find(P.sample[a[0]]->sol) != inc.end() )
return false;
}
else if ( b.size() == 1 )
{
if ( inc.find(P.sample[b[0]]->sol) != inc.end() )
return false;
}
if ( a.size() == 1 && b.size() == 1 )
if ( P.sample[a[0]]->sol == P.sample[b[0]]->sol )
return false;
return true;
}
void Plink::permutationIBSTest(Perm & perm)
{
// Take the IBS distance matrix, and ask (by permutation)
// where the average difference between two groups is larger
// than we would expect by chance
// i.e. statistic = average between group IBS distance
// permutation = label swapping
// 1-sided test, asking whether people between
// groups are *less* similar than we'd expect
/////////////////////////////////
// Calculate distances
// (will exit before clustering)
buildCluster();
////////////////
// Perform test
perm.setTests(12);
perm.setPermClusters(*this);
perm.originalOrder();
// Tests (1 sided), where ">" means less similar?
// as tests are based on 1-f(mdist)
// 0 1a. Case/control < all others
// 1 1b. Case/control > all others
// 2 2a. Case/case < control/control
// 3 2b. Case/case > control/control
// 4 3a. Case/case < all others
// 5 3b. Case/case > all others
// 6 4a. Control/control < all others
// 7 4b. Control/control > all others
// 8 5a. Case/case < Case/control
// 9 5b. Case/case > Case/control
// 10 6a. Control/control < Case/control
// 11 6b. Control/control > Case/control
///////////////////////////
// Original test statistics
vector<double> original(12,0);
double bg_mean = 0;
double ig1_mean = 0;
double ig2_mean = 0;
int bg_n = 0;
int ig1_n = 0;
int ig2_n = 0;
double bg_var = 0;
double ig1_var = 0;
double ig2_var = 0;
// Add addition 11 tests here...
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++)
{
if ( sample[i]->aff != sample[j]->aff )
{
bg_mean += mdist[j][i];
bg_n++;
}
else if ( sample[i]->aff )
{
ig2_mean += mdist[j][i];
ig2_n++;
}
else
{
ig1_mean += mdist[j][i];
ig1_n++;
}
}
// 0 1a. Case/control < all others
// 1 1b. Case/control > all others
// 2 2a. Case/case < control/control
// 3 2b. Case/case > control/control
// 4 3a. Case/case < all others
// 5 3b. Case/case > all others
// 6 4a. Control/control < all others
// 7 4b. Control/control > all others
// 8 5a. Case/case < Case/control
// 9 5b. Case/case > Case/control
// 10 6a. Control/control < Case/control
// 11 6b. Control/control > Case/control
original[0] -= bg_mean;
original[1] += bg_mean;
original[2] += ig1_mean - ig2_mean;
original[3] += ig2_mean - ig1_mean;
original[4] -= ig2_mean;
original[5] += ig2_mean;
original[6] -= ig1_mean;
original[7] += ig1_mean;
original[8] += bg_mean - ig2_mean;
original[9] += ig2_mean - bg_mean;
original[10] += bg_mean - ig1_mean;
original[11] += ig1_mean - bg_mean;
if (bg_n==0)
error("No between group individuals observed");
double tot_mean = (bg_mean+ig1_mean+ig2_mean)
/(double)(bg_n+ig1_n+ig2_n);
bg_mean /= (double)bg_n;
ig1_mean /= (double)ig1_n;
ig2_mean /= (double)ig2_n;
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++)
{
if ( sample[i]->aff != sample[j]->aff )
bg_var += ( mdist[j][i] - bg_mean ) * ( mdist[j][i] - bg_mean );
else if ( sample[i]->aff )
ig2_var += ( mdist[j][i] - ig2_mean ) * ( mdist[j][i] - ig2_mean );
else
ig1_var += ( mdist[j][i] - ig1_mean ) * ( mdist[j][i] - ig1_mean );
}
// Total sum of squares
double total_ss = bg_var + ig2_var + ig1_var;
// Between sum of squares
double ig_mean = (ig1_mean * ig1_n + ig2_mean * ig2_n)
/ ( double ) ( ig1_n + ig2_n );
double between_ss = (double)bg_n * ( bg_mean - tot_mean ) * ( bg_mean - tot_mean ) +
(double)(ig1_n+ig2_n) * ( ig_mean - tot_mean ) * ( ig_mean - tot_mean );
bg_var /= (double)(bg_n-1);
ig2_var /= (double)(ig2_n-1);
ig1_var /= (double)(ig1_n-1);
printLOG("\nBetween-group IBS (mean, SD) = "
+dbl2str(bg_mean)+", "+dbl2str(sqrt(bg_var))+"\n");
printLOG("In-group (2) IBS (mean, SD) = "
+dbl2str(ig2_mean)+", "+dbl2str(sqrt(ig2_var))+"\n");
printLOG("In-group (1) IBS (mean, SD) = "
+dbl2str(ig1_mean)+", "+dbl2str(sqrt(ig1_var))+"\n");
printLOG("Approximate proportion of variance between group = "
+dbl2str(between_ss / total_ss)+"\n");
////////////////////
// Begin permutations
bool finished = false;
while(!finished)
{
vector<double> pr(12,0);
// Permute
perm.permuteInCluster();
// Retest
bg_mean = ig1_mean = ig2_mean = 0;
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++)
{
if ( sample[i]->pperson->aff != sample[j]->pperson->aff )
{
bg_mean += mdist[j][i];
}
else if ( sample[i]->pperson->aff )
{
ig2_mean += mdist[j][i];
}
else
{
ig1_mean += mdist[j][i];
}
}
pr[0] -= bg_mean;
pr[1] += bg_mean; // are case/control more similar?
pr[2] += ig1_mean - ig2_mean;
pr[3] += ig2_mean - ig1_mean;
pr[4] -= ig2_mean;
pr[5] += ig2_mean;
pr[6] -= ig1_mean;
pr[7] += ig1_mean;
pr[8] += bg_mean - ig2_mean;
pr[9] += ig2_mean - bg_mean;
pr[10] += bg_mean - ig1_mean;
pr[11] += ig1_mean - bg_mean;
////////////////////////////////
// Standard permutation counting
finished = perm.update(pr,original);
}
if (!par::silent)
cout << "\n\n";
////////////////////////////
// Display permuted p-values
printLOG("IBS group-difference empirical p-values:\n\n");
printLOG(" T1: Case/control less similar p = " + dbl2str(perm.pvalue(0)) +"\n");
printLOG(" T2: Case/control more similar p = " + dbl2str(perm.pvalue(1)) +"\n\n");
printLOG(" T3: Case/case less similar than control/control p = " + dbl2str(perm.pvalue(2)) +"\n");
printLOG(" T4: Case/case more similar than control/control p = " + dbl2str(perm.pvalue(3)) +"\n\n");
printLOG(" T5: Case/case less similar p = " + dbl2str(perm.pvalue(4)) +"\n");
printLOG(" T6: Case/case more similar p = " + dbl2str(perm.pvalue(5)) +"\n\n");
printLOG(" T7: Control/control less similar p = " + dbl2str(perm.pvalue(6)) +"\n");
printLOG(" T8: Control/control more similar p = " + dbl2str(perm.pvalue(7)) +"\n\n");
printLOG(" T9: Case/case less similar than case/control p = " +dbl2str(perm.pvalue(8)) +"\n" );
printLOG("T10: Case/case more similar than case/control p = " +dbl2str(perm.pvalue(9)) +"\n\n");
printLOG("T11: Control/control less similar than case/control p = " + dbl2str(perm.pvalue(10)) +"\n");
printLOG("T12: Control/control more similar than case/control p = " + dbl2str(perm.pvalue(11)) +"\n");
}
void Plink::groupGenome()
{
// Read from a (non-verbose) genome file
checkFileExists(par::ibd_file);
if ( par::ibd_read_minimal )
printLOG("Reading IBS estimates (minimal format) from [ "
+par::ibd_file+" ] \n");
else
printLOG("Reading genome-wide IBS estimates from [ "
+par::ibd_file+" ] \n");
ifstream INC;
INC.open(par::ibd_file.c_str());
map<string,int> mperson;
for (int i=0; i<n; i++)
mperson.insert(make_pair( sample[i]->fid+"_"+sample[i]->iid , i ));
map<Individual*,int> mcode;
for (int i=0; i<n; i++)
mcode.insert(make_pair( sample[i] , i ));
vector<Individual*> peeps;
// We wish to read in an NxN matrix, and convert it to a KxK one
matrix_t dk( nk );
sizeMatrix(dk,nk,0);
for (int j=0; j<nk; j++)
dk[j].resize(j,0);
table_t dkn;
sizeTable(dkn,nk,0);
for (int j=0; j<nk; j++)
dkn[j].resize(j,0);
// Read in .genome file in verbose mode
// We only want FID1,IID1,FID2,IID2 (always first four)
// DST and PPC
// Get field codes from header
int dst_code = -1;
int col_length = 0;
double mydst;
vector<string> tokens = tokenizeLine(INC);
col_length = tokens.size();
if ( tokens.size() < 4 ||
tokens[0] != "FID1" ||
tokens[1] != "IID1" ||
tokens[2] != "FID2" ||
tokens[3] != "IID2" )
error("Problem with header row of .genome file");
for ( int i = 4; i<tokens.size(); i++)
{
if ( tokens[i] == "DST" )
dst_code = i;
}
if ( dst_code == -1 )
error("Could not find DST fields in .genome file");
// Read each pair at a time
while ( ! INC.eof() )
{
vector<string> tokens = tokenizeLine(INC);
if ( tokens.size() == 0 )
continue;
if ( col_length != tokens.size() )
{
string strmsg = "";
for (int i=0;i<tokens.size();i++)
strmsg += tokens[i] + " ";
error("Problem reading line in .genome file:\n"+strmsg+"\n");
}
string fid1 = tokens[0];
string iid1 = tokens[1];
string fid2 = tokens[2];
string iid2 = tokens[3];
string idst = tokens[dst_code];
if (fid1=="") continue;
if ( ! from_string<double>( mydst , idst , std::dec) )
mydst = 0;
map<string,int>::iterator person1 = mperson.find(fid1+"_"+iid1);
map<string,int>::iterator person2 = mperson.find(fid2+"_"+iid2);
if ( person1 == mperson.end() ||
person2 == mperson.end() ||
person1 == person2 )
continue;
int k1 = sample[ person1->second ]->sol;
int k2 = sample[ person2->second ]->sol;
if ( k1 < 0 || k2 < 0 || k1 == k2 )
continue;
if ( k2 > k1 )
{
int tmp = k2;
k2 = k1;
k1 = tmp;
}
// Record IBS distance
dk[k1][k2] = mydst;
++dkn[k1][k2];
} // Read next line in .genome
INC.close();
for (int i=0; i<nk; i++)
for (int j=0; j<i; j++)
{
if ( dkn[i][j] > 0 )
dk[i][j] /= (double)dkn[i][j];
}
// Output a dummy .genome file
ofstream GOUT0;
GOUT0.open( (par::output_file_name + ".plst").c_str(), ios::out);
printLOG("Writing person include list to [ " + par::output_file_name + ".plst ]\n");
ofstream GOUT1;
GOUT1.open( (par::output_file_name + ".clst").c_str(), ios::out);
printLOG("Writing cluster list to [ " + par::output_file_name + ".clst ]\n");
map<int,int> k2i;
for (int i=0;i<n; i++)
{
int j = sample[i]->sol;
if ( j < 0 )
continue;
if ( k2i.find(j) == k2i.end() )
{
k2i.insert(make_pair(j,i));
GOUT0 << sample[i]->fid << " "
<< sample[i]->iid << "\n";
GOUT1 << sample[i]->fid << " "
<< sample[i]->iid << " "
<< kname[j] << "\n";
}
}
GOUT0.close();
GOUT1.close();
ofstream GOUT;
GOUT.open( (par::output_file_name + ".genome").c_str(), ios::out);
printLOG("Writing grouped .genome file to [ " + par::output_file_name + ".genome ]\n");
GOUT << setw(par::pp_maxfid) << "FID1" << " "
<< setw(par::pp_maxiid) << "IID1" << " "
<< setw(par::pp_maxfid) << "FID2" << " "
<< setw(par::pp_maxiid) << "IID2" << " "
<< setw(8) << "DST" << " "
<< setw(8) << "PPC" << "\n";
for (int i=0; i<nk; i++)
for (int j=0; j<i; j++)
{
Individual * s1 = sample[k2i.find(i)->second];
Individual * s2 = sample[k2i.find(j)->second];
GOUT << s1->fid << " " << s1->iid << " "
<< s2->fid << " " << s2->iid << " ";
// cout << i << " " << j << " ";
// cout << dk.size() << " " << dk[i].size() << "\n";
// cout << dkn.size() << " " << dkn[i].size() << "\n";
// cout << dk[i][j] << " of " << dkn[i][j] << "\n";
GOUT << dk[i][j] << " 1\n";
}
GOUT.close();
}
|