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