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