5 * Created by Pat Schloss on 6/6/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "trimseqscommand.h"
12 //***************************************************************************************************************
14 TrimSeqsCommand::TrimSeqsCommand(){
18 totalBarcodeCount = 0;
19 matchBarcodeCount = 0;
21 globaldata = GlobalData::getInstance();
22 if(globaldata->getFastaFile() == ""){
23 cout << "you need to at least enter a fasta file name" << endl;
26 if(isTrue(globaldata->getFlip())) { flip = 1; }
27 if(globaldata->getOligosFile() != "") { oligos = 1; }
28 if(!flip && !oligos) { cout << "huh?" << endl; }
32 cout << "Standard Error: " << e.what() << " has occurred in the TrimSeqsCommand class Function TrimSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
36 cout << "An unknown error has occurred in the TrimSeqsCommand class function TrimSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
41 //***************************************************************************************************************
43 TrimSeqsCommand::~TrimSeqsCommand(){ /* do nothing */ }
45 //***************************************************************************************************************
47 int TrimSeqsCommand::execute(){
52 openInputFile(globaldata->getFastaFile(), inFASTA);
55 string trimSeqFile = getRootName(globaldata->getFastaFile()) + "trim.fasta";
56 openOutputFile(trimSeqFile, outFASTA);
59 string groupFile = getRootName(globaldata->getFastaFile()) + "groups";
60 openOutputFile(groupFile, outGroups);
63 string scrapSeqFile = getRootName(globaldata->getFastaFile()) + "scrap.fasta";
64 openOutputFile(scrapSeqFile, scrapFASTA);
68 while(!inFASTA.eof()){
69 Sequence currSeq(inFASTA);
70 string origSeq = currSeq.getUnaligned();
72 string trashCode = "";
74 if(barcodes.size() != 0){
75 success = stripBarcode(currSeq, group);
76 if(!success){ trashCode += 'b'; }
79 success = stripForward(currSeq);
80 if(!success){ trashCode += 'f'; }
83 success = stripReverse(currSeq);
84 if(!success){ trashCode += 'r'; }
86 if(flip){ currSeq.reverseComplement(); } // should go last
88 if(trashCode.length() == 0){
89 currSeq.printSequence(outFASTA);
90 outGroups << currSeq.getName() << '\t' << group << endl;
93 currSeq.setName(currSeq.getName() + '|' + trashCode);
94 currSeq.setUnaligned(origSeq);
95 currSeq.printSequence(scrapFASTA);
107 catch(exception& e) {
108 cout << "Standard Error: " << e.what() << " has occurred in the TrimSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
112 cout << "An unknown error has occurred in the TrimSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
117 //***************************************************************************************************************
119 void TrimSeqsCommand::getOligos(){
122 openInputFile(globaldata->getOligosFile(), inOligos);
124 string type, oligo, group;
126 while(!inOligos.eof()){
129 if(type == "forward"){
131 forPrimer.push_back(oligo);
133 else if(type == "reverse"){
135 revPrimer.push_back(oligo);
137 else if(type == "barcode"){
138 inOligos >> oligo >> group;
139 barcodes[oligo]=group;
141 else if(type[0] == '#'){
143 while ((c = inOligos.get()) != EOF) { if (c == 10){ break; } } // get rest of line
149 numFPrimers = forPrimer.size();
150 numRPrimers = revPrimer.size();
153 //***************************************************************************************************************
155 bool TrimSeqsCommand::stripBarcode(Sequence& seq, string& group){
157 string rawSequence = seq.getUnaligned();
158 bool success = 0; //guilty until proven innocent
160 for(map<string,string>::iterator it=barcodes.begin();it!=barcodes.end();it++){
161 string oligo = it->first;
163 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
168 if (rawSequence.compare(0,oligo.length(),oligo) == 0){
170 seq.setUnaligned(rawSequence.substr(oligo.length()));
181 //***************************************************************************************************************
183 bool TrimSeqsCommand::stripForward(Sequence& seq){
185 string rawSequence = seq.getUnaligned();
186 bool success = 0; //guilty until proven innocent
188 for(int i=0;i<numFPrimers;i++){
189 string oligo = forPrimer[i];
191 if(rawSequence.length() < oligo.length()){
196 if (rawSequence.compare(0,oligo.length(),oligo) == 0){
197 seq.setUnaligned(rawSequence.substr(oligo.length()));
209 //***************************************************************************************************************
211 bool TrimSeqsCommand::stripReverse(Sequence& seq){
213 string rawSequence = seq.getUnaligned();
214 bool success = 0; //guilty until proven innocent
216 for(int i=0;i<numRPrimers;i++){
217 string oligo = revPrimer[i];
219 if(rawSequence.length() < oligo.length()){
224 if(rawSequence.compare(rawSequence.length()-oligo.length(),oligo.length(),oligo) == 0){
225 seq.setUnaligned(rawSequence.substr(rawSequence.length()-oligo.length()));
237 //***************************************************************************************************************