2 * getsharedotucommand.cpp
5 * Created by westcott on 9/22/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "getsharedotucommand.h"
11 #include "sharedutilities.h"
13 //**********************************************************************************************************************
14 vector<string> GetSharedOTUCommand::setParameters(){
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pfasta);
17 CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT", "none",false,true); parameters.push_back(pgroup);
18 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none",false,true); parameters.push_back(plist);
19 CommandParameter poutput("output", "Multiple", "accnos-default", "default", "", "", "",false,false); parameters.push_back(poutput);
20 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
21 CommandParameter punique("unique", "String", "", "", "", "", "",false,false); parameters.push_back(punique);
22 CommandParameter pshared("shared", "String", "", "", "", "", "",false,false); parameters.push_back(pshared);
23 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "GetSharedOTUCommand", "setParameters");
35 //**********************************************************************************************************************
36 string GetSharedOTUCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The get.sharedseqs command parameters are list, group, label, unique, shared, output and fasta. The list and group parameters are required, unless you have valid current files.\n";
40 helpString += "The label parameter allows you to select what distance levels you would like output files for, and are separated by dashes.\n";
41 helpString += "The unique and shared parameters allow you to select groups you would like to know the shared info for, and are separated by dashes.\n";
42 helpString += "If you enter your groups under the unique parameter mothur will return the otus that contain ONLY sequences from those groups.\n";
43 helpString += "If you enter your groups under the shared parameter mothur will return the otus that contain sequences from those groups and may also contain sequences from other groups.\n";
44 helpString += "If you do not enter any groups then the get.sharedseqs command will return sequences that are unique to all groups in your group file.\n";
45 helpString += "The fasta parameter allows you to input a fasta file and outputs a fasta file for each distance level containing only the sequences that are in OTUs shared by the groups specified.\n";
46 helpString += "The output parameter allows you to output the list of names without the group and bin number added. \n";
47 helpString += "With this option you can use the names file as an input in get.seqs and remove.seqs commands. To do this enter output=accnos. \n";
48 helpString += "The get.sharedseqs command outputs a .names file for each distance level containing a list of sequences in the OTUs shared by the groups specified.\n";
49 helpString += "The get.sharedseqs command should be in the following format: get.sharedseqs(list=yourListFile, group=yourGroupFile, label=yourLabels, unique=yourGroups, fasta=yourFastafile, output=yourOutput).\n";
50 helpString += "Example get.sharedseqs(list=amazon.fn.list, label=unique-0.01, group= amazon.groups, unique=forest-pasture, fasta=amazon.fasta, output=accnos).\n";
51 helpString += "The output to the screen is the distance and the number of otus at that distance for the groups you specified.\n";
52 helpString += "The default value for label is all labels in your inputfile. The default for groups is all groups in your file.\n";
53 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n";
57 m->errorOut(e, "GetSharedOTUCommand", "getHelpString");
61 //**********************************************************************************************************************
62 string GetSharedOTUCommand::getOutputFileNameTag(string type, string inputName=""){
64 string outputFileName = "";
65 map<string, vector<string> >::iterator it;
67 //is this a type this command creates
68 it = outputTypes.find(type);
69 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
71 if (type == "fasta") { outputFileName = "shared.fasta"; }
72 else if (type == "accnos") { outputFileName = "accnos"; }
73 else if (type == "sharedseqs") { outputFileName = "shared.seqs"; }
74 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
76 return outputFileName;
79 m->errorOut(e, "GetSharedOTUCommand", "getOutputFileNameTag");
84 //**********************************************************************************************************************
85 GetSharedOTUCommand::GetSharedOTUCommand(){
87 abort = true; calledHelp = true;
89 vector<string> tempOutNames;
90 outputTypes["fasta"] = tempOutNames;
91 outputTypes["accnos"] = tempOutNames;
92 outputTypes["sharedseqs"] = tempOutNames;
95 m->errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
99 //**********************************************************************************************************************
100 GetSharedOTUCommand::GetSharedOTUCommand(string option) {
103 abort = false; calledHelp = false;
107 //allow user to run help
108 if(option == "help") { help(); abort = true; calledHelp = true; }
109 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
112 vector<string> myArray = setParameters();
114 OptionParser parser(option);
115 map<string,string> parameters = parser.getParameters();
117 ValidParameters validParameter;
118 map<string,string>::iterator it;
120 //check to make sure all parameters are valid for command
121 for (it = parameters.begin(); it != parameters.end(); it++) {
122 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
125 //initialize outputTypes
126 vector<string> tempOutNames;
127 outputTypes["fasta"] = tempOutNames;
128 outputTypes["accnos"] = tempOutNames;
129 outputTypes["sharedseqs"] = tempOutNames;
131 //if the user changes the output directory command factory will send this info to us in the output parameter
132 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
134 //if the user changes the input directory command factory will send this info to us in the output parameter
135 string inputDir = validParameter.validFile(parameters, "inputdir", false);
136 if (inputDir == "not found"){ inputDir = ""; }
139 it = parameters.find("fasta");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["fasta"] = inputDir + it->second; }
147 it = parameters.find("list");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["list"] = inputDir + it->second; }
155 it = parameters.find("group");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["group"] = inputDir + it->second; }
165 //check for required parameters
166 listfile = validParameter.validFile(parameters, "list", true);
167 if (listfile == "not open") { abort = true; }
168 else if (listfile == "not found") {
169 listfile = m->getListFile();
170 if (listfile != "") { format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
172 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
175 }else { format = "list"; m->setListFile(listfile); }
177 groupfile = validParameter.validFile(parameters, "group", true);
178 if (groupfile == "not open") { abort = true; }
179 else if (groupfile == "not found") {
180 groupfile = m->getGroupFile();
181 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
183 m->mothurOut("No valid current group file. You must provide a group file."); m->mothurOutEndLine();
186 }else { m->setGroupFile(groupfile); }
188 if ((listfile == "") || (groupfile == "")) { m->mothurOut("The list and group parameters are required."); m->mothurOutEndLine(); abort = true; }
190 //check for optional parameter and set defaults
191 // ...at some point should added some additional type checking...
192 label = validParameter.validFile(parameters, "label", false);
193 if (label == "not found") { label = ""; }
195 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
196 else { allLines = 1; }
199 output = validParameter.validFile(parameters, "output", false);
200 if (output == "not found") { output = ""; }
201 else if (output == "default") { output = ""; }
203 groups = validParameter.validFile(parameters, "unique", false);
204 if (groups == "not found") { groups = ""; }
206 userGroups = "unique." + groups;
207 m->splitAtDash(groups, Groups);
208 m->setGroups(Groups);
212 groups = validParameter.validFile(parameters, "shared", false);
213 if (groups == "not found") { groups = ""; }
216 m->splitAtDash(groups, Groups);
217 m->setGroups(Groups);
221 fastafile = validParameter.validFile(parameters, "fasta", true);
222 if (fastafile == "not open") { abort = true; }
223 else if (fastafile == "not found") { fastafile = ""; }
224 else { m->setFastaFile(fastafile); }
228 catch(exception& e) {
229 m->errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
233 //**********************************************************************************************************************
235 int GetSharedOTUCommand::execute(){
238 if (abort == true) { if (calledHelp) { return 0; } return 2; }
240 groupMap = new GroupMap(groupfile);
241 int error = groupMap->readMap();
242 if (error == 1) { delete groupMap; return 0; }
244 if (m->control_pressed) { delete groupMap; return 0; }
246 if (Groups.size() == 0) {
247 Groups = groupMap->getNamesOfGroups();
249 //make string for outputfile name
250 userGroups = "unique.";
251 for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
252 userGroups = userGroups.substr(0, userGroups.length()-1);
254 //sanity check for group names
256 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
257 util.setGroups(Groups, namesOfGroups);
258 groupMap->setNamesOfGroups(namesOfGroups);
261 //put groups in map to find easier
262 for(int i = 0; i < Groups.size(); i++) {
263 groupFinder[Groups[i]] = Groups[i];
266 if (fastafile != "") {
268 m->openInputFile(fastafile, inFasta);
270 while(!inFasta.eof()) {
271 if (m->control_pressed) { outputTypes.clear(); inFasta.close(); delete groupMap; return 0; }
273 Sequence seq(inFasta); m->gobble(inFasta);
274 if (seq.getName() != "") { seqs.push_back(seq); }
279 ListVector* lastlist = NULL;
280 string lastLabel = "";
282 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
283 set<string> processedLabels;
284 set<string> userLabels = labels;
287 m->openInputFile(listfile, in);
289 //as long as you are not at the end of the file or done wih the lines you want
290 while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
292 if (m->control_pressed) {
293 if (lastlist != NULL) { delete lastlist; }
294 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
295 delete groupMap; return 0;
298 list = new ListVector(in);
300 if(allLines == 1 || labels.count(list->getLabel()) == 1){
301 m->mothurOut(list->getLabel());
304 processedLabels.insert(list->getLabel());
305 userLabels.erase(list->getLabel());
308 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
309 string saveLabel = list->getLabel();
311 m->mothurOut(lastlist->getLabel());
314 processedLabels.insert(lastlist->getLabel());
315 userLabels.erase(lastlist->getLabel());
317 //restore real lastlabel to save below
318 list->setLabel(saveLabel);
321 lastLabel = list->getLabel();
323 if (lastlist != NULL) { delete lastlist; }
329 //output error messages about any remaining user labels
330 set<string>::iterator it;
331 bool needToRun = false;
332 for (it = userLabels.begin(); it != userLabels.end(); it++) {
333 m->mothurOut("Your file does not include the label " + *it);
334 if (processedLabels.count(lastLabel) != 1) {
335 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
338 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
342 //run last label if you need to
343 if (needToRun == true) {
344 m->mothurOut(lastlist->getLabel());
347 processedLabels.insert(lastlist->getLabel());
348 userLabels.erase(lastlist->getLabel());
352 //reset groups parameter
355 if (lastlist != NULL) { delete lastlist; }
357 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete groupMap; return 0; }
359 //set fasta file as new current fastafile
361 itTypes = outputTypes.find("fasta");
362 if (itTypes != outputTypes.end()) {
363 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
366 if (output == "accnos") {
367 itTypes = outputTypes.find("accnos");
368 if (itTypes != outputTypes.end()) {
369 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
373 m->mothurOutEndLine();
374 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
375 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
376 m->mothurOutEndLine();
382 catch(exception& e) {
383 m->errorOut(e, "GetSharedOTUCommand", "execute");
387 /***********************************************************/
388 int GetSharedOTUCommand::process(ListVector* shared) {
391 map<string, string> fastaMap;
394 string outputFileNames;
396 if (outputDir == "") { outputDir += m->hasPath(listfile); }
397 if (output != "accnos") {
398 outputFileNames = outputDir + m->getRootName(m->getSimpleName(listfile)) + shared->getLabel() + userGroups + "." + getOutputFileNameTag("sharedseqs");
400 outputFileNames = outputDir + m->getRootName(m->getSimpleName(listfile)) + shared->getLabel() + userGroups + "." + getOutputFileNameTag("accnos");
402 m->openOutputFile(outputFileNames, outNames);
404 bool wroteSomething = false;
407 //go through each bin, find out if shared
408 for (int i = 0; i < shared->getNumBins(); i++) {
409 if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
411 bool uniqueOTU = true;
413 map<string, int> atLeastOne;
414 for (int f = 0; f < Groups.size(); f++) {
415 atLeastOne[Groups[f]] = 0;
418 vector<string> namesOfSeqsInThisBin;
420 string names = shared->get(i);
421 while ((names.find_first_of(',') != -1)) {
422 string name = names.substr(0,names.find_first_of(','));
423 names = names.substr(names.find_first_of(',')+1, names.length());
426 string seqGroup = groupMap->getGroup(name);
427 if (output != "accnos") {
428 namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + toString(i+1)));
429 }else { namesOfSeqsInThisBin.push_back(name); }
431 if (seqGroup == "not found") { m->mothurOut(name + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1); }
433 //is this seq in one of hte groups we care about
434 it = groupFinder.find(seqGroup);
435 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
436 else { atLeastOne[seqGroup]++; }
440 string seqGroup = groupMap->getGroup(names);
441 if (output != "accnos") {
442 namesOfSeqsInThisBin.push_back((names + "|" + seqGroup + "|" + toString(i+1)));
443 }else { namesOfSeqsInThisBin.push_back(names); }
445 if (seqGroup == "not found") { m->mothurOut(names + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1); }
447 //is this seq in one of hte groups we care about
448 it = groupFinder.find(seqGroup);
449 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
450 else { atLeastOne[seqGroup]++; }
453 //make sure you have at least one seq from each group you want
454 bool sharedByAll = true;
455 map<string, int>::iterator it2;
456 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
457 if (it2->second == 0) { sharedByAll = false; }
460 //if the user wants unique bins and this is unique then print
461 //or this the user wants shared bins and this bin is shared then print
462 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
464 wroteSomething = true;
467 //output list of names
468 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
469 outNames << namesOfSeqsInThisBin[j] << endl;
471 if (fastafile != "") {
472 if (output != "accnos") {
473 string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
474 seqName = seqName.substr(0,seqName.find_last_of('|'));
475 fastaMap[seqName] = namesOfSeqsInThisBin[j]; //fastaMap needs to contain just the seq name for output later
477 fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
486 if (!wroteSomething) {
487 m->mothurRemove(outputFileNames);
488 string outputString = "\t" + toString(num) + " - No otus shared by groups";
490 string groupString = "";
491 for (int h = 0; h < Groups.size(); h++) {
492 groupString += " " + Groups[h];
495 outputString += groupString + ".";
496 m->mothurOut(outputString); m->mothurOutEndLine();
498 m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
499 outputNames.push_back(outputFileNames);
500 if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
501 else { outputTypes["accnos"].push_back(outputFileNames); }
504 //if fasta file provided output new fasta file
505 if ((fastafile != "") && wroteSomething) {
506 if (outputDir == "") { outputDir += m->hasPath(fastafile); }
507 string outputFileFasta = outputDir + m->getRootName(m->getSimpleName(fastafile)) + shared->getLabel() + userGroups + "." + getOutputFileNameTag("fasta");
509 m->openOutputFile(outputFileFasta, outFasta);
510 outputNames.push_back(outputFileFasta); outputTypes["fasta"].push_back(outputFileFasta);
512 for (int k = 0; k < seqs.size(); k++) {
513 if (m->control_pressed) { outFasta.close(); return 0; }
515 //if this is a sequence we want, output it
516 it = fastaMap.find(seqs[k].getName());
517 if (it != fastaMap.end()) {
519 if (output != "accnos") {
520 outFasta << ">" << it->second << endl;
522 outFasta << ">" << it->first << endl;
525 outFasta << seqs[k].getAligned() << endl;
535 catch(exception& e) {
536 m->errorOut(e, "GetSharedOTUCommand", "process");
541 //**********************************************************************************************************************