]> git.donarmstrong.com Git - mothur.git/blob - optionparser.cpp
Revert to previous commit
[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 if (it->first == "biom") {
95                         it->second = m->getBiomFile();
96                                 }else {
97                                         m->mothurOut("[ERROR]: mothur does not save a current file for " + it->first); m->mothurOutEndLine();
98                                 }
99                                 
100                                 if (it->second == "") { //no file was saved for that type, warn and remove from parameters
101                                         m->mothurOut("[WARNING]: no file was saved for " + it->first + " parameter."); m->mothurOutEndLine();
102                                         parameters.erase(it++);
103                                 }else {
104                                         m->mothurOut("Using " + it->second + " as input file for the " + it->first + " parameter."); m->mothurOutEndLine();
105                                         it++;
106                                 }
107                         }else{ it++; }
108                 }
109                 
110                 return parameters;      
111         }
112         catch(exception& e) {
113                 m->errorOut(e, "OptionParser", "getParameters");
114                 exit(1);
115         }
116 }
117
118 /***********************************************************************/
119 //pass a vector of filenames that may match the current namefile.  
120 //this function will look at each one, if the rootnames match, mothur will warn 
121 //the user that they may have neglected to provide a namefile.
122 //stops when it finds a match.
123 bool OptionParser::getNameFile(vector<string> files) {  
124         try {
125                 string namefile = m->getNameFile();
126                 bool match = false;
127                 
128                 if ((namefile != "")&&(!m->mothurCalling)) {
129                         string temp = m->getRootName(m->getSimpleName(namefile));
130                         vector<string> rootName;
131                         m->splitAtChar(temp, rootName, '.');
132                         
133                         for (int i = 0; i < files.size(); i++) {
134                                 temp = m->getRootName(m->getSimpleName(files[i]));
135                                 vector<string> root;
136                                 m->splitAtChar(temp, root, '.');
137                                 
138                                 int smallest = rootName.size();
139                                 if (root.size() < smallest) { smallest = root.size(); }
140                                 
141                                 int numMatches = 0;
142                                 for(int j = 0; j < smallest; j++) {
143                                         if (root[j] == rootName[j]) { numMatches++; }
144                                 }
145                                 
146                                 if (smallest > 0) {
147                                         if ((numMatches >= (smallest-2)) && (root[0] == rootName[0])) {
148                                                 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] + ".");
149                                                 m->mothurOutEndLine();
150                                                 match = true;
151                                                 break;
152                                         }
153                                 }
154                         }
155                         
156                 }
157                 
158                 
159                 return match;
160         }
161         catch(exception& e) {
162                 m->errorOut(e, "OptionParser", "getNameFile");
163                 exit(1);
164         }
165 }
166
167                                 
168 /***********************************************************************/