]> git.donarmstrong.com Git - mothur.git/blob - filterseqscommand.cpp
added logfile feature
[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") { mothurOut("fasta is a required parameter for the filter.seqs command."); mothurOutEndLine(); 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                 errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
64                 exit(1);
65         }
66 }
67
68 //**********************************************************************************************************************
69
70 void FilterSeqsCommand::help(){
71         try {
72                 mothurOut("The filter.seqs command reads a file containing sequences and creates a .filter and .filter.fasta file.\n");
73                 mothurOut("The filter.seqs command parameters are fasta, trump, soft, hard and vertical. \n");
74                 mothurOut("The fasta parameter is required.\n");
75                 mothurOut("The trump parameter .... The default is ...\n");
76                 mothurOut("The soft parameter .... The default is ....\n");
77                 mothurOut("The hard parameter .... The default is ....\n");
78                 mothurOut("The vertical parameter .... The default is T.\n");
79                 mothurOut("The filter.seqs command should be in the following format: \n");
80                 mothurOut("filter.seqs(fasta=yourFastaFile, trump=yourTrump, soft=yourSoft, hard=yourHard, vertical=yourVertical) \n");
81                 mothurOut("Example filter.seqs(fasta=abrecovery.fasta, trump=..., soft=..., hard=..., vertical=T).\n");
82                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
83                 
84         }
85         catch(exception& e) {
86                 errorOut(e, "FilterSeqsCommand", "help");
87                 exit(1);
88         }
89 }
90
91 /**************************************************************************************/
92
93 void FilterSeqsCommand::doHard() {
94         
95         ifstream fileHandle;
96         openInputFile(hard, fileHandle);
97         
98         fileHandle >> filter;
99         
100         fileHandle.close();
101
102 }
103
104 /**************************************************************************************/
105
106 void FilterSeqsCommand::doTrump(Sequence seq) {
107         
108         string curAligned = seq.getAligned();
109
110         for(int j = 0; j < alignmentLength; j++) {
111                 if(curAligned[j] == trump){
112                         filter[j] = '0';
113                 }
114         }
115
116 }
117
118 /**************************************************************************************/
119
120 void FilterSeqsCommand::doVertical() {
121
122         for(int i=0;i<alignmentLength;i++){
123                 if(gap[i] == numSeqs)   {       filter[i] = '0';        }
124         }
125         
126 }
127
128 /**************************************************************************************/
129
130 void FilterSeqsCommand::doSoft() {
131         
132         int threshold = int (soft * numSeqs);
133         
134         for(int i=0;i<alignmentLength;i++){
135                 if(a[i] < threshold && t[i] < threshold && g[i] < threshold && c[i] < threshold){       filter[i] = 0;  }
136         }
137 }
138
139 /**************************************************************************************/
140
141 void FilterSeqsCommand::getFreqs(Sequence seq) {
142
143         string curAligned = seq.getAligned();;
144         
145         for(int j=0;j<alignmentLength;j++){
146                 if(toupper(curAligned[j]) == 'A')                                                                               {       a[j]++;         }
147                 else if(toupper(curAligned[j]) == 'T' || toupper(curAligned[j]) == 'U') {       t[j]++;         }
148                 else if(toupper(curAligned[j]) == 'G')                                                                  {       g[j]++;         }
149                 else if(toupper(curAligned[j]) == 'C')                                                                  {       c[j]++;         }
150                 else if(curAligned[j] == '-' || curAligned[j] == '.')                                   {       gap[j]++;       }
151         }
152         
153 }
154
155 /**************************************************************************************/
156
157 int FilterSeqsCommand::execute() {      
158         try {
159         
160                 if (abort == true) { return 0; }
161                 
162                 ifstream inFASTA;
163                 openInputFile(fastafile, inFASTA);
164                 
165                 Sequence testSeq(inFASTA);
166                 alignmentLength = testSeq.getAlignLength();
167                 inFASTA.seekg(0);
168                 
169                 if(soft != 0 || isTrue(vertical)){
170                         a.assign(alignmentLength, 0);
171                         t.assign(alignmentLength, 0);
172                         g.assign(alignmentLength, 0);
173                         c.assign(alignmentLength, 0);
174                         gap.assign(alignmentLength, 0);
175                 }
176                 
177                 if(hard.compare("") != 0)       {       doHard();               }
178                 else                                            {       filter = string(alignmentLength, '1');  }
179
180                 if(trump != '*' || isTrue(vertical) || soft != 0){
181                         while(!inFASTA.eof()){  //read through and create the filter...
182                                 Sequence seq(inFASTA);
183                                 if(trump != '*'){       doTrump(seq);   }
184                                 if(isTrue(vertical) || soft != 0){      getFreqs(seq);  }
185                                 numSeqs++;
186                                 cout.flush();
187                         }
188                 
189                 }
190                 inFASTA.close();
191                 
192                 if(isTrue(vertical) == 1)       {       doVertical();   }
193                 if(soft != 0)   {       doSoft();               }                       
194
195                 ofstream outFilter;
196                 string filterFile = getRootName(fastafile) + "filter";
197                 openOutputFile(filterFile, outFilter);
198                 outFilter << filter << endl;
199                 outFilter.close();
200                 
201                 ifstream inFasta2;
202                 openInputFile(fastafile, inFasta2);
203                 string filteredFasta = getRootName(fastafile) + "filter.fasta";
204                 ofstream outFASTA;
205                 openOutputFile(filteredFasta, outFASTA);
206
207                 numSeqs = 0;
208                 while(!inFasta2.eof()){
209                         Sequence seq(inFasta2);
210                         string align = seq.getAligned();
211                         string filterSeq = "";
212         
213                         for(int j=0;j<alignmentLength;j++){
214                                 if(filter[j] == '1'){
215                                         filterSeq += align[j];
216                                 }
217                         }
218
219                         outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
220                         numSeqs++;
221                         gobble(inFasta2);
222                 }
223                 outFASTA.close();
224                 inFasta2.close();
225                 
226                 
227                 int filteredLength = 0;
228                 for(int i=0;i<alignmentLength;i++){
229                         if(filter[i] == '1'){   filteredLength++;       }
230                 }
231                 
232                 mothurOutEndLine();
233                 mothurOut("Length of filtered alignment: " + toString(filteredLength)); mothurOutEndLine();
234                 mothurOut("Number of columns removed: " + toString((alignmentLength-filteredLength))); mothurOutEndLine();
235                 mothurOut("Length of the original alignment: " + toString(alignmentLength)); mothurOutEndLine();
236                 mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); mothurOutEndLine();
237                 
238                 return 0;
239                 
240         }
241         catch(exception& e) {
242                 errorOut(e, "FilterSeqsCommand", "execute");
243                 exit(1);
244         }
245 }
246
247 /**************************************************************************************/