+//**********************************************************************************************************************
+int SortSeqsCommand::readFlow(){
+ try {
+ string thisOutputDir = outputDir;
+ if (outputDir == "") { thisOutputDir += m->hasPath(flowfile); }
+ string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowfile)) + "sorted" + m->getExtension(flowfile);
+ outputTypes["flow"].push_back(outputFileName); outputNames.push_back(outputFileName);
+
+ ofstream out;
+ m->openOutputFile(outputFileName, out);
+
+ ifstream in;
+ m->openInputFile(flowfile, in);
+ int numFlows;
+ string name;
+
+ in >> numFlows; m->gobble(in);
+
+ if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
+
+ if (large) { //if the file is too large to fit in memory we can still process it, but the io will be very time consuming.
+ //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
+ //this way we only store 1000 seqs in memory at a time.
+
+ int numNames = names.size();
+ int numNamesInFile = 0;
+
+ //to make sure we dont miss any seqs, add any seqs that are not in names but in the file to the end of names
+ while(!in.eof()){
+ if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
+
+ in >> name;
+ string rest = m->getline(in);
+
+ if (name != "") {
+ numNamesInFile++;
+ map<string, int>::iterator it = names.find(name);
+ if (it == names.end()) {
+ names[name] = numNames; numNames++;
+ m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
+ }
+ }
+ m->gobble(in);
+ }
+ in.close();
+ out.close();
+
+ int numLeft = names.size();
+ if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
+
+ int size = 1000; //assume that user can hold 1000 seqs in memory
+ if (numLeft < size) { size = numLeft; }
+ int times = 0;
+
+ vector<string> seqs; seqs.resize(size, "");
+
+ while (numLeft > 0) {
+
+ ifstream in2;
+ m->openInputFile(flowfile, in2); in2 >> numFlows; m->gobble(in2);
+
+ if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
+
+ int found = 0;
+ int needToFind = size;
+ if (numLeft < size) { needToFind = numLeft; }
+
+ while(!in2.eof()){
+ if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
+
+ //stop reading if we already found the seqs we are looking for
+ if (found >= needToFind) { break; }
+
+ in2 >> name;
+ string rest = m->getline(in2);
+
+ if (name != "") {
+ map<string, int>::iterator it = names.find(name);
+ if (it != names.end()) { //we found it, so put it in the vector in the right place.
+ //is it in the set of seqs we are looking for this time around
+ int thisSeqsPlace = it->second;
+ thisSeqsPlace -= (times * size);
+ if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
+ seqs[thisSeqsPlace] = (name +'\t' + rest);
+ found++;
+ }
+ }else { m->mothurOut("[ERROR]: in logic of readFlow function.\n"); m->control_pressed = true; }
+ }
+ m->gobble(in2);
+ }
+ in2.close();
+
+ ofstream out2;
+ m->openOutputFileAppend(outputFileName, out2);
+
+ int output = seqs.size();
+ if (numLeft < seqs.size()) { output = numLeft; }
+
+ for (int i = 0; i < output; i++) {
+ if (seqs[i] != "") {
+ out2 << seqs[i] << endl;
+ }
+ }
+ out2.close();
+
+ times++;
+ numLeft -= output;
+ }
+
+ m->mothurOut("Ordered " + toString(numNamesInFile) + " flows from " + flowfile + ".\n");
+ }else {
+
+ vector<string> seqs; seqs.resize(names.size(), "");
+
+ while(!in.eof()){
+ if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
+
+ in >> name;
+ string rest = m->getline(in);
+
+ if (name != "") {
+ map<string, int>::iterator it = names.find(name);
+ if (it != names.end()) { //we found it, so put it in the vector in the right place.
+ seqs[it->second] = (name + '\t' + rest);
+ }else { //if we cant find it then add it to the end
+ names[name] = seqs.size();
+ seqs.push_back((name + '\t' + rest));
+ m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
+ }
+ }
+ m->gobble(in);
+ }
+ in.close();
+
+ int count = 0;
+ for (int i = 0; i < seqs.size(); i++) {
+ if (seqs[i] != "") {
+ out << seqs[i] << endl;
+ count++;
+ }
+ }
+ out.close();
+
+ m->mothurOut("Ordered " + toString(count) + " flows from " + flowfile + ".\n");
+ }
+
+ }else { //read in file to fill names
+ int count = 0;
+
+ while(!in.eof()){
+ if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
+
+ in >> name;
+ string rest = m->getline(in);
+
+ if (name != "") {
+ //if this name is in the accnos file
+ names[name] = count;
+ count++;
+ out << name << '\t' << rest << endl;
+ }
+ m->gobble(in);
+ }
+ in.close();
+ out.close();
+
+ m->mothurOut("\nUsing " + flowfile + " to determine the order. It contains " + toString(count) + " flows.\n");
+ }
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SortSeqsCommand", "readFlow");
+ exit(1);
+ }
+}
+