+/**************************************************************************************************/
+//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;
+ openInputFile(fastafile, in);
+ int fastaAlignLength = 0;
+ if (in) {
+ Sequence tempIn(in);
+ fastaAlignLength = tempIn.getAligned().length();
+ }
+ in.close();
+
+ ifstream in2;
+ 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;
+ 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
+ }
+
+ gobble(inFasta);
+ }
+
+ inFasta.close();
+
+ //read through the column file checking names and removing distances above the cutoff
+ ifstream inDist;
+ openInputFile(column, inDist);
+
+ ofstream outDist;
+ string outputFile = column + ".temp";
+ openOutputFile(outputFile, outDist);
+
+ string name1, name2;
+ float dist;
+ while (!inDist.eof()) {
+ if (m->control_pressed) { inDist.close(); outDist.close(); remove(outputFile.c_str()); return good; }
+
+ inDist >> name1 >> name2 >> dist; 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) {
+ remove(column.c_str());
+ rename(outputFile.c_str(), column.c_str());
+ }else{
+ remove(outputFile.c_str()); //temp file is bad because file mismatch above
+ }
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "DistanceCommand", "appendFiles");
+ exit(1);
+ }
+}
+