]> git.donarmstrong.com Git - mothur.git/blob - getsharedotucommand.cpp
added get.sharedotu command
[mothur.git] / getsharedotucommand.cpp
1 /*
2  *  getsharedotucommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/22/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "getsharedotucommand.h"
11
12 //**********************************************************************************************************************
13
14 GetSharedOTUCommand::GetSharedOTUCommand(string option){
15         try {
16         
17                 globaldata = GlobalData::getInstance();
18                 abort = false;
19                 allLines = 1;
20                 labels.clear();
21                 
22                 //allow user to run help
23                 if(option == "help") { help(); abort = true; }
24                 
25                 else {
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)));
29                         
30                         OptionParser parser(option);
31                         map<string,string> parameters = parser.getParameters();
32                         
33                         ValidParameters validParameter;
34                         
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;  }
38                         }
39                         
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");      }
45                         
46                         groupfile = validParameter.validFile(parameters, "group", true);
47                         if (groupfile == "not open") { abort = true; }  
48                         else if (groupfile == "not found") { groupfile = ""; }
49                                                 
50                         if ((listfile == "") || (groupfile == "")) { mothurOut("The list and group parameters are required."); mothurOutEndLine(); abort = true; }
51                         
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 = ""; }
56                         else { 
57                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
58                                 else { allLines = 1;  }
59                         }
60                         
61                         output = validParameter.validFile(parameters, "output", false);                 
62                         if (output == "not found") { output = ""; }
63                         
64                         groups = validParameter.validFile(parameters, "groups", false);                 
65                         if (groups == "not found") { groups = ""; }
66                         else { 
67                                 splitAtDash(groups, Groups);
68                                 globaldata->Groups = Groups;
69                         }
70                         
71                         fastafile = validParameter.validFile(parameters, "fasta", true);
72                         if (fastafile == "not open") { abort = true; }
73                         else if (fastafile == "not found") {  fastafile = "";  }        
74                                 
75                 }
76
77         }
78         catch(exception& e) {
79                 errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84
85 void GetSharedOTUCommand::help(){
86         try {
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(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");
98         }
99         catch(exception& e) {
100                 errorOut(e, "GetSharedOTUCommand", "help");
101                 exit(1);
102         }
103 }
104
105 //**********************************************************************************************************************
106
107 GetSharedOTUCommand::~GetSharedOTUCommand(){}
108
109 //**********************************************************************************************************************
110
111 int GetSharedOTUCommand::execute(){
112         try {
113                 
114                 if (abort == true) { return 0; }
115                 
116                 groupMap = new GroupMap(groupfile);
117                 groupMap->readMap();
118                 globaldata->gGroupmap = groupMap;
119                 
120                 if (Groups.size() == 0) {
121                         Groups = groupMap->namesOfGroups;
122                 }
123                 
124                 //put groups in map to find easier
125                 for(int i = 0; i < Groups.size(); i++) {
126                         groupFinder[Groups[i]] = Groups[i];
127                 }
128                 
129                 if (fastafile != "") {
130                         ifstream inFasta;
131                         openInputFile(fastafile, inFasta);
132                         
133                         while(!inFasta.eof()) {
134                                 Sequence seq(inFasta);
135                                 seqs.push_back(seq);
136                         }
137                         inFasta.close();
138                 }
139                 
140                 ListVector* lastlist = NULL;
141                 string lastLabel = "";
142                 
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;
146                 
147                 ifstream in;
148                 openInputFile(listfile, in);
149                 
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))) {
152                         
153                         list = new ListVector(in);
154                         
155                         if(allLines == 1 || labels.count(list->getLabel()) == 1){                       
156                                 mothurOut(list->getLabel()); 
157                                 process(list);
158                                 
159                                 processedLabels.insert(list->getLabel());
160                                 userLabels.erase(list->getLabel());
161                         }
162                         
163                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
164                                         
165                                         mothurOut(lastlist->getLabel()); 
166                                         process(lastlist);
167                                         
168                                         processedLabels.insert(lastlist->getLabel());
169                                         userLabels.erase(lastlist->getLabel());
170                         }
171
172                         lastLabel = list->getLabel();
173                         
174                         if (lastlist != NULL) {         delete lastlist;        }
175                         lastlist = list;                        
176                 }
177                 
178                 in.close();
179                 
180                 //output error messages about any remaining user labels
181                 set<string>::iterator it;
182                 bool needToRun = false;
183                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
184                         mothurOut("Your file does not include the label " + *it); 
185                         if (processedLabels.count(lastLabel) != 1) {
186                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
187                                 needToRun = true;
188                         }else {
189                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
190                         }
191                 }
192                 
193                 //run last label if you need to
194                 if (needToRun == true)  {
195                                 mothurOut(lastlist->getLabel()); 
196                                 process(lastlist);
197                                         
198                                 processedLabels.insert(lastlist->getLabel());
199                                 userLabels.erase(lastlist->getLabel());
200                 }
201                 
202
203                 //reset groups parameter
204                 globaldata->Groups.clear();  
205                 
206                 if (lastlist != NULL) {         delete lastlist;        }
207                 return 0;
208         }
209
210         catch(exception& e) {
211                 errorOut(e, "GetSharedOTUCommand", "execute");
212                 exit(1);
213         }
214 }
215 /***********************************************************/
216 void GetSharedOTUCommand::process(ListVector* shared) {
217         try {
218                 
219                 map<string, string> fastaMap;
220                 
221                 ofstream outNames;
222                 string outputFileNames = getRootName(listfile) + shared->getLabel() + ".names";
223                 openOutputFile(outputFileNames, outNames);
224                 
225                 bool wroteSomething = false;
226                                 
227                 //go through each bin, find out if shared
228                 for (int i = 0; i < shared->getNumBins(); i++) {
229                         
230                         bool sharedByAll = true;
231                         
232                         map<string, int> atLeastOne;
233                         for (int m = 0; m < Groups.size(); m++) {
234                                 atLeastOne[Groups[m]] = 0;
235                         }
236                         
237                         vector<string> namesOfSeqsInThisBin;
238                         
239                         string names = shared->get(i);  
240                         while ((names.find_first_of(',') != -1) && sharedByAll) { 
241                                 string name = names.substr(0,names.find_first_of(','));
242                                 names = names.substr(names.find_first_of(',')+1, names.length());
243                                                                 
244                                 //find group
245                                 string seqGroup = groupMap->getGroup(name);
246                                 if (output != "accnos") {
247                                         namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + toString(i)));
248                                 }else {  namesOfSeqsInThisBin.push_back(name);  }
249
250                                 if (seqGroup == "not found") { mothurOut(name + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1);  }
251                                 
252                                 //is this seq in one of hte groups we care about
253                                 it = groupFinder.find(seqGroup);
254                                 if (it == groupFinder.end()) {  sharedByAll = false;  } //you have a sequence from a group you don't want
255                                 else {  atLeastOne[seqGroup]++;  }
256                         }
257                         
258                         //get last name
259                         //find group
260                         if (sharedByAll) {
261                                 string seqGroup = groupMap->getGroup(names);
262                                 if (output != "accnos") {
263                                         namesOfSeqsInThisBin.push_back((names + "|" + seqGroup + "|" + toString(i)));
264                                 }else {  namesOfSeqsInThisBin.push_back(names); }
265                                 
266                                 if (seqGroup == "not found") { mothurOut(names + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1);  }
267                         
268                                 //is this seq in one of hte groups we care about
269                                 it = groupFinder.find(seqGroup);
270                                 if (it == groupFinder.end()) {  sharedByAll = false;  } //you have a sequence from a group you don't want
271                                 else {  atLeastOne[seqGroup]++;  }
272                         }
273                         
274                         //make sure you have at least one seq from each group you want
275                         map<string, int>::iterator it2;
276                         for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
277                                 if (it2->second == 0) {  sharedByAll = false;   }
278                         }
279                         
280                         //if shared, save names of seqs in that bin
281                         if (sharedByAll) {
282                                 
283                                 wroteSomething = true;
284                                 
285                                 //output list of names 
286                                 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
287                                         outNames << namesOfSeqsInThisBin[j] << endl;
288                                         
289                                         if (fastafile != "") { 
290                                                 if (output != "accnos") {
291                                                         string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
292                                                         seqName = seqName.substr(0,seqName.find_last_of('|'));
293                                                         fastaMap[seqName] = namesOfSeqsInThisBin[j];  //fastaMap needs to contain just the seq name for output later
294                                                 }else {
295                                                         fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
296                                                 }
297                                         }
298                                 }
299                         }
300                         
301                         
302                                 
303                 }
304                 
305                 outNames.close();
306                 
307                 if (!wroteSomething) {
308                         remove(outputFileNames.c_str());
309                         string outputString = " - No otus shared by groups";
310                         
311                         string groupString = "";
312                         for (int h = 0; h < Groups.size(); h++) {
313                                 groupString += "  " + Groups[h];
314                         }
315                         
316                         outputString += groupString + ".";
317                         mothurOut(outputString); mothurOutEndLine();
318                 }else { mothurOutEndLine(); }
319                 
320                 //if fasta file provided output new fasta file
321                 if ((fastafile != "") && wroteSomething) {      
322                         string outputFileFasta = getRootName(fastafile) + shared->getLabel() + ".fasta";
323                         ofstream outFasta;
324                         openOutputFile(outputFileFasta, outFasta);
325                         
326                         for (int k = 0; k < seqs.size(); k++) {
327                                 //if this is a sequence we want, output it
328                                 it = fastaMap.find(seqs[k].getName());
329                                 if (it != fastaMap.end()) {
330                                 
331                                         if (output != "accnos") {
332                                                 outFasta << ">" << it->second << endl;
333                                         }else {
334                                                 outFasta << ">" << it->first << endl;
335                                         }
336                                         
337                                         outFasta << seqs[k].getAligned() << endl;
338                                 }
339                         }
340                         
341                         outFasta.close();
342                 }
343
344                 
345         }
346         catch(exception& e) {
347                 errorOut(e, "GetSharedOTUCommand", "process");
348                 exit(1);
349         }
350 }
351
352 //**********************************************************************************************************************