]> git.donarmstrong.com Git - mothur.git/blob - secondarystructurecommand.cpp
moved utilities out of mothur.h and into mothurOut class.
[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
15 AlignCheckCommand::AlignCheckCommand(string option)  {
16         try {
17                 abort = false;
18                 haderror = 0;
19                         
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; }
22                 
23                 else {
24                         //valid paramters for this command
25                         string Array[] =  {"fasta","map", "outputdir","inputdir"};
26                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string,string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                         map<string,string>::iterator it;
33                         
34                         //check to make sure all parameters are valid for command
35                         for (it = parameters.begin(); it != parameters.end(); it++) { 
36                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
37                         }
38                         
39                         //if the user changes the input directory command factory will send this info to us in the output parameter 
40                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
41                         if (inputDir == "not found"){   inputDir = "";          }
42                         else {
43                                 string path;
44                                 it = parameters.find("fasta");
45                                 //user has given a template file
46                                 if(it != parameters.end()){ 
47                                         path = m->hasPath(it->second);
48                                         //if the user has not given a path then, add inputdir. else leave path alone.
49                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
50                                 }
51                                 
52                                 it = parameters.find("map");
53                                 //user has given a template file
54                                 if(it != parameters.end()){ 
55                                         path = m->hasPath(it->second);
56                                         //if the user has not given a path then, add inputdir. else leave path alone.
57                                         if (path == "") {       parameters["map"] = inputDir + it->second;              }
58                                 }
59                         }
60
61                         //check for required parameters
62                         mapfile = validParameter.validFile(parameters, "map", true);
63                         if (mapfile == "not open") { abort = true; }
64                         else if (mapfile == "not found") {  mapfile = "";  m->mothurOut("You must provide an map file."); m->mothurOutEndLine(); abort = true; }        
65                         
66                         fastafile = validParameter.validFile(parameters, "fasta", true);
67                         if (fastafile == "not open") { abort = true; }
68                         else if (fastafile == "not found") {  fastafile = "";  m->mothurOut("You must provide an fasta file."); m->mothurOutEndLine(); abort = true;  } 
69                         
70                         //if the user changes the output directory command factory will send this info to us in the output parameter 
71                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
72                                 outputDir = ""; 
73                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
74                         }
75
76                 }
77
78         }
79         catch(exception& e) {
80                 m->errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
81                 exit(1);
82         }
83 }
84 //**********************************************************************************************************************
85
86 void AlignCheckCommand::help(){
87         try {
88                 m->mothurOut("The align.check command reads a fasta file and map file.\n");
89                 m->mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
90                 m->mothurOut("The align.check command parameters are fasta and map, both are required.\n");
91                 m->mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
92                 m->mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
93                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
94         }
95         catch(exception& e) {
96                 m->errorOut(e, "AlignCheckCommand", "help");
97                 exit(1);
98         }
99 }
100
101 //**********************************************************************************************************************
102
103 int AlignCheckCommand::execute(){
104         try {
105                 
106                 if (abort == true) { return 0; }
107                 
108                 //get secondary structure info.
109                 readMap();
110                 
111                 ifstream in;
112                 m->openInputFile(fastafile, in);
113                 
114                 ofstream out;
115                 string outfile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "align.check";
116                 m->openOutputFile(outfile, out);
117                 
118                 out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t';
119                 out << "loop" << '\t' << "tilde" << '\t' << "total" << endl;
120
121                 
122                 while(!in.eof()){
123                         if (m->control_pressed) { in.close(); out.close(); remove(outfile.c_str()); return 0; }
124                         
125                         Sequence seq(in);  m->gobble(in);
126                         if (seq.getName() != "") {
127                                 statData data = getStats(seq.getAligned());
128                                 
129                                 if (haderror == 1) { break; }
130                                 
131                                 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
132                                 out << data.loop << '\t' << data.tilde << '\t' << data.total << endl;
133                         }
134                 }
135
136                 in.close();
137                 out.close();
138                 
139                 if (m->control_pressed) {  remove(outfile.c_str()); return 0; }
140                 
141                 m->mothurOutEndLine();
142                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
143                 m->mothurOut(outfile); m->mothurOutEndLine();   
144                 m->mothurOutEndLine();
145                 
146                 return 0;               
147         }
148
149         catch(exception& e) {
150                 m->errorOut(e, "AlignCheckCommand", "execute");
151                 exit(1);
152         }
153 }
154 //**********************************************************************************************************************
155 void AlignCheckCommand::readMap(){
156         try {
157                         
158                 structMap.resize(1, 0);
159                 ifstream in;
160                 
161                 m->openInputFile(mapfile, in);
162                 
163                 while(!in.eof()){
164                         int position;
165                         in >> position;
166                         structMap.push_back(position);  
167                         m->gobble(in);
168                 }
169                 in.close();
170
171                 seqLength = structMap.size();
172                 
173                 
174                 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
175                 for(int i=0;i<seqLength;i++){
176                         if(structMap[i] != 0){
177                                 if(structMap[structMap[i]] != i){
178                                         m->mothurOut("Your map file contains an error:  line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
179                                 }
180                         }
181                 }
182                 
183                 
184         }
185         catch(exception& e) {
186                 m->errorOut(e, "AlignCheckCommand", "readMap");
187                 exit(1);
188         }
189 }
190 /**************************************************************************************************/
191
192 statData AlignCheckCommand::getStats(string sequence){
193         try {
194         
195                 statData data;
196                 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
197                 
198                 int length = sequence.length();
199                 
200                 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;  }
201                 
202                 for(int i=1;i<length;i++){
203                         if(structMap[i] != 0){
204                                 if(sequence[i] == 'A'){
205                                         if(sequence[structMap[i]] == 'T')               {       data.tilde++;   }
206                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
207                                         else if(sequence[structMap[i]] == 'G')  {       data.equal++;   }
208                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
209                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
210                                         data.total++;
211                                 }
212                                 else if(sequence[i] == 'T'){
213                                         if(sequence[structMap[i]] == 'T')               {       data.plus++;    }
214                                         else if(sequence[structMap[i]] == 'A')  {       data.tilde++;   }
215                                         else if(sequence[structMap[i]] == 'G')  {       data.dash++;    }
216                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
217                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
218                                         data.total++;
219                                 }
220                                 else if(sequence[i] == 'G'){
221                                         if(sequence[structMap[i]] == 'T')               {       data.dash++;    }
222                                         else if(sequence[structMap[i]] == 'A')  {       data.equal++;   }
223                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   }
224                                         else if(sequence[structMap[i]] == 'C')  {       data.tilde++;   }
225                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
226                                         data.total++;
227                                 }
228                                 else if(sequence[i] == 'C'){
229                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   }
230                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
231                                         else if(sequence[structMap[i]] == 'G')  {       data.tilde++;   }
232                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
233                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
234                                         data.total++;
235                                 }
236                                 else if(sequence[i] == '-'){
237                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   data.total++;   }
238                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   data.total++;   }
239                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   data.total++;   }
240                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   data.total++;   }
241                                         else if(sequence[structMap[i]] == '-')  {               /*donothing*/                           }
242                                 }                       
243                         }
244                         else if(isalnum(sequence[i])){
245                                 data.loop++;
246                                 data.total++;
247                         }
248                 }
249                 return data;
250                 
251         }
252         catch(exception& e) {
253                 m->errorOut(e, "AlignCheckCommand", "getStats");
254                 exit(1);
255         }
256 }
257
258
259 //**********************************************************************************************************************