5 * Created by Sarah Westcott on 1/20/09.
6 * Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
10 #include "readotucommand.h"
12 //**********************************************************************************************************************
13 ReadOtuCommand::ReadOtuCommand(string option){
15 globaldata = GlobalData::getInstance();
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"list","order","shared", "line", "label","group","sabund", "rabund"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string, string> parameters = parser.getParameters();
30 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 globaldata->newRead();
39 //check for required parameters
40 listfile = validParameter.validFile(parameters, "list", true);
41 if (listfile == "not open") { abort = true; }
42 else if (listfile == "not found") { listfile = ""; }
43 else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
45 sabundfile = validParameter.validFile(parameters, "sabund", true);
46 if (sabundfile == "not open") { abort = true; }
47 else if (sabundfile == "not found") { sabundfile = ""; }
48 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
50 rabundfile = validParameter.validFile(parameters, "rabund", true);
51 if (rabundfile == "not open") { abort = true; }
52 else if (rabundfile == "not found") { rabundfile = ""; }
53 else { globaldata->setRabundFile(rabundfile); globaldata->setFormat("rabund");}
55 sharedfile = validParameter.validFile(parameters, "shared", true);
56 if (sharedfile == "not open") { abort = true; }
57 else if (sharedfile == "not found") { sharedfile = ""; }
58 else { globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile"); }
60 groupfile = validParameter.validFile(parameters, "group", true);
61 if (groupfile == "not open") { abort = true; }
62 else if (groupfile == "not found") { groupfile = ""; }
64 globaldata->setGroupFile(groupfile);
65 groupMap = new GroupMap(groupfile);
69 //you are doing a list and group shared
70 if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
72 //you have not given a file
73 if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "")) {
74 cout << "You must enter either a listfile, rabundfile, sabundfile or a sharedfile with the read.otu command. " << endl; abort = true;
77 //check for optional parameter and set defaults
78 // ...at some point should added some additional type checking...
79 line = validParameter.validFile(parameters, "line", false);
80 if (line == "not found") { line = ""; }
82 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
83 else { allLines = 1; }
84 globaldata->lines = lines;
87 label = validParameter.validFile(parameters, "label", false);
88 if (label == "not found") { label = ""; }
90 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
91 else { allLines = 1; }
92 globaldata->labels = labels;
95 globaldata->allLines = allLines;
97 //make sure user did not use both the line and label parameters
98 if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
100 orderfile = validParameter.validFile(parameters, "order", true);
101 if (orderfile == "not open") { abort = true; }
102 else if (orderfile == "not found") { orderfile = ""; }
103 else { globaldata->setOrderFile(orderfile); }
106 if (abort == false) {
107 //gets whichever one of the above is set
108 filename = globaldata->inputFileName;
109 read = new ReadOTUFile(filename);
115 catch(exception& e) {
116 cout << "Standard Error: " << e.what() << " has occurred in the ReadOtuCommand class Function ReadOtuCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120 cout << "An unknown error has occurred in the ReadOtuCommand class function ReadOtuCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124 //**********************************************************************************************************************
126 void ReadOtuCommand::help(){
128 cout << "The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, " << "\n";
129 cout << "collect.shared, rarefaction.shared or summary.shared command. Mothur will generate a .list, .rabund and .sabund upon completion of the cluster command " << "\n";
130 cout << "or you may use your own. The read.otu command parameter options are list, rabund, sabund, shared, group, order, line and label." << "\n";
131 cout << "The read.otu command can be used in two ways. The first is to read a list, rabund or sabund and run the collect.single, rarefaction.single or summary.single." << "\n";
132 cout << "For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels)." << "\n";
133 cout << "The list, rabund or sabund parameter is required, but you may only use one of them." << "\n";
134 cout << "The line and label parameters are optional but you may not use both the line and label parameters at the same time." << "\n";
135 cout << "The label and line parameters are used to read specific lines in your input." << "\n";
136 cout << "The second way to use the read.otu command is to read a list and a group, or a shared so you can use the collect.shared, rarefaction.shared or summary.shared commands." << "\n";
137 cout << "In this case the read.otu command should be in the following format: read.otu(list=yourListFile, group=yourGroupFile, line=yourLines) or read.otu(shared=yourSharedFile). " << "\n";
138 cout << "The list parameter and group paramaters or the shared paremeter is required. When using the command the second way with a list and group file read.otu command parses the .list file" << "\n";
139 cout << "and separates it into groups. It outputs a .shared file containing the OTU information for each group. The read.otu command also outputs a .list file for each group. " << "\n";
140 cout << "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile)." << "\n" << "\n";
143 catch(exception& e) {
144 cout << "Standard Error: " << e.what() << " has occurred in the ReadOtuCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148 cout << "An unknown error has occurred in the ReadOtuCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
155 //**********************************************************************************************************************
157 ReadOtuCommand::~ReadOtuCommand(){
158 if (abort == false) { delete read; }
161 //**********************************************************************************************************************
163 int ReadOtuCommand::execute(){
166 if (abort == true) { return 0; }
168 read->read(&*globaldata);
169 if (globaldata->getFormat() == "shared") {
172 //if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; }
173 globaldata->gGroupmap = groupMap;
174 shared = new SharedCommand();
177 parselist = new ParseListCommand();
178 parselist->execute();
180 //change format to shared to speed up commands
181 globaldata->setFormat("sharedfile");
182 globaldata->setListFile("");
183 globaldata->setGroupFile("");
184 globaldata->setSharedFile(getRootName(filename) + "shared");
188 catch(exception& e) {
189 cout << "Standard Error: " << e.what() << " has occurred in the ReadOtuCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 cout << "An unknown error has occurred in the ReadOtuCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
197 //**********************************************************************************************************************