int SharedCommand::execute(){ try { if (abort == true) { if (calledHelp) { return 0; } return 2; } //getting output filename filename = listfile; if (outputDir == "") { outputDir += m->hasPath(filename); } filename = outputDir + m->getRootName(m->getSimpleName(filename)); filename = filename + "shared"; outputTypes["shared"].push_back(filename); m->openOutputFile(filename, out); pickedGroups = false; //if hte user has not specified any groups then use them all if (Groups.size() == 0) { Groups = groupMap->getNamesOfGroups(); m->setGroups(Groups); }else { pickedGroups = true; } //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)); //clears file before we start to write to it below for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); outputNames.push_back((fileroot + Groups[i] + ".rabund")); outputTypes["rabund"].push_back((fileroot + Groups[i] + ".rabund")); } //lookup.clear(); string errorOff = "no error"; //errorOff = ""; //if user provided an order file containing the order the shared file should be in read it if (ordergroupfile != "") { readOrderFile(); } input = new InputData(listfile, "shared"); SharedList = input->getSharedListVector(); string lastLabel = SharedList->getLabel(); vector<SharedRAbundVector*> lookup; if (m->control_pressed) { delete input; delete SharedList; delete groupMap; for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } out.close(); m->mothurRemove(filename); for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } //sanity check int error = ListGroupSameSeqs(); if ((!pickedGroups) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) { //if the user has not specified any groups and their files don't match exit with error m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine(); out.close(); m->mothurRemove(filename); //remove blank shared file you made createMisMatchFile(); //delete memory for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } delete input; delete SharedList; delete groupMap; return 0; } if (error == 1) { m->control_pressed = true; } //if user has specified groups make new groupfile for them if (pickedGroups) { //make new group file string groups = ""; if (m->getNumGroups() < 4) { for (int i = 0; i < m->getNumGroups(); i++) { groups += (m->getGroups())[i] + "."; } }else { groups = "merge"; } string newGroupFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + groups + "groups"; 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 input; delete SharedList; delete groupMap; for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } out.close(); m->mothurRemove(filename); for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } if(allLines == 1 || labels.count(SharedList->getLabel()) == 1){ lookup = SharedList->getSharedRAbundVector(); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); if (pickedGroups) { //check for otus with no seqs in them eliminateZeroOTUS(lookup); } if (m->control_pressed) { delete input; delete SharedList; delete groupMap; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } out.close(); m->mothurRemove(filename); for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } if (!m->printedHeaders) { lookup[0]->printHeaders(out); } printSharedData(lookup); //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 (pickedGroups) { //check for otus with no seqs in them eliminateZeroOTUS(lookup); } if (m->control_pressed) { delete input; delete SharedList; delete groupMap; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } out.close(); m->mothurRemove(filename); for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } if (!m->printedHeaders) { lookup[0]->printHeaders(out); } printSharedData(lookup); //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 (pickedGroups) { //check for otus with no seqs in them eliminateZeroOTUS(lookup); } if (m->control_pressed) { delete input; delete groupMap; for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } out.close(); m->mothurRemove(filename); for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } if (!m->printedHeaders) { lookup[0]->printHeaders(out); } printSharedData(lookup); //prints info to the .shared file for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } delete SharedList; } out.close(); for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } delete input; delete groupMap; if (m->control_pressed) { m->mothurRemove(filename); for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } //set rabund file as new current rabundfile string current = ""; itTypes = outputTypes.find("rabund"); if (itTypes != outputTypes.end()) { if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); } } itTypes = outputTypes.find("shared"); if (itTypes != outputTypes.end()) { if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); } } itTypes = outputTypes.find("group"); if (itTypes != outputTypes.end()) { if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); } } m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } m->mothurOut(filename); m->mothurOutEndLine(); m->mothurOutEndLine(); return 0; } catch(exception& e) { m->errorOut(e, "SharedCommand", "execute"); exit(1); } }
int MergeGroupsCommand::process(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){ try { vector<string> setNames = designMap->getCategory(); if (method == "average") { //create sharedRabundFloatVectors vector<SharedRAbundFloatVector*> temp = thisLookUp[0]->getSharedRAbundFloatVectors(thisLookUp); //follow code below map<string, SharedRAbundFloatVector> merged; map<string, SharedRAbundFloatVector>::iterator it; for (int i = 0; i < setNames.size(); i++) { SharedRAbundFloatVector myLookup(thisLookUp[0]->getNumBins()); myLookup.setLabel(thisLookUp[0]->getLabel()); merged[setNames[i]] = myLookup; } map<string, vector<int> > clearGroupAbunds; map<string, vector<int> >::iterator itAbunds; for (int i = 0; i < temp.size(); i++) { if (m->control_pressed) { return 0; } //what grouping does this group belong to string grouping = designMap->get(temp[i]->getGroup()); if (grouping == "not found") { m->mothurOut("[ERROR]: " + temp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; } else { //do we already have a member of this grouping? it = merged.find(grouping); if (it == merged.end()) { //nope, so create it merged[grouping] = *temp[i]; merged[grouping].setGroup(grouping); vector<int> temp; clearGroupAbunds[grouping] = temp; } } } for (int j = 0; j < temp[0]->getNumBins(); j++) { if (m->control_pressed) { return 0; } map<string, vector<int> > otusGroupAbunds = clearGroupAbunds; for (int i = 0; i < temp.size(); i++) { string grouping = designMap->get(temp[i]->getGroup()); if (grouping == "not found") { m->mothurOut("[ERROR]: " + temp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; } else { otusGroupAbunds[grouping].push_back(temp[i]->getAbundance(j)); } } for (itAbunds = otusGroupAbunds.begin(); itAbunds != otusGroupAbunds.end(); itAbunds++) { int abund = mergeAbund(itAbunds->second); merged[itAbunds->first].set(j, abund, itAbunds->first); } } if (method == "median") { vector<SharedRAbundFloatVector*> temp2; for (it = merged.begin(); it != merged.end(); it++) { temp2.push_back(&(it->second)); } eliminateZeroOTUS(temp2); } //print new file for (it = merged.begin(); it != merged.end(); it++) { if (!m->printedSharedHeaders) { (it->second).printHeaders(out); } out << (it->second).getLabel() << '\t' << it->first << '\t'; (it->second).print(out); } }else { map<string, SharedRAbundVector> merged; map<string, SharedRAbundVector>::iterator it; for (int i = 0; i < setNames.size(); i++) { SharedRAbundVector myLookup(thisLookUp[0]->getNumBins()); myLookup.setLabel(thisLookUp[0]->getLabel()); merged[setNames[i]] = myLookup; } map<string, vector<int> > clearGroupAbunds; map<string, vector<int> >::iterator itAbunds; for (int i = 0; i < thisLookUp.size(); i++) { if (m->control_pressed) { return 0; } //what grouping does this group belong to string grouping = designMap->get(thisLookUp[i]->getGroup()); if (grouping == "not found") { m->mothurOut("[ERROR]: " + thisLookUp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; } else { //do we already have a member of this grouping? it = merged.find(grouping); if (it == merged.end()) { //nope, so create it merged[grouping] = *thisLookUp[i]; merged[grouping].setGroup(grouping); vector<int> temp; clearGroupAbunds[grouping] = temp; } } } for (int j = 0; j < thisLookUp[0]->getNumBins(); j++) { if (m->control_pressed) { return 0; } map<string, vector<int> > otusGroupAbunds = clearGroupAbunds; for (int i = 0; i < thisLookUp.size(); i++) { string grouping = designMap->get(thisLookUp[i]->getGroup()); if (grouping == "not found") { m->mothurOut("[ERROR]: " + thisLookUp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; } else { otusGroupAbunds[grouping].push_back(thisLookUp[i]->getAbundance(j)); } } for (itAbunds = otusGroupAbunds.begin(); itAbunds != otusGroupAbunds.end(); itAbunds++) { int abund = mergeAbund(itAbunds->second); merged[itAbunds->first].set(j, abund, itAbunds->first); } } if (method == "median") { vector<SharedRAbundVector*> temp; for (it = merged.begin(); it != merged.end(); it++) { temp.push_back(&(it->second)); } eliminateZeroOTUS(temp); } //print new file for (it = merged.begin(); it != merged.end(); it++) { if (!m->printedSharedHeaders) { (it->second).printHeaders(out); } out << (it->second).getLabel() << '\t' << it->first << '\t'; (it->second).print(out); } } return 0; } catch(exception& e) { m->errorOut(e, "MergeGroupsCommand", "process"); exit(1); } }
int CorrAxesCommand::execute(){ try { if (abort == true) { if (calledHelp) { return 0; } return 2; } /*************************************************************************************/ // use smart distancing to get right sharedRabund and convert to relabund if needed // /************************************************************************************/ if (sharedfile != "") { InputData* input = new InputData(sharedfile, "sharedfile"); getSharedFloat(input); delete input; if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; } if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; } }else if (relabundfile != "") { InputData* input = new InputData(relabundfile, "relabund"); getSharedFloat(input); delete input; if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; } if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; } }else if (metadatafile != "") { getMetadata(); //reads metadata file and store in lookupFloat, saves column headings in metadataLabels for later if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; } if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading metadata file."); m->mothurOutEndLine(); return 0; } if (pickedGroups) { eliminateZeroOTUS(lookupFloat); } }else { m->mothurOut("[ERROR]: no file given."); m->mothurOutEndLine(); return 0; } if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; } //this is for a sanity check to make sure the axes file and shared file match for (int i = 0; i < lookupFloat.size(); i++) { names.insert(lookupFloat[i]->getGroup()); } /*************************************************************************************/ // read axes file and check for file mismatches with shared or relabund file // /************************************************************************************/ //read axes file map<string, vector<float> > axes = readAxes(); if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; } //sanity check, the read only adds groups that are in the shared or relabund file, but we want to make sure the axes file isn't missing anyone if (axes.size() != lookupFloat.size()) { map<string, vector<float> >::iterator it; for (int i = 0; i < lookupFloat.size(); i++) { it = axes.find(lookupFloat[i]->getGroup()); if (it == axes.end()) { m->mothurOut(lookupFloat[i]->getGroup() + " is in your shared of relabund file but not in your axes file, please correct."); m->mothurOutEndLine(); } } m->control_pressed = true; } if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; } /*************************************************************************************/ // calc the r values // /************************************************************************************/ string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + method + ".corr.axes"; outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName); ofstream out; m->openOutputFile(outputFileName, out); out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint); //output headings if (metadatafile == "") { out << "OTU"; } else { out << "Feature"; } for (int i = 0; i < numaxes; i++) { out << '\t' << "axis" << (i+1) << "\tp-value"; } out << "\tlength" << endl; if (method == "pearson") { calcPearson(axes, out); } else if (method == "spearman") { calcSpearman(axes, out); } else if (method == "kendall") { calcKendall(axes, out); } else { m->mothurOut("[ERROR]: Invalid method."); m->mothurOutEndLine(); } out.close(); for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } if (m->control_pressed) { return 0; } m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } m->mothurOutEndLine(); return 0; } catch(exception& e) { m->errorOut(e, "CorrAxesCommand", "execute"); exit(1); } }