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","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; }
62 //check for required parameters
63 treefile = validParameter.validFile(parameters, "tree", true);
64 if (treefile == "not open") { abort = true; }
65 else if (treefile == "not found") { treefile = ""; mothurOut("tree is a required parameter for the read.tree command."); mothurOutEndLine(); abort = true; }
66 else { globaldata->setTreeFile(treefile); globaldata->setFormat("tree"); }
68 groupfile = validParameter.validFile(parameters, "group", true);
69 if (groupfile == "not open") { abort = true; }
70 else if (groupfile == "not found") { groupfile = ""; mothurOut("group is a required parameter for the read.tree command."); mothurOutEndLine(); abort = true; }
72 globaldata->setGroupFile(groupfile);
73 //read in group map info.
74 treeMap = new TreeMap(groupfile);
76 globaldata->gTreemap = treeMap;
81 read = new ReadNewickTree(filename);
87 errorOut(e, "ReadTreeCommand", "ReadTreeCommand");
91 //**********************************************************************************************************************
93 void ReadTreeCommand::help(){
95 mothurOut("The read.tree command must be run before you execute a unifrac.weighted, unifrac.unweighted. \n");
96 mothurOut("It also must be run before using the parsimony command, unless you are using the randomtree parameter.\n");
97 mothurOut("The read.tree command should be in the following format: read.tree(tree=yourTreeFile, group=yourGroupFile).\n");
98 mothurOut("The tree and group parameters are both required.\n");
99 mothurOut("Note: No spaces between parameter labels (i.e. tree), '=' and parameters (i.e.yourTreefile).\n\n");
101 catch(exception& e) {
102 errorOut(e, "ReadTreeCommand", "help");
107 //**********************************************************************************************************************
109 ReadTreeCommand::~ReadTreeCommand(){
110 if (abort == false) { delete read; }
113 //**********************************************************************************************************************
115 int ReadTreeCommand::execute(){
118 if (abort == true) { return 0; }
122 readOk = read->read();
124 if (readOk != 0) { mothurOut("Read Terminated."); mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; return 0; }
126 vector<Tree*> T = globaldata->gTree;
128 //assemble users trees
129 for (int i = 0; i < T.size(); i++) {
130 T[i]->assembleTree();
133 //output any names that are in names file but not in tree
134 if (globaldata->Treenames.size() < treeMap->getNumSeqs()) {
135 for (int i = 0; i < treeMap->namesOfSeqs.size(); i++) {
136 //is that name in the tree?
138 for (int j = 0; j < globaldata->Treenames.size(); j++) {
139 if (treeMap->namesOfSeqs[i] == globaldata->Treenames[j]) { break; } //found it
143 //then you did not find it so report it
144 if (count == globaldata->Treenames.size()) {
145 mothurOut(treeMap->namesOfSeqs[i] + " is in your namefile and not in your tree. It will be disregarded."); mothurOutEndLine();
146 treeMap->removeSeq(treeMap->namesOfSeqs[i]);
153 catch(exception& e) {
154 errorOut(e, "ReadTreeCommand", "execute");
159 //**********************************************************************************************************************