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","tdiffs", "pdiffs", "bdiffs", "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);
108 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found") { temp = "0"; }
109 convert(temp, bdiffs);
111 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found") { temp = "0"; }
112 convert(temp, pdiffs);
114 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
115 convert(temp, tdiffs);
117 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; }
119 temp = validParameter.validFile(parameters, "qfile", true);
120 if (temp == "not found") { qFileName = ""; }
121 else if(temp == "not open") { abort = true; }
122 else { qFileName = temp; }
124 temp = validParameter.validFile(parameters, "qthreshold", false); if (temp == "not found") { temp = "0"; }
125 convert(temp, qThreshold);
127 temp = validParameter.validFile(parameters, "qtrim", false); if (temp == "not found") { temp = "F"; }
128 qtrim = isTrue(temp);
130 temp = validParameter.validFile(parameters, "qaverage", false); if (temp == "not found") { temp = "0"; }
131 convert(temp, qAverage);
133 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
134 allFiles = isTrue(temp);
136 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
137 convert(temp, processors);
139 if(allFiles && oligoFile == ""){
140 m->mothurOut("You selected allfiles, but didn't enter an oligos file. Ignoring the allfiles request."); m->mothurOutEndLine();
142 if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
143 m->mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); m->mothurOutEndLine();
147 if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){
148 m->mothurOut("You didn't set any options... quiting command."); m->mothurOutEndLine();
154 catch(exception& e) {
155 m->errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
159 //**********************************************************************************************************************
161 void TrimSeqsCommand::help(){
163 m->mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
164 m->mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, diffs, qtrim and allfiles.\n");
165 m->mothurOut("The fasta parameter is required.\n");
166 m->mothurOut("The flip parameter will output the reverse compliment of your trimmed sequence. The default is false.\n");
167 m->mothurOut("The oligos parameter .... The default is "".\n");
168 m->mothurOut("The maxambig parameter .... The default is -1.\n");
169 m->mothurOut("The maxhomop parameter .... The default is 0.\n");
170 m->mothurOut("The minlength parameter .... The default is 0.\n");
171 m->mothurOut("The maxlength parameter .... The default is 0.\n");
172 m->mothurOut("The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is pdiffs + bdiffs.\n");
173 m->mothurOut("The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n");
174 m->mothurOut("The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n");
175 m->mothurOut("The qfile parameter .....\n");
176 m->mothurOut("The qthreshold parameter .... The default is 0.\n");
177 m->mothurOut("The qaverage parameter .... The default is 0.\n");
178 m->mothurOut("The allfiles parameter .... The default is F.\n");
179 m->mothurOut("The qtrim parameter .... The default is F.\n");
180 m->mothurOut("The trim.seqs command should be in the following format: \n");
181 m->mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig, \n");
182 m->mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n");
183 m->mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
184 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
185 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.seqs .\n\n");
188 catch(exception& e) {
189 m->errorOut(e, "TrimSeqsCommand", "help");
195 //***************************************************************************************************************
197 TrimSeqsCommand::~TrimSeqsCommand(){ /* do nothing */ }
199 //***************************************************************************************************************
201 int TrimSeqsCommand::execute(){
204 if (abort == true) { return 0; }
206 numFPrimers = 0; //this needs to be initialized
209 string trimSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "trim.fasta";
210 outputNames.push_back(trimSeqFile);
211 string scrapSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "scrap.fasta";
212 outputNames.push_back(scrapSeqFile);
213 string groupFile = outputDir + getRootName(getSimpleName(fastaFile)) + "groups";
215 vector<string> fastaFileNames;
217 outputNames.push_back(groupFile);
218 getOligos(fastaFileNames);
221 if(qFileName != "") { setLines(qFileName, qLines); }
224 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
228 openInputFile(fastaFile, inFASTA);
229 getNumSeqs(inFASTA, numSeqs);
232 lines.push_back(new linePair(0, numSeqs));
234 driverCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
236 for (int j = 0; j < fastaFileNames.size(); j++) {
237 rename((fastaFileNames[j] + toString(getpid()) + ".temp").c_str(), fastaFileNames[j].c_str());
241 setLines(fastaFile, lines);
242 if(qFileName == "") { qLines = lines; }
244 createProcessesCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames);
246 rename((trimSeqFile + toString(processIDS[0]) + ".temp").c_str(), trimSeqFile.c_str());
247 rename((scrapSeqFile + toString(processIDS[0]) + ".temp").c_str(), scrapSeqFile.c_str());
248 rename((groupFile + toString(processIDS[0]) + ".temp").c_str(), groupFile.c_str());
249 for (int j = 0; j < fastaFileNames.size(); j++) {
250 rename((fastaFileNames[j] + toString(processIDS[0]) + ".temp").c_str(), fastaFileNames[j].c_str());
253 for(int i=1;i<processors;i++){
254 appendFiles((trimSeqFile + toString(processIDS[i]) + ".temp"), trimSeqFile);
255 remove((trimSeqFile + toString(processIDS[i]) + ".temp").c_str());
256 appendFiles((scrapSeqFile + toString(processIDS[i]) + ".temp"), scrapSeqFile);
257 remove((scrapSeqFile + toString(processIDS[i]) + ".temp").c_str());
258 appendFiles((groupFile + toString(processIDS[i]) + ".temp"), groupFile);
259 remove((groupFile + toString(processIDS[i]) + ".temp").c_str());
260 for (int j = 0; j < fastaFileNames.size(); j++) {
261 appendFiles((fastaFileNames[j] + toString(processIDS[i]) + ".temp"), fastaFileNames[j]);
262 remove((fastaFileNames[j] + toString(processIDS[i]) + ".temp").c_str());
267 if (m->control_pressed) { return 0; }
271 openInputFile(fastaFile, inFASTA);
272 getNumSeqs(inFASTA, numSeqs);
275 lines.push_back(new linePair(0, numSeqs));
277 driverCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
279 if (m->control_pressed) { return 0; }
283 for(int i=0;i<fastaFileNames.size();i++){
286 //openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
287 openInputFile(fastaFileNames[i], inFASTA);
289 string outGroupFilename = outputDir + getRootName(getSimpleName(fastaFileNames[i])) + "groups";
290 openOutputFile(outGroupFilename, outGroups);
291 //openOutputFile(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups", outGroups);
292 outputNames.push_back(outGroupFilename);
294 while(!inFASTA.eof()){
295 if(inFASTA.get() == '>'){
297 outGroups << seqName << '\t' << groupVector[i] << endl;
299 while (!inFASTA.eof()) { char c = inFASTA.get(); if (c == 10 || c == 13){ break; } }
305 if (m->control_pressed) {
306 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
310 m->mothurOutEndLine();
311 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
312 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
313 m->mothurOutEndLine();
318 catch(exception& e) {
319 m->errorOut(e, "TrimSeqsCommand", "execute");
324 /**************************************************************************************/
325 int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames, linePair* line, linePair* qline) {
329 int able = openOutputFile(trimFile, outFASTA);
332 openOutputFile(scrapFile, scrapFASTA);
335 vector<ofstream*> fastaFileNames;
336 if (oligoFile != "") {
337 openOutputFile(groupFile, outGroups);
338 for (int i = 0; i < fastaNames.size(); i++) {
339 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
340 fastaFileNames.push_back(new ofstream((fastaNames[i] + toString(getpid()) + ".temp").c_str(), ios::ate));
342 fastaFileNames.push_back(new ofstream((fastaNames[i] + toString(i) + ".temp").c_str(), ios::ate));
348 openInputFile(filename, inFASTA);
351 if(qFileName != "") { openInputFile(qFileName, qFile); }
353 qFile.seekg(qline->start);
354 inFASTA.seekg(line->start);
356 for(int i=0;i<line->num;i++){
358 if (m->control_pressed) {
359 inFASTA.close(); outFASTA.close(); scrapFASTA.close(); if (oligoFile != "") { outGroups.close(); } if(qFileName != "") { qFile.close(); }
360 for(int i=0;i<fastaFileNames.size();i++){ fastaFileNames[i]->close(); delete fastaFileNames[i]; }
361 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
367 Sequence currSeq(inFASTA);
369 string origSeq = currSeq.getUnaligned();
372 string trashCode = "";
373 int currentSeqsDiffs = 0;
376 if(qThreshold != 0) { success = stripQualThreshold(currSeq, qFile); }
377 else if(qAverage != 0) { success = cullQualAverage(currSeq, qFile); }
379 if (qtrim == 1 && (origSeq.length() != currSeq.getUnaligned().length())) {
380 success = 0; //if you don't want to trim and the sequence does not meet quality requirements, move to scrap
383 if(!success) { trashCode += 'q'; }
386 if(barcodes.size() != 0){
387 success = stripBarcode(currSeq, group);
388 if(success > bdiffs){ trashCode += 'b'; }
389 else{ currentSeqsDiffs += success; }
392 if(numFPrimers != 0){
393 success = stripForward(currSeq);
394 if(success > pdiffs){ trashCode += 'f'; }
395 else{ currentSeqsDiffs += success; }
398 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
400 if(numRPrimers != 0){
401 success = stripReverse(currSeq);
402 if(!success){ trashCode += 'r'; }
405 if(minLength > 0 || maxLength > 0){
406 success = cullLength(currSeq);
407 if(!success){ trashCode += 'l'; }
410 success = cullHomoP(currSeq);
411 if(!success){ trashCode += 'h'; }
414 success = cullAmbigs(currSeq);
415 if(!success){ trashCode += 'n'; }
418 if(flip){ currSeq.reverseComplement(); } // should go last
420 if(trashCode.length() == 0){
421 currSeq.setAligned(currSeq.getUnaligned());
422 currSeq.printSequence(outFASTA);
423 if(barcodes.size() != 0){
424 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
427 currSeq.printSequence(*fastaFileNames[group]);
432 currSeq.setName(currSeq.getName() + '|' + trashCode);
433 currSeq.setUnaligned(origSeq);
434 currSeq.setAligned(origSeq);
435 currSeq.printSequence(scrapFASTA);
444 if (oligoFile != "") { outGroups.close(); }
445 if(qFileName != "") { qFile.close(); }
447 for(int i=0;i<fastaFileNames.size();i++){
448 fastaFileNames[i]->close();
449 delete fastaFileNames[i];
454 catch(exception& e) {
455 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
459 /**************************************************************************************************/
460 int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames) {
462 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
467 //loop through and create all the processes you want
468 while (process != processors) {
472 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
475 driverCreateTrim(filename, qFileName, (trimFile + toString(getpid()) + ".temp"), (scrapFile + toString(getpid()) + ".temp"), (groupFile + toString(getpid()) + ".temp"), fastaNames, lines[process], qLines[process]);
477 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
480 //force parent to wait until all the processes are done
481 for (int i=0;i<processors;i++) {
482 int temp = processIDS[i];
489 catch(exception& e) {
490 m->errorOut(e, "TrimSeqsCommand", "createProcessesCreateTrim");
494 /**************************************************************************************************/
496 int TrimSeqsCommand::setLines(string filename, vector<linePair*>& lines) {
501 vector<long int> positions;
504 openInputFile(filename, inFASTA);
507 while(!inFASTA.eof()){
508 input = getline(inFASTA);
510 if (input.length() != 0) {
511 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
516 int numFastaSeqs = positions.size();
521 //get num bytes in file
522 pFile = fopen (filename.c_str(),"rb");
523 if (pFile==NULL) perror ("Error opening file");
525 fseek (pFile, 0, SEEK_END);
530 int numSeqsPerProcessor = numFastaSeqs / processors;
532 for (int i = 0; i < processors; i++) {
534 long int startPos = positions[ i * numSeqsPerProcessor ];
535 if(i == processors - 1){
536 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
538 long int myEnd = positions[ (i+1) * numSeqsPerProcessor ];
540 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
545 catch(exception& e) {
546 m->errorOut(e, "TrimSeqsCommand", "setLines");
550 //***************************************************************************************************************
552 void TrimSeqsCommand::getOligos(vector<string>& outFASTAVec){ //vector<ofstream*>& outFASTAVec
555 openInputFile(oligoFile, inOligos);
559 string type, oligo, group;
562 while(!inOligos.eof()){
566 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
571 for(int i=0;i<oligo.length();i++){
572 oligo[i] = toupper(oligo[i]);
573 if(oligo[i] == 'U') { oligo[i] = 'T'; }
576 if(type == "forward"){
577 forPrimer.push_back(oligo);
579 else if(type == "reverse"){
580 Sequence oligoRC("reverse", oligo);
581 oligoRC.reverseComplement();
582 revPrimer.push_back(oligoRC.getUnaligned());
584 else if(type == "barcode"){
586 barcodes[oligo]=index; index++;
587 groupVector.push_back(group);
590 //outFASTAVec.push_back(new ofstream((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta").c_str(), ios::ate));
591 outputNames.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + toString(index) + "." + group + ".fasta"));
592 outFASTAVec.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + toString(index) + "." + group + ".fasta"));
600 numFPrimers = forPrimer.size();
601 numRPrimers = revPrimer.size();
604 catch(exception& e) {
605 m->errorOut(e, "TrimSeqsCommand", "getOligos");
609 //***************************************************************************************************************
611 int TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
614 string rawSequence = seq.getUnaligned();
615 int success = bdiffs + 1; //guilty until proven innocent
617 //can you find the barcode
618 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
619 string oligo = it->first;
620 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
621 success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out
625 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
627 seq.setUnaligned(rawSequence.substr(oligo.length()));
633 //if you found the barcode or if you don't want to allow for diffs
635 if ((bdiffs == 0) || (success == 0)) { return success; }
637 else { //try aligning and see if you can find it
642 Alignment* alignment;
643 if (barcodes.size() > 0) {
644 map<string,int>::iterator it=barcodes.begin();
646 for(it;it!=barcodes.end();it++){
647 if(it->first.length() > maxLength){
648 maxLength = it->first.length();
651 alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+bdiffs+1));
653 }else{ alignment = NULL; }
655 //can you find the barcode
661 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
662 string oligo = it->first;
663 // int length = oligo.length();
665 if(rawSequence.length() < maxLength){ //let's just assume that the barcodes are the same length
666 success = bdiffs + 10;
670 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
671 alignment->align(oligo, rawSequence.substr(0,oligo.length()+bdiffs));
672 oligo = alignment->getSeqAAln();
673 string temp = alignment->getSeqBAln();
675 int alnLength = oligo.length();
677 for(int i=oligo.length()-1;i>=0;i--){
678 if(oligo[i] != '-'){ alnLength = i+1; break; }
680 oligo = oligo.substr(0,alnLength);
681 temp = temp.substr(0,alnLength);
684 int numDiff = countDiffs(oligo, temp);
686 // cout << oligo << '\t' << temp << '\t' << numDiff << endl;
688 if(numDiff < minDiff){
691 minGroup = it->second;
693 for(int i=0;i<alnLength;i++){
699 else if(numDiff == minDiff){
705 if(minDiff > bdiffs) { success = minDiff; } //no good matches
706 else if(minCount > 1) { success = bdiffs + 100; } //can't tell the difference between multiple barcodes
707 else{ //use the best match
709 seq.setUnaligned(rawSequence.substr(minPos));
713 if (alignment != NULL) { delete alignment; }
716 // cout << success << endl;
721 catch(exception& e) {
722 m->errorOut(e, "TrimSeqsCommand", "stripBarcode");
728 //***************************************************************************************************************
730 int TrimSeqsCommand::stripForward(Sequence& seq){
732 string rawSequence = seq.getUnaligned();
733 int success = pdiffs + 1; //guilty until proven innocent
735 //can you find the primer
736 for(int i=0;i<numFPrimers;i++){
737 string oligo = forPrimer[i];
739 if(rawSequence.length() < oligo.length()){
740 success = pdiffs + 1;
744 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
745 seq.setUnaligned(rawSequence.substr(oligo.length()));
751 //if you found the barcode or if you don't want to allow for diffs
753 if ((pdiffs == 0) || (success == 0)) { return success; }
755 else { //try aligning and see if you can find it
760 Alignment* alignment;
761 if (numFPrimers > 0) {
763 for(int i=0;i<numFPrimers;i++){
764 if(forPrimer[i].length() > maxLength){
765 maxLength = forPrimer[i].length();
768 alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+pdiffs+1));
770 }else{ alignment = NULL; }
772 //can you find the barcode
777 for(int i=0;i<numFPrimers;i++){
778 string oligo = forPrimer[i];
780 if(rawSequence.length() < maxLength){
781 success = pdiffs + 100;
785 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
786 alignment->align(oligo, rawSequence.substr(0,oligo.length()+pdiffs));
787 oligo = alignment->getSeqAAln();
788 string temp = alignment->getSeqBAln();
790 int alnLength = oligo.length();
792 for(int i=oligo.length()-1;i>=0;i--){
793 if(oligo[i] != '-'){ alnLength = i+1; break; }
795 oligo = oligo.substr(0,alnLength);
796 temp = temp.substr(0,alnLength);
799 int numDiff = countDiffs(oligo, temp);
800 if(numDiff < minDiff){
804 for(int i=0;i<alnLength;i++){
810 else if(numDiff == minDiff){
815 if(minDiff > pdiffs) { success = minDiff; }
816 else if(minCount > 1) { success = pdiffs + 10; }
818 seq.setUnaligned(rawSequence.substr(minPos));
822 if (alignment != NULL) { delete alignment; }
828 catch(exception& e) {
829 m->errorOut(e, "TrimSeqsCommand", "stripForward");
834 //***************************************************************************************************************
836 bool TrimSeqsCommand::stripReverse(Sequence& seq){
838 string rawSequence = seq.getUnaligned();
839 bool success = 0; //guilty until proven innocent
841 for(int i=0;i<numRPrimers;i++){
842 string oligo = revPrimer[i];
844 if(rawSequence.length() < oligo.length()){
849 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
850 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
858 catch(exception& e) {
859 m->errorOut(e, "TrimSeqsCommand", "stripReverse");
864 //***************************************************************************************************************
866 bool TrimSeqsCommand::cullLength(Sequence& seq){
869 int length = seq.getNumBases();
870 bool success = 0; //guilty until proven innocent
872 if(length >= minLength && maxLength == 0) { success = 1; }
873 else if(length >= minLength && length <= maxLength) { success = 1; }
874 else { success = 0; }
879 catch(exception& e) {
880 m->errorOut(e, "TrimSeqsCommand", "cullLength");
886 //***************************************************************************************************************
888 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
890 int longHomoP = seq.getLongHomoPolymer();
891 bool success = 0; //guilty until proven innocent
893 if(longHomoP <= maxHomoP){ success = 1; }
894 else { success = 0; }
898 catch(exception& e) {
899 m->errorOut(e, "TrimSeqsCommand", "cullHomoP");
905 //***************************************************************************************************************
907 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
909 int numNs = seq.getAmbigBases();
910 bool success = 0; //guilty until proven innocent
912 if(numNs <= maxAmbig) { success = 1; }
913 else { success = 0; }
917 catch(exception& e) {
918 m->errorOut(e, "TrimSeqsCommand", "cullAmbigs");
924 //***************************************************************************************************************
926 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
929 int length = oligo.length();
931 for(int i=0;i<length;i++){
933 if(oligo[i] != seq[i]){
934 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
935 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
936 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
937 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
938 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
939 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
940 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
941 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
942 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
943 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
944 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
945 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
947 if(success == 0) { break; }
956 catch(exception& e) {
957 m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
962 //***************************************************************************************************************
964 int TrimSeqsCommand::countDiffs(string oligo, string seq){
967 int length = oligo.length();
970 for(int i=0;i<length;i++){
972 if(oligo[i] != seq[i]){
973 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C' || oligo[i] == '-' || oligo[i] == '.') { countDiffs++; }
974 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { countDiffs++; }
975 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { countDiffs++; }
976 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { countDiffs++; }
977 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { countDiffs++; }
978 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
979 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { countDiffs++; }
980 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
981 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
982 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
983 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { countDiffs++; }
984 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
991 catch(exception& e) {
992 m->errorOut(e, "TrimSeqsCommand", "countDiffs");
997 //***************************************************************************************************************
999 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
1001 // string rawSequence = seq.getUnaligned();
1002 // int seqLength; // = rawSequence.length();
1003 // string name, temp, temp2;
1007 // //get rest of line
1009 // while (!qFile.eof()) {
1010 // char c = qFile.get();
1011 // if (c == 10 || c == 13){ break; }
1012 // else { temp += c; }
1015 // int pos = temp.find("length");
1016 // if (pos == temp.npos) { m->mothurOut("Cannot find length in qfile for " + seq.getName()); m->mothurOutEndLine(); seqLength = 0; }
1018 // string tempLength = temp.substr(pos);
1019 // istringstream iss (tempLength,istringstream::in);
1023 // splitAtEquals(temp2, temp); //separates length=242, temp=length, temp2=242
1024 // convert(temp, seqLength); //converts string to int
1026 // if (name.length() != 0) { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta and qual file"); m->mothurOutEndLine(); } }
1028 string rawSequence = seq.getUnaligned();
1029 int seqLength = seq.getNumBases();
1030 bool success = 0; //guilty until proven innocent
1034 if (name[0] == '>') { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta: " + seq.getName() + " and qual file: " + name); m->mothurOutEndLine(); } }
1036 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
1039 int end = seqLength;
1041 for(int i=0;i<seqLength;i++){
1044 if(score < qThreshold){
1049 for(int i=end+1;i<seqLength;i++){
1053 seq.setUnaligned(rawSequence.substr(0,end));
1057 catch(exception& e) {
1058 m->errorOut(e, "TrimSeqsCommand", "stripQualThreshold");
1063 //***************************************************************************************************************
1065 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
1067 string rawSequence = seq.getUnaligned();
1068 int seqLength = seq.getNumBases();
1069 bool success = 0; //guilty until proven innocent
1073 if (name[0] == '>') { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta: " + seq.getName() + " and qual file: " + name); m->mothurOutEndLine(); } }
1075 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
1080 for(int i=0;i<seqLength;i++){
1084 average /= seqLength;
1086 if(average >= qAverage) { success = 1; }
1087 else { success = 0; }
1091 catch(exception& e) {
1092 m->errorOut(e, "TrimSeqsCommand", "cullQualAverage");
1097 //***************************************************************************************************************