2 * filterseqscommand.cpp
5 * Created by Thomas Ryabin on 5/4/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "filterseqscommand.h"
11 #include "sequence.hpp"
13 /**************************************************************************************/
15 FilterSeqsCommand::FilterSeqsCommand(string option){
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"fasta", "trump", "soft", "hard", "vertical"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
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; }
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; }
42 //check for optional parameter and set defaults
43 // ...at some point should added some additional type checking...
46 temp = validParameter.validFile(parameters, "trump", false); if (temp == "not found") { temp = "*"; }
49 temp = validParameter.validFile(parameters, "soft", false); if (temp == "not found") { soft = 0; }
50 else { soft = (float)atoi(temp.c_str()) / 100.0; }
52 hard = validParameter.validFile(parameters, "hard", true); if (hard == "not found") { hard = ""; }
53 else if (hard == "not open") { abort = true; }
55 vertical = validParameter.validFile(parameters, "vertical", false); if (vertical == "not found") { vertical = "T"; }
63 cout << "Standard Error: " << e.what() << " has occurred in the FilterSeqsCommand class Function FilterSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
67 cout << "An unknown error has occurred in the FilterSeqsCommand class function FilterSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72 //**********************************************************************************************************************
74 void FilterSeqsCommand::help(){
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";
90 cout << "Standard Error: " << e.what() << " has occurred in the FilterSeqsCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
94 cout << "An unknown error has occurred in the FilterSeqsCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99 /**************************************************************************************/
101 void FilterSeqsCommand::doHard() {
104 openInputFile(hard, fileHandle);
106 fileHandle >> filter;
112 /**************************************************************************************/
114 void FilterSeqsCommand::doTrump(Sequence seq) {
116 string curAligned = seq.getAligned();
118 for(int j = 0; j < alignmentLength; j++) {
119 if(curAligned[j] == trump){
126 /**************************************************************************************/
128 void FilterSeqsCommand::doVertical() {
130 for(int i=0;i<alignmentLength;i++){
131 if(gap[i] == numSeqs) { filter[i] = '0'; }
136 /**************************************************************************************/
138 void FilterSeqsCommand::doSoft() {
140 int threshold = int (soft * numSeqs);
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; }
147 /**************************************************************************************/
149 void FilterSeqsCommand::getFreqs(Sequence seq) {
151 string curAligned = seq.getAligned();;
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]++; }
163 /**************************************************************************************/
165 int FilterSeqsCommand::execute() {
168 if (abort == true) { return 0; }
171 openInputFile(fastafile, inFASTA);
173 Sequence testSeq(inFASTA);
174 alignmentLength = testSeq.getAlignLength();
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);
185 if(hard.compare("") != 0) { doHard(); }
186 else { filter = string(alignmentLength, '1'); }
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); }
200 if(isTrue(vertical) == 1) { doVertical(); }
201 if(soft != 0) { doSoft(); }
204 string filterFile = getRootName(fastafile) + "filter";
205 openOutputFile(filterFile, outFilter);
206 outFilter << filter << endl;
210 openInputFile(fastafile, inFASTA);
211 string filteredFasta = getRootName(fastafile) + "filter.fasta";
213 openOutputFile(filteredFasta, outFASTA);
216 while(!inFASTA.eof()){
217 Sequence seq(inFASTA);
218 string align = seq.getAligned();
219 string filterSeq = "";
221 for(int j=0;j<alignmentLength;j++){
222 if(filter[j] == '1'){
223 filterSeq += align[j];
227 outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
235 int filteredLength = 0;
236 for(int i=0;i<alignmentLength;i++){
237 if(filter[i] == '1'){ filteredLength++; }
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;
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";
254 cout << "An unknown error has occurred in the FilterSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259 /**************************************************************************************/