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","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","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; }
112 //check for required parameters
113 mapfile = validParameter.validFile(parameters, "map", true);
114 if (mapfile == "not open") { abort = true; }
115 else if (mapfile == "not found") { mapfile = ""; m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }
117 fastafile = validParameter.validFile(parameters, "fasta", true);
118 if (fastafile == "not open") { abort = true; }
119 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("You must provide an fasta file."); m->mothurOutEndLine(); abort = true; }
121 //if the user changes the output directory command factory will send this info to us in the output parameter
122 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
124 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
130 catch(exception& e) {
131 m->errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
135 //**********************************************************************************************************************
137 void AlignCheckCommand::help(){
139 m->mothurOut("The align.check command reads a fasta file and map file.\n");
140 m->mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
141 m->mothurOut("The align.check command parameters are fasta and map, both are required.\n");
142 m->mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
143 m->mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
144 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
146 catch(exception& e) {
147 m->errorOut(e, "AlignCheckCommand", "help");
152 //**********************************************************************************************************************
154 int AlignCheckCommand::execute(){
157 if (abort == true) { if (calledHelp) { return 0; } return 2; }
159 //get secondary structure info.
163 m->openInputFile(fastafile, in);
166 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "align.check";
167 m->openOutputFile(outfile, out);
169 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
170 out << "loop" << '\t' << "tilde" << '\t' << "total" << endl;
174 if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
176 Sequence seq(in); m->gobble(in);
177 if (seq.getName() != "") {
178 statData data = getStats(seq.getAligned());
180 if (haderror == 1) { break; }
182 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
183 out << data.loop << '\t' << data.tilde << '\t' << data.total << endl;
190 if (m->control_pressed) { remove(outfile.c_str()); return 0; }
192 m->mothurOutEndLine();
193 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
194 m->mothurOut(outfile); m->mothurOutEndLine(); outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
195 m->mothurOutEndLine();
200 catch(exception& e) {
201 m->errorOut(e, "AlignCheckCommand", "execute");
205 //**********************************************************************************************************************
206 void AlignCheckCommand::readMap(){
209 structMap.resize(1, 0);
212 m->openInputFile(mapfile, in);
217 structMap.push_back(position);
222 seqLength = structMap.size();
225 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
226 for(int i=0;i<seqLength;i++){
227 if(structMap[i] != 0){
228 if(structMap[structMap[i]] != i){
229 m->mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
236 catch(exception& e) {
237 m->errorOut(e, "AlignCheckCommand", "readMap");
241 /**************************************************************************************************/
243 statData AlignCheckCommand::getStats(string sequence){
247 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
249 int length = sequence.length();
251 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; }
253 for(int i=1;i<length;i++){
254 if(structMap[i] != 0){
255 if(sequence[i] == 'A'){
256 if(sequence[structMap[i]] == 'T') { data.tilde++; }
257 else if(sequence[structMap[i]] == 'A') { data.pound++; }
258 else if(sequence[structMap[i]] == 'G') { data.equal++; }
259 else if(sequence[structMap[i]] == 'C') { data.pound++; }
260 else if(sequence[structMap[i]] == '-') { data.pound++; }
263 else if(sequence[i] == 'T'){
264 if(sequence[structMap[i]] == 'T') { data.plus++; }
265 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
266 else if(sequence[structMap[i]] == 'G') { data.dash++; }
267 else if(sequence[structMap[i]] == 'C') { data.pound++; }
268 else if(sequence[structMap[i]] == '-') { data.pound++; }
271 else if(sequence[i] == 'G'){
272 if(sequence[structMap[i]] == 'T') { data.dash++; }
273 else if(sequence[structMap[i]] == 'A') { data.equal++; }
274 else if(sequence[structMap[i]] == 'G') { data.pound++; }
275 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
276 else if(sequence[structMap[i]] == '-') { data.pound++; }
279 else if(sequence[i] == 'C'){
280 if(sequence[structMap[i]] == 'T') { data.pound++; }
281 else if(sequence[structMap[i]] == 'A') { data.pound++; }
282 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
283 else if(sequence[structMap[i]] == 'C') { data.pound++; }
284 else if(sequence[structMap[i]] == '-') { data.pound++; }
287 else if(sequence[i] == '-'){
288 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
289 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
290 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
291 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
292 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
295 else if(isalnum(sequence[i])){
303 catch(exception& e) {
304 m->errorOut(e, "AlignCheckCommand", "getStats");
310 //**********************************************************************************************************************