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