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