2 // makecontigscommand.cpp
5 // Created by Sarah Westcott on 5/15/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "makecontigscommand.h"
11 //**********************************************************************************************************************
12 vector<string> MakeContigsCommand::setParameters(){
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);
28 CommandParameter palign("align", "Multiple", "needleman-gotoh", "needleman", "", "", "","",false,false); parameters.push_back(palign);
29 CommandParameter pallfiles("allfiles", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pallfiles);
30 CommandParameter pmatch("match", "Number", "", "1.0", "", "", "","",false,false); parameters.push_back(pmatch);
31 CommandParameter pmismatch("mismatch", "Number", "", "-1.0", "", "", "","",false,false); parameters.push_back(pmismatch);
32 CommandParameter pgapopen("gapopen", "Number", "", "-2.0", "", "", "","",false,false); parameters.push_back(pgapopen);
33 CommandParameter pgapextend("gapextend", "Number", "", "-1.0", "", "", "","",false,false); parameters.push_back(pgapextend);
34 CommandParameter pthreshold("threshold", "Number", "", "40", "", "", "","",false,false); parameters.push_back(pthreshold);
35 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
36 CommandParameter pformat("format", "Multiple", "sanger-illumina-solexa", "sanger", "", "", "","",false,false,true); parameters.push_back(pformat);
37 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
38 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
40 vector<string> myArray;
41 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
45 m->errorOut(e, "MakeContigsCommand", "setParameters");
49 //**********************************************************************************************************************
50 string MakeContigsCommand::getHelpString(){
52 string helpString = "";
53 helpString += "The make.contigs command reads a file, forward fastq file and a reverse fastq file or forward fasta and reverse fasta files and outputs new fasta. It will also provide new quality files if the fastq or file parameter is used.\n";
54 helpString += "If an oligos file is provided barcodes and primers will be trimmed, and a group file will be created.\n";
55 helpString += "The make.contigs command parameters are ffastq, rfastq, oligos, format, tdiffs, bdiffs, ldiffs, sdiffs, pdiffs, align, match, mismatch, gapopen, gapextend, allfiles and processors.\n";
56 helpString += "The ffastq and rfastq, file, or ffasta and rfasta parameters are required.\n";
57 helpString += "The file parameter is 2 column file containing the forward fastq files in the first column and their matching reverse fastq files in the second column. Mothur will process each pair and create a combined fasta and qual file with all the sequences.\n";
58 helpString += "The ffastq and rfastq parameters are used to provide a forward fastq and reverse fastq file to process. If you provide one, you must provide the other.\n";
59 helpString += "The ffasta and rfasta parameters are used to provide a forward fasta and reverse fasta file to process. If you provide one, you must provide the other.\n";
60 helpString += "The fqfile and rqfile parameters are used to provide a forward quality and reverse quality files to process with the ffasta and rfasta parameters. If you provide one, you must provide the other.\n";
61 helpString += "The format parameter is used to indicate whether your sequences are sanger, solexa or illumina, default=sanger.\n";
62 helpString += "The align parameter allows you to specify the alignment method to use. Your options are: gotoh and needleman. The default is needleman.\n";
63 helpString += "The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is pdiffs + bdiffs + sdiffs + ldiffs.\n";
64 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
65 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
66 helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
67 helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
68 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
69 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n";
70 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
71 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n";
72 helpString += "The threshold parameter allows you to set a quality scores threshold. In the case where we are trying to decide whether to keep a base or remove it because the base is compared to a gap in the other fragment, if the base has a quality score below the threshold we eliminate it. Default=40.\n";
73 helpString += "The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n";
74 helpString += "The allfiles parameter will create separate group and fasta file for each grouping. The default is F.\n";
75 helpString += "The make.contigs command should be in the following format: \n";
76 helpString += "make.contigs(ffastq=yourForwardFastqFile, rfastq=yourReverseFastqFile, align=yourAlignmentMethod) \n";
77 helpString += "Note: No spaces between parameter labels (i.e. ffastq), '=' and parameters (i.e.yourForwardFastqFile).\n";
81 m->errorOut(e, "MakeContigsCommand", "getHelpString");
85 //**********************************************************************************************************************
86 string MakeContigsCommand::getOutputPattern(string type) {
90 if (type == "fasta") { pattern = "[filename],[tag],contigs.fasta"; }
91 else if (type == "qfile") { pattern = "[filename],[tag],contigs.qual"; }
92 else if (type == "group") { pattern = "[filename],[tag],contigs.groups"; }
93 else if (type == "mismatch") { pattern = "[filename],[tag],contigs.mismatch"; }
94 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
99 m->errorOut(e, "MakeContigsCommand", "getOutputPattern");
103 //**********************************************************************************************************************
104 MakeContigsCommand::MakeContigsCommand(){
106 abort = true; calledHelp = true;
108 vector<string> tempOutNames;
109 outputTypes["fasta"] = tempOutNames;
110 outputTypes["qfile"] = tempOutNames;
111 outputTypes["group"] = tempOutNames;
112 outputTypes["mismatch"] = tempOutNames;
114 catch(exception& e) {
115 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
119 //**********************************************************************************************************************
120 MakeContigsCommand::MakeContigsCommand(string option) {
122 abort = false; calledHelp = false;
124 //allow user to run help
125 if(option == "help") { help(); abort = true; calledHelp = true; }
126 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
129 vector<string> myArray = setParameters();
131 OptionParser parser(option);
132 map<string, string> parameters = parser.getParameters();
134 ValidParameters validParameter("pairwise.seqs");
135 map<string, string>::iterator it;
137 //check to make sure all parameters are valid for command
138 for (it = parameters.begin(); it != parameters.end(); it++) {
139 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
142 //initialize outputTypes
143 vector<string> tempOutNames;
144 outputTypes["fasta"] = tempOutNames;
145 outputTypes["qfile"] = tempOutNames;
146 outputTypes["mismatch"] = tempOutNames;
147 outputTypes["group"] = tempOutNames;
150 //if the user changes the input directory command factory will send this info to us in the output parameter
151 string inputDir = validParameter.validFile(parameters, "inputdir", false);
152 if (inputDir == "not found"){ inputDir = ""; }
155 it = parameters.find("ffastq");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["ffastq"] = inputDir + it->second; }
163 it = parameters.find("rfastq");
164 //user has given a template file
165 if(it != parameters.end()){
166 path = m->hasPath(it->second);
167 //if the user has not given a path then, add inputdir. else leave path alone.
168 if (path == "") { parameters["rfastq"] = inputDir + it->second; }
171 it = parameters.find("ffasta");
172 //user has given a template file
173 if(it != parameters.end()){
174 path = m->hasPath(it->second);
175 //if the user has not given a path then, add inputdir. else leave path alone.
176 if (path == "") { parameters["ffasta"] = inputDir + it->second; }
179 it = parameters.find("rfasta");
180 //user has given a template file
181 if(it != parameters.end()){
182 path = m->hasPath(it->second);
183 //if the user has not given a path then, add inputdir. else leave path alone.
184 if (path == "") { parameters["rfasta"] = inputDir + it->second; }
187 it = parameters.find("fqfile");
188 //user has given a template file
189 if(it != parameters.end()){
190 path = m->hasPath(it->second);
191 //if the user has not given a path then, add inputdir. else leave path alone.
192 if (path == "") { parameters["fqfile"] = inputDir + it->second; }
195 it = parameters.find("rqfile");
196 //user has given a template file
197 if(it != parameters.end()){
198 path = m->hasPath(it->second);
199 //if the user has not given a path then, add inputdir. else leave path alone.
200 if (path == "") { parameters["rqfile"] = inputDir + it->second; }
203 it = parameters.find("file");
204 //user has given a template file
205 if(it != parameters.end()){
206 path = m->hasPath(it->second);
207 //if the user has not given a path then, add inputdir. else leave path alone.
208 if (path == "") { parameters["file"] = inputDir + it->second; }
211 it = parameters.find("oligos");
212 //user has given a template file
213 if(it != parameters.end()){
214 path = m->hasPath(it->second);
215 //if the user has not given a path then, add inputdir. else leave path alone.
216 if (path == "") { parameters["oligos"] = inputDir + it->second; }
220 ffastqfile = validParameter.validFile(parameters, "ffastq", true);
221 if (ffastqfile == "not open") { abort = true; }
222 else if (ffastqfile == "not found") { ffastqfile = ""; }
224 rfastqfile = validParameter.validFile(parameters, "rfastq", true);
225 if (rfastqfile == "not open") { abort = true; }
226 else if (rfastqfile == "not found") { rfastqfile = ""; }
228 ffastafile = validParameter.validFile(parameters, "ffasta", true);
229 if (ffastafile == "not open") { abort = true; }
230 else if (ffastafile == "not found") { ffastafile = ""; }
232 rfastafile = validParameter.validFile(parameters, "rfasta", true);
233 if (rfastafile == "not open") { abort = true; }
234 else if (rfastafile == "not found") { rfastafile = ""; }
236 fqualfile = validParameter.validFile(parameters, "fqfile", true);
237 if (fqualfile == "not open") { abort = true; }
238 else if (fqualfile == "not found") { fqualfile = ""; }
240 rqualfile = validParameter.validFile(parameters, "rqfile", true);
241 if (rqualfile == "not open") { abort = true; }
242 else if (rqualfile == "not found") { rqualfile = ""; }
244 file = validParameter.validFile(parameters, "file", true);
245 if (file == "not open") { abort = true; }
246 else if (file == "not found") { file = ""; }
249 if ((file == "") && (ffastafile == "") && (ffastqfile == "")) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
250 if ((file != "") && ((ffastafile != "") || (ffastqfile != ""))) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
251 if ((ffastqfile != "") && (rfastqfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffastq, you must provide a rfastq file.\n"); }
252 if ((ffastqfile == "") && (rfastqfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfastq, you must provide a ffastq file.\n"); }
253 if ((ffastafile != "") && (rfastafile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffasta, you must provide a rfasta file.\n"); }
254 if ((ffastafile == "") && (rfastafile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfasta, you must provide a ffasta file.\n"); }
255 if ((fqualfile != "") && (rqualfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the fqfile, you must provide a rqfile file.\n"); }
256 if ((fqualfile == "") && (rqualfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile, you must provide a fqfile file.\n"); }
257 if (((fqualfile != "") || (rqualfile != "")) && ((ffastafile == "") || (rfastafile == ""))) {
258 abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile or fqfile file, you must provide the ffasta and rfasta parameters.\n");
261 oligosfile = validParameter.validFile(parameters, "oligos", true);
262 if (oligosfile == "not found") { oligosfile = ""; }
263 else if(oligosfile == "not open") { abort = true; }
264 else { m->setOligosFile(oligosfile); }
266 //if the user changes the output directory command factory will send this info to us in the output parameter
267 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
272 //check for optional parameter and set defaults
273 // ...at some point should added some additional type checking...
275 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
276 m->mothurConvert(temp, match);
278 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
279 m->mothurConvert(temp, misMatch);
280 if (misMatch > 0) { m->mothurOut("[ERROR]: mismatch must be negative.\n"); abort=true; }
282 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
283 m->mothurConvert(temp, gapOpen);
284 if (gapOpen > 0) { m->mothurOut("[ERROR]: gapopen must be negative.\n"); abort=true; }
286 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
287 m->mothurConvert(temp, gapExtend);
288 if (gapExtend > 0) { m->mothurOut("[ERROR]: gapextend must be negative.\n"); abort=true; }
290 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found"){ temp = "40"; }
291 m->mothurConvert(temp, threshold);
292 if ((threshold < 0) || (threshold > 40)) { m->mothurOut("[ERROR]: threshold must be between 0 and 40.\n"); abort=true; }
294 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
295 m->setProcessors(temp);
296 m->mothurConvert(temp, processors);
298 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found") { temp = "0"; }
299 m->mothurConvert(temp, bdiffs);
301 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found") { temp = "0"; }
302 m->mothurConvert(temp, pdiffs);
304 // temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
305 // m->mothurConvert(temp, ldiffs);
308 // temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
309 // m->mothurConvert(temp, sdiffs);
312 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
313 m->mothurConvert(temp, tdiffs);
315 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; } //+ ldiffs + sdiffs;
317 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
318 allFiles = m->isTrue(temp);
320 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
321 if ((align != "needleman") && (align != "gotoh")) { m->mothurOut(align + " is not a valid alignment method. Options are needleman or gotoh. I will use needleman."); m->mothurOutEndLine(); align = "needleman"; }
323 format = validParameter.validFile(parameters, "format", false); if (format == "not found"){ format = "sanger"; }
325 if ((format != "sanger") && (format != "illumina") && (format != "solexa")) {
326 m->mothurOut(format + " is not a valid format. Your format choices are sanger, solexa and illumina, aborting." ); m->mothurOutEndLine();
330 //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
331 for (int i = -64; i < 65; i++) {
332 char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
333 convertTable.push_back(temp);
338 catch(exception& e) {
339 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
343 //**********************************************************************************************************************
344 int MakeContigsCommand::execute(){
346 if (abort == true) { if (calledHelp) { return 0; } return 2; }
348 //read ffastq and rfastq files creating fasta and qual files.
349 //this function will create a forward and reverse, fasta and qual files for each processor.
350 //files has an entry for each processor. files[i][0] = forwardFasta, files[i][1] = forwardQual, files[i][2] = reverseFasta, files[i][3] = reverseQual. filesToProcess is for each filepair in the file parameter file. for ffastq and rfastq this will be size 1.
351 unsigned long int numReads = 0;
352 int start = time(NULL);
354 m->mothurOut("Reading fastq data...\n");
355 vector < vector< vector<string> > > filesToProcess = preProcessData(numReads);
356 m->mothurOut("Done.\n");
358 if (m->control_pressed) { return 0; }
360 map<string, string> cvars;
361 string compOutputDir = outputDir;
362 if (outputDir == "") { compOutputDir = m->hasPath(file); }
363 cvars["[filename]"] = compOutputDir + m->getRootName(m->getSimpleName(file));
365 string compositeGroupFile = getOutputFileName("group",cvars);
366 cvars["[tag]"] = "trim";
367 string compositeFastaFile = getOutputFileName("fasta",cvars);
368 string compositeQualFile = getOutputFileName("qfile",cvars);
369 cvars["[tag]"] = "scrap";
370 string compositeScrapFastaFile = getOutputFileName("fasta",cvars);
371 string compositeScrapQualFile = getOutputFileName("qfile",cvars);
373 string compositeMisMatchFile = getOutputFileName("mismatch",cvars);
375 if (filesToProcess.size() > 1) { //clear files for append below
376 ofstream outCTFasta, outCTQual, outCSFasta, outCSQual, outCMisMatch;
377 m->openOutputFile(compositeFastaFile, outCTFasta); outCTFasta.close();
378 m->openOutputFile(compositeScrapFastaFile, outCSFasta); outCSFasta.close();
379 m->openOutputFile(compositeMisMatchFile, outCMisMatch); outCMisMatch.close();
380 m->openOutputFile(compositeQualFile, outCTQual); outCTQual.close();
381 m->openOutputFile(compositeScrapQualFile, outCSQual); outCSQual.close();
382 outputNames.push_back(compositeFastaFile); outputTypes["fasta"].push_back(compositeFastaFile);
383 outputNames.push_back(compositeQualFile); outputTypes["qfile"].push_back(compositeQualFile);
384 outputNames.push_back(compositeMisMatchFile); outputTypes["mismatch"].push_back(compositeMisMatchFile);
385 outputNames.push_back(compositeScrapFastaFile); outputTypes["fasta"].push_back(compositeScrapFastaFile);
386 outputNames.push_back(compositeScrapQualFile); outputTypes["qfile"].push_back(compositeScrapQualFile);
389 for (int l = 0; l < filesToProcess.size(); l++) {
391 m->mothurOut("\n>>>>>\tProcessing " + filesToProcess[l][0][0] + " (file " + toString(l+1) + " of " + toString(filesToProcess.size()) + ")\t<<<<<\n");
393 vector<vector<string> > fastaFileNames;
394 vector<vector<string> > qualFileNames;
396 string outputGroupFileName;
397 map<string, string> variables;
398 string thisOutputDir = outputDir;
399 if (outputDir == "") { thisOutputDir = m->hasPath(filesToProcess[l][0][0]); }
400 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(filesToProcess[l][0][0]));
401 variables["[tag]"] = "";
402 if(oligosfile != ""){
403 createGroup = getOligos(fastaFileNames, qualFileNames, variables["[filename]"]);
405 outputGroupFileName = getOutputFileName("group",variables);
406 outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName);
410 variables["[tag]"] = "trim";
411 string outFastaFile = getOutputFileName("fasta",variables);
412 string outQualFile = getOutputFileName("qfile",variables);
413 variables["[tag]"] = "scrap";
414 string outScrapFastaFile = getOutputFileName("fasta",variables);
415 string outScrapQualFile = getOutputFileName("qfile",variables);
416 variables["[tag]"] = "";
417 string outMisMatchFile = getOutputFileName("mismatch",variables);
418 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
419 outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile);
420 if (filesToProcess[l][0][1] != "") {
421 outputNames.push_back(outQualFile); outputTypes["qfile"].push_back(outQualFile);
422 outputNames.push_back(outScrapQualFile); outputTypes["qfile"].push_back(outScrapQualFile);
424 outputNames.push_back(outMisMatchFile); outputTypes["mismatch"].push_back(outMisMatchFile);
426 m->mothurOut("Making contigs...\n");
427 createProcesses(filesToProcess[l], outFastaFile, outQualFile, outScrapFastaFile, outScrapQualFile, outMisMatchFile, fastaFileNames, qualFileNames);
428 m->mothurOut("Done.\n");
430 //remove temp fasta and qual files
431 for (int i = 0; i < processors; i++) { for(int j = 0; j < filesToProcess[l][i].size(); j++) { m->mothurRemove(filesToProcess[l][i][j]); } }
433 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
436 map<string, string> uniqueFastaNames;// so we don't add the same groupfile multiple times
437 map<string, string>::iterator it;
438 set<string> namesToRemove;
439 for(int i=0;i<fastaFileNames.size();i++){
440 for(int j=0;j<fastaFileNames[0].size();j++){
441 if (fastaFileNames[i][j] != "") {
442 if (namesToRemove.count(fastaFileNames[i][j]) == 0) {
443 if(m->isBlank(fastaFileNames[i][j])){
444 m->mothurRemove(fastaFileNames[i][j]);
445 namesToRemove.insert(fastaFileNames[i][j]);
447 if (filesToProcess[l][0][1] != "") {
448 m->mothurRemove(qualFileNames[i][j]);
449 namesToRemove.insert(qualFileNames[i][j]);
452 it = uniqueFastaNames.find(fastaFileNames[i][j]);
453 if (it == uniqueFastaNames.end()) {
454 uniqueFastaNames[fastaFileNames[i][j]] = barcodeNameVector[i];
462 //remove names for outputFileNames, just cleans up the output
463 vector<string> outputNames2;
464 for(int i = 0; i < outputNames.size(); i++) { if (namesToRemove.count(outputNames[i]) == 0) { outputNames2.push_back(outputNames[i]); } }
465 outputNames = outputNames2;
467 for (it = uniqueFastaNames.begin(); it != uniqueFastaNames.end(); it++) {
469 m->openInputFile(it->first, in);
472 string thisGroupName = thisOutputDir + m->getRootName(m->getSimpleName(it->first));
473 thisGroupName += getOutputFileName("group",variables); outputNames.push_back(thisGroupName); outputTypes["group"].push_back(thisGroupName);
474 m->openOutputFile(thisGroupName, out);
477 if (m->control_pressed) { break; }
479 Sequence currSeq(in); m->gobble(in);
480 out << currSeq.getName() << '\t' << it->second << endl;
489 m->openOutputFile(outputGroupFileName, outGroup);
490 for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
491 outGroup << itGroup->first << '\t' << itGroup->second << endl;
496 if (filesToProcess.size() > 1) { //merge into large combo files
500 m->openOutputFile(compositeGroupFile, outCGroup); outCGroup.close();
501 outputNames.push_back(compositeGroupFile); outputTypes["group"].push_back(compositeGroupFile);
503 m->appendFiles(outputGroupFileName, compositeGroupFile);
505 m->appendFiles(outMisMatchFile, compositeMisMatchFile);
506 m->appendFiles(outFastaFile, compositeFastaFile);
507 m->appendFiles(outQualFile, compositeQualFile);
508 m->appendFiles(outScrapFastaFile, compositeScrapFastaFile);
509 m->appendFiles(outScrapQualFile, compositeScrapQualFile);
512 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process " + toString(numReads) + " sequences.\n");
514 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
516 //output group counts
517 m->mothurOutEndLine();
519 if (groupCounts.size() != 0) { m->mothurOut("Group count: \n"); }
520 for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
521 total += it->second; m->mothurOut(it->first + "\t" + toString(it->second)); m->mothurOutEndLine();
523 if (total != 0) { m->mothurOut("Total of all groups is " + toString(total)); m->mothurOutEndLine(); }
525 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
527 string currentFasta = "";
528 itTypes = outputTypes.find("fasta");
529 if (itTypes != outputTypes.end()) {
530 if ((itTypes->second).size() != 0) { currentFasta = (itTypes->second)[0]; m->setFastaFile(currentFasta); }
533 string currentQual = "";
534 itTypes = outputTypes.find("qfile");
535 if (itTypes != outputTypes.end()) {
536 if ((itTypes->second).size() != 0) { currentQual = (itTypes->second)[0]; m->setQualFile(currentQual); }
539 string currentGroup = "";
540 itTypes = outputTypes.find("group");
541 if (itTypes != outputTypes.end()) {
542 if ((itTypes->second).size() != 0) { currentGroup = (itTypes->second)[0]; m->setGroupFile(currentGroup); }
545 //output files created by command
546 m->mothurOutEndLine();
547 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
548 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
549 m->mothurOutEndLine();
553 catch(exception& e) {
554 m->errorOut(e, "MakeContigsCommand", "execute");
558 //**********************************************************************************************************************
559 vector< vector< vector<string> > > MakeContigsCommand::preProcessData(unsigned long int& numReads) {
561 vector< vector< vector<string> > > filesToProcess;
563 if (ffastqfile != "") { //reading one file
564 vector< vector<string> > files = readFastqFiles(numReads, ffastqfile, rfastqfile);
565 //adjust for really large processors or really small files
566 if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
567 if (numReads < processors) {
568 for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
569 files.resize(numReads);
570 processors = numReads;
572 filesToProcess.push_back(files);
573 }else if (file != "") { //reading multiple files
574 //return only valid pairs
575 vector< vector<string> > filePairsToProcess = readFileNames(file);
577 if (m->control_pressed) { return filesToProcess; }
579 if (filePairsToProcess.size() != 0) {
580 for (int i = 0; i < filePairsToProcess.size(); i++) {
582 if (m->control_pressed) { for (int l = 0; l < filesToProcess.size(); l++) { for (int k = 0; k < filesToProcess[l].size(); k++) { for(int j = 0; j < filesToProcess[l][k].size(); j++) { m->mothurRemove(filesToProcess[l][k][j]); } filesToProcess[l][k].clear(); } return filesToProcess; } }
584 unsigned long int thisFilesReads;
585 vector< vector<string> > files = readFastqFiles(thisFilesReads, filePairsToProcess[i][0], filePairsToProcess[i][1]);
587 //adjust for really large processors or really small files
588 if (thisFilesReads < processors) {
589 m->mothurOut("[ERROR]: " + filePairsToProcess[i][0] + " has less than " + toString(processors) + " good reads, skipping\n");
590 for (int k = 0; k < files.size(); k++) { for(int j = 0; j < files[k].size(); j++) { m->mothurRemove(files[k][j]); } files[k].clear(); }
592 filesToProcess.push_back(files);
593 numReads += thisFilesReads;
597 if (numReads == 0) { m->control_pressed = true; }
599 }else if (ffastafile != "") {
600 vector< vector<string> > files = readFastaFiles(numReads, ffastafile, rfastafile);
601 //adjust for really large processors or really small files
602 if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
603 if (numReads < processors) {
604 for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
605 files.resize(numReads);
606 processors = numReads;
608 filesToProcess.push_back(files);
609 }else { m->control_pressed = true; } //should not get here
611 return filesToProcess;
613 catch(exception& e) {
614 m->errorOut(e, "MakeContigsCommand", "preProcessData");
618 //**********************************************************************************************************************
619 int MakeContigsCommand::createProcesses(vector< vector<string> > files, string outputFasta, string outputQual, string outputScrapFasta, string outputScrapQual, string outputMisMatches, vector<vector<string> > fastaFileNames, vector<vector<string> > qualFileNames) {
622 vector<int> processIDS;
623 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
626 //loop through and create all the processes you want
627 while (process != processors-1) {
631 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
634 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
635 vector<vector<string> > tempPrimerQualFileNames = qualFileNames;
640 for(int i=0;i<tempFASTAFileNames.size();i++){
641 for(int j=0;j<tempFASTAFileNames[i].size();j++){
642 if (tempFASTAFileNames[i][j] != "") {
643 tempFASTAFileNames[i][j] += toString(getpid()) + ".temp";
644 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
646 if (files[processors-1][1] != "") {
647 tempPrimerQualFileNames[i][j] += toString(getpid()) + ".temp";
648 m->openOutputFile(tempPrimerQualFileNames[i][j], temp); temp.close();
655 num = driver(files[process],
656 outputFasta + toString(getpid()) + ".temp",
657 outputQual + toString(getpid()) + ".temp",
658 outputScrapFasta + toString(getpid()) + ".temp",
659 outputScrapQual + toString(getpid()) + ".temp",
660 outputMisMatches + toString(getpid()) + ".temp",
662 tempPrimerQualFileNames);
664 //pass groupCounts to parent
666 string tempFile = toString(getpid()) + ".num.temp";
667 m->openOutputFile(tempFile, out);
670 out << groupCounts.size() << endl;
672 for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
673 out << it->first << '\t' << it->second << endl;
676 out << groupMap.size() << endl;
677 for (map<string, string>::iterator it = groupMap.begin(); it != groupMap.end(); it++) {
678 out << it->first << '\t' << it->second << endl;
685 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
686 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
692 m->openOutputFile(outputFasta, temp); temp.close();
693 m->openOutputFile(outputScrapFasta, temp); temp.close();
694 if (files[processors-1][1] != "") {
695 m->openOutputFile(outputScrapQual, temp); temp.close();
696 m->openOutputFile(outputQual, temp); temp.close();
700 num = driver(files[processors-1], outputFasta, outputQual, outputScrapFasta, outputScrapQual, outputMisMatches, fastaFileNames, qualFileNames);
702 //force parent to wait until all the processes are done
703 for (int i=0;i<processIDS.size();i++) {
704 int temp = processIDS[i];
708 for (int i = 0; i < processIDS.size(); i++) {
710 string tempFile = toString(processIDS[i]) + ".num.temp";
711 m->openInputFile(tempFile, in);
713 in >> tempNum; num += tempNum; m->gobble(in);
717 in >> tempNum; m->gobble(in);
720 for (int j = 0; j < tempNum; j++) {
722 in >> group >> groupNum; m->gobble(in);
724 map<string, int>::iterator it = groupCounts.find(group);
725 if (it == groupCounts.end()) { groupCounts[group] = groupNum; }
726 else { groupCounts[it->first] += groupNum; }
729 in >> tempNum; m->gobble(in);
731 for (int j = 0; j < tempNum; j++) {
732 string group, seqName;
733 in >> seqName >> group; m->gobble(in);
735 map<string, string>::iterator it = groupMap.find(seqName);
736 if (it == groupMap.end()) { groupMap[seqName] = group; }
737 else { m->mothurOut("[ERROR]: " + seqName + " is in your fasta file more than once. Sequence names must be unique. please correct.\n"); }
741 in.close(); m->mothurRemove(tempFile);
745 //////////////////////////////////////////////////////////////////////////////////////////////////////
746 //Windows version shared memory, so be careful when passing variables through the contigsData struct.
747 //Above fork() will clone, so memory is separate, but that's not the case with windows,
748 //////////////////////////////////////////////////////////////////////////////////////////////////////
750 vector<contigsData*> pDataArray;
751 DWORD dwThreadIdArray[processors-1];
752 HANDLE hThreadArray[processors-1];
754 //Create processor worker threads.
755 for( int h=0; h<processors-1; h++ ){
756 string extension = "";
757 if (h != 0) { extension = toString(h) + ".temp"; processIDS.push_back(h); }
758 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
759 vector<vector<string> > tempPrimerQualFileNames = qualFileNames;
764 for(int i=0;i<tempFASTAFileNames.size();i++){
765 for(int j=0;j<tempFASTAFileNames[i].size();j++){
766 if (tempFASTAFileNames[i][j] != "") {
767 tempFASTAFileNames[i][j] += extension;
768 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
770 if (files[processors-1][1] != "") {
771 tempPrimerQualFileNames[i][j] += extension;
772 m->openOutputFile(tempPrimerQualFileNames[i][j], temp); temp.close();
780 contigsData* tempcontig = new contigsData(files[h], (outputFasta + extension), (outputQual + extension), (outputScrapFasta + extension), (outputScrapQual + extension),(outputMisMatches + extension), align, m, match, misMatch, gapOpen, gapExtend, threshold, barcodes, primers, tempFASTAFileNames, tempPrimerQualFileNames, barcodeNameVector, primerNameVector, pdiffs, bdiffs, tdiffs, createGroup, allFiles, h);
781 pDataArray.push_back(tempcontig);
783 hThreadArray[h] = CreateThread(NULL, 0, MyContigsThreadFunction, pDataArray[h], 0, &dwThreadIdArray[h]);
786 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
787 vector<vector<string> > tempPrimerQualFileNames = qualFileNames;
791 string extension = toString(processors-1) + ".temp";
793 for(int i=0;i<tempFASTAFileNames.size();i++){
794 for(int j=0;j<tempFASTAFileNames[i].size();j++){
795 if (tempFASTAFileNames[i][j] != "") {
796 tempFASTAFileNames[i][j] += extension;
797 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
799 if (files[processors-1][1] != "") {
800 tempPrimerQualFileNames[i][j] += extension;
801 m->openOutputFile(tempPrimerQualFileNames[i][j], temp); temp.close();
810 m->openOutputFile(outputFasta, temp); temp.close();
811 m->openOutputFile(outputScrapFasta, temp); temp.close();
812 if (files[processors-1][1] != "") {
813 m->openOutputFile(outputScrapQual, temp); temp.close();
814 m->openOutputFile(outputQual, temp); temp.close();
818 processIDS.push_back(processors-1);
819 num = driver(files[processors-1], (outputFasta+ toString(processors-1) + ".temp"), (outputQual+ toString(processors-1) + ".temp"), (outputScrapFasta+ toString(processors-1) + ".temp"), (outputScrapQual+ toString(processors-1) + ".temp"), (outputMisMatches+ toString(processors-1) + ".temp"), tempFASTAFileNames, tempPrimerQualFileNames);
821 //Wait until all threads have terminated.
822 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
824 //Close all thread handles and free memory allocations.
825 for(int i=0; i < pDataArray.size(); i++){
826 num += pDataArray[i]->count;
827 if (!pDataArray[i]->done) {
828 m->mothurOut("[ERROR]: process " + toString(i) + " only processed " + toString(pDataArray[i]->count) + " of sequences assigned to it, quitting. \n"); m->control_pressed = true;
830 for (map<string, int>::iterator it = pDataArray[i]->groupCounts.begin(); it != pDataArray[i]->groupCounts.end(); it++) {
831 map<string, int>::iterator it2 = groupCounts.find(it->first);
832 if (it2 == groupCounts.end()) { groupCounts[it->first] = it->second; }
833 else { groupCounts[it->first] += it->second; }
835 for (map<string, string>::iterator it = pDataArray[i]->groupMap.begin(); it != pDataArray[i]->groupMap.end(); it++) {
836 map<string, string>::iterator it2 = groupMap.find(it->first);
837 if (it2 == groupMap.end()) { groupMap[it->first] = it->second; }
838 else { m->mothurOut("[ERROR]: " + it->first + " is in your fasta file more than once. Sequence names must be unique. please correct.\n"); }
840 CloseHandle(hThreadArray[i]);
841 delete pDataArray[i];
846 for (int i = 0; i < processIDS.size(); i++) {
847 m->appendFiles((outputFasta + toString(processIDS[i]) + ".temp"), outputFasta);
848 m->mothurRemove((outputFasta + toString(processIDS[i]) + ".temp"));
850 m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta);
851 m->mothurRemove((outputScrapFasta + toString(processIDS[i]) + ".temp"));
853 if (files[processors-1][1] != "") {
854 m->appendFiles((outputScrapQual + toString(processIDS[i]) + ".temp"), outputScrapQual);
855 m->mothurRemove((outputScrapQual + toString(processIDS[i]) + ".temp"));
857 m->appendFiles((outputQual + toString(processIDS[i]) + ".temp"), outputQual);
858 m->mothurRemove((outputQual + toString(processIDS[i]) + ".temp"));
861 m->appendFiles((outputMisMatches + toString(processIDS[i]) + ".temp"), outputMisMatches);
862 m->mothurRemove((outputMisMatches + toString(processIDS[i]) + ".temp"));
865 for(int j=0;j<fastaFileNames.size();j++){
866 for(int k=0;k<fastaFileNames[j].size();k++){
867 if (fastaFileNames[j][k] != "") {
868 m->appendFiles((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"), fastaFileNames[j][k]);
869 m->mothurRemove((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"));
871 if (files[processors-1][1] != "") {
872 m->appendFiles((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"), qualFileNames[j][k]);
873 m->mothurRemove((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"));
883 catch(exception& e) {
884 m->errorOut(e, "MakeContigsCommand", "createProcesses");
888 //**********************************************************************************************************************
889 int MakeContigsCommand::driver(vector<string> files, string outputFasta, string outputQual, string outputScrapFasta, string outputScrapQual, string outputMisMatches, vector<vector<string> > fastaFileNames, vector<vector<string> > qualFileNames){
892 Alignment* alignment;
893 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
894 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
897 string thisffastafile = files[0];
898 string thisfqualfile = files[1];
899 string thisrfastafile = files[2];
900 string thisrqualfile = files[3];
902 if (m->debug) { m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
904 ifstream inFFasta, inRFasta, inFQual, inRQual;
905 ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual;
906 m->openInputFile(thisffastafile, inFFasta);
907 m->openInputFile(thisrfastafile, inRFasta);
908 if (thisfqualfile != "") {
909 m->openInputFile(thisfqualfile, inFQual);
910 m->openInputFile(thisrqualfile, inRQual);
911 m->openOutputFile(outputScrapQual, outScrapQual);
912 m->openOutputFile(outputQual, outQual);
914 m->openOutputFile(outputFasta, outFasta);
915 m->openOutputFile(outputScrapFasta, outScrapFasta);
916 m->openOutputFile(outputMisMatches, outMisMatch);
917 outMisMatch << "Name\tLength\tMisMatches\n";
919 TrimOligos trimOligos(pdiffs, bdiffs, 0, 0, primers, barcodes);
921 while ((!inFFasta.eof()) && (!inRFasta.eof())) {
923 if (m->control_pressed) { break; }
926 string trashCode = "";
927 int currentSeqsDiffs = 0;
929 //read seqs and quality info
930 Sequence fSeq(inFFasta); m->gobble(inFFasta);
931 Sequence rSeq(inRFasta); m->gobble(inRFasta);
932 QualityScores* fQual = NULL; QualityScores* rQual = NULL;
933 if (thisfqualfile != "") {
934 fQual = new QualityScores(inFQual); m->gobble(inFQual);
935 rQual = new QualityScores(inRQual); m->gobble(inRQual);
938 int barcodeIndex = 0;
941 if(barcodes.size() != 0){
942 if (thisfqualfile != "") {
943 success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
945 success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
947 if(success > bdiffs) { trashCode += 'b'; }
948 else{ currentSeqsDiffs += success; }
951 if(primers.size() != 0){
952 if (thisfqualfile != "") {
953 success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
955 success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
957 if(success > pdiffs) { trashCode += 'f'; }
958 else{ currentSeqsDiffs += success; }
961 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
963 //flip the reverse reads
964 rSeq.reverseComplement();
965 if (thisfqualfile != "") { rQual->flipQScores(); }
968 alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
969 map<int, int> ABaseMap = alignment->getSeqAAlnBaseMap();
970 map<int, int> BBaseMap = alignment->getSeqBAlnBaseMap();
971 fSeq.setAligned(alignment->getSeqAAln());
972 rSeq.setAligned(alignment->getSeqBAln());
973 int length = fSeq.getAligned().length();
975 //traverse alignments merging into one contiguous seq
977 vector<int> contigScores;
978 int numMismatches = 0;
979 string seq1 = fSeq.getAligned();
980 string seq2 = rSeq.getAligned();
981 vector<int> scores1, scores2;
982 if (thisfqualfile != "") {
983 scores1 = fQual->getQualityScores();
984 scores2 = rQual->getQualityScores();
985 delete fQual; delete rQual;
988 // if (num < 5) { cout << fSeq.getStartPos() << '\t' << fSeq.getEndPos() << '\t' << rSeq.getStartPos() << '\t' << rSeq.getEndPos() << endl; }
989 int overlapStart = fSeq.getStartPos();
990 int seq2Start = rSeq.getStartPos();
991 //bigger of the 2 starting positions is the location of the overlapping start
992 if (overlapStart < seq2Start) { //seq2 starts later so take from 0 to seq2Start from seq1
993 overlapStart = seq2Start;
994 for (int i = 0; i < overlapStart; i++) {
996 if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
998 }else { //seq1 starts later so take from 0 to overlapStart from seq2
999 for (int i = 0; i < overlapStart; i++) {
1001 if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
1005 int seq1End = fSeq.getEndPos();
1006 int seq2End = rSeq.getEndPos();
1007 int overlapEnd = seq1End;
1008 if (seq2End < overlapEnd) { overlapEnd = seq2End; } //smallest end position is where overlapping ends
1010 for (int i = overlapStart; i < overlapEnd; i++) {
1011 if (seq1[i] == seq2[i]) { //match, add base and choose highest score
1013 if (thisfqualfile != "") {
1014 contigScores.push_back(scores1[ABaseMap[i]]);
1015 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; }
1017 }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
1018 if (thisfqualfile != "") {
1019 if (scores2[BBaseMap[i]] < threshold) { } //
1022 contigScores.push_back(scores2[BBaseMap[i]]);
1024 }else { contig += seq2[i]; } //with no quality info, then we keep it?
1025 }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
1026 if (thisfqualfile != "") {
1027 if (scores1[ABaseMap[i]] < threshold) { } //
1030 contigScores.push_back(scores1[ABaseMap[i]]);
1032 }else { contig += seq1[i]; } //with no quality info, then we keep it?
1033 }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
1034 if (thisfqualfile != "") {
1036 contigScores.push_back(scores1[ABaseMap[i]]);
1037 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; }
1040 }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch
1041 }else { //should never get here
1042 m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
1046 if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
1047 for (int i = overlapEnd; i < length; i++) {
1049 if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
1051 }else { //seq2 ends before seq1 so take from overlap to length from seq1
1052 for (int i = overlapEnd; i < length; i++) {
1054 if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
1059 if(trashCode.length() == 0){
1060 bool ignore = false;
1062 if (m->debug) { m->mothurOut(fSeq.getName()); }
1065 if(barcodes.size() != 0){
1066 string thisGroup = barcodeNameVector[barcodeIndex];
1067 if (primers.size() != 0) {
1068 if (primerNameVector[primerIndex] != "") {
1069 if(thisGroup != "") {
1070 thisGroup += "." + primerNameVector[primerIndex];
1072 thisGroup = primerNameVector[primerIndex];
1077 if (m->debug) { m->mothurOut(", group= " + thisGroup + "\n"); }
1079 int pos = thisGroup.find("ignore");
1080 if (pos == string::npos) {
1081 groupMap[fSeq.getName()] = thisGroup;
1083 map<string, int>::iterator it = groupCounts.find(thisGroup);
1084 if (it == groupCounts.end()) { groupCounts[thisGroup] = 1; }
1085 else { groupCounts[it->first] ++; }
1086 }else { ignore = true; }
1090 if (m->debug) { m->mothurOut("\n"); }
1092 if(allFiles && !ignore){
1094 m->openOutputFileAppend(fastaFileNames[barcodeIndex][primerIndex], output);
1095 output << ">" << fSeq.getName() << endl << contig << endl;
1098 if (thisfqualfile != "") {
1099 m->openOutputFileAppend(qualFileNames[barcodeIndex][primerIndex], output);
1100 output << ">" << fSeq.getName() << endl;
1101 for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; }
1108 outFasta << ">" << fSeq.getName() << endl << contig << endl;
1109 if (thisfqualfile != "") {
1110 outQual << ">" << fSeq.getName() << endl;
1111 for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; }
1114 outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << numMismatches << endl;
1117 outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl;
1118 if (thisfqualfile != "") {
1119 outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl;
1120 for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; }
1121 outScrapQual << endl;
1127 if((num) % 1000 == 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
1131 if((num) % 1000 != 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
1136 outScrapFasta.close();
1137 outMisMatch.close();
1138 if (thisfqualfile != "") {
1142 outScrapQual.close();
1146 if (m->control_pressed) { m->mothurRemove(outputFasta); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches); if (thisfqualfile != "") { m->mothurRemove(outputQual); m->mothurRemove(outputScrapQual); } }
1150 catch(exception& e) {
1151 m->errorOut(e, "MakeContigsCommand", "driver");
1155 //**********************************************************************************************************************
1156 vector< vector<string> > MakeContigsCommand::readFastqFiles(unsigned long int& count, string ffastq, string rfastq){
1158 vector< vector<string> > files;
1159 //maps processors number to file pointer
1160 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1161 map<int, vector<ofstream*> >::iterator it;
1163 //create files to write to
1164 for (int i = 0; i < processors; i++) {
1165 vector<ofstream*> temp;
1166 ofstream* outFF = new ofstream; temp.push_back(outFF);
1167 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
1168 ofstream* outRF = new ofstream; temp.push_back(outRF);
1169 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
1170 tempfiles[i] = temp;
1172 vector<string> names;
1173 string thisOutputDir = outputDir;
1174 if (outputDir == "") { thisOutputDir = m->hasPath(ffastq); }
1175 string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "ffastatemp";
1176 string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rfastatemp";
1177 string fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "fqualtemp";
1178 string rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rqualtemp";
1179 names.push_back(ffastafilename); names.push_back(fqualfilename);
1180 names.push_back(rfastafilename); names.push_back(rqualfilename);
1181 files.push_back(names);
1183 m->openOutputFile(ffastafilename, *outFF);
1184 m->openOutputFile(rfastafilename, *outRF);
1185 m->openOutputFile(fqualfilename, *outFQ);
1186 m->openOutputFile(rqualfilename, *outRQ);
1189 if (m->control_pressed) {
1190 //close files, delete ofstreams
1191 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]; } }
1193 for (int i = 0; i < files.size(); i++) {
1194 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1199 m->openInputFile(ffastq, inForward);
1202 m->openInputFile(rfastq, inReverse);
1205 map<string, fastqRead> uniques;
1206 map<string, fastqRead>::iterator itUniques;
1207 while ((!inForward.eof()) || (!inReverse.eof())) {
1209 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 //get a read from forward and reverse fastq files
1212 bool ignoref, ignorer;
1213 fastqRead thisFread, thisRread;
1214 if (!inForward.eof()) { thisFread = readFastq(inForward, ignoref); }
1215 else { ignoref = true; }
1216 if (!inReverse.eof()) { thisRread = readFastq(inReverse, ignorer); }
1217 else { ignorer = true; }
1219 vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1221 for (int i = 0; i < reads.size(); i++) {
1222 fastqRead fread = reads[i].forward;
1223 fastqRead rread = reads[i].reverse;
1225 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1227 if (checkReads(fread, rread, ffastq, rfastq)) {
1228 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; }
1230 //if the reads are okay write to output files
1231 int process = count % processors;
1233 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1234 *(tempfiles[process][1]) << ">" << fread.name << endl;
1235 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1236 *(tempfiles[process][1]) << endl;
1237 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1238 *(tempfiles[process][3]) << ">" << rread.name << endl;
1239 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1240 *(tempfiles[process][3]) << endl;
1245 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1250 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1252 if (uniques.size() != 0) {
1253 for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1254 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1256 m->mothurOutEndLine();
1259 //close files, delete ofstreams
1260 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]; } }
1266 catch(exception& e) {
1267 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1271 //**********************************************************************************************************************
1272 vector< vector<string> > MakeContigsCommand::readFastaFiles(unsigned long int& count, string ffasta, string rfasta){
1274 vector< vector<string> > files;
1275 //maps processors number to file pointer
1276 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1277 map<int, vector<ofstream*> >::iterator it;
1279 //create files to write to
1280 for (int i = 0; i < processors; i++) {
1281 vector<ofstream*> temp;
1282 ofstream* outFF = new ofstream; temp.push_back(outFF);
1283 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
1284 ofstream* outRF = new ofstream; temp.push_back(outRF);
1285 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
1286 tempfiles[i] = temp;
1288 vector<string> names;
1289 string thisOutputDir = outputDir;
1290 if (outputDir == "") { thisOutputDir = m->hasPath(ffasta); }
1291 string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffasta)) + toString(i) + "ffastatemp";
1292 string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfasta)) + toString(i) + "rfastatemp";
1293 string fqualfilename = "";
1294 if (fqualfile != "") { fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(fqualfile)) + toString(i) + "fqual.temp"; m->openOutputFile(fqualfilename, *outFQ); }
1295 string rqualfilename = "";
1296 if (rqualfile != "") { rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rqualfile)) + toString(i) + "rqual.temp"; m->openOutputFile(rqualfilename, *outRQ); }
1297 names.push_back(ffastafilename); names.push_back(fqualfilename);
1298 names.push_back(rfastafilename); names.push_back(rqualfilename);
1299 files.push_back(names);
1301 m->openOutputFile(ffastafilename, *outFF);
1302 m->openOutputFile(rfastafilename, *outRF);
1305 if (m->control_pressed) {
1306 //close files, delete ofstreams
1307 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]; } }
1309 for (int i = 0; i < files.size(); i++) {
1310 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1314 ifstream inForwardFasta;
1315 m->openInputFile(ffasta, inForwardFasta);
1317 ifstream inReverseFasta;
1318 m->openInputFile(rfasta, inReverseFasta);
1320 ifstream inForwardQual, inReverseQual;
1321 if (fqualfile != "") { m->openInputFile(fqualfile, inForwardQual); m->openInputFile(rqualfile, inReverseQual); }
1324 map<string, fastqRead> uniques;
1325 map<string, fastqRead>::iterator itUniques;
1326 while ((!inForwardFasta.eof()) || (!inReverseFasta.eof())) {
1328 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; }
1330 //get a reads from forward and reverse fasta files
1331 bool ignoref, ignorer;
1332 fastqRead thisFread, thisRread;
1333 if (!inForwardFasta.eof()) {
1335 Sequence temp(inForwardFasta); m->gobble(inForwardFasta);
1336 thisFread.name = temp.getName();
1337 thisFread.sequence = temp.getUnaligned();
1338 }else { ignoref = true; }
1339 if (!inReverseFasta.eof()) {
1341 Sequence temp(inReverseFasta); m->gobble(inReverseFasta);
1342 thisRread.name = temp.getName();
1343 thisRread.sequence = temp.getUnaligned();
1344 }else { ignorer = true; }
1346 //get qual reads if given
1347 if (fqualfile != "") {
1348 if (!inForwardQual.eof() && !ignoref) {
1349 QualityScores temp(inForwardQual); m->gobble(inForwardQual);
1350 //if forward files dont match ignore read
1351 if (thisFread.name != temp.getName()) { ignoref = true; }
1352 else { thisFread.scores = temp.getQualityScores(); }
1353 }else { ignoref = true; }
1354 if (!inReverseQual.eof() && !ignorer) {
1355 QualityScores temp(inReverseQual); m->gobble(inReverseQual);
1356 //if reverse files dont match ignore read
1357 if (thisRread.name != temp.getName()) { ignorer = true; }
1358 else { thisRread.scores = temp.getQualityScores(); }
1359 }else { ignorer = true; }
1362 vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1364 for (int i = 0; i < reads.size(); i++) {
1365 fastqRead fread = reads[i].forward;
1366 fastqRead rread = reads[i].reverse;
1368 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1370 if (checkReads(fread, rread, ffasta, rfasta)) {
1371 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; }
1373 //if the reads are okay write to output files
1374 int process = count % processors;
1376 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1377 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1378 if (fqualfile != "") { //if you have quality info, print it
1379 *(tempfiles[process][1]) << ">" << fread.name << endl;
1380 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1381 *(tempfiles[process][1]) << endl;
1382 *(tempfiles[process][3]) << ">" << rread.name << endl;
1383 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1384 *(tempfiles[process][3]) << endl;
1389 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1394 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1396 if (uniques.size() != 0) {
1397 for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1398 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1400 m->mothurOutEndLine();
1403 //close files, delete ofstreams
1404 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]; } }
1405 inReverseFasta.close();
1406 inForwardFasta.close();
1407 if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); }
1411 catch(exception& e) {
1412 m->errorOut(e, "MakeContigsCommand", "readFastaFiles");
1416 //**********************************************************************************************************************
1417 vector<pairFastqRead> MakeContigsCommand::getReads(bool ignoref, bool ignorer, fastqRead forward, fastqRead reverse, map<string, fastqRead>& uniques){
1419 vector<pairFastqRead> reads;
1420 map<string, fastqRead>::iterator itUniques;
1422 if (!ignoref && !ignorer) {
1423 if (forward.name == reverse.name) {
1424 pairFastqRead temp(forward, reverse);
1425 reads.push_back(temp);
1428 //if no match are the names only different by 1 and 2?
1429 string tempFRead = forward.name.substr(0, forward.name.length()-1);
1430 string tempRRead = reverse.name.substr(0, reverse.name.length()-1);
1431 if (tempFRead == tempRRead) {
1432 if ((forward.name[forward.name.length()-1] == '1') && (reverse.name[reverse.name.length()-1] == '2')) {
1433 forward.name = tempFRead;
1434 reverse.name = tempRRead;
1435 pairFastqRead temp(forward, reverse);
1436 reads.push_back(temp);
1442 //look for forward pair
1443 itUniques = uniques.find(forward.name);
1444 if (itUniques != uniques.end()) { //we have the pair for this read
1445 pairFastqRead temp(forward, itUniques->second);
1446 reads.push_back(temp);
1447 uniques.erase(itUniques);
1448 }else { //save this read for later
1449 uniques[forward.name] = forward;
1452 //look for reverse pair
1453 itUniques = uniques.find(reverse.name);
1454 if (itUniques != uniques.end()) { //we have the pair for this read
1455 pairFastqRead temp(itUniques->second, reverse);
1456 reads.push_back(temp);
1457 uniques.erase(itUniques);
1458 }else { //save this read for later
1459 uniques[reverse.name] = reverse;
1464 }else if (!ignoref && ignorer) { //ignore reverse keep forward
1465 //look for forward pair
1466 itUniques = uniques.find(forward.name);
1467 if (itUniques != uniques.end()) { //we have the pair for this read
1468 pairFastqRead temp(forward, itUniques->second);
1469 reads.push_back(temp);
1470 uniques.erase(itUniques);
1471 }else { //save this read for later
1472 uniques[forward.name] = forward;
1475 }else if (ignoref && !ignorer) { //ignore forward keep reverse
1476 //look for reverse pair
1477 itUniques = uniques.find(reverse.name);
1478 if (itUniques != uniques.end()) { //we have the pair for this read
1479 pairFastqRead temp(itUniques->second, reverse);
1480 reads.push_back(temp);
1481 uniques.erase(itUniques);
1482 }else { //save this read for later
1483 uniques[reverse.name] = reverse;
1485 }//else ignore both and do nothing
1489 catch(exception& e) {
1490 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1494 //**********************************************************************************************************************
1495 fastqRead MakeContigsCommand::readFastq(ifstream& in, bool& ignore){
1501 //read sequence name
1502 string line = m->getline(in); m->gobble(in);
1503 vector<string> pieces = m->splitWhiteSpace(line);
1504 string name = ""; if (pieces.size() != 0) { name = pieces[0]; }
1505 if (name == "") { m->mothurOut("[WARNING]: Blank fasta name, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1506 else if (name[0] != '@') { m->mothurOut("[WARNING]: reading " + name + " expected a name with @ as a leading character, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1507 else { name = name.substr(1); }
1510 string sequence = m->getline(in); m->gobble(in);
1511 if (sequence == "") { m->mothurOut("[WARNING]: missing sequence for " + name + ", ignoring."); ignore=true; }
1513 //read sequence name
1514 line = m->getline(in); m->gobble(in);
1515 pieces = m->splitWhiteSpace(line);
1516 string name2 = ""; if (pieces.size() != 0) { name2 = pieces[0]; }
1517 if (name2 == "") { m->mothurOut("[WARNING]: expected a name with + as a leading character, ignoring."); ignore=true; }
1518 else if (name2[0] != '+') { m->mothurOut("[WARNING]: reading " + name2 + " expected a name with + as a leading character, ignoring."); ignore=true; }
1519 else { name2 = name2.substr(1); if (name2 == "") { name2 = name; } }
1521 //read quality scores
1522 string quality = m->getline(in); m->gobble(in);
1523 if (quality == "") { m->mothurOut("[WARNING]: missing quality for " + name2 + ", ignoring."); ignore=true; }
1525 //sanity check sequence length and number of quality scores match
1526 if (name2 != "") { if (name != name2) { m->mothurOut("[WARNING]: names do not match. read " + name + " for fasta and " + name2 + " for quality, ignoring."); ignore=true; } }
1527 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; }
1529 vector<int> qualScores = convertQual(quality);
1532 read.sequence = sequence;
1533 read.scores = qualScores;
1537 catch(exception& e) {
1538 m->errorOut(e, "MakeContigsCommand", "readFastq");
1542 //**********************************************************************************************************************
1543 bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse, string ffile, string rfile){
1547 //do sequence lengths match
1548 if (forward.sequence.length() != reverse.sequence.length()) {
1549 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");
1553 //do number of qual scores match
1554 if (forward.scores.size() != reverse.scores.size()) {
1555 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");
1561 catch(exception& e) {
1562 m->errorOut(e, "MakeContigsCommand", "checkReads");
1566 //***************************************************************************************************************
1567 vector< vector<string> > MakeContigsCommand::readFileNames(string filename){
1569 vector< vector<string> > files;
1570 string forward, reverse;
1573 m->openInputFile(filename, in);
1577 if (m->control_pressed) { return files; }
1579 in >> forward; m->gobble(in);
1580 in >> reverse; m->gobble(in);
1582 //check to make sure both are able to be opened
1584 int openForward = m->openInputFile(forward, in2, "noerror");
1586 //if you can't open it, try default location
1587 if (openForward == 1) {
1588 if (m->getDefaultPath() != "") { //default path is set
1589 string tryPath = m->getDefaultPath() + m->getSimpleName(forward);
1590 m->mothurOut("Unable to open " + forward + ". Trying default " + tryPath); m->mothurOutEndLine();
1592 openForward = m->openInputFile(tryPath, in3, "noerror");
1598 //if you can't open it, try output location
1599 if (openForward == 1) {
1600 if (m->getOutputDir() != "") { //default path is set
1601 string tryPath = m->getOutputDir() + m->getSimpleName(forward);
1602 m->mothurOut("Unable to open " + forward + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1604 openForward = m->openInputFile(tryPath, in4, "noerror");
1610 if (openForward == 1) { //can't find it
1611 m->mothurOut("[WARNING]: can't find " + forward + ", ignoring pair.\n");
1612 }else{ in2.close(); }
1615 int openReverse = m->openInputFile(reverse, in3, "noerror");
1617 //if you can't open it, try default location
1618 if (openReverse == 1) {
1619 if (m->getDefaultPath() != "") { //default path is set
1620 string tryPath = m->getDefaultPath() + m->getSimpleName(reverse);
1621 m->mothurOut("Unable to open " + reverse + ". Trying default " + tryPath); m->mothurOutEndLine();
1623 openReverse = m->openInputFile(tryPath, in3, "noerror");
1629 //if you can't open it, try output location
1630 if (openReverse == 1) {
1631 if (m->getOutputDir() != "") { //default path is set
1632 string tryPath = m->getOutputDir() + m->getSimpleName(reverse);
1633 m->mothurOut("Unable to open " + reverse + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1635 openReverse = m->openInputFile(tryPath, in4, "noerror");
1641 if (openReverse == 1) { //can't find it
1642 m->mothurOut("[WARNING]: can't find " + reverse + ", ignoring pair.\n");
1643 }else{ in3.close(); }
1645 if ((openForward != 1) && (openReverse != 1)) { //good pair
1646 vector<string> pair;
1647 pair.push_back(forward);
1648 pair.push_back(reverse);
1649 files.push_back(pair);
1657 catch(exception& e) {
1658 m->errorOut(e, "MakeContigsCommand", "checkReads");
1662 //***************************************************************************************************************
1663 //illumina data requires paired forward and reverse data
1664 //BARCODE atgcatgc atgcatgc groupName
1665 //PRIMER atgcatgc atgcatgc groupName
1666 //PRIMER atgcatgc atgcatgc
1667 bool MakeContigsCommand::getOligos(vector<vector<string> >& fastaFileNames, vector<vector<string> >& qualFileNames, string rootname){
1670 m->openInputFile(oligosfile, in);
1674 string type, foligo, roligo, group;
1676 int indexPrimer = 0;
1677 int indexBarcode = 0;
1678 set<string> uniquePrimers;
1679 set<string> uniqueBarcodes;
1684 cout << type << endl;
1685 if (m->debug) { m->mothurOut("[DEBUG]: reading type - " + type + ".\n"); }
1688 while (!in.eof()) { char c = in.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
1693 //make type case insensitive
1694 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); }
1698 if (m->debug) { m->mothurOut("[DEBUG]: reading - " + foligo + ".\n"); }
1700 for(int i=0;i<foligo.length();i++){
1701 foligo[i] = toupper(foligo[i]);
1702 if(foligo[i] == 'U') { foligo[i] = 'T'; }
1705 if(type == "FORWARD"){
1710 for(int i=0;i<roligo.length();i++){
1711 roligo[i] = toupper(roligo[i]);
1712 if(roligo[i] == 'U') { roligo[i] = 'T'; }
1714 //roligo = reverseOligo(roligo);
1718 // get rest of line in case there is a primer name
1721 if (c == 10 || c == 13){ break; }
1722 else if (c == 32 || c == 9){;} //space or tab
1723 else { group += c; }
1726 oligosPair newPrimer(foligo, roligo);
1728 //check for repeat barcodes
1729 string tempPair = foligo+roligo;
1730 if (uniquePrimers.count(tempPair) != 0) { m->mothurOut("primer pair " + newPrimer.forward + " " + newPrimer.reverse + " is in your oligos file already."); m->mothurOutEndLine(); }
1731 else { uniquePrimers.insert(tempPair); }
1733 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"); } }
1735 primers[indexPrimer]=newPrimer; indexPrimer++;
1736 primerNameVector.push_back(group);
1737 }else if(type == "BARCODE"){
1742 for(int i=0;i<roligo.length();i++){
1743 roligo[i] = toupper(roligo[i]);
1744 if(roligo[i] == 'U') { roligo[i] = 'T'; }
1746 //roligo = reverseOligo(roligo);
1748 oligosPair newPair(foligo, roligo);
1753 if (c == 10 || c == 13){ break; }
1754 else if (c == 32 || c == 9){;} //space or tab
1755 else { group += c; }
1758 if (m->debug) { m->mothurOut("[DEBUG]: barcode pair " + newPair.forward + " " + newPair.reverse + ", and group = " + group + ".\n"); }
1760 //check for repeat barcodes
1761 string tempPair = foligo+roligo;
1762 if (uniqueBarcodes.count(tempPair) != 0) { m->mothurOut("barcode pair " + newPair.forward + " " + newPair.reverse + " is in your oligos file already, disregarding."); m->mothurOutEndLine(); }
1763 else { uniqueBarcodes.insert(tempPair); }
1765 barcodes[indexBarcode]=newPair; indexBarcode++;
1766 barcodeNameVector.push_back(group);
1767 cout << group << endl;
1768 }else if(type == "LINKER"){
1769 linker.push_back(foligo);
1770 m->mothurOut("[WARNING]: make.contigs is not setup to remove linkers, ignoring.\n");
1771 }else if(type == "SPACER"){
1772 spacer.push_back(foligo);
1773 m->mothurOut("[WARNING]: make.contigs is not setup to remove spacers, ignoring.\n");
1775 else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are primer, barcode, linker and spacer. Ignoring " + foligo + "."); m->mothurOutEndLine(); }
1781 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0; }
1783 //add in potential combos
1784 if(barcodeNameVector.size() == 0){
1785 oligosPair temp("", "");
1787 barcodeNameVector.push_back("");
1790 if(primerNameVector.size() == 0){
1791 oligosPair temp("", "");
1793 primerNameVector.push_back("");
1796 fastaFileNames.resize(barcodeNameVector.size());
1797 for(int i=0;i<fastaFileNames.size();i++){
1798 fastaFileNames[i].assign(primerNameVector.size(), "");
1800 qualFileNames = fastaFileNames;
1803 set<string> uniqueNames; //used to cleanup outputFileNames
1804 for(map<int, oligosPair>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1805 for(map<int, oligosPair>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1807 string primerName = primerNameVector[itPrimer->first];
1808 string barcodeName = barcodeNameVector[itBar->first];
1810 if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing
1812 string comboGroupName = "";
1813 string fastaFileName = "";
1814 string qualFileName = "";
1815 string nameFileName = "";
1816 string countFileName = "";
1818 if(primerName == ""){
1819 comboGroupName = barcodeNameVector[itBar->first];
1822 if(barcodeName == ""){
1823 comboGroupName = primerNameVector[itPrimer->first];
1826 comboGroupName = barcodeNameVector[itBar->first] + "." + primerNameVector[itPrimer->first];
1832 fastaFileName = rootname + comboGroupName + ".fasta";
1833 if (uniqueNames.count(fastaFileName) == 0) {
1834 outputNames.push_back(fastaFileName);
1835 outputTypes["fasta"].push_back(fastaFileName);
1836 uniqueNames.insert(fastaFileName);
1839 fastaFileNames[itBar->first][itPrimer->first] = fastaFileName;
1840 m->openOutputFile(fastaFileName, temp); temp.close();
1842 if ((fqualfile != "") || (ffastqfile != "") || (file != "")) {
1843 qualFileName = rootname + ".qual";
1844 if (uniqueNames.count(qualFileName) == 0) {
1845 outputNames.push_back(qualFileName);
1846 outputTypes["qfile"].push_back(qualFileName);
1849 qualFileNames[itBar->first][itPrimer->first] = qualFileName;
1850 m->openOutputFile(qualFileName, temp); temp.close();
1857 bool allBlank = true;
1858 for (int i = 0; i < barcodeNameVector.size(); i++) {
1859 if (barcodeNameVector[i] != "") {
1864 for (int i = 0; i < primerNameVector.size(); i++) {
1865 if (primerNameVector[i] != "") {
1872 m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a groupfile."); m->mothurOutEndLine();
1880 catch(exception& e) {
1881 m->errorOut(e, "MakeContigsCommand", "getOligos");
1885 //********************************************************************/
1886 string MakeContigsCommand::reverseOligo(string oligo){
1888 string reverse = "";
1890 for(int i=oligo.length()-1;i>=0;i--){
1892 if(oligo[i] == 'A') { reverse += 'T'; }
1893 else if(oligo[i] == 'T'){ reverse += 'A'; }
1894 else if(oligo[i] == 'U'){ reverse += 'A'; }
1896 else if(oligo[i] == 'G'){ reverse += 'C'; }
1897 else if(oligo[i] == 'C'){ reverse += 'G'; }
1899 else if(oligo[i] == 'R'){ reverse += 'Y'; }
1900 else if(oligo[i] == 'Y'){ reverse += 'R'; }
1902 else if(oligo[i] == 'M'){ reverse += 'K'; }
1903 else if(oligo[i] == 'K'){ reverse += 'M'; }
1905 else if(oligo[i] == 'W'){ reverse += 'W'; }
1906 else if(oligo[i] == 'S'){ reverse += 'S'; }
1908 else if(oligo[i] == 'B'){ reverse += 'V'; }
1909 else if(oligo[i] == 'V'){ reverse += 'B'; }
1911 else if(oligo[i] == 'D'){ reverse += 'H'; }
1912 else if(oligo[i] == 'H'){ reverse += 'D'; }
1914 else { reverse += 'N'; }
1920 catch(exception& e) {
1921 m->errorOut(e, "MakeContigsCommand", "reverseOligo");
1925 //**********************************************************************************************************************
1926 vector<int> MakeContigsCommand::convertQual(string qual) {
1928 vector<int> qualScores;
1930 for (int i = 0; i < qual.length(); i++) {
1933 temp = int(qual[i]);
1934 if (format == "illumina") {
1935 temp -= 64; //char '@'
1936 }else if (format == "solexa") {
1937 temp = int(convertTable[temp]); //convert to sanger
1938 temp -= int('!'); //char '!'
1940 temp -= int('!'); //char '!'
1942 qualScores.push_back(temp);
1947 catch(exception& e) {
1948 m->errorOut(e, "MakeContigsCommand", "convertQual");
1953 //**********************************************************************************************************************