]> git.donarmstrong.com Git - mothur.git/blob - deconvolutecommand.cpp
paralellized seq.error and dist.shared added some error checks to libshuff and dist...
[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", "", "", "none", "none", "none",false,false); 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, "DeconvoluteCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string DeconvoluteCommand::getHelpString(){     
32         try {
33                 string helpString = "";
34                 helpString += "The unique.seqs command reads a fastafile and creates a namesfile.\n";
35                 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";
36                 helpString += "If the sequence is unique the second column will just contain its name. \n";
37                 helpString += "The unique.seqs command parameters are fasta and name.  fasta is required, unless there is a valid current fasta file.\n";
38                 helpString += "The unique.seqs command should be in the following format: \n";
39                 helpString += "unique.seqs(fasta=yourFastaFile) \n";    
40                 return helpString;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "DeconvoluteCommand", "getHelpString");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 DeconvoluteCommand::DeconvoluteCommand(){       
49         try {
50                 abort = true; calledHelp = true; 
51                 setParameters();
52                 vector<string> tempOutNames;
53                 outputTypes["fasta"] = tempOutNames;
54                 outputTypes["name"] = tempOutNames;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
58                 exit(1);
59         }
60 }
61 /**************************************************************************************/
62 DeconvoluteCommand::DeconvoluteCommand(string option)  {        
63         try {
64                 abort = false; calledHelp = false;   
65                 
66                 //allow user to run help
67                 if(option == "help") { help(); abort = true; calledHelp = true; }
68                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
69                 
70                 else {
71                         vector<string> myArray = setParameters();
72                         
73                         OptionParser parser(option);
74                         map<string,string> parameters = parser.getParameters();
75                         
76                         ValidParameters validParameter;
77                         map<string, string>::iterator it;
78                 
79                         //check to make sure all parameters are valid for command
80                         for (it = parameters.begin(); it != parameters.end(); it++) { 
81                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
82                         }
83                         
84                         //initialize outputTypes
85                         vector<string> tempOutNames;
86                         outputTypes["fasta"] = tempOutNames;
87                         outputTypes["name"] = tempOutNames;
88                 
89                         //if the user changes the input directory command factory will send this info to us in the output parameter 
90                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
91                         if (inputDir == "not found"){   inputDir = "";          }
92                         else {
93                                 string path;
94                                 it = parameters.find("fasta");
95                                 //user has given a template file
96                                 if(it != parameters.end()){ 
97                                         path = m->hasPath(it->second);
98                                         //if the user has not given a path then, add inputdir. else leave path alone.
99                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
100                                 }
101                                 
102                                 it = parameters.find("name");
103                                 //user has given a template file
104                                 if(it != parameters.end()){ 
105                                         path = m->hasPath(it->second);
106                                         //if the user has not given a path then, add inputdir. else leave path alone.
107                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
108                                 }
109                         }
110
111                         
112                         //check for required parameters
113                         inFastaName = validParameter.validFile(parameters, "fasta", true);
114                         if (inFastaName == "not open") { abort = true; }
115                         else if (inFastaName == "not found") {                          
116                                 inFastaName = m->getFastaFile(); 
117                                 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
118                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
119                         }       
120                         
121                         //if the user changes the output directory command factory will send this info to us in the output parameter 
122                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
123                                 outputDir = ""; 
124                                 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it     
125                         }
126                         
127                         oldNameMapFName = validParameter.validFile(parameters, "name", true);
128                         if (oldNameMapFName == "not open") { abort = true; }
129                         else if (oldNameMapFName == "not found"){       oldNameMapFName = "";   }
130                 }
131
132         }
133         catch(exception& e) {
134                 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
135                 exit(1);
136         }
137 }
138 /**************************************************************************************/
139 int DeconvoluteCommand::execute() {     
140         try {
141                 
142                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
143
144                 //prepare filenames and open files
145                 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
146                 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
147                 
148                 map<string, string> nameMap;
149                 map<string, string>::iterator itNames;
150                 if (oldNameMapFName != "")  {  m->readNames(oldNameMapFName, nameMap); }
151                 
152                 if (m->control_pressed) { return 0; }
153                 
154                 ifstream in; 
155                 m->openInputFile(inFastaName, in);
156                 
157                 ofstream outFasta;
158                 m->openOutputFile(outFastaFile, outFasta);
159                 
160                 map<string, string> sequenceStrings; //sequenceString -> list of names.  "atgc...." -> seq1,seq2,seq3.
161                 map<string, string>::iterator itStrings;
162                 set<string> nameInFastaFile; //for sanity checking
163                 set<string>::iterator itname;
164                 int count = 0;
165                 while (!in.eof()) {
166                         
167                         if (m->control_pressed) { in.close(); outFasta.close(); remove(outFastaFile.c_str()); return 0; }
168                         
169                         Sequence seq(in);
170                         
171                         if (seq.getName() != "") {
172                                 
173                                 //sanity checks
174                                 itname = nameInFastaFile.find(seq.getName());
175                                 if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName());  }
176                                 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(); }
177
178                                 itStrings = sequenceStrings.find(seq.getAligned());
179                                 
180                                 if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
181                                         //output to unique fasta file
182                                         seq.printSequence(outFasta);
183                                         
184                                         if (oldNameMapFName != "") {
185                                                 itNames = nameMap.find(seq.getName());
186                                                 
187                                                 if (itNames == nameMap.end()) { //namefile and fastafile do not match
188                                                         m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
189                                                 }else {
190                                                         sequenceStrings[seq.getAligned()] = itNames->second;
191                                                 }
192                                         }else { sequenceStrings[seq.getAligned()] = seq.getName();      }
193                                 }else { //this is a dup
194                                         if (oldNameMapFName != "") {
195                                                 itNames = nameMap.find(seq.getName());
196                                                 
197                                                 if (itNames == nameMap.end()) { //namefile and fastafile do not match
198                                                         m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
199                                                 }else {
200                                                         sequenceStrings[seq.getAligned()] += "," + itNames->second;
201                                                 }
202                                         }else { sequenceStrings[seq.getAligned()] += "," + seq.getName();       }
203                                 }
204                                 
205                                 count++;
206                         }
207                         
208                         m->gobble(in);
209                         
210                         if(count % 1000 == 0)   { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine();       }
211                 }
212                 
213                 if(count % 1000 != 0)   { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine();       }
214                 
215                 in.close();
216                 outFasta.close();
217                 
218                 if (m->control_pressed) { remove(outFastaFile.c_str()); return 0; }
219                 
220                 //print new names file
221                 ofstream outNames;
222                 m->openOutputFile(outNameFile, outNames);
223                 
224                 for (itStrings = sequenceStrings.begin(); itStrings != sequenceStrings.end(); itStrings++) {
225                         if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); outNames.close(); remove(outNameFile.c_str()); return 0; }
226                         
227                         //get rep name
228                         int pos = (itStrings->second).find_first_of(',');
229                         
230                         if (pos == string::npos) { // only reps itself
231                                 outNames << itStrings->second << '\t' << itStrings->second << endl;
232                         }else {
233                                 outNames << (itStrings->second).substr(0, pos) << '\t' << itStrings->second << endl;
234                         }
235                 }
236                 outNames.close();
237                 
238                 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
239                 
240                 m->mothurOutEndLine();
241                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
242                 m->mothurOut(outFastaFile); m->mothurOutEndLine();      
243                 m->mothurOut(outNameFile); m->mothurOutEndLine();
244                 outputNames.push_back(outFastaFile);  outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile);  outputTypes["name"].push_back(outNameFile); 
245                 m->mothurOutEndLine();
246
247                 //set fasta file as new current fastafile
248                 string current = "";
249                 itTypes = outputTypes.find("fasta");
250                 if (itTypes != outputTypes.end()) {
251                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
252                 }
253                 
254                 itTypes = outputTypes.find("name");
255                 if (itTypes != outputTypes.end()) {
256                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
257                 }
258                 
259                 return 0;
260         }
261         catch(exception& e) {
262                 m->errorOut(e, "DeconvoluteCommand", "execute");
263                 exit(1);
264         }
265 }
266 /**************************************************************************************/