Sleipnir
src/bayesnet.cpp
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 #include "stdafx.h"
00023 #include "bayesnet.h"
00024 #include "dataset.h"
00025 
00026 namespace Sleipnir {
00027 
00028 const char  CBayesNetImpl::c_szFR[]     = "FR";
00029 const char  CBayesNetImpl::c_szZero[]   = "zero";
00030 
00031 void CBayesNetImpl::EncodeData( const IDataset* pData, TMapData& mapData ) {
00032     size_t              i, j;
00033     string              strCur;
00034     TMapData::iterator  iterDatum;
00035 
00036     for( i = 0; i < pData->GetGenes( ); ++i )
00037         for( j = ( i + 1 ); j < pData->GetGenes( ); ++j ) {
00038             if( !pData->IsExample( i, j ) || ( pData->GetDiscrete( i, j, 0 ) == -1 ) )
00039                 continue;
00040             strCur = EncodeDatum( pData, i, j );
00041             if( ( iterDatum = mapData.find( strCur ) ) == mapData.end( ) )
00042                 mapData[ strCur ] = 1;
00043             else
00044                 iterDatum->second++; } }
00045 
00046 string CBayesNetImpl::EncodeDatum( const IDataset* pData, size_t iOne, size_t iTwo ) {
00047     string  strRet;
00048     size_t  i, iCur;
00049 
00050     for( i = 0; i < pData->GetExperiments( ); ++i )
00051         strRet += ( ( iCur = pData->GetDiscrete( iOne, iTwo, i ) ) == -1 ) ? c_cMissing :
00052             (char)( c_cBase + ( iCur & 0xFF ) );
00053 
00054     return strRet; }
00055 
00056 string CBayesNetImpl::EncodeDatum( const CPCLPair& PCL, size_t iGene, const vector<size_t>& veciMap ) {
00057     string  strRet;
00058     size_t  i, iCur;
00059 
00060     for( i = 0; i < veciMap.size( ); ++i )
00061         strRet += ( ( veciMap[ i ] == -1 ) || ( ( iCur = PCL.Quantize( PCL.Get( iGene, veciMap[ i ] ),
00062             veciMap[ i ] ) ) == -1 ) ) ? c_cMissing : (char)( c_cBase + ( iCur & 0xFF ) );
00063 
00064     return strRet; }
00065 
00066 void CBayesNetImpl::DecodeDatum( const std::string& strDatum, std::vector<size_t>& veciDatum ) {
00067     size_t  i;
00068 
00069     for( i = 0; i < strDatum.length( ); ++i )
00070         veciDatum[ i ] = ( strDatum[ i ] == c_cMissing ) ? -1 : ( strDatum[ i ] - c_cBase ); }
00071 
00072 bool CBayesNetImpl::IsAnswer( const std::string& strDatum ) {
00073 
00074     return ( strDatum[ 0 ] != c_cMissing ); }
00075 
00076 CBayesNetImpl::CBayesNetImpl( bool fGroup ) : m_fGroup(fGroup) { }
00077 
00078 }