]> git.donarmstrong.com Git - mothur.git/blob - secondarystructurecommand.cpp
added namefile to align.check and added seqs from namefile to optimizing piece of...
[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", "name","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; calledHelp = true; 
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; calledHelp = false;   
65                 haderror = 0;
66                         
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; calledHelp = true; }
69                 
70                 else {
71                         //valid paramters for this command
72                         string Array[] =  {"fasta","name","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                                 it = parameters.find("name");
112                                 //user has given a template file
113                                 if(it != parameters.end()){ 
114                                         path = m->hasPath(it->second);
115                                         //if the user has not given a path then, add inputdir. else leave path alone.
116                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
117                                 }
118                         }
119
120                         //check for required parameters
121                         mapfile = validParameter.validFile(parameters, "map", true);
122                         if (mapfile == "not open") { abort = true; }
123                         else if (mapfile == "not found") {  mapfile = "";  m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }        
124                         
125                         fastafile = validParameter.validFile(parameters, "fasta", true);
126                         if (fastafile == "not open") { abort = true; }
127                         else if (fastafile == "not found") {  fastafile = "";  m->mothurOut("You must provide an fasta file."); m->mothurOutEndLine(); abort = true;  } 
128                         
129                         namefile = validParameter.validFile(parameters, "name", true);
130                         if (namefile == "not open") { namefile = ""; abort = true; }
131                         else if (namefile == "not found") { namefile = "";  }   
132                         
133                         //if the user changes the output directory command factory will send this info to us in the output parameter 
134                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
135                                 outputDir = ""; 
136                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
137                         }
138
139                 }
140
141         }
142         catch(exception& e) {
143                 m->errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
144                 exit(1);
145         }
146 }
147 //**********************************************************************************************************************
148
149 void AlignCheckCommand::help(){
150         try {
151                 m->mothurOut("The align.check command reads a fasta file and map file.\n");
152                 m->mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
153                 m->mothurOut("The align.check command parameters are fasta and map, both are required.\n");
154                 m->mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
155                 m->mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
156                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
157         }
158         catch(exception& e) {
159                 m->errorOut(e, "AlignCheckCommand", "help");
160                 exit(1);
161         }
162 }
163
164 //**********************************************************************************************************************
165
166 int AlignCheckCommand::execute(){
167         try {
168                 
169                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
170                 
171                 //get secondary structure info.
172                 readMap();
173                 
174                 if (namefile != "") { nameMap = m->readNames(namefile); }
175                 
176                 if (m->control_pressed) { return 0; }
177                 
178                 ifstream in;
179                 m->openInputFile(fastafile, in);
180                 
181                 ofstream out;
182                 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "align.check";
183                 m->openOutputFile(outfile, out);
184                 
185                                 
186                 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
187                 out << "loop" << '\t' << "tilde" << '\t' << "total"  << '\t' << "numseqs" << endl;
188
189                 
190                 while(!in.eof()){
191                         if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
192                         
193                         Sequence seq(in);  m->gobble(in);
194                         if (seq.getName() != "") {
195                                 statData data = getStats(seq.getAligned());
196                                 
197                                 if (haderror == 1) { break; }
198                                 
199                                 int num = 1;
200                                 if (namefile != "") {
201                                         //make sure this sequence is in the namefile, else error 
202                                         map<string, int>::iterator it = nameMap.find(seq.getName());
203                                         
204                                         if (it == nameMap.end()) { cout << "[ERROR]: " << seq.getName() << " is not in your namefile, please correct." << endl; m->control_pressed = true; }
205                                         else { num = it->second; }
206                                 }
207                                 
208                                 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
209                                 out << data.loop << '\t' << data.tilde << '\t' << data.total << '\t' << num << endl;
210                         }
211                 }
212
213                 in.close();
214                 out.close();
215                 
216                 if (m->control_pressed) {  remove(outfile.c_str()); return 0; }
217                 
218                 m->mothurOutEndLine();
219                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
220                 m->mothurOut(outfile); m->mothurOutEndLine();   outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
221                 m->mothurOutEndLine();
222                 
223                 return 0;               
224         }
225
226         catch(exception& e) {
227                 m->errorOut(e, "AlignCheckCommand", "execute");
228                 exit(1);
229         }
230 }
231 //**********************************************************************************************************************
232 void AlignCheckCommand::readMap(){
233         try {
234                         
235                 structMap.resize(1, 0);
236                 ifstream in;
237                 
238                 m->openInputFile(mapfile, in);
239                 
240                 while(!in.eof()){
241                         int position;
242                         in >> position;
243                         structMap.push_back(position);  
244                         m->gobble(in);
245                 }
246                 in.close();
247
248                 seqLength = structMap.size();
249                 
250                 
251                 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
252                 for(int i=0;i<seqLength;i++){
253                         if(structMap[i] != 0){
254                                 if(structMap[structMap[i]] != i){
255                                         m->mothurOut("Your map file contains an error:  line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
256                                 }
257                         }
258                 }
259                 
260                 
261         }
262         catch(exception& e) {
263                 m->errorOut(e, "AlignCheckCommand", "readMap");
264                 exit(1);
265         }
266 }
267 /**************************************************************************************************/
268
269 statData AlignCheckCommand::getStats(string sequence){
270         try {
271         
272                 statData data;
273                 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
274                 
275                 int length = sequence.length();
276                 
277                 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;  }
278                 
279                 for(int i=1;i<length;i++){
280                         if(structMap[i] != 0){
281                                 if(sequence[i] == 'A'){
282                                         if(sequence[structMap[i]] == 'T')               {       data.tilde++;   }
283                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
284                                         else if(sequence[structMap[i]] == 'G')  {       data.equal++;   }
285                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
286                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
287                                         data.total++;
288                                 }
289                                 else if(sequence[i] == 'T'){
290                                         if(sequence[structMap[i]] == 'T')               {       data.plus++;    }
291                                         else if(sequence[structMap[i]] == 'A')  {       data.tilde++;   }
292                                         else if(sequence[structMap[i]] == 'G')  {       data.dash++;    }
293                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
294                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
295                                         data.total++;
296                                 }
297                                 else if(sequence[i] == 'G'){
298                                         if(sequence[structMap[i]] == 'T')               {       data.dash++;    }
299                                         else if(sequence[structMap[i]] == 'A')  {       data.equal++;   }
300                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   }
301                                         else if(sequence[structMap[i]] == 'C')  {       data.tilde++;   }
302                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
303                                         data.total++;
304                                 }
305                                 else if(sequence[i] == 'C'){
306                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   }
307                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
308                                         else if(sequence[structMap[i]] == 'G')  {       data.tilde++;   }
309                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
310                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
311                                         data.total++;
312                                 }
313                                 else if(sequence[i] == '-'){
314                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   data.total++;   }
315                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   data.total++;   }
316                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   data.total++;   }
317                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   data.total++;   }
318                                         else if(sequence[structMap[i]] == '-')  {               /*donothing*/                           }
319                                 }                       
320                         }
321                         else if(isalnum(sequence[i])){
322                                 data.loop++;
323                                 data.total++;
324                         }
325                 }
326                 return data;
327                 
328         }
329         catch(exception& e) {
330                 m->errorOut(e, "AlignCheckCommand", "getStats");
331                 exit(1);
332         }
333 }
334 //**********************************************************************************************************************