]> git.donarmstrong.com Git - mothur.git/blob - collect.cpp
added ability for user to select which groups to analyze with the collect.shared...
[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
68                 //create and initialize vector of sharedvectors, one for each group
69                 for (int i = 0; i < globaldata->Groups.size(); i++) { 
70                         SharedRAbundVector* temp = new SharedRAbundVector(sharedorder->getNumBins());
71                         temp->setLabel(sharedorder->getLabel());
72                         temp->setGroup(globaldata->Groups[i]);
73                         temp->setGroupIndex(globaldata->gGroupmap->groupIndex[globaldata->Groups[i]]);
74                         lookup.push_back(temp);
75                 }
76
77                 SharedCollectorsCurveData* ccd = new SharedCollectorsCurveData();
78         
79                 //initialize labels for output
80                 //makes  'uniqueAB         uniqueAC  uniqueBC' if your groups are A, B, C
81                 getGroupComb();
82                 groupLabel = "";
83                 for (int s = 0; s < groupComb.size(); s++) {
84                         groupLabel = groupLabel + label + groupComb[s] + "\t";
85                 }
86
87                 for(int i=0;i<displays.size();i++){
88                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
89                         displays[i]->init(groupLabel);                  
90                 }
91                 
92                 //sample all the members
93                 for(int i=0;i<numSeqs;i++){
94                         //get first sample
95                         individual chosen = sharedorder->get(i);
96                         int abundance; 
97                                         
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         
108                         //calculate at 0 and the given increment
109                         if((i == 0) || (i+1) % increment == 0){
110                                 //randomize group order
111                                 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
112                                 //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
113                                 int n = 1;
114                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
115                                         for (int l = n; l < lookup.size(); l++) {
116                                                 ccd->updateSharedData(lookup[k], lookup[l], i+1, globaldata->Groups.size());
117                                         }
118                                         n++;
119                                 }
120                         }
121                         totalNumSeq = i+1;
122                 }
123                 
124                 //calculate last line if you haven't already
125                 if(numSeqs % increment != 0){
126                         //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
127                         int n = 1;
128                         for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
129                                 for (int l = n; l < lookup.size(); l++) {
130                                         ccd->updateSharedData(lookup[k], lookup[l], totalNumSeq, globaldata->Groups.size());
131                                 }
132                                 n++;
133                         }
134                 }
135                 
136                 //resets output files
137                 for(int i=0;i<displays.size();i++){
138                         displays[i]->reset();
139                 }
140         }
141         catch(exception& e) {
142                 cout << "Standard Error: " << e.what() << " has occurred in the Collect class Function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143                 exit(1);
144         }
145         catch(...) {
146                 cout << "An unknown error has occurred in the Collect class function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }
149
150 }
151
152 /**************************************************************************************/
153
154 void Collect::getGroupComb() {
155         string group;
156                 
157         numGroupComb = 0;
158                 
159         int n = 1;
160         for (int i = 0; i < (globaldata->Groups.size() - 1); i++) {
161                 for (int l = n; l < globaldata->Groups.size(); l++) {
162                         group = globaldata->Groups[i] + globaldata->Groups[l];
163                         groupComb.push_back(group);        
164                         numGroupComb++;
165                 }
166                 n++;
167         }
168
169 }
170
171 /**************************************************************************************/