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"
13 //***************************************************************************************************************
15 TrimSeqsCommand::TrimSeqsCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string AlignArray[] = {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile",
26 "qthreshold", "qaverage", "allfiles", "qtrim","diffs", "processors", "outputdir","inputdir"};
28 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
30 OptionParser parser(option);
31 map<string,string> parameters = parser.getParameters();
33 ValidParameters validParameter;
34 map<string,string>::iterator it;
36 //check to make sure all parameters are valid for command
37 for (it = parameters.begin(); it != parameters.end(); it++) {
38 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
41 //if the user changes the input directory command factory will send this info to us in the output parameter
42 string inputDir = validParameter.validFile(parameters, "inputdir", false);
43 if (inputDir == "not found"){ inputDir = ""; }
46 it = parameters.find("fasta");
47 //user has given a template file
48 if(it != parameters.end()){
49 path = hasPath(it->second);
50 //if the user has not given a path then, add inputdir. else leave path alone.
51 if (path == "") { parameters["fasta"] = inputDir + it->second; }
54 it = parameters.find("oligos");
55 //user has given a template file
56 if(it != parameters.end()){
57 path = hasPath(it->second);
58 //if the user has not given a path then, add inputdir. else leave path alone.
59 if (path == "") { parameters["oligos"] = inputDir + it->second; }
62 it = parameters.find("qfile");
63 //user has given a template file
64 if(it != parameters.end()){
65 path = hasPath(it->second);
66 //if the user has not given a path then, add inputdir. else leave path alone.
67 if (path == "") { parameters["qfile"] = inputDir + it->second; }
72 //check for required parameters
73 fastaFile = validParameter.validFile(parameters, "fasta", true);
74 if (fastaFile == "not found") { m->mothurOut("fasta is a required parameter for the screen.seqs command."); m->mothurOutEndLine(); abort = true; }
75 else if (fastaFile == "not open") { abort = true; }
77 //if the user changes the output directory command factory will send this info to us in the output parameter
78 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
80 outputDir += hasPath(fastaFile); //if user entered a file with a path then preserve it
83 //check for optional parameter and set defaults
84 // ...at some point should added some additional type checking...
86 temp = validParameter.validFile(parameters, "flip", false);
87 if (temp == "not found"){ flip = 0; }
88 else if(isTrue(temp)) { flip = 1; }
90 temp = validParameter.validFile(parameters, "oligos", true);
91 if (temp == "not found"){ oligoFile = ""; }
92 else if(temp == "not open"){ abort = true; }
93 else { oligoFile = temp; }
95 temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; }
96 convert(temp, maxAmbig);
98 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "0"; }
99 convert(temp, maxHomoP);
101 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "0"; }
102 convert(temp, minLength);
104 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "0"; }
105 convert(temp, maxLength);
107 temp = validParameter.validFile(parameters, "diffs", false); if (temp == "not found") { temp = "0"; }
108 convert(temp, diffs);
110 temp = validParameter.validFile(parameters, "qfile", true);
111 if (temp == "not found") { qFileName = ""; }
112 else if(temp == "not open") { abort = true; }
113 else { qFileName = temp; }
115 temp = validParameter.validFile(parameters, "qthreshold", false); if (temp == "not found") { temp = "0"; }
116 convert(temp, qThreshold);
118 temp = validParameter.validFile(parameters, "qtrim", false); if (temp == "not found") { temp = "F"; }
119 qtrim = isTrue(temp);
121 temp = validParameter.validFile(parameters, "qaverage", false); if (temp == "not found") { temp = "0"; }
122 convert(temp, qAverage);
124 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
125 allFiles = isTrue(temp);
127 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
128 convert(temp, processors);
130 if(allFiles && oligoFile == ""){
131 m->mothurOut("You selected allfiles, but didn't enter an oligos file. Ignoring the allfiles request."); m->mothurOutEndLine();
133 if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
134 m->mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); m->mothurOutEndLine();
138 if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){
139 m->mothurOut("You didn't set any options... quiting command."); m->mothurOutEndLine();
145 catch(exception& e) {
146 m->errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
150 //**********************************************************************************************************************
152 void TrimSeqsCommand::help(){
154 m->mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
155 m->mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, diffs, qtrim and allfiles.\n");
156 m->mothurOut("The fasta parameter is required.\n");
157 m->mothurOut("The flip parameter .... The default is 0.\n");
158 m->mothurOut("The oligos parameter .... The default is "".\n");
159 m->mothurOut("The maxambig parameter .... The default is -1.\n");
160 m->mothurOut("The maxhomop parameter .... The default is 0.\n");
161 m->mothurOut("The minlength parameter .... The default is 0.\n");
162 m->mothurOut("The maxlength parameter .... The default is 0.\n");
163 m->mothurOut("The diffs parameter .... The default is 0.\n");
164 m->mothurOut("The qfile parameter .....\n");
165 m->mothurOut("The qthreshold parameter .... The default is 0.\n");
166 m->mothurOut("The qaverage parameter .... The default is 0.\n");
167 m->mothurOut("The allfiles parameter .... The default is F.\n");
168 m->mothurOut("The qtrim parameter .... The default is F.\n");
169 m->mothurOut("The trim.seqs command should be in the following format: \n");
170 m->mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig, \n");
171 m->mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n");
172 m->mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
173 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
174 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.seqs .\n\n");
177 catch(exception& e) {
178 m->errorOut(e, "TrimSeqsCommand", "help");
184 //***************************************************************************************************************
186 TrimSeqsCommand::~TrimSeqsCommand(){ /* do nothing */ }
188 //***************************************************************************************************************
190 int TrimSeqsCommand::execute(){
193 if (abort == true) { return 0; }
195 numFPrimers = 0; //this needs to be initialized
198 string trimSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "trim.fasta";
199 outputNames.push_back(trimSeqFile);
200 string scrapSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "scrap.fasta";
201 outputNames.push_back(scrapSeqFile);
202 string groupFile = outputDir + getRootName(getSimpleName(fastaFile)) + "groups";
204 vector<string> fastaFileNames;
206 outputNames.push_back(groupFile);
207 getOligos(fastaFileNames);
210 if(qFileName != "") { setLines(qFileName, qLines); }
213 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
216 openInputFile(fastaFile, inFASTA);
217 int numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
220 lines.push_back(new linePair(0, numSeqs));
222 driverCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
224 for (int j = 0; j < fastaFileNames.size(); j++) {
225 rename((fastaFileNames[j] + toString(getpid()) + ".temp").c_str(), fastaFileNames[j].c_str());
229 setLines(fastaFile, lines);
230 if(qFileName == "") { qLines = lines; }
232 createProcessesCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames);
234 rename((trimSeqFile + toString(processIDS[0]) + ".temp").c_str(), trimSeqFile.c_str());
235 rename((scrapSeqFile + toString(processIDS[0]) + ".temp").c_str(), scrapSeqFile.c_str());
236 rename((groupFile + toString(processIDS[0]) + ".temp").c_str(), groupFile.c_str());
237 for (int j = 0; j < fastaFileNames.size(); j++) {
238 rename((fastaFileNames[j] + toString(processIDS[0]) + ".temp").c_str(), fastaFileNames[j].c_str());
241 for(int i=1;i<processors;i++){
242 appendFiles((trimSeqFile + toString(processIDS[i]) + ".temp"), trimSeqFile);
243 remove((trimSeqFile + toString(processIDS[i]) + ".temp").c_str());
244 appendFiles((scrapSeqFile + toString(processIDS[i]) + ".temp"), scrapSeqFile);
245 remove((scrapSeqFile + toString(processIDS[i]) + ".temp").c_str());
246 appendFiles((groupFile + toString(processIDS[i]) + ".temp"), groupFile);
247 remove((groupFile + toString(processIDS[i]) + ".temp").c_str());
248 for (int j = 0; j < fastaFileNames.size(); j++) {
249 appendFiles((fastaFileNames[j] + toString(processIDS[i]) + ".temp"), fastaFileNames[j]);
250 remove((fastaFileNames[j] + toString(processIDS[i]) + ".temp").c_str());
255 if (m->control_pressed) { return 0; }
258 openInputFile(fastafileNames[s], inFASTA);
259 numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
262 lines.push_back(new linePair(0, numSeqs));
264 driverCreateSummary(fastafile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
266 if (m->control_pressed) { return 0; }
270 for(int i=0;i<fastaFileNames.size();i++){
273 openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
275 openOutputFile(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups", outGroups);
276 outputNames.push_back(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups");
278 while(!inFASTA.eof()){
279 if(inFASTA.get() == '>'){
281 outGroups << seqName << '\t' << groupVector[i] << endl;
283 while (!inFASTA.eof()) { char c = inFASTA.get(); if (c == 10 || c == 13){ break; } }
289 if (m->control_pressed) {
290 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
294 m->mothurOutEndLine();
295 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
296 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
297 m->mothurOutEndLine();
302 catch(exception& e) {
303 m->errorOut(e, "TrimSeqsCommand", "execute");
308 /**************************************************************************************/
309 int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames, linePair* line, linePair* qline) {
313 int able = openOutputFile(trimFile, outFASTA);
316 openOutputFile(scrapFile, scrapFASTA);
319 vector<ofstream*> fastaFileNames;
320 if (oligoFile != "") {
321 openOutputFile(groupFile, outGroups);
322 for (int i = 0; i < fastaNames.size(); i++) {
323 fastaFileNames.push_back(new ofstream((fastaNames[i] + toString(getpid()) + ".temp").c_str(), ios::ate));
328 openInputFile(filename, inFASTA);
331 if(qFileName != "") { openInputFile(qFileName, qFile); }
333 qFile.seekg(qline->start);
334 inFASTA.seekg(line->start);
336 for(int i=0;i<line->num;i++){
338 if (m->control_pressed) {
342 if (oligoFile != "") { outGroups.close(); }
343 if(qFileName != "") { qFile.close(); }
344 for(int i=0;i<fastaFileNames.size();i++){
345 fastaFileNames[i]->close();
346 delete fastaFileNames[i];
348 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
354 Sequence currSeq(inFASTA);
356 string origSeq = currSeq.getUnaligned();
359 string trashCode = "";
362 if(qThreshold != 0) { success = stripQualThreshold(currSeq, qFile); }
363 else if(qAverage != 0) { success = cullQualAverage(currSeq, qFile); }
364 if ((!qtrim) && (origSeq.length() != currSeq.getUnaligned().length())) {
365 success = 0; //if you don't want to trim and the sequence does not meet quality requirements, move to scrap
367 if(!success) { trashCode += 'q'; }
370 if(barcodes.size() != 0){
371 success = stripBarcode(currSeq, group);
372 if(!success){ trashCode += 'b'; }
375 if(numFPrimers != 0){
376 success = stripForward(currSeq);
377 if(!success){ trashCode += 'f'; }
380 if(numRPrimers != 0){
381 success = stripReverse(currSeq);
382 if(!success){ trashCode += 'r'; }
385 if(minLength > 0 || maxLength > 0){
386 success = cullLength(currSeq);
387 if(!success){ trashCode += 'l'; }
390 success = cullHomoP(currSeq);
391 if(!success){ trashCode += 'h'; }
394 success = cullAmbigs(currSeq);
395 if(!success){ trashCode += 'n'; }
398 if(flip){ currSeq.reverseComplement(); } // should go last
400 if(trashCode.length() == 0){
401 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.
402 currSeq.printSequence(outFASTA);
403 if(barcodes.size() != 0){
404 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
407 currSeq.printSequence(*fastaFileNames[group]);
412 currSeq.setName(currSeq.getName() + '|' + trashCode);
413 currSeq.setUnaligned(origSeq);
414 currSeq.printSequence(scrapFASTA);
423 if (oligoFile != "") { outGroups.close(); }
424 if(qFileName != "") { qFile.close(); }
426 for(int i=0;i<fastaFileNames.size();i++){
427 fastaFileNames[i]->close();
428 delete fastaFileNames[i];
433 catch(exception& e) {
434 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
438 /**************************************************************************************************/
439 int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames) {
441 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
446 //loop through and create all the processes you want
447 while (process != processors) {
451 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
454 driverCreateTrim(filename, qFileName, (trimFile + toString(getpid()) + ".temp"), (scrapFile + toString(getpid()) + ".temp"), (groupFile + toString(getpid()) + ".temp"), fastaNames, lines[process], qLines[process]);
456 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
459 //force parent to wait until all the processes are done
460 for (int i=0;i<processors;i++) {
461 int temp = processIDS[i];
468 catch(exception& e) {
469 m->errorOut(e, "TrimSeqsCommand", "createProcessesCreateTrim");
473 /**************************************************************************************************/
475 int TrimSeqsCommand::setLines(string filename, vector<linePair*>& lines) {
480 vector<long int> positions;
483 openInputFile(filename, inFASTA);
486 while(!inFASTA.eof()){
487 input = getline(inFASTA);
489 if (input.length() != 0) {
490 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
495 int numFastaSeqs = positions.size();
500 //get num bytes in file
501 pFile = fopen (filename.c_str(),"rb");
502 if (pFile==NULL) perror ("Error opening file");
504 fseek (pFile, 0, SEEK_END);
509 int numSeqsPerProcessor = numFastaSeqs / processors;
511 for (int i = 0; i < processors; i++) {
513 long int startPos = positions[ i * numSeqsPerProcessor ];
514 if(i == processors - 1){
515 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
517 long int myEnd = positions[ (i+1) * numSeqsPerProcessor ];
519 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
524 catch(exception& e) {
525 m->errorOut(e, "TrimSeqsCommand", "setLines");
529 //***************************************************************************************************************
531 void TrimSeqsCommand::getOligos(vector<string>& outFASTAVec){ //vector<ofstream*>& outFASTAVec
534 openInputFile(oligoFile, inOligos);
538 string type, oligo, group;
541 while(!inOligos.eof()){
545 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
550 for(int i=0;i<oligo.length();i++){
551 oligo[i] = toupper(oligo[i]);
552 if(oligo[i] == 'U') { oligo[i] = 'T'; }
555 if(type == "forward"){
556 forPrimer.push_back(oligo);
558 else if(type == "reverse"){
559 Sequence oligoRC("reverse", oligo);
560 oligoRC.reverseComplement();
561 revPrimer.push_back(oligoRC.getUnaligned());
563 else if(type == "barcode"){
565 barcodes[oligo]=index++;
566 groupVector.push_back(group);
569 //outFASTAVec.push_back(new ofstream((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta").c_str(), ios::ate));
570 outputNames.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta"));
571 outFASTAVec.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta"));
579 numFPrimers = forPrimer.size();
580 numRPrimers = revPrimer.size();
583 catch(exception& e) {
584 m->errorOut(e, "TrimSeqsCommand", "getOligos");
588 //***************************************************************************************************************
590 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
592 string rawSequence = seq.getUnaligned();
593 bool success = 0; //guilty until proven innocent
595 //can you find the barcode
596 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
597 string oligo = it->first;
598 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
603 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
605 seq.setUnaligned(rawSequence.substr(oligo.length()));
611 //if you found the barcode or if you don't want to allow for diffs
612 if ((diffs == 0) || (success == 1)) { return success; }
614 else { //try aligning and see if you can find it
616 Alignment* alignment;
617 if (barcodes.size() > 0) { //assumes barcodes are all the same length
618 map<string,int>::iterator it=barcodes.begin();
619 string temp = it->first;
621 alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (temp.length()+diffs+1));
622 }else{ alignment = NULL; }
625 //can you find the barcode
626 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
627 string oligo = it->first;
628 int length = oligo.length();
630 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
635 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
636 alignment->align(oligo, rawSequence.substr(0,length+diffs));
637 oligo = alignment->getSeqAAln();
638 string temp = alignment->getSeqBAln();
639 //cout << "barcode = " << oligo << " raw = " << rawSequence.substr(0,oligo.length()) << " raw aligned = " << temp << endl;
642 if(compareDNASeq(oligo, temp, length, newStart)){
644 seq.setUnaligned(rawSequence.substr(newStart));
650 if (alignment != NULL) { delete alignment; }
655 catch(exception& e) {
656 m->errorOut(e, "TrimSeqsCommand", "stripBarcode");
662 //***************************************************************************************************************
664 bool TrimSeqsCommand::stripForward(Sequence& seq){
666 string rawSequence = seq.getUnaligned();
667 bool success = 0; //guilty until proven innocent
669 for(int i=0;i<numFPrimers;i++){
670 string oligo = forPrimer[i];
672 if(rawSequence.length() < oligo.length()){
677 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
678 seq.setUnaligned(rawSequence.substr(oligo.length()));
684 //if you found the primer or if you don't want to allow for diffs
685 if ((diffs == 0) || (success == 1)) { return success; }
687 else { //try aligning and see if you can find it
690 Alignment* alignment;
691 if (numFPrimers > 0) { alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (forPrimer[0].length()+diffs+1)); } //assumes primers are all the same length
692 else{ alignment = NULL; }
694 //can you find the primer
695 for(int i=0;i<numFPrimers;i++){
696 string oligo = forPrimer[i];
697 int length = oligo.length();
699 if(rawSequence.length() < oligo.length()){
704 //use needleman to align first primer.length()+numdiffs of sequence to each primer
705 alignment->align(oligo, rawSequence.substr(0,length+diffs));
706 oligo = alignment->getSeqAAln();
707 string temp = alignment->getSeqBAln();
710 if(compareDNASeq(oligo, temp, length, newStart)){
711 seq.setUnaligned(rawSequence.substr(newStart));
717 if (alignment != NULL) { delete alignment; }
723 catch(exception& e) {
724 m->errorOut(e, "TrimSeqsCommand", "stripForward");
729 //***************************************************************************************************************
731 bool TrimSeqsCommand::stripReverse(Sequence& seq){
733 string rawSequence = seq.getUnaligned();
734 bool success = 0; //guilty until proven innocent
736 for(int i=0;i<numRPrimers;i++){
737 string oligo = revPrimer[i];
739 if(rawSequence.length() < oligo.length()){
744 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
745 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
753 catch(exception& e) {
754 m->errorOut(e, "TrimSeqsCommand", "stripReverse");
759 //***************************************************************************************************************
761 bool TrimSeqsCommand::cullLength(Sequence& seq){
764 int length = seq.getNumBases();
765 bool success = 0; //guilty until proven innocent
767 if(length >= minLength && maxLength == 0) { success = 1; }
768 else if(length >= minLength && length <= maxLength) { success = 1; }
769 else { success = 0; }
774 catch(exception& e) {
775 m->errorOut(e, "TrimSeqsCommand", "cullLength");
781 //***************************************************************************************************************
783 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
785 int longHomoP = seq.getLongHomoPolymer();
786 bool success = 0; //guilty until proven innocent
788 if(longHomoP <= maxHomoP){ success = 1; }
789 else { success = 0; }
793 catch(exception& e) {
794 m->errorOut(e, "TrimSeqsCommand", "cullHomoP");
800 //***************************************************************************************************************
802 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
804 int numNs = seq.getAmbigBases();
805 bool success = 0; //guilty until proven innocent
807 if(numNs <= maxAmbig) { success = 1; }
808 else { success = 0; }
812 catch(exception& e) {
813 m->errorOut(e, "TrimSeqsCommand", "cullAmbigs");
819 //***************************************************************************************************************
821 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
824 int length = oligo.length();
826 for(int i=0;i<length;i++){
828 if(oligo[i] != seq[i]){
829 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
830 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
831 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
832 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
833 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
834 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
835 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
836 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
837 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
838 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
839 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
840 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
842 if(success == 0) { break; }
851 catch(exception& e) {
852 m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
857 //***************************************************************************************************************
859 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq, int numBases, int& end){
862 int length = oligo.length();
868 if ((oligo[0] == '-') || (oligo[0] == '.')) { success = 0; return success; } //no gaps allowed at beginning
871 for(int i=0;i<length;i++){
873 if ((oligo[i] != '-') && (oligo[i] != '.')) { countBases++; }
875 //cout << oligo[i] << " " << seq[i] << " diffs = " << countDiffs << " countBases = " << countBases << endl;
877 if(oligo[i] != seq[i]){
878 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C' || oligo[i] == '-' || oligo[i] == '.') { countDiffs++; }
879 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { countDiffs++; }
880 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { countDiffs++; }
881 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { countDiffs++; }
882 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { countDiffs++; }
883 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
884 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { countDiffs++; }
885 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
886 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
887 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
888 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { countDiffs++; }
889 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
891 if(countDiffs > diffs) { success = 0; break; }
897 if (countBases >= numBases) { end = i; break; } //stop checking after end of barcode or primer
902 catch(exception& e) {
903 m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
908 //***************************************************************************************************************
910 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
912 string rawSequence = seq.getUnaligned();
913 int seqLength; // = rawSequence.length();
914 string name, temp, temp2;
916 qFile >> name >> temp;
918 splitAtEquals(temp2, temp); //separates length=242, temp=length, temp2=242
919 convert(temp, seqLength); //converts string to int
921 if (name.length() != 0) { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta and qual file"); m->mothurOutEndLine(); } }
922 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
927 for(int i=0;i<seqLength;i++){
930 if(score <= qThreshold){
935 for(int i=end+1;i<seqLength;i++){
939 seq.setUnaligned(rawSequence.substr(0,end));
943 catch(exception& e) {
944 m->errorOut(e, "TrimSeqsCommand", "stripQualThreshold");
949 //***************************************************************************************************************
951 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
953 string rawSequence = seq.getUnaligned();
954 int seqLength = seq.getNumBases();
955 bool success = 0; //guilty until proven innocent
959 if (name[0] == '>') { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta: " + seq.getName() + " and qual file: " + name); m->mothurOutEndLine(); } }
961 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
966 for(int i=0;i<seqLength;i++){
970 average /= seqLength;
972 if(average >= qAverage) { success = 1; }
973 else { success = 0; }
977 catch(exception& e) {
978 m->errorOut(e, "TrimSeqsCommand", "cullQualAverage");
983 //***************************************************************************************************************