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