1 #ifndef Mothur_pcrseqscommand_h
2 #define Mothur_pcrseqscommand_h
8 // Created by Sarah Westcott on 3/14/12.
9 // Copyright (c) 2012 Schloss Lab. All rights reserved.
13 #include "command.hpp"
14 #include "sequence.hpp"
15 #include "trimoligos.h"
16 #include "alignment.hpp"
17 #include "needlemanoverlap.hpp"
19 class PcrSeqsCommand : public Command {
21 PcrSeqsCommand(string);
25 vector<string> setParameters();
26 string getCommandName() { return "pcr.seqs"; }
27 string getCommandCategory() { return "Sequence Processing"; }
28 string getHelpString();
29 string getCitation() { return "http://www.mothur.org/wiki/Pcr.seqs"; }
30 string getDescription() { return "pcr.seqs"; }
33 void help() { m->mothurOut(getHelpString()); }
38 unsigned long long start;
39 unsigned long long end;
40 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
44 vector<linePair> lines;
45 bool getOligos(vector<vector<string> >&, vector<vector<string> >&, vector<vector<string> >&);
46 bool abort, keepprimer, keepdots;
47 string fastafile, oligosfile, taxfile, groupfile, namefile, ecolifile, outputDir, nomatch;
48 int start, end, pdiffs, processors, length;
50 vector<string> revPrimer, outputNames;
51 vector<string> primers;
53 int writeAccnos(set<string>);
54 int readName(set<string>&);
55 int readGroup(set<string>);
56 int readTax(set<string>);
59 int driverPcr(string, string, string, set<string>&, linePair);
60 int createProcesses(string, string, string, set<string>&);
61 bool findForward(Sequence&, int&, int&);
62 bool findReverse(Sequence&, int&, int&);
63 bool isAligned(string, map<int, int>&);
64 bool compareDNASeq(string, string);
65 string reverseOligo(string);
68 /**************************************************************************************************/
69 //custom data structure for threads to use.
70 // This is passed by void pointer so it can be any data type
71 // that can be passed using a single void pointer (LPVOID).
74 string goodFasta, badFasta, oligosfile, ecolifile, nomatch;
75 unsigned long long fstart;
76 unsigned long long fend;
77 int count, start, end, length;
79 vector<string> primers;
80 vector<string> revPrimer;
81 set<string> badSeqNames;
82 bool keepprimer, keepdots;
86 pcrData(string f, string gf, string bfn, MothurOut* mout, string ol, string ec, vector<string> pr, vector<string> rpr, string nm, bool kp, bool kd, int st, int en, int l, unsigned long long fst, unsigned long long fen) {
106 /**************************************************************************************************/
107 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
109 static DWORD WINAPI MyPcrThreadFunction(LPVOID lpParam){
111 pDataArray = (pcrData*)lpParam;
115 pDataArray->m->openOutputFile(pDataArray->goodFasta, goodFile);
118 pDataArray->m->openOutputFile(pDataArray->badFasta, badFile);
121 pDataArray->m->openInputFile(pDataArray->filename, inFASTA);
123 //print header if you are process 0
124 if ((pDataArray->fstart == 0) || (pDataArray->fstart == 1)) {
126 }else { //this accounts for the difference in line endings.
127 inFASTA.seekg(pDataArray->fstart-1); pDataArray->m->gobble(inFASTA);
131 pDataArray->count = pDataArray->fend;
132 for(int i = 0; i < pDataArray->fend; i++){ //end is the number of sequences to process
134 if (pDataArray->m->control_pressed) { break; }
136 Sequence currSeq(inFASTA); pDataArray->m->gobble(inFASTA);
138 string trashCode = "";
139 if (currSeq.getName() != "") {
142 if (pDataArray->oligosfile != "") {
143 map<int, int> mapAligned;
144 //bool aligned = isAligned(currSeq.getAligned(), mapAligned);
145 ///////////////////////////////////////////////////////////////
146 bool aligned = false;
147 string seq = currSeq.getAligned();
149 for (int k = 0; k < seq.length(); k++) {
150 if (!isalpha(seq[k])) { aligned = true; }
151 else { mapAligned[countBases] = k; countBases++; } //maps location in unaligned -> location in aligned.
152 } //ie. the 3rd base may be at spot 10 in the alignment
153 //later when we trim we want to trim from spot 10.
154 ///////////////////////////////////////////////////////////////
157 if (pDataArray->primers.size() != 0) {
158 int primerStart = 0; int primerEnd = 0;
159 //bool good = findForward(currSeq, primerStart, primerEnd);
160 ///////////////////////////////////////////////////////////////
162 string rawSequence = currSeq.getUnaligned();
164 for(int j=0;j<pDataArray->primers.size();j++){
165 string oligo = pDataArray->primers[j];
167 if (pDataArray->m->control_pressed) { primerStart = 0; primerEnd = 0; good = false; break; }
169 if(rawSequence.length() < oligo.length()) { break; }
172 int olength = oligo.length();
173 for (int l = 0; l < rawSequence.length()-olength; l++){
174 if (pDataArray->m->control_pressed) { primerStart = 0; primerEnd = 0; good = false; break; }
175 string rawChunk = rawSequence.substr(l, olength);
176 //compareDNASeq(oligo, rawChunk)
177 ////////////////////////////////////////////////////////
179 for(int k=0;k<olength;k++){
181 if(oligo[k] != rawChunk[k]){
182 if(oligo[k] == 'A' || oligo[k] == 'T' || oligo[k] == 'G' || oligo[k] == 'C') { success = 0; }
183 else if((oligo[k] == 'N' || oligo[k] == 'I') && (rawChunk[k] == 'N')) { success = 0; }
184 else if(oligo[k] == 'R' && (rawChunk[k] != 'A' && rawChunk[k] != 'G')) { success = 0; }
185 else if(oligo[k] == 'Y' && (rawChunk[k] != 'C' && rawChunk[k] != 'T')) { success = 0; }
186 else if(oligo[k] == 'M' && (rawChunk[k] != 'C' && rawChunk[k] != 'A')) { success = 0; }
187 else if(oligo[k] == 'K' && (rawChunk[k] != 'T' && rawChunk[k] != 'G')) { success = 0; }
188 else if(oligo[k] == 'W' && (rawChunk[k] != 'T' && rawChunk[k] != 'A')) { success = 0; }
189 else if(oligo[k] == 'S' && (rawChunk[k] != 'C' && rawChunk[k] != 'G')) { success = 0; }
190 else if(oligo[k] == 'B' && (rawChunk[k] != 'C' && rawChunk[k] != 'T' && rawChunk[k] != 'G')) { success = 0; }
191 else if(oligo[k] == 'D' && (rawChunk[k] != 'A' && rawChunk[k] != 'T' && rawChunk[k] != 'G')) { success = 0; }
192 else if(oligo[k] == 'H' && (rawChunk[k] != 'A' && rawChunk[k] != 'T' && rawChunk[k] != 'C')) { success = 0; }
193 else if(oligo[k] == 'V' && (rawChunk[k] != 'A' && rawChunk[k] != 'C' && rawChunk[k] != 'G')) { success = 0; }
195 if(success == 0) { break; }
202 ////////////////////////////////////////////////////////////////////
205 primerEnd = primerStart + olength;
212 if (!good) { primerStart = 0; primerEnd = 0; }
213 ///////////////////////////////////////////////////////////////
216 if(!good){ if (pDataArray->nomatch == "reject") { goodSeq = false; } trashCode += "f"; }
220 if (!pDataArray->keepprimer) {
221 if (pDataArray->keepdots) { currSeq.filterToPos(mapAligned[primerEnd]); }
222 else { currSeq.setAligned(currSeq.getAligned().substr(mapAligned[primerEnd])); }
225 if (pDataArray->keepdots) { currSeq.filterToPos(mapAligned[primerStart]); }
226 else { currSeq.setAligned(currSeq.getAligned().substr(mapAligned[primerStart])); }
229 if (!pDataArray->keepprimer) { currSeq.setAligned(currSeq.getUnaligned().substr(primerEnd)); }
230 else { currSeq.setAligned(currSeq.getUnaligned().substr(primerStart)); }
235 //process reverse primers
236 if (pDataArray->revPrimer.size() != 0) {
237 int primerStart = 0; int primerEnd = 0;
239 //findReverse(currSeq, primerStart, primerEnd);
240 ///////////////////////////////////////////////////////////////
241 string rawSequence = currSeq.getUnaligned();
243 for(int j=0;j<pDataArray->revPrimer.size();j++){
244 string oligo = pDataArray->revPrimer[j];
245 if (pDataArray->m->control_pressed) { primerStart = 0; primerEnd = 0; good = false; break; }
246 if(rawSequence.length() < oligo.length()) { break; }
249 int olength = oligo.length();
250 for (int l = rawSequence.length()-olength; l >= 0; l--){
252 string rawChunk = rawSequence.substr(l, olength);
253 //compareDNASeq(oligo, rawChunk)
254 ////////////////////////////////////////////////////////
256 for(int k=0;k<olength;k++){
258 if(oligo[k] != rawChunk[k]){
259 if(oligo[k] == 'A' || oligo[k] == 'T' || oligo[k] == 'G' || oligo[k] == 'C') { success = 0; }
260 else if((oligo[k] == 'N' || oligo[k] == 'I') && (rawChunk[k] == 'N')) { success = 0; }
261 else if(oligo[k] == 'R' && (rawChunk[k] != 'A' && rawChunk[k] != 'G')) { success = 0; }
262 else if(oligo[k] == 'Y' && (rawChunk[k] != 'C' && rawChunk[k] != 'T')) { success = 0; }
263 else if(oligo[k] == 'M' && (rawChunk[k] != 'C' && rawChunk[k] != 'A')) { success = 0; }
264 else if(oligo[k] == 'K' && (rawChunk[k] != 'T' && rawChunk[k] != 'G')) { success = 0; }
265 else if(oligo[k] == 'W' && (rawChunk[k] != 'T' && rawChunk[k] != 'A')) { success = 0; }
266 else if(oligo[k] == 'S' && (rawChunk[k] != 'C' && rawChunk[k] != 'G')) { success = 0; }
267 else if(oligo[k] == 'B' && (rawChunk[k] != 'C' && rawChunk[k] != 'T' && rawChunk[k] != 'G')) { success = 0; }
268 else if(oligo[k] == 'D' && (rawChunk[k] != 'A' && rawChunk[k] != 'T' && rawChunk[k] != 'G')) { success = 0; }
269 else if(oligo[k] == 'H' && (rawChunk[k] != 'A' && rawChunk[k] != 'T' && rawChunk[k] != 'C')) { success = 0; }
270 else if(oligo[k] == 'V' && (rawChunk[k] != 'A' && rawChunk[k] != 'C' && rawChunk[k] != 'G')) { success = 0; }
272 if(success == 0) { break; }
279 ////////////////////////////////////////////////////////////////////
282 primerEnd = primerStart + olength;
289 if (!good) { primerStart = 0; primerEnd = 0; }
291 ///////////////////////////////////////////////////////////////
292 if(!good){ if (pDataArray->nomatch == "reject") { goodSeq = false; } trashCode += "r"; }
296 if (!pDataArray->keepprimer) {
297 if (pDataArray->keepdots) { currSeq.filterFromPos(mapAligned[primerStart]); }
298 else { currSeq.setAligned(currSeq.getAligned().substr(0, mapAligned[primerStart])); }
301 if (pDataArray->keepdots) { currSeq.filterFromPos(mapAligned[primerEnd]); }
302 else { currSeq.setAligned(currSeq.getAligned().substr(0, mapAligned[primerEnd])); }
305 if (!pDataArray->keepprimer) { currSeq.setAligned(currSeq.getUnaligned().substr(0, primerStart)); }
306 else { currSeq.setAligned(currSeq.getUnaligned().substr(0, primerEnd)); }
310 }else if (pDataArray->ecolifile != "") {
311 //make sure the seqs are aligned
312 lengths.insert(currSeq.getAligned().length());
313 if (lengths.size() > 1) { pDataArray->m->mothurOut("[ERROR]: seqs are not aligned. When using start and end your sequences must be aligned.\n"); pDataArray->m->control_pressed = true; break; }
314 else if (currSeq.getAligned().length() != pDataArray->length) {
315 pDataArray->m->mothurOut("[ERROR]: seqs are not the same length as ecoli seq. When using ecoli option your sequences must be aligned and the same length as the ecoli sequence.\n"); pDataArray->m->control_pressed = true; break;
317 if (pDataArray->keepdots) {
318 currSeq.filterToPos(start);
319 currSeq.filterFromPos(end);
321 string seqString = currSeq.getAligned().substr(0, end);
322 seqString = seqString.substr(start);
323 currSeq.setAligned(seqString);
326 }else{ //using start and end to trim
327 //make sure the seqs are aligned
328 lengths.insert(currSeq.getAligned().length());
329 if (lengths.size() > 1) { pDataArray->m->mothurOut("[ERROR]: seqs are not aligned. When using start and end your sequences must be aligned.\n"); pDataArray->m->control_pressed = true; break; }
331 if (pDataArray->end != -1) {
332 if (pDataArray->end > currSeq.getAligned().length()) { pDataArray->m->mothurOut("[ERROR]: end is longer than your sequence length, aborting.\n"); pDataArray->m->control_pressed = true; break; }
334 if (pDataArray->keepdots) { currSeq.filterFromPos(end); }
336 string seqString = currSeq.getAligned().substr(0, end);
337 currSeq.setAligned(seqString);
341 if (pDataArray->start != -1) {
342 if (pDataArray->keepdots) { currSeq.filterToPos(start); }
344 string seqString = currSeq.getAligned().substr(start);
345 currSeq.setAligned(seqString);
352 if(goodSeq == 1) { currSeq.printSequence(goodFile); }
354 pDataArray->badSeqNames.insert(currSeq.getName());
355 currSeq.setName(currSeq.getName() + '|' + trashCode);
356 currSeq.printSequence(badFile);
361 if((i+1) % 100 == 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(i+1)); pDataArray->m->mothurOutEndLine(); }
364 if((pDataArray->count) % 100 != 0){ pDataArray->m->mothurOut("Thread Processing sequence: " + toString(pDataArray->count)); pDataArray->m->mothurOutEndLine(); }
373 catch(exception& e) {
374 pDataArray->m->errorOut(e, "PcrSeqsCommand", "MyPcrThreadFunction");
381 /**************************************************************************************************/