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(label=yourLabels, groups=yourGroups, fasta=yourFastafile, output=yourOutput).\n";
50 helpString += "Example get.sharedseqs(list=amazon.fn.list, label=unique-0.01, group=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 GetSharedOTUCommand::GetSharedOTUCommand(){
64 abort = true; calledHelp = true;
66 vector<string> tempOutNames;
67 outputTypes["fasta"] = tempOutNames;
68 outputTypes["accnos"] = tempOutNames;
69 outputTypes["sharedseqs"] = tempOutNames;
72 m->errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
76 //**********************************************************************************************************************
77 GetSharedOTUCommand::GetSharedOTUCommand(string option) {
80 abort = false; calledHelp = false;
84 //allow user to run help
85 if(option == "help") { help(); abort = true; calledHelp = true; }
86 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
89 vector<string> myArray = setParameters();
91 OptionParser parser(option);
92 map<string,string> parameters = parser.getParameters();
94 ValidParameters validParameter;
95 map<string,string>::iterator it;
97 //check to make sure all parameters are valid for command
98 for (it = parameters.begin(); it != parameters.end(); it++) {
99 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
102 //initialize outputTypes
103 vector<string> tempOutNames;
104 outputTypes["fasta"] = tempOutNames;
105 outputTypes["accnos"] = tempOutNames;
106 outputTypes["sharedseqs"] = tempOutNames;
108 //if the user changes the output directory command factory will send this info to us in the output parameter
109 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
111 //if the user changes the input directory command factory will send this info to us in the output parameter
112 string inputDir = validParameter.validFile(parameters, "inputdir", false);
113 if (inputDir == "not found"){ inputDir = ""; }
116 it = parameters.find("fasta");
117 //user has given a template file
118 if(it != parameters.end()){
119 path = m->hasPath(it->second);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { parameters["fasta"] = inputDir + it->second; }
124 it = parameters.find("list");
125 //user has given a template file
126 if(it != parameters.end()){
127 path = m->hasPath(it->second);
128 //if the user has not given a path then, add inputdir. else leave path alone.
129 if (path == "") { parameters["list"] = inputDir + it->second; }
132 it = parameters.find("group");
133 //user has given a template file
134 if(it != parameters.end()){
135 path = m->hasPath(it->second);
136 //if the user has not given a path then, add inputdir. else leave path alone.
137 if (path == "") { parameters["group"] = inputDir + it->second; }
142 //check for required parameters
143 listfile = validParameter.validFile(parameters, "list", true);
144 if (listfile == "not open") { abort = true; }
145 else if (listfile == "not found") {
146 listfile = m->getListFile();
147 if (listfile != "") { format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
149 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
152 }else { format = "list"; m->setListFile(listfile); }
154 groupfile = validParameter.validFile(parameters, "group", true);
155 if (groupfile == "not open") { abort = true; }
156 else if (groupfile == "not found") {
157 groupfile = m->getGroupFile();
158 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
160 m->mothurOut("No valid current group file. You must provide a group file."); m->mothurOutEndLine();
163 }else { m->setGroupFile(groupfile); }
165 if ((listfile == "") || (groupfile == "")) { m->mothurOut("The list and group parameters are required."); m->mothurOutEndLine(); abort = true; }
167 //check for optional parameter and set defaults
168 // ...at some point should added some additional type checking...
169 label = validParameter.validFile(parameters, "label", false);
170 if (label == "not found") { label = ""; }
172 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
173 else { allLines = 1; }
176 output = validParameter.validFile(parameters, "output", false);
177 if (output == "not found") { output = ""; }
178 else if (output == "default") { output = ""; }
180 groups = validParameter.validFile(parameters, "unique", false);
181 if (groups == "not found") { groups = ""; }
183 userGroups = "unique." + groups;
184 m->splitAtDash(groups, Groups);
185 m->setGroups(Groups);
189 groups = validParameter.validFile(parameters, "shared", false);
190 if (groups == "not found") { groups = ""; }
193 m->splitAtDash(groups, Groups);
194 m->setGroups(Groups);
198 fastafile = validParameter.validFile(parameters, "fasta", true);
199 if (fastafile == "not open") { abort = true; }
200 else if (fastafile == "not found") { fastafile = ""; }
201 else { m->setFastaFile(fastafile); }
205 catch(exception& e) {
206 m->errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
210 //**********************************************************************************************************************
212 int GetSharedOTUCommand::execute(){
215 if (abort == true) { if (calledHelp) { return 0; } return 2; }
217 groupMap = new GroupMap(groupfile);
218 int error = groupMap->readMap();
219 if (error == 1) { delete groupMap; return 0; }
221 if (m->control_pressed) { delete groupMap; return 0; }
223 if (Groups.size() == 0) {
224 Groups = groupMap->getNamesOfGroups();
226 //make string for outputfile name
227 userGroups = "unique.";
228 for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
229 userGroups = userGroups.substr(0, userGroups.length()-1);
231 //sanity check for group names
233 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
234 util.setGroups(Groups, namesOfGroups);
235 groupMap->setNamesOfGroups(namesOfGroups);
238 //put groups in map to find easier
239 for(int i = 0; i < Groups.size(); i++) {
240 groupFinder[Groups[i]] = Groups[i];
243 if (fastafile != "") {
245 m->openInputFile(fastafile, inFasta);
247 while(!inFasta.eof()) {
248 if (m->control_pressed) { outputTypes.clear(); inFasta.close(); delete groupMap; return 0; }
250 Sequence seq(inFasta); m->gobble(inFasta);
251 if (seq.getName() != "") { seqs.push_back(seq); }
256 ListVector* lastlist = NULL;
257 string lastLabel = "";
259 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
260 set<string> processedLabels;
261 set<string> userLabels = labels;
264 m->openInputFile(listfile, in);
266 //as long as you are not at the end of the file or done wih the lines you want
267 while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
269 if (m->control_pressed) {
270 if (lastlist != NULL) { delete lastlist; }
271 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
272 delete groupMap; return 0;
275 list = new ListVector(in);
277 if(allLines == 1 || labels.count(list->getLabel()) == 1){
278 m->mothurOut(list->getLabel());
281 processedLabels.insert(list->getLabel());
282 userLabels.erase(list->getLabel());
285 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
286 string saveLabel = list->getLabel();
288 m->mothurOut(lastlist->getLabel());
291 processedLabels.insert(lastlist->getLabel());
292 userLabels.erase(lastlist->getLabel());
294 //restore real lastlabel to save below
295 list->setLabel(saveLabel);
298 lastLabel = list->getLabel();
300 if (lastlist != NULL) { delete lastlist; }
306 //output error messages about any remaining user labels
307 set<string>::iterator it;
308 bool needToRun = false;
309 for (it = userLabels.begin(); it != userLabels.end(); it++) {
310 m->mothurOut("Your file does not include the label " + *it);
311 if (processedLabels.count(lastLabel) != 1) {
312 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
315 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
319 //run last label if you need to
320 if (needToRun == true) {
321 m->mothurOut(lastlist->getLabel());
324 processedLabels.insert(lastlist->getLabel());
325 userLabels.erase(lastlist->getLabel());
329 //reset groups parameter
332 if (lastlist != NULL) { delete lastlist; }
334 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete groupMap; return 0; }
336 //set fasta file as new current fastafile
338 itTypes = outputTypes.find("fasta");
339 if (itTypes != outputTypes.end()) {
340 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
343 if (output == "accnos") {
344 itTypes = outputTypes.find("accnos");
345 if (itTypes != outputTypes.end()) {
346 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
350 m->mothurOutEndLine();
351 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
352 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
353 m->mothurOutEndLine();
359 catch(exception& e) {
360 m->errorOut(e, "GetSharedOTUCommand", "execute");
364 /***********************************************************/
365 int GetSharedOTUCommand::process(ListVector* shared) {
368 map<string, string> fastaMap;
371 string outputFileNames;
373 if (outputDir == "") { outputDir += m->hasPath(listfile); }
374 if (output != "accnos") {
375 outputFileNames = outputDir + m->getRootName(m->getSimpleName(listfile)) + shared->getLabel() + userGroups + ".shared.seqs";
377 outputFileNames = outputDir + m->getRootName(m->getSimpleName(listfile)) + shared->getLabel() + userGroups + ".accnos";
379 m->openOutputFile(outputFileNames, outNames);
381 bool wroteSomething = false;
384 //go through each bin, find out if shared
385 for (int i = 0; i < shared->getNumBins(); i++) {
386 if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
388 bool uniqueOTU = true;
390 map<string, int> atLeastOne;
391 for (int f = 0; f < Groups.size(); f++) {
392 atLeastOne[Groups[f]] = 0;
395 vector<string> namesOfSeqsInThisBin;
397 string names = shared->get(i);
398 while ((names.find_first_of(',') != -1)) {
399 string name = names.substr(0,names.find_first_of(','));
400 names = names.substr(names.find_first_of(',')+1, names.length());
403 string seqGroup = groupMap->getGroup(name);
404 if (output != "accnos") {
405 namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + toString(i+1)));
406 }else { namesOfSeqsInThisBin.push_back(name); }
408 if (seqGroup == "not found") { m->mothurOut(name + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1); }
410 //is this seq in one of hte groups we care about
411 it = groupFinder.find(seqGroup);
412 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
413 else { atLeastOne[seqGroup]++; }
417 string seqGroup = groupMap->getGroup(names);
418 if (output != "accnos") {
419 namesOfSeqsInThisBin.push_back((names + "|" + seqGroup + "|" + toString(i+1)));
420 }else { namesOfSeqsInThisBin.push_back(names); }
422 if (seqGroup == "not found") { m->mothurOut(names + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1); }
424 //is this seq in one of hte groups we care about
425 it = groupFinder.find(seqGroup);
426 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
427 else { atLeastOne[seqGroup]++; }
430 //make sure you have at least one seq from each group you want
431 bool sharedByAll = true;
432 map<string, int>::iterator it2;
433 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
434 if (it2->second == 0) { sharedByAll = false; }
437 //if the user wants unique bins and this is unique then print
438 //or this the user wants shared bins and this bin is shared then print
439 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
441 wroteSomething = true;
444 //output list of names
445 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
446 outNames << namesOfSeqsInThisBin[j] << endl;
448 if (fastafile != "") {
449 if (output != "accnos") {
450 string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
451 seqName = seqName.substr(0,seqName.find_last_of('|'));
452 fastaMap[seqName] = namesOfSeqsInThisBin[j]; //fastaMap needs to contain just the seq name for output later
454 fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
463 if (!wroteSomething) {
464 m->mothurRemove(outputFileNames);
465 string outputString = "\t" + toString(num) + " - No otus shared by groups";
467 string groupString = "";
468 for (int h = 0; h < Groups.size(); h++) {
469 groupString += " " + Groups[h];
472 outputString += groupString + ".";
473 m->mothurOut(outputString); m->mothurOutEndLine();
475 m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
476 outputNames.push_back(outputFileNames);
477 if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
478 else { outputTypes["accnos"].push_back(outputFileNames); }
481 //if fasta file provided output new fasta file
482 if ((fastafile != "") && wroteSomething) {
483 if (outputDir == "") { outputDir += m->hasPath(fastafile); }
484 string outputFileFasta = outputDir + m->getRootName(m->getSimpleName(fastafile)) + shared->getLabel() + userGroups + ".shared.fasta";
486 m->openOutputFile(outputFileFasta, outFasta);
487 outputNames.push_back(outputFileFasta); outputTypes["fasta"].push_back(outputFileFasta);
489 for (int k = 0; k < seqs.size(); k++) {
490 if (m->control_pressed) { outFasta.close(); return 0; }
492 //if this is a sequence we want, output it
493 it = fastaMap.find(seqs[k].getName());
494 if (it != fastaMap.end()) {
496 if (output != "accnos") {
497 outFasta << ">" << it->second << endl;
499 outFasta << ">" << it->first << endl;
502 outFasta << seqs[k].getAligned() << endl;
512 catch(exception& e) {
513 m->errorOut(e, "GetSharedOTUCommand", "process");
518 //**********************************************************************************************************************