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