5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedcommand.h"
11 //********************************************************************************************************************
12 //sorts lowest to highest
13 inline bool compareSharedRabunds(SharedRAbundVector* left, SharedRAbundVector* right){
14 return (left->getGroup() < right->getGroup());
16 //**********************************************************************************************************************
17 vector<string> SharedCommand::getValidParameters(){
19 vector<string> myArray;
23 m->errorOut(e, "SharedCommand", "getValidParameters");
27 //**********************************************************************************************************************
28 SharedCommand::SharedCommand(){
31 //initialize outputTypes
32 vector<string> tempOutNames;
33 outputTypes["rabund"] = tempOutNames;
34 outputTypes["shared"] = tempOutNames;
37 m->errorOut(e, "SharedCommand", "SharedCommand");
41 //**********************************************************************************************************************
42 vector<string> SharedCommand::getRequiredParameters(){
44 vector<string> myArray;
48 m->errorOut(e, "SharedCommand", "getRequiredParameters");
52 //**********************************************************************************************************************
53 vector<string> SharedCommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "SharedCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
65 SharedCommand::SharedCommand(string o) : outputDir(o) {
67 globaldata = GlobalData::getInstance();
69 //initialize outputTypes
70 vector<string> tempOutNames;
71 outputTypes["rabund"] = tempOutNames;
72 outputTypes["shared"] = tempOutNames;
74 //getting output filename
75 filename = globaldata->inputFileName;
76 if (outputDir == "") { outputDir += m->hasPath(filename); }
78 filename = outputDir + m->getRootName(m->getSimpleName(filename));
79 filename = filename + "shared";
80 outputTypes["shared"].push_back(filename);
82 m->openOutputFile(filename, out);
85 groupMap = globaldata->gGroupmap;
87 //if hte user has not specified any groups then use them all
88 if (globaldata->Groups.size() == 0) {
89 groups = groupMap->namesOfGroups;
90 }else{ //they have specified groups
91 groups = globaldata->Groups;
95 //fill filehandles with neccessary ofstreams
98 for (i=0; i<groups.size(); i++) {
100 filehandles[groups[i]] = temp;
104 fileroot = outputDir + m->getRootName(m->getSimpleName(globaldata->getListFile()));
106 //clears file before we start to write to it below
107 for (int i=0; i<groups.size(); i++) {
108 remove((fileroot + groups[i] + ".rabund").c_str());
109 outputNames.push_back((fileroot + groups[i] + ".rabund"));
110 outputTypes["rabund"].push_back((fileroot + groups[i] + ".rabund"));
114 catch(exception& e) {
115 m->errorOut(e, "SharedCommand", "SharedCommand");
119 //**********************************************************************************************************************
121 int SharedCommand::execute(){
125 string errorOff = "no error";
128 //if user provided an order file containing the order the shared file should be in read it
129 if (globaldata->getOrderGroupFile() != "") { readOrderFile(); }
132 read = new ReadOTUFile(globaldata->inputFileName);
133 read->read(&*globaldata);
136 input = globaldata->ginput;
137 SharedList = globaldata->gSharedList;
138 string lastLabel = SharedList->getLabel();
139 vector<SharedRAbundVector*> lookup;
141 if (m->control_pressed) {
142 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
143 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
144 out.close(); remove(filename.c_str());
145 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
149 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
150 m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine();
153 remove(filename.c_str()); //remove blank shared file you made
155 createMisMatchFile();
158 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
162 globaldata->ginput = NULL;
164 globaldata->gSharedList = NULL;
169 //if user has specified groups make new groupfile for them
170 if (globaldata->Groups.size() != 0) { //make new group file
172 for (int i = 0; i < globaldata->Groups.size(); i++) {
173 groups += globaldata->Groups[i] + ".";
176 string newGroupFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + groups + "groups";
178 m->openOutputFile(newGroupFile, outGroups);
180 vector<string> names = groupMap->getNamesSeqs();
182 for (int i = 0; i < names.size(); i++) {
183 groupName = groupMap->getGroup(names[i]);
184 if (isValidGroup(groupName, globaldata->Groups)) {
185 outGroups << names[i] << '\t' << groupName << endl;
191 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
192 set<string> processedLabels;
193 set<string> userLabels = globaldata->labels;
195 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
196 if (m->control_pressed) {
197 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
198 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
199 out.close(); remove(filename.c_str());
200 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
204 if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
206 lookup = SharedList->getSharedRAbundVector();
208 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
209 if (pickedGroups) { //check for otus with no seqs in them
210 eliminateZeroOTUS(lookup);
213 if (m->control_pressed) {
214 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
215 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
216 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
217 out.close(); remove(filename.c_str());
218 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
222 printSharedData(lookup); //prints info to the .shared file
223 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
225 processedLabels.insert(SharedList->getLabel());
226 userLabels.erase(SharedList->getLabel());
229 if ((m->anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
230 string saveLabel = SharedList->getLabel();
233 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
235 lookup = SharedList->getSharedRAbundVector();
236 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
237 if (pickedGroups) { //check for otus with no seqs in them
238 eliminateZeroOTUS(lookup);
242 if (m->control_pressed) {
243 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
244 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
245 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
246 out.close(); remove(filename.c_str());
247 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
251 printSharedData(lookup); //prints info to the .shared file
252 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
254 processedLabels.insert(SharedList->getLabel());
255 userLabels.erase(SharedList->getLabel());
257 //restore real lastlabel to save below
258 SharedList->setLabel(saveLabel);
262 lastLabel = SharedList->getLabel();
265 SharedList = input->getSharedListVector(); //get new list vector to process
268 //output error messages about any remaining user labels
269 set<string>::iterator it;
270 bool needToRun = false;
271 for (it = userLabels.begin(); it != userLabels.end(); it++) {
272 if (processedLabels.count(lastLabel) != 1) {
277 //run last label if you need to
278 if (needToRun == true) {
279 if (SharedList != NULL) { delete SharedList; }
280 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
282 lookup = SharedList->getSharedRAbundVector();
283 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
284 if (pickedGroups) { //check for otus with no seqs in them
285 eliminateZeroOTUS(lookup);
288 if (m->control_pressed) {
289 delete input; globaldata->ginput = NULL;
290 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
291 out.close(); remove(filename.c_str());
292 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
296 printSharedData(lookup); //prints info to the .shared file
297 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
301 globaldata->gSharedList = NULL;
305 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
310 //change format to shared to speed up commands
311 globaldata->setFormat("sharedfile");
312 globaldata->setListFile("");
313 globaldata->setGroupFile("");
314 globaldata->setSharedFile(filename);
317 if (m->control_pressed) {
318 delete input; globaldata->ginput = NULL;
319 remove(filename.c_str());
320 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
324 //set rabund file as new current rabundfile
326 itTypes = outputTypes.find("rabund");
327 if (itTypes != outputTypes.end()) {
328 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
331 itTypes = outputTypes.find("shared");
332 if (itTypes != outputTypes.end()) {
333 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
336 m->mothurOutEndLine();
337 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
338 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
339 m->mothurOut(filename); m->mothurOutEndLine();
340 m->mothurOutEndLine();
344 catch(exception& e) {
345 m->errorOut(e, "SharedCommand", "execute");
349 //**********************************************************************************************************************
350 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
353 if (order.size() == 0) { //user has not specified an order so do aplabetically
354 sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
356 globaldata->Groups.clear();
358 //initialize bin values
359 for (int i = 0; i < thislookup.size(); i++) {
360 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
361 thislookup[i]->print(out);
363 globaldata->Groups.push_back(thislookup[i]->getGroup());
365 RAbundVector rav = thislookup[i]->getRAbundVector();
366 m->openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
367 rav.print(*(filehandles[thislookup[i]->getGroup()]));
368 (*(filehandles[thislookup[i]->getGroup()])).close();
371 //create a map from groupName to each sharedrabund
372 map<string, SharedRAbundVector*> myMap;
373 map<string, SharedRAbundVector*>::iterator myIt;
375 for (int i = 0; i < thislookup.size(); i++) {
376 myMap[thislookup[i]->getGroup()] = thislookup[i];
379 globaldata->Groups.clear();
381 //loop through ordered list and print the rabund
382 for (int i = 0; i < order.size(); i++) {
383 myIt = myMap.find(order[i]);
385 if(myIt != myMap.end()) { //we found it
386 out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
387 (myIt->second)->print(out);
389 globaldata->Groups.push_back((myIt->second)->getGroup());
391 RAbundVector rav = (myIt->second)->getRAbundVector();
392 m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()]));
393 rav.print(*(filehandles[(myIt->second)->getGroup()]));
394 (*(filehandles[(myIt->second)->getGroup()])).close();
396 m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
403 catch(exception& e) {
404 m->errorOut(e, "SharedCommand", "printSharedData");
408 //**********************************************************************************************************************
409 int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
412 vector<SharedRAbundVector*> newLookup;
413 for (int i = 0; i < thislookup.size(); i++) {
414 SharedRAbundVector* temp = new SharedRAbundVector();
415 temp->setLabel(thislookup[i]->getLabel());
416 temp->setGroup(thislookup[i]->getGroup());
417 newLookup.push_back(temp);
421 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
422 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
424 //look at each sharedRabund and make sure they are not all zero
426 for (int j = 0; j < thislookup.size(); j++) {
427 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
430 //if they are not all zero add this bin
432 for (int j = 0; j < thislookup.size(); j++) {
433 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
436 //else{ cout << "bin # " << i << " is all zeros" << endl; }
439 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
440 thislookup = newLookup;
445 catch(exception& e) {
446 m->errorOut(e, "SharedCommand", "eliminateZeroOTUS");
450 //**********************************************************************************************************************
451 int SharedCommand::createMisMatchFile() {
453 ofstream outMisMatch;
454 string outputMisMatchName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
456 //you have sequences in your list file that are not in your group file
457 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) {
458 outputMisMatchName += "missing.group";
459 m->mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); m->mothurOutEndLine();
461 m->openOutputFile(outputMisMatchName, outMisMatch);
463 map<string, string> listNames;
464 map<string, string>::iterator itList;
466 //go through list and if group returns "not found" output it
467 for (int i = 0; i < SharedList->getNumBins(); i++) {
468 if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; }
470 string names = SharedList->get(i);
472 while (names.find_first_of(',') != -1) {
473 string name = names.substr(0,names.find_first_of(','));
474 names = names.substr(names.find_first_of(',')+1, names.length());
475 string group = groupMap->getGroup(name);
477 if(group == "not found") { outMisMatch << name << endl; }
479 itList = listNames.find(name);
480 if (itList != listNames.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
481 else { listNames[name] = name; }
485 string group = groupMap->getGroup(names);
486 if(group == "not found") { outMisMatch << names << endl; }
488 itList = listNames.find(names);
489 if (itList != listNames.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
490 else { listNames[names] = names; }
497 }else {//you have sequences in your group file that are not in you list file
499 outputMisMatchName += "missing.name";
500 m->mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); m->mothurOutEndLine();
502 map<string, string> namesInList;
503 map<string, string>::iterator itList;
505 //go through listfile and get names
506 for (int i = 0; i < SharedList->getNumBins(); i++) {
507 if (m->control_pressed) { return 0; }
510 string names = SharedList->get(i);
512 while (names.find_first_of(',') != -1) {
513 string name = names.substr(0,names.find_first_of(','));
514 names = names.substr(names.find_first_of(',')+1, names.length());
516 itList = namesInList.find(name);
517 if (itList != namesInList.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
519 namesInList[name] = name;
523 itList = namesInList.find(names);
524 if (itList != namesInList.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
527 namesInList[names] = names;
530 //get names of sequences in groupfile
531 vector<string> seqNames = groupMap->getNamesSeqs();
533 map<string, string>::iterator itMatch;
535 m->openOutputFile(outputMisMatchName, outMisMatch);
537 //loop through names in seqNames and if they aren't in namesIn list output them
538 for (int i = 0; i < seqNames.size(); i++) {
539 if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; }
541 itMatch = namesInList.find(seqNames[i]);
543 if (itMatch == namesInList.end()) {
545 outMisMatch << seqNames[i] << endl;
553 catch(exception& e) {
554 m->errorOut(e, "SharedCommand", "createMisMatchFile");
559 //**********************************************************************************************************************
561 SharedCommand::~SharedCommand(){
566 //**********************************************************************************************************************
567 int SharedCommand::readOrderFile() {
573 m->openInputFile(globaldata->getOrderGroupFile(), in);
577 in >> thisGroup; m->gobble(in);
579 order.push_back(thisGroup);
581 if (m->control_pressed) { order.clear(); break; }
587 catch(exception& e) {
588 m->errorOut(e, "SharedCommand", "readOrderFile");
592 //**********************************************************************************************************************
594 bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
596 for (int i = 0; i < groups.size(); i++) {
597 if (groupname == groups[i]) { return true; }
602 catch(exception& e) {
603 m->errorOut(e, "SharedCommand", "isValidGroup");
607 /************************************************************/