sleipnir / src / pcl.cpp

   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
/*****************************************************************************
 * This file is provided under the Creative Commons Attribution 3.0 license.
 *
 * You are free to share, copy, distribute, transmit, or adapt this work
 * PROVIDED THAT you attribute the work to the authors listed below.
 * For more information, please see the following web page:
 * http://creativecommons.org/licenses/by/3.0/
 *
 * This file is a component of the Sleipnir library for functional genomics,
 * authored by:
 * Curtis Huttenhower (chuttenh@princeton.edu)
 * Mark Schroeder
 * Maria D. Chikina
 * Olga G. Troyanskaya (ogt@princeton.edu, primary contact)
 *
 * If you use this library, the included executable tools, or any related
 * code in your work, please cite the following publication:
 * Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and
 * Olga G. Troyanskaya.
 * "The Sleipnir library for computational functional genomics"
 *****************************************************************************/
#include "stdafx.h"
#include "pcl.h"
#include "meta.h"
#include "statistics.h"
#include "genome.h"
#include "measure.h"
#include "dat.h"

namespace Sleipnir {

const char CPCLImpl::c_szEWEIGHT[] = "EWEIGHT";
const char CPCLImpl::c_szGENE[] = "GENE";
const char CPCLImpl::c_szGID[] = "GID";
const char CPCLImpl::c_szGWEIGHT[] = "GWEIGHT";
const char CPCLImpl::c_szNAME[] = "NAME";
const char CPCLImpl::c_szOne[] = "1";
const char CPCLImpl::c_szExtension[] = ".pcl";
const char CPCLImpl::c_szBinExtension[] = ".bin";
const char CPCLImpl::c_szDabExtension[] = ".dab";

struct SNeighbors {
	CFullMatrix<pair<size_t, float> > m_MatNeighbors;
	vector<size_t> m_veciMin;
	vector<size_t> m_veciColumns;

	void Initialize(const float* adValues, size_t iValues, size_t iNeighbors) {
		size_t i, j;

		m_veciColumns.clear();
		for (i = 0; i < iValues; ++i)
			if (CMeta::IsNaN(adValues[i]))
				m_veciColumns.push_back(i);

		m_veciMin.resize(m_veciColumns.size());
		m_MatNeighbors.Initialize(iNeighbors, m_veciColumns.size());
		for (i = 0; i < m_MatNeighbors.GetRows(); ++i)
			for (j = 0; j < m_MatNeighbors.GetColumns(); ++j)
				m_MatNeighbors.Get(i, j).second = -FLT_MAX;
	}

	bool Add(size_t iNeighbor, float dSim, const float* adValues) {
		size_t i, j, iCol;
		bool fRet;

		if (!m_MatNeighbors.GetRows())
			return false;
		for (fRet = false, i = 0; i < m_veciColumns.size(); ++i) {
			iCol = m_veciColumns[i];
			if (!CMeta::IsNaN(adValues[iCol]) && (dSim > m_MatNeighbors.Get(
					m_veciMin[i], i).second)) {
				fRet = true;
				m_MatNeighbors.Get(m_veciMin[i], i).first = iNeighbor;
				m_MatNeighbors.Get(m_veciMin[i], i).second = dSim;

				for (m_veciMin[i] = 0, j = 1; j < m_MatNeighbors.GetRows(); ++j)
					if (m_MatNeighbors.Get(j, i).second < m_MatNeighbors.Get(
							m_veciMin[i], i).second)
						m_veciMin[i] = j;
			}
		}

		return fRet;
	}

	size_t GetColumn(size_t iColumn) const {
		size_t i;

		for (i = 0; i < m_veciColumns.size(); ++i)
			if (m_veciColumns[i] == iColumn)
				return i;

		return -1;
	}
};

/*!
 * \brief
 * Kitchen sink method for completely loading a PCL and calculating its pairwise similarity scores into a CDat.
 *
 * \param szFile
 * If non-null, file from which PCL is to be loaded; if null, standard input is used.
 *
 * \param iSkip
 * Number of columns to skip in the PCL file between gene IDs and experimental data.
 *
 * \param szSimilarityMeasure
 * String identifier of similarity measure to use for CDat generation.
 *
 * \param fNormalize
 * If true, normalize the generated CDat to the range [0, 1].
 *
 * \param fZScore
 * If true, normalize the generated CDat to z-scores (subtract mean, divide by standard deviation).
 *
 * \param fAutocorrelate
 * If true, autocorrelate the requested similarity measure.
 *
 * \param szGeneFile
 * If non-null, only convert genes in the given file to pairwise scores in the CDat.
 *
 * \param dCutoff
 * If finite, remove all pairwise scores less than the given cutoff.
 *
 * \param iLimit
 * If not equal to -1 and the PCL contains more genes than this limit, do not precalculate pairwise scores;
 * instead, configure the CDat to calculate scores on the fly as needed from the PCL.
 *
 * \param PCL
 * Output PCL with the loaded data.
 *
 * \param Dat
 * Output CDat with the calculated pairwise scores.
 *
 * \param eMap
 * Way in which returned value should be centered (implementation-specific).
 *
 * \param fFrequencyWeight
 * If true, weight each condition by the frequency with which it is nonzero over all genes.
 *
 * \returns
 * 0 on successes, a nonzero value on failure.
 *
 * The (many) steps performed by this method are as follows:
 * <ol>
 * <li>A PCL is loaded from szFile into PCL using Open.  If szFile is null, the PCL is loaded from
 * standard input.</li>
 * <li>An IMeasure object is constructed by iterating over the available implementations and finding
 * one whose name corresponds with szSimilarityMeasure.  Names which are distance measures (e.g. Euclidean)
 * are automatically inverted.  If requested, the measure is autocorrelated.</li>
 * <li>If given, szGeneFile is loaded into a CGenes object.</li>
 * <li>If iLimit is not -1 and the PCL contains more than the limiting number of genes, Dat is given a
 * reference to PCL and configured to calculate pairwise scores as needed.  Processing then stops.</li>
 * <li>Otherwise, an empty CDat is initialized to contain either the genes in szGeneFile (if given) or
 * all of the genes in the PCL.</li>
 * <li>Gene pairs are assigned scores in the CDat using the requested similarity measure.</li>
 * <li>If given, scores below dCutoff are replaced with missing values.</li>
 * <li>If requested, the remaining scores are normalized either to the range [0, 1] or to z-scores.</li>
 * </ol>
 *
 * \remarks
 * This method is written to make it easy to construct tools that must load a PCL and immediately convert
 * it to pairwise scores using some user-selected similarity measure.
 */
int CPCL::Distance(const char* szFile, size_t iSkip,
		const char* szSimilarityMeasure, bool fNormalize, bool fZScore,
		bool fAutocorrelate, const char* szGeneFile, float dCutoff,
		size_t iLimit, CPCL& PCL, CDat& Dat, IMeasure::EMap eMap,
		bool fFrequencyWeight, float dAlpha, int nThreads) {


	size_t i, j, iOne, iTwo;
	float d;
	ifstream ifsm;
	vector<string> vecstrGenes;
	CGenome Genome;
	CGenes GenesIn(Genome);
	vector<size_t> veciGenes;
	const float* adOne;
	const float* adWeights;
	vector<float> vecdWeights;
	IMeasure* pMeasure;
	CMeasurePearson Pearson;
	CMeasureEuclidean Euclidean;
	CMeasureKendallsTau KendallsTau;
	CMeasureKolmogorovSmirnov KolmSmir;
	CMeasureSpearman Spearman(true);
	CMeasurePearNorm PearNorm;
	CMeasureHypergeometric Hypergeom;
	CMeasureQuickPearson PearQuick;
	CMeasureInnerProduct InnerProd;
	CMeasureBinaryInnerProduct BinInnerProd;
	CMeasureMutualInformation MutualInfo;
	CMeasureRelativeAUC RelAuc;
	CMeasurePearsonSignificance PearSig;
	CMeasureDice Dice( dAlpha );
	CMeasureDistanceCorrelation DCor;
	CMeasureSignedDistanceCorrelation SDCor;
	if (szFile) {
        g_CatSleipnir().debug("Opening PCL for distance");
        if (!PCL.Open(szFile, iSkip, false, false)) {
			g_CatSleipnir().error(
					"CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) failed to open PCL",
					szFile, iSkip, szSimilarityMeasure, fNormalize, fZScore,
					fAutocorrelate, szGeneFile ? szGeneFile : "", dCutoff);
			return 1;
		}
		ifsm.close();
	} else if (!PCL.Open(cin, iSkip)) {
		g_CatSleipnir().error(
				"CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) failed to open PCL",
				"stdin", iSkip, szSimilarityMeasure, fNormalize, fZScore,
				fAutocorrelate, szGeneFile ? szGeneFile : "", dCutoff);
		return 1;
	}
	if (!szSimilarityMeasure)
		return 0;

	CMeasureSigmoid
			EuclideanSig(&Euclidean, false, 1.0f / PCL.GetExperiments());
	IMeasure* apMeasures[] = { &Pearson, &EuclideanSig, &KendallsTau,
			&KolmSmir, &Spearman, &PearNorm, &Hypergeom, &PearQuick,
			&InnerProd, &BinInnerProd, &MutualInfo, &RelAuc, &PearSig, &Dice,&DCor,&SDCor, NULL };

	pMeasure = NULL;
	for (i = 0; apMeasures[i]; ++i)
		if (!strcmp(apMeasures[i]->GetName(), szSimilarityMeasure)) {
			pMeasure = apMeasures[i];
			break;
		}
	if (!pMeasure)
		return 1;
	g_CatSleipnir().info("Method: %s ", pMeasure->GetName(), i);
	if (fFrequencyWeight) {
		vecdWeights.resize(PCL.GetExperiments());
		for (i = 0; i < vecdWeights.size(); ++i) {
			for (iOne = j = 0; j < PCL.GetGenes(); ++j)
				if (!CMeta::IsNaN(d = PCL.Get(j, i)) && (d > 0))
					iOne++;
			vecdWeights[i] = (float) ((PCL.GetGenes() + 1) - iOne)
					/ PCL.GetGenes();
		}
	}
	adWeights = vecdWeights.empty() ? NULL : &vecdWeights[0];

	CMeasureAutocorrelate Autocorrelate(pMeasure, false);
	if (fAutocorrelate)
		pMeasure = &Autocorrelate;

	if (szGeneFile) {
		ifsm.clear();
		ifsm.open(szGeneFile);
		if (!GenesIn.Open(ifsm)) {
			g_CatSleipnir().error(
					"CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) failed to open genes",
					szFile ? szFile : "stdin", iSkip, szSimilarityMeasure,
					fNormalize, fZScore, fAutocorrelate, szGeneFile, dCutoff);
			return 1;
		}
		ifsm.close();
	} else
		GenesIn.Open(PCL.GetGeneNames());
	veciGenes.resize(GenesIn.GetGenes());
	for (i = 0; i < veciGenes.size(); ++i)
		veciGenes[i] = szGeneFile ? PCL.GetGene(GenesIn.GetGene(i).GetName())
				: i;

	if (pMeasure->IsRank())
		PCL.RankTransform();

	g_CatSleipnir().info("Number of Experiments: %d", PCL.GetExperiments());

	if ((iLimit != -1) && (PCL.GetGenes() > iLimit))
		Dat.Open(PCL, pMeasure->Clone(), true);
	else {
		Dat.Open(GenesIn.GetGeneNames());
		for (i = 0; i < Dat.GetGenes(); ++i)
			for (j = (i + 1); j < Dat.GetGenes(); ++j)
				Dat.Set(i, j, CMeta::GetNaN());
		int origNThreads = omp_get_num_threads();
		omp_set_num_threads(nThreads);
		for (i = 0; i < GenesIn.GetGenes(); ++i) {
			if (!(i % 100))
				g_CatSleipnir().info(
						"CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) processing gene %d/%d",
						szFile ? szFile : "stdin", iSkip, szSimilarityMeasure,
						fNormalize, fZScore, fAutocorrelate,
						szGeneFile ? szGeneFile : "", dCutoff, i,
						GenesIn.GetGenes());
			if ((iOne = veciGenes[i]) == -1)
				continue;
			adOne = PCL.Get(iOne);
			for (j = (i + 1); j < GenesIn.GetGenes(); ++j){
				if ((iTwo = veciGenes[j]) != -1){
					Dat.Set(i, j, (float) pMeasure->Measure(adOne,
							PCL.GetExperiments(), PCL.Get(iTwo),
							PCL.GetExperiments(), eMap, adWeights, adWeights));
				}
			}
		}
		omp_set_num_threads(origNThreads);
		if (fNormalize || fZScore)
			Dat.Normalize(fZScore ? CDat::ENormalizeZScore
					: CDat::ENormalizeMinMax);
		if (!CMeta::IsNaN(dCutoff))
			for (i = 0; i < Dat.GetGenes(); ++i)
				for (j = (i + 1); j < Dat.GetGenes(); ++j)
					if (!CMeta::IsNaN(d = Dat.Get(i, j)) && (d < dCutoff))
						Dat.Set(i, j, CMeta::GetNaN());

		if(pMeasure->GetName()=="pearson" || pMeasure->GetName()=="pearnorm"){
			vector<unsigned long> bins;
			bins.resize(55);
			float upper = 0;
			float lower = 0;
			if(pMeasure->GetName()=="pearson"){
				upper = 1.0;
				lower = -1.0;
			}else if(pMeasure->GetName()=="pearnorm"){
				upper = 5.0;
				lower = -5.0;
			}
			float bin_size = (upper - lower) / 50;
			for(i=0; i<55; i++)
				bins[i] = 0;
			for(i=0; i<Dat.GetGenes(); i++){
				for(j=i+1; j<Dat.GetGenes(); j++){
					d = Dat.Get(i,j);
					if(CMeta::IsNaN(d)) continue;
					int b = (int) ((d - lower) / bin_size);
					if(b<0){
						bins[0]++;
						continue;
					}
					if(b>=55){
						bins[54]++;
						continue;
					}
					bins[b]++;
				}
			}
			g_CatSleipnir().info(
			"Distribution of distances: bin size: %.5f, number of bins: %d, min bin value: %.5f, max bin value: %.5f",
			bin_size, 55, lower, upper);
			for(i=0; i<55; i++){
				g_CatSleipnir().info("%lu\t%lu", i, bins[i]);
			}
		}

	}

	return 0;
}

/*!
 * \brief
 * Kitchen sink method for completely loading a PCL and calculating its pairwise similarity scores into a CDat.  This method supports weighted metrics as opposed to the above method which does not.  This allows distancer to take a weights vector (currently included in the documentation but it was not previously functioning).  The weights vector is used only if freqweight is false.
 *
 * \param szFile
 * If non-null, file from which PCL is to be loaded; if null, standard input is used.
 *
 * \param iSkip
 * Number of columns to skip in the PCL file between gene IDs and experimental data.
 *
 * \param szWeights
 * If non-null, file from which weights are to be loaded; if null, this is ignored.
 *
 * \param szSimilarityMeasure
 * String identifier of similarity measure to use for CDat generation.
 *
 * \param fNormalize
 * If true, normalize the generated CDat to the range [0, 1].
 *
 * \param fZScore
 * If true, normalize the generated CDat to z-scores (subtract mean, divide by standard deviation).
 *
 * \param fAutocorrelate
 * If true, autocorrelate the requested similarity measure.
 *
 * \param szGeneFile
 * If non-null, only convert genes in the given file to pairwise scores in the CDat.
 *
 * \param dCutoff
 * If finite, remove all pairwise scores less than the given cutoff.
 *
 * \param iLimit
 * If not equal to -1 and the PCL contains more genes than this limit, do not precalculate pairwise scores;
 * instead, configure the CDat to calculate scores on the fly as needed from the PCL.
 *
 * \param PCL
 * Output PCL with the loaded data.
 *
 * \param Dat
 * Output CDat with the calculated pairwise scores.
 *
 * \param eMap
 * Way in which returned value should be centered (implementation-specific).
 *
 * \param fFrequencyWeight
 * If true, weight each condition by the frequency with which it is nonzero over all genes.
 *
 * \returns
 * 0 on successes, a nonzero value on failure.
 *
 * The (many) steps performed by this method are as follows:
 * <ol>
 * <li>A PCL is loaded from szFile into PCL using Open.  If szFile is null, the PCL is loaded from
 * standard input.</li>
 * <li>An IMeasure object is constructed by iterating over the available implementations and finding
 * one whose name corresponds with szSimilarityMeasure.  Names which are distance measures (e.g. Euclidean)
 * are automatically inverted.  If requested, the measure is autocorrelated.</li>
 * <li>If given, szGeneFile is loaded into a CGenes object.</li>
 * <li>If iLimit is not -1 and the PCL contains more than the limiting number of genes, Dat is given a
 * reference to PCL and configured to calculate pairwise scores as needed.  Processing then stops.</li>
 * <li>Otherwise, an empty CDat is initialized to contain either the genes in szGeneFile (if given) or
 * all of the genes in the PCL.</li>
 * <li>Gene pairs are assigned scores in the CDat using the requested similarity measure.</li>
 * <li>If given, scores below dCutoff are replaced with missing values.</li>
 * <li>If requested, the remaining scores are normalized either to the range [0, 1] or to z-scores.</li>
 * </ol>
 *
 * \remarks
 * This method is written to make it easy to construct tools that must load a PCL and immediately convert
 * it to pairwise scores using some user-selected similarity measure.
 */
int CPCL::Distance(const char* szFile, size_t iSkip, const char* szWeights,
		const char* szSimilarityMeasure, bool fNormalize, bool fZScore,
		bool fAutocorrelate, const char* szGeneFile, float dCutoff,
		size_t iLimit, CPCL& PCL, CDat& Dat, IMeasure::EMap eMap,
		bool fFrequencyWeight, float dAlpha, int nThreads) {
	size_t i, j, iOne, iTwo;
	float d;
	ifstream ifsm;
	vector<string> vecstrGenes;
	CGenome Genome;
	CGenes GenesIn(Genome);
	vector<size_t> veciGenes;
	const float* adOne;
	const float* adWeights;
	vector<float> vecdWeights;
	vector<string> vecstrWeights;
    string acStr;
	IMeasure* pMeasure;
	CMeasurePearson Pearson;
	CMeasureEuclidean Euclidean;
	CMeasureKendallsTau KendallsTau;
	CMeasureKolmogorovSmirnov KolmSmir;
	CMeasureSpearman Spearman(true);
	CMeasurePearNorm PearNorm;
	CMeasureHypergeometric Hypergeom;
	CMeasureQuickPearson PearQuick;
	CMeasureInnerProduct InnerProd;
	CMeasureBinaryInnerProduct BinInnerProd;
	CMeasureMutualInformation MutualInfo;
	CMeasureRelativeAUC RelAuc;
	CMeasurePearsonSignificance PearSig;
	CMeasureDice Dice( dAlpha );
	CMeasureDistanceCorrelation DCor;
	CMeasureSignedDistanceCorrelation SDCor;
	if (szFile) {
        g_CatSleipnir().debug("Opening PCL for distance");
        if (!PCL.Open(szFile, iSkip, false, false)) {
			g_CatSleipnir().error(
					"CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) failed to open PCL",
					szFile, iSkip, szSimilarityMeasure, fNormalize, fZScore,
					fAutocorrelate, szGeneFile ? szGeneFile : "", dCutoff);
			return 1;
		}
		ifsm.close();
	} else if (!PCL.Open(cin, iSkip)) {
		g_CatSleipnir().error(
				"CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) failed to open PCL",
				"stdin", iSkip, szSimilarityMeasure, fNormalize, fZScore,
				fAutocorrelate, szGeneFile ? szGeneFile : "", dCutoff);
		return 1;
	}
	if (!szSimilarityMeasure)
		return 0;

	CMeasureSigmoid
			EuclideanSig(&Euclidean, false, 1.0f / PCL.GetExperiments());
	IMeasure* apMeasures[] = { &Pearson, &EuclideanSig, &KendallsTau,
			&KolmSmir, &Spearman, &PearNorm, &Hypergeom, &PearQuick,
			&InnerProd, &BinInnerProd, &MutualInfo, &RelAuc, &PearSig, &Dice,&DCor,&SDCor, NULL };

	pMeasure = NULL;
	for (i = 0; apMeasures[i]; ++i)
		if (!strcmp(apMeasures[i]->GetName(), szSimilarityMeasure)) {
			pMeasure = apMeasures[i];
			break;
		}
	if (!pMeasure)
		return 1;
	g_CatSleipnir().info("Method: %s ", pMeasure->GetName(), i);
	if (fFrequencyWeight) {
		vecdWeights.resize(PCL.GetExperiments());
		for (i = 0; i < vecdWeights.size(); ++i) {
			for (iOne = j = 0; j < PCL.GetGenes(); ++j)
				if (!CMeta::IsNaN(d = PCL.Get(j, i)) && (d > 0))
					iOne++;
			vecdWeights[i] = (float) ((PCL.GetGenes() + 1) - iOne)
					/ PCL.GetGenes();
		}
	}
	adWeights = vecdWeights.empty() ? NULL : &vecdWeights[0];

    if ( adWeights == NULL ) {
        if (szWeights != NULL) {
            ifsm.open(szWeights);
            getline(ifsm, acStr);
            ifsm.close();
            CMeta::Tokenize( acStr.c_str(), vecstrWeights, CMeta::c_szWS, true);
            if ( vecstrWeights.size() != PCL.GetExperiments()) {
                g_CatSleipnir().error(
                        "CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) length of weights file (%s, length: %d) did not match number of experiments (%d).",
                        szFile, iSkip, szSimilarityMeasure, fNormalize, fZScore,
                        fAutocorrelate, szGeneFile ? szGeneFile : "", dCutoff, szWeights,
                        vecstrWeights.size(), PCL.GetExperiments());
                return 1;
            }
		    vecdWeights.resize(PCL.GetExperiments());
            for (i = 0; i < vecstrWeights.size(); ++i) {
                vecdWeights[i] = atof(vecstrWeights[i].c_str());
            }
	        adWeights = vecdWeights.empty() ? NULL : &vecdWeights[0];
        }
    }

	CMeasureAutocorrelate Autocorrelate(pMeasure, false);
	if (fAutocorrelate)
		pMeasure = &Autocorrelate;

	if (szGeneFile) {
		ifsm.clear();
		ifsm.open(szGeneFile);
		if (!GenesIn.Open(ifsm)) {
			g_CatSleipnir().error(
					"CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) failed to open genes",
					szFile ? szFile : "stdin", iSkip, szSimilarityMeasure,
					fNormalize, fZScore, fAutocorrelate, szGeneFile, dCutoff);
			return 1;
		}
		ifsm.close();
	} else
		GenesIn.Open(PCL.GetGeneNames());
	veciGenes.resize(GenesIn.GetGenes());
	for (i = 0; i < veciGenes.size(); ++i)
		veciGenes[i] = szGeneFile ? PCL.GetGene(GenesIn.GetGene(i).GetName())
				: i;

	if (pMeasure->IsRank())
		PCL.RankTransform();

	g_CatSleipnir().info("Number of Experiments: %d", PCL.GetExperiments());

	if ((iLimit != -1) && (PCL.GetGenes() > iLimit))
		Dat.Open(PCL, pMeasure->Clone(), true);
	else {
		Dat.Open(GenesIn.GetGeneNames());
		for (i = 0; i < Dat.GetGenes(); ++i)
			for (j = (i + 1); j < Dat.GetGenes(); ++j)
				Dat.Set(i, j, CMeta::GetNaN());
		for (i = 0; i < GenesIn.GetGenes(); ++i) {
			if (!(i % 100))
				g_CatSleipnir().info(
						"CPCL::Distance( %s, %d, %s, %d, %d, %d, %s, %g ) processing gene %d/%d",
						szFile ? szFile : "stdin", iSkip, szSimilarityMeasure,
						fNormalize, fZScore, fAutocorrelate,
						szGeneFile ? szGeneFile : "", dCutoff, i,
						GenesIn.GetGenes());
			if ((iOne = veciGenes[i]) == -1)
				continue;
			adOne = PCL.Get(iOne);
			#pragma omp parallel for num_threads(nThreads)
			for (j = (i + 1); j < GenesIn.GetGenes(); ++j){
				if ((iTwo = veciGenes[j]) != -1){
					Dat.Set(i, j, (float) pMeasure->Measure(adOne,
							PCL.GetExperiments(), PCL.Get(iTwo),
							PCL.GetExperiments(), eMap, adWeights, adWeights));
				}
			}
		}

		if (fNormalize || fZScore)
			Dat.Normalize(fZScore ? CDat::ENormalizeZScore
					: CDat::ENormalizeMinMax);

		if (!CMeta::IsNaN(dCutoff))
			for (i = 0; i < Dat.GetGenes(); ++i)
				for (j = (i + 1); j < Dat.GetGenes(); ++j)
					if (!CMeta::IsNaN(d = Dat.Get(i, j)) && (d < dCutoff))
						Dat.Set(i, j, CMeta::GetNaN());

		if(pMeasure->GetName()=="pearson" || pMeasure->GetName()=="pearnorm"){
			vector<unsigned long> bins;
			bins.resize(55);
			float upper = 0;
			float lower = 0;
			if(pMeasure->GetName()=="pearson"){
				upper = 1.0;
				lower = -1.0;
			}else if(pMeasure->GetName()=="pearnorm"){
				upper = 5.0;
				lower = -5.0;
			}
			float bin_size = (upper - lower) / 50;
			for(i=0; i<55; i++)
				bins[i] = 0;
			for(i=0; i<Dat.GetGenes(); i++){
				for(j=i+1; j<Dat.GetGenes(); j++){
					d = Dat.Get(i,j);
					if(CMeta::IsNaN(d)) continue;
					int b = (int) ((d - lower) / bin_size);
					if(b<0){
						bins[0]++;
						continue;
					}
					if(b>=55){
						bins[54]++;
						continue;
					}
					bins[b]++;
				}
			}
			g_CatSleipnir().info(
			"Distribution of distances: bin size: %.5f, number of bins: %d, min bin value: %.5f, max bin value: %.5f",
			bin_size, 55, lower, upper);
			for(i=0; i<55; i++){
				g_CatSleipnir().info("%lu\t%lu", i, bins[i]);
			}
		}
	}
	return 0;
}

CPCLImpl::~CPCLImpl() {

	Reset();
}

void CPCLImpl::Reset() {

	m_Data.Reset();
	m_vecstrGenes.clear();
	m_vecstrExperiments.clear();
	m_vecstrFeatures.clear();
	m_vecvecstrFeatures.clear();
	m_setiGenes.clear();
	m_mapstriGenes.clear();
}

/*!
 * \brief
 * Create a new PCL by reading from text or binary
 *
 * \param szFile
 * File from which PCL file is loaded.
 *
 * \returns
 * True if the PCL was opened successfully.
 *
 */
//bool CPCL::Open(const char* szFile, size_t iSkip, bool Memmap) {
//	std::ifstream ifsm;
//	if (szFile)
//		ifsm.open(szFile);
//
//	// is this a binary binary file?
//	if (!strcmp(szFile + strlen(szFile) - strlen(c_szBinExtension),
//			c_szBinExtension))
//		return OpenBinary(szFile ? ifsm : cin, Memmap);
//	else
//		// is this a text based PCL file?
//		return Open(szFile ? ifsm : cin, iSkip);
//}


bool CPCL::Open(const char* szFile, size_t iSkip, bool Memmap, bool rTable) {
	bool isBinary = false;
	bool isDAB = false;
	ifstream ifsm;
	if (!strcmp(szFile + strlen(szFile) - strlen(c_szBinExtension), c_szBinExtension)) {
		isBinary = true;
	}
	if (!strcmp(szFile + strlen(szFile) - strlen(c_szDabExtension), c_szDabExtension)) {
		isDAB = true;
	}
	if (isBinary && Memmap) {
		g_CatSleipnir().notice("CPCL::Open, openning with memory mapping");
		Reset();
		if (!CMeta::MapRead(m_abData, m_hndlData, m_iData, szFile)) {
			g_CatSleipnir().error("CPCL::Open( %s, %d ) failed memory mapping",
					szFile, Memmap);

			return false;
		}
		bool fRet = OpenHelper();
		return fRet;
	}
	else if (isBinary) {
		ifsm.open(szFile, ios::binary);
		return OpenBinary(ifsm);
	} else if (isDAB) {
		CDat dat;
		if (!dat.Open(szFile,false, 0, false, false)) {
		    cerr << "Could not open dab to make pcl." << endl;
		    return false;
		}
		std::vector<std::string> features;
		std::vector<std::string> genes = dat.GetGeneNames();
		Open(genes, genes, features);
		for (size_t i = 0; i < dat.GetGenes(); i++) {
		    for (size_t j = i; j < dat.GetGenes(); j++) {
			if (i == j) {
			    Set(i, j, 1);
			}
			else {
			    float pairVal = dat.Get(i,j);
			    Set(i, j, pairVal);
			    Set(j, i, pairVal);
			}
		    }
		}
		return true;
	} else {
		ifstream ifsm;
		if (szFile)
		  ifsm.open(szFile);
		return Open(szFile ? ifsm : cin, iSkip, rTable);
	}
}



/*!
 * \brief
 * Save a PCL to the given text file.
 *
 * \param szFile
 * File into which PCL file is saved.
 *
 * \remarks
 * If null, output defaults to stdout.
 *
 * \see
 * Save
 */
void CPCL::Save(const char* szFile) {
	std::ofstream ofsm;

	if (szFile)
		ofsm.open(szFile);

	// Save as binary PCL file?
	if (!strcmp(szFile + strlen(szFile) - strlen(c_szExtension), c_szBinExtension))
		SaveBinary(szFile ? ofsm : cout);
	else
		Save(szFile ? ofsm : cout);
		// Save as text file

}

/*!
 * \brief
 * Create a new PCL by copying the given one.
 *
 * \param PCL
 * PCL to be copied into the current one.
 *
 * \remarks
 * All values are copied into newly allocated memory within the current PCL, so it's safe to destroy the
 * input PCL after the new one is opened.
 */
void CPCL::Open(const CPCL& PCL) {
	size_t i, j;
	TSetI::const_iterator iterGene;
	TMapStrI::const_iterator iterGene2;

	Reset();
	m_fHeader = PCL.m_fHeader;
	m_Data.Initialize(PCL.m_Data.GetRows(), PCL.m_Data.GetColumns());
	for (i = 0; i < m_Data.GetRows(); ++i)
		for (j = 0; j < m_Data.GetColumns(); ++j)
			m_Data.Set(i, j, PCL.m_Data.Get(i, j));

	for (iterGene = PCL.m_setiGenes.begin(); iterGene != PCL.m_setiGenes.end(); ++iterGene)
		m_setiGenes.insert(*iterGene);
	m_vecstrExperiments.resize(PCL.m_vecstrExperiments.size());
	copy(PCL.m_vecstrExperiments.begin(), PCL.m_vecstrExperiments.end(),
			m_vecstrExperiments.begin());
	m_vecstrFeatures.resize(PCL.m_vecstrFeatures.size());
	copy(PCL.m_vecstrFeatures.begin(), PCL.m_vecstrFeatures.end(),
			m_vecstrFeatures.begin());
	m_vecstrGenes.resize(PCL.m_vecstrGenes.size());
	copy(PCL.m_vecstrGenes.begin(), PCL.m_vecstrGenes.end(),
			m_vecstrGenes.begin());
	for (iterGene2 = PCL.m_mapstriGenes.begin(); iterGene2
			!= PCL.m_mapstriGenes.end(); ++iterGene2)
		m_mapstriGenes[iterGene2->first] = iterGene2->second;
	m_vecvecstrFeatures.resize(PCL.m_vecvecstrFeatures.size());
	for (i = 0; i < m_vecvecstrFeatures.size(); ++i) {
		m_vecvecstrFeatures[i].resize(PCL.m_vecvecstrFeatures[i].size());
		copy(PCL.m_vecvecstrFeatures[i].begin(),
				PCL.m_vecvecstrFeatures[i].end(),
				m_vecvecstrFeatures[i].begin());
	}
}

/*!
 * \brief
 * Load a PCL from the given text stream.
 *
 * \param istm
 * Stream from which PCL file is loaded.
 *
 * \param iSkip
 * Number of feature columns to skip between the gene IDs and first experimental column.
 *
 * \param rTable
 * Is this PCL file generated by R language (This means its missing the "YORF" label in the first line, thus has 1 less token compared to sleipnir PCL format)
 *
 * \returns
 * True if the PCL was opened successfully.
 *
 * \see
 * Save
 */

bool CPCL::Open(std::istream& istm, size_t iSkip, bool rTable) {
	vector<float> vecdData;
	size_t i;
	char* acBuf;
	bool fRet;
	string acStr;

	if (!istm.good())
		return false;

	acStr.reserve(c_iBufferSize);
	
	if (!OpenExperiments(istm, iSkip, acStr, rTable))
		fRet = false;
	else {
		m_vecvecstrFeatures.resize(m_vecstrFeatures.size() - 1);
		while (OpenGene(istm, vecdData, acStr))
			;
		for (fRet = !!GetGenes(), i = 0; i < GetGenes(); ++i)
			if (GetGene(i).empty() || !isprint(GetGene(i)[0])) {
				fRet = false;
				g_CatSleipnir().error(
						"CPCL::Open( %d ) invalid gene at index %d: %s", iSkip,
						i, GetGene(i).c_str());
				break;
			}
		if (fRet) {
			m_Data.Initialize(GetGenes(), GetExperiments());
			for (i = 0; i < m_Data.GetRows(); ++i)
				m_Data.Set(i, &vecdData[i * m_Data.GetColumns()]);
		}
	}
	//delete[] acBuf;

	return fRet;
}

bool CPCLImpl::OpenHelper() {
	unsigned char* pb;
	pb = m_abData;
	size_t i, j;
	m_vecstrFeatures.resize(*(uint32_t*) pb);
	pb += sizeof(uint32_t);
	for (i = 0; i < m_vecstrFeatures.size(); ++i) {
		string& strFeature = m_vecstrFeatures[i];
		strFeature.resize(*(uint32_t*) pb);
		pb += sizeof(uint32_t);
		for (j = 0; j < strFeature.size(); ++j)
			strFeature[j] = *pb++;
	}
	m_vecstrExperiments.resize(*(uint32_t*) pb);
	i = 0;
	pb += sizeof(uint32_t);
	for (i = 0; i < m_vecstrExperiments.size(); ++i) {
		string& strExperiment = m_vecstrExperiments[i];
		strExperiment.resize(*(uint32_t*) pb);
		pb += sizeof(uint32_t);
		for (j = 0; j < strExperiment.size(); ++j) {
			strExperiment[j] = *pb++;
		}
	}
	m_vecstrGenes.resize(*(uint32_t*) pb);
	pb += sizeof(uint32_t);
	for (i = 0; i < m_vecstrGenes.size(); ++i) {
		string& strGene = m_vecstrGenes[i];
		strGene.resize(*(uint32_t*) pb);
		pb += sizeof(uint32_t);
		for (j = 0; j < strGene.size(); ++j)
			strGene[j] = *pb++;
		m_mapstriGenes[strGene] = i;
	}
	return OpenMemmap(pb);

}

bool CPCLImpl::OpenMemmap(const unsigned char* pb) {
	size_t i;

	m_aadData = new float*[m_vecstrGenes.size()];
	m_aadData[0] = (float*) pb;
	for (i = 1; i < m_vecstrGenes.size(); ++i)
		m_aadData[i] = m_aadData[i - 1] + m_vecstrExperiments.size();
	m_Data.Initialize(m_vecstrGenes.size(), m_vecstrExperiments.size(), (float**) m_aadData);
	return true;
}

bool CPCLImpl::OpenExperiments(std::istream& istmInput, size_t iFeatures,
			       string& acStr, bool rTable) {
	const char* pc;
	string strToken;
	size_t iToken;
	
	Reset();
	if (!m_fHeader) {
		m_vecstrFeatures.resize(1 + iFeatures);
		return true;
	}
	getline(istmInput, acStr);
	if (!acStr.size()) {
		return false;
	}
	
	if(rTable){	  
	  m_vecstrFeatures.push_back("");
	}
	
	for (iToken = 0, pc = acStr.c_str(); (strToken = OpenToken(pc, &pc)).length()
			|| *pc; ++iToken)
	  if (iToken < iFeatures)
	    m_vecstrFeatures.push_back(strToken);
	  else if (iToken <= iFeatures && !rTable)
	    m_vecstrFeatures.push_back(strToken);
	  else
	    m_vecstrExperiments.push_back(strToken);
	
	return true;
}

bool CPCLImpl::OpenGene(std::istream& istmInput, std::vector<float>& vecdData,
		string& acStr) {
	const char* pc;
	char* pcEnd;
	string strToken;
	size_t iToken, iData, iBase, i;
	float d;
	map<string, size_t> mapstriValues;
	map<string, size_t>::iterator iterValue;
	const char* acLine = acStr.c_str();

	iBase = vecdData.size();
	getline(istmInput, acStr);
	if ((strToken = OpenToken(acLine)).empty())
		return false;
	if (strToken == "EWEIGHT")
		return true;
	if (!m_vecstrExperiments.empty())
		vecdData.resize(vecdData.size() + m_vecstrExperiments.size());
	for (iData = iToken = 0, pc = acLine; (strToken = OpenToken(pc, &pc)).length()
			|| *pc; ++iToken) {
		if (!iToken) {
			m_mapstriGenes[strToken] = m_vecstrGenes.size();
			m_vecstrGenes.push_back(strToken);
		} else if (iToken < m_vecstrFeatures.size())
			m_vecvecstrFeatures[iToken - 1].push_back(strToken);
		else if (!m_vecstrExperiments.empty() && (iData
				>= m_vecstrExperiments.size()))
			return false;
		else {
			d = CMeta::GetNaN();
			strToken = CMeta::Trim(strToken.c_str());
			if (strToken.length()) {
				d = (float) strtod(strToken.c_str(), &pcEnd);
				if (pcEnd != (strToken.c_str() + strToken.length())) {
					iterValue = mapstriValues.find(strToken);
					if (iterValue == mapstriValues.end()) {
						i = mapstriValues.size();
						mapstriValues[strToken] = i;
						d = i;
					} else
						d = iterValue->second;
				}
			}
			if (m_vecstrExperiments.empty())
				vecdData.push_back(d);
			else if ((i = (iBase + iData++)) >= vecdData.size())
				return false;
			else
				vecdData[i] = d;
		}
	}

	if (m_vecstrExperiments.empty())
		m_vecstrExperiments.resize(vecdData.size());
	else
		while (iData < m_vecstrExperiments.size())
			vecdData[iBase + iData++] = CMeta::GetNaN();

	return !!iToken;
}

/*!
 * \brief
 * Save the PCL's header row to the given text stream.
 *
 * \param ostm
 * Stream into which PCL header is saved.
 *
 * \param fCDT
 * If true, generate an initial CDT GENE column header.
 *
 * If called with fCDT false, saves standard PCL header and EWEIGHT rows to the given text stream using the
 * CPCL's current feature and experimental headers.  If fCDT is true, the output will instead be in CDT
 * format, with an extra initial column header for gene index identifiers.
 *
 * \see
 * Save
 */
void CPCL::SaveHeader(std::ostream& ostm, bool fCDT) const {
	size_t i;

	if (!m_fHeader)
		return;

/*	if (fCDT)
		ostm << c_szGID << '\t';
	ostm << m_vecstrFeatures[0];*/
	for (i = 1; i < m_vecstrFeatures.size(); ++i)
		ostm << '\t' << m_vecstrFeatures[i];
	for (i = 0; i < m_vecstrExperiments.size(); ++i)
		ostm << '\t' << m_vecstrExperiments[i];
	ostm << endl;

//	ostm << c_szEWEIGHT;
//	for (i = fCDT ? 0 : 1; i < m_vecstrFeatures.size(); ++i)
//		ostm << '\t';
//	for (i = 0; i < m_vecstrExperiments.size(); ++i)
//		ostm << '\t' << 1;
//	ostm << endl;
}

/*!
 * \brief
 * Save a single gene row to the given text stream.
 *
 * \param ostm
 * Stream into which gene row is saved.
 *
 * \param iGene
 * Gene index to be saved.
 *
 * \param iOriginal
 * If not equal to -1, generate an initial CDT GENE ID using the given original gene index.
 *
 * If called with iGene set to -1, saves a single row of a standard PCL to the given text stream using the
 * CPCL's current gene ID, features, and values for the row.  If a gene index is provided, the output will
 * instead be in CDT format, with an extra initial column of IDs of the form "GENE###", where the number
 * indicates the gene's original index in the pre-clustered file.
 *
 * \see
 * Save
 */
void CPCL::SaveGene(std::ostream& ostm, size_t iGene, size_t iOriginal) const {
	size_t i;
	float d;

	if (iOriginal != -1)
		ostm << c_szGENE << iOriginal << '\t';
	ostm << GetGene(iGene);
	for (i = 0; i < m_vecvecstrFeatures.size(); ++i)
		ostm << '\t' << m_vecvecstrFeatures[i][iGene];
	for (i = 0; i < m_vecstrExperiments.size(); ++i) {
		ostm << '\t';
		if (!CMeta::IsNaN(d = Get(iGene, i)))
			ostm << Get(iGene, i);
	}
	ostm << endl;
}

/*!
 * \brief
 * Save a PCL to the given text stream.
 *
 * \param ostm
 * Stream into which PCL file is saved.
 *
 * \param pveciGenes
 * If non-null, generate an initial CDT GENE column using the given original gene indices.
 *
 * If called without a vector of gene indices, saves a standard PCL to the given text stream using the CPCL's
 * current gene ID list, features, experiments, and data.  If a vector of gene indices is provided, the
 * output will instead be in CDT format, with an extra initial column of IDs of the form "GENE###", where the
 * number indicates the gene's original index in the pre-clustered file.
 *
 * \remarks
 * If pveciGenes is non-null, it must be of the same length as the PCL's gene list.
 *
 * \see
 * Open | CClustHierarchical
 */
//Save only certain genes
bool CPCL::Save(const char* szFile, const std::vector<size_t>* pveciGenes) const {
	ofstream ofsm;
	if (!strcmp(szFile + strlen(szFile) - 4, ".bin")) {
		ofsm.open(szFile, ios::binary);
		SaveBinary(ofsm);
	} else {
		ofsm.open(szFile);
		Save(ofsm, pveciGenes);
	}

}
void CPCL::Save(std::ostream& ostm, const std::vector<size_t>* pveciGenes) const {
	size_t i;

	SaveHeader(ostm, !!pveciGenes);

	for (i = 0; i < GetGenes(); ++i) {
		if (m_setiGenes.find(i) != m_setiGenes.end())
			continue;
		SaveGene(ostm, i, pveciGenes ? (*pveciGenes)[i] : -1);
	}
}

/*!
 * \brief
 * Save a PCL to the given binary stream.
 *
 * \param ostm
 * Stream into which PCL file is saved.
 *
 * \remarks
 * Most PCLs are saved as text files; binary files are more compact but very simple, encoding the size and
 * gene/feature/experiment values as simple character strings and saving data using the underlying
 * CFullMatrix::SaveBinary method.
 *
 * \see
 * SaveBinary
 */
void CPCL::SaveBinary(std::ostream& ostm) const {
	uint32_t iTmp;
	size_t i;

	iTmp = GetFeatures();
	ostm.write((const char*) &iTmp, sizeof(iTmp));
	for (i = 0; i < GetFeatures(); ++i)
		SaveString(ostm, GetFeature(i));
	iTmp = GetExperiments();
	ostm.write((const char*) &iTmp, sizeof(iTmp));
	for (i = 0; i < GetExperiments(); ++i)
		SaveString(ostm, GetExperiment(i));
	size_t iCount = 0;
	for (i = 0; i < GetGenes(); i++)
		if (!IsMasked(i))
			iCount++;
	iTmp = iCount;
	ostm.write((const char*) &iTmp, sizeof(iTmp));
	for (i = 0; i < GetGenes(); ++i)
		if (!IsMasked(i))
		SaveString(ostm, GetGene(i));

	for (i = 0; i < GetGenes(); ++i)
		if (!IsMasked(i))
		ostm.write((const char*) Get(i), GetExperiments() * sizeof(*Get(i)));
}

/*!
 * \brief
 * Load a PCL from the given binary stream.
 *
 * \param istm
 * Stream from which PCL file is loaded.
 *
 * \see
 * OpenBinary
 */
bool CPCL::OpenBinary(std::istream& istm) {
	uint32_t iTmp;
	size_t i;

	if (!istm.good())
		return false;
	

	Reset();
	istm.read((char*) &iTmp, sizeof(iTmp));
	m_vecstrFeatures.resize(iTmp);
	for (i = 0; i < m_vecstrFeatures.size(); ++i)
		OpenString(istm, m_vecstrFeatures[i]);

	istm.read((char*) &iTmp, sizeof(iTmp));
	m_vecstrExperiments.resize(iTmp);
	for (i = 0; i < m_vecstrExperiments.size(); ++i)
		OpenString(istm, m_vecstrExperiments[i]);

	istm.read((char*) &iTmp, sizeof(iTmp));
	m_vecstrGenes.resize(iTmp);
	for (i = 0; i < m_vecstrGenes.size(); ++i) {
		OpenString(istm, m_vecstrGenes[i]);
		m_mapstriGenes[m_vecstrGenes[i]] = i;
	}

	m_Data.Initialize(GetGenes(), GetExperiments());
	for (i = 0; i < m_Data.GetRows(); ++i)
		istm.read((char*) m_Data.Get(i), GetExperiments() * sizeof(*m_Data.Get(
				i)));

	return true;
}

/*!
 * \brief
 * Create a new PCL using the given genes, experiments, and features.
 *
 * \param vecstrGenes
 * Gene IDs to be used in the new PCL.
 *
 * \param vecstrExperiments
 * Experiment labels to be used in the new PCL.
 *
 * \param vecstrFeatures
 * Feature labels to be used in the new PCL (possibly empty).
 *
 * This creates an empty PCL (all entries missing) with the requested number of gene rows and experiment
 * columns; the given gene IDs and experiment labels are inserted into the appropriate header rows/columns.
 * The PCL will contain the given number of feature columns between the gene IDs and experimental values,
 * the values for which will be initialized to empty strings.
 */
void CPCL::Open(const std::vector<std::string>& vecstrGenes, const std::vector<
		std::string>& vecstrExperiments,
		const std::vector<std::string>& vecstrFeatures) {
	size_t i, j;

	Reset();
	if (vecstrFeatures.empty())
		m_vecstrFeatures.push_back("GID");
	else {
		m_vecstrFeatures.resize(vecstrFeatures.size());
		copy(vecstrFeatures.begin(), vecstrFeatures.end(),
				m_vecstrFeatures.begin());
		m_vecvecstrFeatures.resize(m_vecstrFeatures.size() - 1);
		for (i = 0; i < m_vecvecstrFeatures.size(); ++i)
			m_vecvecstrFeatures[i].resize(vecstrGenes.size());
	}

	m_vecstrGenes.resize(vecstrGenes.size());
	for (i = 0; i < GetGenes(); ++i)
		SetGene(i, vecstrGenes[i]);
	m_vecstrExperiments.resize(vecstrExperiments.size());
	for (i = 0; i < GetExperiments(); ++i)
		SetExperiment(i, vecstrExperiments[i]);

	m_Data.Initialize(GetGenes(), GetExperiments());
	for (i = 0; i < m_Data.GetRows(); ++i)
		for (j = 0; j < m_Data.GetColumns(); ++j)
			m_Data.Set(i, j, CMeta::GetNaN());
}

/*!
 * \brief
 * Create a new PCL using the given genes, experiments, and gene order.
 *
 * \param veciGenes
 * Order in which genes will be placed in the new PCL.
 *
 * \param vecstrGenes
 * Gene IDs to be used in the new PCL.
 *
 * \param vecstrExperiments
 * Experiment labels to be used in the new PCL.
 *
 * This creates an empty PCL (all entries missing) with the requested number of gene rows and experiment
 * columns; the given gene IDs and experiment labels are inserted into the appropriate header rows/columns.
 * However, the gene order will be the order of indices given in veciGenes.  For example, if veciGenes is
 * [1, 0, 2] and vecstrGenes contains [A, B, C], the order of genes within the new PCL will be [B, A, C].
 * Experiment order is unaffected and will be as given in vecstrExperiments.
 *
 * \remarks
 * veciGenes and vecstrGenes must be of the same length.
 *
 * \see
 * SortGenes
 */
void CPCL::Open(const std::vector<size_t>& veciGenes, const std::vector<
		std::string>& vecstrGenes,
		const std::vector<std::string>& vecstrExperiments) {
	size_t i, j;
	char ac[16];

	Reset();
	m_vecstrFeatures.resize(4);
	m_vecstrFeatures[0] = "GID";
	m_vecstrFeatures[1] = "YORF";
	m_vecstrFeatures[2] = "NAME";
	m_vecstrFeatures[3] = "GWEIGHT";
	m_vecvecstrFeatures.resize(m_vecstrFeatures.size() - 1);
	for (i = 0; i < m_vecvecstrFeatures.size(); ++i)
		m_vecvecstrFeatures[i].resize(veciGenes.size());
	for (i = 0; i < veciGenes.size(); ++i) {
		m_vecvecstrFeatures[0][i] = m_vecvecstrFeatures[1][i]
				= vecstrGenes[veciGenes[i]];
		m_vecvecstrFeatures[2][i] = "1";
	}

	m_vecstrGenes.resize(vecstrGenes.size());
	for (i = 0; i < m_vecstrGenes.size(); ++i) {
		m_vecstrGenes[i] = "GENE";
		sprintf_s(ac, "%d", veciGenes[i]);
		m_vecstrGenes[i] += ac;
		m_mapstriGenes[m_vecstrGenes[i]] = i;
	}
	m_vecstrExperiments.resize(vecstrExperiments.size());
	for (i = 0; i < m_vecstrExperiments.size(); ++i)
		m_vecstrExperiments[i] = vecstrExperiments[i];

	m_Data.Initialize(GetGenes(), GetExperiments());
	for (i = 0; i < m_Data.GetRows(); ++i)
		for (j = 0; j < m_Data.GetColumns(); ++j)
			m_Data.Set(i, j, CMeta::GetNaN());
}

/*!
 * \brief
 * Reorder the PCL's genes based on the order of the given indices.
 *
 * \param veciOrder
 * Index order in which genes should be placed.
 *
 * \returns
 * True if genes were reordered successfully.
 *
 * Reorders the PCL's gene rows based on the given indices.  For example, if the current row order is
 * [A, B, C] and the given indices are [1, 0, 2], the new row order will be [B, A, C].
 */
bool CPCL::SortGenes(const vector<size_t>& veciOrder) {
	size_t i;

	if (veciOrder.size() != m_Data.GetRows())
		return false;

	CMeta::Permute(m_Data.Get(), veciOrder);
	CMeta::Permute(m_vecstrGenes, veciOrder);
	for (i = 0; i < GetGenes(); ++i)
		m_mapstriGenes[GetGene(i)] = i;
	for (i = 0; i < m_vecvecstrFeatures.size(); ++i)
		CMeta::Permute(m_vecvecstrFeatures[i], veciOrder);

	return true;
}

/*!
 * \brief
 * Rank transform each row of the PCL in increasing order.
 *
 * Replaces all values in the PCL with their increasing ranks by row.  For example, a row containing
 * [-0.1, 0.1, 0.3] would be replaced with [0, 1, 2]; a row containing [0.5, 0.4, 0.3] would be replaced
 * with [2, 1, 0].
 *
 * \see
 * IMeasure::IsRank
 */
void CPCL::RankTransform() {
	size_t i, j, k;
	vector<size_t> veciRanks, veciCounts;

	veciRanks.resize(GetExperiments());
	veciCounts.resize(GetExperiments());
	for (i = 0; i < GetGenes(); ++i) {
		fill(veciRanks.begin(), veciRanks.end(), 0);
		for (j = 0; j < GetExperiments(); ++j) {
			if (CMeta::IsNaN(Get(i, j)))
				continue;
			for (k = 0; k < GetExperiments(); ++k) {
				if (CMeta::IsNaN(Get(i, k)))
					continue;
				if ((j != k) && (Get(i, k) < Get(i, j)))
					veciRanks[j]++;
			}
		}

		fill(veciCounts.begin(), veciCounts.end(), 0);
		for (j = 0; j < GetExperiments(); ++j)
			if (!CMeta::IsNaN(Get(i, j)))
				veciCounts[veciRanks[j]]++;

		for (j = 0; j < GetExperiments(); ++j)
			if (!CMeta::IsNaN(Get(i, j))) {
				k = veciRanks[j];
				// Closed form for sum(rank, rank + n) / n
				Set(i, j, k + ((veciCounts[k] + 1) / 2.0f));
			}
	}
}

/*!
 * \brief
 * Appends new, empty gene rows to the end of the PCL using the given gene IDs.
 *
 * \param vecstrGenes
 * Gene names to be appended to the PCL.
 *
 * \returns
 * True if the gene rows were appended successfully.
 *
 * \remarks
 * New rows will initially have empty strings for all features and missing values for all data.
 */
bool CPCL::AddGenes(const std::vector<std::string>& vecstrGenes) {
	size_t i, j, iStart;

	iStart = m_Data.GetRows();
	if (!m_Data.AddRows(vecstrGenes.size()))
		return false;
	for (i = iStart; i < m_Data.GetRows(); ++i)
		for (j = 0; j < m_Data.GetColumns(); ++j)
			m_Data.Set(i, j, CMeta::GetNaN());

	m_vecstrGenes.resize(m_vecstrGenes.size() + vecstrGenes.size());
	for (i = 0; i < vecstrGenes.size(); ++i)
		SetGene(iStart + i, vecstrGenes[i]);
	for (i = 0; i < m_vecvecstrFeatures.size(); ++i) {
		m_vecvecstrFeatures[i].resize(m_vecvecstrFeatures[i].size()
				+ vecstrGenes.size());
		if (m_vecstrFeatures[i + 1] == c_szNAME)
			for (j = 0; j < vecstrGenes.size(); ++j)
				m_vecvecstrFeatures[i][iStart + j] = vecstrGenes[j];
		else if (m_vecstrFeatures[i + 1] == c_szGWEIGHT)
			for (j = 0; j < vecstrGenes.size(); ++j)
				m_vecvecstrFeatures[i][iStart + j] = c_szOne;
	}

	return true;
}

/*!
 * \brief
 * Normalizes the PCL's values in the requested manner.
 *
 * \param eNormalize
 * Algorithm by which the PCL's values should be normalized.
 *
 * \see
 * ENormalize
 */
void CPCL::Normalize(ENormalize eNormalize) {
	size_t i, j, iCount;
	double dAve, dStd;
	float d, dMin, dMax;

	switch (eNormalize) {
	case ENormalizeMean:
		dMin = FLT_MAX;
		dAve = 0;
		for (iCount = i = 0; i < GetGenes(); ++i)
			for (j = 0; j < GetExperiments(); ++j)
				if (!CMeta::IsNaN(d = Get(i, j))) {
					if (d < dMin)
						dMin = d;
					iCount++;
					dAve += d;
				}
		if (!iCount)
			break;
		dAve = (dAve / iCount) - dMin;
		for (i = 0; i < GetGenes(); ++i)
			for (j = 0; j < GetExperiments(); ++j)
				if (!CMeta::IsNaN(d = Get(i, j)))
					Set(i, j, (float) ((d - dMin) / dAve));
		break;

	case ENormalizeZScore:
		dAve = dStd = 0;
		for (iCount = i = 0; i < GetGenes(); ++i)
			for (j = 0; j < GetExperiments(); ++j)
				if (!CMeta::IsNaN(d = Get(i, j))) {
					iCount++;
					dAve += d;
					dStd += d * d;
				}
		dAve /= iCount;
		dStd = sqrt((dStd / iCount) - (dAve * dAve));
		for (i = 0; i < GetGenes(); ++i)
			for (j = 0; j < GetExperiments(); ++j)
				if (!CMeta::IsNaN(d = Get(i, j)))
					Set(i, j, (float) ((d - dAve) / dStd));
		break;

	case ENormalizeRow:
		for (i = 0; i < GetGenes(); ++i) {
			dAve = CStatistics::Average(Get(i), Get(i) + GetExperiments());
			dStd = sqrt(CStatistics::Variance(Get(i),
					Get(i) + GetExperiments(), dAve));
			if (dStd)
				for (j = 0; j < GetExperiments(); ++j)
					Set(i, j, (float) ((Get(i, j) - dAve) / dStd));
		}
		break;

	case ENormalizeColumn:
	case ENormalizeColumnCenter:
	case ENormalizeColumnFraction:
		for (i = 0; i < GetExperiments(); ++i) {
			dAve = dStd = 0;
			for (iCount = j = 0; j < GetGenes(); ++j)
				if (!CMeta::IsNaN(d = Get(j, i))) {
					iCount++;
					dAve += d;
					dStd += d * d;
				}
			if (iCount) {
				if (eNormalize != ENormalizeColumnFraction) {
					dAve /= iCount;
					dStd = (dStd / iCount) - (dAve * dAve);
					dStd = (dStd <= 0) ? 1 : sqrt(dStd);
					if (eNormalize == ENormalizeColumnCenter)
						dStd = 1;
				}
				for (j = 0; j < GetGenes(); ++j)
					if (!CMeta::IsNaN(d = Get(j, i))) {
						if (eNormalize == ENormalizeColumnFraction)
							d /= (float) dAve;
						else
							d = (float) ((d - dAve) / dStd);
						Set(j, i, d);
					}
			}
		}
		break;

	case ENormalizeMinMax:
		dMin = FLT_MAX;
		dMax = -dMin;
		for (i = 0; i < GetGenes(); ++i)
			for (j = 0; j < GetExperiments(); ++j)
				if (!CMeta::IsNaN(d = Get(i, j))) {
					if (d < dMin)
						dMin = d;
					if (d > dMax)
						dMax = d;
				}
		if (!(dMax -= dMin))
			dMax = 1;
		for (i = 0; i < GetGenes(); ++i)
			for (j = 0; j < GetExperiments(); ++j)
				if (!CMeta::IsNaN(d = Get(i, j)))
					Set(i, j, (d - dMin) / dMax);
		break;
	case EMeanSubtractColumn:
		for (i = 0; i < GetExperiments(); ++i) {
			dAve = 0;
			for (iCount = j = 0; j < GetGenes(); ++j)
				if (!CMeta::IsNaN(d = Get(j, i))) {
					iCount++;
					dAve += d;

				}

			if (iCount) {
				dAve /= iCount;
				for (j = 0; j < GetGenes(); ++j)
					if (!CMeta::IsNaN(d = Get(j, i)))
						Set(j, i, (float) (d - dAve));
			}
		}
		break;
	}
}

/*!
 * \brief
 * Impute missing values using the knnimpute algorithm; optionally mask genes with too many missing values.
 *
 * \param iNeighbors
 * Number of nearest neighbors to use for imputation.
 *
 * \param dMinimumPresent
 * Fraction of conditions that must be present; genes with fewer than this many values present will be masked
 * instead of imputed.
 *
 * \param DatSimilarity
 * CDat similarity matrix from which nearest neighbors are determined.
 *
 * Imputes missing values in the PCL using the knnimpute algorithm of Troyanskaya et al, Bioinformatics 2001.
 * Briefly, for each gene with missing values, the k nearest neighbors (most similar genes) are found, and
 * the missing value is replaced with a weighted average of the values in the neighbors.  Optionally,
 * Impute can also completely mask genes that are missing too many values; genes with less than the
 * requested fraction of data present will be completely masked rather than imputed.
 *
 * \remarks
 * iNeighbors must be greater than zero, dMinimumPresent must be between zero and one inclusive, and
 * DatSimilarity must be of exactly the same size as the current PCL.
 */
void CPCL::Impute(size_t iNeighbors, float dMinimumPresent,
		const CDat& DatSimilarity) {
	vector<float> vecdMissing;
	vector<size_t> veciMissing;
	vector<SNeighbors> vecsNeighbors;
	size_t i, j, k, iCol, iMissing, iOne, iTwo;
	float d, dSum;
	const float* ad;

	vecdMissing.resize(GetGenes());
	for (i = 0; i < vecdMissing.size(); ++i) {
		for (iMissing = j = 0; j < GetExperiments(); ++j)
			if (CMeta::IsNaN(Get(i, j)))
				iMissing++;
		if ((vecdMissing[i] = (float) (GetExperiments() - iMissing)
				/ GetExperiments()) >= dMinimumPresent)
			veciMissing.push_back(i);
	}
	if (iNeighbors) {
		vecsNeighbors.resize(veciMissing.size());
		for (i = 0; i < vecsNeighbors.size(); ++i)
			vecsNeighbors[i].Initialize(Get(veciMissing[i]), GetExperiments(),
					iNeighbors);
		for (i = 0; i < veciMissing.size(); ++i) {
			if (!(i % 100))
				g_CatSleipnir().info(
						"CPCL::Impute( %d, %g ) finding neighbors for gene %d/%d",
						iNeighbors, dMinimumPresent, i, veciMissing.size());
			ad = Get(iOne = veciMissing[i]);
			for (j = (i + 1); j < veciMissing.size(); ++j) {
				d = DatSimilarity.Get(iOne, iTwo = veciMissing[j]);
				vecsNeighbors[i].Add(iTwo, d, Get(iTwo));
				vecsNeighbors[j].Add(iOne, d, ad);
			}
		}
	}

	for (iOne = i = 0; i < GetGenes(); ++i) {
		if (vecdMissing[i] < dMinimumPresent) {
			MaskGene(i);
			continue;
		}
		if (vecsNeighbors.empty())
			continue;
		{
			const SNeighbors& sGene = vecsNeighbors[iOne++];

			for (j = 0; j < GetExperiments(); ++j) {
				if (!CMeta::IsNaN(Get(i, j)))
					continue;

				iCol = sGene.GetColumn(j);
				for (d = dSum = 0, iMissing = k = 0; k < iNeighbors; ++k) {
					const pair<size_t, float>& prNeighbor =
							sGene.m_MatNeighbors.Get(k, iCol);

					if (prNeighbor.second == -FLT_MAX)
						continue;
					iMissing++;
					dSum += prNeighbor.second;
					d += Get(prNeighbor.first, j) * prNeighbor.second;
				}
				if (dSum)
					d /= dSum;
				Set(i, j, iMissing ? d : CMeta::GetNaN());
			}
		}
	}
}

/*!
 * \brief
 * Impute missing values using the knnimpute algorithm; optionally mask genes with too many missing values.
 *
 * \param iNeighbors
 * Number of nearest neighbors to use for imputation.
 *
 * \param dMinimumPresent
 * Fraction of conditions that must be present; genes with fewer than this many values present will be masked
 * instead of imputed.
 *
 * \param pMeasure
 * Similarity measure with which nearest neighbors are determined.
 *
 * \param fPrecompute
 * If true, precompute and cache in memory all pairwise similarities; otherwise, calculate these on the fly
 * from values in the PCL.
 *
 * Imputes missing values in the PCL using the knnimpute algorithm of Troyanskaya et al, Bioinformatics 2001.
 * Briefly, for each gene with missing values, the k nearest neighbors (most similar genes) are found, and
 * the missing value is replaced with a weighted average of the values in the neighbors.  Optionally,
 * Impute can also completely mask genes that are missing too many values; genes with less than the
 * requested fraction of data present will be completely masked rather than imputed.
 *
 * \remarks
 * iNeighbors must be greater than zero, dMinimumPresent must be between zero and one inclusive.  If
 * precomputation is requested, imputation will be faster, but memory proportional to the number of genes
 * squared will be required.  If it is not, imputation will be slower, but no additional memory is required.
 * As a rule of thumb, precomputation is desirable for anything less than ~25,000 genes (which will consume
 * roughly 1GB of memory; if you have RAM to spare, feel free to precompute at will).
 */
void CPCL::Impute(size_t iNeighbors, float dMinimumPresent,
		const IMeasure* pMeasure, bool fPrecompute) {
	CDat Dat;
	size_t i, j;

	if (fPrecompute) {
		Dat.Open(GetGeneNames());
		for (i = 0; i < Dat.GetGenes(); ++i)
			for (j = (i + 1); j < Dat.GetGenes(); ++j)
				Dat.Set(i, j, (float) pMeasure->Measure(Get(i),
						GetExperiments(), Get(j), GetExperiments()));
	} else
		Dat.Open(*this, pMeasure, false);
	Impute(iNeighbors, dMinimumPresent, Dat);
}

/*!
 * \brief
 * Resolves probesets by averaging agreeing probes and discarding conflicting ones.
 *
 * \param iSample
 * Number of random samples to generate for probeset null distribution.
 *
 * \param iBins
 * Number of bins for probeset distribution histogram.
 *
 * \param dBinSize
 * Size of bins for probeset distribution histogram in z-score units.
 *
 * \remarks
 * Performance can vary wildly depending on characteristics of the input PCL's probesets; use with caution.
 */
void CPCL::MedianMultiples(size_t iSample, size_t iBins, float dBinSize) {
	size_t i, j, k, iBin, iOne, iCutoff;
	CMeasureEuclidean Euclidean;
	vector<float> vecdEuclidean, vecdMapped, vecdBG, vecdFG, vecdMean;
	float d, dAve, dStd, dSample;
	vector<vector<size_t> > vecveciGenes;
	vector<size_t> veciMean;
	vector<string> vecstrAdd;

	{
		map<string, size_t> mapstriClasses;
		map<string, size_t>::iterator iterClass;

		for (i = 0; i < m_vecstrGenes.size(); ++i) {
			const string& strGene = m_vecstrGenes[i];

			if ((iterClass = mapstriClasses.find(strGene))
					== mapstriClasses.end()) {
				mapstriClasses[strGene] = vecveciGenes.size();
				vecveciGenes.push_back(vector<size_t> ());
				vecveciGenes.back().push_back(i);
			} else
				vecveciGenes[iterClass->second].push_back(i);
		}
	}

	dSample = 2.0f * iSample / GetGenes() / (GetGenes() - 1);
	dAve = dStd = 0;
	for (k = i = 0; i < GetGenes(); ++i)
		for (j = (i + 1); j < GetGenes(); ++j) {
			if (((float) rand() / RAND_MAX) >= dSample)
				continue;
			vecdEuclidean.push_back(d = (float) Euclidean.Measure(Get(i),
					GetExperiments(), Get(j), GetExperiments(),
					IMeasure::EMapNone));
			k++;
			dAve += d;
			dStd += d * d;
		}
	dAve /= k;
	dStd = sqrt((dStd / (k - 1)) - (dAve * dAve));
	if (!(iBins % 2))
		iBins++;

	vecdBG.resize(iBins);
	for (i = 0; i < vecdEuclidean.size(); ++i)
		vecdBG[MedianMultiplesBin(vecdEuclidean[i], dAve, dStd, iBins, dBinSize)]++;
	for (i = 0; i < vecdBG.size(); ++i)
		//		vecdBG[i] /= vecdEuclidean.size( );
		vecdBG[i] = (vecdBG[i] + 1) / (vecdEuclidean.size() + vecdBG.size());
	//*
	MedianMultiplesSmooth(2, vecdBG);
	//*/

	MedianMultiplesMapped(vecveciGenes, vecdMapped);
	vecdFG.resize(iBins);
	for (i = 0; i < vecdMapped.size(); ++i)
		vecdFG[MedianMultiplesBin(vecdMapped[i], dAve, dStd, iBins, dBinSize)]++;
	/*
	 dMax = 0;
	 for( iMax = i = 0; i < vecdBG.size( ); ++i )
	 if( vecdBG[i] > dMax ) {
	 dMax = vecdBG[i];
	 iMax = i; }
	 for( i = 0; i <= iMax; ++i )
	 vecdFG[i]++;
	 for( i = 0; i < vecdFG.size( ); ++i )
	 vecdFG[i] /= vecdMapped.size( ) + iMax + 1;
	 //*/
	//*/
	for (i = 0; i < vecdFG.size(); ++i)
		//		vecdFG[i] /= vecdMapped.size( );
		vecdFG[i] = (vecdFG[i] + 1) / (vecdMapped.size() + vecdFG.size());
	MedianMultiplesSmooth(2, vecdFG);
	/*
	 for( i = 0; i < vecdBG.size( ); ++i )
	 cerr << vecdBG[i] << '\t';
	 cerr << endl;
	 for( i = 0; i < vecdFG.size( ); ++i )
	 cerr << vecdFG[i] << '\t';
	 cerr << endl;
	 //*/
	for (iCutoff = 0; iCutoff < vecdFG.size(); ++iCutoff)
		if (vecdFG[iCutoff] < vecdBG[iCutoff])
			break;

	vecstrAdd.resize(1);
	veciMean.resize(GetExperiments());
	vecdMean.resize(GetExperiments());
	for (i = 0; i < vecveciGenes.size(); ++i) {
		const vector<size_t>& veciGenes = vecveciGenes[i];
		vector<size_t> veciAgree;

		if (veciGenes.size() < 2)
			continue;
		veciAgree.resize(veciGenes.size());
		for (j = 0; j < veciGenes.size(); ++j) {
			iOne = veciGenes[j];
			for (k = (j + 1); k < veciGenes.size(); ++k) {
				iBin = MedianMultiplesBin((float) Euclidean.Measure(Get(iOne),
						GetExperiments(), Get(veciGenes[k]), GetExperiments(),
						IMeasure::EMapNone), dAve, dStd, iBins, dBinSize);
				//				if( iBin <= iCutoff ) {
				if ((1.01 * vecdFG[iBin]) >= vecdBG[iBin]) {
					veciAgree[j]++;
					veciAgree[k]++;
				}
			}
		}

		fill(vecdMean.begin(), vecdMean.end(), 0.0f);
		fill(veciMean.begin(), veciMean.end(), 0);
		for (iBin = j = 0; j < veciGenes.size(); ++j)
			if ((2 * veciAgree[j]) >= (veciAgree.size() - 1)) {
				iBin++;
				iOne = veciGenes[j];
				for (k = 0; k < GetExperiments(); ++k)
					if (!CMeta::IsNaN(d = Get(iOne, k))) {
						veciMean[k]++;
						vecdMean[k] += d;
					}
			}

		for (j = 0; j < veciGenes.size(); ++j)
			MaskGene(veciGenes[j]);
		if ((iBin * 2) > veciAgree.size()) {
			for (j = 0; j < vecdMean.size(); ++j)
				vecdMean[j] = veciMean[j] ? (vecdMean[j] / veciMean[j])
						: CMeta::GetNaN();
			vecstrAdd[0] = GetGene(veciGenes[0]);
			AddGenes(vecstrAdd);
			Set(GetGenes() - 1, &vecdMean[0]);
		}
	}
}

void CPCLImpl::MedianMultiplesMapped(
		const vector<vector<size_t> >& vecveciGenes, vector<float>& vecdMapped) {
	size_t i, j, k;
	CMeasureEuclidean Euclidean;
	const float* adOne;

	for (i = 0; i < vecveciGenes.size(); ++i) {
		const vector<size_t>& veciGenes = vecveciGenes[i];

		if (veciGenes.size() < 2)
			continue;
		for (j = 0; j < veciGenes.size(); ++j) {
			adOne = m_Data.Get(veciGenes[j]);
			for (k = (j + 1); k < veciGenes.size(); ++k)
				vecdMapped.push_back((float) Euclidean.Measure(adOne,
						m_Data.GetColumns(), m_Data.Get(veciGenes[k]),
						m_Data.GetColumns(), IMeasure::EMapNone));
		}
	}
}

/**
 * Fills a PCL object from a tab text file (gene1 gene2 val)
 * gene1 is row, gene2 is col
 * used for creating a directed gene-gene network
 */
  bool CPCL::populate(const char* szFile, float dDefault){
  ifstream istm;
  const char*	pc;
  char*		pcTail;
  char*		acBuf;
  string		strToken, strCache, strValue;
  size_t		iOne, iTwo, i;
  float		dScore;
  
  istm.open( szFile );
  
  acBuf = new char[ c_iBufferSize ];
  while( istm.peek( ) != EOF ) {
    istm.getline( acBuf, c_iBufferSize - 1 );
    strToken = OpenToken( acBuf, &pc );
    if( !strToken.length( ) )
      break;
    //if( strToken == c_acComment )
    //  continue;
    if( strToken != strCache ) {
      strCache = strToken;			
      iOne = GetGene( strToken );
    }
    
    strToken = OpenToken( pc, &pc );
    if( !strToken.length( ) ) {
      delete[] acBuf;
      return false; }
    iTwo = GetGene( strToken );
    
    strValue = OpenToken( pc );
    if( !strValue.length( ) ) {
      if( CMeta::IsNaN( dScore = dDefault ) ) {
	delete[] acBuf;
	return false; } }
    else if( !( dScore = (float)strtod( strValue.c_str( ), &pcTail ) ) &&
	     ( pcTail != ( strValue.c_str( ) + strValue.length( ) ) ) ) {
      delete[] acBuf;
      return false; }
    
    Set(iOne, iTwo, dScore);
    
  }
  delete[] acBuf;
  
  return true;
}
  
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.