]> git.donarmstrong.com Git - mothur.git/blob - collect.cpp
added concensus command and updated calcs
[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 void Collect::getCurve(int increment = 1){
15         try {
16                 RAbundVector* lookup = new RAbundVector(order->getNumBins());
17                 SAbundVector* rank        = new SAbundVector(order->getMaxRank()+1);
18
19                 CollectorsCurveData* ccd = new CollectorsCurveData();
20         
21                 for(int i=0;i<displays.size();i++){
22                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
23                         displays[i]->init(label);                   //sets displays label
24                 }                                                                           
25                 for(int i=0;i<numSeqs;i++){
26
27                         int binNumber = order->get(i);
28                         int abundance = lookup->get(binNumber);
29                 
30                         rank->set(abundance, rank->get(abundance)-1); 
31                 
32                         abundance++;
33                 
34                         lookup->set(binNumber, abundance);
35                         rank->set(abundance, rank->get(abundance)+1); //increment rank(abundance)
36
37                         if((i == 0) || (i+1) % increment == 0){
38                                 ccd->updateRankData(rank);
39                         }
40                 }
41         
42                 if(numSeqs % increment != 0){
43                         ccd->updateRankData(rank);
44                 }
45         
46                 for(int i=0;i<displays.size();i++){
47                         displays[i]->reset();
48                 }
49         }
50         catch(exception& e) {
51                 cout << "Standard Error: " << e.what() << " has occurred in the Collect class Function getCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52                 exit(1);
53         }
54         catch(...) {
55                 cout << "An unknown error has occurred in the Collect class function getCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56                 exit(1);
57         }
58
59 }
60
61 /***********************************************************************/
62 void Collect::getSharedCurve(int increment = 1){
63 try {
64                 globaldata = GlobalData::getInstance();
65                 vector<SharedRAbundVector*> lookup; 
66                                 vector<SharedRAbundVector*> subset;
67
68                 //create and initialize vector of sharedvectors, one for each group
69                 for (int i = 0; i < globaldata->Groups.size(); i++) { 
70                         SharedRAbundVector* temp = new SharedRAbundVector(sharedorder->getNumBins());
71                         temp->setLabel(sharedorder->getLabel());
72                         temp->setGroup(globaldata->Groups[i]);
73                         temp->setGroupIndex(globaldata->gGroupmap->groupIndex[globaldata->Groups[i]]);
74                         lookup.push_back(temp);
75                 }
76
77                 SharedCollectorsCurveData* ccd = new SharedCollectorsCurveData();
78         
79                 //initialize labels for output
80                 //makes  'uniqueAB         uniqueAC  uniqueBC' if your groups are A, B, C
81                 getGroupComb();
82                 groupLabel = ""; 
83                 for (int s = 0; s < groupComb.size(); s++) {
84                         groupLabel = groupLabel + label + groupComb[s] + "\t";
85                 }
86                                 
87                                 //for multi displays
88                                 string groupLabelAll = groupLabel + label + "all\t";
89                                 
90                 for(int i=0;i<displays.size();i++){
91                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
92                                                 if (displays[i]->isCalcMultiple() == true)  {   displays[i]->init(groupLabelAll); }
93                                                 else {  displays[i]->init(groupLabel);  }           
94                 }
95                 
96                 //sample all the members
97                 for(int i=0;i<numSeqs;i++){
98                         //get first sample
99                         individual chosen = sharedorder->get(i);
100                         int abundance; 
101                                         
102                         //set info for sharedvector in chosens group
103                         for (int j = 0; j < lookup.size(); j++) { 
104                                                         if (chosen.group == lookup[j]->getGroup()) {
105                                                                 abundance = lookup[j]->getAbundance(chosen.bin);
106                                                                 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
107                                                                 break;
108                                                         }
109                         }
110                         
111         
112                         //calculate at 0 and the given increment
113                         if((i == 0) || (i+1) % increment == 0){
114                                                                 //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
115                                 int n = 1;
116                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
117                                         for (int l = n; l < lookup.size(); l++) {
118                                                                                                 subset.clear(); //clear out old pair of sharedrabunds
119                                                                                                 //add new pair of sharedrabund vectors
120                                                                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
121                                                 ccd->updateSharedData(subset, i+1, globaldata->Groups.size());
122                                         }
123                                         n++;
124                                 }
125                                                                 //if this is a calculator that can do multiples then do them
126                                                                 ccd->updateSharedData(lookup, i+1, globaldata->Groups.size()); 
127                         }
128                         totalNumSeq = i+1;
129                 }
130                 
131                 //calculate last line if you haven't already
132                 if(numSeqs % increment != 0){
133                         //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
134                         int n = 1;
135                         for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
136                                 for (int l = n; l < lookup.size(); l++) {
137                                                                                 subset.clear(); //clear out old pair of sharedrabunds
138                                                                                 //add new pair of sharedrabund vectors
139                                                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
140                                                                                 ccd->updateSharedData(subset, totalNumSeq, globaldata->Groups.size());
141                                 }
142                                 n++;
143                         }
144                                                 //if this is a calculator that can do multiples then do them
145                                                 ccd->updateSharedData(lookup, totalNumSeq, globaldata->Groups.size()); 
146                 }
147                 
148                 //resets output files
149                 for(int i=0;i<displays.size();i++){
150                         displays[i]->reset();
151                 }
152         }
153         catch(exception& e) {
154                 cout << "Standard Error: " << e.what() << " has occurred in the Collect class Function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
155                 exit(1);
156         }
157         catch(...) {
158                 cout << "An unknown error has occurred in the Collect class function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
159                 exit(1);
160         }
161
162 }
163
164 /**************************************************************************************/
165
166 void Collect::getGroupComb() {
167         string group;
168                 
169         numGroupComb = 0;
170                 
171         int n = 1;
172         for (int i = 0; i < (globaldata->Groups.size() - 1); i++) {
173                 for (int l = n; l < globaldata->Groups.size(); l++) {
174                         group = globaldata->Groups[i] + globaldata->Groups[l];
175                         groupComb.push_back(group);        
176                         numGroupComb++;
177                 }
178                 n++;
179         }
180
181 }
182
183 /**************************************************************************************/