]> git.donarmstrong.com Git - mothur.git/blob - filterseqscommand.cpp
modified sequence class to read fasta files with comments. this required modification...
[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"};
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                         
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;  }
35                         }
36                         
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; }     
41
42                         //check for optional parameter and set defaults
43                         // ...at some point should added some additional type checking...
44                         
45                         string temp;
46                         temp = validParameter.validFile(parameters, "trump", false);                    if (temp == "not found") { temp = "*"; }
47                         trump = temp[0];
48                         
49                         temp = validParameter.validFile(parameters, "soft", false);                             if (temp == "not found") { soft = 0; }
50                         else {  soft = (float)atoi(temp.c_str()) / 100.0;  }
51                         
52                         hard = validParameter.validFile(parameters, "hard", true);                              if (hard == "not found") { hard = ""; }
53                         else if (hard == "not open") { abort = true; }  
54                         
55                         vertical = validParameter.validFile(parameters, "vertical", false);             if (vertical == "not found") { vertical = "T"; }
56                         
57                         numSeqs = 0;
58                         
59                         if (abort == false) {
60                         
61                                 if (soft != 0)                  {  F.setSoft(soft);             }
62                                 if (trump != '*')               {  F.setTrump(trump);   }
63                         
64                         }
65                                                 
66                 }
67                 
68         }
69         catch(exception& e) {
70                 errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
71                 exit(1);
72         }
73 }
74
75 //**********************************************************************************************************************
76
77 void FilterSeqsCommand::help(){
78         try {
79                 mothurOut("The filter.seqs command reads a file containing sequences and creates a .filter and .filter.fasta file.\n");
80                 mothurOut("The filter.seqs command parameters are fasta, trump, soft, hard and vertical. \n");
81                 mothurOut("The fasta parameter is required.\n");
82                 mothurOut("The trump parameter .... The default is ...\n");
83                 mothurOut("The soft parameter .... The default is ....\n");
84                 mothurOut("The hard parameter .... The default is ....\n");
85                 mothurOut("The vertical parameter .... The default is T.\n");
86                 mothurOut("The filter.seqs command should be in the following format: \n");
87                 mothurOut("filter.seqs(fasta=yourFastaFile, trump=yourTrump, soft=yourSoft, hard=yourHard, vertical=yourVertical) \n");
88                 mothurOut("Example filter.seqs(fasta=abrecovery.fasta, trump=..., soft=..., hard=..., vertical=T).\n");
89                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
90                 
91         }
92         catch(exception& e) {
93                 errorOut(e, "FilterSeqsCommand", "help");
94                 exit(1);
95         }
96 }
97
98 /**************************************************************************************/
99
100 int FilterSeqsCommand::execute() {      
101         try {
102         
103                 if (abort == true) { return 0; }
104                 
105                 ifstream inFASTA;
106                 openInputFile(fastafile, inFASTA);
107                 
108                 Sequence testSeq(inFASTA);
109                 alignmentLength = testSeq.getAlignLength();
110                 inFASTA.seekg(0);
111                 
112                 F.setLength(alignmentLength);
113                 
114                 if(soft != 0 || isTrue(vertical)){
115                         F.initialize();
116                 }
117                 
118                 if(hard.compare("") != 0)       {       F.doHard(hard);         }
119                 else                                            {       F.setFilter(string(alignmentLength, '1'));      }
120
121                 if(trump != '*' || isTrue(vertical) || soft != 0){
122                         while(!inFASTA.eof()){  //read through and create the filter...
123                                 Sequence seq(inFASTA);
124                                 if (seq.getName() != "") {
125                                         if(trump != '*'){       F.doTrump(seq); }
126                                         if(isTrue(vertical) || soft != 0){      F.getFreqs(seq);        }
127                                         numSeqs++;
128                                         cout.flush();
129                                 }
130                         }
131                 
132                 }
133                 inFASTA.close();
134                 F.setNumSeqs(numSeqs);
135                 
136                 
137                 if(isTrue(vertical) == 1)       {       F.doVertical(); }
138                 if(soft != 0)                           {       F.doSoft();             }
139                 
140                 filter = F.getFilter();
141
142                 ofstream outFilter;
143                 string filterFile = getRootName(fastafile) + "filter";
144                 openOutputFile(filterFile, outFilter);
145                 outFilter << filter << endl;
146                 outFilter.close();
147                 
148                 ifstream inFasta2;
149                 openInputFile(fastafile, inFasta2);
150                 string filteredFasta = getRootName(fastafile) + "filter.fasta";
151                 ofstream outFASTA;
152                 openOutputFile(filteredFasta, outFASTA);
153
154                 numSeqs = 0;
155                 while(!inFasta2.eof()){
156                         Sequence seq(inFasta2);
157                         if (seq.getName() != "") {
158                                 string align = seq.getAligned();
159                                 string filterSeq = "";
160                                 
161                                 for(int j=0;j<alignmentLength;j++){
162                                         if(filter[j] == '1'){
163                                                 filterSeq += align[j];
164                                         }
165                                 }
166                                 
167                                 outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
168                                 numSeqs++;
169                         }
170                         gobble(inFasta2);
171                 }
172                 outFASTA.close();
173                 inFasta2.close();
174                 
175                 
176                 int filteredLength = 0;
177                 for(int i=0;i<alignmentLength;i++){
178                         if(filter[i] == '1'){   filteredLength++;       }
179                 }
180                 
181                 mothurOutEndLine();
182                 mothurOut("Length of filtered alignment: " + toString(filteredLength)); mothurOutEndLine();
183                 mothurOut("Number of columns removed: " + toString((alignmentLength-filteredLength))); mothurOutEndLine();
184                 mothurOut("Length of the original alignment: " + toString(alignmentLength)); mothurOutEndLine();
185                 mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); mothurOutEndLine();
186                 
187                 return 0;
188                 
189         }
190         catch(exception& e) {
191                 errorOut(e, "FilterSeqsCommand", "execute");
192                 exit(1);
193         }
194 }
195
196 /**************************************************************************************/