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(){
55 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();
73 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
77 globaldata->gSharedList = NULL;
82 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
83 set<string> processedLabels;
84 set<string> userLabels = globaldata->labels;
85 set<int> userLines = globaldata->lines;
88 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
91 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
93 lookup = SharedList->getSharedRAbundVector();
94 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
96 printSharedData(lookup); //prints info to the .shared file
97 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
99 processedLabels.insert(SharedList->getLabel());
100 userLabels.erase(SharedList->getLabel());
101 userLines.erase(count);
104 if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
106 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
108 lookup = SharedList->getSharedRAbundVector();
109 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
111 printSharedData(lookup); //prints info to the .shared file
112 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
114 processedLabels.insert(SharedList->getLabel());
115 userLabels.erase(SharedList->getLabel());
119 lastLabel = SharedList->getLabel();
122 SharedList = input->getSharedListVector(); //get new list vector to process
127 //output error messages about any remaining user labels
128 set<string>::iterator it;
129 bool needToRun = false;
130 for (it = userLabels.begin(); it != userLabels.end(); it++) {
131 if (processedLabels.count(lastLabel) != 1) {
136 //run last line if you need to
137 if (needToRun == true) {
138 if (SharedList != NULL) { delete SharedList; }
139 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
141 lookup = SharedList->getSharedRAbundVector();
142 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
144 printSharedData(lookup); //prints info to the .shared file
145 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
149 globaldata->gSharedList = NULL;
153 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
160 catch(exception& e) {
161 errorOut(e, "SharedCommand", "execute");
165 //**********************************************************************************************************************
166 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
169 //initialize bin values
170 for (int i = 0; i < thislookup.size(); i++) {
171 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
172 thislookup[i]->print(out);
174 RAbundVector rav = thislookup[i]->getRAbundVector();
175 openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
176 rav.print(*(filehandles[thislookup[i]->getGroup()]));
177 (*(filehandles[thislookup[i]->getGroup()])).close();
181 catch(exception& e) {
182 errorOut(e, "SharedCommand", "printSharedData");
186 //**********************************************************************************************************************
187 void SharedCommand::createMisMatchFile() {
189 ofstream outMisMatch;
190 string outputMisMatchName = getRootName(globaldata->inputFileName);
192 //you have sequences in your list file that are not in your group file
193 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) {
194 outputMisMatchName += "missing.group";
195 mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
197 openOutputFile(outputMisMatchName, outMisMatch);
199 //go through list and if group returns "not found" output it
200 for (int i = 0; i < SharedList->getNumBins(); i++) {
202 string names = SharedList->get(i);
204 while (names.find_first_of(',') != -1) {
205 string name = names.substr(0,names.find_first_of(','));
206 names = names.substr(names.find_first_of(',')+1, names.length());
207 string group = groupMap->getGroup(name);
209 if(group == "not found") { outMisMatch << name << endl; }
213 string group = groupMap->getGroup(names);
214 if(group == "not found") { outMisMatch << names << endl; }
220 }else {//you have sequences in your group file that are not in you list file
222 outputMisMatchName += "missing.name";
223 mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
225 map<string, string> namesInList;
227 //go through listfile and get names
228 for (int i = 0; i < SharedList->getNumSeqs(); i++) {
230 string names = SharedList->get(i);
232 while (names.find_first_of(',') != -1) {
233 string name = names.substr(0,names.find_first_of(','));
234 names = names.substr(names.find_first_of(',')+1, names.length());
236 namesInList[name] = name;
240 namesInList[names] = names;
243 //get names of sequences in groupfile
244 vector<string> seqNames = groupMap->getNamesSeqs();
246 map<string, string>::iterator itMatch;
248 openOutputFile(outputMisMatchName, outMisMatch);
250 //loop through names in seqNames and if they aren't in namesIn list output them
251 for (int i = 0; i < seqNames.size(); i++) {
253 itMatch = namesInList.find(seqNames[i]);
255 if (itMatch == namesInList.end()) {
257 outMisMatch << seqNames[i] << endl;
264 catch(exception& e) {
265 errorOut(e, "SharedCommand", "createMisMatchFile");
270 //**********************************************************************************************************************
272 SharedCommand::~SharedCommand(){
278 //**********************************************************************************************************************