+ }else {
+ if (!m->printedSharedHeaders) { lookup[0]->printHeaders(out); }
+ printSharedData(lookup, out); //prints info to the .shared file
+ }
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+
+ processedLabels.insert(SharedList->getLabel());
+ userLabels.erase(SharedList->getLabel());
+ }
+
+ if ((m->anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
+ string saveLabel = SharedList->getLabel();
+
+ delete SharedList;
+ SharedList = input.getSharedListVector(lastLabel); //get new list vector to process
+
+ lookup = SharedList->getSharedRAbundVector();
+ m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
+
+ if (m->control_pressed) {
+ delete SharedList; if (groupMap != NULL) { delete groupMap; } if (countTable != NULL) { delete countTable; }
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+ for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
+ if (!pickedGroups) { out.close(); m->mothurRemove(filename); }
+ for (int i=0; i<Groups.size(); i++) { variables["[group]"] = Groups[i];
+ string rabundFIleName = getOutputFileName("rabund",variables);
+ m->mothurRemove(rabundFIleName); }
+ return 0;
+ }
+
+ //if picked groups must split the shared file by label
+ if (pickedGroups) {
+ string filename = listfile;
+ if (outputDir == "") { outputDir += m->hasPath(filename); }
+
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(filename));
+ variables["[distance]"] = lookup[0]->getLabel();
+ filename = getOutputFileName("shared",variables);
+ outputNames.push_back(filename); outputTypes["shared"].push_back(filename);
+ ofstream out2;
+ m->openOutputFile(filename, out2);
+
+ vector<string> savedLabels = m->currentSharedBinLabels;
+ eliminateZeroOTUS(lookup);
+ lookup[0]->printHeaders(out2);
+ printSharedData(lookup, out2);
+ out2.close();
+ m->currentSharedBinLabels = savedLabels; //restore old labels
+
+ }else {
+ if (!m->printedSharedHeaders) { lookup[0]->printHeaders(out); }
+ printSharedData(lookup, out); //prints info to the .shared file
+ }
+
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+
+ processedLabels.insert(SharedList->getLabel());
+ userLabels.erase(SharedList->getLabel());
+
+ //restore real lastlabel to save below
+ SharedList->setLabel(saveLabel);
+ }
+
+
+ lastLabel = SharedList->getLabel();
+
+ delete SharedList;
+ SharedList = input.getSharedListVector(); //get new list vector to process
+ }
+
+ //output error messages about any remaining user labels
+ set<string>::iterator it;
+ bool needToRun = false;
+ for (it = userLabels.begin(); it != userLabels.end(); it++) {
+ if (processedLabels.count(lastLabel) != 1) {
+ needToRun = true;
+ }
+ }
+
+ //run last label if you need to
+ if (needToRun == true) {
+ if (SharedList != NULL) { delete SharedList; }
+ SharedList = input.getSharedListVector(lastLabel); //get new list vector to process
+
+ lookup = SharedList->getSharedRAbundVector();
+ m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
+
+ if (m->control_pressed) {
+ if (groupMap != NULL) { delete groupMap; } if (countTable != NULL) { delete countTable; }
+ for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
+ if (!pickedGroups) { out.close(); m->mothurRemove(filename); }
+ for (int i=0; i<Groups.size(); i++) { variables["[group]"] = Groups[i];
+ string rabundFIleName = getOutputFileName("rabund",variables);
+ m->mothurRemove(rabundFIleName); }
+ return 0;
+ }
+
+ //if picked groups must split the shared file by label
+ if (pickedGroups) {
+ string filename = listfile;
+ if (outputDir == "") { outputDir += m->hasPath(filename); }
+
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(filename));
+ variables["[distance]"] = lookup[0]->getLabel();
+ filename = getOutputFileName("shared",variables);
+ outputNames.push_back(filename); outputTypes["shared"].push_back(filename);
+ ofstream out2;
+ m->openOutputFile(filename, out2);
+
+ vector<string> savedLabels = m->currentSharedBinLabels;
+ eliminateZeroOTUS(lookup);
+ lookup[0]->printHeaders(out2);
+ printSharedData(lookup, out2);
+ out2.close();
+ m->currentSharedBinLabels = savedLabels; //restore old labels
+
+ }else {
+ if (!m->printedSharedHeaders) { lookup[0]->printHeaders(out); }
+ printSharedData(lookup, out); //prints info to the .shared file
+ }
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+ delete SharedList;
+ }
+
+ if (!pickedGroups) { out.close(); }
+
+ for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
+ delete it3->second;
+ }
+
+ if (groupMap != NULL) { delete groupMap; } if (countTable != NULL) { delete countTable; }
+
+ if (m->control_pressed) {
+ if (!pickedGroups) { m->mothurRemove(filename); }
+ for (int i=0; i<Groups.size(); i++) { variables["[group]"] = Groups[i];
+ string rabundFIleName = getOutputFileName("rabund",variables);
+ m->mothurRemove(rabundFIleName); }
+ return 0;
+ }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SharedCommand", "createSharedFromListGroup");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup, ofstream& out) {
+ try {
+
+ if (order.size() == 0) { //user has not specified an order so do aplabetically
+ sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
+
+ m->clearGroups();
+ vector<string> Groups;
+
+ //initialize bin values
+ for (int i = 0; i < thislookup.size(); i++) {
+ out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
+ thislookup[i]->print(out);
+
+ Groups.push_back(thislookup[i]->getGroup());
+
+ RAbundVector rav = thislookup[i]->getRAbundVector();
+ map<string, string> variables;
+ variables["[filename]"] = fileroot;
+ variables["[group]"] = thislookup[i]->getGroup();
+ m->openOutputFileAppend(getOutputFileName("rabund",variables), *(filehandles[thislookup[i]->getGroup()]));
+ rav.print(*(filehandles[thislookup[i]->getGroup()]));
+ (*(filehandles[thislookup[i]->getGroup()])).close();
+ }
+ m->setGroups(Groups);
+ }else{
+ //create a map from groupName to each sharedrabund
+ map<string, SharedRAbundVector*> myMap;
+ map<string, SharedRAbundVector*>::iterator myIt;
+
+ for (int i = 0; i < thislookup.size(); i++) {
+ myMap[thislookup[i]->getGroup()] = thislookup[i];
+ }
+
+ m->clearGroups();
+ vector<string> Groups;
+
+ //loop through ordered list and print the rabund
+ for (int i = 0; i < order.size(); i++) {
+ myIt = myMap.find(order[i]);
+
+ if(myIt != myMap.end()) { //we found it
+ out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
+ (myIt->second)->print(out);
+
+ Groups.push_back((myIt->second)->getGroup());
+
+ RAbundVector rav = (myIt->second)->getRAbundVector();
+ map<string, string> variables;
+ variables["[filename]"] = fileroot;
+ variables["[group]"] = (myIt->second)->getGroup();
+ m->openOutputFileAppend(getOutputFileName("rabund",variables), *(filehandles[(myIt->second)->getGroup()]));
+ rav.print(*(filehandles[(myIt->second)->getGroup()]));
+ (*(filehandles[(myIt->second)->getGroup()])).close();
+ }else{
+ m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
+ }
+ }
+
+ m->setGroups(Groups);
+
+ }
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SharedCommand", "printSharedData");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SharedCommand::ListGroupSameSeqs(vector<string>& groupMapsSeqs, SharedListVector* SharedList) {
+ try {
+ int error = 0;
+
+ set<string> groupNamesSeqs;
+ for(int i = 0; i < groupMapsSeqs.size(); i++) {
+ groupNamesSeqs.insert(groupMapsSeqs[i]);
+ }
+
+ //go through list and if group returns "not found" output it
+ for (int i = 0; i < SharedList->getNumBins(); i++) {
+ if (m->control_pressed) { return 0; }
+
+ string names = SharedList->get(i);
+
+ vector<string> listNames;
+ m->splitAtComma(names, listNames);
+
+ for (int j = 0; j < listNames.size(); j++) {
+ int num = groupNamesSeqs.count(listNames[j]);
+
+ if (num == 0) {
+ error = 1;
+ if (groupfile != "") {
+ m->mothurOut("[ERROR]: " + listNames[j] + " is in your listfile and not in your groupfile. Please correct."); m->mothurOutEndLine(); }
+ else{ m->mothurOut("[ERROR]: " + listNames[j] + " is in your listfile and not in your count file. Please correct."); m->mothurOutEndLine(); }
+ }else { groupNamesSeqs.erase(listNames[j]); }
+ }
+ }
+
+ for (set<string>::iterator itGroupSet = groupNamesSeqs.begin(); itGroupSet != groupNamesSeqs.end(); itGroupSet++) {
+ error = 1;
+ m->mothurOut("[ERROR]: " + (*itGroupSet) + " is in your groupfile and not your listfile. Please correct."); m->mothurOutEndLine();
+ }
+
+ return error;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SharedCommand", "ListGroupSameSeqs");
+ exit(1);
+ }
+}