]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
Initial revision
[mothur.git] / summarysharedcommand.cpp
1 /*
2  *  summarysharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "summarysharedcommand.h"
11 #include "sharedchao1.h"
12 #include "sharedace.h"
13 #include "sharedjabund.h"
14 #include "sharedsorabund.h"
15 #include "sharedjclass.h"
16 #include "sharedsorclass.h"
17 #include "sharedjest.h"
18 #include "sharedsorest.h"
19 #include "sharedthetayc.h"
20 #include "sharedthetan.h"
21
22 //**********************************************************************************************************************
23
24 SummarySharedCommand::SummarySharedCommand(){
25         try {
26                 globaldata = GlobalData::getInstance();
27                 
28                 int i;
29                 for (i=0; i<globaldata->sharedSummaryEstimators.size(); i++) {
30                         if (globaldata->sharedSummaryEstimators[i] == "sharedChao") { 
31                                 sumCalculators.push_back(new SharedChao1());
32                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedAce") { 
33                                 sumCalculators.push_back(new SharedAce());
34                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedJabund") {   
35                                 sumCalculators.push_back(new SharedJAbund());
36                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedSorensonAbund") { 
37                                 sumCalculators.push_back(new SharedSorAbund());
38                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedJclass") { 
39                                 sumCalculators.push_back(new SharedJclass());
40                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedSorClass") { 
41                                 sumCalculators.push_back(new SharedSorClass());
42                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedJest") { 
43                                 sumCalculators.push_back(new SharedJest());
44                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedSorEst") { 
45                                 sumCalculators.push_back(new SharedSorEst());
46                         }else if (globaldata->sharedSummaryEstimators[i] == "SharedThetaYC") { 
47                                 sumCalculators.push_back(new SharedThetaYC());
48                         }else if (globaldata->sharedSummaryEstimators[i] == "SharedThetaN") { 
49                                 sumCalculators.push_back(new SharedThetaN());
50                         }
51                 }
52         }
53         catch(exception& e) {
54                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
55                 exit(1);
56         }
57         catch(...) {
58                 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
59                 exit(1);
60         }       
61 }
62 //**********************************************************************************************************************
63
64 SummarySharedCommand::~SummarySharedCommand(){
65         delete input;
66         delete read;
67 }
68
69 //**********************************************************************************************************************
70
71 int SummarySharedCommand::execute(){
72         try {
73                 outputFileName = ((getRootName(globaldata->inputFileName)) + "sharedSummary");
74                 openOutputFile(outputFileName, outputFileHandle);
75         
76                 read = new ReadPhilFile(globaldata->inputFileName);     
77                 read->read(&*globaldata); 
78                 
79                 outputFileHandle << '\t' << '\t' << '\t' << '\t'; //pads file for labels and groupnames
80                 for(int i=0;i<sumCalculators.size();i++){
81                         outputFileHandle << '\t' << sumCalculators[i]->getName();
82                 }
83                 outputFileHandle << endl;
84                 
85                 list = globaldata->glist;
86                 input = globaldata->ginput;
87                 order = list->getSharedOrderVector();
88                 getGroupComb();
89                 
90                 int count = 1;
91                 while(order != NULL){
92                 
93                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
94         
95                                 cout << order->getLabel() << '\t' << count << endl;
96                                 getSharedVectors();  //fills group vectors from order vector.
97                                 
98                                 //randomize group order
99                                 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
100
101                                 int n = 1; 
102                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
103                                         for (int l = n; l < lookup.size(); l++) {
104                                                 outputFileHandle << order->getLabel() << '\t' << groupComb[n-1] << '\t'; //print out label and group
105                                                 for(int i=0;i<sumCalculators.size();i++){
106                                                         sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
107                                                         outputFileHandle << '\t';
108                                                         sumCalculators[i]->print(outputFileHandle);
109                                                 }
110                                                 outputFileHandle << endl;
111                                         }
112                                         n++;
113                                 }
114                         }
115                 
116                         list = input->getListVector(); //get new list vector to process
117                         if (list != NULL) {
118                                 order = list->getSharedOrderVector(); //gets new order vector with group info.
119                                 count++;
120                         }else {
121                                 break;
122                         }
123                 }
124         
125                 return 0;
126         }
127         catch(exception& e) {
128                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
129                 exit(1);
130         }
131         catch(...) {
132                 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
133                 exit(1);
134         }               
135 }
136
137 //**********************************************************************************************************************
138
139 void SummarySharedCommand::getSharedVectors(){
140 try {
141                 lookup.clear();
142                 //create and initialize vector of sharedvectors, one for each group
143                 for (int i = 0; i < globaldata->gGroupmap->getNumGroups(); i++) { 
144                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
145                         temp->setLabel(order->getLabel());
146                         temp->setGroup(globaldata->gGroupmap->namesOfGroups[i]);
147                         lookup.push_back(temp);
148                 }
149                 
150                 int numSeqs = order->size();
151                 //sample all the members
152                 for(int i=0;i<numSeqs;i++){
153                         //get first sample
154                         individual chosen = order->get(i);
155                         int abundance; 
156                                         
157                         //set info for sharedvector in chosens group
158                         for (int j = 0; j < lookup.size(); j++) { 
159                                 if (chosen.group == lookup[j]->getGroup()) {
160                                          abundance = lookup[j]->getAbundance(chosen.bin);
161                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
162                                          break;
163                                 }
164                         }
165                         
166                 }
167         }
168         catch(exception& e) {
169                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170                 exit(1);
171         }
172         catch(...) {
173                 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174                 exit(1);
175         }
176
177 }
178
179 /**************************************************************************************/
180 void SummarySharedCommand::getGroupComb() {
181         try {
182                 string group;
183                 
184                 int n = 1;
185                 for (int i = 0; i < (globaldata->gGroupmap->getNumGroups() - 1); i++) {
186                         for (int l = n; l < globaldata->gGroupmap->getNumGroups(); l++) {
187                                 group = globaldata->gGroupmap->namesOfGroups[i] + globaldata->gGroupmap->namesOfGroups[l];
188                                 groupComb.push_back(group);     
189                         }
190                         n++;
191                 }
192         }
193         catch(exception& e) {
194                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getGroupComb. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195                 exit(1);
196         }
197         catch(...) {
198                 cout << "An unknown error has occurred in the SummarySharedCommand class function getGroupComb. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }
201
202 }
203