5 * Created by Pat Schloss on 7/15/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "seqerrorcommand.h"
12 //***************************************************************************************************************
14 SeqErrorCommand::SeqErrorCommand(string option) {
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string AlignArray[] = {"query", "reference", "name", "threshold"};
28 //need to implement name file option
30 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
32 OptionParser parser(option);
33 map<string,string> parameters = parser.getParameters();
35 ValidParameters validParameter;
36 map<string,string>::iterator it;
38 //check to make sure all parameters are valid for command
39 for (it = parameters.begin(); it != parameters.end(); it++) {
40 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
43 //if the user changes the input directory command factory will send this info to us in the output parameter
44 string inputDir = validParameter.validFile(parameters, "inputdir", false);
45 if (inputDir == "not found"){ inputDir = ""; }
48 it = parameters.find("query");
49 //user has given a template file
50 if(it != parameters.end()){
51 path = hasPath(it->second);
52 //if the user has not given a path then, add inputdir. else leave path alone.
53 if (path == "") { parameters["query"] = inputDir + it->second; }
56 it = parameters.find("reference");
57 //user has given a template file
58 if(it != parameters.end()){
59 path = hasPath(it->second);
60 //if the user has not given a path then, add inputdir. else leave path alone.
61 if (path == "") { parameters["reference"] = inputDir + it->second; }
64 it = parameters.find("name");
65 //user has given a template file
66 if(it != parameters.end()){
67 path = hasPath(it->second);
68 //if the user has not given a path then, add inputdir. else leave path alone.
69 if (path == "") { parameters["name"] = inputDir + it->second; }
73 //check for required parameters
74 queryFileName = validParameter.validFile(parameters, "query", true);
75 if (queryFileName == "not found") { m->mothurOut("query is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
76 else if (queryFileName == "not open") { abort = true; }
78 referenceFileName = validParameter.validFile(parameters, "reference", true);
79 if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
80 else if (referenceFileName == "not open") { abort = true; }
82 //if the user changes the output directory command factory will send this info to us in the output parameter
83 namesFileName = validParameter.validFile(parameters, "name", true);
84 if(namesFileName == "not found"){ namesFileName = ""; }
85 cout << namesFileName << endl;
87 outputDir = validParameter.validFile(parameters, "outputdir", false);
88 if (outputDir == "not found"){
90 outputDir += hasPath(queryFileName); //if user entered a file with a path then preserve it
93 //check for optional parameter and set defaults
94 // ...at some point should added some additional type checking...
95 temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found") { temp = "1.00"; }
96 convert(temp, threshold);
98 errorFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".errors";
99 openOutputFile(errorFileName, errorFile);
103 catch(exception& e) {
104 m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
109 //**********************************************************************************************************************
111 void SeqErrorCommand::help(){
113 m->mothurOut("The seq.error command reads a query alignment file and a reference alignment file and creates .....\n");
117 m->mothurOut("Example seq.error(...).\n");
118 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
119 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n\n");
121 catch(exception& e) {
122 m->errorOut(e, "SeqErrorCommand", "help");
127 //***************************************************************************************************************
129 SeqErrorCommand::~SeqErrorCommand(){ errorFile.close(); }
131 //***************************************************************************************************************
133 int SeqErrorCommand::execute(){
135 if (abort == true) { return 0; }
137 getReferences(); //read in reference sequences - make sure there's no ambiguous bases
139 map<string, int> weights;
140 if(namesFileName != ""){ weights = getWeights(); }
143 openInputFile(queryFileName, queryFile);
146 int totalMatches = 0;
148 vector<int> misMatchCounts(11, 0);
152 map<string, int>::iterator it;
157 Sequence query(queryFile);
159 for(int i=0;i<numRefs;i++){
160 Compare currCompare = getErrors(query, referenceSeqs[i]);
162 if(currCompare.errorRate < minCompare.errorRate){
163 minCompare = currCompare;
168 if(namesFileName != ""){
169 it = weights.find(query.getName());
170 minCompare.weight = it->second;
173 minCompare.weight = 1;
176 printErrorData(minCompare);
178 if(minCompare.errorRate < threshold){
179 totalBases += (minCompare.total * minCompare.weight);
180 totalMatches += minCompare.matches * minCompare.weight;
181 if(minCompare.mismatches > maxMismatch){
182 maxMismatch = minCompare.mismatches;
183 misMatchCounts.resize(maxMismatch + 1, 0);
185 misMatchCounts[minCompare.mismatches] += minCompare.weight;
195 string errorCountFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".count";
196 ofstream errorCountFile;
197 openOutputFile(errorCountFileName, errorCountFile);
199 m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n\n");
200 m->mothurOut("Errors\tSequences\n");
202 errorCountFile << "Errors\tSequences\n";
204 for(int i=0;i<misMatchCounts.size();i++){
205 m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
206 errorCountFile << i << '\t' << misMatchCounts[i] << endl;
211 catch(exception& e) {
212 m->errorOut(e, "SeqErrorCommand", "execute");
217 //***************************************************************************************************************
219 void SeqErrorCommand::getReferences(){
222 ifstream referenceFile;
223 openInputFile(referenceFileName, referenceFile);
225 while(referenceFile){
226 Sequence currentSeq(referenceFile);
227 int numAmbigs = currentSeq.getAmbigBases();
230 m->mothurOut("Warning: " + toString(currentSeq.getName()) + " has " + toString(numAmbigs) + " ambiguous bases, these bases will be removed\n");
231 currentSeq.removeAmbigBases();
233 referenceSeqs.push_back(currentSeq);
234 gobble(referenceFile);
236 numRefs = referenceSeqs.size();
238 referenceFile.close();
240 catch(exception& e) {
241 m->errorOut(e, "SeqErrorCommand", "getReferences");
246 //***************************************************************************************************************
248 Compare SeqErrorCommand::getErrors(Sequence query, Sequence reference){
250 if(query.getAlignLength() != reference.getAlignLength()){
251 m->mothurOut("Warning: " + toString(query.getName()) + " and " + toString(reference.getName()) + " are different lengths\n");
253 int alignLength = query.getAlignLength();
255 string q = query.getAligned();
256 string r = reference.getAligned();
261 for(int i=0;i<alignLength;i++){
262 if(q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){ // no missing data and no double gaps
266 if(r[i] == 'A'){ errors.AA++; errors.matches++; }
267 if(r[i] == 'T'){ errors.AT++; }
268 if(r[i] == 'G'){ errors.AG++; }
269 if(r[i] == 'C'){ errors.AC++; }
270 if(r[i] == '-'){ errors.Ai++; }
272 else if(q[i] == 'T'){
273 if(r[i] == 'A'){ errors.TA++; }
274 if(r[i] == 'T'){ errors.TT++; errors.matches++; }
275 if(r[i] == 'G'){ errors.TG++; }
276 if(r[i] == 'C'){ errors.TC++; }
277 if(r[i] == '-'){ errors.Ti++; }
279 else if(q[i] == 'G'){
280 if(r[i] == 'A'){ errors.GA++; }
281 if(r[i] == 'T'){ errors.GT++; }
282 if(r[i] == 'G'){ errors.GG++; errors.matches++; }
283 if(r[i] == 'C'){ errors.GC++; }
284 if(r[i] == '-'){ errors.Gi++; }
286 else if(q[i] == 'C'){
287 if(r[i] == 'A'){ errors.CA++; }
288 if(r[i] == 'T'){ errors.CT++; }
289 if(r[i] == 'G'){ errors.CG++; }
290 if(r[i] == 'C'){ errors.CC++; errors.matches++; }
291 if(r[i] == '-'){ errors.Ci++; }
293 else if(q[i] == 'N'){
294 if(r[i] == 'A'){ errors.NA++; }
295 if(r[i] == 'T'){ errors.NT++; }
296 if(r[i] == 'G'){ errors.NG++; }
297 if(r[i] == 'C'){ errors.NC++; }
298 if(r[i] == '-'){ errors.Ni++; }
300 else if(q[i] == '-' && r[i] != '-'){
301 if(r[i] == 'A'){ errors.dA++; }
302 if(r[i] == 'T'){ errors.dT++; }
303 if(r[i] == 'G'){ errors.dG++; }
304 if(r[i] == 'C'){ errors.dC++; }
309 else if(q[i] == '.' && r[i] != '.'){ // reference extends beyond query
310 if(started == 1){ break; }
312 else if(q[i] != '.' && r[i] == '.'){ // query extends beyond reference
313 m->mothurOut("Warning: " + toString(query.getName()) + " extend beyond " + toString(reference.getName()) + ". Ignoring the extra bases in the query\n");
314 if(started == 1){ break; }
316 else if(q[i] == '.' && r[i] == '.'){ // both are missing data
317 if(started == 1){ break; }
321 errors.mismatches = errors.total-errors.matches;
322 errors.errorRate = (double)(errors.total-errors.matches) / (double)errors.total;
323 errors.queryName = query.getName();
324 errors.refName = reference.getName();
328 catch(exception& e) {
329 m->errorOut(e, "SeqErrorCommand", "getErrors");
334 //***************************************************************************************************************
336 map<string, int> SeqErrorCommand::getWeights(){
338 openInputFile(namesFileName, nameFile);
341 string redundantSeqs;
342 map<string, int> nameCountMap;
345 nameFile >> seqName >> redundantSeqs;
346 nameCountMap[seqName] = getNumNames(redundantSeqs);
353 //***************************************************************************************************************
355 void SeqErrorCommand::printErrorHeader(){
357 errorFile << "query\treference\tweight\t";
358 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";
359 errorFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\n";
361 errorFile << setprecision(6);
362 errorFile.setf(ios::fixed);
364 catch(exception& e) {
365 m->errorOut(e, "SeqErrorCommand", "printErrorHeader");
370 //***************************************************************************************************************
372 void SeqErrorCommand::printErrorData(Compare error){
374 errorFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
375 errorFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
376 errorFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
377 errorFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
378 errorFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
379 errorFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
380 errorFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t' ;
381 errorFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
383 errorFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t'; //insertions
384 errorFile << error.dA + error.dT + error.dG + error.dC << '\t'; //deletions
385 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
386 errorFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t'; //ambiguities
387 errorFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << endl;
390 catch(exception& e) {
391 m->errorOut(e, "SeqErrorCommand", "printErrorData");
396 //***************************************************************************************************************