]> git.donarmstrong.com Git - mothur.git/blob - secondarystructurecommand.cpp
created mothurOut class to handle logfiles
[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 = 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 = 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 += 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                 openInputFile(fastafile, in);
113                 
114                 ofstream out;
115                 string outfile = outputDir + getRootName(getSimpleName(fastafile)) + "align.check";
116                 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                         
124                         Sequence seq(in);  gobble(in);
125                         if (seq.getName() != "") {
126                                 statData data = getStats(seq.getAligned());
127                                 
128                                 if (haderror == 1) { break; }
129                                 
130                                 out << seq.getName() << '\t' << data.pound << '\t' << data.dash << '\t' << data.plus << '\t' << data.equal << '\t';
131                                 out << data.loop << '\t' << data.tilde << '\t' << data.total << endl;
132                         }
133                 }
134
135                 in.close();
136                 out.close();
137                 
138                 m->mothurOutEndLine();
139                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
140                 m->mothurOut(outfile); m->mothurOutEndLine();   
141                 m->mothurOutEndLine();
142                 
143                 return 0;               
144         }
145
146         catch(exception& e) {
147                 m->errorOut(e, "AlignCheckCommand", "execute");
148                 exit(1);
149         }
150 }
151 //**********************************************************************************************************************
152 void AlignCheckCommand::readMap(){
153         try {
154                         
155                 structMap.resize(1, 0);
156                 ifstream in;
157                 
158                 openInputFile(mapfile, in);
159                 
160                 while(!in.eof()){
161                         int position;
162                         in >> position;
163                         structMap.push_back(position);  
164                         gobble(in);
165                 }
166                 in.close();
167
168                 seqLength = structMap.size();
169                 
170                 
171                 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
172                 for(int i=0;i<seqLength;i++){
173                         if(structMap[i] != 0){
174                                 if(structMap[structMap[i]] != i){
175                                         m->mothurOut("Your map file contains an error:  line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); m->mothurOutEndLine();
176                                 }
177                         }
178                 }
179                 
180                 
181         }
182         catch(exception& e) {
183                 m->errorOut(e, "AlignCheckCommand", "readMap");
184                 exit(1);
185         }
186 }
187 /**************************************************************************************************/
188
189 statData AlignCheckCommand::getStats(string sequence){
190         try {
191         
192                 statData data;
193                 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
194                 
195                 int length = sequence.length();
196                 
197                 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;  }
198                 
199                 for(int i=1;i<length;i++){
200                         if(structMap[i] != 0){
201                                 if(sequence[i] == 'A'){
202                                         if(sequence[structMap[i]] == 'T')               {       data.tilde++;   }
203                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
204                                         else if(sequence[structMap[i]] == 'G')  {       data.equal++;   }
205                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
206                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
207                                         data.total++;
208                                 }
209                                 else if(sequence[i] == 'T'){
210                                         if(sequence[structMap[i]] == 'T')               {       data.plus++;    }
211                                         else if(sequence[structMap[i]] == 'A')  {       data.tilde++;   }
212                                         else if(sequence[structMap[i]] == 'G')  {       data.dash++;    }
213                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
214                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
215                                         data.total++;
216                                 }
217                                 else if(sequence[i] == 'G'){
218                                         if(sequence[structMap[i]] == 'T')               {       data.dash++;    }
219                                         else if(sequence[structMap[i]] == 'A')  {       data.equal++;   }
220                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   }
221                                         else if(sequence[structMap[i]] == 'C')  {       data.tilde++;   }
222                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
223                                         data.total++;
224                                 }
225                                 else if(sequence[i] == 'C'){
226                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   }
227                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
228                                         else if(sequence[structMap[i]] == 'G')  {       data.tilde++;   }
229                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
230                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
231                                         data.total++;
232                                 }
233                                 else if(sequence[i] == '-'){
234                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   data.total++;   }
235                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   data.total++;   }
236                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   data.total++;   }
237                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   data.total++;   }
238                                         else if(sequence[structMap[i]] == '-')  {               /*donothing*/                           }
239                                 }                       
240                         }
241                         else if(isalnum(sequence[i])){
242                                 data.loop++;
243                                 data.total++;
244                         }
245                 }
246                 return data;
247                 
248         }
249         catch(exception& e) {
250                 m->errorOut(e, "AlignCheckCommand", "getStats");
251                 exit(1);
252         }
253 }
254
255
256 //**********************************************************************************************************************