]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
deleted read.shared command and added its functionality to read.otu.
[mothur.git] / rarefactsharedcommand.cpp
1 /*
2  *  rarefactsharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/6/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "rarefactsharedcommand.h"
11 #include "sharedsobs.h"
12
13 //**********************************************************************************************************************
14
15 RareFactSharedCommand::RareFactSharedCommand(){
16         try {
17                 globaldata = GlobalData::getInstance();
18                 string fileNameRoot;
19                 fileNameRoot = getRootName(globaldata->inputFileName);
20                 format = globaldata->getFormat();
21                 validCalculator = new ValidCalculators();
22                                 
23                 int i;
24                 for (i=0; i<globaldata->Estimators.size(); i++) {
25                         if (validCalculator->isValidCalculator("sharedrarefaction", globaldata->Estimators[i]) == true) { 
26                                 if (globaldata->Estimators[i] == "sharedobserved") { 
27                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
28                                 }
29                         }
30                 }
31                 
32                 //reset calc for next command
33                 globaldata->setCalc("");
34
35         }
36         catch(exception& e) {
37                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
38                 exit(1);
39         }
40         catch(...) {
41                 cout << "An unknown error has occurred in the RareFactSharedCommand class function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
42                 exit(1);
43         }       
44                         
45 }
46
47 //**********************************************************************************************************************
48
49 RareFactSharedCommand::~RareFactSharedCommand(){
50         delete order;
51         delete input;
52         delete rCurve;
53         delete read;
54 }
55
56 //**********************************************************************************************************************
57
58 int RareFactSharedCommand::execute(){
59         try {
60                 int count = 1;
61                 
62                 //if the users entered no valid calculators don't execute command
63                 if (rDisplays.size() == 0) { return 0; }
64
65                 if (format == "sharedfile") {
66                         read = new ReadPhilFile(globaldata->inputFileName);     
67                         read->read(&*globaldata); 
68                         
69                         input = globaldata->ginput;
70                         order = input->getSharedOrderVector();
71                 }else {
72                         //you are using a list and a groupfile
73                         read = new ReadPhilFile(globaldata->inputFileName);     
74                         read->read(&*globaldata); 
75                 
76                         input = globaldata->ginput;
77                         SharedList = globaldata->gSharedList;
78                         order = SharedList->getSharedOrderVector();
79                 }
80                 
81                 //set users groups
82                 setGroups();
83                 
84                 while(order != NULL){
85                 
86                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
87                                 //create collectors curve
88                                 rCurve = new Rarefact(order, rDisplays);
89                                 convert(globaldata->getFreq(), freq);
90                                 convert(globaldata->getIters(), nIters);
91                                 rCurve->getSharedCurve(freq, nIters);
92                         
93                                 delete rCurve;
94                         
95                                 cout << order->getLabel() << '\t' << count << endl;
96                         }
97                         
98                         //get next line to process
99                         if (format == "sharedfile") {
100                                 order = input->getSharedOrderVector();
101                         }else {
102                                 //you are using a list and a groupfile
103                                 SharedList = input->getSharedListVector(); //get new list vector to process
104                                 if (SharedList != NULL) {
105                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
106                                 }else {
107                                         break;
108                                 }
109                         }
110                         
111                         count++;
112                 }
113         
114                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
115                 
116                 //reset groups parameter
117                 globaldata->Groups.clear();  globaldata->setGroups("");
118
119                 return 0;
120         }
121         catch(exception& e) {
122                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123                 exit(1);
124         }
125         catch(...) {
126                 cout << "An unknown error has occurred in the RareFactSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127                 exit(1);
128         }       
129 }
130
131
132 //**********************************************************************************************************************
133
134 void RareFactSharedCommand::setGroups() {
135         try {
136                 //if the user has not entered specific groups to analyze then do them all
137                 if (globaldata->Groups.size() != 0) {
138                         if (globaldata->Groups[0] != "all") {
139                                 //check that groups are valid
140                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
141                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
142                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
143                                                 // erase the invalid group from globaldata->Groups
144                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
145                                         }
146                                 }
147                         
148                                 //if the user only entered invalid groups
149                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
150                                         cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl; 
151                                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
152                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
153                                         }
154                                 }
155                         }else{//user has enter "all" and wants the default groups
156                                 globaldata->Groups.clear();
157                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
158                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
159                                 }
160                                 globaldata->setGroups("");
161                         }
162                 }else {
163                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
164                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
165                         }
166                 }
167                 
168         }
169         catch(exception& e) {
170                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
171                 exit(1);
172         }
173         catch(...) {
174                 cout << "An unknown error has occurred in the RareFactSharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
175                 exit(1);
176         }               
177
178 }
179 /***********************************************************/
180