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 //fill filehandles with neccessary ofstreams
29 for (i=0; i<groupMap->getNumGroups(); i++) {
31 filehandles[groupMap->namesOfGroups[i]] = temp;
35 fileroot = getRootName(globaldata->getListFile());
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());
44 errorOut(e, "SharedCommand", "SharedCommand");
48 //**********************************************************************************************************************
50 int SharedCommand::execute(){
54 string errorOff = "no error";
58 read = new ReadOTUFile(globaldata->inputFileName);
59 read->read(&*globaldata);
62 input = globaldata->ginput;
63 SharedList = globaldata->gSharedList;
64 string lastLabel = SharedList->getLabel();
65 vector<SharedRAbundVector*> lookup;
67 if (SharedList->getNumSeqs() != groupMap->getNumSeqs()) {
68 mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); mothurOutEndLine();
71 remove(filename.c_str()); //remove blank shared file you made
76 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
80 globaldata->gSharedList = NULL;
85 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
86 set<string> processedLabels;
87 set<string> userLabels = globaldata->labels;
89 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
92 if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
94 lookup = SharedList->getSharedRAbundVector();
95 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
97 printSharedData(lookup); //prints info to the .shared file
98 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
100 processedLabels.insert(SharedList->getLabel());
101 userLabels.erase(SharedList->getLabel());
104 if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
105 string saveLabel = SharedList->getLabel();
108 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
110 lookup = SharedList->getSharedRAbundVector();
111 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
113 printSharedData(lookup); //prints info to the .shared file
114 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
116 processedLabels.insert(SharedList->getLabel());
117 userLabels.erase(SharedList->getLabel());
119 //restore real lastlabel to save below
120 SharedList->setLabel(saveLabel);
124 lastLabel = SharedList->getLabel();
127 SharedList = input->getSharedListVector(); //get new list vector to process
130 //output error messages about any remaining user labels
131 set<string>::iterator it;
132 bool needToRun = false;
133 for (it = userLabels.begin(); it != userLabels.end(); it++) {
134 if (processedLabels.count(lastLabel) != 1) {
139 //run last label if you need to
140 if (needToRun == true) {
141 if (SharedList != NULL) { delete SharedList; }
142 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
144 lookup = SharedList->getSharedRAbundVector();
145 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
147 printSharedData(lookup); //prints info to the .shared file
148 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
152 globaldata->gSharedList = NULL;
156 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
163 catch(exception& e) {
164 errorOut(e, "SharedCommand", "execute");
168 //**********************************************************************************************************************
169 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
172 //initialize bin values
173 for (int i = 0; i < thislookup.size(); i++) {
174 //cout << "in printData " << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << endl;
175 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
176 thislookup[i]->print(out);
178 RAbundVector rav = thislookup[i]->getRAbundVector();
179 openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
180 rav.print(*(filehandles[thislookup[i]->getGroup()]));
181 (*(filehandles[thislookup[i]->getGroup()])).close();
185 catch(exception& e) {
186 errorOut(e, "SharedCommand", "printSharedData");
190 //**********************************************************************************************************************
191 void SharedCommand::createMisMatchFile() {
193 ofstream outMisMatch;
194 string outputMisMatchName = getRootName(globaldata->inputFileName);
196 //you have sequences in your list file that are not in your group file
197 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) {
198 outputMisMatchName += "missing.group";
199 mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
201 openOutputFile(outputMisMatchName, outMisMatch);
203 //go through list and if group returns "not found" output it
204 for (int i = 0; i < SharedList->getNumBins(); i++) {
206 string names = SharedList->get(i);
208 while (names.find_first_of(',') != -1) {
209 string name = names.substr(0,names.find_first_of(','));
210 names = names.substr(names.find_first_of(',')+1, names.length());
211 string group = groupMap->getGroup(name);
213 if(group == "not found") { outMisMatch << name << endl; }
217 string group = groupMap->getGroup(names);
218 if(group == "not found") { outMisMatch << names << endl; }
224 }else {//you have sequences in your group file that are not in you list file
226 outputMisMatchName += "missing.name";
227 mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
229 map<string, string> namesInList;
231 //go through listfile and get names
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());
240 namesInList[name] = name;
244 namesInList[names] = names;
247 //get names of sequences in groupfile
248 vector<string> seqNames = groupMap->getNamesSeqs();
250 map<string, string>::iterator itMatch;
252 openOutputFile(outputMisMatchName, outMisMatch);
254 //loop through names in seqNames and if they aren't in namesIn list output them
255 for (int i = 0; i < seqNames.size(); i++) {
257 itMatch = namesInList.find(seqNames[i]);
259 if (itMatch == namesInList.end()) {
261 outMisMatch << seqNames[i] << endl;
268 catch(exception& e) {
269 errorOut(e, "SharedCommand", "createMisMatchFile");
274 //**********************************************************************************************************************
276 SharedCommand::~SharedCommand(){
282 //**********************************************************************************************************************