]> git.donarmstrong.com Git - mothur.git/blob - filterseqscommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[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", "outputdir","inputdir"};
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                         map<string,string>::iterator it;
32                         
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;  }
36                         }
37                         
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 = "";          }
41                         else {
42                                 string path;
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;            }
49                                 }
50                                 
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;             }
57                                 }
58                         }
59                         
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; }     
64                         
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"){  
67                                 outputDir = ""; 
68                                 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it  
69                         }
70
71                         //check for optional parameter and set defaults
72                         // ...at some point should added some additional type checking...
73                         
74                         string temp;
75                         temp = validParameter.validFile(parameters, "trump", false);                    if (temp == "not found") { temp = "*"; }
76                         trump = temp[0];
77                         
78                         temp = validParameter.validFile(parameters, "soft", false);                             if (temp == "not found") { soft = 0; }
79                         else {  soft = (float)atoi(temp.c_str()) / 100.0;  }
80                         
81                         hard = validParameter.validFile(parameters, "hard", true);                              if (hard == "not found") { hard = ""; }
82                         else if (hard == "not open") { abort = true; }  
83                         
84                         vertical = validParameter.validFile(parameters, "vertical", false);             if (vertical == "not found") { vertical = "T"; }
85                         
86                         numSeqs = 0;
87                         
88                         if (abort == false) {
89                         
90                                 if (soft != 0)                  {  F.setSoft(soft);             }
91                                 if (trump != '*')               {  F.setTrump(trump);   }
92                         
93                         }
94                                                 
95                 }
96                 
97         }
98         catch(exception& e) {
99                 errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
100                 exit(1);
101         }
102 }
103
104 //**********************************************************************************************************************
105
106 void FilterSeqsCommand::help(){
107         try {
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");
119                 
120         }
121         catch(exception& e) {
122                 errorOut(e, "FilterSeqsCommand", "help");
123                 exit(1);
124         }
125 }
126
127 /**************************************************************************************/
128
129 int FilterSeqsCommand::execute() {      
130         try {
131         
132                 if (abort == true) { return 0; }
133                 
134                 ifstream inFASTA;
135                 openInputFile(fastafile, inFASTA);
136                 
137                 Sequence testSeq(inFASTA);
138                 alignmentLength = testSeq.getAlignLength();
139                 inFASTA.seekg(0);
140                 
141                 F.setLength(alignmentLength);
142                 
143                 if(soft != 0 || isTrue(vertical)){
144                         F.initialize();
145                 }
146                 
147                 if(hard.compare("") != 0)       {       F.doHard(hard);         }
148                 else                                            {       F.setFilter(string(alignmentLength, '1'));      }
149
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);        }
156                                         numSeqs++;
157                                         cout.flush();
158                                 }
159                         }
160                 
161                 }
162                 inFASTA.close();
163                 F.setNumSeqs(numSeqs);
164                 
165                 
166                 if(isTrue(vertical) == 1)       {       F.doVertical(); }
167                 if(soft != 0)                           {       F.doSoft();             }
168                 
169                 filter = F.getFilter();
170
171                 ofstream outFilter;
172                 string filterFile = outputDir + getRootName(getSimpleName(fastafile)) + "filter";
173                 openOutputFile(filterFile, outFilter);
174                 outFilter << filter << endl;
175                 outFilter.close();
176                 
177                 ifstream inFasta2;
178                 openInputFile(fastafile, inFasta2);
179                 string filteredFasta = outputDir + getRootName(getSimpleName(fastafile)) + "filter.fasta";
180                 ofstream outFASTA;
181                 openOutputFile(filteredFasta, outFASTA);
182
183                 numSeqs = 0;
184                 while(!inFasta2.eof()){
185                         Sequence seq(inFasta2);
186                         if (seq.getName() != "") {
187                                 string align = seq.getAligned();
188                                 string filterSeq = "";
189                                 
190                                 for(int j=0;j<alignmentLength;j++){
191                                         if(filter[j] == '1'){
192                                                 filterSeq += align[j];
193                                         }
194                                 }
195                                 
196                                 outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
197                                 numSeqs++;
198                         }
199                         gobble(inFasta2);
200                 }
201                 outFASTA.close();
202                 inFasta2.close();
203                 
204                 
205                 int filteredLength = 0;
206                 for(int i=0;i<alignmentLength;i++){
207                         if(filter[i] == '1'){   filteredLength++;       }
208                 }
209                 
210                 mothurOutEndLine();
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();
215                 
216                 return 0;
217                 
218         }
219         catch(exception& e) {
220                 errorOut(e, "FilterSeqsCommand", "execute");
221                 exit(1);
222         }
223 }
224
225 /**************************************************************************************/