]> git.donarmstrong.com Git - mothur.git/blob - secondarystructurecommand.cpp
fixes while testing
[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 vector<string> AlignCheckCommand::getValidParameters(){ 
15         try {
16                 string Array[] =  {"fasta","map", "outputdir","inputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "AlignCheckCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 AlignCheckCommand::AlignCheckCommand(){ 
27         try {
28                 abort = true;
29                 //initialize outputTypes
30                 vector<string> tempOutNames;
31                 outputTypes["aligncheck"] = tempOutNames;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 vector<string> AlignCheckCommand::getRequiredParameters(){      
40         try {
41                 string Array[] =  {"fasta","map"};
42                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "AlignCheckCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> AlignCheckCommand::getRequiredFiles(){   
52         try {
53                 vector<string> myArray;
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "AlignCheckCommand", "getRequiredFiles");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62
63 AlignCheckCommand::AlignCheckCommand(string option)  {
64         try {
65                 abort = false;
66                 haderror = 0;
67                         
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; }
70                 
71                 else {
72                         //valid paramters for this command
73                         string Array[] =  {"fasta","map", "outputdir","inputdir"};
74                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75                         
76                         OptionParser parser(option);
77                         map<string,string> parameters = parser.getParameters();
78                         
79                         ValidParameters validParameter;
80                         map<string,string>::iterator it;
81                         
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;  }
85                         }
86                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["aligncheck"] = tempOutNames;
90                         
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 = "";          }
94                         else {
95                                 string path;
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;            }
102                                 }
103                                 
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;              }
110                                 }
111                         }
112
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; }        
117                         
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;  } 
121                         
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"){  
124                                 outputDir = ""; 
125                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
126                         }
127
128                 }
129
130         }
131         catch(exception& e) {
132                 m->errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
133                 exit(1);
134         }
135 }
136 //**********************************************************************************************************************
137
138 void AlignCheckCommand::help(){
139         try {
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");
146         }
147         catch(exception& e) {
148                 m->errorOut(e, "AlignCheckCommand", "help");
149                 exit(1);
150         }
151 }
152
153 //**********************************************************************************************************************
154
155 int AlignCheckCommand::execute(){
156         try {
157                 
158                 if (abort == true) { return 0; }
159                 
160                 //get secondary structure info.
161                 readMap();
162                 
163                 ifstream in;
164                 m->openInputFile(fastafile, in);
165                 
166                 ofstream out;
167                 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "align.check";
168                 m->openOutputFile(outfile, out);
169                 
170                 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
171                 out << "loop" << '\t' << "tilde" << '\t' << "total" << endl;
172
173                 
174                 while(!in.eof()){
175                         if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
176                         
177                         Sequence seq(in);  m->gobble(in);
178                         if (seq.getName() != "") {
179                                 statData data = getStats(seq.getAligned());
180                                 
181                                 if (haderror == 1) { break; }
182                                 
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;
185                         }
186                 }
187
188                 in.close();
189                 out.close();
190                 
191                 if (m->control_pressed) {  remove(outfile.c_str()); return 0; }
192                 
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();
197                 
198                 return 0;               
199         }
200
201         catch(exception& e) {
202                 m->errorOut(e, "AlignCheckCommand", "execute");
203                 exit(1);
204         }
205 }
206 //**********************************************************************************************************************
207 void AlignCheckCommand::readMap(){
208         try {
209                         
210                 structMap.resize(1, 0);
211                 ifstream in;
212                 
213                 m->openInputFile(mapfile, in);
214                 
215                 while(!in.eof()){
216                         int position;
217                         in >> position;
218                         structMap.push_back(position);  
219                         m->gobble(in);
220                 }
221                 in.close();
222
223                 seqLength = structMap.size();
224                 
225                 
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();
231                                 }
232                         }
233                 }
234                 
235                 
236         }
237         catch(exception& e) {
238                 m->errorOut(e, "AlignCheckCommand", "readMap");
239                 exit(1);
240         }
241 }
242 /**************************************************************************************************/
243
244 statData AlignCheckCommand::getStats(string sequence){
245         try {
246         
247                 statData data;
248                 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
249                 
250                 int length = sequence.length();
251                 
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;  }
253                 
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++;   }
262                                         data.total++;
263                                 }
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++;   }
270                                         data.total++;
271                                 }
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++;   }
278                                         data.total++;
279                                 }
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++;   }
286                                         data.total++;
287                                 }
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*/                           }
294                                 }                       
295                         }
296                         else if(isalnum(sequence[i])){
297                                 data.loop++;
298                                 data.total++;
299                         }
300                 }
301                 return data;
302                 
303         }
304         catch(exception& e) {
305                 m->errorOut(e, "AlignCheckCommand", "getStats");
306                 exit(1);
307         }
308 }
309
310
311 //**********************************************************************************************************************