]> git.donarmstrong.com Git - mothur.git/blob - optionparser.cpp
fixed bug with shhh.flow from file path name in write functions, added "smart" featur...
[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
44 map<string, string> OptionParser::getParameters() {     
45         try {
46                 
47                 //loop through parameters and look for "current" so you can return the appropriate file
48                 //doing it here to avoid code duplication in each of the commands
49                 
50                 map<string, string>::iterator it;
51                 for (it = parameters.begin(); it != parameters.end();) {
52                         
53                         if (it->second == "current") {
54                                 
55                                 //look for file types
56                                 if (it->first == "fasta") {
57                                         it->second = m->getFastaFile();
58                                 }else if (it->first == "qfile") {
59                                         it->second = m->getQualFile();
60                                 }else if (it->first == "phylip") {
61                                         it->second = m->getPhylipFile();
62                                 }else if (it->first == "column") {
63                                         it->second = m->getColumnFile();
64                                 }else if (it->first == "list") {
65                                         it->second = m->getListFile();
66                                 }else if (it->first == "rabund") {
67                                         it->second = m->getRabundFile();
68                                 }else if (it->first == "sabund") {
69                                         it->second = m->getSabundFile();
70                                 }else if (it->first == "name") {
71                                         it->second = m->getNameFile();
72                                 }else if (it->first == "group") {
73                                         it->second = m->getGroupFile();
74                                 }else if (it->first == "order") {
75                                         it->second = m->getOrderFile();
76                                 }else if (it->first == "ordergroup") {
77                                         it->second = m->getOrderGroupFile();
78                                 }else if (it->first == "tree") {
79                                         it->second = m->getTreeFile();
80                                 }else if (it->first == "shared") {
81                                         it->second = m->getSharedFile();
82                                 }else if (it->first == "relabund") {
83                                         it->second = m->getRelAbundFile();
84                                 }else if (it->first == "design") {
85                                         it->second = m->getDesignFile();
86                                 }else if (it->first == "sff") {
87                                         it->second = m->getSFFFile();
88                                 }else if (it->first == "oligos") {
89                                         it->second = m->getOligosFile();
90                                 }else if (it->first == "accnos") {
91                                         it->second = m->getAccnosFile();
92                                 }else if (it->first == "taxonomy") {
93                                         it->second = m->getTaxonomyFile();
94                                 }else {
95                                         m->mothurOut("[ERROR]: mothur does not save a current file for " + it->first); m->mothurOutEndLine();
96                                 }
97                                 
98                                 if (it->second == "") { //no file was saved for that type, warn and remove from parameters
99                                         m->mothurOut("[WARNING]: no file was saved for " + it->first + " parameter."); m->mothurOutEndLine();
100                                         parameters.erase(it++);
101                                 }else {
102                                         m->mothurOut("Using " + it->second + " as input file for the " + it->first + " parameter."); m->mothurOutEndLine();
103                                         it++;
104                                 }
105                         }else{ it++; }
106                 }
107                 
108                 return parameters;      
109         }
110         catch(exception& e) {
111                 m->errorOut(e, "OptionParser", "getParameters");
112                 exit(1);
113         }
114 }
115
116 /***********************************************************************/
117 //pass a vector of filenames that may match the current namefile.  
118 //this function will look at each one, if the rootnames match, mothur will warn 
119 //the user that they may have neglected to provide a namefile.
120 //stops when it finds a match.
121 bool OptionParser::getNameFile(vector<string> files) {  
122         try {
123                 string namefile = m->getNameFile();
124                 bool match = false;
125                 
126                 if (namefile != "") {
127                         string temp = m->getRootName(m->getSimpleName(namefile));
128                         vector<string> rootName;
129                         m->splitAtChar(temp, rootName, '.');
130                         
131                         for (int i = 0; i < files.size(); i++) {
132                                 temp = m->getRootName(m->getSimpleName(files[i]));
133                                 vector<string> root;
134                                 m->splitAtChar(temp, root, '.');
135                                 
136                                 int smallest = rootName.size();
137                                 if (root.size() < smallest) { smallest = root.size(); }
138                                 
139                                 int numMatches = 0;
140                                 for(int j = 0; j < smallest; j++) {
141                                         if (root[j] == rootName[j]) { numMatches++; }
142                                 }
143                                 
144                                 if (smallest > 0) {
145                                         if ((numMatches >= (smallest-2)) && (root[0] == rootName[0])) {
146                                                 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] + ".");
147                                                 m->mothurOutEndLine();
148                                                 match = true;
149                                                 break;
150                                         }
151                                 }
152                         }
153                         
154                 }
155                 
156                 
157                 return match;
158         }
159         catch(exception& e) {
160                 m->errorOut(e, "OptionParser", "getNameFile");
161                 exit(1);
162         }
163 }
164
165                                 
166 /***********************************************************************/