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