]> git.donarmstrong.com Git - mothur.git/blob - trimflowscommand.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / trimflowscommand.cpp
1 /*
2  *  trimflowscommand.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 12/22/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "trimflowscommand.h"
11 #include "needlemanoverlap.hpp"
12
13
14 //**********************************************************************************************************************
15 vector<string> TrimFlowsCommand::setParameters(){       
16         try {
17                 CommandParameter pflow("flow", "InputTypes", "", "", "none", "none", "none","flow-file",false,true,true); parameters.push_back(pflow);
18                 CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none","",false,false,true); parameters.push_back(poligos);
19                 CommandParameter pmaxhomop("maxhomop", "Number", "", "9", "", "", "","",false,false); parameters.push_back(pmaxhomop);
20                 CommandParameter pmaxflows("maxflows", "Number", "", "450", "", "", "","",false,false); parameters.push_back(pmaxflows);
21                 CommandParameter pminflows("minflows", "Number", "", "450", "", "", "","",false,false); parameters.push_back(pminflows);
22                 CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "","",false,false,true); parameters.push_back(ppdiffs);
23                 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "","",false,false,true); parameters.push_back(pbdiffs);
24         CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pldiffs);
25                 CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(psdiffs);
26         CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ptdiffs);
27                 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
28                 CommandParameter psignal("signal", "Number", "", "0.50", "", "", "","",false,false); parameters.push_back(psignal);
29                 CommandParameter pnoise("noise", "Number", "", "0.70", "", "", "","",false,false); parameters.push_back(pnoise);
30                 CommandParameter pallfiles("allfiles", "Boolean", "", "t", "", "", "","",false,false); parameters.push_back(pallfiles);
31         CommandParameter porder("order", "Multiple", "A-B", "A", "", "", "","",false,false, true); parameters.push_back(porder);
32                 CommandParameter pfasta("fasta", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pfasta);
33                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
34                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
35                 
36                 vector<string> myArray;
37                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
38                 return myArray;
39         }
40         catch(exception& e) {
41                 m->errorOut(e, "TrimFlowsCommand", "setParameters");
42                 exit(1);
43         }
44 }
45 //**********************************************************************************************************************
46 string TrimFlowsCommand::getHelpString(){       
47         try {
48                 string helpString = "";
49                 helpString += "The trim.flows command reads a flowgram file and creates .....\n";
50                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
51                 helpString += "For more details please check out the wiki http://www.mothur.org/wiki/Trim.flows.\n";
52                 return helpString;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "TrimFlowsCommand", "getHelpString");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60 string TrimFlowsCommand::getOutputPattern(string type) {
61     try {
62         string pattern = "";
63         
64         if (type == "flow") {  pattern = "[filename],[tag],flow"; } 
65         else if (type == "fasta") {  pattern = "[filename],flow.fasta"; } 
66         else if (type == "file") {  pattern = "[filename],flow.files"; }
67         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
68         
69         return pattern;
70     }
71     catch(exception& e) {
72         m->errorOut(e, "TrimFlowsCommand", "getOutputPattern");
73         exit(1);
74     }
75 }
76 //**********************************************************************************************************************
77
78 TrimFlowsCommand::TrimFlowsCommand(){   
79         try {
80                 abort = true; calledHelp = true; 
81                 setParameters();
82                 vector<string> tempOutNames;
83                 outputTypes["flow"] = tempOutNames;
84                 outputTypes["fasta"] = tempOutNames;
85         outputTypes["file"] = tempOutNames;
86         }
87         catch(exception& e) {
88                 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
89                 exit(1);
90         }
91 }
92 //**********************************************************************************************************************
93
94 TrimFlowsCommand::TrimFlowsCommand(string option)  {
95         try {
96                 
97                 abort = false; calledHelp = false;   
98                 comboStarts = 0;
99                 
100                 //allow user to run help
101                 if(option == "help") { help(); abort = true; calledHelp = true; }
102                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
103                 
104                 else {
105                                                 
106                         vector<string> myArray = setParameters();
107                         
108                         OptionParser parser(option);
109                         map<string,string> parameters = parser.getParameters();
110                         
111                         ValidParameters validParameter;
112                         map<string,string>::iterator it;
113                         
114                         //check to make sure all parameters are valid for command
115                         for (it = parameters.begin(); it != parameters.end(); it++) { 
116                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
117                         }
118                         
119                         //initialize outputTypes
120                         vector<string> tempOutNames;
121                         outputTypes["flow"] = tempOutNames;
122                         outputTypes["fasta"] = tempOutNames;
123             outputTypes["file"] = tempOutNames;
124                         
125                         //if the user changes the input directory command factory will send this info to us in the output parameter 
126                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
127                         if (inputDir == "not found"){   inputDir = "";          }
128                         else {
129                                 string path;
130                                 it = parameters.find("flow");
131                                 //user has given a template file
132                                 if(it != parameters.end()){ 
133                                         path = m->hasPath(it->second);
134                                         //if the user has not given a path then, add inputdir. else leave path alone.
135                                         if (path == "") {       parameters["flow"] = inputDir + it->second;             }
136                                 }
137                                 
138                                 it = parameters.find("oligos");
139                                 //user has given a template file
140                                 if(it != parameters.end()){ 
141                                         path = m->hasPath(it->second);
142                                         //if the user has not given a path then, add inputdir. else leave path alone.
143                                         if (path == "") {       parameters["oligos"] = inputDir + it->second;           }
144                                 }
145                                 
146                         }
147                         
148                         
149                         //check for required parameters
150                         flowFileName = validParameter.validFile(parameters, "flow", true);
151                         if (flowFileName == "not found") { 
152                                 flowFileName = m->getFlowFile(); 
153                                 if (flowFileName != "") {  m->mothurOut("Using " + flowFileName + " as input file for the flow parameter."); m->mothurOutEndLine(); }
154                                 else { 
155                                         m->mothurOut("No valid current flow file. You must provide a flow file."); m->mothurOutEndLine(); 
156                                         abort = true;
157                                 } 
158                         }else if (flowFileName == "not open") { flowFileName = ""; abort = true; }      
159                         
160                         //if the user changes the output directory command factory will send this info to us in the output parameter 
161                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
162                                 outputDir = ""; 
163                                 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it    
164                         }
165                         
166                         
167                         //check for optional parameter and set defaults
168                         // ...at some point should added some additional type checking...
169                         
170                         string temp;
171                         temp = validParameter.validFile(parameters, "minflows", false); if (temp == "not found") { temp = "450"; }
172                         m->mothurConvert(temp, minFlows);  
173
174                         temp = validParameter.validFile(parameters, "maxflows", false); if (temp == "not found") { temp = "450"; }
175                         m->mothurConvert(temp, maxFlows);  
176                         
177                         
178                         temp = validParameter.validFile(parameters, "oligos", true);
179                         if (temp == "not found")        {       oligoFileName = "";             }
180                         else if(temp == "not open")     {       abort = true;                   } 
181                         else                                            {       oligoFileName = temp;   m->setOligosFile(oligoFileName); }
182                         
183                         temp = validParameter.validFile(parameters, "fasta", false);            if (temp == "not found"){       fasta = 0;              }
184                         else if(m->isTrue(temp))        {       fasta = 1;      }
185                         
186                         temp = validParameter.validFile(parameters, "maxhomop", false);         if (temp == "not found"){       temp = "9";             }
187                         m->mothurConvert(temp, maxHomoP);  
188
189                         temp = validParameter.validFile(parameters, "signal", false);           if (temp == "not found"){       temp = "0.50";  }
190                         m->mothurConvert(temp, signal);  
191
192                         temp = validParameter.validFile(parameters, "noise", false);            if (temp == "not found"){       temp = "0.70";  }
193                         m->mothurConvert(temp, noise);  
194         
195                         temp = validParameter.validFile(parameters, "bdiffs", false);           if (temp == "not found"){       temp = "0";             }
196                         m->mothurConvert(temp, bdiffs);
197                         
198                         temp = validParameter.validFile(parameters, "pdiffs", false);           if (temp == "not found"){       temp = "0";             }
199                         m->mothurConvert(temp, pdiffs);
200                         
201             temp = validParameter.validFile(parameters, "ldiffs", false);               if (temp == "not found") { temp = "0"; }
202                         m->mothurConvert(temp, ldiffs);
203             
204             temp = validParameter.validFile(parameters, "sdiffs", false);               if (temp == "not found") { temp = "0"; }
205                         m->mothurConvert(temp, sdiffs);
206                         
207                         temp = validParameter.validFile(parameters, "tdiffs", false);           if (temp == "not found") { int tempTotal = pdiffs + bdiffs + ldiffs + sdiffs;  temp = toString(tempTotal); }
208                         m->mothurConvert(temp, tdiffs);
209                         
210                         if(tdiffs == 0){        tdiffs = bdiffs + pdiffs + ldiffs + sdiffs;     }
211
212                         
213                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
214                         m->setProcessors(temp);
215                         m->mothurConvert(temp, processors);
216         
217                         temp = validParameter.validFile(parameters, "order", false);  if (temp == "not found"){         temp = "A";     }
218             if (temp.length() > 1) {  m->mothurOut("[ERROR]: " + temp + " is not a valid option for order. order options are A or B. A = TACG and B = TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC.\n");  abort=true;
219             }
220             else {
221                 if (toupper(temp[0]) == 'A') {  flowOrder = "TACG";   }
222                 else if(toupper(temp[0]) == 'B'){ 
223                     flowOrder = "TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC";   }
224                 else {
225                     m->mothurOut("[ERROR]: " + temp + " is not a valid option for order. order options are A or B. A = TACG and B = TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC.\n");  abort=true;
226                 }
227             }
228             
229                         if(oligoFileName == "") {       allFiles = 0;           }
230                         else                                    {       allFiles = 1;           }
231
232                         numFPrimers = 0;
233                         numRPrimers = 0;
234             numLinkers = 0;
235             numSpacers = 0;
236                 }
237         }
238         catch(exception& e) {
239                 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
240                 exit(1);
241         }
242 }
243
244 //***************************************************************************************************************
245
246 int TrimFlowsCommand::execute(){
247         try{
248                 
249                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
250
251         map<string, string> variables; 
252                 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(flowFileName));
253         string fastaFileName = getOutputFileName("fasta",variables);
254                 if(fasta){ outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName); }
255         
256         variables["[tag]"] = "trim";
257                 string trimFlowFileName = getOutputFileName("flow",variables);
258                 outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
259                 
260         variables["[tag]"] = "scrap";
261                 string scrapFlowFileName = getOutputFileName("flow",variables);
262                 outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
263
264                 
265                 
266                 vector<unsigned long long> flowFilePos;
267         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
268                 flowFilePos = getFlowFileBreaks();
269                 for (int i = 0; i < (flowFilePos.size()-1); i++) {
270                         lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
271                 }       
272         #else
273                 ifstream in; m->openInputFile(flowFileName, in); in >> numFlows; in.close();
274         ///////////////////////////////////////// until I fix multiple processors for windows //////////////////        
275                 processors = 1;
276         ///////////////////////////////////////// until I fix multiple processors for windows //////////////////                
277                 if (processors == 1) {
278                         lines.push_back(new linePair(0, 1000));
279                 }else {
280                         int numFlowLines;
281                         flowFilePos = m->setFilePosEachLine(flowFileName, numFlowLines);
282                         flowFilePos.erase(flowFilePos.begin() + 1); numFlowLines--;
283                         
284                         //figure out how many sequences you have to process
285                         int numSeqsPerProcessor = numFlowLines / processors;
286                         cout << numSeqsPerProcessor << '\t' << numFlowLines << endl;
287                         for (int i = 0; i < processors; i++) {
288                                 int startIndex =  i * numSeqsPerProcessor;
289                                 if(i == (processors - 1)){      numSeqsPerProcessor = numFlowLines - i * numSeqsPerProcessor;   }
290                                 lines.push_back(new linePair(flowFilePos[startIndex], numSeqsPerProcessor));
291                                 cout << flowFilePos[startIndex] << '\t' << numSeqsPerProcessor << endl;
292                         }
293                 }
294         #endif
295                 
296                 vector<vector<string> > barcodePrimerComboFileNames;
297                 if(oligoFileName != ""){
298                         getOligos(barcodePrimerComboFileNames); 
299                 }
300                 
301                 if(processors == 1){
302                         driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
303                 }else{
304                         createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames); 
305                 }       
306                 
307                 if (m->control_pressed) {  return 0; }                  
308                 
309                 string flowFilesFileName;
310                 ofstream output;
311                 
312                 if(allFiles){
313                         set<string> namesAlreadyProcessed;
314                         flowFilesFileName = getOutputFileName("file",variables);
315                         m->openOutputFile(flowFilesFileName, output);
316
317                         for(int i=0;i<barcodePrimerComboFileNames.size();i++){
318                                 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
319                                         if (namesAlreadyProcessed.count(barcodePrimerComboFileNames[i][j]) == 0) {
320                         if (barcodePrimerComboFileNames[i][j] != "") {
321                             FILE * pFile;
322                             unsigned long long size;
323                             
324                             //get num bytes in file
325                             pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
326                             if (pFile==NULL) perror ("Error opening file");
327                             else{
328                                 fseek (pFile, 0, SEEK_END);
329                                 size=ftell(pFile);
330                                 fclose (pFile);
331                             }
332                             
333                             if(size < 10){
334                                 m->mothurRemove(barcodePrimerComboFileNames[i][j]);
335                             }
336                             else{
337                                 output << m->getFullPathName(barcodePrimerComboFileNames[i][j]) << endl;
338                                 outputNames.push_back(barcodePrimerComboFileNames[i][j]);
339                                 outputTypes["flow"].push_back(barcodePrimerComboFileNames[i][j]);
340                             }
341                             namesAlreadyProcessed.insert(barcodePrimerComboFileNames[i][j]);
342                         }
343                                         }
344                                 }
345                         }
346                         output.close();
347                 }
348                 else{
349                         flowFilesFileName = getOutputFileName("file",variables);
350                         m->openOutputFile(flowFilesFileName, output);
351                         
352                         output << m->getFullPathName(trimFlowFileName) << endl;
353                         
354                         output.close();
355                 }
356                 outputTypes["file"].push_back(flowFilesFileName);
357                 outputNames.push_back(flowFilesFileName);
358                         
359                 m->mothurOutEndLine();
360                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
361                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
362                 m->mothurOutEndLine();
363                 
364                 return 0;       
365         }
366         catch(exception& e) {
367                 m->errorOut(e, "TrimSeqsCommand", "execute");
368                 exit(1);
369         }
370 }
371
372 //***************************************************************************************************************
373
374 int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > thisBarcodePrimerComboFileNames, linePair* line){
375         
376         try {
377                 ofstream trimFlowFile;
378                 m->openOutputFile(trimFlowFileName, trimFlowFile);
379                 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
380
381                 ofstream scrapFlowFile;
382                 m->openOutputFile(scrapFlowFileName, scrapFlowFile);
383                 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
384                 
385                 ofstream fastaFile;
386                 if(fasta){      m->openOutputFile(fastaFileName, fastaFile);    }
387                 
388                 ifstream flowFile;
389                 m->openInputFile(flowFileName, flowFile);
390                 
391                 flowFile.seekg(line->start);
392                 
393                 if(line->start == 0){
394                         flowFile >> numFlows; m->gobble(flowFile);
395                         scrapFlowFile << maxFlows << endl;
396                         trimFlowFile << maxFlows << endl;
397                         if(allFiles){
398                                 for(int i=0;i<thisBarcodePrimerComboFileNames.size();i++){
399                                         for(int j=0;j<thisBarcodePrimerComboFileNames[0].size();j++){
400                         if (thisBarcodePrimerComboFileNames[i][j] != "") {
401                             ofstream temp;
402                             m->openOutputFile(thisBarcodePrimerComboFileNames[i][j], temp);
403                             temp << maxFlows << endl;
404                             temp.close();
405                         }
406                                         }
407                                 }                       
408                         }
409                 }
410                 
411                 FlowData flowData(numFlows, signal, noise, maxHomoP, flowOrder);
412                 //cout << " driver flowdata address " <<  &flowData  << &flowFile << endl;      
413                 int count = 0;
414                 bool moreSeqs = 1;
415                 
416                 TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, revPrimer, linker, spacer);
417                 
418                 while(moreSeqs) {
419                                 
420                         if (m->control_pressed) { break; }
421                         
422                         int success = 1;
423                         int currentSeqDiffs = 0;
424                         string trashCode = "";
425                         
426                         flowData.getNext(flowFile); 
427                         flowData.capFlows(maxFlows);    
428                         
429                         Sequence currSeq = flowData.getSequence();
430                         if(!flowData.hasMinFlows(minFlows)){    //screen to see if sequence is of a minimum number of flows
431                                 success = 0;
432                                 trashCode += 'l';
433                         }
434                         
435                         int primerIndex = 0;
436                         int barcodeIndex = 0;
437                         
438             if(numLinkers != 0){
439                 success = trimOligos.stripLinker(currSeq);
440                 if(success > ldiffs)            {       trashCode += 'k';       }
441                 else{ currentSeqDiffs += success;  }
442                 
443             }
444             
445             if (m->debug) { m->mothurOut("[DEBUG]: " + currSeq.getName() + " " + currSeq.getUnaligned() + "\n"); }
446             
447                         if(barcodes.size() != 0){
448                                 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
449                                 if(success > bdiffs)            {       trashCode += 'b';       }
450                                 else{ currentSeqDiffs += success;  }
451                         }
452                         
453             if(numSpacers != 0){
454                 success = trimOligos.stripSpacer(currSeq);
455                 if(success > sdiffs)            {       trashCode += 's';       }
456                 else{ currentSeqDiffs += success;  }
457                 
458             }
459             
460                         if(numFPrimers != 0){
461                                 success = trimOligos.stripForward(currSeq, primerIndex);
462                                 if(success > pdiffs)            {       trashCode += 'f';       }
463                                 else{ currentSeqDiffs += success;  }
464                         }
465                         
466                         if (currentSeqDiffs > tdiffs)   {       trashCode += 't';   }
467                         
468                         if(numRPrimers != 0){
469                                 success = trimOligos.stripReverse(currSeq);
470                                 if(!success)                            {       trashCode += 'r';       }
471                         }
472                         
473                         if(trashCode.length() == 0){
474                 string thisGroup = "";
475                 if(barcodes.size() != 0){
476                     thisGroup = barcodeNameVector[barcodeIndex];
477                     if (primers.size() != 0) { 
478                         if (primerNameVector[primerIndex] != "") { 
479                             if(thisGroup != "") {
480                                 thisGroup += "." + primerNameVector[primerIndex]; 
481                             }else {
482                                 thisGroup = primerNameVector[primerIndex]; 
483                             }
484                         } 
485                     }
486                 }
487                 
488                 int pos = thisGroup.find("ignore");
489                 if (pos == string::npos) {              
490                     flowData.printFlows(trimFlowFile);
491                     
492                     if(fasta)   {       currSeq.printSequence(fastaFile);       }
493                     
494                     if(allFiles){
495                         ofstream output;
496                         m->openOutputFileAppend(thisBarcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
497                         output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
498                         
499                         flowData.printFlows(output);
500                         output.close();
501                     }
502                 }
503                         }
504                         else{
505                                 flowData.printFlows(scrapFlowFile, trashCode);
506                         }
507                                 
508                         count++;
509                         //cout << "driver" << '\t' << currSeq.getName() << endl;                        
510                         //report progress
511                         if((count) % 10000 == 0){       m->mothurOut(toString(count)); m->mothurOutEndLine();           }
512
513 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
514                         unsigned long long pos = flowFile.tellg();
515
516                         if ((pos == -1) || (pos >= line->end)) { break; }
517 #else
518                         if (flowFile.eof()) { break; }
519 #endif
520                         
521                 }
522                 //report progress
523                 if((count) % 10000 != 0){       m->mothurOut(toString(count)); m->mothurOutEndLine();           }
524                 
525                 trimFlowFile.close();
526                 scrapFlowFile.close();
527                 flowFile.close();
528                 if(fasta){      fastaFile.close();      }
529                 
530                 return count;
531         }
532         catch(exception& e) {
533                 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
534                 exit(1);
535         }
536 }
537
538 //***************************************************************************************************************
539
540 void TrimFlowsCommand::getOligos(vector<vector<string> >& outFlowFileNames){
541         try {
542                 ifstream oligosFile;
543                 m->openInputFile(oligoFileName, oligosFile);
544                 
545                 string type, oligo, group;
546
547                 int indexPrimer = 0;
548                 int indexBarcode = 0;
549                 
550                 while(!oligosFile.eof()){
551                 
552                         oligosFile >> type; m->gobble(oligosFile);      //get the first column value of the row - is it a comment or a feature we are interested in?
553
554                         if(type[0] == '#'){     //igore the line because there's a comment
555                                 while (!oligosFile.eof())       {       char c = oligosFile.get(); if (c == 10 || c == 13){     break;  }       } // get rest of line if there's any crap there
556                         }
557                         else{                           //there's a feature we're interested in
558
559                                 for(int i=0;i<type.length();i++){       type[i] = toupper(type[i]);  }                                  //make type case insensitive
560
561                                 oligosFile >> oligo;    //get the DNA sequence for the feature
562
563                                 for(int i=0;i<oligo.length();i++){      //make type case insensitive and change any U's to T's
564                                         oligo[i] = toupper(oligo[i]);
565                                         if(oligo[i] == 'U')     {       oligo[i] = 'T'; }
566                                 }
567
568                                 if(type == "FORWARD"){  //if the feature is a forward primer...
569                                         group = "";
570
571                                         while (!oligosFile.eof())       {       // get rest of line in case there is a primer name = will have the name of the primer
572                                                 char c = oligosFile.get(); 
573                                                 if (c == 10 || c == 13){        break;  }
574                                                 else if (c == 32 || c == 9){;} //space or tab
575                                                 else {  group += c;  }
576                                         } 
577
578                                         //have we seen this primer already?
579                                         map<string, int>::iterator itPrimer = primers.find(oligo);
580                                         if (itPrimer != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine();  }
581
582                                         primers[oligo]=indexPrimer; indexPrimer++;
583                                         primerNameVector.push_back(group);
584
585                                 }
586                                 else if(type == "REVERSE"){
587                                         string oligoRC = reverseOligo(oligo);
588                                         revPrimer.push_back(oligoRC);
589                                 }
590                                 else if(type == "BARCODE"){
591                                         oligosFile >> group;
592
593                                         //check for repeat barcodes
594                                         map<string, int>::iterator itBar = barcodes.find(oligo);
595                                         if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine();  }
596
597                                         barcodes[oligo]=indexBarcode; indexBarcode++;
598                                         barcodeNameVector.push_back(group);
599                                 }else if(type == "LINKER"){
600                                         linker.push_back(oligo);
601                                 }else if(type == "SPACER"){
602                                         spacer.push_back(oligo);
603                                 }
604                                 else{
605                                         m->mothurOut(type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine();  
606                                 }
607                         }
608
609                         m->gobble(oligosFile);
610                 }
611                 oligosFile.close();
612                 
613                 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0;   }
614                 
615                 //add in potential combos
616                 if(barcodeNameVector.size() == 0){
617                         barcodes[""] = 0;
618                         barcodeNameVector.push_back("");                        
619                 }
620                 
621                 if(primerNameVector.size() == 0){
622                         primers[""] = 0;
623                         primerNameVector.push_back("");                 
624                 }
625                 
626                 
627                 outFlowFileNames.resize(barcodeNameVector.size());
628                 for(int i=0;i<outFlowFileNames.size();i++){
629                         outFlowFileNames[i].assign(primerNameVector.size(), "");
630                 }
631                 
632                 if(allFiles){
633
634                         for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
635                                 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
636
637                                         string primerName = primerNameVector[itPrimer->second];
638                                         string barcodeName = barcodeNameVector[itBar->second];
639                     
640                                         if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing 
641                                         else {                                  
642                         string comboGroupName = "";
643                         string fileName = "";
644                         
645                         map<string, string> variables; 
646                         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(flowFileName));
647                         
648                         if(primerName == ""){
649                             comboGroupName = barcodeNameVector[itBar->second];
650                             variables["[tag]"] = comboGroupName;
651                             fileName = getOutputFileName("flow", variables);
652                         }
653                         else{
654                             if(barcodeName == ""){
655                                 comboGroupName = primerNameVector[itPrimer->second];
656                             }
657                             else{
658                                 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
659                             }
660                             variables["[tag]"] = comboGroupName;
661                             fileName = getOutputFileName("flow", variables);
662                         }
663                         
664                         outFlowFileNames[itBar->second][itPrimer->second] = fileName;
665                         
666                         ofstream temp;
667                         m->openOutputFile(fileName, temp);
668                         temp.close();
669                     }
670                                 }
671                         }
672                 }
673                 
674                 numFPrimers = primers.size();
675                 numRPrimers = revPrimer.size();
676         numLinkers = linker.size();
677         numSpacers = spacer.size();
678                 
679         }
680         catch(exception& e) {
681                 m->errorOut(e, "TrimSeqsCommand", "getOligos");
682                 exit(1);
683         }
684 }
685 //********************************************************************/
686 string TrimFlowsCommand::reverseOligo(string oligo){
687         try {
688         string reverse = "";
689         
690         for(int i=oligo.length()-1;i>=0;i--){
691             
692             if(oligo[i] == 'A')         {       reverse += 'T'; }
693             else if(oligo[i] == 'T'){   reverse += 'A'; }
694             else if(oligo[i] == 'U'){   reverse += 'A'; }
695             
696             else if(oligo[i] == 'G'){   reverse += 'C'; }
697             else if(oligo[i] == 'C'){   reverse += 'G'; }
698             
699             else if(oligo[i] == 'R'){   reverse += 'Y'; }
700             else if(oligo[i] == 'Y'){   reverse += 'R'; }
701             
702             else if(oligo[i] == 'M'){   reverse += 'K'; }
703             else if(oligo[i] == 'K'){   reverse += 'M'; }
704             
705             else if(oligo[i] == 'W'){   reverse += 'W'; }
706             else if(oligo[i] == 'S'){   reverse += 'S'; }
707             
708             else if(oligo[i] == 'B'){   reverse += 'V'; }
709             else if(oligo[i] == 'V'){   reverse += 'B'; }
710             
711             else if(oligo[i] == 'D'){   reverse += 'H'; }
712             else if(oligo[i] == 'H'){   reverse += 'D'; }
713             
714             else                                                {       reverse += 'N'; }
715         }
716         
717         
718         return reverse;
719     }
720         catch(exception& e) {
721                 m->errorOut(e, "TrimFlowsCommand", "reverseOligo");
722                 exit(1);
723         }
724 }
725
726 /**************************************************************************************************/
727 vector<unsigned long long> TrimFlowsCommand::getFlowFileBreaks() {
728
729         try{
730                         
731                 vector<unsigned long long> filePos;
732                 filePos.push_back(0);
733                                         
734                 FILE * pFile;
735                 unsigned long long size;
736                 
737                 //get num bytes in file
738                 pFile = fopen (flowFileName.c_str(),"rb");
739                 if (pFile==NULL) perror ("Error opening file");
740                 else{
741                         fseek (pFile, 0, SEEK_END);
742                         size=ftell (pFile);
743                         fclose (pFile);
744                 }
745                                 
746                 //estimate file breaks
747                 unsigned long long chunkSize = 0;
748                 chunkSize = size / processors;
749
750                 //file too small to divide by processors
751                 if (chunkSize == 0)  {  processors = 1; filePos.push_back(size); return filePos;        }
752                 
753                 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
754                 for (int i = 0; i < processors; i++) {
755                         unsigned long long spot = (i+1) * chunkSize;
756                         
757                         ifstream in;
758                         m->openInputFile(flowFileName, in);
759                         in.seekg(spot);
760                         
761                         string dummy = m->getline(in);
762                         
763                         //there was not another sequence before the end of the file
764                         unsigned long long sanityPos = in.tellg();
765                         
766 //                      if (sanityPos == -1) {  break;  }
767 //                      else {  filePos.push_back(newSpot);  }
768                         if (sanityPos == -1) {  break;  }
769                         else {  filePos.push_back(sanityPos);  }
770                         
771                         in.close();
772                 }
773                 
774                 //save end pos
775                 filePos.push_back(size);
776                 
777                 //sanity check filePos
778                 for (int i = 0; i < (filePos.size()-1); i++) {
779                         if (filePos[(i+1)] <= filePos[i]) {  filePos.erase(filePos.begin()+(i+1)); i--; }
780                 }
781
782                 ifstream in;
783                 m->openInputFile(flowFileName, in);
784                 in >> numFlows;
785                 m->gobble(in);
786                 //unsigned long long spot = in.tellg();
787                 //filePos[0] = spot;
788                 in.close();
789                 
790                 processors = (filePos.size() - 1);
791                 
792                 return filePos; 
793         }
794         catch(exception& e) {
795                 m->errorOut(e, "TrimSeqsCommand", "getFlowFileBreaks");
796                 exit(1);
797         }
798 }
799
800 /**************************************************************************************************/
801
802 int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
803
804         try {
805                 processIDS.clear();
806                 int exitCommand = 1;
807                 
808 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
809                 int process = 1;
810                 
811                 //loop through and create all the processes you want
812                 while (process != processors) {
813                         int pid = fork();
814                         
815                         if (pid > 0) {
816                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
817                                 process++;
818                         }else if (pid == 0){
819                                 
820                                 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
821                                 if(allFiles){
822                                         for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
823                                                 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
824                             if (tempBarcodePrimerComboFileNames[i][j] != "") {
825                                 tempBarcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
826                                 ofstream temp;
827                                 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
828                                 temp.close();
829                             }
830                                                 }
831                                         }
832                                 }
833                                 driverCreateTrim(flowFileName,
834                                                                  (trimFlowFileName + toString(getpid()) + ".temp"),
835                                                                  (scrapFlowFileName + toString(getpid()) + ".temp"),
836                                                                  (fastaFileName + toString(getpid()) + ".temp"),
837                                                                  tempBarcodePrimerComboFileNames, lines[process]);
838
839                                 exit(0);
840                         }else { 
841                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
842                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
843                                 exit(0);
844                         }
845                 }
846                 
847                 //parent do my part
848                 ofstream temp;
849                 m->openOutputFile(trimFlowFileName, temp);
850                 temp.close();
851
852                 m->openOutputFile(scrapFlowFileName, temp);
853                 temp.close();
854                 
855                 if(fasta){
856                         m->openOutputFile(fastaFileName, temp);
857                         temp.close();
858                 }
859                 
860                 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
861
862                 //force parent to wait until all the processes are done
863                 for (int i=0;i<processIDS.size();i++) { 
864                         int temp = processIDS[i];
865                         wait(&temp);
866                 }
867 #else
868                 //////////////////////////////////////////////////////////////////////////////////////////////////////
869                 //Windows version shared memory, so be careful when passing variables through the trimFlowData struct. 
870                 //Above fork() will clone, so memory is separate, but that's not the case with windows, 
871                 //////////////////////////////////////////////////////////////////////////////////////////////////////
872                 
873                 vector<trimFlowData*> pDataArray; 
874                 DWORD   dwThreadIdArray[processors-1];
875                 HANDLE  hThreadArray[processors-1]; 
876                 
877                 //Create processor worker threads.
878                 for( int i=0; i<processors-1; i++ ){
879                         // Allocate memory for thread data.
880                         string extension = "";
881                         if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
882                         
883                         vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
884                         if(allFiles){
885                                 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
886                                         for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
887                         if (tempBarcodePrimerComboFileNames[i][j] != "") {
888                             tempBarcodePrimerComboFileNames[i][j] += extension;
889                             ofstream temp;
890                             m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
891                             temp.close();
892                                                 }
893                                         }
894                                 }
895                         }
896                         
897                         trimFlowData* tempflow = new trimFlowData(flowFileName, (trimFlowFileName + extension), (scrapFlowFileName + extension), fastaFileName, flowOrder, tempBarcodePrimerComboFileNames, barcodes, primers, revPrimer, fasta, allFiles, lines[i]->start, lines[i]->end, m, signal, noise, numFlows, maxFlows, minFlows, maxHomoP, tdiffs, bdiffs, pdiffs, i);
898                         pDataArray.push_back(tempflow);
899                         
900                         //MyTrimFlowThreadFunction is in header. It must be global or static to work with the threads.
901                         //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
902                         hThreadArray[i] = CreateThread(NULL, 0, MyTrimFlowThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);   
903                 }
904                 
905                 //using the main process as a worker saves time and memory
906                 ofstream temp;
907                 m->openOutputFile(trimFlowFileName, temp);
908                 temp.close();
909                 
910                 m->openOutputFile(scrapFlowFileName, temp);
911                 temp.close();
912                 
913                 if(fasta){
914                         m->openOutputFile(fastaFileName, temp);
915                         temp.close();
916                 }
917                 
918                 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
919                 if(allFiles){
920                         for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
921                                 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
922                     if (tempBarcodePrimerComboFileNames[i][j] != "") {
923                         tempBarcodePrimerComboFileNames[i][j] += toString(processors-1) + ".temp";
924                         ofstream temp;
925                         m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
926                         temp.close();
927                     }
928                                         
929                                 }
930                         }
931                 }
932                 
933                 //do my part - do last piece because windows is looking for eof
934                 int num = driverCreateTrim(flowFileName, (trimFlowFileName  + toString(processors-1) + ".temp"), (scrapFlowFileName  + toString(processors-1) + ".temp"), (fastaFileName + toString(processors-1) + ".temp"), tempBarcodePrimerComboFileNames, lines[processors-1]);
935                 processIDS.push_back((processors-1)); 
936                 
937                 //Wait until all threads have terminated.
938                 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
939                 
940                 //Close all thread handles and free memory allocations.
941                 for(int i=0; i < pDataArray.size(); i++){
942                         num += pDataArray[i]->count;
943                         CloseHandle(hThreadArray[i]);
944                         delete pDataArray[i];
945                 }
946                 
947                 
948 #endif  
949                 //append files
950                 m->mothurOutEndLine();
951                 for(int i=0;i<processIDS.size();i++){
952                         
953                         m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
954                         
955                         m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
956                         m->mothurRemove((trimFlowFileName + toString(processIDS[i]) + ".temp"));
957 //                      m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
958
959                         m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
960                         m->mothurRemove((scrapFlowFileName + toString(processIDS[i]) + ".temp"));
961 //                      m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
962
963                         if(fasta){
964                                 m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
965                                 m->mothurRemove((fastaFileName + toString(processIDS[i]) + ".temp"));
966 //                              m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
967                         }
968                         if(allFiles){                                           
969                                 for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
970                                         for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
971                         if (barcodePrimerComboFileNames[j][k] != "") {
972                             m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
973                             m->mothurRemove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"));
974                         }
975                                         }
976                                 }
977                         }
978                 }
979                 
980                 return exitCommand;
981         
982         }
983         catch(exception& e) {
984                 m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");
985                 exit(1);
986         }
987 }
988
989 //***************************************************************************************************************