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