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