-/***********************************************************************/
-
-inline int renameFile(string oldName, string newName){
-
- ifstream inTest;
- int exist = openInputFile(newName, inTest, "");
-
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
- if (exist == 0) { //you could open it so you want to delete it
- inTest.close();
- string command = "rm " + newName;
- system(command.c_str());
- }
-
- string command = "mv " + oldName + " " + newName;
- system(command.c_str());
-#else
- remove(newName.c_str());
- int renameOk = rename(oldName.c_str(), newName.c_str());
-#endif
- return 0;
-}
-
-/***********************************************************************/
-
-inline int openOutputFile(string fileName, ofstream& fileHandle){
-
- string completeFileName = getFullPathName(fileName);
-
- fileHandle.open(completeFileName.c_str(), ios::trunc);
- if(!fileHandle) {
- mothurOut("Error: Could not open " + completeFileName); mothurOutEndLine();
- return 1;
- }
- else {
- return 0;
- }
-
-}
-
-/***********************************************************************/
-
-inline int getNumSeqs(ifstream& file){
-
- int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
- file.seekg(0);
- return numSeqs;
-
-}
-/***********************************************************************/
-
-inline bool inVector(string member, vector<string> group){
-
- for (int i = 0; i < group.size(); i++) {
- if (group[i] == member) { return true; }
- }
-
- return false;
-}
-/***********************************************************************/
-
-//This function parses the estimator options and puts them in a vector
-inline void splitAtDash(string& estim, vector<string>& container) {
- try {
- string individual;
-
- while (estim.find_first_of('-') != -1) {
- individual = estim.substr(0,estim.find_first_of('-'));
- if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
- estim = estim.substr(estim.find_first_of('-')+1, estim.length());
- container.push_back(individual);
- }
- }
- //get last one
- container.push_back(estim);
- }
- catch(exception& e) {
- errorOut(e, "mothur", "splitAtDash");
- exit(1);
- }
-}
-
-/***********************************************************************/
-//This function parses the label options and puts them in a set
-inline void splitAtDash(string& estim, set<string>& container) {
- try {
- string individual;
-
- while (estim.find_first_of('-') != -1) {
- individual = estim.substr(0,estim.find_first_of('-'));
- if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
- estim = estim.substr(estim.find_first_of('-')+1, estim.length());
- container.insert(individual);
- }
- }
- //get last one
- container.insert(estim);
- }
- catch(exception& e) {
- errorOut(e, "mothur", "splitAtDash");
- exit(1);
- }
-}
-/***********************************************************************/
-//This function parses the line options and puts them in a set
-inline void splitAtDash(string& estim, set<int>& container) {
- try {
- string individual;
- int lineNum;
-
- while (estim.find_first_of('-') != -1) {
- individual = estim.substr(0,estim.find_first_of('-'));
- if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
- estim = estim.substr(estim.find_first_of('-')+1, estim.length());
- convert(individual, lineNum); //convert the string to int
- container.insert(lineNum);
- }
- }
- //get last one
- convert(estim, lineNum); //convert the string to int
- container.insert(lineNum);
- }
- catch(exception& e) {
- errorOut(e, "mothur", "splitAtDash");
- exit(1);
- }
-}
-/***********************************************************************/
-//This function parses the a string and puts peices in a vector
-inline void splitAtComma(string& estim, vector<string>& container) {
- try {
- string individual;
-
- while (estim.find_first_of(',') != -1) {
- individual = estim.substr(0,estim.find_first_of(','));
- if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
- estim = estim.substr(estim.find_first_of(',')+1, estim.length());
- container.push_back(individual);
- }
- }
- //get last one
- container.push_back(estim);
- }
- catch(exception& e) {
- errorOut(e, "mothur", "splitAtComma");
- exit(1);
- }
-}
-/***********************************************************************/
-
-//This function splits up the various option parameters
-inline void splitAtComma(string& prefix, string& suffix){
- try {
- prefix = suffix.substr(0,suffix.find_first_of(','));
- if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
- suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
- string space = " ";
- while(suffix.at(0) == ' ')
- suffix = suffix.substr(1, suffix.length());
- }
-
- }
- catch(exception& e) {
- errorOut(e, "mothur", "splitAtComma");
- exit(1);
- }
-}
-/***********************************************************************/
-
-//This function separates the key value from the option value i.e. dist=96_...
-inline void splitAtEquals(string& key, string& value){
- try {
- if(value.find_first_of('=') != -1){
- key = value.substr(0,value.find_first_of('='));
- if ((value.find_first_of('=')+1) <= value.length()) {
- value = value.substr(value.find_first_of('=')+1, value.length());
- }
- }else{
- key = value;
- value = 1;
- }
- }
- catch(exception& e) {
- errorOut(e, "mothur", "splitAtEquals");
- exit(1);
- }
-}
-/**************************************************************************************************/
-
-inline bool inUsersGroups(string groupname, vector<string> Groups) {
- try {
- for (int i = 0; i < Groups.size(); i++) {
- if (groupname == Groups[i]) { return true; }
- }
- return false;
- }
- catch(exception& e) {
- errorOut(e, "mothur", "inUsersGroups");
- exit(1);
- }
-}
-
-/**************************************************************************************************/
-
-inline void mothurOutJustToLog(string message) {
- try {
- ofstream out;
- string logFileName = "mothur.logFile";
- openOutputFileAppend(logFileName, out);
-
- out << message;
-
- out.close();
- }
- catch(exception& e) {
- errorOut(e, "mothur", "mothurOutJustToLog");
- exit(1);
- }
-}
-
-
-/**************************************************************************************************/
-
-inline void mothurOut(float num) {
- try {
- ofstream out;
- string logFileName = "mothur.logFile";
- openOutputFileAppend(logFileName, out);
-
- cout << num;
- out << num;
-
- out.close();
- }
- catch(exception& e) {
- cout << "Error in mothur class mothurOut float" << endl;
- exit(1);
- }
-}
-/***********************************************************************/
-inline void mothurOut(double value) {
- try {
- ofstream out;
- string logFileName = "mothur.logFile";
- openOutputFileAppend(logFileName, out);
-
- cout << value;
- out << value;
-
- out.close();
- }
- catch(exception& e) {
- cout << "Error in mothur class mothurOut double" << endl;
- exit(1);
- }
-}
-
-/***********************************************************************/
-//this function determines if the user has given us labels that are smaller than the given label.
-//if so then it returns true so that the calling function can run the previous valid distance.
-//it's a "smart" distance function. It also checks for invalid labels.
-inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
- try {
- set<string>::iterator it;
- vector<float> orderFloat;
- map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
- map<string, float>::iterator it2;
- float labelFloat;
- bool smaller = false;
-
- //unique is the smallest line
- if (label == "unique") { return false; }
- else { convert(label, labelFloat); }
-
- //go through users set and make them floats
- for(it = userLabels.begin(); it != userLabels.end(); ++it) {
-
- float temp;
- if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
- convert(*it, temp);
- orderFloat.push_back(temp);
- userMap[*it] = temp;
- }else if (*it == "unique") {
- orderFloat.push_back(-1.0);
- userMap["unique"] = -1.0;
- }else {
- if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
- userLabels.erase(*it);
- it--;
- }
- }
-
- //sort order
- sort(orderFloat.begin(), orderFloat.end());
-
- /*************************************************/
- //is this label bigger than any of the users labels
- /*************************************************/
-
- //loop through order until you find a label greater than label
- for (int i = 0; i < orderFloat.size(); i++) {
- if (orderFloat[i] < labelFloat) {
- smaller = true;
- if (orderFloat[i] == -1) {
- if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
- userLabels.erase("unique");
- }
- else {
- if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
- string s = "";
- for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
- if (it2->second == orderFloat[i]) {
- s = it2->first;
- //remove small labels
- userLabels.erase(s);
- break;
- }
- }
- if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
- }
- //since they are sorted once you find a bigger one stop looking
- }else { break; }
- }
-
- return smaller;
-
- }
- catch(exception& e) {
- errorOut(e, "mothur", "anyLabelsToProcess");
- exit(1);
- }
-}