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){
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"fasta","map", "outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string,string>::iterator it;
33 //check to make sure all parameters are valid for command
34 for (it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 //if the user changes the input directory command factory will send this info to us in the output parameter
39 string inputDir = validParameter.validFile(parameters, "inputdir", false);
40 if (inputDir == "not found"){ inputDir = ""; }
43 it = parameters.find("fasta");
44 //user has given a template file
45 if(it != parameters.end()){
46 path = hasPath(it->second);
47 //if the user has not given a path then, add inputdir. else leave path alone.
48 if (path == "") { parameters["fasta"] = inputDir + it->second; }
51 it = parameters.find("map");
52 //user has given a template file
53 if(it != parameters.end()){
54 path = hasPath(it->second);
55 //if the user has not given a path then, add inputdir. else leave path alone.
56 if (path == "") { parameters["map"] = inputDir + it->second; }
60 //check for required parameters
61 mapfile = validParameter.validFile(parameters, "map", true);
62 if (mapfile == "not open") { abort = true; }
63 else if (mapfile == "not found") { mapfile = ""; mothurOut("You must provide an map file."); mothurOutEndLine(); abort = true; }
65 fastafile = validParameter.validFile(parameters, "fasta", true);
66 if (fastafile == "not open") { abort = true; }
67 else if (fastafile == "not found") { fastafile = ""; mothurOut("You must provide an fasta file."); mothurOutEndLine(); abort = true; }
69 //if the user changes the output directory command factory will send this info to us in the output parameter
70 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
72 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
79 errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
83 //**********************************************************************************************************************
85 void AlignCheckCommand::help(){
87 mothurOut("The align.check command reads a fasta file and map file.\n");
88 mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
89 mothurOut("The align.check command parameters are fasta and map, both are required.\n");
90 mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
91 mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
92 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
95 errorOut(e, "AlignCheckCommand", "help");
100 //**********************************************************************************************************************
102 int AlignCheckCommand::execute(){
105 if (abort == true) { return 0; }
107 //get secondary structure info.
111 openInputFile(fastafile, in);
114 string outfile = outputDir + getRootName(getSimpleName(fastafile)) + "align.check";
115 openOutputFile(outfile, out);
117 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
118 out << "loop" << '\t' << "tilde" << '\t' << "total" << endl;
123 Sequence seq(in); gobble(in);
124 if (seq.getName() != "") {
125 statData data = getStats(seq.getAligned());
127 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
128 out << data.loop << '\t' << data.tilde << '\t' << data.total << endl;
138 catch(exception& e) {
139 errorOut(e, "AlignCheckCommand", "execute");
143 //**********************************************************************************************************************
144 void AlignCheckCommand::readMap(){
147 structMap.resize(1, 0);
150 openInputFile(mapfile, in);
155 structMap.push_back(position);
160 seqLength = structMap.size();
163 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
164 for(int i=0;i<seqLength;i++){
165 if(structMap[i] != 0){
166 if(structMap[structMap[i]] != i){
167 mothurOut("Your map file contains an error: line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); mothurOutEndLine();
174 catch(exception& e) {
175 errorOut(e, "AlignCheckCommand", "readMap");
179 /**************************************************************************************************/
181 statData AlignCheckCommand::getStats(string sequence){
185 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
187 int seqLength = sequence.length();
188 for(int i=1;i<seqLength;i++){
189 if(structMap[i] != 0){
190 if(sequence[i] == 'A'){
191 if(sequence[structMap[i]] == 'T') { data.tilde++; }
192 else if(sequence[structMap[i]] == 'A') { data.pound++; }
193 else if(sequence[structMap[i]] == 'G') { data.equal++; }
194 else if(sequence[structMap[i]] == 'C') { data.pound++; }
195 else if(sequence[structMap[i]] == '-') { data.pound++; }
198 else if(sequence[i] == 'T'){
199 if(sequence[structMap[i]] == 'T') { data.plus++; }
200 else if(sequence[structMap[i]] == 'A') { data.tilde++; }
201 else if(sequence[structMap[i]] == 'G') { data.dash++; }
202 else if(sequence[structMap[i]] == 'C') { data.pound++; }
203 else if(sequence[structMap[i]] == '-') { data.pound++; }
206 else if(sequence[i] == 'G'){
207 if(sequence[structMap[i]] == 'T') { data.dash++; }
208 else if(sequence[structMap[i]] == 'A') { data.equal++; }
209 else if(sequence[structMap[i]] == 'G') { data.pound++; }
210 else if(sequence[structMap[i]] == 'C') { data.tilde++; }
211 else if(sequence[structMap[i]] == '-') { data.pound++; }
214 else if(sequence[i] == 'C'){
215 if(sequence[structMap[i]] == 'T') { data.pound++; }
216 else if(sequence[structMap[i]] == 'A') { data.pound++; }
217 else if(sequence[structMap[i]] == 'G') { data.tilde++; }
218 else if(sequence[structMap[i]] == 'C') { data.pound++; }
219 else if(sequence[structMap[i]] == '-') { data.pound++; }
222 else if(sequence[i] == '-'){
223 if(sequence[structMap[i]] == 'T') { data.pound++; data.total++; }
224 else if(sequence[structMap[i]] == 'A') { data.pound++; data.total++; }
225 else if(sequence[structMap[i]] == 'G') { data.pound++; data.total++; }
226 else if(sequence[structMap[i]] == 'C') { data.pound++; data.total++; }
227 else if(sequence[structMap[i]] == '-') { /*donothing*/ }
230 else if(isalnum(sequence[i])){
238 catch(exception& e) {
239 errorOut(e, "AlignCheckCommand", "getStats");
245 //**********************************************************************************************************************