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 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
|
//////////////////////////////////////////////////////////////////
// //
// 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 //
// //
//////////////////////////////////////////////////////////////////
// Rules:
// ID cannot have any spaces, tabs, commas (,) or plus (+) signs
// Attributes can have commas
#include "idhelp.h"
#include "options.h"
#include "helper.h"
#include "nlist.h"
#include <iomanip>
extern Plink * PP;
map<IDField*,set<IDValue> > IDHelper::parseQuery(string q)
{
map<IDField*,set<IDValue> > lookup;
// Convert a query string into a searchable form
NList tlist(0);
vector<string> ids = tlist.deparseStringList( q );
// Consider each term (comma-delimited list)
map<string,string> qmap;
map<string,string> qjoint;
for ( int i = 0 ; i < ids.size() ; i++)
{
string s = ids[i];
if ( s.find("=") == string::npos )
error("Query should be: ID=value or ATTRIB=value[,ATTRIB=value]");
string fs = s.substr(0, s.find("="));
string vs = s.substr(s.find("=")+1);
// Is this a joint field?
if ( fs.find("+") != string::npos )
{
// We need to split both fs and vs: they must have the same
// number of entries on both sides
vector<string> flist =
tlist.deparseStringList( searchAndReplace( fs,"+",",") );
vector<string> vlist =
tlist.deparseStringList( searchAndReplace( vs,"+",",") );
if ( flist.size() != vlist.size() )
error("Joint query wrong: " + fs + " = " + vs);
for (int i=0; i<flist.size(); i++)
{
qmap.insert(make_pair(flist[i],vlist[i]));
qjoint.insert(make_pair(flist[i],vs));
}
}
else
{
qmap.insert(make_pair(fs,vs));
}
}
// Check that all named fields exist
map<string,string>::iterator i = qmap.begin();
while ( i != qmap.end() )
{
string fs = i->first;
map<string,IDField*>::iterator f = fieldMap.find( fs );
if ( f == fieldMap.end() )
error("Cannot find field " + fs );
IDField * thisField = f->second;
IDValue t;
t.field = thisField;
t.value = i->second;
if ( t.field->equiv )
t.updateAlias();
//////////////////////////////////////////////////
// Is this query being framed as a joint field?
map<string,string>::iterator k = qjoint.find( fs );
if ( k != qjoint.end() )
{
t.jointValue = k->second;
t.field->joint = true;
}
else
{
t.field->joint = false;
}
map<IDField*,set<IDValue> >::iterator j = lookup.find( thisField );
if ( j == lookup.end() )
{
set<IDValue> ts;
ts.insert(t);
lookup.insert(make_pair(thisField,ts) );
}
else
j->second.insert(t);
++i;
}
return lookup;
}
bool IDHelper::matchIndividual(IDGroup * group,
map<IDField*,set<IDValue> > & matchTemplate )
{
bool match = true;
// Compare
// map<IDField*, set<IDValue> > lookupValues;
// with this group
int found = 0;
for (int g=0; g<group->values.size(); g++)
{
IDField * f = group->values[g]->field;
map<IDField*, set<IDValue> >::iterator mf = matchTemplate.find( f );
if ( mf == matchTemplate.end() )
continue;
++found;
// The observed value
IDValue * myValue = group->values[g];
set<IDValue>::iterator j = mf->second.begin();
bool matchField = false;
while ( j != mf->second.end() )
{
if ( *myValue == *j )
{
matchField = true;
}
++j;
}
if ( ! matchField )
return false;
}
// Did we get a look at all the match fields?
if ( found != matchTemplate.size() )
{
return false;
}
// If still here, we must match
return true;
}
void IDHelper::setJointValues( set<IDValue> & val )
{
// Make a dummy ID group, with pointers to the originals
IDGroup g;
set<IDValue>::iterator i = val.begin();
while ( i != val.end() )
{
g.values.push_back( (IDValue*)&(*i) );
++i;
}
// Use main joint-value update function
setJointValues( &g );
// We will add "jointValue" attribs to val, but no need
// to remove any items
}
void IDHelper::setJointValues( IDGroup * group )
{
// Find the joint fields and edit in values
// Similar to the above function, except here if the joint values
// are completely missing, then we need to remove the entire set
// from the ID group
for (int j = 0 ; j < jointField.size(); j++ )
{
set<IDField*> & jf = jointField[j];
vector<IDField*> & jo = jointOrder[j];
// Does this group contain one of these
// joint fields?
bool hasJoint = false;
map<IDField*,int> mapback;
for (int j = 0 ; j < group->values.size(); j++)
{
if ( jf.find( group->values[j]->field ) != jf.end() )
{
hasJoint = true;
mapback.insert( make_pair( group->values[j]->field , j ) );
}
}
if ( ! hasJoint )
continue;
string jointValue = "";
bool doneFirst = false;
bool jointMissing = false;
bool jointOneSeen = false;
set<IDField*>::iterator k = jf.begin();
while ( k != jf.end() )
{
map<IDField*,int>::iterator mi = mapback.find( *k );
if ( mi == mapback.end() )
{
jointMissing = true;
if ( doneFirst )
jointValue += "+.";
else
{
jointValue += ".";
doneFirst = true;
}
}
else
{
jointOneSeen = true;
if ( doneFirst )
jointValue += "+" + group->values[ mi->second ]->value;
else
{
jointValue += group->values[mi->second]->value;
doneFirst = true;
}
}
++k;
}
// Update values accordingly
// Except, remove entirely missing values
vector<bool> mask( group->values.size(), false);
set<IDField*>::iterator k2 = jf.begin();
while ( k2 != jf.end() )
{
map<IDField*,int>::iterator mi = mapback.find( *k2 );
if ( mi != mapback.end() )
{
int j = mi->second;
if ( jointMissing && ! jointOneSeen )
mask[j] = true;
else
group->values[j]->jointValue = jointValue;
}
++k2;
}
// Remove entirely missing values
if ( jointMissing && ! jointOneSeen )
{
vector<IDValue*> newValues = group->values;
group->values.clear();
for ( int i = 0 ; i < mask.size() ; i++)
{
if ( ! mask[i] )
group->values.push_back( newValues[i] );
}
}
}
return;
}
IDGroup * IDHelper::findUniqueIndividual( set<IDValue> & matchTemplate )
{
set<IDGroup *> thisGroup;
set<IDValue>::iterator k = matchTemplate.begin();
while ( k != matchTemplate.end() )
{
// Hmm need to decide how to handle: is default joint or single lookup?
// for IDValues?
map<IDValue,set<IDGroup*> >::iterator i = idmap.find( *k );
if ( i != idmap.end() )
{
set<IDGroup*>::iterator j = i->second.begin();
while ( j != i->second.end() )
{
if ( ! (*j)->resolved )
thisGroup.insert( *j );
++j;
}
}
++k;
}
if ( thisGroup.size() > 1 )
error("Internal problem: found more than one match when expecting a unique match");
return *thisGroup.begin();
}
set<IDGroup*> IDHelper::findAllIndividuals( map<IDField*,set<IDValue> > & matchTemplate )
{
set<IDGroup*> matched;
for ( int g = 0 ; g < idgroup.size(); g++ )
{
IDGroup * group = idgroup[g];
if ( matchIndividual( group , matchTemplate ) )
matched.insert( group );
}
return matched;
}
bool IDHelper::setAlias(IDField * myField, string val, int f, map<IDField*,string> & originalEquivalence)
{
bool needToStore = true;
map<IDField*,string>::iterator i = originalEquivalence.find( myField );
if ( i == originalEquivalence.end() )
{
set<string>::iterator k = myField->aliasList.find( val );
if ( k != myField->aliasList.end() )
error("Alias specified more than once in "
+ files[f].filename + " : "
+ myField->name + " " + val );
myField->masterList.insert( val );
originalEquivalence.insert(make_pair( myField, val ));
}
else
{
// We need to add an equivalence value to this field?
map<string, string>::iterator k = myField->eqid.find( val );
// We only let each alias be specified once
if ( k != myField->eqid.end() )
error("Alias specified more than once in "
+ files[f].filename + " : "
+ myField->name + " " + val );
k = myField->eqid.find( i->second );
if ( k != myField->eqid.end() )
error("Alias specified more than once in "
+ files[f].filename + " : "
+ myField->name + " " + i->second);
if ( myField->masterList.find( val ) != myField->masterList.end() )
error("Alias specified more than once in "
+ files[f].filename + " : "
+ myField->name + " " + val );
// Keep track of this alias
myField->eqid.insert( make_pair ( val , i->second ) );
myField->aliasList.insert( val );
// And now we do not need to store this particular value
// as a distinct field
needToStore = false;
}
return needToStore;
}
void IDHelper::idHelp()
{
// Turn off the "-" initiated range-delimiting (i.e. so that
// IDs can have hyphens)
par::range_delimiter = " ";
// Are we only performing a "simple match", performed without
// a dictionary being specified? If so, jump there now, creating
// the dictionary on the fly.
if ( par::idhelp_match && par::idhelp_no_dict )
{
idMatch();
return;
}
// 1. Read in dictionary, and make the fields and files
// Contains files (and can be full path) and description of each field
// {file name} {col names } : { rules }
// e.g.
// ../files/id1.txt FID IID FID1 FID2 : uniq=FID,IID uniq=FID1,IID2
// ../names/id.lst ID2 ID23 : missing=NA,---,0
// ../names/id2.lst ID3 : equiv
//
// note: for "equiv" files, assume all IDs on same line are equivalent,
// only one ID can be specified here
PP->printLOG("ID helper, with dictionary [ " + par::idhelp_dictionary + " ]\n");
checkFileExists( par::idhelp_dictionary );
ifstream DICT( par::idhelp_dictionary.c_str() , ios::in );
while ( ! DICT.eof() )
{
vector<string> tokens = tokenizeLine( DICT );
// Needs atleast three fields:
// filename, and two IDs
if ( tokens.size() == 0 )
continue;
if ( tokens.size() < 2 || tokens[1] == ":" )
error("Expecting at least 2 fields w/ 1 ID in every dictionary row\n");
IDFile d;
checkFileExists( tokens[0] );
d.filename = tokens[0];
int p = 1;
while (1)
{
if ( tokens[p] == ":" )
{
++p;
break;
}
// Is this a new field name?
IDField f;
f.name = tokens[p];
// Set to skip this field?
if ( f.name == "." )
f.null = f.attribute = true;
iField = fields.find( f );
if ( iField == fields.end() )
{
fields.insert( f );
iField = fields.find( f );
}
// Track that this field was found in this file
IDField * ip = (IDField*)&( *iField );
d.fields.push_back( ip );
++d.uniqFieldCount;
// Consider the next field in this file
if ( ++p == tokens.size() )
break;
}
bool seenJoint = false;
bool seenAttrib = false;
// Now read any rules
if ( p < tokens.size() )
{
while (1)
{
string cmd = tokens[p];
// Commands
// attrib=X,Y,Z
// joint=X,Y
// set:X=Y
// header
// missing=NA,-9
// alias-delimit=|
bool parsed = false;
if ( cmd.size() > 6 && cmd.substr(0,6) == "joint=" )
{
parsed = true;
bool seenJoint = true;
string u = cmd.substr(6);
string jName = searchAndReplace(u,","," ");
// This should contain at least two ID fields
// These fields must always then appear together in
// any file that features at least one
NList tlist(0);
vector<string> ids = tlist.deparseStringList( u );
if ( ids.size() < 2 )
error("Problem with specification of : " + cmd );
set<string> t;
for (int i = 0 ; i < ids.size() ; i++)
{
t.insert(ids[i]);
}
for (int i = 0 ; i < ids.size() ; i++)
jointMap.insert( make_pair( ids[i] , t ) );
set<IDField*> jointf;
vector<IDField*> jointo;
for (int i = 0 ; i < ids.size() ; i++)
{
IDField f;
f.name = ids[i];
iField = fields.find( f );
if ( iField == fields.end() )
{
error("Could not find field " + ids[i]
+ " which is specified as joint");
}
IDField * ip = (IDField*)&( *iField );
ip->joint = true;
ip->jointName = jName;
jointf.insert( ip );
jointo.push_back( ip );
}
jointField.push_back( jointf );
jointOrder.push_back( jointo );
}
if ( cmd.size() > 7 && cmd.substr(0,7) == "attrib=" )
{
parsed = true;
seenAttrib = true;
string u = cmd.substr(7);
NList tlist(0);
vector<string> ids = tlist.deparseStringList( u );
for (int i = 0 ; i < ids.size() ; i++)
attribFields.insert( ids[i] );
}
if ( cmd.size() > 8 && cmd.substr(0,8) == "missing=" )
{
parsed = true;
string u = cmd.substr(8);
NList tlist(0);
vector<string> ids = tlist.deparseStringList( u );
for (int i = 0 ; i < ids.size() ; i++)
d.missingValues.insert( ids[i] );
}
if ( cmd.size() > 4 && cmd.substr(0,4) == "set:" )
{
parsed = true;
if ( seenJoint )
error("Must specify set:X=Y before joint=X,Z");
if ( seenAttrib )
error("Must specify set:X=Y before attrib=X");
string u = cmd.substr(4);
bool okay = true;
if ( u.find("=") == string::npos )
okay = false;
else
{
string u1 = u.substr(0,u.find("="));
string u2 = u.substr(u.find("=")+1);
if ( u1.size() < 1 )
okay = false;
if ( u2.size() < 1 )
okay = false;
if ( okay )
{
d.injections.insert(make_pair(u1,u2));
IDField f;
f.name = u1;
if ( f.name == "." )
error("Cannot set field value name to .");
iField = fields.find( f );
if ( iField == fields.end() )
{
fields.insert( f );
iField = fields.find( f );
}
// Track that this field was found in this file
IDField * ip = (IDField*)&( *iField );
d.fields.push_back( ip );
++d.uniqFieldCount;
}
}
if ( ! okay )
error("Badly formed set:X=V command");
}
if( cmd == "header" || cmd == "hasHeader" )
{
parsed = true;
d.hasHeader = true;
}
// if ( cmd.size() > 10 && cmd.substr(0,10) == "delimiter=" )
// {
// string u = cmd.substr(10);
// if ( u.size() != 1 )
// error("Delimiters can only be a single character length");
// d.delimit = u;
// }
if ( cmd.size() > 16 && cmd.substr(0,16) == "alias-delimiter=" )
{
parsed = true;
string u = cmd.substr(16);
if ( u.size() != 1 )
error("Delimiters can only be a single character length");
if ( u == d.delimit )
error("Delimiter and alias delimiter cannot be the same value");
d.alias_delimit = u;
}
if ( ! parsed )
error("Could not parse the following rule in the ID dictionary: " + cmd );
if ( ++p == tokens.size() )
break;
}
}
files.push_back(d);
// Next line
}
DICT.close();
PP->printLOG("Read " + int2str( fields.size() ) + " unique fields\n");
set<IDField>::iterator i = fields.begin();
while ( i != fields.end() )
{
fieldMap.insert(make_pair( i->name , (IDField*)&(*i) ));
++i;
}
/////////////////////////////////////////////////
// Set flags for any attribute fields
if ( attribFields.size() > 0 )
{
PP->printLOG(" Attribute fields: ");
set<string>::iterator i = attribFields.begin();
while ( i != attribFields.end() )
{
if ( fieldMap.find( *i ) == fieldMap.end() )
error("Cannot find specified attribute "
+ *i
+ " -- please check your dictionary file");
// Set as an attribute field
fieldMap.find(*i)->second->attribute = true;
PP->printLOG( *i + " " );
++i;
}
PP->printLOG("\n");
}
/////////////////////////////////////////////////
// If set fields, check either joint or attrib
for ( int f = 0 ; f < files.size() ; f++ )
{
map<string,string>::iterator i = files[f].injections.begin();
while ( i != files[f].injections.end() )
{
IDField * f = fieldMap.find( i->first )->second;
if ( ! ( f->joint || f->attribute ) )
error("Any set:field=value should be an attribute or a joint field");
++i;
}
}
////////////////////////////////////////////////
// If joint fields, check always all specified
for (int j = 0 ; j < jointField.size(); j++ )
{
set<IDField*> & jf = jointField[j];
for ( int f = 0 ; f < files.size() ; f++ )
{
for ( int j = 0; j < files[f].fields.size(); j++)
{
if ( jointMap.find( files[f].fields[j]->name ) != jointMap.end() )
{
map<string,set<string> >::iterator i = jointMap.find( files[f].fields[j]->name );
set<string> & ss = i->second;
set<string>::iterator is = ss.begin();
while ( is != ss.end() )
{
bool okay = false;
for ( int j = 0; j < files[f].fields.size(); j++)
if ( *is == files[f].fields[j]->name )
okay =true;
if ( ! okay )
error("Need to specify all joint fields in dictionary, [" + files[f].filename + " ]");
++is;
}
}
}
}
}
if ( jointField.size() > 0 )
{
PP->printLOG(" Joint fields:");
for (int j = 0 ; j < jointField.size(); j++ )
{
set<IDField*> & jf = jointField[j];
set<IDField*>::iterator j_iter = jf.begin();
PP->printLOG(" { ");
while ( j_iter != jf.end() )
{
PP->printLOG( (*j_iter)->name + " " );
++j_iter;
}
PP->printLOG(" }");
}
PP->printLOG("\n");
}
/////////////////////////////////
// 2. Read in and index all IDs
for ( int f = 0 ; f < files.size() ; f++ )
{
IDFile * file = &files[f];
PP->printLOG("Reading [ " + files[f].filename + " ] with fields : ");
for ( int j = 0 ; j < files[f].fields.size(); j++ )
{
if (j>0 )
PP->printLOG(", ");
PP->printLOG( files[f].fields[j]->name );
}
////////////////////////////
// Find any equivalence sets
bool foundEquiv = false;
map<string, vector<int> > equiv;
map<string, map<int,int> > equivMap;
for ( int j = 0 ; j < files[f].fields.size(); j++)
{
string n = files[f].fields[j]->name;
map<string, vector<int> >::iterator i = equiv.find( n );
if ( i == equiv.end() )
{
vector<int> t;
t.push_back(j);
equiv.insert(make_pair( n , t ));
}
else
{
i->second.push_back(j);
files[f].fields[j]->equiv = true;
foundEquiv = true;
}
}
if ( foundEquiv )
{
PP->printLOG(" : ");
map<string, vector<int> >::iterator i = equiv.begin();
while ( i != equiv.end() )
{
if ( i->second.size() > 1 )
{
map<int,int> tmap;
PP->printLOG(" "+files[f].fields[i->second[0]]->name + "(");
for (int k = 0 ; k < i->second.size(); k++)
{
if ( k>0 )
{
if ( k==1 )
PP->printLOG("<-");
else
PP->printLOG(",");
tmap.insert(make_pair( i->second[k] , i->second[0] ) );
}
PP->printLOG(int2str( i->second[k]+1 ));
}
PP->printLOG(")");
equivMap.insert( make_pair( files[f].fields[i->second[0]]->name , tmap ));
}
++i;
}
}
PP->printLOG("\n");
////////////////////////////////////
// Read the raw data
ifstream ID1( files[f].filename.c_str() , ios::in );
if ( files[f].hasHeader )
{
vector<string> header = tokenizeLine( ID1 );
}
while ( !ID1.eof() )
{
vector<string> tokens = tokenizeLine( ID1 );
if ( tokens.size() == 0 )
continue;
// Insert SET:X=Y values here
map<string,string>::iterator i = files[f].injections.begin();
while ( i != files[f].injections.end() )
{
tokens.push_back( i->second );
// Note -- this won't be same order -- need to check/fix this???
++i;
}
if ( tokens.size() != file->uniqFieldCount )
{
PP->printLOG("\n\nIn [ " + file->filename
+ " ] encountered a row with the wrong number of fields\n");
PP->printLOG("Found " + int2str( tokens.size() )
+ " fields but expecting " + int2str( file->fields.size() ) + "\n");
int mx = tokens.size() > file->uniqFieldCount ? tokens.size() : file->uniqFieldCount ;
for (int j = 0 ; j < mx ; j++)
{
if ( j < file->uniqFieldCount )
PP->printLOG( " " + file->fields[j]->name + ":\t" );
else
PP->printLOG( " {?}:\t" );
if ( j < tokens.size() )
PP->printLOG( " " + tokens[j] + "\n" );
else
PP->printLOG( " {?}\n" );
}
error("Problem with [ " + file->filename + " ]\n" );
}
IDGroup * g = new IDGroup;
// Track which file this group of IDs came form
g->file = file;
// Track what the original eq-value is for this line
map<IDField*,string> originalEquivalence;
for ( int j = 0 ; j < files[f].fields.size(); j++ )
{
// Is this a missing value?
if ( files[f].missingValues.find( tokens[j] ) != files[f].missingValues.end() )
continue;
IDField * myField = files[f].fields[j];
if ( myField->null )
continue;
string val = tokens[j];
// Handle equivalence specifications (aliases)
bool needToStore = true;
if ( val.find( files[f].alias_delimit ) != string::npos )
{
// This is now an equiv. field
myField->equiv = true;
NList nl(0);
nl.setDelimiter( files[f].alias_delimit );
nl.setRangeChar(" "); // allow hyphens
vector<string> atoken = nl.deparseStringList( val );
string first = "";
for ( int i=0; i<atoken.size(); i++)
{
if ( files[f].missingValues.find( atoken[i] ) == files[f].missingValues.end() )
{
setAlias( myField, atoken[i] , f, originalEquivalence );
if ( first=="" ) first = atoken[i];
}
}
// Store the first value as the main value
if ( first != "" )
tokens[j] = first;
else
continue; // if all missing
}
else if ( myField->equiv )
needToStore = setAlias( myField, tokens[j] , f , originalEquivalence );
if ( needToStore )
{
IDValue * v = new IDValue;
v->field = myField;
v->value = tokens[j];
g->values.push_back(v);
// for pretty-printing
if ( v->value.size() + 3 > myField->width )
myField->width = v->value.size() + 3 ;
}
}
idgroup.push_back(g);
}
ID1.close();
}
/////////////////////////////////
// Done reading in the raw data
// cout << "DISPLAY LEVEL 0\n";
// for ( int g = 0 ; g < idgroup.size(); g++ )
// idgroup[g]->display();
// cout << "-------------------------------------------\n";
//////////////////////////////////////////////////////////////
// 1. Swap in preferred values for any equiv fields
for ( int g = 0 ; g < idgroup.size(); g++ )
{
IDGroup * group = idgroup[g];
for (int j = 0 ; j < group->values.size(); j++)
if ( group->values[j]->field->equiv )
group->values[j]->updateAlias();
}
//////////////////////////////////////////////////////////////
// 1b. Compile joint fields into joint values
for ( int g = 0 ; g < idgroup.size(); g++ )
{
IDGroup * group = idgroup[g];
setJointValues( idgroup[g] );
}
////////////////////////////////
// 2. Create the idmap
for ( int g = 0 ; g < idgroup.size(); g++ )
{
IDGroup * group = idgroup[g];
for (int j = 0 ; j < group->values.size(); j++)
{
IDValue & v = *(group->values[j]);
map<IDValue,set<IDGroup*> >::iterator i = idmap.find( *(group->values[j]) );
if ( i == idmap.end() )
{
set<IDGroup*> t;
t.insert(group);
idmap.insert(make_pair( *(group->values[j]) , t) );
}
else
{
i->second.insert( group );
}
}
}
///////////////////////////////////////////////
// 2.5 Simple match in any file?
if ( par::idhelp_dump_from_dict )
{
idDump();
return;
}
///////////////////////////////////////////////
// 3. Attempt to resolve into a single table
bool okay = true;
map<string,string> problem ;
while (1)
{
bool allDone = true;
for ( int g = 0 ; g < idgroup.size(); g++ )
{
IDGroup * group = idgroup[g];
// Has this group already been assigned to a person?
if ( group->resolved )
continue;
// Find all other groups (resolved or otherwise) that this group
// matches with, but ignoring attributes
set<IDGroup*> matches;
for (int j = 0 ; j < group->values.size(); j++)
{
// Skip matching on attributes
if ( group->values[j]->field->attribute )
continue;
map<IDValue, set<IDGroup*> >::iterator i = idmap.find( *(group->values[j]) );
if ( i != idmap.end() )
{
set<IDGroup*>::iterator i2 = i->second.begin();
while ( i2 != i->second.end() )
{
matches.insert( *i2 );
++i2;
}
}
}
//////////////////////////////
// Merge into the key group
// Make a set of the key groups IDValues
map<IDField*,IDValue*> keyValues;
for ( int j = 0; j < group->values.size(); j++ )
{
keyValues.insert( make_pair( group->values[j]->field, group->values[j] ));
}
set<IDGroup*>::iterator i0 = matches.begin();
while ( i0 != matches.end() )
{
if ( *i0 == group )
{
++i0;
continue;
}
// Step through all the values in this matching group
for ( int k = 0; k < (*i0)->values.size(); k++)
{
// Does the key have this field?
IDField * f = (*i0)->values[k]->field;
map<IDField*,IDValue*>::iterator i = keyValues.find( f );
if ( i == keyValues.end() )
{
// Insert this key value into place
IDValue * t = new IDValue;
t->field = f;
t->value = (*i0)->values[k]->value;
t->jointValue = (*i0)->values[k]->jointValue;
group->values.push_back(t);
allDone = false;
// Keep track of what has been added, so we don't add twice
keyValues.insert( make_pair( f , (*i0)->values[k] ));
}
else
{
// Something already exists -- check it is not inconsistent
// as if is an ID, i.e. attributes are allowed to not be
// unique by definition
if ( *((*i0)->values[k]) != *(i->second) )
{
okay = false;
string title = "Two unique entries [ " + (*i0)->values[k]->field->name + " = ";
if ( (*i0)->values[k]->field->joint )
{
if ( (*i0)->values[k]->jointValue <= i->second->jointValue )
title += (*i0)->values[k]->jointValue + " and " + i->second->jointValue + " (joint)";
else
title += i->second->jointValue + " and " + (*i0)->values[k]->jointValue + " (joint)";
}
else
{
if ( (*i0)->values[k]->value <= i->second->value )
title += (*i0)->values[k]->value + " and " + i->second->value;
else
title += i->second->value + " and " + (*i0)->values[k]->value;
}
title += " ] that match elsewhere";
if ( problem.find( title ) == problem.end() )
{
string p = "\n a) ";
for (int z=0; z<group->values.size(); z++)
p += group->values[z]->field->name + "="
+ group->values[z]->value + " ";
p += "\n";
p += " b) ";
for (int z=0; z<(*i0)->values.size(); z++)
p += (*i0)->values[z]->field->name + "="
+ (*i0)->values[z]->value + " ";
p += "\n\n";
problem.insert(make_pair(title,p));
}
}
}
}
// We are now done with this IDGroup
(*i0)->resolved = true;
++i0;
}
}
if ( allDone )
break;
}
if ( ! okay )
{
PP->printLOG("\n\n*** Problems were detected in the ID lists:\n\n");
map<string,string>::iterator p = problem.begin();
while ( p != problem.end() )
{
PP->printLOG( p->first + p->second );
++p;
}
error("You need to fix the above problems");
}
//////////////////////////////////////////////////////////////
// Update the IDMAP, now only for resolved values
// value -> idgroup
idmap.clear();
for ( int g = 0 ; g < idgroup.size(); g++ )
{
if ( idgroup[g]->resolved )
continue;
IDGroup * group = idgroup[g];
for (int j = 0 ; j < group->values.size(); j++)
{
IDValue & v = *(group->values[j]);
map<IDValue,set<IDGroup*> >::iterator i = idmap.find( *(group->values[j]) );
if ( i == idmap.end() )
{
set<IDGroup*> t;
t.insert(group);
idmap.insert(make_pair( *(group->values[j]) , t) );
}
else
{
i->second.insert( group );
}
}
}
// cout << "DISPLAY LEVEL 1\n";
// for ( int g = 0 ; g < idgroup.size(); g++ )
// idgroup[g]->display();
// cout << "-------------------------------------------\n";
//////////////////////////////////////////////////////////////
// 4. Figure out actual transformation required, and perform
// Rules: ID cannot contain whitespace or commas
// Can contain "-", "_", "=", ".", etc.
// IDs are case-sensitive
// Cannot contain "+" or ","
// Values if "." are taken to mean not known, n/a
// Functions: dump all fields on this person, or group
if ( par::idhelp_list_aliases )
// Lookup a single person --id-lookup ID=27364883-1
// or match on an attribute --id-lookup SITE=Boston
// Dump the entire table (DEFAULT)
// or subset of cols --id-table ID,CLIN_ID,BSP_ID
// Take an existing file and replace a field
// --id-replace [header|noheader,field=N,skip|miss|warn|list] mydata.txt ID1 ID2
// Default behavior is to put 'missing' as the field
// default = to autodetect a header field
// skip (do not print these lines)
// miss (print a missing ID code)
// warn (do not allow if >1 missing)
// list (print only these lines, ID in file not in DB)
// Take an existing file and replace a field {file} {field} {old ID} {new ID}
// --id-replace mydata.txt ID1 CLIN_ID
// par::idhelp_command =
// dump_table, dump_subtable, replace, lookup
if ( par::idhelp_list_aliases )
{
idListAlias();
return;
}
////////////////////////////////////////////////////////////////////////
// Replace mode
if ( par::idhelp_replace )
{
idReplace();
return;
}
///////////////////////////////////////////////////////////////
// Line up 1 or more files based on the first file
if ( par::idhelp_match )
{
idMatch();
return;
}
/////////////////////////////////////////////////////////////////////////
// Lookup functions
set<string> subsetFields;
map<IDField*, set<IDValue> > lookupValues;
if ( par::idhelp_subset )
{
NList tlist(0);
vector<string> ids = tlist.deparseStringList( par::idhelp_subset_string );
for (int i=0; i<ids.size(); i++)
{
if ( fieldMap.find(ids[i]) == fieldMap.end() )
error("Cannot find field " + ids[i] );
subsetFields.insert(ids[i]);
}
}
if ( par::idhelp_lookup )
{
// Input should be in form of a comma delimited list ID=value,
// or, attrib=value. More than on attrib is allowed, as a comma-
// delimited list. Only one ID can be specified (although it may be
// a joint one)
// If same attrib C=2,C=1,D=1
// e.g. ( C==2 OR C==1 ) AND (D==1)
// Use "+" to identify a joint ID set
// FID+IID=6363723+8383293
lookupValues = parseQuery( par::idhelp_lookup_string );
PP->printLOG("Looking up items matching: ");
// These will be sorted in field name order, so we
// can easily figure out OR versus AND conditions
map<IDField*,set<IDValue> >::iterator i = lookupValues.begin();
while ( i != lookupValues.end() )
{
set<IDValue>::iterator j = i->second.begin();
PP->printLOG( "\n " + i->first->name + " = " );
while ( j != i->second.end() )
{
PP->printLOG( j->value + " " );
++j;
}
if ( i->first->attribute ) PP->printLOG(" (attribute)");
else PP->printLOG(" (id)");
++i;
}
PP->printLOG("\n");
}
//////////////////////////////////////////////////////////
// Main output routine
PP->printLOG("Writing output to [ "
+ par::output_file_name + ".id ]\n");
ofstream OFILE( (par::output_file_name+".id").c_str() , ios::out );
// Header row
set<IDField>::iterator f = fields.begin();
while ( f != fields.end() )
{
if ( f->null )
{
++f;
continue;
}
if ( (! par::idhelp_subset )
|| subsetFields.find( f->name )!=subsetFields.end() )
OFILE << setw(f->width) << f->name << par::idhelp_output_delimit << " ";
++f;
}
OFILE << "\n";
// Keep track of how many records we retrieve
int numMatched = 0;
for ( int g = 0 ; g < idgroup.size(); g++ )
{
IDGroup * group = idgroup[g];
// Has this group already been assigned to a person?
if ( group->resolved )
continue;
// Make a set of the key groups IDValues
// If doing a lookup, we might also need to include
// all fields here
map<IDField*,IDValue*> keyValues;
for ( int j = 0; j < group->values.size(); j++ )
{
if ( par::idhelp_lookup ||
( ! par::idhelp_subset ) ||
subsetFields.find( group->values[j]->field->name )!=subsetFields.end() )
keyValues.insert( make_pair( group->values[j]->field, group->values[j] ));
}
///////////////////////////////////////////////////
// If we are filtering, does this person match?
if ( par::idhelp_lookup )
{
if ( ! matchIndividual( group, lookupValues ) )
continue;
}
++numMatched;
///////////////////////////////////////////
// Print row, in same order for all fields
set<IDField>::iterator f = fields.begin();
while ( f != fields.end() )
{
if ( f->null )
{
++f;
continue;
}
if ( (! par::idhelp_subset)
|| subsetFields.find( f->name ) != subsetFields.end() )
{
map<IDField*,IDValue*>::iterator k = keyValues.find( (IDField*)&(*f) );
if ( k == keyValues.end() )
OFILE << setw( f->width ) << "." << par::idhelp_output_delimit << " ";
else
{
OFILE << setw( f->width ) << k->second->value << par::idhelp_output_delimit << " ";
}
}
++f;
}
OFILE << "\n";
}
OFILE.close();
PP->printLOG( int2str( numMatched ) + " unique records retrieved\n");
}
void IDHelper::idListAlias()
{
PP->printLOG("Listing ID equivalents/aliases to [ " + par::output_file_name + ".id.eq ]\n");
ofstream O1( (par::output_file_name + ".id.eq").c_str() , ios::out );
O1 << setw(20) << "FIELD" << " "
<< setw(20) << "PREF" << " "
<< setw(20) << "EQUIV" << "\n";
map<string,IDField*>::iterator i1 = fieldMap.begin();
while ( i1 != fieldMap.end() )
{
if ( i1->second->equiv )
{
IDField * f = i1->second;
map<string,string>::iterator j = f->eqid.begin();
while ( j != f->eqid.end() )
{
O1 << setw(20) << i1->first << " "
<< setw(20) << j->second << " "
<< setw(20) << j->first << "\n";
++j;
}
}
++i1;
}
O1.close();
return;
}
void IDHelper::idReplace()
{
NList nl(0);
vector<string> tok = nl.deparseStringList( par::idhelp_replace_string );
if ( tok.size() != 3 )
error("Problem with --id-replace string format\n");
checkFileExists( tok[0] );
// This/these are the fields are
vector<int> rep_field;
NList tlist(0);
string t = searchAndReplace( tok[1] , "+" , "," );
vector<string> targetFields = tlist.deparseStringList( t );
for ( int i=0; i<targetFields.size(); i++)
if ( fieldMap.find( targetFields[i] ) == fieldMap.end() )
error("Cannot find target field [ " + targetFields[i] + " ]");
if ( fieldMap.find( tok[2] ) == fieldMap.end() )
error("Cannot find replacement field [ " + tok[2] + " ]" );
string fname = tok[2];
IDField * f = fieldMap.find( tok[2] )->second;
if ( f->joint )
fname = f->jointName;
PP->printLOG("Replacing " + tok[1] + " with "
+ fname + " from [ " + tok[0] + " ]\n");
PP->printLOG("Writing new file to [ " + par::output_file_name + ".rep ]\n");
OptionSet * id_opt = par::opt.getOptions("IDHELP");
bool skipMode = id_opt->isSet("skip");
bool missMode = id_opt->isSet("miss");
bool warnMode = id_opt->isSet("warn");
bool listMode = id_opt->isSet("list");
int c = 0;
if ( skipMode )
{
PP->printLOG("Set to skip unmatched observations\n");
++c;
}
if ( missMode )
{
PP->printLOG("Set to set unmatched observations to NA\n");
++c;
}
if ( warnMode )
{
PP->printLOG("Set to give error for first unmatched observation\n");
++c;
}
if ( listMode )
{
PP->printLOG("Set to list only IDs in file but not in database\n");
++c;
}
if ( c == 0 )
PP->printLOG("Set to keep original value for unmatched observations\n");
if ( c>1 )
error("Can only specify one of [miss|warn|skip|list] options in --id-replace");
// Do we have a header row?
bool header = false;
if ( id_opt->isSet("header") )
header = true;
// If not, we need a number specified
if ( ! header )
{
string field_str;
if ( id_opt->isSet("field") )
field_str = id_opt->getValue("field");
else
error("Need to specify field={N,N} if no header");
// Convert field str to vector of fields
NList tlist(0);
string t = searchAndReplace( field_str , "+" , "," );
vector<string> ids = tlist.deparseStringList( t );
if ( ids.size() != targetFields.size() )
error("Must specify the same number of fields/cols");
rep_field.resize( ids.size() , -1 );
for (int i=0; i<ids.size(); i++)
{
if ( ! from_string<int>( rep_field[i], ids[i] , std::dec ) )
error("Problem with field specified in --id-replace options");
// Make 0-based
--rep_field[i];
}
}
else
{
// Lookup in header row
ifstream IN1( tok[0].c_str() , ios::in );
vector<string> tokens = tokenizeLine( IN1 );
rep_field.resize( targetFields.size() , -1 );
for (int f = 0 ; f < targetFields.size(); f++)
for (int i = 0 ; i < tokens.size(); i++)
{
if ( tokens[i] == targetFields[f] )
rep_field[f] = i;
}
for (int i=0; i<rep_field.size(); i++)
if ( rep_field[i] == -1 )
error("Could not find field in header for --id-replace");
IN1.close();
IN1.clear();
}
int insertField = rep_field[0];
/////////////////////////////////////////////
// Load input file
ifstream IN1( tok[0].c_str() , ios::in );
ofstream OUT1( (par::output_file_name+".rep").c_str() , ios::out );
int notFound = 0;
bool readHeader = false;
if ( ! header )
readHeader = true;
int maxfield = 0;
for (int i=0; i<rep_field.size(); i++)
if ( rep_field[i] > maxfield )
maxfield = rep_field[i];
while( ! IN1.eof() )
{
vector<string> tokens = tokenizeLine( IN1 );
if ( tokens.size() == 0 )
continue;
if ( tokens.size() <= maxfield )
error("Not enough columns here");
bool changed = false;
// Deal with header row?
if ( ! readHeader )
{
for (int i=0; i<rep_field.size(); i++)
{
if ( i == 0 )
tokens[ rep_field[i] ] = fname;
else
tokens[ rep_field[i] ] = "";
}
readHeader = true;
changed = true;
}
else
{
// For actual data, read all fields, and find the
// individual that matches
set<IDValue> myTemplate;
for (int i=0; i<rep_field.size(); i++)
{
IDValue findField;
findField.field = fieldMap.find( targetFields[i] )->second;
findField.value = tokens[ rep_field[i] ];
if ( findField.field->equiv )
findField.updateAlias();
myTemplate.insert( findField );
}
// Connect up any joint fields
setJointValues( myTemplate );
// Find the match
IDGroup * thisGroup = findUniqueIndividual( myTemplate );
if ( thisGroup == NULL )
{
++notFound;
}
else
{
// Find each old field
for (int f=0; f<targetFields.size(); f++)
{
for (int i = 0 ; i < thisGroup->values.size(); i++)
if ( thisGroup->values[i]->field->name == tok[2] )
{
if ( f>0 )
tokens[ rep_field[f] ] = ".";
else
{
// Replace this item: handle if the replacing
// field is itself a joint one
if ( thisGroup->values[i]->field->joint )
{
tokens[ rep_field[f] ] =
searchAndReplace( thisGroup->values[i]->jointValue,"+"," " );
}
else
tokens[ rep_field[f] ] = thisGroup->values[i]->value;
}
changed = true;
}
}
}
// Done reading, processng this line
}
///////////////////////////////
// Output this line
if ( ! changed )
{
if ( listMode )
{
for (int i=0; i<rep_field.size(); i++)
OUT1 << tokens[ rep_field[i] ] << "\t";
OUT1 << "\n";
continue;
}
if ( skipMode )
continue;
if ( warnMode )
error("Could not find replacement for " + tokens[insertField] + "\n");
if ( missMode )
{
for (int f=0; f<targetFields.size(); f++)
{
if ( f == 0 )
tokens[ rep_field[0] ] = "NA";
else
tokens[ rep_field[f] ] = ".";
}
}
}
if ( listMode )
continue;
////////////////////////////////////////////////////////
// Print out line, which may or may not be modified
for (int i = 0 ; i < tokens.size() ; i++ )
OUT1 << tokens[i] << par::idhelp_output_delimit << " ";
OUT1 << "\n";
}
if ( notFound > 0 )
PP->printLOG("Could not find matches for " + int2str( notFound ) + " lines\n");
OUT1.close();
IN1.close();
return;
}
void IDHelper::idMatch()
{
// e.g. --id-match myfile.fam FID+IID 1+2 file1.txt CLIN_ID,1 file2.txt ID
// in form {file} {id,{col}}
// where joint IDs are ID1+ID2, or with fields: ID1+ID2,5+7
// If joint IDs, then all must be specified.
// Cannot specify more than 1 non-joint ID though
// Can be different IDs in different files
if ( par::idhelp_match_string.size() < 4 )
error("Must specify more than 1 file to match");
// Assemble all data here:
vector<map<IDGroup*, vector<string> > > table;
vector<int> tableSize;
// And keep track of which files which individuals are in
map<IDGroup*,set<int> > foundIn;
// Keep track of order from first file
map<int,IDGroup*> fileOrder;
set<IDGroup*> seenBefore;
// do we see at least 1 header
bool atleastOneHeader = false;
vector< vector<string> > headers;
// Create a single field on the fly, for use in simple match mode
IDField * nullField = new IDField;
nullField->name = "tmp1";
//////////////////
// Read each file
for (int s=0; s< par::idhelp_match_string.size(); s+=2)
{
PP->printLOG("Matching [ " + par::idhelp_match_string[s]
+ " ] on " + par::idhelp_match_string[s+1] + "\n");
int t = (int)s/2;
if ( par::idhelp_no_dict )
{
IDFile f;
f.filename = "F" + int2str(t);
files.push_back(f);
}
map<IDGroup*, vector<string> > inserts;
// Each element should be in form: filename ID filename2 ID+ID filename3 ID ...
checkFileExists( par::idhelp_match_string[s] );
ifstream I1( (par::idhelp_match_string[s]).c_str() , ios::in );
string id = par::idhelp_match_string[s+1];
// A Implies header row
// A,2 Implies no header row
// A+B Implies header row
// A+B,2+3 Implies no header row
// If in "quick-match" mode, then we assume that the ID column is always the
// same, whether or not it is explicitly named differently here
bool jointQuery = id.find("+") != string::npos;
// Fields to match on
vector<int> fieldCodes;
vector<string> fieldNames;
int maxF = -1;
if ( id.find(",") != string::npos )
{
NList nl(0);
nl.setDelimiter("+");
nl.setRangeChar(" ");
vector<string> fstr = nl.deparseStringList( id.substr( id.find(",")+1 ) );
for (int i=0; i<fstr.size(); i++)
{
int myf;
if ( ! from_string<int>( myf, id.substr( id.find(",")+1 ) , std::dec ) )
error("Trouble converting to a field number");
// Make zero-based
--myf;
if ( myf < 0 )
error("Invalid value for field # specified");
if ( myf > maxF ) maxF = myf;
fieldCodes.push_back( myf );
}
string tmp = id.substr( 0, id.find(",") );
NList nl2(0);
nl2.setDelimiter("+");
nl2.setRangeChar(" ");
fieldNames = nl2.deparseStringList( tmp );
if ( fieldNames.size() != fieldCodes.size() )
error("Problem with joint ID specification in: " + id );
if ( ! par::idhelp_no_dict )
for (int f = 0; fieldNames.size(); f++)
if ( fieldMap.find( fieldNames[f] ) == fieldMap.end() )
error("Field " + fieldNames[f] + " does not exist in the database");
// Read rest of line; insert dummy headers
vector<string> h = tokenizeLine(I1);
I1.close();
I1.clear();
I1.open( (par::idhelp_match_string[s]).c_str() , ios::in );
vector<string> header;
for (int k=0; k<h.size(); k++)
{
header.push_back( "F"+int2str(t)+"_"+int2str(k) );
}
headers.push_back(header);
}
else
{
// Read first line, which should be a header
atleastOneHeader = true;
vector<string> header = tokenizeLine(I1);
NList nl2(0);
nl2.setDelimiter("+");
nl2.setRangeChar(" ");
fieldNames = nl2.deparseStringList( id );
// Find each field
for (int f = 0; f<fieldNames.size(); f++)
{
if ( ! par::idhelp_no_dict )
if ( fieldMap.find( fieldNames[f] ) == fieldMap.end() )
error("Field " + fieldNames[f] + " does not exist in the database");
bool foundField = false;
for (int i=0; i<header.size(); i++)
{
if ( header[i] == fieldNames[f] )
{
fieldCodes.push_back( i );
if ( i > maxF ) maxF = i;
foundField = true;
break;
}
}
if ( ! foundField )
error("Could not find field " + fieldNames[f]
+ " in [ " + par::idhelp_match_string[s] + " ]");
}
headers.push_back(header);
}
vector<IDField *> thisField;
if ( ! par::idhelp_no_dict )
{
for (int f = 0 ; f < fieldNames.size(); f++)
thisField.push_back( fieldMap.find( fieldNames[f] )->second );
}
else
{
thisField.push_back( nullField );
}
// Keep track of the # of columns per file, as a check
// for rectangular files
tableSize.push_back(-1);
////////////////////////////////////////
// Read each row of the data files:
int notFound = 0;
string missingList = "";
while ( ! I1.eof() )
{
vector<string> tok = tokenizeLine(I1);
if ( tok.size() == 0 )
continue;
if ( tableSize[t] == -1 )
tableSize[t] = tok.size();
else
if ( tok.size() != tableSize[t] )
{
string msg = "Problem with non-rectangular file [ "
+ par::idhelp_match_string[s] + " ]\n";
msg += "Execting " + int2str( tableSize[t] )
+ " fields but found " + int2str(tok.size()) + "\n";
for (int k=0; k<tok.size(); k++)
msg += tok[k] + " ";
error( msg );
}
// Warn when line does not contain enough columns
if ( maxF >= tok.size() )
error("Line does not contain enough columns");
set<IDValue> myTemplate;
for (int f=0; f<fieldCodes.size(); f++)
{
IDValue findField;
findField.field = thisField[f];
findField.value = tok[ fieldCodes[f] ];
if ( findField.field->equiv )
findField.updateAlias();
myTemplate.insert( findField );
}
// Connect up any joint fields
setJointValues( myTemplate );
// If running without a dictionary, now add this person in
if ( par::idhelp_no_dict )
{
IDGroup * thisGroup = findUniqueIndividual( myTemplate );
if ( thisGroup == NULL )
{
IDGroup * g = new IDGroup;
g->resolved = false;
g->file = &files[t];
set<IDValue>::iterator k = myTemplate.begin();
while ( k != myTemplate.end() )
{
IDValue * nv = new IDValue;
*nv = *k;
g->values.push_back( nv );
idgroup.push_back(g);
++k;
}
// Add to the ID Map
set<IDGroup*> t;
t.insert( idgroup[ idgroup.size()-1] );
for (int v=0; v<g->values.size(); v++)
idmap.insert(make_pair( *(g->values[v]) ,t ));
}
}
// Find the match
IDGroup * thisGroup = findUniqueIndividual( myTemplate );
if ( thisGroup != NULL )
{
// Add this row to the collection
inserts.insert(make_pair( thisGroup, tok ) );
// Keep track of order in which we came across this person for the
// first time
if ( seenBefore.find( thisGroup ) == seenBefore.end() )
{
int sz = fileOrder.size();
fileOrder.insert(make_pair(sz,thisGroup));
seenBefore.insert( thisGroup );
}
// Keep track that this person was in this file
map<IDGroup*,set<int> >::iterator i1 = foundIn.find( thisGroup );
if ( i1 == foundIn.end() )
{
set<int> t;
t.insert(int(s/2));
foundIn.insert(make_pair( thisGroup, t ) );
}
else
i1->second.insert(int(s/2));
}
else
{
set<IDValue>::iterator i = myTemplate.begin();
while ( i != myTemplate.end() )
{
missingList += i->field->name + " = " + i->value + "\t";
++i;
}
missingList += "\n";
++notFound;
}
// Get next line from this file
}
// Add this file to big collection
table.push_back( inserts );
if ( notFound > 0 )
{
PP->printLOG("Could not find "
+ int2str( notFound )
+ " individuals from [ "
+ par::idhelp_match_string[s]
+ " ] in database\n");
PP->printLOG("Writing this list to [ " + par::output_file_name + ".noid ]\n");
ofstream O1( ( par::output_file_name+".noid").c_str() , ios::out );
O1 << "FIELD = VALUE\n";
O1 << missingList ;
O1.close();
}
I1.close();
// Get next file
}
// Now output all files tied together
// Output, in order of the order in which we encountered
// each unique individual
// Only output complete rows?
OptionSet * id_opt = par::opt.getOptions("IDHELP");
bool complete = false;
if ( id_opt->isSet("complete") )
complete = true;
if ( id_opt->isSet("noheader") )
atleastOneHeader = false;
PP->printLOG("Writing output file to [ " + par::output_file_name + ".matched ]\n");
ofstream O1( (par::output_file_name+".matched").c_str() , ios::out );
// What about header row?
// skip for now...
if ( atleastOneHeader )
{
for (int k=0; k<headers.size(); k++)
for (int j=0; j<headers[k].size(); j++)
O1 << setw(12) << headers[k][j] << " ";
O1 << "\n";
}
int tfiles = table.size();
map<int,IDGroup*>::iterator j = fileOrder.begin();
while ( j != fileOrder.end() )
{
map<IDGroup*,set<int> >::iterator i = foundIn.find( (IDGroup* const)j->second );
int f = i->second.size();
if ( complete && f != tfiles )
{
++j;
continue;
}
for (int t = 0 ; t < tfiles; t++)
{
// Does this individual exist for this file?
map<IDGroup*,vector<string> > & thisTable = table[t];
if ( thisTable.find( i->first ) == thisTable.end() )
{
for (int k=0; k<tableSize[t]; k++)
O1 << setw(12) << "NA" << " ";
}
else
{
vector<string> & thisLine = thisTable.find( i->first )->second;
for (int k = 0 ; k < thisLine.size() ; k++ )
O1 << setw(12) << thisLine[k] << " ";
}
}
O1 << "\n";
// Next individual
++j;
}
O1.close();
return;
} // end of id-match routine
void IDHelper::idDump()
{
// Note: use of parseQuery modifies the data structure, setting
// fields to non-joint potentially, and so subsequent attempts to
// line things up will not work; therefore, for now we stop here.
PP->printLOG("\nReporting rows that match [ " + par::idhelp_dump_from_dict_cmd + " ] \n\n");
map<IDField*,set<IDValue> > myTemplate = parseQuery( par::idhelp_dump_from_dict_cmd );
for ( int g = 0 ; g < idgroup.size(); g++ )
{
IDGroup * group = idgroup[g];
if ( matchIndividual( group , myTemplate ) )
{
for (int j = 0 ; j < group->values.size(); j++ )
{
PP->printLOG( group->file->filename + " : " );
PP->printLOG( group->values[j]->field->name + " = " );
PP->printLOG( group->values[j]->value + "\n" );
}
PP->printLOG("\n");
}
}
PP->printLOG("---------------------------------\n");
// Important: joint status of fields will have changed; in any
// case, let's stop here
return;
}
|