1 #ifndef TRIMFLOWSCOMMAND_H
2 #define TRIMFLOWSCOMMAND_H
8 * Created by Pat Schloss on 12/22/10.
9 * Copyright 2010 Schloss Lab. All rights reserved.
14 #include "command.hpp"
15 #include "sequence.hpp"
18 #include "trimoligos.h"
20 class TrimFlowsCommand : public Command {
22 TrimFlowsCommand(string);
24 ~TrimFlowsCommand() {}
26 vector<string> setParameters();
27 string getCommandName() { return "trim.flows"; }
28 string getCommandCategory() { return "Sequence Processing"; }
29 string getOutputFileNameTag(string, string);
30 string getHelpString();
31 string getCitation() { return "http://www.mothur.org/wiki/Trim.flows"; }
32 string getDescription() { return "trim.flows"; }
36 void help() { m->mothurOut(getHelpString()); }
42 unsigned long long start;
43 unsigned long long end;
44 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
47 vector<int> processIDS; //processid
48 vector<linePair*> lines;
50 vector<unsigned long long> getFlowFileBreaks();
51 int createProcessesCreateTrim(string, string, string, string, vector<vector<string> >);
52 int driverCreateTrim(string, string, string, string, vector<vector<string> >, linePair*);
53 string reverseOligo(string);
55 vector<string> outputNames;
56 set<string> filesToRemove;
58 void getOligos(vector<vector<string> >&); //a rewrite of what is in trimseqscommand.h
62 int numFPrimers, numRPrimers;
63 int maxFlows, minFlows, minLength, maxLength, maxHomoP, tdiffs, bdiffs, pdiffs, sdiffs, ldiffs, numLinkers, numSpacers;
69 string flowFileName, oligoFileName, outputDir;
71 map<string, int> barcodes;
72 map<string, int> primers;
73 vector<string> revPrimer;
74 vector<string> linker;
75 vector<string> spacer;
77 vector<string> primerNameVector; //needed here?
78 vector<string> barcodeNameVector; //needed here?
80 map<string, int> combos; //needed here?
81 map<string, int> groupToIndex; //needed here?
85 /**************************************************************************************************/
86 //custom data structure for threads to use.
87 // This is passed by void pointer so it can be any data type
88 // that can be passed using a single void pointer (LPVOID).
91 string trimFlowFileName;
92 string scrapFlowFileName;
95 vector<vector<string> > barcodePrimerComboFileNames;
96 map<string, int> barcodes;
97 map<string, int> primers;
98 vector<string> revPrimer;
100 unsigned long long start;
101 unsigned long long end;
104 int numFlows, maxFlows, minFlows, maxHomoP, tdiffs, bdiffs, pdiffs, threadID, count;
107 trimFlowData(string ff, string tf, string sf, string f, string fo, vector<vector<string> > bfn, map<string, int> bar, map<string, int> pri, vector<string> rev, bool fa, bool al, unsigned long long st, unsigned long long en, MothurOut* mout, float sig, float n, int numF, int maxF, int minF, int maxH, int td, int bd, int pd, int tid) {
109 trimFlowFileName = tf;
110 scrapFlowFileName = sf;
113 barcodePrimerComboFileNames = bfn;
135 /**************************************************************************************************/
136 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
138 static DWORD WINAPI MyTrimFlowThreadFunction(LPVOID lpParam){
139 trimFlowData* pDataArray;
140 pDataArray = (trimFlowData*)lpParam;
143 ofstream trimFlowFile;
144 pDataArray->m->openOutputFile(pDataArray->trimFlowFileName, trimFlowFile);
145 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
147 ofstream scrapFlowFile;
148 pDataArray->m->openOutputFile(pDataArray->scrapFlowFileName, scrapFlowFile);
149 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
152 if(pDataArray->fasta){ pDataArray->m->openOutputFile(pDataArray->fastaFileName, fastaFile); }
155 pDataArray->m->openInputFile(pDataArray->flowFileName, flowFile);
157 flowFile.seekg(pDataArray->start);
159 if(pDataArray->start == 0){
160 flowFile >> pDataArray->numFlows; pDataArray->m->gobble(flowFile);
161 scrapFlowFile << pDataArray->maxFlows << endl;
162 trimFlowFile << pDataArray->maxFlows << endl;
163 if(pDataArray->allFiles){
164 for(int i=0;i<pDataArray->barcodePrimerComboFileNames.size();i++){
165 for(int j=0;j<pDataArray->barcodePrimerComboFileNames[0].size();j++){
167 pDataArray->m->openOutputFile(pDataArray->barcodePrimerComboFileNames[i][j], temp);
168 temp << pDataArray->maxFlows << endl;
175 FlowData flowData(pDataArray->numFlows, pDataArray->signal, pDataArray->noise, pDataArray->maxHomoP, pDataArray->flowOrder);
176 cout << " thread flowdata address " << &flowData << '\t' << &flowFile << endl;
177 TrimOligos trimOligos(pDataArray->pdiffs, pDataArray->bdiffs, pDataArray->primers, pDataArray->barcodes, pDataArray->revPrimer);
179 pDataArray->count = pDataArray->end;
180 cout << pDataArray->threadID << '\t' << pDataArray->count << endl;
182 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
184 if (pDataArray->m->control_pressed) { break; }
185 cout << pDataArray->threadID << '\t' << count << endl;
187 int currentSeqDiffs = 0;
188 string trashCode = "";
190 flowData.getNext(flowFile);
191 cout << "thread good bit " << flowFile.good() << endl;
192 flowData.capFlows(pDataArray->maxFlows);
194 Sequence currSeq = flowData.getSequence();
195 if(!flowData.hasMinFlows(pDataArray->minFlows)){ //screen to see if sequence is of a minimum number of flows
201 int barcodeIndex = 0;
203 if(pDataArray->barcodes.size() != 0){
204 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
205 if(success > pDataArray->bdiffs) { trashCode += 'b'; }
206 else{ currentSeqDiffs += success; }
209 if(pDataArray->primers.size() != 0){
210 success = trimOligos.stripForward(currSeq, primerIndex);
211 if(success > pDataArray->pdiffs) { trashCode += 'f'; }
212 else{ currentSeqDiffs += success; }
215 if (currentSeqDiffs > pDataArray->tdiffs) { trashCode += 't'; }
217 if(pDataArray->revPrimer.size() != 0){
218 success = trimOligos.stripReverse(currSeq);
219 if(!success) { trashCode += 'r'; }
222 if(trashCode.length() == 0){
224 flowData.printFlows(trimFlowFile);
226 if(pDataArray->fasta) { currSeq.printSequence(fastaFile); }
228 if(pDataArray->allFiles){
230 pDataArray->m->openOutputFileAppend(pDataArray->barcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
231 output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
233 flowData.printFlows(output);
238 flowData.printFlows(scrapFlowFile, trashCode);
242 cout << pDataArray->threadID << '\t' << currSeq.getName() << endl;
244 if((count) % 10000 == 0){ pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine(); }
248 if((count) % 10000 != 0){ pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine(); }
250 trimFlowFile.close();
251 scrapFlowFile.close();
253 if(pDataArray->fasta){ fastaFile.close(); }
256 catch(exception& e) {
257 pDataArray->m->errorOut(e, "TrimFlowsCommand", "MyTrimFlowsThreadFunction");