Example #1
0
int main( int argc, char* argv[] ) {    

  using namespace GetOpt;
  GetOpt_pp opts(argc, argv);

  if(argc == 1 || opts >> OptionPresent('h',"help")){
    printUsage();    
  }

  opts >> OptionPresent('v', "verbose", options::verbose);
  opts >> Option('i', "input-format", options::inputFormat, "");
  opts >> Option('o', "output-format", options::outputFormat, "");
  opts >> Option('a',"evolution-model", options::distMethod, "");
  opts >> Option('m',"memory-size", options::memSize, -1);
  opts >> OptionPresent('r',"rapidnj", options::rapidNJ);
  opts >> Option('k',"rapidnj-mem", options::percentageMemoryUsage, "");
  opts >> Option('d',"rapidnj-disk", options::cacheDir, "");
  opts >> OptionPresent('s',"simplenj", options::simpleNJ);
  opts >> OptionPresent('f', "no-rapiddist", options::fastdist);
  opts >> Option('c',"cores", options::cores, -1);
  opts >> Option('b',"bootstrap", options::replicates, -1);
  opts >> Option('t',"alignment-type", options::inputtype, "");
  opts >> OptionPresent('g', "gpu", options::gpu);
  opts >> OptionPresent('n', "no-negative-length", options::negative_branches);
  opts >> Option('x', "outfile", options::outputFile, "");

  options::fastdist = !options::fastdist;
  vector<string> fileNames;
  opts >> GlobalOption(fileNames);
  if(fileNames.size() < 1){
    cerr << "ERROR: An input file must be specified!" << endl;
    exit(1);
  }
  if(fileNames.size() > 1){
    cerr << "ERROR: Only one input file can be specified!" << endl;
    exit(1);
  }
  options::fileName = fileNames[0];

  if(opts.options_remain()){
    cerr << "ERROR: One or more options were not recognised!" << endl;
    exit(0);      
  }

  streambuf* sbuf;  
  ofstream fileOutStream;

  if(options::outputFile != "") {	  
    fileOutStream.open(options::outputFile.data(), fstream::out | fstream::in | fstream::binary | fstream::trunc);
    if(!fileOutStream.is_open()){      
      cerr << "Could not open the output file \"" << options::outputFile << "\"\n";
      exit(1);
    }
    sbuf = fileOutStream.rdbuf();
  } else {
    sbuf = cout.rdbuf();
  }

  ostream out(sbuf);

  if(options::verbose){
    cerr << "RapidNJ v. " << VERSION << endl;
    if(sizeof(void*) == 4) {
      cerr << "32 bit system detected." << endl;
    } else {
      cerr << "64 bit system detected." << endl;
    }
  }

  configureNumberOfCores();

#ifdef __linux__
  gettimeofday(&start, NULL);
#endif

  if(options::inputFormat == ""){
    // try to determine the input format
    if (options::fileName.compare(options::fileName.length() - 4, 4, ".sth") == 0){
      if(options::verbose){
        cerr << "Input format determined as stockholm" << endl;
      }
      options::inputFormat="sth";
    } else if (options::fileName.compare(options::fileName.length() - 3, 3, ".fa") == 0 ||
      options::fileName.compare(options::fileName.length() - 6, 6, ".fasta") == 0) {
        if(options::verbose) {
          cerr << "Input format determined as FASTA" << endl;
        }
        options::inputFormat="fa";
    } else {
      options::inputFormat = guessPhylipType(options::fileName);
      if(options::verbose){
        cerr << "Input format determined as phylip ";
        if(options::inputFormat == "pa"){
          cerr << "alignment" << endl;
        } else {
          cerr << "distance matrix" << endl;
        }
      }
    }
  }

  dataloader* dl = NULL;
  ProgressBar* pb = new ProgressBar();
    
  if(options::inputFormat == "pd"){
    //input is a distance matrix.
    if(options::replicates > -1) {
      cerr << "ERROR: Cannot perform bootstrapping with a distance matrix input." << endl;
      exit(1);
    }
    distanceMatrixInput = true;
    matrixSize = getMatrixSize();
  } else {
    dl = loadAlignment();
    matrixSize = dl->getSequenceCount();
    distanceMatrixInput = false;        
  }
  
  if(options::outputFormat == "m") {
    double memSize = getMemSize();
    double requiredMemory = 0;
    if(dl == NULL) {
      cerr << "ERROR: Both input and output format is a distance matrix." << endl;
      exit(1);
    }
    if(dl->type == DNA) {
      requiredMemory += matrixSize * dl->getSequenceLength() / 2.0;
    } else {
      requiredMemory += matrixSize * dl->getSequenceLength();
    }    
    requiredMemory += matrixSize * matrixSize * (double)sizeof(distType);    
    if(memSize < requiredMemory || options::cacheDir != "") {
      //use a disk matrix
      if(options::verbose) {
        cerr << "Using disk based matrix" << endl;
      }
      computeDistanceMatrix(true, out, true, dl);  
    } else {
      computeDistanceMatrix(false, out, true, dl);  
    }    
  } else {
    if(options::replicates > -1) {
      pb->childProgress(1.0 / (options::replicates + 1.0));
    }
    polytree* tree = computeTree(out, dl, pb);
    if(options::replicates > -1) {
      bootstrapTree(out, tree, dl, pb);
      tree->serialize_tree(out);
    } else {    
      tree->serialize_tree(out);
    }
    delete tree;
  }

  if(!distanceMatrixInput) {
    delete dl;
  }
  delete pb;

  if(options::verbose){
    cerr << endl;
    //printTime("Total running time:");
  }

  out.flush();
  if(fileOutStream.is_open()) {
    fileOutStream.close();
  }
  return 0;
}