]> git.donarmstrong.com Git - mothur.git/blob - readtreecommand.cpp
6e5742ddbbc35cdc6d01b9d57b3d58a8c7856816
[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"};
24                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
25                         
26                         parser = new OptionParser();
27                         parser->parse(option, parameters);  delete parser;
28                         
29                         ValidParameters* validParameter = new ValidParameters();
30                 
31                         //check to make sure all parameters are valid for command
32                         for (it = parameters.begin(); it != parameters.end(); it++) { 
33                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
34                         }
35                         
36                         globaldata->newRead();
37                         
38                         //check for required parameters
39                         treefile = validParameter->validFile(parameters, "tree", true);
40                         if (treefile == "not open") { abort = true; }
41                         else if (treefile == "not found") { treefile = ""; cout << "tree is a required parameter for the read.tree command." << endl; abort = true;  }  
42                         else {  globaldata->setTreeFile(treefile);  globaldata->setFormat("tree");      }
43                         
44                         groupfile = validParameter->validFile(parameters, "group", true);
45                         if (groupfile == "not open") { abort = true; }  
46                         else if (groupfile == "not found") { groupfile = ""; cout << "group is a required parameter for the read.tree command." << endl; abort = true;  }
47                         else {  
48                                 globaldata->setGroupFile(groupfile); 
49                                 //read in group map info.
50                                 treeMap = new TreeMap(groupfile);
51                                 treeMap->readMap();
52                                 globaldata->gTreemap = treeMap;
53                         }
54                         
55                         if (abort == false) {
56                                 filename = treefile;
57                                 read = new ReadNewickTree(filename);
58                         }
59                                                 
60                         delete validParameter;
61                 }
62         }
63         catch(exception& e) {
64                 cout << "Standard Error: " << e.what() << " has occurred in the ReadTreeCommand class Function ReadTreeCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67         catch(...) {
68                 cout << "An unknown error has occurred in the ReadTreeCommand class function ReadTreeCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }
71 }
72 //**********************************************************************************************************************
73
74 void ReadTreeCommand::help(){
75         try {
76                 cout << "The read.tree command must be run before you execute a unifrac.weighted, unifrac.unweighted. " << "\n";
77                 cout << "It also must be run before using the parsimony command, unless you are using the randomtree parameter." << "\n";
78                 cout << "The read.tree command should be in the following format: read.tree(tree=yourTreeFile, group=yourGroupFile)." << "\n";
79                 cout << "The tree and group parameters are both required." << "\n";
80                 cout << "Note: No spaces between parameter labels (i.e. tree), '=' and parameters (i.e.yourTreefile)." << "\n" << "\n";
81         }
82         catch(exception& e) {
83                 cout << "Standard Error: " << e.what() << " has occurred in the ReadTreeCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84                 exit(1);
85         }
86         catch(...) {
87                 cout << "An unknown error has occurred in the ReadTreeCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
88                 exit(1);
89         }       
90 }
91
92 //**********************************************************************************************************************
93
94 ReadTreeCommand::~ReadTreeCommand(){
95         delete read;
96 }
97
98 //**********************************************************************************************************************
99
100 int ReadTreeCommand::execute(){
101         try {
102         
103                 if (abort == true) { return 0; }
104                 
105                 int readOk;
106                 
107                 readOk = read->read(); 
108                 
109                 if (readOk != 0) { cout << "Read Terminated." << endl; globaldata->gTree.clear(); delete globaldata->gTreemap; return 0; }
110                 
111                 vector<Tree*> T = globaldata->gTree;
112                 
113                 //assemble users trees
114                 for (int i = 0; i < T.size(); i++) {
115                         T[i]->assembleTree();
116                 }
117
118                 //output any names that are in names file but not in tree
119                 if (globaldata->Treenames.size() < treeMap->getNumSeqs()) {
120                         for (int i = 0; i < treeMap->namesOfSeqs.size(); i++) {
121                                 //is that name in the tree?
122                                 int count = 0;
123                                 for (int j = 0; j < globaldata->Treenames.size(); j++) {
124                                         if (treeMap->namesOfSeqs[i] == globaldata->Treenames[j]) { break; } //found it
125                                         count++;
126                                 }
127                                 
128                                 //then you did not find it so report it 
129                                 if (count == globaldata->Treenames.size()) { 
130                                         cout << treeMap->namesOfSeqs[i] << " is in your namefile and not in your tree. It will be disregarded." << endl;
131                                 }
132                         }
133                 }
134                 
135                 return 0;
136         }
137         catch(exception& e) {
138                 cout << "Standard Error: " << e.what() << " has occurred in the ReadTreeCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
139                 exit(1);
140         }
141         catch(...) {
142                 cout << "An unknown error has occurred in the ReadTreeCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143                 exit(1);
144         }
145 }
146
147 //**********************************************************************************************************************