Sleipnir
tools/SVDer/SVDer.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 "cmdline.h"
00024 
00025 int main( int iArgs, char** aszArgs ) {
00026     gengetopt_args_info sArgs;
00027     CPCL                PCLIn, PCLOut;
00028     CDataMatrix         MatU, MatV;
00029     vector<float>       vecdS;
00030     size_t              i, iNonzero;
00031     float               dSum, dCur;
00032 
00033     if( cmdline_parser( iArgs, aszArgs, &sArgs ) ) {
00034         cmdline_parser_print_help( );
00035         return 1; }
00036     CMeta Meta( sArgs.verbosity_arg );
00037 
00038     if( !PCLIn.Open( sArgs.input_arg, sArgs.skip_arg ) ) {
00039         cerr << "Could not open: " << ( sArgs.input_arg ? sArgs.input_arg : "stdin" ) << endl;
00040         return 1; }
00041     PCLIn.Get( ).SVD( MatU, MatV, vecdS );
00042 
00043     PCLOut.Open( PCLIn );
00044     if( sArgs.umatrix_arg ) {
00045         for( i = 0; i < PCLOut.GetGenes( ); ++i )
00046             PCLOut.Set( i, MatU.Get( i ) );
00047         PCLOut.Save( sArgs.umatrix_arg ); }
00048 
00049     for( dSum = 0,i = 0; i < vecdS.size( ); ++i )
00050         dSum += vecdS[ i ];
00051     for( dCur = 0,i = 0; i < vecdS.size( ); ++i )
00052         if( ( dCur += ( vecdS[ i ] / dSum ) ) > sArgs.reprojection_arg )
00053             break;
00054     i++;
00055     iNonzero = min( vecdS.size( ), i );
00056     for( ; i < vecdS.size( ); ++i ) {
00057         dSum -= vecdS[ i ];
00058         vecdS[ i ] = 0; }
00059     if( sArgs.signal_balance_flag ) {
00060         dSum /= iNonzero;
00061         fill( vecdS.begin( ), vecdS.end( ), dSum ); }
00062     else {
00063         for( dCur = 0,i = 0; i < iNonzero; ++i )
00064             dCur += ( vecdS[ i ] = pow( vecdS[ i ] / dSum, (float)sArgs.transform_arg ) );
00065         for( i = 0; i < iNonzero; ++i )
00066             vecdS[ i ] *= dSum / dCur; }
00067 
00068     MatU.Multiply( vecdS );
00069     MatU.Multiply( MatV, true );
00070     for( i = 0; i < PCLOut.GetGenes( ); ++i )
00071         PCLOut.Set( i, MatU.Get( i ) );
00072     PCLOut.Save( sArgs.output_arg );
00073 
00074     return 0; }