]> git.donarmstrong.com Git - mothur.git/blob - filterseqscommand.cpp
fixed some bugs
[mothur.git] / filterseqscommand.cpp
1 /*
2  *  filterseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 5/4/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "filterseqscommand.h"
11 #include "sequence.hpp"
12
13 /**************************************************************************************/
14
15 FilterSeqsCommand::FilterSeqsCommand(string option){
16         try {
17                 abort = false;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"fasta", "trump", "soft", "hard", "vertical"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                         
32                         //check to make sure all parameters are valid for command
33                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
34                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
35                         }
36                         
37                         //check for required parameters
38                         fastafile = validParameter.validFile(parameters, "fasta", true);
39                         if (fastafile == "not found") { cout << "fasta is a required parameter for the filter.seqs command." << endl; abort = true; }
40                         else if (fastafile == "not open") { abort = true; }     
41
42                         //check for optional parameter and set defaults
43                         // ...at some point should added some additional type checking...
44                         
45                         string temp;
46                         temp = validParameter.validFile(parameters, "trump", false);                    if (temp == "not found") { temp = "*"; }
47                         trump = temp[0];
48                         
49                         temp = validParameter.validFile(parameters, "soft", false);                             if (temp == "not found") { soft = 0; }
50                         else {  soft = (float)atoi(temp.c_str()) / 100.0;  }
51                         
52                         hard = validParameter.validFile(parameters, "hard", true);                              if (hard == "not found") { hard = ""; }
53                         else if (hard == "not open") { abort = true; }  
54                         
55                         vertical = validParameter.validFile(parameters, "vertical", false);             if (vertical == "not found") { vertical = "T"; }
56                         
57                         numSeqs = 0;
58                         
59                 }
60                 
61         }
62         catch(exception& e) {
63                 cout << "Standard Error: " << e.what() << " has occurred in the FilterSeqsCommand class Function FilterSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
64                 exit(1);
65         }
66         catch(...) {
67                 cout << "An unknown error has occurred in the FilterSeqsCommand class function FilterSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68                 exit(1);
69         }       
70 }
71
72 //**********************************************************************************************************************
73
74 void FilterSeqsCommand::help(){
75         try {
76                 cout << "The filter.seqs command reads a file containing sequences and creates a .filter and .filter.fasta file." << "\n";
77                 cout << "The filter.seqs command parameters are fasta, trump, soft, hard and vertical.  " << "\n";
78                 cout << "The fasta parameter is required." << "\n";
79                 cout << "The trump parameter .... The default is ..." << "\n";
80                 cout << "The soft parameter .... The default is ...." << "\n";
81                 cout << "The hard parameter .... The default is ...." << "\n";
82                 cout << "The vertical parameter .... The default is T." << "\n";
83                 cout << "The filter.seqs command should be in the following format: " << "\n";
84                 cout << "filter.seqs(fasta=yourFastaFile, trump=yourTrump, soft=yourSoft, hard=yourHard, vertical=yourVertical) " << "\n";
85                 cout << "Example filter.seqs(fasta=abrecovery.fasta, trump=..., soft=..., hard=..., vertical=T)." << "\n";
86                 cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta)." << "\n" << "\n";
87                 
88         }
89         catch(exception& e) {
90                 cout << "Standard Error: " << e.what() << " has occurred in the FilterSeqsCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
91                 exit(1);
92         }
93         catch(...) {
94                 cout << "An unknown error has occurred in the FilterSeqsCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95                 exit(1);
96         }       
97 }
98
99 /**************************************************************************************/
100
101 void FilterSeqsCommand::doHard() {
102         
103         ifstream fileHandle;
104         openInputFile(hard, fileHandle);
105         
106         fileHandle >> filter;
107         
108         fileHandle.close();
109
110 }
111
112 /**************************************************************************************/
113
114 void FilterSeqsCommand::doTrump(Sequence seq) {
115         
116         string curAligned = seq.getAligned();
117
118         for(int j = 0; j < alignmentLength; j++) {
119                 if(curAligned[j] == trump){
120                         filter[j] = '0';
121                 }
122         }
123
124 }
125
126 /**************************************************************************************/
127
128 void FilterSeqsCommand::doVertical() {
129
130         for(int i=0;i<alignmentLength;i++){
131                 if(gap[i] == numSeqs)   {       filter[i] = '0';        }
132         }
133         
134 }
135
136 /**************************************************************************************/
137
138 void FilterSeqsCommand::doSoft() {
139         
140         int threshold = int (soft * numSeqs);
141         
142         for(int i=0;i<alignmentLength;i++){
143                 if(a[i] < threshold && t[i] < threshold && g[i] < threshold && c[i] < threshold){       filter[i] = 0;  }
144         }
145 }
146
147 /**************************************************************************************/
148
149 void FilterSeqsCommand::getFreqs(Sequence seq) {
150
151         string curAligned = seq.getAligned();;
152         
153         for(int j=0;j<alignmentLength;j++){
154                 if(toupper(curAligned[j]) == 'A')                                                                               {       a[j]++;         }
155                 else if(toupper(curAligned[j]) == 'T' || toupper(curAligned[j]) == 'U') {       t[j]++;         }
156                 else if(toupper(curAligned[j]) == 'G')                                                                  {       g[j]++;         }
157                 else if(toupper(curAligned[j]) == 'C')                                                                  {       c[j]++;         }
158                 else if(curAligned[j] == '-' || curAligned[j] == '.')                                   {       gap[j]++;       }
159         }
160         
161 }
162
163 /**************************************************************************************/
164
165 int FilterSeqsCommand::execute() {      
166         try {
167         
168                 if (abort == true) { return 0; }
169                 
170                 ifstream inFASTA;
171                 openInputFile(fastafile, inFASTA);
172                 
173                 Sequence testSeq(inFASTA);
174                 alignmentLength = testSeq.getAlignLength();
175                 inFASTA.seekg(0);
176                 
177                 if(soft != 0 || isTrue(vertical)){
178                         a.assign(alignmentLength, 0);
179                         t.assign(alignmentLength, 0);
180                         g.assign(alignmentLength, 0);
181                         c.assign(alignmentLength, 0);
182                         gap.assign(alignmentLength, 0);
183                 }
184                 
185                 if(hard.compare("") != 0)       {       doHard();               }
186                 else                                            {       filter = string(alignmentLength, '1');  }
187
188                 if(trump != '*' || isTrue(vertical) || soft != 0){
189                         while(!inFASTA.eof()){  //read through and create the filter...
190                                 Sequence seq(inFASTA);
191                                 if(trump != '*'){       doTrump(seq);   }
192                                 if(isTrue(vertical) || soft != 0){      getFreqs(seq);  }
193                                 numSeqs++;
194                                 cout.flush();
195                         }
196                 
197                 }
198                 inFASTA.close();
199                 
200                 if(isTrue(vertical) == 1)       {       doVertical();   }
201                 if(soft != 0)   {       doSoft();               }                       
202
203                 ofstream outFilter;
204                 string filterFile = getRootName(fastafile) + "filter";
205                 openOutputFile(filterFile, outFilter);
206                 outFilter << filter << endl;
207                 outFilter.close();
208                 
209
210                 openInputFile(fastafile, inFASTA);
211                 string filteredFasta = getRootName(fastafile) + "filter.fasta";
212                 ofstream outFASTA;
213                 openOutputFile(filteredFasta, outFASTA);
214
215                 numSeqs = 0;
216                 while(!inFASTA.eof()){
217                         Sequence seq(inFASTA);
218                         string align = seq.getAligned();
219                         string filterSeq = "";
220         
221                         for(int j=0;j<alignmentLength;j++){
222                                 if(filter[j] == '1'){
223                                         filterSeq += align[j];
224                                 }
225                         }
226
227                         outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
228                         numSeqs++;
229                         gobble(inFASTA);
230                 }
231                 outFASTA.close();
232                 inFASTA.close();
233                 
234                 
235                 int filteredLength = 0;
236                 for(int i=0;i<alignmentLength;i++){
237                         if(filter[i] == '1'){   filteredLength++;       }
238                 }
239                 
240                 cout << endl;
241                 cout << "Length of filtered alignment: " << filteredLength << endl;
242                 cout << "Number of columns removed: " << alignmentLength-filteredLength << endl;
243                 cout << "Length of the original alignment: " << alignmentLength << endl;
244                 cout << "Number of sequences used to construct filter: " << numSeqs << endl;
245                 
246                 return 0;
247                 
248         }
249         catch(exception& e) {
250                 cout << "Standard Error: " << e.what() << " has occurred in the FilterSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
251                 exit(1);
252         }
253         catch(...) {
254                 cout << "An unknown error has occurred in the FilterSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
255                 exit(1);
256         }
257 }
258
259 /**************************************************************************************/