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";
57 read = new ReadOTUFile(globaldata->inputFileName);
58 read->read(&*globaldata);
61 input = globaldata->ginput;
62 SharedList = globaldata->gSharedList;
63 string lastLabel = SharedList->getLabel();
64 vector<SharedRAbundVector*> lookup;
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();
72 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
76 globaldata->gSharedList = NULL;
81 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
82 set<string> processedLabels;
83 set<string> userLabels = globaldata->labels;
86 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
89 if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
91 lookup = SharedList->getSharedRAbundVector();
92 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
94 printSharedData(lookup); //prints info to the .shared file
95 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
97 processedLabels.insert(SharedList->getLabel());
98 userLabels.erase(SharedList->getLabel());
101 if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
103 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
105 lookup = SharedList->getSharedRAbundVector();
106 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
108 printSharedData(lookup); //prints info to the .shared file
109 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
111 processedLabels.insert(SharedList->getLabel());
112 userLabels.erase(SharedList->getLabel());
116 lastLabel = SharedList->getLabel();
119 SharedList = input->getSharedListVector(); //get new list vector to process
122 //output error messages about any remaining user labels
123 set<string>::iterator it;
124 bool needToRun = false;
125 for (it = userLabels.begin(); it != userLabels.end(); it++) {
126 if (processedLabels.count(lastLabel) != 1) {
131 //run last label if you need to
132 if (needToRun == true) {
133 if (SharedList != NULL) { delete SharedList; }
134 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
136 lookup = SharedList->getSharedRAbundVector();
137 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
139 printSharedData(lookup); //prints info to the .shared file
140 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
144 globaldata->gSharedList = NULL;
148 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
155 catch(exception& e) {
156 errorOut(e, "SharedCommand", "execute");
160 //**********************************************************************************************************************
161 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
164 //initialize bin values
165 for (int i = 0; i < thislookup.size(); i++) {
166 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
167 thislookup[i]->print(out);
169 RAbundVector rav = thislookup[i]->getRAbundVector();
170 openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
171 rav.print(*(filehandles[thislookup[i]->getGroup()]));
172 (*(filehandles[thislookup[i]->getGroup()])).close();
176 catch(exception& e) {
177 errorOut(e, "SharedCommand", "printSharedData");
181 //**********************************************************************************************************************
182 void SharedCommand::createMisMatchFile() {
184 ofstream outMisMatch;
185 string outputMisMatchName = getRootName(globaldata->inputFileName);
187 //you have sequences in your list file that are not in your group file
188 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) {
189 outputMisMatchName += "missing.group";
190 mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
192 openOutputFile(outputMisMatchName, outMisMatch);
194 //go through list and if group returns "not found" output it
195 for (int i = 0; i < SharedList->getNumBins(); i++) {
197 string names = SharedList->get(i);
199 while (names.find_first_of(',') != -1) {
200 string name = names.substr(0,names.find_first_of(','));
201 names = names.substr(names.find_first_of(',')+1, names.length());
202 string group = groupMap->getGroup(name);
204 if(group == "not found") { outMisMatch << name << endl; }
208 string group = groupMap->getGroup(names);
209 if(group == "not found") { outMisMatch << names << endl; }
215 }else {//you have sequences in your group file that are not in you list file
217 outputMisMatchName += "missing.name";
218 mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
220 map<string, string> namesInList;
222 //go through listfile and get names
223 for (int i = 0; i < SharedList->getNumSeqs(); i++) {
225 string names = SharedList->get(i);
227 while (names.find_first_of(',') != -1) {
228 string name = names.substr(0,names.find_first_of(','));
229 names = names.substr(names.find_first_of(',')+1, names.length());
231 namesInList[name] = name;
235 namesInList[names] = names;
238 //get names of sequences in groupfile
239 vector<string> seqNames = groupMap->getNamesSeqs();
241 map<string, string>::iterator itMatch;
243 openOutputFile(outputMisMatchName, outMisMatch);
245 //loop through names in seqNames and if they aren't in namesIn list output them
246 for (int i = 0; i < seqNames.size(); i++) {
248 itMatch = namesInList.find(seqNames[i]);
250 if (itMatch == namesInList.end()) {
252 outMisMatch << seqNames[i] << endl;
259 catch(exception& e) {
260 errorOut(e, "SharedCommand", "createMisMatchFile");
265 //**********************************************************************************************************************
267 SharedCommand::~SharedCommand(){
273 //**********************************************************************************************************************