]> git.donarmstrong.com Git - mothur.git/blob - readtreecommand.cpp
added logfile feature
[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                         OptionParser parser(option);
27                         map<string, string> parameters = parser.getParameters();
28                         
29                         ValidParameters validParameter;
30                 
31                         //check to make sure all parameters are valid for command
32                         for (map<string, string>::iterator 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 = ""; mothurOut("tree is a required parameter for the read.tree command."); mothurOutEndLine(); 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 = ""; mothurOut("group is a required parameter for the read.tree command."); mothurOutEndLine(); 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                 }
61         }
62         catch(exception& e) {
63                 errorOut(e, "ReadTreeCommand", "ReadTreeCommand");              
64                 exit(1);
65         }
66 }
67 //**********************************************************************************************************************
68
69 void ReadTreeCommand::help(){
70         try {
71                 mothurOut("The read.tree command must be run before you execute a unifrac.weighted, unifrac.unweighted. \n");
72                 mothurOut("It also must be run before using the parsimony command, unless you are using the randomtree parameter.\n");
73                 mothurOut("The read.tree command should be in the following format: read.tree(tree=yourTreeFile, group=yourGroupFile).\n");
74                 mothurOut("The tree and group parameters are both required.\n");
75                 mothurOut("Note: No spaces between parameter labels (i.e. tree), '=' and parameters (i.e.yourTreefile).\n\n");
76         }
77         catch(exception& e) {
78                 errorOut(e, "ReadTreeCommand", "help"); 
79                 exit(1);
80         }
81 }
82
83 //**********************************************************************************************************************
84
85 ReadTreeCommand::~ReadTreeCommand(){
86         if (abort == false) { delete read; }
87 }
88
89 //**********************************************************************************************************************
90
91 int ReadTreeCommand::execute(){
92         try {
93         
94                 if (abort == true) { return 0; }
95                 
96                 int readOk;
97                 
98                 readOk = read->read(); 
99                 
100                 if (readOk != 0) { mothurOut("Read Terminated."); mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; return 0; }
101                 
102                 vector<Tree*> T = globaldata->gTree;
103
104                 //assemble users trees
105                 for (int i = 0; i < T.size(); i++) {
106                         T[i]->assembleTree();
107                 }
108
109                 //output any names that are in names file but not in tree
110                 if (globaldata->Treenames.size() < treeMap->getNumSeqs()) {
111                         for (int i = 0; i < treeMap->namesOfSeqs.size(); i++) {
112                                 //is that name in the tree?
113                                 int count = 0;
114                                 for (int j = 0; j < globaldata->Treenames.size(); j++) {
115                                         if (treeMap->namesOfSeqs[i] == globaldata->Treenames[j]) { break; } //found it
116                                         count++;
117                                 }
118                                 
119                                 //then you did not find it so report it 
120                                 if (count == globaldata->Treenames.size()) { 
121                                         mothurOut(treeMap->namesOfSeqs[i] + " is in your namefile and not in your tree. It will be disregarded."); mothurOutEndLine();
122                                 }
123                         }
124                 }
125                 
126                 return 0;
127         }
128         catch(exception& e) {
129                 errorOut(e, "ReadTreeCommand", "execute");      
130                 exit(1);
131         }
132 }
133
134 //**********************************************************************************************************************