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