]> git.donarmstrong.com Git - mothur.git/blob - trimflowscommand.h
testing 1.22.0
[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;
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
74         vector<string> primerNameVector;        //needed here?
75         vector<string> barcodeNameVector;       //needed here?
76
77         map<string, int> combos;                        //needed here?
78         map<string, int> groupToIndex;          //needed here?
79         
80 };
81
82 /**************************************************************************************************/
83 //custom data structure for threads to use.
84 // This is passed by void pointer so it can be any data type
85 // that can be passed using a single void pointer (LPVOID).
86 typedef struct trimFlowData {
87         string flowFileName; 
88         string trimFlowFileName; 
89         string scrapFlowFileName;
90         string fastaFileName;
91         string flowOrder;
92         vector<vector<string> > barcodePrimerComboFileNames;
93         map<string, int> barcodes;
94         map<string, int> primers;
95         vector<string> revPrimer;
96         bool fasta, allFiles;
97         unsigned long long start;
98         unsigned long long end;
99         MothurOut* m;
100         float signal, noise;
101         int numFlows, maxFlows, minFlows, maxHomoP, tdiffs, bdiffs, pdiffs, threadID, count;
102         
103         trimFlowData(){}
104         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) {
105                 flowFileName = ff;
106                 trimFlowFileName = tf;
107                 scrapFlowFileName = sf;
108                 fastaFileName = f;
109                 flowOrder = fo;
110                 barcodePrimerComboFileNames = bfn;
111                 barcodes = bar;
112                 primers = pri;
113                 revPrimer = rev;
114                 fasta = fa;
115                 allFiles = al;
116                 start = st;
117                 end = en;
118                 m = mout;
119                 signal = sig;
120                 noise = n;
121                 numFlows = numF;
122                 maxFlows = maxF;
123                 minFlows = minF;
124                 maxHomoP = maxH;
125                 tdiffs = td;
126                 bdiffs = bd;
127                 pdiffs = pd;
128                 threadID = tid;
129         }
130 };
131
132 /**************************************************************************************************/
133 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
134 #else
135 static DWORD WINAPI MyTrimFlowThreadFunction(LPVOID lpParam){ 
136         trimFlowData* pDataArray;
137         pDataArray = (trimFlowData*)lpParam;
138         
139         try {
140                 ofstream trimFlowFile;
141                 pDataArray->m->openOutputFile(pDataArray->trimFlowFileName, trimFlowFile);
142                 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
143                 
144                 ofstream scrapFlowFile;
145                 pDataArray->m->openOutputFile(pDataArray->scrapFlowFileName, scrapFlowFile);
146                 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
147                 
148                 ofstream fastaFile;
149                 if(pDataArray->fasta){  pDataArray->m->openOutputFile(pDataArray->fastaFileName, fastaFile);    }
150                 
151                 ifstream flowFile;
152                 pDataArray->m->openInputFile(pDataArray->flowFileName, flowFile);
153                 
154                 flowFile.seekg(pDataArray->start);
155                 
156                 if(pDataArray->start == 0){
157                         flowFile >> pDataArray->numFlows; pDataArray->m->gobble(flowFile);
158                         scrapFlowFile << pDataArray->maxFlows << endl;
159                         trimFlowFile << pDataArray->maxFlows << endl;
160                         if(pDataArray->allFiles){
161                                 for(int i=0;i<pDataArray->barcodePrimerComboFileNames.size();i++){
162                                         for(int j=0;j<pDataArray->barcodePrimerComboFileNames[0].size();j++){
163                                                 ofstream temp;
164                                                 pDataArray->m->openOutputFile(pDataArray->barcodePrimerComboFileNames[i][j], temp);
165                                                 temp << pDataArray->maxFlows << endl;
166                                                 temp.close();
167                                         }
168                                 }                       
169                         }
170                 }
171                 
172                 FlowData flowData(pDataArray->numFlows, pDataArray->signal, pDataArray->noise, pDataArray->maxHomoP, pDataArray->flowOrder);
173                 cout << " thread flowdata address " <<  &flowData  << '\t' << &flowFile << endl;
174                 TrimOligos trimOligos(pDataArray->pdiffs, pDataArray->bdiffs, pDataArray->primers, pDataArray->barcodes, pDataArray->revPrimer);
175                 
176                 pDataArray->count = pDataArray->end;
177                 cout << pDataArray->threadID << '\t' << pDataArray->count << endl;
178                 int count = 0;
179                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
180                         
181                         if (pDataArray->m->control_pressed) {  break; }
182                         cout << pDataArray->threadID << '\t' << count << endl;
183                         int success = 1;
184                         int currentSeqDiffs = 0;
185                         string trashCode = "";
186                                 
187                         flowData.getNext(flowFile);
188                         cout << "thread good bit " << flowFile.good() << endl;
189                         flowData.capFlows(pDataArray->maxFlows);        
190                         
191                         Sequence currSeq = flowData.getSequence();
192                         if(!flowData.hasMinFlows(pDataArray->minFlows)){        //screen to see if sequence is of a minimum number of flows
193                                 success = 0;
194                                 trashCode += 'l';
195                         }
196                         
197                         int primerIndex = 0;
198                         int barcodeIndex = 0;
199                         
200                         if(pDataArray->barcodes.size() != 0){
201                                 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
202                                 if(success > pDataArray->bdiffs)                {       trashCode += 'b';       }
203                                 else{ currentSeqDiffs += success;  }
204                         }
205                         
206                         if(pDataArray->primers.size() != 0){
207                                 success = trimOligos.stripForward(currSeq, primerIndex);
208                                 if(success > pDataArray->pdiffs)                {       trashCode += 'f';       }
209                                 else{ currentSeqDiffs += success;  }
210                         }
211                         
212                         if (currentSeqDiffs > pDataArray->tdiffs)       {       trashCode += 't';   }
213                         
214                         if(pDataArray->revPrimer.size() != 0){
215                                 success = trimOligos.stripReverse(currSeq);
216                                 if(!success)                            {       trashCode += 'r';       }
217                         }
218                         
219                         if(trashCode.length() == 0){
220                                 
221                                 flowData.printFlows(trimFlowFile);
222                                 
223                                 if(pDataArray->fasta)   {       currSeq.printSequence(fastaFile);       }
224                                 
225                                 if(pDataArray->allFiles){
226                                         ofstream output;
227                                         pDataArray->m->openOutputFileAppend(pDataArray->barcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
228                                         output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
229                                         
230                                         flowData.printFlows(output);
231                                         output.close();
232                                 }                               
233                         }
234                         else{
235                                 flowData.printFlows(scrapFlowFile, trashCode);
236                         }
237                         
238                         count++;
239                                 cout << pDataArray->threadID << '\t' << currSeq.getName() << endl;              
240                         //report progress
241                         if((count) % 10000 == 0){       pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine();           }
242                         
243                 }
244                 //report progress
245                 if((count) % 10000 != 0){       pDataArray->m->mothurOut(toString(count)); pDataArray->m->mothurOutEndLine();           }
246                 
247                 trimFlowFile.close();
248                 scrapFlowFile.close();
249                 flowFile.close();
250                 if(pDataArray->fasta){  fastaFile.close();      }
251                 
252         }
253         catch(exception& e) {
254                 pDataArray->m->errorOut(e, "TrimFlowsCommand", "MyTrimFlowsThreadFunction");
255                 exit(1);
256         }
257
258 #endif
259
260
261 #endif