]> git.donarmstrong.com Git - mothur.git/blob - trimseqscommand.cpp
b185da0905321c7980f2237a9d938bd260d9397d
[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", "qthreshold", "qaverage", "allfiles", "qtrim"};
25                         
26                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string,string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                         
33                         //check to make sure all parameters are valid for command
34                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
36                         }
37                         
38                         //check for required parameters
39                         fastaFile = validParameter.validFile(parameters, "fasta", true);
40                         if (fastaFile == "not found") { mothurOut("fasta is a required parameter for the screen.seqs command."); mothurOutEndLine(); abort = true; }
41                         else if (fastaFile == "not open") { abort = true; }     
42                 
43                 
44                         //check for optional parameter and set defaults
45                         // ...at some point should added some additional type checking...
46                         string temp;
47                         temp = validParameter.validFile(parameters, "flip", false);
48                         if (temp == "not found"){       flip = 0;       }
49                         else if(isTrue(temp))   {       flip = 1;       }
50                 
51                         temp = validParameter.validFile(parameters, "oligos", true);
52                         if (temp == "not found"){       oligoFile = "";         }
53                         else if(temp == "not open"){    abort = true;   } 
54                         else                                    {       oligoFile = temp;               }
55                         
56                         temp = validParameter.validFile(parameters, "maxambig", false);         if (temp == "not found") { temp = "-1"; }
57                         convert(temp, maxAmbig);  
58
59                         temp = validParameter.validFile(parameters, "maxhomop", false);         if (temp == "not found") { temp = "0"; }
60                         convert(temp, maxHomoP);  
61
62                         temp = validParameter.validFile(parameters, "minlength", false);        if (temp == "not found") { temp = "0"; }
63                         convert(temp, minLength); 
64                         
65                         temp = validParameter.validFile(parameters, "maxlength", false);        if (temp == "not found") { temp = "0"; }
66                         convert(temp, maxLength);
67                         
68                         temp = validParameter.validFile(parameters, "qfile", true);     
69                         if (temp == "not found")        {       qFileName = "";         }
70                         else if(temp == "not open")     {       abort = 0;              }
71                         else                                            {       qFileName = temp;       }
72                         
73                         temp = validParameter.validFile(parameters, "qthreshold", false);       if (temp == "not found") { temp = "0"; }
74                         convert(temp, qThreshold);
75                         
76                         temp = validParameter.validFile(parameters, "qtrim", false);    if (temp == "not found") { temp = "F"; }
77                         qtrim = isTrue(temp);
78
79                         temp = validParameter.validFile(parameters, "qaverage", false);         if (temp == "not found") { temp = "0"; }
80                         convert(temp, qAverage);
81                         
82                         temp = validParameter.validFile(parameters, "allfiles", false);         if (temp == "not found") { temp = "F"; }
83                         allFiles = isTrue(temp);
84                         
85                         if(allFiles && oligoFile == ""){
86                                 mothurOut("You selected allfiles, but didn't enter an oligos file.  Ignoring the allfiles request."); mothurOutEndLine();
87                         }
88                         if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
89                                 mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); mothurOutEndLine();
90                                 qAverage=0;
91                                 qThreshold=0;
92                         }
93                         if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){               
94                                 mothurOut("You didn't set any options... quiting command."); mothurOutEndLine();
95                                 abort = true;
96                         }
97                 }
98
99         }
100         catch(exception& e) {
101                 errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
102                 exit(1);
103         }
104 }
105 //**********************************************************************************************************************
106
107 void TrimSeqsCommand::help(){
108         try {
109                 mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
110                 mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, qtrim and allfiles.\n");
111                 mothurOut("The fasta parameter is required.\n");
112                 mothurOut("The flip parameter .... The default is 0.\n");
113                 mothurOut("The oligos parameter .... The default is "".\n");
114                 mothurOut("The maxambig parameter .... The default is -1.\n");
115                 mothurOut("The maxhomop parameter .... The default is 0.\n");
116                 mothurOut("The minlength parameter .... The default is 0.\n");
117                 mothurOut("The maxlength parameter .... The default is 0.\n");
118                 mothurOut("The qfile parameter .....\n");
119                 mothurOut("The qthreshold parameter .... The default is 0.\n");
120                 mothurOut("The qaverage parameter .... The default is 0.\n");
121                 mothurOut("The allfiles parameter .... The default is F.\n");
122                 mothurOut("The qtrim parameter .... The default is F.\n");
123                 mothurOut("The trim.seqs command should be in the following format: \n");
124                 mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig,  \n");
125                 mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength)  \n");       
126                 mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
127                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
128                 mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.seqs .\n\n");
129
130         }
131         catch(exception& e) {
132                 errorOut(e, "TrimSeqsCommand", "help");
133                 exit(1);
134         }
135 }
136
137
138 //***************************************************************************************************************
139
140 TrimSeqsCommand::~TrimSeqsCommand(){    /*      do nothing      */      }
141
142 //***************************************************************************************************************
143
144 int TrimSeqsCommand::execute(){
145         try{
146         
147                 if (abort == true) { return 0; }
148
149                 ifstream inFASTA;
150                 openInputFile(fastaFile, inFASTA);
151                 
152                 ofstream outFASTA;
153                 string trimSeqFile = getRootName(fastaFile) + "trim.fasta";
154                 openOutputFile(trimSeqFile, outFASTA);
155                 
156                 ofstream outGroups;
157                 vector<ofstream*> fastaFileNames;
158                 if(oligoFile != ""){
159                         string groupFile = getRootName(fastaFile) + "groups"; 
160                         openOutputFile(groupFile, outGroups);
161                         getOligos(fastaFileNames);
162                 }
163                 
164                 ofstream scrapFASTA;
165                 string scrapSeqFile = getRootName(fastaFile) + "scrap.fasta";
166                 openOutputFile(scrapSeqFile, scrapFASTA);
167                 
168                 ifstream qFile;
169                 if(qFileName != "")     {       openInputFile(qFileName, qFile);        }
170                 
171                 bool success;
172                         
173                 while(!inFASTA.eof()){
174                         Sequence currSeq(inFASTA);
175                         string origSeq = currSeq.getUnaligned();
176                         if (origSeq != "") {
177                                 int group;
178                                 string trashCode = "";
179                                 
180                                 if(qFileName != ""){
181                                         if(qThreshold != 0)             {       success = stripQualThreshold(currSeq, qFile);   }
182                                         else if(qAverage != 0)  {       success = cullQualAverage(currSeq, qFile);              }
183                                         if ((!qtrim) && (origSeq.length() != currSeq.getUnaligned().length())) { 
184                                                 success = 0; //if you don't want to trim and the sequence does not meet quality requirements, move to scrap
185                                         }
186                                         if(!success)                    {       trashCode += 'q';                                                               }
187                                 }
188                                 
189                                 if(barcodes.size() != 0){
190                                         success = stripBarcode(currSeq, group);
191                                         if(!success){   trashCode += 'b';       }
192                                 }
193                                 
194                                 if(numFPrimers != 0){
195                                         success = stripForward(currSeq);
196                                         if(!success){   trashCode += 'f';       }
197                                 }
198                                 
199                                 if(numRPrimers != 0){
200                                         success = stripReverse(currSeq);
201                                         if(!success){   trashCode += 'r';       }
202                                 }
203                                 if(minLength > 0 || maxLength > 0){
204                                         success = cullLength(currSeq);
205                                         if(!success){   trashCode += 'l'; }
206                                 }
207                                 if(maxHomoP > 0){
208                                         success = cullHomoP(currSeq);
209                                         if(!success){   trashCode += 'h';       }
210                                 }
211                                 if(maxAmbig != -1){
212                                         success = cullAmbigs(currSeq);
213                                         if(!success){   trashCode += 'n';       }
214                                 }
215                                 
216                                 if(flip){       currSeq.reverseComplement();    }               // should go last                       
217                                 
218                                 if(trashCode.length() == 0){
219                                         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.
220                                         currSeq.printSequence(outFASTA);
221                                         if(barcodes.size() != 0){
222                                                 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
223                                                 
224                                                 if(allFiles){
225                                                         currSeq.printSequence(*fastaFileNames[group]);                                  
226                                                 }
227                                         }
228                                 }
229                                 else{
230                                         currSeq.setName(currSeq.getName() + '|' + trashCode);
231                                         currSeq.setUnaligned(origSeq);
232                                         currSeq.printSequence(scrapFASTA);
233                                 }
234                         }
235                         gobble(inFASTA);
236                 }
237                 inFASTA.close();
238                 outFASTA.close();
239                 scrapFASTA.close();
240                 outGroups.close();
241                 if(qFileName != "")     {       qFile.close();  }
242                 
243                 for(int i=0;i<fastaFileNames.size();i++){
244                         fastaFileNames[i]->close();
245                         delete fastaFileNames[i];
246                 }               
247                 
248                 for(int i=0;i<fastaFileNames.size();i++){
249                         string seqName;
250                         openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
251                         ofstream outGroups;
252                         openOutputFile(getRootName(fastaFile) + groupVector[i] + ".groups", outGroups);
253                         
254                         while(!inFASTA.eof()){
255                                 if(inFASTA.get() == '>'){
256                                         inFASTA >> seqName;
257                                         outGroups << seqName << '\t' << groupVector[i] << endl;
258                                 }
259                                 while (!inFASTA.eof())  {       char c = inFASTA.get(); if (c == 10 || c == 13){        break;  }       }
260                         }
261                         outGroups.close();
262                         inFASTA.close();
263                 }
264                 
265                 
266                 return 0;               
267         }
268         catch(exception& e) {
269                 errorOut(e, "TrimSeqsCommand", "execute");
270                 exit(1);
271         }
272 }
273
274 //***************************************************************************************************************
275
276 void TrimSeqsCommand::getOligos(vector<ofstream*>& outFASTAVec){
277         try {
278                 ifstream inOligos;
279                 openInputFile(oligoFile, inOligos);
280                 
281                 ofstream test;
282                 
283                 string type, oligo, group;
284                 int index=0;
285                 
286                 while(!inOligos.eof()){
287                         inOligos >> type;
288                         
289                         if(type[0] == '#'){
290                                 while (!inOligos.eof()) {       char c = inOligos.get(); if (c == 10 || c == 13){       break;  }       } // get rest of line if there's any crap there
291                         }
292                         else{
293                                 inOligos >> oligo;
294                                 
295                                 for(int i=0;i<oligo.length();i++){
296                                         oligo[i] = toupper(oligo[i]);
297                                         if(oligo[i] == 'U')     {       oligo[i] = 'T'; }
298                                 }
299                                 
300                                 if(type == "forward"){
301                                         forPrimer.push_back(oligo);
302                                 }
303                                 else if(type == "reverse"){
304                                         Sequence oligoRC("reverse", oligo);
305                                         oligoRC.reverseComplement();
306                                         revPrimer.push_back(oligoRC.getUnaligned());
307                                 }
308                                 else if(type == "barcode"){
309                                         inOligos >> group;
310                                         barcodes[oligo]=index++;
311                                         groupVector.push_back(group);
312                                         
313                                         if(allFiles){
314                                                 outFASTAVec.push_back(new ofstream((getRootName(fastaFile) + group + ".fasta").c_str(), ios::ate));
315                                         }
316                                 }
317                         }
318                 }
319                 
320                 inOligos.close();
321                 
322                 numFPrimers = forPrimer.size();
323                 numRPrimers = revPrimer.size();
324                 
325         }
326         catch(exception& e) {
327                 errorOut(e, "TrimSeqsCommand", "getOligos");
328                 exit(1);
329         }
330
331 }
332
333 //***************************************************************************************************************
334
335 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
336         try {
337                 string rawSequence = seq.getUnaligned();
338                 bool success = 0;       //guilty until proven innocent
339                 
340                 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
341                         string oligo = it->first;
342                         if(rawSequence.length() < oligo.length()){      //let's just assume that the barcodes are the same length
343                                 success = 0;
344                                 break;
345                         }
346                         
347                         if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
348                                 group = it->second;
349                                 seq.setUnaligned(rawSequence.substr(oligo.length()));
350                                 success = 1;
351                                 break;
352                         }
353                 }
354                 return success;
355                 
356         }
357         catch(exception& e) {
358                 errorOut(e, "TrimSeqsCommand", "stripBarcode");
359                 exit(1);
360         }
361
362 }
363
364 //***************************************************************************************************************
365
366 bool TrimSeqsCommand::stripForward(Sequence& seq){
367         try {
368                 string rawSequence = seq.getUnaligned();
369                 bool success = 0;       //guilty until proven innocent
370                 
371                 for(int i=0;i<numFPrimers;i++){
372                         string oligo = forPrimer[i];
373                         
374                         if(rawSequence.length() < oligo.length()){
375                                 success = 0;
376                                 break;
377                         }
378                         
379                         if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
380                                 seq.setUnaligned(rawSequence.substr(oligo.length()));
381                                 success = 1;
382                                 break;
383                         }
384                 }
385                 
386                 return success;
387                 
388         }
389         catch(exception& e) {
390                 errorOut(e, "TrimSeqsCommand", "stripForward");
391                 exit(1);
392         }
393 }
394
395 //***************************************************************************************************************
396
397 bool TrimSeqsCommand::stripReverse(Sequence& seq){
398         try {
399                 string rawSequence = seq.getUnaligned();
400                 bool success = 0;       //guilty until proven innocent
401                 
402                 for(int i=0;i<numRPrimers;i++){
403                         string oligo = revPrimer[i];
404                         
405                         if(rawSequence.length() < oligo.length()){
406                                 success = 0;
407                                 break;
408                         }
409                         
410                         if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
411                                 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
412                                 success = 1;
413                                 break;
414                         }
415                 }       
416                 return success;
417                 
418         }
419         catch(exception& e) {
420                 errorOut(e, "TrimSeqsCommand", "stripReverse");
421                 exit(1);
422         }
423 }
424
425 //***************************************************************************************************************
426
427 bool TrimSeqsCommand::cullLength(Sequence& seq){
428         try {
429         
430                 int length = seq.getNumBases();
431                 bool success = 0;       //guilty until proven innocent
432                 
433                 if(length >= minLength && maxLength == 0)                       {       success = 1;    }
434                 else if(length >= minLength && length <= maxLength)     {       success = 1;    }
435                 else                                                                                            {       success = 0;    }
436                 
437                 return success;
438         
439         }
440         catch(exception& e) {
441                 errorOut(e, "TrimSeqsCommand", "cullLength");
442                 exit(1);
443         }
444         
445 }
446
447 //***************************************************************************************************************
448
449 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
450         try {
451                 int longHomoP = seq.getLongHomoPolymer();
452                 bool success = 0;       //guilty until proven innocent
453                 
454                 if(longHomoP <= maxHomoP){      success = 1;    }
455                 else                                    {       success = 0;    }
456                 
457                 return success;
458         }
459         catch(exception& e) {
460                 errorOut(e, "TrimSeqsCommand", "cullHomoP");
461                 exit(1);
462         }
463         
464 }
465
466 //***************************************************************************************************************
467
468 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
469         try {
470                 int numNs = seq.getAmbigBases();
471                 bool success = 0;       //guilty until proven innocent
472                 
473                 if(numNs <= maxAmbig)   {       success = 1;    }
474                 else                                    {       success = 0;    }
475                 
476                 return success;
477         }
478         catch(exception& e) {
479                 errorOut(e, "TrimSeqsCommand", "cullAmbigs");
480                 exit(1);
481         }
482         
483 }
484
485 //***************************************************************************************************************
486
487 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
488         try {
489                 bool success = 1;
490                 int length = oligo.length();
491                 
492                 for(int i=0;i<length;i++){
493                         
494                         if(oligo[i] != seq[i]){
495                                 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C')    {       success = 0;    }
496                                 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N'))                                {       success = 0;    }
497                                 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G'))                                    {       success = 0;    }
498                                 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T'))                                    {       success = 0;    }
499                                 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A'))                                    {       success = 0;    }
500                                 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G'))                                    {       success = 0;    }
501                                 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A'))                                    {       success = 0;    }
502                                 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G'))                                    {       success = 0;    }
503                                 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G'))   {       success = 0;    }
504                                 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G'))   {       success = 0;    }
505                                 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C'))   {       success = 0;    }
506                                 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G'))   {       success = 0;    }                       
507                                 
508                                 if(success == 0)        {       break;  }
509                         }
510                         else{
511                                 success = 1;
512                         }
513                 }
514                 
515                 return success;
516         }
517         catch(exception& e) {
518                 errorOut(e, "TrimSeqsCommand", "compareDNASeq");
519                 exit(1);
520         }
521
522 }
523
524 //***************************************************************************************************************
525
526 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
527         try {
528                 string rawSequence = seq.getUnaligned();
529                 int seqLength = rawSequence.length();
530                 string name;
531                 
532                 qFile >> name;
533                 if (name.length() != 0) {  if(name.substr(1) != seq.getName())  {       mothurOut("sequence name mismatch btwn fasta and qual file"); mothurOutEndLine();       }  } 
534                 while (!qFile.eof())    {       char c = qFile.get(); if (c == 10 || c == 13){  break;  }       }
535                 
536                 int score;
537                 int end = seqLength;
538                 
539                 for(int i=0;i<seqLength;i++){
540                         qFile >> score;
541                         
542                         if(score <= qThreshold){
543                                 end = i;
544                                 break;
545                         }
546                 }
547                 for(int i=end+1;i<seqLength;i++){
548                         qFile >> score;
549                 }
550                 
551                 seq.setUnaligned(rawSequence.substr(0,end));
552                 
553                 return 1;
554         }
555         catch(exception& e) {
556                 errorOut(e, "TrimSeqsCommand", "stripQualThreshold");
557                 exit(1);
558         }
559 }
560
561 //***************************************************************************************************************
562
563 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
564         try {
565                 string rawSequence = seq.getUnaligned();
566                 int seqLength = seq.getNumBases();
567                 bool success = 0;       //guilty until proven innocent
568                 string name;
569                 
570                 qFile >> name;
571                 if (name[0] == '>') {  if(name.substr(1) != seq.getName())      {       mothurOut("sequence name mismatch btwn fasta: " + seq.getName() + " and qual file: " + name); mothurOutEndLine();       } }
572                 
573                 while (!qFile.eof())    {       char c = qFile.get(); if (c == 10 || c == 13){  break;  }       }
574                 
575                 float score;    
576                 float average = 0;
577                 
578                 for(int i=0;i<seqLength;i++){
579                         qFile >> score;
580                         average += score;
581                 }
582                 average /= seqLength;
583
584                 if(average >= qAverage) {       success = 1;    }
585                 else                                    {       success = 0;    }
586                 
587                 return success;
588         }
589         catch(exception& e) {
590                 errorOut(e, "TrimSeqsCommand", "cullQualAverage");
591                 exit(1);
592         }
593 }
594
595 //***************************************************************************************************************