]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
6f640eb0732274bf6970c346dc9d6b9de553b1b4
[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\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                                         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                         output = validParameter.validFile(parameters, "output", false);                 
159                         if (output == "not found") { output = "";  }
160                         else{ filename = output; }
161                         
162                         groups = validParameter.validFile(parameters, "groups", false);                 
163                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
164                         else { m->splitAtDash(groups, groupsNames);     }
165
166                         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;  }
167                 }
168         }
169         catch(exception& e) {
170                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
171                 exit(1);
172         }
173 }
174 //**********************************************************************************************************************
175
176 int MakeGroupCommand::execute(){
177         try {
178                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
179                 
180                 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
181                         
182                 filename = outputDir + filename;
183                 
184                 ofstream out;
185                 m->openOutputFile(filename, out);
186                 
187                 for (int i = 0; i < fastaFileNames.size(); i++) {
188                 
189                         if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); return 0; }
190                         
191                         ifstream in;
192                         m->openInputFile(fastaFileNames[i], in);
193                         
194                         while (!in.eof()) {
195                                 
196                                 Sequence seq(in, "no align"); m->gobble(in);
197                                 
198                                 if (m->control_pressed) { outputTypes.clear();  in.close(); out.close(); remove(filename.c_str()); return 0; }
199                                 
200                                 if (seq.getName() != "") {      out << seq.getName() << '\t' << groupsNames[i] << endl;         }
201                         }
202                         in.close();
203                 }
204                 
205                 out.close();
206                 
207                 m->mothurOutEndLine();
208                 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename); 
209                 m->mothurOutEndLine();
210                 
211                 //set group file as new current groupfile
212                 string current = "";
213                 itTypes = outputTypes.find("group");
214                 if (itTypes != outputTypes.end()) {
215                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
216                 }
217                 
218                 return 0;
219         }
220         catch(exception& e) {
221                 m->errorOut(e, "MakeGroupCommand", "execute");
222                 exit(1);
223         }
224 }
225 //**********************************************************************************************************************
226
227