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