]> git.donarmstrong.com Git - mothur.git/blob - filterseqscommand.cpp
68450fb15a7b140803aab2d54d8c5bc84e7ce688
[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(trump != '*'){       F.doTrump(seq); }
125                                 if(isTrue(vertical) || soft != 0){      F.getFreqs(seq);        }
126                                 numSeqs++;
127                                 cout.flush();
128                         }
129                 
130                 }
131                 inFASTA.close();
132                 F.setNumSeqs(numSeqs);
133                 
134                 
135                 if(isTrue(vertical) == 1)       {       F.doVertical(); }
136                 if(soft != 0)                           {       F.doSoft();             }
137                 
138                 filter = F.getFilter();
139
140                 ofstream outFilter;
141                 string filterFile = getRootName(fastafile) + "filter";
142                 openOutputFile(filterFile, outFilter);
143                 outFilter << filter << endl;
144                 outFilter.close();
145                 
146                 ifstream inFasta2;
147                 openInputFile(fastafile, inFasta2);
148                 string filteredFasta = getRootName(fastafile) + "filter.fasta";
149                 ofstream outFASTA;
150                 openOutputFile(filteredFasta, outFASTA);
151
152                 numSeqs = 0;
153                 while(!inFasta2.eof()){
154                         Sequence seq(inFasta2);
155                         string align = seq.getAligned();
156                         string filterSeq = "";
157         
158                         for(int j=0;j<alignmentLength;j++){
159                                 if(filter[j] == '1'){
160                                         filterSeq += align[j];
161                                 }
162                         }
163
164                         outFASTA << '>' << seq.getName() << endl << filterSeq << endl;
165                         numSeqs++;
166                         gobble(inFasta2);
167                 }
168                 outFASTA.close();
169                 inFasta2.close();
170                 
171                 
172                 int filteredLength = 0;
173                 for(int i=0;i<alignmentLength;i++){
174                         if(filter[i] == '1'){   filteredLength++;       }
175                 }
176                 
177                 mothurOutEndLine();
178                 mothurOut("Length of filtered alignment: " + toString(filteredLength)); mothurOutEndLine();
179                 mothurOut("Number of columns removed: " + toString((alignmentLength-filteredLength))); mothurOutEndLine();
180                 mothurOut("Length of the original alignment: " + toString(alignmentLength)); mothurOutEndLine();
181                 mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); mothurOutEndLine();
182                 
183                 return 0;
184                 
185         }
186         catch(exception& e) {
187                 errorOut(e, "FilterSeqsCommand", "execute");
188                 exit(1);
189         }
190 }
191
192 /**************************************************************************************/