]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
7ca4aa64d28f9ec23bcc93c37e42c753ef6a8137
[mothur.git] / makegroupcommand.cpp
1 /*
2  *  makegroupcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/7/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "makegroupcommand.h"
11 #include "sequence.hpp"
12
13
14 //**********************************************************************************************************************
15 vector<string> MakeGroupCommand::setParameters(){       
16         try {
17                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
18                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
19                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "MakeGroupCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string MakeGroupCommand::getHelpString(){       
33         try {
34                 string helpString = "";
35                 helpString += "The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n";
36                 helpString += "The make.group command parameters are fasta, groups and output. Fasta and group are required.\n";
37                 helpString += "The output parameter allows you to specify the name of groupfile created. \n";
38                 helpString += "The make.group command should be in the following format: \n";
39                 helpString += "make.group(fasta=yourFastaFiles, groups=yourGroups). \n";
40                 helpString += "Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n";
41                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n";
42                 return helpString;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "MakeGroupCommand", "getHelpString");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 MakeGroupCommand::MakeGroupCommand(){   
51         try {
52                 abort = true; calledHelp = true; 
53                 setParameters();
54                 vector<string> tempOutNames;
55                 outputTypes["group"] = tempOutNames;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
59                 exit(1);
60         }
61 }
62
63 //**********************************************************************************************************************
64
65 MakeGroupCommand::MakeGroupCommand(string option)  {
66         try {
67                 
68                 abort = false; calledHelp = false;   
69         
70                 //allow user to run help
71                 if(option == "help") { help(); abort = true; calledHelp = true; }
72                 
73                 else {
74                         vector<string> myArray = setParameters();
75                         
76                         OptionParser parser(option);
77                         map<string, string> parameters = parser.getParameters(); 
78                         
79                         ValidParameters validParameter;
80                         map<string, string>::iterator it;
81                         
82                         //check to make sure all parameters are valid for command
83                         for (it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["group"] = tempOutNames;
90                 
91                         //if the user changes the input directory command factory will send this info to us in the output parameter 
92                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
93                         if (inputDir == "not found"){   inputDir = "";          }
94
95                         fastaFileName = validParameter.validFile(parameters, "fasta", false);
96                         if (fastaFileName == "not found") {                             //if there is a current fasta file, use it
97                                 string filename = m->getFastaFile(); 
98                                 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
99                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
100                         }else { 
101                                 m->splitAtDash(fastaFileName, fastaFileNames);
102                                 
103                                 //go through files and make sure they are good, if not, then disregard them
104                                 for (int i = 0; i < fastaFileNames.size(); i++) {
105                                         
106                                         bool ignore = false;
107                                         if (fastaFileNames[i] == "current") { 
108                                                 fastaFileNames[i] = m->getFastaFile(); 
109                                                 if (fastaFileNames[i] != "") {  
110                                                         m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); 
111                                                         filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));
112                                                 }
113                                                 else {  
114                                                         m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
115                                                         //erase from file list
116                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
117                                                         i--;
118                                                 }
119                                         }
120                                         
121                                         if (!ignore) {
122                                                 if (inputDir != "") {
123                                                         string path = m->hasPath(fastaFileNames[i]);
124                                                         //if the user has not given a path then, add inputdir. else leave path alone.
125                                                         if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
126                                                 }
127                 
128                                                 ifstream in;
129                                                 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
130                                         
131                                                 //if you can't open it, try default location
132                                                 if (ableToOpen == 1) {
133                                                         if (m->getDefaultPath() != "") { //default path is set
134                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
135                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
136                                                                 ifstream in2;
137                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
138                                                                 in2.close();
139                                                                 fastaFileNames[i] = tryPath;
140                                                         }
141                                                 }
142                                                 
143                                                 //if you can't open it, try default location
144                                                 if (ableToOpen == 1) {
145                                                         if (m->getOutputDir() != "") { //default path is set
146                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
147                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
148                                                                 ifstream in2;
149                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
150                                                                 in2.close();
151                                                                 fastaFileNames[i] = tryPath;
152                                                         }
153                                                 }
154                                                 in.close();
155                                                 
156                                                 if (ableToOpen == 1) { 
157                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
158                                                         //erase from file list
159                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
160                                                         i--;
161                                                 }else{  filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));  }
162                                         }
163                                 }
164                                 
165                                 //prevent giantic file name
166                                 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
167                                 else {  filename += "groups";  }
168                                 
169                                 //make sure there is at least one valid file left
170                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
171                         }
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 = "";         }
175                         
176                         output = validParameter.validFile(parameters, "output", false);                 
177                         if (output == "not found") { output = "";  }
178                         else{ filename = output; }
179                         
180                         groups = validParameter.validFile(parameters, "groups", false);                 
181                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
182                         else { m->splitAtDash(groups, groupsNames);     }
183
184                         if (groupsNames.size() != fastaFileNames.size()) { m->mothurOut("You do not have the same number of valid fastfile files as groups.  This could be because we could not open a fastafile."); m->mothurOutEndLine(); abort = true;  }
185                 }
186         }
187         catch(exception& e) {
188                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
189                 exit(1);
190         }
191 }
192 //**********************************************************************************************************************
193
194 int MakeGroupCommand::execute(){
195         try {
196                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
197                 
198                 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
199                         
200                 filename = outputDir + filename;
201                 
202                 ofstream out;
203                 m->openOutputFile(filename, out);
204                 
205                 for (int i = 0; i < fastaFileNames.size(); i++) {
206                 
207                         if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); return 0; }
208                         
209                         ifstream in;
210                         m->openInputFile(fastaFileNames[i], in);
211                         
212                         while (!in.eof()) {
213                                 
214                                 Sequence seq(in, "no align"); m->gobble(in);
215                                 
216                                 if (m->control_pressed) { outputTypes.clear();  in.close(); out.close(); remove(filename.c_str()); return 0; }
217                                 
218                                 if (seq.getName() != "") {      out << seq.getName() << '\t' << groupsNames[i] << endl;         }
219                         }
220                         in.close();
221                 }
222                 
223                 out.close();
224                 
225                 m->mothurOutEndLine();
226                 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename); 
227                 m->mothurOutEndLine();
228                 
229                 //set group file as new current groupfile
230                 string current = "";
231                 itTypes = outputTypes.find("group");
232                 if (itTypes != outputTypes.end()) {
233                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
234                 }
235                 
236                 return 0;
237         }
238         catch(exception& e) {
239                 m->errorOut(e, "MakeGroupCommand", "execute");
240                 exit(1);
241         }
242 }
243 //**********************************************************************************************************************
244
245