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