]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
added get.rabund and get.sabund command and fixed bug introduced by line by line...
[mothur.git] / sharedcommand.cpp
1 /*
2  *  sharedcommand.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 "sharedcommand.h"
11
12 //**********************************************************************************************************************
13
14 SharedCommand::SharedCommand(){
15         try {
16                 globaldata = GlobalData::getInstance();
17                 
18                 //getting output filename
19                 filename = globaldata->inputFileName;
20                 filename = getRootName(filename);
21                 filename = filename + "shared";
22                 openOutputFile(filename, out);
23         }
24         catch(exception& e) {
25                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function SharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
26                 exit(1);
27         }
28         catch(...) {
29                 cout << "An unknown error has occurred in the SharedCommand class function SharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
30                 exit(1);
31         }
32
33 }
34 //**********************************************************************************************************************
35
36 int SharedCommand::execute(){
37         try {
38                 globaldata = GlobalData::getInstance();
39                 int count = 1;
40                 string errorOff = "no error";
41                         
42                 //read in listfile
43                 read = new ReadOTUFile(globaldata->inputFileName);      
44                 read->read(&*globaldata); 
45                 input = globaldata->ginput;
46                 SharedList = globaldata->gSharedList;
47                 SharedListVector* lastList = SharedList;
48                 
49                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
50                 set<string> processedLabels;
51                 set<string> userLabels = globaldata->labels;
52                 set<int> userLines = globaldata->lines;
53                 
54                 shared = new Shared();
55                 
56                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
57                         
58                                                 
59                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
60                         
61                                         shared->getSharedVectors(SharedList); //fills sharedGroups with new info and updates sharedVector
62                                         printSharedData(); //prints info to the .shared file
63                                 
64                                         processedLabels.insert(SharedList->getLabel());
65                                         userLabels.erase(SharedList->getLabel());
66                                         userLines.erase(count);
67                         }
68                         
69                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
70                                         shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector
71                                         printSharedData(); //prints info to the .shared file
72
73                                         processedLabels.insert(lastList->getLabel());
74                                         userLabels.erase(lastList->getLabel());
75                         }
76                         
77                         if (count != 1) { delete lastList; }
78                         lastList = SharedList;                  
79
80                         SharedList = input->getSharedListVector(); //get new list vector to process
81                 }
82                 
83                 //output error messages about any remaining user labels
84                 set<string>::iterator it;
85                 bool needToRun = false;
86                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
87                         //cout << "Your file does not include the label "<< *it; 
88                         if (processedLabels.count(lastList->getLabel()) != 1) {
89                                 //cout << ". I will use " << lastList->getLabel() << "." << endl;
90                                 needToRun = true;
91                         }else {
92                                 //cout << ". Please refer to " << lastList->getLabel() << "." << endl;
93                         }
94                 }
95                 
96                 //run last line if you need to
97                 if (needToRun == true)  {
98                         shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector
99                         printSharedData(); //prints info to the .shared file
100                 }
101                 
102                 delete lastList;
103                 delete shared;
104                 
105                 return 0;
106         }
107         catch(exception& e) {
108                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109                 exit(1);
110         }
111         catch(...) {
112                 cout << "An unknown error has occurred in the SharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
113                 exit(1);
114         }
115
116 }
117 //**********************************************************************************************************************
118 void SharedCommand::printSharedData() {
119         try {
120                 //prints out horizontally
121                 for (it = shared->sharedGroups.begin(); it != shared->sharedGroups.end(); it++) {
122                         out << it->second->getLabel() << "\t" << it->first << "\t"; //prints out label and groupname
123                         it->second->print(out); // prints sharedrabundvector
124                 }
125         }
126         catch(exception& e) {
127                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
128                 exit(1);
129         }
130         catch(...) {
131                 cout << "An unknown error has occurred in the SharedCommand class function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
132                 exit(1);
133         }
134         
135 }
136
137 //**********************************************************************************************************************
138
139 SharedCommand::~SharedCommand(){
140         //delete list;
141         delete read;
142         
143 }
144
145 //**********************************************************************************************************************