+ m->errorOut(e, "DistanceCommand", "convertMatrix");
+ exit(1);
+ }
+}
+**************************************************************************************************
+int DistanceCommand::convertToLowerTriangle(string outputFile) {
+ try{
+
+ //sort file by first column so the distances for each row are together
+ string outfile = m->getRootName(outputFile) + "sorted.dist.temp";
+
+ //use the unix sort
+ #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+ string command = "sort -n " + outputFile + " -o " + outfile;
+ system(command.c_str());
+ #else //sort using windows sort
+ string command = "sort " + outputFile + " /O " + outfile;
+ system(command.c_str());
+ #endif
+
+
+ //output to new file distance for each row and save positions in file where new row begins
+ ifstream in;
+ m->openInputFile(outfile, in);
+
+ ofstream out;
+ m->openOutputFile(outputFile, out);
+
+ out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
+
+ out << alignDB.getNumSeqs() << endl;
+
+ //get first currentRow
+ string first, currentRow, second;
+ float dist;
+ int i, j;
+ i = 0; j = 0;
+ map<string, float> rowDists; //take advantage of the fact that maps are already sorted by key
+ map<string, float>::iterator it;
+
+ in >> first;
+ currentRow = first;
+
+ rowDists[first] = 0.00; //distance to yourself is 0.0
+
+ in.seekg(0);
+ //m->openInputFile(outfile, in);
+
+ while(!in.eof()) {
+ if (m->control_pressed) { in.close(); m->mothurRemove(outfile); out.close(); return 0; }
+
+ in >> first >> second >> dist; m->gobble(in);
+
+ if (first != currentRow) {
+ //print out last row
+ out << currentRow << '\t'; //print name
+
+ //print dists
+ for (it = rowDists.begin(); it != rowDists.end(); it++) {
+ if (j >= i) { break; }
+ out << it->second << '\t';
+ j++;
+ }
+ out << endl;
+
+ //start new row
+ currentRow = first;
+ rowDists.clear();
+ rowDists[first] = 0.00;
+ rowDists[second] = dist;
+ j = 0;
+ i++;
+ }else{
+ rowDists[second] = dist;
+ }
+ }
+ //print out last row
+ out << currentRow << '\t'; //print name
+
+ //print dists
+ for (it = rowDists.begin(); it != rowDists.end(); it++) {
+ out << it->second << '\t';
+ }
+ out << endl;
+
+ in.close();
+ out.close();
+
+ m->mothurRemove(outfile);
+
+ return 1;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "DistanceCommand", "convertToLowerTriangle");
+ exit(1);
+ }
+}
+**************************************************************************************************/
+//its okay if the column file does not contain all the names in the fasta file, since some distance may have been above a cutoff,
+//but no sequences can be in the column file that are not in oldfasta. also, if a distance is above the cutoff given then remove it.
+//also check to make sure the 2 files have the same alignment length.
+bool DistanceCommand::sanityCheck() {
+ try{
+ bool good = true;
+
+ //make sure the 2 fasta files have the same alignment length
+ ifstream in;
+ m->openInputFile(fastafile, in);
+ int fastaAlignLength = 0;
+ if (in) {
+ Sequence tempIn(in);
+ fastaAlignLength = tempIn.getAligned().length();
+ }
+ in.close();
+
+ ifstream in2;
+ m->openInputFile(oldfastafile, in2);
+ int oldfastaAlignLength = 0;
+ if (in2) {
+ Sequence tempIn2(in2);
+ oldfastaAlignLength = tempIn2.getAligned().length();
+ }
+ in2.close();
+
+ if (fastaAlignLength != oldfastaAlignLength) { m->mothurOut("fasta files do not have the same alignment length."); m->mothurOutEndLine(); return false; }
+
+ //read fasta file and save names as well as adding them to the alignDB
+ set<string> namesOldFasta;
+
+ ifstream inFasta;
+ m->openInputFile(oldfastafile, inFasta);
+
+ while (!inFasta.eof()) {
+ if (m->control_pressed) { inFasta.close(); return good; }
+
+ Sequence temp(inFasta);
+
+ if (temp.getName() != "") {
+ namesOldFasta.insert(temp.getName()); //save name
+ alignDB.push_back(temp); //add to DB
+ }
+
+ m->gobble(inFasta);
+ }
+
+ inFasta.close();
+
+ //read through the column file checking names and removing distances above the cutoff
+ ifstream inDist;
+ m->openInputFile(column, inDist);
+
+ ofstream outDist;
+ string outputFile = column + ".temp";
+ m->openOutputFile(outputFile, outDist);
+
+ string name1, name2;
+ float dist;
+ while (!inDist.eof()) {
+ if (m->control_pressed) { inDist.close(); outDist.close(); m->mothurRemove(outputFile); return good; }
+
+ inDist >> name1 >> name2 >> dist; m->gobble(inDist);
+
+ //both names are in fasta file and distance is below cutoff
+ if ((namesOldFasta.count(name1) == 0) || (namesOldFasta.count(name2) == 0)) { good = false; break; }
+ else{
+ if (dist <= cutoff) {
+ outDist << name1 << '\t' << name2 << '\t' << dist << endl;
+ }
+ }
+ }
+
+ inDist.close();
+ outDist.close();
+
+ if (good) {
+ m->mothurRemove(column);
+ rename(outputFile.c_str(), column.c_str());
+ }else{
+ m->mothurRemove(outputFile); //temp file is bad because file mismatch above
+ }
+
+ return good;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "DistanceCommand", "sanityCheck");