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 "myPerseus.h"
15 #include "filterseqscommand.h"
18 //**********************************************************************************************************************
20 vector<string> SeqErrorCommand::setParameters(){
22 CommandParameter pquery("fasta", "InputTypes", "", "", "none", "none", "none","errorType",false,true,true); parameters.push_back(pquery);
23 CommandParameter preference("reference", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(preference);
24 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "QualReport","",false,false); parameters.push_back(pqfile);
25 CommandParameter preport("report", "InputTypes", "", "", "none", "none", "QualReport","",false,false); parameters.push_back(preport);
26 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(pname);
27 CommandParameter pignorechimeras("ignorechimeras", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pignorechimeras);
28 CommandParameter pthreshold("threshold", "Number", "", "1.0", "", "", "","",false,false); parameters.push_back(pthreshold);
29 CommandParameter paligned("aligned", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(paligned);
30 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
31 CommandParameter psave("save", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(psave);
32 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
33 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
35 vector<string> myArray;
36 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
40 m->errorOut(e, "SeqErrorCommand", "setParameters");
45 //**********************************************************************************************************************
47 string SeqErrorCommand::getHelpString(){
49 string helpString = "";
50 helpString += "The seq.error command reads a query alignment file and a reference alignment file and creates .....\n";
51 helpString += "The fasta parameter...\n";
52 helpString += "The reference parameter...\n";
53 helpString += "The qfile parameter...\n";
54 helpString += "The report parameter...\n";
55 helpString += "The name parameter...\n";
56 helpString += "The ignorechimeras parameter...\n";
57 helpString += "The threshold parameter...\n";
58 helpString += "The processors parameter...\n";
59 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.";
60 helpString += "Example seq.error(...).\n";
61 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
62 helpString += "For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n";
66 m->errorOut(e, "SeqErrorCommand", "getHelpString");
71 //**********************************************************************************************************************
73 string SeqErrorCommand::getOutputPattern(string type) {
77 if (type == "errorsummary") { pattern = "[filename],error.summary"; }
78 else if (type == "errorseq") { pattern = "[filename],error.seq"; }
79 else if (type == "errorquality") { pattern = "[filename],error.quality"; }
80 else if (type == "errorqualforward") { pattern = "[filename],error.qual.forward"; }
81 else if (type == "errorqualreverse") { pattern = "[filename],error.qual.reverse"; }
82 else if (type == "errorforward") { pattern = "[filename],error.seq.forward"; }
83 else if (type == "errorreverse") { pattern = "[filename],error.seq.reverse"; }
84 else if (type == "errorcount") { pattern = "[filename],error.count"; }
85 else if (type == "errormatrix") { pattern = "[filename],error.matrix"; }
86 else if (type == "errorchimera") { pattern = "[filename],error.chimera"; }
87 else if (type == "errorref-query") { pattern = "[filename],error.ref-query"; }
88 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
93 m->errorOut(e, "SeqErrorCommand", "getOutputPattern");
98 //**********************************************************************************************************************
100 SeqErrorCommand::SeqErrorCommand(){
102 abort = true; calledHelp = true;
104 vector<string> tempOutNames;
105 outputTypes["errorsummary"] = tempOutNames;
106 outputTypes["errorseq"] = tempOutNames;
107 outputTypes["errorquality"] = tempOutNames;
108 outputTypes["errorqualforward"] = tempOutNames;
109 outputTypes["errorqualreverse"] = tempOutNames;
110 outputTypes["errorforward"] = tempOutNames;
111 outputTypes["errorreverse"] = tempOutNames;
112 outputTypes["errorcount"] = tempOutNames;
113 outputTypes["errormatrix"] = tempOutNames;
114 outputTypes["errorchimera"] = tempOutNames;
115 outputTypes["errorref-query"] = tempOutNames;
117 catch(exception& e) {
118 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
123 //***************************************************************************************************************
125 SeqErrorCommand::SeqErrorCommand(string option) {
128 abort = false; calledHelp = false;
129 rdb = ReferenceDB::getInstance();
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;}
137 vector<string> myArray = setParameters();
139 OptionParser parser(option);
140 map<string,string> parameters = parser.getParameters();
142 ValidParameters validParameter;
143 map<string,string>::iterator it;
145 //check to make sure all parameters are valid for command
146 for (it = parameters.begin(); it != parameters.end(); it++) {
147 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
150 //initialize outputTypes
151 vector<string> tempOutNames;
152 outputTypes["errorsummary"] = tempOutNames;
153 outputTypes["errorseq"] = tempOutNames;
154 outputTypes["errorquality"] = tempOutNames;
155 outputTypes["errorqualforward"] = tempOutNames;
156 outputTypes["errorqualreverse"] = tempOutNames;
157 outputTypes["errorforward"] = tempOutNames;
158 outputTypes["errorreverse"] = tempOutNames;
159 outputTypes["errorcount"] = tempOutNames;
160 outputTypes["errormatrix"] = tempOutNames;
161 outputTypes["errorchimera"] = tempOutNames;
162 outputTypes["errorref-query"] = tempOutNames;
165 //if the user changes the input directory command factory will send this info to us in the output parameter
166 string inputDir = validParameter.validFile(parameters, "inputdir", false);
167 if (inputDir == "not found"){ inputDir = ""; }
170 it = parameters.find("fasta");
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["fasta"] = inputDir + it->second; }
178 it = parameters.find("reference");
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["reference"] = inputDir + it->second; }
186 it = parameters.find("name");
187 //user has given a names 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["name"] = inputDir + it->second; }
194 it = parameters.find("qfile");
195 //user has given a quality score 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["qfile"] = inputDir + it->second; }
202 it = parameters.find("report");
203 //user has given a alignment report 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["report"] = inputDir + it->second; }
211 //check for required parameters
212 queryFileName = validParameter.validFile(parameters, "fasta", true);
213 if (queryFileName == "not found") {
214 queryFileName = m->getFastaFile();
215 if (queryFileName != "") { m->mothurOut("Using " + queryFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
216 else { m->mothurOut("You have no current fasta file and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
218 else if (queryFileName == "not open") { queryFileName = ""; abort = true; }
219 else { m->setFastaFile(queryFileName); }
221 referenceFileName = validParameter.validFile(parameters, "reference", true);
222 if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
223 else if (referenceFileName == "not open") { abort = true; }
225 //check for optional parameters
226 namesFileName = validParameter.validFile(parameters, "name", true);
227 if(namesFileName == "not found"){ namesFileName = ""; }
228 else if (namesFileName == "not open") { namesFileName = ""; abort = true; }
229 else { m->setNameFile(namesFileName); }
231 qualFileName = validParameter.validFile(parameters, "qfile", true);
232 if(qualFileName == "not found"){ qualFileName = ""; }
233 else if (qualFileName == "not open") { qualFileName = ""; abort = true; }
234 else { m->setQualFile(qualFileName); }
236 reportFileName = validParameter.validFile(parameters, "report", true);
237 if(reportFileName == "not found"){ reportFileName = ""; }
238 else if (reportFileName == "not open") { reportFileName = ""; abort = true; }
240 outputDir = validParameter.validFile(parameters, "outputdir", false);
241 if (outputDir == "not found"){
243 outputDir += m->hasPath(queryFileName); //if user entered a file with a path then preserve it
246 //check for optional parameter and set defaults
247 // ...at some point should added some additional type checking...
248 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found") { temp = "1.00"; }
249 m->mothurConvert(temp, threshold);
252 temp = validParameter.validFile(parameters, "save", false); if (temp == "not found"){ temp = "f"; }
253 save = m->isTrue(temp);
255 if (save) { //clear out old references
259 //this has to go after save so that if the user sets save=t and provides no reference we abort
260 referenceFileName = validParameter.validFile(parameters, "reference", true);
261 if (referenceFileName == "not found") {
262 //check for saved reference sequences
263 if (rdb->referenceSeqs.size() != 0) {
264 referenceFileName = "saved";
266 m->mothurOut("[ERROR]: You don't have any saved reference sequences and the reference parameter is a required.");
267 m->mothurOutEndLine();
270 }else if (referenceFileName == "not open") { abort = true; }
271 else { if (save) { rdb->setSavedReference(referenceFileName); } }
274 temp = validParameter.validFile(parameters, "ignorechimeras", false); if (temp == "not found") { temp = "T"; }
275 ignoreChimeras = m->isTrue(temp);
277 temp = validParameter.validFile(parameters, "aligned", false); if (temp == "not found"){ temp = "t"; }
278 aligned = 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 if((reportFileName != "" && qualFileName == "") || (reportFileName == "" && qualFileName != "")){
294 m->mothurOut("if you use either a qual file or a report file, you have to have both.");
295 m->mothurOutEndLine();
300 if(reportFileName != ""){
301 m->mothurOut("we are ignoring the report file if your sequences are not aligned. we will check that the sequences in your fasta and and qual fileare the same length.");
302 m->mothurOutEndLine();
309 catch(exception& e) {
310 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
315 //***************************************************************************************************************
317 int SeqErrorCommand::execute(){
319 if (abort == true) { if (calledHelp) { return 0; } return 2; }
321 int start = time(NULL);
326 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
327 map<string, string> variables;
328 variables["[filename]"] = fileNameRoot;
329 string errorSummaryFileName = getOutputFileName("errorsummary",variables);
330 outputNames.push_back(errorSummaryFileName); outputTypes["errorsummary"].push_back(errorSummaryFileName);
332 string errorSeqFileName = getOutputFileName("errorseq",variables);
333 outputNames.push_back(errorSeqFileName); outputTypes["errorseq"].push_back(errorSeqFileName);
335 string errorChimeraFileName = getOutputFileName("errorchimera",variables);
336 outputNames.push_back(errorChimeraFileName); outputTypes["errorchimera"].push_back(errorChimeraFileName);
338 getReferences(); //read in reference sequences - make sure there's no ambiguous bases
340 if(namesFileName != ""){ weights = getWeights(); }
342 vector<unsigned long long> fastaFilePos;
343 vector<unsigned long long> qFilePos;
344 vector<unsigned long long> reportFilePos;
346 setLines(queryFileName, qualFileName, reportFileName, fastaFilePos, qFilePos, reportFilePos);
348 if (m->control_pressed) { return 0; }
350 for (int i = 0; i < (fastaFilePos.size()-1); i++) {
351 lines.push_back(linePair(fastaFilePos[i], fastaFilePos[(i+1)]));
352 if (qualFileName != "") { qLines.push_back(linePair(qFilePos[i], qFilePos[(i+1)])); }
353 if (reportFileName != "" && aligned == true) { rLines.push_back(linePair(reportFilePos[i], reportFilePos[(i+1)])); }
355 if(qualFileName == "") { qLines = lines; rLines = lines; } //fills with duds
356 if(aligned == false){ rLines = lines; }
358 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
360 numSeqs = driver(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName, lines[0], qLines[0], rLines[0]);
363 numSeqs = createProcesses(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName);
366 numSeqs = driver(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName, lines[0], qLines[0], rLines[0]);
369 if(qualFileName != ""){
370 printErrorQuality(qScoreErrorMap);
371 printQualityFR(qualForwardMap, qualReverseMap);
374 printErrorFRFile(errorForward, errorReverse);
376 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
378 string errorCountFileName = getOutputFileName("errorcount",variables);
379 ofstream errorCountFile;
380 m->openOutputFile(errorCountFileName, errorCountFile);
381 outputNames.push_back(errorCountFileName); outputTypes["errorcount"].push_back(errorCountFileName);
382 m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n");
383 m->mothurOut("Errors\tSequences\n");
384 errorCountFile << "Errors\tSequences\n";
385 for(int i=0;i<misMatchCounts.size();i++){
386 m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
387 errorCountFile << i << '\t' << misMatchCounts[i] << endl;
389 errorCountFile.close();
391 // if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
395 string megAlignmentFileName = getOutputFileName("errorref-query",variables);
396 ofstream megAlignmentFile;
397 m->openOutputFile(megAlignmentFileName, megAlignmentFile);
398 outputNames.push_back(megAlignmentFileName); outputTypes["errorref-query"].push_back(megAlignmentFileName);
400 for(int i=0;i<numRefs;i++){
401 megAlignmentFile << referenceSeqs[i].getInlineSeq() << endl;
402 megAlignmentFile << megaAlignVector[i] << endl;
404 megAlignmentFile.close();
407 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences.");
408 m->mothurOutEndLine();
410 m->mothurOutEndLine();
411 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
412 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
413 m->mothurOutEndLine();
417 catch(exception& e) {
418 m->errorOut(e, "SeqErrorCommand", "execute");
423 //**********************************************************************************************************************
425 int SeqErrorCommand::createProcesses(string filename, string qFileName, string rFileName, string summaryFileName, string errorOutputFileName, string chimeraOutputFileName) {
429 map<char, vector<int> >::iterator it;
431 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
433 //loop through and create all the processes you want
434 while (process != processors) {
438 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
442 num = driver(filename, qFileName, rFileName, summaryFileName + toString(getpid()) + ".temp", errorOutputFileName+ toString(getpid()) + ".temp", chimeraOutputFileName + toString(getpid()) + ".temp", lines[process], qLines[process], rLines[process]);
444 //pass groupCounts to parent
446 string tempFile = filename + toString(getpid()) + ".info.temp";
447 m->openOutputFile(tempFile, out);
449 //output totalBases and totalMatches
450 out << num << '\t' << totalBases << '\t' << totalMatches << endl << endl;
452 //output substitutionMatrix
453 for(int i = 0; i < substitutionMatrix.size(); i++) {
454 for (int j = 0; j < substitutionMatrix[i].size(); j++) {
455 out << substitutionMatrix[i][j] << '\t';
461 //output qScoreErrorMap
462 for (it = qScoreErrorMap.begin(); it != qScoreErrorMap.end(); it++) {
463 vector<int> thisScoreErrorMap = it->second;
464 out << it->first << '\t';
465 for (int i = 0; i < thisScoreErrorMap.size(); i++) {
466 out << thisScoreErrorMap[i] << '\t';
472 //output qualForwardMap
473 for(int i = 0; i < qualForwardMap.size(); i++) {
474 for (int j = 0; j < qualForwardMap[i].size(); j++) {
475 out << qualForwardMap[i][j] << '\t';
481 //output qualReverseMap
482 for(int i = 0; i < qualReverseMap.size(); i++) {
483 for (int j = 0; j < qualReverseMap[i].size(); j++) {
484 out << qualReverseMap[i][j] << '\t';
491 //output errorForward
492 for (it = errorForward.begin(); it != errorForward.end(); it++) {
493 vector<int> thisErrorForward = it->second;
494 out << it->first << '\t';
495 for (int i = 0; i < thisErrorForward.size(); i++) {
496 out << thisErrorForward[i] << '\t';
502 //output errorReverse
503 for (it = errorReverse.begin(); it != errorReverse.end(); it++) {
504 vector<int> thisErrorReverse = it->second;
505 out << it->first << '\t';
506 for (int i = 0; i < thisErrorReverse.size(); i++) {
507 out << thisErrorReverse[i] << '\t';
513 //output misMatchCounts
514 out << misMatchCounts.size() << endl;
515 for (int j = 0; j < misMatchCounts.size(); j++) {
516 out << misMatchCounts[j] << '\t';
521 //output megaAlignVector
522 for (int j = 0; j < megaAlignVector.size(); j++) {
523 out << megaAlignVector[j] << endl;
531 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
532 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
538 num = driver(filename, qFileName, rFileName, summaryFileName, errorOutputFileName, chimeraOutputFileName, lines[0], qLines[0], rLines[0]);
540 //force parent to wait until all the processes are done
541 for (int i=0;i<processIDS.size();i++) {
542 int temp = processIDS[i];
547 for(int i=0;i<processIDS.size();i++){
549 m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
551 m->appendFiles((summaryFileName + toString(processIDS[i]) + ".temp"), summaryFileName);
552 m->mothurRemove((summaryFileName + toString(processIDS[i]) + ".temp"));
553 m->appendFiles((errorOutputFileName + toString(processIDS[i]) + ".temp"), errorOutputFileName);
554 m->mothurRemove((errorOutputFileName + toString(processIDS[i]) + ".temp"));
555 m->appendFiles((chimeraOutputFileName + toString(processIDS[i]) + ".temp"), chimeraOutputFileName);
556 m->mothurRemove((chimeraOutputFileName + toString(processIDS[i]) + ".temp"));
559 string tempFile = filename + toString(processIDS[i]) + ".info.temp";
560 m->openInputFile(tempFile, in);
562 //input totalBases and totalMatches
563 int tempBases, tempMatches, tempNumSeqs;
564 in >> tempNumSeqs >> tempBases >> tempMatches; m->gobble(in);
565 totalBases += tempBases; totalMatches += tempMatches; num += tempNumSeqs;
567 //input substitutionMatrix
569 for(int i = 0; i < substitutionMatrix.size(); i++) {
570 for (int j = 0; j < substitutionMatrix[i].size(); j++) {
571 in >> tempNum; substitutionMatrix[i][j] += tempNum;
577 //input qScoreErrorMap
579 for (int i = 0; i < qScoreErrorMap.size(); i++) {
581 vector<int> thisScoreErrorMap = qScoreErrorMap[first];
583 for (int i = 0; i < thisScoreErrorMap.size(); i++) {
584 in >> tempNum; thisScoreErrorMap[i] += tempNum;
586 qScoreErrorMap[first] = thisScoreErrorMap;
591 //input qualForwardMap
592 for(int i = 0; i < qualForwardMap.size(); i++) {
593 for (int j = 0; j < qualForwardMap[i].size(); j++) {
594 in >> tempNum; qualForwardMap[i][j] += tempNum;
600 //input qualReverseMap
601 for(int i = 0; i < qualReverseMap.size(); i++) {
602 for (int j = 0; j < qualReverseMap[i].size(); j++) {
603 in >> tempNum; qualReverseMap[i][j] += tempNum;
610 for (int i = 0; i < errorForward.size(); i++) {
612 vector<int> thisErrorForward = errorForward[first];
614 for (int i = 0; i < thisErrorForward.size(); i++) {
615 in >> tempNum; thisErrorForward[i] += tempNum;
617 errorForward[first] = thisErrorForward;
623 for (int i = 0; i < errorReverse.size(); i++) {
625 vector<int> thisErrorReverse = errorReverse[first];
627 for (int i = 0; i < thisErrorReverse.size(); i++) {
628 in >> tempNum; thisErrorReverse[i] += tempNum;
630 errorReverse[first] = thisErrorReverse;
635 //input misMatchCounts
637 in >> misMatchSize; m->gobble(in);
638 if (misMatchSize > misMatchCounts.size()) { misMatchCounts.resize(misMatchSize, 0); }
639 for (int j = 0; j < misMatchCounts.size(); j++) {
640 in >> tempNum; misMatchCounts[j] += tempNum;
644 //input megaAlignVector
646 for (int j = 0; j < megaAlignVector.size(); j++) {
647 thisLine = m->getline(in); m->gobble(in); megaAlignVector[j] += thisLine + '\n';
651 in.close(); m->mothurRemove(tempFile);
657 catch(exception& e) {
658 m->errorOut(e, "SeqErrorCommand", "createProcesses");
663 //**********************************************************************************************************************
665 int SeqErrorCommand::driver(string filename, string qFileName, string rFileName, string summaryFileName, string errorOutputFileName, string chimeraOutputFileName, linePair line, linePair qline, linePair rline) {
669 QualityScores quality;
671 misMatchCounts.resize(11, 0);
675 map<string, int>::iterator it;
676 qScoreErrorMap['m'].assign(41, 0);
677 qScoreErrorMap['s'].assign(41, 0);
678 qScoreErrorMap['i'].assign(41, 0);
679 qScoreErrorMap['a'].assign(41, 0);
681 errorForward['m'].assign(maxLength,0);
682 errorForward['s'].assign(maxLength,0);
683 errorForward['i'].assign(maxLength,0);
684 errorForward['d'].assign(maxLength,0);
685 errorForward['a'].assign(maxLength,0);
687 errorReverse['m'].assign(maxLength,0);
688 errorReverse['s'].assign(maxLength,0);
689 errorReverse['i'].assign(maxLength,0);
690 errorReverse['d'].assign(maxLength,0);
691 errorReverse['a'].assign(maxLength,0);
693 //open inputfiles and go to beginning place for this processor
695 m->openInputFile(filename, queryFile);
697 queryFile.seekg(line.start);
701 if((qFileName != "" && rFileName != "" && aligned)){
702 m->openInputFile(qFileName, qualFile);
703 qualFile.seekg(qline.start);
706 if (rline.start == 0) { report = ReportFile(reportFile, rFileName); }
708 m->openInputFile(rFileName, reportFile);
709 reportFile.seekg(rline.start);
712 qualForwardMap.resize(maxLength);
713 qualReverseMap.resize(maxLength);
714 for(int i=0;i<maxLength;i++){
715 qualForwardMap[i].assign(41,0);
716 qualReverseMap[i].assign(41,0);
719 else if(qFileName != "" && !aligned){
721 m->openInputFile(qFileName, qualFile);
722 qualFile.seekg(qline.start);
724 qualForwardMap.resize(maxLength);
725 qualReverseMap.resize(maxLength);
726 for(int i=0;i<maxLength;i++){
727 qualForwardMap[i].assign(41,0);
728 qualReverseMap[i].assign(41,0);
732 ofstream outChimeraReport;
733 m->openOutputFile(chimeraOutputFileName, outChimeraReport);
736 RefChimeraTest chimeraTest;
738 chimeraTest = RefChimeraTest(referenceSeqs, aligned);
739 if (line.start == 0) { chimeraTest.printHeader(outChimeraReport); }
742 ofstream errorSummaryFile;
743 m->openOutputFile(summaryFileName, errorSummaryFile);
744 if (line.start == 0) { printErrorHeader(errorSummaryFile); }
746 ofstream errorSeqFile;
747 m->openOutputFile(errorOutputFileName, errorSeqFile);
749 megaAlignVector.assign(numRefs, "");
757 Sequence query(queryFile);
759 int numParentSeqs = -1;
760 int closestRefIndex = -1;
762 numParentSeqs = chimeraTest.analyzeQuery(query.getName(), query.getAligned(), outChimeraReport);
764 closestRefIndex = chimeraTest.getClosestRefIndex();
766 reference = referenceSeqs[closestRefIndex];
768 reference.setAligned(chimeraTest.getClosestRefAlignment());
769 query.setAligned(chimeraTest.getQueryAlignment());
771 if(numParentSeqs > 1 && ignoreChimeras == 1) { ignoreSeq = 1; }
772 else { ignoreSeq = 0; }
776 getErrors(query, reference, minCompare);
778 if(namesFileName != ""){
779 it = weights.find(query.getName());
780 minCompare.weight = it->second;
782 else{ minCompare.weight = 1; }
785 printErrorData(minCompare, numParentSeqs, errorSummaryFile, errorSeqFile);
788 for(int i=0;i<minCompare.sequence.length();i++){
789 char letter = minCompare.sequence[i];
791 errorForward[letter][i] += minCompare.weight;
792 errorReverse[letter][minCompare.total-i-1] += minCompare.weight;
797 if(aligned && qualFileName != "" && reportFileName != ""){
798 report = ReportFile(reportFile);
800 // int origLength = report.getQueryLength();
801 int startBase = report.getQueryStart();
802 int endBase = report.getQueryEnd();
804 quality = QualityScores(qualFile);
807 quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
808 quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
809 quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
812 else if(aligned == false && qualFileName != ""){
814 quality = QualityScores(qualFile);
815 int qualityLength = quality.getLength();
817 if(qualityLength != query.getNumBases()){ cout << "warning - quality and fasta sequence files do not match at " << query.getName() << '\t' << qualityLength <<'\t' << query.getNumBases() << endl; }
820 int endBase = qualityLength;
823 quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
824 quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
825 quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
829 if(minCompare.errorRate <= threshold && !ignoreSeq){
830 totalBases += (minCompare.total * minCompare.weight);
831 totalMatches += minCompare.matches * minCompare.weight;
832 if(minCompare.mismatches > maxMismatch){
833 maxMismatch = minCompare.mismatches;
834 misMatchCounts.resize(maxMismatch + 1, 0);
836 misMatchCounts[minCompare.mismatches] += minCompare.weight;
839 megaAlignVector[closestRefIndex] += query.getInlineSeq() + '\n';
844 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
845 unsigned long long pos = queryFile.tellg();
846 if ((pos == -1) || (pos >= line.end)) { break; }
848 if (queryFile.eof()) { break; }
851 if(index % 100 == 0){ m->mothurOut(toString(index)); m->mothurOutEndLine(); }
854 outChimeraReport.close();
855 errorSummaryFile.close();
856 errorSeqFile.close();
858 if(qFileName != "" && rFileName != "") { reportFile.close(); qualFile.close(); }
859 else if(qFileName != "" && aligned == false){ qualFile.close(); }
862 m->mothurOut(toString(index)); m->mothurOutEndLine();
866 catch(exception& e) {
867 m->errorOut(e, "SeqErrorCommand", "driver");
872 //***************************************************************************************************************
874 void SeqErrorCommand::getReferences(){
876 int numAmbigSeqs = 0;
879 int minEndPos = 100000;
881 if (referenceFileName == "saved") {
882 int start = time(NULL);
883 m->mothurOutEndLine(); m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory."); m->mothurOutEndLine();
885 for (int i = 0; i < rdb->referenceSeqs.size(); i++) {
886 int numAmbigs = rdb->referenceSeqs[i].getAmbigBases();
887 if(numAmbigs > 0){ numAmbigSeqs++; }
889 // int startPos = rdb->referenceSeqs[i].getStartPos();
890 // if(startPos > maxStartPos) { maxStartPos = startPos; }
892 // int endPos = rdb->referenceSeqs[i].getEndPos();
893 // if(endPos < minEndPos) { minEndPos = endPos; }
894 if (rdb->referenceSeqs[i].getNumBases() == 0) {
895 m->mothurOut("[WARNING]: " + rdb->referenceSeqs[i].getName() + " is blank, ignoring.");m->mothurOutEndLine();
897 referenceSeqs.push_back(rdb->referenceSeqs[i]);
901 referenceFileName = rdb->getSavedReference();
903 m->mothurOut("It took " + toString(time(NULL) - start) + " to load " + toString(referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();
906 int start = time(NULL);
908 ifstream referenceFile;
909 m->openInputFile(referenceFileName, referenceFile);
911 while(referenceFile){
912 Sequence currentSeq(referenceFile);
913 int numAmbigs = currentSeq.getAmbigBases();
914 if(numAmbigs > 0){ numAmbigSeqs++; }
916 // int startPos = currentSeq.getStartPos();
917 // if(startPos > maxStartPos) { maxStartPos = startPos; }
919 // int endPos = currentSeq.getEndPos();
920 // if(endPos < minEndPos) { minEndPos = endPos; }
921 if (currentSeq.getNumBases() == 0) {
922 m->mothurOut("[WARNING]: " + currentSeq.getName() + " is blank, ignoring.");m->mothurOutEndLine();
924 referenceSeqs.push_back(currentSeq);
925 if (rdb->save) { rdb->referenceSeqs.push_back(currentSeq); }
928 m->gobble(referenceFile);
930 referenceFile.close();
932 m->mothurOut("It took " + toString(time(NULL) - start) + " to read " + toString(referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();
935 numRefs = referenceSeqs.size();
937 for(int i=0;i<numRefs;i++){
938 referenceSeqs[i].padToPos(maxStartPos);
939 referenceSeqs[i].padFromPos(minEndPos);
942 if(numAmbigSeqs != 0){
943 m->mothurOut("Warning: " + toString(numAmbigSeqs) + " reference sequences have ambiguous bases, these bases will be ignored\n");
947 catch(exception& e) {
948 m->errorOut(e, "SeqErrorCommand", "getReferences");
953 //***************************************************************************************************************
955 int SeqErrorCommand::getErrors(Sequence query, Sequence reference, Compare& errors){
957 if(query.getAlignLength() != reference.getAlignLength()){
958 m->mothurOut("Warning: " + toString(query.getName()) + " and " + toString(reference.getName()) + " are different lengths\n");
960 int alignLength = query.getAlignLength();
962 string q = query.getAligned();
963 string r = reference.getAligned();
968 errors.sequence = "";
969 for(int i=0;i<alignLength;i++){
971 if(q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){ // no missing data and no double gaps
976 if(r[i] == 'A'){ errors.AA++; errors.matches++; errors.sequence += 'm'; }
977 if(r[i] == 'T'){ errors.AT++; errors.sequence += 's'; }
978 if(r[i] == 'G'){ errors.AG++; errors.sequence += 's'; }
979 if(r[i] == 'C'){ errors.AC++; errors.sequence += 's'; }
980 if(r[i] == '-'){ errors.Ai++; errors.sequence += 'i'; }
982 else if(q[i] == 'T'){
983 if(r[i] == 'A'){ errors.TA++; errors.sequence += 's'; }
984 if(r[i] == 'T'){ errors.TT++; errors.matches++; errors.sequence += 'm'; }
985 if(r[i] == 'G'){ errors.TG++; errors.sequence += 's'; }
986 if(r[i] == 'C'){ errors.TC++; errors.sequence += 's'; }
987 if(r[i] == '-'){ errors.Ti++; errors.sequence += 'i'; }
989 else if(q[i] == 'G'){
990 if(r[i] == 'A'){ errors.GA++; errors.sequence += 's'; }
991 if(r[i] == 'T'){ errors.GT++; errors.sequence += 's'; }
992 if(r[i] == 'G'){ errors.GG++; errors.matches++; errors.sequence += 'm'; }
993 if(r[i] == 'C'){ errors.GC++; errors.sequence += 's'; }
994 if(r[i] == '-'){ errors.Gi++; errors.sequence += 'i'; }
996 else if(q[i] == 'C'){
997 if(r[i] == 'A'){ errors.CA++; errors.sequence += 's'; }
998 if(r[i] == 'T'){ errors.CT++; errors.sequence += 's'; }
999 if(r[i] == 'G'){ errors.CG++; errors.sequence += 's'; }
1000 if(r[i] == 'C'){ errors.CC++; errors.matches++; errors.sequence += 'm'; }
1001 if(r[i] == '-'){ errors.Ci++; errors.sequence += 'i'; }
1003 else if(q[i] == 'N'){
1004 if(r[i] == 'A'){ errors.NA++; errors.sequence += 'a'; }
1005 if(r[i] == 'T'){ errors.NT++; errors.sequence += 'a'; }
1006 if(r[i] == 'G'){ errors.NG++; errors.sequence += 'a'; }
1007 if(r[i] == 'C'){ errors.NC++; errors.sequence += 'a'; }
1008 if(r[i] == '-'){ errors.Ni++; errors.sequence += 'a'; }
1010 else if(q[i] == '-' && r[i] != '-'){
1011 if(r[i] == 'A'){ errors.dA++; errors.sequence += 'd'; }
1012 if(r[i] == 'T'){ errors.dT++; errors.sequence += 'd'; }
1013 if(r[i] == 'G'){ errors.dG++; errors.sequence += 'd'; }
1014 if(r[i] == 'C'){ errors.dC++; errors.sequence += 'd'; }
1021 errors.sequence += 'd'; errors.total++;
1024 errors.sequence += 'r';
1028 else if(q[i] == '.' && r[i] != '.'){ // reference extends beyond query
1029 if(started == 1){ break; }
1031 else if(q[i] != '.' && r[i] == '.'){ // query extends beyond reference
1032 if(started == 1){ break; }
1034 else if(q[i] == '.' && r[i] == '.'){ // both are missing data
1035 if(started == 1){ break; }
1039 errors.mismatches = errors.total-errors.matches;
1040 if(errors.total != 0){ errors.errorRate = (double)(errors.total-errors.matches) / (double)errors.total; }
1041 else{ errors.errorRate = 0; }
1043 errors.queryName = query.getName();
1044 errors.refName = reference.getName();
1048 catch(exception& e) {
1049 m->errorOut(e, "SeqErrorCommand", "getErrors");
1054 //***************************************************************************************************************
1056 map<string, int> SeqErrorCommand::getWeights(){
1058 m->openInputFile(namesFileName, nameFile);
1061 string redundantSeqs;
1062 map<string, int> nameCountMap;
1065 nameFile >> seqName >> redundantSeqs;
1066 nameCountMap[seqName] = m->getNumNames(redundantSeqs);
1067 m->gobble(nameFile);
1072 return nameCountMap;
1075 //***************************************************************************************************************
1077 void SeqErrorCommand::printErrorHeader(ofstream& errorSummaryFile){
1079 errorSummaryFile << "query\treference\tweight\t";
1080 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";
1081 errorSummaryFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\tnumparents\n";
1083 errorSummaryFile << setprecision(6);
1084 errorSummaryFile.setf(ios::fixed);
1086 catch(exception& e) {
1087 m->errorOut(e, "SeqErrorCommand", "printErrorHeader");
1092 //***************************************************************************************************************
1094 void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs, ofstream& errorSummaryFile, ofstream& errorSeqFile){
1097 errorSummaryFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
1098 errorSummaryFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
1099 errorSummaryFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
1100 errorSummaryFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
1101 errorSummaryFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
1102 errorSummaryFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
1103 errorSummaryFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t';
1104 errorSummaryFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
1106 errorSummaryFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t'; //insertions
1107 errorSummaryFile << error.dA + error.dT + error.dG + error.dC << '\t'; //deletions
1108 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
1109 errorSummaryFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t'; //ambiguities
1110 errorSummaryFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << '\t' << numParentSeqs << endl;
1112 errorSeqFile << '>' << error.queryName << "\tref:" << error.refName << '\n' << error.sequence << endl;
1114 int a=0; int t=1; int g=2; int c=3;
1117 if(numParentSeqs == 1 || ignoreChimeras == 0){
1118 substitutionMatrix[a][a] += error.weight * error.AA;
1119 substitutionMatrix[a][t] += error.weight * error.TA;
1120 substitutionMatrix[a][g] += error.weight * error.GA;
1121 substitutionMatrix[a][c] += error.weight * error.CA;
1122 substitutionMatrix[a][gap] += error.weight * error.dA;
1123 substitutionMatrix[a][n] += error.weight * error.NA;
1125 substitutionMatrix[t][a] += error.weight * error.AT;
1126 substitutionMatrix[t][t] += error.weight * error.TT;
1127 substitutionMatrix[t][g] += error.weight * error.GT;
1128 substitutionMatrix[t][c] += error.weight * error.CT;
1129 substitutionMatrix[t][gap] += error.weight * error.dT;
1130 substitutionMatrix[t][n] += error.weight * error.NT;
1132 substitutionMatrix[g][a] += error.weight * error.AG;
1133 substitutionMatrix[g][t] += error.weight * error.TG;
1134 substitutionMatrix[g][g] += error.weight * error.GG;
1135 substitutionMatrix[g][c] += error.weight * error.CG;
1136 substitutionMatrix[g][gap] += error.weight * error.dG;
1137 substitutionMatrix[g][n] += error.weight * error.NG;
1139 substitutionMatrix[c][a] += error.weight * error.AC;
1140 substitutionMatrix[c][t] += error.weight * error.TC;
1141 substitutionMatrix[c][g] += error.weight * error.GC;
1142 substitutionMatrix[c][c] += error.weight * error.CC;
1143 substitutionMatrix[c][gap] += error.weight * error.dC;
1144 substitutionMatrix[c][n] += error.weight * error.NC;
1146 substitutionMatrix[gap][a] += error.weight * error.Ai;
1147 substitutionMatrix[gap][t] += error.weight * error.Ti;
1148 substitutionMatrix[gap][g] += error.weight * error.Gi;
1149 substitutionMatrix[gap][c] += error.weight * error.Ci;
1150 substitutionMatrix[gap][n] += error.weight * error.Ni;
1153 catch(exception& e) {
1154 m->errorOut(e, "SeqErrorCommand", "printErrorData");
1159 //***************************************************************************************************************
1161 void SeqErrorCommand::printSubMatrix(){
1163 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
1164 map<string, string> variables;
1165 variables["[filename]"] = fileNameRoot;
1166 string subMatrixFileName = getOutputFileName("errormatrix",variables);
1167 ofstream subMatrixFile;
1168 m->openOutputFile(subMatrixFileName, subMatrixFile);
1169 outputNames.push_back(subMatrixFileName); outputTypes["errormatrix"].push_back(subMatrixFileName);
1170 vector<string> bases(6);
1177 vector<int> refSums(5,1);
1179 for(int i=0;i<5;i++){
1180 subMatrixFile << "\tr" << bases[i];
1182 for(int j=0;j<6;j++){
1183 refSums[i] += substitutionMatrix[i][j];
1186 subMatrixFile << endl;
1188 for(int i=0;i<6;i++){
1189 subMatrixFile << 'q' << bases[i];
1190 for(int j=0;j<5;j++){
1191 subMatrixFile << '\t' << substitutionMatrix[j][i];
1193 subMatrixFile << endl;
1196 subMatrixFile << "total";
1197 for(int i=0;i<5;i++){
1198 subMatrixFile << '\t' << refSums[i];
1200 subMatrixFile << endl;
1201 subMatrixFile.close();
1203 catch(exception& e) {
1204 m->errorOut(e, "SeqErrorCommand", "printSubMatrix");
1209 //***************************************************************************************************************
1211 void SeqErrorCommand::printErrorFRFile(map<char, vector<int> > errorForward, map<char, vector<int> > errorReverse){
1213 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
1214 map<string, string> variables;
1215 variables["[filename]"] = fileNameRoot;
1216 string errorForwardFileName = getOutputFileName("errorforward",variables);
1217 ofstream errorForwardFile;
1218 m->openOutputFile(errorForwardFileName, errorForwardFile);
1219 outputNames.push_back(errorForwardFileName); outputTypes["errorforward"].push_back(errorForwardFileName);
1221 errorForwardFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
1222 for(int i=0;i<maxLength;i++){
1223 float match = (float)errorForward['m'][i];
1224 float subst = (float)errorForward['s'][i];
1225 float insert = (float)errorForward['i'][i];
1226 float del = (float)errorForward['d'][i];
1227 float amb = (float)errorForward['a'][i];
1228 float total = match + subst + insert + del + amb;
1229 if(total == 0){ break; }
1230 errorForwardFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
1232 errorForwardFile.close();
1234 string errorReverseFileName = getOutputFileName("errorreverse",variables);
1235 ofstream errorReverseFile;
1236 m->openOutputFile(errorReverseFileName, errorReverseFile);
1237 outputNames.push_back(errorReverseFileName); outputTypes["errorreverse"].push_back(errorReverseFileName);
1239 errorReverseFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
1240 for(int i=0;i<maxLength;i++){
1241 float match = (float)errorReverse['m'][i];
1242 float subst = (float)errorReverse['s'][i];
1243 float insert = (float)errorReverse['i'][i];
1244 float del = (float)errorReverse['d'][i];
1245 float amb = (float)errorReverse['a'][i];
1246 float total = match + subst + insert + del + amb;
1247 if(total == 0){ break; }
1248 errorReverseFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
1250 errorReverseFile.close();
1252 catch(exception& e) {
1253 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
1258 //***************************************************************************************************************
1260 void SeqErrorCommand::printErrorQuality(map<char, vector<int> > qScoreErrorMap){
1262 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
1263 map<string, string> variables;
1264 variables["[filename]"] = fileNameRoot;
1265 string errorQualityFileName = getOutputFileName("errorquality",variables);
1266 ofstream errorQualityFile;
1267 m->openOutputFile(errorQualityFileName, errorQualityFile);
1268 outputNames.push_back(errorQualityFileName); outputTypes["errorquality"].push_back(errorQualityFileName);
1270 errorQualityFile << "qscore\tmatches\tsubstitutions\tinsertions\tambiguous" << endl;
1271 for(int i=0;i<41;i++){
1272 errorQualityFile << i << '\t' << qScoreErrorMap['m'][i] << '\t' << qScoreErrorMap['s'][i] << '\t' << qScoreErrorMap['i'][i] << '\t'<< qScoreErrorMap['a'][i] << endl;
1274 errorQualityFile.close();
1276 catch(exception& e) {
1277 m->errorOut(e, "SeqErrorCommand", "printErrorQuality");
1282 //***************************************************************************************************************
1284 void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector<vector<int> > qualReverseMap){
1288 int numColumns = qualForwardMap[0].size();
1290 for(int i=0;i<qualForwardMap.size();i++){
1291 for(int j=0;j<numColumns;j++){
1292 if(qualForwardMap[i][j] != 0){
1293 if(numRows < i) { numRows = i+20; }
1297 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
1298 map<string, string> variables;
1299 variables["[filename]"] = fileNameRoot;
1300 string qualityForwardFileName = getOutputFileName("errorqualforward",variables);
1301 ofstream qualityForwardFile;
1302 m->openOutputFile(qualityForwardFileName, qualityForwardFile);
1303 outputNames.push_back(qualityForwardFileName); outputTypes["errorqualforward"].push_back(qualityForwardFileName);
1305 for(int i=0;i<numColumns;i++){ qualityForwardFile << '\t' << i; } qualityForwardFile << endl;
1307 for(int i=0;i<numRows;i++){
1308 qualityForwardFile << i+1;
1309 for(int j=0;j<numColumns;j++){
1310 qualityForwardFile << '\t' << qualForwardMap[i][j];
1313 qualityForwardFile << endl;
1315 qualityForwardFile.close();
1318 string qualityReverseFileName = getOutputFileName("errorqualreverse",variables);
1319 ofstream qualityReverseFile;
1320 m->openOutputFile(qualityReverseFileName, qualityReverseFile);
1321 outputNames.push_back(qualityReverseFileName); outputTypes["errorqualreverse"].push_back(qualityReverseFileName);
1323 for(int i=0;i<numColumns;i++){ qualityReverseFile << '\t' << i; } qualityReverseFile << endl;
1324 for(int i=0;i<numRows;i++){
1326 qualityReverseFile << i+1;
1327 for(int j=0;j<numColumns;j++){
1328 qualityReverseFile << '\t' << qualReverseMap[i][j];
1330 qualityReverseFile << endl;
1332 qualityReverseFile.close();
1334 catch(exception& e) {
1335 m->errorOut(e, "SeqErrorCommand", "printQualityFR");
1341 /**************************************************************************************************/
1343 int SeqErrorCommand::setLines(string filename, string qfilename, string rfilename, vector<unsigned long long>& fastaFilePos, vector<unsigned long long>& qfileFilePos, vector<unsigned long long>& rfileFilePos) {
1345 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1346 //set file positions for fasta file
1347 fastaFilePos = m->divideFile(filename, processors);
1349 if (qfilename == "") { return processors; }
1351 //get name of first sequence in each chunk
1352 map<string, int> firstSeqNames;
1353 for (int i = 0; i < (fastaFilePos.size()-1); i++) {
1355 m->openInputFile(filename, in);
1356 in.seekg(fastaFilePos[i]);
1359 firstSeqNames[temp.getName()] = i;
1364 //make copy to use below
1365 map<string, int> firstSeqNamesReport = firstSeqNames;
1367 //seach for filePos of each first name in the qfile and save in qfileFilePos
1369 m->openInputFile(qfilename, inQual);
1372 while(!inQual.eof()){
1373 input = m->getline(inQual);
1375 if (input.length() != 0) {
1376 if(input[0] == '>'){ //this is a sequence name line
1377 istringstream nameStream(input);
1379 string sname = ""; nameStream >> sname;
1380 sname = sname.substr(1);
1382 map<string, int>::iterator it = firstSeqNames.find(sname);
1384 if(it != firstSeqNames.end()) { //this is the start of a new chunk
1385 unsigned long long pos = inQual.tellg();
1386 qfileFilePos.push_back(pos - input.length() - 1);
1387 firstSeqNames.erase(it);
1392 if (firstSeqNames.size() == 0) { break; }
1396 if (firstSeqNames.size() != 0) {
1397 for (map<string, int>::iterator it = firstSeqNames.begin(); it != firstSeqNames.end(); it++) {
1398 m->mothurOut(it->first + " is in your fasta file and not in your quality file, aborting."); m->mothurOutEndLine();
1400 m->control_pressed = true;
1404 //get last file position of qfile
1406 unsigned long long size;
1408 //get num bytes in file
1409 pFile = fopen (qfilename.c_str(),"rb");
1410 if (pFile==NULL) perror ("Error opening file");
1412 fseek (pFile, 0, SEEK_END);
1417 qfileFilePos.push_back(size);
1420 //seach for filePos of each first name in the rfile and save in rfileFilePos
1424 m->openInputFile(rfilename, inR);
1426 //read column headers
1427 for (int i = 0; i < 16; i++) {
1428 if (!inR.eof()) { inR >> junk; }
1434 input = m->getline(inR);
1436 if (input.length() != 0) {
1438 istringstream nameStream(input);
1439 string sname = ""; nameStream >> sname;
1441 map<string, int>::iterator it = firstSeqNamesReport.find(sname);
1443 if(it != firstSeqNamesReport.end()) { //this is the start of a new chunk
1444 unsigned long long pos = inR.tellg();
1445 rfileFilePos.push_back(pos - input.length() - 1);
1446 firstSeqNamesReport.erase(it);
1450 if (firstSeqNamesReport.size() == 0) { break; }
1455 if (firstSeqNamesReport.size() != 0) {
1456 for (map<string, int>::iterator it = firstSeqNamesReport.begin(); it != firstSeqNamesReport.end(); it++) {
1457 m->mothurOut(it->first + " is in your fasta file and not in your report file, aborting."); m->mothurOutEndLine();
1459 m->control_pressed = true;
1463 //get last file position of qfile
1465 unsigned long long sizeR;
1467 //get num bytes in file
1468 rFile = fopen (rfilename.c_str(),"rb");
1469 if (rFile==NULL) perror ("Error opening file");
1471 fseek (rFile, 0, SEEK_END);
1472 sizeR=ftell (rFile);
1476 rfileFilePos.push_back(sizeR);
1482 fastaFilePos.push_back(0); qfileFilePos.push_back(0);
1483 //get last file position of fastafile
1485 unsigned long long size;
1487 //get num bytes in file
1488 pFile = fopen (filename.c_str(),"rb");
1489 if (pFile==NULL) perror ("Error opening file");
1491 fseek (pFile, 0, SEEK_END);
1495 fastaFilePos.push_back(size);
1497 //get last file position of fastafile
1500 //get num bytes in file
1501 qFile = fopen (qfilename.c_str(),"rb");
1502 if (qFile==NULL) perror ("Error opening file");
1504 fseek (qFile, 0, SEEK_END);
1508 qfileFilePos.push_back(size);
1514 catch(exception& e) {
1515 m->errorOut(e, "SeqErrorCommand", "setLines");
1520 //***************************************************************************************************************