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"
13 //**********************************************************************************************************************
14 vector<string> AlignCheckCommand::getValidParameters(){
16 string Array[] = {"fasta", "name","map", "outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "AlignCheckCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 AlignCheckCommand::AlignCheckCommand(){
28 abort = true; calledHelp = true;
29 vector<string> tempOutNames;
30 outputTypes["aligncheck"] = tempOutNames;
33 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
37 //**********************************************************************************************************************
38 vector<string> AlignCheckCommand::getRequiredParameters(){
40 string Array[] = {"fasta","map"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "AlignCheckCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> AlignCheckCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "AlignCheckCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
62 AlignCheckCommand::AlignCheckCommand(string option) {
64 abort = false; calledHelp = false;
67 //allow user to run help
68 if(option == "help") { help(); abort = true; calledHelp = true; }
71 //valid paramters for this command
72 string Array[] = {"fasta","name","map", "outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["aligncheck"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("fasta");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["fasta"] = inputDir + it->second; }
103 it = parameters.find("map");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["map"] = inputDir + it->second; }
111 it = parameters.find("name");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["name"] = inputDir + it->second; }
120 //check for required parameters
121 mapfile = validParameter.validFile(parameters, "map", true);
122 if (mapfile == "not open") { abort = true; }
123 else if (mapfile == "not found") { mapfile = ""; m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }
125 fastafile = validParameter.validFile(parameters, "fasta", true);
126 if (fastafile == "not open") { abort = true; }
127 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("You must provide an fasta file."); m->mothurOutEndLine(); abort = true; }
129 namefile = validParameter.validFile(parameters, "name", true);
130 if (namefile == "not open") { namefile = ""; abort = true; }
131 else if (namefile == "not found") { namefile = ""; }
133 //if the user changes the output directory command factory will send this info to us in the output parameter
134 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
136 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
142 catch(exception& e) {
143 m->errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
147 //**********************************************************************************************************************
149 void AlignCheckCommand::help(){
151 m->mothurOut("The align.check command reads a fasta file and map file.\n");
152 m->mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
153 m->mothurOut("The align.check command parameters are fasta and map, both are required.\n");
154 m->mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
155 m->mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
156 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
158 catch(exception& e) {
159 m->errorOut(e, "AlignCheckCommand", "help");
164 //**********************************************************************************************************************
166 int AlignCheckCommand::execute(){
169 if (abort == true) { if (calledHelp) { return 0; } return 2; }
171 //get secondary structure info.
174 if (namefile != "") { nameMap = m->readNames(namefile); }
176 if (m->control_pressed) { return 0; }
179 m->openInputFile(fastafile, in);
182 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "align.check";
183 m->openOutputFile(outfile, out);
186 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
187 out << "loop" << '\t' << "tilde" << '\t' << "total" << '\t' << "numseqs" << endl;
199 if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
201 Sequence seq(in); m->gobble(in);
202 if (seq.getName() != "") {
203 statData data = getStats(seq.getAligned());
205 if (haderror == 1) { m->control_pressed = true; break; }
208 if (namefile != "") {
209 //make sure this sequence is in the namefile, else error
210 map<string, int>::iterator it = nameMap.find(seq.getName());
212 if (it == nameMap.end()) { cout << "[ERROR]: " << seq.getName() << " is not in your namefile, please correct." << endl; m->control_pressed = true; }
213 else { num = it->second; }
216 //for each sequence this sequence represents
217 for (int i = 0; i < num; i++) {
218 pound.push_back(data.pound);
219 dash.push_back(data.dash);
220 plus.push_back(data.plus);
221 equal.push_back(data.equal);
222 loop.push_back(data.loop);
223 tilde.push_back(data.tilde);
224 total.push_back(data.total);
228 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
229 out << data.loop << '\t' << data.tilde << '\t' << data.total << '\t' << num << endl;
236 if (m->control_pressed) { remove(outfile.c_str()); return 0; }
238 sort(pound.begin(), pound.end());
239 sort(dash.begin(), dash.end());
240 sort(plus.begin(), plus.end());
241 sort(equal.begin(), equal.end());
242 sort(loop.begin(), loop.end());
243 sort(tilde.begin(), tilde.end());
244 sort(total.begin(), total.end());
245 int size = pound.size();
247 int ptile0_25 = int(size * 0.025);
248 int ptile25 = int(size * 0.250);
249 int ptile50 = int(size * 0.500);
250 int ptile75 = int(size * 0.750);
251 int ptile97_5 = int(size * 0.975);
252 int ptile100 = size - 1;
254 if (m->control_pressed) { remove(outfile.c_str()); return 0; }
256 m->mothurOutEndLine();
257 m->mothurOut("\t\tPound\tDash\tPlus\tEqual\tLoop\tTilde\tTotal"); m->mothurOutEndLine();
258 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();
259 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();
260 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();
261 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();
262 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();
263 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();
264 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();
265 if (namefile == "") { m->mothurOut("# of Seqs:\t" + toString(count)); m->mothurOutEndLine(); }
266 else { m->mothurOut("# of unique seqs:\t" + toString(count)); m->mothurOutEndLine(); m->mothurOut("total # of seqs:\t" + toString(size)); m->mothurOutEndLine(); }
269 m->mothurOutEndLine();
270 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
271 m->mothurOut(outfile); m->mothurOutEndLine(); outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
272 m->mothurOutEndLine();
277 catch(exception& e) {
278 m->errorOut(e, "AlignCheckCommand", "execute");
282 //**********************************************************************************************************************
283 void AlignCheckCommand::readMap(){
286 structMap.resize(1, 0);
289 m->openInputFile(mapfile, in);
294 structMap.push_back(position);
299 seqLength = structMap.size();
302 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
303 for(int i=0;i<seqLength;i++){
304 if(structMap[i] != 0){
305 if(structMap[structMap[i]] != i){
306 m->mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
313 catch(exception& e) {
314 m->errorOut(e, "AlignCheckCommand", "readMap");
318 /**************************************************************************************************/
320 statData AlignCheckCommand::getStats(string sequence){
324 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
326 int length = sequence.length();
328 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; }
330 for(int i=1;i<length;i++){
331 if(structMap[i] != 0){
332 if(sequence[i] == 'A'){
333 if(sequence[structMap[i]] == 'T') { data.tilde++; }
334 else if(sequence[structMap[i]] == 'A') { data.pound++; }
335 else if(sequence[structMap[i]] == 'G') { data.equal++; }
336 else if(sequence[structMap[i]] == 'C') { data.pound++; }
337 else if(sequence[structMap[i]] == '-') { data.pound++; }
340 else if(sequence[i] == 'T'){
341 if(sequence[structMap[i]] == 'T') { data.plus++; }
342 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
343 else if(sequence[structMap[i]] == 'G') { data.dash++; }
344 else if(sequence[structMap[i]] == 'C') { data.pound++; }
345 else if(sequence[structMap[i]] == '-') { data.pound++; }
348 else if(sequence[i] == 'G'){
349 if(sequence[structMap[i]] == 'T') { data.dash++; }
350 else if(sequence[structMap[i]] == 'A') { data.equal++; }
351 else if(sequence[structMap[i]] == 'G') { data.pound++; }
352 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
353 else if(sequence[structMap[i]] == '-') { data.pound++; }
356 else if(sequence[i] == 'C'){
357 if(sequence[structMap[i]] == 'T') { data.pound++; }
358 else if(sequence[structMap[i]] == 'A') { data.pound++; }
359 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
360 else if(sequence[structMap[i]] == 'C') { data.pound++; }
361 else if(sequence[structMap[i]] == '-') { data.pound++; }
364 else if(sequence[i] == '-'){
365 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
366 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
367 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
368 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
369 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
372 else if(isalnum(sequence[i])){
380 catch(exception& e) {
381 m->errorOut(e, "AlignCheckCommand", "getStats");
385 //**********************************************************************************************************************