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