]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
version working on mac with .tellg changed to unget for windows
[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                         count++;
85                         
86                         //if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
87                         //lastLookup = lookup; 
88                         //if (SharedList != NULL) { lookup = SharedList->getSharedRAbundVector(); }
89                 }
90                 
91                 //output error messages about any remaining user labels
92                 set<string>::iterator it;
93                 bool needToRun = false;
94                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
95                         //cout << "Your file does not include the label "<< *it; 
96                         if (processedLabels.count(lastList->getLabel()) != 1) {
97                                 //cout << ". I will use " << lastList->getLabel() << "." << endl;
98                                 needToRun = true;
99                         }else {
100                                 //cout << ". Please refer to " << lastList->getLabel() << "." << endl;
101                         }
102                 }
103                 
104                 //run last line if you need to
105                 if (needToRun == true)  {
106                         shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector
107                         printSharedData(); //prints info to the .shared file
108                 }
109                 
110                 delete lastList; globaldata->gSharedList = NULL;
111                 delete shared;
112                 out.close();
113                 
114                 return 0;
115         }
116         catch(exception& e) {
117                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
118                 exit(1);
119         }
120         catch(...) {
121                 cout << "An unknown error has occurred in the SharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }
124
125 }
126 //**********************************************************************************************************************
127 void SharedCommand::printSharedData() {
128         try {
129         
130                 ///for (int i = 0; i < thislookup.size(); i++) {
131                 //      out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
132 //cout << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << endl;                       
133                 //      thislookup[i]->print(out);
134         //      }
135                 //prints out horizontally
136                 for (it = shared->sharedGroups.begin(); it != shared->sharedGroups.end(); it++) {
137                         out << it->second->getLabel() << "\t" << it->first << "\t"; //prints out label and groupname
138                         it->second->print(out); // prints sharedrabundvector
139                 }
140         }
141         catch(exception& e) {
142                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143                 exit(1);
144         }
145         catch(...) {
146                 cout << "An unknown error has occurred in the SharedCommand class function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }
149         
150 }
151
152 //**********************************************************************************************************************
153
154 SharedCommand::~SharedCommand(){
155         //delete list;
156         delete read;
157         
158 }
159
160 //**********************************************************************************************************************