]> git.donarmstrong.com Git - mothur.git/blob - collect.cpp
adding treeclimber and unifrac pieces
[mothur.git] / collect.cpp
1 /*
2  *  collect.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 11/18/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "collect.h"
11
12
13 /***********************************************************************/
14
15 void Collect::getCurve(int increment = 1){
16         try {
17                 RAbundVector* lookup = new RAbundVector(order->getNumBins());
18                 SAbundVector* rank      = new SAbundVector(order->getMaxRank()+1);
19
20                 CollectorsCurveData* ccd = new CollectorsCurveData();
21         
22                 for(int i=0;i<displays.size();i++){
23                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
24                         displays[i]->init(label);                  //sets displays label
25                 }                                                                          
26                 for(int i=0;i<numSeqs;i++){
27
28                         int binNumber = order->get(i);
29                         int abundance = lookup->get(binNumber);
30                 
31                         rank->set(abundance, rank->get(abundance)-1); 
32                 
33                         abundance++;
34                 
35                         lookup->set(binNumber, abundance);
36                         rank->set(abundance, rank->get(abundance)+1); //increment rank(abundance)
37
38                         if((i == 0) || (i+1) % increment == 0){
39                                 ccd->updateRankData(rank);
40                         }
41                 }
42         
43                 if(numSeqs % increment != 0){
44                         ccd->updateRankData(rank);
45                 }
46         
47                 for(int i=0;i<displays.size();i++){
48                         displays[i]->reset();
49                 }
50         }
51         catch(exception& e) {
52                 cout << "Standard Error: " << e.what() << " has occurred in the Collect class Function getCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53                 exit(1);
54         }
55         catch(...) {
56                 cout << "An unknown error has occurred in the Collect class function getCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57                 exit(1);
58         }
59
60 }
61
62 /***********************************************************************/
63 void Collect::getSharedCurve(int increment = 1){
64 try {
65                 globaldata = GlobalData::getInstance();
66                 vector<SharedRAbundVector*> lookup; 
67
68                 //create and initialize vector of sharedvectors, one for each group
69                 for (int i = 0; i < globaldata->gGroupmap->getNumGroups(); i++) { 
70                         SharedRAbundVector* temp = new SharedRAbundVector(sharedorder->getNumBins());
71                         temp->setLabel(sharedorder->getLabel());
72                         temp->setGroup(globaldata->gGroupmap->namesOfGroups[i]);
73                         lookup.push_back(temp);
74                 }
75
76                 SharedCollectorsCurveData* ccd = new SharedCollectorsCurveData();
77         
78                 //initialize labels for output
79                 //makes  'uniqueAB       uniqueAC  uniqueBC' if your groups are A, B, C
80                 getGroupComb();
81                 groupLabel = "";
82                 for (int s = 0; s < groupComb.size(); s++) {
83                         groupLabel = groupLabel + label + groupComb[s] + "\t";
84                 }
85
86                 for(int i=0;i<displays.size();i++){
87                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
88                         displays[i]->init(groupLabel);            
89                 }
90                 
91                 //sample all the members
92                 for(int i=0;i<numSeqs;i++){
93                         //get first sample
94                         individual chosen = sharedorder->get(i);
95                         int abundance; 
96                                         
97                         //set info for sharedvector in chosens group
98                         for (int j = 0; j < lookup.size(); j++) { 
99                                 if (chosen.group == lookup[j]->getGroup()) {
100                                          abundance = lookup[j]->getAbundance(chosen.bin);
101                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
102                                          break;
103                                 }
104                         }
105                         
106                         //calculate at 0 and the given increment
107                         if((i == 0) || (i+1) % increment == 0){
108                                 //randomize group order
109                                 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
110                                 //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
111                                 int n = 1;
112                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
113                                         for (int l = n; l < lookup.size(); l++) {
114                                                 ccd->updateSharedData(lookup[k], lookup[l], i+1, numGroupComb);
115                                         }
116                                         n++;
117                                 }
118                         }
119                         totalNumSeq = i+1;
120                 }
121                 
122                 //calculate last line if you haven't already
123                 if(numSeqs % increment != 0){
124                         //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
125                         int n = 1;
126                         for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
127                                 for (int l = n; l < lookup.size(); l++) {
128                                         ccd->updateSharedData(lookup[k], lookup[l], totalNumSeq, numGroupComb);
129                                 }
130                                 n++;
131                         }
132                 }
133                 
134                 //resets output files
135                 for(int i=0;i<displays.size();i++){
136                         displays[i]->reset();
137                 }
138         }
139         catch(exception& e) {
140                 cout << "Standard Error: " << e.what() << " has occurred in the Collect class Function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141                 exit(1);
142         }
143         catch(...) {
144                 cout << "An unknown error has occurred in the Collect class function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145                 exit(1);
146         }
147
148 }
149
150 /**************************************************************************************/
151
152 void Collect::getGroupComb() {
153                 string group;
154                 
155                 numGroupComb = 0;
156                 
157                 int n = 1;
158                 for (int i = 0; i < (globaldata->gGroupmap->getNumGroups() - 1); i++) {
159                         for (int l = n; l < globaldata->gGroupmap->getNumGroups(); l++) {
160                                 group = globaldata->gGroupmap->namesOfGroups[i] + globaldata->gGroupmap->namesOfGroups[l];
161                                 groupComb.push_back(group);     
162                                 numGroupComb++;
163                         }
164                         n++;
165                 }
166
167 }
168
169 /**************************************************************************************/