void Tree::writeTree(Node *node, string temp){
    if(node->isLeaf() == true){
        if(node->getContent() == '('){
            tree_written += "$(";
            sizeTree++;
        }
        else if(node->getContent() == ')'){
            tree_written += "$)";
            sizeTree++;
        }
        else if(node->getContent() == '$'){
            tree_written += "$$";
            sizeTree++;
        }
        else{
            tree_written += (char)node->getContent();
            sizeTree++;
        }
    }
    else{
        tree_written += "(";
        sizeTree++;
        writeTree(node->getLeftChild(), temp);
        writeTree(node->getRightChild(), temp);
        tree_written += ")";
        sizeTree++;
    }
}
Exemple #2
0
void writeTree(FILE *file, Tree *t) {
    if(t == NULL) return;

    writeLong(file, (long)t); // first write the id of this node
    writeTreeNode(file, t); // write the node
    writeTree(file, t->left); // recursively write the left subtree
    writeTree(file, t->right); // recursively write the right subtree
}
void autoDtd(char *inXml, char *outDtd, char *outStats, char *treeFileName,
	char *atreeFileName)
/* autoDtd - Give this a XML document to look at and it will come up with a 
 * DTD to describe it.. */
{
struct xap *xap = xapNew(startHandler, endHandler, inXml);
typeHash = newHash(0);
xapParseFile(xap, inXml);
writeDtd(outDtd, outStats, inXml, topType);
if (treeFileName != NULL)
    writeTree(treeFileName, topType, FALSE);
if (atreeFileName != NULL)
    writeTree(atreeFileName, topType, TRUE);
}
Exemple #4
0
void writeTree(Tree * current){
  if(current == NULL){
    return;
  }

  printf("%d\n",current->data);
  if(current->left != NULL){
    printf("going left ");
    writeTree(current->left);
  }
  if(current->right != NULL){
    printf("going right ");
    writeTree(current->right);
  }
}
Exemple #5
0
void ConTree::writeTree(ConNode *p, std::stringstream &ss, int site, bool calcDnds, int discardSamplesBefore) {

	ss << std::fixed << std::setprecision(5);
	
	if (p->getAnc() != NULL)
		if (p->getAnc()->getLft() == p)
			ss << "(";

	for (ConNode* q = p->getLft(); q != NULL; q = q->getSis())
		{
		if (q->getAnc() != NULL)
			if (q->getAnc()->getLft() != q)
				ss << ",";
		writeTree(q, ss, site, calcDnds, discardSamplesBefore);
		}
		
	if (p->getLft() == NULL)
		{
		double x = treeSumPtr->getVal( *(p->getPartition()), site, calcDnds, discardSamplesBefore);
		ss << p->getName() << ":" << p->getV() << ":" << x;
		}
		
	if (p->getSis() == NULL && p->getAnc() != NULL)
		{
		if (p->getAnc()->getAnc() == NULL)
			ss << ")";
		else 
			{
			double x = treeSumPtr->getVal( *(p->getPartition()), site, calcDnds, discardSamplesBefore);
			ss << ")" << ":" << p->getAnc()->getV() << ":" << x;
			}
		}
}
void makeRandomTree (treegen_t* tg, edgefn ef)
{
    resetStack(tg->sp);
    resetTree(tg->tp);
    genTree (tg->N, tg->T, tg->sp, tg->tp);
    writeTree (tg->tp, ef);
}
void writeTree(t_Node *ptTree, FILE* ofp)
{
  if(ptTree->nId != INTERNAL){
    fprintf(ofp,"%d", ptTree->nId);
  }
  else{
    fprintf(ofp,"(");

    writeTree(ptTree->ptLeft, ofp);

    fprintf(ofp,":%.3f,",ptTree->dLeft);

    writeTree(ptTree->ptRight, ofp);

    fprintf(ofp,":%.3f)",ptTree->dRight);
  }
}
Exemple #8
0
void QHelpGenerator::writeTree(QDataStream &s, QHelpDataContentItem *item, int depth)
{
    s << depth;
    s << item->reference();
    s << item->title();
    foreach (QHelpDataContentItem *i, item->children())
        writeTree(s, i, depth+1);
}
void test(){
    node *t1 = buildTree(NULL,3,0,0.5);
    node *t2 = buildTree(NULL,3,0,0.5);
    writeTree(t1,"TreeOne.dot");
    writeTree(t2,"TreeTwo.dot");
    /*
    mutate(t1,0.8);
    mutate(t2,0.8);
    writeTree(t1,"TreeOneMutate.dot");
    writeTree(t2,"TreeTwoMutate.dot");
    */
    
    swap(t1,t2,1);
    writeTree(t1,"TreeOneSwap.dot");
    writeTree(t2,"TreeTwoSwap.dot");

    deleteTree(t1);
    deleteTree(t2);
}
Exemple #10
0
void QHelpGenerator::writeTree(QDataStream &s, QHelpDataContentItem *item, int depth)
{
    QString fReference = QDir::cleanPath(item->reference());
    if (fReference.startsWith(QLatin1String("./")))
        fReference = fReference.mid(2);

    s << depth;
    s << fReference;
    s << item->title();
    foreach (QHelpDataContentItem *i, item->children())
        writeTree(s, i, depth+1);
}
Exemple #11
0
void morf_node::writeTree(morf_node* tree, const char* fileName ) 
{
  FILE * fd;

  if( fileName == NULL )
    fd = fopen("underEvolution.evo", "w+");
  else
    fd = fopen(fileName, "w+");

  writeTree(tree, fd);

  fclose(fd);
}
Exemple #12
0
void morf_node::writeTree(morf_node* tree, FILE* fd) 
{
  int i, size;

  if (tree==NULL) return;

  writeNode( tree, fd );
  size = tree->subnodes.size();
  fwrite( &size, sizeof(int), 1, fd);

  for (i=0;i<size;i++)
    writeTree(tree->subnodes[i], fd);
}
Exemple #13
0
void ParseTreeLablerForm::windowClosing()
{
    writeTree(parseTreeFileName);
    
    ofstream ofile;
    ofile.open(typeListFile,ios::out);
    for(map<string,int>::iterator it=typeMaxId.begin();it!=typeMaxId.end();it++)
    {
        ofile<<it->first<<endl;
    }
    ofile.close();
    pcl::io::savePCDFile(pcdFileName,cloud_orig,true);
    exit(0);
}
Exemple #14
0
static pwr_tStatus
writeTree(wb_dbs::sOentry *oep, FILE *fp)
{
  pwr_tStatus sts;
    
  if (!oep)
    return 1;
    
  if (oep->poep)
    oep->o.flags.b.devOnly |= oep->poep->o.flags.b.devOnly;
  
  if (fwrite(&oep->o, dbs_dAlign(sizeof(oep->o)), 1, fp) < 1)
    return LDH__FILEWRITE;

  sts = writeTree(oep->foep, fp);
  if (EVEN(sts))
    return sts;
    
  sts = writeTree(oep->aoep, fp);
  if (EVEN(sts))
    return sts;

  return 1;
}
Exemple #15
0
int main(){
  //There really must be a better way to write test-code...
  Tree * t = (Tree *) malloc(sizeof(Tree));
  t->data = 5;
  Tree * t2 = (Tree *) malloc(sizeof(Tree));
  Tree * t3 = (Tree *) malloc(sizeof(Tree));
  t2->data = 4;
  t3->data = 6;
  t->left = t2;
  t->right = t3;
  writeTree(t);
  printf("Depth is %d\n",depth(t));
  insert(t,7);
  writeTree(t);
  printf("Depth is %d\n",depth(t));
  tearDown(&t);
  if(t==NULL){
    printf("HEJ");
  }
  writeTree(t);

  return 0;

}
Exemple #16
0
pwr_tStatus
wb_dbs::writeSectObject()
{
    
  if (m_sect[dbs_eSect_object].size == 0)
    return LDH__SUCCESS;
    
  if (fseek(m_fp, m_sect[dbs_eSect_object].offset, SEEK_SET) != 0)
    return LDH__FILEPOS;

  writeTree(m_oep, m_fp);

  assert(ftell(m_fp) == (long)(m_sect[dbs_eSect_object].offset + m_sect[dbs_eSect_object].size));

  return LDH__SUCCESS;
}
Exemple #17
0
bool OptionsTreeWriter::write(QIODevice* device)
{
	setDevice(device);

	// turn it off for even more speed
	setAutoFormatting(true);
	setAutoFormattingIndent(1);

	writeStartDocument();
	writeDTD(QString("<!DOCTYPE %1>").arg(configName_));
	writeStartElement(configName_);
	writeAttribute("version", configVersion_);
	writeAttribute("xmlns", configNS_);

	writeTree(&options_->tree_);

	writeEndDocument();
	return true;
}
int rules2trees(char *fname, Tree *tree[])
{
  FILE *f;
  int ntree=0, flag;
  char buf[80];

  if( (f=fopen(fname, "r"))==NULL)
    { fprintf(stderr, "Cannot open file %s\n", fname); exit(2); }
  fscanf(f, "%s", buf); 
  assert(!strcmp(buf, "+hf+"));
  flag = 1;
  while( flag ){
    tree[ntree] = rules2tree(f, 0, &flag); 
    fprintf(stderr, "Writing tree #%d\n", ntree);
    writeTree(tree[ntree], 0, 0, 1);
    ntree++;
  }
  close(f);
  return ntree;
}
Exemple #19
0
void writeForest(forest_t* f, const char* fname){
    int i;
    char* committeename[8];
    FILE* fp = fopen(fname,"w");
    if(fp == NULL){
        fprintf(stderr,"could not write to output file: %s\n",fname);
        return;
    }
    committeename[BAGGING]="Bagging";
    committeename[BOOSTING]="Boosting";
    committeename[RANDOMFOREST]="RandomForest";

    fprintf(fp, "committee: %d (%s)\n",f->committee, committeename[f->committee]);
    fprintf(fp, "trees: %d\n", f->ngrown);
    fprintf(fp, "features: %d\n", f->nfeat);
    fprintf(fp, "maxdepth: %d\n", f->maxdepth);
    fprintf(fp, "fpnfactor: %g\n", f->factor);
    for(i=0; i<f->ngrown; i++){
        writeTree(fp,f->tree[i]);
    }
    fclose(fp);
}
int main(int argc, char* argv[])
{
  t_Node    *ptTree = NULL;
  FILE      *ifp    = NULL;
  t_Params  tParams;
  t_Data    tData;
  t_Map     tMap;
  t_Node     **aptSplit = (t_Node **) malloc(MAX_SPLIT*sizeof(t_Node *));
  int        nSplit = 0;
  double     dMaxDepth = 0.0, dSplitDepth = 0.0, dDepth = 0.0;
  int        iL = 0, nLast = 0, nCount = 0, i = 0, j = 0;
  int*       anLast = NULL;
  char szDir[MAX_WORD_LENGTH];
  char szTreeFile[MAX_WORD_LENGTH];
  char szDatFile[MAX_WORD_LENGTH];
  char szListFile[MAX_WORD_LENGTH];
  FILE* tfp = NULL, *dfp = NULL, *lfp = NULL;

  getCommandLineParams(&tParams, argc, argv);

  readData(&tData, tParams.szDatFile);

  readMapFile(&tMap, tParams.szMapFile);

  ifp = fopen(tParams.szTreeFile, "r");

  if(ifp){
    addElement(&ptTree, ifp);

    fclose(ifp);
  }
  else{
    printf("Failed to open tree file\n");
  }
  
  setLeaves(ptTree);

  treeSplitEven(ptTree, tParams.nSplit, aptSplit, &nSplit);

  for(i = 0; i < nSplit; i++){

    countLeaves(aptSplit[i],&(aptSplit[i]->nN));

    if(aptSplit[i]->nN < tParams.nMinSize){
      nLast += aptSplit[i]->nN;
    }

    aptSplit[i]->anLeaves = (int *) malloc(sizeof(int)*aptSplit[i]->nN);
    
    nCount = 0;
    
    getLeaves(aptSplit[i],aptSplit[i]->anLeaves, &nCount);
  }

  maxDepth(ptTree, &dMaxDepth);

  setDepth(ptTree, 0.0);

  /*sort on number of leaves*/
  //void qsort(void* field, size_t nElements, size_t sizeOfAnElement,
  //                 int(_USERENTRY *cmpFunc)(const void*, const void*));


  qsort(aptSplit,nSplit,sizeof(t_Node*),compNode);

  i = 0;
  while(i < nSplit && aptSplit[i]->nN >= tParams.nMinSize){

    sprintf(szDir, "C%03d",i);
    
    mkdir(szDir, S_IRWXU);

    sprintf(szTreeFile,"%s/%s%s",szDir,szDir,TREE_SUFFIX);
    sprintf(szDatFile,"%s/%s%s",szDir,szDir,DAT_SUFFIX);
    sprintf(szListFile,"%s/%s%s",szDir,szDir,LIST_SUFFIX);

    printf("%d %d %f\n",i,aptSplit[i]->nN,dMaxDepth - aptSplit[i]->dDepth);


    tfp = fopen(szTreeFile, "w");

    if(tfp){
      writeTree(aptSplit[i], tfp);
      fprintf(tfp, ";\n");
      fclose(tfp);
    }

    dfp = fopen(szDatFile, "w");

    if(dfp){
      writeData(dfp, &tData, aptSplit[i]->nN, aptSplit[i]->anLeaves, &tMap);
    
      fclose(dfp);
    }

    nCount=0;
    renumberLeaves(aptSplit[i], &nCount);

    lfp = fopen(szListFile, "w");

    if(lfp){
      writeList(lfp, aptSplit[i], dMaxDepth - aptSplit[i]->dDepth);
    
      fclose(lfp);
    }
    i++;
  }
  
  if(nLast > 0){
    anLast = (int *) malloc(sizeof(int)*nLast);
    nCount = 0;
    printf("%d %d\n",i,nLast);
    iL = i;
    for(; i < nSplit; i++){
      for(j = 0; j < aptSplit[i]->nN; j++){
	anLast[nCount + j] = aptSplit[i]->anLeaves[j];
      }
      nCount += aptSplit[i]->nN;
    }

    if(nCount > 0){
      sprintf(szDir, "C%03d+",iL);
  
      mkdir(szDir, S_IRWXU);

      sprintf(szTreeFile,"%s/%s%s",szDir,szDir,TREE_SUFFIX);
      sprintf(szDatFile,"%s/%s%s",szDir,szDir,DAT_SUFFIX);
      sprintf(szListFile,"%s/%s%s",szDir,szDir,LIST_SUFFIX);

      dfp = fopen(szDatFile, "w");

      if(dfp){
	writeData(dfp, &tData, nLast, anLast, &tMap);
    
	fclose(dfp);
      }
    }

    free(anLast);
  }
  exit(EXIT_SUCCESS);
}
Exemple #21
0
//Constrói o cabeçalho
void Tree::buildHeader(std::string filename)
{
    std::string aux_s;
    std::string aux_s2;
    bitArrays *bit = new bitArrays();
    int aux_bit;
    int k;

    aux_s.clear();
    writeTree(root, aux_s);
    aux_s.clear();

    //cout << tree_written << endl;

    tree_written = tree_written.substr(1,tree_written.length()-2);

    sizeTree = tree_written.length();
    equilibrar(tree_written);



    switch (sizeTrash) {
    case 1:
        aux_s = "001";
        break;
    case 2:
        aux_s = "010";
        break;
    case 3:
        aux_s = "011";
        break;
    case 4:
        aux_s = "100";
        break;
    case 5:
        aux_s = "101";
        break;
    case 6:
        aux_s = "110";
        break;
    case 7:
        aux_s = "111";
        break;
    case 8:
        aux_s = "000";
    default:
        break;
    }



    aux_bit = sizeTree/2;
    if(sizeTree%2 == 1)
    {
        aux_s2 = "1";
    }
    else if(sizeTree%2 == 0)
    {
        aux_s2 = "0";
    }

    /*
for(int i=0; i < 13; i++)
{
    if(aux_bit == 1)
    {
        aux_s2 += "1";
        break;
    }

    if((aux_bit % 2) == 1)
    {
        aux_s2 += "1";
    }
    else if((aux_bit % 2) == 0)
    {
        aux_s2 += "0";
    }

    aux_bit = aux_bit / 2;
}
*/

    aux_bit = sizeTree;

    while(aux_bit > 1)
    {
        aux_bit = aux_bit/2;
        if(aux_bit % 2 == 1)
        {
            aux_s2 += "1";
        }
        else if(aux_bit % 2 == 0)
        {
            aux_s2 += "0";
        }
    }

    //cout << "string: " << aux_s2 << endl;

    aux_bit = 13 - aux_s2.length();

    reverse(aux_s2.begin(), aux_s2.end());

    //cout << "string2: " << aux_s2 << endl;

    for(int i=0; i < aux_bit; i++)
    {
        aux_s += "0";
    }

    aux_s += aux_s2;

    //cout << "aux_s: " <<aux_s << endl;

    //std::string teste = aux_s.substr(0,8);

    //cout << teste << endl;

    codenode_to_bitarray(aux_s.substr(0,8), bit);

    //cout << "1: " << bit->getArray() << endl;

    codenode_to_bitarray(aux_s.substr(8,8), bit);

    aux_s.clear();

    k = 3 + filename.length() + sizeTree;

    header = new unsigned char[k];

    header = bit->getArray();

    //cout << "header: " << header << endl;

    unsigned char c = filename.length();
    header[2] = c;

    int w =0;
    for(int i =0; i<filename.length();i++)
    {
        w = i +3;
        header[w] = filename[i];
        //    cout << header[w] << " ";
    }
    //cout << endl;

    w = 0;

    int j = 3 + filename.length();

    for(int i = 0;i< sizeTree;i++)
    {
        w = i+j;
        header[w] = tree_written[i];
    }

    sizeHeader = k;
    sizeFileName = filename.length();

    for(int i=0;i<k;i++)
    {
        //cout << "header: " << i << ":" << header[i] << endl;
    }


}
Exemple #22
0
/*!
    Takes the \a helpData and generates a new documentation
    set from it. The Qt compressed help file is written to \a
    outputFileName. Returns true on success, otherwise false.
*/
bool QHelpGenerator::generate(QHelpDataInterface *helpData,
                              const QString &outputFileName)
{
    emit progressChanged(0);
    d->error.clear();
    if (!helpData || helpData->namespaceName().isEmpty()) {
        d->error = tr("Invalid help data!");
        return false;
    }

    QString outFileName = outputFileName;
    if (outFileName.isEmpty()) {
        d->error = tr("No output file name specified!");
        return false;
    }

    QFileInfo fi(outFileName);
    if (fi.exists()) {
        if (!fi.dir().remove(fi.fileName())) {
            d->error = tr("The file %1 cannot be overwritten!").arg(outFileName);
            return false;
        }
    }

    setupProgress(helpData);

    emit statusChanged(tr("Building up file structure..."));
    bool openingOk = true;
    {
        QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), QLatin1String("builder"));
        db.setDatabaseName(outFileName);
        openingOk = db.open();
        if (openingOk)
            d->query = new QSqlQuery(db);
    }

    if (!openingOk) {
        d->error = tr("Cannot open data base file %1!").arg(outFileName);
        cleanupDB();
        return false;
    }

    d->query->exec(QLatin1String("PRAGMA synchronous=OFF"));
    d->query->exec(QLatin1String("PRAGMA cache_size=3000"));

    addProgress(1.0);
    createTables();
    insertFileNotFoundFile();
    insertMetaData(helpData->metaData());

    if (!registerVirtualFolder(helpData->virtualFolder(), helpData->namespaceName())) {
        d->error = tr("Cannot register namespace %1!").arg(helpData->namespaceName());
        cleanupDB();
        return false;
    }
    addProgress(1.0);

    emit statusChanged(tr("Insert custom filters..."));
    foreach (const QHelpDataCustomFilter &f, helpData->customFilters()) {
        if (!registerCustomFilter(f.name, f.filterAttributes, true)) {
            cleanupDB();
            return false;
        }
    }
    addProgress(1.0);

    int i = 1;
    QList<QHelpDataFilterSection>::const_iterator it = helpData->filterSections().constBegin();
    while (it != helpData->filterSections().constEnd()) {
        emit statusChanged(tr("Insert help data for filter section (%1 of %2)...")
            .arg(i++).arg(helpData->filterSections().count()));
        insertFilterAttributes((*it).filterAttributes());
        QByteArray ba;
        QDataStream s(&ba, QIODevice::WriteOnly);
        foreach (QHelpDataContentItem *itm, (*it).contents())
            writeTree(s, itm, 0);
        if (!insertFiles((*it).files(), helpData->rootPath(), (*it).filterAttributes())
            || !insertContents(ba, (*it).filterAttributes())
            || !insertKeywords((*it).indices(), (*it).filterAttributes())) {
            cleanupDB();
            return false;
        }
        ++it;
    }

    cleanupDB();
    emit progressChanged(100);
    emit statusChanged(tr("Documentation successfully generated."));
    return true;
}
void test_Persistency3(bool withdot=false) {
  writeTree(withdot);
  printf("**** Write finished...\n");
  readTree(withdot);
  printf("**** Read finished...\n");
}
void Tree::buildHeader(string filename){
    string aux_s;
    string aux_s2;
    BitArray *bit = new BitArray();
    int aux_bit;
    int k;
    aux_s.clear();
    writeTree(root, aux_s);
    aux_s.clear();
    tree_written = tree_written.substr(1,tree_written.length()-2);
    sizeTree = tree_written.length();
    equilibrar(tree_written);

    switch (sizeTrash) {
    case 1:
        aux_s = "001";
        break;
    case 2:
        aux_s = "010";
        break;
    case 3:
        aux_s = "011";
        break;
    case 4:
        aux_s = "100";
        break;
    case 5:
        aux_s = "101";
        break;
    case 6:
        aux_s = "110";
        break;
    case 7:
        aux_s = "111";
        break;
    case 8:
        aux_s = "000";
    default:
        break;
    }
    aux_bit = sizeTree/2;
    if(sizeTree%2 == 1){
        aux_s2 = "1";
    }
    else if(sizeTree%2 == 0){
        aux_s2 = "0";
    }

    aux_bit = sizeTree;

    while(aux_bit > 1){
        aux_bit = aux_bit/2;
        if(aux_bit % 2 == 1){
            aux_s2 += "1";
        }
        else if(aux_bit % 2 == 0){
            aux_s2 += "0";
        }
    }

    aux_bit = 13 - aux_s2.length();
    reverse(aux_s2.begin(), aux_s2.end());
    for(int i=0; i < aux_bit; i++){
        aux_s += "0";
    }

    aux_s += aux_s2;
    codenode_to_bitarray(aux_s.substr(0,8), bit);
    codenode_to_bitarray(aux_s.substr(8,8), bit);
    aux_s.clear();
    k = 3 + filename.length() + sizeTree;
    header = new unsigned char[k];
    header = bit->getArray();
    unsigned char c = filename.length();
    header[2] = c;

    int w =0;
    for(int i =0; i<filename.length();i++){
        w = i +3;
        header[w] = filename[i];
    }
    w = 0;
    int j = 3 + filename.length();
    for(int i = 0;i< sizeTree;i++){
        w = i+j;
        header[w] = tree_written[i];
    }

    sizeHeader = k;
    sizeFileName = filename.length();

    for(int i=0;i<k;i++);

}
Exemple #25
0
int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
	try{
		vector< vector< vector<seqDist> > > calcDistsTotals;  //each iter, one for each calc, then each groupCombos dists. this will be used to make .dist files
        vector< vector<seqDist>  > calcDists; calcDists.resize(treeCalculators.size()); 		
        
        for (int thisIter = 0; thisIter < iters; thisIter++) {
            
            vector<SharedRAbundVector*> thisItersLookup = thisLookup;
            
            if (subsample) {
                SubSample sample;
                vector<string> tempLabels; //dont need since we arent printing the sampled sharedRabunds
                
                //make copy of lookup so we don't get access violations
                vector<SharedRAbundVector*> newLookup;
                for (int k = 0; k < thisItersLookup.size(); k++) {
                    SharedRAbundVector* temp = new SharedRAbundVector();
                    temp->setLabel(thisItersLookup[k]->getLabel());
                    temp->setGroup(thisItersLookup[k]->getGroup());
                    newLookup.push_back(temp);
                }
                
                //for each bin
                for (int k = 0; k < thisItersLookup[0]->getNumBins(); k++) {
                    if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
                    for (int j = 0; j < thisItersLookup.size(); j++) { newLookup[j]->push_back(thisItersLookup[j]->getAbundance(k), thisItersLookup[j]->getGroup()); }
                }
                
                tempLabels = sample.getSample(newLookup, subsampleSize);
                thisItersLookup = newLookup;
            }
            
            if(processors == 1){
                driver(thisItersLookup, 0, numGroups, calcDists);
            }else{
                int process = 1;
                vector<int> processIDS;
                
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                //loop through and create all the processes you want
                while (process != processors) {
                    pid_t pid = fork();
                    
                    if (pid > 0) {
                        processIDS.push_back(pid); 
                        process++;
                    }else if (pid == 0){
                        
                        driver(thisItersLookup, lines[process].start, lines[process].end, calcDists);   
                        
                        string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + m->mothurGetpid(process) + ".dist";
                        ofstream outtemp;
                        m->openOutputFile(tempdistFileName, outtemp);
                        
                        for (int i = 0; i < calcDists.size(); i++) {
                            outtemp << calcDists[i].size() << endl;
                            
                            for (int j = 0; j < calcDists[i].size(); j++) {
                                outtemp << calcDists[i][j].seq1 << '\t' << calcDists[i][j].seq2 << '\t' << calcDists[i][j].dist << endl;
                            }
                        }
                        outtemp.close();
                        
                        exit(0);
                    }else { 
                        m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
                        for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
                        exit(0);
                    }
                }
                
                //parent do your part
                driver(thisItersLookup, lines[0].start, lines[0].end, calcDists);   
                
                //force parent to wait until all the processes are done
                for (int i = 0; i < processIDS.size(); i++) {
                    int temp = processIDS[i];
                    wait(&temp);
                }
                
                for (int i = 0; i < processIDS.size(); i++) {
                    string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(processIDS[i]) +  ".dist";
                    ifstream intemp;
                    m->openInputFile(tempdistFileName, intemp);
                    
                    for (int k = 0; k < calcDists.size(); k++) {
                        int size = 0;
                        intemp >> size; m->gobble(intemp);
                        
                        for (int j = 0; j < size; j++) {
                            int seq1 = 0;
                            int seq2 = 0;
                            float dist = 1.0;
                            
                            intemp >> seq1 >> seq2 >> dist;   m->gobble(intemp);
                            
                            seqDist tempDist(seq1, seq2, dist);
                            calcDists[k].push_back(tempDist);
                        }
                    }
                    intemp.close();
                    m->mothurRemove(tempdistFileName);
                }
#else
                //////////////////////////////////////////////////////////////////////////////////////////////////////
                //Windows version shared memory, so be careful when passing variables through the treeSharedData struct. 
                //Above fork() will clone, so memory is separate, but that's not the case with windows, 
                //Taking advantage of shared memory to pass results vectors.
                //////////////////////////////////////////////////////////////////////////////////////////////////////
                
                vector<treeSharedData*> pDataArray; 
                DWORD   dwThreadIdArray[processors-1];
                HANDLE  hThreadArray[processors-1]; 
                
                //Create processor worker threads.
                for( int i=1; i<processors; i++ ){
                    
                    //make copy of lookup so we don't get access violations
                    vector<SharedRAbundVector*> newLookup;
                    for (int k = 0; k < thisItersLookup.size(); k++) {
                        SharedRAbundVector* temp = new SharedRAbundVector();
                        temp->setLabel(thisItersLookup[k]->getLabel());
                        temp->setGroup(thisItersLookup[k]->getGroup());
                        newLookup.push_back(temp);
                    }
                    
                    //for each bin
                    for (int k = 0; k < thisItersLookup[0]->getNumBins(); k++) {
                        if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
                        for (int j = 0; j < thisItersLookup.size(); j++) { newLookup[j]->push_back(thisItersLookup[j]->getAbundance(k), thisItersLookup[j]->getGroup()); }
                    }
                    
                    // Allocate memory for thread data.
                    treeSharedData* tempSum = new treeSharedData(m, lines[i].start, lines[i].end, Estimators, newLookup);
                    pDataArray.push_back(tempSum);
                    processIDS.push_back(i);
                    
                    hThreadArray[i-1] = CreateThread(NULL, 0, MyTreeSharedThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);   
                }
                
                //parent do your part
                driver(thisItersLookup, lines[0].start, lines[0].end, calcDists);   
                
                //Wait until all threads have terminated.
                WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
                
                //Close all thread handles and free memory allocations.
                for(int i=0; i < pDataArray.size(); i++){
                    if (pDataArray[i]->count != (pDataArray[i]->end-pDataArray[i]->start)) {
                        m->mothurOut("[ERROR]: process " + toString(i) + " only processed " + toString(pDataArray[i]->count) + " of " + toString(pDataArray[i]->end-pDataArray[i]->start) + " groups assigned to it, quitting. \n"); m->control_pressed = true; 
                    }
                    for (int j = 0; j < pDataArray[i]->thisLookup.size(); j++) {  delete pDataArray[i]->thisLookup[j];  } 
                    
                    for (int k = 0; k < calcDists.size(); k++) {
                        int size = pDataArray[i]->calcDists[k].size();
                        for (int j = 0; j < size; j++) {    calcDists[k].push_back(pDataArray[i]->calcDists[k][j]);    }
                    }
                    
                    CloseHandle(hThreadArray[i]);
                    delete pDataArray[i];
                }
                
#endif
            }
            
            calcDistsTotals.push_back(calcDists);
            
            if (subsample) {  
                
                //clean up memory
                for (int i = 0; i < thisItersLookup.size(); i++) { delete thisItersLookup[i]; }
                thisItersLookup.clear();
                for (int i = 0; i < calcDists.size(); i++) {  calcDists[i].clear(); }
            }
            
            if (m->debug) {  m->mothurOut("[DEBUG]: iter = " + toString(thisIter) + ".\n"); }
		}
        
		if (m->debug) {  m->mothurOut("[DEBUG]: done with iters.\n"); }
            
        if (iters != 1) {
            //we need to find the average distance and standard deviation for each groups distance
            vector< vector<seqDist>  > calcAverages = m->getAverages(calcDistsTotals);  
            
            if (m->debug) {  m->mothurOut("[DEBUG]: found averages.\n"); }
            
            //create average tree for each calc
            for (int i = 0; i < calcDists.size(); i++) {
                vector< vector<double> > matrix; //square matrix to represent the distance
                matrix.resize(thisLookup.size());
                for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
                
                for (int j = 0; j < calcAverages[i].size(); j++) {
                    int row = calcAverages[i][j].seq1;
                    int column = calcAverages[i][j].seq2;
                    float dist = calcAverages[i][j].dist;
                    
                    matrix[row][column] = dist;
                    matrix[column][row] = dist;
                }
                
                //create a new filename
                map<string, string> variables; 
                variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
                variables["[calc]"] = treeCalculators[i]->getName();
                variables["[distance]"] = thisLookup[0]->getLabel();
                variables["[tag]"] = "ave";
                string outputFile = getOutputFileName("tree",variables);				
                outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
                
                //creates tree from similarity matrix and write out file
                Tree* newTree = createTree(matrix);
                if (newTree != NULL) { writeTree(outputFile, newTree); }                
            }
            
            if (m->debug) {  m->mothurOut("[DEBUG]: done averages trees.\n"); }
            
            //create all trees for each calc and find their consensus tree
            for (int i = 0; i < calcDists.size(); i++) {
                if (m->control_pressed) { break; }
                
                //create a new filename
                //create a new filename
                map<string, string> variables; 
                variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
                variables["[calc]"] = treeCalculators[i]->getName();
                variables["[distance]"] = thisLookup[0]->getLabel();
                variables["[tag]"] = "all";
                string outputFile = getOutputFileName("tree",variables);				
                outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
                
                ofstream outAll;
                m->openOutputFile(outputFile, outAll);
                
                vector<Tree*> trees; 
                for (int myIter = 0; myIter < iters; myIter++) {
                    
                    if(m->control_pressed) { break; }
                    
                    //initialize matrix
                    vector< vector<double> > matrix; //square matrix to represent the distance
                    matrix.resize(thisLookup.size());
                    for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
                    
                    for (int j = 0; j < calcDistsTotals[myIter][i].size(); j++) {
                        int row = calcDistsTotals[myIter][i][j].seq1;
                        int column = calcDistsTotals[myIter][i][j].seq2;
                        double dist = calcDistsTotals[myIter][i][j].dist;
                       
                        matrix[row][column] = dist;
                        matrix[column][row] = dist;
                    }
                    
                    //creates tree from similarity matrix and write out file
                    Tree* newTree = createTree(matrix);
                    if (newTree != NULL) { 
                        newTree->print(outAll);
                        trees.push_back(newTree);
                    }
                }
                outAll.close();
                if (m->control_pressed) { for (int k = 0; k < trees.size(); k++) { delete trees[k]; } }
                
                if (m->debug) {  m->mothurOut("[DEBUG]: done all trees.\n"); }
                
                Consensus consensus;
                //clear old tree names if any
                m->Treenames.clear(); m->Treenames = m->getGroups(); //may have changed if subsample eliminated groups
                Tree* conTree = consensus.getTree(trees);
                
                if (m->debug) {  m->mothurOut("[DEBUG]: done cons tree.\n"); }
                
                //create a new filename
                variables["[tag]"] = "cons";
                string conFile = getOutputFileName("tree",variables);				
                outputNames.push_back(conFile); outputTypes["tree"].push_back(conFile); 
                ofstream outTree;
                m->openOutputFile(conFile, outTree);
                
                if (conTree != NULL) { conTree->print(outTree, "boot"); delete conTree; }
            }

        }else {
            
            for (int i = 0; i < calcDists.size(); i++) {
                if (m->control_pressed) { break; }
                
                //initialize matrix
                vector< vector<double> > matrix; //square matrix to represent the distance
                matrix.resize(thisLookup.size());
                for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
                
                for (int j = 0; j < calcDists[i].size(); j++) {
                    int row = calcDists[i][j].seq1;
                    int column = calcDists[i][j].seq2;
                    double dist = calcDists[i][j].dist;
                    
                    matrix[row][column] = dist;
                    matrix[column][row] = dist;
                }
                
                //create a new filename
                map<string, string> variables; 
                variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
                variables["[calc]"] = treeCalculators[i]->getName();
                variables["[distance]"] = thisLookup[0]->getLabel();
                variables["[tag]"] = "";
                string outputFile = getOutputFileName("tree",variables);					
                outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
                
                //creates tree from similarity matrix and write out file
                Tree* newTree = createTree(matrix);
                if (newTree != NULL) { writeTree(outputFile, newTree); delete newTree; }
            }
        }
		
		return 0;
	}
	catch(exception& e) {
		m->errorOut(e, "TreeGroupCommand", "process");
		exit(1);
	}
}
Exemple #26
0
int TreeGroupCommand::execute(){
	try {
	
		if (abort == true) { if (calledHelp) { return 0; }  return 2;	}
		
		if (format == "sharedfile") {
			
			ValidCalculators validCalculator;
		
			for (int i=0; i<Estimators.size(); i++) {
				if (validCalculator.isValidCalculator("treegroup", Estimators[i]) == true) { 
					if (Estimators[i] == "sharedsobs") { 
						treeCalculators.push_back(new SharedSobsCS());
					}else if (Estimators[i] == "sharedchao") { 
						treeCalculators.push_back(new SharedChao1());
					}else if (Estimators[i] == "sharedace") { 
						treeCalculators.push_back(new SharedAce());
					}else if (Estimators[i] == "jabund") { 	
						treeCalculators.push_back(new JAbund());
					}else if (Estimators[i] == "sorabund") { 
						treeCalculators.push_back(new SorAbund());
					}else if (Estimators[i] == "jclass") { 
						treeCalculators.push_back(new Jclass());
					}else if (Estimators[i] == "sorclass") { 
						treeCalculators.push_back(new SorClass());
					}else if (Estimators[i] == "jest") { 
						treeCalculators.push_back(new Jest());
					}else if (Estimators[i] == "sorest") { 
						treeCalculators.push_back(new SorEst());
					}else if (Estimators[i] == "thetayc") { 
						treeCalculators.push_back(new ThetaYC());
					}else if (Estimators[i] == "thetan") { 
						treeCalculators.push_back(new ThetaN());
					}else if (Estimators[i] == "kstest") { 
						treeCalculators.push_back(new KSTest());
					}else if (Estimators[i] == "sharednseqs") { 
						treeCalculators.push_back(new SharedNSeqs());
					}else if (Estimators[i] == "ochiai") { 
						treeCalculators.push_back(new Ochiai());
					}else if (Estimators[i] == "anderberg") { 
						treeCalculators.push_back(new Anderberg());
					}else if (Estimators[i] == "kulczynski") { 
						treeCalculators.push_back(new Kulczynski());
					}else if (Estimators[i] == "kulczynskicody") { 
						treeCalculators.push_back(new KulczynskiCody());
					}else if (Estimators[i] == "lennon") { 
						treeCalculators.push_back(new Lennon());
					}else if (Estimators[i] == "morisitahorn") { 
						treeCalculators.push_back(new MorHorn());
					}else if (Estimators[i] == "braycurtis") { 
						treeCalculators.push_back(new BrayCurtis());
					}else if (Estimators[i] == "whittaker") { 
						treeCalculators.push_back(new Whittaker());
					}else if (Estimators[i] == "odum") { 
						treeCalculators.push_back(new Odum());
					}else if (Estimators[i] == "canberra") { 
						treeCalculators.push_back(new Canberra());
					}else if (Estimators[i] == "structeuclidean") { 
						treeCalculators.push_back(new StructEuclidean());
					}else if (Estimators[i] == "structchord") { 
						treeCalculators.push_back(new StructChord());
					}else if (Estimators[i] == "hellinger") { 
						treeCalculators.push_back(new Hellinger());
					}else if (Estimators[i] == "manhattan") { 
						treeCalculators.push_back(new Manhattan());
					}else if (Estimators[i] == "structpearson") { 
						treeCalculators.push_back(new StructPearson());
					}else if (Estimators[i] == "soergel") { 
						treeCalculators.push_back(new Soergel());
					}else if (Estimators[i] == "spearman") { 
						treeCalculators.push_back(new Spearman());
					}else if (Estimators[i] == "structkulczynski") { 
						treeCalculators.push_back(new StructKulczynski());
					}else if (Estimators[i] == "speciesprofile") { 
						treeCalculators.push_back(new SpeciesProfile());
					}else if (Estimators[i] == "hamming") { 
						treeCalculators.push_back(new Hamming());
					}else if (Estimators[i] == "structchi2") { 
						treeCalculators.push_back(new StructChi2());
					}else if (Estimators[i] == "gower") { 
						treeCalculators.push_back(new Gower());
					}else if (Estimators[i] == "memchi2") { 
						treeCalculators.push_back(new MemChi2());
					}else if (Estimators[i] == "memchord") { 
						treeCalculators.push_back(new MemChord());
					}else if (Estimators[i] == "memeuclidean") { 
						treeCalculators.push_back(new MemEuclidean());
					}else if (Estimators[i] == "mempearson") { 
						treeCalculators.push_back(new MemPearson());
                    }else if (Estimators[i] == "jsd") {
                        treeCalculators.push_back(new JSD());
                    }else if (Estimators[i] == "rjsd") {
                        treeCalculators.push_back(new RJSD());
                    }

				}
			}
			
			//if the users entered no valid calculators don't execute command
			if (treeCalculators.size() == 0) { m->mothurOut("You have given no valid calculators."); m->mothurOutEndLine(); return 0; }
			
			input = new InputData(sharedfile, "sharedfile");
			lookup = input->getSharedRAbundVectors();
			lastLabel = lookup[0]->getLabel();
			
			if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0; }
			
			//used in tree constructor 
			m->runParse = false;
			
			//create treemap class from groupmap for tree class to use
			ct = new CountTable();
            set<string> nameMap;
            map<string, string> groupMap;
            set<string> gps;
            for (int i = 0; i < m->getAllGroups().size(); i++) { 
                nameMap.insert(m->getAllGroups()[i]); 
                gps.insert(m->getAllGroups()[i]); 
                groupMap[m->getAllGroups()[i]] = m->getAllGroups()[i];
            }
            ct->createTable(nameMap, groupMap, gps);
			
			//clear globaldatas old tree names if any
			m->Treenames.clear();
			
			//fills globaldatas tree names
			//m->Treenames = m->getGroups();
            for (int k = 0; k < lookup.size(); k++) {
                m->Treenames.push_back(lookup[k]->getGroup());
            }
		
			if (m->control_pressed) { return 0; }
			
			//create tree file
			makeSimsShared();
			
			if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {	m->mothurRemove(outputNames[i]);  } return 0; }
		}else{
			//read in dist file
			filename = inputfile;
            
            ReadMatrix* readMatrix;
			if (format == "column") { readMatrix = new ReadColumnMatrix(filename); }	
			else if (format == "phylip") { readMatrix = new ReadPhylipMatrix(filename); }
				
			readMatrix->setCutoff(cutoff);
	
            ct = NULL;
            nameMap = NULL;
            if(namefile != ""){	
                nameMap = new NameAssignment(namefile);
                nameMap->readMap();
                readMatrix->read(nameMap);
            }else if (countfile != "") {
                ct = new CountTable();
                ct->readTable(countfile, true, false);
                readMatrix->read(ct);
            }else {
                readMatrix->read(nameMap);
            }

			list = readMatrix->getListVector();
			SparseDistanceMatrix* dMatrix = readMatrix->getDMatrix();
            
            //clear globaldatas old tree names if any
			m->Treenames.clear();
            
			//make treemap
            if (ct != NULL) { delete ct; }
			ct = new CountTable();
            set<string> nameMap;
            map<string, string> groupMap;
            set<string> gps;
            for (int i = 0; i < list->getNumBins(); i++) {
                string bin = list->get(i);
                nameMap.insert(bin); 
                gps.insert(bin); 
                groupMap[bin] = bin;
                m->Treenames.push_back(bin);
            }
            ct->createTable(nameMap, groupMap, gps);
			
			vector<string> namesGroups = ct->getNamesOfGroups();
			m->setGroups(namesGroups);
			
			//used in tree constructor 
			m->runParse = false;
			
			if (m->control_pressed) { return 0; }
			
			vector< vector<double> > matrix = makeSimsDist(dMatrix);
            delete readMatrix;
            delete dMatrix;
			
			if (m->control_pressed) { return 0; }

			//create a new filename
            map<string, string> variables; 
            variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
			string outputFile = getOutputFileName("tree",variables);	
			outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
				
			Tree* newTree = createTree(matrix);
            
            if (newTree != NULL) {  writeTree(outputFile, newTree); delete newTree; }
			
			if (m->control_pressed) { return 0; }

			m->mothurOut("Tree complete. "); m->mothurOutEndLine();
			
		}
				
		//reset groups parameter
		m->clearGroups(); 
		
		//set tree file as new current treefile
		string current = "";
		itTypes = outputTypes.find("tree");
		if (itTypes != outputTypes.end()) {
			if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
		}
		
		m->mothurOutEndLine();
		m->mothurOut("Output File Names: "); m->mothurOutEndLine();
		for (int i = 0; i < outputNames.size(); i++) {	m->mothurOut(outputNames[i]); m->mothurOutEndLine();	}
		m->mothurOutEndLine();

		return 0;
	}
	catch(exception& e) {
		m->errorOut(e, "TreeGroupCommand", "execute");
		exit(1);
	}
}