]> git.donarmstrong.com Git - mothur.git/blob - secondarystructurecommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[mothur.git] / secondarystructurecommand.cpp
1 /*
2  *  secondarystructurecommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/18/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "secondarystructurecommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14
15 AlignCheckCommand::AlignCheckCommand(string option){
16         try {
17                 abort = false;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"fasta","map", "outputdir","inputdir"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                         map<string,string>::iterator it;
32                         
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;  }
36                         }
37                         
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 = "";          }
41                         else {
42                                 string path;
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;            }
49                                 }
50                                 
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;              }
57                                 }
58                         }
59
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; }      
64                         
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;  }       
68                         
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"){  
71                                 outputDir = ""; 
72                                 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it  
73                         }
74
75                 }
76
77         }
78         catch(exception& e) {
79                 errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84
85 void AlignCheckCommand::help(){
86         try {
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");
93         }
94         catch(exception& e) {
95                 errorOut(e, "AlignCheckCommand", "help");
96                 exit(1);
97         }
98 }
99
100 //**********************************************************************************************************************
101
102 int AlignCheckCommand::execute(){
103         try {
104                 
105                 if (abort == true) { return 0; }
106                 
107                 //get secondary structure info.
108                 readMap();
109                 
110                 ifstream in;
111                 openInputFile(fastafile, in);
112                 
113                 ofstream out;
114                 string outfile = outputDir + getRootName(getSimpleName(fastafile)) + "align.check";
115                 openOutputFile(outfile, out);
116                 
117                 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
118                 out << "loop" << '\t' << "tilde" << '\t' << "total" << endl;
119
120                 
121                 while(!in.eof()){
122                         
123                         Sequence seq(in);  gobble(in);
124                         if (seq.getName() != "") {
125                                 statData data = getStats(seq.getAligned());
126                                 
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;
129                         }
130                 }
131
132                 in.close();
133                 out.close();
134                 
135                 return 0;               
136         }
137
138         catch(exception& e) {
139                 errorOut(e, "AlignCheckCommand", "execute");
140                 exit(1);
141         }
142 }
143 //**********************************************************************************************************************
144 void AlignCheckCommand::readMap(){
145         try {
146                         
147                 structMap.resize(1, 0);
148                 ifstream in;
149                 
150                 openInputFile(mapfile, in);
151                 
152                 while(!in.eof()){
153                         int position;
154                         in >> position;
155                         structMap.push_back(position);  
156                         gobble(in);
157                 }
158                 in.close();
159
160                 seqLength = structMap.size();
161                 
162                 
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();
168                                 }
169                         }
170                 }
171                 
172                 
173         }
174         catch(exception& e) {
175                 errorOut(e, "AlignCheckCommand", "readMap");
176                 exit(1);
177         }
178 }
179 /**************************************************************************************************/
180
181 statData AlignCheckCommand::getStats(string sequence){
182         try {
183         
184                 statData data;
185                 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
186                 
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++;   }
196                                         data.total++;
197                                 }
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++;   }
204                                         data.total++;
205                                 }
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++;   }
212                                         data.total++;
213                                 }
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++;   }
220                                         data.total++;
221                                 }
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*/                           }
228                                 }                       
229                         }
230                         else if(isalnum(sequence[i])){
231                                 data.loop++;
232                                 data.total++;
233                         }
234                 }
235                 return data;
236                 
237         }
238         catch(exception& e) {
239                 errorOut(e, "AlignCheckCommand", "getStats");
240                 exit(1);
241         }
242 }
243
244
245 //**********************************************************************************************************************