]> git.donarmstrong.com Git - mothur.git/blob - trimseqscommand.cpp
ef7dfdd0f45d649cc6296d91e106e75b9a6fd167
[mothur.git] / trimseqscommand.cpp
1 /*
2  *  trimseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 6/6/09.
6  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "trimseqscommand.h"
11
12 //***************************************************************************************************************
13
14 TrimSeqsCommand::TrimSeqsCommand(string option)  {
15         try {
16                 
17                 abort = false;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string AlignArray[] =  {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile", 
25                                                                         "qthreshold", "qaverage", "allfiles", "qtrim", "processors", "outputdir","inputdir"};
26                         
27                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28                         
29                         OptionParser parser(option);
30                         map<string,string> parameters = parser.getParameters();
31                         
32                         ValidParameters validParameter;
33                         map<string,string>::iterator it;
34                         
35                         //check to make sure all parameters are valid for command
36                         for (it = parameters.begin(); it != parameters.end(); it++) { 
37                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
38                         }
39                         
40                         //if the user changes the input directory command factory will send this info to us in the output parameter 
41                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
42                         if (inputDir == "not found"){   inputDir = "";          }
43                         else {
44                                 string path;
45                                 it = parameters.find("fasta");
46                                 //user has given a template file
47                                 if(it != parameters.end()){ 
48                                         path = hasPath(it->second);
49                                         //if the user has not given a path then, add inputdir. else leave path alone.
50                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
51                                 }
52                                 
53                                 it = parameters.find("oligos");
54                                 //user has given a template file
55                                 if(it != parameters.end()){ 
56                                         path = hasPath(it->second);
57                                         //if the user has not given a path then, add inputdir. else leave path alone.
58                                         if (path == "") {       parameters["oligos"] = inputDir + it->second;           }
59                                 }
60                                 
61                                 it = parameters.find("qfile");
62                                 //user has given a template file
63                                 if(it != parameters.end()){ 
64                                         path = hasPath(it->second);
65                                         //if the user has not given a path then, add inputdir. else leave path alone.
66                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
67                                 }
68                         }
69
70                         
71                         //check for required parameters
72                         fastaFile = validParameter.validFile(parameters, "fasta", true);
73                         if (fastaFile == "not found") { m->mothurOut("fasta is a required parameter for the screen.seqs command."); m->mothurOutEndLine(); abort = true; }
74                         else if (fastaFile == "not open") { abort = true; }     
75                         
76                         //if the user changes the output directory command factory will send this info to us in the output parameter 
77                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
78                                 outputDir = ""; 
79                                 outputDir += hasPath(fastaFile); //if user entered a file with a path then preserve it  
80                         }
81                 
82                         //check for optional parameter and set defaults
83                         // ...at some point should added some additional type checking...
84                         string temp;
85                         temp = validParameter.validFile(parameters, "flip", false);
86                         if (temp == "not found"){       flip = 0;       }
87                         else if(isTrue(temp))   {       flip = 1;       }
88                 
89                         temp = validParameter.validFile(parameters, "oligos", true);
90                         if (temp == "not found"){       oligoFile = "";         }
91                         else if(temp == "not open"){    abort = true;   } 
92                         else                                    {       oligoFile = temp;               }
93                         
94                         temp = validParameter.validFile(parameters, "maxambig", false);         if (temp == "not found") { temp = "-1"; }
95                         convert(temp, maxAmbig);  
96
97                         temp = validParameter.validFile(parameters, "maxhomop", false);         if (temp == "not found") { temp = "0"; }
98                         convert(temp, maxHomoP);  
99
100                         temp = validParameter.validFile(parameters, "minlength", false);        if (temp == "not found") { temp = "0"; }
101                         convert(temp, minLength); 
102                         
103                         temp = validParameter.validFile(parameters, "maxlength", false);        if (temp == "not found") { temp = "0"; }
104                         convert(temp, maxLength);
105                         
106                         temp = validParameter.validFile(parameters, "qfile", true);     
107                         if (temp == "not found")        {       qFileName = "";         }
108                         else if(temp == "not open")     {       abort = true;           }
109                         else                                            {       qFileName = temp;       }
110                         
111                         temp = validParameter.validFile(parameters, "qthreshold", false);       if (temp == "not found") { temp = "0"; }
112                         convert(temp, qThreshold);
113                         
114                         temp = validParameter.validFile(parameters, "qtrim", false);    if (temp == "not found") { temp = "F"; }
115                         qtrim = isTrue(temp);
116
117                         temp = validParameter.validFile(parameters, "qaverage", false);         if (temp == "not found") { temp = "0"; }
118                         convert(temp, qAverage);
119                         
120                         temp = validParameter.validFile(parameters, "allfiles", false);         if (temp == "not found") { temp = "F"; }
121                         allFiles = isTrue(temp);
122                         
123                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = "1";                             }
124                         convert(temp, processors); 
125                         
126                         if(allFiles && oligoFile == ""){
127                                 m->mothurOut("You selected allfiles, but didn't enter an oligos file.  Ignoring the allfiles request."); m->mothurOutEndLine();
128                         }
129                         if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
130                                 m->mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); m->mothurOutEndLine();
131                                 qAverage=0;
132                                 qThreshold=0;
133                         }
134                         if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){               
135                                 m->mothurOut("You didn't set any options... quiting command."); m->mothurOutEndLine();
136                                 abort = true;
137                         }
138                 }
139
140         }
141         catch(exception& e) {
142                 m->errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
143                 exit(1);
144         }
145 }
146 //**********************************************************************************************************************
147
148 void TrimSeqsCommand::help(){
149         try {
150                 m->mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
151                 m->mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, qtrim and allfiles.\n");
152                 m->mothurOut("The fasta parameter is required.\n");
153                 m->mothurOut("The flip parameter .... The default is 0.\n");
154                 m->mothurOut("The oligos parameter .... The default is "".\n");
155                 m->mothurOut("The maxambig parameter .... The default is -1.\n");
156                 m->mothurOut("The maxhomop parameter .... The default is 0.\n");
157                 m->mothurOut("The minlength parameter .... The default is 0.\n");
158                 m->mothurOut("The maxlength parameter .... The default is 0.\n");
159                 m->mothurOut("The qfile parameter .....\n");
160                 m->mothurOut("The qthreshold parameter .... The default is 0.\n");
161                 m->mothurOut("The qaverage parameter .... The default is 0.\n");
162                 m->mothurOut("The allfiles parameter .... The default is F.\n");
163                 m->mothurOut("The qtrim parameter .... The default is F.\n");
164                 m->mothurOut("The trim.seqs command should be in the following format: \n");
165                 m->mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig,  \n");
166                 m->mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength)  \n");    
167                 m->mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
168                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
169                 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.seqs .\n\n");
170
171         }
172         catch(exception& e) {
173                 m->errorOut(e, "TrimSeqsCommand", "help");
174                 exit(1);
175         }
176 }
177
178
179 //***************************************************************************************************************
180
181 TrimSeqsCommand::~TrimSeqsCommand(){    /*      do nothing      */      }
182
183 //***************************************************************************************************************
184
185 int TrimSeqsCommand::execute(){
186         try{
187         
188                 if (abort == true) { return 0; }
189                 
190                 numFPrimers = 0;  //this needs to be initialized
191                 numRPrimers = 0;
192                 
193                 string trimSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "trim.fasta";
194                 outputNames.push_back(trimSeqFile);
195                 string scrapSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "scrap.fasta";
196                 outputNames.push_back(scrapSeqFile);
197                 string groupFile = outputDir + getRootName(getSimpleName(fastaFile)) + "groups";
198                 
199                 vector<string> fastaFileNames;
200                 if(oligoFile != ""){
201                         outputNames.push_back(groupFile);
202                         getOligos(fastaFileNames);
203                 }
204                 
205                 if(qFileName != "")     {       setLines(qFileName, qLines);    }
206
207                 
208                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
209                                 if(processors == 1){
210                                         ifstream inFASTA;
211                                         openInputFile(fastaFile, inFASTA);
212                                         int numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
213                                         inFASTA.close();
214                                         
215                                         lines.push_back(new linePair(0, numSeqs));
216                                         
217                                         driverCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
218                                         
219                                         for (int j = 0; j < fastaFileNames.size(); j++) {
220                                                 rename((fastaFileNames[j] + toString(getpid()) + ".temp").c_str(), fastaFileNames[j].c_str());
221                                         }
222
223                                 }else{
224                                         setLines(fastaFile, lines);     
225                                         if(qFileName == "")     {       qLines = lines; }       
226                                                                 
227                                         createProcessesCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames); 
228                                         
229                                         rename((trimSeqFile + toString(processIDS[0]) + ".temp").c_str(), trimSeqFile.c_str());
230                                         rename((scrapSeqFile + toString(processIDS[0]) + ".temp").c_str(), scrapSeqFile.c_str());
231                                         rename((groupFile + toString(processIDS[0]) + ".temp").c_str(), groupFile.c_str());
232                                         for (int j = 0; j < fastaFileNames.size(); j++) {
233                                                 rename((fastaFileNames[j] + toString(processIDS[0]) + ".temp").c_str(), fastaFileNames[j].c_str());
234                                         }
235                                         //append files
236                                         for(int i=1;i<processors;i++){
237                                                 appendFiles((trimSeqFile + toString(processIDS[i]) + ".temp"), trimSeqFile);
238                                                 remove((trimSeqFile + toString(processIDS[i]) + ".temp").c_str());
239                                                 appendFiles((scrapSeqFile + toString(processIDS[i]) + ".temp"), scrapSeqFile);
240                                                 remove((scrapSeqFile + toString(processIDS[i]) + ".temp").c_str());
241                                                 appendFiles((groupFile + toString(processIDS[i]) + ".temp"), groupFile);
242                                                 remove((groupFile + toString(processIDS[i]) + ".temp").c_str());
243                                                 for (int j = 0; j < fastaFileNames.size(); j++) {
244                                                         appendFiles((fastaFileNames[j] + toString(processIDS[i]) + ".temp"), fastaFileNames[j]);
245                                                         remove((fastaFileNames[j] + toString(processIDS[i]) + ".temp").c_str());
246                                                 }
247                                         }
248                                 }
249                                 
250                                 if (m->control_pressed) {  return 0; }
251                 #else
252                                 ifstream inFASTA;
253                                 openInputFile(fastafileNames[s], inFASTA);
254                                 numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
255                                 inFASTA.close();
256                                 
257                                 lines.push_back(new linePair(0, numSeqs));
258                                 
259                                 driverCreateSummary(fastafile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
260                                 
261                                 if (m->control_pressed) {  return 0; }
262                 #endif
263                                                 
264                                                                                 
265                 for(int i=0;i<fastaFileNames.size();i++){
266                         ifstream inFASTA;
267                         string seqName;
268                         openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
269                         ofstream outGroups;
270                         openOutputFile(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups", outGroups);
271                         outputNames.push_back(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups");
272                         
273                         while(!inFASTA.eof()){
274                                 if(inFASTA.get() == '>'){
275                                         inFASTA >> seqName;
276                                         outGroups << seqName << '\t' << groupVector[i] << endl;
277                                 }
278                                 while (!inFASTA.eof())  {       char c = inFASTA.get(); if (c == 10 || c == 13){        break;  }       }
279                         }
280                         outGroups.close();
281                         inFASTA.close();
282                 }
283                 
284                 if (m->control_pressed) { 
285                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }
286                         return 0;
287                 }
288
289                 m->mothurOutEndLine();
290                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
291                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
292                 m->mothurOutEndLine();
293                 
294                 return 0;       
295                         
296         }
297         catch(exception& e) {
298                 m->errorOut(e, "TrimSeqsCommand", "execute");
299                 exit(1);
300         }
301 }
302                 
303 /**************************************************************************************/
304 int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames, linePair* line, linePair* qline) {     
305         try {
306                 
307                 ofstream outFASTA;
308                 int able = openOutputFile(trimFile, outFASTA);
309                 
310                 ofstream scrapFASTA;
311                 openOutputFile(scrapFile, scrapFASTA);
312                 
313                 ofstream outGroups;
314                 vector<ofstream*> fastaFileNames;
315                 if (oligoFile != "") {          
316                         openOutputFile(groupFile, outGroups);   
317                         for (int i = 0; i < fastaNames.size(); i++) {
318                                 fastaFileNames.push_back(new ofstream((fastaNames[i] + toString(getpid()) + ".temp").c_str(), ios::ate)); 
319                         }
320                 }
321                 
322                 ifstream inFASTA;
323                 openInputFile(filename, inFASTA);
324                 
325                 ifstream qFile;
326                 if(qFileName != "")     {       openInputFile(qFileName, qFile);        }
327                 
328                 qFile.seekg(qline->start);
329                 inFASTA.seekg(line->start);
330                 
331                 for(int i=0;i<line->num;i++){
332                                 
333                         if (m->control_pressed) { 
334                                 inFASTA.close(); 
335                                 outFASTA.close();
336                                 scrapFASTA.close();
337                                 if (oligoFile != "") {   outGroups.close();   }
338                                 if(qFileName != "")     {       qFile.close();  }
339                                 for(int i=0;i<fastaFileNames.size();i++){
340                                         fastaFileNames[i]->close();
341                                         delete fastaFileNames[i];
342                                 }       
343                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }
344                                 return 0;
345                         }
346                         
347                         bool success = 1;
348                         
349                         Sequence currSeq(inFASTA);
350
351                         string origSeq = currSeq.getUnaligned();
352                         if (origSeq != "") {
353                                 int group;
354                                 string trashCode = "";
355                                 
356                                 if(qFileName != ""){
357                                         if(qThreshold != 0)             {       success = stripQualThreshold(currSeq, qFile);   }
358                                         else if(qAverage != 0)  {       success = cullQualAverage(currSeq, qFile);              }
359                                         if ((!qtrim) && (origSeq.length() != currSeq.getUnaligned().length())) { 
360                                                 success = 0; //if you don't want to trim and the sequence does not meet quality requirements, move to scrap
361                                         }
362                                         if(!success)                    {       trashCode += 'q';                                                               }
363                                 }
364                         
365                                 if(barcodes.size() != 0){
366                                         success = stripBarcode(currSeq, group);
367                                         if(!success){   trashCode += 'b';       }
368                                 }
369                         
370                                 if(numFPrimers != 0){
371                                         success = stripForward(currSeq);
372                                         if(!success){   trashCode += 'f';       }
373                                 }
374                                         
375                                 if(numRPrimers != 0){
376                                         success = stripReverse(currSeq);
377                                         if(!success){   trashCode += 'r';       }
378                                 }
379                 
380                                 if(minLength > 0 || maxLength > 0){
381                                         success = cullLength(currSeq);
382                                         if(!success){   trashCode += 'l'; }
383                                 }
384                                 if(maxHomoP > 0){
385                                         success = cullHomoP(currSeq);
386                                         if(!success){   trashCode += 'h';       }
387                                 }
388                                 if(maxAmbig != -1){
389                                         success = cullAmbigs(currSeq);
390                                         if(!success){   trashCode += 'n';       }
391                                 }
392                                 
393                                 if(flip){       currSeq.reverseComplement();    }               // should go last                       
394                                 
395                                 if(trashCode.length() == 0){
396                                         currSeq.setAligned(currSeq.getUnaligned());  //this is because of a modification we made to the sequence class to fix a bug.  all seqs have an aligned version, which is the version that gets printed.
397                                         currSeq.printSequence(outFASTA);
398                                         if(barcodes.size() != 0){
399                                                 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
400                                                 
401                                                 if(allFiles){
402                                                         currSeq.printSequence(*fastaFileNames[group]);                                  
403                                                 }
404                                         }
405                                 }
406                                 else{
407                                         currSeq.setName(currSeq.getName() + '|' + trashCode);
408                                         currSeq.setUnaligned(origSeq);
409                                         currSeq.printSequence(scrapFASTA);
410                                 }
411                         }
412                         gobble(inFASTA);
413                 }
414                 
415                 inFASTA.close();
416                 outFASTA.close();
417                 scrapFASTA.close();
418                 if (oligoFile != "") {   outGroups.close();   }
419                 if(qFileName != "")     {       qFile.close();  }
420                 
421                 for(int i=0;i<fastaFileNames.size();i++){
422                         fastaFileNames[i]->close();
423                         delete fastaFileNames[i];
424                 }               
425                 
426                 return 0;
427         }
428         catch(exception& e) {
429                 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
430                 exit(1);
431         }
432 }
433 /**************************************************************************************************/
434 int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames) {
435         try {
436 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
437                 int process = 0;
438                 int exitCommand = 1;
439                 processIDS.clear();
440                 
441                 //loop through and create all the processes you want
442                 while (process != processors) {
443                         int pid = fork();
444                         
445                         if (pid > 0) {
446                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
447                                 process++;
448                         }else if (pid == 0){
449                                 driverCreateTrim(filename, qFileName, (trimFile + toString(getpid()) + ".temp"), (scrapFile + toString(getpid()) + ".temp"), (groupFile + toString(getpid()) + ".temp"), fastaNames, lines[process], qLines[process]);
450                                 exit(0);
451                         }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
452                 }
453                 
454                 //force parent to wait until all the processes are done
455                 for (int i=0;i<processors;i++) { 
456                         int temp = processIDS[i];
457                         wait(&temp);
458                 }
459                 
460                 return exitCommand;
461 #endif          
462         }
463         catch(exception& e) {
464                 m->errorOut(e, "TrimSeqsCommand", "createProcessesCreateTrim");
465                 exit(1);
466         }
467 }
468 /**************************************************************************************************/
469
470 int TrimSeqsCommand::setLines(string filename, vector<linePair*>& lines) {
471         try {
472                 
473                 lines.clear();
474                 
475                 vector<long int> positions;
476                 
477                 ifstream inFASTA;
478                 openInputFile(filename, inFASTA);
479                         
480                 string input;
481                 while(!inFASTA.eof()){  
482                         input = getline(inFASTA);
483
484                         if (input.length() != 0) {
485                                 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1);     }
486                         }
487                 }
488                 inFASTA.close();
489                 
490                 int numFastaSeqs = positions.size();
491         
492                 FILE * pFile;
493                 long size;
494                 
495                 //get num bytes in file
496                 pFile = fopen (filename.c_str(),"rb");
497                 if (pFile==NULL) perror ("Error opening file");
498                 else{
499                         fseek (pFile, 0, SEEK_END);
500                         size=ftell (pFile);
501                         fclose (pFile);
502                 }
503                 
504                 int numSeqsPerProcessor = numFastaSeqs / processors;
505                 
506                 for (int i = 0; i < processors; i++) {
507
508                         long int startPos = positions[ i * numSeqsPerProcessor ];
509                         if(i == processors - 1){
510                                 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
511                         }else{  
512                                 long int myEnd = positions[ (i+1) * numSeqsPerProcessor ];
513                         }
514                         lines.push_back(new linePair(startPos, numSeqsPerProcessor));
515                 }
516                 
517                 return numFastaSeqs;
518         }
519         catch(exception& e) {
520                 m->errorOut(e, "TrimSeqsCommand", "setLines");
521                 exit(1);
522         }
523 }
524 //***************************************************************************************************************
525
526 void TrimSeqsCommand::getOligos(vector<string>& outFASTAVec){ //vector<ofstream*>& outFASTAVec
527         try {
528                 ifstream inOligos;
529                 openInputFile(oligoFile, inOligos);
530                 
531                 ofstream test;
532                 
533                 string type, oligo, group;
534                 int index=0;
535                 
536                 while(!inOligos.eof()){
537                         inOligos >> type;
538                         
539                         if(type[0] == '#'){
540                                 while (!inOligos.eof()) {       char c = inOligos.get(); if (c == 10 || c == 13){       break;  }       } // get rest of line if there's any crap there
541                         }
542                         else{
543                                 inOligos >> oligo;
544                                 
545                                 for(int i=0;i<oligo.length();i++){
546                                         oligo[i] = toupper(oligo[i]);
547                                         if(oligo[i] == 'U')     {       oligo[i] = 'T'; }
548                                 }
549                                 
550                                 if(type == "forward"){
551                                         forPrimer.push_back(oligo);
552                                 }
553                                 else if(type == "reverse"){
554                                         Sequence oligoRC("reverse", oligo);
555                                         oligoRC.reverseComplement();
556                                         revPrimer.push_back(oligoRC.getUnaligned());
557                                 }
558                                 else if(type == "barcode"){
559                                         inOligos >> group;
560                                         barcodes[oligo]=index++;
561                                         groupVector.push_back(group);
562                                         
563                                         if(allFiles){
564                                                 //outFASTAVec.push_back(new ofstream((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta").c_str(), ios::ate));
565                                                 outputNames.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta"));
566                                                 outFASTAVec.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta"));
567                                         }
568                                 }
569                         }
570                 }
571                 
572                 inOligos.close();
573                 
574                 numFPrimers = forPrimer.size();
575                 numRPrimers = revPrimer.size();
576                 
577         }
578         catch(exception& e) {
579                 m->errorOut(e, "TrimSeqsCommand", "getOligos");
580                 exit(1);
581         }
582
583 }
584
585 //***************************************************************************************************************
586
587 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
588         try {
589                 string rawSequence = seq.getUnaligned();
590                 bool success = 0;       //guilty until proven innocent
591                 
592                 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
593                         string oligo = it->first;
594                         if(rawSequence.length() < oligo.length()){      //let's just assume that the barcodes are the same length
595                                 success = 0;
596                                 break;
597                         }
598                         
599                         if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
600                                 group = it->second;
601                                 seq.setUnaligned(rawSequence.substr(oligo.length()));
602                                 success = 1;
603                                 break;
604                         }
605                 }
606                 return success;
607                 
608         }
609         catch(exception& e) {
610                 m->errorOut(e, "TrimSeqsCommand", "stripBarcode");
611                 exit(1);
612         }
613
614 }
615
616 //***************************************************************************************************************
617
618 bool TrimSeqsCommand::stripForward(Sequence& seq){
619         try {
620                 string rawSequence = seq.getUnaligned();
621                 bool success = 0;       //guilty until proven innocent
622                 
623                 for(int i=0;i<numFPrimers;i++){
624                         string oligo = forPrimer[i];
625                         
626                         if(rawSequence.length() < oligo.length()){
627                                 success = 0;
628                                 break;
629                         }
630                         
631                         if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
632                                 seq.setUnaligned(rawSequence.substr(oligo.length()));
633                                 success = 1;
634                                 break;
635                         }
636                 }
637                 
638                 return success;
639                 
640         }
641         catch(exception& e) {
642                 m->errorOut(e, "TrimSeqsCommand", "stripForward");
643                 exit(1);
644         }
645 }
646
647 //***************************************************************************************************************
648
649 bool TrimSeqsCommand::stripReverse(Sequence& seq){
650         try {
651                 string rawSequence = seq.getUnaligned();
652                 bool success = 0;       //guilty until proven innocent
653                 
654                 for(int i=0;i<numRPrimers;i++){
655                         string oligo = revPrimer[i];
656                         
657                         if(rawSequence.length() < oligo.length()){
658                                 success = 0;
659                                 break;
660                         }
661                         
662                         if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
663                                 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
664                                 success = 1;
665                                 break;
666                         }
667                 }       
668                 return success;
669                 
670         }
671         catch(exception& e) {
672                 m->errorOut(e, "TrimSeqsCommand", "stripReverse");
673                 exit(1);
674         }
675 }
676
677 //***************************************************************************************************************
678
679 bool TrimSeqsCommand::cullLength(Sequence& seq){
680         try {
681         
682                 int length = seq.getNumBases();
683                 bool success = 0;       //guilty until proven innocent
684                 
685                 if(length >= minLength && maxLength == 0)                       {       success = 1;    }
686                 else if(length >= minLength && length <= maxLength)     {       success = 1;    }
687                 else                                                                                            {       success = 0;    }
688                 
689                 return success;
690         
691         }
692         catch(exception& e) {
693                 m->errorOut(e, "TrimSeqsCommand", "cullLength");
694                 exit(1);
695         }
696         
697 }
698
699 //***************************************************************************************************************
700
701 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
702         try {
703                 int longHomoP = seq.getLongHomoPolymer();
704                 bool success = 0;       //guilty until proven innocent
705                 
706                 if(longHomoP <= maxHomoP){      success = 1;    }
707                 else                                    {       success = 0;    }
708                 
709                 return success;
710         }
711         catch(exception& e) {
712                 m->errorOut(e, "TrimSeqsCommand", "cullHomoP");
713                 exit(1);
714         }
715         
716 }
717
718 //***************************************************************************************************************
719
720 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
721         try {
722                 int numNs = seq.getAmbigBases();
723                 bool success = 0;       //guilty until proven innocent
724                 
725                 if(numNs <= maxAmbig)   {       success = 1;    }
726                 else                                    {       success = 0;    }
727                 
728                 return success;
729         }
730         catch(exception& e) {
731                 m->errorOut(e, "TrimSeqsCommand", "cullAmbigs");
732                 exit(1);
733         }
734         
735 }
736
737 //***************************************************************************************************************
738
739 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
740         try {
741                 bool success = 1;
742                 int length = oligo.length();
743                 
744                 for(int i=0;i<length;i++){
745                         
746                         if(oligo[i] != seq[i]){
747                                 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C')    {       success = 0;    }
748                                 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N'))                                {       success = 0;    }
749                                 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G'))                                    {       success = 0;    }
750                                 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T'))                                    {       success = 0;    }
751                                 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A'))                                    {       success = 0;    }
752                                 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G'))                                    {       success = 0;    }
753                                 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A'))                                    {       success = 0;    }
754                                 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G'))                                    {       success = 0;    }
755                                 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G'))   {       success = 0;    }
756                                 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G'))   {       success = 0;    }
757                                 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C'))   {       success = 0;    }
758                                 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G'))   {       success = 0;    }                       
759                                 
760                                 if(success == 0)        {       break;  }
761                         }
762                         else{
763                                 success = 1;
764                         }
765                 }
766                 
767                 return success;
768         }
769         catch(exception& e) {
770                 m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
771                 exit(1);
772         }
773
774 }
775
776 //***************************************************************************************************************
777
778 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
779         try {
780                 string rawSequence = seq.getUnaligned();
781                 int seqLength = rawSequence.length();
782                 string name;
783                 
784                 qFile >> name;
785                 if (name.length() != 0) {  if(name.substr(1) != seq.getName())  {       m->mothurOut("sequence name mismatch btwn fasta and qual file"); m->mothurOutEndLine(); }  } 
786                 while (!qFile.eof())    {       char c = qFile.get(); if (c == 10 || c == 13){  break;  }       }
787                 
788                 int score;
789                 int end = seqLength;
790                 
791                 for(int i=0;i<seqLength;i++){
792                         qFile >> score;
793                         
794                         if(score <= qThreshold){
795                                 end = i;
796                                 break;
797                         }
798                 }
799                 for(int i=end+1;i<seqLength;i++){
800                         qFile >> score;
801                 }
802                 
803                 seq.setUnaligned(rawSequence.substr(0,end));
804                 
805                 return 1;
806         }
807         catch(exception& e) {
808                 m->errorOut(e, "TrimSeqsCommand", "stripQualThreshold");
809                 exit(1);
810         }
811 }
812
813 //***************************************************************************************************************
814
815 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
816         try {
817                 string rawSequence = seq.getUnaligned();
818                 int seqLength = seq.getNumBases();
819                 bool success = 0;       //guilty until proven innocent
820                 string name;
821                 
822                 qFile >> name;
823                 if (name[0] == '>') {  if(name.substr(1) != seq.getName())      {       m->mothurOut("sequence name mismatch btwn fasta: " + seq.getName() + " and qual file: " + name); m->mothurOutEndLine(); }  }
824                 
825                 while (!qFile.eof())    {       char c = qFile.get(); if (c == 10 || c == 13){  break;  }       }
826                 
827                 float score;    
828                 float average = 0;
829                 
830                 for(int i=0;i<seqLength;i++){
831                         qFile >> score;
832                         average += score;
833                 }
834                 average /= seqLength;
835
836                 if(average >= qAverage) {       success = 1;    }
837                 else                                    {       success = 0;    }
838                 
839                 return success;
840         }
841         catch(exception& e) {
842                 m->errorOut(e, "TrimSeqsCommand", "cullQualAverage");
843                 exit(1);
844         }
845 }
846
847 //***************************************************************************************************************