]> git.donarmstrong.com Git - mothur.git/blobdiff - trimseqscommand.cpp
captured stdout from system command, fixed bug in trim.seqs added error checks for...
[mothur.git] / trimseqscommand.cpp
index dad8b98b64f99026388b32c7d47e60dab33e1fba..4d6b45da96664677f61908874d79b8d847f0e74c 100644 (file)
@@ -240,7 +240,7 @@ TrimSeqsCommand::TrimSeqsCommand(string option)  {
                        
                        temp = validParameter.validFile(parameters, "name", true);      
                        if (temp == "not found")        {       nameFile = "";          }
-                       else if(temp == "not open")     {       abort = true;           }
+                       else if(temp == "not open")     {       nameFile = "";  abort = true;           }
                        else                                            {       nameFile = temp;        }
                        
                        temp = validParameter.validFile(parameters, "qthreshold", false);       if (temp == "not found") { temp = "0"; }
@@ -369,7 +369,7 @@ int TrimSeqsCommand::execute(){
                #endif
                
                if (m->control_pressed) {  return 0; }                  
-       
+       
                if(allFiles){
                        map<string, string> uniqueFastaNames;// so we don't add the same groupfile multiple times
                        map<string, string>::iterator it;
@@ -432,8 +432,9 @@ int TrimSeqsCommand::execute(){
                //output group counts
                m->mothurOutEndLine();
                int total = 0;
+               if (groupCounts.size() != 0) {  m->mothurOut("Group count: \n");  }
                for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
-                        total += it->second; m->mothurOut("Group " + it->first + " contains " + toString(it->second) + " sequences."); m->mothurOutEndLine(); 
+                        total += it->second; m->mothurOut(it->first + "\t" + toString(it->second)); m->mothurOutEndLine(); 
                }
                if (total != 0) { m->mothurOut("Total of all groups is " + toString(total)); m->mothurOutEndLine(); }
                
@@ -554,7 +555,6 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                        int currentSeqsDiffs = 0;
 
                        Sequence currSeq(inFASTA); m->gobble(inFASTA);
-                       
                        QualityScores currQual;
                        if(qFileName != ""){
                                currQual = QualityScores(qFile);  m->gobble(qFile);
@@ -650,6 +650,17 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                                                
                                                outGroupsFile << currSeq.getName() << '\t' << thisGroup << endl;
                                                
+                                               if (nameFile != "") {
+                                                       map<string, string>::iterator itName = nameMap.find(currSeq.getName());
+                                                       if (itName != nameMap.end()) { 
+                                                               vector<string> thisSeqsNames; 
+                                                               m->splitAtChar(itName->second, thisSeqsNames, ',');
+                                                               for (int k = 1; k < thisSeqsNames.size(); k++) { //start at 1 to skip self
+                                                                       outGroupsFile << thisSeqsNames[k] << '\t' << thisGroup << endl;
+                                                               }
+                                                       }else { m->mothurOut("[ERROR]: " + currSeq.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); }                                                   
+                                               }
+                                               
                                                map<string, int>::iterator it = groupCounts.find(thisGroup);
                                                if (it == groupCounts.end()) {  groupCounts[thisGroup] = 1; }
                                                else { groupCounts[it->first]++; }
@@ -680,6 +691,11 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                                        }
                                }
                                else{
+                                       if(nameFile != ""){ //needs to be before the currSeq name is changed
+                                               map<string, string>::iterator itName = nameMap.find(currSeq.getName());
+                                               if (itName != nameMap.end()) {  scrapNameFile << itName->first << '\t' << itName->second << endl; }
+                                               else { m->mothurOut("[ERROR]: " + currSeq.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); }
+                                       }
                                        currSeq.setName(currSeq.getName() + '|' + trashCode);
                                        currSeq.setUnaligned(origSeq);
                                        currSeq.setAligned(origSeq);
@@ -687,11 +703,6 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                                        if(qFileName != ""){
                                                currQual.printQScores(scrapQualFile);
                                        }
-                                       if(nameFile != ""){
-                                               map<string, string>::iterator itName = nameMap.find(currSeq.getName());
-                                               if (itName != nameMap.end()) {  scrapNameFile << itName->first << '\t' << itName->second << endl; }
-                                                       else { m->mothurOut("[ERROR]: " + currSeq.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); }
-                                       }
                                }
                                count++;
                        }
@@ -699,17 +710,18 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                                unsigned long int pos = inFASTA.tellg();
                                if ((pos == -1) || (pos >= line->end)) { break; }
+                       
                        #else
                                if (inFASTA.eof()) { break; }
                        #endif
-                               
+                       
                        //report progress
                        if((count) % 1000 == 0){        m->mothurOut(toString(count)); m->mothurOutEndLine();           }
                        
                }
                //report progress
                if((count) % 1000 != 0){        m->mothurOut(toString(count)); m->mothurOutEndLine();           }
-
+               
                
                inFASTA.close();
                trimFASTAFile.close();
@@ -786,14 +798,18 @@ int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName
                                                                 qLines[process]);
                                
                                //pass groupCounts to parent
-                               ofstream out;
-                               string tempFile = filename + toString(getpid()) + ".num.temp";
-                               m->openOutputFile(tempFile, out);
-                               for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
-                                       out << it->first << '\t' << it->second << endl;
+                               if(oligoFile != ""){
+                                       ofstream out;
+                                       string tempFile = filename + toString(getpid()) + ".num.temp";
+                                       m->openOutputFile(tempFile, out);
+                                       
+                                       out << groupCounts.size() << endl;
+                                       
+                                       for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
+                                               out << it->first << '\t' << it->second << endl;
+                                       }
+                                       out.close();
                                }
-                               out.close();
-                               
                                exit(0);
                        }else { 
                                m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
@@ -806,10 +822,14 @@ int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName
                ofstream temp;
                m->openOutputFile(trimFASTAFileName, temp);             temp.close();
                m->openOutputFile(scrapFASTAFileName, temp);    temp.close();
-               m->openOutputFile(trimQualFileName, temp);              temp.close();
-               m->openOutputFile(scrapQualFileName, temp);             temp.close();
-               m->openOutputFile(trimNameFileName, temp);              temp.close();
-               m->openOutputFile(scrapNameFileName, temp);             temp.close();
+               if(qFileName != ""){
+                       m->openOutputFile(trimQualFileName, temp);              temp.close();
+                       m->openOutputFile(scrapQualFileName, temp);             temp.close();
+               }
+               if (nameFile != "") {
+                       m->openOutputFile(trimNameFileName, temp);              temp.close();
+                       m->openOutputFile(scrapNameFileName, temp);             temp.close();
+               }
 
                driverCreateTrim(filename, qFileName, trimFASTAFileName, scrapFASTAFileName, trimQualFileName, scrapQualFileName, trimNameFileName, scrapNameFileName, groupFile, fastaFileNames, qualFileNames, nameFileNames, lines[0], qLines[0]);
                
@@ -843,8 +863,10 @@ int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName
                                remove((scrapNameFileName + toString(processIDS[i]) + ".temp").c_str());
                        }
                        
-                       m->appendFiles((groupFile + toString(processIDS[i]) + ".temp"), groupFile);
-                       remove((groupFile + toString(processIDS[i]) + ".temp").c_str());
+                       if(oligoFile != ""){
+                               m->appendFiles((groupFile + toString(processIDS[i]) + ".temp"), groupFile);
+                               remove((groupFile + toString(processIDS[i]) + ".temp").c_str());
+                       }
                        
                        
                        if(allFiles){
@@ -868,19 +890,26 @@ int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName
                                }
                        }
                        
-                       ifstream in;
-                       string tempFile =  filename + toString(processIDS[i]) + ".num.temp";
-                       m->openInputFile(tempFile, in);
-                       int tempNum;
-                       string group;
-                       while (!in.eof()) { 
-                               in >> group >> tempNum; m->gobble(in);
+                       if(oligoFile != ""){
+                               ifstream in;
+                               string tempFile =  filename + toString(processIDS[i]) + ".num.temp";
+                               m->openInputFile(tempFile, in);
+                               int tempNum;
+                               string group;
                                
-                               map<string, int>::iterator it = groupCounts.find(group);
-                               if (it == groupCounts.end()) {  groupCounts[group] = tempNum; }
-                               else { groupCounts[it->first] += tempNum; }
+                               in >> tempNum; m->gobble(in);
+                               
+                               if (tempNum != 0) {
+                                       while (!in.eof()) { 
+                                               in >> group >> tempNum; m->gobble(in);
+                               
+                                               map<string, int>::iterator it = groupCounts.find(group);
+                                               if (it == groupCounts.end()) {  groupCounts[group] = tempNum; }
+                                               else { groupCounts[it->first] += tempNum; }
+                                       }
+                               }
+                               in.close(); remove(tempFile.c_str());
                        }
-                       in.close(); remove(tempFile.c_str());
                        
                }
        
@@ -1143,7 +1172,7 @@ void TrimSeqsCommand::getOligos(vector<vector<string> >& fastaFileNames, vector<
                                        
                                        if(qFileName != ""){
                                                qualFileName = outputDir + m->getRootName(m->getSimpleName(qFileName)) + comboGroupName + ".qual";
-                                               if (uniqueNames.count(fastaFileName) == 0) {
+                                               if (uniqueNames.count(qualFileName) == 0) {
                                                        outputNames.push_back(qualFileName);
                                                        outputTypes["qfile"].push_back(qualFileName);
                                                }
@@ -1154,7 +1183,7 @@ void TrimSeqsCommand::getOligos(vector<vector<string> >& fastaFileNames, vector<
                                        
                                        if(nameFile != ""){
                                                nameFileName = outputDir + m->getRootName(m->getSimpleName(nameFile)) + comboGroupName + ".names";
-                                               if (uniqueNames.count(fastaFileName) == 0) {
+                                               if (uniqueNames.count(nameFileName) == 0) {
                                                        outputNames.push_back(nameFileName);
                                                        outputTypes["name"].push_back(nameFileName);
                                                }