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