5 * Created by Pat Schloss on 6/6/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "trimseqscommand.h"
11 #include "needlemanoverlap.hpp"
14 //***************************************************************************************************************
16 TrimSeqsCommand::TrimSeqsCommand(string option) {
21 //allow user to run help
22 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string AlignArray[] = {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile",
27 "qthreshold", "qaverage", "allfiles", "qtrim","diffs", "processors", "outputdir","inputdir"};
29 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
31 OptionParser parser(option);
32 map<string,string> parameters = parser.getParameters();
34 ValidParameters validParameter;
35 map<string,string>::iterator it;
37 //check to make sure all parameters are valid for command
38 for (it = parameters.begin(); it != parameters.end(); it++) {
39 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
42 //if the user changes the input directory command factory will send this info to us in the output parameter
43 string inputDir = validParameter.validFile(parameters, "inputdir", false);
44 if (inputDir == "not found"){ inputDir = ""; }
47 it = parameters.find("fasta");
48 //user has given a template file
49 if(it != parameters.end()){
50 path = hasPath(it->second);
51 //if the user has not given a path then, add inputdir. else leave path alone.
52 if (path == "") { parameters["fasta"] = inputDir + it->second; }
55 it = parameters.find("oligos");
56 //user has given a template file
57 if(it != parameters.end()){
58 path = hasPath(it->second);
59 //if the user has not given a path then, add inputdir. else leave path alone.
60 if (path == "") { parameters["oligos"] = inputDir + it->second; }
63 it = parameters.find("qfile");
64 //user has given a template file
65 if(it != parameters.end()){
66 path = hasPath(it->second);
67 //if the user has not given a path then, add inputdir. else leave path alone.
68 if (path == "") { parameters["qfile"] = inputDir + it->second; }
73 //check for required parameters
74 fastaFile = validParameter.validFile(parameters, "fasta", true);
75 if (fastaFile == "not found") { m->mothurOut("fasta is a required parameter for the screen.seqs command."); m->mothurOutEndLine(); abort = true; }
76 else if (fastaFile == "not open") { abort = true; }
78 //if the user changes the output directory command factory will send this info to us in the output parameter
79 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
81 outputDir += hasPath(fastaFile); //if user entered a file with a path then preserve it
84 //check for optional parameter and set defaults
85 // ...at some point should added some additional type checking...
87 temp = validParameter.validFile(parameters, "flip", false);
88 if (temp == "not found"){ flip = 0; }
89 else if(isTrue(temp)) { flip = 1; }
91 temp = validParameter.validFile(parameters, "oligos", true);
92 if (temp == "not found"){ oligoFile = ""; }
93 else if(temp == "not open"){ abort = true; }
94 else { oligoFile = temp; }
96 temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; }
97 convert(temp, maxAmbig);
99 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "0"; }
100 convert(temp, maxHomoP);
102 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "0"; }
103 convert(temp, minLength);
105 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "0"; }
106 convert(temp, maxLength);
108 temp = validParameter.validFile(parameters, "diffs", false); if (temp == "not found") { temp = "0"; }
109 convert(temp, diffs);
111 temp = validParameter.validFile(parameters, "qfile", true);
112 if (temp == "not found") { qFileName = ""; }
113 else if(temp == "not open") { abort = true; }
114 else { qFileName = temp; }
116 temp = validParameter.validFile(parameters, "qthreshold", false); if (temp == "not found") { temp = "0"; }
117 convert(temp, qThreshold);
119 temp = validParameter.validFile(parameters, "qtrim", false); if (temp == "not found") { temp = "F"; }
120 qtrim = isTrue(temp);
122 temp = validParameter.validFile(parameters, "qaverage", false); if (temp == "not found") { temp = "0"; }
123 convert(temp, qAverage);
125 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
126 allFiles = isTrue(temp);
128 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
129 convert(temp, processors);
131 if(allFiles && oligoFile == ""){
132 m->mothurOut("You selected allfiles, but didn't enter an oligos file. Ignoring the allfiles request."); m->mothurOutEndLine();
134 if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
135 m->mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); m->mothurOutEndLine();
139 if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){
140 m->mothurOut("You didn't set any options... quiting command."); m->mothurOutEndLine();
146 catch(exception& e) {
147 m->errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
151 //**********************************************************************************************************************
153 void TrimSeqsCommand::help(){
155 m->mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
156 m->mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, diffs, qtrim and allfiles.\n");
157 m->mothurOut("The fasta parameter is required.\n");
158 m->mothurOut("The flip parameter .... The default is 0.\n");
159 m->mothurOut("The oligos parameter .... The default is "".\n");
160 m->mothurOut("The maxambig parameter .... The default is -1.\n");
161 m->mothurOut("The maxhomop parameter .... The default is 0.\n");
162 m->mothurOut("The minlength parameter .... The default is 0.\n");
163 m->mothurOut("The maxlength parameter .... The default is 0.\n");
164 m->mothurOut("The diffs parameter .... The default is 0.\n");
165 m->mothurOut("The qfile parameter .....\n");
166 m->mothurOut("The qthreshold parameter .... The default is 0.\n");
167 m->mothurOut("The qaverage parameter .... The default is 0.\n");
168 m->mothurOut("The allfiles parameter .... The default is F.\n");
169 m->mothurOut("The qtrim parameter .... The default is F.\n");
170 m->mothurOut("The trim.seqs command should be in the following format: \n");
171 m->mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig, \n");
172 m->mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n");
173 m->mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
174 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
175 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.seqs .\n\n");
178 catch(exception& e) {
179 m->errorOut(e, "TrimSeqsCommand", "help");
185 //***************************************************************************************************************
187 TrimSeqsCommand::~TrimSeqsCommand(){ /* do nothing */ }
189 //***************************************************************************************************************
191 int TrimSeqsCommand::execute(){
194 if (abort == true) { return 0; }
196 numFPrimers = 0; //this needs to be initialized
199 string trimSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "trim.fasta";
200 outputNames.push_back(trimSeqFile);
201 string scrapSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "scrap.fasta";
202 outputNames.push_back(scrapSeqFile);
203 string groupFile = outputDir + getRootName(getSimpleName(fastaFile)) + "groups";
205 vector<string> fastaFileNames;
207 outputNames.push_back(groupFile);
208 getOligos(fastaFileNames);
211 if(qFileName != "") { setLines(qFileName, qLines); }
214 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
217 openInputFile(fastaFile, inFASTA);
218 int numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
221 lines.push_back(new linePair(0, numSeqs));
223 driverCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
225 for (int j = 0; j < fastaFileNames.size(); j++) {
226 rename((fastaFileNames[j] + toString(getpid()) + ".temp").c_str(), fastaFileNames[j].c_str());
230 setLines(fastaFile, lines);
231 if(qFileName == "") { qLines = lines; }
233 createProcessesCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames);
235 rename((trimSeqFile + toString(processIDS[0]) + ".temp").c_str(), trimSeqFile.c_str());
236 rename((scrapSeqFile + toString(processIDS[0]) + ".temp").c_str(), scrapSeqFile.c_str());
237 rename((groupFile + toString(processIDS[0]) + ".temp").c_str(), groupFile.c_str());
238 for (int j = 0; j < fastaFileNames.size(); j++) {
239 rename((fastaFileNames[j] + toString(processIDS[0]) + ".temp").c_str(), fastaFileNames[j].c_str());
242 for(int i=1;i<processors;i++){
243 appendFiles((trimSeqFile + toString(processIDS[i]) + ".temp"), trimSeqFile);
244 remove((trimSeqFile + toString(processIDS[i]) + ".temp").c_str());
245 appendFiles((scrapSeqFile + toString(processIDS[i]) + ".temp"), scrapSeqFile);
246 remove((scrapSeqFile + toString(processIDS[i]) + ".temp").c_str());
247 appendFiles((groupFile + toString(processIDS[i]) + ".temp"), groupFile);
248 remove((groupFile + toString(processIDS[i]) + ".temp").c_str());
249 for (int j = 0; j < fastaFileNames.size(); j++) {
250 appendFiles((fastaFileNames[j] + toString(processIDS[i]) + ".temp"), fastaFileNames[j]);
251 remove((fastaFileNames[j] + toString(processIDS[i]) + ".temp").c_str());
256 if (m->control_pressed) { return 0; }
259 openInputFile(fastafileNames[s], inFASTA);
260 numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
263 lines.push_back(new linePair(0, numSeqs));
265 driverCreateSummary(fastafile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
267 if (m->control_pressed) { return 0; }
271 for(int i=0;i<fastaFileNames.size();i++){
274 openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
276 openOutputFile(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups", outGroups);
277 outputNames.push_back(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups");
279 while(!inFASTA.eof()){
280 if(inFASTA.get() == '>'){
282 outGroups << seqName << '\t' << groupVector[i] << endl;
284 while (!inFASTA.eof()) { char c = inFASTA.get(); if (c == 10 || c == 13){ break; } }
290 if (m->control_pressed) {
291 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
295 m->mothurOutEndLine();
296 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
297 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
298 m->mothurOutEndLine();
303 catch(exception& e) {
304 m->errorOut(e, "TrimSeqsCommand", "execute");
309 /**************************************************************************************/
310 int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames, linePair* line, linePair* qline) {
314 int able = openOutputFile(trimFile, outFASTA);
317 openOutputFile(scrapFile, scrapFASTA);
320 vector<ofstream*> fastaFileNames;
321 if (oligoFile != "") {
322 openOutputFile(groupFile, outGroups);
323 for (int i = 0; i < fastaNames.size(); i++) {
324 fastaFileNames.push_back(new ofstream((fastaNames[i] + toString(getpid()) + ".temp").c_str(), ios::ate));
329 openInputFile(filename, inFASTA);
332 if(qFileName != "") { openInputFile(qFileName, qFile); }
334 qFile.seekg(qline->start);
335 inFASTA.seekg(line->start);
337 for(int i=0;i<line->num;i++){
339 if (m->control_pressed) {
343 if (oligoFile != "") { outGroups.close(); }
344 if(qFileName != "") { qFile.close(); }
345 for(int i=0;i<fastaFileNames.size();i++){
346 fastaFileNames[i]->close();
347 delete fastaFileNames[i];
349 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
355 Sequence currSeq(inFASTA);
357 string origSeq = currSeq.getUnaligned();
360 string trashCode = "";
363 if(qThreshold != 0) { success = stripQualThreshold(currSeq, qFile); }
364 else if(qAverage != 0) { success = cullQualAverage(currSeq, qFile); }
365 if ((!qtrim) && (origSeq.length() != currSeq.getUnaligned().length())) {
366 success = 0; //if you don't want to trim and the sequence does not meet quality requirements, move to scrap
368 if(!success) { trashCode += 'q'; }
371 if(barcodes.size() != 0){
372 success = stripBarcode(currSeq, group);
373 if(!success){ trashCode += 'b'; }
376 if(numFPrimers != 0){
377 success = stripForward(currSeq);
378 if(!success){ trashCode += 'f'; }
381 if(numRPrimers != 0){
382 success = stripReverse(currSeq);
383 if(!success){ trashCode += 'r'; }
386 if(minLength > 0 || maxLength > 0){
387 success = cullLength(currSeq);
388 if(!success){ trashCode += 'l'; }
391 success = cullHomoP(currSeq);
392 if(!success){ trashCode += 'h'; }
395 success = cullAmbigs(currSeq);
396 if(!success){ trashCode += 'n'; }
399 if(flip){ currSeq.reverseComplement(); } // should go last
401 if(trashCode.length() == 0){
402 currSeq.setAligned(currSeq.getUnaligned()); //this is because of a modification we made to the sequence class to fix a bug. all seqs have an aligned version, which is the version that gets printed.
403 currSeq.printSequence(outFASTA);
404 if(barcodes.size() != 0){
405 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
408 currSeq.printSequence(*fastaFileNames[group]);
413 currSeq.setName(currSeq.getName() + '|' + trashCode);
414 currSeq.setUnaligned(origSeq);
415 currSeq.printSequence(scrapFASTA);
424 if (oligoFile != "") { outGroups.close(); }
425 if(qFileName != "") { qFile.close(); }
427 for(int i=0;i<fastaFileNames.size();i++){
428 fastaFileNames[i]->close();
429 delete fastaFileNames[i];
434 catch(exception& e) {
435 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
439 /**************************************************************************************************/
440 int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames) {
442 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
447 //loop through and create all the processes you want
448 while (process != processors) {
452 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
455 driverCreateTrim(filename, qFileName, (trimFile + toString(getpid()) + ".temp"), (scrapFile + toString(getpid()) + ".temp"), (groupFile + toString(getpid()) + ".temp"), fastaNames, lines[process], qLines[process]);
457 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
460 //force parent to wait until all the processes are done
461 for (int i=0;i<processors;i++) {
462 int temp = processIDS[i];
469 catch(exception& e) {
470 m->errorOut(e, "TrimSeqsCommand", "createProcessesCreateTrim");
474 /**************************************************************************************************/
476 int TrimSeqsCommand::setLines(string filename, vector<linePair*>& lines) {
481 vector<long int> positions;
484 openInputFile(filename, inFASTA);
487 while(!inFASTA.eof()){
488 input = getline(inFASTA);
490 if (input.length() != 0) {
491 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
496 int numFastaSeqs = positions.size();
501 //get num bytes in file
502 pFile = fopen (filename.c_str(),"rb");
503 if (pFile==NULL) perror ("Error opening file");
505 fseek (pFile, 0, SEEK_END);
510 int numSeqsPerProcessor = numFastaSeqs / processors;
512 for (int i = 0; i < processors; i++) {
514 long int startPos = positions[ i * numSeqsPerProcessor ];
515 if(i == processors - 1){
516 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
518 long int myEnd = positions[ (i+1) * numSeqsPerProcessor ];
520 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
525 catch(exception& e) {
526 m->errorOut(e, "TrimSeqsCommand", "setLines");
530 //***************************************************************************************************************
532 void TrimSeqsCommand::getOligos(vector<string>& outFASTAVec){ //vector<ofstream*>& outFASTAVec
535 openInputFile(oligoFile, inOligos);
539 string type, oligo, group;
542 while(!inOligos.eof()){
546 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
551 for(int i=0;i<oligo.length();i++){
552 oligo[i] = toupper(oligo[i]);
553 if(oligo[i] == 'U') { oligo[i] = 'T'; }
556 if(type == "forward"){
557 forPrimer.push_back(oligo);
559 else if(type == "reverse"){
560 Sequence oligoRC("reverse", oligo);
561 oligoRC.reverseComplement();
562 revPrimer.push_back(oligoRC.getUnaligned());
564 else if(type == "barcode"){
566 barcodes[oligo]=index++;
567 groupVector.push_back(group);
570 //outFASTAVec.push_back(new ofstream((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta").c_str(), ios::ate));
571 outputNames.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta"));
572 outFASTAVec.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta"));
580 numFPrimers = forPrimer.size();
581 numRPrimers = revPrimer.size();
584 catch(exception& e) {
585 m->errorOut(e, "TrimSeqsCommand", "getOligos");
589 //***************************************************************************************************************
591 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
593 string rawSequence = seq.getUnaligned();
594 bool success = 0; //guilty until proven innocent
596 //can you find the barcode
597 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
598 string oligo = it->first;
599 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
604 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
606 seq.setUnaligned(rawSequence.substr(oligo.length()));
612 //if you found the barcode or if you don't want to allow for diffs
613 if ((diffs == 0) || (success == 1)) { return success; }
615 else { //try aligning and see if you can find it
616 //can you find the barcode
617 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
618 string oligo = it->first;
619 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
624 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
625 Alignment* alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (oligo.length()+diffs+1));
626 Sequence* templateSeq = new Sequence("temp", rawSequence.substr(0,(oligo.length()+diffs)));
627 Sequence* candidateSeq = new Sequence("temp2", oligo);
628 Nast nast(alignment, candidateSeq, templateSeq);
630 oligo = candidateSeq->getAligned();
631 cout << "barcode = " << oligo << " raw = " << rawSequence.substr(0,(oligo.length())) << endl;
636 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
638 seq.setUnaligned(rawSequence.substr(0,oligo.length()));
647 catch(exception& e) {
648 m->errorOut(e, "TrimSeqsCommand", "stripBarcode");
654 //***************************************************************************************************************
656 bool TrimSeqsCommand::stripForward(Sequence& seq){
658 string rawSequence = seq.getUnaligned();
659 bool success = 0; //guilty until proven innocent
661 for(int i=0;i<numFPrimers;i++){
662 string oligo = forPrimer[i];
664 if(rawSequence.length() < oligo.length()){
669 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
670 seq.setUnaligned(rawSequence.substr(oligo.length()));
676 //if you found the primer or if you don't want to allow for diffs
677 if ((diffs == 0) || (success == 1)) { return success; }
679 else { //try aligning and see if you can find it
680 //can you find the primer
681 for(int i=0;i<numFPrimers;i++){
682 string oligo = forPrimer[i];
683 if(rawSequence.length() < oligo.length()){
688 //use needleman to align first primer.length()+numdiffs of sequence to each primer
689 Alignment* alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (oligo.length()+diffs+1));
690 Sequence* templateSeq = new Sequence("temp", rawSequence.substr(0,(oligo.length()+diffs)));
691 Sequence* candidateSeq = new Sequence("temp2", oligo);
692 Nast nast(alignment, candidateSeq, templateSeq);
694 oligo = candidateSeq->getAligned();
700 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
701 seq.setUnaligned(rawSequence.substr(0,oligo.length()));
711 catch(exception& e) {
712 m->errorOut(e, "TrimSeqsCommand", "stripForward");
717 //***************************************************************************************************************
719 bool TrimSeqsCommand::stripReverse(Sequence& seq){
721 string rawSequence = seq.getUnaligned();
722 bool success = 0; //guilty until proven innocent
724 for(int i=0;i<numRPrimers;i++){
725 string oligo = revPrimer[i];
727 if(rawSequence.length() < oligo.length()){
732 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
733 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
741 catch(exception& e) {
742 m->errorOut(e, "TrimSeqsCommand", "stripReverse");
747 //***************************************************************************************************************
749 bool TrimSeqsCommand::cullLength(Sequence& seq){
752 int length = seq.getNumBases();
753 bool success = 0; //guilty until proven innocent
755 if(length >= minLength && maxLength == 0) { success = 1; }
756 else if(length >= minLength && length <= maxLength) { success = 1; }
757 else { success = 0; }
762 catch(exception& e) {
763 m->errorOut(e, "TrimSeqsCommand", "cullLength");
769 //***************************************************************************************************************
771 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
773 int longHomoP = seq.getLongHomoPolymer();
774 bool success = 0; //guilty until proven innocent
776 if(longHomoP <= maxHomoP){ success = 1; }
777 else { success = 0; }
781 catch(exception& e) {
782 m->errorOut(e, "TrimSeqsCommand", "cullHomoP");
788 //***************************************************************************************************************
790 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
792 int numNs = seq.getAmbigBases();
793 bool success = 0; //guilty until proven innocent
795 if(numNs <= maxAmbig) { success = 1; }
796 else { success = 0; }
800 catch(exception& e) {
801 m->errorOut(e, "TrimSeqsCommand", "cullAmbigs");
807 //***************************************************************************************************************
809 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
812 int length = oligo.length();
814 for(int i=0;i<length;i++){
816 if(oligo[i] != seq[i]){
817 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
818 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
819 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
820 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
821 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
822 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
823 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
824 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
825 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
826 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
827 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
828 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
830 if(success == 0) { break; }
839 catch(exception& e) {
840 m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
846 //***************************************************************************************************************
848 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
850 string rawSequence = seq.getUnaligned();
851 int seqLength; // = rawSequence.length();
852 string name, temp, temp2;
854 qFile >> name >> temp;
856 splitAtEquals(temp2, temp); //separates length=242, temp=length, temp2=242
857 convert(temp, seqLength); //converts string to int
859 if (name.length() != 0) { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta and qual file"); m->mothurOutEndLine(); } }
860 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
865 for(int i=0;i<seqLength;i++){
868 if(score <= qThreshold){
873 for(int i=end+1;i<seqLength;i++){
877 seq.setUnaligned(rawSequence.substr(0,end));
881 catch(exception& e) {
882 m->errorOut(e, "TrimSeqsCommand", "stripQualThreshold");
887 //***************************************************************************************************************
889 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
891 string rawSequence = seq.getUnaligned();
892 int seqLength = seq.getNumBases();
893 bool success = 0; //guilty until proven innocent
897 if (name[0] == '>') { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta: " + seq.getName() + " and qual file: " + name); m->mothurOutEndLine(); } }
899 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
904 for(int i=0;i<seqLength;i++){
908 average /= seqLength;
910 if(average >= qAverage) { success = 1; }
911 else { success = 0; }
915 catch(exception& e) {
916 m->errorOut(e, "TrimSeqsCommand", "cullQualAverage");
921 //***************************************************************************************************************