]> git.donarmstrong.com Git - mothur.git/blobdiff - seqerrorcommand.cpp
added load.logfile command. changed summary.single output for subsample=t.
[mothur.git] / seqerrorcommand.cpp
index af044fec3f1b60b07da406894ebc454d96df21bd..8fd6368154773bcc38e2b950d7b170c88be2186e 100644 (file)
 #include "reportfile.h"
 #include "qualityscores.h"
 #include "refchimeratest.h"
+#include "filterseqscommand.h"
+
 
 //**********************************************************************************************************************
-vector<string> SeqErrorCommand::getValidParameters(){  
+vector<string> SeqErrorCommand::setParameters(){       
        try {
-               string Array[] =  {"query", "reference", "name", "qfile", "report", "threshold", "inputdir", "outputdir"};
-               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               CommandParameter pquery("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pquery);
+               CommandParameter preference("reference", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(preference);
+               CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "QualReport",false,false); parameters.push_back(pqfile);
+               CommandParameter preport("report", "InputTypes", "", "", "none", "none", "QualReport",false,false); parameters.push_back(preport);
+               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
+               CommandParameter pignorechimeras("ignorechimeras", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pignorechimeras);
+               CommandParameter pthreshold("threshold", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pthreshold);
+               CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
+               CommandParameter psave("save", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psave);
+               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);          }
                return myArray;
        }
        catch(exception& e) {
-               m->errorOut(e, "SeqErrorCommand", "getValidParameters");
+               m->errorOut(e, "SeqErrorCommand", "setParameters");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-SeqErrorCommand::SeqErrorCommand(){    
+string SeqErrorCommand::getHelpString(){       
        try {
-               abort = true; calledHelp = true; 
-               vector<string> tempOutNames;
-               outputTypes["error.summary"] = tempOutNames;
-               outputTypes["error.seq"] = tempOutNames;
-               outputTypes["error.quality"] = tempOutNames;
-               outputTypes["error.qual.forward"] = tempOutNames;
-               outputTypes["error.qual.reverse"] = tempOutNames;
-               outputTypes["error.forward"] = tempOutNames;
-               outputTypes["error.reverse"] = tempOutNames;
-               outputTypes["error.count"] = tempOutNames;
-               outputTypes["error.matrix"] = tempOutNames;
+               string helpString = "";
+               helpString += "The seq.error command reads a query alignment file and a reference alignment file and creates .....\n";
+               helpString += "The fasta parameter...\n";
+               helpString += "The reference parameter...\n";
+               helpString += "The qfile parameter...\n";
+               helpString += "The report parameter...\n";
+               helpString += "The name parameter...\n";
+               helpString += "The ignorechimeras parameter...\n";
+               helpString += "The threshold parameter...\n";
+               helpString += "The processors parameter...\n";
+               helpString += "If the save parameter is set to true the reference sequences will be saved in memory, to clear them later you can use the clear.memory command. Default=f.";
+               helpString += "Example seq.error(...).\n";
+               helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
+               helpString += "For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n";
+               return helpString;
        }
        catch(exception& e) {
-               m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
+               m->errorOut(e, "SeqErrorCommand", "getHelpString");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-vector<string> SeqErrorCommand::getRequiredParameters(){       
+string SeqErrorCommand::getOutputFileNameTag(string type, string inputName=""){        
        try {
-               string Array[] =  {"query","reference"};
-               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
-               return myArray;
+        string outputFileName = "";
+               map<string, vector<string> >::iterator it;
+        
+        //is this a type this command creates
+        it = outputTypes.find(type);
+        if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
+        else {
+            if (type == "errorsummary")            {   outputFileName =  "error.summary";   }
+            else if (type == "errorseq")            {   outputFileName =  "error.seq";   }
+            else if (type == "errorquality")            {   outputFileName =  "error.quality";   }
+            else if (type == "errorqualforward")            {   outputFileName =  "error.qual.forward";   }
+            else if (type == "errorqualreverse")            {   outputFileName =  "error.qual.reverse";   }
+            else if (type == "errorforward")            {   outputFileName =  "error.seq.forward";   }
+            else if (type == "errorreverse")            {   outputFileName =  "error.seq.reverse";   }
+            else if (type == "errorcount")            {   outputFileName =  "error.count";   }
+            else if (type == "errormatrix")            {   outputFileName =  "error.matrix";   }
+            else if (type == "errorchimera")            {   outputFileName =  "error.chimera";   }
+            else if (type == "errorref-query")            {   outputFileName =  "error.ref-query";   }
+            else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
+        }
+        return outputFileName;
        }
        catch(exception& e) {
-               m->errorOut(e, "SeqErrorCommand", "getRequiredParameters");
+               m->errorOut(e, "SeqErrorCommand", "getOutputFileNameTag");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-vector<string> SeqErrorCommand::getRequiredFiles(){    
+SeqErrorCommand::SeqErrorCommand(){    
        try {
-               vector<string> myArray;
-               return myArray;
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["errorsummary"] = tempOutNames;
+               outputTypes["errorseq"] = tempOutNames;
+               outputTypes["errorquality"] = tempOutNames;
+               outputTypes["errorqualforward"] = tempOutNames;
+               outputTypes["errorqualreverse"] = tempOutNames;
+               outputTypes["errorforward"] = tempOutNames;
+               outputTypes["errorreverse"] = tempOutNames;
+               outputTypes["errorcount"] = tempOutNames;
+               outputTypes["errormatrix"] = tempOutNames;
+        outputTypes["errorchimera"] = tempOutNames;
+        outputTypes["errorref-query"] = tempOutNames;
        }
        catch(exception& e) {
-               m->errorOut(e, "SeqErrorCommand", "getRequiredFiles");
+               m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
                exit(1);
        }
 }
@@ -73,17 +121,15 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
        try {
                
                abort = false; calledHelp = false;   
+               rdb = ReferenceDB::getInstance();
                
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
                        string temp;
-                       
-                       //valid paramters for this command
-                       string AlignArray[] =  {"query", "reference", "name", "qfile", "report", "threshold", "inputdir", "ignorechimeras", "outputdir"};
-                       
-                       vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string,string> parameters = parser.getParameters();
@@ -98,15 +144,17 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                        
                        //initialize outputTypes
                        vector<string> tempOutNames;
-                       outputTypes["error.summary"] = tempOutNames;
-                       outputTypes["error.seq"] = tempOutNames;
-                       outputTypes["error.quality"] = tempOutNames;
-                       outputTypes["error.qual.forward"] = tempOutNames;
-                       outputTypes["error.qual.reverse"] = tempOutNames;
-                       outputTypes["error.forward"] = tempOutNames;
-                       outputTypes["error.reverse"] = tempOutNames;
-                       outputTypes["error.count"] = tempOutNames;
-                       outputTypes["error.matrix"] = tempOutNames;
+                       outputTypes["errorsummary"] = tempOutNames;
+                       outputTypes["errorseq"] = tempOutNames;
+                       outputTypes["errorquality"] = tempOutNames;
+                       outputTypes["errorqualforward"] = tempOutNames;
+                       outputTypes["errorqualreverse"] = tempOutNames;
+                       outputTypes["errorforward"] = tempOutNames;
+                       outputTypes["errorreverse"] = tempOutNames;
+                       outputTypes["errorcount"] = tempOutNames;
+                       outputTypes["errormatrix"] = tempOutNames;
+            outputTypes["errorchimera"] = tempOutNames;
+            outputTypes["errorref-query"] = tempOutNames;
 
                        
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
@@ -114,12 +162,12 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                        if (inputDir == "not found"){   inputDir = "";          }
                        else {
                                string path;
-                               it = parameters.find("query");
+                               it = parameters.find("fasta");
                                //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["query"] = inputDir + it->second;            }
+                                       if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
                                }
                                
                                it = parameters.find("reference");
@@ -156,24 +204,33 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                                
                        }
                        //check for required parameters
-                       queryFileName = validParameter.validFile(parameters, "query", true);
-                       if (queryFileName == "not found") { m->mothurOut("query is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
-                       else if (queryFileName == "not open") { abort = true; } 
+                       queryFileName = validParameter.validFile(parameters, "fasta", true);
+                       if (queryFileName == "not found") { 
+                               queryFileName = m->getFastaFile(); 
+                               if (queryFileName != "") { m->mothurOut("Using " + queryFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
+                               else {  m->mothurOut("You have no current fasta file and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
+                       }
+                       else if (queryFileName == "not open") { queryFileName = ""; abort = true; }     
+                       else { m->setFastaFile(queryFileName); }
                        
                        referenceFileName = validParameter.validFile(parameters, "reference", true);
                        if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
                        else if (referenceFileName == "not open") { abort = true; }     
                        
-
                        //check for optional parameters
                        namesFileName = validParameter.validFile(parameters, "name", true);
                        if(namesFileName == "not found"){       namesFileName = "";     }
+                       else if (namesFileName == "not open") { namesFileName = ""; abort = true; }     
+                       else { m->setNameFile(namesFileName); }
                        
                        qualFileName = validParameter.validFile(parameters, "qfile", true);
                        if(qualFileName == "not found"){        qualFileName = "";      }
-
+                       else if (qualFileName == "not open") { qualFileName = ""; abort = true; }       
+                       else { m->setQualFile(qualFileName); }
+                       
                        reportFileName = validParameter.validFile(parameters, "report", true);
                        if(reportFileName == "not found"){      reportFileName = "";    }
+                       else if (reportFileName == "not open") { reportFileName = ""; abort = true; }   
                        
                        if((reportFileName != "" && qualFileName == "") || (reportFileName == "" && qualFileName != "")){
                                m->mothurOut("if you use either a qual file or a report file, you have to have both.");
@@ -190,13 +247,44 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                        //check for optional parameter and set defaults
                        // ...at some point should added some additional type checking...
                        temp = validParameter.validFile(parameters, "threshold", false);        if (temp == "not found") { temp = "1.00"; }
-                       convert(temp, threshold);  
+                       m->mothurConvert(temp, threshold);  
                        
-                       temp = validParameter.validFile(parameters, "ignorechimeras", false);   if (temp == "not found") { temp = "1"; }
-                       convert(temp, ignoreChimeras);  
+                       temp = validParameter.validFile(parameters, "save", false);                     if (temp == "not found"){       temp = "f";                             }
+                       save = m->isTrue(temp); 
+                       rdb->save = save; 
+                       if (save) { //clear out old references
+                               rdb->clearMemory();     
+                       }
+                       
+                       //this has to go after save so that if the user sets save=t and provides no reference we abort
+                       referenceFileName = validParameter.validFile(parameters, "reference", true);
+                       if (referenceFileName == "not found") { 
+                               //check for saved reference sequences
+                               if (rdb->referenceSeqs.size() != 0) {
+                                       referenceFileName = "saved";
+                               }else {
+                                       m->mothurOut("[ERROR]: You don't have any saved reference sequences and the reference parameter is a required."); 
+                                       m->mothurOutEndLine();
+                                       abort = true; 
+                               }
+                       }else if (referenceFileName == "not open") { abort = true; }    
+                       else {  if (save) {     rdb->setSavedReference(referenceFileName);      }       }
+                       
+                       
+                       temp = validParameter.validFile(parameters, "ignorechimeras", false);   if (temp == "not found") { temp = "T"; }
+                       ignoreChimeras = m->isTrue(temp);
+                       
+                       temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
+                       m->setProcessors(temp);
+                       m->mothurConvert(temp, processors); 
 
                        substitutionMatrix.resize(6);
-                       for(int i=0;i<6;i++){   substitutionMatrix[i].assign(6,0);      }
+                       for(int i=0;i<6;i++){   substitutionMatrix[i].resize(6,0);      }
+                       
+                       if ((namesFileName == "") && (queryFileName != "")){
+                               vector<string> files; files.push_back(queryFileName); 
+                               parser.getNameFile(files);
+                       }
                }
        }
        catch(exception& e) {
@@ -204,151 +292,471 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                exit(1);
        }
 }
-
-//**********************************************************************************************************************
-
-void SeqErrorCommand::help(){
-       try {
-               m->mothurOut("The seq.error command reads a query alignment file and a reference alignment file and creates .....\n");
-               m->mothurOut("Example seq.error(...).\n");
-               m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
-               m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n\n");
-       }
-       catch(exception& e) {
-               m->errorOut(e, "SeqErrorCommand", "help");
-               exit(1);
-       }
-}
-
-//***************************************************************************************************************
-
-SeqErrorCommand::~SeqErrorCommand(){
-
-}
-
 //***************************************************************************************************************
 
 int SeqErrorCommand::execute(){
        try{
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
-
-               string errorSummaryFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.summary";
-               m->openOutputFile(errorSummaryFileName, errorSummaryFile);
-               outputNames.push_back(errorSummaryFileName); outputTypes["error.summary"].push_back(errorSummaryFileName);
-               printErrorHeader();
                
-               string errorSeqFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq";
-               m->openOutputFile(errorSeqFileName, errorSeqFile);
-               outputNames.push_back(errorSeqFileName); outputTypes["error.seq"].push_back(errorSeqFileName);
-
+               int start = time(NULL);
+               maxLength = 2000;
+               totalBases = 0;
+               totalMatches = 0;
+               
+        string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
+               string errorSummaryFileName = fileNameRoot + getOutputFileNameTag("errorsummary");
+               outputNames.push_back(errorSummaryFileName); outputTypes["errorsummary"].push_back(errorSummaryFileName);
+                       
+               string errorSeqFileName = fileNameRoot + getOutputFileNameTag("errorseq");
+               outputNames.push_back(errorSeqFileName); outputTypes["errorseq"].push_back(errorSeqFileName);
+               
+               string errorChimeraFileName = fileNameRoot + getOutputFileNameTag("errorchimera");
+               outputNames.push_back(errorChimeraFileName); outputTypes["errorchimera"].push_back(errorChimeraFileName);
+               
                getReferences();        //read in reference sequences - make sure there's no ambiguous bases
 
-               map<string, int> weights;
                if(namesFileName != ""){        weights = getWeights(); }
                
-               ifstream queryFile;
-               m->openInputFile(queryFileName, queryFile);
+               vector<unsigned long long> fastaFilePos;
+               vector<unsigned long long> qFilePos;
+               vector<unsigned long long> reportFilePos;
                
-               ifstream reportFile;
-               ifstream qualFile;
+               setLines(queryFileName, qualFileName, reportFileName, fastaFilePos, qFilePos, reportFilePos);
+               
+               if (m->control_pressed) { return 0; }
+               
+               for (int i = 0; i < (fastaFilePos.size()-1); i++) {
+                       lines.push_back(linePair(fastaFilePos[i], fastaFilePos[(i+1)]));
+                       if (qualFileName != "") {  qLines.push_back(linePair(qFilePos[i], qFilePos[(i+1)]));  }
+                       if (reportFileName != "") {  rLines.push_back(linePair(reportFilePos[i], reportFilePos[(i+1)]));  }
+               }       
+               if(qualFileName == "")  {       qLines = lines; rLines = lines; } //fills with duds
+               
+               int numSeqs = 0;
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+               if(processors == 1){
+                       numSeqs = driver(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName, lines[0], qLines[0], rLines[0]);
+               }else{
+                       numSeqs = createProcesses(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName);
+               }       
+#else
+               numSeqs = driver(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName, lines[0], qLines[0], rLines[0]);
+#endif
+               
+               if(qualFileName != "" && reportFileName != ""){         
+                       printErrorQuality(qScoreErrorMap);
+                       printQualityFR(qualForwardMap, qualReverseMap);
+               }
+               
+               printErrorFRFile(errorForward, errorReverse);
+               
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
 
-               ReportFile report;
-               QualityScores quality;
-               vector<vector<int> > qualForwardMap;
-               vector<vector<int> > qualReverseMap;
+               string errorCountFileName = fileNameRoot + getOutputFileNameTag("errorcount");
+               ofstream errorCountFile;
+               m->openOutputFile(errorCountFileName, errorCountFile);
+               outputNames.push_back(errorCountFileName);  outputTypes["errorcount"].push_back(errorCountFileName);
+               m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n");
+               m->mothurOut("Errors\tSequences\n");
+               errorCountFile << "Errors\tSequences\n";                
+               for(int i=0;i<misMatchCounts.size();i++){
+                       m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
+                       errorCountFile << i << '\t' << misMatchCounts[i] << endl;
+               }
+               errorCountFile.close();
+               
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+
+               printSubMatrix();
+                               
+               string megAlignmentFileName = fileNameRoot + getOutputFileNameTag("errorref-query");
+               ofstream megAlignmentFile;
+               m->openOutputFile(megAlignmentFileName, megAlignmentFile);
+               outputNames.push_back(megAlignmentFileName);  outputTypes["errorref-query"].push_back(megAlignmentFileName);
+               
+               for(int i=0;i<numRefs;i++){
+                       megAlignmentFile << referenceSeqs[i].getInlineSeq() << endl;
+                       megAlignmentFile << megaAlignVector[i] << endl;
+               }
                
-               if(qualFileName != "" && reportFileName != ""){
-                       m->openInputFile(qualFileName, qualFile);
-                       report = ReportFile(reportFile, reportFileName);
+               m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences.");
+               m->mothurOutEndLine();
+               
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
+               m->mothurOutEndLine();
+               
+               return 0;       
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SeqErrorCommand", "execute");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int SeqErrorCommand::createProcesses(string filename, string qFileName, string rFileName, string summaryFileName, string errorOutputFileName, string chimeraOutputFileName) {  
+       try {
+               int process = 1;
+               processIDS.clear();
+               map<char, vector<int> >::iterator it;
+               int num = 0;
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+               
+               //loop through and create all the processes you want
+               while (process != processors) {
+                       int pid = fork();
                        
-                       qualForwardMap.resize(1000);
-                       qualReverseMap.resize(1000);
-                       for(int i=0;i<1000;i++){
-                               qualForwardMap[i].assign(100,0);
-                               qualReverseMap[i].assign(100,0);
-                       }                               
+                       if (pid > 0) {
+                               processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
+                               process++;
+                       }else if (pid == 0){
+                               
+                               num = driver(filename, qFileName, rFileName, summaryFileName + toString(getpid()) + ".temp", errorOutputFileName+ toString(getpid()) + ".temp", chimeraOutputFileName + toString(getpid()) + ".temp", lines[process], qLines[process], rLines[process]);
+                               
+                               //pass groupCounts to parent
+                               ofstream out;
+                               string tempFile = filename + toString(getpid()) + ".info.temp";
+                               m->openOutputFile(tempFile, out);
+                               
+                               //output totalBases and totalMatches
+                               out << num << '\t' << totalBases << '\t' << totalMatches << endl << endl;
+                               
+                               //output substitutionMatrix
+                               for(int i = 0; i < substitutionMatrix.size(); i++) {
+                                       for (int j = 0; j < substitutionMatrix[i].size(); j++) {
+                                               out << substitutionMatrix[i][j] << '\t';
+                                       }
+                                       out << endl;
+                               }
+                               out << endl;
+                               
+                               //output qScoreErrorMap
+                               for (it = qScoreErrorMap.begin(); it != qScoreErrorMap.end(); it++) {
+                                       vector<int> thisScoreErrorMap = it->second;
+                                       out << it->first << '\t';
+                                       for (int i = 0; i < thisScoreErrorMap.size(); i++) {
+                                               out << thisScoreErrorMap[i] << '\t';
+                                       }
+                                       out << endl;
+                               }
+                               out << endl;
+                               
+                               //output qualForwardMap
+                               for(int i = 0; i < qualForwardMap.size(); i++) {
+                                       for (int j = 0; j < qualForwardMap[i].size(); j++) {
+                                               out << qualForwardMap[i][j] << '\t';
+                                       }
+                                       out << endl;
+                               }
+                               out << endl;
+                               
+                               //output qualReverseMap
+                               for(int i = 0; i < qualReverseMap.size(); i++) {
+                                       for (int j = 0; j < qualReverseMap[i].size(); j++) {
+                                               out << qualReverseMap[i][j] << '\t';
+                                       }
+                                       out << endl;
+                               }
+                               out << endl;
+                               
+                               
+                               //output errorForward
+                               for (it = errorForward.begin(); it != errorForward.end(); it++) {
+                                       vector<int> thisErrorForward = it->second;
+                                       out << it->first << '\t';
+                                       for (int i = 0; i < thisErrorForward.size(); i++) {
+                                               out << thisErrorForward[i] << '\t';
+                                       }
+                                       out << endl;
+                               }
+                               out << endl;
+                               
+                               //output errorReverse
+                               for (it = errorReverse.begin(); it != errorReverse.end(); it++) {
+                                       vector<int> thisErrorReverse = it->second;
+                                       out << it->first << '\t';
+                                       for (int i = 0; i < thisErrorReverse.size(); i++) {
+                                               out << thisErrorReverse[i] << '\t';
+                                       }
+                                       out << endl;
+                               }
+                               out << endl;
+                               
+                               //output misMatchCounts
+                               out << misMatchCounts.size() << endl;
+                               for (int j = 0; j < misMatchCounts.size(); j++) {
+                                       out << misMatchCounts[j] << '\t';
+                               }
+                               out << endl;
+                               
+                               
+                               //output megaAlignVector
+                               for (int j = 0; j < megaAlignVector.size(); j++) {
+                                       out << megaAlignVector[j] << endl;
+                               }
+                               out << endl;
+                               
+                               out.close();
+                               
+                               exit(0);
+                       }else { 
+                               m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
+                               for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                               exit(0);
+                       }
                }
                
-               int totalBases = 0;
-               int totalMatches = 0;
+               //do my part
+               num = driver(filename, qFileName, rFileName, summaryFileName, errorOutputFileName, chimeraOutputFileName, lines[0], qLines[0], rLines[0]);
                
-               vector<int> misMatchCounts(11, 0);
+               //force parent to wait until all the processes are done
+               for (int i=0;i<processIDS.size();i++) { 
+                       int temp = processIDS[i];
+                       wait(&temp);
+               }
+               
+               //append files
+               for(int i=0;i<processIDS.size();i++){
+                       
+                       m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
+                       
+                       m->appendFiles((summaryFileName + toString(processIDS[i]) + ".temp"), summaryFileName);
+                       m->mothurRemove((summaryFileName + toString(processIDS[i]) + ".temp"));
+                       m->appendFiles((errorOutputFileName + toString(processIDS[i]) + ".temp"), errorOutputFileName);
+                       m->mothurRemove((errorOutputFileName + toString(processIDS[i]) + ".temp"));
+                       m->appendFiles((chimeraOutputFileName + toString(processIDS[i]) + ".temp"), chimeraOutputFileName);
+                       m->mothurRemove((chimeraOutputFileName + toString(processIDS[i]) + ".temp"));
+                       
+                       ifstream in;
+                       string tempFile =  filename + toString(processIDS[i]) + ".info.temp";
+                       m->openInputFile(tempFile, in);
+                       
+                       //input totalBases and totalMatches
+                       int tempBases, tempMatches, tempNumSeqs;
+                       in >> tempNumSeqs >> tempBases >> tempMatches; m->gobble(in);
+                       totalBases += tempBases; totalMatches += tempMatches; num += tempNumSeqs;
+                       
+                       //input substitutionMatrix
+                       int tempNum;
+                       for(int i = 0; i < substitutionMatrix.size(); i++) {
+                               for (int j = 0; j < substitutionMatrix[i].size(); j++) {
+                                       in >> tempNum; substitutionMatrix[i][j] += tempNum;
+                               }
+                               m->gobble(in);
+                       }
+                       m->gobble(in);
+                       
+                       //input qScoreErrorMap
+                       char first;
+                       for (int i = 0; i < qScoreErrorMap.size(); i++) {
+                               in >> first;
+                               vector<int> thisScoreErrorMap = qScoreErrorMap[first];
+                               
+                               for (int i = 0; i < thisScoreErrorMap.size(); i++) {
+                                       in >> tempNum; thisScoreErrorMap[i] += tempNum;
+                               }
+                               qScoreErrorMap[first] = thisScoreErrorMap;
+                               m->gobble(in);
+                       }
+                       m->gobble(in);
+                       
+                       //input qualForwardMap
+                       for(int i = 0; i < qualForwardMap.size(); i++) {
+                               for (int j = 0; j < qualForwardMap[i].size(); j++) {
+                                       in >> tempNum; qualForwardMap[i][j] += tempNum;
+                               }
+                               m->gobble(in);
+                       }
+                       m->gobble(in);
+                       
+                       //input qualReverseMap
+                       for(int i = 0; i < qualReverseMap.size(); i++) {
+                               for (int j = 0; j < qualReverseMap[i].size(); j++) {
+                                       in >> tempNum; qualReverseMap[i][j] += tempNum;
+                               }
+                               m->gobble(in);
+                       }
+                       m->gobble(in);
+                       
+                       //input errorForward
+                       for (int i = 0; i < errorForward.size(); i++) {
+                               in >> first;
+                               vector<int> thisErrorForward = errorForward[first];
+                               
+                               for (int i = 0; i < thisErrorForward.size(); i++) {
+                                       in >> tempNum; thisErrorForward[i] += tempNum;
+                               }
+                               errorForward[first] = thisErrorForward;
+                               m->gobble(in);
+                       }
+                       m->gobble(in);
+                       
+                       //input errorReverse
+                       for (int i = 0; i < errorReverse.size(); i++) {
+                               in >> first;
+                               vector<int> thisErrorReverse = errorReverse[first];
+                               
+                               for (int i = 0; i < thisErrorReverse.size(); i++) {
+                                       in >> tempNum; thisErrorReverse[i] += tempNum;
+                               }
+                               errorReverse[first] = thisErrorReverse;
+                               m->gobble(in);
+                       }
+                       m->gobble(in);
+                       
+                       //input misMatchCounts
+                       int misMatchSize;
+                       in >> misMatchSize; m->gobble(in);
+                       if (misMatchSize > misMatchCounts.size()) {     misMatchCounts.resize(misMatchSize, 0); }
+                       for (int j = 0; j < misMatchCounts.size(); j++) {
+                               in >> tempNum; misMatchCounts[j] += tempNum;
+                       }
+                       m->gobble(in);
+                       
+                       //input megaAlignVector
+                       string thisLine;
+                       for (int j = 0; j < megaAlignVector.size(); j++) {
+                               thisLine = m->getline(in); m->gobble(in); megaAlignVector[j] += thisLine + '\n';
+                       }
+                       m->gobble(in);
+                       
+                       in.close(); m->mothurRemove(tempFile);
+                       
+               }
+#endif         
+               return num;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SeqErrorCommand", "createProcesses");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int SeqErrorCommand::driver(string filename, string qFileName, string rFileName, string summaryFileName, string errorOutputFileName, string chimeraOutputFileName, linePair line, linePair qline, linePair rline) {    
+       
+       try {
+               ReportFile report;
+               QualityScores quality;
+               
+               misMatchCounts.resize(11, 0);
                int maxMismatch = 0;
                int numSeqs = 0;
                
                map<string, int>::iterator it;
-               map<char, vector<int> > qScoreErrorMap;
                qScoreErrorMap['m'].assign(41, 0);
                qScoreErrorMap['s'].assign(41, 0);
                qScoreErrorMap['i'].assign(41, 0);
                qScoreErrorMap['a'].assign(41, 0);
                
-               map<char, vector<int> > errorForward;
-               errorForward['m'].assign(1000,0);
-               errorForward['s'].assign(1000,0);
-               errorForward['i'].assign(1000,0);
-               errorForward['d'].assign(1000,0);
-               errorForward['a'].assign(1000,0);
+               errorForward['m'].assign(maxLength,0);
+               errorForward['s'].assign(maxLength,0);
+               errorForward['i'].assign(maxLength,0);
+               errorForward['d'].assign(maxLength,0);
+               errorForward['a'].assign(maxLength,0);
                
-               map<char, vector<int> > errorReverse;
-               errorReverse['m'].assign(1000,0);
-               errorReverse['s'].assign(1000,0);
-               errorReverse['i'].assign(1000,0);
-               errorReverse['d'].assign(1000,0);
-               errorReverse['a'].assign(1000,0);       
+               errorReverse['m'].assign(maxLength,0);
+               errorReverse['s'].assign(maxLength,0);
+               errorReverse['i'].assign(maxLength,0);
+               errorReverse['d'].assign(maxLength,0);
+               errorReverse['a'].assign(maxLength,0);  
                
-
-               string errorChimeraFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.chimera";
-               RefChimeraTest chimeraTest(referenceSeqs, errorChimeraFileName);
-               outputNames.push_back(errorChimeraFileName); outputTypes["error.chimera"].push_back(errorChimeraFileName);
+               //open inputfiles and go to beginning place for this processor
+               ifstream queryFile;
+               m->openInputFile(filename, queryFile);
+               queryFile.seekg(line.start);
+               
+               ifstream reportFile;
+               ifstream qualFile;
+               if(qFileName != "" && rFileName != ""){
+                       m->openInputFile(qFileName, qualFile);
+                       qualFile.seekg(qline.start);  
+                       
+                       //gobble headers
+                       if (rline.start == 0) {  report = ReportFile(reportFile, rFileName); } 
+                       else{
+                               m->openInputFile(rFileName, reportFile);
+                               reportFile.seekg(rline.start); 
+                       }
+                       
+                       qualForwardMap.resize(maxLength);
+                       qualReverseMap.resize(maxLength);
+                       for(int i=0;i<maxLength;i++){
+                               qualForwardMap[i].assign(41,0);
+                               qualReverseMap[i].assign(41,0);
+                       }       
+               }
+               
+               ofstream outChimeraReport;
+               m->openOutputFile(chimeraOutputFileName, outChimeraReport);
+               RefChimeraTest chimeraTest(referenceSeqs);
+               if (line.start == 0) { chimeraTest.printHeader(outChimeraReport); }
+               
+               ofstream errorSummaryFile;
+               m->openOutputFile(summaryFileName, errorSummaryFile);
+               if (line.start == 0) { printErrorHeader(errorSummaryFile); }
+               
+               ofstream errorSeqFile;
+               m->openOutputFile(errorOutputFileName, errorSeqFile);
+               
+               megaAlignVector.resize(numRefs, "");
                
                int index = 0;
                bool ignoreSeq = 0;
                
-               while(queryFile){
+               bool moreSeqs = 1;
+               while (moreSeqs) {
+                       
+                       if (m->control_pressed) { queryFile.close(); if(qFileName != "" && rFileName != ""){  reportFile.close(); qualFile.close(); } outChimeraReport.close(); errorSummaryFile.close();errorSeqFile.close(); return 0; }
                        
-                       if (m->control_pressed) { errorSummaryFile.close();     errorSeqFile.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
-               
                        Sequence query(queryFile);
-                                               
-                       int numParentSeqs = chimeraTest.analyzeQuery(query.getName(), query.getAligned());
+                       
+                       int numParentSeqs = chimeraTest.analyzeQuery(query.getName(), query.getAligned(), outChimeraReport);
                        int closestRefIndex = chimeraTest.getClosestRefIndex();
-
+                       
                        if(numParentSeqs > 1 && ignoreChimeras == 1)    {       ignoreSeq = 1;  }
                        else                                                                                    {       ignoreSeq = 0;  }
-
-
-                       Compare minCompare = getErrors(query, referenceSeqs[closestRefIndex]);
+                       
+                       Compare minCompare;
+            getErrors(query, referenceSeqs[closestRefIndex], minCompare);
                        
                        if(namesFileName != ""){
                                it = weights.find(query.getName());
                                minCompare.weight = it->second;
                        }
-                       else    {       minCompare.weight = 1;  }
-
-                       printErrorData(minCompare, numParentSeqs);
-
+                       else{   minCompare.weight = 1;  }
+                       
+                       printErrorData(minCompare, numParentSeqs, errorSummaryFile, errorSeqFile);
+                       
                        if(!ignoreSeq){
-                               for(int i=0;i<minCompare.total;i++){
+                               
+                               for(int i=0;i<minCompare.sequence.length();i++){
                                        char letter = minCompare.sequence[i];
-                                       errorForward[letter][i] += minCompare.weight;
-                                       errorReverse[letter][minCompare.total-i-1] += minCompare.weight;                                
+                                       
+                                       if(letter != 'r'){
+                                               errorForward[letter][i] += minCompare.weight;
+                                               errorReverse[letter][minCompare.total-i-1] += minCompare.weight;                                
+                                       }
                                }
                        }
                        
                        if(qualFileName != "" && reportFileName != ""){
                                report = ReportFile(reportFile);
                                
-                               int origLength = report.getQueryLength();
+                               //                              int origLength = report.getQueryLength();
                                int startBase = report.getQueryStart();
                                int endBase = report.getQueryEnd();
-
+                               
                                quality = QualityScores(qualFile);
-
+                               
                                if(!ignoreSeq){
+//                                     cout << query.getName() << '\t';
+                                       
                                        quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
                                        quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
                                        quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
+
+//                                     cout << endl;
                                }
                        }                       
                        
@@ -361,78 +769,103 @@ int SeqErrorCommand::execute(){
                                }                               
                                misMatchCounts[minCompare.mismatches] += minCompare.weight;
                                numSeqs++;
+                               
+                               megaAlignVector[closestRefIndex] += query.getInlineSeq() + '\n';
                        }
                        
                        index++;
-                       if(index % 1000 == 0){  cout << index << endl;  }
+                       
+                       #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+                               unsigned long long pos = queryFile.tellg();
+                               if ((pos == -1) || (pos >= line.end)) { break; }
+                       #else
+                               if (queryFile.eof()) { break; }
+                       #endif
+                       
+                       if(index % 100 == 0){   m->mothurOut(toString(index) + '\n');   }
                }
                queryFile.close();
+               if(qFileName != "" && rFileName != ""){  reportFile.close(); qualFile.close(); }
                errorSummaryFile.close();       
                errorSeqFile.close();
-
-               if(qualFileName != "" && reportFileName != ""){         
-                       printErrorQuality(qScoreErrorMap);
-                       printQualityFR(qualForwardMap, qualReverseMap);
-               }
                
-               printErrorFRFile(errorForward, errorReverse);
+               //report progress
+               if(index % 100 != 0){   m->mothurOut(toString(index) + '\n');   }
                
-               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
-
-               string errorCountFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.count";
-               ofstream errorCountFile;
-               m->openOutputFile(errorCountFileName, errorCountFile);
-               outputNames.push_back(errorCountFileName);  outputTypes["error.count"].push_back(errorCountFileName);
-               m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n\n");
-               m->mothurOut("Errors\tSequences\n");
-               errorCountFile << "Errors\tSequences\n";                
-               for(int i=0;i<misMatchCounts.size();i++){
-                       m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
-                       errorCountFile << i << '\t' << misMatchCounts[i] << endl;
-               }
-               errorCountFile.close();
-               
-               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
-
-               printSubMatrix();
-                               
-               m->mothurOutEndLine();
-               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
-               for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
-               m->mothurOutEndLine();
-               
-               return 0;       
+               return index;
        }
        catch(exception& e) {
-               m->errorOut(e, "SeqErrorCommand", "execute");
+               m->errorOut(e, "SeqErrorCommand", "driver");
                exit(1);
        }
 }
-
 //***************************************************************************************************************
 
 void SeqErrorCommand::getReferences(){
        try {
+               int numAmbigSeqs = 0;
                
-               ifstream referenceFile;
-               m->openInputFile(referenceFileName, referenceFile);
+               int maxStartPos = 0;
+               int minEndPos = 100000;
                
-               int numAmbigSeqs = 0;
+               if (referenceFileName == "saved") {
+                       int start = time(NULL);
+                       m->mothurOutEndLine();  m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory.");        m->mothurOutEndLine();
+                       
+                       for (int i = 0; i < rdb->referenceSeqs.size(); i++) {
+                               int numAmbigs = rdb->referenceSeqs[i].getAmbigBases();
+                               if(numAmbigs > 0){      numAmbigSeqs++; }
+                               
+                               //                      int startPos = rdb->referenceSeqs[i].getStartPos();
+                               //                      if(startPos > maxStartPos)      {       maxStartPos = startPos; }
+                               //
+                               //                      int endPos = rdb->referenceSeqs[i].getEndPos();
+                               //                      if(endPos < minEndPos)          {       minEndPos = endPos;             }                               
+                               
+                               referenceSeqs.push_back(rdb->referenceSeqs[i]);
+                       }
+                       referenceFileName = rdb->getSavedReference();
+                       
+                       m->mothurOut("It took " + toString(time(NULL) - start) + " to load " + toString(rdb->referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();  
                
-               while(referenceFile){
-                       Sequence currentSeq(referenceFile);
-                       int numAmbigs = currentSeq.getAmbigBases();
-                       if(numAmbigs > 0){      numAmbigSeqs++; }
-                       referenceSeqs.push_back(currentSeq);
-                       m->gobble(referenceFile);
+               }else {
+                       int start = time(NULL);
+
+                       ifstream referenceFile;
+                       m->openInputFile(referenceFileName, referenceFile);
+                       
+                       while(referenceFile){
+                               Sequence currentSeq(referenceFile);
+                               int numAmbigs = currentSeq.getAmbigBases();
+                               if(numAmbigs > 0){      numAmbigSeqs++; }
+                               
+       //                      int startPos = currentSeq.getStartPos();
+       //                      if(startPos > maxStartPos)      {       maxStartPos = startPos; }
+       //
+       //                      int endPos = currentSeq.getEndPos();
+       //                      if(endPos < minEndPos)          {       minEndPos = endPos;             }
+                               referenceSeqs.push_back(currentSeq);
+                               
+                               if (rdb->save) { rdb->referenceSeqs.push_back(currentSeq); }
+                                       
+                               m->gobble(referenceFile);
+                       }
+                       referenceFile.close();
+                       
+                       m->mothurOut("It took " + toString(time(NULL) - start) + " to read " + toString(referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();  
+               }
+               
+               numRefs = referenceSeqs.size();
+               
+               for(int i=0;i<numRefs;i++){
+                       referenceSeqs[i].padToPos(maxStartPos);
+                       referenceSeqs[i].padFromPos(minEndPos);
                }
-               referenceFile.close();
                
                if(numAmbigSeqs != 0){
                        m->mothurOut("Warning: " + toString(numAmbigSeqs) + " reference sequences have ambiguous bases, these bases will be ignored\n");
-               }
+               }       
                
-               numRefs = referenceSeqs.size();
        }
        catch(exception& e) {
                m->errorOut(e, "SeqErrorCommand", "getReferences");
@@ -442,7 +875,7 @@ void SeqErrorCommand::getReferences(){
 
 //***************************************************************************************************************
 
-Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
+int SeqErrorCommand::getErrors(Sequence query, Sequence reference, Compare& errors){
        try {
                if(query.getAlignLength() != reference.getAlignLength()){
                        m->mothurOut("Warning: " + toString(query.getName()) + " and " + toString(reference.getName()) + " are different lengths\n");
@@ -453,74 +886,87 @@ Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
                string r = reference.getAligned();
 
                int started = 0;
-               Compare errors;
+               //Compare errors;
 
                for(int i=0;i<alignLength;i++){
-                       if(q[i] != 'N' && q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){                  //      no missing data and no double gaps
-                               started = 1;
-                               
-                               if(q[i] == 'A'){
-                                       if(r[i] == 'A'){        errors.AA++;    errors.matches++;       errors.sequence += 'm'; }
-                                       if(r[i] == 'T'){        errors.AT++;    errors.sequence += 's'; }
-                                       if(r[i] == 'G'){        errors.AG++;    errors.sequence += 's'; }
-                                       if(r[i] == 'C'){        errors.AC++;    errors.sequence += 's'; }
-                                       if(r[i] == '-'){        errors.Ai++;    errors.sequence += 'i'; }
-                               }
-                               else if(q[i] == 'T'){
-                                       if(r[i] == 'A'){        errors.TA++;    errors.sequence += 's'; }
-                                       if(r[i] == 'T'){        errors.TT++;    errors.matches++;       errors.sequence += 'm'; }
-                                       if(r[i] == 'G'){        errors.TG++;    errors.sequence += 's'; }
-                                       if(r[i] == 'C'){        errors.TC++;    errors.sequence += 's'; }
-                                       if(r[i] == '-'){        errors.Ti++;    errors.sequence += 'i'; }
-                               }
-                               else if(q[i] == 'G'){
-                                       if(r[i] == 'A'){        errors.GA++;    errors.sequence += 's'; }
-                                       if(r[i] == 'T'){        errors.GT++;    errors.sequence += 's'; }
-                                       if(r[i] == 'G'){        errors.GG++;    errors.matches++;       errors.sequence += 'm'; }
-                                       if(r[i] == 'C'){        errors.GC++;    errors.sequence += 's'; }
-                                       if(r[i] == '-'){        errors.Gi++;    errors.sequence += 'i'; }
-                               }
-                               else if(q[i] == 'C'){
-                                       if(r[i] == 'A'){        errors.CA++;    errors.sequence += 's'; }
-                                       if(r[i] == 'T'){        errors.CT++;    errors.sequence += 's'; }
-                                       if(r[i] == 'G'){        errors.CG++;    errors.sequence += 's'; }
-                                       if(r[i] == 'C'){        errors.CC++;    errors.matches++;       errors.sequence += 'm'; }
-                                       if(r[i] == '-'){        errors.Ci++;    errors.sequence += 'i'; }
+//                     cout << r[i] << '\t' << q[i] << '\t';
+                       if(q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){                 //      no missing data and no double gaps
+                               if(r[i] != 'N'){
+                                       started = 1;
+                                       
+                                       if(q[i] == 'A'){
+                                               if(r[i] == 'A'){        errors.AA++;    errors.matches++;       errors.sequence += 'm'; }
+                                               if(r[i] == 'T'){        errors.AT++;    errors.sequence += 's'; }
+                                               if(r[i] == 'G'){        errors.AG++;    errors.sequence += 's'; }
+                                               if(r[i] == 'C'){        errors.AC++;    errors.sequence += 's'; }
+                                               if(r[i] == '-'){        errors.Ai++;    errors.sequence += 'i'; }
+                                       }
+                                       else if(q[i] == 'T'){
+                                               if(r[i] == 'A'){        errors.TA++;    errors.sequence += 's'; }
+                                               if(r[i] == 'T'){        errors.TT++;    errors.matches++;       errors.sequence += 'm'; }
+                                               if(r[i] == 'G'){        errors.TG++;    errors.sequence += 's'; }
+                                               if(r[i] == 'C'){        errors.TC++;    errors.sequence += 's'; }
+                                               if(r[i] == '-'){        errors.Ti++;    errors.sequence += 'i'; }
+                                       }
+                                       else if(q[i] == 'G'){
+                                               if(r[i] == 'A'){        errors.GA++;    errors.sequence += 's'; }
+                                               if(r[i] == 'T'){        errors.GT++;    errors.sequence += 's'; }
+                                               if(r[i] == 'G'){        errors.GG++;    errors.matches++;       errors.sequence += 'm'; }
+                                               if(r[i] == 'C'){        errors.GC++;    errors.sequence += 's'; }
+                                               if(r[i] == '-'){        errors.Gi++;    errors.sequence += 'i'; }
+                                       }
+                                       else if(q[i] == 'C'){
+                                               if(r[i] == 'A'){        errors.CA++;    errors.sequence += 's'; }
+                                               if(r[i] == 'T'){        errors.CT++;    errors.sequence += 's'; }
+                                               if(r[i] == 'G'){        errors.CG++;    errors.sequence += 's'; }
+                                               if(r[i] == 'C'){        errors.CC++;    errors.matches++;       errors.sequence += 'm'; }
+                                               if(r[i] == '-'){        errors.Ci++;    errors.sequence += 'i'; }
+                                       }
+                                       else if(q[i] == 'N'){
+                                               if(r[i] == 'A'){        errors.NA++;    errors.sequence += 'a'; }
+                                               if(r[i] == 'T'){        errors.NT++;    errors.sequence += 'a'; }
+                                               if(r[i] == 'G'){        errors.NG++;    errors.sequence += 'a'; }
+                                               if(r[i] == 'C'){        errors.NC++;    errors.sequence += 'a'; }
+                                               if(r[i] == '-'){        errors.Ni++;    errors.sequence += 'a'; }
+                                       }
+                                       else if(q[i] == '-' && r[i] != '-'){
+                                               if(r[i] == 'A'){        errors.dA++;    errors.sequence += 'd'; }
+                                               if(r[i] == 'T'){        errors.dT++;    errors.sequence += 'd'; }
+                                               if(r[i] == 'G'){        errors.dG++;    errors.sequence += 'd'; }
+                                               if(r[i] == 'C'){        errors.dC++;    errors.sequence += 'd'; }
+                                       }
+                                       errors.total++; 
                                }
-                               else if(q[i] == 'N'){
-                                       if(r[i] == 'A'){        errors.NA++;    errors.sequence += 'a'; }
-                                       if(r[i] == 'T'){        errors.NT++;    errors.sequence += 'a'; }
-                                       if(r[i] == 'G'){        errors.NG++;    errors.sequence += 'a'; }
-                                       if(r[i] == 'C'){        errors.NC++;    errors.sequence += 'a'; }
-                                       if(r[i] == '-'){        errors.Ni++;    errors.sequence += 'a'; }
+                               else{
+                                       
+                                       if(q[i] == '-'){
+                                               errors.sequence += 'd'; errors.total++;
+                                       }                                               
+                                       else{
+                                               errors.sequence += 'r';
+                                       }
                                }
-                               else if(q[i] == '-' && r[i] != '-'){
-                                       if(r[i] == 'A'){        errors.dA++;    errors.sequence += 'd'; }
-                                       if(r[i] == 'T'){        errors.dT++;    errors.sequence += 'd'; }
-                                       if(r[i] == 'G'){        errors.dG++;    errors.sequence += 'd'; }
-                                       if(r[i] == 'C'){        errors.dC++;    errors.sequence += 'd'; }
-                               }
-                               errors.total++; 
-                               
                        }
+                               
                        else if(q[i] == '.' && r[i] != '.'){            //      reference extends beyond query
                                if(started == 1){       break;  }
                        }
                        else if(q[i] != '.' && r[i] == '.'){            //      query extends beyond reference
-//                             m->mothurOut("Warning: " + toString(query.getName()) + " extend beyond " + toString(reference.getName()) + ".  Ignoring the extra bases in the query\n");
                                if(started == 1){       break;  }
                        }
                        else if(q[i] == '.' && r[i] == '.'){            //      both are missing data
                                if(started == 1){       break;  }                       
                        }
-                       
+//                     cout << errors.sequence[errors.sequence.length()-1] << endl;
                }
+//             cout << errors.sequence << endl;
                errors.mismatches = errors.total-errors.matches;
                errors.errorRate = (double)(errors.total-errors.matches) / (double)errors.total;
                errors.queryName = query.getName();
                errors.refName = reference.getName();
                
-               return errors;
+               //return errors;
+        return 0;
        }
        catch(exception& e) {
                m->errorOut(e, "SeqErrorCommand", "getErrors");
@@ -549,7 +995,7 @@ map<string, int> SeqErrorCommand::getWeights(){
 
 //***************************************************************************************************************
 
-void SeqErrorCommand::printErrorHeader(){
+void SeqErrorCommand::printErrorHeader(ofstream& errorSummaryFile){
        try {
                errorSummaryFile << "query\treference\tweight\t";
                errorSummaryFile << "AA\tAT\tAG\tAC\tTA\tTT\tTG\tTC\tGA\tGT\tGG\tGC\tCA\tCT\tCG\tCC\tNA\tNT\tNG\tNC\tAi\tTi\tGi\tCi\tNi\tdA\tdT\tdG\tdC\t";
@@ -566,15 +1012,16 @@ void SeqErrorCommand::printErrorHeader(){
 
 //***************************************************************************************************************
 
-void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs){
+void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs, ofstream& errorSummaryFile, ofstream& errorSeqFile){
        try {
+
                errorSummaryFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
                errorSummaryFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
                errorSummaryFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
                errorSummaryFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
                errorSummaryFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
                errorSummaryFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
-               errorSummaryFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t' ;
+               errorSummaryFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t';
                errorSummaryFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
                
                errorSummaryFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t';                  //insertions
@@ -582,12 +1029,12 @@ void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs){
                errorSummaryFile << error.mismatches - (error.Ai + error.Ti + error.Gi + error.Ci) - (error.dA + error.dT + error.dG + error.dC) - (error.NA + error.NT + error.NG + error.NC + error.Ni) << '\t';      //substitutions
                errorSummaryFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t';       //ambiguities
                errorSummaryFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << '\t' << numParentSeqs << endl;
-               
+
                errorSeqFile << '>' << error.queryName << "\tref:" << error.refName << '\n' << error.sequence << endl;
                
-               
                int a=0;                int t=1;                int g=2;                int c=3;
                int gap=4;              int n=5;
+
                if(numParentSeqs == 1 || ignoreChimeras == 0){
                        substitutionMatrix[a][a] += error.weight * error.AA;
                        substitutionMatrix[a][t] += error.weight * error.TA;
@@ -595,7 +1042,7 @@ void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs){
                        substitutionMatrix[a][c] += error.weight * error.CA;
                        substitutionMatrix[a][gap] += error.weight * error.dA;
                        substitutionMatrix[a][n] += error.weight * error.NA;
-
+                       
                        substitutionMatrix[t][a] += error.weight * error.AT;
                        substitutionMatrix[t][t] += error.weight * error.TT;
                        substitutionMatrix[t][g] += error.weight * error.GT;
@@ -634,10 +1081,11 @@ void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs){
 
 void SeqErrorCommand::printSubMatrix(){
        try {
-               string subMatrixFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.matrix";
+        string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
+               string subMatrixFileName = fileNameRoot + getOutputFileNameTag("errormatrix");
                ofstream subMatrixFile;
                m->openOutputFile(subMatrixFileName, subMatrixFile);
-               outputNames.push_back(subMatrixFileName);  outputTypes["error.matrix"].push_back(subMatrixFileName);
+               outputNames.push_back(subMatrixFileName);  outputTypes["errormatrix"].push_back(subMatrixFileName);
                vector<string> bases(6);
                bases[0] = "A";
                bases[1] = "T";
@@ -680,13 +1128,14 @@ void SeqErrorCommand::printSubMatrix(){
 
 void SeqErrorCommand::printErrorFRFile(map<char, vector<int> > errorForward, map<char, vector<int> > errorReverse){
        try{
-               string errorForwardFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq.forward";
+        string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
+               string errorForwardFileName = fileNameRoot + getOutputFileNameTag("errorforward");
                ofstream errorForwardFile;
                m->openOutputFile(errorForwardFileName, errorForwardFile);
-               outputNames.push_back(errorForwardFileName);  outputTypes["error.forward"].push_back(errorForwardFileName);
+               outputNames.push_back(errorForwardFileName);  outputTypes["errorforward"].push_back(errorForwardFileName);
 
                errorForwardFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
-               for(int i=0;i<1000;i++){
+               for(int i=0;i<maxLength;i++){
                        float match = (float)errorForward['m'][i];
                        float subst = (float)errorForward['s'][i];
                        float insert = (float)errorForward['i'][i];
@@ -698,13 +1147,13 @@ void SeqErrorCommand::printErrorFRFile(map<char, vector<int> > errorForward, map
                }
                errorForwardFile.close();
 
-               string errorReverseFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq.reverse";
+               string errorReverseFileName = fileNameRoot + getOutputFileNameTag("errorreverse");
                ofstream errorReverseFile;
                m->openOutputFile(errorReverseFileName, errorReverseFile);
-               outputNames.push_back(errorReverseFileName);  outputTypes["error.reverse"].push_back(errorReverseFileName);
+               outputNames.push_back(errorReverseFileName);  outputTypes["errorreverse"].push_back(errorReverseFileName);
 
                errorReverseFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
-               for(int i=0;i<1000;i++){
+               for(int i=0;i<maxLength;i++){
                        float match = (float)errorReverse['m'][i];
                        float subst = (float)errorReverse['s'][i];
                        float insert = (float)errorReverse['i'][i];
@@ -726,11 +1175,11 @@ void SeqErrorCommand::printErrorFRFile(map<char, vector<int> > errorForward, map
 
 void SeqErrorCommand::printErrorQuality(map<char, vector<int> > qScoreErrorMap){
        try{
-
-               string errorQualityFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.quality";
+        string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
+               string errorQualityFileName = fileNameRoot + getOutputFileNameTag("errorquality");
                ofstream errorQualityFile;
                m->openOutputFile(errorQualityFileName, errorQualityFile);
-               outputNames.push_back(errorQualityFileName);  outputTypes["error.quality"].push_back(errorQualityFileName);
+               outputNames.push_back(errorQualityFileName);  outputTypes["errorquality"].push_back(errorQualityFileName);
 
                errorQualityFile << "qscore\tmatches\tsubstitutions\tinsertions\tambiguous" << endl;
                for(int i=0;i<41;i++){
@@ -748,31 +1197,29 @@ void SeqErrorCommand::printErrorQuality(map<char, vector<int> > qScoreErrorMap){
 //***************************************************************************************************************
 
 void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector<vector<int> > qualReverseMap){
-       try{
 
-
-               int lastRow = 0;
-               int lastColumn = 0;
+       try{
+               int numRows = 0;
+               int numColumns = qualForwardMap[0].size();
 
                for(int i=0;i<qualForwardMap.size();i++){
-                       for(int j=0;j<qualForwardMap[i].size();j++){
+                       for(int j=0;j<numColumns;j++){
                                if(qualForwardMap[i][j] != 0){
-                                       if(lastRow < i)         {       lastRow = i+2;          }
-                                       if(lastColumn < j)      {       lastColumn = j+2;       }
+                                       if(numRows < i)         {       numRows = i+20;         }
                                }
                        }
                }
-
-               string qualityForwardFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.qual.forward";
+        string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
+               string qualityForwardFileName = fileNameRoot + getOutputFileNameTag("errorqualforward");
                ofstream qualityForwardFile;
                m->openOutputFile(qualityForwardFileName, qualityForwardFile);
-               outputNames.push_back(qualityForwardFileName);  outputTypes["error.qual.forward"].push_back(qualityForwardFileName);
+               outputNames.push_back(qualityForwardFileName);  outputTypes["errorqualforward"].push_back(qualityForwardFileName);
 
-               for(int i=0;i<lastColumn;i++){  qualityForwardFile << '\t' << i;        }       qualityForwardFile << endl;
+               for(int i=0;i<numColumns;i++){  qualityForwardFile << '\t' << i;        }       qualityForwardFile << endl;
 
-               for(int i=0;i<lastRow;i++){
+               for(int i=0;i<numRows;i++){
                        qualityForwardFile << i+1;
-                       for(int j=0;j<lastColumn;j++){
+                       for(int j=0;j<numColumns;j++){
                                qualityForwardFile << '\t' << qualForwardMap[i][j];
                        }
 
@@ -781,16 +1228,16 @@ void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector
                qualityForwardFile.close();
 
                
-               string qualityReverseFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.qual.reverse";
+               string qualityReverseFileName = fileNameRoot + getOutputFileNameTag("errorqualreverse");
                ofstream qualityReverseFile;
                m->openOutputFile(qualityReverseFileName, qualityReverseFile);
-               outputNames.push_back(qualityReverseFileName);  outputTypes["error.qual.reverse"].push_back(qualityReverseFileName);
+               outputNames.push_back(qualityReverseFileName);  outputTypes["errorqualreverse"].push_back(qualityReverseFileName);
                
-               for(int i=0;i<lastColumn;i++){  qualityReverseFile << '\t' << i;        }       qualityReverseFile << endl;
-               for(int i=0;i<lastRow;i++){
+               for(int i=0;i<numColumns;i++){  qualityReverseFile << '\t' << i;        }       qualityReverseFile << endl;
+               for(int i=0;i<numRows;i++){
                        
                        qualityReverseFile << i+1;
-                       for(int j=0;j<lastColumn;j++){
+                       for(int j=0;j<numColumns;j++){
                                qualityReverseFile << '\t' << qualReverseMap[i][j];
                        }
                        qualityReverseFile << endl;
@@ -801,7 +1248,184 @@ void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector
                m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
                exit(1);
        }
+       
 }
+/**************************************************************************************************/
 
-
+int SeqErrorCommand::setLines(string filename, string qfilename, string rfilename, vector<unsigned long long>& fastaFilePos, vector<unsigned long long>& qfileFilePos, vector<unsigned long long>& rfileFilePos) {
+       try {
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+               //set file positions for fasta file
+               fastaFilePos = m->divideFile(filename, processors);
+               
+               if (qfilename == "") { return processors; }
+               
+               //get name of first sequence in each chunk
+               map<string, int> firstSeqNames;
+               for (int i = 0; i < (fastaFilePos.size()-1); i++) {
+                       ifstream in;
+                       m->openInputFile(filename, in);
+                       in.seekg(fastaFilePos[i]);
+                       
+                       Sequence temp(in); 
+                       firstSeqNames[temp.getName()] = i;
+                       
+                       in.close();
+               }
+               
+               //make copy to use below
+               map<string, int> firstSeqNamesReport = firstSeqNames;
+               
+               //seach for filePos of each first name in the qfile and save in qfileFilePos
+               ifstream inQual;
+               m->openInputFile(qfilename, inQual);
+               
+               string input;
+               while(!inQual.eof()){   
+                       input = m->getline(inQual);
+                       
+                       if (input.length() != 0) {
+                               if(input[0] == '>'){ //this is a sequence name line
+                                       istringstream nameStream(input);
+                                       
+                                       string sname = "";  nameStream >> sname;
+                                       sname = sname.substr(1);
+                                       
+                                       map<string, int>::iterator it = firstSeqNames.find(sname);
+                                       
+                                       if(it != firstSeqNames.end()) { //this is the start of a new chunk
+                                               unsigned long long pos = inQual.tellg(); 
+                                               qfileFilePos.push_back(pos - input.length() - 1);       
+                                               firstSeqNames.erase(it);
+                                       }
+                               }
+                       }
+                       
+                       if (firstSeqNames.size() == 0) { break; }
+               }
+               inQual.close();
+               
+               if (firstSeqNames.size() != 0) { 
+                       for (map<string, int>::iterator it = firstSeqNames.begin(); it != firstSeqNames.end(); it++) {
+                               m->mothurOut(it->first + " is in your fasta file and not in your quality file, aborting."); m->mothurOutEndLine();
+                       }
+                       m->control_pressed = true;
+                       return processors;
+               }
+               
+               //get last file position of qfile
+               FILE * pFile;
+               unsigned long long size;
+               
+               //get num bytes in file
+               pFile = fopen (qfilename.c_str(),"rb");
+               if (pFile==NULL) perror ("Error opening file");
+               else{
+                       fseek (pFile, 0, SEEK_END);
+                       size=ftell (pFile);
+                       fclose (pFile);
+               }
+               
+               qfileFilePos.push_back(size);
+               
+               //seach for filePos of each first name in the rfile and save in rfileFilePos
+               string junk;
+               ifstream inR;
+               m->openInputFile(rfilename, inR);
+               
+               //read column headers
+               for (int i = 0; i < 16; i++) {  
+                       if (!inR.eof()) {       inR >> junk;    }
+                       else                    {       break;                  }
+               }
+               
+               while(!inR.eof()){
+                       
+                       if (m->control_pressed) { inR.close();  return processors; }
+                       
+                       input = m->getline(inR);        
+                       
+                       if (input.length() != 0) {
+                               
+                               istringstream nameStream(input);
+                               string sname = "";  nameStream >> sname;
+                               
+                               map<string, int>::iterator it = firstSeqNamesReport.find(sname);
+                       
+                               if(it != firstSeqNamesReport.end()) { //this is the start of a new chunk
+                                       unsigned long long pos = inR.tellg(); 
+                                       rfileFilePos.push_back(pos - input.length() - 1);       
+                                       firstSeqNamesReport.erase(it);
+                               }
+                       }
+                       
+                       if (firstSeqNamesReport.size() == 0) { break; }
+                       m->gobble(inR);
+               }
+               inR.close();
+               
+               if (firstSeqNamesReport.size() != 0) { 
+                       for (map<string, int>::iterator it = firstSeqNamesReport.begin(); it != firstSeqNamesReport.end(); it++) {
+                               m->mothurOut(it->first + " is in your fasta file and not in your report file, aborting."); m->mothurOutEndLine();
+                       }
+                       m->control_pressed = true;
+                       return processors;
+               }
+               
+               //get last file position of qfile
+               FILE * rFile;
+               unsigned long long sizeR;
+               
+               //get num bytes in file
+               rFile = fopen (rfilename.c_str(),"rb");
+               if (rFile==NULL) perror ("Error opening file");
+               else{
+                       fseek (rFile, 0, SEEK_END);
+                       sizeR=ftell (rFile);
+                       fclose (rFile);
+               }
+               
+               rfileFilePos.push_back(sizeR);
+               
+               return processors;
+               
+#else
+               
+               fastaFilePos.push_back(0); qfileFilePos.push_back(0);
+               //get last file position of fastafile
+               FILE * pFile;
+               unsigned long long size;
+               
+               //get num bytes in file
+               pFile = fopen (filename.c_str(),"rb");
+               if (pFile==NULL) perror ("Error opening file");
+               else{
+                       fseek (pFile, 0, SEEK_END);
+                       size=ftell (pFile);
+                       fclose (pFile);
+               }
+               fastaFilePos.push_back(size);
+               
+               //get last file position of fastafile
+               FILE * qFile;
+               
+               //get num bytes in file
+               qFile = fopen (qfilename.c_str(),"rb");
+               if (qFile==NULL) perror ("Error opening file");
+               else{
+                       fseek (qFile, 0, SEEK_END);
+                       size=ftell (qFile);
+                       fclose (qFile);
+               }
+               qfileFilePos.push_back(size);
+               
+               return 1;
+               
+#endif
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SeqErrorCommand", "setLines");
+               exit(1);
+       }
+}
 //***************************************************************************************************************