+ bool inQuotes = false;
+ string tag = "";
+ char c = '\"';
+
+ for (int i = 0; i < line.length(); i++) {
+
+ //you want to ignore any ; until you reach the next '
+ if ((line[i] == c) && (!inQuotes)) { inQuotes = true; }
+ else if ((line[i] == c) && (inQuotes)) {
+ inQuotes= false;
+ line = line.substr(i+1);
+ return tag;
+ }
+
+ if (inQuotes) { if (line[i] != c) { tag += line[i]; } }
+ }
+
+ return tag;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SharedCommand", "getInfo");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SharedCommand::createSharedFromListGroup() {
+ try {
+
+ GroupMap* groupMap = NULL;
+ CountTable* countTable = NULL;
+ if (groupfile != "") {
+ groupMap = new GroupMap(groupfile);
+
+ int groupError = groupMap->readMap();
+ if (groupError == 1) { delete groupMap; return 0; }
+ vector<string> allGroups = groupMap->getNamesOfGroups();
+ m->setAllGroups(allGroups);
+ }else{
+ countTable = new CountTable();
+ countTable->readTable(countfile, true, false);
+ }
+
+ if (m->control_pressed) { return 0; }
+
+ pickedGroups = false;
+
+ //if hte user has not specified any groups then use them all
+ if (Groups.size() == 0) {
+ if (groupfile != "") { Groups = groupMap->getNamesOfGroups(); }
+ else { Groups = countTable->getNamesOfGroups(); }
+ m->setGroups(Groups);
+ }else { pickedGroups = true; }
+
+
+ ofstream out;
+ string filename = "";
+ if (!pickedGroups) {
+ string filename = listfile;
+ if (outputDir == "") { outputDir += m->hasPath(filename); }
+
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(filename));
+ filename = getOutputFileName("shared",variables);
+ outputNames.push_back(filename); outputTypes["shared"].push_back(filename);
+ m->openOutputFile(filename, out);
+ }
+
+ //fill filehandles with neccessary ofstreams
+ int i;
+ ofstream* temp;
+ for (i=0; i<Groups.size(); i++) {
+ temp = new ofstream;
+ filehandles[Groups[i]] = temp;
+ }
+
+ //set fileroot
+ fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
+ map<string, string> variables;
+ variables["[filename]"] = fileroot;
+ //clears file before we start to write to it below
+ for (int i=0; i<Groups.size(); i++) {
+ variables["[group]"] = Groups[i];
+ string rabundFIleName = getOutputFileName("rabund",variables);
+ m->mothurRemove(rabundFIleName);
+ outputNames.push_back(rabundFIleName);
+ outputTypes["rabund"].push_back(rabundFIleName);
+ }
+
+ string errorOff = "no error";
+
+ //if user provided an order file containing the order the shared file should be in read it
+ //if (ordergroupfile != "") { readOrderFile(); }
+
+ InputData input(listfile, "shared");
+ SharedListVector* SharedList = input.getSharedListVector();
+ string lastLabel = SharedList->getLabel();
+ vector<SharedRAbundVector*> lookup;
+
+ if (m->control_pressed) {
+ delete SharedList; if (groupMap != NULL) { delete groupMap; } if (countTable != NULL) { delete countTable; }
+ for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
+ out.close(); 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;
+ }
+
+ //sanity check
+ vector<string> namesSeqs;
+ int numGroupNames = 0;
+ if (m->groupMode == "group") { namesSeqs = groupMap->getNamesSeqs(); numGroupNames = groupMap->getNumSeqs(); }
+ else { namesSeqs = countTable->getNamesOfSeqs(); numGroupNames = countTable->getNumUniqueSeqs(); }
+ int error = ListGroupSameSeqs(namesSeqs, SharedList);
+
+ if ((!pickedGroups) && (SharedList->getNumSeqs() != numGroupNames)) { //if the user has not specified any groups and their files don't match exit with error
+ m->mothurOut("Your group file contains " + toString(numGroupNames) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine(); m->control_pressed = true;
+
+ out.close(); if (!pickedGroups) { m->mothurRemove(filename); } //remove blank shared file you made
+
+ //delete memory
+ for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
+ delete SharedList; if (groupMap != NULL) { delete groupMap; } if (countTable != NULL) { delete countTable; }
+ return 0;
+ }
+
+ if (error == 1) { m->control_pressed = true; }
+
+ //if user has specified groups make new groupfile for them
+ if ((pickedGroups) && (m->groupMode == "group")) { //make new group file
+ string groups = "";
+ if (m->getNumGroups() < 4) {
+ for (int i = 0; i < m->getNumGroups()-1; i++) {
+ groups += (m->getGroups())[i] + ".";
+ }
+ groups+=(m->getGroups())[m->getNumGroups()-1];
+ }else { groups = "merge"; }
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listfile));
+ variables["[group]"] = groups;
+ string newGroupFile = getOutputFileName("group",variables);
+ outputTypes["group"].push_back(newGroupFile);
+ outputNames.push_back(newGroupFile);
+ ofstream outGroups;
+ m->openOutputFile(newGroupFile, outGroups);
+
+ vector<string> names = groupMap->getNamesSeqs();
+ string groupName;
+ for (int i = 0; i < names.size(); i++) {
+ groupName = groupMap->getGroup(names[i]);
+ if (isValidGroup(groupName, m->getGroups())) {
+ outGroups << names[i] << '\t' << groupName << endl;
+ }
+ }
+ outGroups.close();
+ }
+
+ //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
+ set<string> processedLabels;
+ set<string> userLabels = labels;
+
+ while((SharedList != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
+ if (m->control_pressed) {
+ delete SharedList; 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(allLines == 1 || labels.count(SharedList->getLabel()) == 1){
+
+ 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());
+ }
+
+ 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; }