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);
25 errorOut(e, "SharedCommand", "SharedCommand");
29 //**********************************************************************************************************************
31 int SharedCommand::execute(){
34 mothurOut("creating sharedfile..."); mothurOutEndLine();
37 string errorOff = "no error";
40 read = new ReadOTUFile(globaldata->inputFileName);
41 read->read(&*globaldata);
43 input = globaldata->ginput;
44 SharedList = globaldata->gSharedList;
45 string lastLabel = SharedList->getLabel();
46 vector<SharedRAbundVector*> lookup;
48 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
49 set<string> processedLabels;
50 set<string> userLabels = globaldata->labels;
51 set<int> userLines = globaldata->lines;
54 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
57 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
58 lookup = SharedList->getSharedRAbundVector();
59 printSharedData(lookup); //prints info to the .shared file
60 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
62 processedLabels.insert(SharedList->getLabel());
63 userLabels.erase(SharedList->getLabel());
64 userLines.erase(count);
67 if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
69 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
71 lookup = SharedList->getSharedRAbundVector();
72 printSharedData(lookup); //prints info to the .shared file
73 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
75 processedLabels.insert(SharedList->getLabel());
76 userLabels.erase(SharedList->getLabel());
80 lastLabel = SharedList->getLabel();
83 SharedList = input->getSharedListVector(); //get new list vector to process
88 //output error messages about any remaining user labels
89 set<string>::iterator it;
90 bool needToRun = false;
91 for (it = userLabels.begin(); it != userLabels.end(); it++) {
92 if (processedLabels.count(lastLabel) != 1) {
97 //run last line if you need to
98 if (needToRun == true) {
100 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
102 lookup = SharedList->getSharedRAbundVector();
103 printSharedData(lookup); //prints info to the .shared file
104 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
108 globaldata->gSharedList = NULL;
113 mothurOut("complete."); mothurOutEndLine();
116 catch(exception& e) {
117 errorOut(e, "SharedCommand", "execute");
121 //**********************************************************************************************************************
122 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
125 //initialize bin values
126 for (int i = 0; i < thislookup.size(); i++) {
127 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
128 thislookup[i]->print(out);
132 catch(exception& e) {
133 errorOut(e, "SharedCommand", "printSharedData");
138 //**********************************************************************************************************************
140 SharedCommand::~SharedCommand(){
146 //**********************************************************************************************************************