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