]> git.donarmstrong.com Git - mothur.git/blob - source/splitgroupscommand.cpp
fixing
[mothur.git] / source / 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 SplitGroupCommand::SplitGroupCommand(){ 
55         try {
56                 abort = true; calledHelp = true; 
57                 setParameters();
58                 vector<string> tempOutNames;
59                 outputTypes["fasta"] = tempOutNames;
60                 outputTypes["name"] = tempOutNames;
61         }
62         catch(exception& e) {
63                 m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
64                 exit(1);
65         }
66 }
67 //**********************************************************************************************************************
68 SplitGroupCommand::SplitGroupCommand(string option)  {
69         try {
70                 abort = false; calledHelp = false;   
71                         
72                 //allow user to run help
73                 if(option == "help") { help(); abort = true; calledHelp = true; }
74                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
75                 
76                 else {
77                         vector<string> myArray = setParameters();
78                         
79                         OptionParser parser(option);
80                         map<string, string> parameters = parser.getParameters();
81                         
82                         ValidParameters validParameter;
83                         map<string, string>::iterator it;
84                 
85                         //check to make sure all parameters are valid for command
86                         for (it = parameters.begin(); it != parameters.end(); it++) { 
87                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
88                         }
89                         
90                         //initialize outputTypes
91                         vector<string> tempOutNames;
92                         outputTypes["fasta"] = tempOutNames;
93                         outputTypes["name"] = tempOutNames;
94                 
95                         //if the user changes the input directory command factory will send this info to us in the output parameter 
96                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
97                         if (inputDir == "not found"){   inputDir = "";          }
98                         else {
99                                 string path;
100                                 it = parameters.find("group");
101                                 //user has given a template file
102                                 if(it != parameters.end()){ 
103                                         path = m->hasPath(it->second);
104                                         //if the user has not given a path then, add inputdir. else leave path alone.
105                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
106                                 }
107                                 
108                                 it = parameters.find("fasta");
109                                 //user has given a template file
110                                 if(it != parameters.end()){ 
111                                         path = m->hasPath(it->second);
112                                         //if the user has not given a path then, add inputdir. else leave path alone.
113                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
114                                 }
115                                 
116                                 it = parameters.find("name");
117                                 //user has given a template file
118                                 if(it != parameters.end()){ 
119                                         path = m->hasPath(it->second);
120                                         //if the user has not given a path then, add inputdir. else leave path alone.
121                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
122                                 }
123
124                         }
125
126                         
127                         namefile = validParameter.validFile(parameters, "name", true);
128                         if (namefile == "not open") { namefile = ""; abort = true; }
129                         else if (namefile == "not found") { namefile = ""; }    
130                         else { m->setNameFile(namefile); }
131                 
132                         fastafile = validParameter.validFile(parameters, "fasta", true);
133                         if (fastafile == "not open") { abort = true; }
134                         else if (fastafile == "not found") {                    
135                                 fastafile = m->getFastaFile(); 
136                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
137                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
138                         }else { m->setFastaFile(fastafile); }   
139                         
140                         groupfile = validParameter.validFile(parameters, "group", true);
141                         if (groupfile == "not open") {  groupfile = ""; abort = true; } 
142                         else if (groupfile == "not found") {                    
143                                 groupfile = m->getGroupFile(); 
144                                 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
145                                 else {  m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
146                         }else {  m->setGroupFile(groupfile); }
147                         
148                         groups = validParameter.validFile(parameters, "groups", false);         
149                         if (groups == "not found") { groups = ""; }
150                         else { m->splitAtDash(groups, Groups);  }
151                                                 
152                         //if the user changes the output directory command factory will send this info to us in the output parameter 
153                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(groupfile);      }
154                         
155                         if (namefile == "") {
156                                 vector<string> files; files.push_back(fastafile);
157                                 parser.getNameFile(files);
158                         }
159                 }
160
161         }
162         catch(exception& e) {
163                 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
164                 exit(1);
165         }
166 }
167 //**********************************************************************************************************************
168 int SplitGroupCommand::execute(){
169         try {
170         
171                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
172                 
173                 SequenceParser* parser;
174                 
175                 if (namefile == "") {   parser = new SequenceParser(groupfile, fastafile);                              }
176                 else                            {       parser = new SequenceParser(groupfile, fastafile, namefile);    }
177                 
178                 if (m->control_pressed) { delete parser; return 0; }
179
180                 vector<string> namesGroups = parser->getNamesOfGroups();
181                 SharedUtil util;  util.setGroups(Groups, namesGroups);  
182                 
183                 string fastafileRoot = outputDir + m->getRootName(m->getSimpleName(fastafile));
184                 string namefileRoot = outputDir + m->getRootName(m->getSimpleName(namefile));
185                 
186                 m->mothurOutEndLine();
187                 for (int i = 0; i < Groups.size(); i++) {
188                         
189                         m->mothurOut("Processing group: " + Groups[i]); m->mothurOutEndLine();
190                         
191                         string newFasta = fastafileRoot + Groups[i] + ".fasta";
192                         string newName = namefileRoot + Groups[i] + ".names";
193                         
194                         parser->getSeqs(Groups[i], newFasta, false);
195                         outputNames.push_back(newFasta); outputTypes["fasta"].push_back(newFasta);
196                         if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]);        } return 0; }
197
198                         if (namefile != "") { 
199                                 parser->getNameMap(Groups[i], newName); 
200                                 outputNames.push_back(newName); outputTypes["name"].push_back(newName);
201                         }
202                         
203                         if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]);        } return 0; }
204                 }
205                 
206                 delete parser;
207                 
208                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);        } return 0; }
209                 
210                 string current = "";
211                 itTypes = outputTypes.find("fasta");
212                 if (itTypes != outputTypes.end()) {
213                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
214                 }
215                 
216                 itTypes = outputTypes.find("name");
217                 if (itTypes != outputTypes.end()) {
218                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
219                 }
220                 
221                 m->mothurOutEndLine();
222                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
223                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
224                 m->mothurOutEndLine();
225                 
226                 return 0;
227         }
228         catch(exception& e) {
229                 m->errorOut(e, "SplitGroupCommand", "execute");
230                 exit(1);
231         }
232 }
233 //**********************************************************************************************************************
234
235