5 * Created by Pat Schloss on 7/15/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "seqerrorcommand.h"
12 //**********************************************************************************************************************
13 vector<string> SeqErrorCommand::getValidParameters(){
15 string Array[] = {"query", "reference", "name", "threshold", "inputdir", "outputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "SeqErrorCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 SeqErrorCommand::SeqErrorCommand(){
27 //initialize outputTypes
28 vector<string> tempOutNames;
29 outputTypes["error"] = tempOutNames;
30 outputTypes["count"] = tempOutNames;
33 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
37 //**********************************************************************************************************************
38 vector<string> SeqErrorCommand::getRequiredParameters(){
40 string Array[] = {"query","reference"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "SeqErrorCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> SeqErrorCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "SeqErrorCommand", "getRequiredFiles");
60 //***************************************************************************************************************
62 SeqErrorCommand::SeqErrorCommand(string option) {
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
73 //valid paramters for this command
74 string AlignArray[] = {"query", "reference", "name", "threshold", "inputdir", "outputdir"};
76 //need to implement name file option
78 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
80 OptionParser parser(option);
81 map<string,string> parameters = parser.getParameters();
83 ValidParameters validParameter;
84 map<string,string>::iterator it;
86 //check to make sure all parameters are valid for command
87 for (it = parameters.begin(); it != parameters.end(); it++) {
88 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
91 //initialize outputTypes
92 vector<string> tempOutNames;
93 outputTypes["error"] = tempOutNames;
94 outputTypes["count"] = tempOutNames;
96 //if the user changes the input directory command factory will send this info to us in the output parameter
97 string inputDir = validParameter.validFile(parameters, "inputdir", false);
98 if (inputDir == "not found"){ inputDir = ""; }
101 it = parameters.find("query");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["query"] = inputDir + it->second; }
109 it = parameters.find("reference");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["reference"] = inputDir + it->second; }
117 it = parameters.find("name");
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["name"] = inputDir + it->second; }
126 //check for required parameters
127 queryFileName = validParameter.validFile(parameters, "query", true);
128 if (queryFileName == "not found") { m->mothurOut("query is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
129 else if (queryFileName == "not open") { abort = true; }
131 referenceFileName = validParameter.validFile(parameters, "reference", true);
132 if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
133 else if (referenceFileName == "not open") { abort = true; }
135 //if the user changes the output directory command factory will send this info to us in the output parameter
136 namesFileName = validParameter.validFile(parameters, "name", true);
137 if(namesFileName == "not found"){ namesFileName = ""; }
138 cout << namesFileName << endl;
140 outputDir = validParameter.validFile(parameters, "outputdir", false);
141 if (outputDir == "not found"){
143 outputDir += m->hasPath(queryFileName); //if user entered a file with a path then preserve it
146 //check for optional parameter and set defaults
147 // ...at some point should added some additional type checking...
148 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found") { temp = "1.00"; }
149 convert(temp, threshold);
151 errorFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".errors";
152 m->openOutputFile(errorFileName, errorFile);
153 outputNames.push_back(errorFileName); outputTypes["error"].push_back(errorFileName);
157 catch(exception& e) {
158 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
163 //**********************************************************************************************************************
165 void SeqErrorCommand::help(){
167 m->mothurOut("The seq.error command reads a query alignment file and a reference alignment file and creates .....\n");
171 m->mothurOut("Example seq.error(...).\n");
172 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
173 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n\n");
175 catch(exception& e) {
176 m->errorOut(e, "SeqErrorCommand", "help");
181 //***************************************************************************************************************
183 SeqErrorCommand::~SeqErrorCommand(){ errorFile.close(); }
185 //***************************************************************************************************************
187 int SeqErrorCommand::execute(){
189 if (abort == true) { return 0; }
191 getReferences(); //read in reference sequences - make sure there's no ambiguous bases
193 map<string, int> weights;
194 if(namesFileName != ""){ weights = getWeights(); }
197 m->openInputFile(queryFileName, queryFile);
200 int totalMatches = 0;
202 vector<int> misMatchCounts(11, 0);
206 map<string, int>::iterator it;
211 Sequence query(queryFile);
213 for(int i=0;i<numRefs;i++){
214 Compare currCompare = getErrors(query, referenceSeqs[i]);
216 if(currCompare.errorRate < minCompare.errorRate){
217 minCompare = currCompare;
222 if(namesFileName != ""){
223 it = weights.find(query.getName());
224 minCompare.weight = it->second;
227 minCompare.weight = 1;
230 printErrorData(minCompare);
232 if(minCompare.errorRate < threshold){
233 totalBases += (minCompare.total * minCompare.weight);
234 totalMatches += minCompare.matches * minCompare.weight;
235 if(minCompare.mismatches > maxMismatch){
236 maxMismatch = minCompare.mismatches;
237 misMatchCounts.resize(maxMismatch + 1, 0);
239 misMatchCounts[minCompare.mismatches] += minCompare.weight;
249 string errorCountFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".count";
250 ofstream errorCountFile;
251 m->openOutputFile(errorCountFileName, errorCountFile);
252 outputNames.push_back(errorCountFileName); outputTypes["count"].push_back(errorCountFileName);
254 m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n\n");
255 m->mothurOut("Errors\tSequences\n");
257 errorCountFile << "Errors\tSequences\n";
259 for(int i=0;i<misMatchCounts.size();i++){
260 m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
261 errorCountFile << i << '\t' << misMatchCounts[i] << endl;
266 catch(exception& e) {
267 m->errorOut(e, "SeqErrorCommand", "execute");
272 //***************************************************************************************************************
274 void SeqErrorCommand::getReferences(){
277 ifstream referenceFile;
278 m->openInputFile(referenceFileName, referenceFile);
280 while(referenceFile){
281 Sequence currentSeq(referenceFile);
282 int numAmbigs = currentSeq.getAmbigBases();
285 m->mothurOut("Warning: " + toString(currentSeq.getName()) + " has " + toString(numAmbigs) + " ambiguous bases, these bases will be removed\n");
286 currentSeq.removeAmbigBases();
288 referenceSeqs.push_back(currentSeq);
289 m->gobble(referenceFile);
291 numRefs = referenceSeqs.size();
293 referenceFile.close();
295 catch(exception& e) {
296 m->errorOut(e, "SeqErrorCommand", "getReferences");
301 //***************************************************************************************************************
303 Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
305 if(query.getAlignLength() != reference.getAlignLength()){
306 m->mothurOut("Warning: " + toString(query.getName()) + " and " + toString(reference.getName()) + " are different lengths\n");
308 int alignLength = query.getAlignLength();
310 string q = query.getAligned();
311 string r = reference.getAligned();
316 for(int i=0;i<alignLength;i++){
317 if(q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){ // no missing data and no double gaps
321 if(r[i] == 'A'){ errors.AA++; errors.matches++; }
322 if(r[i] == 'T'){ errors.AT++; }
323 if(r[i] == 'G'){ errors.AG++; }
324 if(r[i] == 'C'){ errors.AC++; }
325 if(r[i] == '-'){ errors.Ai++; }
327 else if(q[i] == 'T'){
328 if(r[i] == 'A'){ errors.TA++; }
329 if(r[i] == 'T'){ errors.TT++; errors.matches++; }
330 if(r[i] == 'G'){ errors.TG++; }
331 if(r[i] == 'C'){ errors.TC++; }
332 if(r[i] == '-'){ errors.Ti++; }
334 else if(q[i] == 'G'){
335 if(r[i] == 'A'){ errors.GA++; }
336 if(r[i] == 'T'){ errors.GT++; }
337 if(r[i] == 'G'){ errors.GG++; errors.matches++; }
338 if(r[i] == 'C'){ errors.GC++; }
339 if(r[i] == '-'){ errors.Gi++; }
341 else if(q[i] == 'C'){
342 if(r[i] == 'A'){ errors.CA++; }
343 if(r[i] == 'T'){ errors.CT++; }
344 if(r[i] == 'G'){ errors.CG++; }
345 if(r[i] == 'C'){ errors.CC++; errors.matches++; }
346 if(r[i] == '-'){ errors.Ci++; }
348 else if(q[i] == 'N'){
349 if(r[i] == 'A'){ errors.NA++; }
350 if(r[i] == 'T'){ errors.NT++; }
351 if(r[i] == 'G'){ errors.NG++; }
352 if(r[i] == 'C'){ errors.NC++; }
353 if(r[i] == '-'){ errors.Ni++; }
355 else if(q[i] == '-' && r[i] != '-'){
356 if(r[i] == 'A'){ errors.dA++; }
357 if(r[i] == 'T'){ errors.dT++; }
358 if(r[i] == 'G'){ errors.dG++; }
359 if(r[i] == 'C'){ errors.dC++; }
364 else if(q[i] == '.' && r[i] != '.'){ // reference extends beyond query
365 if(started == 1){ break; }
367 else if(q[i] != '.' && r[i] == '.'){ // query extends beyond reference
368 m->mothurOut("Warning: " + toString(query.getName()) + " extend beyond " + toString(reference.getName()) + ". Ignoring the extra bases in the query\n");
369 if(started == 1){ break; }
371 else if(q[i] == '.' && r[i] == '.'){ // both are missing data
372 if(started == 1){ break; }
376 errors.mismatches = errors.total-errors.matches;
377 errors.errorRate = (double)(errors.total-errors.matches) / (double)errors.total;
378 errors.queryName = query.getName();
379 errors.refName = reference.getName();
383 catch(exception& e) {
384 m->errorOut(e, "SeqErrorCommand", "getErrors");
389 //***************************************************************************************************************
391 map<string, int> SeqErrorCommand::getWeights(){
393 m->openInputFile(namesFileName, nameFile);
396 string redundantSeqs;
397 map<string, int> nameCountMap;
400 nameFile >> seqName >> redundantSeqs;
401 nameCountMap[seqName] = m->getNumNames(redundantSeqs);
408 //***************************************************************************************************************
410 void SeqErrorCommand::printErrorHeader(){
412 errorFile << "query\treference\tweight\t";
413 errorFile << "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";
414 errorFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\n";
416 errorFile << setprecision(6);
417 errorFile.setf(ios::fixed);
419 catch(exception& e) {
420 m->errorOut(e, "SeqErrorCommand", "printErrorHeader");
425 //***************************************************************************************************************
427 void SeqErrorCommand::printErrorData(Compare error){
429 errorFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
430 errorFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
431 errorFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
432 errorFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
433 errorFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
434 errorFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
435 errorFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t' ;
436 errorFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
438 errorFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t'; //insertions
439 errorFile << error.dA + error.dT + error.dG + error.dC << '\t'; //deletions
440 errorFile << 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
441 errorFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t'; //ambiguities
442 errorFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << endl;
445 catch(exception& e) {
446 m->errorOut(e, "SeqErrorCommand", "printErrorData");
451 //***************************************************************************************************************