Sleipnir
|
00001 /***************************************************************************** 00002 * This file is provided under the Creative Commons Attribution 3.0 license. 00003 * 00004 * You are free to share, copy, distribute, transmit, or adapt this work 00005 * PROVIDED THAT you attribute the work to the authors listed below. 00006 * For more information, please see the following web page: 00007 * http://creativecommons.org/licenses/by/3.0/ 00008 * 00009 * This file is a component of the Sleipnir library for functional genomics, 00010 * authored by: 00011 * Curtis Huttenhower (chuttenh@princeton.edu) 00012 * Mark Schroeder 00013 * Maria D. Chikina 00014 * Olga G. Troyanskaya (ogt@princeton.edu, primary contact) 00015 * 00016 * If you use this library, the included executable tools, or any related 00017 * code in your work, please cite the following publication: 00018 * Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and 00019 * Olga G. Troyanskaya. 00020 * "The Sleipnir library for computational functional genomics" 00021 *****************************************************************************/ 00022 #ifndef MEASURE_H 00023 #define MEASURE_H 00024 00025 #include <float.h> 00026 #include <math.h> 00027 00028 namespace Sleipnir { 00029 00052 class IMeasure { 00053 public: 00058 enum EMap { 00063 EMapNone = 0, 00068 EMapCenter = EMapNone + 1, 00073 EMapAbs = EMapCenter + 1 00074 }; 00075 00076 virtual ~IMeasure( ) { }; 00077 00085 virtual const char* GetName( ) const = 0; 00097 virtual bool IsRank( ) const = 0; 00108 virtual IMeasure* Clone( ) const = 0; 00140 virtual double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00141 const float* adWX = NULL, const float* adWY = NULL ) const = 0; 00142 }; 00143 00144 } 00145 00146 #include "measurei.h" 00147 00148 namespace Sleipnir { 00149 00162 class CMeasureSigmoid : CMeasureSigmoidImpl, public IMeasure { 00163 public: 00181 CMeasureSigmoid( const IMeasure* pMeasure, bool fMemory, float dMultiplier ) : 00182 CMeasureSigmoidImpl( pMeasure, fMemory, dMultiplier ) { } 00183 00184 const char* GetName( ) const { 00185 00186 return m_pMeasure->GetName( ); } 00187 00188 bool IsRank( ) const { 00189 00190 return m_pMeasure->IsRank( ); } 00191 00192 IMeasure* Clone( ) const { 00193 00194 return new CMeasureSigmoid( m_pMeasure->Clone( ), true, m_dMult ); } 00195 00196 double Measure( const float* adX, size_t iM, const float* adY, size_t iN, EMap eMap, const float* adWX, 00197 const float* adWY ) const { 00198 double dRet; 00199 00200 dRet = m_pMeasure->Measure( adX, iM, adY, iN, eMap, adWX, adWY ); 00201 return ( 2 * ( 1 - ( 1 / ( 1 + exp( -dRet * m_dMult ) ) ) ) ); } 00202 }; 00203 00211 class CMeasureNegate : CMeasureImpl, public IMeasure { 00212 public: 00227 CMeasureNegate( const IMeasure* pMeasure, bool fMemory ) : CMeasureImpl( pMeasure, fMemory ) { } 00228 00229 const char* GetName( ) const { 00230 00231 return m_pMeasure->GetName( ); } 00232 00233 bool IsRank( ) const { 00234 00235 return m_pMeasure->IsRank( ); } 00236 00237 IMeasure* Clone( ) const { 00238 00239 return new CMeasureNegate( m_pMeasure->Clone( ), true ); } 00240 00241 double Measure( const float* adX, size_t iM, const float* adY, size_t iN, EMap eMap, const float* adWX, 00242 const float* adWY ) const { 00243 00244 return -m_pMeasure->Measure( adX, iM, adY, iN, eMap, adWX, adWY ); } 00245 }; 00246 00254 class CMeasureInvert : CMeasureImpl, public IMeasure { 00255 public: 00270 CMeasureInvert( const IMeasure* pMeasure, bool fMemory ) : CMeasureImpl( pMeasure, fMemory ) { } 00271 00272 const char* GetName( ) const { 00273 00274 return m_pMeasure->GetName( ); } 00275 00276 bool IsRank( ) const { 00277 00278 return m_pMeasure->IsRank( ); } 00279 00280 IMeasure* Clone( ) const { 00281 00282 return new CMeasureInvert( m_pMeasure->Clone( ), true ); } 00283 00284 double Measure( const float* adX, size_t iM, const float* adY, size_t iN, EMap eMap, const float* adWX, 00285 const float* adWY ) const { 00286 double d; 00287 00288 d = m_pMeasure->Measure( adX, iM, adY, iN, eMap, adWX, adWY ); 00289 return ( d ? ( 1 / d ) : DBL_MAX ); } 00290 }; 00291 00307 class CMeasureAutocorrelate : CMeasureImpl, public IMeasure { 00308 public: 00323 CMeasureAutocorrelate( const IMeasure* pMeasure, bool fMemory ) : CMeasureImpl( pMeasure, fMemory ) { } 00324 00325 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00326 const float* adWX = NULL, const float* adWY = NULL ) const; 00327 00328 const char* GetName( ) const { 00329 00330 return m_pMeasure->GetName( ); } 00331 00332 bool IsRank( ) const { 00333 00334 return m_pMeasure->IsRank( ); } 00335 00336 IMeasure* Clone( ) const { 00337 00338 return new CMeasureAutocorrelate( m_pMeasure->Clone( ), true ); } 00339 }; 00340 00348 class CMeasureEuclidean : public IMeasure { 00349 public: 00350 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00351 const float* adWX = NULL, const float* adWY = NULL ) const; 00352 00353 const char* GetName( ) const { 00354 00355 return "euclidean"; } 00356 00357 bool IsRank( ) const { 00358 00359 return false; } 00360 00361 IMeasure* Clone( ) const { 00362 00363 return new CMeasureEuclidean( ); } 00364 }; 00365 00366 00367 class CMeasureEuclideanScaled : public IMeasure { 00368 public: 00369 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00370 const float* adWX = NULL, const float* adWY = NULL ) const; 00371 00372 const char* GetName( ) const { 00373 00374 return "euclid_scaled"; } 00375 00376 bool IsRank( ) const { 00377 00378 return false; } 00379 00380 IMeasure* Clone( ) const { 00381 00382 return new CMeasureEuclidean( ); } 00383 }; 00384 00385 00396 class CMeasurePearson : public IMeasure { 00397 public: 00398 static double Pearson( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap, 00399 const float* adWX = NULL, const float* adWY = NULL, size_t* piCount = NULL ); 00400 00401 const char* GetName( ) const { 00402 00403 return "pearson"; } 00404 00405 bool IsRank( ) const { 00406 00407 return false; } 00408 00409 IMeasure* Clone( ) const { 00410 00411 return new CMeasurePearson( ); } 00412 00413 double Measure( const float* adX, size_t iM, const float* adY, size_t iN, EMap eMap, const float* adWX, 00414 const float* adWY ) const { 00415 00416 return CMeasurePearson::Pearson( adX, iM, adY, iN, eMap, adWX, adWY ); } 00417 }; 00418 00429 class CMeasureQuickPearson : public IMeasure { 00430 public: 00431 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00432 const float* adWX = NULL, const float* adWY = NULL ) const; 00433 00434 const char* GetName( ) const { 00435 00436 return "quickpear"; } 00437 00438 bool IsRank( ) const { 00439 00440 return false; } 00441 00442 IMeasure* Clone( ) const { 00443 00444 return new CMeasureQuickPearson( ); } 00445 }; 00446 00456 class CMeasureKolmogorovSmirnov : public IMeasure { 00457 public: 00458 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00459 const float* adWX = NULL, const float* adWY = NULL ) const; 00460 00461 const char* GetName( ) const { 00462 00463 return "kolm-smir"; } 00464 00465 bool IsRank( ) const { 00466 00467 return false; } 00468 00469 IMeasure* Clone( ) const { 00470 00471 return new CMeasureKolmogorovSmirnov( ); } 00472 }; 00473 00479 class CMeasureKendallsTau : CMeasureKendallsTauImpl, public IMeasure { 00480 public: 00481 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00482 const float* adWX = NULL, const float* adWY = NULL ) const; 00483 00484 const char* GetName( ) const { 00485 00486 return "kendalls"; } 00487 00488 bool IsRank( ) const { 00489 00490 return true; } 00491 00492 IMeasure* Clone( ) const { 00493 00494 return new CMeasureKendallsTau( ); } 00495 }; 00496 00501 class CMeasureSpearman : CMeasureSpearmanImpl, public IMeasure { 00502 public: 00516 CMeasureSpearman( bool fTransformed ) : CMeasureSpearmanImpl( fTransformed ) { } 00517 00518 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00519 const float* adWX = NULL, const float* adWY = NULL ) const; 00520 00521 const char* GetName( ) const { 00522 00523 return "spearman"; } 00524 00525 bool IsRank( ) const { 00526 00527 return true; } 00528 00529 IMeasure* Clone( ) const { 00530 00531 return new CMeasureSpearman( m_fTransformed ); } 00532 }; 00533 00550 class CMeasurePearNorm : CMeasurePearNormImpl, public IMeasure { 00551 public: 00556 CMeasurePearNorm( ) : CMeasurePearNormImpl( HUGE_VAL, HUGE_VAL ) { } 00557 00573 CMeasurePearNorm( double dAverage, double dStdev ) : CMeasurePearNormImpl( dAverage, dStdev ) { } 00574 00575 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00576 const float* adWX = NULL, const float* adWY = NULL ) const; 00577 00578 const char* GetName( ) const { 00579 00580 return "pearnorm"; } 00581 00582 bool IsRank( ) const { 00583 00584 return false; } 00585 00586 IMeasure* Clone( ) const { 00587 00588 return new CMeasurePearNorm( m_dAverage, m_dStdDev ); } 00589 }; 00590 00600 class CMeasureHypergeometric : public IMeasure { 00601 public: 00602 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00603 const float* adWX = NULL, const float* adWY = NULL ) const; 00604 00605 const char* GetName( ) const { 00606 00607 return "hypergeom"; } 00608 00609 bool IsRank( ) const { 00610 00611 return false; } 00612 00613 IMeasure* Clone( ) const { 00614 00615 return new CMeasureHypergeometric( ); } 00616 }; 00617 00622 class CMeasureInnerProduct : public IMeasure { 00623 public: 00624 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00625 const float* adWX = NULL, const float* adWY = NULL ) const; 00626 00627 const char* GetName( ) const { 00628 00629 return "innerprod"; } 00630 00631 bool IsRank( ) const { 00632 00633 return false; } 00634 00635 IMeasure* Clone( ) const { 00636 00637 return new CMeasureInnerProduct( ); } 00638 }; 00639 00645 class CMeasureBinaryInnerProduct : public IMeasure { 00646 public: 00647 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00648 const float* adWX = NULL, const float* adWY = NULL ) const; 00649 00650 const char* GetName( ) const { 00651 00652 return "bininnerprod"; } 00653 00654 bool IsRank( ) const { 00655 00656 return false; } 00657 00658 IMeasure* Clone( ) const { 00659 00660 return new CMeasureBinaryInnerProduct( ); } 00661 }; 00662 00671 class CMeasureMutualInformation : public IMeasure { 00672 public: 00673 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00674 const float* adWX = NULL, const float* adWY = NULL ) const; 00675 00676 const char* GetName( ) const { 00677 00678 return "mutinfo"; } 00679 00680 bool IsRank( ) const { 00681 00682 return false; } 00683 00684 IMeasure* Clone( ) const { 00685 00686 return new CMeasureMutualInformation( ); } 00687 }; 00688 00700 class CMeasureRelativeAUC : public IMeasure { 00701 public: 00702 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00703 const float* adWX = NULL, const float* adWY = NULL ) const; 00704 00705 const char* GetName( ) const { 00706 00707 return "relauc"; } 00708 00709 bool IsRank( ) const { 00710 00711 return false; } 00712 00713 IMeasure* Clone( ) const { 00714 00715 return new CMeasureRelativeAUC( ); } 00716 }; 00717 00726 class CMeasurePearsonSignificance : public IMeasure { 00727 public: 00728 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00729 const float* adWX = NULL, const float* adWY = NULL ) const; 00730 00731 const char* GetName( ) const { 00732 00733 return "pearsig"; } 00734 00735 bool IsRank( ) const { 00736 00737 return false; } 00738 00739 IMeasure* Clone( ) const { 00740 00741 return new CMeasurePearsonSignificance( ); } 00742 }; 00743 00748 class CMeasureDistanceCorrelation : public IMeasure { 00749 public: 00750 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00751 const float* adWX = NULL, const float* adWY = NULL ) const; 00752 00753 const char* GetName( ) const { 00754 00755 return "dcor"; } 00756 00757 bool IsRank( ) const { 00758 00759 return false; } 00760 00761 IMeasure* Clone( ) const { 00762 00763 return new CMeasureDistanceCorrelation( ); } 00764 }; 00765 00766 00772 class CMeasureSignedDistanceCorrelation : public IMeasure { 00773 public: 00774 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapNone, 00775 const float* adWX = NULL, const float* adWY = NULL ) const; 00776 00777 const char* GetName( ) const { 00778 00779 return "sdcor"; } 00780 00781 bool IsRank( ) const { 00782 00783 return false; } 00784 00785 IMeasure* Clone( ) const { 00786 00787 return new CMeasureSignedDistanceCorrelation( ); } 00788 }; 00789 00795 class CMeasureDice : public IMeasure { 00796 public: 00797 CMeasureDice( float dAlpha = 0.5 ) : m_dAlpha(dAlpha) { } 00798 00799 double Measure( const float* adX, size_t iN, const float* adY, size_t iM, EMap eMap = EMapCenter, 00800 const float* adWX = NULL, const float* adWY = NULL ) const; 00801 00802 const char* GetName( ) const { 00803 00804 return "dice"; } 00805 00806 bool IsRank( ) const { 00807 00808 return false; } 00809 00810 IMeasure* Clone( ) const { 00811 00812 return new CMeasureDice( ); } 00813 00814 private: 00815 float m_dAlpha; 00816 }; 00817 00818 } 00819 00820 #endif // MEASURE_H