]> git.donarmstrong.com Git - mothur.git/blob - collect.cpp
consensus.seqs can now find a consensus on a whole fasta file without needing a listfile
[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                                 globaldata = GlobalData::getInstance();
74                 vector<SharedRAbundVector*> lookup; 
75                                 vector<SharedRAbundVector*> subset;
76
77                 //create and initialize vector of sharedvectors, one for each group
78                 for (int i = 0; i < globaldata->Groups.size(); i++) { 
79                         SharedRAbundVector* temp = new SharedRAbundVector(sharedorder->getNumBins());
80                         temp->setLabel(sharedorder->getLabel());
81                         temp->setGroup(globaldata->Groups[i]);
82                         temp->setGroupIndex(globaldata->gGroupmap->groupIndex[globaldata->Groups[i]]);
83                         lookup.push_back(temp);
84                 }
85
86                 SharedCollectorsCurveData* ccd = new SharedCollectorsCurveData();
87         
88                 //initialize labels for output
89                 //makes  'uniqueAB         uniqueAC  uniqueBC' if your groups are A, B, C
90                 getGroupComb();
91                                 
92                 for(int i=0;i<displays.size();i++){
93                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
94                                                 bool hasLciHci = displays[i]->hasLciHci();
95                                                 groupLabel = "";
96                                                 for (int s = 0; s < groupComb.size(); s++) {
97                                                         if (hasLciHci) {  groupLabel = groupLabel + label + groupComb[s] + "\t" + label + groupComb[s] + "lci\t" + label + groupComb[s] + "hci\t"; }
98                                                         else{  groupLabel = groupLabel + label + groupComb[s] + "\t";  }
99                                                 }
100
101                                                 string groupLabelAll = groupLabel + label + "all\t"; 
102                                                 if ((displays[i]->isCalcMultiple() == true) && (displays[i]->getAll() == true)) {   displays[i]->init(groupLabelAll); }
103                                                 else {  displays[i]->init(groupLabel);  }           
104                 }
105                 
106                                 //convert freq percentage to number
107                                 int increment = 1;
108                                 if (percentFreq < 1.0) {  increment = numSeqs * percentFreq;  }
109                                 else { increment = percentFreq;  }
110                                 
111                 //sample all the members
112                 for(int i=0;i<numSeqs;i++){
113                                 
114                                                 if (m->control_pressed) { for (int j = 0; j < lookup.size(); j++) {  delete lookup[j]; } delete ccd;  return 1;  }
115                                                 
116                         //get first sample
117                         individual chosen = sharedorder->get(i);
118                         int abundance; 
119                                         
120                         //set info for sharedvector in chosens group
121                         for (int j = 0; j < lookup.size(); j++) { 
122                                                         if (chosen.group == lookup[j]->getGroup()) {
123                                                                 abundance = lookup[j]->getAbundance(chosen.bin);
124                                                                 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
125                                                                 break;
126                                                         }
127                         }
128         
129                         //calculate at 0 and the given increment
130                         if((i == 0) || (i+1) % increment == 0){
131
132                                                                 //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
133
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, i+1, globaldata->Groups.size());
141                                         }
142                                         n++;
143                                 }
144                                                                 //if this is a calculator that can do multiples then do them
145                                                                 ccd->updateSharedData(lookup, i+1, globaldata->Groups.size()); 
146                         }
147                         totalNumSeq = i+1;
148                 }
149                 
150                 //calculate last label if you haven't already
151                 if(numSeqs % increment != 0){
152                         //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
153                         int n = 1;
154                         for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
155                                 for (int l = n; l < lookup.size(); l++) {
156                                                                                 subset.clear(); //clear out old pair of sharedrabunds
157                                                                                 //add new pair of sharedrabund vectors
158                                                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
159                                                                                 ccd->updateSharedData(subset, totalNumSeq, globaldata->Groups.size());
160                                 }
161                                 n++;
162                         }
163                                                 //if this is a calculator that can do multiples then do them
164                                                 ccd->updateSharedData(lookup, totalNumSeq, globaldata->Groups.size()); 
165                 }
166                 
167                 //resets output files
168                 for(int i=0;i<displays.size();i++){
169                         displays[i]->reset();
170                 }
171                                 
172                                 //memory cleanup
173                                 delete ccd;
174                                 for (int i = 0; i < lookup.size(); i++) {
175                                         delete lookup[i];
176                                 }
177                                 
178                                 return 0;
179
180         }
181         catch(exception& e) {
182                 m->errorOut(e, "Collect", "getSharedCurve");
183                                 exit(1);
184         }
185 }
186
187 /**************************************************************************************/
188
189 void Collect::getGroupComb() {
190         string group;
191                 
192         numGroupComb = 0;
193                 
194         int n = 1;
195         for (int i = 0; i < (globaldata->Groups.size() - 1); i++) {
196                 for (int l = n; l < globaldata->Groups.size(); l++) {
197                         group = globaldata->Groups[i] + globaldata->Groups[l];
198                         groupComb.push_back(group);        
199                         numGroupComb++;
200                 }
201                 n++;
202         }
203
204 }
205
206 /**************************************************************************************/