]> git.donarmstrong.com Git - mothur.git/blob - splitgroupscommand.cpp
d389d8907bfaffde16b8ca9eac4dad0061341831
[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                 m->mothurOutEndLine();
188                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
189                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
190                 m->mothurOutEndLine();
191                 
192                 return 0;
193         }
194         catch(exception& e) {
195                 m->errorOut(e, "SplitGroupCommand", "execute");
196                 exit(1);
197         }
198 }
199 /**********************************************************************************************************************/
200 int SplitGroupCommand::readNames() { 
201         try {
202                 //open input file
203                 ifstream in;
204                 m->openInputFile(namefile, in);
205                 
206                 while (!in.eof()) {
207                         if (m->control_pressed) { break; }
208                         
209                         string firstCol, secondCol;
210                         in >> firstCol >> secondCol; m->gobble(in);
211                         
212                         vector<string> names;
213                         m->splitAtComma(secondCol, names);
214                         
215                         nameMap[firstCol] = names;
216                 }
217                 in.close();
218                                 
219                 return 0;
220
221         }
222         catch(exception& e) {
223                 m->errorOut(e, "SplitGroupCommand", "readNames");
224                 exit(1);
225         }
226 }
227
228 /**********************************************************************************************************************/
229 int SplitGroupCommand::splitFasta() { 
230         try {
231                 
232                 string filerootFasta =  outputDir + m->getRootName(m->getSimpleName(fastafile));
233                 string filerootName =  outputDir + m->getRootName(m->getSimpleName(namefile));
234                 ofstream* temp;
235                 ofstream* temp2;
236                 map<string, ofstream*> filehandles;
237                 map<string, ofstream*>::iterator it3;
238
239                 for (int i=0; i<Groups.size(); i++) {
240                         temp = new ofstream;
241                         filehandles[Groups[i]+"fasta"] = temp;
242                         m->openOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"]));
243                         outputNames.push_back(filerootFasta + Groups[i] + ".fasta"); outputTypes["fasta"].push_back(filerootFasta + Groups[i] + ".fasta");
244                         
245                         if (namefile != "") {
246                                 temp2 = new ofstream;
247                                 filehandles[Groups[i]+"name"] = temp2;
248                                 m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"]));
249                                 outputNames.push_back(filerootName + Groups[i] + ".names"); outputTypes["name"].push_back(filerootFasta + Groups[i] + ".names");
250                         }
251                 }
252                         
253                 //open input file
254                 ifstream in;
255                 m->openInputFile(fastafile, in);
256         
257                 while (!in.eof()) {
258                         if (m->control_pressed) { break; }
259                 
260                         Sequence seq(in); m->gobble(in);
261                                 
262                         if (seq.getName() != "") { 
263                                 
264                                 itNames = nameMap.find(seq.getName());
265                                 
266                                 if (itNames == nameMap.end()) {
267                                         if (namefile != "") {  m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine();  }
268                                         else { //no names file given
269                                                 string group = groupMap->getGroup(seq.getName());
270                                                 
271                                                 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
272                                                         seq.printSequence(*(filehandles[group+"fasta"]));
273                                                 }else if(group == "not found") {
274                                                         m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
275                                                 }
276                                         }
277                                 }else{
278                                         vector<string> names = itNames->second;
279                                         map<string, string> group2Names;
280                                         map<string, string>::iterator it;
281                                         
282                                         for (int i = 0; i < names.size(); i++) {  //build strings for new group names file, will select rep below
283                                                 string group = groupMap->getGroup(names[i]);
284                                                 
285                                                 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
286                                                         it = group2Names.find(group);
287                                                         if (it == group2Names.end()) {
288                                                                 group2Names[group] = names[i] + ",";
289                                                         }else{
290                                                                 group2Names[group] += names[i] + ",";
291                                                         }
292                                                 }else if(group == "not found") {
293                                                         m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
294                                                 }
295                                         }
296                                 
297                                         for (map<string, string>::iterator itGroups = group2Names.begin(); itGroups != group2Names.end(); itGroups++) {
298                                                 //edit names string, by grabbing the first guy as the rep and removing the last comma
299                                                 string newNames = itGroups->second;
300                                                 newNames = newNames.substr(0, newNames.length()-1); 
301                                                 string repName = "";
302                                                 
303                                                 int pos = newNames.find_first_of(',');
304                                                 if (pos == newNames.npos) { //only one sequence so it represents itself
305                                                         repName = newNames;
306                                                 }else{
307                                                         repName = newNames.substr(0, pos);
308                                                 }
309                                                 
310                                                 *(filehandles[itGroups->first+"name"]) << repName << '\t' << newNames << endl;
311                                                 *(filehandles[itGroups->first+"fasta"]) << ">" << repName << endl << seq.getAligned() << endl;
312                                         }
313                                 }
314                         }
315                 }
316                         
317                 in.close();
318                         
319                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { 
320                         (*(filehandles[it3->first])).close();
321                         delete it3->second;
322                 }
323                 
324                 vector<string> newOutputNames;
325                 //remove blank files
326                 for (int i = 0; i < outputNames.size(); i++) {
327                         if (m->isBlank(outputNames[i])) {
328                                 remove(outputNames[i].c_str());
329                         }else { newOutputNames.push_back(outputNames[i]); }
330                 }
331                 outputNames = newOutputNames;
332                                 
333                 return 0;
334
335         }
336         catch(exception& e) {
337                 m->errorOut(e, "SplitGroupCommand", "splitFasta");
338                 exit(1);
339         }
340 }
341 /**********************************************************************************************************************/
342