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"
10 #include "counttable.h"
12 //**********************************************************************************************************************
13 vector<string> MakeContigsCommand::setParameters(){
15 CommandParameter pfastq("ffastq", "InputTypes", "", "", "FastaFastqFile", "FastaFastqFile", "fastqGroup","fasta-qfile",false,false,true); parameters.push_back(pfastq);
16 CommandParameter prfastq("rfastq", "InputTypes", "", "", "none", "none", "fastqGroup","fasta-qfile",false,false,true); parameters.push_back(prfastq);
17 CommandParameter pfasta("ffasta", "InputTypes", "", "", "FastaFastqFile", "FastaFastqFile", "fastaGroup","fasta",false,false,true); parameters.push_back(pfasta);
18 CommandParameter prfasta("rfasta", "InputTypes", "", "", "none", "none", "none","fastaGroup",false,false,true); parameters.push_back(prfasta);
19 CommandParameter pfqual("fqfile", "InputTypes", "", "", "none", "none", "qfileGroup","",false,false,true); parameters.push_back(pfqual);
20 CommandParameter prqual("rqfile", "InputTypes", "", "", "none", "none", "qfileGroup","",false,false,true); parameters.push_back(prqual);
21 CommandParameter pfile("file", "InputTypes", "", "", "FastaFastqFile", "FastaFastqFile", "none","fasta-qfile",false,false,true); parameters.push_back(pfile);
22 CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none","group",false,false,true); parameters.push_back(poligos);
23 CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "","",false,false,true); parameters.push_back(ppdiffs);
24 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "","",false,false,true); parameters.push_back(pbdiffs);
25 CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ptdiffs);
27 CommandParameter palign("align", "Multiple", "needleman-gotoh", "needleman", "", "", "","",false,false); parameters.push_back(palign);
28 CommandParameter pallfiles("allfiles", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pallfiles);
29 CommandParameter pmakecount("makecount", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pmakecount);
30 CommandParameter ptrimoverlap("trimoverlap", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(ptrimoverlap);
31 CommandParameter pmatch("match", "Number", "", "1.0", "", "", "","",false,false); parameters.push_back(pmatch);
32 CommandParameter pmismatch("mismatch", "Number", "", "-1.0", "", "", "","",false,false); parameters.push_back(pmismatch);
33 CommandParameter pgapopen("gapopen", "Number", "", "-2.0", "", "", "","",false,false); parameters.push_back(pgapopen);
34 CommandParameter pgapextend("gapextend", "Number", "", "-1.0", "", "", "","",false,false); parameters.push_back(pgapextend);
35 CommandParameter pthreshold("insert", "Number", "", "20", "", "", "","",false,false); parameters.push_back(pthreshold);
36 CommandParameter pdeltaq("deltaq", "Number", "", "6", "", "", "","",false,false); parameters.push_back(pdeltaq);
37 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
38 CommandParameter pformat("format", "Multiple", "sanger-illumina-solexa-illumina1.8+", "illumina1.8+", "", "", "","",false,false,true); parameters.push_back(pformat);
39 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
40 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
42 vector<string> myArray;
43 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
47 m->errorOut(e, "MakeContigsCommand", "setParameters");
51 //**********************************************************************************************************************
52 string MakeContigsCommand::getHelpString(){
54 string helpString = "";
55 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";
56 helpString += "If an oligos file is provided barcodes and primers will be trimmed, and a group file will be created.\n";
57 helpString += "The make.contigs command parameters are file, ffastq, rfastq, ffasta, rfasta, fqfile, rqfile, oligos, format, tdiffs, bdiffs, pdiffs, align, match, mismatch, gapopen, gapextend, insert, deltaq, allfiles, makecount and processors.\n";
58 helpString += "The ffastq and rfastq, file, or ffasta and rfasta parameters are required.\n";
59 helpString += "The file parameter is 2 or 3 column file containing the forward fastq files in the first column and their matching reverse fastq files in the second column, or a groupName then forward fastq file and reverse fastq file. Mothur will process each pair and create a combined fasta and report file with all the sequences.\n";
60 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";
61 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";
62 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";
63 helpString += "The format parameter is used to indicate whether your sequences are sanger, solexa, illumina1.8+ or illumina, default=illumina1.8+.\n";
64 helpString += "The align parameter allows you to specify the alignment method to use. Your options are: gotoh and needleman. The default is needleman.\n";
65 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";
66 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
67 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
68 //helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
69 //helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
70 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
71 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n";
72 helpString += "The deltaq parameter allows you to specify the delta allowed between quality scores of a mismatched base. For example in the overlap, if deltaq=5 and in the alignment seqA, pos 200 has a quality score of 30 and the same position in seqB has a quality score of 20, you take the base from seqA (30-20 >= 5). If the quality score in seqB is 28 then the base in the consensus will be an N (30-28<5) The default is 6.\n";
73 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
74 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n";
75 helpString += "The insert 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 equal to or below the threshold we eliminate it. Default=20.\n";
76 helpString += "The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n";
77 helpString += "The allfiles parameter will create separate group and fasta file for each grouping. The default is F.\n";
78 helpString += "The makecount parameter will create a count table file instead of a group file. The default is F.\n";
80 helpString += "The trimoverlap parameter allows you to trim the sequences to only the overlapping section. The default is F.\n";
81 helpString += "The make.contigs command should be in the following format: \n";
82 helpString += "make.contigs(ffastq=yourForwardFastqFile, rfastq=yourReverseFastqFile, align=yourAlignmentMethod) \n";
83 helpString += "Note: No spaces between parameter labels (i.e. ffastq), '=' and parameters (i.e.yourForwardFastqFile).\n";
87 m->errorOut(e, "MakeContigsCommand", "getHelpString");
91 //**********************************************************************************************************************
92 string MakeContigsCommand::getOutputPattern(string type) {
96 if (type == "fasta") { pattern = "[filename],[tag],contigs.fasta"; }
97 else if (type == "group") { pattern = "[filename],[tag],contigs.groups"; }
98 else if (type == "count") { pattern = "[filename],[tag],contigs.count_table"; }
99 else if (type == "report") { pattern = "[filename],[tag],contigs.report"; }
100 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
104 catch(exception& e) {
105 m->errorOut(e, "MakeContigsCommand", "getOutputPattern");
109 //**********************************************************************************************************************
110 MakeContigsCommand::MakeContigsCommand(){
112 abort = true; calledHelp = true;
114 vector<string> tempOutNames;
115 outputTypes["fasta"] = tempOutNames;
116 outputTypes["group"] = tempOutNames;
117 outputTypes["report"] = tempOutNames;
118 outputTypes["count"] = tempOutNames;
120 catch(exception& e) {
121 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
125 //**********************************************************************************************************************
126 MakeContigsCommand::MakeContigsCommand(string option) {
128 abort = false; calledHelp = false;
129 createFileGroup = false; createOligosGroup = false;
131 //allow user to run help
132 if(option == "help") { help(); abort = true; calledHelp = true; }
133 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
136 vector<string> myArray = setParameters();
138 OptionParser parser(option);
139 map<string, string> parameters = parser.getParameters();
141 ValidParameters validParameter("pairwise.seqs");
142 map<string, string>::iterator it;
144 //check to make sure all parameters are valid for command
145 for (it = parameters.begin(); it != parameters.end(); it++) {
146 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
149 //initialize outputTypes
150 vector<string> tempOutNames;
151 outputTypes["fasta"] = tempOutNames;
152 outputTypes["report"] = tempOutNames;
153 outputTypes["group"] = tempOutNames;
154 outputTypes["count"] = tempOutNames;
157 //if the user changes the input directory command factory will send this info to us in the output parameter
158 string inputDir = validParameter.validFile(parameters, "inputdir", false);
159 if (inputDir == "not found"){ inputDir = ""; }
162 it = parameters.find("ffastq");
163 //user has given a template file
164 if(it != parameters.end()){
165 path = m->hasPath(it->second);
166 //if the user has not given a path then, add inputdir. else leave path alone.
167 if (path == "") { parameters["ffastq"] = inputDir + it->second; }
170 it = parameters.find("rfastq");
171 //user has given a template file
172 if(it != parameters.end()){
173 path = m->hasPath(it->second);
174 //if the user has not given a path then, add inputdir. else leave path alone.
175 if (path == "") { parameters["rfastq"] = inputDir + it->second; }
178 it = parameters.find("ffasta");
179 //user has given a template file
180 if(it != parameters.end()){
181 path = m->hasPath(it->second);
182 //if the user has not given a path then, add inputdir. else leave path alone.
183 if (path == "") { parameters["ffasta"] = inputDir + it->second; }
186 it = parameters.find("rfasta");
187 //user has given a template file
188 if(it != parameters.end()){
189 path = m->hasPath(it->second);
190 //if the user has not given a path then, add inputdir. else leave path alone.
191 if (path == "") { parameters["rfasta"] = inputDir + it->second; }
194 it = parameters.find("fqfile");
195 //user has given a template file
196 if(it != parameters.end()){
197 path = m->hasPath(it->second);
198 //if the user has not given a path then, add inputdir. else leave path alone.
199 if (path == "") { parameters["fqfile"] = inputDir + it->second; }
202 it = parameters.find("rqfile");
203 //user has given a template file
204 if(it != parameters.end()){
205 path = m->hasPath(it->second);
206 //if the user has not given a path then, add inputdir. else leave path alone.
207 if (path == "") { parameters["rqfile"] = inputDir + it->second; }
210 it = parameters.find("file");
211 //user has given a template file
212 if(it != parameters.end()){
213 path = m->hasPath(it->second);
214 //if the user has not given a path then, add inputdir. else leave path alone.
215 if (path == "") { parameters["file"] = inputDir + it->second; }
218 it = parameters.find("oligos");
219 //user has given a template file
220 if(it != parameters.end()){
221 path = m->hasPath(it->second);
222 //if the user has not given a path then, add inputdir. else leave path alone.
223 if (path == "") { parameters["oligos"] = inputDir + it->second; }
227 ffastqfile = validParameter.validFile(parameters, "ffastq", true);
228 if (ffastqfile == "not open") { abort = true; }
229 else if (ffastqfile == "not found") { ffastqfile = ""; }
231 rfastqfile = validParameter.validFile(parameters, "rfastq", true);
232 if (rfastqfile == "not open") { abort = true; }
233 else if (rfastqfile == "not found") { rfastqfile = ""; }
235 ffastafile = validParameter.validFile(parameters, "ffasta", true);
236 if (ffastafile == "not open") { abort = true; }
237 else if (ffastafile == "not found") { ffastafile = ""; }
239 rfastafile = validParameter.validFile(parameters, "rfasta", true);
240 if (rfastafile == "not open") { abort = true; }
241 else if (rfastafile == "not found") { rfastafile = ""; }
243 fqualfile = validParameter.validFile(parameters, "fqfile", true);
244 if (fqualfile == "not open") { abort = true; }
245 else if (fqualfile == "not found") { fqualfile = ""; }
247 rqualfile = validParameter.validFile(parameters, "rqfile", true);
248 if (rqualfile == "not open") { abort = true; }
249 else if (rqualfile == "not found") { rqualfile = ""; }
251 file = validParameter.validFile(parameters, "file", true);
252 if (file == "not open") { abort = true; }
253 else if (file == "not found") { file = ""; }
256 if ((file == "") && (ffastafile == "") && (ffastqfile == "")) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
257 if ((file != "") && ((ffastafile != "") || (ffastqfile != ""))) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
258 if ((ffastqfile != "") && (rfastqfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffastq, you must provide a rfastq file.\n"); }
259 if ((ffastqfile == "") && (rfastqfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfastq, you must provide a ffastq file.\n"); }
260 if ((ffastafile != "") && (rfastafile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffasta, you must provide a rfasta file.\n"); }
261 if ((ffastafile == "") && (rfastafile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfasta, you must provide a ffasta file.\n"); }
262 if ((fqualfile != "") && (rqualfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the fqfile, you must provide a rqfile file.\n"); }
263 if ((fqualfile == "") && (rqualfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile, you must provide a fqfile file.\n"); }
264 if (((fqualfile != "") || (rqualfile != "")) && ((ffastafile == "") || (rfastafile == ""))) {
265 abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile or fqfile file, you must provide the ffasta and rfasta parameters.\n");
268 oligosfile = validParameter.validFile(parameters, "oligos", true);
269 if (oligosfile == "not found") { oligosfile = ""; }
270 else if(oligosfile == "not open") { abort = true; }
271 else { m->setOligosFile(oligosfile); }
273 //if the user changes the output directory command factory will send this info to us in the output parameter
274 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
279 //check for optional parameter and set defaults
280 // ...at some point should added some additional type checking...
282 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
283 m->mothurConvert(temp, match);
285 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
286 m->mothurConvert(temp, misMatch);
287 if (misMatch > 0) { m->mothurOut("[ERROR]: mismatch must be negative.\n"); abort=true; }
289 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
290 m->mothurConvert(temp, gapOpen);
291 if (gapOpen > 0) { m->mothurOut("[ERROR]: gapopen must be negative.\n"); abort=true; }
293 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
294 m->mothurConvert(temp, gapExtend);
295 if (gapExtend > 0) { m->mothurOut("[ERROR]: gapextend must be negative.\n"); abort=true; }
297 temp = validParameter.validFile(parameters, "insert", false); if (temp == "not found"){ temp = "20"; }
298 m->mothurConvert(temp, insert);
299 if ((insert < 0) || (insert > 40)) { m->mothurOut("[ERROR]: insert must be between 0 and 40.\n"); abort=true; }
301 temp = validParameter.validFile(parameters, "deltaq", false); if (temp == "not found"){ temp = "6"; }
302 m->mothurConvert(temp, deltaq);
304 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
305 m->setProcessors(temp);
306 m->mothurConvert(temp, processors);
308 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found") { temp = "0"; }
309 m->mothurConvert(temp, bdiffs);
311 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found") { temp = "0"; }
312 m->mothurConvert(temp, pdiffs);
314 // temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
315 // m->mothurConvert(temp, ldiffs);
318 // temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
319 // m->mothurConvert(temp, sdiffs);
322 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
323 m->mothurConvert(temp, tdiffs);
325 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; } //+ ldiffs + sdiffs;
327 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
328 allFiles = m->isTrue(temp);
330 temp = validParameter.validFile(parameters, "makecount", false); if (temp == "not found") { temp = "F"; }
331 makeCount = m->isTrue(temp);
333 temp = validParameter.validFile(parameters, "trimoverlap", false); if (temp == "not found") { temp = "F"; }
334 trimOverlap = m->isTrue(temp);
336 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
337 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"; }
339 format = validParameter.validFile(parameters, "format", false); if (format == "not found"){ format = "illumina1.8+"; }
341 if ((format != "sanger") && (format != "illumina") && (format != "illumina1.8+") && (format != "solexa")) {
342 m->mothurOut(format + " is not a valid format. Your format choices are sanger, solexa, illumina1.8+ and illumina, aborting." ); m->mothurOutEndLine();
346 //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
347 for (int i = -64; i < 65; i++) {
348 char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
349 convertTable.push_back(temp);
354 catch(exception& e) {
355 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
359 //**********************************************************************************************************************
360 int MakeContigsCommand::execute(){
362 if (abort == true) { if (calledHelp) { return 0; } return 2; }
364 //read ffastq and rfastq files creating fasta and qual files.
365 //this function will create a forward and reverse, fasta and qual files for each processor.
366 //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.
367 unsigned long int numReads = 0;
368 int start = time(NULL);
370 m->mothurOut("Reading fastq data...\n");
371 vector < vector< vector<string> > > filesToProcess = preProcessData(numReads);
372 m->mothurOut("Done.\n");
374 if (m->control_pressed) { return 0; }
376 map<string, string> cvars;
377 string compOutputDir = outputDir;
378 if (outputDir == "") { compOutputDir = m->hasPath(file); }
379 cvars["[filename]"] = compOutputDir + m->getRootName(m->getSimpleName(file));
381 string compositeGroupFile = getOutputFileName("group",cvars);
382 if (makeCount) { compositeGroupFile = getOutputFileName("count",cvars); }
383 cvars["[tag]"] = "trim";
384 string compositeFastaFile = getOutputFileName("fasta",cvars);
385 cvars["[tag]"] = "scrap";
386 string compositeScrapFastaFile = getOutputFileName("fasta",cvars);
388 string compositeMisMatchFile = getOutputFileName("report",cvars);
390 if (filesToProcess.size() > 1) { //clear files for append below
391 ofstream outCTFasta, outCTQual, outCSFasta, outCSQual, outCMisMatch;
392 m->openOutputFile(compositeFastaFile, outCTFasta); outCTFasta.close();
393 m->openOutputFile(compositeScrapFastaFile, outCSFasta); outCSFasta.close();
394 m->openOutputFile(compositeMisMatchFile, outCMisMatch); outCMisMatch.close();
395 outputNames.push_back(compositeFastaFile); outputTypes["fasta"].push_back(compositeFastaFile);
396 outputNames.push_back(compositeMisMatchFile); outputTypes["report"].push_back(compositeMisMatchFile);
397 outputNames.push_back(compositeScrapFastaFile); outputTypes["fasta"].push_back(compositeScrapFastaFile);
400 map<string, int> totalGroupCounts;
401 CountTable compositeCt;
402 for (int l = 0; l < filesToProcess.size(); l++) {
404 m->mothurOut("\n>>>>>\tProcessing " + filesToProcess[l][0][0] + " (file " + toString(l+1) + " of " + toString(filesToProcess.size()) + ")\t<<<<<\n");
408 vector<vector<string> > fastaFileNames;
409 createOligosGroup = false;
410 string outputGroupFileName;
411 map<string, string> variables;
412 string thisOutputDir = outputDir;
413 if (outputDir == "") { thisOutputDir = m->hasPath(filesToProcess[l][0][0]); }
414 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(filesToProcess[l][0][0]));
415 variables["[tag]"] = "";
416 if(oligosfile != ""){ createOligosGroup = getOligos(fastaFileNames, variables["[filename]"]); }
417 if (createOligosGroup || createFileGroup) {
418 outputGroupFileName = getOutputFileName("group",variables);
419 if (makeCount) { outputGroupFileName = getOutputFileName("count",variables); }
422 //give group in file file precedence
423 if (createFileGroup) { createOligosGroup = false; }
425 variables["[tag]"] = "trim";
426 string outFastaFile = getOutputFileName("fasta",variables);
427 variables["[tag]"] = "scrap";
428 string outScrapFastaFile = getOutputFileName("fasta",variables);
429 variables["[tag]"] = "";
430 string outMisMatchFile = getOutputFileName("report",variables);
432 m->mothurOut("Making contigs...\n");
433 createProcesses(filesToProcess[l], outFastaFile, outScrapFastaFile, outMisMatchFile, fastaFileNames, l);
434 m->mothurOut("Done.\n");
436 //remove temp fasta and qual files
437 for (int i = 0; i < processors; i++) { for(int j = 0; j < filesToProcess[l][i].size(); j++) { m->mothurRemove(filesToProcess[l][i][j]); } }
439 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
442 map<string, string> uniqueFastaNames;// so we don't add the same groupfile multiple times
443 map<string, string>::iterator it;
444 set<string> namesToRemove;
445 for(int i=0;i<fastaFileNames.size();i++){
446 for(int j=0;j<fastaFileNames[0].size();j++){
447 if (fastaFileNames[i][j] != "") {
448 if (namesToRemove.count(fastaFileNames[i][j]) == 0) {
449 if(m->isBlank(fastaFileNames[i][j])){
450 m->mothurRemove(fastaFileNames[i][j]);
451 namesToRemove.insert(fastaFileNames[i][j]);
453 it = uniqueFastaNames.find(fastaFileNames[i][j]);
454 if (it == uniqueFastaNames.end()) {
455 uniqueFastaNames[fastaFileNames[i][j]] = barcodeNameVector[i];
463 //remove names for outputFileNames, just cleans up the output
464 vector<string> outputNames2;
465 for(int i = 0; i < outputNames.size(); i++) { if (namesToRemove.count(outputNames[i]) == 0) { outputNames2.push_back(outputNames[i]); } }
466 outputNames = outputNames2;
468 for (it = uniqueFastaNames.begin(); it != uniqueFastaNames.end(); it++) {
470 m->openInputFile(it->first, in);
473 string thisGroupName = thisOutputDir + m->getRootName(m->getSimpleName(it->first));
474 if (!makeCount) { thisGroupName += getOutputFileName("group",variables); outputNames.push_back(thisGroupName); outputTypes["group"].push_back(thisGroupName);
475 m->openOutputFile(thisGroupName, out);
478 if (m->control_pressed) { break; }
480 Sequence currSeq(in); m->gobble(in);
481 out << currSeq.getName() << '\t' << it->second << endl;
486 thisGroupName += getOutputFileName("count",variables); outputNames.push_back(thisGroupName); outputTypes["count"].push_back(thisGroupName);
488 ct.addGroup(it->second);
490 if (m->control_pressed) { break; }
492 Sequence currSeq(in); m->gobble(in);
493 vector<int> tempGroupCount; tempGroupCount.push_back(1);
494 ct.push_back(currSeq.getName(), tempGroupCount);
496 ct.printTable(thisGroupName);
502 if (createFileGroup || createOligosGroup) {
504 if ((allFiles) || (filesToProcess.size() == 1)) {
506 for (map<string, int>::iterator itGroups = groupCounts.begin(); itGroups != groupCounts.end(); itGroups++) {
507 ct.addGroup(itGroups->first);
509 vector<string> groups = ct.getNamesOfGroups();
510 for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
511 vector<int> tempGroupCounts; tempGroupCounts.resize(groups.size(), 0);
512 ct.push_back(itGroup->first, tempGroupCounts);
513 ct.setAbund(itGroup->first, itGroup->second, 1);
515 ct.printTable(outputGroupFileName);
519 m->openOutputFile(outputGroupFileName, outGroup);
520 for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
521 outGroup << itGroup->first << '\t' << itGroup->second << endl;
527 if (filesToProcess.size() > 1) { //merge into large combo files
528 if (createFileGroup || createOligosGroup) {
530 for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
531 vector<string> groups = compositeCt.getNamesOfGroups();
532 if (m->inUsersGroups(itGroup->second, groups)) {
533 vector<int> tempGroupCounts; tempGroupCounts.resize(groups.size(), 0);
534 compositeCt.push_back(itGroup->first, tempGroupCounts);
535 compositeCt.setAbund(itGroup->first, itGroup->second, 1);
537 compositeCt.addGroup(itGroup->second);
538 vector<int> tempGroupCounts; tempGroupCounts.resize(groups.size()+1, 0);
539 compositeCt.push_back(itGroup->first, tempGroupCounts);
540 compositeCt.setAbund(itGroup->first, itGroup->second, 1);
546 m->openOutputFile(compositeGroupFile, outCGroup); outCGroup.close();
547 outputNames.push_back(compositeGroupFile); outputTypes["group"].push_back(compositeGroupFile);
549 m->appendFiles(outputGroupFileName, compositeGroupFile);
550 if (!allFiles) { m->mothurRemove(outputGroupFileName); }
551 else { outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName); }
553 for (map<string, int>::iterator itGroups = groupCounts.begin(); itGroups != groupCounts.end(); itGroups++) {
554 map<string, int>::iterator itTemp = totalGroupCounts.find(itGroups->first);
555 if (itTemp == totalGroupCounts.end()) { totalGroupCounts[itGroups->first] = itGroups->second; } //new group create it in totalGroups
556 else { itTemp->second += itGroups->second; } //existing group, update total
559 if (l == 0) { m->appendFiles(outMisMatchFile, compositeMisMatchFile); }
560 else { m->appendFilesWithoutHeaders(outMisMatchFile, compositeMisMatchFile); }
561 m->appendFiles(outFastaFile, compositeFastaFile);
562 m->appendFiles(outScrapFastaFile, compositeScrapFastaFile);
564 m->mothurRemove(outMisMatchFile);
565 m->mothurRemove(outFastaFile);
566 m->mothurRemove(outScrapFastaFile);
568 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
569 outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile);
570 outputNames.push_back(outMisMatchFile); outputTypes["report"].push_back(outMisMatchFile);
573 totalGroupCounts = groupCounts;
574 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
575 outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile);
576 outputNames.push_back(outMisMatchFile); outputTypes["report"].push_back(outMisMatchFile);
577 if (createFileGroup || createOligosGroup) {
578 if (makeCount) { outputNames.push_back(outputGroupFileName); outputTypes["count"].push_back(outputGroupFileName); }
579 else { outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName); }
584 if ((filesToProcess.size() > 1) && makeCount) { //merge into large combo files
585 compositeCt.printTable(compositeGroupFile);
586 outputNames.push_back(compositeGroupFile); outputTypes["count"].push_back(compositeGroupFile);
588 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process " + toString(numReads) + " sequences.\n");
590 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
592 //output group counts
593 m->mothurOutEndLine();
595 if (totalGroupCounts.size() != 0) { m->mothurOut("Group count: \n"); }
596 for (map<string, int>::iterator it = totalGroupCounts.begin(); it != totalGroupCounts.end(); it++) {
597 total += it->second; m->mothurOut(it->first + "\t" + toString(it->second)); m->mothurOutEndLine();
599 if (total != 0) { m->mothurOut("Total of all groups is " + toString(total)); m->mothurOutEndLine(); }
601 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
603 string currentFasta = "";
604 itTypes = outputTypes.find("fasta");
605 if (itTypes != outputTypes.end()) {
606 if ((itTypes->second).size() != 0) { currentFasta = (itTypes->second)[0]; m->setFastaFile(currentFasta); }
609 string currentGroup = "";
610 itTypes = outputTypes.find("group");
611 if (itTypes != outputTypes.end()) {
612 if ((itTypes->second).size() != 0) { currentGroup = (itTypes->second)[0]; m->setGroupFile(currentGroup); }
615 string currentCount = "";
616 itTypes = outputTypes.find("count");
617 if (itTypes != outputTypes.end()) {
618 if ((itTypes->second).size() != 0) { currentCount = (itTypes->second)[0]; m->setCountTableFile(currentCount); }
621 //output files created by command
622 m->mothurOutEndLine();
623 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
624 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
625 m->mothurOutEndLine();
629 catch(exception& e) {
630 m->errorOut(e, "MakeContigsCommand", "execute");
634 //**********************************************************************************************************************
635 vector< vector< vector<string> > > MakeContigsCommand::preProcessData(unsigned long int& numReads) {
637 vector< vector< vector<string> > > filesToProcess;
639 if (ffastqfile != "") { //reading one file
640 vector< vector<string> > files = readFastqFiles(numReads, ffastqfile, rfastqfile);
641 //adjust for really large processors or really small files
642 if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
643 if (numReads < processors) {
644 for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
645 files.resize(numReads);
646 processors = numReads;
648 filesToProcess.push_back(files);
649 }else if (file != "") { //reading multiple files
650 //return only valid pairs
651 vector< vector<string> > filePairsToProcess = readFileNames(file);
653 if (m->control_pressed) { return filesToProcess; }
655 if (filePairsToProcess.size() != 0) {
656 for (int i = 0; i < filePairsToProcess.size(); i++) {
658 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; } }
660 unsigned long int thisFilesReads;
661 vector< vector<string> > files = readFastqFiles(thisFilesReads, filePairsToProcess[i][0], filePairsToProcess[i][1]);
663 //adjust for really large processors or really small files
664 if (thisFilesReads < processors) {
665 m->mothurOut("[ERROR]: " + filePairsToProcess[i][0] + " has less than " + toString(processors) + " good reads, skipping\n");
666 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(); }
668 filesToProcess.push_back(files);
669 numReads += thisFilesReads;
673 if (numReads == 0) { m->control_pressed = true; }
675 }else if (ffastafile != "") {
676 vector< vector<string> > files = readFastaFiles(numReads, ffastafile, rfastafile);
677 //adjust for really large processors or really small files
678 if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
679 if (numReads < processors) {
680 for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
681 files.resize(numReads);
682 processors = numReads;
684 filesToProcess.push_back(files);
685 }else { m->control_pressed = true; } //should not get here
687 return filesToProcess;
689 catch(exception& e) {
690 m->errorOut(e, "MakeContigsCommand", "preProcessData");
694 //**********************************************************************************************************************
695 int MakeContigsCommand::createProcesses(vector< vector<string> > files, string outputFasta, string outputScrapFasta, string outputMisMatches, vector<vector<string> > fastaFileNames, int index) {
698 vector<int> processIDS;
700 map<int, string>::iterator it = file2Group.find(index);
701 if (it != file2Group.end()) { group = it->second; }
703 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
706 //loop through and create all the processes you want
707 while (process != processors-1) {
711 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
714 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
719 for(int i=0;i<tempFASTAFileNames.size();i++){
720 for(int j=0;j<tempFASTAFileNames[i].size();j++){
721 if (tempFASTAFileNames[i][j] != "") {
722 tempFASTAFileNames[i][j] += toString(getpid()) + ".temp";
723 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
729 num = driver(files[process],
730 outputFasta + toString(getpid()) + ".temp",
731 outputScrapFasta + toString(getpid()) + ".temp",
732 outputMisMatches + toString(getpid()) + ".temp",
733 tempFASTAFileNames, process, group);
735 //pass groupCounts to parent
737 string tempFile = toString(getpid()) + ".num.temp";
738 m->openOutputFile(tempFile, out);
740 if (createFileGroup || createOligosGroup) {
741 out << groupCounts.size() << endl;
743 for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
744 out << it->first << '\t' << it->second << endl;
747 out << groupMap.size() << endl;
748 for (map<string, string>::iterator it = groupMap.begin(); it != groupMap.end(); it++) {
749 out << it->first << '\t' << it->second << endl;
756 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
757 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
763 m->openOutputFile(outputFasta, temp); temp.close();
764 m->openOutputFile(outputScrapFasta, temp); temp.close();
767 num = driver(files[processors-1], outputFasta, outputScrapFasta, outputMisMatches, fastaFileNames, processors-1, group);
769 //force parent to wait until all the processes are done
770 for (int i=0;i<processIDS.size();i++) {
771 int temp = processIDS[i];
775 for (int i = 0; i < processIDS.size(); i++) {
777 string tempFile = toString(processIDS[i]) + ".num.temp";
778 m->openInputFile(tempFile, in);
780 in >> tempNum; num += tempNum; m->gobble(in);
782 if (createFileGroup || createOligosGroup) {
784 in >> tempNum; m->gobble(in);
787 for (int j = 0; j < tempNum; j++) {
789 in >> group >> groupNum; m->gobble(in);
791 map<string, int>::iterator it = groupCounts.find(group);
792 if (it == groupCounts.end()) { groupCounts[group] = groupNum; }
793 else { groupCounts[it->first] += groupNum; }
796 in >> tempNum; m->gobble(in);
798 for (int j = 0; j < tempNum; j++) {
799 string group, seqName;
800 in >> seqName >> group; m->gobble(in);
802 map<string, string>::iterator it = groupMap.find(seqName);
803 if (it == groupMap.end()) { groupMap[seqName] = group; }
804 else { m->mothurOut("[ERROR]: " + seqName + " is in your fasta file more than once. Sequence names must be unique. please correct.\n"); }
808 in.close(); m->mothurRemove(tempFile);
812 //////////////////////////////////////////////////////////////////////////////////////////////////////
813 //Windows version shared memory, so be careful when passing variables through the contigsData struct.
814 //Above fork() will clone, so memory is separate, but that's not the case with windows,
815 //////////////////////////////////////////////////////////////////////////////////////////////////////
817 vector<contigsData*> pDataArray;
818 DWORD dwThreadIdArray[processors-1];
819 HANDLE hThreadArray[processors-1];
821 //Create processor worker threads.
822 for( int h=0; h<processors-1; h++ ){
823 string extension = "";
824 if (h != 0) { extension = toString(h) + ".temp"; processIDS.push_back(h); }
825 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
830 for(int i=0;i<tempFASTAFileNames.size();i++){
831 for(int j=0;j<tempFASTAFileNames[i].size();j++){
832 if (tempFASTAFileNames[i][j] != "") {
833 tempFASTAFileNames[i][j] += extension;
834 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
840 contigsData* tempcontig = new contigsData(group, files[h], (outputFasta + extension), (outputScrapFasta + extension), (outputMisMatches + extension), align, m, match, misMatch, gapOpen, gapExtend, insert, deltaq, barcodes, primers, tempFASTAFileNames, barcodeNameVector, primerNameVector, pdiffs, bdiffs, tdiffs, createOligosGroup, createFileGroup, allFiles, trimOverlap, h);
841 pDataArray.push_back(tempcontig);
843 hThreadArray[h] = CreateThread(NULL, 0, MyContigsThreadFunction, pDataArray[h], 0, &dwThreadIdArray[h]);
846 vector<vector<string> > tempFASTAFileNames = fastaFileNames;
850 string extension = toString(processors-1) + ".temp";
852 for(int i=0;i<tempFASTAFileNames.size();i++){
853 for(int j=0;j<tempFASTAFileNames[i].size();j++){
854 if (tempFASTAFileNames[i][j] != "") {
855 tempFASTAFileNames[i][j] += extension;
856 m->openOutputFile(tempFASTAFileNames[i][j], temp); temp.close();
864 m->openOutputFile(outputFasta, temp); temp.close();
865 m->openOutputFile(outputScrapFasta, temp); temp.close();
868 processIDS.push_back(processors-1);
869 num = driver(files[processors-1], (outputFasta+ toString(processors-1) + ".temp"), (outputScrapFasta+ toString(processors-1) + ".temp"), (outputMisMatches+ toString(processors-1) + ".temp"), tempFASTAFileNames, processors-1, group);
871 //Wait until all threads have terminated.
872 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
874 //Close all thread handles and free memory allocations.
875 for(int i=0; i < pDataArray.size(); i++){
876 num += pDataArray[i]->count;
877 if (!pDataArray[i]->done) {
878 m->mothurOut("[ERROR]: process " + toString(i) + " only processed " + toString(pDataArray[i]->count) + " of sequences assigned to it, quitting. \n"); m->control_pressed = true;
880 for (map<string, int>::iterator it = pDataArray[i]->groupCounts.begin(); it != pDataArray[i]->groupCounts.end(); it++) {
881 map<string, int>::iterator it2 = groupCounts.find(it->first);
882 if (it2 == groupCounts.end()) { groupCounts[it->first] = it->second; }
883 else { groupCounts[it->first] += it->second; }
885 for (map<string, string>::iterator it = pDataArray[i]->groupMap.begin(); it != pDataArray[i]->groupMap.end(); it++) {
886 map<string, string>::iterator it2 = groupMap.find(it->first);
887 if (it2 == groupMap.end()) { groupMap[it->first] = it->second; }
888 else { m->mothurOut("[ERROR]: " + it->first + " is in your fasta file more than once. Sequence names must be unique. please correct.\n"); }
890 CloseHandle(hThreadArray[i]);
891 delete pDataArray[i];
896 for (int i = 0; i < processIDS.size(); i++) {
897 m->appendFiles((outputFasta + toString(processIDS[i]) + ".temp"), outputFasta);
898 m->mothurRemove((outputFasta + toString(processIDS[i]) + ".temp"));
900 m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta);
901 m->mothurRemove((outputScrapFasta + toString(processIDS[i]) + ".temp"));
903 m->appendFilesWithoutHeaders((outputMisMatches + toString(processIDS[i]) + ".temp"), outputMisMatches);
904 m->mothurRemove((outputMisMatches + toString(processIDS[i]) + ".temp"));
907 for(int j=0;j<fastaFileNames.size();j++){
908 for(int k=0;k<fastaFileNames[j].size();k++){
909 if (fastaFileNames[j][k] != "") {
910 m->appendFiles((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"), fastaFileNames[j][k]);
911 m->mothurRemove((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"));
920 catch(exception& e) {
921 m->errorOut(e, "MakeContigsCommand", "createProcesses");
925 //**********************************************************************************************************************
926 int MakeContigsCommand::driver(vector<string> files, string outputFasta, string outputScrapFasta, string outputMisMatches, vector<vector<string> > fastaFileNames, int process, string group){
929 Alignment* alignment;
930 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
931 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
934 string thisffastafile = files[0];
935 string thisfqualfile = files[1];
936 string thisrfastafile = files[2];
937 string thisrqualfile = files[3];
939 if (m->debug) { m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
941 ifstream inFFasta, inRFasta, inFQual, inRQual;
942 ofstream outFasta, outMisMatch, outScrapFasta;
943 m->openInputFile(thisffastafile, inFFasta);
944 m->openInputFile(thisrfastafile, inRFasta);
945 if (thisfqualfile != "") {
946 m->openInputFile(thisfqualfile, inFQual);
947 m->openInputFile(thisrqualfile, inRQual);
949 m->openOutputFile(outputFasta, outFasta);
950 m->openOutputFile(outputScrapFasta, outScrapFasta);
951 m->openOutputFile(outputMisMatches, outMisMatch);
952 outMisMatch << "Name\tLength\tOverlap_Length\tOverlap_Start\tOverlap_End\tMisMatches\tNum_Ns\n";
954 TrimOligos trimOligos(pdiffs, bdiffs, 0, 0, primers, barcodes);
956 while ((!inFFasta.eof()) && (!inRFasta.eof())) {
958 if (m->control_pressed) { break; }
961 string trashCode = "";
962 int currentSeqsDiffs = 0;
964 //read seqs and quality info
965 Sequence fSeq(inFFasta); m->gobble(inFFasta);
966 Sequence rSeq(inRFasta); m->gobble(inRFasta);
967 QualityScores* fQual = NULL; QualityScores* rQual = NULL;
968 if (thisfqualfile != "") {
969 fQual = new QualityScores(inFQual); m->gobble(inFQual);
970 rQual = new QualityScores(inRQual); m->gobble(inRQual);
973 int barcodeIndex = 0;
976 if(barcodes.size() != 0){
977 if (thisfqualfile != "") {
978 success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
980 success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
982 if(success > bdiffs) { trashCode += 'b'; }
983 else{ currentSeqsDiffs += success; }
986 if(primers.size() != 0){
987 if (thisfqualfile != "") {
988 success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
990 success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
992 if(success > pdiffs) { trashCode += 'f'; }
993 else{ currentSeqsDiffs += success; }
996 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
998 //flip the reverse reads
999 rSeq.reverseComplement();
1000 if (thisfqualfile != "") { rQual->flipQScores(); }
1003 alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
1004 map<int, int> ABaseMap = alignment->getSeqAAlnBaseMap();
1005 map<int, int> BBaseMap = alignment->getSeqBAlnBaseMap();
1006 fSeq.setAligned(alignment->getSeqAAln());
1007 rSeq.setAligned(alignment->getSeqBAln());
1008 int length = fSeq.getAligned().length();
1010 //traverse alignments merging into one contiguous seq
1012 int numMismatches = 0;
1013 string seq1 = fSeq.getAligned();
1014 string seq2 = rSeq.getAligned();
1015 vector<int> scores1, scores2;
1016 if (thisfqualfile != "") {
1017 scores1 = fQual->getQualityScores();
1018 scores2 = rQual->getQualityScores();
1019 delete fQual; delete rQual;
1022 // if (num < 5) { cout << fSeq.getStartPos() << '\t' << fSeq.getEndPos() << '\t' << rSeq.getStartPos() << '\t' << rSeq.getEndPos() << endl; }
1023 int overlapStart = fSeq.getStartPos();
1024 int seq2Start = rSeq.getStartPos();
1026 //bigger of the 2 starting positions is the location of the overlapping start
1027 if (overlapStart < seq2Start) { //seq2 starts later so take from 0 to seq2Start from seq1
1028 overlapStart = seq2Start;
1029 for (int i = 0; i < overlapStart; i++) { contig += seq1[i]; }
1030 }else { //seq1 starts later so take from 0 to overlapStart from seq2
1031 for (int i = 0; i < overlapStart; i++) { contig += seq2[i]; }
1034 int seq1End = fSeq.getEndPos();
1035 int seq2End = rSeq.getEndPos();
1036 int overlapEnd = seq1End;
1037 if (seq2End < overlapEnd) { overlapEnd = seq2End; } //smallest end position is where overlapping ends
1039 int oStart = contig.length();
1040 for (int i = overlapStart; i < overlapEnd; i++) {
1041 if (seq1[i] == seq2[i]) { //match, add base and choose highest score
1043 }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 insert. In that case eliminate base
1044 if (thisfqualfile != "") {
1045 if (scores2[BBaseMap[i]] <= insert) { } //
1046 else { contig += seq2[i]; }
1047 }else { contig += seq2[i]; } //with no quality info, then we keep it?
1048 }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 insert. In that case eliminate base
1049 if (thisfqualfile != "") {
1050 if (scores1[ABaseMap[i]] <= insert) { } //
1051 else { contig += seq1[i]; }
1052 }else { contig += seq1[i]; } //with no quality info, then we keep it?
1053 }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
1054 if (thisfqualfile != "") {
1055 if (abs(scores1[ABaseMap[i]] - scores2[BBaseMap[i]]) >= deltaq) { //is the difference in qual scores >= deltaq, if yes choose base with higher score
1057 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { c = seq2[i]; }
1059 }else { //if no, base becomes n
1063 }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch
1064 }else { //should never get here
1065 m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
1068 int oend = contig.length();
1069 if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
1070 for (int i = overlapEnd; i < length; i++) { contig += seq2[i]; }
1071 }else { //seq2 ends before seq1 so take from overlap to length from seq1
1072 for (int i = overlapEnd; i < length; i++) { contig += seq1[i]; }
1075 if (trimOverlap) { contig = contig.substr(overlapStart-1, oend-oStart); if (contig.length() == 0) { trashCode += "l"; } }
1077 if(trashCode.length() == 0){
1078 bool ignore = false;
1080 if (m->debug) { m->mothurOut(fSeq.getName()); }
1082 if (createOligosGroup) {
1083 if(barcodes.size() != 0){
1084 string thisGroup = barcodeNameVector[barcodeIndex];
1085 if (primers.size() != 0) {
1086 if (primerNameVector[primerIndex] != "") {
1087 if(thisGroup != "") {
1088 thisGroup += "." + primerNameVector[primerIndex];
1090 thisGroup = primerNameVector[primerIndex];
1095 if (m->debug) { m->mothurOut(", group= " + thisGroup + "\n"); }
1097 int pos = thisGroup.find("ignore");
1098 if (pos == string::npos) {
1099 groupMap[fSeq.getName()] = thisGroup;
1101 map<string, int>::iterator it = groupCounts.find(thisGroup);
1102 if (it == groupCounts.end()) { groupCounts[thisGroup] = 1; }
1103 else { groupCounts[it->first] ++; }
1104 }else { ignore = true; }
1107 }else if (createFileGroup) {
1108 int pos = group.find("ignore");
1109 if (pos == string::npos) {
1110 groupMap[fSeq.getName()] = group;
1112 map<string, int>::iterator it = groupCounts.find(group);
1113 if (it == groupCounts.end()) { groupCounts[group] = 1; }
1114 else { groupCounts[it->first] ++; }
1115 }else { ignore = true; }
1117 if (m->debug) { m->mothurOut("\n"); }
1119 if(allFiles && !ignore){
1121 m->openOutputFileAppend(fastaFileNames[barcodeIndex][primerIndex], output);
1122 output << ">" << fSeq.getName() << endl << contig << endl;
1127 outFasta << ">" << fSeq.getName() << endl << contig << endl;
1129 for (int i = 0; i < contig.length(); i++) { if (contig[i] == 'N') { numNs++; } }
1130 outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << (oend-oStart) << '\t' << oStart << '\t' << oend << '\t' << numMismatches << '\t' << numNs << endl;
1133 outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl;
1138 if((num) % 1000 == 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
1142 if((num) % 1000 != 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
1147 outScrapFasta.close();
1148 outMisMatch.close();
1149 if (thisfqualfile != "") {
1155 if (m->control_pressed) { m->mothurRemove(outputFasta); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches); }
1159 catch(exception& e) {
1160 m->errorOut(e, "MakeContigsCommand", "driver");
1164 //**********************************************************************************************************************
1165 vector< vector<string> > MakeContigsCommand::readFastqFiles(unsigned long int& count, string ffastq, string rfastq){
1167 vector< vector<string> > files;
1168 //maps processors number to file pointer
1169 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1170 map<int, vector<ofstream*> >::iterator it;
1172 //create files to write to
1173 for (int i = 0; i < processors; i++) {
1174 vector<ofstream*> temp;
1175 ofstream* outFF = new ofstream; temp.push_back(outFF);
1176 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
1177 ofstream* outRF = new ofstream; temp.push_back(outRF);
1178 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
1179 tempfiles[i] = temp;
1181 vector<string> names;
1182 string thisOutputDir = outputDir;
1183 if (outputDir == "") { thisOutputDir = m->hasPath(ffastq); }
1184 string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "ffastatemp";
1185 string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rfastatemp";
1186 string fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "fqualtemp";
1187 string rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rqualtemp";
1188 names.push_back(ffastafilename); names.push_back(fqualfilename);
1189 names.push_back(rfastafilename); names.push_back(rqualfilename);
1190 files.push_back(names);
1192 m->openOutputFile(ffastafilename, *outFF);
1193 m->openOutputFile(rfastafilename, *outRF);
1194 m->openOutputFile(fqualfilename, *outFQ);
1195 m->openOutputFile(rqualfilename, *outRQ);
1198 if (m->control_pressed) {
1199 //close files, delete ofstreams
1200 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]; } }
1202 for (int i = 0; i < files.size(); i++) {
1203 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1208 m->openInputFile(ffastq, inForward);
1211 m->openInputFile(rfastq, inReverse);
1214 map<string, fastqRead> uniques;
1215 map<string, fastqRead>::iterator itUniques;
1216 while ((!inForward.eof()) || (!inReverse.eof())) {
1218 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; }
1220 //get a read from forward and reverse fastq files
1221 bool ignoref, ignorer;
1222 fastqRead thisFread, thisRread;
1223 if (!inForward.eof()) { thisFread = readFastq(inForward, ignoref); }
1224 else { ignoref = true; }
1225 if (!inReverse.eof()) { thisRread = readFastq(inReverse, ignorer); }
1226 else { ignorer = true; }
1228 vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1230 for (int i = 0; i < reads.size(); i++) {
1231 fastqRead fread = reads[i].forward;
1232 fastqRead rread = reads[i].reverse;
1234 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1236 //if (checkReads(fread, rread, ffastq, rfastq)) {
1237 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; }
1239 //if the reads are okay write to output files
1240 int process = count % processors;
1242 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1243 *(tempfiles[process][1]) << ">" << fread.name << endl;
1244 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1245 *(tempfiles[process][1]) << endl;
1246 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1247 *(tempfiles[process][3]) << ">" << rread.name << endl;
1248 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1249 *(tempfiles[process][3]) << endl;
1254 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1259 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1261 if (uniques.size() != 0) {
1262 for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1263 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1265 m->mothurOutEndLine();
1268 //close files, delete ofstreams
1269 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]; } }
1275 catch(exception& e) {
1276 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1280 //**********************************************************************************************************************
1281 vector< vector<string> > MakeContigsCommand::readFastaFiles(unsigned long int& count, string ffasta, string rfasta){
1283 vector< vector<string> > files;
1284 //maps processors number to file pointer
1285 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
1286 map<int, vector<ofstream*> >::iterator it;
1288 //create files to write to
1289 for (int i = 0; i < processors; i++) {
1290 vector<ofstream*> temp;
1291 ofstream* outFF = new ofstream; temp.push_back(outFF);
1292 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
1293 ofstream* outRF = new ofstream; temp.push_back(outRF);
1294 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
1295 tempfiles[i] = temp;
1297 vector<string> names;
1298 string thisOutputDir = outputDir;
1299 if (outputDir == "") { thisOutputDir = m->hasPath(ffasta); }
1300 string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffasta)) + toString(i) + "ffastatemp";
1301 string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfasta)) + toString(i) + "rfastatemp";
1302 string fqualfilename = "";
1303 if (fqualfile != "") { fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(fqualfile)) + toString(i) + "fqual.temp"; m->openOutputFile(fqualfilename, *outFQ); }
1304 string rqualfilename = "";
1305 if (rqualfile != "") { rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rqualfile)) + toString(i) + "rqual.temp"; m->openOutputFile(rqualfilename, *outRQ); }
1306 names.push_back(ffastafilename); names.push_back(fqualfilename);
1307 names.push_back(rfastafilename); names.push_back(rqualfilename);
1308 files.push_back(names);
1310 m->openOutputFile(ffastafilename, *outFF);
1311 m->openOutputFile(rfastafilename, *outRF);
1314 if (m->control_pressed) {
1315 //close files, delete ofstreams
1316 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]; } }
1318 for (int i = 0; i < files.size(); i++) {
1319 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
1323 ifstream inForwardFasta;
1324 m->openInputFile(ffasta, inForwardFasta);
1326 ifstream inReverseFasta;
1327 m->openInputFile(rfasta, inReverseFasta);
1329 ifstream inForwardQual, inReverseQual;
1330 if (fqualfile != "") { m->openInputFile(fqualfile, inForwardQual); m->openInputFile(rqualfile, inReverseQual); }
1333 map<string, fastqRead> uniques;
1334 map<string, fastqRead>::iterator itUniques;
1335 while ((!inForwardFasta.eof()) || (!inReverseFasta.eof())) {
1337 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; }
1339 //get a reads from forward and reverse fasta files
1340 bool ignoref, ignorer;
1341 fastqRead thisFread, thisRread;
1342 if (!inForwardFasta.eof()) {
1344 Sequence temp(inForwardFasta); m->gobble(inForwardFasta);
1345 thisFread.name = temp.getName();
1346 thisFread.sequence = temp.getUnaligned();
1347 }else { ignoref = true; }
1348 if (!inReverseFasta.eof()) {
1350 Sequence temp(inReverseFasta); m->gobble(inReverseFasta);
1351 thisRread.name = temp.getName();
1352 thisRread.sequence = temp.getUnaligned();
1353 }else { ignorer = true; }
1355 //get qual reads if given
1356 if (fqualfile != "") {
1357 if (!inForwardQual.eof() && !ignoref) {
1358 QualityScores temp(inForwardQual); m->gobble(inForwardQual);
1359 //if forward files dont match ignore read
1360 if (thisFread.name != temp.getName()) { ignoref = true; }
1361 else { thisFread.scores = temp.getQualityScores(); }
1362 }else { ignoref = true; }
1363 if (!inReverseQual.eof() && !ignorer) {
1364 QualityScores temp(inReverseQual); m->gobble(inReverseQual);
1365 //if reverse files dont match ignore read
1366 if (thisRread.name != temp.getName()) { ignorer = true; }
1367 else { thisRread.scores = temp.getQualityScores(); }
1368 }else { ignorer = true; }
1371 vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
1373 for (int i = 0; i < reads.size(); i++) {
1374 fastqRead fread = reads[i].forward;
1375 fastqRead rread = reads[i].reverse;
1377 if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
1379 // if (checkReads(fread, rread, ffasta, rfasta)) {
1380 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; }
1382 //if the reads are okay write to output files
1383 int process = count % processors;
1385 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
1386 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
1387 if (fqualfile != "") { //if you have quality info, print it
1388 *(tempfiles[process][1]) << ">" << fread.name << endl;
1389 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
1390 *(tempfiles[process][1]) << endl;
1391 *(tempfiles[process][3]) << ">" << rread.name << endl;
1392 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
1393 *(tempfiles[process][3]) << endl;
1398 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1403 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
1405 if (uniques.size() != 0) {
1406 for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
1407 m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
1409 m->mothurOutEndLine();
1412 //close files, delete ofstreams
1413 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]; } }
1414 inReverseFasta.close();
1415 inForwardFasta.close();
1416 if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); }
1420 catch(exception& e) {
1421 m->errorOut(e, "MakeContigsCommand", "readFastaFiles");
1425 //**********************************************************************************************************************
1426 vector<pairFastqRead> MakeContigsCommand::getReads(bool ignoref, bool ignorer, fastqRead forward, fastqRead reverse, map<string, fastqRead>& uniques){
1428 vector<pairFastqRead> reads;
1429 map<string, fastqRead>::iterator itUniques;
1431 if (!ignoref && !ignorer) {
1432 if (forward.name == reverse.name) {
1433 pairFastqRead temp(forward, reverse);
1434 reads.push_back(temp);
1437 //if no match are the names only different by 1 and 2?
1438 string tempFRead = forward.name.substr(0, forward.name.length()-1);
1439 string tempRRead = reverse.name.substr(0, reverse.name.length()-1);
1440 if (tempFRead == tempRRead) {
1441 if ((forward.name[forward.name.length()-1] == '1') && (reverse.name[reverse.name.length()-1] == '2')) {
1442 forward.name = tempFRead;
1443 reverse.name = tempRRead;
1444 pairFastqRead temp(forward, reverse);
1445 reads.push_back(temp);
1451 //look for forward pair
1452 itUniques = uniques.find(forward.name);
1453 if (itUniques != uniques.end()) { //we have the pair for this read
1454 pairFastqRead temp(forward, itUniques->second);
1455 reads.push_back(temp);
1456 uniques.erase(itUniques);
1457 }else { //save this read for later
1458 uniques[forward.name] = forward;
1461 //look for reverse pair
1462 itUniques = uniques.find(reverse.name);
1463 if (itUniques != uniques.end()) { //we have the pair for this read
1464 pairFastqRead temp(itUniques->second, reverse);
1465 reads.push_back(temp);
1466 uniques.erase(itUniques);
1467 }else { //save this read for later
1468 uniques[reverse.name] = reverse;
1473 }else if (!ignoref && ignorer) { //ignore reverse keep forward
1474 //look for forward pair
1475 itUniques = uniques.find(forward.name);
1476 if (itUniques != uniques.end()) { //we have the pair for this read
1477 pairFastqRead temp(forward, itUniques->second);
1478 reads.push_back(temp);
1479 uniques.erase(itUniques);
1480 }else { //save this read for later
1481 uniques[forward.name] = forward;
1484 }else if (ignoref && !ignorer) { //ignore forward keep reverse
1485 //look for reverse pair
1486 itUniques = uniques.find(reverse.name);
1487 if (itUniques != uniques.end()) { //we have the pair for this read
1488 pairFastqRead temp(itUniques->second, reverse);
1489 reads.push_back(temp);
1490 uniques.erase(itUniques);
1491 }else { //save this read for later
1492 uniques[reverse.name] = reverse;
1494 }//else ignore both and do nothing
1498 catch(exception& e) {
1499 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
1503 //**********************************************************************************************************************
1504 fastqRead MakeContigsCommand::readFastq(ifstream& in, bool& ignore){
1510 //read sequence name
1511 string line = m->getline(in); m->gobble(in);
1512 vector<string> pieces = m->splitWhiteSpace(line);
1513 string name = ""; if (pieces.size() != 0) { name = pieces[0]; }
1514 if (name == "") { m->mothurOut("[WARNING]: Blank fasta name, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1515 else if (name[0] != '@') { m->mothurOut("[WARNING]: reading " + name + " expected a name with @ as a leading character, ignoring read."); m->mothurOutEndLine(); ignore=true; }
1516 else { name = name.substr(1); }
1519 string sequence = m->getline(in); m->gobble(in);
1520 if (sequence == "") { m->mothurOut("[WARNING]: missing sequence for " + name + ", ignoring."); ignore=true; }
1522 //read sequence name
1523 line = m->getline(in); m->gobble(in);
1524 pieces = m->splitWhiteSpace(line);
1525 string name2 = ""; if (pieces.size() != 0) { name2 = pieces[0]; }
1526 if (name2 == "") { m->mothurOut("[WARNING]: expected a name with + as a leading character, ignoring."); ignore=true; }
1527 else if (name2[0] != '+') { m->mothurOut("[WARNING]: reading " + name2 + " expected a name with + as a leading character, ignoring."); ignore=true; }
1528 else { name2 = name2.substr(1); if (name2 == "") { name2 = name; } }
1530 //read quality scores
1531 string quality = m->getline(in); m->gobble(in);
1532 if (quality == "") { m->mothurOut("[WARNING]: missing quality for " + name2 + ", ignoring."); ignore=true; }
1534 //sanity check sequence length and number of quality scores match
1535 if (name2 != "") { if (name != name2) { m->mothurOut("[WARNING]: names do not match. read " + name + " for fasta and " + name2 + " for quality, ignoring."); ignore=true; } }
1536 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; }
1538 vector<int> qualScores = convertQual(quality);
1541 read.sequence = sequence;
1542 read.scores = qualScores;
1546 catch(exception& e) {
1547 m->errorOut(e, "MakeContigsCommand", "readFastq");
1551 /**********************************************************************************************************************
1552 bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse, string ffile, string rfile){
1556 //do sequence lengths match
1557 if (forward.sequence.length() != reverse.sequence.length()) {
1558 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");
1562 //do number of qual scores match
1563 if (forward.scores.size() != reverse.scores.size()) {
1564 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");
1570 catch(exception& e) {
1571 m->errorOut(e, "MakeContigsCommand", "checkReads");
1575 //***************************************************************************************************************
1576 vector< vector<string> > MakeContigsCommand::readFileNames(string filename){
1578 vector< vector<string> > files;
1579 string forward, reverse;
1582 m->openInputFile(filename, in);
1586 if (m->control_pressed) { return files; }
1588 in >> forward; m->gobble(in);
1592 while (!in.eof()) { //do we have a group assigned to this pair
1594 if (c == 10 || c == 13 || c == -1){ break; }
1595 else if (c == 32 || c == 9){;} //space or tab
1596 else { group += c; }
1600 //line in file look like: group forward reverse
1601 string temp = forward;
1605 createFileGroup = true;
1610 //check to make sure both are able to be opened
1612 int openForward = m->openInputFile(forward, in2, "noerror");
1614 //if you can't open it, try default location
1615 if (openForward == 1) {
1616 if (m->getDefaultPath() != "") { //default path is set
1617 string tryPath = m->getDefaultPath() + m->getSimpleName(forward);
1618 m->mothurOut("Unable to open " + forward + ". Trying default " + tryPath); m->mothurOutEndLine();
1620 openForward = m->openInputFile(tryPath, in3, "noerror");
1626 //if you can't open it, try output location
1627 if (openForward == 1) {
1628 if (m->getOutputDir() != "") { //default path is set
1629 string tryPath = m->getOutputDir() + m->getSimpleName(forward);
1630 m->mothurOut("Unable to open " + forward + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1632 openForward = m->openInputFile(tryPath, in4, "noerror");
1638 if (openForward == 1) { //can't find it
1639 m->mothurOut("[WARNING]: can't find " + forward + ", ignoring pair.\n");
1640 }else{ in2.close(); }
1643 int openReverse = m->openInputFile(reverse, in3, "noerror");
1645 //if you can't open it, try default location
1646 if (openReverse == 1) {
1647 if (m->getDefaultPath() != "") { //default path is set
1648 string tryPath = m->getDefaultPath() + m->getSimpleName(reverse);
1649 m->mothurOut("Unable to open " + reverse + ". Trying default " + tryPath); m->mothurOutEndLine();
1651 openReverse = m->openInputFile(tryPath, in3, "noerror");
1657 //if you can't open it, try output location
1658 if (openReverse == 1) {
1659 if (m->getOutputDir() != "") { //default path is set
1660 string tryPath = m->getOutputDir() + m->getSimpleName(reverse);
1661 m->mothurOut("Unable to open " + reverse + ". Trying output directory " + tryPath); m->mothurOutEndLine();
1663 openReverse = m->openInputFile(tryPath, in4, "noerror");
1669 if (openReverse == 1) { //can't find it
1670 m->mothurOut("[WARNING]: can't find " + reverse + ", ignoring pair.\n");
1671 }else{ in3.close(); }
1673 if ((openForward != 1) && (openReverse != 1)) { //good pair
1674 file2Group[files.size()] = group;
1675 vector<string> pair;
1676 pair.push_back(forward);
1677 pair.push_back(reverse);
1678 files.push_back(pair);
1685 catch(exception& e) {
1686 m->errorOut(e, "MakeContigsCommand", "checkReads");
1690 //***************************************************************************************************************
1691 //illumina data requires paired forward and reverse data
1692 //BARCODE atgcatgc atgcatgc groupName
1693 //PRIMER atgcatgc atgcatgc groupName
1694 //PRIMER atgcatgc atgcatgc
1695 bool MakeContigsCommand::getOligos(vector<vector<string> >& fastaFileNames, string rootname){
1698 m->openInputFile(oligosfile, in);
1702 string type, foligo, roligo, group;
1704 int indexPrimer = 0;
1705 int indexBarcode = 0;
1706 set<string> uniquePrimers;
1707 set<string> uniqueBarcodes;
1713 if (m->debug) { m->mothurOut("[DEBUG]: reading type - " + type + ".\n"); }
1716 while (!in.eof()) { char c = in.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
1721 //make type case insensitive
1722 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); }
1726 if (m->debug) { m->mothurOut("[DEBUG]: reading - " + foligo + ".\n"); }
1728 for(int i=0;i<foligo.length();i++){
1729 foligo[i] = toupper(foligo[i]);
1730 if(foligo[i] == 'U') { foligo[i] = 'T'; }
1733 if(type == "PRIMER"){
1738 for(int i=0;i<roligo.length();i++){
1739 roligo[i] = toupper(roligo[i]);
1740 if(roligo[i] == 'U') { roligo[i] = 'T'; }
1742 //roligo = reverseOligo(roligo);
1746 // get rest of line in case there is a primer name
1749 if (c == 10 || c == 13){ break; }
1750 else if (c == 32 || c == 9){;} //space or tab
1751 else { group += c; }
1754 oligosPair newPrimer(foligo, roligo);
1756 //check for repeat barcodes
1757 string tempPair = foligo+roligo;
1758 if (uniquePrimers.count(tempPair) != 0) { m->mothurOut("primer pair " + newPrimer.forward + " " + newPrimer.reverse + " is in your oligos file already."); m->mothurOutEndLine(); }
1759 else { uniquePrimers.insert(tempPair); }
1761 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"); } }
1763 primers[indexPrimer]=newPrimer; indexPrimer++;
1764 primerNameVector.push_back(group);
1765 }else if(type == "BARCODE"){
1770 for(int i=0;i<roligo.length();i++){
1771 roligo[i] = toupper(roligo[i]);
1772 if(roligo[i] == 'U') { roligo[i] = 'T'; }
1774 //roligo = reverseOligo(roligo);
1776 oligosPair newPair(foligo, roligo);
1781 if (c == 10 || c == 13){ break; }
1782 else if (c == 32 || c == 9){;} //space or tab
1783 else { group += c; }
1786 if (m->debug) { m->mothurOut("[DEBUG]: barcode pair " + newPair.forward + " " + newPair.reverse + ", and group = " + group + ".\n"); }
1788 //check for repeat barcodes
1789 string tempPair = foligo+roligo;
1790 if (uniqueBarcodes.count(tempPair) != 0) { m->mothurOut("barcode pair " + newPair.forward + " " + newPair.reverse + " is in your oligos file already, disregarding."); m->mothurOutEndLine(); }
1791 else { uniqueBarcodes.insert(tempPair); }
1793 barcodes[indexBarcode]=newPair; indexBarcode++;
1794 barcodeNameVector.push_back(group);
1795 }else if(type == "LINKER"){
1796 linker.push_back(foligo);
1797 m->mothurOut("[WARNING]: make.contigs is not setup to remove linkers, ignoring.\n");
1798 }else if(type == "SPACER"){
1799 spacer.push_back(foligo);
1800 m->mothurOut("[WARNING]: make.contigs is not setup to remove spacers, ignoring.\n");
1802 else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are primer, barcode, linker and spacer. Ignoring " + foligo + "."); m->mothurOutEndLine(); }
1808 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0; }
1810 //add in potential combos
1811 if(barcodeNameVector.size() == 0){
1812 oligosPair temp("", "");
1814 barcodeNameVector.push_back("");
1817 if(primerNameVector.size() == 0){
1818 oligosPair temp("", "");
1820 primerNameVector.push_back("");
1823 fastaFileNames.resize(barcodeNameVector.size());
1824 for(int i=0;i<fastaFileNames.size();i++){
1825 fastaFileNames[i].assign(primerNameVector.size(), "");
1829 set<string> uniqueNames; //used to cleanup outputFileNames
1830 for(map<int, oligosPair>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1831 for(map<int, oligosPair>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1833 string primerName = primerNameVector[itPrimer->first];
1834 string barcodeName = barcodeNameVector[itBar->first];
1836 if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing
1838 string comboGroupName = "";
1839 string fastaFileName = "";
1840 string qualFileName = "";
1841 string nameFileName = "";
1842 string countFileName = "";
1844 if(primerName == ""){
1845 comboGroupName = barcodeNameVector[itBar->first];
1848 if(barcodeName == ""){
1849 comboGroupName = primerNameVector[itPrimer->first];
1852 comboGroupName = barcodeNameVector[itBar->first] + "." + primerNameVector[itPrimer->first];
1858 fastaFileName = rootname + comboGroupName + ".fasta";
1859 if (uniqueNames.count(fastaFileName) == 0) {
1860 outputNames.push_back(fastaFileName);
1861 outputTypes["fasta"].push_back(fastaFileName);
1862 uniqueNames.insert(fastaFileName);
1865 fastaFileNames[itBar->first][itPrimer->first] = fastaFileName;
1866 m->openOutputFile(fastaFileName, temp); temp.close();
1872 bool allBlank = true;
1873 for (int i = 0; i < barcodeNameVector.size(); i++) {
1874 if (barcodeNameVector[i] != "") {
1879 for (int i = 0; i < primerNameVector.size(); i++) {
1880 if (primerNameVector[i] != "") {
1887 m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a groupfile."); m->mothurOutEndLine();
1895 catch(exception& e) {
1896 m->errorOut(e, "MakeContigsCommand", "getOligos");
1900 //********************************************************************/
1901 string MakeContigsCommand::reverseOligo(string oligo){
1903 string reverse = "";
1905 for(int i=oligo.length()-1;i>=0;i--){
1907 if(oligo[i] == 'A') { reverse += 'T'; }
1908 else if(oligo[i] == 'T'){ reverse += 'A'; }
1909 else if(oligo[i] == 'U'){ reverse += 'A'; }
1911 else if(oligo[i] == 'G'){ reverse += 'C'; }
1912 else if(oligo[i] == 'C'){ reverse += 'G'; }
1914 else if(oligo[i] == 'R'){ reverse += 'Y'; }
1915 else if(oligo[i] == 'Y'){ reverse += 'R'; }
1917 else if(oligo[i] == 'M'){ reverse += 'K'; }
1918 else if(oligo[i] == 'K'){ reverse += 'M'; }
1920 else if(oligo[i] == 'W'){ reverse += 'W'; }
1921 else if(oligo[i] == 'S'){ reverse += 'S'; }
1923 else if(oligo[i] == 'B'){ reverse += 'V'; }
1924 else if(oligo[i] == 'V'){ reverse += 'B'; }
1926 else if(oligo[i] == 'D'){ reverse += 'H'; }
1927 else if(oligo[i] == 'H'){ reverse += 'D'; }
1929 else { reverse += 'N'; }
1935 catch(exception& e) {
1936 m->errorOut(e, "MakeContigsCommand", "reverseOligo");
1940 //**********************************************************************************************************************
1941 vector<int> MakeContigsCommand::convertQual(string qual) {
1943 vector<int> qualScores;
1944 bool negativeScores = false;
1946 for (int i = 0; i < qual.length(); i++) {
1949 temp = int(qual[i]);
1950 if (format == "illumina") {
1951 temp -= 64; //char '@'
1952 }else if (format == "illumina1.8+") {
1953 temp -= int('!'); //char '!'
1954 }else if (format == "solexa") {
1955 temp = int(convertTable[temp]); //convert to sanger
1956 temp -= int('!'); //char '!'
1958 temp -= int('!'); //char '!'
1961 if (temp < -5) { negativeScores = true; }
1962 qualScores.push_back(temp);
1965 if (negativeScores) { m->mothurOut("[ERROR]: finding negative quality scores, do you have the right format selected? http://en.wikipedia.org/wiki/FASTQ_format#Encoding \n"); m->control_pressed = true; }
1969 catch(exception& e) {
1970 m->errorOut(e, "MakeContigsCommand", "convertQual");
1975 //**********************************************************************************************************************