]> git.donarmstrong.com Git - mothur.git/blob - optionparser.cpp
changing command name classify.shared to classifyrf.shared
[mothur.git] / optionparser.cpp
1 /*
2  *  optionparser.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "optionparser.h"
11
12 /***********************************************************************/
13
14 OptionParser::OptionParser(string option) {
15         try {
16                 m = MothurOut::getInstance();
17                 if (option != "") {
18                         
19                         string key, value;              
20                         //reads in parameters and values
21                         while((option.find_first_of(',') != -1)) {  //while there are parameters
22                                 m->splitAtComma(value, option);
23                                 m->splitAtEquals(key, value);
24                                 if ((key == "candidate") || (key == "query")) { key = "fasta"; }
25                                 if (key == "template") { key = "reference"; }
26                                 parameters[key] = value;
27                         }
28                         
29                         //in case there is no comma and to get last parameter after comma
30                         m->splitAtEquals(key, option);
31                         if ((key == "candidate") || (key == "query")) { key = "fasta"; }
32                         if (key == "template") { key = "reference"; }
33                         parameters[key] = option;
34                 }
35         }
36         catch(exception& e) {
37                 m->errorOut(e, "OptionParser", "OptionParser");
38                 exit(1);
39         }
40 }
41 /***********************************************************************/
42
43 OptionParser::OptionParser(string option, map<string, string>& copy) {
44         try {
45                 m = MothurOut::getInstance();
46                 if (option != "") {
47                         
48                         string key, value;              
49                         //reads in parameters and values
50                         while((option.find_first_of(',') != -1)) {  //while there are parameters
51                                 m->splitAtComma(value, option);
52                                 m->splitAtEquals(key, value);
53                                 if ((key == "candidate") || (key == "query")) { key = "fasta"; }
54                                 if (key == "template") { key = "reference"; }
55                                 parameters[key] = value;
56                         }
57                         
58                         //in case there is no comma and to get last parameter after comma
59                         m->splitAtEquals(key, option);
60                         if ((key == "candidate") || (key == "query")) { key = "fasta"; }
61                         if (key == "template") { key = "reference"; }
62                         parameters[key] = option;
63                 }
64         
65         copy = parameters;
66         }
67         catch(exception& e) {
68                 m->errorOut(e, "OptionParser", "OptionParser");
69                 exit(1);
70         }
71 }
72 /***********************************************************************/
73
74 map<string, string> OptionParser::getParameters() {     
75         try {
76                 
77                 //loop through parameters and look for "current" so you can return the appropriate file
78                 //doing it here to avoid code duplication in each of the commands
79                 
80        
81             map<string, string>::iterator it;
82             for (it = parameters.begin(); it != parameters.end();) {
83                 
84                 if (it->second == "current") {
85                     
86                     //look for file types
87                     if (it->first == "fasta") {
88                         it->second = m->getFastaFile();
89                     }else if (it->first == "qfile") {
90                         it->second = m->getQualFile();
91                     }else if (it->first == "phylip") {
92                         it->second = m->getPhylipFile();
93                     }else if (it->first == "column") {
94                         it->second = m->getColumnFile();
95                     }else if (it->first == "list") {
96                         it->second = m->getListFile();
97                     }else if (it->first == "rabund") {
98                         it->second = m->getRabundFile();
99                     }else if (it->first == "sabund") {
100                         it->second = m->getSabundFile();
101                     }else if (it->first == "name") {
102                         it->second = m->getNameFile();
103                     }else if (it->first == "group") {
104                         it->second = m->getGroupFile();
105                     }else if (it->first == "order") {
106                         it->second = m->getOrderFile();
107                     }else if (it->first == "ordergroup") {
108                         it->second = m->getOrderGroupFile();
109                     }else if (it->first == "tree") {
110                         it->second = m->getTreeFile();
111                     }else if (it->first == "shared") {
112                         it->second = m->getSharedFile();
113                     }else if (it->first == "relabund") {
114                         it->second = m->getRelAbundFile();
115                     }else if (it->first == "design") {
116                         it->second = m->getDesignFile();
117                     }else if (it->first == "sff") {
118                         it->second = m->getSFFFile();
119                     }else if (it->first == "flow") {
120                             it->second = m->getFlowFile();
121                     }else if (it->first == "oligos") {
122                         it->second = m->getOligosFile();
123                     }else if (it->first == "accnos") {
124                         it->second = m->getAccnosFile();
125                     }else if (it->first == "taxonomy") {
126                         it->second = m->getTaxonomyFile();
127                     }else if (it->first == "biom") {
128                         it->second = m->getBiomFile();
129                     }else if (it->first == "count") {
130                             it->second = m->getCountTableFile();
131                     }else if (it->first == "summary") {
132                             it->second = m->getSummaryFile();
133                     }else {
134                         m->mothurOut("[ERROR]: mothur does not save a current file for " + it->first); m->mothurOutEndLine();
135                     }
136                     
137                     if (it->second == "") { //no file was saved for that type, warn and remove from parameters
138                         m->mothurOut("[WARNING]: no file was saved for " + it->first + " parameter."); m->mothurOutEndLine();
139                         parameters.erase(it++);
140                     }else {
141                         m->mothurOut("Using " + it->second + " as input file for the " + it->first + " parameter."); m->mothurOutEndLine();
142                         it++;
143                     }
144                 }else{ it++; }
145             }
146                 
147                 return parameters;      
148         }
149         catch(exception& e) {
150                 m->errorOut(e, "OptionParser", "getParameters");
151                 exit(1);
152         }
153 }
154
155 /***********************************************************************/
156 //pass a vector of filenames that may match the current namefile.  
157 //this function will look at each one, if the rootnames match, mothur will warn 
158 //the user that they may have neglected to provide a namefile.
159 //stops when it finds a match.
160 bool OptionParser::getNameFile(vector<string> files) {  
161         try {
162                 string namefile = m->getNameFile();
163                 bool match = false;
164                 
165                 if ((namefile != "")&&(!m->mothurCalling)) {
166                         string temp = m->getRootName(m->getSimpleName(namefile));
167                         vector<string> rootName;
168                         m->splitAtChar(temp, rootName, '.');
169                         
170                         for (int i = 0; i < files.size(); i++) {
171                                 temp = m->getRootName(m->getSimpleName(files[i]));
172                                 vector<string> root;
173                                 m->splitAtChar(temp, root, '.');
174                                 
175                                 int smallest = rootName.size();
176                                 if (root.size() < smallest) { smallest = root.size(); }
177                                 
178                                 int numMatches = 0;
179                                 for(int j = 0; j < smallest; j++) {
180                                         if (root[j] == rootName[j]) { numMatches++; }
181                                 }
182                                 
183                                 if (smallest > 0) {
184                                         if ((numMatches >= (smallest-2)) && (root[0] == rootName[0])) {
185                                                 m->mothurOut("[WARNING]: This command can take a namefile and you did not provide one. The current namefile is " + namefile + " which seems to match " + files[i] + ".");
186                                                 m->mothurOutEndLine();
187                                                 match = true;
188                                                 break;
189                                         }
190                                 }
191                         }
192                         
193                 }
194                 
195                 
196                 return match;
197         }
198         catch(exception& e) {
199                 m->errorOut(e, "OptionParser", "getNameFile");
200                 exit(1);
201         }
202 }
203
204                                 
205 /***********************************************************************/