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"
15 //**********************************************************************************************************************
16 vector<string> SeqErrorCommand::setParameters(){
18 CommandParameter pquery("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pquery);
19 CommandParameter preference("reference", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(preference);
20 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "QualReport",false,false); parameters.push_back(pqfile);
21 CommandParameter preport("report", "InputTypes", "", "", "none", "none", "QualReport",false,false); parameters.push_back(preport);
22 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
23 CommandParameter pignorechimeras("ignorechimeras", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pignorechimeras);
24 CommandParameter pthreshold("threshold", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pthreshold);
25 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
26 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
28 vector<string> myArray;
29 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
33 m->errorOut(e, "SeqErrorCommand", "setParameters");
37 //**********************************************************************************************************************
38 string SeqErrorCommand::getHelpString(){
40 string helpString = "";
41 helpString += "The seq.error command reads a query alignment file and a reference alignment file and creates .....\n";
42 helpString += "Example seq.error(...).\n";
43 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
44 helpString += "For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n";
48 m->errorOut(e, "SeqErrorCommand", "getHelpString");
52 //**********************************************************************************************************************
53 SeqErrorCommand::SeqErrorCommand(){
55 abort = true; calledHelp = true;
56 vector<string> tempOutNames;
57 outputTypes["error.summary"] = tempOutNames;
58 outputTypes["error.seq"] = tempOutNames;
59 outputTypes["error.quality"] = tempOutNames;
60 outputTypes["error.qual.forward"] = tempOutNames;
61 outputTypes["error.qual.reverse"] = tempOutNames;
62 outputTypes["error.forward"] = tempOutNames;
63 outputTypes["error.reverse"] = tempOutNames;
64 outputTypes["error.count"] = tempOutNames;
65 outputTypes["error.matrix"] = tempOutNames;
68 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
72 //***************************************************************************************************************
74 SeqErrorCommand::SeqErrorCommand(string option) {
77 abort = false; calledHelp = false;
79 //allow user to run help
80 if(option == "help") { help(); abort = true; calledHelp = true; }
84 vector<string> myArray = setParameters();
86 OptionParser parser(option);
87 map<string,string> parameters = parser.getParameters();
89 ValidParameters validParameter;
90 map<string,string>::iterator it;
92 //check to make sure all parameters are valid for command
93 for (it = parameters.begin(); it != parameters.end(); it++) {
94 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
97 //initialize outputTypes
98 vector<string> tempOutNames;
99 outputTypes["error.summary"] = tempOutNames;
100 outputTypes["error.seq"] = tempOutNames;
101 outputTypes["error.quality"] = tempOutNames;
102 outputTypes["error.qual.forward"] = tempOutNames;
103 outputTypes["error.qual.reverse"] = tempOutNames;
104 outputTypes["error.forward"] = tempOutNames;
105 outputTypes["error.reverse"] = tempOutNames;
106 outputTypes["error.count"] = tempOutNames;
107 outputTypes["error.matrix"] = tempOutNames;
110 //if the user changes the input directory command factory will send this info to us in the output parameter
111 string inputDir = validParameter.validFile(parameters, "inputdir", false);
112 if (inputDir == "not found"){ inputDir = ""; }
115 it = parameters.find("fasta");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["fasta"] = inputDir + it->second; }
123 it = parameters.find("reference");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["reference"] = inputDir + it->second; }
131 it = parameters.find("name");
132 //user has given a names file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["name"] = inputDir + it->second; }
139 it = parameters.find("qfile");
140 //user has given a quality score file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["qfile"] = inputDir + it->second; }
147 it = parameters.find("report");
148 //user has given a alignment report file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["report"] = inputDir + it->second; }
156 //check for required parameters
157 queryFileName = validParameter.validFile(parameters, "fasta", true);
158 if (queryFileName == "not found") {
159 queryFileName = m->getFastaFile();
160 if (queryFileName != "") { m->mothurOut("Using " + queryFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
161 else { m->mothurOut("You have no current fasta file and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
163 else if (queryFileName == "not open") { abort = true; }
165 referenceFileName = validParameter.validFile(parameters, "reference", true);
166 if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
167 else if (referenceFileName == "not open") { abort = true; }
170 //check for optional parameters
171 namesFileName = validParameter.validFile(parameters, "name", true);
172 if(namesFileName == "not found"){ namesFileName = ""; }
173 else if (namesFileName == "not open") { namesFileName = ""; abort = true; }
175 qualFileName = validParameter.validFile(parameters, "qfile", true);
176 if(qualFileName == "not found"){ qualFileName = ""; }
177 else if (qualFileName == "not open") { qualFileName = ""; abort = true; }
179 reportFileName = validParameter.validFile(parameters, "report", true);
180 if(reportFileName == "not found"){ reportFileName = ""; }
181 else if (reportFileName == "not open") { reportFileName = ""; abort = true; }
183 if((reportFileName != "" && qualFileName == "") || (reportFileName == "" && qualFileName != "")){
184 m->mothurOut("if you use either a qual file or a report file, you have to have both.");
185 m->mothurOutEndLine();
189 outputDir = validParameter.validFile(parameters, "outputdir", false);
190 if (outputDir == "not found"){
192 outputDir += m->hasPath(queryFileName); //if user entered a file with a path then preserve it
195 //check for optional parameter and set defaults
196 // ...at some point should added some additional type checking...
197 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found") { temp = "1.00"; }
198 convert(temp, threshold);
200 temp = validParameter.validFile(parameters, "ignorechimeras", false); if (temp == "not found") { temp = "1"; }
201 convert(temp, ignoreChimeras);
203 substitutionMatrix.resize(6);
204 for(int i=0;i<6;i++){ substitutionMatrix[i].resize(6,0); }
207 catch(exception& e) {
208 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
212 //***************************************************************************************************************
214 int SeqErrorCommand::execute(){
216 if (abort == true) { if (calledHelp) { return 0; } return 2; }
220 string errorSummaryFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.summary";
221 m->openOutputFile(errorSummaryFileName, errorSummaryFile);
222 outputNames.push_back(errorSummaryFileName); outputTypes["error.summary"].push_back(errorSummaryFileName);
225 string errorSeqFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq";
226 m->openOutputFile(errorSeqFileName, errorSeqFile);
227 outputNames.push_back(errorSeqFileName); outputTypes["error.seq"].push_back(errorSeqFileName);
229 getReferences(); //read in reference sequences - make sure there's no ambiguous bases
231 map<string, int> weights;
232 if(namesFileName != ""){ weights = getWeights(); }
235 m->openInputFile(queryFileName, queryFile);
241 QualityScores quality;
242 vector<vector<int> > qualForwardMap;
243 vector<vector<int> > qualReverseMap;
245 if(qualFileName != "" && reportFileName != ""){
246 m->openInputFile(qualFileName, qualFile);
247 report = ReportFile(reportFile, reportFileName);
249 qualForwardMap.resize(maxLength);
250 qualReverseMap.resize(maxLength);
251 for(int i=0;i<maxLength;i++){
252 qualForwardMap[i].assign(41,0);
253 qualReverseMap[i].assign(41,0);
258 int totalMatches = 0;
260 vector<int> misMatchCounts(11, 0);
264 map<string, int>::iterator it;
265 map<char, vector<int> > qScoreErrorMap;
266 qScoreErrorMap['m'].assign(41, 0);
267 qScoreErrorMap['s'].assign(41, 0);
268 qScoreErrorMap['i'].assign(41, 0);
269 qScoreErrorMap['a'].assign(41, 0);
271 map<char, vector<int> > errorForward;
272 errorForward['m'].assign(maxLength,0);
273 errorForward['s'].assign(maxLength,0);
274 errorForward['i'].assign(maxLength,0);
275 errorForward['d'].assign(maxLength,0);
276 errorForward['a'].assign(maxLength,0);
278 map<char, vector<int> > errorReverse;
279 errorReverse['m'].assign(maxLength,0);
280 errorReverse['s'].assign(maxLength,0);
281 errorReverse['i'].assign(maxLength,0);
282 errorReverse['d'].assign(maxLength,0);
283 errorReverse['a'].assign(maxLength,0);
286 string errorChimeraFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.chimera";
287 RefChimeraTest chimeraTest(referenceSeqs, errorChimeraFileName);
288 outputNames.push_back(errorChimeraFileName); outputTypes["error.chimera"].push_back(errorChimeraFileName);
290 vector<string> megaAlignVector(numRefs, "");
297 if (m->control_pressed) { errorSummaryFile.close(); errorSeqFile.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
299 Sequence query(queryFile);
301 int numParentSeqs = chimeraTest.analyzeQuery(query.getName(), query.getAligned());
302 int closestRefIndex = chimeraTest.getClosestRefIndex();
304 if(numParentSeqs > 1 && ignoreChimeras == 1) { ignoreSeq = 1; }
305 else { ignoreSeq = 0; }
307 Compare minCompare = getErrors(query, referenceSeqs[closestRefIndex]);
309 if(namesFileName != ""){
310 it = weights.find(query.getName());
311 minCompare.weight = it->second;
313 else{ minCompare.weight = 1; }
315 printErrorData(minCompare, numParentSeqs);
319 for(int i=0;i<minCompare.sequence.length();i++){
320 char letter = minCompare.sequence[i];
322 errorForward[letter][i] += minCompare.weight;
323 errorReverse[letter][minCompare.total-i-1] += minCompare.weight;
327 if(qualFileName != "" && reportFileName != ""){
328 report = ReportFile(reportFile);
330 // int origLength = report.getQueryLength();
331 int startBase = report.getQueryStart();
332 int endBase = report.getQueryEnd();
334 quality = QualityScores(qualFile);
337 quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
338 quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
339 quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
343 if(minCompare.errorRate < threshold && !ignoreSeq){
344 totalBases += (minCompare.total * minCompare.weight);
345 totalMatches += minCompare.matches * minCompare.weight;
346 if(minCompare.mismatches > maxMismatch){
347 maxMismatch = minCompare.mismatches;
348 misMatchCounts.resize(maxMismatch + 1, 0);
350 misMatchCounts[minCompare.mismatches] += minCompare.weight;
353 megaAlignVector[closestRefIndex] += query.getInlineSeq() + '\n';
358 if(index % 1000 == 0){ m->mothurOut(toString(index) + '\n'); }
361 errorSummaryFile.close();
362 errorSeqFile.close();
364 if(qualFileName != "" && reportFileName != ""){
365 printErrorQuality(qScoreErrorMap);
366 printQualityFR(qualForwardMap, qualReverseMap);
369 printErrorFRFile(errorForward, errorReverse);
371 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
373 string errorCountFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.count";
374 ofstream errorCountFile;
375 m->openOutputFile(errorCountFileName, errorCountFile);
376 outputNames.push_back(errorCountFileName); outputTypes["error.count"].push_back(errorCountFileName);
377 m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n");
378 m->mothurOut("Errors\tSequences\n");
379 errorCountFile << "Errors\tSequences\n";
380 for(int i=0;i<misMatchCounts.size();i++){
381 m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
382 errorCountFile << i << '\t' << misMatchCounts[i] << endl;
384 errorCountFile.close();
386 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
390 string megAlignmentFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.ref-query";
391 ofstream megAlignmentFile;
392 m->openOutputFile(megAlignmentFileName, megAlignmentFile);
393 outputNames.push_back(megAlignmentFileName); outputTypes["error.ref-query"].push_back(megAlignmentFileName);
395 for(int i=0;i<numRefs;i++){
396 megAlignmentFile << referenceSeqs[i].getInlineSeq() << endl;
397 megAlignmentFile << megaAlignVector[i] << endl;
401 m->mothurOutEndLine();
402 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
403 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
404 m->mothurOutEndLine();
408 catch(exception& e) {
409 m->errorOut(e, "SeqErrorCommand", "execute");
414 //***************************************************************************************************************
416 void SeqErrorCommand::getReferences(){
419 ifstream referenceFile;
420 m->openInputFile(referenceFileName, referenceFile);
422 int numAmbigSeqs = 0;
425 int minEndPos = 100000;
427 while(referenceFile){
428 Sequence currentSeq(referenceFile);
429 int numAmbigs = currentSeq.getAmbigBases();
430 if(numAmbigs > 0){ numAmbigSeqs++; }
432 // int startPos = currentSeq.getStartPos();
433 // if(startPos > maxStartPos) { maxStartPos = startPos; }
435 // int endPos = currentSeq.getEndPos();
436 // if(endPos < minEndPos) { minEndPos = endPos; }
437 referenceSeqs.push_back(currentSeq);
439 m->gobble(referenceFile);
441 referenceFile.close();
442 numRefs = referenceSeqs.size();
445 for(int i=0;i<numRefs;i++){
446 referenceSeqs[i].padToPos(maxStartPos);
447 referenceSeqs[i].padFromPos(minEndPos);
450 if(numAmbigSeqs != 0){
451 m->mothurOut("Warning: " + toString(numAmbigSeqs) + " reference sequences have ambiguous bases, these bases will be ignored\n");
455 catch(exception& e) {
456 m->errorOut(e, "SeqErrorCommand", "getReferences");
461 //***************************************************************************************************************
463 Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
465 if(query.getAlignLength() != reference.getAlignLength()){
466 m->mothurOut("Warning: " + toString(query.getName()) + " and " + toString(reference.getName()) + " are different lengths\n");
468 int alignLength = query.getAlignLength();
470 string q = query.getAligned();
471 string r = reference.getAligned();
476 for(int i=0;i<alignLength;i++){
477 if(r[i] != 'N' && q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){ // no missing data and no double gaps
481 if(r[i] == 'A'){ errors.AA++; errors.matches++; errors.sequence += 'm'; }
482 if(r[i] == 'T'){ errors.AT++; errors.sequence += 's'; }
483 if(r[i] == 'G'){ errors.AG++; errors.sequence += 's'; }
484 if(r[i] == 'C'){ errors.AC++; errors.sequence += 's'; }
485 if(r[i] == '-'){ errors.Ai++; errors.sequence += 'i'; }
487 else if(q[i] == 'T'){
488 if(r[i] == 'A'){ errors.TA++; errors.sequence += 's'; }
489 if(r[i] == 'T'){ errors.TT++; errors.matches++; errors.sequence += 'm'; }
490 if(r[i] == 'G'){ errors.TG++; errors.sequence += 's'; }
491 if(r[i] == 'C'){ errors.TC++; errors.sequence += 's'; }
492 if(r[i] == '-'){ errors.Ti++; errors.sequence += 'i'; }
494 else if(q[i] == 'G'){
495 if(r[i] == 'A'){ errors.GA++; errors.sequence += 's'; }
496 if(r[i] == 'T'){ errors.GT++; errors.sequence += 's'; }
497 if(r[i] == 'G'){ errors.GG++; errors.matches++; errors.sequence += 'm'; }
498 if(r[i] == 'C'){ errors.GC++; errors.sequence += 's'; }
499 if(r[i] == '-'){ errors.Gi++; errors.sequence += 'i'; }
501 else if(q[i] == 'C'){
502 if(r[i] == 'A'){ errors.CA++; errors.sequence += 's'; }
503 if(r[i] == 'T'){ errors.CT++; errors.sequence += 's'; }
504 if(r[i] == 'G'){ errors.CG++; errors.sequence += 's'; }
505 if(r[i] == 'C'){ errors.CC++; errors.matches++; errors.sequence += 'm'; }
506 if(r[i] == '-'){ errors.Ci++; errors.sequence += 'i'; }
508 else if(q[i] == 'N'){
509 if(r[i] == 'A'){ errors.NA++; errors.sequence += 'a'; }
510 if(r[i] == 'T'){ errors.NT++; errors.sequence += 'a'; }
511 if(r[i] == 'G'){ errors.NG++; errors.sequence += 'a'; }
512 if(r[i] == 'C'){ errors.NC++; errors.sequence += 'a'; }
513 if(r[i] == '-'){ errors.Ni++; errors.sequence += 'a'; }
515 else if(q[i] == '-' && r[i] != '-'){
516 if(r[i] == 'A'){ errors.dA++; errors.sequence += 'd'; }
517 if(r[i] == 'T'){ errors.dT++; errors.sequence += 'd'; }
518 if(r[i] == 'G'){ errors.dG++; errors.sequence += 'd'; }
519 if(r[i] == 'C'){ errors.dC++; errors.sequence += 'd'; }
524 else if(q[i] == '.' && r[i] != '.'){ // reference extends beyond query
525 if(started == 1){ break; }
527 else if(q[i] != '.' && r[i] == '.'){ // query extends beyond reference
528 if(started == 1){ break; }
530 else if(q[i] == '.' && r[i] == '.'){ // both are missing data
531 if(started == 1){ break; }
535 errors.mismatches = errors.total-errors.matches;
536 errors.errorRate = (double)(errors.total-errors.matches) / (double)errors.total;
537 errors.queryName = query.getName();
538 errors.refName = reference.getName();
542 catch(exception& e) {
543 m->errorOut(e, "SeqErrorCommand", "getErrors");
548 //***************************************************************************************************************
550 map<string, int> SeqErrorCommand::getWeights(){
552 m->openInputFile(namesFileName, nameFile);
555 string redundantSeqs;
556 map<string, int> nameCountMap;
559 nameFile >> seqName >> redundantSeqs;
560 nameCountMap[seqName] = m->getNumNames(redundantSeqs);
567 //***************************************************************************************************************
569 void SeqErrorCommand::printErrorHeader(){
571 errorSummaryFile << "query\treference\tweight\t";
572 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";
573 errorSummaryFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\tnumparents\n";
575 errorSummaryFile << setprecision(6);
576 errorSummaryFile.setf(ios::fixed);
578 catch(exception& e) {
579 m->errorOut(e, "SeqErrorCommand", "printErrorHeader");
584 //***************************************************************************************************************
586 void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs){
589 errorSummaryFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
590 errorSummaryFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
591 errorSummaryFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
592 errorSummaryFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
593 errorSummaryFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
594 errorSummaryFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
595 errorSummaryFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t';
596 errorSummaryFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
598 errorSummaryFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t'; //insertions
599 errorSummaryFile << error.dA + error.dT + error.dG + error.dC << '\t'; //deletions
600 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
601 errorSummaryFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t'; //ambiguities
602 errorSummaryFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << '\t' << numParentSeqs << endl;
604 errorSeqFile << '>' << error.queryName << "\tref:" << error.refName << '\n' << error.sequence << endl;
606 int a=0; int t=1; int g=2; int c=3;
609 if(numParentSeqs == 1 || ignoreChimeras == 0){
610 substitutionMatrix[a][a] += error.weight * error.AA;
611 substitutionMatrix[a][t] += error.weight * error.TA;
612 substitutionMatrix[a][g] += error.weight * error.GA;
613 substitutionMatrix[a][c] += error.weight * error.CA;
614 substitutionMatrix[a][gap] += error.weight * error.dA;
615 substitutionMatrix[a][n] += error.weight * error.NA;
617 substitutionMatrix[t][a] += error.weight * error.AT;
618 substitutionMatrix[t][t] += error.weight * error.TT;
619 substitutionMatrix[t][g] += error.weight * error.GT;
620 substitutionMatrix[t][c] += error.weight * error.CT;
621 substitutionMatrix[t][gap] += error.weight * error.dT;
622 substitutionMatrix[t][n] += error.weight * error.NT;
624 substitutionMatrix[g][a] += error.weight * error.AG;
625 substitutionMatrix[g][t] += error.weight * error.TG;
626 substitutionMatrix[g][g] += error.weight * error.GG;
627 substitutionMatrix[g][c] += error.weight * error.CG;
628 substitutionMatrix[g][gap] += error.weight * error.dG;
629 substitutionMatrix[g][n] += error.weight * error.NG;
631 substitutionMatrix[c][a] += error.weight * error.AC;
632 substitutionMatrix[c][t] += error.weight * error.TC;
633 substitutionMatrix[c][g] += error.weight * error.GC;
634 substitutionMatrix[c][c] += error.weight * error.CC;
635 substitutionMatrix[c][gap] += error.weight * error.dC;
636 substitutionMatrix[c][n] += error.weight * error.NC;
638 substitutionMatrix[gap][a] += error.weight * error.Ai;
639 substitutionMatrix[gap][t] += error.weight * error.Ti;
640 substitutionMatrix[gap][g] += error.weight * error.Gi;
641 substitutionMatrix[gap][c] += error.weight * error.Ci;
642 substitutionMatrix[gap][n] += error.weight * error.Ni;
645 catch(exception& e) {
646 m->errorOut(e, "SeqErrorCommand", "printErrorData");
651 //***************************************************************************************************************
653 void SeqErrorCommand::printSubMatrix(){
655 string subMatrixFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.matrix";
656 ofstream subMatrixFile;
657 m->openOutputFile(subMatrixFileName, subMatrixFile);
658 outputNames.push_back(subMatrixFileName); outputTypes["error.matrix"].push_back(subMatrixFileName);
659 vector<string> bases(6);
666 vector<int> refSums(5,1);
668 for(int i=0;i<5;i++){
669 subMatrixFile << "\tr" << bases[i];
671 for(int j=0;j<6;j++){
672 refSums[i] += substitutionMatrix[i][j];
675 subMatrixFile << endl;
677 for(int i=0;i<6;i++){
678 subMatrixFile << 'q' << bases[i];
679 for(int j=0;j<5;j++){
680 subMatrixFile << '\t' << substitutionMatrix[j][i];
682 subMatrixFile << endl;
685 subMatrixFile << "total";
686 for(int i=0;i<5;i++){
687 subMatrixFile << '\t' << refSums[i];
689 subMatrixFile << endl;
690 subMatrixFile.close();
692 catch(exception& e) {
693 m->errorOut(e, "SeqErrorCommand", "printSubMatrix");
697 //***************************************************************************************************************
699 void SeqErrorCommand::printErrorFRFile(map<char, vector<int> > errorForward, map<char, vector<int> > errorReverse){
701 string errorForwardFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq.forward";
702 ofstream errorForwardFile;
703 m->openOutputFile(errorForwardFileName, errorForwardFile);
704 outputNames.push_back(errorForwardFileName); outputTypes["error.forward"].push_back(errorForwardFileName);
706 errorForwardFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
707 for(int i=0;i<maxLength;i++){
708 float match = (float)errorForward['m'][i];
709 float subst = (float)errorForward['s'][i];
710 float insert = (float)errorForward['i'][i];
711 float del = (float)errorForward['d'][i];
712 float amb = (float)errorForward['a'][i];
713 float total = match + subst + insert + del + amb;
714 if(total == 0){ break; }
715 errorForwardFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
717 errorForwardFile.close();
719 string errorReverseFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq.reverse";
720 ofstream errorReverseFile;
721 m->openOutputFile(errorReverseFileName, errorReverseFile);
722 outputNames.push_back(errorReverseFileName); outputTypes["error.reverse"].push_back(errorReverseFileName);
724 errorReverseFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
725 for(int i=0;i<maxLength;i++){
726 float match = (float)errorReverse['m'][i];
727 float subst = (float)errorReverse['s'][i];
728 float insert = (float)errorReverse['i'][i];
729 float del = (float)errorReverse['d'][i];
730 float amb = (float)errorReverse['a'][i];
731 float total = match + subst + insert + del + amb;
732 if(total == 0){ break; }
733 errorReverseFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
735 errorReverseFile.close();
737 catch(exception& e) {
738 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
743 //***************************************************************************************************************
745 void SeqErrorCommand::printErrorQuality(map<char, vector<int> > qScoreErrorMap){
748 string errorQualityFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.quality";
749 ofstream errorQualityFile;
750 m->openOutputFile(errorQualityFileName, errorQualityFile);
751 outputNames.push_back(errorQualityFileName); outputTypes["error.quality"].push_back(errorQualityFileName);
753 errorQualityFile << "qscore\tmatches\tsubstitutions\tinsertions\tambiguous" << endl;
754 for(int i=0;i<41;i++){
755 errorQualityFile << i << '\t' << qScoreErrorMap['m'][i] << '\t' << qScoreErrorMap['s'][i] << '\t' << qScoreErrorMap['i'][i] << '\t'<< qScoreErrorMap['a'][i] << endl;
757 errorQualityFile.close();
759 catch(exception& e) {
760 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
766 //***************************************************************************************************************
768 void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector<vector<int> > qualReverseMap){
772 int numColumns = qualForwardMap[0].size();
774 for(int i=0;i<qualForwardMap.size();i++){
775 for(int j=0;j<numColumns;j++){
776 if(qualForwardMap[i][j] != 0){
777 if(numRows < i) { numRows = i+20; }
782 string qualityForwardFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.qual.forward";
783 ofstream qualityForwardFile;
784 m->openOutputFile(qualityForwardFileName, qualityForwardFile);
785 outputNames.push_back(qualityForwardFileName); outputTypes["error.qual.forward"].push_back(qualityForwardFileName);
787 for(int i=0;i<numColumns;i++){ qualityForwardFile << '\t' << i; } qualityForwardFile << endl;
789 for(int i=0;i<numRows;i++){
790 qualityForwardFile << i+1;
791 for(int j=0;j<numColumns;j++){
792 qualityForwardFile << '\t' << qualForwardMap[i][j];
795 qualityForwardFile << endl;
797 qualityForwardFile.close();
800 string qualityReverseFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.qual.reverse";
801 ofstream qualityReverseFile;
802 m->openOutputFile(qualityReverseFileName, qualityReverseFile);
803 outputNames.push_back(qualityReverseFileName); outputTypes["error.qual.reverse"].push_back(qualityReverseFileName);
805 for(int i=0;i<numColumns;i++){ qualityReverseFile << '\t' << i; } qualityReverseFile << endl;
806 for(int i=0;i<numRows;i++){
808 qualityReverseFile << i+1;
809 for(int j=0;j<numColumns;j++){
810 qualityReverseFile << '\t' << qualReverseMap[i][j];
812 qualityReverseFile << endl;
814 qualityReverseFile.close();
816 catch(exception& e) {
817 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
823 //***************************************************************************************************************