5 * Created by Pat Schloss on 7/6/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "sensspeccommand.h"
12 //***************************************************************************************************************
14 SensSpecCommand::SensSpecCommand(string option) {
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string AlignArray[] = {"list", "phylip", "column", "name", "hard", "label", "cutoff", "outputdir", "inputdir"};
28 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
30 OptionParser parser(option);
31 map<string,string> parameters = parser.getParameters();
33 ValidParameters validParameter;
34 map<string,string>::iterator it;
36 //check to make sure all parameters are valid for command
37 for (it = parameters.begin(); it != parameters.end(); it++) {
38 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
41 //if the user changes the input directory command factory will send this info to us in the output parameter
42 string inputDir = validParameter.validFile(parameters, "inputdir", false);
43 if (inputDir == "not found"){ inputDir = ""; }
46 it = parameters.find("list");
47 //user has given a template file
48 if(it != parameters.end()){
49 path = hasPath(it->second);
50 //if the user has not given a path then, add inputdir. else leave path alone.
51 if (path == "") { parameters["list"] = inputDir + it->second; }
54 it = parameters.find("phylip");
55 //user has given a template file
56 if(it != parameters.end()){
57 path = hasPath(it->second);
58 //if the user has not given a path then, add inputdir. else leave path alone.
59 if (path == "") { parameters["phylip"] = inputDir + it->second; }
62 it = parameters.find("column");
63 //user has given a template file
64 if(it != parameters.end()){
65 path = hasPath(it->second);
66 //if the user has not given a path then, add inputdir. else leave path alone.
67 if (path == "") { parameters["column"] = inputDir + it->second; }
70 it = parameters.find("name");
71 //user has given a template file
72 if(it != parameters.end()){
73 path = hasPath(it->second);
74 //if the user has not given a path then, add inputdir. else leave path alone.
75 if (path == "") { parameters["name"] = inputDir + it->second; }
79 //check for required parameters
80 listFile = validParameter.validFile(parameters, "list", true);
81 if (listFile == "not found") { m->mothurOut("list is a required parameter for the sens.spec command."); m->mothurOutEndLine(); abort = true; }
82 else if (listFile == "not open") { abort = true; }
84 distFile = validParameter.validFile(parameters, "column", true);
86 if(distFile == "not found") {
87 distFile = validParameter.validFile(parameters, "phylip", true);
90 if(distFile == "not found") { m->mothurOut("either column or phylip are required for the sens.spec command."); m->mothurOutEndLine(); abort = true; }
91 else if (distFile == "not open") { abort = true; }
93 //if the user changes the output directory command factory will send this info to us in the output parameter
94 outputDir = validParameter.validFile(parameters, "outputdir", false);
95 if (outputDir == "not found"){
97 outputDir += hasPath(listFile); //if user entered a file with a path then preserve it
100 //check for optional parameter and set defaults
101 // ...at some point should added some additional type checking...
102 temp = validParameter.validFile(parameters, "hard", false);
103 if (temp == "not found"){ hard = 0; }
104 else if(!isTrue(temp)) { hard = 0; }
105 else if(isTrue(temp)) { hard = 1; }
107 // temp = validParameter.validFile(parameters, "name", true);
108 // if (temp == "not found") { nameFile = ""; }
109 // else if(temp == "not open") { abort = true; }
110 // else { nameFile = temp; }
111 // cout << "name:\t" << nameFile << endl;
113 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = ""; }
114 convert(temp, cutoff);
115 cout << "cutoff:\t" << cutoff << endl;
119 lineLabel = validParameter.validFile(parameters, "label", false); if (lineLabel == "not found") { lineLabel = ""; }
124 catch(exception& e) {
125 m->errorOut(e, "SensSpecCommand", "SensSpecCommand");
129 //**********************************************************************************************************************
131 void SensSpecCommand::help(){
133 m->mothurOut("The sens.spec command reads a fastaFile and creates .....\n");
137 m->mothurOut("Example sens.spec(...).\n");
138 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
139 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.seqs .\n\n");
142 catch(exception& e) {
143 m->errorOut(e, "SensSpecCommand", "help");
149 //***************************************************************************************************************
151 SensSpecCommand::~SensSpecCommand(){ /* do nothing */ }
153 //***************************************************************************************************************
155 int SensSpecCommand::execute(){
157 if (abort == true) { return 0; }
159 if(format == "phylip") { processPhylip(); }
160 // else if(format == "column") { processColumn(seqMap); }
164 // map<string, int> seqMap;
170 catch(exception& e) {
171 m->errorOut(e, "SensSpecCommand", "execute");
176 //***************************************************************************************************************
178 void SensSpecCommand::processPhylip(){
180 ifstream inputListFile;
181 openInputFile(listFile, inputListFile);
186 map<string, int> seqMap;
189 while(inputListFile){
190 inputListFile >> label >> numOTUs;
191 for(int i=0;i<numOTUs;i++){
192 inputListFile >> seqList;
193 int seqListLength = seqList.length();
195 for(int j=0;j<seqListLength;j++){
197 if(seqList[j] == ','){
202 seqName += seqList[j];
209 inputListFile.close();
211 int lNumSeqs = seqMap.size();
215 openInputFile(distFile, phylipFile);
216 phylipFile >> pNumSeqs;
217 if(pNumSeqs != lNumSeqs){ cout << "numSeq mismatch!" << endl; }
221 vector<int> otuIndices(lNumSeqs, -1);
229 for(int i=0;i<lNumSeqs;i++){
230 phylipFile >> seqName;
231 otuIndices[i] = seqMap[seqName];
233 for(int j=0;j<i;j++){
234 phylipFile >> distance;
235 if(distance <= cutoff){
236 if(otuIndices[i] == otuIndices[j]){
244 if(otuIndices[i] == otuIndices[j]){
255 cout << "truePositives:\t" << truePositives << endl;
256 cout << "trueNegatives:\t" << trueNegatives << endl;
257 cout << "falsePositives:\t" << falsePositives << endl;
258 cout << "falseNegatives:\t" << falseNegatives << endl;
260 catch(exception& e) {
261 m->errorOut(e, "SensSpecCommand", "processPhylip");
266 //***************************************************************************************************************
268 //void SensSpecCommand::processColumn(map<string, int> seqMap){
270 // truePositives = 0;
271 // falsePositives = 0;
272 // trueNegatives = numDists;
273 // falseNegatives = 0;
275 // ifstream columnFile;
276 // openInputFile(distFile, columnFile);
278 // string seqNameA, seqNameB, oldSeqNameA;
279 // int otuA, otuB, oldOTUA;
282 // while(columnFile){
283 // columnFile >> seqNameA >> seqNameB >> distance;
285 // if(seqNameA == oldSeqNameA) { otuA = oldOTUA; }
286 // else { otuA = seqMap[seqNameA]; oldOTUA = otuA; }
288 // otuB = seqMap[seqNameB];
290 // if(distance <= cutoff){
306 // gobble(columnFile);
308 // columnFile.close();
310 // cout << "truePositives:\t" << truePositives << endl;
311 // cout << "trueNegatives:\t" << trueNegatives << endl;
312 // cout << "falsePositives:\t" << falsePositives << endl;
313 // cout << "falseNegatives:\t" << falseNegatives << endl;
316 //***************************************************************************************************************