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