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(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["aligncheck"] = tempOutNames;
34 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
38 //**********************************************************************************************************************
39 vector<string> AlignCheckCommand::getRequiredParameters(){
41 string Array[] = {"fasta","map"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "AlignCheckCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> AlignCheckCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "AlignCheckCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
63 AlignCheckCommand::AlignCheckCommand(string option) {
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"fasta","map", "outputdir","inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string,string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["aligncheck"] = tempOutNames;
91 //if the user changes the input directory command factory will send this info to us in the output parameter
92 string inputDir = validParameter.validFile(parameters, "inputdir", false);
93 if (inputDir == "not found"){ inputDir = ""; }
96 it = parameters.find("fasta");
97 //user has given a template file
98 if(it != parameters.end()){
99 path = m->hasPath(it->second);
100 //if the user has not given a path then, add inputdir. else leave path alone.
101 if (path == "") { parameters["fasta"] = inputDir + it->second; }
104 it = parameters.find("map");
105 //user has given a template file
106 if(it != parameters.end()){
107 path = m->hasPath(it->second);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { parameters["map"] = inputDir + it->second; }
113 //check for required parameters
114 mapfile = validParameter.validFile(parameters, "map", true);
115 if (mapfile == "not open") { abort = true; }
116 else if (mapfile == "not found") { mapfile = ""; m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }
118 fastafile = validParameter.validFile(parameters, "fasta", true);
119 if (fastafile == "not open") { abort = true; }
120 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("You must provide an fasta file."); m->mothurOutEndLine(); abort = true; }
122 //if the user changes the output directory command factory will send this info to us in the output parameter
123 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
125 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
131 catch(exception& e) {
132 m->errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
136 //**********************************************************************************************************************
138 void AlignCheckCommand::help(){
140 m->mothurOut("The align.check command reads a fasta file and map file.\n");
141 m->mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
142 m->mothurOut("The align.check command parameters are fasta and map, both are required.\n");
143 m->mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
144 m->mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
145 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
147 catch(exception& e) {
148 m->errorOut(e, "AlignCheckCommand", "help");
153 //**********************************************************************************************************************
155 int AlignCheckCommand::execute(){
158 if (abort == true) { return 0; }
160 //get secondary structure info.
164 m->openInputFile(fastafile, in);
167 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "align.check";
168 m->openOutputFile(outfile, out);
170 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
171 out << "loop" << '\t' << "tilde" << '\t' << "total" << endl;
175 if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
177 Sequence seq(in); m->gobble(in);
178 if (seq.getName() != "") {
179 statData data = getStats(seq.getAligned());
181 if (haderror == 1) { break; }
183 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
184 out << data.loop << '\t' << data.tilde << '\t' << data.total << endl;
191 if (m->control_pressed) { remove(outfile.c_str()); return 0; }
193 m->mothurOutEndLine();
194 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
195 m->mothurOut(outfile); m->mothurOutEndLine(); outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
196 m->mothurOutEndLine();
201 catch(exception& e) {
202 m->errorOut(e, "AlignCheckCommand", "execute");
206 //**********************************************************************************************************************
207 void AlignCheckCommand::readMap(){
210 structMap.resize(1, 0);
213 m->openInputFile(mapfile, in);
218 structMap.push_back(position);
223 seqLength = structMap.size();
226 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
227 for(int i=0;i<seqLength;i++){
228 if(structMap[i] != 0){
229 if(structMap[structMap[i]] != i){
230 m->mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
237 catch(exception& e) {
238 m->errorOut(e, "AlignCheckCommand", "readMap");
242 /**************************************************************************************************/
244 statData AlignCheckCommand::getStats(string sequence){
248 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
250 int length = sequence.length();
252 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; }
254 for(int i=1;i<length;i++){
255 if(structMap[i] != 0){
256 if(sequence[i] == 'A'){
257 if(sequence[structMap[i]] == 'T') { data.tilde++; }
258 else if(sequence[structMap[i]] == 'A') { data.pound++; }
259 else if(sequence[structMap[i]] == 'G') { data.equal++; }
260 else if(sequence[structMap[i]] == 'C') { data.pound++; }
261 else if(sequence[structMap[i]] == '-') { data.pound++; }
264 else if(sequence[i] == 'T'){
265 if(sequence[structMap[i]] == 'T') { data.plus++; }
266 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
267 else if(sequence[structMap[i]] == 'G') { data.dash++; }
268 else if(sequence[structMap[i]] == 'C') { data.pound++; }
269 else if(sequence[structMap[i]] == '-') { data.pound++; }
272 else if(sequence[i] == 'G'){
273 if(sequence[structMap[i]] == 'T') { data.dash++; }
274 else if(sequence[structMap[i]] == 'A') { data.equal++; }
275 else if(sequence[structMap[i]] == 'G') { data.pound++; }
276 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
277 else if(sequence[structMap[i]] == '-') { data.pound++; }
280 else if(sequence[i] == 'C'){
281 if(sequence[structMap[i]] == 'T') { data.pound++; }
282 else if(sequence[structMap[i]] == 'A') { data.pound++; }
283 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
284 else if(sequence[structMap[i]] == 'C') { data.pound++; }
285 else if(sequence[structMap[i]] == '-') { data.pound++; }
288 else if(sequence[i] == '-'){
289 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
290 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
291 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
292 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
293 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
296 else if(isalnum(sequence[i])){
304 catch(exception& e) {
305 m->errorOut(e, "AlignCheckCommand", "getStats");
311 //**********************************************************************************************************************