]> git.donarmstrong.com Git - mothur.git/blob - getsharedotucommand.cpp
added groups option to read.otu, added qtrim option to trim.seqs, fixed bug in get...
[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(list=amazon.fn.list, label=unique-0.01, group=forest-pasture, fasta=amazon.fasta, output=accnos).\n");
96                 mothurOut("The output to the screen is the distance and the number of otus at that distance for the groups you specified.\n");
97                 mothurOut("The default value for label is all labels in your inputfile. The default for groups is all groups in your file.\n");
98                 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
99         }
100         catch(exception& e) {
101                 errorOut(e, "GetSharedOTUCommand", "help");
102                 exit(1);
103         }
104 }
105
106 //**********************************************************************************************************************
107
108 GetSharedOTUCommand::~GetSharedOTUCommand(){}
109
110 //**********************************************************************************************************************
111
112 int GetSharedOTUCommand::execute(){
113         try {
114                 
115                 if (abort == true) { return 0; }
116                 
117                 groupMap = new GroupMap(groupfile);
118                 groupMap->readMap();
119                 globaldata->gGroupmap = groupMap;
120                 
121                 if (Groups.size() == 0) {
122                         Groups = groupMap->namesOfGroups;
123                 }
124                 
125                 //put groups in map to find easier
126                 for(int i = 0; i < Groups.size(); i++) {
127                         groupFinder[Groups[i]] = Groups[i];
128                 }
129                 
130                 if (fastafile != "") {
131                         ifstream inFasta;
132                         openInputFile(fastafile, inFasta);
133                         
134                         while(!inFasta.eof()) {
135                                 Sequence seq(inFasta);
136                                 seqs.push_back(seq);
137                         }
138                         inFasta.close();
139                 }
140                 
141                 ListVector* lastlist = NULL;
142                 string lastLabel = "";
143                 
144                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
145                 set<string> processedLabels;
146                 set<string> userLabels = labels;
147                 
148                 ifstream in;
149                 openInputFile(listfile, in);
150                 
151                 //as long as you are not at the end of the file or done wih the lines you want
152                 while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
153                         
154                         list = new ListVector(in);
155                         
156                         if(allLines == 1 || labels.count(list->getLabel()) == 1){                       
157                                 mothurOut(list->getLabel()); 
158                                 process(list);
159                                 
160                                 processedLabels.insert(list->getLabel());
161                                 userLabels.erase(list->getLabel());
162                         }
163                         
164                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
165                                         string saveLabel = list->getLabel();
166                                         
167                                         mothurOut(lastlist->getLabel()); 
168                                         process(lastlist);
169                                         
170                                         processedLabels.insert(lastlist->getLabel());
171                                         userLabels.erase(lastlist->getLabel());
172                                         
173                                         //restore real lastlabel to save below
174                                         list->setLabel(saveLabel);
175                         }
176
177                         lastLabel = list->getLabel();
178                         
179                         if (lastlist != NULL) {         delete lastlist;        }
180                         lastlist = list;                        
181                 }
182                 
183                 in.close();
184                 
185                 //output error messages about any remaining user labels
186                 set<string>::iterator it;
187                 bool needToRun = false;
188                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
189                         mothurOut("Your file does not include the label " + *it); 
190                         if (processedLabels.count(lastLabel) != 1) {
191                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
192                                 needToRun = true;
193                         }else {
194                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
195                         }
196                 }
197                 
198                 //run last label if you need to
199                 if (needToRun == true)  {
200                                 mothurOut(lastlist->getLabel()); 
201                                 process(lastlist);
202                                         
203                                 processedLabels.insert(lastlist->getLabel());
204                                 userLabels.erase(lastlist->getLabel());
205                 }
206                 
207
208                 //reset groups parameter
209                 globaldata->Groups.clear();  
210                 
211                 if (lastlist != NULL) {         delete lastlist;        }
212                 return 0;
213         }
214
215         catch(exception& e) {
216                 errorOut(e, "GetSharedOTUCommand", "execute");
217                 exit(1);
218         }
219 }
220 /***********************************************************/
221 void GetSharedOTUCommand::process(ListVector* shared) {
222         try {
223                 
224                 map<string, string> fastaMap;
225                 
226                 ofstream outNames;
227                 string outputFileNames = getRootName(listfile) + shared->getLabel() + ".names";
228                 openOutputFile(outputFileNames, outNames);
229                 
230                 bool wroteSomething = false;
231                 int num = 0;
232                                 
233                 //go through each bin, find out if shared
234                 for (int i = 0; i < shared->getNumBins(); i++) {
235                         
236                         bool sharedByAll = true;
237                         
238                         map<string, int> atLeastOne;
239                         for (int m = 0; m < Groups.size(); m++) {
240                                 atLeastOne[Groups[m]] = 0;
241                         }
242                         
243                         vector<string> namesOfSeqsInThisBin;
244                         
245                         string names = shared->get(i);  
246                         while ((names.find_first_of(',') != -1) && sharedByAll) { 
247                                 string name = names.substr(0,names.find_first_of(','));
248                                 names = names.substr(names.find_first_of(',')+1, names.length());
249                                                                 
250                                 //find group
251                                 string seqGroup = groupMap->getGroup(name);
252                                 if (output != "accnos") {
253                                         namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + toString(i+1)));
254                                 }else {  namesOfSeqsInThisBin.push_back(name);  }
255
256                                 if (seqGroup == "not found") { mothurOut(name + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1);  }
257                                 
258                                 //is this seq in one of hte groups we care about
259                                 it = groupFinder.find(seqGroup);
260                                 if (it == groupFinder.end()) {  sharedByAll = false;  } //you have a sequence from a group you don't want
261                                 else {  atLeastOne[seqGroup]++;  }
262                         }
263                         
264                         //get last name
265                         //find group
266                         if (sharedByAll) {
267                                 string seqGroup = groupMap->getGroup(names);
268                                 if (output != "accnos") {
269                                         namesOfSeqsInThisBin.push_back((names + "|" + seqGroup + "|" + toString(i+1)));
270                                 }else {  namesOfSeqsInThisBin.push_back(names); }
271                                 
272                                 if (seqGroup == "not found") { mothurOut(names + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1);  }
273                         
274                                 //is this seq in one of hte groups we care about
275                                 it = groupFinder.find(seqGroup);
276                                 if (it == groupFinder.end()) {  sharedByAll = false;  } //you have a sequence from a group you don't want
277                                 else {  atLeastOne[seqGroup]++;  }
278                         }
279                         
280                         //make sure you have at least one seq from each group you want
281                         map<string, int>::iterator it2;
282                         for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
283                                 if (it2->second == 0) {  sharedByAll = false;   }
284                         }
285                         
286                         //if shared, save names of seqs in that bin
287                         if (sharedByAll) {
288                                 
289                                 wroteSomething = true;
290                                 num++;
291                                 
292                                 //output list of names 
293                                 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
294                                         outNames << namesOfSeqsInThisBin[j] << endl;
295                                         
296                                         if (fastafile != "") { 
297                                                 if (output != "accnos") {
298                                                         string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
299                                                         seqName = seqName.substr(0,seqName.find_last_of('|'));
300                                                         fastaMap[seqName] = namesOfSeqsInThisBin[j];  //fastaMap needs to contain just the seq name for output later
301                                                 }else {
302                                                         fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
303                                                 }
304                                         }
305                                 }
306                         }
307                         
308                         
309                                 
310                 }
311                 
312                 outNames.close();
313                 
314                 if (!wroteSomething) {
315                         remove(outputFileNames.c_str());
316                         string outputString = "\t" + toString(num) + " - No otus shared by groups";
317                         
318                         string groupString = "";
319                         for (int h = 0; h < Groups.size(); h++) {
320                                 groupString += "  " + Groups[h];
321                         }
322                         
323                         outputString += groupString + ".";
324                         mothurOut(outputString); mothurOutEndLine();
325                 }else { mothurOut("\t" + toString(num)); mothurOutEndLine(); }
326                 
327                 //if fasta file provided output new fasta file
328                 if ((fastafile != "") && wroteSomething) {      
329                         string outputFileFasta = getRootName(fastafile) + shared->getLabel() + ".fasta";
330                         ofstream outFasta;
331                         openOutputFile(outputFileFasta, outFasta);
332                         
333                         for (int k = 0; k < seqs.size(); k++) {
334                                 //if this is a sequence we want, output it
335                                 it = fastaMap.find(seqs[k].getName());
336                                 if (it != fastaMap.end()) {
337                                 
338                                         if (output != "accnos") {
339                                                 outFasta << ">" << it->second << endl;
340                                         }else {
341                                                 outFasta << ">" << it->first << endl;
342                                         }
343                                         
344                                         outFasta << seqs[k].getAligned() << endl;
345                                 }
346                         }
347                         
348                         outFasta.close();
349                 }
350
351                 
352         }
353         catch(exception& e) {
354                 errorOut(e, "GetSharedOTUCommand", "process");
355                 exit(1);
356         }
357 }
358
359 //**********************************************************************************************************************