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