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