]> git.donarmstrong.com Git - mothur.git/blobdiff - binsequencecommand.cpp
modified bin.seqs and get.oturep commands to include use of a groupfile if provided...
[mothur.git] / binsequencecommand.cpp
index 4db41bf4c1ef91d8fd6a2c11d349aa167bef9bcb..f1ba8b5f38effa755a215e93a87dcf322001dc6d 100644 (file)
@@ -15,8 +15,15 @@ BinSeqCommand::BinSeqCommand(){
                globaldata = GlobalData::getInstance();
                fastafile = globaldata->getFastaFile();
                namesfile = globaldata->getNameFile();
+               groupfile = globaldata->getGroupFile();
                openInputFile(fastafile, in);
                
+               if (groupfile != "") {
+                       //read in group map info.
+                       groupMap = new GroupMap(groupfile);
+                       groupMap->readMap();
+               }
+               
                fasta = new FastaMap();
        }
        catch(exception& e) {
@@ -36,6 +43,9 @@ BinSeqCommand::~BinSeqCommand(){
        delete read;
        delete fasta;
        delete list;
+       if (groupfile != "") {
+               delete groupMap;
+       }
 }
 
 //**********************************************************************************************************************
@@ -57,7 +67,7 @@ int BinSeqCommand::execute(){
                }
                
                //read list file
-               read = new ReadPhilFile(globaldata->getListFile());     
+               read = new ReadOTUFile(globaldata->getListFile());      
                read->read(&*globaldata); 
                
                input = globaldata->ginput;
@@ -67,7 +77,6 @@ int BinSeqCommand::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() + ".fasta";
                                openOutputFile(outputFileName, out);
 
@@ -75,6 +84,7 @@ int BinSeqCommand::execute(){
                                
                                //for each bin in the list vector
                                for (int i = 0; i < list->size(); i++) {
+
                                        binnames = list->get(i);
                                        while (binnames.find_first_of(',') != -1) { 
                                                name = binnames.substr(0,binnames.find_first_of(','));
@@ -83,9 +93,23 @@ int BinSeqCommand::execute(){
                                                //do work for that name
                                                sequence = fasta->getSequence(name);
                                                if (sequence != "not found") {
-                                                       name = name + "bin" + toString(i+1);
-                                                       out << ">" << name << endl;
-                                                       out << sequence << endl;
+                                                       //if you don't have groups
+                                                       if (groupfile == "") {
+                                                               name = name + "|" + toString(i+1);
+                                                               out << ">" << name << endl;
+                                                               out << sequence << endl;
+                                                       }else {//if you do have groups
+                                                               string group = groupMap->getGroup(name);
+                                                               if (group == "not found") {  
+                                                                       cout << name << " is missing from your group file. Please correct. " << endl;
+                                                                       remove(outputFileName.c_str());
+                                                                       return 0;
+                                                               }else{
+                                                                       name = name + "|" + group + "|" + toString(i+1);
+                                                                       out << ">" << name << endl;
+                                                                       out << sequence << endl;
+                                                               }
+                                                       }
                                                }else { 
                                                        cout << name << " is missing from your fasta or name file. Please correct. " << endl; 
                                                        remove(outputFileName.c_str());
@@ -97,17 +121,34 @@ int BinSeqCommand::execute(){
                                        //get last name
                                        sequence = fasta->getSequence(binnames);
                                        if (sequence != "not found") {
-                                               name = binnames + "bin" + toString(i+1);
-                                               out << ">" << name << endl;
-                                               out << sequence << endl;
+                                               //if you don't have groups
+                                               if (groupfile == "") {
+                                                       binnames = binnames + "|" + toString(i+1);
+                                                       out << ">" << binnames << endl;
+                                                       out << sequence << endl;
+                                               }else {//if you do have groups
+                                                       string group = groupMap->getGroup(binnames);
+                                                       if (group == "not found") {  
+                                                               cout << binnames << " is missing from your group file. Please correct. " << endl;
+                                                               remove(outputFileName.c_str());
+                                                               return 0;
+                                                       }else{
+                                                               binnames = binnames + "|" + group + "|" + toString(i+1);
+                                                               out << ">" << binnames << endl;
+                                                               out << sequence << endl;
+                                                       }
+                                               }
                                        }else { 
                                                cout << binnames << " is missing from your fasta or name file. Please correct. " << endl; 
                                                remove(outputFileName.c_str());
                                                return 0;
                                        }
+                                       
                                }
+                               out.close();
                        }
                        
+                       delete list;
                        list = input->getListVector();
                        count++;
                }
@@ -153,12 +194,15 @@ void BinSeqCommand::readNamesFile() {
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
                exit(1);
        }
        catch(...) {
-               cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
                exit(1);
        }       
 }
 //**********************************************************************************************************************
+
+
+