]> git.donarmstrong.com Git - mothur.git/blob - splitgroupscommand.cpp
af3ca665b5791d0001141f27192b78aab171d71a
[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 #include "sequenceparser.h"
13
14 //**********************************************************************************************************************
15 vector<string> SplitGroupCommand::setParameters(){      
16         try {           
17                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
18                 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
19                 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
20                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
21                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "SplitGroupCommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string SplitGroupCommand::getHelpString(){      
35         try {
36                 string helpString = "";
37                 helpString += "The split.group command reads a group file, and parses your fasta and names files by groups. \n";
38                 helpString += "The split.group command parameters are fasta, name, group and groups.\n";
39                 helpString += "The fasta and group parameters are required.\n";
40                 helpString += "The groups parameter allows you to select groups to create files for.  \n";
41                 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";
42                 helpString += "If you want .fasta and .names files for all groups, set groups=all.  \n";
43                 helpString += "The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n";
44                 helpString += "Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n";
45                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
46                 return helpString;
47         }
48         catch(exception& e) {
49                 m->errorOut(e, "SplitGroupCommand", "getHelpString");
50                 exit(1);
51         }
52 }
53 //**********************************************************************************************************************
54 string SplitGroupCommand::getOutputFileNameTag(string type, string inputName=""){       
55         try {
56         string outputFileName = "";
57                 map<string, vector<string> >::iterator it;
58         
59         //is this a type this command creates
60         it = outputTypes.find(type);
61         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
62         else {
63             if (type == "fasta")            {   outputFileName =  "fasta";   }
64             else if (type == "name")        {   outputFileName =  "names";   }
65             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
66         }
67         return outputFileName;
68         }
69         catch(exception& e) {
70                 m->errorOut(e, "SplitGroupCommand", "getOutputFileNameTag");
71                 exit(1);
72         }
73 }
74 //**********************************************************************************************************************
75 SplitGroupCommand::SplitGroupCommand(){ 
76         try {
77                 abort = true; calledHelp = true; 
78                 setParameters();
79                 vector<string> tempOutNames;
80                 outputTypes["fasta"] = tempOutNames;
81                 outputTypes["name"] = tempOutNames;
82         }
83         catch(exception& e) {
84                 m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
85                 exit(1);
86         }
87 }
88 //**********************************************************************************************************************
89 SplitGroupCommand::SplitGroupCommand(string option)  {
90         try {
91                 abort = false; calledHelp = false;   
92                         
93                 //allow user to run help
94                 if(option == "help") { help(); abort = true; calledHelp = true; }
95                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
96                 
97                 else {
98                         vector<string> myArray = setParameters();
99                         
100                         OptionParser parser(option);
101                         map<string, string> parameters = parser.getParameters();
102                         
103                         ValidParameters validParameter;
104                         map<string, string>::iterator it;
105                 
106                         //check to make sure all parameters are valid for command
107                         for (it = parameters.begin(); it != parameters.end(); it++) { 
108                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
109                         }
110                         
111                         //initialize outputTypes
112                         vector<string> tempOutNames;
113                         outputTypes["fasta"] = tempOutNames;
114                         outputTypes["name"] = tempOutNames;
115                 
116                         //if the user changes the input directory command factory will send this info to us in the output parameter 
117                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
118                         if (inputDir == "not found"){   inputDir = "";          }
119                         else {
120                                 string path;
121                                 it = parameters.find("group");
122                                 //user has given a template file
123                                 if(it != parameters.end()){ 
124                                         path = m->hasPath(it->second);
125                                         //if the user has not given a path then, add inputdir. else leave path alone.
126                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
127                                 }
128                                 
129                                 it = parameters.find("fasta");
130                                 //user has given a template file
131                                 if(it != parameters.end()){ 
132                                         path = m->hasPath(it->second);
133                                         //if the user has not given a path then, add inputdir. else leave path alone.
134                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
135                                 }
136                                 
137                                 it = parameters.find("name");
138                                 //user has given a template file
139                                 if(it != parameters.end()){ 
140                                         path = m->hasPath(it->second);
141                                         //if the user has not given a path then, add inputdir. else leave path alone.
142                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
143                                 }
144
145                         }
146
147                         
148                         namefile = validParameter.validFile(parameters, "name", true);
149                         if (namefile == "not open") { namefile = ""; abort = true; }
150                         else if (namefile == "not found") { namefile = ""; }    
151                         else { m->setNameFile(namefile); }
152                 
153                         fastafile = validParameter.validFile(parameters, "fasta", true);
154                         if (fastafile == "not open") { abort = true; }
155                         else if (fastafile == "not found") {                    
156                                 fastafile = m->getFastaFile(); 
157                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
158                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
159                         }else { m->setFastaFile(fastafile); }   
160                         
161                         groupfile = validParameter.validFile(parameters, "group", true);
162                         if (groupfile == "not open") {  groupfile = ""; abort = true; } 
163                         else if (groupfile == "not found") {                    
164                                 groupfile = m->getGroupFile(); 
165                                 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
166                                 else {  m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
167                         }else {  m->setGroupFile(groupfile); }
168                         
169                         groups = validParameter.validFile(parameters, "groups", false);         
170                         if (groups == "not found") { groups = ""; }
171                         else { m->splitAtDash(groups, Groups);  }
172                                                 
173                         //if the user changes the output directory command factory will send this info to us in the output parameter 
174                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(groupfile);      }
175                         
176                         if (namefile == "") {
177                                 vector<string> files; files.push_back(fastafile);
178                                 parser.getNameFile(files);
179                         }
180                 }
181
182         }
183         catch(exception& e) {
184                 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
185                 exit(1);
186         }
187 }
188 //**********************************************************************************************************************
189 int SplitGroupCommand::execute(){
190         try {
191         
192                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
193                 
194                 SequenceParser* parser;
195                 
196                 if (namefile == "") {   parser = new SequenceParser(groupfile, fastafile);                              }
197                 else                            {       parser = new SequenceParser(groupfile, fastafile, namefile);    }
198                 
199                 if (m->control_pressed) { delete parser; return 0; }
200
201                 vector<string> namesGroups = parser->getNamesOfGroups();
202                 SharedUtil util;  util.setGroups(Groups, namesGroups);  
203                 
204                 string fastafileRoot = outputDir + m->getRootName(m->getSimpleName(fastafile));
205                 string namefileRoot = outputDir + m->getRootName(m->getSimpleName(namefile));
206                 
207                 m->mothurOutEndLine();
208                 for (int i = 0; i < Groups.size(); i++) {
209                         
210                         m->mothurOut("Processing group: " + Groups[i]); m->mothurOutEndLine();
211                         
212                         string newFasta = fastafileRoot + Groups[i] + "." + getOutputFileNameTag("fasta");
213                         string newName = namefileRoot + Groups[i] + "." + getOutputFileNameTag("name");
214                         
215                         parser->getSeqs(Groups[i], newFasta, false);
216                         outputNames.push_back(newFasta); outputTypes["fasta"].push_back(newFasta);
217                         if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]);        } return 0; }
218
219                         if (namefile != "") { 
220                                 parser->getNameMap(Groups[i], newName); 
221                                 outputNames.push_back(newName); outputTypes["name"].push_back(newName);
222                         }
223                         
224                         if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]);        } return 0; }
225                 }
226                 
227                 delete parser;
228                 
229                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);        } return 0; }
230                 
231                 string current = "";
232                 itTypes = outputTypes.find("fasta");
233                 if (itTypes != outputTypes.end()) {
234                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
235                 }
236                 
237                 itTypes = outputTypes.find("name");
238                 if (itTypes != outputTypes.end()) {
239                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
240                 }
241                 
242                 m->mothurOutEndLine();
243                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
244                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
245                 m->mothurOutEndLine();
246                 
247                 return 0;
248         }
249         catch(exception& e) {
250                 m->errorOut(e, "SplitGroupCommand", "execute");
251                 exit(1);
252         }
253 }
254 //**********************************************************************************************************************
255
256