]> git.donarmstrong.com Git - mothur.git/blob - secondarystructurecommand.cpp
fixed bugs for 1.8
[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 = "";  mothurOut("You must provide an map file."); 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 = "";  mothurOut("You must provide an fasta file."); 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                 errorOut(e, "AlignCheckCommand", "RemoveSeqsCommand");
81                 exit(1);
82         }
83 }
84 //**********************************************************************************************************************
85
86 void AlignCheckCommand::help(){
87         try {
88                 mothurOut("The align.check command reads a fasta file and map file.\n");
89                 mothurOut("It outputs a file containing the secondary structure matches in the .align.check file.\n");
90                 mothurOut("The align.check command parameters are fasta and map, both are required.\n");
91                 mothurOut("The align.check command should be in the following format: align.check(fasta=yourFasta, map=yourMap).\n");
92                 mothurOut("Example align.check(map=silva.ss.map, fasta=amazon.fasta).\n");
93                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
94         }
95         catch(exception& e) {
96                 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                 return 0;               
139         }
140
141         catch(exception& e) {
142                 errorOut(e, "AlignCheckCommand", "execute");
143                 exit(1);
144         }
145 }
146 //**********************************************************************************************************************
147 void AlignCheckCommand::readMap(){
148         try {
149                         
150                 structMap.resize(1, 0);
151                 ifstream in;
152                 
153                 openInputFile(mapfile, in);
154                 
155                 while(!in.eof()){
156                         int position;
157                         in >> position;
158                         structMap.push_back(position);  
159                         gobble(in);
160                 }
161                 in.close();
162
163                 seqLength = structMap.size();
164                 
165                 
166                 //check you make sure is structMap[10] = 380 then structMap[380] = 10.
167                 for(int i=0;i<seqLength;i++){
168                         if(structMap[i] != 0){
169                                 if(structMap[structMap[i]] != i){
170                                         mothurOut("Your map file contains an error:  line " + toString(i) + " does not match line " + toString(structMap[i]) + "."); mothurOutEndLine();
171                                 }
172                         }
173                 }
174                 
175                 
176         }
177         catch(exception& e) {
178                 errorOut(e, "AlignCheckCommand", "readMap");
179                 exit(1);
180         }
181 }
182 /**************************************************************************************************/
183
184 statData AlignCheckCommand::getStats(string sequence){
185         try {
186         
187                 statData data;
188                 sequence = "*" + sequence; // need to pad the sequence so we can index it by 1
189                 
190                 int length = sequence.length();
191                 
192                 if (length != seqLength) { mothurOut("your sequences are " + toString(length) + " long, but your map file only contains " + toString(seqLength) + " entries. please correct."); mothurOutEndLine(); haderror = 1; return data;  }
193                 
194                 for(int i=1;i<length;i++){
195                         if(structMap[i] != 0){
196                                 if(sequence[i] == 'A'){
197                                         if(sequence[structMap[i]] == 'T')               {       data.tilde++;   }
198                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
199                                         else if(sequence[structMap[i]] == 'G')  {       data.equal++;   }
200                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
201                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
202                                         data.total++;
203                                 }
204                                 else if(sequence[i] == 'T'){
205                                         if(sequence[structMap[i]] == 'T')               {       data.plus++;    }
206                                         else if(sequence[structMap[i]] == 'A')  {       data.tilde++;   }
207                                         else if(sequence[structMap[i]] == 'G')  {       data.dash++;    }
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] == 'G'){
213                                         if(sequence[structMap[i]] == 'T')               {       data.dash++;    }
214                                         else if(sequence[structMap[i]] == 'A')  {       data.equal++;   }
215                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   }
216                                         else if(sequence[structMap[i]] == 'C')  {       data.tilde++;   }
217                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
218                                         data.total++;
219                                 }
220                                 else if(sequence[i] == 'C'){
221                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   }
222                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   }
223                                         else if(sequence[structMap[i]] == 'G')  {       data.tilde++;   }
224                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   }
225                                         else if(sequence[structMap[i]] == '-')  {       data.pound++;   }
226                                         data.total++;
227                                 }
228                                 else if(sequence[i] == '-'){
229                                         if(sequence[structMap[i]] == 'T')               {       data.pound++;   data.total++;   }
230                                         else if(sequence[structMap[i]] == 'A')  {       data.pound++;   data.total++;   }
231                                         else if(sequence[structMap[i]] == 'G')  {       data.pound++;   data.total++;   }
232                                         else if(sequence[structMap[i]] == 'C')  {       data.pound++;   data.total++;   }
233                                         else if(sequence[structMap[i]] == '-')  {               /*donothing*/                           }
234                                 }                       
235                         }
236                         else if(isalnum(sequence[i])){
237                                 data.loop++;
238                                 data.total++;
239                         }
240                 }
241                 return data;
242                 
243         }
244         catch(exception& e) {
245                 errorOut(e, "AlignCheckCommand", "getStats");
246                 exit(1);
247         }
248 }
249
250
251 //**********************************************************************************************************************