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",false,true); parameters.push_back(pfasta);
18 CommandParameter pmap("map", "InputTypes", "", "", "none", "none", "none",false,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::getOutputFileNameTag(string type, string inputName=""){
53 string outputFileName = "";
54 map<string, vector<string> >::iterator it;
56 //is this a type this command creates
57 it = outputTypes.find(type);
58 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
60 if (type == "aligncheck") { outputFileName = "align.check"; }
61 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
63 return outputFileName;
66 m->errorOut(e, "AlignCheckCommand", "getOutputFileNameTag");
70 //**********************************************************************************************************************
71 AlignCheckCommand::AlignCheckCommand(){
73 abort = true; calledHelp = true;
75 vector<string> tempOutNames;
76 outputTypes["aligncheck"] = tempOutNames;
79 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
83 //**********************************************************************************************************************
85 AlignCheckCommand::AlignCheckCommand(string option) {
87 abort = false; calledHelp = false;
90 //allow user to run help
91 if(option == "help") { help(); abort = true; calledHelp = true; }
92 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
95 vector<string> myArray = setParameters();
97 OptionParser parser(option);
98 map<string,string> parameters = parser.getParameters();
100 ValidParameters validParameter;
101 map<string,string>::iterator it;
103 //check to make sure all parameters are valid for command
104 for (it = parameters.begin(); it != parameters.end(); it++) {
105 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
108 //initialize outputTypes
109 vector<string> tempOutNames;
110 outputTypes["aligncheck"] = 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("fasta");
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["fasta"] = inputDir + it->second; }
125 it = parameters.find("map");
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["map"] = inputDir + it->second; }
133 it = parameters.find("name");
134 //user has given a template 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("count");
142 //user has given a template 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["count"] = inputDir + it->second; }
150 //check for required parameters
151 mapfile = validParameter.validFile(parameters, "map", true);
152 if (mapfile == "not open") { abort = true; }
153 else if (mapfile == "not found") { mapfile = ""; m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }
155 fastafile = validParameter.validFile(parameters, "fasta", true);
156 if (fastafile == "not open") { fastafile = ""; abort = true; }
157 else if (fastafile == "not found") {
158 fastafile = m->getFastaFile();
159 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
160 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
161 }else { m->setFastaFile(fastafile); }
163 namefile = validParameter.validFile(parameters, "name", true);
164 if (namefile == "not open") { namefile = ""; abort = true; }
165 else if (namefile == "not found") { namefile = ""; }
166 else { m->setNameFile(namefile); }
168 countfile = validParameter.validFile(parameters, "count", true);
169 if (countfile == "not open") { abort = true; countfile = ""; }
170 else if (countfile == "not found") { countfile = ""; }
171 else { m->setCountTableFile(countfile); }
173 if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
175 //if the user changes the output directory command factory will send this info to us in the output parameter
176 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
178 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
181 if (countfile == "") {
182 if ((namefile == "") && (fastafile != "")){
183 vector<string> files; files.push_back(fastafile);
184 parser.getNameFile(files);
190 catch(exception& e) {
191 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
195 //**********************************************************************************************************************
197 int AlignCheckCommand::execute(){
200 if (abort == true) { if (calledHelp) { return 0; } return 2; }
202 //get secondary structure info.
205 if (namefile != "") { nameMap = m->readNames(namefile); }
206 else if (countfile != "") {
208 ct.readTable(countfile);
209 nameMap = ct.getNameMap();
212 if (m->control_pressed) { return 0; }
215 m->openInputFile(fastafile, in);
218 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("aligncheck");
219 m->openOutputFile(outfile, out);
222 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
223 out << "loop" << '\t' << "tilde" << '\t' << "total" << '\t' << "numseqs" << endl;
235 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outfile); return 0; }
237 Sequence seq(in); m->gobble(in);
238 if (seq.getName() != "") {
239 statData data = getStats(seq.getAligned());
241 if (haderror == 1) { m->control_pressed = true; break; }
244 if ((namefile != "") || (countfile != "")) {
245 //make sure this sequence is in the namefile, else error
246 map<string, int>::iterator it = nameMap.find(seq.getName());
248 if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + seq.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
249 else { num = it->second; }
252 //for each sequence this sequence represents
253 for (int i = 0; i < num; i++) {
254 pound.push_back(data.pound);
255 dash.push_back(data.dash);
256 plus.push_back(data.plus);
257 equal.push_back(data.equal);
258 loop.push_back(data.loop);
259 tilde.push_back(data.tilde);
260 total.push_back(data.total);
264 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
265 out << data.loop << '\t' << data.tilde << '\t' << data.total << '\t' << num << endl;
272 if (m->control_pressed) { m->mothurRemove(outfile); return 0; }
274 sort(pound.begin(), pound.end());
275 sort(dash.begin(), dash.end());
276 sort(plus.begin(), plus.end());
277 sort(equal.begin(), equal.end());
278 sort(loop.begin(), loop.end());
279 sort(tilde.begin(), tilde.end());
280 sort(total.begin(), total.end());
281 int size = pound.size();
283 int ptile0_25 = int(size * 0.025);
284 int ptile25 = int(size * 0.250);
285 int ptile50 = int(size * 0.500);
286 int ptile75 = int(size * 0.750);
287 int ptile97_5 = int(size * 0.975);
288 int ptile100 = size - 1;
290 if (m->control_pressed) { m->mothurRemove(outfile); return 0; }
292 m->mothurOutEndLine();
293 m->mothurOut("\t\tPound\tDash\tPlus\tEqual\tLoop\tTilde\tTotal"); m->mothurOutEndLine();
294 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();
295 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();
296 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();
297 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();
298 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();
299 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();
300 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();
301 if ((namefile == "") && (countfile == "")) { m->mothurOut("# of Seqs:\t" + toString(count)); m->mothurOutEndLine(); }
302 else { m->mothurOut("# of unique seqs:\t" + toString(count)); m->mothurOutEndLine(); m->mothurOut("total # of seqs:\t" + toString(size)); m->mothurOutEndLine(); }
305 m->mothurOutEndLine();
306 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
307 m->mothurOut(outfile); m->mothurOutEndLine(); outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
308 m->mothurOutEndLine();
313 catch(exception& e) {
314 m->errorOut(e, "AlignCheckCommand", "execute");
318 //**********************************************************************************************************************
319 void AlignCheckCommand::readMap(){
322 structMap.resize(1, 0);
325 m->openInputFile(mapfile, in);
330 structMap.push_back(position);
335 seqLength = structMap.size();
338 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
339 for(int i=0;i<seqLength;i++){
340 if(structMap[i] != 0){
341 if(structMap[structMap[i]] != i){
342 m->mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
349 catch(exception& e) {
350 m->errorOut(e, "AlignCheckCommand", "readMap");
354 /**************************************************************************************************/
356 statData AlignCheckCommand::getStats(string sequence){
360 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
362 int length = sequence.length();
364 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; }
366 for(int i=1;i<length;i++){
367 if(structMap[i] != 0){
368 if(sequence[i] == 'A'){
369 if(sequence[structMap[i]] == 'T') { data.tilde++; }
370 else if(sequence[structMap[i]] == 'A') { data.pound++; }
371 else if(sequence[structMap[i]] == 'G') { data.equal++; }
372 else if(sequence[structMap[i]] == 'C') { data.pound++; }
373 else if(sequence[structMap[i]] == '-') { data.pound++; }
376 else if(sequence[i] == 'T'){
377 if(sequence[structMap[i]] == 'T') { data.plus++; }
378 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
379 else if(sequence[structMap[i]] == 'G') { data.dash++; }
380 else if(sequence[structMap[i]] == 'C') { data.pound++; }
381 else if(sequence[structMap[i]] == '-') { data.pound++; }
384 else if(sequence[i] == 'G'){
385 if(sequence[structMap[i]] == 'T') { data.dash++; }
386 else if(sequence[structMap[i]] == 'A') { data.equal++; }
387 else if(sequence[structMap[i]] == 'G') { data.pound++; }
388 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
389 else if(sequence[structMap[i]] == '-') { data.pound++; }
392 else if(sequence[i] == 'C'){
393 if(sequence[structMap[i]] == 'T') { data.pound++; }
394 else if(sequence[structMap[i]] == 'A') { data.pound++; }
395 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
396 else if(sequence[structMap[i]] == 'C') { data.pound++; }
397 else if(sequence[structMap[i]] == '-') { data.pound++; }
400 else if(sequence[i] == '-'){
401 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
402 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
403 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
404 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
405 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
408 else if(isalnum(sequence[i])){
416 catch(exception& e) {
417 m->errorOut(e, "AlignCheckCommand", "getStats");
421 //**********************************************************************************************************************