]> git.donarmstrong.com Git - mothur.git/blob - readtreecommand.cpp
c476f5a89928cf131593178b40eb50d60130a7bd
[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 //**********************************************************************************************************************
14 ReadTreeCommand::ReadTreeCommand(string option)  {
15         try {
16                 abort = false; calledHelp = false;   
17                                 
18                 //allow user to run help
19                 if(option == "help") { help(); abort = true; calledHelp = true; }
20                 
21                 else {
22                         /*
23                         //valid paramters for this command
24                         string Array[] =  {"tree","group","name","outputdir","inputdir"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string, string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                         map<string, string>::iterator it;
32                 
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;  }
36                         }
37                         
38                         globaldata->newRead();
39                         
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 = "";          }
43                         else {
44                                 string path;
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;             }
51                                 }
52                                 
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;            }
59                                 }
60                                 
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;             }
67                                 }
68
69                         }
70
71                         
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;  }     
76                         
77                         groupfile = validParameter.validFile(parameters, "group", true);
78                         if (groupfile == "not open") { abort = true; }  
79                         else if (groupfile == "not found") { 
80                                 groupfile = ""; 
81                                 
82                                 m->mothurOut("You have not provided a group file. I am assumming all sequence are from the same group."); m->mothurOutEndLine();        
83                                 
84                                 if (treefile != "") {  Tree* tree = new Tree(treefile); delete tree;  } //extracts names from tree to make faked out groupmap
85                                 
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"); }
89                                         
90                         }else {  
91                                 //read in group map info.
92                                 treeMap = new TreeMap(groupfile);
93                                 treeMap->readMap();
94                         }
95                         
96                         namefile = validParameter.validFile(parameters, "name", true);
97                         if (namefile == "not open") { abort = true; }
98                         else if (namefile == "not found") { namefile = ""; }
99                         else { readNamesFile(); }       
100                         
101                         if (abort == false) {
102                                 filename = treefile;
103                                 read = new ReadNewickTree(filename);
104                         }
105                         */                      
106                 }
107         }
108         catch(exception& e) {
109                 m->errorOut(e, "ReadTreeCommand", "ReadTreeCommand");           
110                 exit(1);
111         }
112 }
113 //**********************************************************************************************************************
114
115 int ReadTreeCommand::execute(){
116         try {
117         
118                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
119                 m->mothurOut(getHelpString()); m->mothurOutEndLine();
120                 /*
121                 int readOk;
122                 
123                 readOk = read->read(treeMap); 
124                 
125                 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; return 0; }
126                 
127                 vector<Tree*> T = read->gTree;
128
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;
135                                 return 0;
136                         }
137         
138                         T[i]->assembleTree();
139                 }
140
141                 
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.
143                 int numNamesInTree;
144                 if (namefile != "")  {  
145                         if (numUniquesInName == globaldata->Treenames.size()) {  numNamesInTree = nameMap.size();  }
146                         else {   numNamesInTree = globaldata->Treenames.size();  }
147                 }else {  numNamesInTree = globaldata->Treenames.size();  }
148                 
149                 
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?
154                                 int count = 0;
155                                 for (int j = 0; j < globaldata->Treenames.size(); j++) {
156                                         if (treeMap->namesOfSeqs[i] == globaldata->Treenames[j]) { break; } //found it
157                                         count++;
158                                 }
159                                 
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;
164                                         return 0;
165                                 }
166                                 
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]);
171                                         
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
176                                         }
177                                 }
178                         }
179                         
180                         globaldata->gTreemap = treeMap;
181                 }
182                  */
183                 return 0;
184         }
185         catch(exception& e) {
186                 m->errorOut(e, "ReadTreeCommand", "execute");   
187                 exit(1);
188         }
189 }
190 /*****************************************************************/
191 int ReadTreeCommand::readNamesFile() {
192         try {
193                 /*
194                 m->names.clear();
195                 numUniquesInName = 0;
196                 
197                 ifstream in;
198                 m->openInputFile(namefile, in);
199                 
200                 string first, second;
201                 map<string, string>::iterator itNames;
202                 
203                 while(!in.eof()) {
204                         in >> first >> second; m->gobble(in);
205                         
206                         numUniquesInName++;
207
208                         itNames = m->names.find(first);
209                         if (itNames == globaldata->names.end()) {  
210                                 globaldata->names[first] = second; 
211                                 
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);
215                                 
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; }                 
218                 }
219                 in.close();
220                 */
221                 return 0;
222         }
223         catch(exception& e) {
224                 m->errorOut(e, "ReadTreeCommand", "readNamesFile");
225                 exit(1);
226         }
227 }
228
229 //**********************************************************************************************************************