]> git.donarmstrong.com Git - mothur.git/blob - phylotypecommand.cpp
changed label outputs for phylotype command
[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"};
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                         
35                         //check to make sure all parameters are valid for command
36                         for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
37                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
38                         }
39                         
40                         taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
41                         if (taxonomyFileName == "not found") { 
42                                 mothurOut("taxonomy is a required parameter for the phylotype command."); 
43                                 mothurOutEndLine();
44                                 abort = true; 
45                         }else if (taxonomyFileName == "not open") { abort = true; }     
46                         
47                         string temp = validParameter.validFile(parameters, "cutoff", false);
48                         if (temp == "not found") { temp = "-1"; }
49                         convert(temp, cutoff); 
50                         
51                         label = validParameter.validFile(parameters, "label", false);                   
52                         if (label == "not found") { label = ""; allLines = 1; }
53                         else { 
54                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
55                                 else { allLines = 1;  }
56                         }
57                         
58                 }
59         }
60         catch(exception& e) {
61                 errorOut(e, "PhylotypeCommand", "PhylotypeCommand");
62                 exit(1);
63         }
64 }
65 /**********************************************************************************************************************/
66
67 void PhylotypeCommand::help(){
68         try {
69                 mothurOut("The phylotype command reads a taxonomy file and outputs a .list, .rabund and .sabund file. \n");
70                 mothurOut("The phylotype command parameter options are taxonomy, cutoff and label. The taxonomy parameter is required.\n");
71                 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");
72                 mothurOut("For example: taxonomy = Bacteria;Bacteroidetes-Chlorobi;Bacteroidetes; - cutoff=2, would truncate the taxonomy to Bacteria;Bacteroidetes-Chlorobi; \n");
73                 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");
74                 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");
75                 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");
76                 mothurOut("The phylotype command should be in the following format: \n");
77                 mothurOut("phylotype(taxonomy=yourTaxonomyFile, cutoff=yourCutoff, label=yourLabels) \n");
78                 mothurOut("Eaxample: phylotype(taxonomy=amazon.taxonomy, cutoff=5, label=1-3-5).\n\n");
79         }
80         catch(exception& e) {
81                 errorOut(e, "PhylotypeCommand", "help");
82                 exit(1);
83         }
84 }
85 /**********************************************************************************************************************/
86
87 PhylotypeCommand::~PhylotypeCommand(){}
88
89 /**********************************************************************************************************************/
90
91 int PhylotypeCommand::execute(){
92         try {
93         
94                 if (abort == true) { return 0; }
95                 
96                 //reads in taxonomy file and makes all the taxonomies the same length 
97                 //by appending the last taxon to a given taxonomy as many times as needed to 
98                 //make it as long as the longest taxonomy in the file 
99                 TaxEqualizer* taxEqual = new TaxEqualizer(taxonomyFileName, cutoff);
100                 
101                 string equalizedTaxFile = taxEqual->getEqualizedTaxFile();
102                 
103                 delete taxEqual;
104                 
105                 //build taxonomy tree from equalized file
106                 PhyloTree* tree = new PhyloTree(equalizedTaxFile);
107                 vector<int> leaves = tree->getGenusNodes();
108                 
109                 //store leaf nodes in current map
110                 for (int i = 0; i < leaves.size(); i++)         {       currentNodes[leaves[i]] = leaves[i];    }
111                 
112                 bool done = false;
113                 if (tree->get(leaves[0]).parent == -1) {  mothurOut("Empty Tree"); mothurOutEndLine();  done = true;    }
114                 
115                 ofstream outList;
116                 string outputListFile = getRootName(taxonomyFileName) + "tax.list";
117                 openOutputFile(outputListFile, outList);
118                 ofstream outSabund;
119                 string outputSabundFile = getRootName(taxonomyFileName) + "tax.sabund";
120                 openOutputFile(outputSabundFile, outSabund);
121                 ofstream outRabund;
122                 string outputRabundFile = getRootName(taxonomyFileName) + "tax.rabund";
123                 openOutputFile(outputRabundFile, outRabund);
124                 
125                 int count = 1;          
126                 //start at leaves of tree and work towards root, processing the labels the user wants
127                 while((!done) && ((allLines == 1) || (labels.size() != 0))) {
128                 
129                         string level = toString(count); 
130                         count++;
131                         
132                         //is this a level the user want output for
133                         if(allLines == 1 || labels.count(level) == 1){  
134                                 
135                                 //output level
136                                 mothurOut(level); mothurOutEndLine();
137                                 
138                                 ListVector list;
139                                 list.setLabel(level);
140                                 //go through nodes and build listvector 
141                                 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
142                         
143                                         //get parents
144                                         TaxNode node = tree->get(itCurrent->first);
145                                         parentNodes[node.parent] = node.parent;
146                                         
147                                         vector<string> names = node.accessions;
148                                         
149                                         //make the names compatable with listvector
150                                         string name = "";
151                                         for (int i = 0; i < names.size(); i++) {  name += names[i] + ",";       }
152                                         name = name.substr(0, name.length()-1);  //rip off extra ','
153                                         
154                                         //add bin to list vector
155                                         list.push_back(name);
156                                 }       
157                                 
158                                 //print listvector
159                                 list.print(outList);
160                                 //print rabund
161                                 list.getRAbundVector().print(outRabund);
162                                 //print sabund
163                                 list.getSAbundVector().print(outSabund);
164                         
165                                 labels.erase(level);
166                                 
167                         }else {
168                                 
169                                 //just get parents
170                                 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
171                                         int parent = tree->get(itCurrent->first).parent;
172                                         parentNodes[parent] = parent;
173                                 }
174                         }
175                         
176                         //move up a level
177                         currentNodes = parentNodes;
178                         parentNodes.clear();
179                         
180                         //have we reached the rootnode
181                         if (tree->get(currentNodes.begin()->first).parent == -1)  {  done = true;  }
182                 }
183                         
184                 outList.close();
185                 outSabund.close();
186                 outRabund.close();      
187                 
188                 delete tree;
189                 
190                 return 0;               
191         }
192
193         catch(exception& e) {
194                 errorOut(e, "PhylotypeCommand", "execute");
195                 exit(1);
196         }
197 }
198 /**********************************************************************************************************************/