]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
added read.shared, broke up globaldata a bit
[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                 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
29                 openOutputFile(outputFileName, outputFileHandle);
30                 format = globaldata->getFormat();
31                 validCalculator = new ValidCalculators();
32                 
33                 int i;
34                 for (i=0; i<globaldata->Estimators.size(); i++) {
35                         if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) { 
36                                 if (globaldata->Estimators[i] == "sharedsobs") { 
37                                         sumCalculators.push_back(new SharedSobsCS());
38                                 }else if (globaldata->Estimators[i] == "sharedchao") { 
39                                         sumCalculators.push_back(new SharedChao1());
40                                 }else if (globaldata->Estimators[i] == "sharedace") { 
41                                         sumCalculators.push_back(new SharedAce());
42                                 }else if (globaldata->Estimators[i] == "sharedjabund") {        
43                                         sumCalculators.push_back(new SharedJAbund());
44                                 }else if (globaldata->Estimators[i] == "sharedsorensonabund") { 
45                                         sumCalculators.push_back(new SharedSorAbund());
46                                 }else if (globaldata->Estimators[i] == "sharedjclass") { 
47                                         sumCalculators.push_back(new SharedJclass());
48                                 }else if (globaldata->Estimators[i] == "sharedsorclass") { 
49                                         sumCalculators.push_back(new SharedSorClass());
50                                 }else if (globaldata->Estimators[i] == "sharedjest") { 
51                                         sumCalculators.push_back(new SharedJest());
52                                 }else if (globaldata->Estimators[i] == "sharedsorest") { 
53                                         sumCalculators.push_back(new SharedSorEst());
54                                 }else if (globaldata->Estimators[i] == "sharedthetayc") { 
55                                         sumCalculators.push_back(new SharedThetaYC());
56                                 }else if (globaldata->Estimators[i] == "sharedthetan") { 
57                                         sumCalculators.push_back(new SharedThetaN());
58                                 }
59                         }
60                 }
61                 
62                 //reset calc for next command
63                 globaldata->setCalc("");
64
65         }
66         catch(exception& e) {
67                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68                 exit(1);
69         }
70         catch(...) {
71                 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }       
74 }
75 //**********************************************************************************************************************
76
77 SummarySharedCommand::~SummarySharedCommand(){
78         delete input;
79         delete read;
80 }
81
82 //**********************************************************************************************************************
83
84 int SummarySharedCommand::execute(){
85         try {
86                 int count = 1;  
87                 
88                 //if the users entered no valid calculators don't execute command
89                 if (sumCalculators.size() == 0) { return 0; }
90
91                 if (format == "sharedfile") {
92                         read = new ReadPhilFile(globaldata->inputFileName);     
93                         read->read(&*globaldata); 
94                         
95                         input = globaldata->ginput;
96                         order = input->getSharedOrderVector();
97                 }else {
98                         //you are using a list and a groupfile
99                         read = new ReadPhilFile(globaldata->inputFileName);     
100                         read->read(&*globaldata); 
101                 
102                         input = globaldata->ginput;
103                         SharedList = globaldata->gSharedList;
104                         order = SharedList->getSharedOrderVector();
105                 }
106                 
107                 //output estimator names as column headers
108                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
109                 for(int i=0;i<sumCalculators.size();i++){
110                         outputFileHandle << '\t' << sumCalculators[i]->getName();
111                 }
112                 outputFileHandle << endl;
113
114                 while(order != NULL){
115                 
116                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
117         
118                                 cout << order->getLabel() << '\t' << count << endl;
119                                 getSharedVectors();  //fills group vectors from order vector.
120                                 
121                                 //randomize group order
122                                 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
123
124                                 int n = 1; 
125                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
126                                         for (int l = n; l < lookup.size(); l++) {
127                                                 outputFileHandle << order->getLabel() << '\t' << (lookup[k]->getGroup() + lookup[l]->getGroup()) << '\t' << '\t'; //print out label and group
128                                                 for(int i=0;i<sumCalculators.size();i++){
129                                                         sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
130                                                         outputFileHandle << '\t';
131                                                         sumCalculators[i]->print(outputFileHandle);
132                                                 }
133                                                 outputFileHandle << endl;
134                                         }
135                                         n++;
136                                 }
137                         }
138                 
139                         //get next line to process
140                         if (format == "sharedfile") {
141                                 order = input->getSharedOrderVector();
142                         }else {
143                                 //you are using a list and a groupfile
144                                 SharedList = input->getSharedListVector(); //get new list vector to process
145                                 if (SharedList != NULL) {
146                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
147                                 }else {
148                                         break;
149                                 }
150                         }
151                         count++;
152                 }
153         
154                 return 0;
155         }
156         catch(exception& e) {
157                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
158                 exit(1);
159         }
160         catch(...) {
161                 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
162                 exit(1);
163         }               
164 }
165
166 //**********************************************************************************************************************
167
168 void SummarySharedCommand::getSharedVectors(){
169 try {
170                 lookup.clear();
171                 //create and initialize vector of sharedvectors, one for each group
172                 for (int i = 0; i < globaldata->gGroupmap->getNumGroups(); i++) { 
173                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
174                         temp->setLabel(order->getLabel());
175                         temp->setGroup(globaldata->gGroupmap->namesOfGroups[i]);
176                         lookup.push_back(temp);
177                 }
178                 
179                 int numSeqs = order->size();
180                 //sample all the members
181                 for(int i=0;i<numSeqs;i++){
182                         //get first sample
183                         individual chosen = order->get(i);
184                         int abundance; 
185                                         
186                         //set info for sharedvector in chosens group
187                         for (int j = 0; j < lookup.size(); j++) { 
188                                 if (chosen.group == lookup[j]->getGroup()) {
189                                          abundance = lookup[j]->getAbundance(chosen.bin);
190                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
191                                          break;
192                                 }
193                         }
194                         
195                 }
196         }
197         catch(exception& e) {
198                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }
201         catch(...) {
202                 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203                 exit(1);
204         }
205
206 }
207
208 //**********************************************************************************************************************