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;
191 if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
193 Sequence seq(in); m->gobble(in);
194 if (seq.getName() != "") {
195 statData data = getStats(seq.getAligned());
197 if (haderror == 1) { break; }
200 if (namefile != "") {
201 //make sure this sequence is in the namefile, else error
202 map<string, int>::iterator it = nameMap.find(seq.getName());
204 if (it == nameMap.end()) { cout << "[ERROR]: " << seq.getName() << " is not in your namefile, please correct." << endl; m->control_pressed = true; }
205 else { num = it->second; }
208 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
209 out << data.loop << '\t' << data.tilde << '\t' << data.total << '\t' << num << endl;
216 if (m->control_pressed) { remove(outfile.c_str()); return 0; }
218 m->mothurOutEndLine();
219 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
220 m->mothurOut(outfile); m->mothurOutEndLine(); outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
221 m->mothurOutEndLine();
226 catch(exception& e) {
227 m->errorOut(e, "AlignCheckCommand", "execute");
231 //**********************************************************************************************************************
232 void AlignCheckCommand::readMap(){
235 structMap.resize(1, 0);
238 m->openInputFile(mapfile, in);
243 structMap.push_back(position);
248 seqLength = structMap.size();
251 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
252 for(int i=0;i<seqLength;i++){
253 if(structMap[i] != 0){
254 if(structMap[structMap[i]] != i){
255 m->mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
262 catch(exception& e) {
263 m->errorOut(e, "AlignCheckCommand", "readMap");
267 /**************************************************************************************************/
269 statData AlignCheckCommand::getStats(string sequence){
273 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
275 int length = sequence.length();
277 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; }
279 for(int i=1;i<length;i++){
280 if(structMap[i] != 0){
281 if(sequence[i] == 'A'){
282 if(sequence[structMap[i]] == 'T') { data.tilde++; }
283 else if(sequence[structMap[i]] == 'A') { data.pound++; }
284 else if(sequence[structMap[i]] == 'G') { data.equal++; }
285 else if(sequence[structMap[i]] == 'C') { data.pound++; }
286 else if(sequence[structMap[i]] == '-') { data.pound++; }
289 else if(sequence[i] == 'T'){
290 if(sequence[structMap[i]] == 'T') { data.plus++; }
291 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
292 else if(sequence[structMap[i]] == 'G') { data.dash++; }
293 else if(sequence[structMap[i]] == 'C') { data.pound++; }
294 else if(sequence[structMap[i]] == '-') { data.pound++; }
297 else if(sequence[i] == 'G'){
298 if(sequence[structMap[i]] == 'T') { data.dash++; }
299 else if(sequence[structMap[i]] == 'A') { data.equal++; }
300 else if(sequence[structMap[i]] == 'G') { data.pound++; }
301 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
302 else if(sequence[structMap[i]] == '-') { data.pound++; }
305 else if(sequence[i] == 'C'){
306 if(sequence[structMap[i]] == 'T') { data.pound++; }
307 else if(sequence[structMap[i]] == 'A') { data.pound++; }
308 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
309 else if(sequence[structMap[i]] == 'C') { data.pound++; }
310 else if(sequence[structMap[i]] == '-') { data.pound++; }
313 else if(sequence[i] == '-'){
314 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
315 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
316 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
317 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
318 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
321 else if(isalnum(sequence[i])){
329 catch(exception& e) {
330 m->errorOut(e, "AlignCheckCommand", "getStats");
334 //**********************************************************************************************************************