]> git.donarmstrong.com Git - mothur.git/blob - deuniqueseqscommand.cpp
added set.current and get.current commands and modified existing commands to update...
[mothur.git] / deuniqueseqscommand.cpp
1 /*
2  *  deuniqueseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/19/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "deuniqueseqscommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14 vector<string> DeUniqueSeqsCommand::getValidParameters(){       
15         try {
16                 string Array[] =  {"fasta", "name","outputdir","inputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "DeUniqueSeqsCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 DeUniqueSeqsCommand::DeUniqueSeqsCommand(){     
27         try {
28                 abort = true; calledHelp = true; 
29                 vector<string> tempOutNames;
30                 outputTypes["fasta"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "DeUniqueSeqsCommand", "DeconvoluteCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> DeUniqueSeqsCommand::getRequiredParameters(){    
39         try {
40                 string Array[] =  {"fasta","name"};
41                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42                 return myArray;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "DeUniqueSeqsCommand", "getRequiredParameters");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 vector<string> DeUniqueSeqsCommand::getRequiredFiles(){ 
51         try {
52                 vector<string> myArray;
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "DeUniqueSeqsCommand", "getRequiredFiles");
57                 exit(1);
58         }
59 }
60 /**************************************************************************************/
61 DeUniqueSeqsCommand::DeUniqueSeqsCommand(string option)  {      
62         try {
63                 abort = false; calledHelp = false;   
64                 
65                 //allow user to run help
66                 if(option == "help") { help(); abort = true; calledHelp = true; }
67                 
68                 else {
69                         //valid paramters for this command
70                         string Array[] =  {"fasta", "name","outputdir","inputdir"};
71                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
72                         
73                         OptionParser parser(option);
74                         map<string,string> parameters = parser.getParameters();
75                         
76                         ValidParameters validParameter;
77                         map<string, string>::iterator it;
78                 
79                         //check to make sure all parameters are valid for command
80                         for (it = parameters.begin(); it != parameters.end(); it++) { 
81                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
82                         }
83                         
84                         //initialize outputTypes
85                         vector<string> tempOutNames;
86                         outputTypes["fasta"] = tempOutNames;
87                 
88                         //if the user changes the input directory command factory will send this info to us in the output parameter 
89                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
90                         if (inputDir == "not found"){   inputDir = "";          }
91                         else {
92                                 string path;
93                                 it = parameters.find("fasta");
94                                 //user has given a template file
95                                 if(it != parameters.end()){ 
96                                         path = m->hasPath(it->second);
97                                         //if the user has not given a path then, add inputdir. else leave path alone.
98                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
99                                 }
100                                 
101                                 it = parameters.find("name");
102                                 //user has given a template file
103                                 if(it != parameters.end()){ 
104                                         path = m->hasPath(it->second);
105                                         //if the user has not given a path then, add inputdir. else leave path alone.
106                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
107                                 }
108                         }
109
110                         
111                         //check for required parameters
112                         fastaFile = validParameter.validFile(parameters, "fasta", true);
113                         if (fastaFile == "not open") { abort = true; }
114                         else if (fastaFile == "not found") { fastaFile = ""; m->mothurOut("fasta is a required parameter for the deunique.seqs command."); m->mothurOutEndLine(); abort = true;  }      
115                         
116                         //if the user changes the output directory command factory will send this info to us in the output parameter 
117                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
118                                 outputDir = ""; 
119                                 outputDir += m->hasPath(fastaFile); //if user entered a file with a path then preserve it       
120                         }
121                         
122                         nameFile = validParameter.validFile(parameters, "name", true);
123                         if (nameFile == "not open") { abort = true; }
124                         else if (nameFile == "not found"){      nameFile = "";  m->mothurOut("name is a required parameter for the deunique.seqs command."); m->mothurOutEndLine(); abort = true;  }
125                 }
126
127         }
128         catch(exception& e) {
129                 m->errorOut(e, "DeUniqueSeqsCommand", "DeUniqueSeqsCommand");
130                 exit(1);
131         }
132 }
133 //**********************************************************************************************************************
134
135 void DeUniqueSeqsCommand::help(){
136         try {
137                 m->mothurOut("The deunique.seqs command reads a fastafile and namefile, and creates a fastafile containing all the sequences.\n");
138                 m->mothurOut("The deunique.seqs command parameters are fasta and name, both are required.\n");
139                 m->mothurOut("The deunique.seqs command should be in the following format: \n");
140                 m->mothurOut("deunique.seqs(fasta=yourFastaFile, name=yourNameFile) \n");       
141                 m->mothurOut("Example deunique.seqs(fasta=abrecovery.unique.fasta, name=abrecovery.names).\n");
142                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
143
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "DeUniqueSeqsCommand", "help");
147                 exit(1);
148         }
149 }
150
151 /**************************************************************************************/
152 int DeUniqueSeqsCommand::execute() {    
153         try {
154                 
155                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
156
157                 //prepare filenames and open files
158                 ofstream out;
159                 string outFastaFile = m->getRootName(m->getSimpleName(fastaFile));
160                 int pos = outFastaFile.find("unique");
161                 if (pos != string::npos) {
162                         outFastaFile = outputDir + outFastaFile.substr(0, pos) + "redundant" + m->getExtension(fastaFile);
163                 }else{
164                         outFastaFile = outputDir + m->getRootName(m->getSimpleName(fastaFile)) + "redundant" + m->getExtension(fastaFile);
165                 }
166                 m->openOutputFile(outFastaFile, out);
167                 
168                 readNamesFile();
169                 if (m->control_pressed) {  out.close(); outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
170                 
171                 ifstream in;
172                 m->openInputFile(fastaFile, in);
173                 
174                 while (!in.eof()) {
175                 
176                         if (m->control_pressed) { in.close(); out.close(); outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
177                         
178                         Sequence seq(in); m->gobble(in);
179                         
180                         if (seq.getName() != "") {
181                                 
182                                 //look for sequence name in nameMap
183                                 map<string, string>::iterator it = nameMap.find(seq.getName());
184                                 
185                                 if (it == nameMap.end()) {      m->mothurOut("[ERROR]: Your namefile does not contain " + seq.getName() + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
186                                 else {
187                                         vector<string> names;
188                                         m->splitAtComma(it->second, names);
189                                         
190                                         //output sequences
191                                         for (int i = 0; i < names.size(); i++) {
192                                                 out << ">" << names[i] << endl;
193                                                 out << seq.getAligned() << endl;
194                                         }
195                                         
196                                         //remove seq from name map so we can check for seqs in namefile not in fastafile later
197                                         nameMap.erase(it);
198                                 }
199                         }
200                 }
201                 in.close();
202                 out.close(); 
203                 
204                 if (nameMap.size() != 0) { //then there are names in the namefile not in the fastafile
205                         for (map<string, string>::iterator it = nameMap.begin(); it != nameMap.end(); it++) {  
206                                 m->mothurOut(it->first + " is not in your fasta file, but is in your name file. Please correct."); m->mothurOutEndLine();
207                         }
208                 }
209                                 
210                 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
211                 
212                 m->mothurOutEndLine();
213                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
214                 m->mothurOut(outFastaFile); m->mothurOutEndLine();      
215                 outputNames.push_back(outFastaFile);  outputTypes["fasta"].push_back(outFastaFile);  
216                 m->mothurOutEndLine();
217                 
218                 //set fasta file as new current fastafile
219                 string current = "";
220                 itTypes = outputTypes.find("fasta");
221                 if (itTypes != outputTypes.end()) {
222                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
223                 }
224                 
225                 return 0;
226         }
227         catch(exception& e) {
228                 m->errorOut(e, "DeUniqueSeqsCommand", "execute");
229                 exit(1);
230         }
231 }
232 //**********************************************************************************************************************
233 int DeUniqueSeqsCommand::readNamesFile() {
234         try {
235                 
236                 ifstream inNames;
237                 m->openInputFile(nameFile, inNames);
238                 
239                 string name, names;
240                 map<string, string>::iterator it;
241         
242                 while(inNames){
243                         
244                         if(m->control_pressed) { break; }
245                         
246                         inNames >> name;        m->gobble(inNames);             
247                         inNames >> names;               
248                         
249                         it = nameMap.find(name);
250                         
251                         if (it == nameMap.end()) {      nameMap[name] = names; }
252                         else { m->mothurOut("[ERROR]: Your namefile already contains " + name + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
253                                         
254                         m->gobble(inNames);
255                 }
256                 inNames.close();
257                 
258                 return 0;
259
260         }
261         catch(exception& e) {
262                 m->errorOut(e, "DeUniqueSeqsCommand", "readNamesFile");
263                 exit(1);
264         }
265 }
266
267 /**************************************************************************************/