Sleipnir
tools/Filterer/Filterer.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     CDat                Dat;
00028     vector<string>      vecstrTokens;
00029     size_t              iArg, i, j;
00030     float               d, dMin, dMax;
00031     CHalfMatrix<char>   MatStatus;
00032     bool                fDefaultExclude;
00033     char                c;
00034 
00035     if( cmdline_parser( iArgs, aszArgs, &sArgs ) ) {
00036         cmdline_parser_print_help( );
00037         return 1; }
00038     CMeta Meta( sArgs.verbosity_arg );
00039 
00040     if( !Dat.Open( sArgs.input_arg ) ) {
00041         cerr << "Could not open: " << sArgs.input_arg << endl;
00042         return 1; }
00043 
00044     MatStatus.Initialize( Dat.GetGenes( ) );
00045     MatStatus.Clear( );
00046     fDefaultExclude = false;
00047     for( iArg = 0; iArg < sArgs.inputs_num; ++iArg ) {
00048         vecstrTokens.clear( );
00049         CMeta::Tokenize( sArgs.inputs[iArg] + 1, vecstrTokens, "=", true );
00050         d = (float)atof( vecstrTokens[0].c_str( ) );
00051         if( vecstrTokens.size( ) == 1 ) {
00052             if( ( sArgs.inputs[iArg][1] ) == '=' ) {
00053                 dMin = -FLT_MAX;
00054                 dMax = d; }
00055             else {
00056                 dMin = d;
00057                 dMax = FLT_MAX; } }
00058         else {
00059             dMin = d;
00060             dMax = (float)atof( vecstrTokens[1].c_str( ) ); }
00061         switch( sArgs.inputs[iArg][0] ) {
00062             case 'i':
00063                 fDefaultExclude = true;
00064                 for( i = 0; i < Dat.GetGenes( ); ++i )
00065                     for( j = ( i + 1 ); j < Dat.GetGenes( ); ++j )
00066                         if( !CMeta::IsNaN( d = Dat.Get( i, j ) ) && ( d <= dMax ) && ( d >= dMin ) &&
00067                             ( MatStatus.Get( i, j ) == 0 ) )
00068                             MatStatus.Set( i, j, 1 );
00069                 break;
00070 
00071             case 'x':
00072                 for( i = 0; i < Dat.GetGenes( ); ++i )
00073                     for( j = ( i + 1 ); j < Dat.GetGenes( ); ++j )
00074                         if( !CMeta::IsNaN( d = Dat.Get( i, j ) ) && ( d <= dMax ) && ( d >= dMin ) )
00075                             MatStatus.Set( i, j, -1 );
00076                 break;
00077 
00078             default:
00079                 cerr << "Unrecognized command: " << sArgs.inputs[iArg] << endl;
00080                 return 1; } }
00081 
00082     for( i = 0; i < Dat.GetGenes( ); ++i )
00083         for( j = ( i + 1 ); j < Dat.GetGenes( ); ++j ) {
00084             c = MatStatus.Get( i, j );
00085             if( ( c < 0 ) || ( fDefaultExclude && ( c < 1 ) ) )
00086                 Dat.Set( i, j, CMeta::GetNaN( ) ); }
00087     Dat.Save( sArgs.output_arg );
00088 
00089     return 0; }