5 * Created by Sarah Westcott on 1/23/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "readtreecommand.h"
12 //**********************************************************************************************************************
13 ReadTreeCommand::ReadTreeCommand(string option) {
15 globaldata = GlobalData::getInstance();
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"tree","group","name","outputdir","inputdir"};
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string, string> parameters = parser.getParameters();
29 ValidParameters validParameter;
30 map<string, string>::iterator it;
32 //check to make sure all parameters are valid for command
33 for (it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 globaldata->newRead();
39 //if the user changes the input directory command factory will send this info to us in the output parameter
40 string inputDir = validParameter.validFile(parameters, "inputdir", false);
41 if (inputDir == "not found"){ inputDir = ""; }
44 it = parameters.find("tree");
45 //user has given a template file
46 if(it != parameters.end()){
47 path = hasPath(it->second);
48 //if the user has not given a path then, add inputdir. else leave path alone.
49 if (path == "") { parameters["tree"] = inputDir + it->second; }
52 it = parameters.find("group");
53 //user has given a template file
54 if(it != parameters.end()){
55 path = hasPath(it->second);
56 //if the user has not given a path then, add inputdir. else leave path alone.
57 if (path == "") { parameters["group"] = inputDir + it->second; }
60 it = parameters.find("name");
61 //user has given a template file
62 if(it != parameters.end()){
63 path = hasPath(it->second);
64 //if the user has not given a path then, add inputdir. else leave path alone.
65 if (path == "") { parameters["name"] = inputDir + it->second; }
71 //check for required parameters
72 treefile = validParameter.validFile(parameters, "tree", true);
73 if (treefile == "not open") { abort = true; }
74 else if (treefile == "not found") { treefile = ""; m->mothurOut("tree is a required parameter for the read.tree command."); m->mothurOutEndLine(); abort = true; }
75 else { globaldata->setTreeFile(treefile); globaldata->setFormat("tree"); }
77 groupfile = validParameter.validFile(parameters, "group", true);
78 if (groupfile == "not open") { abort = true; }
79 else if (groupfile == "not found") { groupfile = ""; m->mothurOut("group is a required parameter for the read.tree command."); m->mothurOutEndLine(); abort = true; }
81 globaldata->setGroupFile(groupfile);
82 //read in group map info.
83 treeMap = new TreeMap(groupfile);
85 globaldata->gTreemap = treeMap;
88 namefile = validParameter.validFile(parameters, "name", true);
89 if (namefile == "not open") { abort = true; }
90 else if (namefile == "not found") { namefile = ""; }
91 else { readNamesFile(); }
95 read = new ReadNewickTree(filename);
100 catch(exception& e) {
101 m->errorOut(e, "ReadTreeCommand", "ReadTreeCommand");
105 //**********************************************************************************************************************
107 void ReadTreeCommand::help(){
109 m->mothurOut("The read.tree command must be run before you execute a unifrac.weighted, unifrac.unweighted. \n");
110 m->mothurOut("It also must be run before using the parsimony command, unless you are using the randomtree parameter.\n");
111 m->mothurOut("The read.tree command parameters are tree, group and name.\n");
112 m->mothurOut("The read.tree command should be in the following format: read.tree(tree=yourTreeFile, group=yourGroupFile).\n");
113 m->mothurOut("The tree and group parameters are both required.\n");
114 m->mothurOut("The name parameter allows you to enter a namefile.\n");
115 m->mothurOut("Note: No spaces between parameter labels (i.e. tree), '=' and parameters (i.e.yourTreefile).\n\n");
117 catch(exception& e) {
118 m->errorOut(e, "ReadTreeCommand", "help");
123 //**********************************************************************************************************************
125 ReadTreeCommand::~ReadTreeCommand(){
126 if (abort == false) { delete read; }
129 //**********************************************************************************************************************
131 int ReadTreeCommand::execute(){
134 if (abort == true) { return 0; }
138 readOk = read->read();
140 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; return 0; }
142 vector<Tree*> T = globaldata->gTree;
144 //assemble users trees
145 for (int i = 0; i < T.size(); i++) {
146 if (m->control_pressed) {
147 for (int i = 0; i < T.size(); i++) { delete T[i]; }
148 globaldata->gTree.clear();
149 delete globaldata->gTreemap;
153 T[i]->assembleTree();
156 //output any names that are in group file but not in tree
157 if (globaldata->Treenames.size() < treeMap->getNumSeqs()) {
158 for (int i = 0; i < treeMap->namesOfSeqs.size(); i++) {
159 //is that name in the tree?
161 for (int j = 0; j < globaldata->Treenames.size(); j++) {
162 if (treeMap->namesOfSeqs[i] == globaldata->Treenames[j]) { break; } //found it
166 if (m->control_pressed) {
167 for (int i = 0; i < T.size(); i++) { delete T[i]; }
168 globaldata->gTree.clear();
169 delete globaldata->gTreemap;
173 //then you did not find it so report it
174 if (count == globaldata->Treenames.size()) {
175 //if it is in your namefile then don't remove
176 map<string, string>::iterator it = nameMap.find(treeMap->namesOfSeqs[i]);
178 if (it == nameMap.end()) {
179 m->mothurOut(treeMap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
180 treeMap->removeSeq(treeMap->namesOfSeqs[i]);
181 i--; //need this because removeSeq removes name from namesOfSeqs
186 globaldata->gTreemap = treeMap;
191 catch(exception& e) {
192 m->errorOut(e, "ReadTreeCommand", "execute");
196 /*****************************************************************/
197 int ReadTreeCommand::readNamesFile() {
199 globaldata->names.clear();
202 openInputFile(namefile, in);
204 string first, second;
205 map<string, string>::iterator itNames;
208 in >> first >> second; gobble(in);
210 itNames = globaldata->names.find(first);
211 if (itNames == globaldata->names.end()) {
212 globaldata->names[first] = second;
214 //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
215 vector<string> dupNames;
216 splitAtComma(second, dupNames);
218 for (int i = 0; i < dupNames.size(); i++) { nameMap[dupNames[i]] = dupNames[i]; }
219 }else { m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); globaldata->names.clear(); return 1; }
225 catch(exception& e) {
226 m->errorOut(e, "ReadTreeCommand", "readNamesFile");
231 //**********************************************************************************************************************