]> git.donarmstrong.com Git - mothur.git/blobdiff - sortseqscommand.cpp
fixes while testing 1.33.0
[mothur.git] / sortseqscommand.cpp
index 1cc142f041e5ad4b9e7e21d5d53cec57c48724ad..d844452e1f72376535bc6edb57c36d3e2c808474 100644 (file)
 //**********************************************************************************************************************
 vector<string> SortSeqsCommand::setParameters(){       
        try {
-               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pfasta);
-               CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
-               CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pgroup);
-               CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
-               CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pqfile);
-               CommandParameter plarge("large", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(plarge);
-               CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
-        CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
-               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none","fasta",false,false); parameters.push_back(pfasta);
+        CommandParameter pflow("flow", "InputTypes", "", "", "none", "FNGLT", "none","flow",false,false); parameters.push_back(pflow);
+        CommandParameter pname("name", "InputTypes", "", "", "NameCount", "FNGLT", "none","name",false,false); parameters.push_back(pname);
+        CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "FNGLT", "none","count",false,false); parameters.push_back(pcount);
+               CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "FNGLT", "none","group",false,false); parameters.push_back(pgroup);
+               CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none","taxonomy",false,false); parameters.push_back(ptaxonomy);
+               CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "FNGLT", "none","qfile",false,false); parameters.push_back(pqfile);
+               CommandParameter plarge("large", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(plarge);
+               CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
+        CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
+               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
                
                vector<string> myArray;
                for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
@@ -36,8 +38,8 @@ vector<string> SortSeqsCommand::setParameters(){
 string SortSeqsCommand::getHelpString(){       
        try {
                string helpString = "";
-               helpString += "The sort.seqs command puts the sequences in the same order for the following file types: accnos fasta, name, group, taxonomy or quality file.\n";
-        helpString += "The sort.seqs command parameters are accnos, fasta, name, group, taxonomy, qfile and large.\n";
+               helpString += "The sort.seqs command puts the sequences in the same order for the following file types: accnos fasta, name, group, count, taxonomy, flow or quality file.\n";
+        helpString += "The sort.seqs command parameters are accnos, fasta, name, group, count, taxonomy, flow, qfile and large.\n";
         helpString += "The accnos file allows you to specify the order you want the files in.  If none is provided, mothur will use the order of the first file it reads.\n";
         helpString += "The large parameters is used to indicate your files are too large to fit in RAM.\n";
                helpString += "The sort.seqs command should be in the following format: sort.seqs(fasta=yourFasta).\n";
@@ -50,8 +52,27 @@ string SortSeqsCommand::getHelpString(){
                exit(1);
        }
 }
-
-
+//**********************************************************************************************************************
+string SortSeqsCommand::getOutputPattern(string type) {
+    try {
+        string pattern = "";
+        
+        if (type == "fasta")            {   pattern = "[filename],sorted,[extension]";    }
+        else if (type == "taxonomy")    {   pattern = "[filename],sorted,[extension]";    }
+        else if (type == "name")        {   pattern = "[filename],sorted,[extension]";    }
+        else if (type == "group")       {   pattern = "[filename],sorted,[extension]";    }
+        else if (type == "count")       {   pattern = "[filename],sorted,[extension]";    }
+        else if (type == "flow")        {   pattern = "[filename],sorted,[extension]";    }
+        else if (type == "qfile")      {   pattern = "[filename],sorted,[extension]";    }
+        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
+        
+        return pattern;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "SortSeqsCommand", "getOutputPattern");
+        exit(1);
+    }
+}
 //**********************************************************************************************************************
 SortSeqsCommand::SortSeqsCommand(){    
        try {
@@ -61,8 +82,10 @@ SortSeqsCommand::SortSeqsCommand(){
                outputTypes["fasta"] = tempOutNames;
                outputTypes["taxonomy"] = tempOutNames;
                outputTypes["name"] = tempOutNames;
+        outputTypes["count"] = tempOutNames;
                outputTypes["group"] = tempOutNames;
                outputTypes["qfile"] = tempOutNames;
+        outputTypes["flow"] = tempOutNames;
        }
        catch(exception& e) {
                m->errorOut(e, "SortSeqsCommand", "SortSeqsCommand");
@@ -99,6 +122,8 @@ SortSeqsCommand::SortSeqsCommand(string option)  {
                        outputTypes["name"] = tempOutNames;
                        outputTypes["group"] = tempOutNames;
                        outputTypes["qfile"] = tempOutNames;
+            outputTypes["flow"] = tempOutNames;
+            outputTypes["count"] = tempOutNames;
                        
                        //if the user changes the output directory command factory will send this info to us in the output parameter 
                        outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
@@ -155,6 +180,22 @@ SortSeqsCommand::SortSeqsCommand(string option)  {
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
                                }
+                
+                it = parameters.find("flow");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["flow"] = inputDir + it->second;             }
+                               }
+                
+                it = parameters.find("count");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["count"] = inputDir + it->second;            }
+                               }
                        }
             
                        
@@ -169,6 +210,11 @@ SortSeqsCommand::SortSeqsCommand(string option)  {
                        else if (fastafile == "not found") {  fastafile = "";  }        
                        else { m->setFastaFile(fastafile); }
             
+            flowfile = validParameter.validFile(parameters, "flow", true);
+                       if (flowfile == "not open") { flowfile = ""; abort = true; }
+                       else if (flowfile == "not found") {  flowfile = "";  }  
+                       else { m->setFlowFile(flowfile); }
+            
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { namefile = ""; abort = true; }
                        else if (namefile == "not found") {  namefile = "";  }  
@@ -188,16 +234,31 @@ SortSeqsCommand::SortSeqsCommand(string option)  {
                        if (qualfile == "not open") { abort = true; }
                        else if (qualfile == "not found") {  qualfile = "";  }                  
                        else { m->setQualFile(qualfile); }
+            
+            countfile = validParameter.validFile(parameters, "count", true);
+                       if (countfile == "not open") { countfile = ""; abort = true; }
+                       else if (countfile == "not found") { countfile = "";  } 
+                       else { m->setCountTableFile(countfile); }
+            
+            if ((namefile != "") && (countfile != "")) {
+                m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
+            }
+                       
+            if ((groupfile != "") && (countfile != "")) {
+                m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
+            }
                        
             string temp = validParameter.validFile(parameters, "large", false);                if (temp == "not found") { temp = "f"; }
                        large = m->isTrue(temp);
             
-                       if ((fastafile == "") && (namefile == "") && (groupfile == "") && (taxfile == "") && (qualfile == ""))  { m->mothurOut("You must provide at least one of the following: fasta, name, group, taxonomy or quality."); m->mothurOutEndLine(); abort = true; }
+                       if ((fastafile == "") && (namefile == "") && (countfile == "") && (groupfile == "") && (taxfile == "") && (flowfile == "") && (qualfile == ""))  { m->mothurOut("You must provide at least one of the following: fasta, name, group, count, taxonomy, flow or quality."); m->mothurOutEndLine(); abort = true; }
                        
-                       if ((fastafile != "") && (namefile == "")) {
-                               vector<string> files; files.push_back(fastafile);
-                               parser.getNameFile(files);
-                       }
+            if (countfile == "") {
+                if ((fastafile != "") && (namefile == "")) {
+                    vector<string> files; files.push_back(fastafile);
+                    parser.getNameFile(files);
+                }
+            }
                }
         
        }
@@ -214,11 +275,19 @@ int SortSeqsCommand::execute(){
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                //read through the correct file and output lines you want to keep
-        if (accnosfile != "")          {               readAccnos();   }
+        if (accnosfile != "")          {               
+            vector<string> temp;
+            m->readAccnos(accnosfile, temp);
+            for (int i = 0; i < temp.size(); i++) {  names[temp[i]] = i;  }
+            m->mothurOut("\nUsing " + accnosfile + " to determine the order. It contains " + toString(temp.size()) + " representative sequences.\n");  
+        }
+        
                if (fastafile != "")            {               readFasta();    }
+        if (flowfile != "")         {          readFlow();     }
         if (qualfile != "")                    {               readQual();             }
         if (namefile != "")                    {               readName();             }
                if (groupfile != "")            {               readGroup();    }
+        if (countfile != "")           {               readCount();    }
         if (taxfile != "")                     {               readTax();              }
                
                if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
@@ -255,7 +324,17 @@ int SortSeqsCommand::execute(){
                        itTypes = outputTypes.find("qfile");
                        if (itTypes != outputTypes.end()) {
                                if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
-                       }                       
+                       }       
+            
+            itTypes = outputTypes.find("flow");
+                       if (itTypes != outputTypes.end()) {
+                               if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
+                       }
+            
+            itTypes = outputTypes.find("count");
+                       if (itTypes != outputTypes.end()) {
+                               if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
+                       }
                }
                
                return 0;               
@@ -272,7 +351,10 @@ int SortSeqsCommand::readFasta(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "sorted" + m->getExtension(fastafile);
+               map<string, string> variables; 
+        variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(fastafile));
+        variables["[extension]"] = m->getExtension(fastafile);
+               string outputFileName = getOutputFileName("fasta", variables);
                outputTypes["fasta"].push_back(outputFileName);  outputNames.push_back(outputFileName);
         
                ofstream out;
@@ -319,6 +401,7 @@ int SortSeqsCommand::readFasta(){
                 int times = 0;
                 
                 vector<Sequence> seqs; seqs.resize(size);
+                for (int i = 0; i < seqs.size(); i++) { seqs[i].setName(""); } //this is so if some of the seqs are missing we dont print out garbage
                 
                 while (numLeft > 0) {
                     
@@ -363,7 +446,7 @@ int SortSeqsCommand::readFasta(){
                     if (numLeft < seqs.size()) { output = numLeft; }
                         
                     for (int i = 0; i < output; i++) {
-                        seqs[i].printSequence(out2);
+                        if (seqs[i].getName() != "") { seqs[i].printSequence(out2); }
                     }
                     out2.close();
                     
@@ -375,6 +458,7 @@ int SortSeqsCommand::readFasta(){
             }else {
                 
                 vector<Sequence> seqs; seqs.resize(names.size());
+                for (int i = 0; i < seqs.size(); i++) { seqs[i].setName(""); } //this is so if some of the seqs are missing we dont print out garbage
                 
                 while(!in.eof()){
                     if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
@@ -396,12 +480,15 @@ int SortSeqsCommand::readFasta(){
                 }
                 in.close();    
                 
+                int count = 0;
                 for (int i = 0; i < seqs.size(); i++) {
-                    seqs[i].printSequence(out);
+                    if (seqs[i].getName() != "") {
+                        seqs[i].printSequence(out); count++;
+                    }
                 }
                 out.close();
                 
-                m->mothurOut("Ordered " + toString(seqs.size()) + " sequences from " + fastafile + ".\n");
+                m->mothurOut("Ordered " + toString(count) + " sequences from " + fastafile + ".\n");
             }
                         
         }else { //read in file to fill names
@@ -435,12 +522,196 @@ int SortSeqsCommand::readFasta(){
                exit(1);
        }
 }
+//**********************************************************************************************************************
+int SortSeqsCommand::readFlow(){
+       try {
+               string thisOutputDir = outputDir;
+               if (outputDir == "") {  thisOutputDir += m->hasPath(flowfile);  }
+        map<string, string> variables; 
+        variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(flowfile));
+        variables["[extension]"] = m->getExtension(flowfile);
+               string outputFileName = getOutputFileName("flow", variables);
+               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);
+       }
+}
+
 //**********************************************************************************************************************
 int SortSeqsCommand::readQual(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(qualfile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "sorted" +  m->getExtension(qualfile);
+               map<string, string> variables; 
+        variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(qualfile));
+        variables["[extension]"] = m->getExtension(qualfile);
+               string outputFileName = getOutputFileName("qfile", variables);
         outputTypes["qfile"].push_back(outputFileName);  outputNames.push_back(outputFileName);
         
                ofstream out;
@@ -489,6 +760,7 @@ int SortSeqsCommand::readQual(){
 
                 
                 vector<QualityScores> seqs; seqs.resize(size);
+                for (int i = 0; i < seqs.size(); i++) { seqs[i].setName(""); } //this is so if some of the seqs are missing we dont print out garbage
                 
                 while (numLeft > 0) {
                     
@@ -534,7 +806,9 @@ int SortSeqsCommand::readQual(){
                     if (numLeft < seqs.size()) { output = numLeft; }
                     
                     for (int i = 0; i < output; i++) {
-                        seqs[i].printQScores(out2);
+                        if (seqs[i].getName() != "") {
+                            seqs[i].printQScores(out2);
+                        }
                     }
                     out2.close();
                     
@@ -547,6 +821,7 @@ int SortSeqsCommand::readQual(){
             }else {
                 
                 vector<QualityScores> seqs; seqs.resize(names.size());
+                for (int i = 0; i < seqs.size(); i++) { seqs[i].setName(""); } //this is so if some of the seqs are missing we dont print out garbage
                 
                 while(!in.eof()){
                     if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
@@ -569,12 +844,13 @@ int SortSeqsCommand::readQual(){
                 }
                 in.close();    
                 
+                int count = 0;
                 for (int i = 0; i < seqs.size(); i++) {
-                    seqs[i].printQScores(out);
+                    if (seqs[i].getName() != "") { seqs[i].printQScores(out); count++; }
                 }
                 out.close();
                 
-                m->mothurOut("Ordered " + toString(seqs.size()) + " sequences from " + qualfile + ".\n");
+                m->mothurOut("Ordered " + toString(count) + " sequences from " + qualfile + ".\n");
             }
             
         }else { //read in file to fill names
@@ -615,7 +891,10 @@ int SortSeqsCommand::readName(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "sorted" + m->getExtension(namefile);
+        map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
+        variables["[extension]"] = m->getExtension(namefile);
+               string outputFileName = getOutputFileName("name", variables);
         outputTypes["name"].push_back(outputFileName);  outputNames.push_back(outputFileName);
         
                ofstream out;
@@ -627,7 +906,7 @@ int SortSeqsCommand::readName(){
                
         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());
+                vector<string> seqs; seqs.resize(names.size(), "");
                 
                 while(!in.eof()){
                     if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
@@ -648,12 +927,13 @@ int SortSeqsCommand::readName(){
                 }
                 in.close();    
                 
+                int count = 0;
                 for (int i = 0; i < seqs.size(); i++) {
-                    out << seqs[i] << endl;
+                    if (seqs[i] != "") { out << seqs[i] << endl; count++; }
                 }
                 out.close();
                 
-                m->mothurOut("Ordered " + toString(seqs.size()) + " sequences from " + namefile + ".\n");
+                m->mothurOut("Ordered " + toString(count) + " sequences from " + namefile + ".\n");
             
         }else { //read in file to fill names
             int count = 0;
@@ -685,14 +965,101 @@ int SortSeqsCommand::readName(){
                exit(1);
        }
 }
-
+//**********************************************************************************************************************
+int SortSeqsCommand::readCount(){
+       try {
+               string thisOutputDir = outputDir;
+               if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
+        map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
+        variables["[extension]"] = m->getExtension(countfile);
+               string outputFileName = getOutputFileName("count", variables);
+        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);
+       }
+}
 //**********************************************************************************************************************
 int SortSeqsCommand::readGroup(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
-               outputTypes["group"].push_back(outputFileName);  outputNames.push_back(outputFileName);
+               map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
+        variables["[extension]"] = m->getExtension(groupfile);
+               string outputFileName = getOutputFileName("group", variables);
+        outputTypes["group"].push_back(outputFileName);  outputNames.push_back(outputFileName);
         
                ofstream out;
                m->openOutputFile(outputFileName, out);
@@ -703,7 +1070,7 @@ int SortSeqsCommand::readGroup(){
                
                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());
+            vector<string> seqs; seqs.resize(names.size(), "");
             
             while(!in.eof()){
                 if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
@@ -724,12 +1091,13 @@ int SortSeqsCommand::readGroup(){
             }
             in.close();        
             
+            int count = 0;
             for (int i = 0; i < seqs.size(); i++) {
-                out << seqs[i] << endl;
+                if (seqs[i] != "") { out << seqs[i] << endl; count++; }
             }
             out.close();
             
-            m->mothurOut("Ordered " + toString(seqs.size()) + " sequences from " + groupfile + ".\n");
+            m->mothurOut("Ordered " + toString(count) + " sequences from " + groupfile + ".\n");
             
         }else { //read in file to fill names
             int count = 0;
@@ -766,7 +1134,11 @@ int SortSeqsCommand::readTax(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
+               map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
+        variables["[extension]"] = m->getExtension(taxfile);
+               string outputFileName = getOutputFileName("taxonomy", variables);
+
         outputTypes["taxonomy"].push_back(outputFileName);  outputNames.push_back(outputFileName);
         
                ofstream out;
@@ -778,7 +1150,7 @@ int SortSeqsCommand::readTax(){
                
                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());
+            vector<string> seqs; seqs.resize(names.size(), "");
             
             while(!in.eof()){
                 if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
@@ -799,12 +1171,13 @@ int SortSeqsCommand::readTax(){
             }
             in.close();        
             
+            int count = 0;
             for (int i = 0; i < seqs.size(); i++) {
-                out << seqs[i] << endl;
+                if (seqs[i] != "") { out << seqs[i] << endl; count++; }
             }
             out.close();
             
-            m->mothurOut("Ordered " + toString(seqs.size()) + " sequences from " + taxfile + ".\n");
+            m->mothurOut("Ordered " + toString(count) + " sequences from " + taxfile + ".\n");
             
         }else { //read in file to fill names
             int count = 0;
@@ -838,38 +1211,6 @@ int SortSeqsCommand::readTax(){
        }
 }
 //**********************************************************************************************************************
-int SortSeqsCommand::readAccnos(){
-       try {
-               
-               ifstream in;
-               m->openInputFile(accnosfile, in);
-               string name;
-        int count = 0;
-               
-               while(!in.eof()){
-            
-            if (m->control_pressed) { break; }
-            
-                       in >> name; m->gobble(in);
-            
-            if (name != "") {
-                names[name] = count;
-                count++;
-            }
-               }
-               in.close();             
-        
-        m->mothurOut("\nUsing " + accnosfile + " to determine the order. It contains " + toString(count) + " representative sequences.\n");
-        
-        return 0;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "SortSeqsCommand", "readAccnos");
-               exit(1);
-       }
-}
-
-//**********************************************************************************************************************