5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedcommand.h"
12 //**********************************************************************************************************************
14 SharedCommand::SharedCommand(){
16 globaldata = GlobalData::getInstance();
18 //getting output filename
19 filename = globaldata->inputFileName;
20 filename = getRootName(filename);
21 filename = filename + "shared";
22 openOutputFile(filename, out);
24 groupMap = globaldata->gGroupmap;
26 //if hte user has not specified any groups then use them all
27 if (globaldata->Groups.size() == 0) {
28 groups = groupMap->namesOfGroups;
29 }else{ //they have specified groups
30 groups = globaldata->Groups;
33 //fill filehandles with neccessary ofstreams
36 for (i=0; i<groups.size(); i++) {
38 filehandles[groups[i]] = temp;
42 fileroot = getRootName(globaldata->getListFile());
44 //clears file before we start to write to it below
45 for (int i=0; i<groups.size(); i++) {
46 remove((fileroot + groups[i] + ".rabund").c_str());
51 errorOut(e, "SharedCommand", "SharedCommand");
55 //**********************************************************************************************************************
57 int SharedCommand::execute(){
61 string errorOff = "no error";
65 read = new ReadOTUFile(globaldata->inputFileName);
66 read->read(&*globaldata);
69 input = globaldata->ginput;
70 SharedList = globaldata->gSharedList;
71 string lastLabel = SharedList->getLabel();
72 vector<SharedRAbundVector*> lookup;
74 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
75 mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); mothurOutEndLine();
78 remove(filename.c_str()); //remove blank shared file you made
83 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
87 globaldata->gSharedList = NULL;
92 //if user has specified groups make new groupfile for them
93 if (globaldata->Groups.size() != 0) { //make new group file
95 for (int i = 0; i < globaldata->Groups.size(); i++) {
96 groups += globaldata->Groups[i] + ".";
99 string newGroupFile = getRootName(globaldata->inputFileName) + groups + "groups";
101 openOutputFile(newGroupFile, outGroups);
103 vector<string> names = groupMap->getNamesSeqs();
105 for (int i = 0; i < names.size(); i++) {
106 groupName = groupMap->getGroup(names[i]);
107 if (isValidGroup(groupName, globaldata->Groups)) {
108 outGroups << names[i] << '\t' << groupName << endl;
114 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
115 set<string> processedLabels;
116 set<string> userLabels = globaldata->labels;
118 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
120 if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
122 lookup = SharedList->getSharedRAbundVector();
123 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
125 printSharedData(lookup); //prints info to the .shared file
126 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
128 processedLabels.insert(SharedList->getLabel());
129 userLabels.erase(SharedList->getLabel());
132 if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
133 string saveLabel = SharedList->getLabel();
136 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
138 lookup = SharedList->getSharedRAbundVector();
139 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
141 printSharedData(lookup); //prints info to the .shared file
142 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
144 processedLabels.insert(SharedList->getLabel());
145 userLabels.erase(SharedList->getLabel());
147 //restore real lastlabel to save below
148 SharedList->setLabel(saveLabel);
152 lastLabel = SharedList->getLabel();
155 SharedList = input->getSharedListVector(); //get new list vector to process
158 //output error messages about any remaining user labels
159 set<string>::iterator it;
160 bool needToRun = false;
161 for (it = userLabels.begin(); it != userLabels.end(); it++) {
162 if (processedLabels.count(lastLabel) != 1) {
167 //run last label if you need to
168 if (needToRun == true) {
169 if (SharedList != NULL) { delete SharedList; }
170 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
172 lookup = SharedList->getSharedRAbundVector();
173 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
175 printSharedData(lookup); //prints info to the .shared file
176 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
180 globaldata->gSharedList = NULL;
184 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
191 catch(exception& e) {
192 errorOut(e, "SharedCommand", "execute");
196 //**********************************************************************************************************************
197 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
200 //initialize bin values
201 for (int i = 0; i < thislookup.size(); i++) {
202 //cout << "in printData " << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << endl;
203 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
204 thislookup[i]->print(out);
206 RAbundVector rav = thislookup[i]->getRAbundVector();
207 openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
208 rav.print(*(filehandles[thislookup[i]->getGroup()]));
209 (*(filehandles[thislookup[i]->getGroup()])).close();
213 catch(exception& e) {
214 errorOut(e, "SharedCommand", "printSharedData");
218 //**********************************************************************************************************************
219 void SharedCommand::createMisMatchFile() {
221 ofstream outMisMatch;
222 string outputMisMatchName = getRootName(globaldata->inputFileName);
224 //you have sequences in your list file that are not in your group file
225 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) {
226 outputMisMatchName += "missing.group";
227 mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
229 openOutputFile(outputMisMatchName, outMisMatch);
231 //go through list and if group returns "not found" output it
232 for (int i = 0; i < SharedList->getNumBins(); i++) {
234 string names = SharedList->get(i);
236 while (names.find_first_of(',') != -1) {
237 string name = names.substr(0,names.find_first_of(','));
238 names = names.substr(names.find_first_of(',')+1, names.length());
239 string group = groupMap->getGroup(name);
241 if(group == "not found") { outMisMatch << name << endl; }
245 string group = groupMap->getGroup(names);
246 if(group == "not found") { outMisMatch << names << endl; }
252 }else {//you have sequences in your group file that are not in you list file
254 outputMisMatchName += "missing.name";
255 mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
257 map<string, string> namesInList;
259 //go through listfile and get names
260 for (int i = 0; i < SharedList->getNumBins(); i++) {
262 string names = SharedList->get(i);
264 while (names.find_first_of(',') != -1) {
265 string name = names.substr(0,names.find_first_of(','));
266 names = names.substr(names.find_first_of(',')+1, names.length());
268 namesInList[name] = name;
272 namesInList[names] = names;
275 //get names of sequences in groupfile
276 vector<string> seqNames = groupMap->getNamesSeqs();
278 map<string, string>::iterator itMatch;
280 openOutputFile(outputMisMatchName, outMisMatch);
282 //loop through names in seqNames and if they aren't in namesIn list output them
283 for (int i = 0; i < seqNames.size(); i++) {
285 itMatch = namesInList.find(seqNames[i]);
287 if (itMatch == namesInList.end()) {
289 outMisMatch << seqNames[i] << endl;
296 catch(exception& e) {
297 errorOut(e, "SharedCommand", "createMisMatchFile");
302 //**********************************************************************************************************************
304 SharedCommand::~SharedCommand(){
310 //**********************************************************************************************************************
312 bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
314 for (int i = 0; i < groups.size(); i++) {
315 if (groupname == groups[i]) { return true; }
320 catch(exception& e) {
321 errorOut(e, "SharedCommand", "isValidGroup");
325 /************************************************************/