5 * Created by Pat Schloss on 7/15/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "seqerrorcommand.h"
11 #include "reportfile.h"
12 #include "qualityscores.h"
13 #include "refchimeratest.h"
14 #include "filterseqscommand.h"
17 //**********************************************************************************************************************
18 vector<string> SeqErrorCommand::setParameters(){
20 CommandParameter pquery("fasta", "InputTypes", "", "", "none", "none", "none","errorType",false,true,true); parameters.push_back(pquery);
21 CommandParameter preference("reference", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(preference);
22 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "QualReport","",false,false); parameters.push_back(pqfile);
23 CommandParameter preport("report", "InputTypes", "", "", "none", "none", "QualReport","",false,false); parameters.push_back(preport);
24 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(pname);
25 CommandParameter pignorechimeras("ignorechimeras", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pignorechimeras);
26 CommandParameter pthreshold("threshold", "Number", "", "1.0", "", "", "","",false,false); parameters.push_back(pthreshold);
27 CommandParameter paligned("aligned", "Boolean", "T", "", "", "", "","",false,false); parameters.push_back(paligned);
28 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
29 CommandParameter psave("save", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(psave);
30 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
31 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
33 vector<string> myArray;
34 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
38 m->errorOut(e, "SeqErrorCommand", "setParameters");
42 //**********************************************************************************************************************
43 string SeqErrorCommand::getHelpString(){
45 string helpString = "";
46 helpString += "The seq.error command reads a query alignment file and a reference alignment file and creates .....\n";
47 helpString += "The fasta parameter...\n";
48 helpString += "The reference parameter...\n";
49 helpString += "The qfile parameter...\n";
50 helpString += "The report parameter...\n";
51 helpString += "The name parameter...\n";
52 helpString += "The ignorechimeras parameter...\n";
53 helpString += "The threshold parameter...\n";
54 helpString += "The processors parameter...\n";
55 helpString += "If the save parameter is set to true the reference sequences will be saved in memory, to clear them later you can use the clear.memory command. Default=f.";
56 helpString += "Example seq.error(...).\n";
57 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
58 helpString += "For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n";
62 m->errorOut(e, "SeqErrorCommand", "getHelpString");
66 //**********************************************************************************************************************
67 string SeqErrorCommand::getOutputPattern(string type) {
71 if (type == "errorsummary") { pattern = "[filename],error.summary"; }
72 else if (type == "errorseq") { pattern = "[filename],error.seq"; }
73 else if (type == "errorquality") { pattern = "[filename],error.quality"; }
74 else if (type == "errorqualforward") { pattern = "[filename],error.qual.forward"; }
75 else if (type == "errorqualreverse") { pattern = "[filename],error.qual.reverse"; }
76 else if (type == "errorforward") { pattern = "[filename],error.seq.forward"; }
77 else if (type == "errorreverse") { pattern = "[filename],error.seq.reverse"; }
78 else if (type == "errorcount") { pattern = "[filename],error.count"; }
79 else if (type == "errormatrix") { pattern = "[filename],error.matrix"; }
80 else if (type == "errorchimera") { pattern = "[filename],error.chimera"; }
81 else if (type == "errorref-query") { pattern = "[filename],error.ref-query"; }
82 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
87 m->errorOut(e, "SeqErrorCommand", "getOutputPattern");
91 //**********************************************************************************************************************
92 SeqErrorCommand::SeqErrorCommand(){
94 abort = true; calledHelp = true;
96 vector<string> tempOutNames;
97 outputTypes["errorsummary"] = tempOutNames;
98 outputTypes["errorseq"] = tempOutNames;
99 outputTypes["errorquality"] = tempOutNames;
100 outputTypes["errorqualforward"] = tempOutNames;
101 outputTypes["errorqualreverse"] = tempOutNames;
102 outputTypes["errorforward"] = tempOutNames;
103 outputTypes["errorreverse"] = tempOutNames;
104 outputTypes["errorcount"] = tempOutNames;
105 outputTypes["errormatrix"] = tempOutNames;
106 outputTypes["errorchimera"] = tempOutNames;
107 outputTypes["errorref-query"] = tempOutNames;
109 catch(exception& e) {
110 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
114 //***************************************************************************************************************
116 SeqErrorCommand::SeqErrorCommand(string option) {
119 abort = false; calledHelp = false;
120 rdb = ReferenceDB::getInstance();
122 //allow user to run help
123 if(option == "help") { help(); abort = true; calledHelp = true; }
124 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
128 vector<string> myArray = setParameters();
130 OptionParser parser(option);
131 map<string,string> parameters = parser.getParameters();
133 ValidParameters validParameter;
134 map<string,string>::iterator it;
136 //check to make sure all parameters are valid for command
137 for (it = parameters.begin(); it != parameters.end(); it++) {
138 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
141 //initialize outputTypes
142 vector<string> tempOutNames;
143 outputTypes["errorsummary"] = tempOutNames;
144 outputTypes["errorseq"] = tempOutNames;
145 outputTypes["errorquality"] = tempOutNames;
146 outputTypes["errorqualforward"] = tempOutNames;
147 outputTypes["errorqualreverse"] = tempOutNames;
148 outputTypes["errorforward"] = tempOutNames;
149 outputTypes["errorreverse"] = tempOutNames;
150 outputTypes["errorcount"] = tempOutNames;
151 outputTypes["errormatrix"] = tempOutNames;
152 outputTypes["errorchimera"] = tempOutNames;
153 outputTypes["errorref-query"] = tempOutNames;
156 //if the user changes the input directory command factory will send this info to us in the output parameter
157 string inputDir = validParameter.validFile(parameters, "inputdir", false);
158 if (inputDir == "not found"){ inputDir = ""; }
161 it = parameters.find("fasta");
162 //user has given a template file
163 if(it != parameters.end()){
164 path = m->hasPath(it->second);
165 //if the user has not given a path then, add inputdir. else leave path alone.
166 if (path == "") { parameters["fasta"] = inputDir + it->second; }
169 it = parameters.find("reference");
170 //user has given a template file
171 if(it != parameters.end()){
172 path = m->hasPath(it->second);
173 //if the user has not given a path then, add inputdir. else leave path alone.
174 if (path == "") { parameters["reference"] = inputDir + it->second; }
177 it = parameters.find("name");
178 //user has given a names file
179 if(it != parameters.end()){
180 path = m->hasPath(it->second);
181 //if the user has not given a path then, add inputdir. else leave path alone.
182 if (path == "") { parameters["name"] = inputDir + it->second; }
185 it = parameters.find("qfile");
186 //user has given a quality score file
187 if(it != parameters.end()){
188 path = m->hasPath(it->second);
189 //if the user has not given a path then, add inputdir. else leave path alone.
190 if (path == "") { parameters["qfile"] = inputDir + it->second; }
193 it = parameters.find("report");
194 //user has given a alignment report file
195 if(it != parameters.end()){
196 path = m->hasPath(it->second);
197 //if the user has not given a path then, add inputdir. else leave path alone.
198 if (path == "") { parameters["report"] = inputDir + it->second; }
202 //check for required parameters
203 queryFileName = validParameter.validFile(parameters, "fasta", true);
204 if (queryFileName == "not found") {
205 queryFileName = m->getFastaFile();
206 if (queryFileName != "") { m->mothurOut("Using " + queryFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
207 else { m->mothurOut("You have no current fasta file and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
209 else if (queryFileName == "not open") { queryFileName = ""; abort = true; }
210 else { m->setFastaFile(queryFileName); }
212 referenceFileName = validParameter.validFile(parameters, "reference", true);
213 if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
214 else if (referenceFileName == "not open") { abort = true; }
216 //check for optional parameters
217 namesFileName = validParameter.validFile(parameters, "name", true);
218 if(namesFileName == "not found"){ namesFileName = ""; }
219 else if (namesFileName == "not open") { namesFileName = ""; abort = true; }
220 else { m->setNameFile(namesFileName); }
222 qualFileName = validParameter.validFile(parameters, "qfile", true);
223 if(qualFileName == "not found"){ qualFileName = ""; }
224 else if (qualFileName == "not open") { qualFileName = ""; abort = true; }
225 else { m->setQualFile(qualFileName); }
227 reportFileName = validParameter.validFile(parameters, "report", true);
228 if(reportFileName == "not found"){ reportFileName = ""; }
229 else if (reportFileName == "not open") { reportFileName = ""; abort = true; }
231 if((reportFileName != "" && qualFileName == "") || (reportFileName == "" && qualFileName != "")){
232 m->mothurOut("if you use either a qual file or a report file, you have to have both.");
233 m->mothurOutEndLine();
237 outputDir = validParameter.validFile(parameters, "outputdir", false);
238 if (outputDir == "not found"){
240 outputDir += m->hasPath(queryFileName); //if user entered a file with a path then preserve it
243 //check for optional parameter and set defaults
244 // ...at some point should added some additional type checking...
245 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found") { temp = "1.00"; }
246 m->mothurConvert(temp, threshold);
248 temp = validParameter.validFile(parameters, "aligned", true); if (temp == "not found"){ temp = "t"; }
249 aligned = m->isTrue(temp);
250 // rdb->aligned = aligned; #do we need these lines for aligned?
251 // if (aligned) { //clear out old references
252 // rdb->clearMemory();
255 temp = validParameter.validFile(parameters, "save", false); if (temp == "not found"){ temp = "f"; }
256 save = m->isTrue(temp);
258 if (save) { //clear out old references
262 //this has to go after save so that if the user sets save=t and provides no reference we abort
263 referenceFileName = validParameter.validFile(parameters, "reference", true);
264 if (referenceFileName == "not found") {
265 //check for saved reference sequences
266 if (rdb->referenceSeqs.size() != 0) {
267 referenceFileName = "saved";
269 m->mothurOut("[ERROR]: You don't have any saved reference sequences and the reference parameter is a required.");
270 m->mothurOutEndLine();
273 }else if (referenceFileName == "not open") { abort = true; }
274 else { if (save) { rdb->setSavedReference(referenceFileName); } }
277 temp = validParameter.validFile(parameters, "ignorechimeras", false); if (temp == "not found") { temp = "T"; }
278 ignoreChimeras = m->isTrue(temp);
280 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
281 m->setProcessors(temp);
282 m->mothurConvert(temp, processors);
284 substitutionMatrix.resize(6);
285 for(int i=0;i<6;i++){ substitutionMatrix[i].resize(6,0); }
287 if ((namesFileName == "") && (queryFileName != "")){
288 vector<string> files; files.push_back(queryFileName);
289 parser.getNameFile(files);
293 catch(exception& e) {
294 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
298 //***************************************************************************************************************
300 int SeqErrorCommand::execute(){
302 if (abort == true) { if (calledHelp) { return 0; } return 2; }
304 int start = time(NULL);
309 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
310 map<string, string> variables;
311 variables["[filename]"] = fileNameRoot;
312 string errorSummaryFileName = getOutputFileName("errorsummary",variables);
313 outputNames.push_back(errorSummaryFileName); outputTypes["errorsummary"].push_back(errorSummaryFileName);
315 string errorSeqFileName = getOutputFileName("errorseq",variables);
316 outputNames.push_back(errorSeqFileName); outputTypes["errorseq"].push_back(errorSeqFileName);
318 string errorChimeraFileName = getOutputFileName("errorchimera",variables);
319 outputNames.push_back(errorChimeraFileName); outputTypes["errorchimera"].push_back(errorChimeraFileName);
321 getReferences(); //read in reference sequences - make sure there's no ambiguous bases
323 if(namesFileName != ""){ weights = getWeights(); }
325 vector<unsigned long long> fastaFilePos;
326 vector<unsigned long long> qFilePos;
327 vector<unsigned long long> reportFilePos;
329 setLines(queryFileName, qualFileName, reportFileName, fastaFilePos, qFilePos, reportFilePos);
331 if (m->control_pressed) { return 0; }
333 for (int i = 0; i < (fastaFilePos.size()-1); i++) {
334 lines.push_back(linePair(fastaFilePos[i], fastaFilePos[(i+1)]));
335 if (qualFileName != "") { qLines.push_back(linePair(qFilePos[i], qFilePos[(i+1)])); }
336 if (reportFileName != "") { rLines.push_back(linePair(reportFilePos[i], reportFilePos[(i+1)])); }
338 if(qualFileName == "") { qLines = lines; rLines = lines; } //fills with duds
341 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
343 numSeqs = driver(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName, lines[0], qLines[0], rLines[0]);
345 numSeqs = createProcesses(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName);
348 numSeqs = driver(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName, lines[0], qLines[0], rLines[0]);
351 if(qualFileName != "" && reportFileName != ""){
352 printErrorQuality(qScoreErrorMap);
353 printQualityFR(qualForwardMap, qualReverseMap);
356 printErrorFRFile(errorForward, errorReverse);
358 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
360 string errorCountFileName = getOutputFileName("errorcount",variables);
361 ofstream errorCountFile;
362 m->openOutputFile(errorCountFileName, errorCountFile);
363 outputNames.push_back(errorCountFileName); outputTypes["errorcount"].push_back(errorCountFileName);
364 m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n");
365 m->mothurOut("Errors\tSequences\n");
366 errorCountFile << "Errors\tSequences\n";
367 for(int i=0;i<misMatchCounts.size();i++){
368 m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
369 errorCountFile << i << '\t' << misMatchCounts[i] << endl;
371 errorCountFile.close();
373 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
377 string megAlignmentFileName = getOutputFileName("errorref-query",variables);
378 ofstream megAlignmentFile;
379 m->openOutputFile(megAlignmentFileName, megAlignmentFile);
380 outputNames.push_back(megAlignmentFileName); outputTypes["errorref-query"].push_back(megAlignmentFileName);
382 for(int i=0;i<numRefs;i++){
383 megAlignmentFile << referenceSeqs[i].getInlineSeq() << endl;
384 megAlignmentFile << megaAlignVector[i] << endl;
387 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences.");
388 m->mothurOutEndLine();
390 m->mothurOutEndLine();
391 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
392 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
393 m->mothurOutEndLine();
397 catch(exception& e) {
398 m->errorOut(e, "SeqErrorCommand", "execute");
402 //**********************************************************************************************************************
403 int SeqErrorCommand::createProcesses(string filename, string qFileName, string rFileName, string summaryFileName, string errorOutputFileName, string chimeraOutputFileName) {
407 map<char, vector<int> >::iterator it;
409 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
411 //loop through and create all the processes you want
412 while (process != processors) {
416 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
420 num = driver(filename, qFileName, rFileName, summaryFileName + toString(getpid()) + ".temp", errorOutputFileName+ toString(getpid()) + ".temp", chimeraOutputFileName + toString(getpid()) + ".temp", lines[process], qLines[process], rLines[process]);
422 //pass groupCounts to parent
424 string tempFile = filename + toString(getpid()) + ".info.temp";
425 m->openOutputFile(tempFile, out);
427 //output totalBases and totalMatches
428 out << num << '\t' << totalBases << '\t' << totalMatches << endl << endl;
430 //output substitutionMatrix
431 for(int i = 0; i < substitutionMatrix.size(); i++) {
432 for (int j = 0; j < substitutionMatrix[i].size(); j++) {
433 out << substitutionMatrix[i][j] << '\t';
439 //output qScoreErrorMap
440 for (it = qScoreErrorMap.begin(); it != qScoreErrorMap.end(); it++) {
441 vector<int> thisScoreErrorMap = it->second;
442 out << it->first << '\t';
443 for (int i = 0; i < thisScoreErrorMap.size(); i++) {
444 out << thisScoreErrorMap[i] << '\t';
450 //output qualForwardMap
451 for(int i = 0; i < qualForwardMap.size(); i++) {
452 for (int j = 0; j < qualForwardMap[i].size(); j++) {
453 out << qualForwardMap[i][j] << '\t';
459 //output qualReverseMap
460 for(int i = 0; i < qualReverseMap.size(); i++) {
461 for (int j = 0; j < qualReverseMap[i].size(); j++) {
462 out << qualReverseMap[i][j] << '\t';
469 //output errorForward
470 for (it = errorForward.begin(); it != errorForward.end(); it++) {
471 vector<int> thisErrorForward = it->second;
472 out << it->first << '\t';
473 for (int i = 0; i < thisErrorForward.size(); i++) {
474 out << thisErrorForward[i] << '\t';
480 //output errorReverse
481 for (it = errorReverse.begin(); it != errorReverse.end(); it++) {
482 vector<int> thisErrorReverse = it->second;
483 out << it->first << '\t';
484 for (int i = 0; i < thisErrorReverse.size(); i++) {
485 out << thisErrorReverse[i] << '\t';
491 //output misMatchCounts
492 out << misMatchCounts.size() << endl;
493 for (int j = 0; j < misMatchCounts.size(); j++) {
494 out << misMatchCounts[j] << '\t';
499 //output megaAlignVector
500 for (int j = 0; j < megaAlignVector.size(); j++) {
501 out << megaAlignVector[j] << endl;
509 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
510 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
516 num = driver(filename, qFileName, rFileName, summaryFileName, errorOutputFileName, chimeraOutputFileName, lines[0], qLines[0], rLines[0]);
518 //force parent to wait until all the processes are done
519 for (int i=0;i<processIDS.size();i++) {
520 int temp = processIDS[i];
525 for(int i=0;i<processIDS.size();i++){
527 m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
529 m->appendFiles((summaryFileName + toString(processIDS[i]) + ".temp"), summaryFileName);
530 m->mothurRemove((summaryFileName + toString(processIDS[i]) + ".temp"));
531 m->appendFiles((errorOutputFileName + toString(processIDS[i]) + ".temp"), errorOutputFileName);
532 m->mothurRemove((errorOutputFileName + toString(processIDS[i]) + ".temp"));
533 m->appendFiles((chimeraOutputFileName + toString(processIDS[i]) + ".temp"), chimeraOutputFileName);
534 m->mothurRemove((chimeraOutputFileName + toString(processIDS[i]) + ".temp"));
537 string tempFile = filename + toString(processIDS[i]) + ".info.temp";
538 m->openInputFile(tempFile, in);
540 //input totalBases and totalMatches
541 int tempBases, tempMatches, tempNumSeqs;
542 in >> tempNumSeqs >> tempBases >> tempMatches; m->gobble(in);
543 totalBases += tempBases; totalMatches += tempMatches; num += tempNumSeqs;
545 //input substitutionMatrix
547 for(int i = 0; i < substitutionMatrix.size(); i++) {
548 for (int j = 0; j < substitutionMatrix[i].size(); j++) {
549 in >> tempNum; substitutionMatrix[i][j] += tempNum;
555 //input qScoreErrorMap
557 for (int i = 0; i < qScoreErrorMap.size(); i++) {
559 vector<int> thisScoreErrorMap = qScoreErrorMap[first];
561 for (int i = 0; i < thisScoreErrorMap.size(); i++) {
562 in >> tempNum; thisScoreErrorMap[i] += tempNum;
564 qScoreErrorMap[first] = thisScoreErrorMap;
569 //input qualForwardMap
570 for(int i = 0; i < qualForwardMap.size(); i++) {
571 for (int j = 0; j < qualForwardMap[i].size(); j++) {
572 in >> tempNum; qualForwardMap[i][j] += tempNum;
578 //input qualReverseMap
579 for(int i = 0; i < qualReverseMap.size(); i++) {
580 for (int j = 0; j < qualReverseMap[i].size(); j++) {
581 in >> tempNum; qualReverseMap[i][j] += tempNum;
588 for (int i = 0; i < errorForward.size(); i++) {
590 vector<int> thisErrorForward = errorForward[first];
592 for (int i = 0; i < thisErrorForward.size(); i++) {
593 in >> tempNum; thisErrorForward[i] += tempNum;
595 errorForward[first] = thisErrorForward;
601 for (int i = 0; i < errorReverse.size(); i++) {
603 vector<int> thisErrorReverse = errorReverse[first];
605 for (int i = 0; i < thisErrorReverse.size(); i++) {
606 in >> tempNum; thisErrorReverse[i] += tempNum;
608 errorReverse[first] = thisErrorReverse;
613 //input misMatchCounts
615 in >> misMatchSize; m->gobble(in);
616 if (misMatchSize > misMatchCounts.size()) { misMatchCounts.resize(misMatchSize, 0); }
617 for (int j = 0; j < misMatchCounts.size(); j++) {
618 in >> tempNum; misMatchCounts[j] += tempNum;
622 //input megaAlignVector
624 for (int j = 0; j < megaAlignVector.size(); j++) {
625 thisLine = m->getline(in); m->gobble(in); megaAlignVector[j] += thisLine + '\n';
629 in.close(); m->mothurRemove(tempFile);
635 catch(exception& e) {
636 m->errorOut(e, "SeqErrorCommand", "createProcesses");
640 //**********************************************************************************************************************
641 int SeqErrorCommand::driver(string filename, string qFileName, string rFileName, string summaryFileName, string errorOutputFileName, string chimeraOutputFileName, linePair line, linePair qline, linePair rline) {
645 QualityScores quality;
647 misMatchCounts.resize(11, 0);
651 map<string, int>::iterator it;
652 qScoreErrorMap['m'].assign(41, 0);
653 qScoreErrorMap['s'].assign(41, 0);
654 qScoreErrorMap['i'].assign(41, 0);
655 qScoreErrorMap['a'].assign(41, 0);
657 errorForward['m'].assign(maxLength,0);
658 errorForward['s'].assign(maxLength,0);
659 errorForward['i'].assign(maxLength,0);
660 errorForward['d'].assign(maxLength,0);
661 errorForward['a'].assign(maxLength,0);
663 errorReverse['m'].assign(maxLength,0);
664 errorReverse['s'].assign(maxLength,0);
665 errorReverse['i'].assign(maxLength,0);
666 errorReverse['d'].assign(maxLength,0);
667 errorReverse['a'].assign(maxLength,0);
669 //open inputfiles and go to beginning place for this processor
671 m->openInputFile(filename, queryFile);
672 queryFile.seekg(line.start);
676 if(qFileName != "" && rFileName != ""){
677 m->openInputFile(qFileName, qualFile);
678 qualFile.seekg(qline.start);
681 if (rline.start == 0) { report = ReportFile(reportFile, rFileName); }
683 m->openInputFile(rFileName, reportFile);
684 reportFile.seekg(rline.start);
687 qualForwardMap.resize(maxLength);
688 qualReverseMap.resize(maxLength);
689 for(int i=0;i<maxLength;i++){
690 qualForwardMap[i].assign(41,0);
691 qualReverseMap[i].assign(41,0);
695 ofstream outChimeraReport;
696 m->openOutputFile(chimeraOutputFileName, outChimeraReport);
698 RefChimeraTest chimeraTest(referenceSeqs, aligned);
700 if (line.start == 0) { chimeraTest.printHeader(outChimeraReport); }
702 ofstream errorSummaryFile;
703 m->openOutputFile(summaryFileName, errorSummaryFile);
704 if (line.start == 0) { printErrorHeader(errorSummaryFile); }
706 ofstream errorSeqFile;
707 m->openOutputFile(errorOutputFileName, errorSeqFile);
709 megaAlignVector.resize(numRefs, "");
717 if (m->control_pressed) { queryFile.close(); if(qFileName != "" && rFileName != ""){ reportFile.close(); qualFile.close(); } outChimeraReport.close(); errorSummaryFile.close();errorSeqFile.close(); return 0; }
719 Sequence query(queryFile);
721 int numParentSeqs = chimeraTest.analyzeQuery(query.getName(), query.getAligned(), outChimeraReport);
722 int closestRefIndex = chimeraTest.getClosestRefIndex();
724 if(numParentSeqs > 1 && ignoreChimeras == 1) { ignoreSeq = 1; }
725 else { ignoreSeq = 0; }
728 getErrors(query, referenceSeqs[closestRefIndex], minCompare);
730 if(namesFileName != ""){
731 it = weights.find(query.getName());
732 minCompare.weight = it->second;
734 else{ minCompare.weight = 1; }
736 printErrorData(minCompare, numParentSeqs, errorSummaryFile, errorSeqFile);
740 for(int i=0;i<minCompare.sequence.length();i++){
741 char letter = minCompare.sequence[i];
744 errorForward[letter][i] += minCompare.weight;
745 errorReverse[letter][minCompare.total-i-1] += minCompare.weight;
750 if(qualFileName != "" && reportFileName != ""){
751 report = ReportFile(reportFile);
753 // int origLength = report.getQueryLength();
754 int startBase = report.getQueryStart();
755 int endBase = report.getQueryEnd();
757 quality = QualityScores(qualFile);
760 // cout << query.getName() << '\t';
762 quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
763 quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
764 quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
770 if(minCompare.errorRate < threshold && !ignoreSeq){
771 totalBases += (minCompare.total * minCompare.weight);
772 totalMatches += minCompare.matches * minCompare.weight;
773 if(minCompare.mismatches > maxMismatch){
774 maxMismatch = minCompare.mismatches;
775 misMatchCounts.resize(maxMismatch + 1, 0);
777 misMatchCounts[minCompare.mismatches] += minCompare.weight;
780 megaAlignVector[closestRefIndex] += query.getInlineSeq() + '\n';
785 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
786 unsigned long long pos = queryFile.tellg();
787 if ((pos == -1) || (pos >= line.end)) { break; }
789 if (queryFile.eof()) { break; }
792 if(index % 100 == 0){ m->mothurOut(toString(index)); m->mothurOutEndLine(); }
795 if(qFileName != "" && rFileName != ""){ reportFile.close(); qualFile.close(); }
796 errorSummaryFile.close();
797 errorSeqFile.close();
800 if(index % 100 != 0){ m->mothurOut(toString(index)); m->mothurOutEndLine(); }
804 catch(exception& e) {
805 m->errorOut(e, "SeqErrorCommand", "driver");
809 //***************************************************************************************************************
811 void SeqErrorCommand::getReferences(){
813 int numAmbigSeqs = 0;
816 int minEndPos = 100000;
818 if (referenceFileName == "saved") {
819 int start = time(NULL);
820 m->mothurOutEndLine(); m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory."); m->mothurOutEndLine();
822 for (int i = 0; i < rdb->referenceSeqs.size(); i++) {
823 int numAmbigs = rdb->referenceSeqs[i].getAmbigBases();
824 if(numAmbigs > 0){ numAmbigSeqs++; }
826 // int startPos = rdb->referenceSeqs[i].getStartPos();
827 // if(startPos > maxStartPos) { maxStartPos = startPos; }
829 // int endPos = rdb->referenceSeqs[i].getEndPos();
830 // if(endPos < minEndPos) { minEndPos = endPos; }
831 if (rdb->referenceSeqs[i].getNumBases() == 0) {
832 m->mothurOut("[WARNING]: " + rdb->referenceSeqs[i].getName() + " is blank, ignoring.");m->mothurOutEndLine();
834 referenceSeqs.push_back(rdb->referenceSeqs[i]);
838 referenceFileName = rdb->getSavedReference();
840 m->mothurOut("It took " + toString(time(NULL) - start) + " to load " + toString(referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();
843 int start = time(NULL);
845 ifstream referenceFile;
846 m->openInputFile(referenceFileName, referenceFile);
848 while(referenceFile){
849 Sequence currentSeq(referenceFile);
850 int numAmbigs = currentSeq.getAmbigBases();
851 if(numAmbigs > 0){ numAmbigSeqs++; }
853 // int startPos = currentSeq.getStartPos();
854 // if(startPos > maxStartPos) { maxStartPos = startPos; }
856 // int endPos = currentSeq.getEndPos();
857 // if(endPos < minEndPos) { minEndPos = endPos; }
858 if (currentSeq.getNumBases() == 0) {
859 m->mothurOut("[WARNING]: " + currentSeq.getName() + " is blank, ignoring.");m->mothurOutEndLine();
861 referenceSeqs.push_back(currentSeq);
862 if (rdb->save) { rdb->referenceSeqs.push_back(currentSeq); }
865 m->gobble(referenceFile);
867 referenceFile.close();
869 m->mothurOut("It took " + toString(time(NULL) - start) + " to read " + toString(referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();
872 numRefs = referenceSeqs.size();
874 for(int i=0;i<numRefs;i++){
875 referenceSeqs[i].padToPos(maxStartPos);
876 referenceSeqs[i].padFromPos(minEndPos);
879 if(numAmbigSeqs != 0){
880 m->mothurOut("Warning: " + toString(numAmbigSeqs) + " reference sequences have ambiguous bases, these bases will be ignored\n");
884 catch(exception& e) {
885 m->errorOut(e, "SeqErrorCommand", "getReferences");
890 //***************************************************************************************************************
892 int SeqErrorCommand::getErrors(Sequence query, Sequence reference, Compare& errors){
894 if(query.getAlignLength() != reference.getAlignLength()){
895 m->mothurOut("Warning: " + toString(query.getName()) + " and " + toString(reference.getName()) + " are different lengths\n");
897 int alignLength = query.getAlignLength();
899 string q = query.getAligned();
900 string r = reference.getAligned();
905 for(int i=0;i<alignLength;i++){
906 // cout << r[i] << '\t' << q[i] << '\t';
907 if(q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){ // no missing data and no double gaps
912 if(r[i] == 'A'){ errors.AA++; errors.matches++; errors.sequence += 'm'; }
913 if(r[i] == 'T'){ errors.AT++; errors.sequence += 's'; }
914 if(r[i] == 'G'){ errors.AG++; errors.sequence += 's'; }
915 if(r[i] == 'C'){ errors.AC++; errors.sequence += 's'; }
916 if(r[i] == '-'){ errors.Ai++; errors.sequence += 'i'; }
918 else if(q[i] == 'T'){
919 if(r[i] == 'A'){ errors.TA++; errors.sequence += 's'; }
920 if(r[i] == 'T'){ errors.TT++; errors.matches++; errors.sequence += 'm'; }
921 if(r[i] == 'G'){ errors.TG++; errors.sequence += 's'; }
922 if(r[i] == 'C'){ errors.TC++; errors.sequence += 's'; }
923 if(r[i] == '-'){ errors.Ti++; errors.sequence += 'i'; }
925 else if(q[i] == 'G'){
926 if(r[i] == 'A'){ errors.GA++; errors.sequence += 's'; }
927 if(r[i] == 'T'){ errors.GT++; errors.sequence += 's'; }
928 if(r[i] == 'G'){ errors.GG++; errors.matches++; errors.sequence += 'm'; }
929 if(r[i] == 'C'){ errors.GC++; errors.sequence += 's'; }
930 if(r[i] == '-'){ errors.Gi++; errors.sequence += 'i'; }
932 else if(q[i] == 'C'){
933 if(r[i] == 'A'){ errors.CA++; errors.sequence += 's'; }
934 if(r[i] == 'T'){ errors.CT++; errors.sequence += 's'; }
935 if(r[i] == 'G'){ errors.CG++; errors.sequence += 's'; }
936 if(r[i] == 'C'){ errors.CC++; errors.matches++; errors.sequence += 'm'; }
937 if(r[i] == '-'){ errors.Ci++; errors.sequence += 'i'; }
939 else if(q[i] == 'N'){
940 if(r[i] == 'A'){ errors.NA++; errors.sequence += 'a'; }
941 if(r[i] == 'T'){ errors.NT++; errors.sequence += 'a'; }
942 if(r[i] == 'G'){ errors.NG++; errors.sequence += 'a'; }
943 if(r[i] == 'C'){ errors.NC++; errors.sequence += 'a'; }
944 if(r[i] == '-'){ errors.Ni++; errors.sequence += 'a'; }
946 else if(q[i] == '-' && r[i] != '-'){
947 if(r[i] == 'A'){ errors.dA++; errors.sequence += 'd'; }
948 if(r[i] == 'T'){ errors.dT++; errors.sequence += 'd'; }
949 if(r[i] == 'G'){ errors.dG++; errors.sequence += 'd'; }
950 if(r[i] == 'C'){ errors.dC++; errors.sequence += 'd'; }
957 errors.sequence += 'd'; errors.total++;
960 errors.sequence += 'r';
965 else if(q[i] == '.' && r[i] != '.'){ // reference extends beyond query
966 if(started == 1){ break; }
968 else if(q[i] != '.' && r[i] == '.'){ // query extends beyond reference
969 if(started == 1){ break; }
971 else if(q[i] == '.' && r[i] == '.'){ // both are missing data
972 if(started == 1){ break; }
974 // cout << errors.sequence[errors.sequence.length()-1] << endl;
976 // cout << errors.sequence << endl;
977 errors.mismatches = errors.total-errors.matches;
978 errors.errorRate = (double)(errors.total-errors.matches) / (double)errors.total;
979 errors.queryName = query.getName();
980 errors.refName = reference.getName();
984 catch(exception& e) {
985 m->errorOut(e, "SeqErrorCommand", "getErrors");
990 //***************************************************************************************************************
992 map<string, int> SeqErrorCommand::getWeights(){
994 m->openInputFile(namesFileName, nameFile);
997 string redundantSeqs;
998 map<string, int> nameCountMap;
1001 nameFile >> seqName >> redundantSeqs;
1002 nameCountMap[seqName] = m->getNumNames(redundantSeqs);
1003 m->gobble(nameFile);
1005 return nameCountMap;
1009 //***************************************************************************************************************
1011 void SeqErrorCommand::printErrorHeader(ofstream& errorSummaryFile){
1013 errorSummaryFile << "query\treference\tweight\t";
1014 errorSummaryFile << "AA\tAT\tAG\tAC\tTA\tTT\tTG\tTC\tGA\tGT\tGG\tGC\tCA\tCT\tCG\tCC\tNA\tNT\tNG\tNC\tAi\tTi\tGi\tCi\tNi\tdA\tdT\tdG\tdC\t";
1015 errorSummaryFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\tnumparents\n";
1017 errorSummaryFile << setprecision(6);
1018 errorSummaryFile.setf(ios::fixed);
1020 catch(exception& e) {
1021 m->errorOut(e, "SeqErrorCommand", "printErrorHeader");
1026 //***************************************************************************************************************
1028 void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs, ofstream& errorSummaryFile, ofstream& errorSeqFile){
1031 errorSummaryFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
1032 errorSummaryFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
1033 errorSummaryFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
1034 errorSummaryFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
1035 errorSummaryFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
1036 errorSummaryFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
1037 errorSummaryFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t';
1038 errorSummaryFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
1040 errorSummaryFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t'; //insertions
1041 errorSummaryFile << error.dA + error.dT + error.dG + error.dC << '\t'; //deletions
1042 errorSummaryFile << error.mismatches - (error.Ai + error.Ti + error.Gi + error.Ci) - (error.dA + error.dT + error.dG + error.dC) - (error.NA + error.NT + error.NG + error.NC + error.Ni) << '\t'; //substitutions
1043 errorSummaryFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t'; //ambiguities
1044 errorSummaryFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << '\t' << numParentSeqs << endl;
1046 errorSeqFile << '>' << error.queryName << "\tref:" << error.refName << '\n' << error.sequence << endl;
1048 int a=0; int t=1; int g=2; int c=3;
1051 if(numParentSeqs == 1 || ignoreChimeras == 0){
1052 substitutionMatrix[a][a] += error.weight * error.AA;
1053 substitutionMatrix[a][t] += error.weight * error.TA;
1054 substitutionMatrix[a][g] += error.weight * error.GA;
1055 substitutionMatrix[a][c] += error.weight * error.CA;
1056 substitutionMatrix[a][gap] += error.weight * error.dA;
1057 substitutionMatrix[a][n] += error.weight * error.NA;
1059 substitutionMatrix[t][a] += error.weight * error.AT;
1060 substitutionMatrix[t][t] += error.weight * error.TT;
1061 substitutionMatrix[t][g] += error.weight * error.GT;
1062 substitutionMatrix[t][c] += error.weight * error.CT;
1063 substitutionMatrix[t][gap] += error.weight * error.dT;
1064 substitutionMatrix[t][n] += error.weight * error.NT;
1066 substitutionMatrix[g][a] += error.weight * error.AG;
1067 substitutionMatrix[g][t] += error.weight * error.TG;
1068 substitutionMatrix[g][g] += error.weight * error.GG;
1069 substitutionMatrix[g][c] += error.weight * error.CG;
1070 substitutionMatrix[g][gap] += error.weight * error.dG;
1071 substitutionMatrix[g][n] += error.weight * error.NG;
1073 substitutionMatrix[c][a] += error.weight * error.AC;
1074 substitutionMatrix[c][t] += error.weight * error.TC;
1075 substitutionMatrix[c][g] += error.weight * error.GC;
1076 substitutionMatrix[c][c] += error.weight * error.CC;
1077 substitutionMatrix[c][gap] += error.weight * error.dC;
1078 substitutionMatrix[c][n] += error.weight * error.NC;
1080 substitutionMatrix[gap][a] += error.weight * error.Ai;
1081 substitutionMatrix[gap][t] += error.weight * error.Ti;
1082 substitutionMatrix[gap][g] += error.weight * error.Gi;
1083 substitutionMatrix[gap][c] += error.weight * error.Ci;
1084 substitutionMatrix[gap][n] += error.weight * error.Ni;
1087 catch(exception& e) {
1088 m->errorOut(e, "SeqErrorCommand", "printErrorData");
1093 //***************************************************************************************************************
1095 void SeqErrorCommand::printSubMatrix(){
1097 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
1098 map<string, string> variables;
1099 variables["[filename]"] = fileNameRoot;
1100 string subMatrixFileName = getOutputFileName("errormatrix",variables);
1101 ofstream subMatrixFile;
1102 m->openOutputFile(subMatrixFileName, subMatrixFile);
1103 outputNames.push_back(subMatrixFileName); outputTypes["errormatrix"].push_back(subMatrixFileName);
1104 vector<string> bases(6);
1111 vector<int> refSums(5,1);
1113 for(int i=0;i<5;i++){
1114 subMatrixFile << "\tr" << bases[i];
1116 for(int j=0;j<6;j++){
1117 refSums[i] += substitutionMatrix[i][j];
1120 subMatrixFile << endl;
1122 for(int i=0;i<6;i++){
1123 subMatrixFile << 'q' << bases[i];
1124 for(int j=0;j<5;j++){
1125 subMatrixFile << '\t' << substitutionMatrix[j][i];
1127 subMatrixFile << endl;
1130 subMatrixFile << "total";
1131 for(int i=0;i<5;i++){
1132 subMatrixFile << '\t' << refSums[i];
1134 subMatrixFile << endl;
1135 subMatrixFile.close();
1137 catch(exception& e) {
1138 m->errorOut(e, "SeqErrorCommand", "printSubMatrix");
1142 //***************************************************************************************************************
1144 void SeqErrorCommand::printErrorFRFile(map<char, vector<int> > errorForward, map<char, vector<int> > errorReverse){
1146 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
1147 map<string, string> variables;
1148 variables["[filename]"] = fileNameRoot;
1149 string errorForwardFileName = getOutputFileName("errorforward",variables);
1150 ofstream errorForwardFile;
1151 m->openOutputFile(errorForwardFileName, errorForwardFile);
1152 outputNames.push_back(errorForwardFileName); outputTypes["errorforward"].push_back(errorForwardFileName);
1154 errorForwardFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
1155 for(int i=0;i<maxLength;i++){
1156 float match = (float)errorForward['m'][i];
1157 float subst = (float)errorForward['s'][i];
1158 float insert = (float)errorForward['i'][i];
1159 float del = (float)errorForward['d'][i];
1160 float amb = (float)errorForward['a'][i];
1161 float total = match + subst + insert + del + amb;
1162 if(total == 0){ break; }
1163 errorForwardFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
1165 errorForwardFile.close();
1167 string errorReverseFileName = getOutputFileName("errorreverse",variables);
1168 ofstream errorReverseFile;
1169 m->openOutputFile(errorReverseFileName, errorReverseFile);
1170 outputNames.push_back(errorReverseFileName); outputTypes["errorreverse"].push_back(errorReverseFileName);
1172 errorReverseFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
1173 for(int i=0;i<maxLength;i++){
1174 float match = (float)errorReverse['m'][i];
1175 float subst = (float)errorReverse['s'][i];
1176 float insert = (float)errorReverse['i'][i];
1177 float del = (float)errorReverse['d'][i];
1178 float amb = (float)errorReverse['a'][i];
1179 float total = match + subst + insert + del + amb;
1180 if(total == 0){ break; }
1181 errorReverseFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
1183 errorReverseFile.close();
1185 catch(exception& e) {
1186 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
1191 //***************************************************************************************************************
1193 void SeqErrorCommand::printErrorQuality(map<char, vector<int> > qScoreErrorMap){
1195 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
1196 map<string, string> variables;
1197 variables["[filename]"] = fileNameRoot;
1198 string errorQualityFileName = getOutputFileName("errorquality",variables);
1199 ofstream errorQualityFile;
1200 m->openOutputFile(errorQualityFileName, errorQualityFile);
1201 outputNames.push_back(errorQualityFileName); outputTypes["errorquality"].push_back(errorQualityFileName);
1203 errorQualityFile << "qscore\tmatches\tsubstitutions\tinsertions\tambiguous" << endl;
1204 for(int i=0;i<41;i++){
1205 errorQualityFile << i << '\t' << qScoreErrorMap['m'][i] << '\t' << qScoreErrorMap['s'][i] << '\t' << qScoreErrorMap['i'][i] << '\t'<< qScoreErrorMap['a'][i] << endl;
1207 errorQualityFile.close();
1209 catch(exception& e) {
1210 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
1216 //***************************************************************************************************************
1218 void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector<vector<int> > qualReverseMap){
1222 int numColumns = qualForwardMap[0].size();
1224 for(int i=0;i<qualForwardMap.size();i++){
1225 for(int j=0;j<numColumns;j++){
1226 if(qualForwardMap[i][j] != 0){
1227 if(numRows < i) { numRows = i+20; }
1231 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
1232 map<string, string> variables;
1233 variables["[filename]"] = fileNameRoot;
1234 string qualityForwardFileName = getOutputFileName("errorqualforward",variables);
1235 ofstream qualityForwardFile;
1236 m->openOutputFile(qualityForwardFileName, qualityForwardFile);
1237 outputNames.push_back(qualityForwardFileName); outputTypes["errorqualforward"].push_back(qualityForwardFileName);
1239 for(int i=0;i<numColumns;i++){ qualityForwardFile << '\t' << i; } qualityForwardFile << endl;
1241 for(int i=0;i<numRows;i++){
1242 qualityForwardFile << i+1;
1243 for(int j=0;j<numColumns;j++){
1244 qualityForwardFile << '\t' << qualForwardMap[i][j];
1247 qualityForwardFile << endl;
1249 qualityForwardFile.close();
1252 string qualityReverseFileName = getOutputFileName("errorqualreverse",variables);
1253 ofstream qualityReverseFile;
1254 m->openOutputFile(qualityReverseFileName, qualityReverseFile);
1255 outputNames.push_back(qualityReverseFileName); outputTypes["errorqualreverse"].push_back(qualityReverseFileName);
1257 for(int i=0;i<numColumns;i++){ qualityReverseFile << '\t' << i; } qualityReverseFile << endl;
1258 for(int i=0;i<numRows;i++){
1260 qualityReverseFile << i+1;
1261 for(int j=0;j<numColumns;j++){
1262 qualityReverseFile << '\t' << qualReverseMap[i][j];
1264 qualityReverseFile << endl;
1266 qualityReverseFile.close();
1268 catch(exception& e) {
1269 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
1274 /**************************************************************************************************/
1276 int SeqErrorCommand::setLines(string filename, string qfilename, string rfilename, vector<unsigned long long>& fastaFilePos, vector<unsigned long long>& qfileFilePos, vector<unsigned long long>& rfileFilePos) {
1278 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1279 //set file positions for fasta file
1280 fastaFilePos = m->divideFile(filename, processors);
1282 if (qfilename == "") { return processors; }
1284 //get name of first sequence in each chunk
1285 map<string, int> firstSeqNames;
1286 for (int i = 0; i < (fastaFilePos.size()-1); i++) {
1288 m->openInputFile(filename, in);
1289 in.seekg(fastaFilePos[i]);
1292 firstSeqNames[temp.getName()] = i;
1297 //make copy to use below
1298 map<string, int> firstSeqNamesReport = firstSeqNames;
1300 //seach for filePos of each first name in the qfile and save in qfileFilePos
1302 m->openInputFile(qfilename, inQual);
1305 while(!inQual.eof()){
1306 input = m->getline(inQual);
1308 if (input.length() != 0) {
1309 if(input[0] == '>'){ //this is a sequence name line
1310 istringstream nameStream(input);
1312 string sname = ""; nameStream >> sname;
1313 sname = sname.substr(1);
1315 map<string, int>::iterator it = firstSeqNames.find(sname);
1317 if(it != firstSeqNames.end()) { //this is the start of a new chunk
1318 unsigned long long pos = inQual.tellg();
1319 qfileFilePos.push_back(pos - input.length() - 1);
1320 firstSeqNames.erase(it);
1325 if (firstSeqNames.size() == 0) { break; }
1329 if (firstSeqNames.size() != 0) {
1330 for (map<string, int>::iterator it = firstSeqNames.begin(); it != firstSeqNames.end(); it++) {
1331 m->mothurOut(it->first + " is in your fasta file and not in your quality file, aborting."); m->mothurOutEndLine();
1333 m->control_pressed = true;
1337 //get last file position of qfile
1339 unsigned long long size;
1341 //get num bytes in file
1342 pFile = fopen (qfilename.c_str(),"rb");
1343 if (pFile==NULL) perror ("Error opening file");
1345 fseek (pFile, 0, SEEK_END);
1350 qfileFilePos.push_back(size);
1352 //seach for filePos of each first name in the rfile and save in rfileFilePos
1355 m->openInputFile(rfilename, inR);
1357 //read column headers
1358 for (int i = 0; i < 16; i++) {
1359 if (!inR.eof()) { inR >> junk; }
1365 if (m->control_pressed) { inR.close(); return processors; }
1367 input = m->getline(inR);
1369 if (input.length() != 0) {
1371 istringstream nameStream(input);
1372 string sname = ""; nameStream >> sname;
1374 map<string, int>::iterator it = firstSeqNamesReport.find(sname);
1376 if(it != firstSeqNamesReport.end()) { //this is the start of a new chunk
1377 unsigned long long pos = inR.tellg();
1378 rfileFilePos.push_back(pos - input.length() - 1);
1379 firstSeqNamesReport.erase(it);
1383 if (firstSeqNamesReport.size() == 0) { break; }
1388 if (firstSeqNamesReport.size() != 0) {
1389 for (map<string, int>::iterator it = firstSeqNamesReport.begin(); it != firstSeqNamesReport.end(); it++) {
1390 m->mothurOut(it->first + " is in your fasta file and not in your report file, aborting."); m->mothurOutEndLine();
1392 m->control_pressed = true;
1396 //get last file position of qfile
1398 unsigned long long sizeR;
1400 //get num bytes in file
1401 rFile = fopen (rfilename.c_str(),"rb");
1402 if (rFile==NULL) perror ("Error opening file");
1404 fseek (rFile, 0, SEEK_END);
1405 sizeR=ftell (rFile);
1409 rfileFilePos.push_back(sizeR);
1415 fastaFilePos.push_back(0); qfileFilePos.push_back(0);
1416 //get last file position of fastafile
1418 unsigned long long size;
1420 //get num bytes in file
1421 pFile = fopen (filename.c_str(),"rb");
1422 if (pFile==NULL) perror ("Error opening file");
1424 fseek (pFile, 0, SEEK_END);
1428 fastaFilePos.push_back(size);
1430 //get last file position of fastafile
1433 //get num bytes in file
1434 qFile = fopen (qfilename.c_str(),"rb");
1435 if (qFile==NULL) perror ("Error opening file");
1437 fseek (qFile, 0, SEEK_END);
1441 qfileFilePos.push_back(size);
1447 catch(exception& e) {
1448 m->errorOut(e, "SeqErrorCommand", "setLines");
1452 //***************************************************************************************************************