]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
rewrote metastats command in c++, added mothurRemove function to handle ~ error....
[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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73                 
74                 else {
75                         vector<string> myArray = setParameters();
76                         
77                         OptionParser parser(option);
78                         map<string, string> parameters = parser.getParameters(); 
79                         
80                         ValidParameters validParameter;
81                         map<string, string>::iterator it;
82                         
83                         //check to make sure all parameters are valid for command
84                         for (it = parameters.begin(); it != parameters.end(); it++) { 
85                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
86                         }
87                         
88                         //initialize outputTypes
89                         vector<string> tempOutNames;
90                         outputTypes["group"] = tempOutNames;
91                 
92                         //if the user changes the input directory command factory will send this info to us in the output parameter 
93                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
94                         if (inputDir == "not found"){   inputDir = "";          }
95
96                         fastaFileName = validParameter.validFile(parameters, "fasta", false);
97                         if (fastaFileName == "not found") {                             //if there is a current fasta file, use it
98                                 string filename = m->getFastaFile(); 
99                                 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
100                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
101                         }else { 
102                                 m->splitAtDash(fastaFileName, fastaFileNames);
103                                 
104                                 //go through files and make sure they are good, if not, then disregard them
105                                 for (int i = 0; i < fastaFileNames.size(); i++) {
106                                         
107                                         bool ignore = false;
108                                         if (fastaFileNames[i] == "current") { 
109                                                 fastaFileNames[i] = m->getFastaFile(); 
110                                                 if (fastaFileNames[i] != "") {  
111                                                         m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); 
112                                                         filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));
113                                                 }
114                                                 else {  
115                                                         m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
116                                                         //erase from file list
117                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
118                                                         i--;
119                                                 }
120                                         }
121                                         
122                                         if (!ignore) {
123                                                 if (inputDir != "") {
124                                                         string path = m->hasPath(fastaFileNames[i]);
125                                                         //if the user has not given a path then, add inputdir. else leave path alone.
126                                                         if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
127                                                 }
128                 
129                                                 ifstream in;
130                                                 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
131                                         
132                                                 //if you can't open it, try default location
133                                                 if (ableToOpen == 1) {
134                                                         if (m->getDefaultPath() != "") { //default path is set
135                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
136                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
137                                                                 ifstream in2;
138                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
139                                                                 in2.close();
140                                                                 fastaFileNames[i] = tryPath;
141                                                         }
142                                                 }
143                                                 
144                                                 //if you can't open it, try default location
145                                                 if (ableToOpen == 1) {
146                                                         if (m->getOutputDir() != "") { //default path is set
147                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
148                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
149                                                                 ifstream in2;
150                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
151                                                                 in2.close();
152                                                                 fastaFileNames[i] = tryPath;
153                                                         }
154                                                 }
155                                                 in.close();
156                                                 
157                                                 if (ableToOpen == 1) { 
158                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
159                                                         //erase from file list
160                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
161                                                         i--;
162                                                 }else{  filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));  m->setFastaFile(fastaFileNames[i]); }
163                                         }
164                                 }
165                                 
166                                 //prevent giantic file name
167                                 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
168                                 else {  filename += "groups";  }
169                                 
170                                 //make sure there is at least one valid file left
171                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
172                         }
173                         
174                         //if the user changes the output directory command factory will send this info to us in the output parameter 
175                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
176                         
177                         output = validParameter.validFile(parameters, "output", false);                 
178                         if (output == "not found") { output = "";  }
179                         else{ filename = output; }
180                         
181                         groups = validParameter.validFile(parameters, "groups", false);                 
182                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
183                         else { m->splitAtDash(groups, groupsNames);     }
184
185                         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;  }
186                 }
187         }
188         catch(exception& e) {
189                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
190                 exit(1);
191         }
192 }
193 //**********************************************************************************************************************
194
195 int MakeGroupCommand::execute(){
196         try {
197                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
198                 
199                 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
200                         
201                 filename = outputDir + filename;
202                 
203                 ofstream out;
204                 m->openOutputFile(filename, out);
205                 
206                 for (int i = 0; i < fastaFileNames.size(); i++) {
207                 
208                         if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); return 0; }
209                         
210                         ifstream in;
211                         m->openInputFile(fastaFileNames[i], in);
212                         
213                         while (!in.eof()) {
214                                 
215                                 Sequence seq(in, "no align"); m->gobble(in);
216                                 
217                                 if (m->control_pressed) { outputTypes.clear();  in.close(); out.close(); m->mothurRemove(filename); return 0; }
218                                 
219                                 if (seq.getName() != "") {      out << seq.getName() << '\t' << groupsNames[i] << endl;         }
220                         }
221                         in.close();
222                 }
223                 
224                 out.close();
225                 
226                 m->mothurOutEndLine();
227                 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename); 
228                 m->mothurOutEndLine();
229                 
230                 //set group file as new current groupfile
231                 string current = "";
232                 itTypes = outputTypes.find("group");
233                 if (itTypes != outputTypes.end()) {
234                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
235                 }
236                 
237                 return 0;
238         }
239         catch(exception& e) {
240                 m->errorOut(e, "MakeGroupCommand", "execute");
241                 exit(1);
242         }
243 }
244 //**********************************************************************************************************************
245
246