TARIS-Matrices.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Universidad Nacional de Colombia                *
00003  *   http://www.unal.edu.co                                                *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 
00024 #include <iostream>
00025 #include <cstdlib>
00026 #include <iomanip>
00027 using namespace std;
00028 
00029 #include <HyperSurface.h>
00030 #include <Surface.h>
00031 #include <TarisApplication.h>
00032 #include <Tree.h>
00033 #include <vector>
00034 
00035 namespace Programs{
00036 
00048 class TARIS_Matrices{
00049         public:
00050                 static string usage()
00051                 {
00052                         system("clear") ;
00053                         return
00054                         "============================\n"
00055                         "TARIS-Matrices (" __DATE__ ")\n"
00056                         "============================\n"
00057                         "\n"
00058                         "Produces a text file containing distance and similiraty matrices\n"
00059                         "for a given set of molecules, taking as parameters a text file with \n"
00060                         "the list of .cube files for each molecule, the initial cutoff, the \n"
00061                         "final cutoff and the step-size for the scan\n"
00062                         "\n"
00063                         "Syntax:\n"
00064                         "   $ TARIS-Matrices -i list [PARAMETERS]\n"
00065                         "\n"
00066                         "Required parameters:\n"
00067                         "   -i   list\n"
00068                         "           This is the name of the file containing the name of the\n"
00069                         "           cube/gml files to be analyzed\n"
00070                         "\n"
00071                         "Optional parameters:\n"
00072                         "   -t   format\n"
00073                         "           Specifies the input data type: cube or gml files\n"
00074                         "           (default=cube)\n"
00075                         "   -b   cutoffBegin\n"
00076                         "           Initial cutoff for the scan\n"
00077                         "           (default=-0.1)\n"
00078                         "   -e   cutoffEnd\n"
00079                         "           Final cutoff for the scan\n"
00080                         "           (default=-0.07)\n"
00081                         "   -s   stepSize\n"
00082                         "           Step-size for the scan\n"
00083                         "           (default=-0.005)\n"
00084                         "   -m   type\n"
00085                         "           Type of matrix to be generated: distance, similarity or both\n"
00086                         "           (default=both)\n"
00087                         "   -o   output\n"
00088                         "           Name of the output file\n"
00089                         "           (default=screen)\n"
00090                         "\n"
00091                         "For example:\n"
00092                         "   Generating matrices from .cube files\n"
00093                         "      $ find . -name \"*.cube\" > list\n"
00094                         "      $ TARIS-Matrices -i list -o matrices.dat\n"
00095                         "   Generating matrices from .gml files\n"
00096                         "      $ find . -name \"*.gml\" > list\n"
00097                         "      $ TARIS-Matrices -i list -type gml -o matrices.dat\n"
00098                         "\n"
00099                         "Authors:\n"
00100                         "    Nestor F. Aguirre, Ray M. Marin and Edgar E. Daza\n"
00101                         "    Universidad Nacional de Colombia\n"
00102                         ;
00103                 }
00104                 
00105                 static string extractFileName( const string& path )
00106                 {
00107                         char pathTMP[256] ;
00108                         strcpy( pathTMP, path.c_str() ) ;
00109                         vector<string> tokens ;
00110                         
00111                         char* pch = strtok( pathTMP, "/." );
00112                         while( pch != NULL )
00113                         {
00114                                 tokens.push_back( pch ) ;
00115                                 pch = strtok( NULL, "/." );
00116                         }
00117                         
00118                         return tokens[ tokens.size() - 2 ] ;
00119                 }
00120                 
00121                 static void printMatrix( double** matrix, int size, vector< string > moleculesName, ostream& os )
00122                 {
00123                         os.setf( ios::fixed ) ;
00124                         os.precision(6) ;
00125                         
00126                         string filename ;
00127                         int spaceForName = 15 ;
00128                         
00129                         for (int i = 0; i< size; i++){
00130                                 
00131                                 filename = extractFileName( moleculesName[i] ) ;
00132                                 
00133                                 if( filename.length() <= spaceForName ){
00134                                         os << setw(spaceForName) << extractFileName( moleculesName[i] ) << " :\t" ;
00135                                 }else{
00136                                         os << setw(spaceForName-3) << extractFileName( moleculesName[i] ).substr(0, spaceForName-3) << "... :\t" ;
00137                                 }
00138                                 
00139                                 for (int j = 0; j< size; j++){
00140                                         os << setw(12) << matrix[i][j] ;
00141                                 }
00142                                 os << endl ;
00143                         }
00144                 }
00145                 
00146                 static int main( int argc, char **argv )
00147                 {
00148                         
00149                         if( argc > 1 ){
00150                                 
00151                                 string cubeList = TarisApplication::extractParameter( argc, argv, "-i" ) ;
00152                                 string type = TarisApplication::extractParameter( argc, argv, "-t", "cube" ) ;
00153                                 double cutoffBegin = atof( TarisApplication::extractParameter( argc, argv, "-b", "-0.1" ) ) ;
00154                                 double cutoffEnd = atof( TarisApplication::extractParameter( argc, argv, "-e", "-0.07" ) ) ;
00155                                 double stepSize = atof( TarisApplication::extractParameter( argc, argv, "-s", "0.005" ) ) ;
00156                                 string matrix = TarisApplication::extractParameter( argc, argv, "-m", "both" ) ;
00157                                 string outputFileName = TarisApplication::extractParameter( argc, argv, "-o", "screen" ) ;
00158                                 
00159                                 ifstream file( cubeList.c_str() ) ;
00160                                 
00161                                 //Este arreglo contiene los nombres de las moleculas
00162                                 vector< string > moleculesName ;
00163                                 string filenameTMP ;
00164         
00165                                 while( !file.eof() ) {
00166                 
00167                                         file >> filenameTMP ;
00168                                         moleculesName.push_back( filenameTMP ) ;
00169                                 }
00170                                 
00171                                 int size = moleculesName.size() - 1 ;
00172                                 
00173                                 //Se crea la matriz de distancias
00174                                 double** distance = new double*[ size ];
00175                                 for (int i = 0; i< size; i++){
00176                                         distance[i] = new double[ size ] ; 
00177                                 }
00178         
00179                                 //Se crea la matriz de similitud
00180                                 double** similarity = new double*[ size ];
00181                                 for (int i = 0; i< size; i++){
00182                                         similarity[i] = new double[ size ] ; 
00183                                 }
00184                                 
00185                                 vector<Tree> treeVector ;
00186                                 
00187                                 if( type == "cube" ){
00188                                         
00189                                         HyperSurface hs ;       
00190                                         /*******************************************************
00191                                         * Se genera el arbol de cada molecula y se guarda en
00192                                         * un vector de arboles treeVector
00193                                         */
00194                                         for (int i=0 ; i<size; i++){
00195                                                 cout << "BUILDING TREE FOR: (" << i + 1 << ") " << extractFileName( moleculesName[i] ) << endl ;
00196                                                 hs.load( moleculesName[i] ) ;
00197                                                 treeVector.push_back( hs.buildAreaTree( cutoffBegin, cutoffEnd, stepSize ) ) ;
00198                                         }
00199                                         
00200                                 }else if( type == "gml" ){
00201                                         
00202 //                                      Tree treeTMP ;
00203                                         
00204                                         /*******************************************************
00205                                         * Se carga el arbol de cada molecula y se guarda en
00206                                         * un vector de arboles treeVector
00207                                         */
00208                                         for (int i=0 ; i<size; i++){
00209                                                 Tree treeTMP ;
00210                                                 treeTMP.load( moleculesName[i] ) ;
00211                                                 treeVector.push_back( treeTMP ) ;
00212 //                                              treeTMP.clear() ;
00213                                         }
00214                                         
00215                                 }else{
00216                                         cout << "Ilegal value for parameter -type; please use \"cube\" or \"gml\"" << endl ;
00217                                         exit(-1) ;
00218                                 }
00219                                 
00220                                 cout << endl ;
00221                                 cout << "GENERATING DISTANCE MATRIX ... "  ;
00222                         
00223                                 double max = 0.0 ;
00224                         
00225                                 // Se calcula la matriz de distancia
00226                                 for (int i = 0; i< size; i++){
00227                 
00228                                         if ( i < size-1 ) {
00229                         
00230                                                 for (int j = i+1; j< size; j++){
00231                                 
00232                                                         distance[i][j] = Tree::distance( treeVector[i], treeVector[j] ) ; 
00233                                                         distance[j][i] = distance[i][j] ;
00234                                                         if(distance[i][j] > max){
00235                                                                 max = distance[i][j] ;
00236                                                         }
00237                                                 }
00238                                         }
00239                                 }
00240                                 
00241                                 cout << "OK" << endl ;
00242                                 cout << "GENERATING SIMILARITY PERCENTAGE MATRIX ... "  ;
00243                                 
00244                                 // Se calcula la matriz de similitud
00245                                 if (max >= 1e-6){
00246                                         for (int i = 0; i< size; i++){
00247                                                 for (int j = 0; j< size; j++){
00248                                                         similarity[i][j] = ( 1-(distance[i][j] / max)) *100.0 ; 
00249                                                 }
00250                                         }
00251                                 }else{
00252                                         for (int i = 0; i< size; i++){
00253                                                 for (int j = 0; j< size; j++){
00254                                                         similarity[i][j] = 100.0 ; 
00255                                                 }
00256                                         }
00257                                 }
00258                                 
00259                                 cout << "OK" << endl ;
00260                                 cout << endl ;
00261                                 
00262                                 if( outputFileName.compare("screen") == 0  ){
00263                                         
00264                                         if( matrix.compare("distance") == 0  || matrix.compare("both") == 0 ){
00265                                                 cout << "DISTANCE MATRIX 'D'" << endl ;
00266                                                 cout << "-------------------" << endl ;
00267         
00268                                                 printMatrix( distance, size, moleculesName, cout ) ;
00269                                                 cout << endl ;
00270                                                 cout << "THE MAXIMUM DISTANCE IS: " << max << endl ;
00271                                                 cout << endl ;
00272                                         }
00273                                         
00274                                         if( matrix.compare("similarity") == 0 || matrix.compare("both") == 0 ){
00275                                                 cout << "SIMILARITY PERCENTAGE MATRIX 'S'" << endl ;
00276                                                 cout << "--------------------------------" << endl ;
00277         
00278                                                 printMatrix( similarity, size, moleculesName, cout ) ;
00279                                         }
00280 
00281                                 }else{
00282                                         ofstream file( outputFileName.c_str() ) ;
00283                                         
00284                                         if( matrix.compare("distance") == 0  || matrix.compare("both") == 0 ){
00285                                                 file << "DISTANCE MATRIX 'D'" << endl ;
00286                                                 file << "-------------------" << endl ;
00287         
00288                                                 printMatrix( distance, size, moleculesName, file ) ;
00289                                                 file << endl ;
00290                                                 file << "THE MAXIMUM DISTANCE IS: " << max << endl ;
00291                                                 file << endl ;
00292                                         }
00293                                         
00294                                         if( matrix.compare("similarity") == 0 || matrix.compare("both") == 0 ){
00295                                                 file << "SIMILARITY PERCENTAGE MATRIX 'S'" << endl ;
00296                                                 file << "--------------------------------" << endl ;
00297         
00298                                                 printMatrix( similarity, size, moleculesName, file ) ;
00299                                         }
00300                                         
00301                                         file.close() ;
00302                                 }
00303                                 
00304                         }else{
00305                                 cout << usage() << endl ;
00306                         }
00307 
00308                         return EXIT_SUCCESS;
00309                 }
00310 };
00311 
00312 }
00313 
00314 int main( int argc, char **argv )
00315 {
00316         return Programs::TARIS_Matrices::main( argc, argv ) ;
00317 }

Generated on Mon May 26 20:29:46 2008 for TARIS by  doxygen 1.5.4