/*
we want to cut the first row and the fourth row.
chromosomes and positions.
But we want to check if the cromosomes are 1-22
These are the ones to include, so well return a keeplist,
and the correct pars->chromo and pars->positions.

This is rather slow because we use make a new vector on each line.
This can be optimized in future versions. */
bArray *doBimFile(functionPars* pars,const char *filename,const std::string delim){
   ///@param filename A filename to read.@param delim A string of delimiters.
  
  std::vector<int> chromos;//for all lines
  std::vector<double> positions;//for all lines
  

  const int SIZE=500000;//this should only be 6 elements but lets make it big..
  char buffer[SIZE];
 
  std::ifstream pFile (filename,std::ios::in);
 
  
  if(!pFile){
    fileError(filename);
    exit(0);
  }

  
  std::string tmp_string;
  int itemsInRow;
  int numRows =0;
  while(!pFile.eof()){
    pFile.getline(buffer,SIZE);
    tmp_string = std::string(buffer);
    std::vector<std::string> tokens;
    itemsInRow = get_lexemes(tmp_string,tokens,delim);
      
    if (itemsInRow!=6 && itemsInRow!=0){
      printf("plink bim file:%s doesn't have 6 columns in row:%d\n",filename,numRows);
      exit(0);
    }else if(itemsInRow==0)
      break;
    chromos.push_back(atoi((tokens[0]).c_str()));
    positions.push_back(atof((tokens[3]).c_str()));
    numRows++;
  }
  //  copy(chromos.begin(), chromos.end(), std::ostream_iterator<int>(std::cout, ", "));  
  //  copy(positions.begin(), positions.end(), std::ostream_iterator<float>(std::cout, ", "));  
  bArray *ret = allocBoolArray(chromos.size());
  int numTrue = 0;
  for(unsigned int i=0;i<chromos.size();i++)
    if(chromos[i]!=0 && chromos[i]<23 ){
      ret->array[i] = 1;
      numTrue++;
    }else
      ret->array[i] = 0;
  ret->numTrue = numTrue;

  dArray *pos = allocDoubleArray(ret->numTrue);
  iArray *chr = allocIntArray(ret->numTrue);
  int atPos=0;
  for(int i=0;i<ret->x;i++){
    if(ret->array[i]){
      pos->array[atPos] = positions[i]/PLINK_POS_SCALING;
      chr->array[atPos] = chromos[i];
      
      atPos++;
    }

  }
  pars->chr  = chr;
  pars->position= pos;
  return ret;
}
예제 #2
0
 //! constructor.
 Matrices()
 {
   //work=new double[n];
   work=allocDoubleArray(n);
   lwork=n;
 }
dArray *typecast_stringarray_to_dArray(const std::vector<std::string> &tokens){
  dArray *ary = allocDoubleArray(tokens.size());
  for (unsigned int i=0;i<tokens.size();i++)
    ary->array[i] = to_double( tokens.at(i));
  return ary;
}