]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
changed how we name the make.group groups file, and filter file in filter.seqs, finis...
[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 MakeGroupCommand::MakeGroupCommand(string option)  {
16         try {
17                 
18                 abort = false;
19         
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; }
22                 
23                 else {
24                         
25                         //valid paramters for this command
26                         string AlignArray[] =  {"fasta","groups","outputdir","inputdir"};
27                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28                         
29                         OptionParser parser(option);
30                         map<string, string> parameters = parser.getParameters(); 
31                         
32                         ValidParameters validParameter;
33                         map<string, string>::iterator it;
34                         
35                         //check to make sure all parameters are valid for command
36                         for (it = parameters.begin(); it != parameters.end(); it++) { 
37                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
38                         }
39
40                         //if the user changes the input directory command factory will send this info to us in the output parameter 
41                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
42                         if (inputDir == "not found"){   inputDir = "";          }
43                         
44                         filename = outputDir;
45
46                         fastaFileName = validParameter.validFile(parameters, "fasta", false);
47                         if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
48                         else { 
49                                 m->splitAtDash(fastaFileName, fastaFileNames);
50                                 
51                                 //go through files and make sure they are good, if not, then disregard them
52                                 for (int i = 0; i < fastaFileNames.size(); i++) {
53                                         if (inputDir != "") {
54                                                 string path = m->hasPath(fastaFileNames[i]);
55                                                 //if the user has not given a path then, add inputdir. else leave path alone.
56                                                 if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
57                                         }
58         
59                                         ifstream in;
60                                         int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
61                                 
62                                         //if you can't open it, try default location
63                                         if (ableToOpen == 1) {
64                                                 if (m->getDefaultPath() != "") { //default path is set
65                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
66                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
67                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
68                                                         fastaFileNames[i] = tryPath;
69                                                 }
70                                         }
71                                         in.close();
72                                         
73                                         if (ableToOpen == 1) { 
74                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
75                                                 //erase from file list
76                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
77                                                 i--;
78                                         }else{  filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));  }
79                                 }
80                                 
81                                 //prevent giantic file name
82                                 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
83                                 else {  filename += "groups";  }
84                                 
85                                 //make sure there is at least one valid file left
86                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
87                         }
88                         
89                         //if the user changes the output directory command factory will send this info to us in the output parameter 
90                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
91                         
92                         groups = validParameter.validFile(parameters, "groups", false);                 
93                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
94                         else { m->splitAtDash(groups, groupsNames);     }
95
96                         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;  }
97                 }
98                 
99         }
100         catch(exception& e) {
101                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
102                 exit(1);
103         }
104 }
105
106 //**********************************************************************************************************************
107
108 MakeGroupCommand::~MakeGroupCommand(){  }
109
110 //**********************************************************************************************************************
111
112 void MakeGroupCommand::help(){
113         try {
114                 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
115                 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
116                 m->mothurOut("The make.group command should be in the following format: \n");
117                 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
118                 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
119                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
120         }
121         catch(exception& e) {
122                 m->errorOut(e, "MakeGroupCommand", "help");
123                 exit(1);
124         }
125 }
126
127
128 //**********************************************************************************************************************
129
130 int MakeGroupCommand::execute(){
131         try {
132                 if (abort == true) {    return 0;       }
133                 
134                 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
135                 
136                 filename = outputDir + filename;
137                 
138                 ofstream out;
139                 m->openOutputFile(filename, out);
140                 
141                 for (int i = 0; i < fastaFileNames.size(); i++) {
142                 
143                         if (m->control_pressed) {  out.close(); remove(filename.c_str()); return 0; }
144                         
145                         ifstream in;
146                         m->openInputFile(fastaFileNames[i], in);
147                         
148                         while (!in.eof()) {
149                                 
150                                 Sequence seq(in, "no align"); m->gobble(in);
151                                 
152                                 if (m->control_pressed) {  in.close(); out.close(); remove(filename.c_str()); return 0; }
153                                 
154                                 if (seq.getName() != "") {      out << seq.getName() << '\t' << groupsNames[i] << endl;         }
155                         }
156                         in.close();
157                 }
158                 
159                 out.close();
160                 
161                 m->mothurOutEndLine();
162                 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine();
163                 m->mothurOutEndLine();
164
165                 return 0;
166         }
167         catch(exception& e) {
168                 m->errorOut(e, "MakeGroupCommand", "execute");
169                 exit(1);
170         }
171 }
172 //**********************************************************************************************************************
173
174