]> git.donarmstrong.com Git - mothur.git/blob - trimflowscommand.h
f4faacb899a8fd547a14d2bd2224b07882356cdf
[mothur.git] / trimflowscommand.h
1 #ifndef TRIMFLOWSCOMMAND_H
2 #define TRIMFLOWSCOMMAND_H
3
4 /*
5  *  trimflowscommand.h
6  *  Mothur
7  *
8  *  Created by Pat Schloss on 12/22/10.
9  *  Copyright 2010 Schloss Lab. All rights reserved.
10  *
11  */
12
13 #include "mothur.h"
14 #include "command.hpp"
15 #include "sequence.hpp"
16 #include "flowdata.h"
17 #include "groupmap.h"
18 #include "trimoligos.h"
19
20 class TrimFlowsCommand : public Command {
21 public:
22         TrimFlowsCommand(string);
23         TrimFlowsCommand();
24         ~TrimFlowsCommand() {}
25         
26         vector<string> setParameters();
27         string getCommandName()                 { return "trim.flows";  }
28         string getCommandCategory()             { return "Sequence Processing";         }
29         string getHelpString(); 
30         string getCitation() { return "http://www.mothur.org/wiki/Trim.flows"; }
31         string getDescription()         { return "trim.flows"; }
32
33         
34         int execute(); 
35         void help() { m->mothurOut(getHelpString()); }  
36         
37 private:
38         bool abort;
39
40         struct linePair {
41                 unsigned long long start;
42                 unsigned long long end;
43                 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
44         };
45         int comboStarts;
46         vector<int> processIDS;   //processid
47         vector<linePair*> lines;
48
49         vector<unsigned long long> getFlowFileBreaks();
50         int createProcessesCreateTrim(string, string, string, string, vector<vector<string> >); 
51         int driverCreateTrim(string, string, string, string, vector<vector<string> >, linePair*);
52
53         vector<string> outputNames;
54         set<string> filesToRemove;
55         
56         void getOligos(vector<vector<string> >&);               //a rewrite of what is in trimseqscommand.h
57         
58         bool allFiles;
59         int processors;
60         int numFPrimers, numRPrimers;
61         int maxFlows, minFlows, minLength, maxLength, maxHomoP, tdiffs, bdiffs, pdiffs, sdiffs, ldiffs, numLinkers, numSpacers;
62         int numFlows;
63         float signal, noise;
64         bool fasta;
65         string flowOrder;       
66         
67         string flowFileName, oligoFileName, outputDir;
68
69
70         map<string, int> barcodes;
71         map<string, int> primers;
72         vector<string> revPrimer;
73     vector<string> linker;
74     vector<string> spacer;
75
76         vector<string> primerNameVector;        //needed here?
77         vector<string> barcodeNameVector;       //needed here?
78
79         map<string, int> combos;                        //needed here?
80         map<string, int> groupToIndex;          //needed here?
81         
82 };
83
84 /**************************************************************************************************/
85 //custom data structure for threads to use.
86 // This is passed by void pointer so it can be any data type
87 // that can be passed using a single void pointer (LPVOID).
88 struct trimFlowData {
89         string flowFileName; 
90         string trimFlowFileName; 
91         string scrapFlowFileName;
92         string fastaFileName;
93         string flowOrder;
94         vector<vector<string> > barcodePrimerComboFileNames;
95         map<string, int> barcodes;
96         map<string, int> primers;
97         vector<string> revPrimer;
98         bool fasta, allFiles;
99         unsigned long long start;
100         unsigned long long end;
101         MothurOut* m;
102         float signal, noise;
103         int numFlows, maxFlows, minFlows, maxHomoP, tdiffs, bdiffs, pdiffs, threadID, count;
104         
105         trimFlowData(){}
106         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) {
107                 flowFileName = ff;
108                 trimFlowFileName = tf;
109                 scrapFlowFileName = sf;
110                 fastaFileName = f;
111                 flowOrder = fo;
112                 barcodePrimerComboFileNames = bfn;
113                 barcodes = bar;
114                 primers = pri;
115                 revPrimer = rev;
116                 fasta = fa;
117                 allFiles = al;
118                 start = st;
119                 end = en;
120                 m = mout;
121                 signal = sig;
122                 noise = n;
123                 numFlows = numF;
124                 maxFlows = maxF;
125                 minFlows = minF;
126                 maxHomoP = maxH;
127                 tdiffs = td;
128                 bdiffs = bd;
129                 pdiffs = pd;
130                 threadID = tid;
131         }
132 };
133
134 /**************************************************************************************************/
135 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
136 #else
137 static DWORD WINAPI MyTrimFlowThreadFunction(LPVOID lpParam){ 
138         trimFlowData* pDataArray;
139         pDataArray = (trimFlowData*)lpParam;
140         
141         try {
142                 ofstream trimFlowFile;
143                 pDataArray->m->openOutputFile(pDataArray->trimFlowFileName, trimFlowFile);
144                 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
145                 
146                 ofstream scrapFlowFile;
147                 pDataArray->m->openOutputFile(pDataArray->scrapFlowFileName, scrapFlowFile);
148                 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
149                 
150                 ofstream fastaFile;
151                 if(pDataArray->fasta){  pDataArray->m->openOutputFile(pDataArray->fastaFileName, fastaFile);    }
152                 
153                 ifstream flowFile;
154                 pDataArray->m->openInputFile(pDataArray->flowFileName, flowFile);
155                 
156                 flowFile.seekg(pDataArray->start);
157                 
158                 if(pDataArray->start == 0){
159                         flowFile >> pDataArray->numFlows; pDataArray->m->gobble(flowFile);
160                         scrapFlowFile << pDataArray->maxFlows << endl;
161                         trimFlowFile << pDataArray->maxFlows << endl;
162                         if(pDataArray->allFiles){
163                                 for(int i=0;i<pDataArray->barcodePrimerComboFileNames.size();i++){
164                                         for(int j=0;j<pDataArray->barcodePrimerComboFileNames[0].size();j++){
165                                                 ofstream temp;
166                                                 pDataArray->m->openOutputFile(pDataArray->barcodePrimerComboFileNames[i][j], temp);
167                                                 temp << pDataArray->maxFlows << endl;
168                                                 temp.close();
169                                         }
170                                 }                       
171                         }
172                 }
173                 
174                 FlowData flowData(pDataArray->numFlows, pDataArray->signal, pDataArray->noise, pDataArray->maxHomoP, pDataArray->flowOrder);
175                 cout << " thread flowdata address " <<  &flowData  << '\t' << &flowFile << endl;
176                 TrimOligos trimOligos(pDataArray->pdiffs, pDataArray->bdiffs, pDataArray->primers, pDataArray->barcodes, pDataArray->revPrimer);
177                 
178                 pDataArray->count = pDataArray->end;
179                 cout << pDataArray->threadID << '\t' << pDataArray->count << endl;
180                 int count = 0;
181                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
182                         
183                         if (pDataArray->m->control_pressed) {  break; }
184                         cout << pDataArray->threadID << '\t' << count << endl;
185                         int success = 1;
186                         int currentSeqDiffs = 0;
187                         string trashCode = "";
188                                 
189                         flowData.getNext(flowFile);
190                         cout << "thread good bit " << flowFile.good() << endl;
191                         flowData.capFlows(pDataArray->maxFlows);        
192                         
193                         Sequence currSeq = flowData.getSequence();
194                         if(!flowData.hasMinFlows(pDataArray->minFlows)){        //screen to see if sequence is of a minimum number of flows
195                                 success = 0;
196                                 trashCode += 'l';
197                         }
198                         
199                         int primerIndex = 0;
200                         int barcodeIndex = 0;
201                         
202                         if(pDataArray->barcodes.size() != 0){
203                                 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
204                                 if(success > pDataArray->bdiffs)                {       trashCode += 'b';       }
205                                 else{ currentSeqDiffs += success;  }
206                         }
207                         
208                         if(pDataArray->primers.size() != 0){
209                                 success = trimOligos.stripForward(currSeq, primerIndex);
210                                 if(success > pDataArray->pdiffs)                {       trashCode += 'f';       }
211                                 else{ currentSeqDiffs += success;  }
212                         }
213                         
214                         if (currentSeqDiffs > pDataArray->tdiffs)       {       trashCode += 't';   }
215                         
216                         if(pDataArray->revPrimer.size() != 0){
217                                 success = trimOligos.stripReverse(currSeq);
218                                 if(!success)                            {       trashCode += 'r';       }
219                         }
220                         
221                         if(trashCode.length() == 0){
222                                 
223                                 flowData.printFlows(trimFlowFile);
224                                 
225                                 if(pDataArray->fasta)   {       currSeq.printSequence(fastaFile);       }
226                                 
227                                 if(pDataArray->allFiles){
228                                         ofstream output;
229                                         pDataArray->m->openOutputFileAppend(pDataArray->barcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
230                                         output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
231                                         
232                                         flowData.printFlows(output);
233                                         output.close();
234                                 }                               
235                         }
236                         else{
237                                 flowData.printFlows(scrapFlowFile, trashCode);
238                         }
239                         
240                         count++;
241                                 cout << pDataArray->threadID << '\t' << currSeq.getName() << endl;              
242                         //report progress
243                         if((count) % 10000 == 0){       pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine();           }
244                         
245                 }
246                 //report progress
247                 if((count) % 10000 != 0){       pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine();           }
248                 
249                 trimFlowFile.close();
250                 scrapFlowFile.close();
251                 flowFile.close();
252                 if(pDataArray->fasta){  fastaFile.close();      }
253                 
254         }
255         catch(exception& e) {
256                 pDataArray->m->errorOut(e, "TrimFlowsCommand", "MyTrimFlowsThreadFunction");
257                 exit(1);
258         }
259
260 #endif
261
262
263 #endif