5 * Created by Sarah Westcott on 1/21/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "deconvolutecommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> DeconvoluteCommand::setParameters(){
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);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "DeconvoluteCommand", "setParameters");
30 //**********************************************************************************************************************
31 string DeconvoluteCommand::getHelpString(){
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";
43 m->errorOut(e, "DeconvoluteCommand", "getHelpString");
47 //**********************************************************************************************************************
48 string DeconvoluteCommand::getOutputFileNameTag(string type, string inputName=""){
50 string outputFileName = "";
51 map<string, vector<string> >::iterator it;
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"); }
57 if (type == "fasta") { outputFileName = "unique" + m->getExtension(inputName); }
58 else if (type == "name") { outputFileName = "names"; }
59 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
61 return outputFileName;
64 m->errorOut(e, "DeconvoluteCommand", "getOutputFileNameTag");
68 //**********************************************************************************************************************
69 DeconvoluteCommand::DeconvoluteCommand(){
71 abort = true; calledHelp = true;
73 vector<string> tempOutNames;
74 outputTypes["fasta"] = tempOutNames;
75 outputTypes["name"] = tempOutNames;
78 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
82 /**************************************************************************************/
83 DeconvoluteCommand::DeconvoluteCommand(string option) {
85 abort = false; calledHelp = false;
87 //allow user to run help
88 if(option == "help") { help(); abort = true; calledHelp = true; }
89 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
92 vector<string> myArray = setParameters();
94 OptionParser parser(option);
95 map<string,string> parameters = parser.getParameters();
97 ValidParameters validParameter;
98 map<string, string>::iterator it;
100 //check to make sure all parameters are valid for command
101 for (it = parameters.begin(); it != parameters.end(); it++) {
102 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
105 //initialize outputTypes
106 vector<string> tempOutNames;
107 outputTypes["fasta"] = tempOutNames;
108 outputTypes["name"] = tempOutNames;
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 = ""; }
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; }
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; }
133 //check for required parameters
134 inFastaName = validParameter.validFile(parameters, "fasta", true);
135 if (inFastaName == "not open") { abort = true; }
136 else if (inFastaName == "not found") {
137 inFastaName = m->getFastaFile();
138 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
139 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
140 }else { m->setFastaFile(inFastaName); }
142 //if the user changes the output directory command factory will send this info to us in the output parameter
143 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
145 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it
148 oldNameMapFName = validParameter.validFile(parameters, "name", true);
149 if (oldNameMapFName == "not open") { oldNameMapFName = ""; abort = true; }
150 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
151 else { m->setNameFile(oldNameMapFName); }
153 if (oldNameMapFName == "") {
154 vector<string> files; files.push_back(inFastaName);
155 parser.getNameFile(files);
161 catch(exception& e) {
162 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
166 /**************************************************************************************/
167 int DeconvoluteCommand::execute() {
170 if (abort == true) { if (calledHelp) { return 0; } return 2; }
172 //prepare filenames and open files
173 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + getOutputFileNameTag("name");
174 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + getOutputFileNameTag("fasta", inFastaName);
176 map<string, string> nameMap;
177 map<string, string>::iterator itNames;
178 if (oldNameMapFName != "") {
179 m->readNames(oldNameMapFName, nameMap);
180 if (oldNameMapFName == outNameFile){ outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique." + getOutputFileNameTag("name"); }
183 if (m->control_pressed) { return 0; }
186 m->openInputFile(inFastaName, in);
189 m->openOutputFile(outFastaFile, outFasta);
191 map<string, string> sequenceStrings; //sequenceString -> list of names. "atgc...." -> seq1,seq2,seq3.
192 map<string, string>::iterator itStrings;
193 set<string> nameInFastaFile; //for sanity checking
194 set<string>::iterator itname;
195 vector<string> nameFileOrder;
199 if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
203 if (seq.getName() != "") {
206 itname = nameInFastaFile.find(seq.getName());
207 if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName()); }
208 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(); }
210 itStrings = sequenceStrings.find(seq.getAligned());
212 if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
213 //output to unique fasta file
214 seq.printSequence(outFasta);
216 if (oldNameMapFName != "") {
217 itNames = nameMap.find(seq.getName());
219 if (itNames == nameMap.end()) { //namefile and fastafile do not match
220 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
222 sequenceStrings[seq.getAligned()] = itNames->second;
223 nameFileOrder.push_back(seq.getAligned());
225 }else { sequenceStrings[seq.getAligned()] = seq.getName(); nameFileOrder.push_back(seq.getAligned()); }
226 }else { //this is a dup
227 if (oldNameMapFName != "") {
228 itNames = nameMap.find(seq.getName());
230 if (itNames == nameMap.end()) { //namefile and fastafile do not match
231 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
233 sequenceStrings[seq.getAligned()] += "," + itNames->second;
235 }else { sequenceStrings[seq.getAligned()] += "," + seq.getName(); }
243 if(count % 1000 == 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
246 if(count % 1000 != 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
251 if (m->control_pressed) { m->mothurRemove(outFastaFile); return 0; }
253 //print new names file
255 m->openOutputFile(outNameFile, outNames);
257 for (int i = 0; i < nameFileOrder.size(); i++) {
258 //for (itStrings = sequenceStrings.begin(); itStrings != sequenceStrings.end(); itStrings++) {
259 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); outNames.close(); m->mothurRemove(outNameFile); return 0; }
261 itStrings = sequenceStrings.find(nameFileOrder[i]);
263 if (itStrings != sequenceStrings.end()) {
265 int pos = (itStrings->second).find_first_of(',');
267 if (pos == string::npos) { // only reps itself
268 outNames << itStrings->second << '\t' << itStrings->second << endl;
270 outNames << (itStrings->second).substr(0, pos) << '\t' << itStrings->second << endl;
272 }else{ m->mothurOut("[ERROR]: mismatch in namefile print."); m->mothurOutEndLine(); m->control_pressed = true; }
276 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); m->mothurRemove(outNameFile); return 0; }
278 m->mothurOutEndLine();
279 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
280 m->mothurOut(outFastaFile); m->mothurOutEndLine();
281 m->mothurOut(outNameFile); m->mothurOutEndLine();
282 outputNames.push_back(outFastaFile); outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile); outputTypes["name"].push_back(outNameFile);
283 m->mothurOutEndLine();
285 //set fasta file as new current fastafile
287 itTypes = outputTypes.find("fasta");
288 if (itTypes != outputTypes.end()) {
289 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
292 itTypes = outputTypes.find("name");
293 if (itTypes != outputTypes.end()) {
294 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
299 catch(exception& e) {
300 m->errorOut(e, "DeconvoluteCommand", "execute");
304 /**************************************************************************************/