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) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta", "trump", "soft", "hard", "vertical", "outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the input directory command factory will send this info to us in the output parameter
40 string inputDir = validParameter.validFile(parameters, "inputdir", false);
41 if (inputDir == "not found"){ inputDir = ""; }
44 it = parameters.find("fasta");
45 //user has given a template file
46 if(it != parameters.end()){
47 path = hasPath(it->second);
48 //if the user has not given a path then, add inputdir. else leave path alone.
49 if (path == "") { parameters["fasta"] = inputDir + it->second; }
52 it = parameters.find("hard");
53 //user has given a template file
54 if(it != parameters.end()){
55 path = hasPath(it->second);
56 //if the user has not given a path then, add inputdir. else leave path alone.
57 if (path == "") { parameters["hard"] = inputDir + it->second; }
61 //check for required parameters
62 fasta = validParameter.validFile(parameters, "fasta", false);
63 if (fasta == "not found") { m->mothurOut("fasta is a required parameter for the filter.seqs command."); m->mothurOutEndLine(); abort = true; }
65 splitAtDash(fasta, fastafileNames);
67 //go through files and make sure they are good, if not, then disregard them
68 for (int i = 0; i < fastafileNames.size(); i++) {
70 string path = hasPath(fastafileNames[i]);
71 //if the user has not given a path then, add inputdir. else leave path alone.
72 if (path == "") { fastafileNames[i] = inputDir + fastafileNames[i]; }
77 ableToOpen = openInputFile(fastafileNames[i], in);
78 if (ableToOpen == 1) {
79 m->mothurOut(fastafileNames[i] + " will be disregarded."); m->mothurOutEndLine();
80 //erase from file list
81 fastafileNames.erase(fastafileNames.begin()+i);
84 string simpleName = getSimpleName(fastafileNames[i]);
85 filterFileName += simpleName.substr(0, simpleName.find_first_of('.'));
90 //make sure there is at least one valid file left
91 if (fastafileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
95 //if the user changes the output directory command factory will send this info to us in the output parameter
96 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
98 outputDir += hasPath(fastafileNames[0]); //if user entered a file with a path then preserve it
101 //check for optional parameter and set defaults
102 // ...at some point should added some additional type checking...
105 temp = validParameter.validFile(parameters, "trump", false); if (temp == "not found") { temp = "*"; }
108 temp = validParameter.validFile(parameters, "soft", false); if (temp == "not found") { soft = 0; }
109 else { soft = (float)atoi(temp.c_str()) / 100.0; }
111 hard = validParameter.validFile(parameters, "hard", true); if (hard == "not found") { hard = ""; }
112 else if (hard == "not open") { abort = true; }
114 vertical = validParameter.validFile(parameters, "vertical", false); if (vertical == "not found") { vertical = "T"; }
118 if (abort == false) {
120 if (soft != 0) { F.setSoft(soft); }
121 if (trump != '*') { F.setTrump(trump); }
128 catch(exception& e) {
129 m->errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
134 //**********************************************************************************************************************
136 void FilterSeqsCommand::help(){
138 m->mothurOut("The filter.seqs command reads a file containing sequences and creates a .filter and .filter.fasta file.\n");
139 m->mothurOut("The filter.seqs command parameters are fasta, trump, soft, hard and vertical. \n");
140 m->mothurOut("The fasta parameter is required. You may enter several fasta files to build the filter from and filter, by separating their names with -'s.\n");
141 m->mothurOut("For example: fasta=abrecovery.fasta-amazon.fasta \n");
142 m->mothurOut("The trump parameter .... The default is ...\n");
143 m->mothurOut("The soft parameter .... The default is ....\n");
144 m->mothurOut("The hard parameter .... The default is ....\n");
145 m->mothurOut("The vertical parameter .... The default is T.\n");
146 m->mothurOut("The filter.seqs command should be in the following format: \n");
147 m->mothurOut("filter.seqs(fasta=yourFastaFile, trump=yourTrump, soft=yourSoft, hard=yourHard, vertical=yourVertical) \n");
148 m->mothurOut("Example filter.seqs(fasta=abrecovery.fasta, trump=..., soft=..., hard=..., vertical=T).\n");
149 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
152 catch(exception& e) {
153 m->errorOut(e, "FilterSeqsCommand", "help");
158 /**************************************************************************************/
160 int FilterSeqsCommand::execute() {
163 if (abort == true) { return 0; }
164 vector<string> outputNames;
167 openInputFile(fastafileNames[0], inFASTA);
169 Sequence testSeq(inFASTA);
170 alignmentLength = testSeq.getAlignLength();
173 F.setLength(alignmentLength);
175 if(soft != 0 || isTrue(vertical)){
179 if(hard.compare("") != 0) { F.doHard(hard); }
180 else { F.setFilter(string(alignmentLength, '1')); }
182 if(trump != '*' || isTrue(vertical) || soft != 0){
183 for (int i = 0; i < fastafileNames.size(); i++) {
185 openInputFile(fastafileNames[i], in);
187 while(!in.eof()){ //read through and create the filter...
189 if (m->control_pressed) { in.close(); return 0; }
192 if (seq.getName() != "") {
193 if(trump != '*'){ F.doTrump(seq); }
194 if(isTrue(vertical) || soft != 0){ F.getFreqs(seq); }
203 F.setNumSeqs(numSeqs);
206 if(isTrue(vertical) == 1) { F.doVertical(); }
207 if(soft != 0) { F.doSoft(); }
209 filter = F.getFilter();
213 string filterFile = outputDir + filterFileName + ".filter";
214 openOutputFile(filterFile, outFilter);
215 outFilter << filter << endl;
217 outputNames.push_back(filterFile);
220 for (int i = 0; i < fastafileNames.size(); i++) {
222 openInputFile(fastafileNames[i], in);
223 string filteredFasta = outputDir + getRootName(getSimpleName(fastafileNames[i])) + "filter.fasta";
225 openOutputFile(filteredFasta, outFASTA);
226 outputNames.push_back(filteredFasta);
230 if (m->control_pressed) { in.close(); outFASTA.close(); for(int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
233 if (seq.getName() != "") {
234 string align = seq.getAligned();
235 string filterSeq = "";
237 for(int j=0;j<alignmentLength;j++){
238 if(filter[j] == '1'){
239 filterSeq += align[j];
243 outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
252 int filteredLength = 0;
253 for(int i=0;i<alignmentLength;i++){
254 if(filter[i] == '1'){ filteredLength++; }
257 if (m->control_pressed) { for(int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
260 m->mothurOutEndLine();
261 m->mothurOut("Length of filtered alignment: " + toString(filteredLength)); m->mothurOutEndLine();
262 m->mothurOut("Number of columns removed: " + toString((alignmentLength-filteredLength))); m->mothurOutEndLine();
263 m->mothurOut("Length of the original alignment: " + toString(alignmentLength)); m->mothurOutEndLine();
264 m->mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); m->mothurOutEndLine();
267 m->mothurOutEndLine();
268 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
269 for(int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
270 m->mothurOutEndLine();
275 catch(exception& e) {
276 m->errorOut(e, "FilterSeqsCommand", "execute");
281 /**************************************************************************************/