+//**********************************************************************************************************************
+int SortSeqsCommand::readCount(){
+ try {
+ string thisOutputDir = outputDir;
+ if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
+ string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(countfile)) + getOutputFileNameTag("count", countfile);
+ outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
+
+ ofstream out;
+ m->openOutputFile(outputFileName, out);
+
+ ifstream in;
+ m->openInputFile(countfile, in);
+ string firstCol, rest;
+
+ if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
+
+ vector<string> seqs; seqs.resize(names.size(), "");
+
+ string headers = m->getline(in); m->gobble(in);
+
+ while(!in.eof()){
+ if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
+
+ in >> firstCol; m->gobble(in);
+ rest = m->getline(in); m->gobble(in);
+
+ if (firstCol != "") {
+ map<string, int>::iterator it = names.find(firstCol);
+ if (it != names.end()) { //we found it, so put it in the vector in the right place.
+ seqs[it->second] = firstCol + '\t' + rest;
+ }else { //if we cant find it then add it to the end
+ names[firstCol] = seqs.size();
+ seqs.push_back((firstCol + '\t' + rest));
+ m->mothurOut(firstCol + " was not in the contained the file which determined the order, adding it to the end.\n");
+ }
+ }
+ }
+ in.close();
+
+ int count = 0;
+ out << headers << endl;
+ for (int i = 0; i < seqs.size(); i++) {
+ if (seqs[i] != "") { out << seqs[i] << endl; count++; }
+ }
+ out.close();
+
+ m->mothurOut("Ordered " + toString(count) + " sequences from " + countfile + ".\n");
+
+ }else { //read in file to fill names
+ int count = 0;
+
+ string headers = m->getline(in); m->gobble(in);
+ out << headers << endl;
+
+ while(!in.eof()){
+ if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
+
+ in >> firstCol; m->gobble(in);
+ rest = m->getline(in); m->gobble(in);
+
+ if (firstCol != "") {
+ //if this name is in the accnos file
+ names[firstCol] = count;
+ count++;
+ out << firstCol << '\t' << rest << endl;
+ }
+ m->gobble(in);
+ }
+ in.close();
+ out.close();
+
+ m->mothurOut("\nUsing " + countfile + " to determine the order. It contains " + toString(count) + " representative sequences.\n");
+ }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SortSeqsCommand", "readCount");
+ exit(1);
+ }
+}