]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
fixing minor bugs
[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                 //lookup.clear();
40                 int count = 1;
41                 string errorOff = "no error";
42                         
43                 //read in listfile
44                 read = new ReadOTUFile(globaldata->inputFileName);      
45                 read->read(&*globaldata); 
46
47                 input = globaldata->ginput;
48                 SharedList = globaldata->gSharedList;
49                 SharedListVector* lastList = SharedList;
50                 //lookup = SharedList->getSharedRAbundVector();
51                 //vector<SharedRAbundVector*> lastLookup = lookup;
52                 
53                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
54                 set<string> processedLabels;
55                 set<string> userLabels = globaldata->labels;
56                 set<int> userLines = globaldata->lines;
57                 
58                 shared = new Shared();
59                 
60                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
61                         
62
63                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
64                                         
65                                         shared->getSharedVectors(SharedList); //fills sharedGroups with new info and updates sharedVector
66                                         printSharedData(); //prints info to the .shared file
67                                 
68                                         processedLabels.insert(SharedList->getLabel());
69                                         userLabels.erase(SharedList->getLabel());
70                                         userLines.erase(count);
71                         }
72                         
73                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
74                                         shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector
75                                         printSharedData(); //prints info to the .shared file
76
77                                         processedLabels.insert(lastList->getLabel());
78                                         userLabels.erase(lastList->getLabel());
79                         }
80                         
81                         if (count != 1) { delete lastList; }
82                         lastList = SharedList;                  
83                         SharedList = input->getSharedListVector(); //get new list vector to process
84                         
85                         //if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
86                         //lastLookup = lookup; 
87                         //if (SharedList != NULL) { lookup = SharedList->getSharedRAbundVector(); }
88                 }
89                 
90                 //output error messages about any remaining user labels
91                 set<string>::iterator it;
92                 bool needToRun = false;
93                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
94                         //cout << "Your file does not include the label "<< *it; 
95                         if (processedLabels.count(lastList->getLabel()) != 1) {
96                                 //cout << ". I will use " << lastList->getLabel() << "." << endl;
97                                 needToRun = true;
98                         }else {
99                                 //cout << ". Please refer to " << lastList->getLabel() << "." << endl;
100                         }
101                 }
102                 
103                 //run last line if you need to
104                 if (needToRun == true)  {
105                         shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector
106                         printSharedData(); //prints info to the .shared file
107                 }
108                 
109                 delete lastList;
110                 delete shared;
111                 out.close();
112                 
113                 return 0;
114         }
115         catch(exception& e) {
116                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
117                 exit(1);
118         }
119         catch(...) {
120                 cout << "An unknown error has occurred in the SharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121                 exit(1);
122         }
123
124 }
125 //**********************************************************************************************************************
126 void SharedCommand::printSharedData() {
127         try {
128         
129                 ///for (int i = 0; i < thislookup.size(); i++) {
130                 //      out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
131 //cout << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << endl;                       
132                 //      thislookup[i]->print(out);
133         //      }
134                 //prints out horizontally
135                 for (it = shared->sharedGroups.begin(); it != shared->sharedGroups.end(); it++) {
136                         out << it->second->getLabel() << "\t" << it->first << "\t"; //prints out label and groupname
137                         it->second->print(out); // prints sharedrabundvector
138                 }
139         }
140         catch(exception& e) {
141                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142                 exit(1);
143         }
144         catch(...) {
145                 cout << "An unknown error has occurred in the SharedCommand class function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
146                 exit(1);
147         }
148         
149 }
150
151 //**********************************************************************************************************************
152
153 SharedCommand::~SharedCommand(){
154         //delete list;
155         delete read;
156         
157 }
158
159 //**********************************************************************************************************************