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 pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
37 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
39 vector<string> myArray;
40 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
44 m->errorOut(e, "MakeContigsCommand", "setParameters");
48 //**********************************************************************************************************************
49 string MakeContigsCommand::getHelpString(){
51 string helpString = "";
52 helpString += "The make.contigs command reads a file, forward fastq file and a reverse fastq file or forward fasta and reverse fasta files and outputs new fasta. It will also provide new quality files if the fastq or file parameter is used.\n";
53 helpString += "If an oligos file is provided barcodes and primers will be trimmed, and a group file will be created.\n";
54 helpString += "The make.contigs command parameters are ffastq, rfastq, oligos, tdiffs, bdiffs, ldiffs, sdiffs, pdiffs, align, match, mismatch, gapopen, gapextend, allfiles and processors.\n";
55 helpString += "The ffastq and rfastq, file, or ffasta and rfasta parameters are required.\n";
56 helpString += "The file parameter is 2 column file containing the forward fastq files in the first column and their matching reverse fastq files in the second column. Mothur will process each pair and create a combined fasta and qual file with all the sequences.\n";
57 helpString += "The ffastq and rfastq parameters are used to provide a forward fastq and reverse fastq file to process. If you provide one, you must provide the other.\n";
58 helpString += "The ffasta and rfasta parameters are used to provide a forward fasta and reverse fasta file to process. If you provide one, you must provide the other.\n";
59 helpString += "The fqfile and rqfile parameters are used to provide a forward quality and reverse quality files to process with the ffasta and rfasta parameters. If you provide one, you must provide the other.\n";
60 helpString += "The align parameter allows you to specify the alignment method to use. Your options are: gotoh and needleman. The default is needleman.\n";
61 helpString += "The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is pdiffs + bdiffs + sdiffs + ldiffs.\n";
62 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
63 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
64 helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
65 helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
66 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
67 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n";
68 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
69 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n";
70 helpString += "The threshold parameter allows you to set a quality scores threshold. In the case where we are trying to decide whether to keep a base or remove it because the base is compared to a gap in the other fragment, if the base has a quality score below the threshold we eliminate it. Default=40.\n";
71 helpString += "The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n";
72 helpString += "The allfiles parameter will create separate group and fasta file for each grouping. The default is F.\n";
73 helpString += "The make.contigs command should be in the following format: \n";
74 helpString += "make.contigs(ffastq=yourForwardFastqFile, rfastq=yourReverseFastqFile, align=yourAlignmentMethod) \n";
75 helpString += "Note: No spaces between parameter labels (i.e. ffastq), '=' and parameters (i.e.yourForwardFastqFile).\n";
79 m->errorOut(e, "MakeContigsCommand", "getHelpString");
83 //**********************************************************************************************************************
84 string MakeContigsCommand::getOutputPattern(string type) {
88 if (type == "fasta") { pattern = "[filename],[tag],contigs.fasta"; }
89 else if (type == "qfile") { pattern = "[filename],[tag],contigs.qual"; }
90 else if (type == "group") { pattern = "[filename],[tag],contigs.groups"; }
91 else if (type == "mismatch") { pattern = "[filename],[tag],contigs.mismatch"; }
92 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
97 m->errorOut(e, "MakeContigsCommand", "getOutputPattern");
101 //**********************************************************************************************************************
102 MakeContigsCommand::MakeContigsCommand(){
104 abort = true; calledHelp = true;
106 vector<string> tempOutNames;
107 outputTypes["fasta"] = tempOutNames;
108 outputTypes["qfile"] = tempOutNames;
109 outputTypes["group"] = tempOutNames;
110 outputTypes["mismatch"] = tempOutNames;
112 catch(exception& e) {
113 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
117 //**********************************************************************************************************************
118 MakeContigsCommand::MakeContigsCommand(string option) {
120 abort = false; calledHelp = false;
122 //allow user to run help
123 if(option == "help") { help(); abort = true; calledHelp = true; }
124 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
127 vector<string> myArray = setParameters();
129 OptionParser parser(option);
130 map<string, string> parameters = parser.getParameters();
132 ValidParameters validParameter("pairwise.seqs");
133 map<string, string>::iterator it;
135 //check to make sure all parameters are valid for command
136 for (it = parameters.begin(); it != parameters.end(); it++) {
137 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
140 //initialize outputTypes
141 vector<string> tempOutNames;
142 outputTypes["fasta"] = tempOutNames;
143 outputTypes["qfile"] = tempOutNames;
144 outputTypes["mismatch"] = tempOutNames;
145 outputTypes["group"] = tempOutNames;
148 //if the user changes the input directory command factory will send this info to us in the output parameter
149 string inputDir = validParameter.validFile(parameters, "inputdir", false);
150 if (inputDir == "not found"){ inputDir = ""; }
153 it = parameters.find("ffastq");
154 //user has given a template file
155 if(it != parameters.end()){
156 path = m->hasPath(it->second);
157 //if the user has not given a path then, add inputdir. else leave path alone.
158 if (path == "") { parameters["ffastq"] = inputDir + it->second; }
161 it = parameters.find("rfastq");
162 //user has given a template file
163 if(it != parameters.end()){
164 path = m->hasPath(it->second);
165 //if the user has not given a path then, add inputdir. else leave path alone.
166 if (path == "") { parameters["rfastq"] = inputDir + it->second; }
169 it = parameters.find("ffasta");
170 //user has given a template file
171 if(it != parameters.end()){
172 path = m->hasPath(it->second);
173 //if the user has not given a path then, add inputdir. else leave path alone.
174 if (path == "") { parameters["ffasta"] = inputDir + it->second; }
177 it = parameters.find("rfasta");
178 //user has given a template file
179 if(it != parameters.end()){
180 path = m->hasPath(it->second);
181 //if the user has not given a path then, add inputdir. else leave path alone.
182 if (path == "") { parameters["rfasta"] = inputDir + it->second; }
185 it = parameters.find("fqfile");
186 //user has given a template file
187 if(it != parameters.end()){
188 path = m->hasPath(it->second);
189 //if the user has not given a path then, add inputdir. else leave path alone.
190 if (path == "") { parameters["fqfile"] = inputDir + it->second; }
193 it = parameters.find("rqfile");
194 //user has given a template file
195 if(it != parameters.end()){
196 path = m->hasPath(it->second);
197 //if the user has not given a path then, add inputdir. else leave path alone.
198 if (path == "") { parameters["rqfile"] = inputDir + it->second; }
201 it = parameters.find("file");
202 //user has given a template file
203 if(it != parameters.end()){
204 path = m->hasPath(it->second);
205 //if the user has not given a path then, add inputdir. else leave path alone.
206 if (path == "") { parameters["file"] = inputDir + it->second; }
209 it = parameters.find("oligos");
210 //user has given a template file
211 if(it != parameters.end()){
212 path = m->hasPath(it->second);
213 //if the user has not given a path then, add inputdir. else leave path alone.
214 if (path == "") { parameters["oligos"] = inputDir + it->second; }
218 ffastqfile = validParameter.validFile(parameters, "ffastq", true);
219 if (ffastqfile == "not open") { abort = true; }
220 else if (ffastqfile == "not found") { ffastqfile = ""; }
222 rfastqfile = validParameter.validFile(parameters, "rfastq", true);
223 if (rfastqfile == "not open") { abort = true; }
224 else if (rfastqfile == "not found") { rfastqfile = ""; }
226 ffastafile = validParameter.validFile(parameters, "ffasta", true);
227 if (ffastafile == "not open") { abort = true; }
228 else if (ffastafile == "not found") { ffastafile = ""; }
230 rfastafile = validParameter.validFile(parameters, "rfasta", true);
231 if (rfastafile == "not open") { abort = true; }
232 else if (rfastafile == "not found") { rfastafile = ""; }
234 fqualfile = validParameter.validFile(parameters, "fqfile", true);
235 if (fqualfile == "not open") { abort = true; }
236 else if (fqualfile == "not found") { fqualfile = ""; }
238 rqualfile = validParameter.validFile(parameters, "rqfile", true);
239 if (rqualfile == "not open") { abort = true; }
240 else if (rqualfile == "not found") { rqualfile = ""; }
242 file = validParameter.validFile(parameters, "file", true);
243 if (file == "not open") { abort = true; }
244 else if (file == "not found") { file = ""; }
247 if ((file == "") && (ffastafile == "") && (ffastqfile == "")) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
248 if ((file != "") && ((ffastafile != "") || (ffastqfile != ""))) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
249 if ((ffastqfile != "") && (rfastqfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffastq, you must provide a rfastq file.\n"); }
250 if ((ffastqfile == "") && (rfastqfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfastq, you must provide a ffastq file.\n"); }
251 if ((ffastafile != "") && (rfastafile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffasta, you must provide a rfasta file.\n"); }
252 if ((ffastafile == "") && (rfastafile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfasta, you must provide a ffasta file.\n"); }
253 if ((fqualfile != "") && (rqualfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the fqfile, you must provide a rqfile file.\n"); }
254 if ((fqualfile == "") && (rqualfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile, you must provide a fqfile file.\n"); }
255 if (((fqualfile != "") || (rqualfile != "")) && ((ffastafile == "") || (rfastafile == ""))) {
256 abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile or fqfile file, you must provide the ffasta and rfasta parameters.\n");
259 oligosfile = validParameter.validFile(parameters, "oligos", true);
260 if (oligosfile == "not found") { oligosfile = ""; }
261 else if(oligosfile == "not open") { abort = true; }
262 else { m->setOligosFile(oligosfile); }
264 //if the user changes the output directory command factory will send this info to us in the output parameter
265 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
270 //check for optional parameter and set defaults
271 // ...at some point should added some additional type checking...
273 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
274 m->mothurConvert(temp, match);
276 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
277 m->mothurConvert(temp, misMatch);
278 if (misMatch > 0) { m->mothurOut("[ERROR]: mismatch must be negative.\n"); abort=true; }
280 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
281 m->mothurConvert(temp, gapOpen);
282 if (gapOpen > 0) { m->mothurOut("[ERROR]: gapopen must be negative.\n"); abort=true; }
284 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
285 m->mothurConvert(temp, gapExtend);
286 if (gapExtend > 0) { m->mothurOut("[ERROR]: gapextend must be negative.\n"); abort=true; }
288 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found"){ temp = "40"; }
289 m->mothurConvert(temp, threshold);
290 if ((threshold < 0) || (threshold > 40)) { m->mothurOut("[ERROR]: threshold must be between 0 and 40.\n"); abort=true; }
292 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
293 m->setProcessors(temp);
294 m->mothurConvert(temp, processors);
296 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found") { temp = "0"; }
297 m->mothurConvert(temp, bdiffs);
299 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found") { temp = "0"; }
300 m->mothurConvert(temp, pdiffs);
302 // temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
303 // m->mothurConvert(temp, ldiffs);
306 // temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
307 // m->mothurConvert(temp, sdiffs);
310 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
311 m->mothurConvert(temp, tdiffs);
313 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; } //+ ldiffs + sdiffs;
315 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
316 allFiles = m->isTrue(temp);
318 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
319 if ((align != "needleman") && (align != "gotoh")) { m->mothurOut(align + " is not a valid alignment method. Options are needleman or gotoh. I will use needleman."); m->mothurOutEndLine(); align = "needleman"; }
323 catch(exception& e) {
324 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
328 //**********************************************************************************************************************
329 int MakeContigsCommand::execute(){
331 if (abort == true) { if (calledHelp) { return 0; } return 2; }
333 //read ffastq and rfastq files creating fasta and qual files.
334 //this function will create a forward and reverse, fasta and qual files for each processor.
335 //files has an entry for each processor. files[i][0] = forwardFasta, files[i][1] = forwardQual, files[i][2] = reverseFasta, files[i][3] = reverseQual. filesToProcess is for each filepair in the file parameter file. for ffastq and rfastq this will be size 1.
336 unsigned long int numReads = 0;
337 int start = time(NULL);
339 m->mothurOut("Reading fastq data...\n");
340 vector < vector< vector<string> > > filesToProcess = preProcessData(numReads);
341 m->mothurOut("Done.\n");
343 if (m->control_pressed) { return 0; }
345 map<string, string> cvars;
346 string compOutputDir = outputDir;
347 if (outputDir == "") { compOutputDir = m->hasPath(file); }
348 cvars["[filename]"] = compOutputDir + m->getRootName(m->getSimpleName(file));
350 string compositeGroupFile = getOutputFileName("group",cvars);
351 cvars["[tag]"] = "trim";
352 string compositeFastaFile = getOutputFileName("fasta",cvars);
353 string compositeQualFile = getOutputFileName("qfile",cvars);
354 cvars["[tag]"] = "scrap";
355 string compositeScrapFastaFile = getOutputFileName("fasta",cvars);
356 string compositeScrapQualFile = getOutputFileName("qfile",cvars);
358 string compositeMisMatchFile = getOutputFileName("mismatch",cvars);
360 if (filesToProcess.size() > 1) { //clear files for append below
361 ofstream outCTFasta, outCTQual, outCSFasta, outCSQual, outCMisMatch;
362 m->openOutputFile(compositeFastaFile, outCTFasta); outCTFasta.close();
363 m->openOutputFile(compositeScrapFastaFile, outCSFasta); outCSFasta.close();
364 m->openOutputFile(compositeMisMatchFile, outCMisMatch); outCMisMatch.close();
365 m->openOutputFile(compositeQualFile, outCTQual); outCTQual.close();
366 m->openOutputFile(compositeScrapQualFile, outCSQual); outCSQual.close();
367 outputNames.push_back(compositeFastaFile); outputTypes["fasta"].push_back(compositeFastaFile);
368 outputNames.push_back(compositeQualFile); outputTypes["qfile"].push_back(compositeQualFile);
369 outputNames.push_back(compositeMisMatchFile); outputTypes["mismatch"].push_back(compositeMisMatchFile);
370 outputNames.push_back(compositeScrapFastaFile); outputTypes["fasta"].push_back(compositeScrapFastaFile);
371 outputNames.push_back(compositeScrapQualFile); outputTypes["qfile"].push_back(compositeScrapQualFile);
374 for (int l = 0; l < filesToProcess.size(); l++) {
376 m->mothurOut("\n>>>>>\tProcessing " + filesToProcess[l][0][0] + " (file " + toString(l+1) + " of " + toString(filesToProcess.size()) + ")\t<<<<<\n");
378 vector<vector<string> > fastaFileNames;
379 vector<vector<string> > qualFileNames;
381 string outputGroupFileName;
382 map<string, string> variables;
383 string thisOutputDir = outputDir;
384 if (outputDir == "") { thisOutputDir = m->hasPath(filesToProcess[l][0][0]); }
385 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(filesToProcess[l][0][0]));
386 variables["[tag]"] = "";
387 if(oligosfile != ""){
388 createGroup = getOligos(fastaFileNames, qualFileNames, variables["[filename]"]);
390 outputGroupFileName = getOutputFileName("group",variables);
391 outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName);
395 variables["[tag]"] = "trim";
396 string outFastaFile = getOutputFileName("fasta",variables);
397 string outQualFile = getOutputFileName("qfile",variables);
398 variables["[tag]"] = "scrap";
399 string outScrapFastaFile = getOutputFileName("fasta",variables);
400 string outScrapQualFile = getOutputFileName("qfile",variables);
401 variables["[tag]"] = "";
402 string outMisMatchFile = getOutputFileName("mismatch",variables);
403 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
404 outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile);
405 if (filesToProcess[l][0][1] != "") {
406 outputNames.push_back(outQualFile); outputTypes["qfile"].push_back(outQualFile);
407 outputNames.push_back(outScrapQualFile); outputTypes["qfile"].push_back(outScrapQualFile);
409 outputNames.push_back(outMisMatchFile); outputTypes["mismatch"].push_back(outMisMatchFile);
411 m->mothurOut("Making contigs...\n");
412 createProcesses(filesToProcess[l], outFastaFile, outQualFile, outScrapFastaFile, outScrapQualFile, outMisMatchFile, fastaFileNames, qualFileNames);
413 m->mothurOut("Done.\n");
415 //remove temp fasta and qual files
416 for (int i = 0; i < processors; i++) { for(int j = 0; j < filesToProcess[l][i].size(); j++) { m->mothurRemove(filesToProcess[l][i][j]); } }
418 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
421 map<string, string> uniqueFastaNames;// so we don't add the same groupfile multiple times
422 map<string, string>::iterator it;
423 set<string> namesToRemove;
424 for(int i=0;i<fastaFileNames.size();i++){
425 for(int j=0;j<fastaFileNames[0].size();j++){
426 if (fastaFileNames[i][j] != "") {
427 if (namesToRemove.count(fastaFileNames[i][j]) == 0) {
428 if(m->isBlank(fastaFileNames[i][j])){
429 m->mothurRemove(fastaFileNames[i][j]);
430 namesToRemove.insert(fastaFileNames[i][j]);
432 if (filesToProcess[l][0][1] != "") {
433 m->mothurRemove(qualFileNames[i][j]);
434 namesToRemove.insert(qualFileNames[i][j]);
437 it = uniqueFastaNames.find(fastaFileNames[i][j]);
438 if (it == uniqueFastaNames.end()) {
439 uniqueFastaNames[fastaFileNames[i][j]] = barcodeNameVector[i];
447 //remove names for outputFileNames, just cleans up the output
448 vector<string> outputNames2;
449 for(int i = 0; i < outputNames.size(); i++) { if (namesToRemove.count(outputNames[i]) == 0) { outputNames2.push_back(outputNames[i]); } }
450 outputNames = outputNames2;
452 for (it = uniqueFastaNames.begin(); it != uniqueFastaNames.end(); it++) {
454 m->openInputFile(it->first, in);
457 string thisGroupName = thisOutputDir + m->getRootName(m->getSimpleName(it->first));
458 thisGroupName += getOutputFileName("group",variables); outputNames.push_back(thisGroupName); outputTypes["group"].push_back(thisGroupName);
459 m->openOutputFile(thisGroupName, out);
462 if (m->control_pressed) { break; }
464 Sequence currSeq(in); m->gobble(in);
465 out << currSeq.getName() << '\t' << it->second << endl;
474 m->openOutputFile(outputGroupFileName, outGroup);
475 for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
476 outGroup << itGroup->first << '\t' << itGroup->second << endl;
481 if (filesToProcess.size() > 1) { //merge into large combo files
485 m->openOutputFile(compositeGroupFile, outCGroup); outCGroup.close();
486 outputNames.push_back(compositeGroupFile); outputTypes["group"].push_back(compositeGroupFile);
488 m->appendFiles(outputGroupFileName, compositeGroupFile);
490 m->appendFiles(outMisMatchFile, compositeMisMatchFile);
491 m->appendFiles(outFastaFile, compositeFastaFile);
492 m->appendFiles(outQualFile, compositeQualFile);
493 m->appendFiles(outScrapFastaFile, compositeScrapFastaFile);
494 m->appendFiles(outScrapQualFile, compositeScrapQualFile);
497 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process " + toString(numReads) + " sequences.\n");
499 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
501 //output group counts
502 m->mothurOutEndLine();
504 if (groupCounts.size() != 0) { m->mothurOut("Group count: \n"); }
505 for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
506 total += it->second; m->mothurOut(it->first + "\t" + toString(it->second)); m->mothurOutEndLine();
508 if (total != 0) { m->mothurOut("Total of all groups is " + toString(total)); m->mothurOutEndLine(); }
510 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
512 string currentFasta = "";
513 itTypes = outputTypes.find("fasta");
514 if (itTypes != outputTypes.end()) {
515 if ((itTypes->second).size() != 0) { currentFasta = (itTypes->second)[0]; m->setFastaFile(currentFasta); }
518 string currentQual = "";
519 itTypes = outputTypes.find("qfile");
520 if (itTypes != outputTypes.end()) {
521 if ((itTypes->second).size() != 0) { currentQual = (itTypes->second)[0]; m->setQualFile(currentQual); }
524 string currentGroup = "";
525 itTypes = outputTypes.find("group");
526 if (itTypes != outputTypes.end()) {
527 if ((itTypes->second).size() != 0) { currentGroup = (itTypes->second)[0]; m->setGroupFile(currentGroup); }
530 //output files created by command
531 m->mothurOutEndLine();
532 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
533 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
534 m->mothurOutEndLine();
538 catch(exception& e) {
539 m->errorOut(e, "MakeContigsCommand", "execute");
543 //**********************************************************************************************************************
544 vector< vector< vector<string> > > MakeContigsCommand::preProcessData(unsigned long int& numReads) {
546 vector< vector< vector<string> > > filesToProcess;
548 if (ffastqfile != "") { //reading one file
549 vector< vector<string> > files = readFastqFiles(numReads, ffastqfile, rfastqfile);
550 //adjust for really large processors or really small files
551 if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
552 if (numReads < processors) {
553 for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
554 files.resize(numReads);
555 processors = numReads;
557 filesToProcess.push_back(files);
558 }else if (file != "") { //reading multiple files
559 //return only valid pairs
560 vector< vector<string> > filePairsToProcess = readFileNames(file);
562 if (m->control_pressed) { return filesToProcess; }
564 if (filePairsToProcess.size() != 0) {
565 for (int i = 0; i < filePairsToProcess.size(); i++) {
567 if (m->control_pressed) { for (int l = 0; l < filesToProcess.size(); l++) { for (int k = 0; k < filesToProcess[l].size(); k++) { for(int j = 0; j < filesToProcess[l][k].size(); j++) { m->mothurRemove(filesToProcess[l][k][j]); } filesToProcess[l][k].clear(); } return filesToProcess; } }
569 unsigned long int thisFilesReads;
570 vector< vector<string> > files = readFastqFiles(thisFilesReads, filePairsToProcess[i][0], filePairsToProcess[i][1]);
572 //adjust for really large processors or really small files
573 if (thisFilesReads < processors) {
574 m->mothurOut("[ERROR]: " + filePairsToProcess[i][0] + " has less than " + toString(processors) + " good reads, skipping\n");
575 for (int k = 0; k < files.size(); k++) { for(int j = 0; j < files[k].size(); j++) { m->mothurRemove(files[k][j]); } files[k].clear(); }
577 filesToProcess.push_back(files);
578 numReads += thisFilesReads;
582 if (numReads == 0) { m->control_pressed = true; }
584 }else if (ffastafile != "") {
585 vector< vector<string> > files = readFastaFiles(numReads, ffastafile, rfastafile);
586 //adjust for really large processors or really small files
587 if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
588 if (numReads < processors) {
589 for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
590 files.resize(numReads);
591 processors = numReads;
593 filesToProcess.push_back(files);
594 }else { m->control_pressed = true; } //should not get here
596 return filesToProcess;
598 catch(exception& e) {
599 m->errorOut(e, "MakeContigsCommand", "preProcessData");
603 //**********************************************************************************************************************
604 int MakeContigsCommand::createProcesses(vector< vector<string> > files, string outputFasta, string outputQual, string outputScrapFasta, string outputScrapQual, string outputMisMatches, vector<vector<string> > fastaFileNames, vector<vector<string> > qualFileNames) {
607 vector<int> processIDS;
608 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
611 //loop through and create all the processes you want
612 while (process != processors-1) {
616 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
619 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
620 vector<vector<string> > tempPrimerQualFileNames = qualFileNames;
625 for(int i=0;i<tempFASTAFileNames.size();i++){
626 for(int j=0;j<tempFASTAFileNames[i].size();j++){
627 if (tempFASTAFileNames[i][j] != "") {
628 tempFASTAFileNames[i][j] += toString(getpid()) + ".temp";
629 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
631 if (files[processors-1][1] != "") {
632 tempPrimerQualFileNames[i][j] += toString(getpid()) + ".temp";
633 m->openOutputFile(tempPrimerQualFileNames[i][j], temp); temp.close();
640 num = driver(files[process],
641 outputFasta + toString(getpid()) + ".temp",
642 outputQual + toString(getpid()) + ".temp",
643 outputScrapFasta + toString(getpid()) + ".temp",
644 outputScrapQual + toString(getpid()) + ".temp",
645 outputMisMatches + toString(getpid()) + ".temp",
647 tempPrimerQualFileNames);
649 //pass groupCounts to parent
651 string tempFile = toString(getpid()) + ".num.temp";
652 m->openOutputFile(tempFile, out);
655 out << groupCounts.size() << endl;
657 for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
658 out << it->first << '\t' << it->second << endl;
661 out << groupMap.size() << endl;
662 for (map<string, string>::iterator it = groupMap.begin(); it != groupMap.end(); it++) {
663 out << it->first << '\t' << it->second << endl;
670 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
671 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
677 m->openOutputFile(outputFasta, temp); temp.close();
678 m->openOutputFile(outputScrapFasta, temp); temp.close();
679 if (files[processors-1][1] != "") {
680 m->openOutputFile(outputScrapQual, temp); temp.close();
681 m->openOutputFile(outputQual, temp); temp.close();
685 num = driver(files[processors-1], outputFasta, outputQual, outputScrapFasta, outputScrapQual, outputMisMatches, fastaFileNames, qualFileNames);
687 //force parent to wait until all the processes are done
688 for (int i=0;i<processIDS.size();i++) {
689 int temp = processIDS[i];
693 for (int i = 0; i < processIDS.size(); i++) {
695 string tempFile = toString(processIDS[i]) + ".num.temp";
696 m->openInputFile(tempFile, in);
698 in >> tempNum; num += tempNum; m->gobble(in);
702 in >> tempNum; m->gobble(in);
705 for (int j = 0; j < tempNum; j++) {
707 in >> group >> groupNum; m->gobble(in);
709 map<string, int>::iterator it = groupCounts.find(group);
710 if (it == groupCounts.end()) { groupCounts[group] = groupNum; }
711 else { groupCounts[it->first] += groupNum; }
714 in >> tempNum; m->gobble(in);
716 for (int j = 0; j < tempNum; j++) {
717 string group, seqName;
718 in >> seqName >> group; m->gobble(in);
720 map<string, string>::iterator it = groupMap.find(seqName);
721 if (it == groupMap.end()) { groupMap[seqName] = group; }
722 else { m->mothurOut("[ERROR]: " + seqName + " is in your fasta file more than once. Sequence names must be unique. please correct.\n"); }
726 in.close(); m->mothurRemove(tempFile);
730 //////////////////////////////////////////////////////////////////////////////////////////////////////
731 //Windows version shared memory, so be careful when passing variables through the contigsData struct.
732 //Above fork() will clone, so memory is separate, but that's not the case with windows,
733 //////////////////////////////////////////////////////////////////////////////////////////////////////
735 vector<contigsData*> pDataArray;
736 DWORD dwThreadIdArray[processors-1];
737 HANDLE hThreadArray[processors-1];
739 //Create processor worker threads.
740 for( int h=0; h<processors-1; h++ ){
741 string extension = "";
742 if (h != 0) { extension = toString(h) + ".temp"; processIDS.push_back(h); }
743 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
744 vector<vector<string> > tempPrimerQualFileNames = qualFileNames;
749 for(int i=0;i<tempFASTAFileNames.size();i++){
750 for(int j=0;j<tempFASTAFileNames[i].size();j++){
751 if (tempFASTAFileNames[i][j] != "") {
752 tempFASTAFileNames[i][j] += extension;
753 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
755 if (files[processors-1][1] != "") {
756 tempPrimerQualFileNames[i][j] += extension;
757 m->openOutputFile(tempPrimerQualFileNames[i][j], temp); temp.close();
765 contigsData* tempcontig = new contigsData(files[h], (outputFasta + extension), (outputQual + extension), (outputScrapFasta + extension), (outputScrapQual + extension),(outputMisMatches + extension), align, m, match, misMatch, gapOpen, gapExtend, threshold, barcodes, primers, tempFASTAFileNames, tempPrimerQualFileNames, barcodeNameVector, primerNameVector, pdiffs, bdiffs, tdiffs, createGroup, allFiles, h);
766 pDataArray.push_back(tempcontig);
768 hThreadArray[h] = CreateThread(NULL, 0, MyContigsThreadFunction, pDataArray[h], 0, &dwThreadIdArray[h]);
771 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
772 vector<vector<string> > tempPrimerQualFileNames = qualFileNames;
776 string extension = toString(processors-1) + ".temp";
778 for(int i=0;i<tempFASTAFileNames.size();i++){
779 for(int j=0;j<tempFASTAFileNames[i].size();j++){
780 if (tempFASTAFileNames[i][j] != "") {
781 tempFASTAFileNames[i][j] += extension;
782 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
784 if (files[processors-1][1] != "") {
785 tempPrimerQualFileNames[i][j] += extension;
786 m->openOutputFile(tempPrimerQualFileNames[i][j], temp); temp.close();
795 m->openOutputFile(outputFasta, temp); temp.close();
796 m->openOutputFile(outputScrapFasta, temp); temp.close();
797 if (files[processors-1][1] != "") {
798 m->openOutputFile(outputScrapQual, temp); temp.close();
799 m->openOutputFile(outputQual, temp); temp.close();
803 processIDS.push_back(processors-1);
804 num = driver(files[processors-1], (outputFasta+ toString(processors-1) + ".temp"), (outputQual+ toString(processors-1) + ".temp"), (outputScrapFasta+ toString(processors-1) + ".temp"), (outputScrapQual+ toString(processors-1) + ".temp"), (outputMisMatches+ toString(processors-1) + ".temp"), tempFASTAFileNames, tempPrimerQualFileNames);
806 //Wait until all threads have terminated.
807 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
809 //Close all thread handles and free memory allocations.
810 for(int i=0; i < pDataArray.size(); i++){
811 num += pDataArray[i]->count;
812 for (map<string, int>::iterator it = pDataArray[i]->groupCounts.begin(); it != pDataArray[i]->groupCounts.end(); it++) {
813 map<string, int>::iterator it2 = groupCounts.find(it->first);
814 if (it2 == groupCounts.end()) { groupCounts[it->first] = it->second; }
815 else { groupCounts[it->first] += it->second; }
817 for (map<string, string>::iterator it = pDataArray[i]->groupMap.begin(); it != pDataArray[i]->groupMap.end(); it++) {
818 map<string, string>::iterator it2 = groupMap.find(it->first);
819 if (it2 == groupMap.end()) { groupMap[it->first] = it->second; }
820 else { m->mothurOut("[ERROR]: " + it->first + " is in your fasta file more than once. Sequence names must be unique. please correct.\n"); }
822 CloseHandle(hThreadArray[i]);
823 delete pDataArray[i];
828 for (int i = 0; i < processIDS.size(); i++) {
829 m->appendFiles((outputFasta + toString(processIDS[i]) + ".temp"), outputFasta);
830 m->mothurRemove((outputFasta + toString(processIDS[i]) + ".temp"));
832 m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta);
833 m->mothurRemove((outputScrapFasta + toString(processIDS[i]) + ".temp"));
835 if (files[processors-1][1] != "") {
836 m->appendFiles((outputScrapQual + toString(processIDS[i]) + ".temp"), outputScrapQual);
837 m->mothurRemove((outputScrapQual + toString(processIDS[i]) + ".temp"));
839 m->appendFiles((outputQual + toString(processIDS[i]) + ".temp"), outputQual);
840 m->mothurRemove((outputQual + toString(processIDS[i]) + ".temp"));
843 m->appendFiles((outputMisMatches + toString(processIDS[i]) + ".temp"), outputMisMatches);
844 m->mothurRemove((outputMisMatches + toString(processIDS[i]) + ".temp"));
847 for(int j=0;j<fastaFileNames.size();j++){
848 for(int k=0;k<fastaFileNames[j].size();k++){
849 if (fastaFileNames[j][k] != "") {
850 m->appendFiles((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"), fastaFileNames[j][k]);
851 m->mothurRemove((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"));
853 if (files[processors-1][1] != "") {
854 m->appendFiles((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"), qualFileNames[j][k]);
855 m->mothurRemove((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"));
865 catch(exception& e) {
866 m->errorOut(e, "MakeContigsCommand", "createProcesses");
870 //**********************************************************************************************************************
871 int MakeContigsCommand::driver(vector<string> files, string outputFasta, string outputQual, string outputScrapFasta, string outputScrapQual, string outputMisMatches, vector<vector<string> > fastaFileNames, vector<vector<string> > qualFileNames){
874 Alignment* alignment;
875 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
876 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
879 string thisffastafile = files[0];
880 string thisfqualfile = files[1];
881 string thisrfastafile = files[2];
882 string thisrqualfile = files[3];
884 if (m->debug) { m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
886 ifstream inFFasta, inRFasta, inFQual, inRQual;
887 ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual;
888 m->openInputFile(thisffastafile, inFFasta);
889 m->openInputFile(thisrfastafile, inRFasta);
890 if (thisfqualfile != "") {
891 m->openInputFile(thisfqualfile, inFQual);
892 m->openInputFile(thisrqualfile, inRQual);
893 m->openOutputFile(outputScrapQual, outScrapQual);
894 m->openOutputFile(outputQual, outQual);
896 m->openOutputFile(outputFasta, outFasta);
897 m->openOutputFile(outputScrapFasta, outScrapFasta);
898 m->openOutputFile(outputMisMatches, outMisMatch);
899 outMisMatch << "Name\tLength\tMisMatches\n";
901 TrimOligos trimOligos(pdiffs, bdiffs, 0, 0, primers, barcodes);
903 while ((!inFFasta.eof()) && (!inRFasta.eof())) {
905 if (m->control_pressed) { break; }
908 string trashCode = "";
909 int currentSeqsDiffs = 0;
911 //read seqs and quality info
912 Sequence fSeq(inFFasta); m->gobble(inFFasta);
913 Sequence rSeq(inRFasta); m->gobble(inRFasta);
914 QualityScores* fQual = NULL; QualityScores* rQual = NULL;
915 if (thisfqualfile != "") {
916 fQual = new QualityScores(inFQual); m->gobble(inFQual);
917 rQual = new QualityScores(inRQual); m->gobble(inRQual);
920 int barcodeIndex = 0;
923 if(barcodes.size() != 0){
924 if (thisfqualfile != "") {
925 success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
927 success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
929 if(success > bdiffs) { trashCode += 'b'; }
930 else{ currentSeqsDiffs += success; }
933 if(primers.size() != 0){
934 if (thisfqualfile != "") {
935 success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
937 success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
939 if(success > pdiffs) { trashCode += 'f'; }
940 else{ currentSeqsDiffs += success; }
943 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
945 //flip the reverse reads
946 rSeq.reverseComplement();
947 if (thisfqualfile != "") { rQual->flipQScores(); }
950 alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
951 map<int, int> ABaseMap = alignment->getSeqAAlnBaseMap();
952 map<int, int> BBaseMap = alignment->getSeqBAlnBaseMap();
953 fSeq.setAligned(alignment->getSeqAAln());
954 rSeq.setAligned(alignment->getSeqBAln());
955 int length = fSeq.getAligned().length();
957 //traverse alignments merging into one contiguous seq
959 vector<int> contigScores;
960 int numMismatches = 0;
961 string seq1 = fSeq.getAligned();
962 string seq2 = rSeq.getAligned();
963 vector<int> scores1, scores2;
964 if (thisfqualfile != "") {
965 scores1 = fQual->getQualityScores();
966 scores2 = rQual->getQualityScores();
967 delete fQual; delete rQual;
970 // if (num < 5) { cout << fSeq.getStartPos() << '\t' << fSeq.getEndPos() << '\t' << rSeq.getStartPos() << '\t' << rSeq.getEndPos() << endl; }
971 int overlapStart = fSeq.getStartPos();
972 int seq2Start = rSeq.getStartPos();
973 //bigger of the 2 starting positions is the location of the overlapping start
974 if (overlapStart < seq2Start) { //seq2 starts later so take from 0 to seq2Start from seq1
975 overlapStart = seq2Start;
976 for (int i = 0; i < overlapStart; i++) {
978 if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
980 }else { //seq1 starts later so take from 0 to overlapStart from seq2
981 for (int i = 0; i < overlapStart; i++) {
983 if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
987 int seq1End = fSeq.getEndPos();
988 int seq2End = rSeq.getEndPos();
989 int overlapEnd = seq1End;
990 if (seq2End < overlapEnd) { overlapEnd = seq2End; } //smallest end position is where overlapping ends
992 for (int i = overlapStart; i < overlapEnd; i++) {
993 if (seq1[i] == seq2[i]) { //match, add base and choose highest score
995 if (thisfqualfile != "") {
996 contigScores.push_back(scores1[ABaseMap[i]]);
997 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; }
999 }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base
1000 if (thisfqualfile != "") {
1001 if (scores2[BBaseMap[i]] < threshold) { } //
1004 contigScores.push_back(scores2[BBaseMap[i]]);
1006 }else { contig += seq2[i]; } //with no quality info, then we keep it?
1007 }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below threshold. In that case eliminate base
1008 if (thisfqualfile != "") {
1009 if (scores1[ABaseMap[i]] < threshold) { } //
1012 contigScores.push_back(scores1[ABaseMap[i]]);
1014 }else { contig += seq1[i]; } //with no quality info, then we keep it?
1015 }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
1016 if (thisfqualfile != "") {
1018 contigScores.push_back(scores1[ABaseMap[i]]);
1019 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; }
1022 }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch
1023 }else { //should never get here
1024 m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
1028 if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
1029 for (int i = overlapEnd; i < length; i++) {
1031 if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
1033 }else { //seq2 ends before seq1 so take from overlap to length from seq1
1034 for (int i = overlapEnd; i < length; i++) {
1036 if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
1041 if(trashCode.length() == 0){
1042 bool ignore = false;
1044 if (m->debug) { m->mothurOut(fSeq.getName()); }
1047 if(barcodes.size() != 0){
1048 string thisGroup = barcodeNameVector[barcodeIndex];
1049 if (primers.size() != 0) {
1050 if (primerNameVector[primerIndex] != "") {
1051 if(thisGroup != "") {
1052 thisGroup += "." + primerNameVector[primerIndex];
1054 thisGroup = primerNameVector[primerIndex];
1059 if (m->debug) { m->mothurOut(", group= " + thisGroup + "\n"); }
1061 int pos = thisGroup.find("ignore");
1062 if (pos == string::npos) {
1063 groupMap[fSeq.getName()] = thisGroup;
1065 map<string, int>::iterator it = groupCounts.find(thisGroup);
1066 if (it == groupCounts.end()) { groupCounts[thisGroup] = 1; }
1067 else { groupCounts[it->first] ++; }
1068 }else { ignore = true; }
1072 if (m->debug) { m->mothurOut("\n"); }
1074 if(allFiles && !ignore){
1076 m->openOutputFileAppend(fastaFileNames[barcodeIndex][primerIndex], output);
1077 output << ">" << fSeq.getName() << endl << contig << endl;
1080 if (thisfqualfile != "") {
1081 m->openOutputFileAppend(qualFileNames[barcodeIndex][primerIndex], output);
1082 output << ">" << fSeq.getName() << endl;
1083 for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; }
1090 outFasta << ">" << fSeq.getName() << endl << contig << endl;
1091 if (thisfqualfile != "") {
1092 outQual << ">" << fSeq.getName() << endl;
1093 for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; }
1096 outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << numMismatches << endl;
1099 outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl;
1100 if (thisfqualfile != "") {
1101 outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl;
1102 for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; }
1103 outScrapQual << endl;
1109 if((num) % 1000 == 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
1113 if((num) % 1000 != 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
1118 outScrapFasta.close();
1119 outMisMatch.close();
1120 if (thisfqualfile != "") {
1124 outScrapQual.close();
1128 if (m->control_pressed) { m->mothurRemove(outputFasta); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches); if (thisfqualfile != "") { m->mothurRemove(outputQual); m->mothurRemove(outputScrapQual); } }
1132 catch(exception& e) {
1133 m->errorOut(e, "MakeContigsCommand", "driver");
1137 //**********************************************************************************************************************
1138 vector< vector<string> > MakeContigsCommand::readFastqFiles(unsigned long int& count, string ffastq, string rfastq){
1140 vector< vector<string> > files;
1141 //maps processors number to file pointer
1142 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1143 map<int, vector<ofstream*> >::iterator it;
1145 //create files to write to
1146 for (int i = 0; i < processors; i++) {
1147 vector<ofstream*> temp;
1148 ofstream* outFF = new ofstream; temp.push_back(outFF);
1149 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
1150 ofstream* outRF = new ofstream; temp.push_back(outRF);
1151 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
1152 tempfiles[i] = temp;
1154 vector<string> names;
1155 string thisOutputDir = outputDir;
1156 if (outputDir == "") { thisOutputDir = m->hasPath(ffastq); }
1157 string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "ffastatemp";
1158 string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rfastatemp";
1159 string fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "fqualtemp";
1160 string rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rqualtemp";
1161 names.push_back(ffastafilename); names.push_back(fqualfilename);
1162 names.push_back(rfastafilename); names.push_back(rqualfilename);
1163 files.push_back(names);
1165 m->openOutputFile(ffastafilename, *outFF);
1166 m->openOutputFile(rfastafilename, *outRF);
1167 m->openOutputFile(fqualfilename, *outFQ);
1168 m->openOutputFile(rqualfilename, *outRQ);
1171 if (m->control_pressed) {
1172 //close files, delete ofstreams
1173 for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
1175 for (int i = 0; i < files.size(); i++) {
1176 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1181 m->openInputFile(ffastq, inForward);
1184 m->openInputFile(rfastq, inReverse);
1187 map<string, fastqRead> uniques;
1188 map<string, fastqRead>::iterator itUniques;
1189 while ((!inForward.eof()) || (!inReverse.eof())) {
1191 if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inForward.close(); inReverse.close(); return files; }
1193 //get a read from forward and reverse fastq files
1194 bool ignoref, ignorer;
1195 fastqRead thisFread, thisRread;
1196 if (!inForward.eof()) { thisFread = readFastq(inForward, ignoref); }
1197 else { ignoref = true; }
1198 if (!inReverse.eof()) { thisRread = readFastq(inReverse, ignorer); }
1199 else { ignorer = true; }
1201 vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1203 for (int i = 0; i < reads.size(); i++) {
1204 fastqRead fread = reads[i].forward;
1205 fastqRead rread = reads[i].reverse;
1207 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1209 if (checkReads(fread, rread, ffastq, rfastq)) {
1210 if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inForward.close(); inReverse.close(); return files; }
1212 //if the reads are okay write to output files
1213 int process = count % processors;
1215 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1216 *(tempfiles[process][1]) << ">" << fread.name << endl;
1217 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1218 *(tempfiles[process][1]) << endl;
1219 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1220 *(tempfiles[process][3]) << ">" << rread.name << endl;
1221 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1222 *(tempfiles[process][3]) << endl;
1227 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1232 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1234 if (uniques.size() != 0) {
1235 for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1236 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1238 m->mothurOutEndLine();
1241 //close files, delete ofstreams
1242 for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
1248 catch(exception& e) {
1249 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1253 //**********************************************************************************************************************
1254 vector< vector<string> > MakeContigsCommand::readFastaFiles(unsigned long int& count, string ffasta, string rfasta){
1256 vector< vector<string> > files;
1257 //maps processors number to file pointer
1258 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1259 map<int, vector<ofstream*> >::iterator it;
1261 //create files to write to
1262 for (int i = 0; i < processors; i++) {
1263 vector<ofstream*> temp;
1264 ofstream* outFF = new ofstream; temp.push_back(outFF);
1265 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
1266 ofstream* outRF = new ofstream; temp.push_back(outRF);
1267 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
1268 tempfiles[i] = temp;
1270 vector<string> names;
1271 string thisOutputDir = outputDir;
1272 if (outputDir == "") { thisOutputDir = m->hasPath(ffasta); }
1273 string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffasta)) + toString(i) + "ffastatemp";
1274 string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfasta)) + toString(i) + "rfastatemp";
1275 string fqualfilename = "";
1276 if (fqualfile != "") { fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(fqualfile)) + toString(i) + "fqual.temp"; m->openOutputFile(fqualfilename, *outFQ); }
1277 string rqualfilename = "";
1278 if (rqualfile != "") { rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rqualfile)) + toString(i) + "rqual.temp"; m->openOutputFile(rqualfilename, *outRQ); }
1279 names.push_back(ffastafilename); names.push_back(fqualfilename);
1280 names.push_back(rfastafilename); names.push_back(rqualfilename);
1281 files.push_back(names);
1283 m->openOutputFile(ffastafilename, *outFF);
1284 m->openOutputFile(rfastafilename, *outRF);
1287 if (m->control_pressed) {
1288 //close files, delete ofstreams
1289 for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
1291 for (int i = 0; i < files.size(); i++) {
1292 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1296 ifstream inForwardFasta;
1297 m->openInputFile(ffasta, inForwardFasta);
1299 ifstream inReverseFasta;
1300 m->openInputFile(rfasta, inReverseFasta);
1302 ifstream inForwardQual, inReverseQual;
1303 if (fqualfile != "") { m->openInputFile(fqualfile, inForwardQual); m->openInputFile(rqualfile, inReverseQual); }
1306 map<string, fastqRead> uniques;
1307 map<string, fastqRead>::iterator itUniques;
1308 while ((!inForwardFasta.eof()) || (!inReverseFasta.eof())) {
1310 if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; }
1312 //get a reads from forward and reverse fasta files
1313 bool ignoref, ignorer;
1314 fastqRead thisFread, thisRread;
1315 if (!inForwardFasta.eof()) {
1317 Sequence temp(inForwardFasta); m->gobble(inForwardFasta);
1318 thisFread.name = temp.getName();
1319 thisFread.sequence = temp.getUnaligned();
1320 }else { ignoref = true; }
1321 if (!inReverseFasta.eof()) {
1323 Sequence temp(inReverseFasta); m->gobble(inReverseFasta);
1324 thisRread.name = temp.getName();
1325 thisRread.sequence = temp.getUnaligned();
1326 }else { ignorer = true; }
1328 //get qual reads if given
1329 if (fqualfile != "") {
1330 if (!inForwardQual.eof() && !ignoref) {
1331 QualityScores temp(inForwardQual); m->gobble(inForwardQual);
1332 //if forward files dont match ignore read
1333 if (thisFread.name != temp.getName()) { ignoref = true; }
1334 else { thisFread.scores = temp.getQualityScores(); }
1335 }else { ignoref = true; }
1336 if (!inReverseQual.eof() && !ignorer) {
1337 QualityScores temp(inReverseQual); m->gobble(inReverseQual);
1338 //if reverse files dont match ignore read
1339 if (thisRread.name != temp.getName()) { ignorer = true; }
1340 else { thisRread.scores = temp.getQualityScores(); }
1341 }else { ignorer = true; }
1344 vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1346 for (int i = 0; i < reads.size(); i++) {
1347 fastqRead fread = reads[i].forward;
1348 fastqRead rread = reads[i].reverse;
1350 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1352 if (checkReads(fread, rread, ffasta, rfasta)) {
1353 if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; }
1355 //if the reads are okay write to output files
1356 int process = count % processors;
1358 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1359 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1360 if (fqualfile != "") { //if you have quality info, print it
1361 *(tempfiles[process][1]) << ">" << fread.name << endl;
1362 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1363 *(tempfiles[process][1]) << endl;
1364 *(tempfiles[process][3]) << ">" << rread.name << endl;
1365 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1366 *(tempfiles[process][3]) << endl;
1371 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1376 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1378 if (uniques.size() != 0) {
1379 for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1380 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1382 m->mothurOutEndLine();
1385 //close files, delete ofstreams
1386 for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
1387 inReverseFasta.close();
1388 inForwardFasta.close();
1389 if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); }
1393 catch(exception& e) {
1394 m->errorOut(e, "MakeContigsCommand", "readFastaFiles");
1398 //**********************************************************************************************************************
1399 vector<pairFastqRead> MakeContigsCommand::getReads(bool ignoref, bool ignorer, fastqRead forward, fastqRead reverse, map<string, fastqRead>& uniques){
1401 vector<pairFastqRead> reads;
1402 map<string, fastqRead>::iterator itUniques;
1404 if (!ignoref && !ignorer) {
1405 if (forward.name == reverse.name) {
1406 pairFastqRead temp(forward, reverse);
1407 reads.push_back(temp);
1410 //if no match are the names only different by 1 and 2?
1411 string tempFRead = forward.name.substr(0, forward.name.length()-1);
1412 string tempRRead = reverse.name.substr(0, reverse.name.length()-1);
1413 if (tempFRead == tempRRead) {
1414 if ((forward.name[forward.name.length()-1] == '1') && (reverse.name[reverse.name.length()-1] == '2')) {
1415 forward.name = tempFRead;
1416 reverse.name = tempRRead;
1417 pairFastqRead temp(forward, reverse);
1418 reads.push_back(temp);
1424 //look for forward pair
1425 itUniques = uniques.find(forward.name);
1426 if (itUniques != uniques.end()) { //we have the pair for this read
1427 pairFastqRead temp(forward, itUniques->second);
1428 reads.push_back(temp);
1429 uniques.erase(itUniques);
1430 }else { //save this read for later
1431 uniques[forward.name] = forward;
1434 //look for reverse pair
1435 itUniques = uniques.find(reverse.name);
1436 if (itUniques != uniques.end()) { //we have the pair for this read
1437 pairFastqRead temp(itUniques->second, reverse);
1438 reads.push_back(temp);
1439 uniques.erase(itUniques);
1440 }else { //save this read for later
1441 uniques[reverse.name] = reverse;
1446 }else if (!ignoref && ignorer) { //ignore reverse keep forward
1447 //look for forward pair
1448 itUniques = uniques.find(forward.name);
1449 if (itUniques != uniques.end()) { //we have the pair for this read
1450 pairFastqRead temp(forward, itUniques->second);
1451 reads.push_back(temp);
1452 uniques.erase(itUniques);
1453 }else { //save this read for later
1454 uniques[forward.name] = forward;
1457 }else if (ignoref && !ignorer) { //ignore forward keep reverse
1458 //look for reverse pair
1459 itUniques = uniques.find(reverse.name);
1460 if (itUniques != uniques.end()) { //we have the pair for this read
1461 pairFastqRead temp(itUniques->second, reverse);
1462 reads.push_back(temp);
1463 uniques.erase(itUniques);
1464 }else { //save this read for later
1465 uniques[reverse.name] = reverse;
1467 }//else ignore both and do nothing
1471 catch(exception& e) {
1472 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1476 //**********************************************************************************************************************
1477 fastqRead MakeContigsCommand::readFastq(ifstream& in, bool& ignore){
1483 //read sequence name
1484 string line = m->getline(in); m->gobble(in);
1485 vector<string> pieces = m->splitWhiteSpace(line);
1486 string name = ""; if (pieces.size() != 0) { name = pieces[0]; }
1487 if (name == "") { m->mothurOut("[WARNING]: Blank fasta name, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1488 else if (name[0] != '@') { m->mothurOut("[WARNING]: reading " + name + " expected a name with @ as a leading character, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1489 else { name = name.substr(1); }
1492 string sequence = m->getline(in); m->gobble(in);
1493 if (sequence == "") { m->mothurOut("[WARNING]: missing sequence for " + name + ", ignoring."); ignore=true; }
1495 //read sequence name
1496 line = m->getline(in); m->gobble(in);
1497 pieces = m->splitWhiteSpace(line);
1498 string name2 = ""; if (pieces.size() != 0) { name2 = pieces[0]; }
1499 if (name2 == "") { m->mothurOut("[WARNING]: expected a name with + as a leading character, ignoring."); ignore=true; }
1500 else if (name2[0] != '+') { m->mothurOut("[WARNING]: reading " + name2 + " expected a name with + as a leading character, ignoring."); ignore=true; }
1501 else { name2 = name2.substr(1); if (name2 == "") { name2 = name; } }
1503 //read quality scores
1504 string quality = m->getline(in); m->gobble(in);
1505 if (quality == "") { m->mothurOut("[WARNING]: missing quality for " + name2 + ", ignoring."); ignore=true; }
1507 //sanity check sequence length and number of quality scores match
1508 if (name2 != "") { if (name != name2) { m->mothurOut("[WARNING]: names do not match. read " + name + " for fasta and " + name2 + " for quality, ignoring."); ignore=true; } }
1509 if (quality.length() != sequence.length()) { m->mothurOut("[WARNING]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores, ignoring read."); ignore=true; }
1511 vector<int> qualScores;
1512 int controlChar = int('!');
1513 for (int i = 0; i < quality.length(); i++) {
1514 int temp = int(quality[i]);
1515 temp -= controlChar;
1517 qualScores.push_back(temp);
1521 read.sequence = sequence;
1522 read.scores = qualScores;
1526 catch(exception& e) {
1527 m->errorOut(e, "MakeContigsCommand", "readFastq");
1531 //**********************************************************************************************************************
1532 bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse, string ffile, string rfile){
1536 //do sequence lengths match
1537 if (forward.sequence.length() != reverse.sequence.length()) {
1538 m->mothurOut("[WARNING]: For sequence " + forward.name + " I read a sequence of length " + toString(forward.sequence.length()) + " from " + ffile + ", but read a sequence of length " + toString(reverse.sequence.length()) + " from " + rfile + ", ignoring.\n");
1542 //do number of qual scores match
1543 if (forward.scores.size() != reverse.scores.size()) {
1544 m->mothurOut("[WARNING]: For sequence " + forward.name + " I read " + toString(forward.scores.size()) + " quality scores from " + ffile + ", but read " + toString(reverse.scores.size()) + " quality scores from " + rfile + ", ignoring.\n");
1550 catch(exception& e) {
1551 m->errorOut(e, "MakeContigsCommand", "checkReads");
1555 //***************************************************************************************************************
1556 vector< vector<string> > MakeContigsCommand::readFileNames(string filename){
1558 vector< vector<string> > files;
1559 string forward, reverse;
1562 m->openInputFile(filename, in);
1566 if (m->control_pressed) { return files; }
1568 in >> forward; m->gobble(in);
1569 in >> reverse; m->gobble(in);
1571 //check to make sure both are able to be opened
1573 int openForward = m->openInputFile(forward, in2, "noerror");
1575 //if you can't open it, try default location
1576 if (openForward == 1) {
1577 if (m->getDefaultPath() != "") { //default path is set
1578 string tryPath = m->getDefaultPath() + m->getSimpleName(forward);
1579 m->mothurOut("Unable to open " + forward + ". Trying default " + tryPath); m->mothurOutEndLine();
1581 openForward = m->openInputFile(tryPath, in3, "noerror");
1587 //if you can't open it, try output location
1588 if (openForward == 1) {
1589 if (m->getOutputDir() != "") { //default path is set
1590 string tryPath = m->getOutputDir() + m->getSimpleName(forward);
1591 m->mothurOut("Unable to open " + forward + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1593 openForward = m->openInputFile(tryPath, in4, "noerror");
1599 if (openForward == 1) { //can't find it
1600 m->mothurOut("[WARNING]: can't find " + forward + ", ignoring pair.\n");
1601 }else{ in2.close(); }
1604 int openReverse = m->openInputFile(reverse, in3, "noerror");
1606 //if you can't open it, try default location
1607 if (openReverse == 1) {
1608 if (m->getDefaultPath() != "") { //default path is set
1609 string tryPath = m->getDefaultPath() + m->getSimpleName(reverse);
1610 m->mothurOut("Unable to open " + reverse + ". Trying default " + tryPath); m->mothurOutEndLine();
1612 openReverse = m->openInputFile(tryPath, in3, "noerror");
1618 //if you can't open it, try output location
1619 if (openReverse == 1) {
1620 if (m->getOutputDir() != "") { //default path is set
1621 string tryPath = m->getOutputDir() + m->getSimpleName(reverse);
1622 m->mothurOut("Unable to open " + reverse + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1624 openReverse = m->openInputFile(tryPath, in4, "noerror");
1630 if (openReverse == 1) { //can't find it
1631 m->mothurOut("[WARNING]: can't find " + reverse + ", ignoring pair.\n");
1632 }else{ in3.close(); }
1634 if ((openForward != 1) && (openReverse != 1)) { //good pair
1635 vector<string> pair;
1636 pair.push_back(forward);
1637 pair.push_back(reverse);
1638 files.push_back(pair);
1646 catch(exception& e) {
1647 m->errorOut(e, "MakeContigsCommand", "checkReads");
1651 //***************************************************************************************************************
1652 //illumina data requires paired forward and reverse data
1653 //BARCODE atgcatgc atgcatgc groupName
1654 //PRIMER atgcatgc atgcatgc groupName
1655 //PRIMER atgcatgc atgcatgc
1656 bool MakeContigsCommand::getOligos(vector<vector<string> >& fastaFileNames, vector<vector<string> >& qualFileNames, string rootname){
1659 m->openInputFile(oligosfile, in);
1663 string type, foligo, roligo, group;
1665 int indexPrimer = 0;
1666 int indexBarcode = 0;
1667 set<string> uniquePrimers;
1668 set<string> uniqueBarcodes;
1673 cout << type << endl;
1674 if (m->debug) { m->mothurOut("[DEBUG]: reading type - " + type + ".\n"); }
1677 while (!in.eof()) { char c = in.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
1682 //make type case insensitive
1683 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); }
1687 if (m->debug) { m->mothurOut("[DEBUG]: reading - " + foligo + ".\n"); }
1689 for(int i=0;i<foligo.length();i++){
1690 foligo[i] = toupper(foligo[i]);
1691 if(foligo[i] == 'U') { foligo[i] = 'T'; }
1694 if(type == "FORWARD"){
1699 for(int i=0;i<roligo.length();i++){
1700 roligo[i] = toupper(roligo[i]);
1701 if(roligo[i] == 'U') { roligo[i] = 'T'; }
1703 //roligo = reverseOligo(roligo);
1707 // get rest of line in case there is a primer name
1710 if (c == 10 || c == 13){ break; }
1711 else if (c == 32 || c == 9){;} //space or tab
1712 else { group += c; }
1715 oligosPair newPrimer(foligo, roligo);
1717 //check for repeat barcodes
1718 string tempPair = foligo+roligo;
1719 if (uniquePrimers.count(tempPair) != 0) { m->mothurOut("primer pair " + newPrimer.forward + " " + newPrimer.reverse + " is in your oligos file already."); m->mothurOutEndLine(); }
1720 else { uniquePrimers.insert(tempPair); }
1722 if (m->debug) { if (group != "") { m->mothurOut("[DEBUG]: reading group " + group + ".\n"); }else{ m->mothurOut("[DEBUG]: no group for primer pair " + newPrimer.forward + " " + newPrimer.reverse + ".\n"); } }
1724 primers[indexPrimer]=newPrimer; indexPrimer++;
1725 primerNameVector.push_back(group);
1726 }else if(type == "BARCODE"){
1731 for(int i=0;i<roligo.length();i++){
1732 roligo[i] = toupper(roligo[i]);
1733 if(roligo[i] == 'U') { roligo[i] = 'T'; }
1735 //roligo = reverseOligo(roligo);
1737 oligosPair newPair(foligo, roligo);
1742 if (c == 10 || c == 13){ break; }
1743 else if (c == 32 || c == 9){;} //space or tab
1744 else { group += c; }
1747 if (m->debug) { m->mothurOut("[DEBUG]: barcode pair " + newPair.forward + " " + newPair.reverse + ", and group = " + group + ".\n"); }
1749 //check for repeat barcodes
1750 string tempPair = foligo+roligo;
1751 if (uniqueBarcodes.count(tempPair) != 0) { m->mothurOut("barcode pair " + newPair.forward + " " + newPair.reverse + " is in your oligos file already, disregarding."); m->mothurOutEndLine(); }
1752 else { uniqueBarcodes.insert(tempPair); }
1754 barcodes[indexBarcode]=newPair; indexBarcode++;
1755 barcodeNameVector.push_back(group);
1756 cout << group << endl;
1757 }else if(type == "LINKER"){
1758 linker.push_back(foligo);
1759 m->mothurOut("[WARNING]: make.contigs is not setup to remove linkers, ignoring.\n");
1760 }else if(type == "SPACER"){
1761 spacer.push_back(foligo);
1762 m->mothurOut("[WARNING]: make.contigs is not setup to remove spacers, ignoring.\n");
1764 else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are primer, barcode, linker and spacer. Ignoring " + foligo + "."); m->mothurOutEndLine(); }
1770 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0; }
1772 //add in potential combos
1773 if(barcodeNameVector.size() == 0){
1774 oligosPair temp("", "");
1776 barcodeNameVector.push_back("");
1779 if(primerNameVector.size() == 0){
1780 oligosPair temp("", "");
1782 primerNameVector.push_back("");
1785 fastaFileNames.resize(barcodeNameVector.size());
1786 for(int i=0;i<fastaFileNames.size();i++){
1787 fastaFileNames[i].assign(primerNameVector.size(), "");
1789 qualFileNames = fastaFileNames;
1792 set<string> uniqueNames; //used to cleanup outputFileNames
1793 for(map<int, oligosPair>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1794 for(map<int, oligosPair>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1796 string primerName = primerNameVector[itPrimer->first];
1797 string barcodeName = barcodeNameVector[itBar->first];
1799 if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing
1801 string comboGroupName = "";
1802 string fastaFileName = "";
1803 string qualFileName = "";
1804 string nameFileName = "";
1805 string countFileName = "";
1807 if(primerName == ""){
1808 comboGroupName = barcodeNameVector[itBar->first];
1811 if(barcodeName == ""){
1812 comboGroupName = primerNameVector[itPrimer->first];
1815 comboGroupName = barcodeNameVector[itBar->first] + "." + primerNameVector[itPrimer->first];
1821 fastaFileName = rootname + comboGroupName + ".fasta";
1822 if (uniqueNames.count(fastaFileName) == 0) {
1823 outputNames.push_back(fastaFileName);
1824 outputTypes["fasta"].push_back(fastaFileName);
1825 uniqueNames.insert(fastaFileName);
1828 fastaFileNames[itBar->first][itPrimer->first] = fastaFileName;
1829 m->openOutputFile(fastaFileName, temp); temp.close();
1831 if ((fqualfile != "") || (ffastqfile != "") || (file != "")) {
1832 qualFileName = rootname + ".qual";
1833 if (uniqueNames.count(qualFileName) == 0) {
1834 outputNames.push_back(qualFileName);
1835 outputTypes["qfile"].push_back(qualFileName);
1838 qualFileNames[itBar->first][itPrimer->first] = qualFileName;
1839 m->openOutputFile(qualFileName, temp); temp.close();
1846 bool allBlank = true;
1847 for (int i = 0; i < barcodeNameVector.size(); i++) {
1848 if (barcodeNameVector[i] != "") {
1853 for (int i = 0; i < primerNameVector.size(); i++) {
1854 if (primerNameVector[i] != "") {
1861 m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a groupfile."); m->mothurOutEndLine();
1869 catch(exception& e) {
1870 m->errorOut(e, "MakeContigsCommand", "getOligos");
1874 //********************************************************************/
1875 string MakeContigsCommand::reverseOligo(string oligo){
1877 string reverse = "";
1879 for(int i=oligo.length()-1;i>=0;i--){
1881 if(oligo[i] == 'A') { reverse += 'T'; }
1882 else if(oligo[i] == 'T'){ reverse += 'A'; }
1883 else if(oligo[i] == 'U'){ reverse += 'A'; }
1885 else if(oligo[i] == 'G'){ reverse += 'C'; }
1886 else if(oligo[i] == 'C'){ reverse += 'G'; }
1888 else if(oligo[i] == 'R'){ reverse += 'Y'; }
1889 else if(oligo[i] == 'Y'){ reverse += 'R'; }
1891 else if(oligo[i] == 'M'){ reverse += 'K'; }
1892 else if(oligo[i] == 'K'){ reverse += 'M'; }
1894 else if(oligo[i] == 'W'){ reverse += 'W'; }
1895 else if(oligo[i] == 'S'){ reverse += 'S'; }
1897 else if(oligo[i] == 'B'){ reverse += 'V'; }
1898 else if(oligo[i] == 'V'){ reverse += 'B'; }
1900 else if(oligo[i] == 'D'){ reverse += 'H'; }
1901 else if(oligo[i] == 'H'){ reverse += 'D'; }
1903 else { reverse += 'N'; }
1909 catch(exception& e) {
1910 m->errorOut(e, "MakeContigsCommand", "reverseOligo");
1914 //**********************************************************************************************************************