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