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::getValidParameters(){
18 string Array[] = {"query", "reference", "name", "qfile", "report", "threshold", "ignorechimeras", "inputdir", "outputdir"};
19 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
23 m->errorOut(e, "SeqErrorCommand", "getValidParameters");
27 //**********************************************************************************************************************
28 SeqErrorCommand::SeqErrorCommand(){
30 abort = true; calledHelp = true;
31 vector<string> tempOutNames;
32 outputTypes["error.summary"] = tempOutNames;
33 outputTypes["error.seq"] = tempOutNames;
34 outputTypes["error.quality"] = tempOutNames;
35 outputTypes["error.qual.forward"] = tempOutNames;
36 outputTypes["error.qual.reverse"] = tempOutNames;
37 outputTypes["error.forward"] = tempOutNames;
38 outputTypes["error.reverse"] = tempOutNames;
39 outputTypes["error.count"] = tempOutNames;
40 outputTypes["error.matrix"] = tempOutNames;
43 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
47 //**********************************************************************************************************************
48 vector<string> SeqErrorCommand::getRequiredParameters(){
50 string Array[] = {"query","reference"};
51 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55 m->errorOut(e, "SeqErrorCommand", "getRequiredParameters");
59 //**********************************************************************************************************************
60 vector<string> SeqErrorCommand::getRequiredFiles(){
62 vector<string> myArray;
66 m->errorOut(e, "SeqErrorCommand", "getRequiredFiles");
70 //***************************************************************************************************************
72 SeqErrorCommand::SeqErrorCommand(string option) {
75 abort = false; calledHelp = false;
77 //allow user to run help
78 if(option == "help") { help(); abort = true; calledHelp = true; }
83 //valid paramters for this command
84 string AlignArray[] = {"query", "reference", "name", "qfile", "report", "threshold", "inputdir", "ignorechimeras", "outputdir"};
86 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
88 OptionParser parser(option);
89 map<string,string> parameters = parser.getParameters();
91 ValidParameters validParameter;
92 map<string,string>::iterator it;
94 //check to make sure all parameters are valid for command
95 for (it = parameters.begin(); it != parameters.end(); it++) {
96 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
99 //initialize outputTypes
100 vector<string> tempOutNames;
101 outputTypes["error.summary"] = tempOutNames;
102 outputTypes["error.seq"] = tempOutNames;
103 outputTypes["error.quality"] = tempOutNames;
104 outputTypes["error.qual.forward"] = tempOutNames;
105 outputTypes["error.qual.reverse"] = tempOutNames;
106 outputTypes["error.forward"] = tempOutNames;
107 outputTypes["error.reverse"] = tempOutNames;
108 outputTypes["error.count"] = tempOutNames;
109 outputTypes["error.matrix"] = tempOutNames;
112 //if the user changes the input directory command factory will send this info to us in the output parameter
113 string inputDir = validParameter.validFile(parameters, "inputdir", false);
114 if (inputDir == "not found"){ inputDir = ""; }
117 it = parameters.find("query");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["query"] = inputDir + it->second; }
125 it = parameters.find("reference");
126 //user has given a template file
127 if(it != parameters.end()){
128 path = m->hasPath(it->second);
129 //if the user has not given a path then, add inputdir. else leave path alone.
130 if (path == "") { parameters["reference"] = inputDir + it->second; }
133 it = parameters.find("name");
134 //user has given a names file
135 if(it != parameters.end()){
136 path = m->hasPath(it->second);
137 //if the user has not given a path then, add inputdir. else leave path alone.
138 if (path == "") { parameters["name"] = inputDir + it->second; }
141 it = parameters.find("qfile");
142 //user has given a quality score file
143 if(it != parameters.end()){
144 path = m->hasPath(it->second);
145 //if the user has not given a path then, add inputdir. else leave path alone.
146 if (path == "") { parameters["qfile"] = inputDir + it->second; }
149 it = parameters.find("report");
150 //user has given a alignment report file
151 if(it != parameters.end()){
152 path = m->hasPath(it->second);
153 //if the user has not given a path then, add inputdir. else leave path alone.
154 if (path == "") { parameters["report"] = inputDir + it->second; }
158 //check for required parameters
159 queryFileName = validParameter.validFile(parameters, "query", true);
160 if (queryFileName == "not found") { m->mothurOut("query is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
161 else if (queryFileName == "not open") { abort = true; }
163 referenceFileName = validParameter.validFile(parameters, "reference", true);
164 if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
165 else if (referenceFileName == "not open") { abort = true; }
168 //check for optional parameters
169 namesFileName = validParameter.validFile(parameters, "name", true);
170 if(namesFileName == "not found"){ namesFileName = ""; }
172 qualFileName = validParameter.validFile(parameters, "qfile", true);
173 if(qualFileName == "not found"){ qualFileName = ""; }
175 reportFileName = validParameter.validFile(parameters, "report", true);
176 if(reportFileName == "not found"){ reportFileName = ""; }
178 if((reportFileName != "" && qualFileName == "") || (reportFileName == "" && qualFileName != "")){
179 m->mothurOut("if you use either a qual file or a report file, you have to have both.");
180 m->mothurOutEndLine();
184 outputDir = validParameter.validFile(parameters, "outputdir", false);
185 if (outputDir == "not found"){
187 outputDir += m->hasPath(queryFileName); //if user entered a file with a path then preserve it
190 //check for optional parameter and set defaults
191 // ...at some point should added some additional type checking...
192 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found") { temp = "1.00"; }
193 convert(temp, threshold);
195 temp = validParameter.validFile(parameters, "ignorechimeras", false); if (temp == "not found") { temp = "1"; }
196 convert(temp, ignoreChimeras);
198 substitutionMatrix.resize(6);
199 for(int i=0;i<6;i++){ substitutionMatrix[i].resize(6,0); }
202 catch(exception& e) {
203 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
208 //**********************************************************************************************************************
210 void SeqErrorCommand::help(){
212 m->mothurOut("The seq.error command reads a query alignment file and a reference alignment file and creates .....\n");
213 m->mothurOut("Example seq.error(...).\n");
214 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
215 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n\n");
217 catch(exception& e) {
218 m->errorOut(e, "SeqErrorCommand", "help");
223 //***************************************************************************************************************
225 SeqErrorCommand::~SeqErrorCommand(){ /* void */ }
227 //***************************************************************************************************************
229 int SeqErrorCommand::execute(){
231 if (abort == true) { if (calledHelp) { return 0; } return 2; }
235 string errorSummaryFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.summary";
236 m->openOutputFile(errorSummaryFileName, errorSummaryFile);
237 outputNames.push_back(errorSummaryFileName); outputTypes["error.summary"].push_back(errorSummaryFileName);
240 string errorSeqFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq";
241 m->openOutputFile(errorSeqFileName, errorSeqFile);
242 outputNames.push_back(errorSeqFileName); outputTypes["error.seq"].push_back(errorSeqFileName);
244 getReferences(); //read in reference sequences - make sure there's no ambiguous bases
246 map<string, int> weights;
247 if(namesFileName != ""){ weights = getWeights(); }
250 m->openInputFile(queryFileName, queryFile);
256 QualityScores quality;
257 vector<vector<int> > qualForwardMap;
258 vector<vector<int> > qualReverseMap;
260 if(qualFileName != "" && reportFileName != ""){
261 m->openInputFile(qualFileName, qualFile);
262 report = ReportFile(reportFile, reportFileName);
264 qualForwardMap.resize(maxLength);
265 qualReverseMap.resize(maxLength);
266 for(int i=0;i<maxLength;i++){
267 qualForwardMap[i].assign(41,0);
268 qualReverseMap[i].assign(41,0);
273 int totalMatches = 0;
275 vector<int> misMatchCounts(11, 0);
279 map<string, int>::iterator it;
280 map<char, vector<int> > qScoreErrorMap;
281 qScoreErrorMap['m'].assign(41, 0);
282 qScoreErrorMap['s'].assign(41, 0);
283 qScoreErrorMap['i'].assign(41, 0);
284 qScoreErrorMap['a'].assign(41, 0);
286 map<char, vector<int> > errorForward;
287 errorForward['m'].assign(maxLength,0);
288 errorForward['s'].assign(maxLength,0);
289 errorForward['i'].assign(maxLength,0);
290 errorForward['d'].assign(maxLength,0);
291 errorForward['a'].assign(maxLength,0);
293 map<char, vector<int> > errorReverse;
294 errorReverse['m'].assign(maxLength,0);
295 errorReverse['s'].assign(maxLength,0);
296 errorReverse['i'].assign(maxLength,0);
297 errorReverse['d'].assign(maxLength,0);
298 errorReverse['a'].assign(maxLength,0);
301 string errorChimeraFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.chimera";
302 RefChimeraTest chimeraTest(referenceSeqs, errorChimeraFileName);
303 outputNames.push_back(errorChimeraFileName); outputTypes["error.chimera"].push_back(errorChimeraFileName);
305 vector<string> megaAlignVector(numRefs, "");
312 if (m->control_pressed) { errorSummaryFile.close(); errorSeqFile.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
314 Sequence query(queryFile);
316 int numParentSeqs = chimeraTest.analyzeQuery(query.getName(), query.getAligned());
317 int closestRefIndex = chimeraTest.getClosestRefIndex();
319 if(numParentSeqs > 1 && ignoreChimeras == 1) { ignoreSeq = 1; }
320 else { ignoreSeq = 0; }
322 Compare minCompare = getErrors(query, referenceSeqs[closestRefIndex]);
324 if(namesFileName != ""){
325 it = weights.find(query.getName());
326 minCompare.weight = it->second;
328 else{ minCompare.weight = 1; }
330 printErrorData(minCompare, numParentSeqs);
334 for(int i=0;i<minCompare.total;i++){
335 char letter = minCompare.sequence[i];
337 errorForward[letter][i] += minCompare.weight;
338 errorReverse[letter][minCompare.total-i-1] += minCompare.weight;
342 if(qualFileName != "" && reportFileName != ""){
343 report = ReportFile(reportFile);
345 // int origLength = report.getQueryLength();
346 int startBase = report.getQueryStart();
347 int endBase = report.getQueryEnd();
349 quality = QualityScores(qualFile);
352 quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
353 quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
354 quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
358 if(minCompare.errorRate < threshold && !ignoreSeq){
359 totalBases += (minCompare.total * minCompare.weight);
360 totalMatches += minCompare.matches * minCompare.weight;
361 if(minCompare.mismatches > maxMismatch){
362 maxMismatch = minCompare.mismatches;
363 misMatchCounts.resize(maxMismatch + 1, 0);
365 misMatchCounts[minCompare.mismatches] += minCompare.weight;
368 megaAlignVector[closestRefIndex] += query.getInlineSeq() + '\n';
373 if(index % 1000 == 0){ m->mothurOut(toString(index) + '\n'); }
376 errorSummaryFile.close();
377 errorSeqFile.close();
379 if(qualFileName != "" && reportFileName != ""){
380 printErrorQuality(qScoreErrorMap);
381 printQualityFR(qualForwardMap, qualReverseMap);
384 printErrorFRFile(errorForward, errorReverse);
386 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
388 string errorCountFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.count";
389 ofstream errorCountFile;
390 m->openOutputFile(errorCountFileName, errorCountFile);
391 outputNames.push_back(errorCountFileName); outputTypes["error.count"].push_back(errorCountFileName);
392 m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n\n");
393 m->mothurOut("Errors\tSequences\n");
394 errorCountFile << "Errors\tSequences\n";
395 for(int i=0;i<misMatchCounts.size();i++){
396 m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
397 errorCountFile << i << '\t' << misMatchCounts[i] << endl;
399 errorCountFile.close();
401 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
405 string megAlignmentFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.ref-query";
406 ofstream megAlignmentFile;
407 m->openOutputFile(megAlignmentFileName, megAlignmentFile);
408 outputNames.push_back(megAlignmentFileName); outputTypes["error.ref-query"].push_back(megAlignmentFileName);
410 for(int i=0;i<numRefs;i++){
411 megAlignmentFile << referenceSeqs[i].getInlineSeq() << endl;
412 megAlignmentFile << megaAlignVector[i] << endl;
416 m->mothurOutEndLine();
417 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
418 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
419 m->mothurOutEndLine();
423 catch(exception& e) {
424 m->errorOut(e, "SeqErrorCommand", "execute");
429 //***************************************************************************************************************
431 void SeqErrorCommand::getReferences(){
434 ifstream referenceFile;
435 m->openInputFile(referenceFileName, referenceFile);
437 int numAmbigSeqs = 0;
440 int minEndPos = 100000;
442 while(referenceFile){
443 Sequence currentSeq(referenceFile);
444 int numAmbigs = currentSeq.getAmbigBases();
445 if(numAmbigs > 0){ numAmbigSeqs++; }
447 int startPos = currentSeq.getStartPos();
448 if(startPos > maxStartPos) { maxStartPos = startPos; }
450 int endPos = currentSeq.getEndPos();
451 if(endPos < minEndPos) { minEndPos = endPos; }
452 referenceSeqs.push_back(currentSeq);
453 m->gobble(referenceFile);
455 referenceFile.close();
456 numRefs = referenceSeqs.size();
459 for(int i=0;i<numRefs;i++){
460 referenceSeqs[i].padToPos(maxStartPos);
461 referenceSeqs[i].padFromPos(minEndPos);
464 if(numAmbigSeqs != 0){
465 m->mothurOut("Warning: " + toString(numAmbigSeqs) + " reference sequences have ambiguous bases, these bases will be ignored\n");
469 catch(exception& e) {
470 m->errorOut(e, "SeqErrorCommand", "getReferences");
475 //***************************************************************************************************************
477 Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
479 if(query.getAlignLength() != reference.getAlignLength()){
480 m->mothurOut("Warning: " + toString(query.getName()) + " and " + toString(reference.getName()) + " are different lengths\n");
482 int alignLength = query.getAlignLength();
484 string q = query.getAligned();
485 string r = reference.getAligned();
490 for(int i=0;i<alignLength;i++){
491 if(r[i] != 'N' && q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){ // no missing data and no double gaps
495 if(r[i] == 'A'){ errors.AA++; errors.matches++; errors.sequence += 'm'; }
496 if(r[i] == 'T'){ errors.AT++; errors.sequence += 's'; }
497 if(r[i] == 'G'){ errors.AG++; errors.sequence += 's'; }
498 if(r[i] == 'C'){ errors.AC++; errors.sequence += 's'; }
499 if(r[i] == '-'){ errors.Ai++; errors.sequence += 'i'; }
501 else if(q[i] == 'T'){
502 if(r[i] == 'A'){ errors.TA++; errors.sequence += 's'; }
503 if(r[i] == 'T'){ errors.TT++; errors.matches++; errors.sequence += 'm'; }
504 if(r[i] == 'G'){ errors.TG++; errors.sequence += 's'; }
505 if(r[i] == 'C'){ errors.TC++; errors.sequence += 's'; }
506 if(r[i] == '-'){ errors.Ti++; errors.sequence += 'i'; }
508 else if(q[i] == 'G'){
509 if(r[i] == 'A'){ errors.GA++; errors.sequence += 's'; }
510 if(r[i] == 'T'){ errors.GT++; errors.sequence += 's'; }
511 if(r[i] == 'G'){ errors.GG++; errors.matches++; errors.sequence += 'm'; }
512 if(r[i] == 'C'){ errors.GC++; errors.sequence += 's'; }
513 if(r[i] == '-'){ errors.Gi++; errors.sequence += 'i'; }
515 else if(q[i] == 'C'){
516 if(r[i] == 'A'){ errors.CA++; errors.sequence += 's'; }
517 if(r[i] == 'T'){ errors.CT++; errors.sequence += 's'; }
518 if(r[i] == 'G'){ errors.CG++; errors.sequence += 's'; }
519 if(r[i] == 'C'){ errors.CC++; errors.matches++; errors.sequence += 'm'; }
520 if(r[i] == '-'){ errors.Ci++; errors.sequence += 'i'; }
522 else if(q[i] == 'N'){
523 if(r[i] == 'A'){ errors.NA++; errors.sequence += 'a'; }
524 if(r[i] == 'T'){ errors.NT++; errors.sequence += 'a'; }
525 if(r[i] == 'G'){ errors.NG++; errors.sequence += 'a'; }
526 if(r[i] == 'C'){ errors.NC++; errors.sequence += 'a'; }
527 if(r[i] == '-'){ errors.Ni++; errors.sequence += 'a'; }
529 else if(q[i] == '-' && r[i] != '-'){
530 if(r[i] == 'A'){ errors.dA++; errors.sequence += 'd'; }
531 if(r[i] == 'T'){ errors.dT++; errors.sequence += 'd'; }
532 if(r[i] == 'G'){ errors.dG++; errors.sequence += 'd'; }
533 if(r[i] == 'C'){ errors.dC++; errors.sequence += 'd'; }
538 else if(q[i] == '.' && r[i] != '.'){ // reference extends beyond query
539 if(started == 1){ break; }
541 else if(q[i] != '.' && r[i] == '.'){ // query extends beyond reference
542 if(started == 1){ break; }
544 else if(q[i] == '.' && r[i] == '.'){ // both are missing data
545 if(started == 1){ break; }
549 errors.mismatches = errors.total-errors.matches;
550 errors.errorRate = (double)(errors.total-errors.matches) / (double)errors.total;
551 errors.queryName = query.getName();
552 errors.refName = reference.getName();
556 catch(exception& e) {
557 m->errorOut(e, "SeqErrorCommand", "getErrors");
562 //***************************************************************************************************************
564 map<string, int> SeqErrorCommand::getWeights(){
566 m->openInputFile(namesFileName, nameFile);
569 string redundantSeqs;
570 map<string, int> nameCountMap;
573 nameFile >> seqName >> redundantSeqs;
574 nameCountMap[seqName] = m->getNumNames(redundantSeqs);
581 //***************************************************************************************************************
583 void SeqErrorCommand::printErrorHeader(){
585 errorSummaryFile << "query\treference\tweight\t";
586 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";
587 errorSummaryFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\tnumparents\n";
589 errorSummaryFile << setprecision(6);
590 errorSummaryFile.setf(ios::fixed);
592 catch(exception& e) {
593 m->errorOut(e, "SeqErrorCommand", "printErrorHeader");
598 //***************************************************************************************************************
600 void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs){
603 errorSummaryFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
604 errorSummaryFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
605 errorSummaryFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
606 errorSummaryFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
607 errorSummaryFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
608 errorSummaryFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
609 errorSummaryFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t';
610 errorSummaryFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
612 errorSummaryFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t'; //insertions
613 errorSummaryFile << error.dA + error.dT + error.dG + error.dC << '\t'; //deletions
614 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
615 errorSummaryFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t'; //ambiguities
616 errorSummaryFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << '\t' << numParentSeqs << endl;
618 errorSeqFile << '>' << error.queryName << "\tref:" << error.refName << '\n' << error.sequence << endl;
620 int a=0; int t=1; int g=2; int c=3;
623 if(numParentSeqs == 1 || ignoreChimeras == 0){
624 substitutionMatrix[a][a] += error.weight * error.AA;
625 substitutionMatrix[a][t] += error.weight * error.TA;
626 substitutionMatrix[a][g] += error.weight * error.GA;
627 substitutionMatrix[a][c] += error.weight * error.CA;
628 substitutionMatrix[a][gap] += error.weight * error.dA;
629 substitutionMatrix[a][n] += error.weight * error.NA;
631 substitutionMatrix[t][a] += error.weight * error.AT;
632 substitutionMatrix[t][t] += error.weight * error.TT;
633 substitutionMatrix[t][g] += error.weight * error.GT;
634 substitutionMatrix[t][c] += error.weight * error.CT;
635 substitutionMatrix[t][gap] += error.weight * error.dT;
636 substitutionMatrix[t][n] += error.weight * error.NT;
638 substitutionMatrix[g][a] += error.weight * error.AG;
639 substitutionMatrix[g][t] += error.weight * error.TG;
640 substitutionMatrix[g][g] += error.weight * error.GG;
641 substitutionMatrix[g][c] += error.weight * error.CG;
642 substitutionMatrix[g][gap] += error.weight * error.dG;
643 substitutionMatrix[g][n] += error.weight * error.NG;
645 substitutionMatrix[c][a] += error.weight * error.AC;
646 substitutionMatrix[c][t] += error.weight * error.TC;
647 substitutionMatrix[c][g] += error.weight * error.GC;
648 substitutionMatrix[c][c] += error.weight * error.CC;
649 substitutionMatrix[c][gap] += error.weight * error.dC;
650 substitutionMatrix[c][n] += error.weight * error.NC;
652 substitutionMatrix[gap][a] += error.weight * error.Ai;
653 substitutionMatrix[gap][t] += error.weight * error.Ti;
654 substitutionMatrix[gap][g] += error.weight * error.Gi;
655 substitutionMatrix[gap][c] += error.weight * error.Ci;
656 substitutionMatrix[gap][n] += error.weight * error.Ni;
659 catch(exception& e) {
660 m->errorOut(e, "SeqErrorCommand", "printErrorData");
665 //***************************************************************************************************************
667 void SeqErrorCommand::printSubMatrix(){
669 string subMatrixFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.matrix";
670 ofstream subMatrixFile;
671 m->openOutputFile(subMatrixFileName, subMatrixFile);
672 outputNames.push_back(subMatrixFileName); outputTypes["error.matrix"].push_back(subMatrixFileName);
673 vector<string> bases(6);
680 vector<int> refSums(5,1);
682 for(int i=0;i<5;i++){
683 subMatrixFile << "\tr" << bases[i];
685 for(int j=0;j<6;j++){
686 refSums[i] += substitutionMatrix[i][j];
689 subMatrixFile << endl;
691 for(int i=0;i<6;i++){
692 subMatrixFile << 'q' << bases[i];
693 for(int j=0;j<5;j++){
694 subMatrixFile << '\t' << substitutionMatrix[j][i];
696 subMatrixFile << endl;
699 subMatrixFile << "total";
700 for(int i=0;i<5;i++){
701 subMatrixFile << '\t' << refSums[i];
703 subMatrixFile << endl;
704 subMatrixFile.close();
706 catch(exception& e) {
707 m->errorOut(e, "SeqErrorCommand", "printSubMatrix");
711 //***************************************************************************************************************
713 void SeqErrorCommand::printErrorFRFile(map<char, vector<int> > errorForward, map<char, vector<int> > errorReverse){
715 string errorForwardFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq.forward";
716 ofstream errorForwardFile;
717 m->openOutputFile(errorForwardFileName, errorForwardFile);
718 outputNames.push_back(errorForwardFileName); outputTypes["error.forward"].push_back(errorForwardFileName);
720 errorForwardFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
721 for(int i=0;i<maxLength;i++){
722 float match = (float)errorForward['m'][i];
723 float subst = (float)errorForward['s'][i];
724 float insert = (float)errorForward['i'][i];
725 float del = (float)errorForward['d'][i];
726 float amb = (float)errorForward['a'][i];
727 float total = match + subst + insert + del + amb;
728 if(total == 0){ break; }
729 errorForwardFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
731 errorForwardFile.close();
733 string errorReverseFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.seq.reverse";
734 ofstream errorReverseFile;
735 m->openOutputFile(errorReverseFileName, errorReverseFile);
736 outputNames.push_back(errorReverseFileName); outputTypes["error.reverse"].push_back(errorReverseFileName);
738 errorReverseFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
739 for(int i=0;i<maxLength;i++){
740 float match = (float)errorReverse['m'][i];
741 float subst = (float)errorReverse['s'][i];
742 float insert = (float)errorReverse['i'][i];
743 float del = (float)errorReverse['d'][i];
744 float amb = (float)errorReverse['a'][i];
745 float total = match + subst + insert + del + amb;
746 if(total == 0){ break; }
747 errorReverseFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
749 errorReverseFile.close();
751 catch(exception& e) {
752 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
757 //***************************************************************************************************************
759 void SeqErrorCommand::printErrorQuality(map<char, vector<int> > qScoreErrorMap){
762 string errorQualityFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.quality";
763 ofstream errorQualityFile;
764 m->openOutputFile(errorQualityFileName, errorQualityFile);
765 outputNames.push_back(errorQualityFileName); outputTypes["error.quality"].push_back(errorQualityFileName);
767 errorQualityFile << "qscore\tmatches\tsubstitutions\tinsertions\tambiguous" << endl;
768 for(int i=0;i<41;i++){
769 errorQualityFile << i << '\t' << qScoreErrorMap['m'][i] << '\t' << qScoreErrorMap['s'][i] << '\t' << qScoreErrorMap['i'][i] << '\t'<< qScoreErrorMap['a'][i] << endl;
771 errorQualityFile.close();
773 catch(exception& e) {
774 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
780 //***************************************************************************************************************
782 void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector<vector<int> > qualReverseMap){
786 int numColumns = qualForwardMap[0].size();
788 for(int i=0;i<qualForwardMap.size();i++){
789 for(int j=0;j<numColumns;j++){
790 if(qualForwardMap[i][j] != 0){
791 if(numRows < i) { numRows = i+20; }
796 string qualityForwardFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.qual.forward";
797 ofstream qualityForwardFile;
798 m->openOutputFile(qualityForwardFileName, qualityForwardFile);
799 outputNames.push_back(qualityForwardFileName); outputTypes["error.qual.forward"].push_back(qualityForwardFileName);
801 for(int i=0;i<numColumns;i++){ qualityForwardFile << '\t' << i; } qualityForwardFile << endl;
803 for(int i=0;i<numRows;i++){
804 qualityForwardFile << i+1;
805 for(int j=0;j<numColumns;j++){
806 qualityForwardFile << '\t' << qualForwardMap[i][j];
809 qualityForwardFile << endl;
811 qualityForwardFile.close();
814 string qualityReverseFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.qual.reverse";
815 ofstream qualityReverseFile;
816 m->openOutputFile(qualityReverseFileName, qualityReverseFile);
817 outputNames.push_back(qualityReverseFileName); outputTypes["error.qual.reverse"].push_back(qualityReverseFileName);
819 for(int i=0;i<numColumns;i++){ qualityReverseFile << '\t' << i; } qualityReverseFile << endl;
820 for(int i=0;i<numRows;i++){
822 qualityReverseFile << i+1;
823 for(int j=0;j<numColumns;j++){
824 qualityReverseFile << '\t' << qualReverseMap[i][j];
826 qualityReverseFile << endl;
828 qualityReverseFile.close();
830 catch(exception& e) {
831 m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
837 //***************************************************************************************************************