]> git.donarmstrong.com Git - mothur.git/blob - secondarystructurecommand.cpp
major change to the tree class to use the count table class instead of tree map....
[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::setParameters(){      
15         try {
16                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
17                 CommandParameter pmap("map", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pmap);
18         CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
19         CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "AlignCheckCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string AlignCheckCommand::getHelpString(){      
33         try {
34                 string helpString = "";
35                 helpString += "The align.check command reads a fasta file and map file as well as an optional name file.\n";
36                 helpString += "It outputs a file containing the secondary structure matches in the .align.check file.\n";
37                 helpString += "The align.check command parameters are fasta and map, both are required.\n";
38                 helpString += "The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n";
39                 helpString += "Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n";
40                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "AlignCheckCommand", "getHelpString");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 string AlignCheckCommand::getOutputFileNameTag(string type, string inputName=""){       
50         try {
51         string outputFileName = "";
52                 map<string, vector<string> >::iterator it;
53         
54         //is this a type this command creates
55         it = outputTypes.find(type);
56         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
57         else {
58             if (type == "aligncheck")            {   outputFileName =  "align.check";   }
59             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
60         }
61         return outputFileName;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "AlignCheckCommand", "getOutputFileNameTag");
65                 exit(1);
66         }
67 }
68 //**********************************************************************************************************************
69 AlignCheckCommand::AlignCheckCommand(){ 
70         try {
71                 abort = true; calledHelp = true; 
72                 setParameters();
73                 vector<string> tempOutNames;
74                 outputTypes["aligncheck"] = tempOutNames;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
78                 exit(1);
79         }
80 }
81 //**********************************************************************************************************************
82
83 AlignCheckCommand::AlignCheckCommand(string option)  {
84         try {
85                 abort = false; calledHelp = false;   
86                 haderror = 0;
87                         
88                 //allow user to run help
89                 if(option == "help") { help(); abort = true; calledHelp = true; }
90                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
91                 
92                 else {
93                         vector<string> myArray = setParameters();
94                         
95                         OptionParser parser(option);
96                         map<string,string> parameters = parser.getParameters();
97                         
98                         ValidParameters validParameter;
99                         map<string,string>::iterator it;
100                         
101                         //check to make sure all parameters are valid for command
102                         for (it = parameters.begin(); it != parameters.end(); it++) { 
103                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
104                         }
105                         
106                         //initialize outputTypes
107                         vector<string> tempOutNames;
108                         outputTypes["aligncheck"] = tempOutNames;
109                         
110                         //if the user changes the input directory command factory will send this info to us in the output parameter 
111                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
112                         if (inputDir == "not found"){   inputDir = "";          }
113                         else {
114                                 string path;
115                                 it = parameters.find("fasta");
116                                 //user has given a template file
117                                 if(it != parameters.end()){ 
118                                         path = m->hasPath(it->second);
119                                         //if the user has not given a path then, add inputdir. else leave path alone.
120                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
121                                 }
122                                 
123                                 it = parameters.find("map");
124                                 //user has given a template file
125                                 if(it != parameters.end()){ 
126                                         path = m->hasPath(it->second);
127                                         //if the user has not given a path then, add inputdir. else leave path alone.
128                                         if (path == "") {       parameters["map"] = inputDir + it->second;              }
129                                 }
130                                 
131                                 it = parameters.find("name");
132                                 //user has given a template file
133                                 if(it != parameters.end()){ 
134                                         path = m->hasPath(it->second);
135                                         //if the user has not given a path then, add inputdir. else leave path alone.
136                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
137                                 }
138                         }
139
140                         //check for required parameters
141                         mapfile = validParameter.validFile(parameters, "map", true);
142                         if (mapfile == "not open") { abort = true; }
143                         else if (mapfile == "not found") {  mapfile = "";  m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }        
144                         
145                         fastafile = validParameter.validFile(parameters, "fasta", true);
146                         if (fastafile == "not open") { fastafile = ""; abort = true; }
147                         else if (fastafile == "not found") {                            
148                                 fastafile = m->getFastaFile(); 
149                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
150                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
151                         }else { m->setFastaFile(fastafile); }   
152                         
153                         namefile = validParameter.validFile(parameters, "name", true);
154                         if (namefile == "not open") { namefile = ""; abort = true; }
155                         else if (namefile == "not found") { namefile = "";  }   
156                         else { m->setNameFile(namefile); }
157                         
158                         //if the user changes the output directory command factory will send this info to us in the output parameter 
159                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
160                                 outputDir = ""; 
161                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
162                         }
163                         
164                         if ((namefile == "") && (fastafile != "")){
165                                 vector<string> files; files.push_back(fastafile); 
166                                 parser.getNameFile(files);
167                         }
168                 }
169
170         }
171         catch(exception& e) {
172                 m->errorOut(e, "AlignCheckCommand", "AlignCheckCommand");
173                 exit(1);
174         }
175 }
176 //**********************************************************************************************************************
177
178 int AlignCheckCommand::execute(){
179         try {
180                 
181                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
182                 
183                 //get secondary structure info.
184                 readMap();
185                 
186                 if (namefile != "") { nameMap = m->readNames(namefile); }
187                 
188                 if (m->control_pressed) { return 0; }
189                 
190                 ifstream in;
191                 m->openInputFile(fastafile, in);
192                 
193                 ofstream out;
194                 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("aligncheck");
195                 m->openOutputFile(outfile, out);
196                 
197                                 
198                 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
199                 out << "loop" << '\t' << "tilde" << '\t' << "total"  << '\t' << "numseqs" << endl;
200
201                 vector<int> pound;
202                 vector<int> dash;
203                 vector<int> plus;
204                 vector<int> equal;
205                 vector<int> loop;
206                 vector<int> tilde;
207                 vector<int> total;
208                 
209                 int count = 0;
210                 while(!in.eof()){
211                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outfile); return 0; }
212                         
213                         Sequence seq(in);  m->gobble(in);
214                         if (seq.getName() != "") {
215                                 statData data = getStats(seq.getAligned());
216                                 
217                                 if (haderror == 1) { m->control_pressed = true; break; }
218                                 
219                                 int num = 1;
220                                 if (namefile != "") {
221                                         //make sure this sequence is in the namefile, else error 
222                                         map<string, int>::iterator it = nameMap.find(seq.getName());
223                                         
224                                         if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + seq.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
225                                         else { num = it->second; }
226                                 }
227                                 
228                                 //for each sequence this sequence represents
229                                 for (int i = 0; i < num; i++) {
230                                         pound.push_back(data.pound);
231                                         dash.push_back(data.dash);
232                                         plus.push_back(data.plus);
233                                         equal.push_back(data.equal);
234                                         loop.push_back(data.loop);
235                                         tilde.push_back(data.tilde);
236                                         total.push_back(data.total);
237                                 }       
238                                 count++;
239                                 
240                                 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
241                                 out << data.loop << '\t' << data.tilde << '\t' << data.total << '\t' << num << endl;
242                         }
243                 }
244
245                 in.close();
246                 out.close();
247                 
248                 if (m->control_pressed) {  m->mothurRemove(outfile); return 0; }
249                 
250                 sort(pound.begin(), pound.end());
251                 sort(dash.begin(), dash.end());
252                 sort(plus.begin(), plus.end());
253                 sort(equal.begin(), equal.end());
254                 sort(loop.begin(), loop.end());
255                 sort(tilde.begin(), tilde.end());
256                 sort(total.begin(), total.end());
257                 int size = pound.size();
258                 
259                 int ptile0_25   = int(size * 0.025);
260                 int ptile25             = int(size * 0.250);
261                 int ptile50             = int(size * 0.500);
262                 int ptile75             = int(size * 0.750);
263                 int ptile97_5   = int(size * 0.975);
264                 int ptile100    = size - 1;
265                 
266                 if (m->control_pressed) {  m->mothurRemove(outfile); return 0; }
267                 
268                 m->mothurOutEndLine();
269                 m->mothurOut("\t\tPound\tDash\tPlus\tEqual\tLoop\tTilde\tTotal"); m->mothurOutEndLine();
270                 m->mothurOut("Minimum:\t" + toString(pound[0]) + "\t" + toString(dash[0]) + "\t" + toString(plus[0]) + "\t" + toString(equal[0]) + "\t" + toString(loop[0]) + "\t" + toString(tilde[0]) + "\t" + toString(total[0])); m->mothurOutEndLine();
271                 m->mothurOut("2.5%-tile:\t" + toString(pound[ptile0_25]) + "\t" + toString(dash[ptile0_25]) + "\t" + toString(plus[ptile0_25]) + "\t" + toString(equal[ptile0_25]) + "\t"+ toString(loop[ptile0_25]) + "\t"+ toString(tilde[ptile0_25]) + "\t"+ toString(total[ptile0_25])); m->mothurOutEndLine();
272                 m->mothurOut("25%-tile:\t" + toString(pound[ptile25]) + "\t" + toString(dash[ptile25]) + "\t" + toString(plus[ptile25]) + "\t" + toString(equal[ptile25]) + "\t" + toString(loop[ptile25]) + "\t" + toString(tilde[ptile25]) + "\t" + toString(total[ptile25])); m->mothurOutEndLine();
273                 m->mothurOut("Median: \t" + toString(pound[ptile50]) + "\t" + toString(dash[ptile50]) + "\t" + toString(plus[ptile50]) + "\t" + toString(equal[ptile50]) + "\t" + toString(loop[ptile50]) + "\t" + toString(tilde[ptile50]) + "\t" + toString(total[ptile50])); m->mothurOutEndLine();
274                 m->mothurOut("75%-tile:\t" + toString(pound[ptile75]) + "\t" + toString(dash[ptile75]) + "\t" + toString(plus[ptile75]) + "\t" + toString(equal[ptile75]) + "\t" + toString(loop[ptile75]) + "\t" + toString(tilde[ptile75]) + "\t" + toString(total[ptile75])); m->mothurOutEndLine();
275                 m->mothurOut("97.5%-tile:\t" + toString(pound[ptile97_5]) + "\t" + toString(dash[ptile97_5]) + "\t" + toString(plus[ptile97_5]) + "\t" + toString(equal[ptile97_5]) + "\t" + toString(loop[ptile97_5]) + "\t" + toString(tilde[ptile97_5]) + "\t" + toString(total[ptile97_5])); m->mothurOutEndLine();
276                 m->mothurOut("Maximum:\t" + toString(pound[ptile100]) + "\t" + toString(dash[ptile100]) + "\t" + toString(plus[ptile100]) + "\t" + toString(equal[ptile100]) + "\t" + toString(loop[ptile100]) + "\t" + toString(tilde[ptile100]) + "\t" + toString(total[ptile100])); m->mothurOutEndLine();
277                 if (namefile == "") {  m->mothurOut("# of Seqs:\t" + toString(count)); m->mothurOutEndLine(); }
278                 else { m->mothurOut("# of unique seqs:\t" + toString(count)); m->mothurOutEndLine(); m->mothurOut("total # of seqs:\t" + toString(size)); m->mothurOutEndLine(); }
279                 
280                 
281                 m->mothurOutEndLine();
282                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
283                 m->mothurOut(outfile); m->mothurOutEndLine();   outputNames.push_back(outfile); outputTypes["aligncheck"].push_back(outfile);
284                 m->mothurOutEndLine();
285                 
286                 return 0;               
287         }
288
289         catch(exception& e) {
290                 m->errorOut(e, "AlignCheckCommand", "execute");
291                 exit(1);
292         }
293 }
294 //**********************************************************************************************************************
295 void AlignCheckCommand::readMap(){
296         try {
297                         
298                 structMap.resize(1, 0);
299                 ifstream in;
300                 
301                 m->openInputFile(mapfile, in);
302                 
303                 while(!in.eof()){
304                         int position;
305                         in >> position;
306                         structMap.push_back(position);  
307                         m->gobble(in);
308                 }
309                 in.close();
310
311                 seqLength = structMap.size();
312                 
313                 
314                 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
315                 for(int i=0;i<seqLength;i++){
316                         if(structMap[i] != 0){
317                                 if(structMap[structMap[i]] != i){
318                                         m->mothurOut("Your map file contains an error:  line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
319                                 }
320                         }
321                 }
322                 
323                 
324         }
325         catch(exception& e) {
326                 m->errorOut(e, "AlignCheckCommand", "readMap");
327                 exit(1);
328         }
329 }
330 /**************************************************************************************************/
331
332 statData AlignCheckCommand::getStats(string sequence){
333         try {
334         
335                 statData data;
336                 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
337                 
338                 int length = sequence.length();
339                 
340                 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;  }
341                 
342                 for(int i=1;i<length;i++){
343                         if(structMap[i] != 0){
344                                 if(sequence[i] == 'A'){
345                                         if(sequence[structMap[i]] == 'T')               {       data.tilde++;   }
346                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
347                                         else if(sequence[structMap[i]] == 'G')  {       data.equal++;   }
348                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
349                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
350                                         data.total++;
351                                 }
352                                 else if(sequence[i] == 'T'){
353                                         if(sequence[structMap[i]] == 'T')               {       data.plus++;    }
354                                         else if(sequence[structMap[i]] == 'A')  {       data.tilde++;   }
355                                         else if(sequence[structMap[i]] == 'G')  {       data.dash++;    }
356                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
357                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
358                                         data.total++;
359                                 }
360                                 else if(sequence[i] == 'G'){
361                                         if(sequence[structMap[i]] == 'T')               {       data.dash++;    }
362                                         else if(sequence[structMap[i]] == 'A')  {       data.equal++;   }
363                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   }
364                                         else if(sequence[structMap[i]] == 'C')  {       data.tilde++;   }
365                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
366                                         data.total++;
367                                 }
368                                 else if(sequence[i] == 'C'){
369                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   }
370                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
371                                         else if(sequence[structMap[i]] == 'G')  {       data.tilde++;   }
372                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
373                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
374                                         data.total++;
375                                 }
376                                 else if(sequence[i] == '-'){
377                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   data.total++;   }
378                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   data.total++;   }
379                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   data.total++;   }
380                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   data.total++;   }
381                                         else if(sequence[structMap[i]] == '-')  {               /*donothing*/                           }
382                                 }                       
383                         }
384                         else if(isalnum(sequence[i])){
385                                 data.loop++;
386                                 data.total++;
387                         }
388                 }
389                 return data;
390                 
391         }
392         catch(exception& e) {
393                 m->errorOut(e, "AlignCheckCommand", "getStats");
394                 exit(1);
395         }
396 }
397 //**********************************************************************************************************************