]> git.donarmstrong.com Git - mothur.git/blobdiff - preclustercommand.cpp
fixed filterseqscommand bug where trump wasn't being used
[mothur.git] / preclustercommand.cpp
index fa5329b1e4094b4c87d60b5b4dfbd211f1bc1aeb..cab29633c8528c30fe0ba032cc6d76bfea44eccb 100644 (file)
@@ -13,7 +13,7 @@
 inline bool comparePriority(seqPNode first, seqPNode second) {  return (first.numIdentical > second.numIdentical); }
 //**********************************************************************************************************************
 
-PreClusterCommand::PreClusterCommand(string option){
+PreClusterCommand::PreClusterCommand(string option) {
        try {
                abort = false;
                
@@ -60,7 +60,7 @@ PreClusterCommand::PreClusterCommand(string option){
 
                        //check for required parameters
                        fastafile = validParameter.validFile(parameters, "fasta", true);
-                       if (fastafile == "not found") { mothurOut("fasta is a required parameter for the pre.cluster command."); mothurOutEndLine(); abort = true; }
+                       if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the pre.cluster command."); m->mothurOutEndLine(); abort = true; }
                        else if (fastafile == "not open") { abort = true; }     
                        
                        //if the user changes the output directory command factory will send this info to us in the output parameter 
@@ -82,7 +82,7 @@ PreClusterCommand::PreClusterCommand(string option){
                                
        }
        catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "PreClusterCommand");
+               m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
                exit(1);
        }
 }
@@ -93,18 +93,18 @@ PreClusterCommand::~PreClusterCommand(){}
 
 void PreClusterCommand::help(){
        try {
-               mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n");
-               mothurOut("The pre.cluster command outputs a new fasta and name file.\n");
-               mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n");
-               mothurOut("The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n");
-               mothurOut("The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n");
-               mothurOut("The pre.cluster command should be in the following format: \n");
-               mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n");
-               mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n");
-               mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
+               m->mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n");
+               m->mothurOut("The pre.cluster command outputs a new fasta and name file.\n");
+               m->mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n");
+               m->mothurOut("The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n");
+               m->mothurOut("The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n");
+               m->mothurOut("The pre.cluster command should be in the following format: \n");
+               m->mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n");
+               m->mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n");
+               m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
        }
        catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "help");
+               m->errorOut(e, "PreClusterCommand", "help");
                exit(1);
        }
 }
@@ -117,65 +117,88 @@ int PreClusterCommand::execute(){
                
                //reads fasta file and return number of seqs
                int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
-       
-               if (numSeqs == 0) { mothurOut("Error reading fasta file...please correct."); mothurOutEndLine(); return 0;  }
-               if (diffs > length) { mothurOut("Error: diffs is greater than your sequence length."); mothurOutEndLine(); return 0;  }
                
-               //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
-//             sizes.clear();
+               if (m->control_pressed) { return 0; }
        
+               if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0;  }
+               if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0;  }
+               
+               string fileroot = outputDir + getRootName(getSimpleName(fastafile));
+               string newFastaFile = fileroot + "precluster" + getExtension(fastafile);
+               string newNamesFile = fileroot + "precluster.names";
+               ofstream outFasta;
+               ofstream outNames;
+               
+               openOutputFile(newFastaFile, outFasta);
+               openOutputFile(newNamesFile, outNames);
+
                //sort seqs by number of identical seqs
-               sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
-       
-               int count = 0;
+               alignSeqs.sort(comparePriority);
 
+               int count = 0;
+               int i = 0;
                //think about running through twice...
-               for (int i = 0; i < numSeqs; i++) {
-                       
-                       //are you active
-                       //                      itActive = active.find(alignSeqs[i].seq.getName());
+               list<seqPNode>::iterator itList;
+               list<seqPNode>::iterator itList2;
+               for (itList = alignSeqs.begin(); itList != alignSeqs.end();) {
                        
-                       if (alignSeqs[i].active) {  //this sequence has not been merged yet
+                       //try to merge it with all smaller seqs
+                       for (itList2 = alignSeqs.begin(); itList2 != alignSeqs.end();) {
+                               
+                               if (m->control_pressed) { outFasta.close(); outNames.close(); remove(newFastaFile.c_str()); remove(newNamesFile.c_str());  return 0; }
                                
-                               //try to merge it with all smaller seqs
-                               for (int j = i+1; j < numSeqs; j++) {
-                                       if (alignSeqs[j].active) {  //this sequence has not been merged yet
-                                               //are you within "diff" bases
-                                               int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
+                       
+                               if (itList->seq.getName() != itList2->seq.getName()) { //you don't want to merge with yourself
+                                       //are you within "diff" bases
+                                       
+                                       int mismatch = calcMisMatches((*itList).seq.getAligned(), (*itList2).seq.getAligned());
+
+                                       if (mismatch <= diffs) {
+                                               //merge
+                                               (*itList).names += ',' + (*itList2).names;
+                                               (*itList).numIdentical += (*itList2).numIdentical;
                                                
-                                               if (mismatch <= diffs) {
-                                                       //merge
-                                                       alignSeqs[i].names += ',' + alignSeqs[j].names;
-                                                       alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
+                                               itList2 = alignSeqs.erase(itList2); //itList2--;
+                                               count++;
+                                       }else{ itList2++; }
+                               }else{ itList2++; }
 
-                                                       alignSeqs[j].active = 0;
-                                                       alignSeqs[j].numIdentical = 0;
-                                                       count++;
-                                               }
-                                       }//end if j active
-                               }//end if i != j
+                       }
+
+                       //ouptut this sequence
+                       printData(outFasta, outNames, (*itList));
+                       
+                       //remove sequence
+                       itList = alignSeqs.erase(itList); 
                        
-                       //remove from active list 
-                               alignSeqs[i].active = 0;
-                       }//end if active i
-                       if(i % 100 == 0)        { cout << i << '\t' << numSeqs - count << '\t' << count << endl;        }
+                       i++;
+                       
+                       if(i % 100 == 0)        { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
                }
+                               
+               if(i % 100 != 0)        { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
                
-               string fileroot = outputDir + getRootName(getSimpleName(fastafile));
+               outFasta.close();
+               outNames.close();
                
-               string newFastaFile = fileroot + "precluster" + getExtension(fastafile);
-               string newNamesFile = fileroot + "precluster.names";
+               if (m->control_pressed) {  remove(newFastaFile.c_str()); remove(newNamesFile.c_str());  return 0; }
+               
+               m->mothurOut("Total number of sequences before precluster was " + toString(numSeqs) + "."); m->mothurOutEndLine();
+               m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); 
                
+               if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
                
-               mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine();
-               mothurOut("pre.cluster removed " + toString(count) + " sequences."); mothurOutEndLine(); 
-               printData(newFastaFile, newNamesFile);
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               m->mothurOut(newFastaFile); m->mothurOutEndLine();      
+               m->mothurOut(newNamesFile); m->mothurOutEndLine();      
+               m->mothurOutEndLine();
 
                return 0;
                
        }
        catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "execute");
+               m->errorOut(e, "PreClusterCommand", "execute");
                exit(1);
        }
 }
@@ -196,7 +219,9 @@ int PreClusterCommand::readFASTA(){
                length = 0;
                
                while (!inFasta.eof()) {
-       
+                       
+                       if (m->control_pressed) { inFasta.close(); return 0; }
+                       
                        //inNames >> firstCol >> secondCol;
                        //nameString = secondCol;
                        
@@ -213,16 +238,16 @@ int PreClusterCommand::readFASTA(){
                                if (namefile != "") {
                                        itSize = sizes.find(seq.getName());
                                        
-                                       if (itSize == sizes.end()) { mothurOut(seq.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
+                                       if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
                                        else{
                                                seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
                                                alignSeqs.push_back(tempNode);
-                                               if (seq.getAligned().length() > length) {  length = alignSeqs[0].seq.getAligned().length();  }
+                                               if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
                                        }       
                                }else { //no names file, you are identical to yourself 
                                        seqPNode tempNode(1, seq, seq.getName());
                                        alignSeqs.push_back(tempNode);
-                                       if (seq.getAligned().length() > length) {  length = alignSeqs[0].seq.getAligned().length();  }
+                                       if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
                                }
                        }
                }
@@ -232,7 +257,7 @@ int PreClusterCommand::readFASTA(){
        }
        
        catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "readFASTA");
+               m->errorOut(e, "PreClusterCommand", "readFASTA");
                exit(1);
        }
 }
@@ -252,39 +277,23 @@ int PreClusterCommand::calcMisMatches(string seq1, string seq2){
                return numBad;
        }
        catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "calcMisMatches");
+               m->errorOut(e, "PreClusterCommand", "calcMisMatches");
                exit(1);
        }
 }
 
 /**************************************************************************************************/
 
-void PreClusterCommand::printData(string newfasta, string newname){
+void PreClusterCommand::printData(ofstream& outFasta, ofstream& outNames, seqPNode thisSeq){
        try {
-               ofstream outFasta;
-               ofstream outNames;
-               
-               openOutputFile(newfasta, outFasta);
-               openOutputFile(newname, outNames);
-                               
-               
-               for (int i = 0; i < alignSeqs.size(); i++) {
-                       if (alignSeqs[i].numIdentical != 0) {
-                               alignSeqs[i].seq.printSequence(outFasta); 
-                               outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
-                       }
-               }
-               
-               outFasta.close();
-               outNames.close();
-               
+               thisSeq.seq.printSequence(outFasta); 
+               outNames << thisSeq.seq.getName() << '\t' << thisSeq.names << endl;
        }
        catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "printData");
+               m->errorOut(e, "PreClusterCommand", "printData");
                exit(1);
        }
 }
-
 /**************************************************************************************************/
 
 void PreClusterCommand::readNameFile(){
@@ -306,7 +315,7 @@ void PreClusterCommand::readNameFile(){
                in.close();
        }
        catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "readNameFile");
+               m->errorOut(e, "PreClusterCommand", "readNameFile");
                exit(1);
        }
 }