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 pfasta("ffastq", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
15 CommandParameter prfasta("rfastq", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(prfasta);
16 CommandParameter palign("align", "Multiple", "needleman-gotoh", "needleman", "", "", "",false,false); parameters.push_back(palign);
17 CommandParameter pmatch("match", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pmatch);
18 CommandParameter pmismatch("mismatch", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pmismatch);
19 CommandParameter pgapopen("gapopen", "Number", "", "-2.0", "", "", "",false,false); parameters.push_back(pgapopen);
20 CommandParameter pgapextend("gapextend", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pgapextend);
21 CommandParameter pthreshold("threshold", "Number", "", "40", "", "", "",false,false); parameters.push_back(pthreshold);
22 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
23 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "MakeContigsCommand", "setParameters");
35 //**********************************************************************************************************************
36 string MakeContigsCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The make.contigs command reads a forward fastq file and a reverse fastq file and outputs new fasta and quality files.\n";
40 helpString += "The make.contigs command parameters are ffastq, rfastq, align, match, mismatch, gapopen, gapextend and processors.\n";
41 helpString += "The ffastq and rfastq parameter is required.\n";
42 helpString += "The align parameter allows you to specify the alignment method to use. Your options are: gotoh, needleman, blast and noalign. The default is needleman.\n";
43 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
44 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n";
45 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
46 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n";
47 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";
48 helpString += "The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n";
49 helpString += "The make.contigs command should be in the following format: \n";
50 helpString += "make.contigs(ffastq=yourForwardFastqFile, rfastq=yourReverseFastqFile, align=yourAlignmentMethod) \n";
51 helpString += "Note: No spaces between parameter labels (i.e. ffastq), '=' and parameters (i.e.yourForwardFastqFile).\n";
55 m->errorOut(e, "MakeContigsCommand", "getHelpString");
60 //**********************************************************************************************************************
61 MakeContigsCommand::MakeContigsCommand(){
63 abort = true; calledHelp = true;
65 vector<string> tempOutNames;
66 outputTypes["fasta"] = tempOutNames;
67 outputTypes["qfile"] = tempOutNames;
68 outputTypes["mismatch"] = tempOutNames;
71 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
75 //**********************************************************************************************************************
76 MakeContigsCommand::MakeContigsCommand(string option) {
78 abort = false; calledHelp = false;
80 //allow user to run help
81 if(option == "help") { help(); abort = true; calledHelp = true; }
82 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85 vector<string> myArray = setParameters();
87 OptionParser parser(option);
88 map<string, string> parameters = parser.getParameters();
90 ValidParameters validParameter("pairwise.seqs");
91 map<string, string>::iterator it;
93 //check to make sure all parameters are valid for command
94 for (it = parameters.begin(); it != parameters.end(); it++) {
95 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
98 //initialize outputTypes
99 vector<string> tempOutNames;
100 outputTypes["fasta"] = tempOutNames;
101 outputTypes["qfile"] = tempOutNames;
102 outputTypes["mismatch"] = tempOutNames;
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("ffastq");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["ffastq"] = inputDir + it->second; }
118 it = parameters.find("rfastq");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["rfastq"] = inputDir + it->second; }
127 ffastqfile = validParameter.validFile(parameters, "ffastq", true);
128 if (ffastqfile == "not open") { ffastqfile = ""; abort = true; }
129 else if (ffastqfile == "not found") { ffastqfile = ""; abort=true; m->mothurOut("The ffastq parameter is required.\n"); }
131 rfastqfile = validParameter.validFile(parameters, "rfastq", true);
132 if (rfastqfile == "not open") { rfastqfile = ""; abort = true; }
133 else if (rfastqfile == "not found") { rfastqfile = ""; abort=true; m->mothurOut("The rfastq parameter is required.\n"); }
135 //if the user changes the output directory command factory will send this info to us in the output parameter
136 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(ffastqfile); }
139 //check for optional parameter and set defaults
140 // ...at some point should added some additional type checking...
142 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
143 m->mothurConvert(temp, match);
145 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
146 m->mothurConvert(temp, misMatch);
147 if (misMatch > 0) { m->mothurOut("[ERROR]: mismatch must be negative.\n"); abort=true; }
149 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
150 m->mothurConvert(temp, gapOpen);
151 if (gapOpen > 0) { m->mothurOut("[ERROR]: gapopen must be negative.\n"); abort=true; }
153 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
154 m->mothurConvert(temp, gapExtend);
155 if (gapExtend > 0) { m->mothurOut("[ERROR]: gapextend must be negative.\n"); abort=true; }
157 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found"){ temp = "40"; }
158 m->mothurConvert(temp, threshold);
159 if ((threshold < 0) || (threshold > 40)) { m->mothurOut("[ERROR]: threshold must be between 0 and 40.\n"); abort=true; }
161 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
162 m->setProcessors(temp);
163 m->mothurConvert(temp, processors);
165 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
166 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"; }
170 catch(exception& e) {
171 m->errorOut(e, "MakeContigsCommand", "MakeContigsCommand");
175 //**********************************************************************************************************************
176 int MakeContigsCommand::execute(){
178 if (abort == true) { if (calledHelp) { return 0; } return 2; }
180 //read ffastq and rfastq files creating fasta and qual files.
181 //this function will create a forward and reverse, fasta and qual files for each processor.
182 //files has an entry for each processor. files[i][0] = forwardFasta, files[i][1] = forwardQual, files[i][2] = reverseFasta, files[i][3] = reverseQual
184 int start = time(NULL);
186 m->mothurOut("Reading fastq data...\n");
187 vector< vector<string> > files = readFastqFiles(numReads);
188 m->mothurOut("Done.\n");
190 if (m->control_pressed) { return 0; }
192 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + "contigs.fasta";
193 string outQualFile = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + "contigs.qual";
194 string outMisMatchFile = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + "contigs.mismatches";
195 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
196 outputNames.push_back(outQualFile); outputTypes["qfile"].push_back(outQualFile);
197 outputNames.push_back(outMisMatchFile); outputTypes["mismatch"].push_back(outMisMatchFile);
199 m->mothurOut("Making contigs...\n");
200 createProcesses(files, outFastaFile, outQualFile, outMisMatchFile);
201 m->mothurOut("Done.\n");
203 //remove temp fasta and qual files
204 for (int i = 0; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } }
206 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
208 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process " + toString(numReads) + " sequences.\n");
210 string currentFasta = "";
211 itTypes = outputTypes.find("fasta");
212 if (itTypes != outputTypes.end()) {
213 if ((itTypes->second).size() != 0) { currentFasta = (itTypes->second)[0]; m->setFastaFile(currentFasta); }
216 string currentQual = "";
217 itTypes = outputTypes.find("qfile");
218 if (itTypes != outputTypes.end()) {
219 if ((itTypes->second).size() != 0) { currentQual = (itTypes->second)[0]; m->setQualFile(currentQual); }
222 //output files created by command
223 m->mothurOutEndLine();
224 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
225 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
226 m->mothurOutEndLine();
231 catch(exception& e) {
232 m->errorOut(e, "MakeContigsCommand", "execute");
236 //**********************************************************************************************************************
237 int MakeContigsCommand::createProcesses(vector< vector<string> > files, string outputFasta, string outputQual, string outputMisMatches) {
240 vector<int> processIDS;
241 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
244 //loop through and create all the processes you want
245 while (process != processors-1) {
249 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
252 num = driver(files[process], outputFasta + toString(getpid()) + ".temp", outputQual + toString(getpid()) + ".temp", outputMisMatches + toString(getpid()) + ".temp");
254 //pass numSeqs to parent
256 string tempFile = outputFasta + toString(getpid()) + ".num.temp";
257 m->openOutputFile(tempFile, out);
263 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
264 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
270 num = driver(files[processors-1], outputFasta, outputQual, outputMisMatches);
272 //force parent to wait until all the processes are done
273 for (int i=0;i<processIDS.size();i++) {
274 int temp = processIDS[i];
278 for (int i = 0; i < processIDS.size(); i++) {
280 string tempFile = outputFasta + toString(processIDS[i]) + ".num.temp";
281 m->openInputFile(tempFile, in);
282 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
283 in.close(); m->mothurRemove(tempFile);
287 //////////////////////////////////////////////////////////////////////////////////////////////////////
288 //Windows version shared memory, so be careful when passing variables through the contigsData struct.
289 //Above fork() will clone, so memory is separate, but that's not the case with windows,
290 //////////////////////////////////////////////////////////////////////////////////////////////////////
292 vector<contigsData*> pDataArray;
293 DWORD dwThreadIdArray[processors-1];
294 HANDLE hThreadArray[processors-1];
296 //Create processor worker threads.
297 for( int i=0; i<processors-1; i++ ){
298 string extension = toString(i) + ".temp";
300 contigsData* tempcontig = new contigsData(files[i], (outputFasta + extension), (outputQual + extension), (outputMisMatches + extension), align, m, match, misMatch, gapOpen, gapExtend, threshold, i);
301 pDataArray.push_back(tempcontig);
302 processIDS.push_back(i);
304 hThreadArray[i] = CreateThread(NULL, 0, MyContigsThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
307 num = driver(files[processors-1], outputFasta, outputQual, outputMisMatches);
309 //Wait until all threads have terminated.
310 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
312 //Close all thread handles and free memory allocations.
313 for(int i=0; i < pDataArray.size(); i++){
314 num += pDataArray[i]->count;
315 CloseHandle(hThreadArray[i]);
316 delete pDataArray[i];
321 for (int i = 0; i < processIDS.size(); i++) {
322 m->appendFiles((outputFasta + toString(processIDS[i]) + ".temp"), outputFasta);
323 m->mothurRemove((outputFasta + toString(processIDS[i]) + ".temp"));
325 m->appendFiles((outputQual + toString(processIDS[i]) + ".temp"), outputQual);
326 m->mothurRemove((outputQual + toString(processIDS[i]) + ".temp"));
328 m->appendFiles((outputMisMatches + toString(processIDS[i]) + ".temp"), outputMisMatches);
329 m->mothurRemove((outputMisMatches + toString(processIDS[i]) + ".temp"));
334 catch(exception& e) {
335 m->errorOut(e, "MakeContigsCommand", "createProcesses");
339 //**********************************************************************************************************************
340 int MakeContigsCommand::driver(vector<string> files, string outputFasta, string outputQual, string outputMisMatches){
343 Alignment* alignment;
344 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
345 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
348 string thisffastafile = files[0];
349 string thisfqualfile = files[1];
350 string thisrfastafile = files[2];
351 string thisrqualfile = files[3];
353 if (m->debug) { m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
355 ifstream inFFasta, inRFasta, inFQual, inRQual;
356 m->openInputFile(thisffastafile, inFFasta);
357 m->openInputFile(thisfqualfile, inFQual);
358 m->openInputFile(thisrfastafile, inRFasta);
359 m->openInputFile(thisrqualfile, inRQual);
361 ofstream outFasta, outQual, outMisMatch;
362 m->openOutputFile(outputFasta, outFasta);
363 m->openOutputFile(outputQual, outQual);
364 m->openOutputFile(outputMisMatches, outMisMatch);
365 outMisMatch << "Name\tLength\tMisMatches\n";
367 while ((!inFQual.eof()) && (!inFFasta.eof()) && (!inRFasta.eof()) && (!inRQual.eof())) {
369 if (m->control_pressed) { break; }
371 //read seqs and quality info
372 Sequence fSeq(inFFasta); m->gobble(inFFasta);
373 Sequence rSeq(inRFasta); m->gobble(inRFasta);
374 QualityScores fQual(inFQual); m->gobble(inFQual);
375 QualityScores rQual(inRQual); m->gobble(inRQual);
377 //flip the reverse reads
378 rSeq.reverseComplement();
382 alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
383 map<int, int> ABaseMap = alignment->getSeqAAlnBaseMap();
384 map<int, int> BBaseMap = alignment->getSeqBAlnBaseMap();
385 fSeq.setAligned(alignment->getSeqAAln());
386 rSeq.setAligned(alignment->getSeqBAln());
387 int length = fSeq.getAligned().length();
389 //traverse alignments merging into one contiguous seq
391 vector<int> contigScores;
392 int numMismatches = 0;
393 string seq1 = fSeq.getAligned();
394 string seq2 = rSeq.getAligned();
396 vector<int> scores1 = fQual.getQualityScores();
397 vector<int> scores2 = rQual.getQualityScores();
398 for (int i = 0; i < length; i++) {
399 if (seq1[i] == seq2[i]) { //match, add base and choose highest score
401 contigScores.push_back(scores1[ABaseMap[i]]);
402 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; }
403 }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
404 if (scores2[BBaseMap[i]] < threshold) { } //
407 contigScores.push_back(scores2[BBaseMap[i]]);
409 }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
410 if (scores1[ABaseMap[i]] < threshold) { } //
413 contigScores.push_back(scores1[ABaseMap[i]]);
415 }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
417 contigScores.push_back(scores1[ABaseMap[i]]);
418 if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; }
421 }else { //should never get here
422 m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
427 outFasta << ">" << fSeq.getName() << endl << contig << endl;
428 outQual << ">" << fSeq.getName() << endl;
429 for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; }
431 outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << numMismatches << endl;
436 if((num) % 1000 == 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
440 if((num) % 1000 != 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
451 if (m->control_pressed) { m->mothurRemove(outputQual); m->mothurRemove(outputFasta); m->mothurRemove(outputMisMatches);}
455 catch(exception& e) {
456 m->errorOut(e, "MakeContigsCommand", "driver");
460 //**********************************************************************************************************************
461 vector< vector<string> > MakeContigsCommand::readFastqFiles(int& count){
463 vector< vector<string> > files;
465 //maps processors number to file pointer
466 map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
467 map<int, vector<ofstream*> >::iterator it;
469 //create files to write to
470 for (int i = 0; i < processors; i++) {
471 vector<ofstream*> temp;
472 ofstream* outFF = new ofstream; temp.push_back(outFF);
473 ofstream* outFQ = new ofstream; temp.push_back(outFQ);
474 ofstream* outRF = new ofstream; temp.push_back(outRF);
475 ofstream* outRQ = new ofstream; temp.push_back(outRQ);
478 vector<string> names;
479 string ffastafilename = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + toString(i) + "ffasta.temp";
480 string rfastafilename = outputDir + m->getRootName(m->getSimpleName(rfastqfile)) + toString(i) + "rfasta.temp";
481 string fqualfilename = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + toString(i) + "fqual.temp";
482 string rqualfilename = outputDir + m->getRootName(m->getSimpleName(rfastqfile)) + toString(i) + "rqual.temp";
483 names.push_back(ffastafilename); names.push_back(fqualfilename);
484 names.push_back(rfastafilename); names.push_back(rqualfilename);
485 files.push_back(names);
487 m->openOutputFile(ffastafilename, *outFF);
488 m->openOutputFile(rfastafilename, *outRF);
489 m->openOutputFile(fqualfilename, *outFQ);
490 m->openOutputFile(rqualfilename, *outRQ);
493 if (m->control_pressed) {
494 //close files, delete ofstreams
495 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]; } }
497 for (int i = 0; i < files.size(); i++) {
498 for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
503 m->openInputFile(ffastqfile, inForward);
506 m->openInputFile(rfastqfile, inReverse);
509 while ((!inForward.eof()) && (!inReverse.eof())) {
511 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; }
513 //get a read from forward and reverse fastq files
514 fastqRead fread = readFastq(inForward);
515 fastqRead rread = readFastq(inReverse);
516 checkReads(fread, rread);
518 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; }
520 //if the reads are okay write to output files
521 int process = count % processors;
523 *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
524 *(tempfiles[process][1]) << ">" << fread.name << endl;
525 for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
526 *(tempfiles[process][1]) << endl;
527 *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
528 *(tempfiles[process][3]) << ">" << rread.name << endl;
529 for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
530 *(tempfiles[process][3]) << endl;
535 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
539 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
543 //close files, delete ofstreams
544 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]; } }
548 //adjust for really large processors or really small files
549 if (count < processors) {
550 for (int i = count; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
557 catch(exception& e) {
558 m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
562 //**********************************************************************************************************************
563 fastqRead MakeContigsCommand::readFastq(ifstream& in){
568 string name = m->getline(in); m->gobble(in);
569 if (name == "") { m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; return read; }
570 else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; return read; }
571 else { name = name.substr(1); }
574 string sequence = m->getline(in); m->gobble(in);
575 if (sequence == "") { m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; return read; }
578 string name2 = m->getline(in); m->gobble(in);
579 if (name2 == "") { m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; return read; }
580 else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; return read; }
581 else { name2 = name2.substr(1); }
583 //read quality scores
584 string quality = m->getline(in); m->gobble(in);
585 if (quality == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; return read; }
587 //sanity check sequence length and number of quality scores match
588 if (name2 != "") { if (name != name2) { m->mothurOut("[ERROR]: names do not match. read " + name + " for fasta and " + name2 + " for quality."); m->mothurOutEndLine(); m->control_pressed = true; return read; } }
589 if (quality.length() != sequence.length()) { m->mothurOut("[ERROR]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores."); m->mothurOutEndLine(); m->control_pressed = true; return read; }
591 vector<int> qualScores;
592 int controlChar = int('@');
593 for (int i = 0; i < quality.length(); i++) {
594 int temp = int(quality[i]);
597 qualScores.push_back(temp);
601 read.sequence = sequence;
602 read.scores = qualScores;
606 catch(exception& e) {
607 m->errorOut(e, "MakeContigsCommand", "readFastq");
611 //**********************************************************************************************************************
612 bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse){
617 if ((forward.name.length() > 2) && (reverse.name.length() > 2)) {
618 forward.name = forward.name.substr(0, forward.name.length()-2);
619 reverse.name = reverse.name.substr(0, reverse.name.length()-2);
620 }else { good = false; m->control_pressed = true; }
623 if (forward.name != reverse.name) {
624 m->mothurOut("[ERROR]: read " + forward.name + " from " + ffastqfile + ", but read " + reverse.name + " from " + rfastqfile + ".\n");
625 good = false; m->control_pressed = true;
628 //do sequence lengths match
629 if (forward.sequence.length() != reverse.sequence.length()) {
630 m->mothurOut("[ERROR]: For sequence " + forward.name + " I read a sequence of length " + toString(forward.sequence.length()) + " from " + ffastqfile + ", but read a sequence of length " + toString(reverse.sequence.length()) + " from " + rfastqfile + ".\n");
631 good = false; m->control_pressed = true;
634 //do number of qual scores match
635 if (forward.scores.size() != reverse.scores.size()) {
636 m->mothurOut("[ERROR]: For sequence " + forward.name + " I read " + toString(forward.scores.size()) + " quality scores from " + ffastqfile + ", but read " + toString(reverse.scores.size()) + " quality scores from " + rfastqfile + ".\n");
637 good = false; m->control_pressed = true;
642 catch(exception& e) {
643 m->errorOut(e, "MakeContigsCommand", "readFastq");
647 //**********************************************************************************************************************