]> git.donarmstrong.com Git - mothur.git/blobdiff - seqerrorcommand.cpp
mods to shhhercommand and seqerrorcommand
[mothur.git] / seqerrorcommand.cpp
index 1f880f38cdf5c48d4dd0418c1d9c68172af7b8b7..88794788a0472743665838721bb28b9b761f1765 100644 (file)
@@ -8,11 +8,14 @@
  */
 
 #include "seqerrorcommand.h"
+#include "reportfile.h"
+#include "qualityscores.h"
+#include "refchimeratest.h"
 
 //**********************************************************************************************************************
 vector<string> SeqErrorCommand::getValidParameters(){  
        try {
-               string Array[] =  {"query", "reference", "name", "threshold", "inputdir", "outputdir"};
+               string Array[] =  {"query", "reference", "name", "qfile", "report", "threshold", "inputdir", "outputdir"};
                vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                return myArray;
        }
@@ -24,10 +27,17 @@ vector<string> SeqErrorCommand::getValidParameters(){
 //**********************************************************************************************************************
 SeqErrorCommand::SeqErrorCommand(){    
        try {
-               //initialize outputTypes
+               abort = true; calledHelp = true; 
                vector<string> tempOutNames;
-               outputTypes["error"] = tempOutNames;
-               outputTypes["count"] = 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;
        }
        catch(exception& e) {
                m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
@@ -62,18 +72,16 @@ vector<string> SeqErrorCommand::getRequiredFiles(){
 SeqErrorCommand::SeqErrorCommand(string option)  {
        try {
                
-               abort = false;
+               abort = false; calledHelp = false;   
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
                
                else {
                        string temp;
                        
                        //valid paramters for this command
-                       string AlignArray[] =  {"query", "reference", "name", "threshold", "inputdir", "outputdir"};
-                       
-//need to implement name file option
+                       string AlignArray[] =  {"query", "reference", "name", "qfile", "report", "threshold", "inputdir", "ignorechimeras", "outputdir"};
                        
                        vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
                        
@@ -90,8 +98,16 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                        
                        //initialize outputTypes
                        vector<string> tempOutNames;
-                       outputTypes["error"] = tempOutNames;
-                       outputTypes["count"] = 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;
+
                        
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
@@ -115,12 +131,28 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                                }
                                
                                it = parameters.find("name");
-                               //user has given a template file
+                               //user has given a names 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["name"] = inputDir + it->second;             }
                                }
+
+                               it = parameters.find("qfile");
+                               //user has given a quality score 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["qfile"] = inputDir + it->second;            }
+                               }
+                               
+                               it = parameters.find("report");
+                               //user has given a alignment report 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["report"] = inputDir + it->second;           }
+                               }
                                
                        }
                        //check for required parameters
@@ -132,10 +164,22 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                        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; }     
                        
-                       //if the user changes the output directory command factory will send this info to us in the output parameter 
+
+                       //check for optional parameters
                        namesFileName = validParameter.validFile(parameters, "name", true);
                        if(namesFileName == "not found"){       namesFileName = "";     }
-                       cout << namesFileName << endl;
+                       
+                       qualFileName = validParameter.validFile(parameters, "qfile", true);
+                       if(qualFileName == "not found"){        qualFileName = "";      }
+
+                       reportFileName = validParameter.validFile(parameters, "report", true);
+                       if(reportFileName == "not found"){      reportFileName = "";    }
+                       
+                       if((reportFileName != "" && qualFileName == "") || (reportFileName == "" && qualFileName != "")){
+                               m->mothurOut("if you use either a qual file or a report file, you have to have both.");
+                               m->mothurOutEndLine();
+                               abort = true; 
+                       }
                        
                        outputDir = validParameter.validFile(parameters, "outputdir", false);
                        if (outputDir == "not found"){  
@@ -147,11 +191,12 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                        // ...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);  
-                                               
-                       errorFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".errors";
-                       m->openOutputFile(errorFileName, errorFile);
-                       outputNames.push_back(errorFileName); outputTypes["error"].push_back(errorFileName);
-                       printErrorHeader();
+                       
+                       temp = validParameter.validFile(parameters, "ignorechimeras", false);   if (temp == "not found") { temp = "1"; }
+                       convert(temp, ignoreChimeras);  
+
+                       substitutionMatrix.resize(6);
+                       for(int i=0;i<6;i++){   substitutionMatrix[i].assign(6,0);      }
                }
        }
        catch(exception& e) {
@@ -165,9 +210,6 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
 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");
@@ -180,13 +222,24 @@ void SeqErrorCommand::help(){
 
 //***************************************************************************************************************
 
-SeqErrorCommand::~SeqErrorCommand(){   errorFile.close();      }
+SeqErrorCommand::~SeqErrorCommand(){
+
+}
 
 //***************************************************************************************************************
 
 int SeqErrorCommand::execute(){
        try{
-               if (abort == true) { return 0; }
+               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);
 
                getReferences();        //read in reference sequences - make sure there's no ambiguous bases
 
@@ -195,7 +248,27 @@ int SeqErrorCommand::execute(){
                
                ifstream queryFile;
                m->openInputFile(queryFileName, queryFile);
-                               
+               
+               ifstream reportFile;
+               ifstream qualFile;
+
+               ReportFile report;
+               QualityScores quality;
+               vector<vector<int> > qualForwardMap;
+               vector<vector<int> > qualReverseMap;
+               
+               if(qualFileName != "" && reportFileName != ""){
+                       m->openInputFile(qualFileName, qualFile);
+                       report = ReportFile(reportFile, reportFileName);
+                       
+                       qualForwardMap.resize(1000);
+                       qualReverseMap.resize(1000);
+                       for(int i=0;i<1000;i++){
+                               qualForwardMap[i].assign(100,0);
+                               qualReverseMap[i].assign(100,0);
+                       }                               
+               }
+               
                int totalBases = 0;
                int totalMatches = 0;
                
@@ -204,32 +277,82 @@ int SeqErrorCommand::execute(){
                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);
+               
+               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);       
+               
+
+               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);
+               
+               int index = 0;
+               bool ignoreSeq = 0;
                
                while(queryFile){
-                       Compare minCompare;
                        
+                       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);
-                       
-                       for(int i=0;i<numRefs;i++){
-                               Compare currCompare = getErrors(query, referenceSeqs[i]);
-                               
-                               if(currCompare.errorRate < minCompare.errorRate){
-                                       minCompare = currCompare;
-                               }
-                               
-                       }
+                                               
+                       int numParentSeqs = chimeraTest.analyzeQuery(query.getName(), query.getAligned());
+                       int closestRefIndex = chimeraTest.getClosestRefIndex();
 
+                       if(numParentSeqs > 1 && ignoreChimeras == 1)    {       ignoreSeq = 1;  }
+                       else                                                                                    {       ignoreSeq = 0;  }
+
+
+                       Compare minCompare = getErrors(query, referenceSeqs[closestRefIndex]);
+                       
                        if(namesFileName != ""){
                                it = weights.find(query.getName());
                                minCompare.weight = it->second;
                        }
-                       else {
-                               minCompare.weight = 1;
+                       else    {       minCompare.weight = 1;  }
+
+                       printErrorData(minCompare, numParentSeqs);
+
+                       if(!ignoreSeq){
+                               for(int i=0;i<minCompare.total;i++){
+                                       char letter = minCompare.sequence[i];
+                                       errorForward[letter][i] += minCompare.weight;
+                                       errorReverse[letter][minCompare.total-i-1] += minCompare.weight;                                
+                               }
                        }
+                       
+                       if(qualFileName != "" && reportFileName != ""){
+                               report = ReportFile(reportFile);
+                               
+                               int origLength = report.getQueryLength();
+                               int startBase = report.getQueryStart();
+                               int endBase = report.getQueryEnd();
 
-                       printErrorData(minCompare);
+                               quality = QualityScores(qualFile, origLength);
 
-                       if(minCompare.errorRate < threshold){
+                               if(!ignoreSeq){
+                                       quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
+                                       quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
+                                       quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
+                               }
+                       }                       
+                       
+                       if(minCompare.errorRate < threshold && !ignoreSeq){
                                totalBases += (minCompare.total * minCompare.weight);
                                totalMatches += minCompare.matches * minCompare.weight;
                                if(minCompare.mismatches > maxMismatch){
@@ -240,26 +363,43 @@ int SeqErrorCommand::execute(){
                                numSeqs++;
                        }
                        
+                       index++;
+                       if(index % 1000 == 0){  cout << index << endl;  }
                }
                queryFile.close();
+               errorSummaryFile.close();       
+               errorSeqFile.close();
+
+               if(qualFileName != "" && reportFileName != ""){         
+                       printErrorQuality(qScoreErrorMap);
+                       printQualityFR(qualForwardMap, qualReverseMap);
+               }
                
-               int total = 0;
-               
+               printErrorFRFile(errorForward, errorReverse);
                
-               string errorCountFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".count";
+               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["count"].push_back(errorCountFileName);
-               
+               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";
-               
+               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;       
        }
@@ -318,45 +458,45 @@ Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
                                started = 1;
                                
                                if(q[i] == 'A'){
-                                       if(r[i] == 'A'){        errors.AA++;    errors.matches++;       }
-                                       if(r[i] == 'T'){        errors.AT++;    }
-                                       if(r[i] == 'G'){        errors.AG++;    }
-                                       if(r[i] == 'C'){        errors.AC++;    }
-                                       if(r[i] == '-'){        errors.Ai++;    }
+                                       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++;    }
-                                       if(r[i] == 'T'){        errors.TT++;    errors.matches++;       }
-                                       if(r[i] == 'G'){        errors.TG++;    }
-                                       if(r[i] == 'C'){        errors.TC++;    }
-                                       if(r[i] == '-'){        errors.Ti++;    }
+                                       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++;    }
-                                       if(r[i] == 'T'){        errors.GT++;    }
-                                       if(r[i] == 'G'){        errors.GG++;    errors.matches++;       }
-                                       if(r[i] == 'C'){        errors.GC++;    }
-                                       if(r[i] == '-'){        errors.Gi++;    }
+                                       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++;    }
-                                       if(r[i] == 'T'){        errors.CT++;    }
-                                       if(r[i] == 'G'){        errors.CG++;    }
-                                       if(r[i] == 'C'){        errors.CC++;    errors.matches++;       }
-                                       if(r[i] == '-'){        errors.Ci++;    }
+                                       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++;    }
-                                       if(r[i] == 'T'){        errors.NT++;    }
-                                       if(r[i] == 'G'){        errors.NG++;    }
-                                       if(r[i] == 'C'){        errors.NC++;    }
-                                       if(r[i] == '-'){        errors.Ni++;    }
+                                       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++;    }
-                                       if(r[i] == 'T'){        errors.dT++;    }
-                                       if(r[i] == 'G'){        errors.dG++;    }
-                                       if(r[i] == 'C'){        errors.dC++;    }
+                                       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++; 
                                
@@ -365,7 +505,7 @@ Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
                                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");
+//                             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
@@ -409,12 +549,12 @@ map<string, int> SeqErrorCommand::getWeights(){
 
 void SeqErrorCommand::printErrorHeader(){
        try {
-               errorFile << "query\treference\tweight\t";
-               errorFile << "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";
-               errorFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\n";
+               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";
+               errorSummaryFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\tnumparents\n";
                
-               errorFile << setprecision(6);
-               errorFile.setf(ios::fixed);
+               errorSummaryFile << setprecision(6);
+               errorSummaryFile.setf(ios::fixed);
        }
        catch(exception& e) {
                m->errorOut(e, "SeqErrorCommand", "printErrorHeader");
@@ -424,23 +564,63 @@ void SeqErrorCommand::printErrorHeader(){
 
 //***************************************************************************************************************
 
-void SeqErrorCommand::printErrorData(Compare error){
+void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs){
        try {
-               errorFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
-               errorFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
-               errorFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
-               errorFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
-               errorFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
-               errorFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
-               errorFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t' ;
-               errorFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
-               
-               errorFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t';                 //insertions
-               errorFile << error.dA + error.dT + error.dG + error.dC << '\t';                 //deletions
-               errorFile << 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
-               errorFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t';      //ambiguities
-               errorFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << endl;
+               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.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
+               
+               errorSummaryFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t';                  //insertions
+               errorSummaryFile << error.dA + error.dT + error.dG + error.dC << '\t';                  //deletions
+               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;
+                       substitutionMatrix[a][g] += error.weight * error.GA;
+                       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;
+                       substitutionMatrix[t][c] += error.weight * error.CT;
+                       substitutionMatrix[t][gap] += error.weight * error.dT;
+                       substitutionMatrix[t][n] += error.weight * error.NT;
+
+                       substitutionMatrix[g][a] += error.weight * error.AG;
+                       substitutionMatrix[g][t] += error.weight * error.TG;
+                       substitutionMatrix[g][g] += error.weight * error.GG;
+                       substitutionMatrix[g][c] += error.weight * error.CG;
+                       substitutionMatrix[g][gap] += error.weight * error.dG;
+                       substitutionMatrix[g][n] += error.weight * error.NG;
+
+                       substitutionMatrix[c][a] += error.weight * error.AC;
+                       substitutionMatrix[c][t] += error.weight * error.TC;
+                       substitutionMatrix[c][g] += error.weight * error.GC;
+                       substitutionMatrix[c][c] += error.weight * error.CC;
+                       substitutionMatrix[c][gap] += error.weight * error.dC;
+                       substitutionMatrix[c][n] += error.weight * error.NC;
+
+                       substitutionMatrix[gap][a] += error.weight * error.Ai;
+                       substitutionMatrix[gap][t] += error.weight * error.Ti;
+                       substitutionMatrix[gap][g] += error.weight * error.Gi;
+                       substitutionMatrix[gap][c] += error.weight * error.Ci;
+                       substitutionMatrix[gap][n] += error.weight * error.Ni;
+               }
        }
        catch(exception& e) {
                m->errorOut(e, "SeqErrorCommand", "printErrorData");
@@ -450,9 +630,176 @@ void SeqErrorCommand::printErrorData(Compare error){
 
 //***************************************************************************************************************
 
+void SeqErrorCommand::printSubMatrix(){
+       try {
+               string subMatrixFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.matrix";
+               ofstream subMatrixFile;
+               m->openOutputFile(subMatrixFileName, subMatrixFile);
+               outputNames.push_back(subMatrixFileName);  outputTypes["error.matrix"].push_back(subMatrixFileName);
+               vector<string> bases(6);
+               bases[0] = "A";
+               bases[1] = "T";
+               bases[2] = "G";
+               bases[3] = "C";
+               bases[4] = "Gap";
+               bases[5] = "N";
+               vector<int> refSums(5,1);
+
+               for(int i=0;i<5;i++){
+                       subMatrixFile << "\tr" << bases[i];
+                       
+                       for(int j=0;j<6;j++){
+                               refSums[i] += substitutionMatrix[i][j];                         
+                       }
+               }
+               subMatrixFile << endl;
+               
+               for(int i=0;i<6;i++){
+                       subMatrixFile << 'q' << bases[i];
+                       for(int j=0;j<5;j++){
+                               subMatrixFile << '\t' << substitutionMatrix[j][i];                              
+                       }
+                       subMatrixFile << endl;
+               }
+
+               subMatrixFile << "total";
+               for(int i=0;i<5;i++){
+                       subMatrixFile << '\t' << refSums[i];
+               }
+               subMatrixFile << endl;
+               subMatrixFile.close();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SeqErrorCommand", "printSubMatrix");
+               exit(1);
+       }
+}
+//***************************************************************************************************************
+
+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";
+               ofstream errorForwardFile;
+               m->openOutputFile(errorForwardFileName, errorForwardFile);
+               outputNames.push_back(errorForwardFileName);  outputTypes["error.forward"].push_back(errorForwardFileName);
+
+               errorForwardFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
+               for(int i=0;i<1000;i++){
+                       float match = (float)errorForward['m'][i];
+                       float subst = (float)errorForward['s'][i];
+                       float insert = (float)errorForward['i'][i];
+                       float del = (float)errorForward['d'][i];
+                       float amb = (float)errorForward['a'][i];
+                       float total = match + subst + insert + del + amb;
+                       if(total == 0){ break;  }
+                       errorForwardFile << i+1 << '\t' << total << '\t' << match/total  << '\t' << subst/total  << '\t' << insert/total  << '\t' << del/total  << '\t' << amb/total << endl;
+               }
+               errorForwardFile.close();
+
+               string errorReverseFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq.reverse";
+               ofstream errorReverseFile;
+               m->openOutputFile(errorReverseFileName, errorReverseFile);
+               outputNames.push_back(errorReverseFileName);  outputTypes["error.reverse"].push_back(errorReverseFileName);
+
+               errorReverseFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
+               for(int i=0;i<1000;i++){
+                       float match = (float)errorReverse['m'][i];
+                       float subst = (float)errorReverse['s'][i];
+                       float insert = (float)errorReverse['i'][i];
+                       float del = (float)errorReverse['d'][i];
+                       float amb = (float)errorReverse['a'][i];
+                       float total = match + subst + insert + del + amb;
+                       if(total == 0){ break;  }
+                       errorReverseFile << i+1 << '\t' << total << '\t' << match/total  << '\t' << subst/total  << '\t' << insert/total  << '\t' << del/total  << '\t' << amb/total << endl;
+               }
+               errorReverseFile.close();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
+               exit(1);
+       }
+}
+
+//***************************************************************************************************************
+
+void SeqErrorCommand::printErrorQuality(map<char, vector<int> > qScoreErrorMap){
+       try{
+
+               string errorQualityFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.quality";
+               ofstream errorQualityFile;
+               m->openOutputFile(errorQualityFileName, errorQualityFile);
+               outputNames.push_back(errorQualityFileName);  outputTypes["error.quality"].push_back(errorQualityFileName);
+
+               errorQualityFile << "qscore\tmatches\tsubstitutions\tinsertions\tambiguous" << endl;
+               for(int i=0;i<41;i++){
+                       errorQualityFile << i << '\t' << qScoreErrorMap['m'][i] << '\t' << qScoreErrorMap['s'][i] << '\t' << qScoreErrorMap['i'][i] << '\t'<< qScoreErrorMap['a'][i] << endl;
+               }
+               errorQualityFile.close();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
+               exit(1);
+       }
+}
+
+
+//***************************************************************************************************************
+
+void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector<vector<int> > qualReverseMap){
+       try{
 
 
+               int lastRow = 0;
+               int lastColumn = 0;
 
+               for(int i=0;i<qualForwardMap.size();i++){
+                       for(int j=0;j<qualForwardMap[i].size();j++){
+                               if(qualForwardMap[i][j] != 0){
+                                       if(lastRow < i)         {       lastRow = i+2;          }
+                                       if(lastColumn < j)      {       lastColumn = j+2;       }
+                               }
+                       }
+               }
 
+               string qualityForwardFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.qual.forward";
+               ofstream qualityForwardFile;
+               m->openOutputFile(qualityForwardFileName, qualityForwardFile);
+               outputNames.push_back(qualityForwardFileName);  outputTypes["error.qual.forward"].push_back(qualityForwardFileName);
 
+               for(int i=0;i<lastColumn;i++){  qualityForwardFile << '\t' << i;        }       qualityForwardFile << endl;
 
+               for(int i=0;i<lastRow;i++){
+                       qualityForwardFile << i+1;
+                       for(int j=0;j<lastColumn;j++){
+                               qualityForwardFile << '\t' << qualForwardMap[i][j];
+                       }
+
+                       qualityForwardFile << endl;
+               }
+               qualityForwardFile.close();
+
+               
+               string qualityReverseFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.qual.reverse";
+               ofstream qualityReverseFile;
+               m->openOutputFile(qualityReverseFileName, qualityReverseFile);
+               outputNames.push_back(qualityReverseFileName);  outputTypes["error.qual.reverse"].push_back(qualityReverseFileName);
+               
+               for(int i=0;i<lastColumn;i++){  qualityReverseFile << '\t' << i;        }       qualityReverseFile << endl;
+               for(int i=0;i<lastRow;i++){
+                       
+                       qualityReverseFile << i+1;
+                       for(int j=0;j<lastColumn;j++){
+                               qualityReverseFile << '\t' << qualReverseMap[i][j];
+                       }
+                       qualityReverseFile << endl;
+               }
+               qualityReverseFile.close();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
+               exit(1);
+       }
+}
+
+
+//***************************************************************************************************************