]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
added make.group command
[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                                 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 = 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                                         int ableToOpen;
64                                         ifstream in;
65                                         
66                                         ableToOpen = openInputFile(fastaFileNames[i], in);
67                                         in.close();
68
69                                         if (ableToOpen == 1) { 
70                                                 //erase from file list
71                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
72                                                 i--;
73                                         }else{  filename += getRootName(getSimpleName(fastaFileNames[i]));  }
74                                         
75                                 }
76                                 
77                                 filename += "groups";
78                                 
79                                 //make sure there is at least one valid file left
80                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
81                         }
82                         
83                         groups = validParameter.validFile(parameters, "groups", false);                 
84                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
85                         else { splitAtDash(groups, groupsNames);        }
86
87                         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;  }
88                 }
89                 
90         }
91         catch(exception& e) {
92                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
93                 exit(1);
94         }
95 }
96
97 //**********************************************************************************************************************
98
99 MakeGroupCommand::~MakeGroupCommand(){  }
100
101 //**********************************************************************************************************************
102
103 void MakeGroupCommand::help(){
104         try {
105                 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
106                 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
107                 m->mothurOut("The make.group command should be in the following format: \n");
108                 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
109                 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
110                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
111         }
112         catch(exception& e) {
113                 m->errorOut(e, "MakeGroupCommand", "help");
114                 exit(1);
115         }
116 }
117
118
119 //**********************************************************************************************************************
120
121 int MakeGroupCommand::execute(){
122         try {
123                 if (abort == true) {    return 0;       }
124                 
125                 ofstream out;
126                 openOutputFile(filename, out);
127                 
128                 for (int i = 0; i < fastaFileNames.size(); i++) {
129                 
130                         ifstream in;
131                         openInputFile(fastaFileNames[i], in);
132                         
133                         while (!in.eof()) {
134                                 
135                                 Sequence seq(in); gobble(in);
136                                 
137                                 if (seq.getName() != "") {      out << seq.getName() << '\t' << groupsNames[i] << endl;         }
138                         }
139                         in.close();
140                 }
141                 
142                 out.close();
143                 
144                 m->mothurOutEndLine();
145                 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine();
146                 m->mothurOutEndLine();
147
148                 return 0;
149         }
150         catch(exception& e) {
151                 m->errorOut(e, "MakeGroupCommand", "execute");
152                 exit(1);
153         }
154 }
155 //**********************************************************************************************************************
156
157