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