]> git.donarmstrong.com Git - mothur.git/blob - phylotypecommand.cpp
some changes while testing 1.9
[mothur.git] / phylotypecommand.cpp
1 /*
2  *  phylotypecommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 11/20/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "phylotypecommand.h"
11 #include "phylotree.h"
12 #include "listvector.hpp"
13 #include "rabundvector.hpp"
14 #include "sabundvector.hpp"
15
16 /**********************************************************************************************************************/
17 PhylotypeCommand::PhylotypeCommand(string option)  {
18         try {
19                 abort = false;
20                 
21                 //allow user to run help
22                 if(option == "help") { help(); abort = true; }
23                 
24                 else {
25                         
26                         //valid paramters for this command
27                         string AlignArray[] =  {"taxonomy","cutoff","label","outputdir","inputdir"};
28                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
29                         
30                         OptionParser parser(option);
31                         map<string, string> parameters = parser.getParameters(); 
32                         
33                         ValidParameters validParameter;
34                         map<string, string>::iterator it;
35                         
36                         //check to make sure all parameters are valid for command
37                         for (it = parameters.begin(); it != parameters.end(); it++) { 
38                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
39                         }
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("taxonomy");
47                                 //user has given a template file
48                                 if(it != parameters.end()){ 
49                                         path = hasPath(it->second);
50                                         //if the user has not given a path then, add inputdir. else leave path alone.
51                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
52                                 }
53                         }
54
55                         taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
56                         if (taxonomyFileName == "not found") { 
57                                 m->mothurOut("taxonomy is a required parameter for the phylotype command."); 
58                                 m->mothurOutEndLine();
59                                 abort = true; 
60                         }else if (taxonomyFileName == "not open") { abort = true; }     
61                         
62                         //if the user changes the output directory command factory will send this info to us in the output parameter 
63                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
64                                 outputDir = ""; 
65                                 outputDir += hasPath(taxonomyFileName); //if user entered a file with a path then preserve it   
66                         }
67                         
68                         string temp = validParameter.validFile(parameters, "cutoff", false);
69                         if (temp == "not found") { temp = "-1"; }
70                         convert(temp, cutoff); 
71                         
72                         label = validParameter.validFile(parameters, "label", false);                   
73                         if (label == "not found") { label = ""; allLines = 1; }
74                         else { 
75                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
76                                 else { allLines = 1;  }
77                         }
78                         
79                 }
80         }
81         catch(exception& e) {
82                 m->errorOut(e, "PhylotypeCommand", "PhylotypeCommand");
83                 exit(1);
84         }
85 }
86 /**********************************************************************************************************************/
87
88 void PhylotypeCommand::help(){
89         try {
90                 m->mothurOut("The phylotype command reads a taxonomy file and outputs a .list, .rabund and .sabund file. \n");
91                 m->mothurOut("The phylotype command parameter options are taxonomy, cutoff and label. The taxonomy parameter is required.\n");
92                 m->mothurOut("The cutoff parameter allows you to specify the level you want to stop at.  The default is the highest level in your taxonomy file. \n");
93                 m->mothurOut("For example: taxonomy = Bacteria;Bacteroidetes-Chlorobi;Bacteroidetes; - cutoff=2, would truncate the taxonomy to Bacteria;Bacteroidetes-Chlorobi; \n");
94                 m->mothurOut("For the cutoff parameter levels count up from the root of the phylotree. This enables you to look at the grouping down to a specific resolution, say the genus level.\n");
95                 m->mothurOut("The label parameter allows you to specify which level you would like, and are separated by dashes.  The default all levels in your taxonomy file. \n");
96                 m->mothurOut("For the label parameter, levels count down from the root to keep the output similiar to mothur's other commands which report information from finer resolution to coarser resolutions.\n");
97                 m->mothurOut("The phylotype command should be in the following format: \n");
98                 m->mothurOut("phylotype(taxonomy=yourTaxonomyFile, cutoff=yourCutoff, label=yourLabels) \n");
99                 m->mothurOut("Eaxample: phylotype(taxonomy=amazon.taxonomy, cutoff=5, label=1-3-5).\n\n");
100         }
101         catch(exception& e) {
102                 m->errorOut(e, "PhylotypeCommand", "help");
103                 exit(1);
104         }
105 }
106 /**********************************************************************************************************************/
107
108 PhylotypeCommand::~PhylotypeCommand(){}
109
110 /**********************************************************************************************************************/
111
112 int PhylotypeCommand::execute(){
113         try {
114         
115                 if (abort == true) { return 0; }
116                 
117                 vector<string> outputNames;
118                 
119                 //reads in taxonomy file and makes all the taxonomies the same length 
120                 //by appending the last taxon to a given taxonomy as many times as needed to 
121                 //make it as long as the longest taxonomy in the file 
122                 TaxEqualizer* taxEqual = new TaxEqualizer(taxonomyFileName, cutoff, outputDir);
123                 
124                 if (m->control_pressed) { delete taxEqual; return 0; }
125                 
126                 string equalizedTaxFile = taxEqual->getEqualizedTaxFile();
127                 
128                 delete taxEqual;
129                 
130                 //build taxonomy tree from equalized file
131                 PhyloTree* tree = new PhyloTree(equalizedTaxFile);
132                 vector<int> leaves = tree->getGenusNodes();
133                 
134                 //store leaf nodes in current map
135                 for (int i = 0; i < leaves.size(); i++)         {       currentNodes[leaves[i]] = leaves[i];    }
136                 
137                 bool done = false;
138                 if (tree->get(leaves[0]).parent == -1) {  m->mothurOut("Empty Tree"); m->mothurOutEndLine();    done = true;    }
139                 
140                 if (m->control_pressed) { delete tree; return 0; }
141                 
142                 string fileroot = outputDir + getRootName(getSimpleName(taxonomyFileName));
143                 
144                 ofstream outList;
145                 string outputListFile = fileroot + "tx.list";
146                 openOutputFile(outputListFile, outList);
147                 ofstream outSabund;
148                 string outputSabundFile = fileroot + "tx.sabund";
149                 openOutputFile(outputSabundFile, outSabund);
150                 ofstream outRabund;
151                 string outputRabundFile = fileroot + "tx.rabund";
152                 openOutputFile(outputRabundFile, outRabund);
153                 
154                 outputNames.push_back(outputListFile);
155                 outputNames.push_back(outputSabundFile);
156                 outputNames.push_back(outputRabundFile);
157                 
158                 int count = 1;          
159                 //start at leaves of tree and work towards root, processing the labels the user wants
160                 while((!done) && ((allLines == 1) || (labels.size() != 0))) {
161                 
162                         string level = toString(count); 
163                         count++;
164                         
165                         if (m->control_pressed) { 
166                                 outRabund.close(); outSabund.close(); outList.close();
167                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
168                                 delete tree; return 0; 
169                         }
170                         
171                         //is this a level the user want output for
172                         if(allLines == 1 || labels.count(level) == 1){  
173                                 
174                                 //output level
175                                 m->mothurOut(level); m->mothurOutEndLine();
176                                 
177                                 ListVector list;
178                                 list.setLabel(level);
179                                 //go through nodes and build listvector 
180                                 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
181                         
182                                         //get parents
183                                         TaxNode node = tree->get(itCurrent->first);
184                                         parentNodes[node.parent] = node.parent;
185                                         
186                                         vector<string> names = node.accessions;
187                                         
188                                         //make the names compatable with listvector
189                                         string name = "";
190                                         for (int i = 0; i < names.size(); i++) {  name += names[i] + ",";       }
191                                         name = name.substr(0, name.length()-1);  //rip off extra ','
192                                         
193                                         //add bin to list vector
194                                         list.push_back(name);
195                                 }       
196                                 
197                                 //print listvector
198                                 list.print(outList);
199                                 //print rabund
200                                 list.getRAbundVector().print(outRabund);
201                                 //print sabund
202                                 list.getSAbundVector().print(outSabund);
203                         
204                                 labels.erase(level);
205                                 
206                         }else {
207                                 
208                                 //just get parents
209                                 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
210                                         int parent = tree->get(itCurrent->first).parent;
211                                         parentNodes[parent] = parent;
212                                 }
213                         }
214                         
215                         //move up a level
216                         currentNodes = parentNodes;
217                         parentNodes.clear();
218                         
219                         //have we reached the rootnode
220                         if (tree->get(currentNodes.begin()->first).parent == -1)  {  done = true;  }
221                 }
222                         
223                 outList.close();
224                 outSabund.close();
225                 outRabund.close();      
226                 
227                 delete tree;
228                 
229                 if (m->control_pressed) { 
230                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
231                         return 0; 
232                 }
233                 
234                 m->mothurOutEndLine();
235                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
236                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
237                 m->mothurOutEndLine();
238                 
239                 return 0;               
240         }
241
242         catch(exception& e) {
243                 m->errorOut(e, "PhylotypeCommand", "execute");
244                 exit(1);
245         }
246 }
247 /**********************************************************************************************************************/