]> git.donarmstrong.com Git - mothur.git/blobdiff - seqerrorcommand.cpp
bug fix to seq.error
[mothur.git] / seqerrorcommand.cpp
index 88794788a0472743665838721bb28b9b761f1765..2e20c57e6245cc66f4b4f9483f643aacd6c1201a 100644 (file)
@@ -196,7 +196,7 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                        convert(temp, ignoreChimeras);  
 
                        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);      }
                }
        }
        catch(exception& e) {
@@ -222,9 +222,7 @@ void SeqErrorCommand::help(){
 
 //***************************************************************************************************************
 
-SeqErrorCommand::~SeqErrorCommand(){
-
-}
+SeqErrorCommand::~SeqErrorCommand(){   /*      void    */      }
 
 //***************************************************************************************************************
 
@@ -302,48 +300,51 @@ int SeqErrorCommand::execute(){
                RefChimeraTest chimeraTest(referenceSeqs, errorChimeraFileName);
                outputNames.push_back(errorChimeraFileName); outputTypes["error.chimera"].push_back(errorChimeraFileName);
                
+               vector<string> megaAlignVector(numRefs, "");
+
                int index = 0;
                bool ignoreSeq = 0;
                
                while(queryFile){
-                       
+
                        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 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 origLength = report.getQueryLength();
                                int startBase = report.getQueryStart();
                                int endBase = report.getQueryEnd();
 
-                               quality = QualityScores(qualFile, origLength);
+                               quality = QualityScores(qualFile);
 
                                if(!ignoreSeq){
                                        quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
@@ -351,7 +352,7 @@ int SeqErrorCommand::execute(){
                                        quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
                                }
                        }                       
-                       
+
                        if(minCompare.errorRate < threshold && !ignoreSeq){
                                totalBases += (minCompare.total * minCompare.weight);
                                totalMatches += minCompare.matches * minCompare.weight;
@@ -361,8 +362,10 @@ int SeqErrorCommand::execute(){
                                }                               
                                misMatchCounts[minCompare.mismatches] += minCompare.weight;
                                numSeqs++;
+                               
+                               megaAlignVector[closestRefIndex] += query.getInlineSeq() + '\n';
                        }
-                       
+
                        index++;
                        if(index % 1000 == 0){  cout << index << endl;  }
                }
@@ -396,6 +399,17 @@ int SeqErrorCommand::execute(){
 
                printSubMatrix();
                                
+               string megAlignmentFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.ref-query";
+               ofstream megAlignmentFile;
+               m->openOutputFile(megAlignmentFileName, megAlignmentFile);
+               outputNames.push_back(megAlignmentFileName);  outputTypes["error.ref-query"].push_back(megAlignmentFileName);
+               
+               for(int i=0;i<numRefs;i++){
+                       megAlignmentFile << referenceSeqs[i].getInlineSeq() << endl;
+                       megAlignmentFile << megaAlignVector[i] << endl;
+               }
+               
+               
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
@@ -417,20 +431,37 @@ void SeqErrorCommand::getReferences(){
                ifstream referenceFile;
                m->openInputFile(referenceFileName, referenceFile);
                
+               int numAmbigSeqs = 0;
+               
+               int maxStartPos = 0;
+               int minEndPos = 100000;
+               
                while(referenceFile){
                        Sequence currentSeq(referenceFile);
                        int numAmbigs = currentSeq.getAmbigBases();
+                       if(numAmbigs > 0){      numAmbigSeqs++; }
                        
-                       if(numAmbigs != 0){
-                               m->mothurOut("Warning: " + toString(currentSeq.getName()) + " has " + toString(numAmbigs) + " ambiguous bases, these bases will be removed\n");
-                               currentSeq.removeAmbigBases();
-                       }
+                       int startPos = currentSeq.getStartPos();
+                       if(startPos > maxStartPos)      {       maxStartPos = startPos; }
+
+                       int endPos = currentSeq.getEndPos();
+                       if(endPos < minEndPos)          {       minEndPos = endPos;             }
                        referenceSeqs.push_back(currentSeq);
                        m->gobble(referenceFile);
                }
+               referenceFile.close();
                numRefs = referenceSeqs.size();
+
+               
+               for(int i=0;i<numRefs;i++){
+                       referenceSeqs[i].padToPos(maxStartPos);
+                       referenceSeqs[i].padFromPos(minEndPos);
+               }
+               
+               if(numAmbigSeqs != 0){
+                       m->mothurOut("Warning: " + toString(numAmbigSeqs) + " reference sequences have ambiguous bases, these bases will be ignored\n");
+               }               
                
-               referenceFile.close();
        }
        catch(exception& e) {
                m->errorOut(e, "SeqErrorCommand", "getReferences");
@@ -454,7 +485,7 @@ Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
                Compare errors;
 
                for(int i=0;i<alignLength;i++){
-                       if(q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){                 //      no missing data and no double gaps
+                       if(r[i] != 'N' && q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){                  //      no missing data and no double gaps
                                started = 1;
                                
                                if(q[i] == 'A'){
@@ -505,7 +536,6 @@ 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");
                                if(started == 1){       break;  }
                        }
                        else if(q[i] == '.' && r[i] == '.'){            //      both are missing data
@@ -566,13 +596,14 @@ void SeqErrorCommand::printErrorHeader(){
 
 void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs){
        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
@@ -580,12 +611,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;
@@ -593,7 +624,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;