RAbundVector ListVector::getRAbundVector() { try { RAbundVector rav; for(int i=0; i<data.size(); i++) { int binSize = m->getNumNames(data[i]); rav.push_back(binSize); } // This was here before to output data in a nice format, but it screws up the name mapping steps // sort(rav.rbegin(), rav.rend()); // // for(int i=data.size()-1;i>=0;i--){ // if(rav.get(i) == 0){ rav.pop_back(); } // else{ // break; // } // } rav.setLabel(label); return rav; } catch(exception& e) { m->errorOut(e, "ListVector", "getRAbundVector"); exit(1); } }
//********************************************************************************************************************** vector<string> RareFactCommand::parseSharedFile(string filename, map<string, set<int> >& label2Ends) { try { vector<string> filenames; map<string, ofstream*> filehandles; map<string, ofstream*>::iterator it3; input = new InputData(filename, "sharedfile"); vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors(); string sharedFileRoot = m->getRootName(filename); //clears file before we start to write to it below for (int i=0; i<lookup.size(); i++) { m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".rabund")); filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund")); } ofstream* temp; for (int i=0; i<lookup.size(); i++) { temp = new ofstream; filehandles[lookup[i]->getGroup()] = temp; groups.push_back(lookup[i]->getGroup()); } while(lookup[0] != NULL) { for (int i = 0; i < lookup.size(); i++) { RAbundVector rav = lookup[i]->getRAbundVector(); m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()])); rav.print(*(filehandles[lookup[i]->getGroup()])); (*(filehandles[lookup[i]->getGroup()])).close(); label2Ends[lookup[i]->getLabel()].insert(rav.getNumSeqs()); } for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } lookup = input->getSharedRAbundVectors(); } //free memory for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } delete input; m->clearGroups(); return filenames; } catch(exception& e) { m->errorOut(e, "RareFactCommand", "parseSharedFile"); exit(1); } }
TEST(Test_Container_SharedRabundVector, GetRabundVector) { vector<int> abunds(10, 10); SharedRAbundVector temp(abunds); EXPECT_EQ(temp.getNumBins(), 10); EXPECT_EQ(temp.getNumSeqs(), 100); EXPECT_EQ(temp.getMaxRank(), 10); RAbundVector rabund = temp.getRAbundVector(); EXPECT_EQ(rabund.getNumBins(), 10); EXPECT_EQ(rabund.getNumSeqs(), 100); EXPECT_EQ(rabund.getMaxRank(), 10); }
int Rarefact::driver(RarefactionCurveData* rcd, int increment, int nIters = 1000){ try { for(int iter=0;iter<nIters;iter++){ for(int i=0;i<displays.size();i++){ displays[i]->init(label); } RAbundVector* lookup = new RAbundVector(order->getNumBins()); SAbundVector* rank = new SAbundVector(order->getMaxRank()+1); random_shuffle(order->begin(), order->end()); for(int i=0;i<numSeqs;i++){ if (m->control_pressed) { delete lookup; delete rank; delete rcd; return 0; } int binNumber = order->get(i); int abundance = lookup->get(binNumber); rank->set(abundance, rank->get(abundance)-1); abundance++; lookup->set(binNumber, abundance); rank->set(abundance, rank->get(abundance)+1); if((i == 0) || ((i+1) % increment == 0) || (ends.count(i+1) != 0)){ rcd->updateRankData(rank); } } if((numSeqs % increment != 0) || (ends.count(numSeqs) != 0)){ rcd->updateRankData(rank); } for(int i=0;i<displays.size();i++){ displays[i]->reset(); } delete lookup; delete rank; } return 0; } catch(exception& e) { m->errorOut(e, "Rarefact", "driver"); exit(1); } }
SAbundVector SharedSAbundVector::getSAbundVector(){ try { RAbundVector rav; SAbundVector sav; rav = getRAbundVector(); sav = rav.getSAbundVector(); return sav; } catch(exception& e) { m->errorOut(e, "SharedSAbundVector", "getSAbundVector"); exit(1); } }
int Collect::getCurve(float percentFreq = 0.01){ try { RAbundVector* lookup = new RAbundVector(order->getNumBins()); SAbundVector* rank = new SAbundVector(order->getMaxRank()+1); CollectorsCurveData* ccd = new CollectorsCurveData(); //sets displays label for(int i=0;i<displays.size();i++){ displays[i]->init(label); } ccd->registerDisplays(displays); //convert freq percentage to number int increment = 1; if (percentFreq < 1.0) { increment = numSeqs * percentFreq; } else { increment = percentFreq; } for(int i=0;i<numSeqs;i++){ if (m->getControl_pressed()) { delete lookup; delete rank; delete ccd; return 1; } int binNumber = order->get(i); int abundance = lookup->get(binNumber); rank->set(abundance, rank->get(abundance)-1); abundance++; lookup->set(binNumber, abundance); rank->set(abundance, rank->get(abundance)+1); //increment rank(abundance) if((i == 0) || (i+1) % increment == 0){ ccd->updateRankData(rank); } } if(numSeqs % increment != 0){ ccd->updateRankData(rank); } for(int i=0;i<displays.size();i++){ displays[i]->reset(); } delete lookup; delete rank; delete ccd; return 0; } catch(exception& e) { m->errorOut(e, "Collect", "getCurve"); exit(1); } }
RAbundVector SAbundVector::getRAbundVector(){ try { RAbundVector rav; for(int i=1;i < data.size();i++){ for(int j=0;j<data[i];j++){ rav.push_back(i); } } sort(rav.rbegin(), rav.rend()); rav.setLabel(label); return rav; } catch(exception& e) { m->errorOut(e, "SAbundVector", "getRAbundVector"); exit(1); } }
RAbundVector Geom::getRAbundVector(SAbundVector* rank){ vector <int> rData; int mr = 1; int nb = 0; int ns = 0; for(int i = rank->size()-1; i > 0; i--) { int cur = rank->get(i); if(mr == 1 && cur > 0) mr = i; nb += cur; ns += i*cur; for(int j = 0; j < cur; j++) rData.push_back(i); } RAbundVector rav = RAbundVector(rData, mr, nb, ns); rav.setLabel(rank->getLabel()); return rav; }
//********************************************************************************************************************** vector<string> SummaryCommand::parseSharedFile(string filename) { try { vector<string> filenames; map<string, ofstream*> filehandles; map<string, ofstream*>::iterator it3; input = new InputData(filename, "sharedfile"); vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors(); string sharedFileRoot = m->getRootName(filename); /******************************************************/ if (subsample) { if (subsampleSize == -1) { //user has not set size, set size = smallest samples size subsampleSize = lookup[0]->getNumSeqs(); for (int i = 1; i < lookup.size(); i++) { int thisSize = lookup[i]->getNumSeqs(); if (thisSize < subsampleSize) { subsampleSize = thisSize; } } } else { m->clearGroups(); vector<string> Groups; vector<SharedRAbundVector*> temp; for (int i = 0; i < lookup.size(); i++) { if (lookup[i]->getNumSeqs() < subsampleSize) { m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine(); delete lookup[i]; } else { Groups.push_back(lookup[i]->getGroup()); temp.push_back(lookup[i]); } } lookup = temp; m->setGroups(Groups); } if (lookup.size() < 1) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); m->control_pressed = true; delete input; return filenames; } } /******************************************************/ //clears file before we start to write to it below for (int i=0; i<lookup.size(); i++) { m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".rabund")); filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund")); } ofstream* temp; for (int i=0; i<lookup.size(); i++) { temp = new ofstream; filehandles[lookup[i]->getGroup()] = temp; groups.push_back(lookup[i]->getGroup()); } while(lookup[0] != NULL) { for (int i = 0; i < lookup.size(); i++) { RAbundVector rav = lookup[i]->getRAbundVector(); m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()])); rav.print(*(filehandles[lookup[i]->getGroup()])); (*(filehandles[lookup[i]->getGroup()])).close(); } for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } lookup = input->getSharedRAbundVectors(); } //free memory for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } delete input; return filenames; } catch(exception& e) { m->errorOut(e, "SummaryCommand", "parseSharedFile"); exit(1); } }
//********************************************************************************************************************** int SummaryCommand::process(SAbundVector*& sabund, ofstream& outputFileHandle, ofstream& outAve) { try { //calculator -> data -> values vector< vector< vector<double> > > results; results.resize(sumCalculators.size()); outputFileHandle << sabund->getLabel(); SubSample sample; for (int thisIter = 0; thisIter < iters+1; thisIter++) { SAbundVector* thisIterSabund = sabund; //we want the summary results for the whole dataset, then the subsampling if ((thisIter > 0) && subsample) { //subsample sabund and run it //copy sabund since getSample destroys it RAbundVector rabund = sabund->getRAbundVector(); SAbundVector* newSabund = new SAbundVector(); *newSabund = rabund.getSAbundVector(); sample.getSample(newSabund, subsampleSize); thisIterSabund = newSabund; } for(int i=0; i<sumCalculators.size(); i++) { vector<double> data = sumCalculators[i]->getValues(thisIterSabund); if (m->control_pressed) { return 0; } if (thisIter == 0) { outputFileHandle << '\t'; sumCalculators[i]->print(outputFileHandle); } else { //some of the calc have hci and lci need to make room for that if (results[i].size() == 0) { results[i].resize(data.size()); } //save results for ave and std. for (int j = 0; j < data.size(); j++) { if (m->control_pressed) { return 0; } results[i][j].push_back(data[j]); } } } //cleanup memory if ((thisIter > 0) && subsample) { delete thisIterSabund; } } outputFileHandle << endl; if (subsample) { outAve << sabund->getLabel() << '\t' << "ave\t"; //find ave and std for this label and output //will need to modify the createGroupSummary to combine results and not mess with the .summary file. //calcs -> values vector< vector<double> > calcAverages; calcAverages.resize(sumCalculators.size()); for (int i = 0; i < calcAverages.size(); i++) { calcAverages[i].resize(results[i].size(), 0); } for (int thisIter = 0; thisIter < iters; thisIter++) { //sum all groups dists for each calculator for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero. for (int j = 0; j < calcAverages[i].size(); j++) { calcAverages[i][j] += results[i][j][thisIter]; } } } for (int i = 0; i < calcAverages.size(); i++) { //finds average. for (int j = 0; j < calcAverages[i].size(); j++) { calcAverages[i][j] /= (float) iters; outAve << calcAverages[i][j] << '\t'; } } //find standard deviation vector< vector<double> > stdDev; stdDev.resize(sumCalculators.size()); for (int i = 0; i < stdDev.size(); i++) { stdDev[i].resize(results[i].size(), 0); } for (int thisIter = 0; thisIter < iters; thisIter++) { //compute the difference of each dist from the mean, and square the result of each for (int i = 0; i < stdDev.size(); i++) { for (int j = 0; j < stdDev[i].size(); j++) { stdDev[i][j] += ((results[i][j][thisIter] - calcAverages[i][j]) * (results[i][j][thisIter] - calcAverages[i][j])); } } } outAve << endl << sabund->getLabel() << '\t' << "std\t"; for (int i = 0; i < stdDev.size(); i++) { //finds average. for (int j = 0; j < stdDev[i].size(); j++) { stdDev[i][j] /= (float) iters; stdDev[i][j] = sqrt(stdDev[i][j]); outAve << stdDev[i][j] << '\t'; } } outAve << endl; } return 0; } catch(exception& e) { m->errorOut(e, "SummaryCommand", "process"); exit(1); } }
//********************************************************************************************************************** int GetRAbundCommand::processList(ofstream& out){ try { CountTable ct; ct.readTable(countfile, false, false); InputData input(inputfile, format); ListVector* list = input.getListVector(); string lastLabel = list->getLabel(); //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; if (m->control_pressed) { delete list; return 0; } while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { if(allLines == 1 || labels.count(list->getLabel()) == 1){ m->mothurOut(list->getLabel()); m->mothurOutEndLine(); if (m->control_pressed) { delete list; return 0; } RAbundVector* rabund = new RAbundVector(); createRabund(ct, list, rabund); if(sorted) { rabund->print(out); } else { rabund->nonSortedPrint(out); } delete rabund; processedLabels.insert(list->getLabel()); userLabels.erase(list->getLabel()); } if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { string saveLabel = list->getLabel(); delete list; list = input.getListVector(lastLabel); m->mothurOut(list->getLabel()); m->mothurOutEndLine(); if (m->control_pressed) { delete list; return 0; } RAbundVector* rabund = new RAbundVector(); createRabund(ct, list, rabund); if(sorted) { rabund->print(out); } else { rabund->nonSortedPrint(out); } delete rabund; processedLabels.insert(list->getLabel()); userLabels.erase(list->getLabel()); //restore real lastlabel to save below list->setLabel(saveLabel); } lastLabel = list->getLabel(); delete list; list = input.getListVector(); } //output error messages about any remaining user labels set<string>::iterator it; bool needToRun = false; for (it = userLabels.begin(); it != userLabels.end(); it++) { m->mothurOut("Your file does not include the label " + *it); if (processedLabels.count(lastLabel) != 1) { m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine(); needToRun = true; }else { m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine(); } } //run last label if you need to if (needToRun == true) { if (list != NULL) { delete list; } list = input.getListVector(lastLabel); m->mothurOut(list->getLabel()); m->mothurOutEndLine(); if (m->control_pressed) { delete list; return 0; } RAbundVector* rabund = new RAbundVector(); createRabund(ct, list, rabund); if(sorted) { rabund->print(out); } else { rabund->nonSortedPrint(out); } delete rabund; delete list; } return 0; } catch(exception& e) { m->errorOut(e, "GetRAbundCommand", "processList"); exit(1); } }
int GetRAbundCommand::execute(){ try { if (abort == true) { if (calledHelp) { return 0; } return 2; } map<string, string> variables; variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile)); filename = getOutputFileName("rabund", variables); m->openOutputFile(filename, out); if (countfile != "") { processList(out); }else { InputData input(inputfile, format); RAbundVector* rabund = input.getRAbundVector(); string lastLabel = rabund->getLabel(); //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; if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete rabund; return 0; } while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { if(allLines == 1 || labels.count(rabund->getLabel()) == 1){ m->mothurOut(rabund->getLabel()); m->mothurOutEndLine(); if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete rabund; return 0; } if(sorted) { rabund->print(out); } else { rabund->nonSortedPrint(out); } processedLabels.insert(rabund->getLabel()); userLabels.erase(rabund->getLabel()); } if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { string saveLabel = rabund->getLabel(); delete rabund; rabund = input.getRAbundVector(lastLabel); m->mothurOut(rabund->getLabel()); m->mothurOutEndLine(); if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete rabund; return 0; } if(sorted) { rabund->print(out); } else { rabund->nonSortedPrint(out); } processedLabels.insert(rabund->getLabel()); userLabels.erase(rabund->getLabel()); //restore real lastlabel to save below rabund->setLabel(saveLabel); } lastLabel = rabund->getLabel(); delete rabund; rabund = input.getRAbundVector(); } //output error messages about any remaining user labels set<string>::iterator it; bool needToRun = false; for (it = userLabels.begin(); it != userLabels.end(); it++) { m->mothurOut("Your file does not include the label " + *it); if (processedLabels.count(lastLabel) != 1) { m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine(); needToRun = true; }else { m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine(); } } //run last label if you need to if (needToRun == true) { if (rabund != NULL) { delete rabund; } rabund = input.getRAbundVector(lastLabel); m->mothurOut(rabund->getLabel()); m->mothurOutEndLine(); if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete rabund; return 0; } if(sorted) { rabund->print(out); } else { rabund->nonSortedPrint(out); } delete rabund; } } if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); return 0; } m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["rabund"].push_back(filename); m->mothurOutEndLine(); out.close(); //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); } } return 0; } catch(exception& e) { m->errorOut(e, "GetRAbundCommand", "execute"); exit(1); } }
//********************************************************************************************************************** int RemoveRareCommand::processRabund(){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); } string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "pick" + m->getExtension(rabundfile); outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName); ofstream out; m->openOutputFile(outputFileName, out); //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label. InputData input(rabundfile, "rabund"); RAbundVector* rabund = input.getRAbundVector(); string lastLabel = rabund->getLabel(); set<string> processedLabels; set<string> userLabels = labels; while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { if (m->control_pressed) { delete rabund; out.close(); return 0; } if(allLines == 1 || labels.count(rabund->getLabel()) == 1){ m->mothurOut(rabund->getLabel()); m->mothurOutEndLine(); processedLabels.insert(rabund->getLabel()); userLabels.erase(rabund->getLabel()); RAbundVector newRabund; newRabund.setLabel(rabund->getLabel()); for (int i = 0; i < rabund->getNumBins(); i++) { if (rabund->get(i) > nseqs) { newRabund.push_back(rabund->get(i)); } } if (newRabund.getNumBins() > 0) { newRabund.print(out); } } if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { string saveLabel = rabund->getLabel(); delete rabund; rabund = input.getRAbundVector(lastLabel); m->mothurOut(rabund->getLabel()); m->mothurOutEndLine(); processedLabels.insert(rabund->getLabel()); userLabels.erase(rabund->getLabel()); RAbundVector newRabund; newRabund.setLabel(rabund->getLabel()); for (int i = 0; i < rabund->getNumBins(); i++) { if (rabund->get(i) > nseqs) { newRabund.push_back(rabund->get(i)); } } if (newRabund.getNumBins() > 0) { newRabund.print(out); } //restore real lastlabel to save below rabund->setLabel(saveLabel); } lastLabel = rabund->getLabel(); delete rabund; rabund = input.getRAbundVector(); } if (m->control_pressed) { out.close(); return 0; } //output error messages about any remaining user labels set<string>::iterator it; bool needToRun = false; for (it = userLabels.begin(); it != userLabels.end(); it++) { m->mothurOut("Your file does not include the label " + *it); if (processedLabels.count(lastLabel) != 1) { m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine(); needToRun = true; }else { m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine(); } } //run last label if you need to if (needToRun == true) { if (rabund != NULL) { delete rabund; } rabund = input.getRAbundVector(lastLabel); m->mothurOut(rabund->getLabel()); m->mothurOutEndLine(); RAbundVector newRabund; newRabund.setLabel(rabund->getLabel()); for (int i = 0; i < rabund->getNumBins(); i++) { if (rabund->get(i) > nseqs) { newRabund.push_back(rabund->get(i)); } } if (newRabund.getNumBins() > 0) { newRabund.print(out); } delete rabund; } return 0; } catch(exception& e) { m->errorOut(e, "RemoveRareCommand", "processRabund"); exit(1); } }
//********************************************************************************************************************** void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) { 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(); m->openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(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(); m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(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); } }