]> git.donarmstrong.com Git - mothur.git/blob - splitgroupscommand.cpp
added set.current and get.current commands and modified existing commands to update...
[mothur.git] / splitgroupscommand.cpp
1 /*
2  *  splitgroupscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/20/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "splitgroupscommand.h"
11 #include "sharedutilities.h"
12
13 //**********************************************************************************************************************
14 vector<string> SplitGroupCommand::getValidParameters(){ 
15         try {
16                 string Array[] =  {"name","group","groups","fasta","outputdir","inputdir"}; 
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "SplitGroupCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 vector<string> SplitGroupCommand::getRequiredParameters(){      
27         try {
28                 string Array[] =  {"fasta","group"};
29                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
30                 return myArray;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "SplitGroupCommand", "getRequiredParameters");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> SplitGroupCommand::getRequiredFiles(){   
39         try {
40                 vector<string> myArray;
41                 return myArray;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "SplitGroupCommand", "getRequiredFiles");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 SplitGroupCommand::SplitGroupCommand(){ 
50         try {
51                 abort = true; calledHelp = true; 
52                 vector<string> tempOutNames;
53                 outputTypes["fasta"] = tempOutNames;
54                 outputTypes["name"] = tempOutNames;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62 SplitGroupCommand::SplitGroupCommand(string option)  {
63         try {
64                 abort = false; calledHelp = false;   
65                         
66                 //allow user to run help
67                 if(option == "help") { help(); abort = true; calledHelp = true; }
68                 
69                 else {
70                         //valid paramters for this command
71                         string Array[] =  {"name","group","groups","fasta","outputdir","inputdir"}; 
72                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73                         
74                         OptionParser parser(option);
75                         map<string, string> parameters = parser.getParameters();
76                         
77                         ValidParameters validParameter;
78                         map<string, string>::iterator it;
79                 
80                         //check to make sure all parameters are valid for command
81                         for (it = parameters.begin(); it != parameters.end(); it++) { 
82                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
83                         }
84                         
85                         //initialize outputTypes
86                         vector<string> tempOutNames;
87                         outputTypes["fasta"] = tempOutNames;
88                         outputTypes["name"] = tempOutNames;
89                 
90                         //if the user changes the input directory command factory will send this info to us in the output parameter 
91                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
92                         if (inputDir == "not found"){   inputDir = "";          }
93                         else {
94                                 string path;
95                                 it = parameters.find("group");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
101                                 }
102                                 
103                                 it = parameters.find("fasta");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
109                                 }
110                                 
111                                 it = parameters.find("name");
112                                 //user has given a template file
113                                 if(it != parameters.end()){ 
114                                         path = m->hasPath(it->second);
115                                         //if the user has not given a path then, add inputdir. else leave path alone.
116                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
117                                 }
118
119                         }
120
121                         
122                         namefile = validParameter.validFile(parameters, "name", true);
123                         if (namefile == "not open") { abort = true; }
124                         else if (namefile == "not found") { namefile = ""; }    
125                 
126                         fastafile = validParameter.validFile(parameters, "fasta", true);
127                         if (fastafile == "not open") { abort = true; }
128                         else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true;  }       
129                         
130                         groupfile = validParameter.validFile(parameters, "group", true);
131                         if (groupfile == "not open") {  groupfile = ""; abort = true; } 
132                         else if (groupfile == "not found") { groupfile = ""; m->mothurOut("group is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
133                         
134                         groups = validParameter.validFile(parameters, "groups", false);         
135                         if (groups == "not found") { groups = ""; }
136                         else { m->splitAtDash(groups, Groups);  }
137                                                 
138                         //if the user changes the output directory command factory will send this info to us in the output parameter 
139                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(groupfile);      }
140                 }
141
142         }
143         catch(exception& e) {
144                 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
145                 exit(1);
146         }
147 }
148 //**********************************************************************************************************************
149 void SplitGroupCommand::help(){
150         try {
151                 m->mothurOut("The split.group command reads a group file, and parses your fasta and names files by groups. \n");
152                 m->mothurOut("The split.group command parameters are fasta, name, group and groups.\n");
153                 m->mothurOut("The fasta and group parameters are required.\n");
154                 m->mothurOut("The groups parameter allows you to select groups to create files for.  \n");
155                 m->mothurOut("For example if you set groups=A-B-C, you will get a .A.fasta, .A.names, .B.fasta, .B.names, .C.fasta, .C.names files.  \n");
156                 m->mothurOut("If you want .fasta and .names files for all groups, set groups=all.  \n");
157                 m->mothurOut("The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n");
158                 m->mothurOut("Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n");
159                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
160
161         }
162         catch(exception& e) {
163                 m->errorOut(e, "SplitGroupCommand", "help");
164                 exit(1);
165         }
166 }
167 //**********************************************************************************************************************
168 SplitGroupCommand::~SplitGroupCommand(){ }
169 //**********************************************************************************************************************
170 int SplitGroupCommand::execute(){
171         try {
172         
173                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
174                 
175                 groupMap = new GroupMap(groupfile);
176                 groupMap->readMap();
177                 
178                 SharedUtil util;  util.setGroups(Groups, groupMap->namesOfGroups);  
179                 
180                 if (namefile != "") {  readNames();  }
181                 splitFasta();
182                 
183                 delete groupMap;
184                 
185                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
186                 
187                 string current = "";
188                 itTypes = outputTypes.find("fasta");
189                 if (itTypes != outputTypes.end()) {
190                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
191                 }
192                 
193                 itTypes = outputTypes.find("name");
194                 if (itTypes != outputTypes.end()) {
195                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
196                 }
197                 
198                 m->mothurOutEndLine();
199                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
200                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
201                 m->mothurOutEndLine();
202                 
203                 return 0;
204         }
205         catch(exception& e) {
206                 m->errorOut(e, "SplitGroupCommand", "execute");
207                 exit(1);
208         }
209 }
210 /**********************************************************************************************************************/
211 int SplitGroupCommand::readNames() { 
212         try {
213                 //open input file
214                 ifstream in;
215                 m->openInputFile(namefile, in);
216                 
217                 while (!in.eof()) {
218                         if (m->control_pressed) { break; }
219                         
220                         string firstCol, secondCol;
221                         in >> firstCol >> secondCol; m->gobble(in);
222                         
223                         vector<string> names;
224                         m->splitAtComma(secondCol, names);
225                         
226                         nameMap[firstCol] = names;
227                 }
228                 in.close();
229                                 
230                 return 0;
231
232         }
233         catch(exception& e) {
234                 m->errorOut(e, "SplitGroupCommand", "readNames");
235                 exit(1);
236         }
237 }
238
239 /**********************************************************************************************************************/
240 int SplitGroupCommand::splitFasta() { 
241         try {
242                 
243                 string filerootFasta =  outputDir + m->getRootName(m->getSimpleName(fastafile));
244                 string filerootName =  outputDir + m->getRootName(m->getSimpleName(namefile));
245                 ofstream* temp;
246                 ofstream* temp2;
247                 map<string, ofstream*> filehandles;
248                 map<string, ofstream*>::iterator it3;
249
250                 for (int i=0; i<Groups.size(); i++) {
251                         temp = new ofstream;
252                         filehandles[Groups[i]+"fasta"] = temp;
253                         m->openOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"]));
254                         outputNames.push_back(filerootFasta + Groups[i] + ".fasta"); outputTypes["fasta"].push_back(filerootFasta + Groups[i] + ".fasta");
255                         
256                         if (namefile != "") {
257                                 temp2 = new ofstream;
258                                 filehandles[Groups[i]+"name"] = temp2;
259                                 m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"]));
260                                 outputNames.push_back(filerootName + Groups[i] + ".names"); outputTypes["name"].push_back(filerootFasta + Groups[i] + ".names");
261                         }
262                 }
263                         
264                 //open input file
265                 ifstream in;
266                 m->openInputFile(fastafile, in);
267         
268                 while (!in.eof()) {
269                         if (m->control_pressed) { break; }
270                 
271                         Sequence seq(in); m->gobble(in);
272                                 
273                         if (seq.getName() != "") { 
274                                 
275                                 itNames = nameMap.find(seq.getName());
276                                 
277                                 if (itNames == nameMap.end()) {
278                                         if (namefile != "") {  m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine();  }
279                                         else { //no names file given
280                                                 string group = groupMap->getGroup(seq.getName());
281                                                 
282                                                 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
283                                                         seq.printSequence(*(filehandles[group+"fasta"]));
284                                                 }else if(group == "not found") {
285                                                         m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
286                                                 }
287                                         }
288                                 }else{
289                                         vector<string> names = itNames->second;
290                                         map<string, string> group2Names;
291                                         map<string, string>::iterator it;
292                                         
293                                         for (int i = 0; i < names.size(); i++) {  //build strings for new group names file, will select rep below
294                                                 string group = groupMap->getGroup(names[i]);
295                                                 
296                                                 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
297                                                         it = group2Names.find(group);
298                                                         if (it == group2Names.end()) {
299                                                                 group2Names[group] = names[i] + ",";
300                                                         }else{
301                                                                 group2Names[group] += names[i] + ",";
302                                                         }
303                                                 }else if(group == "not found") {
304                                                         m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
305                                                 }
306                                         }
307                                 
308                                         for (map<string, string>::iterator itGroups = group2Names.begin(); itGroups != group2Names.end(); itGroups++) {
309                                                 //edit names string, by grabbing the first guy as the rep and removing the last comma
310                                                 string newNames = itGroups->second;
311                                                 newNames = newNames.substr(0, newNames.length()-1); 
312                                                 string repName = "";
313                                                 
314                                                 int pos = newNames.find_first_of(',');
315                                                 if (pos == newNames.npos) { //only one sequence so it represents itself
316                                                         repName = newNames;
317                                                 }else{
318                                                         repName = newNames.substr(0, pos);
319                                                 }
320                                                 
321                                                 *(filehandles[itGroups->first+"name"]) << repName << '\t' << newNames << endl;
322                                                 *(filehandles[itGroups->first+"fasta"]) << ">" << repName << endl << seq.getAligned() << endl;
323                                         }
324                                 }
325                         }
326                 }
327                         
328                 in.close();
329                         
330                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { 
331                         (*(filehandles[it3->first])).close();
332                         delete it3->second;
333                 }
334                 
335                 vector<string> newOutputNames;
336                 //remove blank files
337                 for (int i = 0; i < outputNames.size(); i++) {
338                         if (m->isBlank(outputNames[i])) {
339                                 remove(outputNames[i].c_str());
340                         }else { newOutputNames.push_back(outputNames[i]); }
341                 }
342                 outputNames = newOutputNames;
343                                 
344                 return 0;
345
346         }
347         catch(exception& e) {
348                 m->errorOut(e, "SplitGroupCommand", "splitFasta");
349                 exit(1);
350         }
351 }
352 /**********************************************************************************************************************/
353