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