2 * secondarystructurecommand.cpp
5 * Created by westcott on 9/18/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "secondarystructurecommand.h"
11 #include "sequence.hpp"
12 #include "counttable.h"
14 //**********************************************************************************************************************
15 vector<string> AlignCheckCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","aligncheck",false,true,true); parameters.push_back(pfasta);
18 CommandParameter pmap("map", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(pmap);
19 CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","",false,false); parameters.push_back(pname);
20 CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","",false,false); parameters.push_back(pcount);
21 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
22 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
24 vector<string> myArray;
25 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
29 m->errorOut(e, "AlignCheckCommand", "setParameters");
33 //**********************************************************************************************************************
34 string AlignCheckCommand::getHelpString(){
36 string helpString = "";
37 helpString += "The align.check command reads a fasta file and map file as well as an optional name or count file.\n";
38 helpString += "It outputs a file containing the secondary structure matches in the .align.check file.\n";
39 helpString += "The align.check command parameters are fasta and map, both are required.\n";
40 helpString += "The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n";
41 helpString += "Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n";
42 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
46 m->errorOut(e, "AlignCheckCommand", "getHelpString");
50 //**********************************************************************************************************************
51 string AlignCheckCommand::getOutputPattern(string type) {
55 if (type == "aligncheck") { pattern = "[filename],align.check"; }
56 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
61 m->errorOut(e, "AlignCheckCommand", "getOutputPattern");
65 //**********************************************************************************************************************
66 AlignCheckCommand::AlignCheckCommand(){
68 abort = true; calledHelp = true;
70 vector<string> tempOutNames;
71 outputTypes["aligncheck"] = tempOutNames;
74 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
78 //**********************************************************************************************************************
80 AlignCheckCommand::AlignCheckCommand(string option) {
82 abort = false; calledHelp = false;
85 //allow user to run help
86 if(option == "help") { help(); abort = true; calledHelp = true; }
87 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90 vector<string> myArray = setParameters();
92 OptionParser parser(option);
93 map<string,string> parameters = parser.getParameters();
95 ValidParameters validParameter;
96 map<string,string>::iterator it;
98 //check to make sure all parameters are valid for command
99 for (it = parameters.begin(); it != parameters.end(); it++) {
100 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //initialize outputTypes
104 vector<string> tempOutNames;
105 outputTypes["aligncheck"] = tempOutNames;
107 //if the user changes the input directory command factory will send this info to us in the output parameter
108 string inputDir = validParameter.validFile(parameters, "inputdir", false);
109 if (inputDir == "not found"){ inputDir = ""; }
112 it = parameters.find("fasta");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["fasta"] = inputDir + it->second; }
120 it = parameters.find("map");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["map"] = inputDir + it->second; }
128 it = parameters.find("name");
129 //user has given a template file
130 if(it != parameters.end()){
131 path = m->hasPath(it->second);
132 //if the user has not given a path then, add inputdir. else leave path alone.
133 if (path == "") { parameters["name"] = inputDir + it->second; }
136 it = parameters.find("count");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["count"] = inputDir + it->second; }
145 //check for required parameters
146 mapfile = validParameter.validFile(parameters, "map", true);
147 if (mapfile == "not open") { abort = true; }
148 else if (mapfile == "not found") { mapfile = ""; m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }
150 fastafile = validParameter.validFile(parameters, "fasta", true);
151 if (fastafile == "not open") { fastafile = ""; abort = true; }
152 else if (fastafile == "not found") {
153 fastafile = m->getFastaFile();
154 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
155 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
156 }else { m->setFastaFile(fastafile); }
158 namefile = validParameter.validFile(parameters, "name", true);
159 if (namefile == "not open") { namefile = ""; abort = true; }
160 else if (namefile == "not found") { namefile = ""; }
161 else { m->setNameFile(namefile); }
163 countfile = validParameter.validFile(parameters, "count", true);
164 if (countfile == "not open") { abort = true; countfile = ""; }
165 else if (countfile == "not found") { countfile = ""; }
166 else { m->setCountTableFile(countfile); }
168 if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
170 //if the user changes the output directory command factory will send this info to us in the output parameter
171 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
173 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
176 if (countfile == "") {
177 if ((namefile == "") && (fastafile != "")){
178 vector<string> files; files.push_back(fastafile);
179 parser.getNameFile(files);
185 catch(exception& e) {
186 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
190 //**********************************************************************************************************************
192 int AlignCheckCommand::execute(){
195 if (abort == true) { if (calledHelp) { return 0; } return 2; }
197 //get secondary structure info.
200 if (namefile != "") { nameMap = m->readNames(namefile); }
201 else if (countfile != "") {
203 ct.readTable(countfile, false);
204 nameMap = ct.getNameMap();
207 if (m->control_pressed) { return 0; }
210 m->openInputFile(fastafile, in);
213 map<string, string> variables;
214 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
215 string outfile = getOutputFileName("aligncheck",variables);
216 m->openOutputFile(outfile, out);
219 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
220 out << "loop" << '\t' << "tilde" << '\t' << "total" << '\t' << "numseqs" << endl;
232 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outfile); return 0; }
234 Sequence seq(in); m->gobble(in);
235 if (seq.getName() != "") {
236 statData data = getStats(seq.getAligned());
238 if (haderror == 1) { m->control_pressed = true; break; }
241 if ((namefile != "") || (countfile != "")) {
242 //make sure this sequence is in the namefile, else error
243 map<string, int>::iterator it = nameMap.find(seq.getName());
245 if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + seq.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
246 else { num = it->second; }
249 //for each sequence this sequence represents
250 for (int i = 0; i < num; i++) {
251 pound.push_back(data.pound);
252 dash.push_back(data.dash);
253 plus.push_back(data.plus);
254 equal.push_back(data.equal);
255 loop.push_back(data.loop);
256 tilde.push_back(data.tilde);
257 total.push_back(data.total);
261 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
262 out << data.loop << '\t' << data.tilde << '\t' << data.total << '\t' << num << endl;
269 if (m->control_pressed) { m->mothurRemove(outfile); return 0; }
271 sort(pound.begin(), pound.end());
272 sort(dash.begin(), dash.end());
273 sort(plus.begin(), plus.end());
274 sort(equal.begin(), equal.end());
275 sort(loop.begin(), loop.end());
276 sort(tilde.begin(), tilde.end());
277 sort(total.begin(), total.end());
278 int size = pound.size();
280 int ptile0_25 = int(size * 0.025);
281 int ptile25 = int(size * 0.250);
282 int ptile50 = int(size * 0.500);
283 int ptile75 = int(size * 0.750);
284 int ptile97_5 = int(size * 0.975);
285 int ptile100 = size - 1;
287 if (m->control_pressed) { m->mothurRemove(outfile); return 0; }
289 m->mothurOutEndLine();
290 m->mothurOut("\t\tPound\tDash\tPlus\tEqual\tLoop\tTilde\tTotal"); m->mothurOutEndLine();
291 m->mothurOut("Minimum:\t" + toString(pound[0]) + "\t" + toString(dash[0]) + "\t" + toString(plus[0]) + "\t" + toString(equal[0]) + "\t" + toString(loop[0]) + "\t" + toString(tilde[0]) + "\t" + toString(total[0])); m->mothurOutEndLine();
292 m->mothurOut("2.5%-tile:\t" + toString(pound[ptile0_25]) + "\t" + toString(dash[ptile0_25]) + "\t" + toString(plus[ptile0_25]) + "\t" + toString(equal[ptile0_25]) + "\t"+ toString(loop[ptile0_25]) + "\t"+ toString(tilde[ptile0_25]) + "\t"+ toString(total[ptile0_25])); m->mothurOutEndLine();
293 m->mothurOut("25%-tile:\t" + toString(pound[ptile25]) + "\t" + toString(dash[ptile25]) + "\t" + toString(plus[ptile25]) + "\t" + toString(equal[ptile25]) + "\t" + toString(loop[ptile25]) + "\t" + toString(tilde[ptile25]) + "\t" + toString(total[ptile25])); m->mothurOutEndLine();
294 m->mothurOut("Median: \t" + toString(pound[ptile50]) + "\t" + toString(dash[ptile50]) + "\t" + toString(plus[ptile50]) + "\t" + toString(equal[ptile50]) + "\t" + toString(loop[ptile50]) + "\t" + toString(tilde[ptile50]) + "\t" + toString(total[ptile50])); m->mothurOutEndLine();
295 m->mothurOut("75%-tile:\t" + toString(pound[ptile75]) + "\t" + toString(dash[ptile75]) + "\t" + toString(plus[ptile75]) + "\t" + toString(equal[ptile75]) + "\t" + toString(loop[ptile75]) + "\t" + toString(tilde[ptile75]) + "\t" + toString(total[ptile75])); m->mothurOutEndLine();
296 m->mothurOut("97.5%-tile:\t" + toString(pound[ptile97_5]) + "\t" + toString(dash[ptile97_5]) + "\t" + toString(plus[ptile97_5]) + "\t" + toString(equal[ptile97_5]) + "\t" + toString(loop[ptile97_5]) + "\t" + toString(tilde[ptile97_5]) + "\t" + toString(total[ptile97_5])); m->mothurOutEndLine();
297 m->mothurOut("Maximum:\t" + toString(pound[ptile100]) + "\t" + toString(dash[ptile100]) + "\t" + toString(plus[ptile100]) + "\t" + toString(equal[ptile100]) + "\t" + toString(loop[ptile100]) + "\t" + toString(tilde[ptile100]) + "\t" + toString(total[ptile100])); m->mothurOutEndLine();
298 if ((namefile == "") && (countfile == "")) { m->mothurOut("# of Seqs:\t" + toString(count)); m->mothurOutEndLine(); }
299 else { m->mothurOut("# of unique seqs:\t" + toString(count)); m->mothurOutEndLine(); m->mothurOut("total # of seqs:\t" + toString(size)); m->mothurOutEndLine(); }
302 m->mothurOutEndLine();
303 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
304 m->mothurOut(outfile); m->mothurOutEndLine(); outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
305 m->mothurOutEndLine();
310 catch(exception& e) {
311 m->errorOut(e, "AlignCheckCommand", "execute");
315 //**********************************************************************************************************************
316 void AlignCheckCommand::readMap(){
319 structMap.resize(1, 0);
322 m->openInputFile(mapfile, in);
327 structMap.push_back(position);
332 seqLength = structMap.size();
335 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
336 for(int i=0;i<seqLength;i++){
337 if(structMap[i] != 0){
338 if(structMap[structMap[i]] != i){
339 m->mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
346 catch(exception& e) {
347 m->errorOut(e, "AlignCheckCommand", "readMap");
351 /**************************************************************************************************/
353 statData AlignCheckCommand::getStats(string sequence){
357 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
359 int length = sequence.length();
361 if (length != seqLength) { m->mothurOut("your sequences are " + toString(length) + " long, but your map file only contains " + toString(seqLength) + " entries. please correct."); m->mothurOutEndLine(); haderror = 1; return data; }
363 for(int i=1;i<length;i++){
364 if(structMap[i] != 0){
365 if(sequence[i] == 'A'){
366 if(sequence[structMap[i]] == 'T') { data.tilde++; }
367 else if(sequence[structMap[i]] == 'A') { data.pound++; }
368 else if(sequence[structMap[i]] == 'G') { data.equal++; }
369 else if(sequence[structMap[i]] == 'C') { data.pound++; }
370 else if(sequence[structMap[i]] == '-') { data.pound++; }
373 else if(sequence[i] == 'T'){
374 if(sequence[structMap[i]] == 'T') { data.plus++; }
375 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
376 else if(sequence[structMap[i]] == 'G') { data.dash++; }
377 else if(sequence[structMap[i]] == 'C') { data.pound++; }
378 else if(sequence[structMap[i]] == '-') { data.pound++; }
381 else if(sequence[i] == 'G'){
382 if(sequence[structMap[i]] == 'T') { data.dash++; }
383 else if(sequence[structMap[i]] == 'A') { data.equal++; }
384 else if(sequence[structMap[i]] == 'G') { data.pound++; }
385 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
386 else if(sequence[structMap[i]] == '-') { data.pound++; }
389 else if(sequence[i] == 'C'){
390 if(sequence[structMap[i]] == 'T') { data.pound++; }
391 else if(sequence[structMap[i]] == 'A') { data.pound++; }
392 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
393 else if(sequence[structMap[i]] == 'C') { data.pound++; }
394 else if(sequence[structMap[i]] == '-') { data.pound++; }
397 else if(sequence[i] == '-'){
398 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
399 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
400 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
401 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
402 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
405 else if(isalnum(sequence[i])){
413 catch(exception& e) {
414 m->errorOut(e, "AlignCheckCommand", "getStats");
418 //**********************************************************************************************************************