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();
22 //allow user to run help
23 if(option == "help") { help(); abort = true; }
26 //valid paramters for this command
27 string Array[] = {"label","groups","fasta","list","group","output"};
28 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
30 OptionParser parser(option);
31 map<string,string> parameters = parser.getParameters();
33 ValidParameters validParameter;
35 //check to make sure all parameters are valid for command
36 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
37 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
40 //check for required parameters
41 listfile = validParameter.validFile(parameters, "list", true);
42 if (listfile == "not open") { abort = true; }
43 else if (listfile == "not found") { listfile = ""; }
44 else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
46 groupfile = validParameter.validFile(parameters, "group", true);
47 if (groupfile == "not open") { abort = true; }
48 else if (groupfile == "not found") { groupfile = ""; }
50 if ((listfile == "") || (groupfile == "")) { mothurOut("The list and group parameters are required."); mothurOutEndLine(); abort = true; }
52 //check for optional parameter and set defaults
53 // ...at some point should added some additional type checking...
54 label = validParameter.validFile(parameters, "label", false);
55 if (label == "not found") { label = ""; }
57 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
58 else { allLines = 1; }
61 output = validParameter.validFile(parameters, "output", false);
62 if (output == "not found") { output = ""; }
64 groups = validParameter.validFile(parameters, "groups", false);
65 if (groups == "not found") { groups = ""; }
67 splitAtDash(groups, Groups);
68 globaldata->Groups = Groups;
71 fastafile = validParameter.validFile(parameters, "fasta", true);
72 if (fastafile == "not open") { abort = true; }
73 else if (fastafile == "not found") { fastafile = ""; }
79 errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
83 //**********************************************************************************************************************
85 void GetSharedOTUCommand::help(){
87 mothurOut("The get.sharedotu command parameters are list, group, label, groups, output and fasta. The list and group parameters are required.\n");
88 mothurOut("The label parameter allows you to select what distance levels you would like output files for, and are separated by dashes.\n");
89 mothurOut("The groups parameter allows you to select groups you would like to know the shared info for, and are separated by dashes.\n");
90 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");
91 mothurOut("The output parameter allows you to output the list of names without the group and bin number added. \n");
92 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");
93 mothurOut("The get.sharedotu command outputs a .names file for each distance level containing a list of sequences in the OTUs shared by the groups specified.\n");
94 mothurOut("The get.sharedotu command should be in the following format: get.sabund(label=yourLabels, groups=yourGroups, fasta=yourFastafile, output=yourOutput).\n");
95 mothurOut("Example get.sharedotu(list=amazon.fn.list, label=unique-0.01, group=forest-pasture, fasta=amazon.fasta, output=accnos).\n");
96 mothurOut("The default value for label is all labels in your inputfile. The default for groups is all groups in your file.\n");
97 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
100 errorOut(e, "GetSharedOTUCommand", "help");
105 //**********************************************************************************************************************
107 GetSharedOTUCommand::~GetSharedOTUCommand(){}
109 //**********************************************************************************************************************
111 int GetSharedOTUCommand::execute(){
114 if (abort == true) { return 0; }
116 groupMap = new GroupMap(groupfile);
118 globaldata->gGroupmap = groupMap;
120 if (Groups.size() == 0) {
121 Groups = groupMap->namesOfGroups;
124 //put groups in map to find easier
125 for(int i = 0; i < Groups.size(); i++) {
126 groupFinder[Groups[i]] = Groups[i];
129 if (fastafile != "") {
131 openInputFile(fastafile, inFasta);
133 while(!inFasta.eof()) {
134 Sequence seq(inFasta);
140 ListVector* lastlist = NULL;
141 string lastLabel = "";
143 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
144 set<string> processedLabels;
145 set<string> userLabels = labels;
148 openInputFile(listfile, in);
150 //as long as you are not at the end of the file or done wih the lines you want
151 while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
153 list = new ListVector(in);
155 if(allLines == 1 || labels.count(list->getLabel()) == 1){
156 mothurOut(list->getLabel());
159 processedLabels.insert(list->getLabel());
160 userLabels.erase(list->getLabel());
163 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
164 string saveLabel = list->getLabel();
166 mothurOut(lastlist->getLabel());
169 processedLabels.insert(lastlist->getLabel());
170 userLabels.erase(lastlist->getLabel());
172 //restore real lastlabel to save below
173 list->setLabel(saveLabel);
176 lastLabel = list->getLabel();
178 if (lastlist != NULL) { delete lastlist; }
184 //output error messages about any remaining user labels
185 set<string>::iterator it;
186 bool needToRun = false;
187 for (it = userLabels.begin(); it != userLabels.end(); it++) {
188 mothurOut("Your file does not include the label " + *it);
189 if (processedLabels.count(lastLabel) != 1) {
190 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
193 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
197 //run last label if you need to
198 if (needToRun == true) {
199 mothurOut(lastlist->getLabel());
202 processedLabels.insert(lastlist->getLabel());
203 userLabels.erase(lastlist->getLabel());
207 //reset groups parameter
208 globaldata->Groups.clear();
210 if (lastlist != NULL) { delete lastlist; }
214 catch(exception& e) {
215 errorOut(e, "GetSharedOTUCommand", "execute");
219 /***********************************************************/
220 void GetSharedOTUCommand::process(ListVector* shared) {
223 map<string, string> fastaMap;
226 string outputFileNames = getRootName(listfile) + shared->getLabel() + ".names";
227 openOutputFile(outputFileNames, outNames);
229 bool wroteSomething = false;
231 //go through each bin, find out if shared
232 for (int i = 0; i < shared->getNumBins(); i++) {
234 bool sharedByAll = true;
236 map<string, int> atLeastOne;
237 for (int m = 0; m < Groups.size(); m++) {
238 atLeastOne[Groups[m]] = 0;
241 vector<string> namesOfSeqsInThisBin;
243 string names = shared->get(i);
244 while ((names.find_first_of(',') != -1) && sharedByAll) {
245 string name = names.substr(0,names.find_first_of(','));
246 names = names.substr(names.find_first_of(',')+1, names.length());
249 string seqGroup = groupMap->getGroup(name);
250 if (output != "accnos") {
251 namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + toString(i)));
252 }else { namesOfSeqsInThisBin.push_back(name); }
254 if (seqGroup == "not found") { mothurOut(name + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1); }
256 //is this seq in one of hte groups we care about
257 it = groupFinder.find(seqGroup);
258 if (it == groupFinder.end()) { sharedByAll = false; } //you have a sequence from a group you don't want
259 else { atLeastOne[seqGroup]++; }
265 string seqGroup = groupMap->getGroup(names);
266 if (output != "accnos") {
267 namesOfSeqsInThisBin.push_back((names + "|" + seqGroup + "|" + toString(i)));
268 }else { namesOfSeqsInThisBin.push_back(names); }
270 if (seqGroup == "not found") { mothurOut(names + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1); }
272 //is this seq in one of hte groups we care about
273 it = groupFinder.find(seqGroup);
274 if (it == groupFinder.end()) { sharedByAll = false; } //you have a sequence from a group you don't want
275 else { atLeastOne[seqGroup]++; }
278 //make sure you have at least one seq from each group you want
279 map<string, int>::iterator it2;
280 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
281 if (it2->second == 0) { sharedByAll = false; }
284 //if shared, save names of seqs in that bin
287 wroteSomething = true;
289 //output list of names
290 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
291 outNames << namesOfSeqsInThisBin[j] << endl;
293 if (fastafile != "") {
294 if (output != "accnos") {
295 string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
296 seqName = seqName.substr(0,seqName.find_last_of('|'));
297 fastaMap[seqName] = namesOfSeqsInThisBin[j]; //fastaMap needs to contain just the seq name for output later
299 fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
311 if (!wroteSomething) {
312 remove(outputFileNames.c_str());
313 string outputString = " - No otus shared by groups";
315 string groupString = "";
316 for (int h = 0; h < Groups.size(); h++) {
317 groupString += " " + Groups[h];
320 outputString += groupString + ".";
321 mothurOut(outputString); mothurOutEndLine();
322 }else { mothurOutEndLine(); }
324 //if fasta file provided output new fasta file
325 if ((fastafile != "") && wroteSomething) {
326 string outputFileFasta = getRootName(fastafile) + shared->getLabel() + ".fasta";
328 openOutputFile(outputFileFasta, outFasta);
330 for (int k = 0; k < seqs.size(); k++) {
331 //if this is a sequence we want, output it
332 it = fastaMap.find(seqs[k].getName());
333 if (it != fastaMap.end()) {
335 if (output != "accnos") {
336 outFasta << ">" << it->second << endl;
338 outFasta << ">" << it->first << endl;
341 outFasta << seqs[k].getAligned() << endl;
350 catch(exception& e) {
351 errorOut(e, "GetSharedOTUCommand", "process");
356 //**********************************************************************************************************************