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