]> git.donarmstrong.com Git - mothur.git/blob - deconvolutecommand.cpp
fixed bug in phylo.diversity rooting. added filename patterns and create filename...
[mothur.git] / deconvolutecommand.cpp
1 /*
2  *  deconvolute.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/21/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "deconvolutecommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14 vector<string> DeconvoluteCommand::setParameters(){     
15         try {
16                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta-name",false,true,true); parameters.push_back(pfasta);
17                 CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","name",false,false,true); parameters.push_back(pname);
18         CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","count",false,false,true); parameters.push_back(pcount);
19         CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
20                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "DeconvoluteCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string DeconvoluteCommand::getHelpString(){     
33         try {
34                 string helpString = "";
35                 helpString += "The unique.seqs command reads a fastafile and creates a name or count file.\n";
36                 helpString += "It creates a file where the first column is the groupname and the second column is a list of sequence names who have the same sequence. \n";
37                 helpString += "If the sequence is unique the second column will just contain its name. \n";
38                 helpString += "The unique.seqs command parameters are fasta and name.  fasta is required, unless there is a valid current fasta file.\n";
39                 helpString += "The unique.seqs command should be in the following format: \n";
40                 helpString += "unique.seqs(fasta=yourFastaFile) \n";    
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "DeconvoluteCommand", "getHelpString");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 string DeconvoluteCommand::getOutputPattern(string type) {
50     try {
51         string pattern = "";
52         
53         if (type == "fasta") {  pattern = "[filename],unique,[extension]"; } 
54         else if (type == "name") {  pattern = "[filename],names-[filename],[tag],names"; } 
55         else if (type == "count") {  pattern = "[filename],count_table-[filename],[tag],count_table"; }
56         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
57         
58         return pattern;
59     }
60     catch(exception& e) {
61         m->errorOut(e, "DeconvoluteCommand", "getOutputPattern");
62         exit(1);
63     }
64 }
65
66 //**********************************************************************************************************************
67 DeconvoluteCommand::DeconvoluteCommand(){       
68         try {
69                 abort = true; calledHelp = true; 
70                 setParameters();
71                 vector<string> tempOutNames;
72                 outputTypes["fasta"] = tempOutNames;
73                 outputTypes["name"] = tempOutNames;
74         outputTypes["count"] = tempOutNames;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
78                 exit(1);
79         }
80 }
81 /**************************************************************************************/
82 DeconvoluteCommand::DeconvoluteCommand(string option)  {        
83         try {
84                 abort = false; calledHelp = false;   
85                 
86                 //allow user to run help
87                 if(option == "help") { help(); abort = true; calledHelp = true; }
88                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
89                 
90                 else {
91                         vector<string> myArray = setParameters();
92                         
93                         OptionParser parser(option);
94                         map<string,string> parameters = parser.getParameters();
95                         
96                         ValidParameters validParameter;
97                         map<string, string>::iterator it;
98                 
99                         //check to make sure all parameters are valid for command
100                         for (it = parameters.begin(); it != parameters.end(); it++) { 
101                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
102                         }
103                         
104                         //initialize outputTypes
105                         vector<string> tempOutNames;
106                         outputTypes["fasta"] = tempOutNames;
107                         outputTypes["name"] = tempOutNames;
108             outputTypes["count"] = tempOutNames;
109                 
110                         //if the user changes the input directory command factory will send this info to us in the output parameter 
111                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
112                         if (inputDir == "not found"){   inputDir = "";          }
113                         else {
114                                 string path;
115                                 it = parameters.find("fasta");
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["fasta"] = inputDir + it->second;            }
121                                 }
122                                 
123                                 it = parameters.find("name");
124                                 //user has given a template file
125                                 if(it != parameters.end()){ 
126                                         path = m->hasPath(it->second);
127                                         //if the user has not given a path then, add inputdir. else leave path alone.
128                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
129                                 }
130                 
131                 it = parameters.find("count");
132                                 //user has given a template file
133                                 if(it != parameters.end()){ 
134                                         path = m->hasPath(it->second);
135                                         //if the user has not given a path then, add inputdir. else leave path alone.
136                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
137                                 }
138                         }
139
140                         
141                         //check for required parameters
142                         inFastaName = validParameter.validFile(parameters, "fasta", true);
143                         if (inFastaName == "not open") { abort = true; }
144                         else if (inFastaName == "not found") {                          
145                                 inFastaName = m->getFastaFile(); 
146                                 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
147                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
148                         }else { m->setFastaFile(inFastaName); } 
149                         
150                         //if the user changes the output directory command factory will send this info to us in the output parameter 
151                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
152                                 outputDir = ""; 
153                                 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it     
154                         }
155                         
156                         oldNameMapFName = validParameter.validFile(parameters, "name", true);
157                         if (oldNameMapFName == "not open") { oldNameMapFName = ""; abort = true; }
158                         else if (oldNameMapFName == "not found"){       oldNameMapFName = "";   }
159                         else { m->setNameFile(oldNameMapFName); }
160             
161             countfile = validParameter.validFile(parameters, "count", true);
162                         if (countfile == "not open") { abort = true; countfile = ""; }  
163                         else if (countfile == "not found") { countfile = ""; }
164                         else { m->setCountTableFile(countfile); }
165                         
166             if ((countfile != "") && (oldNameMapFName != "")) { m->mothurOut("When executing a unique.seqs command you must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
167                         
168
169                         if (countfile == "") {
170                 if (oldNameMapFName == "") {
171                     vector<string> files; files.push_back(inFastaName);
172                     parser.getNameFile(files);
173                 }
174             }
175                         
176                 }
177
178         }
179         catch(exception& e) {
180                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
181                 exit(1);
182         }
183 }
184 /**************************************************************************************/
185 int DeconvoluteCommand::execute() {     
186         try {
187                 
188                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
189
190                 //prepare filenames and open files
191         map<string, string> variables; 
192         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
193         string outNameFile = getOutputFileName("name", variables);
194         string outCountFile = getOutputFileName("count", variables);
195         variables["[extension]"] = m->getExtension(inFastaName);
196                 string outFastaFile = getOutputFileName("fasta", variables);
197                 
198                 map<string, string> nameMap;
199                 map<string, string>::iterator itNames;
200                 if (oldNameMapFName != "")  {  
201             m->readNames(oldNameMapFName, nameMap); 
202             if (oldNameMapFName == outNameFile){ 
203                 variables["[tag]"] = "unique";
204                 outNameFile = getOutputFileName("name", variables);   }
205         }
206         CountTable ct;
207         if (countfile != "")  {  
208             ct.readTable(countfile);
209             if (countfile == outCountFile){ 
210                 variables["[tag]"] = "unique";
211                 outCountFile = getOutputFileName("count", variables);   }
212         }
213                 
214                 if (m->control_pressed) { return 0; }
215                 
216                 ifstream in; 
217                 m->openInputFile(inFastaName, in);
218                 
219                 ofstream outFasta;
220                 m->openOutputFile(outFastaFile, outFasta);
221                 
222                 map<string, string> sequenceStrings; //sequenceString -> list of names.  "atgc...." -> seq1,seq2,seq3.
223                 map<string, string>::iterator itStrings;
224                 set<string> nameInFastaFile; //for sanity checking
225                 set<string>::iterator itname;
226                 vector<string> nameFileOrder;
227                 int count = 0;
228                 while (!in.eof()) {
229                         
230                         if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
231                         
232                         Sequence seq(in);
233                         
234                         if (seq.getName() != "") {
235                                 
236                                 //sanity checks
237                                 itname = nameInFastaFile.find(seq.getName());
238                                 if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName());  }
239                                 else { m->mothurOut("[ERROR]: You already have a sequence named " + seq.getName() + " in your fasta file, sequence names must be unique, please correct."); m->mothurOutEndLine(); }
240
241                                 itStrings = sequenceStrings.find(seq.getAligned());
242                                 
243                                 if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
244                                         //output to unique fasta file
245                                         seq.printSequence(outFasta);
246                                         
247                                         if (oldNameMapFName != "") {
248                                                 itNames = nameMap.find(seq.getName());
249                                                 
250                                                 if (itNames == nameMap.end()) { //namefile and fastafile do not match
251                                                         m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
252                                                 }else {
253                                                         sequenceStrings[seq.getAligned()] = itNames->second;
254                                                         nameFileOrder.push_back(seq.getAligned());
255                                                 }
256                                         }else if (countfile != "") { 
257                         ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
258                         sequenceStrings[seq.getAligned()] = seq.getName();      nameFileOrder.push_back(seq.getAligned());
259                     }else {     sequenceStrings[seq.getAligned()] = seq.getName();      nameFileOrder.push_back(seq.getAligned()); }
260                                 }else { //this is a dup
261                                         if (oldNameMapFName != "") {
262                                                 itNames = nameMap.find(seq.getName());
263                                                 
264                                                 if (itNames == nameMap.end()) { //namefile and fastafile do not match
265                                                         m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
266                                                 }else {
267                                                         sequenceStrings[seq.getAligned()] += "," + itNames->second;
268                                                 }
269                     }else if (countfile != "") { 
270                         int num = ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
271                         if (num != 0) { //its in the table
272                             ct.mergeCounts(itStrings->second, seq.getName()); //merges counts and saves in uniques name
273                         }
274                     }else {     sequenceStrings[seq.getAligned()] += "," + seq.getName();       }
275                                 }
276                                 
277                                 count++;
278                         }
279                         
280                         m->gobble(in);
281                         
282                         if(count % 1000 == 0)   { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine();       }
283                 }
284                 
285                 if(count % 1000 != 0)   { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine();       }
286                 
287                 in.close();
288                 outFasta.close();
289                 
290                 if (m->control_pressed) { m->mothurRemove(outFastaFile); return 0; }
291                 
292                 //print new names file
293                 ofstream outNames;
294                 if (countfile == "") { m->openOutputFile(outNameFile, outNames); outputNames.push_back(outNameFile); outputTypes["name"].push_back(outNameFile);  }
295         else { m->openOutputFile(outCountFile, outNames); ct.printHeaders(outNames); outputTypes["count"].push_back(outCountFile); outputNames.push_back(outCountFile); }
296                 
297                 for (int i = 0; i < nameFileOrder.size(); i++) {
298                         if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); outNames.close(); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
299                         
300                         itStrings = sequenceStrings.find(nameFileOrder[i]);
301                         
302                         if (itStrings != sequenceStrings.end()) {
303                 if (countfile == "") {
304                     //get rep name
305                     int pos = (itStrings->second).find_first_of(',');
306                     
307                     if (pos == string::npos) { // only reps itself
308                         outNames << itStrings->second << '\t' << itStrings->second << endl;
309                     }else {
310                         outNames << (itStrings->second).substr(0, pos) << '\t' << itStrings->second << endl;
311                     }
312                 }else {  ct.printSeq(outNames, itStrings->second);  }
313                         }else{ m->mothurOut("[ERROR]: mismatch in namefile print."); m->mothurOutEndLine(); m->control_pressed = true; }
314                 }
315                 outNames.close();
316                 
317                 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); }  return 0; }
318                 
319                 m->mothurOutEndLine();
320                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
321                 outputNames.push_back(outFastaFile);   outputTypes["fasta"].push_back(outFastaFile);  
322         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
323                 m->mothurOutEndLine();
324
325                 //set fasta file as new current fastafile
326                 string current = "";
327                 itTypes = outputTypes.find("fasta");
328                 if (itTypes != outputTypes.end()) {
329                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
330                 }
331                 
332                 itTypes = outputTypes.find("name");
333                 if (itTypes != outputTypes.end()) {
334                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
335                 }
336         
337         itTypes = outputTypes.find("count");
338                 if (itTypes != outputTypes.end()) {
339                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
340                 }
341                 
342                 return 0;
343         }
344         catch(exception& e) {
345                 m->errorOut(e, "DeconvoluteCommand", "execute");
346                 exit(1);
347         }
348 }
349 /**************************************************************************************/