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();
71 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
75 globaldata->gSharedList = NULL;
80 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
81 set<string> processedLabels;
82 set<string> userLabels = globaldata->labels;
83 set<int> userLines = globaldata->lines;
86 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
89 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 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());
99 userLines.erase(count);
102 if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
104 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
106 lookup = SharedList->getSharedRAbundVector();
107 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
109 printSharedData(lookup); //prints info to the .shared file
110 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
112 processedLabels.insert(SharedList->getLabel());
113 userLabels.erase(SharedList->getLabel());
117 lastLabel = SharedList->getLabel();
120 SharedList = input->getSharedListVector(); //get new list vector to process
125 //output error messages about any remaining user labels
126 set<string>::iterator it;
127 bool needToRun = false;
128 for (it = userLabels.begin(); it != userLabels.end(); it++) {
129 if (processedLabels.count(lastLabel) != 1) {
134 //run last line if you need to
135 if (needToRun == true) {
136 if (SharedList != NULL) { delete SharedList; }
137 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
139 lookup = SharedList->getSharedRAbundVector();
140 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
142 printSharedData(lookup); //prints info to the .shared file
143 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
147 globaldata->gSharedList = NULL;
151 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
158 catch(exception& e) {
159 errorOut(e, "SharedCommand", "execute");
163 //**********************************************************************************************************************
164 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
167 //initialize bin values
168 for (int i = 0; i < thislookup.size(); i++) {
169 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
170 thislookup[i]->print(out);
172 RAbundVector rav = thislookup[i]->getRAbundVector();
173 openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
174 rav.print(*(filehandles[thislookup[i]->getGroup()]));
175 (*(filehandles[thislookup[i]->getGroup()])).close();
179 catch(exception& e) {
180 errorOut(e, "SharedCommand", "printSharedData");
185 //**********************************************************************************************************************
187 SharedCommand::~SharedCommand(){
193 //**********************************************************************************************************************