Sleipnir
src/meta.h
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 META_H
00023 #define META_H
00024 
00025 #include <float.h>
00026 #include <math.h>
00027 #include <vector>
00028 
00029 #include "metai.h"
00030 
00031 #ifdef _MSC_VER
00032 #include <windows.h>
00033 #include <winsock.h>
00034 #else // _MSC_VER
00035 #include <stdarg.h>
00036 #include <sys/time.h>
00037 
00038 #define _finite isfinite
00039 
00040 typedef size_t  HANDLE;
00041 
00042 inline int sprintf_s( char* szDest, const char* szFormat, ... ) {
00043     va_list valArgs;
00044 
00045     va_start( valArgs, szFormat );
00046     return vsprintf( szDest, szFormat, valArgs ); }
00047 
00048 #if !defined(__PIC__) && defined(__GLIBC__) && defined(__GTHREAD_HAS_COND)
00049 asm(".globl pthread_cond_wait");
00050 asm(".globl pthread_cond_broadcast");
00051 #endif
00052 #endif // _MSC_VER
00053 
00054 #ifndef ARRAYSIZE
00055 #define ARRAYSIZE(a)    (sizeof(a)/sizeof(*a))
00056 #endif // ARRAYSIZE
00057 #ifndef SIZE_MAX
00058 #define SIZE_MAX        ((size_t)-1)
00059 #endif // SIZE_MAX
00060 
00061 namespace Sleipnir {
00062 
00073 class CMeta : CMetaImpl {
00074 public:
00079     static const char   c_szWS[];
00080 
00081     static std::string Filename( const std::string& strString, char cReplacement = '_' );
00082     static std::string Basename( const char* szPath );
00083     static void Tokenize( const char* szString, std::vector<std::string>& vecstrTokens,
00084         const char* szSeparators = "\t", bool fNoEmpties = false );
00085     static std::string Trim( const char* szString );
00086     static bool MapRead( unsigned char*& pbData, HANDLE& hndlMap, size_t& iSize, const char* szFile );
00087     static bool MapWrite( unsigned char*& pbData, HANDLE& hndlMap, size_t iSize, const char* szFile );
00088     static void Unmap( const unsigned char* pbData, HANDLE hndlMap, size_t iSize );
00089     static size_t GetMemoryUsage( );
00090 
00109     template<class tType>
00110     static bool IsNaN( tType Value ) {
00111 
00112 
00113         return !_finite( Value ); }
00114 
00128     static float GetNaN( ) {
00129 
00130         return (float)HUGE_VAL; }
00131 
00145     static std::string Deextension( const std::string& strName ) {
00146         size_t  i;
00147 
00148         return ( ( ( i = strName.rfind( c_cPeriod ) ) == std::string::npos ) ? strName :
00149             strName.substr( 0, i ) ); }
00150 
00165     template <class tIterator>
00166     static void Permute( tIterator Items, const std::vector<size_t>& veciOrder ) {
00167         std::vector<size_t>                     veciMap, veciCopy;
00168         size_t                                  i;
00169           typename iterator_traits<tIterator>::value_type   Temp;
00170 
00171         veciCopy.resize( veciOrder.size( ) );
00172         veciMap.resize( veciOrder.size( ) );
00173         for( i = 0; i < veciOrder.size( ); ++i ) {
00174             veciCopy[ i ] = veciOrder[ i ];
00175             veciMap[ veciOrder[ i ] ] = i; }
00176 
00177         for( i = 0; i < veciOrder.size( ); ++i ) {
00178             if( veciCopy[ i ] == i )
00179                 continue;
00180             Temp = Items[ i ];
00181             Items[ i ] = Items[ veciCopy[ i ] ];
00182             Items[ veciCopy[ i ] ] = Temp;
00183             veciCopy[ veciMap[ i ] ] = veciCopy[ i ];
00184             veciMap[ veciCopy[ i ] ] = veciMap[ i ]; } }
00185 
00204     template <class tType>
00205     static void Permute( std::vector<tType>& vecItems, const std::vector<size_t>& veciOrder ) {
00206 
00207         Permute( vecItems.begin( ), veciOrder ); }
00208 
00233     template <class tType>
00234     static size_t Quantize( tType Value, const std::vector<tType>& vecQuants ) {
00235 
00236         if( IsNaN( Value ) )
00237             return -1;
00238 
00239         /*
00240         size_t i;
00241         for( i = 0; i < vecQuants.size( ); ++i )
00242             if( Value <= vecQuants[ i ] )
00243                 break;
00244         size_t r = min(i, vecQuants.size()-1);
00245         return r;
00246          */
00247         
00248         size_t mid = vecQuants.size() / 2;
00249         int i = mid;
00250 
00251         if(Value <= vecQuants[i]){
00252             i--;
00253             //LEFT direction
00254             while(i>=0){
00255                 if(Value <= vecQuants[i]){
00256                     i--;
00257                 }else{
00258                     i++;
00259                     break;
00260                 }
00261             }
00262             if(i==-1){
00263                 i = 0;
00264             }
00265         }else{
00266             i++;
00267             //RIGHT direction
00268             while(i<vecQuants.size()){
00269                 if(Value > vecQuants[i]){
00270                     i++;
00271                 }else{
00272                     break;
00273                 }
00274             }
00275             if(i==vecQuants.size()){
00276                 i = vecQuants.size() - 1;
00277             }
00278         }
00279 
00280         size_t ii = i;
00281 
00282         return ii;
00283     }
00284 
00298     static size_t GetMicroseconds( const struct timeval& sBegin, const struct timeval& sEnd ) {
00299 
00300         return ( ( sBegin.tv_sec == sEnd.tv_sec ) ? ( sEnd.tv_usec - sBegin.tv_usec ) :
00301             ( ( 1000000 * ( sEnd.tv_sec - sBegin.tv_sec ) ) + sEnd.tv_usec - sBegin.tv_usec ) ); }
00302 
00343     static bool SkipEdge( bool fAnswer, size_t i, size_t j, const std::vector<bool>& vecfHere, const std::vector<bool>& vecfUbik, bool fCtxtPos, bool fCtxtNeg, bool fBridgePos, bool fBridgeNeg, bool fOutPos, bool fOutNeg ) {
00344         if ( vecfHere.size( ) ) {
00345         bool fIn = vecfHere[ i ] && vecfHere[ j ];
00346                 if( fIn ) {
00347                 if ( fAnswer && !fCtxtPos ) {
00348                     return true;
00349             }
00350             else if ( !fAnswer && !fCtxtNeg ) {
00351                         return true;
00352                 }
00353             }
00354         bool fBridge;
00355             if ( vecfUbik.size( ) ) {
00356             fBridge = !fIn && ( ( vecfUbik[ i ] && vecfHere[ j ] ) || ( vecfHere[ i ] && vecfUbik[ j ] ) );
00357         }
00358         else {
00359             fBridge = ( !!vecfHere[ i ] ^ !!vecfHere[ j ] );
00360         }
00361         if( fBridge ) {
00362             if ( fAnswer && !fBridgePos ) {
00363             return true;
00364             }
00365             else if ( !fAnswer && !fBridgeNeg ) {
00366             return true;
00367             }
00368         }
00369         bool fOut = !( fIn || fBridge );
00370         if( fOut ) {
00371             if ( fAnswer && !fOutPos) {
00372             return true;
00373             }
00374             else if ( !fAnswer && !fOutNeg ) {
00375             return true;
00376             }
00377         }
00378         }
00379         return false;
00380     }
00381 
00395     static bool IsExtension( const std::string& strFile, const std::string& strExtension ) {
00396         size_t  i;
00397 
00398         return ( ( strFile.length( ) >= strExtension.length( ) ) &&
00399             ( ( i = strFile.rfind( strExtension ) ) ==
00400             ( strFile.length( ) - strExtension.length( ) ) ) ); }
00401 
00420     CMeta( int iVerbosity, size_t iRandomSeed = 0 );
00421     /* 
00422      * \brief
00423      * Perform Sleipnir cleanup.
00424      */
00425     ~CMeta( );
00426 };
00427 
00428 }
00429 
00430 #endif // META_H