]> git.donarmstrong.com Git - mothur.git/blobdiff - preclustercommand.cpp
added count file to get.oturep, pre.cluster, screen.seqs, tree.shared. made remove...
[mothur.git] / preclustercommand.cpp
index 951b2008b062c26c5fabb54dfd8ef2d2a858da67..dadc9186f243952445db24766cf3a08c9dc4d5d0 100644 (file)
@@ -14,8 +14,9 @@
 vector<string> PreClusterCommand::setParameters(){     
        try {
                CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
-               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
-               CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
+               CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none",false,false); parameters.push_back(pname);
+        CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none",false,false); parameters.push_back(pcount);
+               CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none",false,false); parameters.push_back(pgroup);
                CommandParameter pdiffs("diffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pdiffs);
                CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
                CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
@@ -36,9 +37,10 @@ string PreClusterCommand::getHelpString(){
                string helpString = "";
                helpString += "The pre.cluster command groups sequences that are within a given number of base mismatches.\n";
                helpString += "The pre.cluster command outputs a new fasta and name file.\n";
-               helpString += "The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n";
-               helpString += "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";
+               helpString += "The pre.cluster command parameters are fasta, name, group, count, processors and diffs. The fasta parameter is required. \n";
+               helpString += "The name 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";
                helpString += "The group parameter allows you to provide a group file so you can cluster by group. \n";
+        helpString += "The count parameter allows you to provide a count file so you can cluster by group. \n";
                helpString += "The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n";
                helpString += "The pre.cluster command should be in the following format: \n";
                helpString += "pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n";
@@ -63,6 +65,7 @@ string PreClusterCommand::getOutputFileNameTag(string type, string inputName="")
         else {
             if (type == "fasta") {  outputFileName =  "precluster" + m->getExtension(inputName); }
             else if (type == "name") {  outputFileName =  "precluster.names"; }
+            else if (type == "count") {  outputFileName =  "precluster.count_table"; }
             else if (type == "map") {  outputFileName =  "precluster.map"; }
             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
         }
@@ -81,6 +84,7 @@ PreClusterCommand::PreClusterCommand(){
                vector<string> tempOutNames;
                outputTypes["fasta"] = tempOutNames;
                outputTypes["name"] = tempOutNames;
+        outputTypes["count"] = tempOutNames;
                outputTypes["map"] = tempOutNames;
        }
        catch(exception& e) {
@@ -117,6 +121,7 @@ PreClusterCommand::PreClusterCommand(string option) {
                        outputTypes["fasta"] = tempOutNames;
                        outputTypes["name"] = tempOutNames;
                        outputTypes["map"] = tempOutNames;
+            outputTypes["count"] = 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);              
@@ -146,6 +151,14 @@ PreClusterCommand::PreClusterCommand(string option) {
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["group"] = inputDir + it->second;            }
                                }
+                
+                it = parameters.find("count");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["count"] = inputDir + it->second;            }
+                               }
                        }
 
                        //check for required parameters
@@ -175,6 +188,25 @@ PreClusterCommand::PreClusterCommand(string option) {
                        if (groupfile == "not found") { groupfile =  "";  bygroup = false; }
                        else if (groupfile == "not open") { abort = true; groupfile =  ""; }    
                        else {   m->setGroupFile(groupfile); bygroup = true;  }
+            
+            countfile = validParameter.validFile(parameters, "count", true);
+                       if (countfile == "not found") { countfile =  "";   }
+                       else if (countfile == "not open") { abort = true; countfile =  ""; }    
+                       else {   
+                m->setCountTableFile(countfile); 
+                ct.readTable(countfile);
+                if (ct.hasGroupInfo()) { bygroup = true; }
+                else { bygroup = false;  }
+            }
+            
+            if ((namefile != "") && (countfile != "")) {
+                m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
+            }
+            
+            if ((groupfile != "") && (countfile != "")) {
+                m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
+            }
+
                        
                        string temp     = validParameter.validFile(parameters, "diffs", false);         if(temp == "not found"){        temp = "1"; }
                        m->mothurConvert(temp, diffs); 
@@ -183,10 +215,12 @@ PreClusterCommand::PreClusterCommand(string option) {
                        m->setProcessors(temp);
                        m->mothurConvert(temp, processors);
                        
-                       if (namefile == "") {
-                               vector<string> files; files.push_back(fastafile);
-                               parser.getNameFile(files);
-                       }
+            if (countfile == "") {
+                if (namefile == "") {
+                    vector<string> files; files.push_back(fastafile);
+                    parser.getNameFile(files);
+                }
+            }
                }
                                
        }
@@ -207,10 +241,11 @@ int PreClusterCommand::execute(){
                string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
                string newFastaFile = fileroot + getOutputFileNameTag("fasta", fastafile);
                string newNamesFile = fileroot + getOutputFileNameTag("name");
+        string newCountFile = fileroot + getOutputFileNameTag("count");
                string newMapFile = fileroot + getOutputFileNameTag("map"); //add group name if by group
                outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile);
-               outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile);
-               
+               if (countfile == "") { outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile); }
+               else { outputNames.push_back(newCountFile); outputTypes["count"].push_back(newCountFile); }
                
                if (bygroup) {
                        //clear out old files
@@ -219,39 +254,45 @@ int PreClusterCommand::execute(){
                        newMapFile = fileroot + "precluster.";
                        
                        //parse fasta and name file by group
-                       SequenceParser* parser;
-                       if (namefile != "") { parser = new SequenceParser(groupfile, fastafile, namefile);      }
-                       else                            { parser = new SequenceParser(groupfile, fastafile);                    }
-                       
-                       vector<string> groups = parser->getNamesOfGroups();
-                       
-                       if(processors == 1)     {       driverGroups(parser, newFastaFile, newNamesFile, newMapFile, 0, groups.size(), groups); }
-                       else                            {       createProcessesGroups(parser, newFastaFile, newNamesFile, newMapFile, groups);                  }
-                       
-                       delete parser;
-                       
-                       if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);        }        return 0; }
-                       
-                       //run unique.seqs for deconvolute results
-                       string inputString = "fasta=" + newFastaFile;
-                       if (namefile != "") { inputString += ", name=" + newNamesFile; }
-                       m->mothurOutEndLine(); 
-                       m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
-                       m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine(); 
-                       m->mothurCalling = true;
+            vector<string> groups;
+                       if (countfile != "") {
+                cparser = new SequenceCountParser(countfile, fastafile);
+                groups = cparser->getNamesOfGroups();
+            }else {
+                if (namefile != "") { parser = new SequenceParser(groupfile, fastafile, namefile);     }
+                else                           { parser = new SequenceParser(groupfile, fastafile);                    }
+                groups = parser->getNamesOfGroups();
+                       }
             
-                       Command* uniqueCommand = new DeconvoluteCommand(inputString);
-                       uniqueCommand->execute();
-                       
-                       map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
-                       
-                       delete uniqueCommand;
-                       m->mothurCalling = false;
-                       m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
-                       
-                       m->renameFile(filenames["fasta"][0], newFastaFile);
-            m->renameFile(filenames["name"][0], newNamesFile);
-                       
+                       if(processors == 1)     {       driverGroups(newFastaFile, newNamesFile, newMapFile, 0, groups.size(), groups); }
+                       else                            {       createProcessesGroups(newFastaFile, newNamesFile, newMapFile, groups);                  }
+                       
+                       if (countfile != "") { 
+                mergeGroupCounts(newCountFile, newNamesFile, newFastaFile);
+                delete cparser; 
+            }else {  
+                delete parser; 
+                //run unique.seqs for deconvolute results
+                string inputString = "fasta=" + newFastaFile;
+                if (namefile != "") { inputString += ", name=" + newNamesFile; }
+                m->mothurOutEndLine(); 
+                m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
+                m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine(); 
+                m->mothurCalling = true;
+                
+                Command* uniqueCommand = new DeconvoluteCommand(inputString);
+                uniqueCommand->execute();
+                
+                map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
+                
+                delete uniqueCommand;
+                m->mothurCalling = false;
+                m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
+                
+                m->renameFile(filenames["fasta"][0], newFastaFile);
+                m->renameFile(filenames["name"][0], newNamesFile); 
+                       }
+            if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {   m->mothurRemove(outputNames[i]);        }        return 0; }
                        m->mothurOut("It took " + toString(time(NULL) - start) + " secs to run pre.cluster."); m->mothurOutEndLine(); 
                                
                }else {
@@ -272,8 +313,9 @@ int PreClusterCommand::execute(){
                        
                        m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
                        m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
-                       printData(newFastaFile, newNamesFile);
-                       
+                       if (countfile != "") { newNamesFile = newCountFile; }
+            printData(newFastaFile, newNamesFile, "");
+                               
                        m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
                }
                                
@@ -295,6 +337,11 @@ int PreClusterCommand::execute(){
                if (itTypes != outputTypes.end()) {
                        if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
                }
+        
+        itTypes = outputTypes.find("count");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
+               }
                
                return 0;
                
@@ -305,7 +352,7 @@ int PreClusterCommand::execute(){
        }
 }
 /**************************************************************************************************/
-int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newFName, string newNName, string newMFile, vector<string> groups) {
+int PreClusterCommand::createProcessesGroups(string newFName, string newNName, string newMFile, vector<string> groups) {
        try {
                
                vector<int> processIDS;
@@ -336,7 +383,7 @@ int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newF
                                process++;
                        }else if (pid == 0){
                 outputNames.clear();
-                               num = driverGroups(parser, newFName + toString(getpid()) + ".temp", newNName + toString(getpid()) + ".temp", newMFile, lines[process].start, lines[process].end, groups);
+                               num = driverGroups(newFName + toString(getpid()) + ".temp", newNName + toString(getpid()) + ".temp", newMFile, lines[process].start, lines[process].end, groups);
                 
                 string tempFile = toString(getpid()) + ".outputNames.temp";
                 ofstream outTemp;
@@ -355,7 +402,7 @@ int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newF
                }
                
                //do my part
-               num = driverGroups(parser, newFName, newNName, newMFile, lines[0].start, lines[0].end, groups);
+               num = driverGroups(newFName, newNName, newMFile, lines[0].start, lines[0].end, groups);
                
                //force parent to wait until all the processes are done
                for (int i=0;i<processIDS.size();i++) { 
@@ -395,7 +442,7 @@ int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newF
                        // Allocate memory for thread data.
                        string extension = toString(i) + ".temp";
                        
-                       preClusterData* tempPreCluster = new preClusterData(fastafile, namefile, groupfile, (newFName+extension), (newNName+extension), newMFile, groups, m, lines[i].start, lines[i].end, diffs, i);
+                       preClusterData* tempPreCluster = new preClusterData(fastafile, namefile, groupfile, countfile, (newFName+extension), (newNName+extension), newMFile, groups, m, lines[i].start, lines[i].end, diffs, i);
                        pDataArray.push_back(tempPreCluster);
                        processIDS.push_back(i);
                        
@@ -406,7 +453,7 @@ int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newF
                
                                
                //using the main process as a worker saves time and memory
-               num = driverGroups(parser, newFName, newNName, newMFile, lines[0].start, lines[0].end, groups);
+               num = driverGroups(newFName, newNName, newMFile, lines[0].start, lines[0].end, groups);
                
                //Wait until all threads have terminated.
                WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
@@ -443,7 +490,7 @@ int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newF
        }
 }
 /**************************************************************************************************/
-int PreClusterCommand::driverGroups(SequenceParser* parser, string newFFile, string newNFile, string newMFile, int start, int end, vector<string> groups){
+int PreClusterCommand::driverGroups(string newFFile, string newNFile, string newMFile, int start, int end, vector<string> groups){
        try {
                
                int numSeqs = 0;
@@ -458,24 +505,29 @@ int PreClusterCommand::driverGroups(SequenceParser* parser, string newFFile, str
                        m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[i] + ":"); m->mothurOutEndLine();
                        
                        map<string, string> thisNameMap;
-                       if (namefile != "") { thisNameMap = parser->getNameMap(groups[i]); }
-                       vector<Sequence> thisSeqs = parser->getSeqs(groups[i]);
-                       
+            vector<Sequence> thisSeqs;
+                       if (groupfile != "") { 
+                thisSeqs = parser->getSeqs(groups[i]);
+            }else if (countfile != "") {
+                thisSeqs = cparser->getSeqs(groups[i]);
+            }
+                       if (namefile != "") {  thisNameMap = parser->getNameMap(groups[i]); }
+            
                        //fill alignSeqs with this groups info.
-                       numSeqs = loadSeqs(thisNameMap, thisSeqs);
+                       numSeqs = loadSeqs(thisNameMap, thisSeqs, groups[i]);
                        
                        if (m->control_pressed) {   return 0; }
                        
                        if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); m->control_pressed = true; return 0;  }
                        
-                       int count = process(newMFile+groups[i]+".map");
+                       int count= process(newMFile+groups[i]+".map");
                        outputNames.push_back(newMFile+groups[i]+".map"); outputTypes["map"].push_back(newMFile+groups[i]+".map");
                        
                        if (m->control_pressed) {  return 0; }
                        
                        m->mothurOut("Total number of sequences before pre.cluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
                        m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
-                       printData(newFFile, newNFile);
+                       printData(newFFile, newNFile, groups[i]);
                        
                        m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
                        
@@ -559,26 +611,13 @@ int PreClusterCommand::readFASTA(){
                //ifstream inNames;
                ifstream inFasta;
                
-               //m->openInputFile(namefile, inNames);
                m->openInputFile(fastafile, inFasta);
-               
-               //string firstCol, secondCol, nameString;
                set<int> lengths;
                
                while (!inFasta.eof()) {
                        
                        if (m->control_pressed) { inFasta.close(); return 0; }
-                       
-                       //inNames >> firstCol >> secondCol;
-                       //nameString = secondCol;
-                       
-                       //m->gobble(inNames);
-                       //int size = 1;
-                       //while (secondCol.find_first_of(',') != -1) { 
-                       //      size++;
-                       //      secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
-                       //}
-                       
+                                               
                        Sequence seq(inFasta);  m->gobble(inFasta);
                        
                        if (seq.getName() != "") {  //can get "" if commented line is at end of fasta file
@@ -592,14 +631,15 @@ int PreClusterCommand::readFASTA(){
                                                lengths.insert(seq.getAligned().length());
                                        }       
                                }else { //no names file, you are identical to yourself 
-                                       seqPNode tempNode(1, seq, seq.getName());
+                    int numRep = 1;
+                    if (countfile != "") { numRep = ct.getNumSeqs(seq.getName()); }
+                                       seqPNode tempNode(numRep, seq, seq.getName());
                                        alignSeqs.push_back(tempNode);
                                        lengths.insert(seq.getAligned().length());
                                }
                        }
                }
                inFasta.close();
-               //inNames.close();
         
         if (lengths.size() > 1) { m->control_pressed = true; m->mothurOut("[ERROR]: your sequences are not all the same length. pre.cluster requires sequences to be aligned."); m->mothurOutEndLine(); }
         else if (lengths.size() == 1) { length = *(lengths.begin()); }
@@ -613,13 +653,15 @@ int PreClusterCommand::readFASTA(){
        }
 }
 /**************************************************************************************************/
-int PreClusterCommand::loadSeqs(map<string, string>& thisName, vector<Sequence>& thisSeqs){
+int PreClusterCommand::loadSeqs(map<string, string>& thisName, vector<Sequence>& thisSeqs, string group){
        try {
                set<int> lengths;
                alignSeqs.clear();
                map<string, string>::iterator it;
                bool error = false;
-                       
+        map<string, int> thisCount;
+        if (countfile != "") { thisCount = cparser->getCountTable(group);  }
+               
                for (int i = 0; i < thisSeqs.size(); i++) {
                        
                        if (m->control_pressed) { return 0; }
@@ -641,12 +683,20 @@ int PreClusterCommand::loadSeqs(map<string, string>& thisName, vector<Sequence>&
                     lengths.insert(thisSeqs[i].getAligned().length());
                                }       
                        }else { //no names file, you are identical to yourself 
-                               seqPNode tempNode(1, thisSeqs[i], thisSeqs[i].getName());
+                int numRep = 1;
+                if (countfile != "") { 
+                    map<string, int>::iterator it2 = thisCount.find(thisSeqs[i].getName());
+                    
+                    //should never be true since parser checks for this
+                    if (it2 == thisCount.end()) { m->mothurOut(thisSeqs[i].getName() + " is not in your count file, please correct."); m->mothurOutEndLine(); error = true; }
+                    else { numRep = it2->second;  }
+                }
+                               seqPNode tempNode(numRep, thisSeqs[i], thisSeqs[i].getName());
                                alignSeqs.push_back(tempNode);
                                lengths.insert(thisSeqs[i].getAligned().length());
                        }
                }
-               
+    
         if (lengths.size() > 1) { error = true; m->mothurOut("[ERROR]: your sequences are not all the same length. pre.cluster requires sequences to be aligned."); m->mothurOutEndLine(); }
         else if (lengths.size() == 1) { length = *(lengths.begin()); }
         
@@ -683,10 +733,84 @@ int PreClusterCommand::calcMisMatches(string seq1, string seq2){
                exit(1);
        }
 }
+/**************************************************************************************************/
+
+int PreClusterCommand::mergeGroupCounts(string newcount, string newname, string newfasta){
+       try {
+               ifstream inNames;
+        m->openInputFile(newname, inNames);
+        
+        string group, first, second;
+        set<string> uniqueNames;
+        while (!inNames.eof()) {
+            if (m->control_pressed) { break; }
+            inNames >> group; m->gobble(inNames);
+            inNames >> first; m->gobble(inNames);
+            inNames >> second; m->gobble(inNames);
+            
+            vector<string> names;
+            m->splitAtComma(second, names);
+            
+            uniqueNames.insert(first);
+            
+            int total = ct.getGroupCount(first, group);
+            for (int i = 1; i < names.size(); i++) {
+                total += ct.getGroupCount(names[i], group);
+                ct.setAbund(names[i], group, 0);
+            }
+            ct.setAbund(first, group, total);
+        }
+        inNames.close();
+        
+        vector<string> namesOfSeqs = ct.getNamesOfSeqs();
+        for (int i = 0; i < namesOfSeqs.size(); i++) {
+            if (ct.getNumSeqs(namesOfSeqs[i]) == 0) {
+                ct.remove(namesOfSeqs[i]);
+            }
+        }
+        
+        ct.printTable(newcount); 
+        m->mothurRemove(newname);
+        
+        if (bygroup) { //if by group, must remove the duplicate seqs that are named the same
+            ifstream in;
+            m->openInputFile(newfasta, in);
+            
+            ofstream out;
+            m->openOutputFile(newfasta+"temp", out);
+            
+            int count = 0;
+            set<string> already;
+            while(!in.eof()) {
+                if (m->control_pressed) { break; }
+                
+                Sequence seq(in); m->gobble(in);
+                
+                if (seq.getName() != "") {
+                    count++;
+                    if (already.count(seq.getName()) == 0) {
+                        seq.printSequence(out);
+                        already.insert(seq.getName());
+                    }
+                }
+            }
+            in.close();
+            out.close();
+            m->mothurRemove(newfasta);
+            m->renameFile(newfasta+"temp", newfasta);
+        }
+                       return 0;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PreClusterCommand", "mergeGroupCounts");
+               exit(1);
+       }
+}
 
 /**************************************************************************************************/
 
-void PreClusterCommand::printData(string newfasta, string newname){
+void PreClusterCommand::printData(string newfasta, string newname, string group){
        try {
                ofstream outFasta;
                ofstream outNames;
@@ -699,10 +823,14 @@ void PreClusterCommand::printData(string newfasta, string newname){
                        m->openOutputFile(newname, outNames);
                }
                
+        if ((countfile != "") && (group == ""))  { outNames << "Representative_Sequence\ttotal\n";  }
                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;
+                               if (countfile != "") {  
+                    if (group != "") {  outNames << group << '\t' << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl; }
+                    else {  outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].numIdentical << endl;  }
+                }else {  outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;  }
                        }
                }