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", "outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string,string>::iterator it;
33 //check to make sure all parameters are valid for command
34 for (it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 //if the user changes the input directory command factory will send this info to us in the output parameter
39 string inputDir = validParameter.validFile(parameters, "inputdir", false);
40 if (inputDir == "not found"){ inputDir = ""; }
43 it = parameters.find("fasta");
44 //user has given a template file
45 if(it != parameters.end()){
46 path = hasPath(it->second);
47 //if the user has not given a path then, add inputdir. else leave path alone.
48 if (path == "") { parameters["fasta"] = inputDir + it->second; }
51 it = parameters.find("hard");
52 //user has given a template file
53 if(it != parameters.end()){
54 path = hasPath(it->second);
55 //if the user has not given a path then, add inputdir. else leave path alone.
56 if (path == "") { parameters["hard"] = inputDir + it->second; }
60 //check for required parameters
61 fastafile = validParameter.validFile(parameters, "fasta", true);
62 if (fastafile == "not found") { mothurOut("fasta is a required parameter for the filter.seqs command."); mothurOutEndLine(); abort = true; }
63 else if (fastafile == "not open") { abort = true; }
65 //if the user changes the output directory command factory will send this info to us in the output parameter
66 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
68 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
71 //check for optional parameter and set defaults
72 // ...at some point should added some additional type checking...
75 temp = validParameter.validFile(parameters, "trump", false); if (temp == "not found") { temp = "*"; }
78 temp = validParameter.validFile(parameters, "soft", false); if (temp == "not found") { soft = 0; }
79 else { soft = (float)atoi(temp.c_str()) / 100.0; }
81 hard = validParameter.validFile(parameters, "hard", true); if (hard == "not found") { hard = ""; }
82 else if (hard == "not open") { abort = true; }
84 vertical = validParameter.validFile(parameters, "vertical", false); if (vertical == "not found") { vertical = "T"; }
90 if (soft != 0) { F.setSoft(soft); }
91 if (trump != '*') { F.setTrump(trump); }
99 errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
104 //**********************************************************************************************************************
106 void FilterSeqsCommand::help(){
108 mothurOut("The filter.seqs command reads a file containing sequences and creates a .filter and .filter.fasta file.\n");
109 mothurOut("The filter.seqs command parameters are fasta, trump, soft, hard and vertical. \n");
110 mothurOut("The fasta parameter is required.\n");
111 mothurOut("The trump parameter .... The default is ...\n");
112 mothurOut("The soft parameter .... The default is ....\n");
113 mothurOut("The hard parameter .... The default is ....\n");
114 mothurOut("The vertical parameter .... The default is T.\n");
115 mothurOut("The filter.seqs command should be in the following format: \n");
116 mothurOut("filter.seqs(fasta=yourFastaFile, trump=yourTrump, soft=yourSoft, hard=yourHard, vertical=yourVertical) \n");
117 mothurOut("Example filter.seqs(fasta=abrecovery.fasta, trump=..., soft=..., hard=..., vertical=T).\n");
118 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
121 catch(exception& e) {
122 errorOut(e, "FilterSeqsCommand", "help");
127 /**************************************************************************************/
129 int FilterSeqsCommand::execute() {
132 if (abort == true) { return 0; }
135 openInputFile(fastafile, inFASTA);
137 Sequence testSeq(inFASTA);
138 alignmentLength = testSeq.getAlignLength();
141 F.setLength(alignmentLength);
143 if(soft != 0 || isTrue(vertical)){
147 if(hard.compare("") != 0) { F.doHard(hard); }
148 else { F.setFilter(string(alignmentLength, '1')); }
150 if(trump != '*' || isTrue(vertical) || soft != 0){
151 while(!inFASTA.eof()){ //read through and create the filter...
152 Sequence seq(inFASTA);
153 if (seq.getName() != "") {
154 if(trump != '*'){ F.doTrump(seq); }
155 if(isTrue(vertical) || soft != 0){ F.getFreqs(seq); }
163 F.setNumSeqs(numSeqs);
166 if(isTrue(vertical) == 1) { F.doVertical(); }
167 if(soft != 0) { F.doSoft(); }
169 filter = F.getFilter();
172 string filterFile = outputDir + getRootName(getSimpleName(fastafile)) + "filter";
173 openOutputFile(filterFile, outFilter);
174 outFilter << filter << endl;
178 openInputFile(fastafile, inFasta2);
179 string filteredFasta = outputDir + getRootName(getSimpleName(fastafile)) + "filter.fasta";
181 openOutputFile(filteredFasta, outFASTA);
184 while(!inFasta2.eof()){
185 Sequence seq(inFasta2);
186 if (seq.getName() != "") {
187 string align = seq.getAligned();
188 string filterSeq = "";
190 for(int j=0;j<alignmentLength;j++){
191 if(filter[j] == '1'){
192 filterSeq += align[j];
196 outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
205 int filteredLength = 0;
206 for(int i=0;i<alignmentLength;i++){
207 if(filter[i] == '1'){ filteredLength++; }
211 mothurOut("Length of filtered alignment: " + toString(filteredLength)); mothurOutEndLine();
212 mothurOut("Number of columns removed: " + toString((alignmentLength-filteredLength))); mothurOutEndLine();
213 mothurOut("Length of the original alignment: " + toString(alignmentLength)); mothurOutEndLine();
214 mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); mothurOutEndLine();
219 catch(exception& e) {
220 errorOut(e, "FilterSeqsCommand", "execute");
225 /**************************************************************************************/