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") { mothurOut("fasta is a required parameter for the filter.seqs command."); mothurOutEndLine(); 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 errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
68 //**********************************************************************************************************************
70 void FilterSeqsCommand::help(){
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");
86 errorOut(e, "FilterSeqsCommand", "help");
91 /**************************************************************************************/
93 void FilterSeqsCommand::doHard() {
96 openInputFile(hard, fileHandle);
104 /**************************************************************************************/
106 void FilterSeqsCommand::doTrump(Sequence seq) {
108 string curAligned = seq.getAligned();
110 for(int j = 0; j < alignmentLength; j++) {
111 if(curAligned[j] == trump){
118 /**************************************************************************************/
120 void FilterSeqsCommand::doVertical() {
122 for(int i=0;i<alignmentLength;i++){
123 if(gap[i] == numSeqs) { filter[i] = '0'; }
128 /**************************************************************************************/
130 void FilterSeqsCommand::doSoft() {
132 int threshold = int (soft * numSeqs);
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; }
139 /**************************************************************************************/
141 void FilterSeqsCommand::getFreqs(Sequence seq) {
143 string curAligned = seq.getAligned();;
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]++; }
155 /**************************************************************************************/
157 int FilterSeqsCommand::execute() {
160 if (abort == true) { return 0; }
163 openInputFile(fastafile, inFASTA);
165 Sequence testSeq(inFASTA);
166 alignmentLength = testSeq.getAlignLength();
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);
177 if(hard.compare("") != 0) { doHard(); }
178 else { filter = string(alignmentLength, '1'); }
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); }
192 if(isTrue(vertical) == 1) { doVertical(); }
193 if(soft != 0) { doSoft(); }
196 string filterFile = getRootName(fastafile) + "filter";
197 openOutputFile(filterFile, outFilter);
198 outFilter << filter << endl;
202 openInputFile(fastafile, inFasta2);
203 string filteredFasta = getRootName(fastafile) + "filter.fasta";
205 openOutputFile(filteredFasta, outFASTA);
208 while(!inFasta2.eof()){
209 Sequence seq(inFasta2);
210 string align = seq.getAligned();
211 string filterSeq = "";
213 for(int j=0;j<alignmentLength;j++){
214 if(filter[j] == '1'){
215 filterSeq += align[j];
219 outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
227 int filteredLength = 0;
228 for(int i=0;i<alignmentLength;i++){
229 if(filter[i] == '1'){ filteredLength++; }
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();
241 catch(exception& e) {
242 errorOut(e, "FilterSeqsCommand", "execute");
247 /**************************************************************************************/