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