Esempio n. 1
0
iMatrix *readmatrix(std::string filename,const std::string delim=",;: \t"){
  ///@param filename A filename to read.@param delim A string of delimiters.
  std::vector<std::string> tokens;

  const int SIZE=500000;
  char buffer[SIZE];
 
  std::ifstream pFile (filename.c_str(),std::ios::in);
 
  
  if(!pFile){
    std::cout <<"Problems opening file" <<filename<<std::endl;
    exit(0);
  }

  
  std::string tmp_string;
  int doFirstRow =1;
  int itemsInFirstRow=0;
  int numRows =0;
  while(!pFile.eof()){
    pFile.getline(buffer,SIZE);
    tmp_string = std::string(buffer);
    
    if(doFirstRow){
      //if file has a emptystart line
      itemsInFirstRow = get_lexemes(tmp_string,tokens,delim);
      
      if (itemsInFirstRow==0)
	continue;
      // printf("items in first rwo:%d\n",itemsInFirstRow);
      doFirstRow=0;
      numRows++;
    }
    else{
      int nItems = get_lexemes(tmp_string,tokens,delim);
	//if line is empty
	if(nItems==0)
	  continue;
	numRows++;
	if(nItems!=itemsInFirstRow){
	  printf("row length mismatch at line:%d numitems is:%d shouldn't be:%d\t will exit\n",numRows,itemsInFirstRow,nItems);
	  exit(0);
	}
      }
  }

  iMatrix *data_ = allocIntMatrix(numRows,itemsInFirstRow);
  //now we have a token array of string coerce the types now
  typecast_stringarray_to_int_matrix(tokens,data_);
  //copy(tokens.begin(), tokens.end(), ostream_iterator<string>(cout, ", "));
  
  printf("\t-> Dimension of genotype datafile is (%d,%d)\n",data_->x,data_->y);
  return data_; 
}
iMatrix *readmatrix_filty_memory(std::string filename,const std::string delim=",;: \t"){
  ///@param filename A filename to read.@param delim A string of delimiters.
  std::vector<std::string> tokens;

  const int SIZE = MAX_ELEMS_PER_LINE;//defined in conf.h
  char buffer[SIZE];
 
  std::ifstream pFile (filename.c_str(),std::ios::in);
 
  
  if(!pFile){
    fileError(filename);
    exit(0);
  }

  
  std::string tmp_string;
  int doFirstRow =1;
  int itemsInFirstRow=0;
  int numRows =0;
  while(!pFile.eof()){
     pFile.getline(buffer,SIZE);
    tmp_string = std::string(buffer);
    if(doFirstRow){
      //if file has a emptystart line
      itemsInFirstRow = get_lexemes(tmp_string,tokens,delim);
      
      if (itemsInFirstRow==0)
	continue;
      // printf("items in first rwo:%d\n",itemsInFirstRow);
      doFirstRow=0;
      numRows++;
    }
    else{
      int nItems = get_lexemes(tmp_string,tokens,delim);
	//if line is empty
	if(nItems==0)
	  continue;
	numRows++;
	if(nItems!=itemsInFirstRow){
	  printf("row length mismatch at line:%d numitems is:%d shouldn't be:%d\t will exit\n",numRows,itemsInFirstRow,nItems);
	  exit(0);
	}
      }
  }
  flush_print("\r\t-> File has been read in now, will now typecheck...                              ");
  iMatrix *data_ = allocIntMatrix(numRows,itemsInFirstRow);
  //now we have a token array of string coerce the types now
  typecast_stringarray_to_int_matrix(tokens,data_);
  //copy(tokens.begin(), tokens.end(), ostream_iterator<string>(cout, ", "));
  return data_; 
}