#include #include #include #include #include #include #include #include "io.h" // ========================================================= // Open file with error messages // ========================================================= void file_open(string filename, ofstream &outstream){ cout << "Open "<< filename<<" for writing\n"; outstream.open (filename.c_str(), ofstream::out); if ( outstream.is_open() ){ if(outstream.bad()){ cerr << "file_open: An error occured in File" << filename << ".\n"; exit( 1 ); } }else{ cerr<<"file_open: File " << filename <<" could not be opened\n"; exit( 1 ); } return; } // ========================================================= // Convert string to float // ========================================================= float FloatFromString(const std::string& s) { std::istringstream i_tmp(s); float x; if (i_tmp >> x) { return x; } cerr << "FloatFromString: Non-Numeric value '" << s << "'\n"; return 0.0; } // ========================================================= // Convert string to Integer // ========================================================= int IntFromString(const std::string& s) { std::istringstream i_tmp(s); int x; if (i_tmp >> x) return x; cerr << "IntFromString:Non-Numeric value"; return((int)(0)); } /* ============================================== */ int word_count(char *s) { bool blank; int i; int nword; nword = 0; blank = true; while (! (*s)){ if ( *s == ' ' ){ blank = true; }else if( blank ){ nword = nword + 1; blank = false; } *s++; } return nword; } /* ============================================== * Function read_float_vec * ============================================== */ void read_float_vec(string filename, float * &vec, int n){ // Open file for reading FILE *fp = fopen(filename.c_str(), "r"); if(!fp){ cerr << "read_float_vec: An error occured. File " << filename << ".\n"; cerr << "fp = " << fp; return; } // Alloc vec vec = new float[n]; // Read mat float val; for(int i=0;i0); } } fclose(fp); return; } /* ============================================== * Function read_float_mat_bin * ============================================== */ void read_float_mat_bin(string filename, float ** &mat, int &n_rows,int &n_cols) { // Open file for reading FILE *fp = fopen(filename.c_str(), "r"); if(!fp){ cerr << "read_float_mat_bin: An error occured. File " << filename << ".\n"; n_cols=0; n_rows=0; return; } // Read n_rows and n_cols float val; fread (&n_rows,sizeof(n_rows),1,fp); fread (&n_cols,sizeof(n_cols),1,fp); // Alloc mat float * tmp_for_alloc_float; tmp_for_alloc_float=new float[n_rows*n_cols]; mat = new float* [n_rows]; for(int i=0;i &list) { // Open file for reading ifstream infile; infile.open(filename.c_str(), ifstream::in); if ( infile.is_open() ){ if(infile.bad()){ cerr << "read_int_vector: An error occured. File" << filename << ".\n"; exit( 1 ); } }else{ cerr << "** read_int_vector: File " << filename << " could not be opened\n"; exit( 1 ); } int item; while ( infile >> item ){ list.push_back(item); } infile.close(); return; } /* ============================================== * Function read_float_vector * ============================================== */ void read_float_vector(string filename, vector &list) { // Open file for reading ifstream infile; infile.open(filename.c_str(), ifstream::in); if ( infile.is_open() ){ if(infile.bad()){ cerr << "read_float_vector: An error occured. File" << filename << ".\n"; exit( 1 ); } }else{ cerr << "** read_float_vector: File " << filename << " could not be opened\n"; exit( 1 ); } float item; while ( infile >> item ){ list.push_back(item); } infile.close(); return; } /* ============================================== * Function read_float_vector * ============================================== */ void read_double_vector(string filename, vector &list) { // Open file for reading ifstream infile; infile.open(filename.c_str(), ifstream::in); if ( infile.is_open() ){ if(infile.bad()){ cerr << "read_double_vector: An error occured. File" << filename << ".\n"; exit( 1 ); } }else{ cerr << "** read_double_vector: File " << filename << " could not be opened\n"; exit( 1 ); } double item; while ( infile >> item ){ list.push_back(item); } infile.close(); return; } /* ============================================== * Function read_string_vector * ============================================== */ void read_string_vector(string filename, vector &list) { // Open file for reading ifstream infile; infile.open(filename.c_str(), ifstream::in); if ( infile.is_open() ){ if(infile.bad()){ cerr << "read_string_vector: An error occured. File" << filename << ".\n"; exit( 1 ); } }else{ cerr << "** read_string_vector: File " << filename << " could not be opened\n"; exit( 1 ); } string item; while ( infile >> item ){ list.push_back(item); } infile.close(); return; } /* ============================================== * Function read_string_int_hash * ============================================== */ void read_string_int_hash(string filename, map &hash) { // Open file for reading ifstream infile; infile.open(filename.c_str(), ifstream::in); if ( infile.is_open() ){ if(infile.bad()){ cerr << "** An error occured. File " << filename << ".\n"; exit( 1 ); } }else{ cerr << "** File " << filename << " could not be opened\n"; exit( 1 ); } string key; int val; while ( infile >> key >> val ){ hash[key] = val; } infile.close(); return; } /* ==============================================*/ int s_len_trim ( char *s ) { int n; char *t; n = strlen (s); t = s + strlen(s) - 1; while ( 0 < n ){ if ( *t != ' ' ){ return n; } t--; n--; } return n; } /* ============================================== * file_count_columns: count no of columns in a file * ============================================== */ int count_columns(string filename) { // Init int column_num; ifstream infile; bool got_one; char line[256]; // Open file for reading infile.open(filename.c_str(), ifstream::in); if ( infile.is_open() ){ if(infile.bad()){ cerr << "** An error occured. File " << filename << ".\n"; return(-1); } }else{ cerr << "** File " << filename << " could not be opened\n"; return(-1); } // Read one line, but skip blank comment lines (liens with # or %) got_one = false; for ( ; ; ){ infile.getline ( line, sizeof ( line ) ); if ( infile.eof ( ) ){ break; } if ( s_len_trim ( line ) == 0 ){ continue; } if ( line[0] == '#' ){ continue;} if ( line[0] == '%' ){ continue;} got_one = true; break; } infile.close(); // Try again dont ignore comments lines if ( !got_one ) { infile.open ( filename.c_str() ); for ( ; ; ) { infile.getline ( line, sizeof ( line ) ); if ( infile.eof ( ) ){break;} if ( s_len_trim ( line ) == 0 ){ continue; } got_one = true; break; } infile.close(); } if ( !got_one ) { cerr << "** File " << filename << " does not ontain any data\n"; return -1; } column_num = word_count ( line ); return(column_num); } /* ============================================== * file_count_rows: count no of rows in a file * ============================================== */ int count_rows(string filename) { int bad_num; int comment_num; ifstream infile; int i; char line[256]; int record_num; int row_num; row_num = 0; comment_num = 0; record_num = 0; bad_num = 0; // Open file for reading infile.open(filename.c_str(), ifstream::in); if ( infile.is_open() ){ if(infile.bad()){ cerr << "** An error occured. File " << filename << ".\n"; return(-1); } }else{ cerr << "** File " << filename << " could not be opened\n"; return(-1); } for ( ; ; ) { infile.getline ( line, sizeof ( line ) ); if ( infile.eof ( ) ){break;}; record_num = record_num + 1; if ( line[0] == '#' ){comment_num = comment_num + 1; continue;} if ( line[0] == '%' ){comment_num = comment_num + 1; continue;} if ( s_len_trim ( line ) == 0 ){ comment_num = comment_num + 1; continue; } row_num = row_num + 1; } infile.close ( ); return row_num; } // ============================================================== // Read the expression matrix // ============================================================== void read_expression_mat(string& filename, int n_skip, int n_headers, float** &express, bool** &mask, int &n_lines, int &n_fields ){ // Count no of linesand cols in the matrix (tab delimited) unsigned int ii_field; int i_field; unsigned int nn_fields; unsigned int nn_lines; int i_line; char delimiter = '\t'; count_lines_and_fields(filename, nn_lines, nn_fields, delimiter); n_fields = nn_fields - n_skip; n_lines = nn_lines - n_headers; // Allocate contiguous n_rows by n_columns matrix bool * tmp_for_alloc_bool; tmp_for_alloc_bool=new bool[n_lines*n_fields]; mask=new bool* [n_lines] ; for(int i=0;i=n_skip prev = curr+1; } // loop over fields } // lopp over lines // Clean and exit //cerr << "<< Finished reading expression file\n"; in_stream.close(); return; } // ========================================================= // Count no of linesand columns in a matrix // ========================================================= void count_lines_and_fields(string& filename, unsigned int &n_lines, unsigned int &n_fields, char delimiter){ // Open file for reading ifstream in_stream(filename.c_str()); if(!in_stream){ cerr << filename << ": not found.\n"; exit(1); } unsigned int max_n_fields=0; unsigned int prev=0; unsigned int curr=0; // Count n_lines string line; n_lines=0; while (getline(in_stream,line)){ n_lines++; // Count fields per line n_fields = 0; do{ curr = line.find(delimiter, prev); n_fields++; prev = curr+1; }while(curr != string::npos); if(n_fields> max_n_fields) max_n_fields=n_fields; } n_fields = max_n_fields; // Clean and exit in_stream.close(); return; } // ========================================================= // Read the first column of a (tab delimited) matrix file // ========================================================= void read_string_column(string& filename, int n_headers, string* &col, int &n_lines, char delimiter){ int i_line; // Allocate contiguous n_rows by n_columns matrix col=new string[n_lines]; if(col ==NULL) {cerr << "failed malloc for col\n";exit(1);}; // Open file for reading, and skip header lines ifstream in_stream(filename.c_str()); string line; for(i_line=0;i_line vec) { ofstream& newStream( outstream ); newStream << header; newStream << "=["; for(unsigned int i=0;i vec) { ofstream& newStream( outstream ); newStream << header; newStream << "=["; for(unsigned int i=0;i vec) { ofstream& newStream( outstream ); newStream << header; newStream << "=["; for(unsigned int i=0;i vec) { cout << header; cout << "=["; for(unsigned int i=0;i vec) { cout << header; cout << "=["; for(unsigned int i=0;i vec) { cout << header; cout << "=["; for(unsigned int i=0;i