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