File: optionparser.cpp

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (205 lines) | stat: -rw-r--r-- 7,577 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 *  optionparser.cpp
 *  Mothur
 *
 *  Created by Sarah Westcott on 6/8/09.
 *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
 *
 */

#include "optionparser.h"

/***********************************************************************/

OptionParser::OptionParser(string option) {
	try {
		m = MothurOut::getInstance();
		if (option != "") {
			
			string key, value;		
			//reads in parameters and values
			while((option.find_first_of(',') != -1)) {  //while there are parameters
				m->splitAtComma(value, option);
				m->splitAtEquals(key, value);
				if ((key == "candidate") || (key == "query")) { key = "fasta"; }
				if (key == "template") { key = "reference"; }
				parameters[key] = value;
			}
			
			//in case there is no comma and to get last parameter after comma
			m->splitAtEquals(key, option);
			if ((key == "candidate") || (key == "query")) { key = "fasta"; }
			if (key == "template") { key = "reference"; }
			parameters[key] = option;
		}
	}
	catch(exception& e) {
		m->errorOut(e, "OptionParser", "OptionParser");
		exit(1);
	}
}
/***********************************************************************/

OptionParser::OptionParser(string option, map<string, string>& copy) {
	try {
		m = MothurOut::getInstance();
		if (option != "") {
			
			string key, value;		
			//reads in parameters and values
			while((option.find_first_of(',') != -1)) {  //while there are parameters
				m->splitAtComma(value, option);
				m->splitAtEquals(key, value);
				if ((key == "candidate") || (key == "query")) { key = "fasta"; }
				if (key == "template") { key = "reference"; }
				parameters[key] = value;
			}
			
			//in case there is no comma and to get last parameter after comma
			m->splitAtEquals(key, option);
			if ((key == "candidate") || (key == "query")) { key = "fasta"; }
			if (key == "template") { key = "reference"; }
			parameters[key] = option;
		}
        
        copy = parameters;
	}
	catch(exception& e) {
		m->errorOut(e, "OptionParser", "OptionParser");
		exit(1);
	}
}
/***********************************************************************/

map<string, string> OptionParser::getParameters() {	
	try {
		
		//loop through parameters and look for "current" so you can return the appropriate file
		//doing it here to avoid code duplication in each of the commands
		
       
            map<string, string>::iterator it;
            for (it = parameters.begin(); it != parameters.end();) {
                
                if (it->second == "current") {
                    
                    //look for file types
                    if (it->first == "fasta") {
                        it->second = m->getFastaFile();
                    }else if (it->first == "qfile") {
                        it->second = m->getQualFile();
                    }else if (it->first == "phylip") {
                        it->second = m->getPhylipFile();
                    }else if (it->first == "column") {
                        it->second = m->getColumnFile();
                    }else if (it->first == "list") {
                        it->second = m->getListFile();
                    }else if (it->first == "rabund") {
                        it->second = m->getRabundFile();
                    }else if (it->first == "sabund") {
                        it->second = m->getSabundFile();
                    }else if (it->first == "name") {
                        it->second = m->getNameFile();
                    }else if (it->first == "group") {
                        it->second = m->getGroupFile();
                    }else if (it->first == "order") {
                        it->second = m->getOrderFile();
                    }else if (it->first == "ordergroup") {
                        it->second = m->getOrderGroupFile();
                    }else if (it->first == "tree") {
                        it->second = m->getTreeFile();
                    }else if (it->first == "shared") {
                        it->second = m->getSharedFile();
                    }else if (it->first == "relabund") {
                        it->second = m->getRelAbundFile();
                    }else if (it->first == "design") {
                        it->second = m->getDesignFile();
                    }else if (it->first == "sff") {
                        it->second = m->getSFFFile();
                    }else if (it->first == "flow") {
                            it->second = m->getFlowFile();
                    }else if (it->first == "oligos") {
                        it->second = m->getOligosFile();
                    }else if (it->first == "accnos") {
                        it->second = m->getAccnosFile();
                    }else if (it->first == "taxonomy") {
                        it->second = m->getTaxonomyFile();
                    }else if (it->first == "biom") {
                        it->second = m->getBiomFile();
                    }else if (it->first == "count") {
                            it->second = m->getCountTableFile();
                    }else if (it->first == "summary") {
                            it->second = m->getSummaryFile();
                    }else {
                        m->mothurOut("[ERROR]: mothur does not save a current file for " + it->first); m->mothurOutEndLine();
                    }
                    
                    if (it->second == "") { //no file was saved for that type, warn and remove from parameters
                        m->mothurOut("[WARNING]: no file was saved for " + it->first + " parameter."); m->mothurOutEndLine();
                        parameters.erase(it++);
                    }else {
                        m->mothurOut("Using " + it->second + " as input file for the " + it->first + " parameter."); m->mothurOutEndLine();
                        it++;
                    }
                }else{ it++; }
            }
		
		return parameters;	
	}
	catch(exception& e) {
		m->errorOut(e, "OptionParser", "getParameters");
		exit(1);
	}
}

/***********************************************************************/
//pass a vector of filenames that may match the current namefile.  
//this function will look at each one, if the rootnames match, mothur will warn 
//the user that they may have neglected to provide a namefile.
//stops when it finds a match.
bool OptionParser::getNameFile(vector<string> files) {	
	try {
		string namefile = m->getNameFile();
		bool match = false;
		
		if ((namefile != "")&&(!m->mothurCalling)) {
			string temp = m->getRootName(m->getSimpleName(namefile));
			vector<string> rootName;
			m->splitAtChar(temp, rootName, '.');
			
			for (int i = 0; i < files.size(); i++) {
				temp = m->getRootName(m->getSimpleName(files[i]));
				vector<string> root;
				m->splitAtChar(temp, root, '.');
				
				int smallest = rootName.size();
				if (root.size() < smallest) { smallest = root.size(); }
				
				int numMatches = 0;
				for(int j = 0; j < smallest; j++) {
					if (root[j] == rootName[j]) { numMatches++; }
				}
				
				if (smallest > 0) {
					if ((numMatches >= (smallest-2)) && (root[0] == rootName[0])) {
						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] + ".");
						m->mothurOutEndLine();
						match = true;
						break;
					}
				}
			}
			
		}
		
		
		return match;
	}
	catch(exception& e) {
		m->errorOut(e, "OptionParser", "getNameFile");
		exit(1);
	}
}

				
/***********************************************************************/