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"
21 class TrimFlowsCommand : public Command {
23 TrimFlowsCommand(string);
25 ~TrimFlowsCommand() {}
27 vector<string> setParameters();
28 string getCommandName() { return "trim.flows"; }
29 string getCommandCategory() { return "Sequence Processing"; }
31 string getHelpString();
32 string getOutputPattern(string);
33 string getCitation() { return "http://www.mothur.org/wiki/Trim.flows"; }
34 string getDescription() { return "trim.flows"; }
38 void help() { m->mothurOut(getHelpString()); }
44 unsigned long long start;
45 unsigned long long end;
46 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
49 vector<int> processIDS; //processid
50 vector<linePair*> lines;
51 vector<string> outputNames;
52 set<string> filesToRemove;
55 int numFPrimers, numRPrimers, numBarcodes;
56 int maxFlows, minFlows, minLength, maxLength, maxHomoP, tdiffs, bdiffs, pdiffs, sdiffs, ldiffs, numLinkers, numSpacers;
59 bool fasta, pairedOligos, reorient;
60 string flowOrder, flowFileName, oligoFileName, outputDir;
64 vector<unsigned long long> getFlowFileBreaks();
65 int createProcessesCreateTrim(string, string, string, string, vector<vector<string> >);
66 int driverCreateTrim(string, string, string, string, vector<vector<string> >, linePair*);
67 int getOligos(vector<vector<string> >&); //a rewrite of what is in trimseqscommand.h
73 /**************************************************************************************************
74 //custom data structure for threads to use.
75 // This is passed by void pointer so it can be any data type
76 // that can be passed using a single void pointer (LPVOID).
79 string trimFlowFileName;
80 string scrapFlowFileName;
83 vector<vector<string> > barcodePrimerComboFileNames;
84 map<string, int> barcodes;
85 map<string, int> primers;
86 vector<string> revPrimer;
88 unsigned long long start;
89 unsigned long long end;
92 int numFlows, maxFlows, minFlows, maxHomoP, tdiffs, bdiffs, pdiffs, threadID, count;
95 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) {
97 trimFlowFileName = tf;
98 scrapFlowFileName = sf;
101 barcodePrimerComboFileNames = bfn;
123 /**************************************************************************************************
124 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
126 static DWORD WINAPI MyTrimFlowThreadFunction(LPVOID lpParam){
127 trimFlowData* pDataArray;
128 pDataArray = (trimFlowData*)lpParam;
131 ofstream trimFlowFile;
132 pDataArray->m->openOutputFile(pDataArray->trimFlowFileName, trimFlowFile);
133 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
135 ofstream scrapFlowFile;
136 pDataArray->m->openOutputFile(pDataArray->scrapFlowFileName, scrapFlowFile);
137 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
140 if(pDataArray->fasta){ pDataArray->m->openOutputFile(pDataArray->fastaFileName, fastaFile); }
143 pDataArray->m->openInputFile(pDataArray->flowFileName, flowFile);
145 flowFile.seekg(pDataArray->start);
147 if(pDataArray->start == 0){
148 flowFile >> pDataArray->numFlows; pDataArray->m->gobble(flowFile);
149 scrapFlowFile << pDataArray->maxFlows << endl;
150 trimFlowFile << pDataArray->maxFlows << endl;
151 if(pDataArray->allFiles){
152 for(int i=0;i<pDataArray->barcodePrimerComboFileNames.size();i++){
153 for(int j=0;j<pDataArray->barcodePrimerComboFileNames[0].size();j++){
155 pDataArray->m->openOutputFile(pDataArray->barcodePrimerComboFileNames[i][j], temp);
156 temp << pDataArray->maxFlows << endl;
163 FlowData flowData(pDataArray->numFlows, pDataArray->signal, pDataArray->noise, pDataArray->maxHomoP, pDataArray->flowOrder);
164 cout << " thread flowdata address " << &flowData << '\t' << &flowFile << endl;
165 TrimOligos trimOligos(pDataArray->pdiffs, pDataArray->bdiffs, pDataArray->primers, pDataArray->barcodes, pDataArray->revPrimer);
167 pDataArray->count = pDataArray->end;
168 cout << pDataArray->threadID << '\t' << pDataArray->count << endl;
170 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
172 if (pDataArray->m->control_pressed) { break; }
173 cout << pDataArray->threadID << '\t' << count << endl;
175 int currentSeqDiffs = 0;
176 string trashCode = "";
178 flowData.getNext(flowFile);
179 cout << "thread good bit " << flowFile.good() << endl;
180 flowData.capFlows(pDataArray->maxFlows);
182 Sequence currSeq = flowData.getSequence();
183 if(!flowData.hasMinFlows(pDataArray->minFlows)){ //screen to see if sequence is of a minimum number of flows
189 int barcodeIndex = 0;
191 if(pDataArray->barcodes.size() != 0){
192 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
193 if(success > pDataArray->bdiffs) { trashCode += 'b'; }
194 else{ currentSeqDiffs += success; }
197 if(pDataArray->primers.size() != 0){
198 success = trimOligos.stripForward(currSeq, primerIndex);
199 if(success > pDataArray->pdiffs) { trashCode += 'f'; }
200 else{ currentSeqDiffs += success; }
203 if (currentSeqDiffs > pDataArray->tdiffs) { trashCode += 't'; }
205 if(pDataArray->revPrimer.size() != 0){
206 success = trimOligos.stripReverse(currSeq);
207 if(!success) { trashCode += 'r'; }
210 if(trashCode.length() == 0){
212 flowData.printFlows(trimFlowFile);
214 if(pDataArray->fasta) { currSeq.setAligned(currSeq.getUnaligned()); currSeq.printSequence(fastaFile); }
216 if(pDataArray->allFiles){
218 pDataArray->m->openOutputFileAppend(pDataArray->barcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
219 output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
221 flowData.printFlows(output);
226 flowData.printFlows(scrapFlowFile, trashCode);
230 cout << pDataArray->threadID << '\t' << currSeq.getName() << endl;
232 if((count) % 10000 == 0){ pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine(); }
236 if((count) % 10000 != 0){ pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine(); }
238 trimFlowFile.close();
239 scrapFlowFile.close();
241 if(pDataArray->fasta){ fastaFile.close(); }
244 catch(exception& e) {
245 pDataArray->m->errorOut(e, "TrimFlowsCommand", "MyTrimFlowsThreadFunction");