]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
added logfile feature
[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                 errorOut(e, "SharedCommand", "SharedCommand");
26                 exit(1);
27         }
28 }
29 //**********************************************************************************************************************
30
31 int SharedCommand::execute(){
32         try {
33                 
34                 mothurOut("creating sharedfile..."); mothurOutEndLine();
35                 //lookup.clear();
36                 int count = 1;
37                 string errorOff = "no error";
38                         
39                 //read in listfile
40                 read = new ReadOTUFile(globaldata->inputFileName);      
41                 read->read(&*globaldata); 
42
43                 input = globaldata->ginput;
44                 SharedList = globaldata->gSharedList;
45                 string lastLabel = SharedList->getLabel();
46                 vector<SharedRAbundVector*> lookup; 
47                                 
48                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
49                 set<string> processedLabels;
50                 set<string> userLabels = globaldata->labels;
51                 set<int> userLines = globaldata->lines;
52                 
53                 
54                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
55                         
56
57                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
58                                         lookup = SharedList->getSharedRAbundVector();
59                                         printSharedData(lookup); //prints info to the .shared file
60                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
61                                 
62                                         processedLabels.insert(SharedList->getLabel());
63                                         userLabels.erase(SharedList->getLabel());
64                                         userLines.erase(count);
65                         }
66                         
67                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
68                                         delete SharedList;
69                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
70                                         
71                                         lookup = SharedList->getSharedRAbundVector();
72                                         printSharedData(lookup); //prints info to the .shared file
73                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
74                                         
75                                         processedLabels.insert(SharedList->getLabel());
76                                         userLabels.erase(SharedList->getLabel());
77                         }
78                         
79                 
80                         lastLabel = SharedList->getLabel();
81                                 
82                         delete SharedList;
83                         SharedList = input->getSharedListVector(); //get new list vector to process
84                         
85                         count++;                
86                 }
87                 
88                 //output error messages about any remaining user labels
89                 set<string>::iterator it;
90                 bool needToRun = false;
91                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
92                         if (processedLabels.count(lastLabel) != 1) {
93                                 needToRun = true;
94                         }
95                 }
96                 
97                 //run last line if you need to
98                 if (needToRun == true)  {
99                         delete SharedList;
100                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
101                                         
102                         lookup = SharedList->getSharedRAbundVector();
103                         printSharedData(lookup); //prints info to the .shared file
104                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
105                         delete SharedList;
106                 }
107                 
108                 globaldata->gSharedList = NULL;
109                 delete read;
110                 
111                 out.close();
112                 
113                 mothurOut("complete."); mothurOutEndLine();
114                 return 0;
115         }
116         catch(exception& e) {
117                 errorOut(e, "SharedCommand", "execute");
118                 exit(1);
119         }
120 }
121 //**********************************************************************************************************************
122 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
123         try {
124                 
125                 //initialize bin values
126                 for (int i = 0; i < thislookup.size(); i++) {
127                         out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
128                         thislookup[i]->print(out);
129                 }
130  
131         }
132         catch(exception& e) {
133                 errorOut(e, "SharedCommand", "printSharedData");
134                 exit(1);
135         }
136 }
137
138 //**********************************************************************************************************************
139
140 SharedCommand::~SharedCommand(){
141         //delete list;
142         
143         
144 }
145
146 //**********************************************************************************************************************