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