Sleipnir
tools/BNs2Txt/BNs2Txt.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 static const char   c_acXDSL[]  = ".xdsl";
00026 static const char   c_acDSL[]   = ".dsl";
00027 
00028 int main( int iArgs, char** aszArgs ) {
00029     gengetopt_args_info         sArgs;
00030     CBayesNetMinimal            BNDefault;
00031     vector<CBayesNetMinimal>    vecBNs;
00032     ifstream                    ifsm;
00033     uint32_t                    iSize;
00034     size_t                      i;
00035     CBayesNetSmile              BNSmile;
00036     CPCL                        PCLDatasets( false );
00037     string                      strDir, strFile;
00038     vector<string>              vecstrGenes;
00039 
00040     if( cmdline_parser( iArgs, aszArgs, &sArgs ) ) {
00041         cmdline_parser_print_help( );
00042         return 1; }
00043     CMeta Meta( sArgs.verbosity_arg );
00044 
00045     if( !PCLDatasets.Open( sArgs.datasets_arg, 1 ) ) {
00046         cerr << "Could not open: " << ( sArgs.datasets_arg ? sArgs.datasets_arg : "stdin" ) << endl;
00047         return 1; }
00048     vecstrGenes.resize( PCLDatasets.GetGenes( ) );
00049     for( i = 0; i < vecstrGenes.size( ); ++i )
00050         vecstrGenes[ i ] = PCLDatasets.GetFeature( i, 1 );
00051 
00052     ifsm.open( sArgs.input_arg, ios_base::binary );
00053     if( !BNDefault.Open( ifsm ) ) {
00054         cerr << "Could not read: " << sArgs.input_arg << endl;
00055         return 1; }
00056     ifsm.read( (char*)&iSize, sizeof(iSize) );
00057     vecBNs.resize( iSize );
00058     for( i = 0; i < vecBNs.size( ); ++i )
00059         if( !vecBNs[ i ].Open( ifsm ) ) {
00060             cerr << "Could not read: " << sArgs.input_arg << " (" << i << ")" << endl;
00061             return 1; }
00062     ifsm.close( );
00063 
00064     strDir = (string)sArgs.output_arg + '/';
00065     if( !BNSmile.Open( BNDefault, vecstrGenes ) )
00066         return 1;
00067     strFile = strDir + BNDefault.GetID( ) + ( sArgs.xdsl_flag ? c_acXDSL : c_acDSL );
00068     cerr << "Saving: " << strFile << endl;
00069     BNSmile.Save( strFile.c_str( ) );
00070     for( i = 0; i < vecBNs.size( ); ++i ) {
00071         if( !BNSmile.Open( vecBNs[ i ], vecstrGenes ) )
00072             return 1;
00073         strFile = strDir + vecBNs[ i ].GetID( ) + ( sArgs.xdsl_flag ? c_acXDSL : c_acDSL );
00074         cerr << "Saving: " << strFile << endl;
00075         BNSmile.Save( strFile.c_str( ) ); }
00076 
00077     return 0; }