]> git.donarmstrong.com Git - mothur.git/blob - secondarystructurecommand.cpp
added pipeline commands which involved change to command factory and command class...
[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                 //initialize outputTypes
29                 vector<string> tempOutNames;
30                 outputTypes["aligncheck"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> AlignCheckCommand::getRequiredParameters(){      
39         try {
40                 string Array[] =  {"fasta","map"};
41                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42                 return myArray;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "AlignCheckCommand", "getRequiredParameters");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 vector<string> AlignCheckCommand::getRequiredFiles(){   
51         try {
52                 vector<string> myArray;
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "AlignCheckCommand", "getRequiredFiles");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61
62 AlignCheckCommand::AlignCheckCommand(string option)  {
63         try {
64                 abort = false;
65                 haderror = 0;
66                         
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; }
69                 
70                 else {
71                         //valid paramters for this command
72                         string Array[] =  {"fasta","map", "outputdir","inputdir"};
73                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74                         
75                         OptionParser parser(option);
76                         map<string,string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string,string>::iterator it;
80                         
81                         //check to make sure all parameters are valid for command
82                         for (it = parameters.begin(); it != parameters.end(); it++) { 
83                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
84                         }
85                         
86                         //initialize outputTypes
87                         vector<string> tempOutNames;
88                         outputTypes["aligncheck"] = tempOutNames;
89                         
90                         //if the user changes the input directory command factory will send this info to us in the output parameter 
91                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
92                         if (inputDir == "not found"){   inputDir = "";          }
93                         else {
94                                 string path;
95                                 it = parameters.find("fasta");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
101                                 }
102                                 
103                                 it = parameters.find("map");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["map"] = inputDir + it->second;              }
109                                 }
110                         }
111
112                         //check for required parameters
113                         mapfile = validParameter.validFile(parameters, "map", true);
114                         if (mapfile == "not open") { abort = true; }
115                         else if (mapfile == "not found") {  mapfile = "";  m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }        
116                         
117                         fastafile = validParameter.validFile(parameters, "fasta", true);
118                         if (fastafile == "not open") { abort = true; }
119                         else if (fastafile == "not found") {  fastafile = "";  m->mothurOut("You must provide an fasta file."); m->mothurOutEndLine(); abort = true;  } 
120                         
121                         //if the user changes the output directory command factory will send this info to us in the output parameter 
122                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
123                                 outputDir = ""; 
124                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
125                         }
126
127                 }
128
129         }
130         catch(exception& e) {
131                 m->errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
132                 exit(1);
133         }
134 }
135 //**********************************************************************************************************************
136
137 void AlignCheckCommand::help(){
138         try {
139                 m->mothurOut("The align.check command reads a fasta file and map file.\n");
140                 m->mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
141                 m->mothurOut("The align.check command parameters are fasta and map, both are required.\n");
142                 m->mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
143                 m->mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
144                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
145         }
146         catch(exception& e) {
147                 m->errorOut(e, "AlignCheckCommand", "help");
148                 exit(1);
149         }
150 }
151
152 //**********************************************************************************************************************
153
154 int AlignCheckCommand::execute(){
155         try {
156                 
157                 if (abort == true) { return 0; }
158                 
159                 //get secondary structure info.
160                 readMap();
161                 
162                 ifstream in;
163                 m->openInputFile(fastafile, in);
164                 
165                 ofstream out;
166                 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "align.check";
167                 m->openOutputFile(outfile, out);
168                 
169                 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
170                 out << "loop" << '\t' << "tilde" << '\t' << "total" << endl;
171
172                 
173                 while(!in.eof()){
174                         if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
175                         
176                         Sequence seq(in);  m->gobble(in);
177                         if (seq.getName() != "") {
178                                 statData data = getStats(seq.getAligned());
179                                 
180                                 if (haderror == 1) { break; }
181                                 
182                                 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
183                                 out << data.loop << '\t' << data.tilde << '\t' << data.total << endl;
184                         }
185                 }
186
187                 in.close();
188                 out.close();
189                 
190                 if (m->control_pressed) {  remove(outfile.c_str()); return 0; }
191                 
192                 m->mothurOutEndLine();
193                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
194                 m->mothurOut(outfile); m->mothurOutEndLine();   outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
195                 m->mothurOutEndLine();
196                 
197                 return 0;               
198         }
199
200         catch(exception& e) {
201                 m->errorOut(e, "AlignCheckCommand", "execute");
202                 exit(1);
203         }
204 }
205 //**********************************************************************************************************************
206 void AlignCheckCommand::readMap(){
207         try {
208                         
209                 structMap.resize(1, 0);
210                 ifstream in;
211                 
212                 m->openInputFile(mapfile, in);
213                 
214                 while(!in.eof()){
215                         int position;
216                         in >> position;
217                         structMap.push_back(position);  
218                         m->gobble(in);
219                 }
220                 in.close();
221
222                 seqLength = structMap.size();
223                 
224                 
225                 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
226                 for(int i=0;i<seqLength;i++){
227                         if(structMap[i] != 0){
228                                 if(structMap[structMap[i]] != i){
229                                         m->mothurOut("Your map file contains an error:  line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
230                                 }
231                         }
232                 }
233                 
234                 
235         }
236         catch(exception& e) {
237                 m->errorOut(e, "AlignCheckCommand", "readMap");
238                 exit(1);
239         }
240 }
241 /**************************************************************************************************/
242
243 statData AlignCheckCommand::getStats(string sequence){
244         try {
245         
246                 statData data;
247                 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
248                 
249                 int length = sequence.length();
250                 
251                 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;  }
252                 
253                 for(int i=1;i<length;i++){
254                         if(structMap[i] != 0){
255                                 if(sequence[i] == 'A'){
256                                         if(sequence[structMap[i]] == 'T')               {       data.tilde++;   }
257                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
258                                         else if(sequence[structMap[i]] == 'G')  {       data.equal++;   }
259                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
260                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
261                                         data.total++;
262                                 }
263                                 else if(sequence[i] == 'T'){
264                                         if(sequence[structMap[i]] == 'T')               {       data.plus++;    }
265                                         else if(sequence[structMap[i]] == 'A')  {       data.tilde++;   }
266                                         else if(sequence[structMap[i]] == 'G')  {       data.dash++;    }
267                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
268                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
269                                         data.total++;
270                                 }
271                                 else if(sequence[i] == 'G'){
272                                         if(sequence[structMap[i]] == 'T')               {       data.dash++;    }
273                                         else if(sequence[structMap[i]] == 'A')  {       data.equal++;   }
274                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   }
275                                         else if(sequence[structMap[i]] == 'C')  {       data.tilde++;   }
276                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
277                                         data.total++;
278                                 }
279                                 else if(sequence[i] == 'C'){
280                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   }
281                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
282                                         else if(sequence[structMap[i]] == 'G')  {       data.tilde++;   }
283                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
284                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
285                                         data.total++;
286                                 }
287                                 else if(sequence[i] == '-'){
288                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   data.total++;   }
289                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   data.total++;   }
290                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   data.total++;   }
291                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   data.total++;   }
292                                         else if(sequence[structMap[i]] == '-')  {               /*donothing*/                           }
293                                 }                       
294                         }
295                         else if(isalnum(sequence[i])){
296                                 data.loop++;
297                                 data.total++;
298                         }
299                 }
300                 return data;
301                 
302         }
303         catch(exception& e) {
304                 m->errorOut(e, "AlignCheckCommand", "getStats");
305                 exit(1);
306         }
307 }
308
309
310 //**********************************************************************************************************************