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", "label","group","sabund", "rabund","groups"};
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);
67 globaldata->gGroupmap = groupMap;
70 groups = validParameter.validFile(parameters, "groups", false);
71 if (groups == "not found") { groups = ""; }
73 splitAtDash(groups, Groups);
74 globaldata->Groups = Groups;
77 //you are doing a list and group shared
78 if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
80 //you have not given a file
81 if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "")) {
82 mothurOut("You must enter either a listfile, rabundfile, sabundfile or a sharedfile with the read.otu command. "); mothurOutEndLine(); abort = true;
85 //check for optional parameter and set defaults
86 // ...at some point should added some additional type checking...
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 orderfile = validParameter.validFile(parameters, "order", true);
98 if (orderfile == "not open") { abort = true; }
99 else if (orderfile == "not found") { orderfile = ""; }
100 else { globaldata->setOrderFile(orderfile); }
103 if (abort == false) {
104 //gets whichever one of the above is set
105 filename = globaldata->inputFileName;
111 catch(exception& e) {
112 errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
116 //**********************************************************************************************************************
118 void ReadOtuCommand::help(){
120 mothurOut("The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, \n");
121 mothurOut("collect.shared, rarefaction.shared or summary.shared command. Mothur will generate a .list, .rabund and .sabund upon completion of the cluster command \n");
122 mothurOut("or you may use your own. The read.otu command parameter options are list, rabund, sabund, shared, group, order and label.\n");
123 mothurOut("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");
124 mothurOut("For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels).\n");
125 mothurOut("The list, rabund or sabund parameter is required, but you may only use one of them.\n");
126 mothurOut("The label parameter is used to read specific labels in your input.\n");
127 mothurOut("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");
128 mothurOut("In this case the read.otu command should be in the following format: read.otu(list=yourListFile, group=yourGroupFile) or read.otu(shared=yourSharedFile). \n");
129 mothurOut("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");
130 mothurOut("and separates it into groups. It outputs a .shared file containing the OTU information for each group. The read.otu command also outputs a .rabund file for each group. \n");
131 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
134 catch(exception& e) {
135 errorOut(e, "ReadOtuCommand", "help");
142 //**********************************************************************************************************************
144 ReadOtuCommand::~ReadOtuCommand(){
147 //**********************************************************************************************************************
149 int ReadOtuCommand::execute(){
152 if (abort == true) { return 0; }
154 if (globaldata->getFormat() == "shared") {
156 shared = new SharedCommand();
157 int okay = shared->execute();
160 //problem with shared
162 globaldata->setListFile("");
163 globaldata->setGroupFile("");
164 globaldata->setSharedFile("");
167 //change format to shared to speed up commands
168 globaldata->setFormat("sharedfile");
169 globaldata->setListFile("");
170 globaldata->setGroupFile("");
171 globaldata->setSharedFile(getRootName(filename) + "shared");
176 catch(exception& e) {
177 errorOut(e, "ReadOtuCommand", "execute");
181 //**********************************************************************************************************************