]> git.donarmstrong.com Git - mothur.git/commitdiff
added subsample and consensus parameters to unifrac.weighted command
authorSarah Westcott <mothur.westcott@gmail.com>
Wed, 11 Apr 2012 12:07:05 +0000 (08:07 -0400)
committerSarah Westcott <mothur.westcott@gmail.com>
Wed, 11 Apr 2012 12:07:05 +0000 (08:07 -0400)
13 files changed:
indicatorcommand.cpp
preclustercommand.cpp
subsample.cpp
subsample.h
tree.cpp
tree.h
treegroupscommand.cpp
treemap.cpp
treemap.h
trimseqscommand.cpp
unifracweightedcommand.cpp
unifracweightedcommand.h
weighted.cpp

index 01d8c2e8634d65c31f11515f89ecfa950b81a1ae..fe99c46847ff08fa8359f6899a4ddf9a095126ea 100644 (file)
@@ -334,7 +334,6 @@ int IndicatorCommand::execute(){
                        //no longer need original tree, we have output tree to use and label
                        for (int i = 0; i < T.size(); i++) {  delete T[i];  } 
                        
-                                       
                        if (m->control_pressed) { 
                                if (designfile != "") { delete designMap; }
                                if (sharedfile != "") {  for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } }
index f2fbc80088b1adf2927ee78d5b1e02fbf9691cb1..bcff0fcd820a497da04405bef0ddb42d7d60ded9 100644 (file)
@@ -314,7 +314,17 @@ int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newF
                                processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
                                process++;
                        }else if (pid == 0){
+                outputNames.clear();
                                num = driverGroups(parser, newFName + toString(getpid()) + ".temp", newNName + toString(getpid()) + ".temp", newMFile, lines[process].start, lines[process].end, groups);
+                
+                string tempFile = toString(getpid()) + ".outputNames.temp";
+                ofstream outTemp;
+                m->openOutputFile(tempFile, outTemp);
+                
+                outTemp << outputNames.size();
+                for (int i = 0; i < outputNames.size(); i++) { outTemp << outputNames[i] << endl; }
+                outTemp.close();
+                
                                exit(0);
                        }else { 
                                m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
@@ -331,7 +341,23 @@ int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newF
                        int temp = processIDS[i];
                        wait(&temp);
                }
-               
+        
+        for (int i = 0; i < processIDS.size(); i++) {
+            string tempFile = toString(processIDS[i]) +  ".outputNames.temp";
+            ifstream intemp;
+            m->openInputFile(tempFile, intemp);
+            
+            int num;
+            intemp >> num;
+            for (int k = 0; k < num; k++) {
+                string name = "";
+                intemp >> name; m->gobble(intemp);
+                
+                outputNames.push_back(name); outputTypes["map"].push_back(name);
+            }
+            intemp.close();
+            m->mothurRemove(tempFile);
+        }
 #else
                
                //////////////////////////////////////////////////////////////////////////////////////////////////////
index e6dd845adf9b64bec6c932f4cf72de6d8b9fe909..b1e78a44a0a2e5b5cd31e7c38e27710603ee1578 100644 (file)
@@ -8,6 +8,114 @@
 
 #include "subsample.h"
 
+//**********************************************************************************************************************
+Tree* SubSample::getSample(Tree* T, TreeMap* tmap, map<string, string> whole, int size) {
+    try {
+        Tree* newTree = NULL;
+        
+        vector<string> subsampledSeqs = getSample(tmap, size);
+        map<string, string> sampledNameMap = deconvolute(whole, subsampledSeqs); 
+        
+        //remove seqs not in sample from treemap
+        for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
+            //is that name in the subsample?
+            int count = 0;
+            for (int j = 0; j < subsampledSeqs.size(); j++) {
+                if (tmap->namesOfSeqs[i] == subsampledSeqs[j]) { break; } //found it
+                count++;
+            }
+
+            if (m->control_pressed) { return newTree; }
+            
+            //if you didnt find it, remove it 
+            if (count == subsampledSeqs.size()) { 
+                tmap->removeSeq(tmap->namesOfSeqs[i]);
+                i--; //need this because removeSeq removes name from namesOfSeqs
+            }
+        }
+        
+        //create new tree
+        int numUniques = sampledNameMap.size();
+        if (sampledNameMap.size() == 0) { numUniques = subsampledSeqs.size(); }
+        
+        newTree = new Tree(numUniques, tmap); //numNodes, treemap
+        newTree->getSubTree(T, subsampledSeqs, sampledNameMap);
+        
+        return newTree;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "SubSample", "getSample-Tree");
+        exit(1);
+    }
+}      
+//**********************************************************************************************************************
+//assumes whole maps dupName -> uniqueName
+map<string, string> SubSample::deconvolute(map<string, string> whole, vector<string>& wanted) {
+    try {
+        map<string, string> nameMap;
+        
+        //whole will be empty if user gave no name file, so we don't need to make a new one
+        if (whole.size() == 0) { return nameMap; }
+        
+        vector<string> newWanted;
+        for (int i = 0; i < wanted.size(); i++) {
+            
+            if (m->control_pressed) { break; }
+            
+            string dupName = wanted[i];
+            
+            map<string, string>::iterator itWhole = whole.find(dupName);
+            if (itWhole != whole.end()) {
+                string repName = itWhole->second;
+                
+                //do we already have this rep?
+                map<string, string>::iterator itName = nameMap.find(repName);
+                if (itName != nameMap.end()) { //add this seqs to dups list
+                    (itName->second) += "," + dupName;
+                }else { //first sighting of this seq
+                    nameMap[repName] = dupName;
+                    newWanted.push_back(repName);
+                }
+            }else { m->mothurOut("[ERROR]: "+dupName+" is not in your name file, please correct.\n"); m->control_pressed = true; }
+        }
+        
+        wanted = newWanted;
+        return nameMap;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "SubSample", "deconvolute");
+               exit(1);
+       }
+}      
+//**********************************************************************************************************************
+vector<string> SubSample::getSample(TreeMap* tMap, int size) {
+    try {
+        vector<string> sample;
+        
+        vector<string> Groups = tMap->getNamesOfGroups();    
+        for (int i = 0; i < Groups.size(); i++) {
+            
+            if (m->control_pressed) { break; }
+            
+            vector<string> thisGroup; thisGroup.push_back(Groups[i]);
+            vector<string> thisGroupsSeqs = tMap->getNamesSeqs(thisGroup);
+            int thisSize = thisGroupsSeqs.size();
+            
+            if (thisSize >= size) {    
+                
+                random_shuffle(thisGroupsSeqs.begin(), thisGroupsSeqs.end());
+                
+                for (int j = 0; j < size; j++) { sample.push_back(thisGroupsSeqs[j]); }
+            }else {  m->mothurOut("[ERROR]: You have selected a size that is larger than "+Groups[i]+" number of sequences.\n"); m->control_pressed = true; }
+        } 
+        
+        return sample;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "SubSample", "getSample-TreeMap");
+               exit(1);
+       }
+}      
 //**********************************************************************************************************************
 vector<string> SubSample::getSample(vector<SharedRAbundVector*>& thislookup, int size) {
        try {
@@ -64,7 +172,7 @@ vector<string> SubSample::getSample(vector<SharedRAbundVector*>& thislookup, int
                
        }
        catch(exception& e) {
-               m->errorOut(e, "SubSample", "getSample");
+               m->errorOut(e, "SubSample", "getSample-shared");
                exit(1);
        }
 }      
index 09c7dcdf0901f8a229ee3c89633ca1186d03a7ef..aaf52447b026127b27141bb8794a77a80f60c7f4 100644 (file)
@@ -11,6 +11,8 @@
 
 #include "mothurout.h"
 #include "sharedrabundvector.h"
+#include "treemap.h"
+#include "tree.h"
 
 //subsampling overwrites the sharedRabunds.  If you need to reuse the original use the getSamplePreserve function.
 
@@ -22,12 +24,17 @@ class SubSample {
         ~SubSample() {}
     
         vector<string> getSample(vector<SharedRAbundVector*>&, int); //returns the bin labels for the subsample, mothurOuts binlabels are preserved so you can run this multiple times. Overwrites original vector passed in, if you need to preserve it deep copy first.
-    
+        
+        Tree* getSample(Tree*, TreeMap*, map<string, string>, int); //creates new subsampled tree, destroys treemap so copy if needed.
     
     private:
     
         MothurOut* m;
         int eliminateZeroOTUS(vector<SharedRAbundVector*>&);
+    
+        vector<string> getSample(TreeMap*, int); //returns map contains names of seqs in subsample -> group. 
+        map<string, string> deconvolute(map<string, string> wholeSet, vector<string>& subsampleWanted); //returns new nameMap containing only subsampled names, and removes redundants from subsampled wanted because it makes the new nameMap.
+
 
 };
 
index d9d71aea2dd1859d6e198302325fb51d2fdf9367..432811ecaabf1e50161061e291985ca080ed7773 100644 (file)
--- a/tree.cpp
+++ b/tree.cpp
@@ -89,11 +89,123 @@ Tree::Tree(TreeMap* t) : tmap(t) {
                exit(1);
        }
 }
-
+/*****************************************************************/
+Tree::Tree(TreeMap* t, vector< vector<double> >& sims) : tmap(t) {
+       try {
+               m = MothurOut::getInstance();
+               
+               if (m->runParse == true) {  parseTreeFile();  m->runParse = false;  }
+        //for(int i = 0; i <   globaldata->Treenames.size(); i++) { cout << i << '\t' << globaldata->Treenames[i] << endl;  }  
+               numLeaves = m->Treenames.size();
+               numNodes = 2*numLeaves - 1;
+               
+               tree.resize(numNodes);
+        
+               //initialize groupNodeInfo
+               for (int i = 0; i < (tmap->getNamesOfGroups()).size(); i++) {
+                       groupNodeInfo[(tmap->getNamesOfGroups())[i]].resize(0);
+               }
+               
+               //initialize tree with correct number of nodes, name and group info.
+               for (int i = 0; i < numNodes; i++) {
+                       //initialize leaf nodes
+                       if (i <= (numLeaves-1)) {
+                               tree[i].setName(m->Treenames[i]);
+                               
+                               //save group info
+                               string group = tmap->getGroup(m->Treenames[i]);
+                               
+                               vector<string> tempGroups; tempGroups.push_back(group);
+                               tree[i].setGroup(tempGroups);
+                               groupNodeInfo[group].push_back(i); 
+                               
+                               //set pcount and pGroup for groupname to 1.
+                               tree[i].pcount[group] = 1;
+                               tree[i].pGroups[group] = 1;
+                               
+                               //Treemap knows name, group and index to speed up search
+                               tmap->setIndex(m->Treenames[i], i);
+                
+                //intialize non leaf nodes
+                       }else if (i > (numLeaves-1)) {
+                               tree[i].setName("");
+                               vector<string> tempGroups;
+                               tree[i].setGroup(tempGroups);
+                       }
+               }
+        
+        //build tree from matrix
+        //initialize indexes
+        map<int, int> indexes;  //maps row in simMatrix to vector index in the tree
+        int numGroups = (tmap->getNamesOfGroups()).size();
+        for (int g = 0; g < numGroups; g++) {  indexes[g] = g; }
+               
+               //do merges and create tree structure by setting parents and children
+               //there are numGroups - 1 merges to do
+               for (int i = 0; i < (numGroups - 1); i++) {
+                       float largest = -1000.0;
+                       
+                       if (m->control_pressed) { break; }
+                       
+                       int row, column;
+                       //find largest value in sims matrix by searching lower triangle
+                       for (int j = 1; j < sims.size(); j++) {
+                               for (int k = 0; k < j; k++) {
+                                       if (sims[j][k] > largest) {  largest = sims[j][k]; row = j; column = k;  }
+                               }
+                       }
+            
+                       //set non-leaf node info and update leaves to know their parents
+                       //non-leaf
+                       tree[numGroups + i].setChildren(indexes[row], indexes[column]);
+                       
+                       //parents
+                       tree[indexes[row]].setParent(numGroups + i);
+                       tree[indexes[column]].setParent(numGroups + i);
+                       
+                       //blength = distance / 2;
+                       float blength = ((1.0 - largest) / 2);
+                       
+                       //branchlengths
+                       tree[indexes[row]].setBranchLength(blength - tree[indexes[row]].getLengthToLeaves());
+                       tree[indexes[column]].setBranchLength(blength - tree[indexes[column]].getLengthToLeaves());
+                       
+                       //set your length to leaves to your childs length plus branchlength
+                       tree[numGroups + i].setLengthToLeaves(tree[indexes[row]].getLengthToLeaves() + tree[indexes[row]].getBranchLength());
+                       
+                       
+                       //update index 
+                       indexes[row] = numGroups+i;
+                       indexes[column] = numGroups+i;
+                       
+                       //remove highest value that caused the merge.
+                       sims[row][column] = -1000.0;
+                       sims[column][row] = -1000.0;
+                       
+                       //merge values in simsMatrix
+                       for (int n = 0; n < sims.size(); n++)   {
+                               //row becomes merge of 2 groups
+                               sims[row][n] = (sims[row][n] + sims[column][n]) / 2;
+                               sims[n][row] = sims[row][n];
+                               //delete column
+                               sims[column][n] = -1000.0;
+                               sims[n][column] = -1000.0;
+                       }
+               }
+               
+               //adjust tree to make sure root to tip length is .5
+               int root = findRoot();
+               tree[root].setBranchLength((0.5 - tree[root].getLengthToLeaves()));
+       }
+       catch(exception& e) {
+               m->errorOut(e, "Tree", "Tree");
+               exit(1);
+       }
+}
 /*****************************************************************/
 Tree::~Tree() {}
 /*****************************************************************/
-void Tree::addNamesToCounts() {
+void Tree::addNamesToCounts(map<string, string> nameMap) {
        try {
                //ex. seq1      seq2,seq3,se4
                //              seq1 = pasture
@@ -116,12 +228,12 @@ void Tree::addNamesToCounts() {
 
                        string name = tree[i].getName();
                
-                       map<string, string>::iterator itNames = m->names.find(name);
+                       map<string, string>::iterator itNames = nameMap.find(name);
                
-                       if (itNames == m->names.end()) { m->mothurOut(name + " is not in your name file, please correct."); m->mothurOutEndLine(); exit(1);  }
+                       if (itNames == nameMap.end()) { m->mothurOut(name + " is not in your name file, please correct."); m->mothurOutEndLine(); exit(1);  }
                        else {
                                vector<string> dupNames;
-                               m->splitAtComma(m->names[name], dupNames);
+                               m->splitAtComma(nameMap[name], dupNames);
                                
                                map<string, int>::iterator itCounts;
                                int maxPars = 1;
@@ -222,7 +334,7 @@ int Tree::assembleTree() {
                //float A = clock();
 
                //if user has given a names file we want to include that info in the pgroups and pcount info.
-               if(m->names.size() != 0) {  addNamesToCounts();  }
+               if(m->names.size() != 0) {  addNamesToCounts(m->names);  }
                
                //build the pGroups in non leaf nodes to be used in the parsimony calcs.
                for (int i = numLeaves; i < numNodes; i++) {
@@ -261,9 +373,15 @@ int Tree::assembleTree(string n) {
        }
 }
 /*****************************************************************/
-void Tree::getSubTree(Tree* copy, vector<string> Groups) {
+//assumes leaf node names are in groups and no names file - used by indicator command
+void Tree::getSubTree(Tree* Ctree, vector<string> Groups) {
        try {
-                       
+        
+        //copy Tree since we are going to destroy it
+        Tree* copy = new Tree(tmap);
+        copy->getCopy(Ctree);
+        copy->assembleTree("nonames");
+        
                //we want to select some of the leaf nodes to create the output tree
                //go through the input Tree starting at parents of leaves
                for (int i = 0; i < numNodes; i++) {
@@ -408,12 +526,40 @@ void Tree::getSubTree(Tree* copy, vector<string> Groups) {
                        //you found the root
                        if (copy->tree[i].getParent() == -1) { root = i; break; }
                }
-               
+        
                int nextSpot = numLeaves;
                populateNewTree(copy->tree, root, nextSpot);
+        
+        delete copy;
        }
        catch(exception& e) {
-               m->errorOut(e, "Tree", "getCopy");
+               m->errorOut(e, "Tree", "getSubTree");
+               exit(1);
+       }
+}
+/*****************************************************************/
+//assumes nameMap contains unique names as key or is empty. 
+//assumes numLeaves defined in tree constructor equals size of seqsToInclude and seqsToInclude only contains unique seqs.
+int Tree::getSubTree(Tree* copy, vector<string> seqsToInclude, map<string, string> nameMap) {
+       try {
+        
+        if (numLeaves != seqsToInclude.size()) { m->mothurOut("[ERROR]: numLeaves does not equal numUniques, cannot create subtree.\n"); m->control_pressed = true; return 0; }
+        
+        getSubTree(copy, seqsToInclude);
+        if (nameMap.size() != 0) {  addNamesToCounts(nameMap);  }
+        
+        //build the pGroups in non leaf nodes to be used in the parsimony calcs.
+               for (int i = numLeaves; i < numNodes; i++) {
+                       if (m->control_pressed) { return 1; }
+            
+                       tree[i].pGroups = (mergeGroups(i));
+                       tree[i].pcount = (mergeGcounts(i));
+               }
+        
+        return 0;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "Tree", "getSubTree");
                exit(1);
        }
 }
@@ -627,7 +773,6 @@ map<string,int> Tree::mergeGcounts(int position) {
        }
 }
 /**************************************************************************************************/
-
 void Tree::randomLabels(vector<string> g) {
        try {
        
@@ -676,37 +821,7 @@ void Tree::randomLabels(vector<string> g) {
                exit(1);
        }
 }
-/**************************************************************************************************
-
-void Tree::randomLabels(string groupA, string groupB) {
-       try {
-               int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
-               int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
-
-               vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
-               for(int i=numSeqsA;i<randomGroups.size();i++){
-                       randomGroups[i] = groupB;
-               }
-               random_shuffle(randomGroups.begin(), randomGroups.end());
-                               
-               int randomCounter = 0;                          
-               for(int i=0;i<numLeaves;i++){
-                       if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
-                               tree[i].setGroup(randomGroups[randomCounter]);
-                               tree[i].pcount.clear();
-                               tree[i].pcount[randomGroups[randomCounter]] = 1;
-                               tree[i].pGroups.clear();
-                               tree[i].pGroups[randomGroups[randomCounter]] = 1;
-                               randomCounter++;
-                       }
-               }
-       }               
-       catch(exception& e) {
-               m->errorOut(e, "Tree", "randomLabels");
-               exit(1);
-       }
-}
-**************************************************************************************************/
+/**************************************************************************************************/
 void Tree::randomBlengths()  {
        try {
                for(int i=numNodes-1;i>=0;i--){
diff --git a/tree.h b/tree.h
index 2d9d4f815f90bd889c10b886853ca415840060f5..0b61c6e5db4e2eb09638aa44d6b84ccbb07ce1c8 100644 (file)
--- a/tree.h
+++ b/tree.h
@@ -19,13 +19,17 @@ public:
        Tree(string);  //do not use tree generated by this constructor its just to extract the treenames, its a chicken before the egg thing that needs to be revisited.
        Tree(int, TreeMap*); 
        Tree(TreeMap*);         //to generate a tree from a file
+    Tree(TreeMap*, vector< vector<double> >&); //create tree from sim matrix
        ~Tree();
        
        void getCopy(Tree*);  //makes tree a copy of the one passed in.
        void getSubTree(Tree*, vector<string>);  //makes tree a that contains only the names passed in.
+    int getSubTree(Tree* originalToCopy, vector<string> seqToInclude, map<string, string> nameMap);  //used with (int, TreeMap) constructor. SeqsToInclude contains subsample wanted - assumes these are unique seqs and size of vector=numLeaves passed into constructor. nameMap is unique -> redundantList can be empty if no namesfile was provided. 
+    
        void assembleRandomTree();
        void assembleRandomUnifracTree(vector<string>);
        void assembleRandomUnifracTree(string, string);
+    
        void createNewickFile(string);
        int getIndex(string);
        void setIndex(string, int);
@@ -54,7 +58,7 @@ private:
        map<string, int> mergeGroups(int);  //returns a map with a groupname and the number of times that group was seen in the children
        map<string,int> mergeGcounts(int);
        
-       void addNamesToCounts();
+       void addNamesToCounts(map<string, string>);
        void randomTopology();
        void randomBlengths();
        void randomLabels(vector<string>);
index 0150a7a4cd49eb490ba7a208ce4f74fb691b3e92..4a77211d2bd5e8d90585a9f19e6383db6e5f16fc 100644 (file)
@@ -482,9 +482,9 @@ int TreeGroupCommand::execute(){
 Tree* TreeGroupCommand::createTree(vector< vector<double> >& simMatrix){
        try {
                //create tree
-               t = new Tree(tmap);
+               t = new Tree(tmap, simMatrix);
         
-        //initialize index
+       /* //initialize index
         map<int, int> index;  //maps row in simMatrix to vector index in the tree
         for (int g = 0; g < numGroups; g++) {  index[g] = g;   }
                
@@ -544,7 +544,7 @@ Tree* TreeGroupCommand::createTree(vector< vector<double> >& simMatrix){
                //adjust tree to make sure root to tip length is .5
                int root = t->findRoot();
                t->tree[root].setBranchLength((0.5 - t->tree[root].getLengthToLeaves()));
-               
+               */
                //assemble tree
                t->assembleTree();
                
@@ -1004,11 +1004,13 @@ int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
                 if (m->control_pressed) { for (int k = 0; k < trees.size(); k++) { delete trees[k]; } }
                 
                 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, tmap);
                 
                 //create a new filename
                 string conFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".cons.tre";                              
-                outputNames.push_back(conFile); outputTypes["tree"].push_back(outputFile); 
+                outputNames.push_back(conFile); outputTypes["tree"].push_back(conFile); 
                 ofstream outTree;
                 m->openOutputFile(conFile, outTree);
                 
index 1fc5c01b796a67bd948a70147fa4dead9320ea08..450d8fba67b8beafe7c824b6a18b479705b3875c 100644 (file)
@@ -229,6 +229,60 @@ void TreeMap::makeSim(ListVector* list) {
                exit(1);
        }
 }
+/************************************************************/
+int TreeMap::getCopy(TreeMap* copy){
+       try {
+         
+        namesOfGroups = copy->getNamesOfGroups();
+               numGroups = copy->getNumGroups();
+        namesOfSeqs = copy->namesOfSeqs;
+        seqsPerGroup = copy->seqsPerGroup;
+        treemap = copy->treemap;
+        
+        return 0;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeMap", "getCopy");
+               exit(1);
+       }
+}
+/************************************************************/
+vector<string> TreeMap::getNamesSeqs(){
+       try {
+        
+               vector<string> names;
+               
+        for(it = treemap.begin(); it != treemap.end(); it++){
+            names.push_back(it->first);
+               }
+               
+               return names;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeMap", "getNamesSeqs");
+               exit(1);
+       }
+}
+/************************************************************/
+vector<string> TreeMap::getNamesSeqs(vector<string> picked){
+       try {
+               
+               vector<string> names;
+               
+               for(it = treemap.begin(); it != treemap.end(); it++){
+                       //if you are belong to one the the groups in the picked vector add you
+                       if (m->inUsersGroups(it->second.groupname, picked)) {
+                               names.push_back(it->first);
+                       }
+               }
+               
+               return names;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeMap", "getNamesSeqs");
+               exit(1);
+       }
+}
 
 /************************************************************/
 
index 7ed8d04f0886367f5a17a5baeec4848e75687358..fc9c3690fef8900ad3484147ae3fa0487a514b73 100644 (file)
--- a/treemap.h
+++ b/treemap.h
@@ -42,13 +42,19 @@ public:
                sort(namesOfGroups.begin(), namesOfGroups.end());
                return namesOfGroups;
        }
-       vector<string> namesOfSeqs;
-    map<string,int> seqsPerGroup;      //groupname, number of seqs in that group.
-       map<string, GroupIndex> treemap; //sequence name and <groupname, vector index>
-       void print(ostream&);
+    
+    void print(ostream&);
        void makeSim(vector<string>);  //takes groupmap info and fills treemap for use by tree.shared command.
        void makeSim(ListVector*);  //takes listvector info and fills treemap for use by tree.shared command.   
-       
+    vector<string> getNamesSeqs();
+       vector<string> getNamesSeqs(vector<string>); //get names of seqs belonging to a group or set of groups
+    int getCopy(TreeMap*);
+    
+    vector<string> namesOfSeqs;
+    map<string,int> seqsPerGroup;      //groupname, number of seqs in that group.
+       map<string, GroupIndex> treemap; //sequence name and <groupname, vector index>
+
+    
 private:
        vector<string> namesOfGroups;
        ifstream fileHandle;
index f00743c546d22879f930bf25c741cdbcdd3e7fe5..b480702695a2ff6f24c7bcf6657e2f11fb3be86b 100644 (file)
@@ -437,6 +437,17 @@ int TrimSeqsCommand::execute(){
                                        
                                        Sequence currSeq(in); m->gobble(in);
                                        out << currSeq.getName() << '\t' << it->second << endl;
+                    
+                    if (nameFile != "") {
+                        map<string, string>::iterator itName = nameMap.find(currSeq.getName());
+                        if (itName != nameMap.end()) { 
+                            vector<string> thisSeqsNames; 
+                            m->splitAtChar(itName->second, thisSeqsNames, ',');
+                            for (int k = 1; k < thisSeqsNames.size(); k++) { //start at 1 to skip self
+                                out << thisSeqsNames[k] << '\t' << it->second << endl;
+                            }
+                        }else { m->mothurOut("[ERROR]: " + currSeq.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); }                                                  
+                    }
                                }
                                in.close();
                                out.close();
index b3a54c929ace3221fe3ba736a3b5fad7f87818f9..e596db230ee24d80597cfa903a49ed9ee6a7f25b 100644 (file)
@@ -8,6 +8,8 @@
  */
 
 #include "unifracweightedcommand.h"
+#include "consensus.h"
+#include "subsample.h"
 
 //**********************************************************************************************************************
 vector<string> UnifracWeightedCommand::setParameters(){        
@@ -18,7 +20,9 @@ vector<string> UnifracWeightedCommand::setParameters(){
                CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
                CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
                CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
-               CommandParameter prandom("random", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(prandom);
+        CommandParameter psubsample("subsample", "String", "", "", "", "", "",false,false); parameters.push_back(psubsample);
+        CommandParameter pconsensus("consensus", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pconsensus);
+        CommandParameter prandom("random", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(prandom);
                CommandParameter pdistance("distance", "Multiple", "column-lt-square", "column", "", "", "",false,false); parameters.push_back(pdistance);
                CommandParameter proot("root", "Boolean", "F", "", "", "", "",false,false); parameters.push_back(proot);
                CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
@@ -37,14 +41,16 @@ vector<string> UnifracWeightedCommand::setParameters(){
 string UnifracWeightedCommand::getHelpString(){        
        try {
                string helpString = "";
-               helpString += "The unifrac.weighted command parameters are tree, group, name, groups, iters, distance, processors, root and random.  tree parameter is required unless you have valid current tree file.\n";
+               helpString += "The unifrac.weighted command parameters are tree, group, name, groups, iters, distance, processors, root, subsample, consensus and random.  tree parameter is required unless you have valid current tree file.\n";
                helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n";
                helpString += "The group names are separated by dashes.  The iters parameter allows you to specify how many random trees you would like compared to your tree.\n";
                helpString += "The distance parameter allows you to create a distance file from the results. The default is false.\n";
                helpString += "The random parameter allows you to shut off the comparison to random trees. The default is false, meaning don't compare your trees with randomly generated trees.\n";
                helpString += "The root parameter allows you to include the entire root in your calculations. The default is false, meaning stop at the root for this comparision instead of the root of the entire tree.\n";
                helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
-               helpString += "The unifrac.weighted command should be in the following format: unifrac.weighted(groups=yourGroups, iters=yourIters).\n";
+        helpString += "The subsample parameter allows you to enter the size pergroup of the sample or you can set subsample=T and mothur will use the size of your smallest group. The subsample parameter may only be used with a group file.\n";
+        helpString += "The consensus parameter allows you to indicate you would like trees built from distance matrices created with the results, as well as a consensus tree built from these trees. Default=F.\n";
+        helpString += "The unifrac.weighted command should be in the following format: unifrac.weighted(groups=yourGroups, iters=yourIters).\n";
                helpString += "Example unifrac.weighted(groups=A-B-C, iters=500).\n";
                helpString += "The default value for groups is all the groups in your groupfile, and iters is 1000.\n";
                helpString += "The unifrac.weighted command output two files: .weighted and .wsummary their descriptions are in the manual.\n";
@@ -66,6 +72,7 @@ UnifracWeightedCommand::UnifracWeightedCommand(){
                outputTypes["wsummary"] = tempOutNames;
                outputTypes["phylip"] = tempOutNames;
                outputTypes["column"] = tempOutNames;
+        outputTypes["tree"] = tempOutNames;
        }
        catch(exception& e) {
                m->errorOut(e, "UnifracWeightedCommand", "UnifracWeightedCommand");
@@ -102,6 +109,7 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) {
                        outputTypes["wsummary"] = tempOutNames;
                        outputTypes["phylip"] = tempOutNames;
                        outputTypes["column"] = tempOutNames;
+            outputTypes["tree"] = tempOutNames;
                        
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
@@ -159,7 +167,7 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) {
                        else if (namefile == "not found") { namefile = ""; }
                        else { m->setNameFile(namefile); }
                        
-                       outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
+                       outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(treefile);       }
                        
                                                                                                                                        
                        //check for optional parameter and set defaults
@@ -190,9 +198,25 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) {
                        temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
                        m->setProcessors(temp);
                        m->mothurConvert(temp, processors);
+            
+            temp = validParameter.validFile(parameters, "subsample", false);           if (temp == "not found") { temp = "F"; }
+                       if (m->isNumeric1(temp)) { m->mothurConvert(temp, subsampleSize); subsample = true; }
+            else {  
+                if (m->isTrue(temp)) { subsample = true; subsampleSize = -1; }  //we will set it to smallest group later 
+                else { subsample = false; }
+            }
                        
-                       if (!random) {  iters = 0;  } //turn off random calcs
-                       
+            if (!subsample) { subsampleIters = 0;   }
+            else { subsampleIters = iters;          }
+            
+            temp = validParameter.validFile(parameters, "consensus", false);                                   if (temp == "not found") { temp = "F"; }
+                       consensus = m->isTrue(temp);
+            
+                       if (subsample && random) {  m->mothurOut("[ERROR]: random must be false, if subsample=t.\n"); abort=true;  } 
+                       if (subsample && (groupfile == "")) {  m->mothurOut("[ERROR]: if subsample=t, a group file must be provided.\n"); abort=true;  } 
+            if (subsample && (!phylip)) { phylip=true; outputForm = "lt"; }
+            if (consensus && (!subsample)) { m->mothurOut("[ERROR]: you cannot use consensus without subsample.\n"); abort=true; }
+            
                        if (namefile == "") {
                                vector<string> files; files.push_back(treefile);
                                parser.getNameFile(files);
@@ -214,7 +238,389 @@ int UnifracWeightedCommand::execute() {
                
                m->setTreeFile(treefile);
                
-               if (groupfile != "") {
+        readTrees();  if (m->control_pressed) {  delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } return 0; }
+                               
+               sumFile = outputDir + m->getSimpleName(treefile) + ".wsummary";
+               m->openOutputFile(sumFile, outSum);
+               outputNames.push_back(sumFile);  outputTypes["wsummary"].push_back(sumFile);
+               
+        SharedUtil util;
+               string s; //to make work with setgroups
+               Groups = m->getGroups();
+               vector<string> nameGroups = tmap->getNamesOfGroups();
+               util.setGroups(Groups, nameGroups, s, numGroups, "weighted");   //sets the groups the user wants to analyze
+               m->setGroups(Groups);
+               
+        if (m->control_pressed) {  delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } return 0; }
+        
+               Weighted weighted(tmap, includeRoot);
+                       
+               int start = time(NULL);
+            
+        //set or check size
+        if (subsample) {
+            //user has not set size, set size = smallest samples size
+            if (subsampleSize == -1) { 
+                vector<string> temp; temp.push_back(Groups[0]);
+                subsampleSize = (tmap->getNamesSeqs(temp)).size(); //num in first group
+                for (int i = 1; i < Groups.size(); i++) {
+                    temp.clear(); temp.push_back(Groups[i]);
+                    int thisSize = (tmap->getNamesSeqs(temp)).size();
+                    if (thisSize < subsampleSize) {    subsampleSize = thisSize;       }
+                }
+                m->mothurOut("\nSetting subsample size to " + toString(subsampleSize) + ".\n\n");
+            }else { //eliminate any too small groups
+                vector<string> newGroups = Groups;
+                Groups.clear();
+                for (int i = 0; i < newGroups.size(); i++) {
+                    vector<string> thisGroup; thisGroup.push_back(newGroups[i]);
+                    vector<string> thisGroupsSeqs = tmap->getNamesSeqs(thisGroup);
+                    int thisSize = thisGroupsSeqs.size();
+                    
+                    if (thisSize >= subsampleSize) {    Groups.push_back(newGroups[i]);        }
+                    else {  m->mothurOut("You have selected a size that is larger than "+newGroups[i]+" number of sequences, removing "+newGroups[i]+".\n"); }
+                } 
+                m->setGroups(Groups);
+            }
+        }
+        
+        //here in case some groups are removed by subsample
+        util.getCombos(groupComb, Groups, numComp);
+        
+        if (numComp < processors) { processors = numComp; }
+        
+        if (consensus && (numComp < 2)) { m->mothurOut("consensus can only be used with numComparisions greater than 1, setting consensus=f.\n"); consensus=false; }
+        
+        //get weighted scores for users trees
+        for (int i = 0; i < T.size(); i++) {
+            
+            if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } return 0; }
+            
+            counter = 0;
+            rScores.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
+            uScores.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
+            
+            vector<double> userData; userData.resize(numComp,0);  //weighted score info for user tree. data[0] = weightedscore AB, data[1] = weightedscore AC...
+            vector<double> randomData; randomData.resize(numComp,0); //weighted score info for random trees. data[0] = weightedscore AB, data[1] = weightedscore AC...
+            
+            if (random) {  
+                output = new ColumnFile(outputDir + m->getSimpleName(treefile)  + toString(i+1) + ".weighted", itersString);  
+                outputNames.push_back(outputDir + m->getSimpleName(treefile)  + toString(i+1) + ".weighted");
+                outputTypes["weighted"].push_back(outputDir + m->getSimpleName(treefile)  + toString(i+1) + ".weighted");
+            } 
+            
+            userData = weighted.getValues(T[i], processors, outputDir); //userData[0] = weightedscore
+            if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {   m->mothurRemove(outputNames[i]);  } return 0; }
+            
+            //save users score
+            for (int s=0; s<numComp; s++) {
+                //add users score to vector of user scores
+                uScores[s].push_back(userData[s]);
+                //save users tree score for summary file
+                utreeScores.push_back(userData[s]);
+            }
+            
+            if (random) {  runRandomCalcs(T[i], userData); }
+            
+            //clear data
+            rScores.clear();
+            uScores.clear();
+            validScores.clear();
+            
+            //subsample loop
+            vector< vector<double> > calcDistsTotals;  //each iter, each groupCombos dists. this will be used to make .dist files
+            for (int thisIter = 0; thisIter < subsampleIters; thisIter++) { //subsampleIters=0, if subsample=f.
+                
+                if (m->control_pressed) { break; }
+                
+                //copy to preserve old one - would do this in subsample but tree needs it and memory cleanup becomes messy.
+                TreeMap* newTmap = new TreeMap();
+                newTmap->getCopy(tmap);
+                
+                SubSample sample;
+                Tree* subSampleTree = sample.getSample(T[i], newTmap, nameMap, subsampleSize);
+                   
+                //call new weighted function
+                vector<double> iterData; iterData.resize(numComp,0);
+                Weighted thisWeighted(newTmap, includeRoot);
+                iterData = thisWeighted.getValues(subSampleTree, processors, outputDir); //userData[0] = weightedscore
+                
+                //save data to make ave dist, std dist
+                calcDistsTotals.push_back(iterData);
+                
+                delete newTmap;
+                delete subSampleTree;
+            }
+            
+            if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {   m->mothurRemove(outputNames[i]);  } return 0; }
+            
+            if (subsample) {  getAverageSTDMatrices(calcDistsTotals, i); }
+            if (consensus) {  getConsensusTrees(calcDistsTotals, i);  }
+        }
+        
+               
+               if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);  } return 0;  }
+               
+        if (phylip) {  createPhylipFile();             }
+    
+               printWSummaryFile();
+               
+               //clear out users groups
+               m->clearGroups();
+               delete tmap; 
+               for (int i = 0; i < T.size(); i++) { delete T[i]; }
+               
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
+               
+               m->mothurOut("It took " + toString(time(NULL) - start) + " secs to run unifrac.weighted."); m->mothurOutEndLine();
+               
+               //set phylip file as new current phylipfile
+               string current = "";
+               itTypes = outputTypes.find("phylip");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
+               }
+               
+               //set column file as new current columnfile
+               itTypes = outputTypes.find("column");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setColumnFile(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, "UnifracWeightedCommand", "execute");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+int UnifracWeightedCommand::getAverageSTDMatrices(vector< vector<double> >& dists, int treeNum) {
+       try {
+        //we need to find the average distance and standard deviation for each groups distance
+        
+        //finds sum
+        vector<double> averages; averages.resize(numComp, 0); 
+        for (int thisIter = 0; thisIter < subsampleIters; thisIter++) {
+            for (int i = 0; i < dists[thisIter].size(); i++) {  
+                averages[i] += dists[thisIter][i];
+            }
+        }
+        
+        //finds average.
+        for (int i = 0; i < averages.size(); i++) {  averages[i] /= (float) subsampleIters; }
+        
+        //find standard deviation
+        vector<double> stdDev; stdDev.resize(numComp, 0);
+                
+        for (int thisIter = 0; thisIter < iters; thisIter++) { //compute the difference of each dist from the mean, and square the result of each
+            for (int j = 0; j < dists[thisIter].size(); j++) {
+                stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
+            }
+        }
+        for (int i = 0; i < stdDev.size(); i++) {  
+            stdDev[i] /= (float) subsampleIters; 
+            stdDev[i] = sqrt(stdDev[i]);
+        }
+        
+        //make matrix with scores in it
+        vector< vector<double> > avedists;     avedists.resize(m->getNumGroups());
+        for (int i = 0; i < m->getNumGroups(); i++) {
+            avedists[i].resize(m->getNumGroups(), 0.0);
+        }
+        
+        //make matrix with scores in it
+        vector< vector<double> > stddists;     stddists.resize(m->getNumGroups());
+        for (int i = 0; i < m->getNumGroups(); i++) {
+            stddists[i].resize(m->getNumGroups(), 0.0);
+        }
+        
+        //flip it so you can print it
+        int count = 0;
+        for (int r=0; r<m->getNumGroups(); r++) { 
+            for (int l = 0; l < r; l++) {
+                avedists[r][l] = averages[count];
+                avedists[l][r] = averages[count];
+                stddists[r][l] = stdDev[count];
+                stddists[l][r] = stdDev[count];
+                count++;
+            }
+        }
+        
+        string aveFileName = outputDir + m->getSimpleName(treefile)  + toString(treeNum+1) + ".weighted.ave.dist";
+        outputNames.push_back(aveFileName); outputTypes["phylip"].push_back(aveFileName); 
+        
+        ofstream out;
+        m->openOutputFile(aveFileName, out);
+        
+       string stdFileName = outputDir + m->getSimpleName(treefile)  + toString(treeNum+1) + ".weighted.std.dist";
+       outputNames.push_back(stdFileName); outputTypes["phylip"].push_back(stdFileName); 
+        
+        ofstream outStd;
+        m->openOutputFile(stdFileName, outStd);
+        
+        if ((outputForm == "lt") || (outputForm == "square")) {
+            //output numSeqs
+            out << m->getNumGroups() << endl;
+            outStd << m->getNumGroups() << endl;
+        }
+        
+        //output to file
+        for (int r=0; r<m->getNumGroups(); r++) { 
+            //output name
+            string name = (m->getGroups())[r];
+            if (name.length() < 10) { //pad with spaces to make compatible
+                while (name.length() < 10) {  name += " ";  }
+            }
+            
+            if (outputForm == "lt") {
+                out << name << '\t';
+                outStd << name << '\t';
+                
+                //output distances
+                for (int l = 0; l < r; l++) {  out  << avedists[r][l] << '\t';  outStd  << stddists[r][l] << '\t';}
+                out << endl;  outStd << endl;
+            }else if (outputForm == "square") {
+                out << name << '\t';
+                outStd << name << '\t';
+                
+                //output distances
+                for (int l = 0; l < m->getNumGroups(); l++) {  out  << avedists[r][l] << '\t'; outStd  << stddists[r][l] << '\t'; }
+                out << endl; outStd << endl;
+            }else{
+                //output distances
+                for (int l = 0; l < r; l++) {  
+                    string otherName = (m->getGroups())[l];
+                    if (otherName.length() < 10) { //pad with spaces to make compatible
+                        while (otherName.length() < 10) {  otherName += " ";  }
+                    }
+                    
+                    out  << name << '\t' << otherName << avedists[r][l] << endl;  
+                    outStd  << name << '\t' << otherName << stddists[r][l] << endl; 
+                }
+            }
+        }
+        out.close();
+        outStd.close();
+        
+        return 0;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "UnifracWeightedCommand", "getAverageSTDMatrices");
+               exit(1);
+       }
+}
+
+/**************************************************************************************************/
+int UnifracWeightedCommand::getConsensusTrees(vector< vector<double> >& dists, int treeNum) {
+       try {
+        
+        //used in tree constructor 
+        m->runParse = false;
+        
+        //create treemap class from groupmap for tree class to use
+        TreeMap* newTmap = new TreeMap();
+        newTmap->makeSim(m->getGroups());
+        
+        //clear  old tree names if any
+        m->Treenames.clear();
+        
+        //fills globaldatas tree names
+        m->Treenames = m->getGroups();
+        
+        vector<Tree*> newTrees = buildTrees(dists, treeNum, newTmap); //also creates .all.tre file containing the trees created
+        
+        if (m->control_pressed) { delete newTmap; return 0; }
+        
+        Consensus con;
+        Tree* conTree = con.getTree(newTrees, newTmap);
+        
+        //create a new filename
+        string conFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + toString(treeNum+1) + ".weighted.cons.tre";                          
+        outputNames.push_back(conFile); outputTypes["tree"].push_back(conFile); 
+        ofstream outTree;
+        m->openOutputFile(conFile, outTree);
+        
+        if (conTree != NULL) { conTree->print(outTree, "boot"); delete conTree; }
+        outTree.close();
+        delete newTmap;
+        
+        return 0;
+
+    }
+       catch(exception& e) {
+               m->errorOut(e, "UnifracWeightedCommand", "getConsensusTrees");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+
+vector<Tree*> UnifracWeightedCommand::buildTrees(vector< vector<double> >& dists, int treeNum, TreeMap* mytmap) {
+       try {
+        
+        vector<Tree*> trees;
+        
+        //create a new filename
+        string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + toString(treeNum+1) + ".weighted.all.tre";                                
+        outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
+        
+        ofstream outAll;
+        m->openOutputFile(outputFile, outAll);
+        
+
+        for (int i = 0; i < dists.size(); i++) { //dists[0] are the dists for the first subsampled tree.
+            
+            if (m->control_pressed) { break; }
+            
+            //make matrix with scores in it
+            vector< vector<double> > sims;     sims.resize(m->getNumGroups());
+            for (int j = 0; j < m->getNumGroups(); j++) {
+                sims[j].resize(m->getNumGroups(), 0.0);
+            }
+            
+            int count = 0;
+                       for (int r=0; r<m->getNumGroups(); r++) { 
+                               for (int l = 0; l < r; l++) {
+                    double sim = -(dists[i][count]-1.0);
+                                       sims[r][l] = sim;
+                                       sims[l][r] = sim;
+                                       count++;
+                               }
+                       }
+
+            //create tree
+            Tree* tempTree = new Tree(mytmap, sims);
+            tempTree->assembleTree();
+            
+            trees.push_back(tempTree);
+            
+            //print tree
+            tempTree->print(outAll);
+        }
+        
+        outAll.close();
+        
+        if (m->control_pressed) {  for (int i = 0; i < trees.size(); i++) {  delete trees[i]; trees[i] = NULL; } m->mothurRemove(outputFile); }
+        
+        return trees;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "UnifracWeightedCommand", "buildTrees");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+
+int UnifracWeightedCommand::readTrees() {
+       try {
+        
+        if (groupfile != "") {
                        //read in group map info.
                        tmap = new TreeMap(groupfile);
                        tmap->readMap();
@@ -275,183 +681,91 @@ int UnifracWeightedCommand::execute() {
                                }
                        }
                }
-               
-               sumFile = outputDir + m->getSimpleName(treefile) + ".wsummary";
-               m->openOutputFile(sumFile, outSum);
-               outputNames.push_back(sumFile);  outputTypes["wsummary"].push_back(sumFile);
-                       
-               util = new SharedUtil();
-               string s; //to make work with setgroups
-               Groups = m->getGroups();
-               vector<string> nameGroups = tmap->getNamesOfGroups();
-               util->setGroups(Groups, nameGroups, s, numGroups, "weighted");  //sets the groups the user wants to analyze
-               util->getCombos(groupComb, Groups, numComp);
-               m->setGroups(Groups);
-               delete util;
-               
-               weighted = new Weighted(tmap, includeRoot);
-                       
-               int start = time(NULL);
-               
-               //get weighted for users tree
-               userData.resize(numComp,0);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
-               randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
-               
-               if (numComp < processors) { processors = numComp; }
-                               
-               //get weighted scores for users trees
-               for (int i = 0; i < T.size(); i++) {
-                       
-                       if (m->control_pressed) { delete tmap; delete weighted;
-                               for (int i = 0; i < T.size(); i++) { delete T[i]; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {      m->mothurRemove(outputNames[i]);  } return 0; }
-
-                       counter = 0;
-                       rScores.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
-                       uScores.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
-                       
-                       if (random) {  
-                               output = new ColumnFile(outputDir + m->getSimpleName(treefile)  + toString(i+1) + ".weighted", itersString);  
-                               outputNames.push_back(outputDir + m->getSimpleName(treefile)  + toString(i+1) + ".weighted");
-                               outputTypes["weighted"].push_back(outputDir + m->getSimpleName(treefile)  + toString(i+1) + ".weighted");
-                       } 
-
-                       userData = weighted->getValues(T[i], processors, outputDir);  //userData[0] = weightedscore
-                       
-                       if (m->control_pressed) { delete tmap; delete weighted;
-                               for (int i = 0; i < T.size(); i++) { delete T[i]; } if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);  } return 0; }
-                       
-                       //save users score
-                       for (int s=0; s<numComp; s++) {
-                               //add users score to vector of user scores
-                               uScores[s].push_back(userData[s]);
-                               
-                               //save users tree score for summary file
-                               utreeScores.push_back(userData[s]);
-                       }
-                       
-                       if (random) { 
-                       
-                               //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
-                               vector< vector<string> > namesOfGroupCombos;
-                               for (int a=0; a<numGroups; a++) { 
-                                       for (int l = 0; l < a; l++) {   
-                                               vector<string> groups; groups.push_back((m->getGroups())[a]); groups.push_back((m->getGroups())[l]);
-                                               namesOfGroupCombos.push_back(groups);
-                                       }
-                               }
-                               
-                               lines.clear();
-                               
-                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
-                                       if(processors != 1){
-                                               int numPairs = namesOfGroupCombos.size();
-                                               int numPairsPerProcessor = numPairs / processors;
-                                       
-                                               for (int i = 0; i < processors; i++) {
-                                                       int startPos = i * numPairsPerProcessor;
-                                                       if(i == processors - 1){
-                                                               numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
-                                                       }
-                                                       lines.push_back(linePair(startPos, numPairsPerProcessor));
-                                               }
-                                       }
-                               #endif
 
-                               
-                               //get scores for random trees
-                               for (int j = 0; j < iters; j++) {
-                               
-                                       #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
-                                               if(processors == 1){
-                                                       driver(T[i],  namesOfGroupCombos, 0, namesOfGroupCombos.size(),  rScores);
-                                               }else{
-                                                       createProcesses(T[i],  namesOfGroupCombos, rScores);
-                                               }
-                                       #else
-                                               driver(T[i], namesOfGroupCombos, 0, namesOfGroupCombos.size(), rScores);
-                                       #endif
-                                       
-                                       if (m->control_pressed) { delete tmap; delete weighted;
-                                               for (int i = 0; i < T.size(); i++) { delete T[i]; } delete output; outSum.close(); for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);  } return 0; }
-                                       
-                                       //report progress
-//                                     m->mothurOut("Iter: " + toString(j+1)); m->mothurOutEndLine();          
-                               }
-                               lines.clear();
-                       
-                               //find the signifigance of the score for summary file
-                               for (int f = 0; f < numComp; f++) {
-                                       //sort random scores
-                                       sort(rScores[f].begin(), rScores[f].end());
-                                       
-                                       //the index of the score higher than yours is returned 
-                                       //so if you have 1000 random trees the index returned is 100 
-                                       //then there are 900 trees with a score greater then you. 
-                                       //giving you a signifigance of 0.900
-                                       int index = findIndex(userData[f], f);    if (index == -1) { m->mothurOut("error in UnifracWeightedCommand"); m->mothurOutEndLine(); exit(1); } //error code
-                                       
-                                       //the signifigance is the number of trees with the users score or higher 
-                                       WScoreSig.push_back((iters-index)/(float)iters);
-                               }
-                               
-                               //out << "Tree# " << i << endl;
-                               calculateFreqsCumuls();
-                               printWeightedFile();
-                               
-                               delete output;
-                       
-                       }
-                       
-                       //clear data
-                       rScores.clear();
-                       uScores.clear();
-                       validScores.clear();
-               }
-               
-               
-               if (m->control_pressed) { delete tmap; delete weighted;
-                       for (int i = 0; i < T.size(); i++) { delete T[i]; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {      m->mothurRemove(outputNames[i]);  } return 0;  }
-               
-               printWSummaryFile();
-               
-               if (phylip) {   createPhylipFile();             }
-
-               //clear out users groups
-               m->clearGroups();
-               delete tmap; delete weighted;
-               for (int i = 0; i < T.size(); i++) { delete T[i]; }
-               
-               
-               if (m->control_pressed) { 
-                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
-                       return 0; 
-               }
-               
-               m->mothurOut("It took " + toString(time(NULL) - start) + " secs to run unifrac.weighted."); m->mothurOutEndLine();
-               
-               //set phylip file as new current phylipfile
-               string current = "";
-               itTypes = outputTypes.find("phylip");
-               if (itTypes != outputTypes.end()) {
-                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
-               }
-               
-               //set column file as new current columnfile
-               itTypes = outputTypes.find("column");
-               if (itTypes != outputTypes.end()) {
-                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setColumnFile(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;
-               
+        return 0;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "UnifracWeightedCommand", "readTrees");
+               exit(1);
        }
+}
+/**************************************************************************************************/
+
+int UnifracWeightedCommand::runRandomCalcs(Tree* thisTree, vector<double> usersScores) {
+       try {
+        
+        //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
+        vector< vector<string> > namesOfGroupCombos;
+        for (int a=0; a<numGroups; a++) { 
+            for (int l = 0; l < a; l++) {      
+                vector<string> groups; groups.push_back((m->getGroups())[a]); groups.push_back((m->getGroups())[l]);
+                namesOfGroupCombos.push_back(groups);
+            }
+        }
+        
+        lines.clear();
+        
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+        if(processors != 1){
+            int numPairs = namesOfGroupCombos.size();
+            int numPairsPerProcessor = numPairs / processors;
+            
+            for (int i = 0; i < processors; i++) {
+                int startPos = i * numPairsPerProcessor;
+                if(i == processors - 1){
+                    numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
+                }
+                lines.push_back(linePair(startPos, numPairsPerProcessor));
+            }
+        }
+#endif
+        
+        
+        //get scores for random trees
+        for (int j = 0; j < iters; j++) {
+            
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+            if(processors == 1){
+                driver(thisTree,  namesOfGroupCombos, 0, namesOfGroupCombos.size(),  rScores);
+            }else{
+                createProcesses(thisTree,  namesOfGroupCombos, rScores);
+            }
+#else
+            driver(T[i], namesOfGroupCombos, 0, namesOfGroupCombos.size(), rScores);
+#endif
+            
+            if (m->control_pressed) { delete tmap;  for (int i = 0; i < T.size(); i++) { delete T[i]; } delete output; outSum.close(); for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } return 0; }
+            
+            //report progress
+            //                                 m->mothurOut("Iter: " + toString(j+1)); m->mothurOutEndLine();          
+        }
+        lines.clear();
+        
+        //find the signifigance of the score for summary file
+        for (int f = 0; f < numComp; f++) {
+            //sort random scores
+            sort(rScores[f].begin(), rScores[f].end());
+            
+            //the index of the score higher than yours is returned 
+            //so if you have 1000 random trees the index returned is 100 
+            //then there are 900 trees with a score greater then you. 
+            //giving you a signifigance of 0.900
+            int index = findIndex(usersScores[f], f);    if (index == -1) { m->mothurOut("error in UnifracWeightedCommand"); m->mothurOutEndLine(); exit(1); } //error code
+            
+            //the signifigance is the number of trees with the users score or higher 
+            WScoreSig.push_back((iters-index)/(float)iters);
+        }
+        
+        //out << "Tree# " << i << endl;
+        calculateFreqsCumuls();
+        printWeightedFile();
+        
+        delete output;
+        
+        return 0;
+    }
        catch(exception& e) {
-               m->errorOut(e, "UnifracWeightedCommand", "execute");
+               m->errorOut(e, "UnifracWeightedCommand", "runRandomCalcs");
                exit(1);
        }
 }
@@ -524,7 +838,9 @@ int UnifracWeightedCommand::createProcesses(Tree* t, vector< vector<string> > na
 int UnifracWeightedCommand::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector< vector<double> >& scores) { 
  try {
                Tree* randT = new Tree(tmap);
-
+     
+        Weighted weighted(tmap, includeRoot);
+     
                for (int h = start; h < (start+num); h++) {
        
                        if (m->control_pressed) { return 0; }
@@ -542,7 +858,7 @@ int UnifracWeightedCommand::driver(Tree* t, vector< vector<string> > namesOfGrou
                        if (m->control_pressed) { delete randT;  return 0;  }
 
                        //get wscore of random tree
-                       EstOutput randomData = weighted->getValues(randT, groupA, groupB);
+                       EstOutput randomData = weighted.getValues(randT, groupA, groupB);
                
                        if (m->control_pressed) { delete randT;  return 0;  }
                                                                                
@@ -789,7 +1105,7 @@ int UnifracWeightedCommand::readNamesFile() {
                                m->splitAtComma(second, dupNames);
                                
                                for (int i = 0; i < dupNames.size(); i++) {     
-                                       nameMap[dupNames[i]] = dupNames[i]
+                                       nameMap[dupNames[i]] = first
                                        if ((groupfile == "") && (i != 0)) { tmap->addSeq(dupNames[i], "Group1"); } 
                                }
                        }else {  m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); m->names.clear(); namefile = ""; return 1; }                  
index b1db317de0add4b28352315e20dc282110fad789..e1ebd16c6151e373eb86971f53295d2036b4e4c2 100644 (file)
@@ -51,21 +51,18 @@ class UnifracWeightedCommand : public Command {
                vector<double> WScoreSig;  //tree weighted score signifigance when compared to random trees - percentage of random trees with that score or lower.
                vector<string> groupComb; // AB. AC, BC...
                TreeMap* tmap;
-               Weighted* weighted;
                string sumFile, outputDir;
                int iters, numGroups, numComp, counter;
-               EstOutput userData;                     //weighted score info for user tree
-               EstOutput randomData;           //weighted score info for random trees
                vector< vector<double> > rScores;  //vector<weighted scores for random trees.> each group comb has an entry
                vector< vector<double> > uScores;  //vector<weighted scores for user trees.> each group comb has an entry
                vector< map<float, float> > rScoreFreq;  //map <weighted score, number of random trees with that score.> -vector entry for each combination.
                vector< map<float, float> > rCumul;  //map <weighted score, cumulative percentage of number of random trees with that score or higher.> -vector entry for each c                                                                
                map<float, float>  validScores;  //map contains scores from random
                
-               bool abort, phylip, random, includeRoot;
+               bool abort, phylip, random, includeRoot, subsample, consensus;
                string groups, itersString, outputForm, treefile, groupfile, namefile;
                vector<string> Groups, outputNames; //holds groups to be used
-               int processors, numUniquesInName;
+               int processors, numUniquesInName, subsampleSize, subsampleIters;
                ofstream outSum;
                map<string, string> nameMap;
                
@@ -78,6 +75,11 @@ class UnifracWeightedCommand : public Command {
                int createProcesses(Tree*,  vector< vector<string> >,  vector< vector<double> >&);
                int driver(Tree*, vector< vector<string> >, int, int,  vector< vector<double> >&);
                int readNamesFile();
+        int runRandomCalcs(Tree*, vector<double>);
+        int readTrees();
+        vector<Tree*> buildTrees(vector< vector<double> >&, int, TreeMap*);
+        int getConsensusTrees(vector< vector<double> >&, int);
+        int getAverageSTDMatrices(vector< vector<double> >&, int);
                
 };
 
index 7a31da4d55d398f47d022de7b80396bb44f6c6b4..b0a642b1b5024710530e95de5b2a57cc87a7accd 100644 (file)
@@ -87,7 +87,7 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
                                EstOutput Myresults;
                                Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
                        
-                               m->mothurOut("Merging results."); m->mothurOutEndLine();
+                               //m->mothurOut("Merging results."); m->mothurOutEndLine();
                                
                                //pass numSeqs to parent
                                ofstream out;
@@ -142,7 +142,7 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
                        m->mothurRemove(s);
                }
                
-               m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
+               //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
                
                return results;
 #endif