]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[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(string o) : outputDir(o) {
15         try {
16                 globaldata = GlobalData::getInstance();
17                 
18                 //getting output filename
19                 filename = globaldata->inputFileName;
20                 if (outputDir == "") { outputDir += hasPath(filename); }
21                 
22                 filename = outputDir + getRootName(getSimpleName(filename));
23                 filename = filename + "shared";
24                 
25                 openOutputFile(filename, out);
26                 pickedGroups = false;
27                 
28                 groupMap = globaldata->gGroupmap;
29                 
30                 //if hte user has not specified any groups then use them all
31                 if (globaldata->Groups.size() == 0) {
32                         groups = groupMap->namesOfGroups;
33                 }else{ //they have specified groups
34                         groups = globaldata->Groups;
35                         pickedGroups = true;
36                 }
37                 
38                 //fill filehandles with neccessary ofstreams
39                 int i;
40                 ofstream* temp;
41                 for (i=0; i<groups.size(); i++) {
42                         temp = new ofstream;
43                         filehandles[groups[i]] = temp;
44                 }
45                 
46                 //set fileroot
47                 fileroot = outputDir + getRootName(getSimpleName(globaldata->getListFile()));
48                 
49                 //clears file before we start to write to it below
50                 for (int i=0; i<groups.size(); i++) {
51                         remove((fileroot + groups[i] + ".rabund").c_str());
52                 }
53
54         }
55         catch(exception& e) {
56                 errorOut(e, "SharedCommand", "SharedCommand");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61
62 int SharedCommand::execute(){
63         try {
64                 
65                 //lookup.clear();
66                 string errorOff = "no error";
67                 //errorOff = "";
68 cout << globaldata->inputFileName << endl;                      
69                 //read in listfile
70                 read = new ReadOTUFile(globaldata->inputFileName);      
71                 read->read(&*globaldata); 
72                 delete read;
73
74                 input = globaldata->ginput;
75                 SharedList = globaldata->gSharedList;
76                 string lastLabel = SharedList->getLabel();
77                 vector<SharedRAbundVector*> lookup; 
78                 
79                 if ((globaldata->Groups.size() == 0) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) {  //if the user has not specified any groups and their files don't match exit with error
80                         mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); mothurOutEndLine(); 
81                         
82                         out.close();
83                         remove(filename.c_str()); //remove blank shared file you made
84                         
85                         createMisMatchFile();
86                         
87                         //delete memory
88                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
89                                 delete it3->second;
90                         }
91                         delete SharedList;
92                         globaldata->gSharedList = NULL;
93                         
94                         return 1; 
95                 }
96                 
97                 //if user has specified groups make new groupfile for them
98                 if (globaldata->Groups.size() != 0) { //make new group file
99                         string groups = "";
100                         for (int i = 0; i < globaldata->Groups.size(); i++) {
101                                 groups += globaldata->Groups[i] + ".";
102                         }
103                 
104                         string newGroupFile = getRootName(globaldata->inputFileName) + groups + "groups";
105                         ofstream outGroups;
106                         openOutputFile(newGroupFile, outGroups);
107                 
108                         vector<string> names = groupMap->getNamesSeqs();
109                         string groupName;
110                         for (int i = 0; i < names.size(); i++) {
111                                 groupName = groupMap->getGroup(names[i]);
112                                 if (isValidGroup(groupName, globaldata->Groups)) {
113                                         outGroups << names[i] << '\t' << groupName << endl;
114                                 }
115                         }
116                         outGroups.close();
117                 }
118                 
119                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
120                 set<string> processedLabels;
121                 set<string> userLabels = globaldata->labels;    
122         
123                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
124                 
125                         if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
126                         
127                                         lookup = SharedList->getSharedRAbundVector();
128                                         if (pickedGroups) { //check for otus with no seqs in them
129                                                 eliminateZeroOTUS(lookup);
130                                         }
131                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
132                                         
133                                         printSharedData(lookup); //prints info to the .shared file
134                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
135                                 
136                                         processedLabels.insert(SharedList->getLabel());
137                                         userLabels.erase(SharedList->getLabel());
138                         }
139                         
140                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
141                                         string saveLabel = SharedList->getLabel();
142                                         
143                                         delete SharedList;
144                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
145                                         
146                                         lookup = SharedList->getSharedRAbundVector();
147                                         if (pickedGroups) { //check for otus with no seqs in them
148                                                 eliminateZeroOTUS(lookup);
149                                         }
150                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
151                                         
152                                         printSharedData(lookup); //prints info to the .shared file
153                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
154                                         
155                                         processedLabels.insert(SharedList->getLabel());
156                                         userLabels.erase(SharedList->getLabel());
157                                         
158                                         //restore real lastlabel to save below
159                                         SharedList->setLabel(saveLabel);
160                         }
161                         
162                 
163                         lastLabel = SharedList->getLabel();
164                                 
165                         delete SharedList;
166                         SharedList = input->getSharedListVector(); //get new list vector to process
167                 }
168                 
169                 //output error messages about any remaining user labels
170                 set<string>::iterator it;
171                 bool needToRun = false;
172                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
173                         if (processedLabels.count(lastLabel) != 1) {
174                                 needToRun = true;
175                         }
176                 }
177                 
178                 //run last label if you need to
179                 if (needToRun == true)  {
180                         if (SharedList != NULL) {       delete SharedList;      }
181                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
182                                         
183                         lookup = SharedList->getSharedRAbundVector();
184                         if (pickedGroups) { //check for otus with no seqs in them
185                                 eliminateZeroOTUS(lookup);
186                         }
187                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
188                         
189                         printSharedData(lookup); //prints info to the .shared file
190                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
191                         delete SharedList;
192                 }
193                 
194                 globaldata->gSharedList = NULL;
195                 
196                 out.close();
197                 
198                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
199                         delete it3->second;
200                 }
201
202                 globaldata->setSharedFile(filename);
203                 
204                 return 0;
205         }
206         catch(exception& e) {
207                 errorOut(e, "SharedCommand", "execute");
208                 exit(1);
209         }
210 }
211 //**********************************************************************************************************************
212 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
213         try {
214                 
215                 //initialize bin values
216                 for (int i = 0; i < thislookup.size(); i++) {
217 //cout << "in printData " << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() <<  endl;
218                         out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
219                         thislookup[i]->print(out);
220                         
221                         RAbundVector rav = thislookup[i]->getRAbundVector();
222                         openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
223                         rav.print(*(filehandles[thislookup[i]->getGroup()]));
224                         (*(filehandles[thislookup[i]->getGroup()])).close();
225                 }
226  
227         }
228         catch(exception& e) {
229                 errorOut(e, "SharedCommand", "printSharedData");
230                 exit(1);
231         }
232 }
233 //**********************************************************************************************************************
234 void SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
235         try {
236                 
237                 vector<SharedRAbundVector*> newLookup;
238                 for (int i = 0; i < thislookup.size(); i++) {
239                         SharedRAbundVector* temp = new SharedRAbundVector();
240                         temp->setLabel(thislookup[i]->getLabel());
241                         temp->setGroup(thislookup[i]->getGroup());
242                         newLookup.push_back(temp);
243                 }
244                 
245                 //for each bin
246                 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
247                 
248                         //look at each sharedRabund and make sure they are not all zero
249                         bool allZero = true;
250                         for (int j = 0; j < thislookup.size(); j++) {
251                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
252                         }
253                         
254                         //if they are not all zero add this bin
255                         if (!allZero) {
256                                 for (int j = 0; j < thislookup.size(); j++) {
257                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
258                                 }
259                         }
260                         //else{  cout << "bin # " << i << " is all zeros" << endl;  }
261                 }
262         
263                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
264                 thislookup = newLookup;
265         
266  
267         }
268         catch(exception& e) {
269                 errorOut(e, "SharedCommand", "eliminateZeroOTUS");
270                 exit(1);
271         }
272 }
273 //**********************************************************************************************************************
274 void SharedCommand::createMisMatchFile() {
275         try {
276                 ofstream outMisMatch;
277                 string outputMisMatchName = getRootName(globaldata->inputFileName);
278                 
279                 //you have sequences in your list file that are not in your group file
280                 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) { 
281                         outputMisMatchName += "missing.group";
282                         mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
283                         
284                         openOutputFile(outputMisMatchName, outMisMatch);
285                         
286                         //go through list and if group returns "not found" output it
287                         for (int i = 0; i < SharedList->getNumBins(); i++) {
288                         
289                                 string names = SharedList->get(i); 
290                                 
291                                 while (names.find_first_of(',') != -1) { 
292                                         string name = names.substr(0,names.find_first_of(','));
293                                         names = names.substr(names.find_first_of(',')+1, names.length());
294                                         string group = groupMap->getGroup(name);
295                                 
296                                         if(group == "not found") {      outMisMatch << name << endl;  }
297                                 }
298                         
299                                 //get last name
300                                 string group = groupMap->getGroup(names);
301                                 if(group == "not found") {      outMisMatch << names << endl;  }                                
302                         }
303                         
304                         outMisMatch.close();
305                         
306                 
307                 }else {//you have sequences in your group file that are not in you list file
308                         
309                         outputMisMatchName += "missing.name";
310                         mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
311                         
312                         map<string, string> namesInList;
313                         
314                         //go through listfile and get names
315                         for (int i = 0; i < SharedList->getNumBins(); i++) {
316                                 
317                                 string names = SharedList->get(i); 
318                 
319                                 while (names.find_first_of(',') != -1) { 
320                                         string name = names.substr(0,names.find_first_of(','));
321                                         names = names.substr(names.find_first_of(',')+1, names.length());
322                                         
323                                         namesInList[name] = name;
324                                 }
325                                 
326                                 //get last name
327                                 namesInList[names] = names;                             
328                         }
329                         
330                         //get names of sequences in groupfile
331                         vector<string> seqNames = groupMap->getNamesSeqs();
332                 
333                         map<string, string>::iterator itMatch;
334                         
335                         openOutputFile(outputMisMatchName, outMisMatch);
336                         
337                         //loop through names in seqNames and if they aren't in namesIn list output them
338                         for (int i = 0; i < seqNames.size(); i++) {
339                                 
340                                 itMatch = namesInList.find(seqNames[i]);
341                                 
342                                 if (itMatch == namesInList.end()) {
343                                 
344                                         outMisMatch << seqNames[i] << endl; 
345                                 }
346                         }               
347                         outMisMatch.close();
348                 }
349  
350         }
351         catch(exception& e) {
352                 errorOut(e, "SharedCommand", "createMisMatchFile");
353                 exit(1);
354         }
355 }
356
357 //**********************************************************************************************************************
358
359 SharedCommand::~SharedCommand(){
360         //delete list;
361         
362         
363 }
364
365 //**********************************************************************************************************************
366
367 bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
368         try {
369                 for (int i = 0; i < groups.size(); i++) {
370                         if (groupname == groups[i]) { return true; }
371                 }
372                 
373                 return false;
374         }
375         catch(exception& e) {
376                 errorOut(e, "SharedCommand", "isValidGroup");
377                 exit(1);
378         }
379 }
380 /************************************************************/
381
382