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