]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
working on chimeraslayer and found bug in shared command.
[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                 groupMap = globaldata->gGroupmap;
25                 
26                 //fill filehandles with neccessary ofstreams
27                 int i;
28                 ofstream* temp;
29                 for (i=0; i<groupMap->getNumGroups(); i++) {
30                         temp = new ofstream;
31                         filehandles[groupMap->namesOfGroups[i]] = temp;
32                 }
33                 
34                 //set fileroot
35                 fileroot = getRootName(globaldata->getListFile());
36                 
37                 //clears file before we start to write to it below
38                 for (int i=0; i<groupMap->getNumGroups(); i++) {
39                         remove((fileroot + groupMap->namesOfGroups[i] + ".rabund").c_str());
40                 }
41
42         }
43         catch(exception& e) {
44                 errorOut(e, "SharedCommand", "SharedCommand");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49
50 int SharedCommand::execute(){
51         try {
52                 
53                 //lookup.clear();
54                 string errorOff = "no error";
55                         
56                 //read in listfile
57                 read = new ReadOTUFile(globaldata->inputFileName);      
58                 read->read(&*globaldata); 
59                 delete read;
60
61                 input = globaldata->ginput;
62                 SharedList = globaldata->gSharedList;
63                 string lastLabel = SharedList->getLabel();
64                 vector<SharedRAbundVector*> lookup; 
65                 
66                 if (SharedList->getNumSeqs() != groupMap->getNumSeqs()) {  
67                         mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); mothurOutEndLine(); 
68                         
69                         out.close();
70                         remove(filename.c_str()); //remove blank shared file you made
71                         
72                         createMisMatchFile();
73                         
74                         //delete memory
75                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
76                                 delete it3->second;
77                         }
78                         delete SharedList;
79                         globaldata->gSharedList = NULL;
80                         
81                         return 1; 
82                 }
83                 
84                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
85                 set<string> processedLabels;
86                 set<string> userLabels = globaldata->labels;
87                 
88                 
89                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
90                         
91
92                         if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
93                                         
94                                         lookup = SharedList->getSharedRAbundVector();
95                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
96                                         
97                                         printSharedData(lookup); //prints info to the .shared file
98                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
99                                 
100                                         processedLabels.insert(SharedList->getLabel());
101                                         userLabels.erase(SharedList->getLabel());
102                         }
103                         
104                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
105                                         delete SharedList;
106                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
107                                         
108                                         lookup = SharedList->getSharedRAbundVector();
109                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
110                                         
111                                         printSharedData(lookup); //prints info to the .shared file
112                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
113                                         
114                                         processedLabels.insert(SharedList->getLabel());
115                                         userLabels.erase(SharedList->getLabel());
116                         }
117                         
118                 
119                         lastLabel = SharedList->getLabel();
120                                 
121                         delete SharedList;
122                         SharedList = input->getSharedListVector(); //get new list vector to process
123                 }
124                 
125                 //output error messages about any remaining user labels
126                 set<string>::iterator it;
127                 bool needToRun = false;
128                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
129                         if (processedLabels.count(lastLabel) != 1) {
130                                 needToRun = true;
131                         }
132                 }
133                 
134                 //run last label if you need to
135                 if (needToRun == true)  {
136                         if (SharedList != NULL) {       delete SharedList;      }
137                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
138                                         
139                         lookup = SharedList->getSharedRAbundVector();
140                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
141                         
142                         printSharedData(lookup); //prints info to the .shared file
143                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
144                         delete SharedList;
145                 }
146                 
147                 globaldata->gSharedList = NULL;
148                 
149                 out.close();
150                 
151                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
152                         delete it3->second;
153                 }
154
155                 
156                 return 0;
157         }
158         catch(exception& e) {
159                 errorOut(e, "SharedCommand", "execute");
160                 exit(1);
161         }
162 }
163 //**********************************************************************************************************************
164 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
165         try {
166                 
167                 //initialize bin values
168                 for (int i = 0; i < thislookup.size(); i++) {
169                         out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
170                         thislookup[i]->print(out);
171                         
172                         RAbundVector rav = thislookup[i]->getRAbundVector();
173                         openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
174                         rav.print(*(filehandles[thislookup[i]->getGroup()]));
175                         (*(filehandles[thislookup[i]->getGroup()])).close();
176                 }
177  
178         }
179         catch(exception& e) {
180                 errorOut(e, "SharedCommand", "printSharedData");
181                 exit(1);
182         }
183 }
184 //**********************************************************************************************************************
185 void SharedCommand::createMisMatchFile() {
186         try {
187                 ofstream outMisMatch;
188                 string outputMisMatchName = getRootName(globaldata->inputFileName);
189                 
190                 //you have sequences in your list file that are not in your group file
191                 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) { 
192                         outputMisMatchName += "missing.group";
193                         mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
194                         
195                         openOutputFile(outputMisMatchName, outMisMatch);
196                         
197                         //go through list and if group returns "not found" output it
198                         for (int i = 0; i < SharedList->getNumBins(); i++) {
199                         
200                                 string names = SharedList->get(i); 
201                                 
202                                 while (names.find_first_of(',') != -1) { 
203                                         string name = names.substr(0,names.find_first_of(','));
204                                         names = names.substr(names.find_first_of(',')+1, names.length());
205                                         string group = groupMap->getGroup(name);
206                                         
207                                         if(group == "not found") {      outMisMatch << name << endl;  }
208                                 }
209                                 
210                                 //get last name
211                                 string group = groupMap->getGroup(names);
212                                 if(group == "not found") {      outMisMatch << names << endl;  }                                
213                         }
214                         
215                         outMisMatch.close();
216                         
217                 
218                 }else {//you have sequences in your group file that are not in you list file
219                         
220                         outputMisMatchName += "missing.name";
221                         mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
222                         
223                         map<string, string> namesInList;
224                         
225                         //go through listfile and get names
226                         for (int i = 0; i < SharedList->getNumBins(); i++) {
227                                 
228                                 string names = SharedList->get(i); 
229                 
230                                 while (names.find_first_of(',') != -1) { 
231                                         string name = names.substr(0,names.find_first_of(','));
232                                         names = names.substr(names.find_first_of(',')+1, names.length());
233                                         
234                                         namesInList[name] = name;
235                                 }
236                                 
237                                 //get last name
238                                 namesInList[names] = names;                             
239                         }
240                         
241                         //get names of sequences in groupfile
242                         vector<string> seqNames = groupMap->getNamesSeqs();
243                 
244                         map<string, string>::iterator itMatch;
245                         
246                         openOutputFile(outputMisMatchName, outMisMatch);
247                         
248                         //loop through names in seqNames and if they aren't in namesIn list output them
249                         for (int i = 0; i < seqNames.size(); i++) {
250                                 
251                                 itMatch = namesInList.find(seqNames[i]);
252                                 
253                                 if (itMatch == namesInList.end()) {
254                                 
255                                         outMisMatch << seqNames[i] << endl; 
256                                 }
257                         }               
258                         outMisMatch.close();
259                 }
260  
261         }
262         catch(exception& e) {
263                 errorOut(e, "SharedCommand", "createMisMatchFile");
264                 exit(1);
265         }
266 }
267
268 //**********************************************************************************************************************
269
270 SharedCommand::~SharedCommand(){
271         //delete list;
272         
273         
274 }
275
276 //**********************************************************************************************************************