]> git.donarmstrong.com Git - mothur.git/blob - filterseqscommand.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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 }
109
110 /**************************************************************************************/
111
112 void FilterSeqsCommand::doTrump(Sequence seq) {
113         
114         string curAligned = seq.getAligned();
115
116         for(int j = 0; j < alignmentLength; j++) {
117                 if(curAligned[j] == trump){
118                         filter[j] = '0';
119                 }
120         }
121
122 }
123
124 /**************************************************************************************/
125
126 void FilterSeqsCommand::doVertical() {
127
128         for(int i=0;i<alignmentLength;i++){
129                 if(gap[i] == numSeqs)   {       filter[i] = '0';        }
130         }
131         
132 }
133
134 /**************************************************************************************/
135
136 void FilterSeqsCommand::doSoft() {
137         
138         int threshold = int (soft * numSeqs);
139         
140         for(int i=0;i<alignmentLength;i++){
141                 if(a[i] < threshold && t[i] < threshold && g[i] < threshold && c[i] < threshold){       filter[i] = 0;  }
142         }
143 }
144
145 /**************************************************************************************/
146
147 void FilterSeqsCommand::getFreqs(Sequence seq) {
148
149         string curAligned = seq.getAligned();;
150         
151         for(int j=0;j<alignmentLength;j++){
152                 if(toupper(curAligned[j]) == 'A')                                                                               {       a[j]++;         }
153                 else if(toupper(curAligned[j]) == 'T' || toupper(curAligned[j]) == 'U') {       t[j]++;         }
154                 else if(toupper(curAligned[j]) == 'G')                                                                  {       g[j]++;         }
155                 else if(toupper(curAligned[j]) == 'C')                                                                  {       c[j]++;         }
156                 else if(curAligned[j] == '-' || curAligned[j] == '.')                                   {       gap[j]++;       }
157         }
158         
159 }
160
161 /**************************************************************************************/
162
163 int FilterSeqsCommand::execute() {      
164         try {
165         
166                 if (abort == true) { return 0; }
167                 
168                 ifstream inFASTA;
169                 openInputFile(fastafile, inFASTA);
170                 
171                 Sequence testSeq(inFASTA);
172                 alignmentLength = testSeq.getAlignLength();
173                 inFASTA.seekg(0);
174                 
175                 if(soft != 0 || isTrue(vertical)){
176                         a.assign(alignmentLength, 0);
177                         t.assign(alignmentLength, 0);
178                         g.assign(alignmentLength, 0);
179                         c.assign(alignmentLength, 0);
180                         gap.assign(alignmentLength, 0);
181                 }
182                 
183                 if(hard.compare("") != 0)       {       doHard();               }
184                 else                                            {       filter = string(alignmentLength, '1');  }
185
186                 if(trump != '*' || isTrue(vertical) || soft != 0){
187                         while(!inFASTA.eof()){  //read through and create the filter...
188                                 Sequence seq(inFASTA);
189                                 if(trump != '*'){       doTrump(seq);   }
190                                 if(isTrue(vertical) || soft != 0){      getFreqs(seq);  }
191                                 numSeqs++;
192                                 cout.flush();
193                         }
194                 
195                 }
196                 inFASTA.close();
197                 
198                 if(isTrue(vertical) == 1)       {       doVertical();   }
199                 if(soft != 0)   {       doSoft();               }                       
200
201                 ofstream outFilter;
202                 string filterFile = getRootName(fastafile) + "filter";
203                 openOutputFile(filterFile, outFilter);
204                 outFilter << filter << endl;
205                 outFilter.close();
206                 
207
208                 openInputFile(fastafile, inFASTA);
209                 string filteredFasta = getRootName(fastafile) + "filter.fasta";
210                 ofstream outFASTA;
211                 openOutputFile(filteredFasta, outFASTA);
212
213                 numSeqs = 0;
214                 while(!inFASTA.eof()){
215                         Sequence seq(inFASTA);
216                         string align = seq.getAligned();
217                         string filterSeq = "";
218         
219                         for(int j=0;j<alignmentLength;j++){
220                                 if(filter[j] == '1'){
221                                         filterSeq += align[j];
222                                 }
223                         }
224
225                         outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
226                         numSeqs++;
227                         gobble(inFASTA);
228                 }
229                 outFASTA.close();
230                 inFASTA.close();
231                 
232                 
233                 int filteredLength = 0;
234                 for(int i=0;i<alignmentLength;i++){
235                         if(filter[i] == '1'){   filteredLength++;       }
236                 }
237                 
238                 cout << endl;
239                 cout << "Length of filtered alignment: " << filteredLength << endl;
240                 cout << "Number of columns removed: " << alignmentLength-filteredLength << endl;
241                 cout << "Length of the original alignment: " << alignmentLength << endl;
242                 cout << "Number of sequences used to construct filter: " << numSeqs << endl;
243                 
244                 return 0;
245                 
246         }
247         catch(exception& e) {
248                 cout << "Standard Error: " << e.what() << " has occurred in the FilterSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249                 exit(1);
250         }
251         catch(...) {
252                 cout << "An unknown error has occurred in the FilterSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
253                 exit(1);
254         }
255 }
256
257 /**************************************************************************************/