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 for (map<string, int>::iterator it = pDataArray[i]->groupCounts.begin(); it != pDataArray[i]->groupCounts.end(); it++) {
828 map<string, int>::iterator it2 = groupCounts.find(it->first);
829 if (it2 == groupCounts.end()) { groupCounts[it->first] = it->second; }
830 else { groupCounts[it->first] += it->second; }
832 for (map<string, string>::iterator it = pDataArray[i]->groupMap.begin(); it != pDataArray[i]->groupMap.end(); it++) {
833 map<string, string>::iterator it2 = groupMap.find(it->first);
834 if (it2 == groupMap.end()) { groupMap[it->first] = it->second; }
835 else { m->mothurOut("[ERROR]: " + it->first + " is in your fasta file more than once. Sequence names must be unique. please correct.\n"); }
837 CloseHandle(hThreadArray[i]);
838 delete pDataArray[i];
843 for (int i = 0; i < processIDS.size(); i++) {
844 m->appendFiles((outputFasta + toString(processIDS[i]) + ".temp"), outputFasta);
845 m->mothurRemove((outputFasta + toString(processIDS[i]) + ".temp"));
847 m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta);
848 m->mothurRemove((outputScrapFasta + toString(processIDS[i]) + ".temp"));
850 if (files[processors-1][1] != "") {
851 m->appendFiles((outputScrapQual + toString(processIDS[i]) + ".temp"), outputScrapQual);
852 m->mothurRemove((outputScrapQual + toString(processIDS[i]) + ".temp"));
854 m->appendFiles((outputQual + toString(processIDS[i]) + ".temp"), outputQual);
855 m->mothurRemove((outputQual + toString(processIDS[i]) + ".temp"));
858 m->appendFiles((outputMisMatches + toString(processIDS[i]) + ".temp"), outputMisMatches);
859 m->mothurRemove((outputMisMatches + toString(processIDS[i]) + ".temp"));
862 for(int j=0;j<fastaFileNames.size();j++){
863 for(int k=0;k<fastaFileNames[j].size();k++){
864 if (fastaFileNames[j][k] != "") {
865 m->appendFiles((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"), fastaFileNames[j][k]);
866 m->mothurRemove((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"));
868 if (files[processors-1][1] != "") {
869 m->appendFiles((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"), qualFileNames[j][k]);
870 m->mothurRemove((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"));
880 catch(exception& e) {
881 m->errorOut(e, "MakeContigsCommand", "createProcesses");
885 //**********************************************************************************************************************
886 int MakeContigsCommand::driver(vector<string> files, string outputFasta, string outputQual, string outputScrapFasta, string outputScrapQual, string outputMisMatches, vector<vector<string> > fastaFileNames, vector<vector<string> > qualFileNames){
889 Alignment* alignment;
890 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
891 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
894 string thisffastafile = files[0];
895 string thisfqualfile = files[1];
896 string thisrfastafile = files[2];
897 string thisrqualfile = files[3];
899 if (m->debug) { m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
901 ifstream inFFasta, inRFasta, inFQual, inRQual;
902 ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual;
903 m->openInputFile(thisffastafile, inFFasta);
904 m->openInputFile(thisrfastafile, inRFasta);
905 if (thisfqualfile != "") {
906 m->openInputFile(thisfqualfile, inFQual);
907 m->openInputFile(thisrqualfile, inRQual);
908 m->openOutputFile(outputScrapQual, outScrapQual);
909 m->openOutputFile(outputQual, outQual);
911 m->openOutputFile(outputFasta, outFasta);
912 m->openOutputFile(outputScrapFasta, outScrapFasta);
913 m->openOutputFile(outputMisMatches, outMisMatch);
914 outMisMatch << "Name\tLength\tMisMatches\n";
916 TrimOligos trimOligos(pdiffs, bdiffs, 0, 0, primers, barcodes);
918 while ((!inFFasta.eof()) && (!inRFasta.eof())) {
920 if (m->control_pressed) { break; }
923 string trashCode = "";
924 int currentSeqsDiffs = 0;
926 //read seqs and quality info
927 Sequence fSeq(inFFasta); m->gobble(inFFasta);
928 Sequence rSeq(inRFasta); m->gobble(inRFasta);
929 QualityScores* fQual = NULL; QualityScores* rQual = NULL;
930 if (thisfqualfile != "") {
931 fQual = new QualityScores(inFQual); m->gobble(inFQual);
932 rQual = new QualityScores(inRQual); m->gobble(inRQual);
935 int barcodeIndex = 0;
938 if(barcodes.size() != 0){
939 if (thisfqualfile != "") {
940 success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
942 success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
944 if(success > bdiffs) { trashCode += 'b'; }
945 else{ currentSeqsDiffs += success; }
948 if(primers.size() != 0){
949 if (thisfqualfile != "") {
950 success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
952 success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
954 if(success > pdiffs) { trashCode += 'f'; }
955 else{ currentSeqsDiffs += success; }
958 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
960 //flip the reverse reads
961 rSeq.reverseComplement();
962 if (thisfqualfile != "") { rQual->flipQScores(); }
965 alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
966 map<int, int> ABaseMap = alignment->getSeqAAlnBaseMap();
967 map<int, int> BBaseMap = alignment->getSeqBAlnBaseMap();
968 fSeq.setAligned(alignment->getSeqAAln());
969 rSeq.setAligned(alignment->getSeqBAln());
970 int length = fSeq.getAligned().length();
972 //traverse alignments merging into one contiguous seq
974 vector<int> contigScores;
975 int numMismatches = 0;
976 string seq1 = fSeq.getAligned();
977 string seq2 = rSeq.getAligned();
978 vector<int> scores1, scores2;
979 if (thisfqualfile != "") {
980 scores1 = fQual->getQualityScores();
981 scores2 = rQual->getQualityScores();
982 delete fQual; delete rQual;
985 // if (num < 5) { cout << fSeq.getStartPos() << '\t' << fSeq.getEndPos() << '\t' << rSeq.getStartPos() << '\t' << rSeq.getEndPos() << endl; }
986 int overlapStart = fSeq.getStartPos();
987 int seq2Start = rSeq.getStartPos();
988 //bigger of the 2 starting positions is the location of the overlapping start
989 if (overlapStart < seq2Start) { //seq2 starts later so take from 0 to seq2Start from seq1
990 overlapStart = seq2Start;
991 for (int i = 0; i < overlapStart; i++) {
993 if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
995 }else { //seq1 starts later so take from 0 to overlapStart from seq2
996 for (int i = 0; i < overlapStart; i++) {
998 if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
1002 int seq1End = fSeq.getEndPos();
1003 int seq2End = rSeq.getEndPos();
1004 int overlapEnd = seq1End;
1005 if (seq2End < overlapEnd) { overlapEnd = seq2End; } //smallest end position is where overlapping ends
1007 for (int i = overlapStart; i < overlapEnd; i++) {
1008 if (seq1[i] == seq2[i]) { //match, add base and choose highest score
1010 if (thisfqualfile != "") {
1011 contigScores.push_back(scores1[ABaseMap[i]]);
1012 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; }
1014 }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base
1015 if (thisfqualfile != "") {
1016 if (scores2[BBaseMap[i]] < threshold) { } //
1019 contigScores.push_back(scores2[BBaseMap[i]]);
1021 }else { contig += seq2[i]; } //with no quality info, then we keep it?
1022 }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below threshold. In that case eliminate base
1023 if (thisfqualfile != "") {
1024 if (scores1[ABaseMap[i]] < threshold) { } //
1027 contigScores.push_back(scores1[ABaseMap[i]]);
1029 }else { contig += seq1[i]; } //with no quality info, then we keep it?
1030 }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
1031 if (thisfqualfile != "") {
1033 contigScores.push_back(scores1[ABaseMap[i]]);
1034 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; }
1037 }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch
1038 }else { //should never get here
1039 m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
1043 if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
1044 for (int i = overlapEnd; i < length; i++) {
1046 if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
1048 }else { //seq2 ends before seq1 so take from overlap to length from seq1
1049 for (int i = overlapEnd; i < length; i++) {
1051 if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
1056 if(trashCode.length() == 0){
1057 bool ignore = false;
1059 if (m->debug) { m->mothurOut(fSeq.getName()); }
1062 if(barcodes.size() != 0){
1063 string thisGroup = barcodeNameVector[barcodeIndex];
1064 if (primers.size() != 0) {
1065 if (primerNameVector[primerIndex] != "") {
1066 if(thisGroup != "") {
1067 thisGroup += "." + primerNameVector[primerIndex];
1069 thisGroup = primerNameVector[primerIndex];
1074 if (m->debug) { m->mothurOut(", group= " + thisGroup + "\n"); }
1076 int pos = thisGroup.find("ignore");
1077 if (pos == string::npos) {
1078 groupMap[fSeq.getName()] = thisGroup;
1080 map<string, int>::iterator it = groupCounts.find(thisGroup);
1081 if (it == groupCounts.end()) { groupCounts[thisGroup] = 1; }
1082 else { groupCounts[it->first] ++; }
1083 }else { ignore = true; }
1087 if (m->debug) { m->mothurOut("\n"); }
1089 if(allFiles && !ignore){
1091 m->openOutputFileAppend(fastaFileNames[barcodeIndex][primerIndex], output);
1092 output << ">" << fSeq.getName() << endl << contig << endl;
1095 if (thisfqualfile != "") {
1096 m->openOutputFileAppend(qualFileNames[barcodeIndex][primerIndex], output);
1097 output << ">" << fSeq.getName() << endl;
1098 for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; }
1105 outFasta << ">" << fSeq.getName() << endl << contig << endl;
1106 if (thisfqualfile != "") {
1107 outQual << ">" << fSeq.getName() << endl;
1108 for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; }
1111 outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << numMismatches << endl;
1114 outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl;
1115 if (thisfqualfile != "") {
1116 outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl;
1117 for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; }
1118 outScrapQual << endl;
1124 if((num) % 1000 == 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
1128 if((num) % 1000 != 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
1133 outScrapFasta.close();
1134 outMisMatch.close();
1135 if (thisfqualfile != "") {
1139 outScrapQual.close();
1143 if (m->control_pressed) { m->mothurRemove(outputFasta); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches); if (thisfqualfile != "") { m->mothurRemove(outputQual); m->mothurRemove(outputScrapQual); } }
1147 catch(exception& e) {
1148 m->errorOut(e, "MakeContigsCommand", "driver");
1152 //**********************************************************************************************************************
1153 vector< vector<string> > MakeContigsCommand::readFastqFiles(unsigned long int& count, string ffastq, string rfastq){
1155 vector< vector<string> > files;
1156 //maps processors number to file pointer
1157 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1158 map<int, vector<ofstream*> >::iterator it;
1160 //create files to write to
1161 for (int i = 0; i < processors; i++) {
1162 vector<ofstream*> temp;
1163 ofstream* outFF = new ofstream; temp.push_back(outFF);
1164 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
1165 ofstream* outRF = new ofstream; temp.push_back(outRF);
1166 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
1167 tempfiles[i] = temp;
1169 vector<string> names;
1170 string thisOutputDir = outputDir;
1171 if (outputDir == "") { thisOutputDir = m->hasPath(ffastq); }
1172 string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "ffastatemp";
1173 string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rfastatemp";
1174 string fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "fqualtemp";
1175 string rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rqualtemp";
1176 names.push_back(ffastafilename); names.push_back(fqualfilename);
1177 names.push_back(rfastafilename); names.push_back(rqualfilename);
1178 files.push_back(names);
1180 m->openOutputFile(ffastafilename, *outFF);
1181 m->openOutputFile(rfastafilename, *outRF);
1182 m->openOutputFile(fqualfilename, *outFQ);
1183 m->openOutputFile(rqualfilename, *outRQ);
1186 if (m->control_pressed) {
1187 //close files, delete ofstreams
1188 for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
1190 for (int i = 0; i < files.size(); i++) {
1191 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1196 m->openInputFile(ffastq, inForward);
1199 m->openInputFile(rfastq, inReverse);
1202 map<string, fastqRead> uniques;
1203 map<string, fastqRead>::iterator itUniques;
1204 while ((!inForward.eof()) || (!inReverse.eof())) {
1206 if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inForward.close(); inReverse.close(); return files; }
1208 //get a read from forward and reverse fastq files
1209 bool ignoref, ignorer;
1210 fastqRead thisFread, thisRread;
1211 if (!inForward.eof()) { thisFread = readFastq(inForward, ignoref); }
1212 else { ignoref = true; }
1213 if (!inReverse.eof()) { thisRread = readFastq(inReverse, ignorer); }
1214 else { ignorer = true; }
1216 vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1218 for (int i = 0; i < reads.size(); i++) {
1219 fastqRead fread = reads[i].forward;
1220 fastqRead rread = reads[i].reverse;
1222 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1224 if (checkReads(fread, rread, ffastq, rfastq)) {
1225 if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inForward.close(); inReverse.close(); return files; }
1227 //if the reads are okay write to output files
1228 int process = count % processors;
1230 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1231 *(tempfiles[process][1]) << ">" << fread.name << endl;
1232 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1233 *(tempfiles[process][1]) << endl;
1234 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1235 *(tempfiles[process][3]) << ">" << rread.name << endl;
1236 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1237 *(tempfiles[process][3]) << endl;
1242 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1247 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1249 if (uniques.size() != 0) {
1250 for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1251 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1253 m->mothurOutEndLine();
1256 //close files, delete ofstreams
1257 for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
1263 catch(exception& e) {
1264 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1268 //**********************************************************************************************************************
1269 vector< vector<string> > MakeContigsCommand::readFastaFiles(unsigned long int& count, string ffasta, string rfasta){
1271 vector< vector<string> > files;
1272 //maps processors number to file pointer
1273 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1274 map<int, vector<ofstream*> >::iterator it;
1276 //create files to write to
1277 for (int i = 0; i < processors; i++) {
1278 vector<ofstream*> temp;
1279 ofstream* outFF = new ofstream; temp.push_back(outFF);
1280 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
1281 ofstream* outRF = new ofstream; temp.push_back(outRF);
1282 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
1283 tempfiles[i] = temp;
1285 vector<string> names;
1286 string thisOutputDir = outputDir;
1287 if (outputDir == "") { thisOutputDir = m->hasPath(ffasta); }
1288 string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffasta)) + toString(i) + "ffastatemp";
1289 string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfasta)) + toString(i) + "rfastatemp";
1290 string fqualfilename = "";
1291 if (fqualfile != "") { fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(fqualfile)) + toString(i) + "fqual.temp"; m->openOutputFile(fqualfilename, *outFQ); }
1292 string rqualfilename = "";
1293 if (rqualfile != "") { rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rqualfile)) + toString(i) + "rqual.temp"; m->openOutputFile(rqualfilename, *outRQ); }
1294 names.push_back(ffastafilename); names.push_back(fqualfilename);
1295 names.push_back(rfastafilename); names.push_back(rqualfilename);
1296 files.push_back(names);
1298 m->openOutputFile(ffastafilename, *outFF);
1299 m->openOutputFile(rfastafilename, *outRF);
1302 if (m->control_pressed) {
1303 //close files, delete ofstreams
1304 for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
1306 for (int i = 0; i < files.size(); i++) {
1307 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1311 ifstream inForwardFasta;
1312 m->openInputFile(ffasta, inForwardFasta);
1314 ifstream inReverseFasta;
1315 m->openInputFile(rfasta, inReverseFasta);
1317 ifstream inForwardQual, inReverseQual;
1318 if (fqualfile != "") { m->openInputFile(fqualfile, inForwardQual); m->openInputFile(rqualfile, inReverseQual); }
1321 map<string, fastqRead> uniques;
1322 map<string, fastqRead>::iterator itUniques;
1323 while ((!inForwardFasta.eof()) || (!inReverseFasta.eof())) {
1325 if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; }
1327 //get a reads from forward and reverse fasta files
1328 bool ignoref, ignorer;
1329 fastqRead thisFread, thisRread;
1330 if (!inForwardFasta.eof()) {
1332 Sequence temp(inForwardFasta); m->gobble(inForwardFasta);
1333 thisFread.name = temp.getName();
1334 thisFread.sequence = temp.getUnaligned();
1335 }else { ignoref = true; }
1336 if (!inReverseFasta.eof()) {
1338 Sequence temp(inReverseFasta); m->gobble(inReverseFasta);
1339 thisRread.name = temp.getName();
1340 thisRread.sequence = temp.getUnaligned();
1341 }else { ignorer = true; }
1343 //get qual reads if given
1344 if (fqualfile != "") {
1345 if (!inForwardQual.eof() && !ignoref) {
1346 QualityScores temp(inForwardQual); m->gobble(inForwardQual);
1347 //if forward files dont match ignore read
1348 if (thisFread.name != temp.getName()) { ignoref = true; }
1349 else { thisFread.scores = temp.getQualityScores(); }
1350 }else { ignoref = true; }
1351 if (!inReverseQual.eof() && !ignorer) {
1352 QualityScores temp(inReverseQual); m->gobble(inReverseQual);
1353 //if reverse files dont match ignore read
1354 if (thisRread.name != temp.getName()) { ignorer = true; }
1355 else { thisRread.scores = temp.getQualityScores(); }
1356 }else { ignorer = true; }
1359 vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1361 for (int i = 0; i < reads.size(); i++) {
1362 fastqRead fread = reads[i].forward;
1363 fastqRead rread = reads[i].reverse;
1365 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1367 if (checkReads(fread, rread, ffasta, rfasta)) {
1368 if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; }
1370 //if the reads are okay write to output files
1371 int process = count % processors;
1373 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1374 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1375 if (fqualfile != "") { //if you have quality info, print it
1376 *(tempfiles[process][1]) << ">" << fread.name << endl;
1377 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1378 *(tempfiles[process][1]) << endl;
1379 *(tempfiles[process][3]) << ">" << rread.name << endl;
1380 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1381 *(tempfiles[process][3]) << endl;
1386 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1391 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1393 if (uniques.size() != 0) {
1394 for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1395 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1397 m->mothurOutEndLine();
1400 //close files, delete ofstreams
1401 for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
1402 inReverseFasta.close();
1403 inForwardFasta.close();
1404 if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); }
1408 catch(exception& e) {
1409 m->errorOut(e, "MakeContigsCommand", "readFastaFiles");
1413 //**********************************************************************************************************************
1414 vector<pairFastqRead> MakeContigsCommand::getReads(bool ignoref, bool ignorer, fastqRead forward, fastqRead reverse, map<string, fastqRead>& uniques){
1416 vector<pairFastqRead> reads;
1417 map<string, fastqRead>::iterator itUniques;
1419 if (!ignoref && !ignorer) {
1420 if (forward.name == reverse.name) {
1421 pairFastqRead temp(forward, reverse);
1422 reads.push_back(temp);
1425 //if no match are the names only different by 1 and 2?
1426 string tempFRead = forward.name.substr(0, forward.name.length()-1);
1427 string tempRRead = reverse.name.substr(0, reverse.name.length()-1);
1428 if (tempFRead == tempRRead) {
1429 if ((forward.name[forward.name.length()-1] == '1') && (reverse.name[reverse.name.length()-1] == '2')) {
1430 forward.name = tempFRead;
1431 reverse.name = tempRRead;
1432 pairFastqRead temp(forward, reverse);
1433 reads.push_back(temp);
1439 //look for forward pair
1440 itUniques = uniques.find(forward.name);
1441 if (itUniques != uniques.end()) { //we have the pair for this read
1442 pairFastqRead temp(forward, itUniques->second);
1443 reads.push_back(temp);
1444 uniques.erase(itUniques);
1445 }else { //save this read for later
1446 uniques[forward.name] = forward;
1449 //look for reverse pair
1450 itUniques = uniques.find(reverse.name);
1451 if (itUniques != uniques.end()) { //we have the pair for this read
1452 pairFastqRead temp(itUniques->second, reverse);
1453 reads.push_back(temp);
1454 uniques.erase(itUniques);
1455 }else { //save this read for later
1456 uniques[reverse.name] = reverse;
1461 }else if (!ignoref && ignorer) { //ignore reverse keep forward
1462 //look for forward pair
1463 itUniques = uniques.find(forward.name);
1464 if (itUniques != uniques.end()) { //we have the pair for this read
1465 pairFastqRead temp(forward, itUniques->second);
1466 reads.push_back(temp);
1467 uniques.erase(itUniques);
1468 }else { //save this read for later
1469 uniques[forward.name] = forward;
1472 }else if (ignoref && !ignorer) { //ignore forward keep reverse
1473 //look for reverse pair
1474 itUniques = uniques.find(reverse.name);
1475 if (itUniques != uniques.end()) { //we have the pair for this read
1476 pairFastqRead temp(itUniques->second, reverse);
1477 reads.push_back(temp);
1478 uniques.erase(itUniques);
1479 }else { //save this read for later
1480 uniques[reverse.name] = reverse;
1482 }//else ignore both and do nothing
1486 catch(exception& e) {
1487 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1491 //**********************************************************************************************************************
1492 fastqRead MakeContigsCommand::readFastq(ifstream& in, bool& ignore){
1498 //read sequence name
1499 string line = m->getline(in); m->gobble(in);
1500 vector<string> pieces = m->splitWhiteSpace(line);
1501 string name = ""; if (pieces.size() != 0) { name = pieces[0]; }
1502 if (name == "") { m->mothurOut("[WARNING]: Blank fasta name, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1503 else if (name[0] != '@') { m->mothurOut("[WARNING]: reading " + name + " expected a name with @ as a leading character, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1504 else { name = name.substr(1); }
1507 string sequence = m->getline(in); m->gobble(in);
1508 if (sequence == "") { m->mothurOut("[WARNING]: missing sequence for " + name + ", ignoring."); ignore=true; }
1510 //read sequence name
1511 line = m->getline(in); m->gobble(in);
1512 pieces = m->splitWhiteSpace(line);
1513 string name2 = ""; if (pieces.size() != 0) { name2 = pieces[0]; }
1514 if (name2 == "") { m->mothurOut("[WARNING]: expected a name with + as a leading character, ignoring."); ignore=true; }
1515 else if (name2[0] != '+') { m->mothurOut("[WARNING]: reading " + name2 + " expected a name with + as a leading character, ignoring."); ignore=true; }
1516 else { name2 = name2.substr(1); if (name2 == "") { name2 = name; } }
1518 //read quality scores
1519 string quality = m->getline(in); m->gobble(in);
1520 if (quality == "") { m->mothurOut("[WARNING]: missing quality for " + name2 + ", ignoring."); ignore=true; }
1522 //sanity check sequence length and number of quality scores match
1523 if (name2 != "") { if (name != name2) { m->mothurOut("[WARNING]: names do not match. read " + name + " for fasta and " + name2 + " for quality, ignoring."); ignore=true; } }
1524 if (quality.length() != sequence.length()) { m->mothurOut("[WARNING]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores, ignoring read."); ignore=true; }
1526 vector<int> qualScores = convertQual(quality);
1529 read.sequence = sequence;
1530 read.scores = qualScores;
1534 catch(exception& e) {
1535 m->errorOut(e, "MakeContigsCommand", "readFastq");
1539 //**********************************************************************************************************************
1540 bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse, string ffile, string rfile){
1544 //do sequence lengths match
1545 if (forward.sequence.length() != reverse.sequence.length()) {
1546 m->mothurOut("[WARNING]: For sequence " + forward.name + " I read a sequence of length " + toString(forward.sequence.length()) + " from " + ffile + ", but read a sequence of length " + toString(reverse.sequence.length()) + " from " + rfile + ", ignoring.\n");
1550 //do number of qual scores match
1551 if (forward.scores.size() != reverse.scores.size()) {
1552 m->mothurOut("[WARNING]: For sequence " + forward.name + " I read " + toString(forward.scores.size()) + " quality scores from " + ffile + ", but read " + toString(reverse.scores.size()) + " quality scores from " + rfile + ", ignoring.\n");
1558 catch(exception& e) {
1559 m->errorOut(e, "MakeContigsCommand", "checkReads");
1563 //***************************************************************************************************************
1564 vector< vector<string> > MakeContigsCommand::readFileNames(string filename){
1566 vector< vector<string> > files;
1567 string forward, reverse;
1570 m->openInputFile(filename, in);
1574 if (m->control_pressed) { return files; }
1576 in >> forward; m->gobble(in);
1577 in >> reverse; m->gobble(in);
1579 //check to make sure both are able to be opened
1581 int openForward = m->openInputFile(forward, in2, "noerror");
1583 //if you can't open it, try default location
1584 if (openForward == 1) {
1585 if (m->getDefaultPath() != "") { //default path is set
1586 string tryPath = m->getDefaultPath() + m->getSimpleName(forward);
1587 m->mothurOut("Unable to open " + forward + ". Trying default " + tryPath); m->mothurOutEndLine();
1589 openForward = m->openInputFile(tryPath, in3, "noerror");
1595 //if you can't open it, try output location
1596 if (openForward == 1) {
1597 if (m->getOutputDir() != "") { //default path is set
1598 string tryPath = m->getOutputDir() + m->getSimpleName(forward);
1599 m->mothurOut("Unable to open " + forward + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1601 openForward = m->openInputFile(tryPath, in4, "noerror");
1607 if (openForward == 1) { //can't find it
1608 m->mothurOut("[WARNING]: can't find " + forward + ", ignoring pair.\n");
1609 }else{ in2.close(); }
1612 int openReverse = m->openInputFile(reverse, in3, "noerror");
1614 //if you can't open it, try default location
1615 if (openReverse == 1) {
1616 if (m->getDefaultPath() != "") { //default path is set
1617 string tryPath = m->getDefaultPath() + m->getSimpleName(reverse);
1618 m->mothurOut("Unable to open " + reverse + ". Trying default " + tryPath); m->mothurOutEndLine();
1620 openReverse = m->openInputFile(tryPath, in3, "noerror");
1626 //if you can't open it, try output location
1627 if (openReverse == 1) {
1628 if (m->getOutputDir() != "") { //default path is set
1629 string tryPath = m->getOutputDir() + m->getSimpleName(reverse);
1630 m->mothurOut("Unable to open " + reverse + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1632 openReverse = m->openInputFile(tryPath, in4, "noerror");
1638 if (openReverse == 1) { //can't find it
1639 m->mothurOut("[WARNING]: can't find " + reverse + ", ignoring pair.\n");
1640 }else{ in3.close(); }
1642 if ((openForward != 1) && (openReverse != 1)) { //good pair
1643 vector<string> pair;
1644 pair.push_back(forward);
1645 pair.push_back(reverse);
1646 files.push_back(pair);
1654 catch(exception& e) {
1655 m->errorOut(e, "MakeContigsCommand", "checkReads");
1659 //***************************************************************************************************************
1660 //illumina data requires paired forward and reverse data
1661 //BARCODE atgcatgc atgcatgc groupName
1662 //PRIMER atgcatgc atgcatgc groupName
1663 //PRIMER atgcatgc atgcatgc
1664 bool MakeContigsCommand::getOligos(vector<vector<string> >& fastaFileNames, vector<vector<string> >& qualFileNames, string rootname){
1667 m->openInputFile(oligosfile, in);
1671 string type, foligo, roligo, group;
1673 int indexPrimer = 0;
1674 int indexBarcode = 0;
1675 set<string> uniquePrimers;
1676 set<string> uniqueBarcodes;
1681 cout << type << endl;
1682 if (m->debug) { m->mothurOut("[DEBUG]: reading type - " + type + ".\n"); }
1685 while (!in.eof()) { char c = in.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
1690 //make type case insensitive
1691 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); }
1695 if (m->debug) { m->mothurOut("[DEBUG]: reading - " + foligo + ".\n"); }
1697 for(int i=0;i<foligo.length();i++){
1698 foligo[i] = toupper(foligo[i]);
1699 if(foligo[i] == 'U') { foligo[i] = 'T'; }
1702 if(type == "FORWARD"){
1707 for(int i=0;i<roligo.length();i++){
1708 roligo[i] = toupper(roligo[i]);
1709 if(roligo[i] == 'U') { roligo[i] = 'T'; }
1711 //roligo = reverseOligo(roligo);
1715 // get rest of line in case there is a primer name
1718 if (c == 10 || c == 13){ break; }
1719 else if (c == 32 || c == 9){;} //space or tab
1720 else { group += c; }
1723 oligosPair newPrimer(foligo, roligo);
1725 //check for repeat barcodes
1726 string tempPair = foligo+roligo;
1727 if (uniquePrimers.count(tempPair) != 0) { m->mothurOut("primer pair " + newPrimer.forward + " " + newPrimer.reverse + " is in your oligos file already."); m->mothurOutEndLine(); }
1728 else { uniquePrimers.insert(tempPair); }
1730 if (m->debug) { if (group != "") { m->mothurOut("[DEBUG]: reading group " + group + ".\n"); }else{ m->mothurOut("[DEBUG]: no group for primer pair " + newPrimer.forward + " " + newPrimer.reverse + ".\n"); } }
1732 primers[indexPrimer]=newPrimer; indexPrimer++;
1733 primerNameVector.push_back(group);
1734 }else if(type == "BARCODE"){
1739 for(int i=0;i<roligo.length();i++){
1740 roligo[i] = toupper(roligo[i]);
1741 if(roligo[i] == 'U') { roligo[i] = 'T'; }
1743 //roligo = reverseOligo(roligo);
1745 oligosPair newPair(foligo, roligo);
1750 if (c == 10 || c == 13){ break; }
1751 else if (c == 32 || c == 9){;} //space or tab
1752 else { group += c; }
1755 if (m->debug) { m->mothurOut("[DEBUG]: barcode pair " + newPair.forward + " " + newPair.reverse + ", and group = " + group + ".\n"); }
1757 //check for repeat barcodes
1758 string tempPair = foligo+roligo;
1759 if (uniqueBarcodes.count(tempPair) != 0) { m->mothurOut("barcode pair " + newPair.forward + " " + newPair.reverse + " is in your oligos file already, disregarding."); m->mothurOutEndLine(); }
1760 else { uniqueBarcodes.insert(tempPair); }
1762 barcodes[indexBarcode]=newPair; indexBarcode++;
1763 barcodeNameVector.push_back(group);
1764 cout << group << endl;
1765 }else if(type == "LINKER"){
1766 linker.push_back(foligo);
1767 m->mothurOut("[WARNING]: make.contigs is not setup to remove linkers, ignoring.\n");
1768 }else if(type == "SPACER"){
1769 spacer.push_back(foligo);
1770 m->mothurOut("[WARNING]: make.contigs is not setup to remove spacers, ignoring.\n");
1772 else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are primer, barcode, linker and spacer. Ignoring " + foligo + "."); m->mothurOutEndLine(); }
1778 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0; }
1780 //add in potential combos
1781 if(barcodeNameVector.size() == 0){
1782 oligosPair temp("", "");
1784 barcodeNameVector.push_back("");
1787 if(primerNameVector.size() == 0){
1788 oligosPair temp("", "");
1790 primerNameVector.push_back("");
1793 fastaFileNames.resize(barcodeNameVector.size());
1794 for(int i=0;i<fastaFileNames.size();i++){
1795 fastaFileNames[i].assign(primerNameVector.size(), "");
1797 qualFileNames = fastaFileNames;
1800 set<string> uniqueNames; //used to cleanup outputFileNames
1801 for(map<int, oligosPair>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1802 for(map<int, oligosPair>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1804 string primerName = primerNameVector[itPrimer->first];
1805 string barcodeName = barcodeNameVector[itBar->first];
1807 if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing
1809 string comboGroupName = "";
1810 string fastaFileName = "";
1811 string qualFileName = "";
1812 string nameFileName = "";
1813 string countFileName = "";
1815 if(primerName == ""){
1816 comboGroupName = barcodeNameVector[itBar->first];
1819 if(barcodeName == ""){
1820 comboGroupName = primerNameVector[itPrimer->first];
1823 comboGroupName = barcodeNameVector[itBar->first] + "." + primerNameVector[itPrimer->first];
1829 fastaFileName = rootname + comboGroupName + ".fasta";
1830 if (uniqueNames.count(fastaFileName) == 0) {
1831 outputNames.push_back(fastaFileName);
1832 outputTypes["fasta"].push_back(fastaFileName);
1833 uniqueNames.insert(fastaFileName);
1836 fastaFileNames[itBar->first][itPrimer->first] = fastaFileName;
1837 m->openOutputFile(fastaFileName, temp); temp.close();
1839 if ((fqualfile != "") || (ffastqfile != "") || (file != "")) {
1840 qualFileName = rootname + ".qual";
1841 if (uniqueNames.count(qualFileName) == 0) {
1842 outputNames.push_back(qualFileName);
1843 outputTypes["qfile"].push_back(qualFileName);
1846 qualFileNames[itBar->first][itPrimer->first] = qualFileName;
1847 m->openOutputFile(qualFileName, temp); temp.close();
1854 bool allBlank = true;
1855 for (int i = 0; i < barcodeNameVector.size(); i++) {
1856 if (barcodeNameVector[i] != "") {
1861 for (int i = 0; i < primerNameVector.size(); i++) {
1862 if (primerNameVector[i] != "") {
1869 m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a groupfile."); m->mothurOutEndLine();
1877 catch(exception& e) {
1878 m->errorOut(e, "MakeContigsCommand", "getOligos");
1882 //********************************************************************/
1883 string MakeContigsCommand::reverseOligo(string oligo){
1885 string reverse = "";
1887 for(int i=oligo.length()-1;i>=0;i--){
1889 if(oligo[i] == 'A') { reverse += 'T'; }
1890 else if(oligo[i] == 'T'){ reverse += 'A'; }
1891 else if(oligo[i] == 'U'){ reverse += 'A'; }
1893 else if(oligo[i] == 'G'){ reverse += 'C'; }
1894 else if(oligo[i] == 'C'){ reverse += 'G'; }
1896 else if(oligo[i] == 'R'){ reverse += 'Y'; }
1897 else if(oligo[i] == 'Y'){ reverse += 'R'; }
1899 else if(oligo[i] == 'M'){ reverse += 'K'; }
1900 else if(oligo[i] == 'K'){ reverse += 'M'; }
1902 else if(oligo[i] == 'W'){ reverse += 'W'; }
1903 else if(oligo[i] == 'S'){ reverse += 'S'; }
1905 else if(oligo[i] == 'B'){ reverse += 'V'; }
1906 else if(oligo[i] == 'V'){ reverse += 'B'; }
1908 else if(oligo[i] == 'D'){ reverse += 'H'; }
1909 else if(oligo[i] == 'H'){ reverse += 'D'; }
1911 else { reverse += 'N'; }
1917 catch(exception& e) {
1918 m->errorOut(e, "MakeContigsCommand", "reverseOligo");
1922 //**********************************************************************************************************************
1923 vector<int> MakeContigsCommand::convertQual(string qual) {
1925 vector<int> qualScores;
1927 for (int i = 0; i < qual.length(); i++) {
1930 temp = int(qual[i]);
1931 if (format == "illumina") {
1932 temp -= 64; //char '@'
1933 }else if (format == "solexa") {
1934 temp = int(convertTable[temp]); //convert to sanger
1935 temp -= int('!'); //char '!'
1937 temp -= int('!'); //char '!'
1939 qualScores.push_back(temp);
1944 catch(exception& e) {
1945 m->errorOut(e, "MakeContigsCommand", "convertQual");
1950 //**********************************************************************************************************************