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 = ""; mothurOut("You must provide an map file."); mothurOutEndLine(); abort = true; }
66 fastafile = validParameter.validFile(parameters, "fasta", true);
67 if (fastafile == "not open") { abort = true; }
68 else if (fastafile == "not found") { fastafile = ""; mothurOut("You must provide an fasta file."); 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 errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
84 //**********************************************************************************************************************
86 void AlignCheckCommand::help(){
88 mothurOut("The align.check command reads a fasta file and map file.\n");
89 mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
90 mothurOut("The align.check command parameters are fasta and map, both are required.\n");
91 mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
92 mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
93 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
96 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;
124 Sequence seq(in); gobble(in);
125 if (seq.getName() != "") {
126 statData data = getStats(seq.getAligned());
128 if (haderror == 1) { break; }
130 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
131 out << data.loop << '\t' << data.tilde << '\t' << data.total << endl;
141 catch(exception& e) {
142 errorOut(e, "AlignCheckCommand", "execute");
146 //**********************************************************************************************************************
147 void AlignCheckCommand::readMap(){
150 structMap.resize(1, 0);
153 openInputFile(mapfile, in);
158 structMap.push_back(position);
163 seqLength = structMap.size();
166 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
167 for(int i=0;i<seqLength;i++){
168 if(structMap[i] != 0){
169 if(structMap[structMap[i]] != i){
170 mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); mothurOutEndLine();
177 catch(exception& e) {
178 errorOut(e, "AlignCheckCommand", "readMap");
182 /**************************************************************************************************/
184 statData AlignCheckCommand::getStats(string sequence){
188 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
190 int length = sequence.length();
192 if (length != seqLength) { mothurOut("your sequences are " + toString(length) + " long, but your map file only contains " + toString(seqLength) + " entries. please correct."); mothurOutEndLine(); haderror = 1; return data; }
194 for(int i=1;i<length;i++){
195 if(structMap[i] != 0){
196 if(sequence[i] == 'A'){
197 if(sequence[structMap[i]] == 'T') { data.tilde++; }
198 else if(sequence[structMap[i]] == 'A') { data.pound++; }
199 else if(sequence[structMap[i]] == 'G') { data.equal++; }
200 else if(sequence[structMap[i]] == 'C') { data.pound++; }
201 else if(sequence[structMap[i]] == '-') { data.pound++; }
204 else if(sequence[i] == 'T'){
205 if(sequence[structMap[i]] == 'T') { data.plus++; }
206 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
207 else if(sequence[structMap[i]] == 'G') { data.dash++; }
208 else if(sequence[structMap[i]] == 'C') { data.pound++; }
209 else if(sequence[structMap[i]] == '-') { data.pound++; }
212 else if(sequence[i] == 'G'){
213 if(sequence[structMap[i]] == 'T') { data.dash++; }
214 else if(sequence[structMap[i]] == 'A') { data.equal++; }
215 else if(sequence[structMap[i]] == 'G') { data.pound++; }
216 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
217 else if(sequence[structMap[i]] == '-') { data.pound++; }
220 else if(sequence[i] == 'C'){
221 if(sequence[structMap[i]] == 'T') { data.pound++; }
222 else if(sequence[structMap[i]] == 'A') { data.pound++; }
223 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
224 else if(sequence[structMap[i]] == 'C') { data.pound++; }
225 else if(sequence[structMap[i]] == '-') { data.pound++; }
228 else if(sequence[i] == '-'){
229 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
230 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
231 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
232 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
233 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
236 else if(isalnum(sequence[i])){
244 catch(exception& e) {
245 errorOut(e, "AlignCheckCommand", "getStats");
251 //**********************************************************************************************************************