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; }
20 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
24 //valid paramters for this command
25 string Array[] = {"tree","group","name","outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string, string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 globaldata->newRead();
41 //if the user changes the input directory command factory will send this info to us in the output parameter
42 string inputDir = validParameter.validFile(parameters, "inputdir", false);
43 if (inputDir == "not found"){ inputDir = ""; }
46 it = parameters.find("tree");
47 //user has given a template file
48 if(it != parameters.end()){
49 path = m->hasPath(it->second);
50 //if the user has not given a path then, add inputdir. else leave path alone.
51 if (path == "") { parameters["tree"] = inputDir + it->second; }
54 it = parameters.find("group");
55 //user has given a template file
56 if(it != parameters.end()){
57 path = m->hasPath(it->second);
58 //if the user has not given a path then, add inputdir. else leave path alone.
59 if (path == "") { parameters["group"] = inputDir + it->second; }
62 it = parameters.find("name");
63 //user has given a template file
64 if(it != parameters.end()){
65 path = m->hasPath(it->second);
66 //if the user has not given a path then, add inputdir. else leave path alone.
67 if (path == "") { parameters["name"] = inputDir + it->second; }
73 //check for required parameters
74 treefile = validParameter.validFile(parameters, "tree", true);
75 if (treefile == "not open") { abort = true; }
76 else if (treefile == "not found") { treefile = ""; m->mothurOut("tree is a required parameter for the read.tree command."); m->mothurOutEndLine(); abort = true; }
78 groupfile = validParameter.validFile(parameters, "group", true);
79 if (groupfile == "not open") { abort = true; }
80 else if (groupfile == "not found") {
83 m->mothurOut("You have not provided a group file. I am assumming all sequence are from the same group."); m->mothurOutEndLine();
85 if (treefile != "") { Tree* tree = new Tree(treefile); delete tree; } //extracts names from tree to make faked out groupmap
87 //read in group map info.
88 treeMap = new TreeMap();
89 for (int i = 0; i < m->Treenames.size(); i++) { treeMap->addSeq(m->Treenames[i], "Group1"); }
92 //read in group map info.
93 treeMap = new TreeMap(groupfile);
97 namefile = validParameter.validFile(parameters, "name", true);
98 if (namefile == "not open") { abort = true; }
99 else if (namefile == "not found") { namefile = ""; }
100 else { readNamesFile(); }
102 if (abort == false) {
104 read = new ReadNewickTree(filename);
109 catch(exception& e) {
110 m->errorOut(e, "ReadTreeCommand", "ReadTreeCommand");
114 //**********************************************************************************************************************
116 int ReadTreeCommand::execute(){
119 if (abort == true) { if (calledHelp) { return 0; } return 2; }
120 m->mothurOut(getHelpString()); m->mothurOutEndLine();
124 readOk = read->read(treeMap);
126 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; return 0; }
128 vector<Tree*> T = read->gTree;
130 //assemble users trees
131 for (int i = 0; i < T.size(); i++) {
132 if (m->control_pressed) {
133 for (int i = 0; i < T.size(); i++) { delete T[i]; }
134 globaldata->gTree.clear();
135 delete globaldata->gTreemap;
139 T[i]->assembleTree();
143 //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.
145 if (namefile != "") {
146 if (numUniquesInName == globaldata->Treenames.size()) { numNamesInTree = nameMap.size(); }
147 else { numNamesInTree = globaldata->Treenames.size(); }
148 }else { numNamesInTree = globaldata->Treenames.size(); }
151 //output any names that are in group file but not in tree
152 if (numNamesInTree < treeMap->getNumSeqs()) {
153 for (int i = 0; i < treeMap->namesOfSeqs.size(); i++) {
154 //is that name in the tree?
156 for (int j = 0; j < globaldata->Treenames.size(); j++) {
157 if (treeMap->namesOfSeqs[i] == globaldata->Treenames[j]) { break; } //found it
161 if (m->control_pressed) {
162 for (int i = 0; i < T.size(); i++) { delete T[i]; }
163 globaldata->gTree.clear();
164 delete globaldata->gTreemap;
168 //then you did not find it so report it
169 if (count == globaldata->Treenames.size()) {
170 //if it is in your namefile then don't remove
171 map<string, string>::iterator it = nameMap.find(treeMap->namesOfSeqs[i]);
173 if (it == nameMap.end()) {
174 m->mothurOut(treeMap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
175 treeMap->removeSeq(treeMap->namesOfSeqs[i]);
176 i--; //need this because removeSeq removes name from namesOfSeqs
181 globaldata->gTreemap = treeMap;
186 catch(exception& e) {
187 m->errorOut(e, "ReadTreeCommand", "execute");
191 /*****************************************************************/
192 int ReadTreeCommand::readNamesFile() {
196 numUniquesInName = 0;
199 m->openInputFile(namefile, in);
201 string first, second;
202 map<string, string>::iterator itNames;
205 in >> first >> second; m->gobble(in);
209 itNames = m->names.find(first);
210 if (itNames == globaldata->names.end()) {
211 globaldata->names[first] = second;
213 //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
214 vector<string> dupNames;
215 m->splitAtComma(second, dupNames);
217 for (int i = 0; i < dupNames.size(); i++) { nameMap[dupNames[i]] = dupNames[i]; if ((groupfile == "") && (i != 0)) { globaldata->gTreemap->addSeq(dupNames[i], "Group1"); } }
218 }else { m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); globaldata->names.clear(); namefile = ""; return 1; }
224 catch(exception& e) {
225 m->errorOut(e, "ReadTreeCommand", "readNamesFile");
230 //**********************************************************************************************************************