1 /*
2  *  GetlabelCommand.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 1/30/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9 
10 #include "getlabelcommand.h"
11 
12 
13 //**********************************************************************************************************************
setParameters()14 vector<string> GetlabelCommand::setParameters(){
15 	try {
16 		CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false, true); parameters.push_back(plist);
17 		CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false, true); parameters.push_back(prabund);
18 		CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false, true); parameters.push_back(psabund);
19 		CommandParameter pseed("seed", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pseed);
20         CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
21 		CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
22 
23         abort = false; calledHelp = false;
24 
25 		vector<string> myArray;
26 		for (int i = 0; i < parameters.size(); i++) {	myArray.push_back(parameters[i].name);		}
27 		return myArray;
28 	}
29 	catch(exception& e) {
30 		m->errorOut(e, "GetlabelCommand", "setParameters");
31 		exit(1);
32 	}
33 }
34 //**********************************************************************************************************************
getHelpString()35 string GetlabelCommand::getHelpString(){
36 	try {
37 		string helpString = "";
38 		helpString += "The get.label command parameters are list, sabund and rabund file. \n";
39 		helpString += "The get.label command should be in the following format: \n";
40 		helpString += "get.label()\n";
41 		helpString += "Example get.label().\n";
42 		return helpString;
43 	}
44 	catch(exception& e) {
45 		m->errorOut(e, "GetlabelCommand", "getHelpString");
46 		exit(1);
47 	}
48 }
49 //**********************************************************************************************************************
50 
GetlabelCommand(string option)51 GetlabelCommand::GetlabelCommand(string option) : Command()  {
52 	try {
53 
54 		//allow user to run help
55 		if(option == "help") { help(); abort = true; calledHelp = true; }
56 		else if(option == "citation") { citation(); abort = true; calledHelp = true;}
57         else if(option == "category") {  abort = true; calledHelp = true;  }
58 
59 		else {
60 			OptionParser parser(option, setParameters());
61 			map<string,string> parameters = parser.getParameters();
62 
63 			ValidParameters validParameter;
64 			listfile = validParameter.validFile(parameters, "list");
65 			if (listfile == "not open") { listfile = ""; abort = true; }
66 			else if (listfile == "not found") { listfile = ""; }
67 			else {  format = "list"; inputfile = listfile; current->setListFile(listfile); }
68 
69 			sabundfile = validParameter.validFile(parameters, "sabund");
70 			if (sabundfile == "not open") { sabundfile = ""; abort = true; }
71 			else if (sabundfile == "not found") { sabundfile = ""; }
72 			else {  format = "sabund"; inputfile = sabundfile; current->setSabundFile(sabundfile); }
73 
74 			rabundfile = validParameter.validFile(parameters, "rabund");
75 			if (rabundfile == "not open") { rabundfile = ""; abort = true; }
76 			else if (rabundfile == "not found") { rabundfile = ""; }
77 			else {  format = "rabund"; inputfile = rabundfile; current->setRabundFile(rabundfile); }
78 
79 			if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
80 				//is there are current file available for any of these?
81 				//give priority to list, then rabund, then sabund
82 				//if there is a current shared file, use it
83 
84 				listfile = current->getListFile();
85 				if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter.\n");  }
86 				else {
87 					rabundfile = current->getRabundFile();
88 					if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter.\n");  }
89 					else {
90 						sabundfile = current->getSabundFile();
91 						if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter.\n");  }
92 						else {
93 							m->mothurOut("No valid current files. You must provide a list, sabund or rabund file.\n");
94 							abort = true;
95 						}
96 					}
97 				}
98 			}
99 		}
100 
101 	}
102 	catch(exception& e) {
103 		m->errorOut(e, "GetlabelCommand", "GetlabelCommand");
104 		exit(1);
105 	}
106 }
107 //**********************************************************************************************************************
108 
execute()109 int GetlabelCommand::execute(){
110 	try {
111 
112 		if (abort) { if (calledHelp) { return 0; }  return 2;	}
113 
114 		InputData* input = new InputData(inputfile, format, nullVector);
115 		OrderVector* order = input->getOrderVector();
116 		string label = order->getLabel();
117 
118 		while (order != NULL) {
119 
120 			if (m->getControl_pressed()) { delete input;  delete order; return 0; }
121 
122 			label = order->getLabel();
123 
124 			m->mothurOut(label); m->mothurOutEndLine();
125 
126 			delete order;
127 			order = input->getOrderVector();
128 		}
129 
130 		delete input;
131 
132 		return 0;
133 	}
134 
135 	catch(exception& e) {
136 		m->errorOut(e, "GetlabelCommand", "execute");
137 		exit(1);
138 	}
139 }
140 //**********************************************************************************************************************
141 
142 
143