]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
fixes while testing
[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 vector<string> MakeGroupCommand::getValidParameters(){  
15         try {
16                 string Array[] =  {"fasta", "groups","outputdir","inputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "MakeGroupCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 MakeGroupCommand::MakeGroupCommand(){   
27         try {
28                 abort = true;
29                 //initialize outputTypes
30                 vector<string> tempOutNames;
31                 outputTypes["group"] = tempOutNames;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 vector<string> MakeGroupCommand::getRequiredParameters(){       
40         try {
41                 string Array[] =  {"fasta","groups"};
42                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "MakeGroupCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> MakeGroupCommand::getRequiredFiles(){    
52         try {
53                 vector<string> myArray;
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "MakeGroupCommand", "getRequiredFiles");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62
63 MakeGroupCommand::MakeGroupCommand(string option)  {
64         try {
65                 
66                 abort = false;
67         
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; }
70                 
71                 else {
72                         
73                         //valid paramters for this command
74                         string AlignArray[] =  {"fasta","groups","outputdir","inputdir"};
75                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
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                         filename = outputDir;
97
98                         fastaFileName = validParameter.validFile(parameters, "fasta", false);
99                         if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the make.group command."); 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                                         if (inputDir != "") {
106                                                 string path = m->hasPath(fastaFileNames[i]);
107                                                 //if the user has not given a path then, add inputdir. else leave path alone.
108                                                 if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
109                                         }
110         
111                                         ifstream in;
112                                         int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
113                                 
114                                         //if you can't open it, try default location
115                                         if (ableToOpen == 1) {
116                                                 if (m->getDefaultPath() != "") { //default path is set
117                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
118                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
119                                                         ifstream in2;
120                                                         ableToOpen = m->openInputFile(tryPath, in2, "noerror");
121                                                         in2.close();
122                                                         fastaFileNames[i] = tryPath;
123                                                 }
124                                         }
125                                         
126                                         //if you can't open it, try default location
127                                         if (ableToOpen == 1) {
128                                                 if (m->getOutputDir() != "") { //default path is set
129                                                         string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
130                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
131                                                         ifstream in2;
132                                                         ableToOpen = m->openInputFile(tryPath, in2, "noerror");
133                                                         in2.close();
134                                                         fastaFileNames[i] = tryPath;
135                                                 }
136                                         }
137                                         in.close();
138                                         
139                                         if (ableToOpen == 1) { 
140                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
141                                                 //erase from file list
142                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
143                                                 i--;
144                                         }else{  filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));  }
145                                 }
146                                 
147                                 //prevent giantic file name
148                                 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
149                                 else {  filename += "groups";  }
150                                 
151                                 //make sure there is at least one valid file left
152                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
153                         }
154                         
155                         //if the user changes the output directory command factory will send this info to us in the output parameter 
156                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
157                         
158                         groups = validParameter.validFile(parameters, "groups", false);                 
159                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
160                         else { m->splitAtDash(groups, groupsNames);     }
161
162                         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;  }
163                 }
164                 
165         }
166         catch(exception& e) {
167                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
168                 exit(1);
169         }
170 }
171
172 //**********************************************************************************************************************
173
174 MakeGroupCommand::~MakeGroupCommand(){  }
175
176 //**********************************************************************************************************************
177
178 void MakeGroupCommand::help(){
179         try {
180                 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
181                 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
182                 m->mothurOut("The make.group command should be in the following format: \n");
183                 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
184                 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
185                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
186         }
187         catch(exception& e) {
188                 m->errorOut(e, "MakeGroupCommand", "help");
189                 exit(1);
190         }
191 }
192
193
194 //**********************************************************************************************************************
195
196 int MakeGroupCommand::execute(){
197         try {
198                 if (abort == true) {    return 0;       }
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(); remove(filename.c_str()); 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(); remove(filename.c_str()); 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                 return 0;
232         }
233         catch(exception& e) {
234                 m->errorOut(e, "MakeGroupCommand", "execute");
235                 exit(1);
236         }
237 }
238 //**********************************************************************************************************************
239
240