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