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