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);
61 input = globaldata->ginput;
62 SharedList = globaldata->gSharedList;
63 string lastLabel = SharedList->getLabel();
64 vector<SharedRAbundVector*> lookup;
66 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
67 set<string> processedLabels;
68 set<string> userLabels = globaldata->labels;
69 set<int> userLines = globaldata->lines;
72 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
75 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
77 lookup = SharedList->getSharedRAbundVector();
78 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
80 printSharedData(lookup); //prints info to the .shared file
81 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
83 processedLabels.insert(SharedList->getLabel());
84 userLabels.erase(SharedList->getLabel());
85 userLines.erase(count);
88 if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
90 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
92 lookup = SharedList->getSharedRAbundVector();
93 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
95 printSharedData(lookup); //prints info to the .shared file
96 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
98 processedLabels.insert(SharedList->getLabel());
99 userLabels.erase(SharedList->getLabel());
103 lastLabel = SharedList->getLabel();
106 SharedList = input->getSharedListVector(); //get new list vector to process
111 //output error messages about any remaining user labels
112 set<string>::iterator it;
113 bool needToRun = false;
114 for (it = userLabels.begin(); it != userLabels.end(); it++) {
115 if (processedLabels.count(lastLabel) != 1) {
120 //run last line if you need to
121 if (needToRun == true) {
123 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
125 lookup = SharedList->getSharedRAbundVector();
126 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
128 printSharedData(lookup); //prints info to the .shared file
129 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
133 globaldata->gSharedList = NULL;
138 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
145 catch(exception& e) {
146 errorOut(e, "SharedCommand", "execute");
150 //**********************************************************************************************************************
151 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
154 //initialize bin values
155 for (int i = 0; i < thislookup.size(); i++) {
156 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
157 thislookup[i]->print(out);
159 RAbundVector rav = thislookup[i]->getRAbundVector();
160 openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
161 rav.print(*(filehandles[thislookup[i]->getGroup()]));
162 (*(filehandles[thislookup[i]->getGroup()])).close();
166 catch(exception& e) {
167 errorOut(e, "SharedCommand", "printSharedData");
172 //**********************************************************************************************************************
174 SharedCommand::~SharedCommand(){
180 //**********************************************************************************************************************