5 * Created by Sarah Westcott on 1/23/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "readtreecommand.h"
13 //**********************************************************************************************************************
14 ReadTreeCommand::ReadTreeCommand(string option) {
16 abort = false; calledHelp = false;
18 //allow user to run help
19 if(option == "help") { help(); abort = true; calledHelp = true; }
23 //valid paramters for this command
24 string Array[] = {"tree","group","name","outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string, string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string, string>::iterator it;
33 //check to make sure all parameters are valid for command
34 for (it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 globaldata->newRead();
40 //if the user changes the input directory command factory will send this info to us in the output parameter
41 string inputDir = validParameter.validFile(parameters, "inputdir", false);
42 if (inputDir == "not found"){ inputDir = ""; }
45 it = parameters.find("tree");
46 //user has given a template file
47 if(it != parameters.end()){
48 path = m->hasPath(it->second);
49 //if the user has not given a path then, add inputdir. else leave path alone.
50 if (path == "") { parameters["tree"] = inputDir + it->second; }
53 it = parameters.find("group");
54 //user has given a template file
55 if(it != parameters.end()){
56 path = m->hasPath(it->second);
57 //if the user has not given a path then, add inputdir. else leave path alone.
58 if (path == "") { parameters["group"] = inputDir + it->second; }
61 it = parameters.find("name");
62 //user has given a template file
63 if(it != parameters.end()){
64 path = m->hasPath(it->second);
65 //if the user has not given a path then, add inputdir. else leave path alone.
66 if (path == "") { parameters["name"] = inputDir + it->second; }
72 //check for required parameters
73 treefile = validParameter.validFile(parameters, "tree", true);
74 if (treefile == "not open") { abort = true; }
75 else if (treefile == "not found") { treefile = ""; m->mothurOut("tree is a required parameter for the read.tree command."); m->mothurOutEndLine(); abort = true; }
77 groupfile = validParameter.validFile(parameters, "group", true);
78 if (groupfile == "not open") { abort = true; }
79 else if (groupfile == "not found") {
82 m->mothurOut("You have not provided a group file. I am assumming all sequence are from the same group."); m->mothurOutEndLine();
84 if (treefile != "") { Tree* tree = new Tree(treefile); delete tree; } //extracts names from tree to make faked out groupmap
86 //read in group map info.
87 treeMap = new TreeMap();
88 for (int i = 0; i < m->Treenames.size(); i++) { treeMap->addSeq(m->Treenames[i], "Group1"); }
91 //read in group map info.
92 treeMap = new TreeMap(groupfile);
96 namefile = validParameter.validFile(parameters, "name", true);
97 if (namefile == "not open") { abort = true; }
98 else if (namefile == "not found") { namefile = ""; }
99 else { readNamesFile(); }
101 if (abort == false) {
103 read = new ReadNewickTree(filename);
108 catch(exception& e) {
109 m->errorOut(e, "ReadTreeCommand", "ReadTreeCommand");
113 //**********************************************************************************************************************
115 int ReadTreeCommand::execute(){
118 if (abort == true) { if (calledHelp) { return 0; } return 2; }
119 m->mothurOut(getHelpString()); m->mothurOutEndLine();
123 readOk = read->read(treeMap);
125 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; return 0; }
127 vector<Tree*> T = read->gTree;
129 //assemble users trees
130 for (int i = 0; i < T.size(); i++) {
131 if (m->control_pressed) {
132 for (int i = 0; i < T.size(); i++) { delete T[i]; }
133 globaldata->gTree.clear();
134 delete globaldata->gTreemap;
138 T[i]->assembleTree();
142 //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
144 if (namefile != "") {
145 if (numUniquesInName == globaldata->Treenames.size()) { numNamesInTree = nameMap.size(); }
146 else { numNamesInTree = globaldata->Treenames.size(); }
147 }else { numNamesInTree = globaldata->Treenames.size(); }
150 //output any names that are in group file but not in tree
151 if (numNamesInTree < treeMap->getNumSeqs()) {
152 for (int i = 0; i < treeMap->namesOfSeqs.size(); i++) {
153 //is that name in the tree?
155 for (int j = 0; j < globaldata->Treenames.size(); j++) {
156 if (treeMap->namesOfSeqs[i] == globaldata->Treenames[j]) { break; } //found it
160 if (m->control_pressed) {
161 for (int i = 0; i < T.size(); i++) { delete T[i]; }
162 globaldata->gTree.clear();
163 delete globaldata->gTreemap;
167 //then you did not find it so report it
168 if (count == globaldata->Treenames.size()) {
169 //if it is in your namefile then don't remove
170 map<string, string>::iterator it = nameMap.find(treeMap->namesOfSeqs[i]);
172 if (it == nameMap.end()) {
173 m->mothurOut(treeMap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
174 treeMap->removeSeq(treeMap->namesOfSeqs[i]);
175 i--; //need this because removeSeq removes name from namesOfSeqs
180 globaldata->gTreemap = treeMap;
185 catch(exception& e) {
186 m->errorOut(e, "ReadTreeCommand", "execute");
190 /*****************************************************************/
191 int ReadTreeCommand::readNamesFile() {
195 numUniquesInName = 0;
198 m->openInputFile(namefile, in);
200 string first, second;
201 map<string, string>::iterator itNames;
204 in >> first >> second; m->gobble(in);
208 itNames = m->names.find(first);
209 if (itNames == globaldata->names.end()) {
210 globaldata->names[first] = second;
212 //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
213 vector<string> dupNames;
214 m->splitAtComma(second, dupNames);
216 for (int i = 0; i < dupNames.size(); i++) { nameMap[dupNames[i]] = dupNames[i]; if ((groupfile == "") && (i != 0)) { globaldata->gTreemap->addSeq(dupNames[i], "Group1"); } }
217 }else { m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); globaldata->names.clear(); namefile = ""; return 1; }
223 catch(exception& e) {
224 m->errorOut(e, "ReadTreeCommand", "readNamesFile");
229 //**********************************************************************************************************************