]> git.donarmstrong.com Git - mothur.git/blob - deconvolutecommand.cpp
changes while testing
[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                 //prepare filenames and open files
204                 map<string, string> mvariables;
205                 mvariables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
206                 mvariables["[tag]"] = "unique";
207                 outNameFile = getOutputFileName("name", mvariables);
208             }
209         }
210         CountTable ct;
211         if (countfile != "")  {  
212             ct.readTable(countfile, true);
213             if (countfile == outCountFile){
214                 //prepare filenames and open files
215                 map<string, string> mvariables;
216                 mvariables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
217                 mvariables["[tag]"] = "unique";
218                 outCountFile = getOutputFileName("count", mvariables);   }
219         }
220                 
221                 if (m->control_pressed) { return 0; }
222                 
223                 ifstream in; 
224                 m->openInputFile(inFastaName, in);
225                 
226                 ofstream outFasta;
227                 m->openOutputFile(outFastaFile, outFasta);
228                 
229                 map<string, string> sequenceStrings; //sequenceString -> list of names.  "atgc...." -> seq1,seq2,seq3.
230                 map<string, string>::iterator itStrings;
231                 set<string> nameInFastaFile; //for sanity checking
232                 set<string>::iterator itname;
233                 vector<string> nameFileOrder;
234                 int count = 0;
235                 while (!in.eof()) {
236                         
237                         if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
238                         
239                         Sequence seq(in);
240                         
241                         if (seq.getName() != "") {
242                                 
243                                 //sanity checks
244                                 itname = nameInFastaFile.find(seq.getName());
245                                 if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName());  }
246                                 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(); }
247
248                                 itStrings = sequenceStrings.find(seq.getAligned());
249                                 
250                                 if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
251                                         //output to unique fasta file
252                                         seq.printSequence(outFasta);
253                                         
254                                         if (oldNameMapFName != "") {
255                                                 itNames = nameMap.find(seq.getName());
256                                                 
257                                                 if (itNames == nameMap.end()) { //namefile and fastafile do not match
258                                                         m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
259                                                 }else {
260                                                         sequenceStrings[seq.getAligned()] = itNames->second;
261                                                         nameFileOrder.push_back(seq.getAligned());
262                                                 }
263                                         }else if (countfile != "") { 
264                         ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
265                         sequenceStrings[seq.getAligned()] = seq.getName();      nameFileOrder.push_back(seq.getAligned());
266                     }else {     sequenceStrings[seq.getAligned()] = seq.getName();      nameFileOrder.push_back(seq.getAligned()); }
267                                 }else { //this is a dup
268                                         if (oldNameMapFName != "") {
269                                                 itNames = nameMap.find(seq.getName());
270                                                 
271                                                 if (itNames == nameMap.end()) { //namefile and fastafile do not match
272                                                         m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
273                                                 }else {
274                                                         sequenceStrings[seq.getAligned()] += "," + itNames->second;
275                                                 }
276                     }else if (countfile != "") { 
277                         int num = ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
278                         if (num != 0) { //its in the table
279                             ct.mergeCounts(itStrings->second, seq.getName()); //merges counts and saves in uniques name
280                         }
281                     }else {     sequenceStrings[seq.getAligned()] += "," + seq.getName();       }
282                                 }
283                                 
284                                 count++;
285                         }
286                         
287                         m->gobble(in);
288                         
289                         if(count % 1000 == 0)   { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine();       }
290                 }
291                 
292                 if(count % 1000 != 0)   { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine();       }
293                 
294                 in.close();
295                 outFasta.close();
296                 
297                 if (m->control_pressed) { m->mothurRemove(outFastaFile); return 0; }
298                 
299                 //print new names file
300                 ofstream outNames;
301                 if (countfile == "") { m->openOutputFile(outNameFile, outNames); outputNames.push_back(outNameFile); outputTypes["name"].push_back(outNameFile);  }
302         else { m->openOutputFile(outCountFile, outNames); ct.printHeaders(outNames); outputTypes["count"].push_back(outCountFile); outputNames.push_back(outCountFile); }
303                 
304                 for (int i = 0; i < nameFileOrder.size(); i++) {
305                         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; }
306                         
307                         itStrings = sequenceStrings.find(nameFileOrder[i]);
308                         
309                         if (itStrings != sequenceStrings.end()) {
310                 if (countfile == "") {
311                     //get rep name
312                     int pos = (itStrings->second).find_first_of(',');
313                     
314                     if (pos == string::npos) { // only reps itself
315                         outNames << itStrings->second << '\t' << itStrings->second << endl;
316                     }else {
317                         outNames << (itStrings->second).substr(0, pos) << '\t' << itStrings->second << endl;
318                     }
319                 }else {  ct.printSeq(outNames, itStrings->second);  }
320                         }else{ m->mothurOut("[ERROR]: mismatch in namefile print."); m->mothurOutEndLine(); m->control_pressed = true; }
321                 }
322                 outNames.close();
323                 
324                 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); }  return 0; }
325                 
326                 m->mothurOutEndLine();
327                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
328                 outputNames.push_back(outFastaFile);   outputTypes["fasta"].push_back(outFastaFile);  
329         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
330                 m->mothurOutEndLine();
331
332                 //set fasta file as new current fastafile
333                 string current = "";
334                 itTypes = outputTypes.find("fasta");
335                 if (itTypes != outputTypes.end()) {
336                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
337                 }
338                 
339                 itTypes = outputTypes.find("name");
340                 if (itTypes != outputTypes.end()) {
341                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
342                 }
343         
344         itTypes = outputTypes.find("count");
345                 if (itTypes != outputTypes.end()) {
346                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
347                 }
348                 
349                 return 0;
350         }
351         catch(exception& e) {
352                 m->errorOut(e, "DeconvoluteCommand", "execute");
353                 exit(1);
354         }
355 }
356 /**************************************************************************************/