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