]> git.donarmstrong.com Git - mothur.git/blob - readtreecommand.cpp
fixed phylo.diversity
[mothur.git] / readtreecommand.cpp
1 /*
2  *  readtreecommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/23/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "readtreecommand.h"
11
12 //**********************************************************************************************************************
13 ReadTreeCommand::ReadTreeCommand(string option)  {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 abort = false;
17                                 
18                 //allow user to run help
19                 if(option == "help") { help(); abort = true; }
20                 
21                 else {
22                         //valid paramters for this command
23                         string Array[] =  {"tree","group","name","outputdir","inputdir"};
24                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
25                         
26                         OptionParser parser(option);
27                         map<string, string> parameters = parser.getParameters();
28                         
29                         ValidParameters validParameter;
30                         map<string, string>::iterator it;
31                 
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;  }
35                         }
36                         
37                         globaldata->newRead();
38                         
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 = "";          }
42                         else {
43                                 string path;
44                                 it = parameters.find("tree");
45                                 //user has given a template file
46                                 if(it != parameters.end()){ 
47                                         path = m->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;             }
50                                 }
51                                 
52                                 it = parameters.find("group");
53                                 //user has given a template file
54                                 if(it != parameters.end()){ 
55                                         path = m->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;            }
58                                 }
59                                 
60                                 it = parameters.find("name");
61                                 //user has given a template file
62                                 if(it != parameters.end()){ 
63                                         path = m->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;             }
66                                 }
67
68                         }
69
70                         
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");      }
76                         
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;     }
80                         else {  
81                                 globaldata->setGroupFile(groupfile); 
82                                 //read in group map info.
83                                 treeMap = new TreeMap(groupfile);
84                                 treeMap->readMap();
85                                 globaldata->gTreemap = treeMap;
86                         }
87                         
88                         namefile = validParameter.validFile(parameters, "name", true);
89                         if (namefile == "not open") { abort = true; }
90                         else if (namefile == "not found") { namefile = ""; }
91                         else { readNamesFile(); }       
92                         
93                         if (abort == false) {
94                                 filename = treefile;
95                                 read = new ReadNewickTree(filename);
96                         }
97                                                 
98                 }
99         }
100         catch(exception& e) {
101                 m->errorOut(e, "ReadTreeCommand", "ReadTreeCommand");           
102                 exit(1);
103         }
104 }
105 //**********************************************************************************************************************
106
107 void ReadTreeCommand::help(){
108         try {
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");
116         }
117         catch(exception& e) {
118                 m->errorOut(e, "ReadTreeCommand", "help");      
119                 exit(1);
120         }
121 }
122
123 //**********************************************************************************************************************
124
125 ReadTreeCommand::~ReadTreeCommand(){
126         if (abort == false) { delete read; }
127 }
128
129 //**********************************************************************************************************************
130
131 int ReadTreeCommand::execute(){
132         try {
133         
134                 if (abort == true) { return 0; }
135                 
136                 int readOk;
137                 
138                 readOk = read->read(); 
139                 
140                 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; return 0; }
141                 
142                 vector<Tree*> T = globaldata->gTree;
143
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;
150                                 return 0;
151                         }
152         
153                         T[i]->assembleTree();
154                 }
155
156                 
157                 //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.
158                 int numNamesInTree;
159                 if (namefile != "")  {  
160                         if (numUniquesInName == globaldata->Treenames.size()) {  numNamesInTree = nameMap.size();  }
161                         else {   numNamesInTree = globaldata->Treenames.size();  }
162                 }else {  numNamesInTree = globaldata->Treenames.size();  }
163                 
164                 
165                 //output any names that are in group file but not in tree
166                 if (numNamesInTree < treeMap->getNumSeqs()) {
167                         for (int i = 0; i < treeMap->namesOfSeqs.size(); i++) {
168                                 //is that name in the tree?
169                                 int count = 0;
170                                 for (int j = 0; j < globaldata->Treenames.size(); j++) {
171                                         if (treeMap->namesOfSeqs[i] == globaldata->Treenames[j]) { break; } //found it
172                                         count++;
173                                 }
174                                 
175                                 if (m->control_pressed) {  
176                                         for (int i = 0; i < T.size(); i++) {  delete T[i];  }
177                                         globaldata->gTree.clear();
178                                         delete globaldata->gTreemap;
179                                         return 0;
180                                 }
181                                 
182                                 //then you did not find it so report it 
183                                 if (count == globaldata->Treenames.size()) { 
184                                         //if it is in your namefile then don't remove
185                                         map<string, string>::iterator it = nameMap.find(treeMap->namesOfSeqs[i]);
186                                         
187                                         if (it == nameMap.end()) {
188                                                 m->mothurOut(treeMap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
189                                                 treeMap->removeSeq(treeMap->namesOfSeqs[i]);
190                                                 i--; //need this because removeSeq removes name from namesOfSeqs
191                                         }
192                                 }
193                         }
194                         
195                         globaldata->gTreemap = treeMap;
196                 }
197
198                 return 0;
199         }
200         catch(exception& e) {
201                 m->errorOut(e, "ReadTreeCommand", "execute");   
202                 exit(1);
203         }
204 }
205 /*****************************************************************/
206 int ReadTreeCommand::readNamesFile() {
207         try {
208                 globaldata->names.clear();
209                 numUniquesInName = 0;
210                 
211                 ifstream in;
212                 m->openInputFile(namefile, in);
213                 
214                 string first, second;
215                 map<string, string>::iterator itNames;
216                 
217                 while(!in.eof()) {
218                         in >> first >> second; m->gobble(in);
219                         
220                         numUniquesInName++;
221
222                         itNames = globaldata->names.find(first);
223                         if (itNames == globaldata->names.end()) {  
224                                 globaldata->names[first] = second; 
225                                 
226                                 //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
227                                 vector<string> dupNames;
228                                 m->splitAtComma(second, dupNames);
229                                 
230                                 for (int i = 0; i < dupNames.size(); i++) {     nameMap[dupNames[i]] = dupNames[i];  }
231                         }else {  m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); globaldata->names.clear(); namefile = ""; return 1; }                 
232                 }
233                 in.close();
234                 
235                 return 0;
236         }
237         catch(exception& e) {
238                 m->errorOut(e, "ReadTreeCommand", "readNamesFile");
239                 exit(1);
240         }
241 }
242
243 //**********************************************************************************************************************