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