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 //**********************************************************************************************************************
15 AlignCheckCommand::AlignCheckCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta","map", "outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the input directory command factory will send this info to us in the output parameter
40 string inputDir = validParameter.validFile(parameters, "inputdir", false);
41 if (inputDir == "not found"){ inputDir = ""; }
44 it = parameters.find("fasta");
45 //user has given a template file
46 if(it != parameters.end()){
47 path = hasPath(it->second);
48 //if the user has not given a path then, add inputdir. else leave path alone.
49 if (path == "") { parameters["fasta"] = inputDir + it->second; }
52 it = parameters.find("map");
53 //user has given a template file
54 if(it != parameters.end()){
55 path = hasPath(it->second);
56 //if the user has not given a path then, add inputdir. else leave path alone.
57 if (path == "") { parameters["map"] = inputDir + it->second; }
61 //check for required parameters
62 mapfile = validParameter.validFile(parameters, "map", true);
63 if (mapfile == "not open") { abort = true; }
64 else if (mapfile == "not found") { mapfile = ""; m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }
66 fastafile = validParameter.validFile(parameters, "fasta", true);
67 if (fastafile == "not open") { abort = true; }
68 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("You must provide an fasta file."); m->mothurOutEndLine(); abort = true; }
70 //if the user changes the output directory command factory will send this info to us in the output parameter
71 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
73 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
80 m->errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
84 //**********************************************************************************************************************
86 void AlignCheckCommand::help(){
88 m->mothurOut("The align.check command reads a fasta file and map file.\n");
89 m->mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
90 m->mothurOut("The align.check command parameters are fasta and map, both are required.\n");
91 m->mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
92 m->mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
93 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
96 m->errorOut(e, "AlignCheckCommand", "help");
101 //**********************************************************************************************************************
103 int AlignCheckCommand::execute(){
106 if (abort == true) { return 0; }
108 //get secondary structure info.
112 openInputFile(fastafile, in);
115 string outfile = outputDir + getRootName(getSimpleName(fastafile)) + "align.check";
116 openOutputFile(outfile, out);
118 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
119 out << "loop" << '\t' << "tilde" << '\t' << "total" << endl;
123 if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
125 Sequence seq(in); gobble(in);
126 if (seq.getName() != "") {
127 statData data = getStats(seq.getAligned());
129 if (haderror == 1) { break; }
131 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
132 out << data.loop << '\t' << data.tilde << '\t' << data.total << endl;
139 if (m->control_pressed) { remove(outfile.c_str()); return 0; }
141 m->mothurOutEndLine();
142 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
143 m->mothurOut(outfile); m->mothurOutEndLine();
144 m->mothurOutEndLine();
149 catch(exception& e) {
150 m->errorOut(e, "AlignCheckCommand", "execute");
154 //**********************************************************************************************************************
155 void AlignCheckCommand::readMap(){
158 structMap.resize(1, 0);
161 openInputFile(mapfile, in);
166 structMap.push_back(position);
171 seqLength = structMap.size();
174 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
175 for(int i=0;i<seqLength;i++){
176 if(structMap[i] != 0){
177 if(structMap[structMap[i]] != i){
178 m->mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
185 catch(exception& e) {
186 m->errorOut(e, "AlignCheckCommand", "readMap");
190 /**************************************************************************************************/
192 statData AlignCheckCommand::getStats(string sequence){
196 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
198 int length = sequence.length();
200 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; }
202 for(int i=1;i<length;i++){
203 if(structMap[i] != 0){
204 if(sequence[i] == 'A'){
205 if(sequence[structMap[i]] == 'T') { data.tilde++; }
206 else if(sequence[structMap[i]] == 'A') { data.pound++; }
207 else if(sequence[structMap[i]] == 'G') { data.equal++; }
208 else if(sequence[structMap[i]] == 'C') { data.pound++; }
209 else if(sequence[structMap[i]] == '-') { data.pound++; }
212 else if(sequence[i] == 'T'){
213 if(sequence[structMap[i]] == 'T') { data.plus++; }
214 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
215 else if(sequence[structMap[i]] == 'G') { data.dash++; }
216 else if(sequence[structMap[i]] == 'C') { data.pound++; }
217 else if(sequence[structMap[i]] == '-') { data.pound++; }
220 else if(sequence[i] == 'G'){
221 if(sequence[structMap[i]] == 'T') { data.dash++; }
222 else if(sequence[structMap[i]] == 'A') { data.equal++; }
223 else if(sequence[structMap[i]] == 'G') { data.pound++; }
224 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
225 else if(sequence[structMap[i]] == '-') { data.pound++; }
228 else if(sequence[i] == 'C'){
229 if(sequence[structMap[i]] == 'T') { data.pound++; }
230 else if(sequence[structMap[i]] == 'A') { data.pound++; }
231 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
232 else if(sequence[structMap[i]] == 'C') { data.pound++; }
233 else if(sequence[structMap[i]] == '-') { data.pound++; }
236 else if(sequence[i] == '-'){
237 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
238 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
239 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
240 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
241 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
244 else if(isalnum(sequence[i])){
252 catch(exception& e) {
253 m->errorOut(e, "AlignCheckCommand", "getStats");
259 //**********************************************************************************************************************