2 * getsharedotucommand.cpp
5 * Created by westcott on 9/22/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "getsharedotucommand.h"
12 //**********************************************************************************************************************
14 GetSharedOTUCommand::GetSharedOTUCommand(string option){
17 globaldata = GlobalData::getInstance();
23 //allow user to run help
24 if(option == "help") { help(); abort = true; }
27 //valid paramters for this command
28 string Array[] = {"label","unique","shared","fasta","list","group","output","outputdir","inputdir"};
29 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
31 OptionParser parser(option);
32 map<string,string> parameters = parser.getParameters();
34 ValidParameters validParameter;
35 map<string,string>::iterator it;
37 //check to make sure all parameters are valid for command
38 for (it = parameters.begin(); it != parameters.end(); it++) {
39 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
42 //if the user changes the output directory command factory will send this info to us in the output parameter
43 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
45 //if the user changes the input directory command factory will send this info to us in the output parameter
46 string inputDir = validParameter.validFile(parameters, "inputdir", false);
47 if (inputDir == "not found"){ inputDir = ""; }
50 it = parameters.find("fasta");
51 //user has given a template file
52 if(it != parameters.end()){
53 path = hasPath(it->second);
54 //if the user has not given a path then, add inputdir. else leave path alone.
55 if (path == "") { parameters["fasta"] = inputDir + it->second; }
58 it = parameters.find("list");
59 //user has given a template file
60 if(it != parameters.end()){
61 path = hasPath(it->second);
62 //if the user has not given a path then, add inputdir. else leave path alone.
63 if (path == "") { parameters["list"] = inputDir + it->second; }
66 it = parameters.find("group");
67 //user has given a template file
68 if(it != parameters.end()){
69 path = hasPath(it->second);
70 //if the user has not given a path then, add inputdir. else leave path alone.
71 if (path == "") { parameters["group"] = inputDir + it->second; }
76 //check for required parameters
77 listfile = validParameter.validFile(parameters, "list", true);
78 if (listfile == "not open") { abort = true; }
79 else if (listfile == "not found") { listfile = ""; }
80 else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
82 groupfile = validParameter.validFile(parameters, "group", true);
83 if (groupfile == "not open") { abort = true; }
84 else if (groupfile == "not found") { groupfile = ""; }
86 if ((listfile == "") || (groupfile == "")) { mothurOut("The list and group parameters are required."); mothurOutEndLine(); abort = true; }
88 //check for optional parameter and set defaults
89 // ...at some point should added some additional type checking...
90 label = validParameter.validFile(parameters, "label", false);
91 if (label == "not found") { label = ""; }
93 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
94 else { allLines = 1; }
97 output = validParameter.validFile(parameters, "output", false);
98 if (output == "not found") { output = ""; }
100 groups = validParameter.validFile(parameters, "unique", false);
101 if (groups == "not found") { groups = ""; }
103 userGroups = "unique." + groups;
104 splitAtDash(groups, Groups);
105 globaldata->Groups = Groups;
109 groups = validParameter.validFile(parameters, "shared", false);
110 if (groups == "not found") { groups = ""; }
113 splitAtDash(groups, Groups);
114 globaldata->Groups = Groups;
118 fastafile = validParameter.validFile(parameters, "fasta", true);
119 if (fastafile == "not open") { abort = true; }
120 else if (fastafile == "not found") { fastafile = ""; }
125 catch(exception& e) {
126 errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
130 //**********************************************************************************************************************
132 void GetSharedOTUCommand::help(){
134 mothurOut("The get.sharedseqs command parameters are list, group, label, unique, shared, output and fasta. The list and group parameters are required.\n");
135 mothurOut("The label parameter allows you to select what distance levels you would like output files for, and are separated by dashes.\n");
136 mothurOut("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");
137 mothurOut("If you enter your groups under the unique parameter mothur will return the otus that contain ONLY sequences from those groups.\n");
138 mothurOut("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");
139 mothurOut("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");
140 mothurOut("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");
141 mothurOut("The output parameter allows you to output the list of names without the group and bin number added. \n");
142 mothurOut("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");
143 mothurOut("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");
144 mothurOut("The get.sharedseqs command should be in the following format: get.sabund(label=yourLabels, groups=yourGroups, fasta=yourFastafile, output=yourOutput).\n");
145 mothurOut("Example get.sharedseqs(list=amazon.fn.list, label=unique-0.01, group=forest-pasture, fasta=amazon.fasta, output=accnos).\n");
146 mothurOut("The output to the screen is the distance and the number of otus at that distance for the groups you specified.\n");
147 mothurOut("The default value for label is all labels in your inputfile. The default for groups is all groups in your file.\n");
148 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
150 catch(exception& e) {
151 errorOut(e, "GetSharedOTUCommand", "help");
156 //**********************************************************************************************************************
158 GetSharedOTUCommand::~GetSharedOTUCommand(){}
160 //**********************************************************************************************************************
162 int GetSharedOTUCommand::execute(){
165 if (abort == true) { return 0; }
167 groupMap = new GroupMap(groupfile);
168 int error = groupMap->readMap();
169 if (error == 1) { delete groupMap; return 0; }
171 globaldata->gGroupmap = groupMap;
173 if (Groups.size() == 0) {
174 Groups = groupMap->namesOfGroups;
176 //make string for outputfile name
177 userGroups = "unique.";
178 for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
179 userGroups = userGroups.substr(0, userGroups.length()-1);
182 //put groups in map to find easier
183 for(int i = 0; i < Groups.size(); i++) {
184 groupFinder[Groups[i]] = Groups[i];
187 if (fastafile != "") {
189 openInputFile(fastafile, inFasta);
191 while(!inFasta.eof()) {
192 Sequence seq(inFasta); gobble(inFasta);
193 if (seq.getName() != "") { seqs.push_back(seq); }
198 ListVector* lastlist = NULL;
199 string lastLabel = "";
201 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
202 set<string> processedLabels;
203 set<string> userLabels = labels;
206 openInputFile(listfile, in);
208 //as long as you are not at the end of the file or done wih the lines you want
209 while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
211 list = new ListVector(in);
213 if(allLines == 1 || labels.count(list->getLabel()) == 1){
214 mothurOut(list->getLabel());
217 processedLabels.insert(list->getLabel());
218 userLabels.erase(list->getLabel());
221 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
222 string saveLabel = list->getLabel();
224 mothurOut(lastlist->getLabel());
227 processedLabels.insert(lastlist->getLabel());
228 userLabels.erase(lastlist->getLabel());
230 //restore real lastlabel to save below
231 list->setLabel(saveLabel);
234 lastLabel = list->getLabel();
236 if (lastlist != NULL) { delete lastlist; }
242 //output error messages about any remaining user labels
243 set<string>::iterator it;
244 bool needToRun = false;
245 for (it = userLabels.begin(); it != userLabels.end(); it++) {
246 mothurOut("Your file does not include the label " + *it);
247 if (processedLabels.count(lastLabel) != 1) {
248 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
251 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
255 //run last label if you need to
256 if (needToRun == true) {
257 mothurOut(lastlist->getLabel());
260 processedLabels.insert(lastlist->getLabel());
261 userLabels.erase(lastlist->getLabel());
265 //reset groups parameter
266 globaldata->Groups.clear();
268 if (lastlist != NULL) { delete lastlist; }
272 catch(exception& e) {
273 errorOut(e, "GetSharedOTUCommand", "execute");
277 /***********************************************************/
278 void GetSharedOTUCommand::process(ListVector* shared) {
281 map<string, string> fastaMap;
284 string outputFileNames;
286 if (outputDir == "") { outputDir += hasPath(listfile); }
287 if (output != "accnos") {
288 outputFileNames = outputDir + getRootName(getSimpleName(listfile)) + shared->getLabel() + userGroups + ".shared.seqs";
290 outputFileNames = outputDir + getRootName(getSimpleName(listfile)) + shared->getLabel() + userGroups + ".accnos";
292 openOutputFile(outputFileNames, outNames);
294 bool wroteSomething = false;
297 //go through each bin, find out if shared
298 for (int i = 0; i < shared->getNumBins(); i++) {
300 bool uniqueOTU = true;
302 map<string, int> atLeastOne;
303 for (int m = 0; m < Groups.size(); m++) {
304 atLeastOne[Groups[m]] = 0;
307 vector<string> namesOfSeqsInThisBin;
309 string names = shared->get(i);
310 while ((names.find_first_of(',') != -1)) {
311 string name = names.substr(0,names.find_first_of(','));
312 names = names.substr(names.find_first_of(',')+1, names.length());
315 string seqGroup = groupMap->getGroup(name);
316 if (output != "accnos") {
317 namesOfSeqsInThisBin.push_back((name + "\t" + seqGroup + "\t" + toString(i+1)));
318 }else { namesOfSeqsInThisBin.push_back(name); }
320 if (seqGroup == "not found") { mothurOut(name + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1); }
322 //is this seq in one of hte groups we care about
323 it = groupFinder.find(seqGroup);
324 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
325 else { atLeastOne[seqGroup]++; }
329 string seqGroup = groupMap->getGroup(names);
330 if (output != "accnos") {
331 namesOfSeqsInThisBin.push_back((names + "\t" + seqGroup + "\t" + toString(i+1)));
332 }else { namesOfSeqsInThisBin.push_back(names); }
334 if (seqGroup == "not found") { mothurOut(names + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1); }
336 //is this seq in one of hte groups we care about
337 it = groupFinder.find(seqGroup);
338 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
339 else { atLeastOne[seqGroup]++; }
342 //make sure you have at least one seq from each group you want
343 bool sharedByAll = true;
344 map<string, int>::iterator it2;
345 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
346 if (it2->second == 0) { sharedByAll = false; }
349 //if the user wants unique bins and this is unique then print
350 //or this the user wants shared bins and this bin is shared then print
351 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
353 wroteSomething = true;
356 //output list of names
357 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
358 outNames << namesOfSeqsInThisBin[j] << endl;
360 if (fastafile != "") {
361 if (output != "accnos") {
362 string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('\t'));
363 seqName = seqName.substr(0,seqName.find_last_of('\t'));
364 fastaMap[seqName] = namesOfSeqsInThisBin[j]; //fastaMap needs to contain just the seq name for output later
366 fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
375 if (!wroteSomething) {
376 remove(outputFileNames.c_str());
377 string outputString = "\t" + toString(num) + " - No otus shared by groups";
379 string groupString = "";
380 for (int h = 0; h < Groups.size(); h++) {
381 groupString += " " + Groups[h];
384 outputString += groupString + ".";
385 mothurOut(outputString); mothurOutEndLine();
386 }else { mothurOut("\t" + toString(num)); mothurOutEndLine(); }
388 //if fasta file provided output new fasta file
389 if ((fastafile != "") && wroteSomething) {
390 if (outputDir == "") { outputDir += hasPath(fastafile); }
391 string outputFileFasta = outputDir + getRootName(getSimpleName(fastafile)) + shared->getLabel() + userGroups + ".shared.fasta";
393 openOutputFile(outputFileFasta, outFasta);
395 for (int k = 0; k < seqs.size(); k++) {
396 //if this is a sequence we want, output it
397 it = fastaMap.find(seqs[k].getName());
398 if (it != fastaMap.end()) {
400 if (output != "accnos") {
401 outFasta << ">" << it->second << endl;
403 outFasta << ">" << it->first << endl;
406 outFasta << seqs[k].getAligned() << endl;
415 catch(exception& e) {
416 errorOut(e, "GetSharedOTUCommand", "process");
421 //**********************************************************************************************************************