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