]> git.donarmstrong.com Git - mothur.git/blob - makecontigscommand.cpp
make.contigs bug with file option and multiple processors. added make count parameter
[mothur.git] / makecontigscommand.cpp
1 //
2 //  makecontigscommand.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 5/15/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "makecontigscommand.h"
10 #include "counttable.h"
11
12 //**********************************************************************************************************************
13 vector<string> MakeContigsCommand::setParameters(){     
14         try {
15                 CommandParameter pfastq("ffastq", "InputTypes", "", "", "FastaFastqFile", "FastaFastqFile", "fastqGroup","fasta-qfile",false,false,true); parameters.push_back(pfastq);
16         CommandParameter prfastq("rfastq", "InputTypes", "", "", "none", "none", "fastqGroup","fasta-qfile",false,false,true); parameters.push_back(prfastq);
17         CommandParameter pfasta("ffasta", "InputTypes", "", "", "FastaFastqFile", "FastaFastqFile", "fastaGroup","fasta",false,false,true); parameters.push_back(pfasta);
18         CommandParameter prfasta("rfasta", "InputTypes", "", "", "none", "none", "none","fastaGroup",false,false,true); parameters.push_back(prfasta);
19         CommandParameter pfqual("fqfile", "InputTypes", "", "", "none", "none", "qfileGroup","",false,false,true); parameters.push_back(pfqual);
20         CommandParameter prqual("rqfile", "InputTypes", "", "", "none", "none", "qfileGroup","",false,false,true); parameters.push_back(prqual);
21         CommandParameter pfile("file", "InputTypes", "", "", "FastaFastqFile", "FastaFastqFile", "none","fasta-qfile",false,false,true); parameters.push_back(pfile);
22         CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none","group",false,false,true); parameters.push_back(poligos);
23                 CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "","",false,false,true); parameters.push_back(ppdiffs);
24                 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "","",false,false,true); parameters.push_back(pbdiffs);
25         CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ptdiffs);
26
27         CommandParameter palign("align", "Multiple", "needleman-gotoh", "needleman", "", "", "","",false,false); parameters.push_back(palign);
28         CommandParameter pallfiles("allfiles", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pallfiles);
29         CommandParameter pmakecount("makecount", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pmakecount);
30         CommandParameter ptrimoverlap("trimoverlap", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(ptrimoverlap);
31                 CommandParameter pmatch("match", "Number", "", "1.0", "", "", "","",false,false); parameters.push_back(pmatch);
32                 CommandParameter pmismatch("mismatch", "Number", "", "-1.0", "", "", "","",false,false); parameters.push_back(pmismatch);
33                 CommandParameter pgapopen("gapopen", "Number", "", "-2.0", "", "", "","",false,false); parameters.push_back(pgapopen);
34                 CommandParameter pgapextend("gapextend", "Number", "", "-1.0", "", "", "","",false,false); parameters.push_back(pgapextend);
35         CommandParameter pthreshold("insert", "Number", "", "20", "", "", "","",false,false); parameters.push_back(pthreshold);
36         CommandParameter pdeltaq("deltaq", "Number", "", "6", "", "", "","",false,false); parameters.push_back(pdeltaq);
37                 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
38         CommandParameter pformat("format", "Multiple", "sanger-illumina-solexa-illumina1.8+", "illumina1.8+", "", "", "","",false,false,true); parameters.push_back(pformat);
39                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
40                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
41                 
42                 vector<string> myArray;
43                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
44                 return myArray;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "MakeContigsCommand", "setParameters");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 string MakeContigsCommand::getHelpString(){     
53         try {
54                 string helpString = "";
55                 helpString += "The make.contigs command reads a file, forward fastq file and a reverse fastq file or forward fasta and reverse fasta files and outputs new fasta.  It will also provide new quality files if the fastq or file parameter is used.\n";
56         helpString += "If an oligos file is provided barcodes and primers will be trimmed, and a group file will be created.\n";
57                 helpString += "The make.contigs command parameters are file, ffastq, rfastq, ffasta, rfasta, fqfile, rqfile, oligos, format, tdiffs, bdiffs, pdiffs, align, match, mismatch, gapopen, gapextend, insert, deltaq, allfiles, makecount and processors.\n";
58                 helpString += "The ffastq and rfastq, file, or ffasta and rfasta parameters are required.\n";
59         helpString += "The file parameter is 2 or 3 column file containing the forward fastq files in the first column and their matching reverse fastq files in the second column, or a groupName then forward fastq file and reverse fastq file.  Mothur will process each pair and create a combined fasta and report file with all the sequences.\n";
60         helpString += "The ffastq and rfastq parameters are used to provide a forward fastq and reverse fastq file to process.  If you provide one, you must provide the other.\n";
61         helpString += "The ffasta and rfasta parameters are used to provide a forward fasta and reverse fasta file to process.  If you provide one, you must provide the other.\n";
62         helpString += "The fqfile and rqfile parameters are used to provide a forward quality and reverse quality files to process with the ffasta and rfasta parameters.  If you provide one, you must provide the other.\n";
63                 helpString += "The format parameter is used to indicate whether your sequences are sanger, solexa, illumina1.8+ or illumina, default=illumina1.8+.\n";
64         helpString += "The align parameter allows you to specify the alignment method to use.  Your options are: gotoh and needleman. The default is needleman.\n";
65         helpString += "The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is pdiffs + bdiffs + sdiffs + ldiffs.\n";
66                 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
67                 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
68         //helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
69                 //helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
70                 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
71                 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases.  The default is -1.0.\n";
72         helpString += "The deltaq parameter allows you to specify the delta allowed between quality scores of a mismatched base.  For example in the overlap, if deltaq=5 and in the alignment seqA, pos 200 has a quality score of 30 and the same position in seqB has a quality score of 20, you take the base from seqA (30-20 >= 5).  If the quality score in seqB is 28 then the base in the consensus will be an N (30-28<5) The default is 6.\n";
73                 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
74                 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment.  The default is -1.0.\n";
75         helpString += "The insert parameter allows you to set a quality scores threshold. In the case where we are trying to decide whether to keep a base or remove it because the base is compared to a gap in the other fragment, if the base has a quality score equal to or below the threshold we eliminate it. Default=20.\n";
76         helpString += "The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n";
77         helpString += "The allfiles parameter will create separate group and fasta file for each grouping. The default is F.\n";
78         helpString += "The makecount parameter will create a count table file instead of a group file. The default is F.\n";
79
80         helpString += "The trimoverlap parameter allows you to trim the sequences to only the overlapping section. The default is F.\n";
81         helpString += "The make.contigs command should be in the following format: \n";
82                 helpString += "make.contigs(ffastq=yourForwardFastqFile, rfastq=yourReverseFastqFile, align=yourAlignmentMethod) \n";
83                 helpString += "Note: No spaces between parameter labels (i.e. ffastq), '=' and parameters (i.e.yourForwardFastqFile).\n";
84                 return helpString;
85         }
86         catch(exception& e) {
87                 m->errorOut(e, "MakeContigsCommand", "getHelpString");
88                 exit(1);
89         }
90 }
91 //**********************************************************************************************************************
92 string MakeContigsCommand::getOutputPattern(string type) {
93     try {
94         string pattern = "";
95         
96         if (type == "fasta") {  pattern = "[filename],[tag],contigs.fasta"; } 
97         else if (type == "group") {  pattern = "[filename],[tag],contigs.groups"; }
98         else if (type == "count") {  pattern = "[filename],[tag],contigs.count_table"; }
99         else if (type == "report") {  pattern = "[filename],[tag],contigs.report"; }
100         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
101         
102         return pattern;
103     }
104     catch(exception& e) {
105         m->errorOut(e, "MakeContigsCommand", "getOutputPattern");
106         exit(1);
107     }
108 }
109 //**********************************************************************************************************************
110 MakeContigsCommand::MakeContigsCommand(){       
111         try {
112                 abort = true; calledHelp = true; 
113                 setParameters();
114                 vector<string> tempOutNames;
115                 outputTypes["fasta"] = tempOutNames;
116         outputTypes["group"] = tempOutNames;
117         outputTypes["report"] = tempOutNames;
118         outputTypes["count"] = tempOutNames;
119         }
120         catch(exception& e) {
121                 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
122                 exit(1);
123         }
124 }
125 //**********************************************************************************************************************
126 MakeContigsCommand::MakeContigsCommand(string option)  {
127         try {
128                 abort = false; calledHelp = false;
129         createFileGroup = false; createOligosGroup = false;
130         
131                 //allow user to run help
132                 if(option == "help") { help(); abort = true; calledHelp = true; }
133                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
134                 
135                 else {
136                         vector<string> myArray = setParameters();
137                         
138                         OptionParser parser(option);
139                         map<string, string> parameters = parser.getParameters(); 
140                         
141                         ValidParameters validParameter("pairwise.seqs");
142                         map<string, string>::iterator it;
143                         
144                         //check to make sure all parameters are valid for command
145                         for (it = parameters.begin(); it != parameters.end(); it++) { 
146                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
147                         }
148                         
149                         //initialize outputTypes
150                         vector<string> tempOutNames;
151                         outputTypes["fasta"] = tempOutNames;
152             outputTypes["report"] = tempOutNames;
153             outputTypes["group"] = tempOutNames;
154             outputTypes["count"] = tempOutNames;
155                         
156             
157                         //if the user changes the input directory command factory will send this info to us in the output parameter 
158                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
159                         if (inputDir == "not found"){   inputDir = "";          }
160                         else { 
161                                 string path;
162                 it = parameters.find("ffastq");
163                                 //user has given a template file
164                                 if(it != parameters.end()){ 
165                                         path = m->hasPath(it->second);
166                                         //if the user has not given a path then, add inputdir. else leave path alone.
167                                         if (path == "") {       parameters["ffastq"] = inputDir + it->second;           }
168                                 }
169                 
170                 it = parameters.find("rfastq");
171                                 //user has given a template file
172                                 if(it != parameters.end()){ 
173                                         path = m->hasPath(it->second);
174                                         //if the user has not given a path then, add inputdir. else leave path alone.
175                                         if (path == "") {       parameters["rfastq"] = inputDir + it->second;           }
176                                 }
177                 
178                 it = parameters.find("ffasta");
179                                 //user has given a template file
180                                 if(it != parameters.end()){ 
181                                         path = m->hasPath(it->second);
182                                         //if the user has not given a path then, add inputdir. else leave path alone.
183                                         if (path == "") {       parameters["ffasta"] = inputDir + it->second;           }
184                                 }
185                 
186                 it = parameters.find("rfasta");
187                                 //user has given a template file
188                                 if(it != parameters.end()){ 
189                                         path = m->hasPath(it->second);
190                                         //if the user has not given a path then, add inputdir. else leave path alone.
191                                         if (path == "") {       parameters["rfasta"] = inputDir + it->second;           }
192                                 }
193                 
194                 it = parameters.find("fqfile");
195                                 //user has given a template file
196                                 if(it != parameters.end()){ 
197                                         path = m->hasPath(it->second);
198                                         //if the user has not given a path then, add inputdir. else leave path alone.
199                                         if (path == "") {       parameters["fqfile"] = inputDir + it->second;           }
200                                 }
201                 
202                 it = parameters.find("rqfile");
203                                 //user has given a template file
204                                 if(it != parameters.end()){ 
205                                         path = m->hasPath(it->second);
206                                         //if the user has not given a path then, add inputdir. else leave path alone.
207                                         if (path == "") {       parameters["rqfile"] = inputDir + it->second;           }
208                                 }
209                 
210                 it = parameters.find("file");
211                                 //user has given a template file
212                                 if(it != parameters.end()){ 
213                                         path = m->hasPath(it->second);
214                                         //if the user has not given a path then, add inputdir. else leave path alone.
215                                         if (path == "") {       parameters["file"] = inputDir + it->second;             }
216                                 }
217                 
218                 it = parameters.find("oligos");
219                                 //user has given a template file
220                                 if(it != parameters.end()){ 
221                                         path = m->hasPath(it->second);
222                                         //if the user has not given a path then, add inputdir. else leave path alone.
223                                         if (path == "") {       parameters["oligos"] = inputDir + it->second;           }
224                                 }
225             }
226             
227             ffastqfile = validParameter.validFile(parameters, "ffastq", true);
228                         if (ffastqfile == "not open") {  abort = true; }        
229                         else if (ffastqfile == "not found") { ffastqfile = ""; }
230                         
231                         rfastqfile = validParameter.validFile(parameters, "rfastq", true);
232                         if (rfastqfile == "not open") {  abort = true; }        
233                         else if (rfastqfile == "not found") { rfastqfile = "";  }
234             
235             ffastafile = validParameter.validFile(parameters, "ffasta", true);
236                         if (ffastafile == "not open") {  abort = true; }        
237                         else if (ffastafile == "not found") { ffastafile = ""; }
238                         
239                         rfastafile = validParameter.validFile(parameters, "rfasta", true);
240                         if (rfastafile == "not open") {  abort = true; }        
241                         else if (rfastafile == "not found") { rfastafile = "";  }
242             
243             fqualfile = validParameter.validFile(parameters, "fqfile", true);
244                         if (fqualfile == "not open") {  abort = true; } 
245                         else if (fqualfile == "not found") { fqualfile = ""; }
246                         
247                         rqualfile = validParameter.validFile(parameters, "rqfile", true);
248                         if (rqualfile == "not open") {  abort = true; } 
249                         else if (rqualfile == "not found") { rqualfile = "";  }
250             
251             file = validParameter.validFile(parameters, "file", true);
252                         if (file == "not open") {  abort = true; }      
253                         else if (file == "not found") { file = "";  }
254             
255             //provide at least
256             if ((file == "") && (ffastafile == "") && (ffastqfile == "")) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
257             if ((file != "") && ((ffastafile != "") || (ffastqfile != ""))) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
258             if ((ffastqfile != "") && (rfastqfile == "")) {  abort = true; m->mothurOut("[ERROR]: If you provide use the ffastq, you must provide a rfastq file.\n"); }
259             if ((ffastqfile == "") && (rfastqfile != "")) {  abort = true; m->mothurOut("[ERROR]: If you provide use the rfastq, you must provide a ffastq file.\n"); }
260             if ((ffastafile != "") && (rfastafile == "")) {  abort = true; m->mothurOut("[ERROR]: If you provide use the ffasta, you must provide a rfasta file.\n"); }
261             if ((ffastafile == "") && (rfastafile != "")) {  abort = true; m->mothurOut("[ERROR]: If you provide use the rfasta, you must provide a ffasta file.\n"); }
262             if ((fqualfile != "") && (rqualfile == "")) {  abort = true; m->mothurOut("[ERROR]: If you provide use the fqfile, you must provide a rqfile file.\n"); }
263             if ((fqualfile == "") && (rqualfile != "")) {  abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile, you must provide a fqfile file.\n"); }
264             if (((fqualfile != "") || (rqualfile != "")) && ((ffastafile == "") || (rfastafile == ""))) {
265                 abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile or fqfile file, you must provide the ffasta and rfasta parameters.\n");
266             }
267             
268             oligosfile = validParameter.validFile(parameters, "oligos", true);
269                         if (oligosfile == "not found")      {   oligosfile = "";        }
270                         else if(oligosfile == "not open")   {   abort = true;       } 
271                         else {   m->setOligosFile(oligosfile);          }
272             
273             //if the user changes the output directory command factory will send this info to us in the output parameter 
274                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
275                  outputDir = ""; 
276             }
277                         
278
279                         //check for optional parameter and set defaults
280                         // ...at some point should added some additional type checking...
281                         string temp;
282                         temp = validParameter.validFile(parameters, "match", false);            if (temp == "not found"){       temp = "1.0";                   }
283                         m->mothurConvert(temp, match);  
284                         
285                         temp = validParameter.validFile(parameters, "mismatch", false);         if (temp == "not found"){       temp = "-1.0";                  }
286                         m->mothurConvert(temp, misMatch);  
287             if (misMatch > 0) { m->mothurOut("[ERROR]: mismatch must be negative.\n"); abort=true; }
288                         
289                         temp = validParameter.validFile(parameters, "gapopen", false);          if (temp == "not found"){       temp = "-2.0";                  }
290                         m->mothurConvert(temp, gapOpen);  
291             if (gapOpen > 0) { m->mothurOut("[ERROR]: gapopen must be negative.\n"); abort=true; }
292                         
293                         temp = validParameter.validFile(parameters, "gapextend", false);        if (temp == "not found"){       temp = "-1.0";                  }
294                         m->mothurConvert(temp, gapExtend); 
295             if (gapExtend > 0) { m->mothurOut("[ERROR]: gapextend must be negative.\n"); abort=true; }
296                         
297             temp = validParameter.validFile(parameters, "insert", false);       if (temp == "not found"){       temp = "20";                    }
298                         m->mothurConvert(temp, insert); 
299             if ((insert < 0) || (insert > 40)) { m->mothurOut("[ERROR]: insert must be between 0 and 40.\n"); abort=true; }
300
301             temp = validParameter.validFile(parameters, "deltaq", false);       if (temp == "not found"){       temp = "6";                     }
302                         m->mothurConvert(temp, deltaq);
303             
304                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
305                         m->setProcessors(temp);
306                         m->mothurConvert(temp, processors);
307             
308             temp = validParameter.validFile(parameters, "bdiffs", false);               if (temp == "not found") { temp = "0"; }
309                         m->mothurConvert(temp, bdiffs);
310                         
311                         temp = validParameter.validFile(parameters, "pdiffs", false);           if (temp == "not found") { temp = "0"; }
312                         m->mothurConvert(temp, pdiffs);
313             
314   //          temp = validParameter.validFile(parameters, "ldiffs", false);             if (temp == "not found") { temp = "0"; }
315 //                      m->mothurConvert(temp, ldiffs);
316             ldiffs = 0;
317             
318  //           temp = validParameter.validFile(parameters, "sdiffs", false);             if (temp == "not found") { temp = "0"; }
319  //           m->mothurConvert(temp, sdiffs);
320             sdiffs = 0;
321                         
322                         temp = validParameter.validFile(parameters, "tdiffs", false);           if (temp == "not found") { int tempTotal = pdiffs + bdiffs;  temp = toString(tempTotal); }
323                         m->mothurConvert(temp, tdiffs);
324                         
325                         if(tdiffs == 0){        tdiffs = bdiffs + pdiffs;       }  //+ ldiffs + sdiffs;
326
327             temp = validParameter.validFile(parameters, "allfiles", false);             if (temp == "not found") { temp = "F"; }
328                         allFiles = m->isTrue(temp);
329             
330             temp = validParameter.validFile(parameters, "makecount", false);            if (temp == "not found") { temp = "F"; }
331                         makeCount = m->isTrue(temp);
332             
333             temp = validParameter.validFile(parameters, "trimoverlap", false);          if (temp == "not found") { temp = "F"; }
334                         trimOverlap = m->isTrue(temp);
335                         
336                         align = validParameter.validFile(parameters, "align", false);           if (align == "not found"){      align = "needleman";    }
337                         if ((align != "needleman") && (align != "gotoh")) { m->mothurOut(align + " is not a valid alignment method. Options are needleman or gotoh. I will use needleman."); m->mothurOutEndLine(); align = "needleman"; }
338             
339             format = validParameter.validFile(parameters, "format", false);             if (format == "not found"){     format = "illumina1.8+";        }
340             
341             if ((format != "sanger") && (format != "illumina") && (format != "illumina1.8+") && (format != "solexa"))  { 
342                                 m->mothurOut(format + " is not a valid format. Your format choices are sanger, solexa, illumina1.8+ and illumina, aborting." ); m->mothurOutEndLine();
343                                 abort=true;
344                         }
345             
346             //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
347             for (int i = -64; i < 65; i++) { 
348                 char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
349                 convertTable.push_back(temp);
350             }
351         }
352                 
353         }
354         catch(exception& e) {
355                 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
356                 exit(1);
357         }
358 }
359 //**********************************************************************************************************************
360 int MakeContigsCommand::execute(){
361         try {
362                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
363         
364         //read ffastq and rfastq files creating fasta and qual files.
365         //this function will create a forward and reverse, fasta and qual files for each processor.
366         //files has an entry for each processor. files[i][0] = forwardFasta, files[i][1] = forwardQual, files[i][2] = reverseFasta, files[i][3] = reverseQual.  filesToProcess is for each filepair in the file parameter file.  for ffastq and rfastq this will be size 1.
367         unsigned long int numReads = 0;
368         int start = time(NULL);
369         longestBase = 1000;
370         m->mothurOut("Reading fastq data...\n"); 
371         vector < vector< vector<string> > > filesToProcess = preProcessData(numReads);
372         m->mothurOut("Done.\n");
373        
374         if (m->control_pressed) { return 0; }
375         
376         map<string, string> cvars;
377         string compOutputDir = outputDir;
378         if (outputDir == "") { compOutputDir = m->hasPath(file); }
379         cvars["[filename]"] = compOutputDir + m->getRootName(m->getSimpleName(file));
380         cvars["[tag]"] = "";
381         string compositeGroupFile = getOutputFileName("group",cvars);
382         if (makeCount) { compositeGroupFile = getOutputFileName("count",cvars); }
383         cvars["[tag]"] = "trim";
384         string compositeFastaFile = getOutputFileName("fasta",cvars);
385         cvars["[tag]"] = "scrap";
386         string compositeScrapFastaFile = getOutputFileName("fasta",cvars);
387         cvars["[tag]"] = "";
388         string compositeMisMatchFile = getOutputFileName("report",cvars);
389         
390         if (filesToProcess.size() > 1) { //clear files for append below
391             ofstream outCTFasta, outCTQual, outCSFasta, outCSQual, outCMisMatch;
392             m->openOutputFile(compositeFastaFile, outCTFasta); outCTFasta.close();
393             m->openOutputFile(compositeScrapFastaFile, outCSFasta); outCSFasta.close();
394             m->openOutputFile(compositeMisMatchFile, outCMisMatch); outCMisMatch.close();
395             outputNames.push_back(compositeFastaFile); outputTypes["fasta"].push_back(compositeFastaFile);
396             outputNames.push_back(compositeMisMatchFile); outputTypes["report"].push_back(compositeMisMatchFile);
397             outputNames.push_back(compositeScrapFastaFile); outputTypes["fasta"].push_back(compositeScrapFastaFile);
398         }
399         
400         map<string, int> totalGroupCounts;
401         CountTable compositeCt;
402         for (int l = 0; l < filesToProcess.size(); l++) {
403             
404             m->mothurOut("\n>>>>>\tProcessing " + filesToProcess[l][0][0] + " (file " + toString(l+1) + " of " + toString(filesToProcess.size()) + ")\t<<<<<\n");
405             
406             groupCounts.clear();
407             groupMap.clear();
408             vector<vector<string> > fastaFileNames;
409             createOligosGroup = false;
410             string outputGroupFileName;
411             map<string, string> variables; 
412             string thisOutputDir = outputDir;
413             if (outputDir == "") {  thisOutputDir = m->hasPath(filesToProcess[l][0][0]); }
414             variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(filesToProcess[l][0][0]));
415             variables["[tag]"] = "";
416             if(oligosfile != ""){  createOligosGroup = getOligos(fastaFileNames, variables["[filename]"]);  }
417             if (createOligosGroup || createFileGroup) {
418                 outputGroupFileName = getOutputFileName("group",variables);
419                 if (makeCount) { outputGroupFileName = getOutputFileName("count",variables); }
420             }
421             
422             //give group in file file precedence
423             if (createFileGroup) {  createOligosGroup = false; }
424             
425             variables["[tag]"] = "trim";
426             string outFastaFile = getOutputFileName("fasta",variables);
427             variables["[tag]"] = "scrap";
428             string outScrapFastaFile = getOutputFileName("fasta",variables);
429             variables["[tag]"] = "";
430             string outMisMatchFile = getOutputFileName("report",variables);
431                         
432             m->mothurOut("Making contigs...\n"); 
433             createProcesses(filesToProcess[l], outFastaFile, outScrapFastaFile, outMisMatchFile, fastaFileNames, l);
434             m->mothurOut("Done.\n");
435             
436             //remove temp fasta and qual files
437             for (int i = 0; i < processors; i++) { for(int j = 0; j < filesToProcess[l][i].size(); j++) { m->mothurRemove(filesToProcess[l][i][j]); }  }
438             
439             if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {    m->mothurRemove(outputNames[i]); }  return 0; }
440             
441             if(allFiles){
442                 map<string, string> uniqueFastaNames;// so we don't add the same groupfile multiple times
443                 map<string, string>::iterator it;
444                 set<string> namesToRemove;
445                 for(int i=0;i<fastaFileNames.size();i++){
446                     for(int j=0;j<fastaFileNames[0].size();j++){
447                         if (fastaFileNames[i][j] != "") {
448                             if (namesToRemove.count(fastaFileNames[i][j]) == 0) {
449                                 if(m->isBlank(fastaFileNames[i][j])){
450                                     m->mothurRemove(fastaFileNames[i][j]);
451                                     namesToRemove.insert(fastaFileNames[i][j]);
452                                 }else{  
453                                     it = uniqueFastaNames.find(fastaFileNames[i][j]);
454                                     if (it == uniqueFastaNames.end()) { 
455                                         uniqueFastaNames[fastaFileNames[i][j]] = barcodeNameVector[i];  
456                                     }   
457                                 }
458                             }
459                         }
460                     }
461                 }
462                 
463                 //remove names for outputFileNames, just cleans up the output
464                 vector<string> outputNames2;
465                 for(int i = 0; i < outputNames.size(); i++) { if (namesToRemove.count(outputNames[i]) == 0) { outputNames2.push_back(outputNames[i]); } }
466                 outputNames = outputNames2;
467                 
468                 for (it = uniqueFastaNames.begin(); it != uniqueFastaNames.end(); it++) {
469                     ifstream in;
470                     m->openInputFile(it->first, in);
471                     
472                     ofstream out;
473                     string thisGroupName = thisOutputDir + m->getRootName(m->getSimpleName(it->first));
474                     if (!makeCount) { thisGroupName += getOutputFileName("group",variables); outputNames.push_back(thisGroupName); outputTypes["group"].push_back(thisGroupName);
475                         m->openOutputFile(thisGroupName, out);
476                         
477                         while (!in.eof()){
478                             if (m->control_pressed) { break; }
479                             
480                             Sequence currSeq(in); m->gobble(in);
481                             out << currSeq.getName() << '\t' << it->second << endl;
482                         }
483                         out.close();
484                     }
485                     else {
486                         thisGroupName += getOutputFileName("count",variables); outputNames.push_back(thisGroupName); outputTypes["count"].push_back(thisGroupName);
487                         CountTable ct;
488                         ct.addGroup(it->second);
489                         while (!in.eof()){
490                             if (m->control_pressed) { break; }
491                             
492                             Sequence currSeq(in); m->gobble(in);
493                             vector<int> tempGroupCount; tempGroupCount.push_back(1);
494                             ct.push_back(currSeq.getName(), tempGroupCount);
495                         }
496                         ct.printTable(thisGroupName);
497                     }
498                     in.close();
499                 }
500             }
501             
502             if (createFileGroup || createOligosGroup) {
503                 if (makeCount) {
504                     if ((allFiles) || (filesToProcess.size() == 1)) {
505                         CountTable ct;
506                         for (map<string, int>::iterator itGroups = groupCounts.begin(); itGroups != groupCounts.end(); itGroups++) {
507                             ct.addGroup(itGroups->first);
508                         }
509                         vector<string> groups = ct.getNamesOfGroups();
510                         for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
511                             vector<int> tempGroupCounts; tempGroupCounts.resize(groups.size(), 0);
512                             ct.push_back(itGroup->first, tempGroupCounts);
513                             ct.setAbund(itGroup->first, itGroup->second, 1);
514                         }
515                         ct.printTable(outputGroupFileName);
516                     }
517                 }else {
518                     ofstream outGroup;
519                     m->openOutputFile(outputGroupFileName, outGroup);
520                     for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
521                         outGroup << itGroup->first << '\t' << itGroup->second << endl;
522                     }
523                     outGroup.close();
524                 }
525             }
526             
527             if (filesToProcess.size() > 1) { //merge into large combo files
528                 if (createFileGroup || createOligosGroup) {
529                     if (makeCount) {
530                         for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
531                             vector<string> groups = compositeCt.getNamesOfGroups();
532                             if (m->inUsersGroups(itGroup->second, groups)) {
533                                 vector<int> tempGroupCounts; tempGroupCounts.resize(groups.size(), 0);
534                                 compositeCt.push_back(itGroup->first, tempGroupCounts);
535                                 compositeCt.setAbund(itGroup->first, itGroup->second, 1);
536                             }else{
537                                 compositeCt.addGroup(itGroup->second);
538                                 vector<int> tempGroupCounts; tempGroupCounts.resize(groups.size()+1, 0);
539                                 compositeCt.push_back(itGroup->first, tempGroupCounts);
540                                 compositeCt.setAbund(itGroup->first, itGroup->second, 1);
541                             }
542                         }
543                     }else {
544                         if (l == 0) {
545                             ofstream outCGroup;
546                             m->openOutputFile(compositeGroupFile, outCGroup); outCGroup.close();
547                             outputNames.push_back(compositeGroupFile); outputTypes["group"].push_back(compositeGroupFile);
548                         }
549                         m->appendFiles(outputGroupFileName, compositeGroupFile);
550                         if (!allFiles) { m->mothurRemove(outputGroupFileName);  }
551                         else { outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName); }
552                     }
553                     for (map<string, int>::iterator itGroups = groupCounts.begin(); itGroups != groupCounts.end(); itGroups++) {
554                         map<string, int>::iterator itTemp = totalGroupCounts.find(itGroups->first);
555                         if (itTemp == totalGroupCounts.end()) { totalGroupCounts[itGroups->first] = itGroups->second; } //new group create it in totalGroups
556                         else { itTemp->second += itGroups->second; } //existing group, update total
557                     }
558                 }
559                 if (l == 0) {  m->appendFiles(outMisMatchFile, compositeMisMatchFile);  }
560                 else {  m->appendFilesWithoutHeaders(outMisMatchFile, compositeMisMatchFile);  }
561                 m->appendFiles(outFastaFile, compositeFastaFile);
562                 m->appendFiles(outScrapFastaFile, compositeScrapFastaFile);
563                 if (!allFiles) {
564                     m->mothurRemove(outMisMatchFile);
565                     m->mothurRemove(outFastaFile);
566                     m->mothurRemove(outScrapFastaFile);
567                 }else {
568                     outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
569                     outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile);
570                     outputNames.push_back(outMisMatchFile); outputTypes["report"].push_back(outMisMatchFile);
571                 }
572             }else {
573                 totalGroupCounts = groupCounts;
574                 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
575                 outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile);
576                 outputNames.push_back(outMisMatchFile); outputTypes["report"].push_back(outMisMatchFile);
577                 if (createFileGroup || createOligosGroup) {
578                     if (makeCount) { outputNames.push_back(outputGroupFileName); outputTypes["count"].push_back(outputGroupFileName);  }
579                     else { outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName); }
580                 }
581             }
582         }
583         
584         if ((filesToProcess.size() > 1) && makeCount) { //merge into large combo files
585             compositeCt.printTable(compositeGroupFile);
586             outputNames.push_back(compositeGroupFile); outputTypes["count"].push_back(compositeGroupFile);
587         }
588         m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process " + toString(numReads) + " sequences.\n");
589         
590         if (m->control_pressed) {       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } return 0;    }
591         
592                 //output group counts
593                 m->mothurOutEndLine();
594                 int total = 0;
595                 if (totalGroupCounts.size() != 0) {  m->mothurOut("Group count: \n");  }
596                 for (map<string, int>::iterator it = totalGroupCounts.begin(); it != totalGroupCounts.end(); it++) {
597             total += it->second; m->mothurOut(it->first + "\t" + toString(it->second)); m->mothurOutEndLine(); 
598                 }
599                 if (total != 0) { m->mothurOut("Total of all groups is " + toString(total)); m->mothurOutEndLine(); }
600                 
601                 if (m->control_pressed) {       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } return 0;    }
602         
603         string currentFasta = "";
604                 itTypes = outputTypes.find("fasta");
605                 if (itTypes != outputTypes.end()) {
606                         if ((itTypes->second).size() != 0) { currentFasta = (itTypes->second)[0]; m->setFastaFile(currentFasta); }
607                 }
608         
609         string currentGroup = "";
610                 itTypes = outputTypes.find("group");
611                 if (itTypes != outputTypes.end()) {
612                         if ((itTypes->second).size() != 0) { currentGroup = (itTypes->second)[0]; m->setGroupFile(currentGroup); }
613                 }
614         
615         string currentCount = "";
616                 itTypes = outputTypes.find("count");
617                 if (itTypes != outputTypes.end()) {
618                         if ((itTypes->second).size() != 0) { currentCount = (itTypes->second)[0]; m->setCountTableFile(currentCount); }
619                 }
620                 
621         //output files created by command
622                 m->mothurOutEndLine();
623                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
624                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
625                 m->mothurOutEndLine();
626
627         return 0;
628     }
629         catch(exception& e) {
630                 m->errorOut(e, "MakeContigsCommand", "execute");
631                 exit(1);
632         }
633 }
634 //**********************************************************************************************************************
635 vector< vector< vector<string> > > MakeContigsCommand::preProcessData(unsigned long int& numReads) {
636         try {
637         vector< vector< vector<string> > > filesToProcess;
638         
639         if (ffastqfile != "") { //reading one file
640             vector< vector<string> > files = readFastqFiles(numReads, ffastqfile, rfastqfile); 
641             //adjust for really large processors or really small files
642             if (numReads == 0) {  m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
643             if (numReads < processors) { 
644                 for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
645                 files.resize(numReads);
646                 processors = numReads; 
647             }
648             filesToProcess.push_back(files);
649         }else if (file != "") { //reading multiple files
650             //return only valid pairs
651             vector< vector<string> > filePairsToProcess = readFileNames(file);
652             
653             if (m->control_pressed) { return filesToProcess; }
654             
655             if (filePairsToProcess.size() != 0) {
656                 for (int i = 0; i < filePairsToProcess.size(); i++) {
657                     
658                     if (m->control_pressed) { for (int l = 0; l < filesToProcess.size(); l++) { for (int k = 0; k < filesToProcess[l].size(); k++) { for(int j = 0; j < filesToProcess[l][k].size(); j++) { m->mothurRemove(filesToProcess[l][k][j]); } filesToProcess[l][k].clear(); } return filesToProcess; } }
659                     
660                     unsigned long int thisFilesReads;
661                     vector< vector<string> > files = readFastqFiles(thisFilesReads, filePairsToProcess[i][0], filePairsToProcess[i][1]); 
662                     
663                     //adjust for really large processors or really small files
664                     if (thisFilesReads < processors) { 
665                         m->mothurOut("[ERROR]: " + filePairsToProcess[i][0] + " has less than " + toString(processors) + " good reads, skipping\n"); 
666                         for (int k = 0; k < files.size(); k++) { for(int j = 0; j < files[k].size(); j++) { m->mothurRemove(files[k][j]); } files[k].clear(); }
667                     }else {
668                         filesToProcess.push_back(files);
669                         numReads += thisFilesReads;
670                     }
671                 }
672                 //all files are bad
673                 if (numReads == 0) {  m->control_pressed = true; }
674             }
675         }else if (ffastafile != "") {
676             vector< vector<string> > files = readFastaFiles(numReads, ffastafile, rfastafile);
677             //adjust for really large processors or really small files
678             if (numReads == 0) {  m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
679             if (numReads < processors) { 
680                 for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
681                 files.resize(numReads);
682                 processors = numReads; 
683             }
684             filesToProcess.push_back(files);
685         }else { m->control_pressed = true; } //should not get here
686         
687         return filesToProcess;
688     }
689         catch(exception& e) {
690                 m->errorOut(e, "MakeContigsCommand", "preProcessData");
691                 exit(1);
692         }
693 }
694 //**********************************************************************************************************************
695 int MakeContigsCommand::createProcesses(vector< vector<string> > files, string outputFasta, string outputScrapFasta, string outputMisMatches, vector<vector<string> > fastaFileNames, int index) {
696         try {
697                 int num = 0;
698                 vector<int> processIDS;
699         string group = "";
700         map<int, string>::iterator it = file2Group.find(index);
701         if (it != file2Group.end()) { group = it->second; }
702         
703 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
704                 int process = 0;
705                 
706                 //loop through and create all the processes you want
707                 while (process != processors-1) {
708                         int pid = fork();
709                         
710                         if (pid > 0) {
711                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
712                                 process++;
713                         }else if (pid == 0){
714                 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
715                 
716                                 if(allFiles){
717                                         ofstream temp;
718                     
719                                         for(int i=0;i<tempFASTAFileNames.size();i++){
720                                                 for(int j=0;j<tempFASTAFileNames[i].size();j++){
721                                                         if (tempFASTAFileNames[i][j] != "") {
722                                                                 tempFASTAFileNames[i][j] += toString(getpid()) + ".temp";
723                                                                 m->openOutputFile(tempFASTAFileNames[i][j], temp);                      temp.close();
724                                                         }
725                                                 }
726                                         }
727                                 }
728                 
729                                 num = driver(files[process], 
730                              outputFasta + toString(getpid()) + ".temp", 
731                              outputScrapFasta + toString(getpid()) + ".temp", 
732                              outputMisMatches + toString(getpid()) + ".temp",
733                              tempFASTAFileNames, process, group);
734                                 
735                                 //pass groupCounts to parent
736                 ofstream out;
737                 string tempFile = toString(getpid()) + ".num.temp";
738                 m->openOutputFile(tempFile, out);
739                 out << num << endl;
740                                 if (createFileGroup || createOligosGroup) {
741                                         out << groupCounts.size() << endl;
742                                         
743                                         for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
744                                                 out << it->first << '\t' << it->second << endl;
745                                         }
746                     
747                     out << groupMap.size() << endl;
748                     for (map<string, string>::iterator it = groupMap.begin(); it != groupMap.end(); it++) {
749                                                 out << it->first << '\t' << it->second << endl;
750                                         }
751                                 }
752                 out.close();
753                                 
754                                 exit(0);
755                         }else { 
756                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
757                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
758                                 exit(0);
759                         }
760                 }
761                 
762         ofstream temp;
763                 m->openOutputFile(outputFasta, temp);           temp.close();
764         m->openOutputFile(outputScrapFasta, temp);              temp.close();
765                 
766                 //do my part
767                 num = driver(files[processors-1], outputFasta, outputScrapFasta,  outputMisMatches, fastaFileNames, processors-1, group);
768                 
769                 //force parent to wait until all the processes are done
770                 for (int i=0;i<processIDS.size();i++) { 
771                         int temp = processIDS[i];
772                         wait(&temp);
773                 }
774         
775                 for (int i = 0; i < processIDS.size(); i++) {
776             ifstream in;
777             string tempFile = toString(processIDS[i]) + ".num.temp";
778             m->openInputFile(tempFile, in);
779             int tempNum;
780             in >> tempNum; num += tempNum; m->gobble(in);
781             
782                         if (createFileGroup || createOligosGroup) {
783                                 string group;
784                                 in >> tempNum; m->gobble(in);
785                                 
786                                 if (tempNum != 0) {
787                                         for (int j = 0; j < tempNum; j++) { 
788                         int groupNum;
789                                                 in >> group >> groupNum; m->gobble(in);
790                         
791                                                 map<string, int>::iterator it = groupCounts.find(group);
792                                                 if (it == groupCounts.end()) {  groupCounts[group] = groupNum; }
793                                                 else { groupCounts[it->first] += groupNum; }
794                                         }
795                                 }
796                 in >> tempNum; m->gobble(in);
797                 if (tempNum != 0) {
798                                         for (int j = 0; j < tempNum; j++) { 
799                         string group, seqName;
800                                                 in >> seqName >> group; m->gobble(in);
801                         
802                                                 map<string, string>::iterator it = groupMap.find(seqName);
803                                                 if (it == groupMap.end()) {     groupMap[seqName] = group; }
804                                                 else { m->mothurOut("[ERROR]: " + seqName + " is in your fasta file more than once. Sequence names must be unique. please correct.\n");  }
805                                         }
806                                 }
807                         }
808             in.close(); m->mothurRemove(tempFile);
809         }
810     #else
811         
812         //////////////////////////////////////////////////////////////////////////////////////////////////////
813                 //Windows version shared memory, so be careful when passing variables through the contigsData struct. 
814                 //Above fork() will clone, so memory is separate, but that's not the case with windows, 
815                 //////////////////////////////////////////////////////////////////////////////////////////////////////
816                 
817                 vector<contigsData*> pDataArray; 
818                 DWORD   dwThreadIdArray[processors-1];
819                 HANDLE  hThreadArray[processors-1]; 
820                 
821                 //Create processor worker threads.
822                 for( int h=0; h<processors-1; h++ ){
823                         string extension = "";
824                         if (h != 0) { extension = toString(h) + ".temp"; processIDS.push_back(h); }
825             vector<vector<string> > tempFASTAFileNames = fastaFileNames;
826                         
827             if(allFiles){
828                 ofstream temp;
829                 
830                 for(int i=0;i<tempFASTAFileNames.size();i++){
831                     for(int j=0;j<tempFASTAFileNames[i].size();j++){
832                         if (tempFASTAFileNames[i][j] != "") {
833                             tempFASTAFileNames[i][j] += extension;
834                             m->openOutputFile(tempFASTAFileNames[i][j], temp);                  temp.close();
835                         }
836                     }
837                 }
838             }
839                                  
840                         contigsData* tempcontig = new contigsData(group, files[h], (outputFasta + extension), (outputScrapFasta + extension), (outputMisMatches + extension), align, m, match, misMatch, gapOpen, gapExtend, insert, deltaq, barcodes, primers, tempFASTAFileNames, barcodeNameVector, primerNameVector, pdiffs, bdiffs, tdiffs, createOligosGroup, createFileGroup, allFiles, trimOverlap, h);
841                         pDataArray.push_back(tempcontig);
842             
843                         hThreadArray[h] = CreateThread(NULL, 0, MyContigsThreadFunction, pDataArray[h], 0, &dwThreadIdArray[h]);   
844                 }
845         
846         vector<vector<string> > tempFASTAFileNames = fastaFileNames;
847
848         if(allFiles){
849             ofstream temp;
850             string extension = toString(processors-1) + ".temp";
851             
852             for(int i=0;i<tempFASTAFileNames.size();i++){
853                 for(int j=0;j<tempFASTAFileNames[i].size();j++){
854                     if (tempFASTAFileNames[i][j] != "") {
855                         tempFASTAFileNames[i][j] += extension;
856                         m->openOutputFile(tempFASTAFileNames[i][j], temp);                      temp.close();
857                     }
858                 }
859             }
860         }
861
862                 //parent do my part
863                 ofstream temp;
864                 m->openOutputFile(outputFasta, temp);           temp.close();
865         m->openOutputFile(outputScrapFasta, temp);              temp.close();
866         
867         //do my part
868         processIDS.push_back(processors-1);
869                 num = driver(files[processors-1], (outputFasta+ toString(processors-1) + ".temp"),  (outputScrapFasta+ toString(processors-1) + ".temp"),  (outputMisMatches+ toString(processors-1) + ".temp"), tempFASTAFileNames, processors-1, group);
870         
871                 //Wait until all threads have terminated.
872                 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
873                 
874                 //Close all thread handles and free memory allocations.
875                 for(int i=0; i < pDataArray.size(); i++){
876                         num += pDataArray[i]->count;
877             if (!pDataArray[i]->done) {
878                 m->mothurOut("[ERROR]: process " + toString(i) + " only processed " + toString(pDataArray[i]->count) + " of sequences assigned to it, quitting. \n"); m->control_pressed = true; 
879             }
880             for (map<string, int>::iterator it = pDataArray[i]->groupCounts.begin(); it != pDataArray[i]->groupCounts.end(); it++) {
881                 map<string, int>::iterator it2 = groupCounts.find(it->first);
882                 if (it2 == groupCounts.end()) { groupCounts[it->first] = it->second; }
883                 else { groupCounts[it->first] += it->second; }
884             }
885             for (map<string, string>::iterator it = pDataArray[i]->groupMap.begin(); it != pDataArray[i]->groupMap.end(); it++) {
886                 map<string, string>::iterator it2 = groupMap.find(it->first);
887                 if (it2 == groupMap.end()) {    groupMap[it->first] = it->second; }
888                 else { m->mothurOut("[ERROR]: " + it->first + " is in your fasta file more than once. Sequence names must be unique. please correct.\n");  }
889             }
890             CloseHandle(hThreadArray[i]);
891                         delete pDataArray[i];
892         }
893                                 
894     #endif      
895         
896         for (int i = 0; i < processIDS.size(); i++) {
897                         m->appendFiles((outputFasta + toString(processIDS[i]) + ".temp"), outputFasta);
898                         m->mothurRemove((outputFasta + toString(processIDS[i]) + ".temp"));
899                         
900                         m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta);
901                         m->mothurRemove((outputScrapFasta + toString(processIDS[i]) + ".temp"));
902             
903             m->appendFilesWithoutHeaders((outputMisMatches + toString(processIDS[i]) + ".temp"), outputMisMatches);
904                         m->mothurRemove((outputMisMatches + toString(processIDS[i]) + ".temp"));
905             
906             if(allFiles){
907                                 for(int j=0;j<fastaFileNames.size();j++){
908                                         for(int k=0;k<fastaFileNames[j].size();k++){
909                                                 if (fastaFileNames[j][k] != "") {
910                                                         m->appendFiles((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"), fastaFileNames[j][k]);
911                                                         m->mothurRemove((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"));
912                                                 }
913                                         }
914                                 }
915                         }
916                 }
917                 
918                 return num;
919         }
920         catch(exception& e) {
921                 m->errorOut(e, "MakeContigsCommand", "createProcesses");
922                 exit(1);
923         }
924 }
925 //**********************************************************************************************************************
926 int MakeContigsCommand::driver(vector<string> files, string outputFasta, string outputScrapFasta, string outputMisMatches, vector<vector<string> > fastaFileNames, int process, string group){
927     try {
928         
929         Alignment* alignment;
930         if(align == "gotoh")                    {       alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase);                 }
931                 else if(align == "needleman")   {       alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);                                }
932         
933         int num = 0;
934         string thisffastafile = files[0];
935         string thisfqualfile = files[1];
936         string thisrfastafile = files[2];
937         string thisrqualfile = files[3];
938         
939         if (m->debug) {  m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
940         
941         ifstream inFFasta, inRFasta, inFQual, inRQual;
942         ofstream outFasta, outMisMatch, outScrapFasta;
943         m->openInputFile(thisffastafile, inFFasta);
944         m->openInputFile(thisrfastafile, inRFasta);
945         if (thisfqualfile != "") {
946             m->openInputFile(thisfqualfile, inFQual);
947             m->openInputFile(thisrqualfile, inRQual);
948         }
949         m->openOutputFile(outputFasta, outFasta);
950         m->openOutputFile(outputScrapFasta, outScrapFasta);
951         m->openOutputFile(outputMisMatches, outMisMatch);
952         outMisMatch << "Name\tLength\tOverlap_Length\tOverlap_Start\tOverlap_End\tMisMatches\tNum_Ns\n";  
953         
954         TrimOligos trimOligos(pdiffs, bdiffs, 0, 0, primers, barcodes);
955         
956         while ((!inFFasta.eof()) && (!inRFasta.eof())) {
957             
958             if (m->control_pressed) { break; }
959             
960             int success = 1;
961             string trashCode = "";
962             int currentSeqsDiffs = 0;
963
964             //read seqs and quality info
965             Sequence fSeq(inFFasta); m->gobble(inFFasta);
966             Sequence rSeq(inRFasta); m->gobble(inRFasta);
967             QualityScores* fQual = NULL; QualityScores* rQual = NULL;
968             if (thisfqualfile != "") {
969                 fQual = new QualityScores(inFQual); m->gobble(inFQual);
970                 rQual = new QualityScores(inRQual); m->gobble(inRQual);
971             }
972             
973             int barcodeIndex = 0;
974             int primerIndex = 0;
975             
976             if(barcodes.size() != 0){
977                 if (thisfqualfile != "") {
978                     success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
979                 }else {
980                     success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
981                 }
982                 if(success > bdiffs)            {       trashCode += 'b';       }
983                 else{ currentSeqsDiffs += success;  }
984             }
985             
986             if(primers.size() != 0){
987                 if (thisfqualfile != "") {
988                     success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
989                 }else {
990                     success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
991                 }
992                 if(success > pdiffs)            {       trashCode += 'f';       }
993                 else{ currentSeqsDiffs += success;  }
994             }
995             
996             if (currentSeqsDiffs > tdiffs)      {       trashCode += 't';   }
997             
998             //flip the reverse reads
999             rSeq.reverseComplement();
1000             if (thisfqualfile != "") { rQual->flipQScores(); }
1001
1002             //pairwise align
1003             alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
1004             map<int, int> ABaseMap = alignment->getSeqAAlnBaseMap();
1005             map<int, int> BBaseMap = alignment->getSeqBAlnBaseMap();
1006             fSeq.setAligned(alignment->getSeqAAln());
1007             rSeq.setAligned(alignment->getSeqBAln());
1008             int length = fSeq.getAligned().length();
1009             
1010             //traverse alignments merging into one contiguous seq
1011             string contig = "";
1012             int numMismatches = 0;
1013             string seq1 = fSeq.getAligned();
1014             string seq2 = rSeq.getAligned();
1015             vector<int> scores1, scores2; 
1016             if (thisfqualfile != "") {
1017                 scores1 = fQual->getQualityScores();
1018                 scores2 = rQual->getQualityScores();
1019                 delete fQual; delete rQual;
1020             }
1021             
1022             // if (num < 5) {  cout << fSeq.getStartPos() << '\t' << fSeq.getEndPos() << '\t' << rSeq.getStartPos() << '\t' << rSeq.getEndPos() << endl; }
1023             int overlapStart = fSeq.getStartPos();
1024             int seq2Start = rSeq.getStartPos();
1025             
1026             //bigger of the 2 starting positions is the location of the overlapping start
1027             if (overlapStart < seq2Start) { //seq2 starts later so take from 0 to seq2Start from seq1
1028                 overlapStart = seq2Start; 
1029                 for (int i = 0; i < overlapStart; i++) { contig += seq1[i];  }
1030             }else { //seq1 starts later so take from 0 to overlapStart from seq2
1031                 for (int i = 0; i < overlapStart; i++) {  contig += seq2[i]; }
1032             }
1033             
1034             int seq1End = fSeq.getEndPos();
1035             int seq2End = rSeq.getEndPos();
1036             int overlapEnd = seq1End;
1037             if (seq2End < overlapEnd) { overlapEnd = seq2End; }  //smallest end position is where overlapping ends
1038             
1039             int oStart = contig.length();
1040             for (int i = overlapStart; i < overlapEnd; i++) {
1041                 if (seq1[i] == seq2[i]) { //match, add base and choose highest score
1042                     contig += seq1[i];
1043                 }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below insert. In that case eliminate base
1044                     if (thisfqualfile != "") {
1045                         if (scores2[BBaseMap[i]] <= insert) { } //
1046                         else { contig += seq2[i];  }
1047                     }else { contig += seq2[i]; } //with no quality info, then we keep it?
1048                 }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below insert. In that case eliminate base
1049                     if (thisfqualfile != "") {
1050                         if (scores1[ABaseMap[i]] <= insert) { } //
1051                         else { contig += seq1[i];  }
1052                     }else { contig += seq1[i]; } //with no quality info, then we keep it?
1053                 }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
1054                     if (thisfqualfile != "") {
1055                         if (abs(scores1[ABaseMap[i]] - scores2[BBaseMap[i]]) >= deltaq) { //is the difference in qual scores >= deltaq, if yes choose base with higher score
1056                             char c = seq1[i];
1057                             if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { c = seq2[i]; }
1058                             contig += c;
1059                         }else { //if no, base becomes n
1060                             contig += 'N';
1061                         }
1062                         numMismatches++;
1063                     }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch
1064                 }else { //should never get here
1065                     m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
1066                 }
1067             }
1068             int oend = contig.length();
1069             if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
1070                 for (int i = overlapEnd; i < length; i++) { contig += seq2[i];  }
1071             }else { //seq2 ends before seq1 so take from overlap to length from seq1
1072                 for (int i = overlapEnd; i < length; i++) {  contig += seq1[i]; }
1073             }
1074             
1075             if (trimOverlap) { contig = contig.substr(overlapStart-1, oend-oStart);  if (contig.length() == 0) { trashCode += "l"; } }
1076             
1077             if(trashCode.length() == 0){
1078                 bool ignore = false;
1079                 
1080                 if (m->debug) { m->mothurOut(fSeq.getName()); }
1081                 
1082                 if (createOligosGroup) {
1083                     if(barcodes.size() != 0){
1084                         string thisGroup = barcodeNameVector[barcodeIndex];
1085                         if (primers.size() != 0) { 
1086                             if (primerNameVector[primerIndex] != "") { 
1087                                 if(thisGroup != "") {
1088                                     thisGroup += "." + primerNameVector[primerIndex]; 
1089                                 }else {
1090                                     thisGroup = primerNameVector[primerIndex]; 
1091                                 }
1092                             } 
1093                         }
1094                         
1095                         if (m->debug) { m->mothurOut(", group= " + thisGroup + "\n"); }
1096                         
1097                         int pos = thisGroup.find("ignore");
1098                         if (pos == string::npos) {
1099                             groupMap[fSeq.getName()] = thisGroup; 
1100                         
1101                             map<string, int>::iterator it = groupCounts.find(thisGroup);
1102                             if (it == groupCounts.end()) {      groupCounts[thisGroup] = 1; }
1103                             else { groupCounts[it->first] ++; }
1104                         }else { ignore = true; }
1105                         
1106                     }
1107                 }else if (createFileGroup) {
1108                     int pos = group.find("ignore");
1109                     if (pos == string::npos) {
1110                         groupMap[fSeq.getName()] = group;
1111                         
1112                         map<string, int>::iterator it = groupCounts.find(group);
1113                         if (it == groupCounts.end()) {  groupCounts[group] = 1; }
1114                         else { groupCounts[it->first] ++; }
1115                     }else { ignore = true; }
1116                 }
1117                 if (m->debug) { m->mothurOut("\n"); }
1118                 
1119                 if(allFiles && !ignore){
1120                     ofstream output;
1121                     m->openOutputFileAppend(fastaFileNames[barcodeIndex][primerIndex], output);
1122                     output << ">" << fSeq.getName() << endl << contig << endl;
1123                     output.close();
1124                 }
1125                 
1126                 //output
1127                 outFasta << ">" << fSeq.getName() << endl << contig << endl;
1128                 int numNs = 0;
1129                 for (int i = 0; i < contig.length(); i++) { if (contig[i] == 'N') { numNs++; }  }
1130                 outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << (oend-oStart) << '\t' << oStart << '\t' << oend << '\t' << numMismatches << '\t' << numNs << endl;
1131             }else {
1132                 //output
1133                 outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl;
1134             }
1135             num++;
1136             
1137                         //report progress
1138                         if((num) % 1000 == 0){  m->mothurOut(toString(num)); m->mothurOutEndLine();             }
1139                 }
1140         
1141                 //report progress
1142                 if((num) % 1000 != 0){  m->mothurOut(toString(num)); m->mothurOutEndLine();             }
1143         
1144         inFFasta.close();
1145         inRFasta.close();
1146         outFasta.close();
1147         outScrapFasta.close();
1148         outMisMatch.close();
1149         if (thisfqualfile != "") {
1150             inFQual.close();
1151             inRQual.close();
1152         }
1153         delete alignment;
1154         
1155         if (m->control_pressed) {  m->mothurRemove(outputFasta); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches);  }
1156     
1157         return num;
1158     }
1159         catch(exception& e) {
1160                 m->errorOut(e, "MakeContigsCommand", "driver");
1161                 exit(1);
1162         }
1163 }
1164 //**********************************************************************************************************************
1165 vector< vector<string> > MakeContigsCommand::readFastqFiles(unsigned long int& count, string ffastq, string rfastq){
1166     try {
1167         vector< vector<string> > files;
1168         //maps processors number to file pointer
1169         map<int, vector<ofstream*> > tempfiles;  //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1170         map<int, vector<ofstream*> >::iterator it;
1171         
1172         //create files to write to
1173         for (int i = 0; i < processors; i++) {
1174             vector<ofstream*> temp;
1175             ofstream* outFF = new ofstream;     temp.push_back(outFF);
1176             ofstream* outFQ = new ofstream;     temp.push_back(outFQ);
1177             ofstream* outRF = new ofstream;     temp.push_back(outRF);
1178             ofstream* outRQ = new ofstream;     temp.push_back(outRQ);
1179             tempfiles[i] = temp;
1180             
1181             vector<string> names;
1182             string thisOutputDir = outputDir;
1183             if (outputDir == "") { thisOutputDir = m->hasPath(ffastq); }
1184             string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "ffastatemp";
1185             string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rfastatemp";
1186             string fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "fqualtemp";
1187             string rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rqualtemp";
1188             names.push_back(ffastafilename); names.push_back(fqualfilename);
1189             names.push_back(rfastafilename); names.push_back(rqualfilename);
1190             files.push_back(names);
1191             
1192             m->openOutputFile(ffastafilename, *outFF);
1193             m->openOutputFile(rfastafilename, *outRF);
1194             m->openOutputFile(fqualfilename, *outFQ);
1195             m->openOutputFile(rqualfilename, *outRQ);
1196         }
1197         
1198         if (m->control_pressed) {
1199             //close files, delete ofstreams
1200             for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close();  delete (it->second)[i]; } }
1201             //remove files
1202             for (int i = 0; i < files.size(); i++) {  
1203                 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1204             }
1205         }
1206         
1207         ifstream inForward;
1208         m->openInputFile(ffastq, inForward);
1209         
1210         ifstream inReverse;
1211         m->openInputFile(rfastq, inReverse);
1212         
1213         count = 0;
1214         map<string, fastqRead> uniques;
1215         map<string, fastqRead>::iterator itUniques;
1216         while ((!inForward.eof()) || (!inReverse.eof())) {
1217             
1218             if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close();  delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) {  for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inForward.close(); inReverse.close(); return files; }
1219             
1220             //get a read from forward and reverse fastq files
1221             bool ignoref, ignorer;
1222             fastqRead thisFread, thisRread;
1223             if (!inForward.eof()) {  thisFread = readFastq(inForward, ignoref); }
1224             else { ignoref = true; }
1225             if (!inReverse.eof()) { thisRread = readFastq(inReverse, ignorer);  }
1226             else { ignorer = true; }
1227             
1228             vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1229            
1230             for (int i = 0; i < reads.size(); i++) {
1231                 fastqRead fread = reads[i].forward;
1232                 fastqRead rread = reads[i].reverse;
1233                 
1234                 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1235                
1236                 //if (checkReads(fread, rread, ffastq, rfastq)) {
1237                     if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close();  delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) {  for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inForward.close(); inReverse.close(); return files; }
1238                     
1239                     //if the reads are okay write to output files
1240                     int process = count % processors;
1241                     
1242                     *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1243                     *(tempfiles[process][1]) << ">" << fread.name << endl;
1244                     for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1245                     *(tempfiles[process][1]) << endl;
1246                     *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1247                     *(tempfiles[process][3]) << ">" << rread.name << endl;
1248                     for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1249                     *(tempfiles[process][3]) << endl;
1250                     
1251                     count++;
1252                     
1253                     //report progress
1254                     if((count) % 10000 == 0){   m->mothurOut(toString(count)); m->mothurOutEndLine();           }
1255                 //}
1256             }
1257                 }
1258                 //report progress
1259                 if((count) % 10000 != 0){       m->mothurOut(toString(count)); m->mothurOutEndLine();           }
1260         
1261         if (uniques.size() != 0) {
1262             for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1263                 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1264             }
1265             m->mothurOutEndLine();
1266         }
1267         
1268         //close files, delete ofstreams
1269         for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close();  delete (it->second)[i]; } }
1270         inForward.close();
1271         inReverse.close();
1272         
1273         return files;
1274     }
1275     catch(exception& e) {
1276         m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1277         exit(1);
1278     }
1279 }
1280 //**********************************************************************************************************************
1281 vector< vector<string> > MakeContigsCommand::readFastaFiles(unsigned long int& count, string ffasta, string rfasta){
1282     try {
1283         vector< vector<string> > files;
1284         //maps processors number to file pointer
1285         map<int, vector<ofstream*> > tempfiles;  //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1286         map<int, vector<ofstream*> >::iterator it;
1287         
1288         //create files to write to
1289         for (int i = 0; i < processors; i++) {
1290             vector<ofstream*> temp;
1291             ofstream* outFF = new ofstream;     temp.push_back(outFF);
1292             ofstream* outFQ = new ofstream;     temp.push_back(outFQ);
1293             ofstream* outRF = new ofstream;     temp.push_back(outRF);
1294             ofstream* outRQ = new ofstream;     temp.push_back(outRQ);
1295             tempfiles[i] = temp;
1296             
1297             vector<string> names;
1298             string thisOutputDir = outputDir;
1299             if (outputDir == "") { thisOutputDir = m->hasPath(ffasta); }
1300             string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffasta)) + toString(i) + "ffastatemp";
1301             string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfasta)) + toString(i) + "rfastatemp";
1302             string fqualfilename = "";
1303             if (fqualfile != "") { fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(fqualfile)) + toString(i) + "fqual.temp";  m->openOutputFile(fqualfilename, *outFQ); }
1304             string rqualfilename = "";
1305             if (rqualfile != "") { rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rqualfile)) + toString(i) + "rqual.temp"; m->openOutputFile(rqualfilename, *outRQ); }
1306             names.push_back(ffastafilename); names.push_back(fqualfilename);
1307             names.push_back(rfastafilename); names.push_back(rqualfilename);
1308             files.push_back(names);
1309             
1310             m->openOutputFile(ffastafilename, *outFF);
1311             m->openOutputFile(rfastafilename, *outRF);
1312         }
1313         
1314         if (m->control_pressed) {
1315             //close files, delete ofstreams
1316             for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close();  delete (it->second)[i]; } }
1317             //remove files
1318             for (int i = 0; i < files.size(); i++) {  
1319                 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1320             }
1321         }
1322         
1323         ifstream inForwardFasta;
1324         m->openInputFile(ffasta, inForwardFasta);
1325         
1326         ifstream inReverseFasta;
1327         m->openInputFile(rfasta, inReverseFasta);
1328         
1329         ifstream inForwardQual, inReverseQual;
1330         if (fqualfile != "") { m->openInputFile(fqualfile, inForwardQual); m->openInputFile(rqualfile, inReverseQual); }
1331         
1332         count = 0;
1333         map<string, fastqRead> uniques;
1334         map<string, fastqRead>::iterator itUniques;
1335         while ((!inForwardFasta.eof()) || (!inReverseFasta.eof())) {
1336             
1337             if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close();  delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) {  for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; }
1338             
1339             //get a reads from forward and reverse fasta files
1340             bool ignoref, ignorer;
1341             fastqRead thisFread, thisRread;
1342             if (!inForwardFasta.eof()) {  
1343                 ignoref = false; 
1344                 Sequence temp(inForwardFasta); m->gobble(inForwardFasta);
1345                 thisFread.name = temp.getName();
1346                 thisFread.sequence = temp.getUnaligned();
1347             }else { ignoref = true; }
1348             if (!inReverseFasta.eof()) {  
1349                 ignorer = false; 
1350                 Sequence temp(inReverseFasta); m->gobble(inReverseFasta);
1351                 thisRread.name = temp.getName();
1352                 thisRread.sequence = temp.getUnaligned();  
1353             }else { ignorer = true; }
1354             
1355             //get qual reads if given
1356             if (fqualfile != "") {
1357                 if (!inForwardQual.eof() && !ignoref) {  
1358                     QualityScores temp(inForwardQual); m->gobble(inForwardQual);
1359                     //if forward files dont match ignore read
1360                     if (thisFread.name != temp.getName()) { ignoref = true; } 
1361                     else { thisFread.scores = temp.getQualityScores(); }
1362                 }else { ignoref = true; }
1363                 if (!inReverseQual.eof() && !ignorer) {  
1364                     QualityScores temp(inReverseQual); m->gobble(inReverseQual);
1365                     //if reverse files dont match ignore read
1366                     if (thisRread.name != temp.getName()) { ignorer = true; } 
1367                     else { thisRread.scores = temp.getQualityScores(); }
1368                 }else { ignorer = true; }
1369             }
1370             
1371             vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1372             
1373             for (int i = 0; i < reads.size(); i++) {
1374                 fastqRead fread = reads[i].forward;
1375                 fastqRead rread = reads[i].reverse;
1376                 
1377                 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1378                 
1379                // if (checkReads(fread, rread, ffasta, rfasta)) {
1380                     if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close();  delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) {  for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; }
1381                     
1382                     //if the reads are okay write to output files
1383                     int process = count % processors;
1384                     
1385                     *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1386                     *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1387                     if (fqualfile != "") { //if you have quality info, print it
1388                         *(tempfiles[process][1]) << ">" << fread.name << endl;
1389                         for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1390                         *(tempfiles[process][1]) << endl;
1391                         *(tempfiles[process][3]) << ">" << rread.name << endl;
1392                         for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1393                         *(tempfiles[process][3]) << endl;
1394                     }
1395                     count++;
1396                     
1397                     //report progress
1398                     if((count) % 10000 == 0){   m->mothurOut(toString(count)); m->mothurOutEndLine();           }
1399                 //}
1400             }
1401                 }
1402                 //report progress
1403                 if((count) % 10000 != 0){       m->mothurOut(toString(count)); m->mothurOutEndLine();           }
1404         
1405         if (uniques.size() != 0) {
1406             for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1407                 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1408             }
1409             m->mothurOutEndLine();
1410         }
1411         
1412         //close files, delete ofstreams
1413         for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close();  delete (it->second)[i]; } }
1414         inReverseFasta.close(); 
1415         inForwardFasta.close(); 
1416         if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); }
1417         
1418         return files;
1419     }
1420     catch(exception& e) {
1421         m->errorOut(e, "MakeContigsCommand", "readFastaFiles");
1422         exit(1);
1423     }
1424 }
1425 //**********************************************************************************************************************
1426 vector<pairFastqRead> MakeContigsCommand::getReads(bool ignoref, bool ignorer, fastqRead forward, fastqRead reverse, map<string, fastqRead>& uniques){
1427     try {
1428         vector<pairFastqRead> reads;
1429         map<string, fastqRead>::iterator itUniques;
1430             
1431         if (!ignoref && !ignorer) {
1432             if (forward.name == reverse.name) { 
1433                 pairFastqRead temp(forward, reverse);
1434                 reads.push_back(temp);
1435             }else {
1436                 bool match = false;
1437                 //if no match are the names only different by 1 and 2?
1438                 string tempFRead = forward.name.substr(0, forward.name.length()-1);
1439                 string tempRRead = reverse.name.substr(0, reverse.name.length()-1);
1440                 if (tempFRead == tempRRead) {
1441                     if ((forward.name[forward.name.length()-1] == '1') && (reverse.name[reverse.name.length()-1] == '2')) {
1442                         forward.name = tempFRead;
1443                         reverse.name = tempRRead;
1444                         pairFastqRead temp(forward, reverse);
1445                         reads.push_back(temp);
1446                         match = true;
1447                     }
1448                 }
1449                 
1450                 if (!match) {
1451                     //look for forward pair
1452                     itUniques = uniques.find(forward.name);
1453                     if (itUniques != uniques.end()) {  //we have the pair for this read
1454                         pairFastqRead temp(forward, itUniques->second);
1455                         reads.push_back(temp);
1456                         uniques.erase(itUniques);
1457                     }else { //save this read for later
1458                         uniques[forward.name] = forward;
1459                     }
1460                     
1461                     //look for reverse pair
1462                     itUniques = uniques.find(reverse.name);
1463                     if (itUniques != uniques.end()) {  //we have the pair for this read
1464                         pairFastqRead temp(itUniques->second, reverse);
1465                         reads.push_back(temp);
1466                         uniques.erase(itUniques);
1467                     }else { //save this read for later
1468                         uniques[reverse.name] = reverse;
1469                     }
1470                 }
1471                                 
1472             }
1473         }else if (!ignoref && ignorer) { //ignore reverse keep forward
1474             //look for forward pair
1475             itUniques = uniques.find(forward.name);
1476             if (itUniques != uniques.end()) {  //we have the pair for this read
1477                 pairFastqRead temp(forward, itUniques->second);
1478                 reads.push_back(temp);
1479                 uniques.erase(itUniques);
1480             }else { //save this read for later
1481                 uniques[forward.name] = forward;
1482             }
1483
1484         }else if (ignoref && !ignorer) { //ignore forward keep reverse
1485             //look for reverse pair
1486             itUniques = uniques.find(reverse.name);
1487             if (itUniques != uniques.end()) {  //we have the pair for this read
1488                 pairFastqRead temp(itUniques->second, reverse);
1489                 reads.push_back(temp);
1490                 uniques.erase(itUniques);
1491             }else { //save this read for later
1492                 uniques[reverse.name] = reverse;
1493             }
1494         }//else ignore both and do nothing
1495         
1496         return reads;
1497     }
1498     catch(exception& e) {
1499         m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1500         exit(1);
1501     }
1502 }
1503 //**********************************************************************************************************************
1504 fastqRead MakeContigsCommand::readFastq(ifstream& in, bool& ignore){
1505     try {
1506         fastqRead read;
1507         
1508         ignore = false;
1509         
1510         //read sequence name
1511         string line = m->getline(in); m->gobble(in);
1512         vector<string> pieces = m->splitWhiteSpace(line);
1513         string name = "";  if (pieces.size() != 0) { name = pieces[0]; }
1514         if (name == "") {  m->mothurOut("[WARNING]: Blank fasta name, ignoring read."); m->mothurOutEndLine(); ignore=true;  }
1515         else if (name[0] != '@') { m->mothurOut("[WARNING]: reading " + name + " expected a name with @ as a leading character, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1516         else { name = name.substr(1); }
1517         
1518         //read sequence
1519         string sequence = m->getline(in); m->gobble(in);
1520         if (sequence == "") {  m->mothurOut("[WARNING]: missing sequence for " + name + ", ignoring."); ignore=true; }
1521         
1522         //read sequence name
1523         line = m->getline(in); m->gobble(in);
1524         pieces = m->splitWhiteSpace(line);
1525         string name2 = "";  if (pieces.size() != 0) { name2 = pieces[0]; }
1526         if (name2 == "") {  m->mothurOut("[WARNING]: expected a name with + as a leading character, ignoring."); ignore=true; }
1527         else if (name2[0] != '+') { m->mothurOut("[WARNING]: reading " + name2 + " expected a name with + as a leading character, ignoring."); ignore=true; }
1528         else { name2 = name2.substr(1); if (name2 == "") { name2 = name; } }
1529         
1530         //read quality scores
1531         string quality = m->getline(in); m->gobble(in);
1532         if (quality == "") {  m->mothurOut("[WARNING]: missing quality for " + name2 + ", ignoring."); ignore=true; }
1533          
1534         //sanity check sequence length and number of quality scores match
1535         if (name2 != "") { if (name != name2) { m->mothurOut("[WARNING]: names do not match. read " + name + " for fasta and " + name2 + " for quality, ignoring."); ignore=true; } }
1536         if (quality.length() != sequence.length()) { m->mothurOut("[WARNING]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores, ignoring read."); ignore=true; }
1537         
1538         vector<int> qualScores = convertQual(quality);
1539         
1540         read.name = name;
1541         read.sequence = sequence;
1542         read.scores = qualScores;
1543
1544         return read;
1545     }
1546     catch(exception& e) {
1547         m->errorOut(e, "MakeContigsCommand", "readFastq");
1548         exit(1);
1549     }
1550 }
1551 /**********************************************************************************************************************
1552 bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse, string ffile, string rfile){
1553     try {
1554         bool good = true;
1555         
1556         //do sequence lengths match
1557         if (forward.sequence.length() != reverse.sequence.length()) {
1558             m->mothurOut("[WARNING]: For sequence " + forward.name + " I read a sequence of length " + toString(forward.sequence.length()) + " from " + ffile + ", but read a sequence of length " + toString(reverse.sequence.length()) + " from " + rfile + ", ignoring.\n");
1559             good = false; 
1560         }
1561         
1562         //do number of qual scores match 
1563         if (forward.scores.size() != reverse.scores.size()) {
1564             m->mothurOut("[WARNING]: For sequence " + forward.name + " I read " + toString(forward.scores.size()) + " quality scores from " + ffile + ", but read  " + toString(reverse.scores.size()) + " quality scores from " + rfile + ", ignoring.\n");
1565             good = false; 
1566         }
1567
1568         return good;
1569     }
1570     catch(exception& e) {
1571         m->errorOut(e, "MakeContigsCommand", "checkReads");
1572         exit(1);
1573     }
1574 }*/
1575 //***************************************************************************************************************
1576 vector< vector<string> > MakeContigsCommand::readFileNames(string filename){
1577         try {
1578         vector< vector<string> > files;
1579         string forward, reverse;
1580         
1581         ifstream in;
1582         m->openInputFile(filename, in);
1583         
1584         while(!in.eof()) {
1585             
1586             if (m->control_pressed) { return files; }
1587             
1588             in >> forward; m->gobble(in);
1589             in >> reverse;
1590             
1591             string group = "";
1592             while (!in.eof())   { //do we have a group assigned to this pair
1593                 char c = in.get();
1594                 if (c == 10 || c == 13 || c == -1){     break;  }
1595                 else if (c == 32 || c == 9){;} //space or tab
1596                 else {  group += c;  }
1597             }
1598             
1599             if (group != "") {
1600                 //line in file look like: group forward reverse
1601                 string temp = forward;
1602                 forward = reverse;
1603                 reverse = group;
1604                 group = temp;
1605                 createFileGroup = true;
1606             }
1607             m->gobble(in);
1608             
1609             
1610             //check to make sure both are able to be opened
1611             ifstream in2;
1612             int openForward = m->openInputFile(forward, in2, "noerror");
1613             
1614             //if you can't open it, try default location
1615             if (openForward == 1) {
1616                 if (m->getDefaultPath() != "") { //default path is set
1617                     string tryPath = m->getDefaultPath() + m->getSimpleName(forward);
1618                     m->mothurOut("Unable to open " + forward + ". Trying default " + tryPath); m->mothurOutEndLine();
1619                     ifstream in3;
1620                     openForward = m->openInputFile(tryPath, in3, "noerror");
1621                     in3.close();
1622                     forward = tryPath;
1623                 }
1624             }
1625             
1626             //if you can't open it, try output location
1627             if (openForward == 1) {
1628                 if (m->getOutputDir() != "") { //default path is set
1629                     string tryPath = m->getOutputDir() + m->getSimpleName(forward);
1630                     m->mothurOut("Unable to open " + forward + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1631                     ifstream in4;
1632                     openForward = m->openInputFile(tryPath, in4, "noerror");
1633                     forward = tryPath;
1634                     in4.close();
1635                 }
1636             }
1637             
1638             if (openForward == 1) { //can't find it
1639                 m->mothurOut("[WARNING]: can't find " + forward + ", ignoring pair.\n"); 
1640             }else{  in2.close();  }
1641             
1642             ifstream in3;
1643             int openReverse = m->openInputFile(reverse, in3, "noerror");
1644             
1645             //if you can't open it, try default location
1646             if (openReverse == 1) {
1647                 if (m->getDefaultPath() != "") { //default path is set
1648                     string tryPath = m->getDefaultPath() + m->getSimpleName(reverse);
1649                     m->mothurOut("Unable to open " + reverse + ". Trying default " + tryPath); m->mothurOutEndLine();
1650                     ifstream in3;
1651                     openReverse = m->openInputFile(tryPath, in3, "noerror");
1652                     in3.close();
1653                     reverse = tryPath;
1654                 }
1655             }
1656             
1657             //if you can't open it, try output location
1658             if (openReverse == 1) {
1659                 if (m->getOutputDir() != "") { //default path is set
1660                     string tryPath = m->getOutputDir() + m->getSimpleName(reverse);
1661                     m->mothurOut("Unable to open " + reverse + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1662                     ifstream in4;
1663                     openReverse = m->openInputFile(tryPath, in4, "noerror");
1664                     reverse = tryPath;
1665                     in4.close();
1666                 }
1667             }
1668             
1669             if (openReverse == 1) { //can't find it
1670                 m->mothurOut("[WARNING]: can't find " + reverse + ", ignoring pair.\n"); 
1671             }else{  in3.close();  }
1672             
1673             if ((openForward != 1) && (openReverse != 1)) { //good pair
1674                 file2Group[files.size()] = group;
1675                 vector<string> pair;
1676                 pair.push_back(forward);
1677                 pair.push_back(reverse);
1678                 files.push_back(pair);
1679             }
1680         }
1681         in.close();
1682         
1683         return files;
1684     }
1685     catch(exception& e) {
1686         m->errorOut(e, "MakeContigsCommand", "checkReads");
1687         exit(1);
1688     }
1689 }
1690 //***************************************************************************************************************
1691 //illumina data requires paired forward and reverse data
1692 //BARCODE   atgcatgc   atgcatgc    groupName 
1693 //PRIMER   atgcatgc   atgcatgc    groupName  
1694 //PRIMER   atgcatgc   atgcatgc  
1695 bool MakeContigsCommand::getOligos(vector<vector<string> >& fastaFileNames, string rootname){
1696         try {
1697                 ifstream in;
1698                 m->openInputFile(oligosfile, in);
1699                 
1700                 ofstream test;
1701                 
1702                 string type, foligo, roligo, group;
1703         
1704                 int indexPrimer = 0;
1705                 int indexBarcode = 0;
1706         set<string> uniquePrimers;
1707         set<string> uniqueBarcodes;
1708                 
1709                 while(!in.eof()){
1710             
1711                         in >> type; 
1712     
1713                         if (m->debug) { m->mothurOut("[DEBUG]: reading type - " + type + ".\n"); }      
1714             
1715                         if(type[0] == '#'){
1716                                 while (!in.eof())       {       char c = in.get();  if (c == 10 || c == 13){    break;  }       } // get rest of line if there's any crap there
1717                                 m->gobble(in);
1718                         }
1719                         else{
1720                                 m->gobble(in);
1721                                 //make type case insensitive
1722                                 for(int i=0;i<type.length();i++){       type[i] = toupper(type[i]);  }
1723                                 
1724                                 in >> foligo;
1725                 
1726                 if (m->debug) { m->mothurOut("[DEBUG]: reading - " + foligo + ".\n"); }
1727                                 
1728                                 for(int i=0;i<foligo.length();i++){
1729                                         foligo[i] = toupper(foligo[i]);
1730                                         if(foligo[i] == 'U')    {       foligo[i] = 'T';        }
1731                                 }
1732                                 
1733                                 if(type == "PRIMER"){
1734                                         m->gobble(in);
1735                                         
1736                     in >> roligo;
1737                     
1738                     for(int i=0;i<roligo.length();i++){
1739                         roligo[i] = toupper(roligo[i]);
1740                         if(roligo[i] == 'U')    {       roligo[i] = 'T';        }
1741                     }
1742                     //roligo = reverseOligo(roligo);
1743                     
1744                     group = "";
1745                     
1746                                         // get rest of line in case there is a primer name
1747                                         while (!in.eof())       {       
1748                                                 char c = in.get(); 
1749                                                 if (c == 10 || c == 13){        break;  }
1750                                                 else if (c == 32 || c == 9){;} //space or tab
1751                                                 else {  group += c;  }
1752                                         } 
1753                     
1754                     oligosPair newPrimer(foligo, roligo);
1755                                         
1756                                         //check for repeat barcodes
1757                     string tempPair = foligo+roligo;
1758                     if (uniquePrimers.count(tempPair) != 0) { m->mothurOut("primer pair " + newPrimer.forward + " " + newPrimer.reverse + " is in your oligos file already."); m->mothurOutEndLine();  }
1759                     else { uniquePrimers.insert(tempPair); }
1760                                         
1761                     if (m->debug) {  if (group != "") { m->mothurOut("[DEBUG]: reading group " + group + ".\n"); }else{ m->mothurOut("[DEBUG]: no group for primer pair " + newPrimer.forward + " " + newPrimer.reverse + ".\n"); }  }
1762                     
1763                                         primers[indexPrimer]=newPrimer; indexPrimer++;          
1764                                         primerNameVector.push_back(group);
1765                                 }else if(type == "BARCODE"){
1766                                         m->gobble(in);
1767                                         
1768                     in >> roligo;
1769                     
1770                     for(int i=0;i<roligo.length();i++){
1771                         roligo[i] = toupper(roligo[i]);
1772                         if(roligo[i] == 'U')    {       roligo[i] = 'T';        }
1773                     }
1774                     //roligo = reverseOligo(roligo);
1775                     
1776                     oligosPair newPair(foligo, roligo);
1777                     
1778                     group = "";
1779                     while (!in.eof())   {       
1780                                                 char c = in.get(); 
1781                                                 if (c == 10 || c == 13){        break;  }
1782                                                 else if (c == 32 || c == 9){;} //space or tab
1783                                                 else {  group += c;  }
1784                                         } 
1785                                         
1786                     if (m->debug) { m->mothurOut("[DEBUG]: barcode pair " + newPair.forward + " " + newPair.reverse + ", and group = " + group + ".\n"); }
1787                         
1788                     //check for repeat barcodes
1789                     string tempPair = foligo+roligo;
1790                     if (uniqueBarcodes.count(tempPair) != 0) { m->mothurOut("barcode pair " + newPair.forward + " " + newPair.reverse +  " is in your oligos file already, disregarding."); m->mothurOutEndLine();  }
1791                     else { uniqueBarcodes.insert(tempPair); }
1792                         
1793                     barcodes[indexBarcode]=newPair; indexBarcode++;
1794                                         barcodeNameVector.push_back(group);
1795                                 }else if(type == "LINKER"){
1796                                         linker.push_back(foligo);
1797                     m->mothurOut("[WARNING]: make.contigs is not setup to remove linkers, ignoring.\n");
1798                                 }else if(type == "SPACER"){
1799                                         spacer.push_back(foligo);
1800                     m->mothurOut("[WARNING]: make.contigs is not setup to remove spacers, ignoring.\n");
1801                                 }
1802                                 else{   m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are primer, barcode, linker and spacer. Ignoring " + foligo + "."); m->mothurOutEndLine(); }
1803                         }
1804                         m->gobble(in);
1805                 }       
1806                 in.close();
1807                 
1808                 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0;   }
1809                 
1810                 //add in potential combos
1811                 if(barcodeNameVector.size() == 0){
1812             oligosPair temp("", "");
1813                         barcodes[0] = temp;
1814                         barcodeNameVector.push_back("");                        
1815                 }
1816                 
1817                 if(primerNameVector.size() == 0){
1818             oligosPair temp("", "");
1819                         primers[0] = temp;
1820                         primerNameVector.push_back("");                 
1821                 }
1822                 
1823                 fastaFileNames.resize(barcodeNameVector.size());
1824                 for(int i=0;i<fastaFileNames.size();i++){
1825                         fastaFileNames[i].assign(primerNameVector.size(), "");
1826                 }
1827                 
1828                 if(allFiles){
1829                         set<string> uniqueNames; //used to cleanup outputFileNames
1830                         for(map<int, oligosPair>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1831                                 for(map<int, oligosPair>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1832                                         
1833                                         string primerName = primerNameVector[itPrimer->first];
1834                                         string barcodeName = barcodeNameVector[itBar->first];
1835                     
1836                     if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing 
1837                                         else {
1838                         string comboGroupName = "";
1839                         string fastaFileName = "";
1840                         string qualFileName = "";
1841                         string nameFileName = "";
1842                         string countFileName = "";
1843                         
1844                         if(primerName == ""){
1845                             comboGroupName = barcodeNameVector[itBar->first];
1846                         }
1847                         else{
1848                             if(barcodeName == ""){
1849                                 comboGroupName = primerNameVector[itPrimer->first];
1850                             }
1851                             else{
1852                                 comboGroupName = barcodeNameVector[itBar->first] + "." + primerNameVector[itPrimer->first];
1853                             }
1854                         }
1855                         
1856                         
1857                         ofstream temp;
1858                         fastaFileName = rootname + comboGroupName + ".fasta";
1859                         if (uniqueNames.count(fastaFileName) == 0) {
1860                             outputNames.push_back(fastaFileName);
1861                             outputTypes["fasta"].push_back(fastaFileName);
1862                             uniqueNames.insert(fastaFileName);
1863                         }
1864                         
1865                         fastaFileNames[itBar->first][itPrimer->first] = fastaFileName;
1866                         m->openOutputFile(fastaFileName, temp);         temp.close();
1867                     }
1868                                 }
1869                         }
1870                 }
1871                 
1872                 bool allBlank = true;
1873                 for (int i = 0; i < barcodeNameVector.size(); i++) {
1874                         if (barcodeNameVector[i] != "") {
1875                                 allBlank = false;
1876                                 break;
1877                         }
1878                 }
1879                 for (int i = 0; i < primerNameVector.size(); i++) {
1880                         if (primerNameVector[i] != "") {
1881                                 allBlank = false;
1882                                 break;
1883                         }
1884                 }
1885         
1886                 if (allBlank) {
1887                         m->mothurOut("[WARNING]: your oligos file does not contain any group names.  mothur will not create a groupfile."); m->mothurOutEndLine();
1888                         allFiles = false;
1889                         return false;
1890                 }
1891                 
1892                 return true;
1893                 
1894         }
1895         catch(exception& e) {
1896                 m->errorOut(e, "MakeContigsCommand", "getOligos");
1897                 exit(1);
1898         }
1899 }
1900 //********************************************************************/
1901 string MakeContigsCommand::reverseOligo(string oligo){
1902         try {
1903         string reverse = "";
1904         
1905         for(int i=oligo.length()-1;i>=0;i--){
1906             
1907             if(oligo[i] == 'A')         {       reverse += 'T'; }
1908             else if(oligo[i] == 'T'){   reverse += 'A'; }
1909             else if(oligo[i] == 'U'){   reverse += 'A'; }
1910             
1911             else if(oligo[i] == 'G'){   reverse += 'C'; }
1912             else if(oligo[i] == 'C'){   reverse += 'G'; }
1913             
1914             else if(oligo[i] == 'R'){   reverse += 'Y'; }
1915             else if(oligo[i] == 'Y'){   reverse += 'R'; }
1916             
1917             else if(oligo[i] == 'M'){   reverse += 'K'; }
1918             else if(oligo[i] == 'K'){   reverse += 'M'; }
1919             
1920             else if(oligo[i] == 'W'){   reverse += 'W'; }
1921             else if(oligo[i] == 'S'){   reverse += 'S'; }
1922             
1923             else if(oligo[i] == 'B'){   reverse += 'V'; }
1924             else if(oligo[i] == 'V'){   reverse += 'B'; }
1925             
1926             else if(oligo[i] == 'D'){   reverse += 'H'; }
1927             else if(oligo[i] == 'H'){   reverse += 'D'; }
1928             
1929             else                                                {       reverse += 'N'; }
1930         }
1931         
1932         
1933         return reverse;
1934     }
1935         catch(exception& e) {
1936                 m->errorOut(e, "MakeContigsCommand", "reverseOligo");
1937                 exit(1);
1938         }
1939 }
1940 //**********************************************************************************************************************
1941 vector<int> MakeContigsCommand::convertQual(string qual) {
1942         try {
1943                 vector<int> qualScores;
1944         bool negativeScores = false;
1945                 
1946                 for (int i = 0; i < qual.length(); i++) { 
1947             
1948             int temp = 0;
1949             temp = int(qual[i]);
1950             if (format == "illumina") {
1951                 temp -= 64; //char '@'
1952             }else if (format == "illumina1.8+") {
1953                     temp -= int('!'); //char '!'
1954             }else if (format == "solexa") {
1955                 temp = int(convertTable[temp]); //convert to sanger
1956                 temp -= int('!'); //char '!'
1957             }else {
1958                 temp -= int('!'); //char '!'
1959             }
1960             
1961             if (temp < -5) { negativeScores = true; }
1962                         qualScores.push_back(temp);
1963                 }
1964                 
1965         if (negativeScores) { m->mothurOut("[ERROR]: finding negative quality scores, do you have the right format selected? http://en.wikipedia.org/wiki/FASTQ_format#Encoding \n");  m->control_pressed = true;  }
1966         
1967                 return qualScores;
1968         }
1969         catch(exception& e) {
1970                 m->errorOut(e, "MakeContigsCommand", "convertQual");
1971                 exit(1);
1972         }
1973 }
1974
1975 //**********************************************************************************************************************
1976
1977
1978
1979