]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
working on testing for 1.13
[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                                 filename += "groups";
82                                 
83                                 //make sure there is at least one valid file left
84                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
85                         }
86                         
87                         //if the user changes the output directory command factory will send this info to us in the output parameter 
88                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
89                         
90                         groups = validParameter.validFile(parameters, "groups", false);                 
91                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
92                         else { m->splitAtDash(groups, groupsNames);     }
93
94                         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;  }
95                 }
96                 
97         }
98         catch(exception& e) {
99                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
100                 exit(1);
101         }
102 }
103
104 //**********************************************************************************************************************
105
106 MakeGroupCommand::~MakeGroupCommand(){  }
107
108 //**********************************************************************************************************************
109
110 void MakeGroupCommand::help(){
111         try {
112                 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
113                 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
114                 m->mothurOut("The make.group command should be in the following format: \n");
115                 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
116                 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
117                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
118         }
119         catch(exception& e) {
120                 m->errorOut(e, "MakeGroupCommand", "help");
121                 exit(1);
122         }
123 }
124
125
126 //**********************************************************************************************************************
127
128 int MakeGroupCommand::execute(){
129         try {
130                 if (abort == true) {    return 0;       }
131                 
132                 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
133                 
134                 filename = outputDir + filename;
135                 
136                 ofstream out;
137                 m->openOutputFile(filename, out);
138                 
139                 for (int i = 0; i < fastaFileNames.size(); i++) {
140                 
141                         if (m->control_pressed) {  out.close(); remove(filename.c_str()); return 0; }
142                         
143                         ifstream in;
144                         m->openInputFile(fastaFileNames[i], in);
145                         
146                         while (!in.eof()) {
147                                 
148                                 Sequence seq(in, "no align"); m->gobble(in);
149                                 
150                                 if (m->control_pressed) {  in.close(); out.close(); remove(filename.c_str()); return 0; }
151                                 
152                                 if (seq.getName() != "") {      out << seq.getName() << '\t' << groupsNames[i] << endl;         }
153                         }
154                         in.close();
155                 }
156                 
157                 out.close();
158                 
159                 m->mothurOutEndLine();
160                 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine();
161                 m->mothurOutEndLine();
162
163                 return 0;
164         }
165         catch(exception& e) {
166                 m->errorOut(e, "MakeGroupCommand", "execute");
167                 exit(1);
168         }
169 }
170 //**********************************************************************************************************************
171
172