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