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