]> git.donarmstrong.com Git - mothur.git/blob - trimflowscommand.cpp
added N code to flow data. removed changed for trimming of fast=T in trim.flows.
[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, B, or I. A = TACG, B = TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC, and I = TACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGC.\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 if(toupper(temp[0]) == 'I'){
225                     flowOrder = "TACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGC";   }
226                 else {
227                     m->mothurOut("[ERROR]: " + temp + " is not a valid option for order. order options are A, B, or I. A = TACG, B = TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC, and I = TACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGC.\n");  abort=true;
228                 }
229             }
230             
231                         if(oligoFileName == "") {       allFiles = 0;           }
232                         else                                    {       allFiles = 1;           }
233
234                         numFPrimers = 0;
235                         numRPrimers = 0;
236             numLinkers = 0;
237             numSpacers = 0;
238                 }
239         }
240         catch(exception& e) {
241                 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
242                 exit(1);
243         }
244 }
245
246 //***************************************************************************************************************
247
248 int TrimFlowsCommand::execute(){
249         try{
250                 
251                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
252
253         map<string, string> variables; 
254                 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(flowFileName));
255         string fastaFileName = getOutputFileName("fasta",variables);
256                 if(fasta){ outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName); }
257         
258         variables["[tag]"] = "trim";
259                 string trimFlowFileName = getOutputFileName("flow",variables);
260                 outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
261                 
262         variables["[tag]"] = "scrap";
263                 string scrapFlowFileName = getOutputFileName("flow",variables);
264                 outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
265
266                 
267                 
268                 vector<unsigned long long> flowFilePos;
269         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
270                 flowFilePos = getFlowFileBreaks();
271                 for (int i = 0; i < (flowFilePos.size()-1); i++) {
272                         lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
273                 }       
274         #else
275                 ifstream in; m->openInputFile(flowFileName, in); in >> numFlows; in.close();
276         ///////////////////////////////////////// until I fix multiple processors for windows //////////////////        
277                 processors = 1;
278         ///////////////////////////////////////// until I fix multiple processors for windows //////////////////                
279                 if (processors == 1) {
280                         lines.push_back(new linePair(0, 1000));
281                 }else {
282                         int numFlowLines;
283                         flowFilePos = m->setFilePosEachLine(flowFileName, numFlowLines);
284                         flowFilePos.erase(flowFilePos.begin() + 1); numFlowLines--;
285                         
286                         //figure out how many sequences you have to process
287                         int numSeqsPerProcessor = numFlowLines / processors;
288                         cout << numSeqsPerProcessor << '\t' << numFlowLines << endl;
289                         for (int i = 0; i < processors; i++) {
290                                 int startIndex =  i * numSeqsPerProcessor;
291                                 if(i == (processors - 1)){      numSeqsPerProcessor = numFlowLines - i * numSeqsPerProcessor;   }
292                                 lines.push_back(new linePair(flowFilePos[startIndex], numSeqsPerProcessor));
293                                 cout << flowFilePos[startIndex] << '\t' << numSeqsPerProcessor << endl;
294                         }
295                 }
296         #endif
297                 
298                 vector<vector<string> > barcodePrimerComboFileNames;
299                 if(oligoFileName != ""){
300                         getOligos(barcodePrimerComboFileNames); 
301                 }
302                 
303                 if(processors == 1){
304                         driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
305                 }else{
306                         createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames); 
307                 }       
308                 
309                 if (m->control_pressed) {  return 0; }                  
310                 
311                 string flowFilesFileName;
312                 ofstream output;
313                 
314                 if(allFiles){
315                         set<string> namesAlreadyProcessed;
316                         flowFilesFileName = getOutputFileName("file",variables);
317                         m->openOutputFile(flowFilesFileName, output);
318
319                         for(int i=0;i<barcodePrimerComboFileNames.size();i++){
320                                 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
321                                         if (namesAlreadyProcessed.count(barcodePrimerComboFileNames[i][j]) == 0) {
322                         if (barcodePrimerComboFileNames[i][j] != "") {
323                             FILE * pFile;
324                             unsigned long long size;
325                             
326                             //get num bytes in file
327                             pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
328                             if (pFile==NULL) perror ("Error opening file");
329                             else{
330                                 fseek (pFile, 0, SEEK_END);
331                                 size=ftell(pFile);
332                                 fclose (pFile);
333                             }
334                             
335                             if(size < 10){
336                                 m->mothurRemove(barcodePrimerComboFileNames[i][j]);
337                             }
338                             else{
339                                 output << m->getFullPathName(barcodePrimerComboFileNames[i][j]) << endl;
340                                 outputNames.push_back(barcodePrimerComboFileNames[i][j]);
341                                 outputTypes["flow"].push_back(barcodePrimerComboFileNames[i][j]);
342                             }
343                             namesAlreadyProcessed.insert(barcodePrimerComboFileNames[i][j]);
344                         }
345                                         }
346                                 }
347                         }
348                         output.close();
349                 }
350                 else{
351                         flowFilesFileName = getOutputFileName("file",variables);
352                         m->openOutputFile(flowFilesFileName, output);
353                         
354                         output << m->getFullPathName(trimFlowFileName) << endl;
355                         
356                         output.close();
357                 }
358                 outputTypes["file"].push_back(flowFilesFileName);
359                 outputNames.push_back(flowFilesFileName);
360                         
361                 m->mothurOutEndLine();
362                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
363                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
364                 m->mothurOutEndLine();
365                 
366                 return 0;       
367         }
368         catch(exception& e) {
369                 m->errorOut(e, "TrimSeqsCommand", "execute");
370                 exit(1);
371         }
372 }
373
374 //***************************************************************************************************************
375
376 int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > thisBarcodePrimerComboFileNames, linePair* line){
377         
378         try {
379                 ofstream trimFlowFile;
380                 m->openOutputFile(trimFlowFileName, trimFlowFile);
381                 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
382
383                 ofstream scrapFlowFile;
384                 m->openOutputFile(scrapFlowFileName, scrapFlowFile);
385                 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
386                 
387                 ofstream fastaFile;
388                 if(fasta){      m->openOutputFile(fastaFileName, fastaFile);    }
389                 
390                 ifstream flowFile;
391                 m->openInputFile(flowFileName, flowFile);
392                 
393                 flowFile.seekg(line->start);
394                 
395                 if(line->start == 0){
396                         flowFile >> numFlows; m->gobble(flowFile);
397                         scrapFlowFile << maxFlows << endl;
398                         trimFlowFile << maxFlows << endl;
399                         if(allFiles){
400                                 for(int i=0;i<thisBarcodePrimerComboFileNames.size();i++){
401                                         for(int j=0;j<thisBarcodePrimerComboFileNames[0].size();j++){
402                         if (thisBarcodePrimerComboFileNames[i][j] != "") {
403                             ofstream temp;
404                             m->openOutputFile(thisBarcodePrimerComboFileNames[i][j], temp);
405                             temp << maxFlows << endl;
406                             temp.close();
407                         }
408                                         }
409                                 }                       
410                         }
411                 }
412                 
413                 FlowData flowData(numFlows, signal, noise, maxHomoP, flowOrder);
414                 //cout << " driver flowdata address " <<  &flowData  << &flowFile << endl;      
415                 int count = 0;
416                 bool moreSeqs = 1;
417                 
418                 TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, revPrimer, linker, spacer);
419                 
420                 while(moreSeqs) {
421                                 
422                         if (m->control_pressed) { break; }
423                         
424                         int success = 1;
425                         int currentSeqDiffs = 0;
426                         string trashCode = "";
427                         
428                         flowData.getNext(flowFile); 
429                         flowData.capFlows(maxFlows);    
430                         
431                         Sequence currSeq = flowData.getSequence();
432                         if(!flowData.hasMinFlows(minFlows)){    //screen to see if sequence is of a minimum number of flows
433                                 success = 0;
434                                 trashCode += 'l';
435                         }
436                         
437                         int primerIndex = 0;
438                         int barcodeIndex = 0;
439                         
440             if(numLinkers != 0){
441                 success = trimOligos.stripLinker(currSeq);
442                 if(success > ldiffs)            {       trashCode += 'k';       }
443                 else{ currentSeqDiffs += success;  }
444                 
445             }
446             
447             if (m->debug) { m->mothurOut("[DEBUG]: " + currSeq.getName() + " " + currSeq.getUnaligned() + "\n"); }
448             
449                         if(barcodes.size() != 0){
450                                 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
451                                 if(success > bdiffs)            {       trashCode += 'b';       }
452                                 else{ currentSeqDiffs += success;  }
453                         }
454                         
455             if(numSpacers != 0){
456                 success = trimOligos.stripSpacer(currSeq);
457                 if(success > sdiffs)            {       trashCode += 's';       }
458                 else{ currentSeqDiffs += success;  }
459                 
460             }
461             
462                         if(numFPrimers != 0){
463                                 success = trimOligos.stripForward(currSeq, primerIndex);
464                                 if(success > pdiffs)            {       trashCode += 'f';       }
465                                 else{ currentSeqDiffs += success;  }
466                         }
467                         
468                         if (currentSeqDiffs > tdiffs)   {       trashCode += 't';   }
469                         
470                         if(numRPrimers != 0){
471                                 success = trimOligos.stripReverse(currSeq);
472                                 if(!success)                            {       trashCode += 'r';       }
473                         }
474                         
475                         if(trashCode.length() == 0){
476                 string thisGroup = "";
477                 if(barcodes.size() != 0){
478                     thisGroup = barcodeNameVector[barcodeIndex];
479                     if (primers.size() != 0) { 
480                         if (primerNameVector[primerIndex] != "") { 
481                             if(thisGroup != "") {
482                                 thisGroup += "." + primerNameVector[primerIndex]; 
483                             }else {
484                                 thisGroup = primerNameVector[primerIndex]; 
485                             }
486                         } 
487                     }
488                 }
489                 
490                 int pos = thisGroup.find("ignore");
491                 if (pos == string::npos) {              
492                     flowData.printFlows(trimFlowFile);
493                     
494                     if(fasta)   { currSeq.printSequence(fastaFile);     }
495                     
496                     if(allFiles){
497                         ofstream output;
498                         m->openOutputFileAppend(thisBarcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
499                         output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
500                         
501                         flowData.printFlows(output);
502                         output.close();
503                     }
504                 }
505                         }
506                         else{
507                                 flowData.printFlows(scrapFlowFile, trashCode);
508                         }
509                                 
510                         count++;
511                         //cout << "driver" << '\t' << currSeq.getName() << endl;                        
512                         //report progress
513                         if((count) % 10000 == 0){       m->mothurOut(toString(count)); m->mothurOutEndLine();           }
514
515 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
516                         unsigned long long pos = flowFile.tellg();
517
518                         if ((pos == -1) || (pos >= line->end)) { break; }
519 #else
520                         if (flowFile.eof()) { break; }
521 #endif
522                         
523                 }
524                 //report progress
525                 if((count) % 10000 != 0){       m->mothurOut(toString(count)); m->mothurOutEndLine();           }
526                 
527                 trimFlowFile.close();
528                 scrapFlowFile.close();
529                 flowFile.close();
530                 if(fasta){      fastaFile.close();      }
531                 
532                 return count;
533         }
534         catch(exception& e) {
535                 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
536                 exit(1);
537         }
538 }
539
540 //***************************************************************************************************************
541
542 void TrimFlowsCommand::getOligos(vector<vector<string> >& outFlowFileNames){
543         try {
544                 ifstream oligosFile;
545                 m->openInputFile(oligoFileName, oligosFile);
546                 
547                 string type, oligo, group;
548
549                 int indexPrimer = 0;
550                 int indexBarcode = 0;
551                 
552                 while(!oligosFile.eof()){
553                 
554                         oligosFile >> type; m->gobble(oligosFile);      //get the first column value of the row - is it a comment or a feature we are interested in?
555
556                         if(type[0] == '#'){     //igore the line because there's a comment
557                                 while (!oligosFile.eof())       {       char c = oligosFile.get(); if (c == 10 || c == 13){     break;  }       } // get rest of line if there's any crap there
558                         }
559                         else{                           //there's a feature we're interested in
560
561                                 for(int i=0;i<type.length();i++){       type[i] = toupper(type[i]);  }                                  //make type case insensitive
562
563                                 oligosFile >> oligo;    //get the DNA sequence for the feature
564
565                                 for(int i=0;i<oligo.length();i++){      //make type case insensitive and change any U's to T's
566                                         oligo[i] = toupper(oligo[i]);
567                                         if(oligo[i] == 'U')     {       oligo[i] = 'T'; }
568                                 }
569
570                                 if(type == "FORWARD"){  //if the feature is a forward primer...
571                                         group = "";
572
573                                         while (!oligosFile.eof())       {       // get rest of line in case there is a primer name = will have the name of the primer
574                                                 char c = oligosFile.get(); 
575                                                 if (c == 10 || c == 13){        break;  }
576                                                 else if (c == 32 || c == 9){;} //space or tab
577                                                 else {  group += c;  }
578                                         } 
579
580                                         //have we seen this primer already?
581                                         map<string, int>::iterator itPrimer = primers.find(oligo);
582                                         if (itPrimer != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine();  }
583
584                                         primers[oligo]=indexPrimer; indexPrimer++;
585                                         primerNameVector.push_back(group);
586
587                                 }
588                                 else if(type == "REVERSE"){
589                                         string oligoRC = reverseOligo(oligo);
590                                         revPrimer.push_back(oligoRC);
591                                 }
592                                 else if(type == "BARCODE"){
593                                         oligosFile >> group;
594
595                                         //check for repeat barcodes
596                                         map<string, int>::iterator itBar = barcodes.find(oligo);
597                                         if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine();  }
598
599                                         barcodes[oligo]=indexBarcode; indexBarcode++;
600                                         barcodeNameVector.push_back(group);
601                                 }else if(type == "LINKER"){
602                                         linker.push_back(oligo);
603                                 }else if(type == "SPACER"){
604                                         spacer.push_back(oligo);
605                                 }
606                                 else{
607                                         m->mothurOut(type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine();  
608                                 }
609                         }
610
611                         m->gobble(oligosFile);
612                 }
613                 oligosFile.close();
614                 
615                 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0;   }
616                 
617                 //add in potential combos
618                 if(barcodeNameVector.size() == 0){
619                         barcodes[""] = 0;
620                         barcodeNameVector.push_back("");                        
621                 }
622                 
623                 if(primerNameVector.size() == 0){
624                         primers[""] = 0;
625                         primerNameVector.push_back("");                 
626                 }
627                 
628                 
629                 outFlowFileNames.resize(barcodeNameVector.size());
630                 for(int i=0;i<outFlowFileNames.size();i++){
631                         outFlowFileNames[i].assign(primerNameVector.size(), "");
632                 }
633                 
634                 if(allFiles){
635
636                         for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
637                                 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
638
639                                         string primerName = primerNameVector[itPrimer->second];
640                                         string barcodeName = barcodeNameVector[itBar->second];
641                     
642                                         if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing 
643                                         else {                                  
644                         string comboGroupName = "";
645                         string fileName = "";
646                         
647                         map<string, string> variables; 
648                         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(flowFileName));
649                         
650                         if(primerName == ""){
651                             comboGroupName = barcodeNameVector[itBar->second];
652                             variables["[tag]"] = comboGroupName;
653                             fileName = getOutputFileName("flow", variables);
654                         }
655                         else{
656                             if(barcodeName == ""){
657                                 comboGroupName = primerNameVector[itPrimer->second];
658                             }
659                             else{
660                                 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
661                             }
662                             variables["[tag]"] = comboGroupName;
663                             fileName = getOutputFileName("flow", variables);
664                         }
665                         
666                         outFlowFileNames[itBar->second][itPrimer->second] = fileName;
667                         
668                         ofstream temp;
669                         m->openOutputFile(fileName, temp);
670                         temp.close();
671                     }
672                                 }
673                         }
674                 }
675                 
676                 numFPrimers = primers.size();
677                 numRPrimers = revPrimer.size();
678         numLinkers = linker.size();
679         numSpacers = spacer.size();
680                 
681         }
682         catch(exception& e) {
683                 m->errorOut(e, "TrimSeqsCommand", "getOligos");
684                 exit(1);
685         }
686 }
687 //********************************************************************/
688 string TrimFlowsCommand::reverseOligo(string oligo){
689         try {
690         string reverse = "";
691         
692         for(int i=oligo.length()-1;i>=0;i--){
693             
694             if(oligo[i] == 'A')         {       reverse += 'T'; }
695             else if(oligo[i] == 'T'){   reverse += 'A'; }
696             else if(oligo[i] == 'U'){   reverse += 'A'; }
697             
698             else if(oligo[i] == 'G'){   reverse += 'C'; }
699             else if(oligo[i] == 'C'){   reverse += 'G'; }
700             
701             else if(oligo[i] == 'R'){   reverse += 'Y'; }
702             else if(oligo[i] == 'Y'){   reverse += 'R'; }
703             
704             else if(oligo[i] == 'M'){   reverse += 'K'; }
705             else if(oligo[i] == 'K'){   reverse += 'M'; }
706             
707             else if(oligo[i] == 'W'){   reverse += 'W'; }
708             else if(oligo[i] == 'S'){   reverse += 'S'; }
709             
710             else if(oligo[i] == 'B'){   reverse += 'V'; }
711             else if(oligo[i] == 'V'){   reverse += 'B'; }
712             
713             else if(oligo[i] == 'D'){   reverse += 'H'; }
714             else if(oligo[i] == 'H'){   reverse += 'D'; }
715             
716             else                                                {       reverse += 'N'; }
717         }
718         
719         
720         return reverse;
721     }
722         catch(exception& e) {
723                 m->errorOut(e, "TrimFlowsCommand", "reverseOligo");
724                 exit(1);
725         }
726 }
727
728 /**************************************************************************************************/
729 vector<unsigned long long> TrimFlowsCommand::getFlowFileBreaks() {
730
731         try{
732                         
733                 vector<unsigned long long> filePos;
734                 filePos.push_back(0);
735                                         
736                 FILE * pFile;
737                 unsigned long long size;
738                 
739                 //get num bytes in file
740                 pFile = fopen (flowFileName.c_str(),"rb");
741                 if (pFile==NULL) perror ("Error opening file");
742                 else{
743                         fseek (pFile, 0, SEEK_END);
744                         size=ftell (pFile);
745                         fclose (pFile);
746                 }
747                                 
748                 //estimate file breaks
749                 unsigned long long chunkSize = 0;
750                 chunkSize = size / processors;
751
752                 //file too small to divide by processors
753                 if (chunkSize == 0)  {  processors = 1; filePos.push_back(size); return filePos;        }
754                 
755                 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
756                 for (int i = 0; i < processors; i++) {
757                         unsigned long long spot = (i+1) * chunkSize;
758                         
759                         ifstream in;
760                         m->openInputFile(flowFileName, in);
761                         in.seekg(spot);
762                         
763                         string dummy = m->getline(in);
764                         
765                         //there was not another sequence before the end of the file
766                         unsigned long long sanityPos = in.tellg();
767                         
768 //                      if (sanityPos == -1) {  break;  }
769 //                      else {  filePos.push_back(newSpot);  }
770                         if (sanityPos == -1) {  break;  }
771                         else {  filePos.push_back(sanityPos);  }
772                         
773                         in.close();
774                 }
775                 
776                 //save end pos
777                 filePos.push_back(size);
778                 
779                 //sanity check filePos
780                 for (int i = 0; i < (filePos.size()-1); i++) {
781                         if (filePos[(i+1)] <= filePos[i]) {  filePos.erase(filePos.begin()+(i+1)); i--; }
782                 }
783
784                 ifstream in;
785                 m->openInputFile(flowFileName, in);
786                 in >> numFlows;
787                 m->gobble(in);
788                 //unsigned long long spot = in.tellg();
789                 //filePos[0] = spot;
790                 in.close();
791                 
792                 processors = (filePos.size() - 1);
793                 
794                 return filePos; 
795         }
796         catch(exception& e) {
797                 m->errorOut(e, "TrimSeqsCommand", "getFlowFileBreaks");
798                 exit(1);
799         }
800 }
801
802 /**************************************************************************************************/
803
804 int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
805
806         try {
807                 processIDS.clear();
808                 int exitCommand = 1;
809                 
810 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
811                 int process = 1;
812                 
813                 //loop through and create all the processes you want
814                 while (process != processors) {
815                         int pid = fork();
816                         
817                         if (pid > 0) {
818                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
819                                 process++;
820                         }else if (pid == 0){
821                                 
822                                 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
823                                 if(allFiles){
824                                         for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
825                                                 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
826                             if (tempBarcodePrimerComboFileNames[i][j] != "") {
827                                 tempBarcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
828                                 ofstream temp;
829                                 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
830                                 temp.close();
831                             }
832                                                 }
833                                         }
834                                 }
835                                 driverCreateTrim(flowFileName,
836                                                                  (trimFlowFileName + toString(getpid()) + ".temp"),
837                                                                  (scrapFlowFileName + toString(getpid()) + ".temp"),
838                                                                  (fastaFileName + toString(getpid()) + ".temp"),
839                                                                  tempBarcodePrimerComboFileNames, lines[process]);
840
841                                 exit(0);
842                         }else { 
843                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
844                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
845                                 exit(0);
846                         }
847                 }
848                 
849                 //parent do my part
850                 ofstream temp;
851                 m->openOutputFile(trimFlowFileName, temp);
852                 temp.close();
853
854                 m->openOutputFile(scrapFlowFileName, temp);
855                 temp.close();
856                 
857                 if(fasta){
858                         m->openOutputFile(fastaFileName, temp);
859                         temp.close();
860                 }
861                 
862                 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
863
864                 //force parent to wait until all the processes are done
865                 for (int i=0;i<processIDS.size();i++) { 
866                         int temp = processIDS[i];
867                         wait(&temp);
868                 }
869 #else
870                 //////////////////////////////////////////////////////////////////////////////////////////////////////
871                 //Windows version shared memory, so be careful when passing variables through the trimFlowData struct. 
872                 //Above fork() will clone, so memory is separate, but that's not the case with windows, 
873                 //////////////////////////////////////////////////////////////////////////////////////////////////////
874                 
875                 vector<trimFlowData*> pDataArray; 
876                 DWORD   dwThreadIdArray[processors-1];
877                 HANDLE  hThreadArray[processors-1]; 
878                 
879                 //Create processor worker threads.
880                 for( int i=0; i<processors-1; i++ ){
881                         // Allocate memory for thread data.
882                         string extension = "";
883                         if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
884                         
885                         vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
886                         if(allFiles){
887                                 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
888                                         for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
889                         if (tempBarcodePrimerComboFileNames[i][j] != "") {
890                             tempBarcodePrimerComboFileNames[i][j] += extension;
891                             ofstream temp;
892                             m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
893                             temp.close();
894                                                 }
895                                         }
896                                 }
897                         }
898                         
899                         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);
900                         pDataArray.push_back(tempflow);
901                         
902                         //MyTrimFlowThreadFunction is in header. It must be global or static to work with the threads.
903                         //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
904                         hThreadArray[i] = CreateThread(NULL, 0, MyTrimFlowThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);   
905                 }
906                 
907                 //using the main process as a worker saves time and memory
908                 ofstream temp;
909                 m->openOutputFile(trimFlowFileName, temp);
910                 temp.close();
911                 
912                 m->openOutputFile(scrapFlowFileName, temp);
913                 temp.close();
914                 
915                 if(fasta){
916                         m->openOutputFile(fastaFileName, temp);
917                         temp.close();
918                 }
919                 
920                 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
921                 if(allFiles){
922                         for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
923                                 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
924                     if (tempBarcodePrimerComboFileNames[i][j] != "") {
925                         tempBarcodePrimerComboFileNames[i][j] += toString(processors-1) + ".temp";
926                         ofstream temp;
927                         m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
928                         temp.close();
929                     }
930                                         
931                                 }
932                         }
933                 }
934                 
935                 //do my part - do last piece because windows is looking for eof
936                 int num = driverCreateTrim(flowFileName, (trimFlowFileName  + toString(processors-1) + ".temp"), (scrapFlowFileName  + toString(processors-1) + ".temp"), (fastaFileName + toString(processors-1) + ".temp"), tempBarcodePrimerComboFileNames, lines[processors-1]);
937                 processIDS.push_back((processors-1)); 
938                 
939                 //Wait until all threads have terminated.
940                 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
941                 
942                 //Close all thread handles and free memory allocations.
943                 for(int i=0; i < pDataArray.size(); i++){
944                         num += pDataArray[i]->count;
945                         CloseHandle(hThreadArray[i]);
946                         delete pDataArray[i];
947                 }
948                 
949                 
950 #endif  
951                 //append files
952                 m->mothurOutEndLine();
953                 for(int i=0;i<processIDS.size();i++){
954                         
955                         m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
956                         
957                         m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
958                         m->mothurRemove((trimFlowFileName + toString(processIDS[i]) + ".temp"));
959 //                      m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
960
961                         m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
962                         m->mothurRemove((scrapFlowFileName + toString(processIDS[i]) + ".temp"));
963 //                      m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
964
965                         if(fasta){
966                                 m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
967                                 m->mothurRemove((fastaFileName + toString(processIDS[i]) + ".temp"));
968 //                              m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
969                         }
970                         if(allFiles){                                           
971                                 for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
972                                         for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
973                         if (barcodePrimerComboFileNames[j][k] != "") {
974                             m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
975                             m->mothurRemove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"));
976                         }
977                                         }
978                                 }
979                         }
980                 }
981                 
982                 return exitCommand;
983         
984         }
985         catch(exception& e) {
986                 m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");
987                 exit(1);
988         }
989 }
990
991 //***************************************************************************************************************