]> git.donarmstrong.com Git - mothur.git/blobdiff - getoturepcommand.cpp
modified bin.seqs and get.oturep commands to include use of a groupfile if provided...
[mothur.git] / getoturepcommand.cpp
index ea53f25ed4cd9d215869349fa9159f03deda6258..e5f87c25f014afe8be7dda943e6f0b5e3e255908 100644 (file)
@@ -20,15 +20,31 @@ GetOTURepCommand::GetOTURepCommand(){
                if(globaldata->gListVector != NULL)             {       
                        listOfNames = new ListVector(*globaldata->gListVector); 
                        
+                       vector<string> names;
+                       string binnames;
                        //map names to rows in sparsematrix
                        for (int i = 0; i < listOfNames->size(); i++) {
-                               nameToIndex[listOfNames->get(i)] = i;
+                               names.clear();
+                               binnames = listOfNames->get(i);
+                               splitAtComma(binnames, names);
+                               
+                               for (int j = 0; j < names.size(); j++) {
+                                       nameToIndex[names[j]] = i;
+                               }
                        }
-               }else { cout << "error" << endl; }
+               }else { cout << "error, no listvector." << endl; }
 
                
                fastafile = globaldata->getFastaFile();
                namesfile = globaldata->getNameFile();
+               groupfile = globaldata->getGroupFile();
+               
+               if (groupfile != "") {
+                       //read in group map info.
+                       groupMap = new GroupMap(groupfile);
+                       groupMap->readMap();
+               }
+
                openInputFile(fastafile, in);
                
                fasta = new FastaMap();
@@ -51,6 +67,9 @@ GetOTURepCommand::~GetOTURepCommand(){
        delete input;
        delete read;
        delete fasta;
+       if (groupfile != "") {
+               delete groupMap;
+       }
 }
 
 //**********************************************************************************************************************
@@ -72,7 +91,7 @@ int GetOTURepCommand::execute(){
                }
                
                //read list file
-               read = new ReadPhilFile(globaldata->getListFile());     
+               read = new ReadOTUFile(globaldata->getListFile());      
                read->read(&*globaldata); 
                
                input = globaldata->ginput;
@@ -83,28 +102,37 @@ int GetOTURepCommand::execute(){
                        if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
                                
                                //create output file
-                               string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fastarep";
+                               string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".rep.fasta";
                                openOutputFile(outputFileName, out);
 
                                cout << list->getLabel() << '\t' << count << endl;
                                
                                //for each bin in the list vector
                                for (int i = 0; i < list->size(); i++) {
-                                       nameRep = FindRep(i);
+                                       string groups;
+                                       nameRep = FindRep(i, groups);
                                        
                                        //print out name and sequence for that bin
                                        sequence = fasta->getSequence(nameRep);
 
                                        if (sequence != "not found") {
-                                               nameRep = nameRep + "bin" + toString(i+1);
-                                               out << ">" << nameRep << endl;
-                                               out << sequence << endl;
+                                               if (groupfile == "") {
+                                                       nameRep = nameRep + "|" + toString(i+1);
+                                                       out << ">" << nameRep << endl;
+                                                       out << sequence << endl;
+                                               }else {
+                                                       nameRep = nameRep + "|" + groups + "|" + toString(i+1);
+                                                       out << ">" << nameRep << endl;
+                                                       out << sequence << endl;
+                                               }
                                        }else { 
                                                cout << nameRep << " is missing from your fasta or name file. Please correct. " << endl; 
                                                remove(outputFileName.c_str());
                                                return 0;
                                        }
                                }
+                               
+                               out.close();
                        }
                        
                        list = input->getListVector();
@@ -163,7 +191,7 @@ void GetOTURepCommand::readNamesFile() {
        }       
 }
 //**********************************************************************************************************************
-string GetOTURepCommand::FindRep(int bin) {
+string GetOTURepCommand::FindRep(int bin, string& group) {
        try{
                vector<string> names;
                map<string, float> sums;
@@ -172,18 +200,41 @@ string GetOTURepCommand::FindRep(int bin) {
                string binnames;
                float min = 10000;
                string minName;
+               map<string, string> groups;
+               map<string, string>::iterator groupIt;
                
                binnames = list->get(bin);
-               
+       
                //parse names into vector
                splitAtComma(binnames, names);
                
+               //if you have a groupfile
+               if(groupfile != "") {
+                       //find the groups that are in this bin
+                       for (int i = 0; i < names.size(); i++) {
+                               string groupName = groupMap->getGroup(names[i]);
+                               if (groupName == "not found") {  
+                                       cout << names[i] << " is missing from your group file. Please correct. " << endl;
+                                       groupError = true;
+                               }else{
+                                       groups[groupName] = groupName;
+                               }
+                       }
+                       
+                       //turn the groups into a string
+                       for(groupIt = groups.begin(); groupIt != groups.end(); groupIt++) { group += groupIt->first + "-"; }
+                       
+                       //rip off last dash
+                       group = group.substr(0, group.length()-1);
+               }
+               
                //if only 1 sequence in bin then that's the rep
                if (names.size() == 1) { return names[0]; }
                else {
                        //fill binMap
                        for (int i = 0; i < names.size(); i++) {
                                for (it3 = nameToIndex.begin(); it3 != nameToIndex.end(); it3++) {
+
                                        if (it3->first == names[i]) {  
                                                binMap[it3->second] = it3->first;