]> git.donarmstrong.com Git - mothur.git/blob - getlistcountcommand.cpp
added hcluster command and fixed some bugs, namely one with smart distancing.
[mothur.git] / getlistcountcommand.cpp
1 /*
2  *  getlistcountcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/12/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "getlistcountcommand.h"
11
12 //**********************************************************************************************************************
13 GetListCountCommand::GetListCountCommand(string option){
14         try {
15                 globaldata = GlobalData::getInstance();
16                 abort = false;
17                 allLines = 1;
18                 labels.clear();
19                 
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; }
22                 
23                 else {
24                         //valid paramters for this command
25                         string AlignArray[] =  {"list","label"};
26                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string, string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                 
33                         //check to make sure all parameters are valid for command
34                         for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
36                         }
37                         
38                         string ranRead = globaldata->getListFile();
39                         
40                         //check for required parameters
41                         listfile = validParameter.validFile(parameters, "list", true);
42                         if ((listfile == "not found") && (globaldata->getListFile() == ""))  { mothurOut("You must read a listfile before running the get.listcount command.");  mothurOutEndLine(); abort = true; }
43                         else if ((listfile == "not found") && (globaldata->getListFile() != "")) { listfile = globaldata->getListFile(); }
44                         else if (listfile == "not open") { abort = true; }      
45                         else { globaldata->setListFile(listfile); }
46                 
47                         //check for optional parameter and set defaults
48                         // ...at some point should added some additional type checking...
49                         
50                         label = validParameter.validFile(parameters, "label", false);                   
51                         if (label == "not found") { label = ""; }
52                         else { 
53                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
54                                 else { allLines = 1;  }
55                         }
56                         
57                         //if the user has not specified any labels use the ones from read.otu
58                         if ((label == "") && (ranRead != "")) {  
59                                 allLines = globaldata->allLines; 
60                                 labels = globaldata->labels; 
61                         }
62                 }
63         }
64         catch(exception& e) {
65                 errorOut(e, "GetListCountCommand", "GetListCountCommand");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70
71 void GetListCountCommand::help(){
72         try {
73                 mothurOut("The get.listcount command can only be executed after a successful read.otu command of a listfile or providing a list file using the list parameter.\n");
74                 mothurOut("The get.listcount command parameters are list and label.  No parameters are required.\n");
75                 mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n");
76                 mothurOut("The get.listcount command should be in the following format: get.listcount(list=yourlistFile, label=yourLabels).\n");
77                 mothurOut("Example get.listcount(list=amazon.fn.list, label=0.10).\n");
78                 mothurOut("The default value for label is all lines in your inputfile.\n");
79                 mothurOut("The get.listcount command outputs a .otu file for each distance you specify listing the bin number and the names of the sequences in that bin.\n");
80                 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
81         }
82         catch(exception& e) {
83                 errorOut(e, "GetListCountCommand", "help");
84                 exit(1);
85         }
86 }
87
88 //**********************************************************************************************************************
89
90 GetListCountCommand::~GetListCountCommand(){}
91
92 //**********************************************************************************************************************
93
94 int GetListCountCommand::execute(){
95         try {
96                 if (abort == true) {    return 0;       }
97
98                 globaldata->setFormat("list");
99                 
100                 //read list file
101                 read = new ReadOTUFile(listfile);       
102                 read->read(&*globaldata); 
103                 
104                 input = globaldata->ginput;
105                 list = globaldata->gListVector;
106                 string lastLabel = list->getLabel();
107
108                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
109                 set<string> processedLabels;
110                 set<string> userLabels = labels;
111
112                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
113                         
114                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
115                         
116                                 process(list);
117                                                         
118                                 processedLabels.insert(list->getLabel());
119                                 userLabels.erase(list->getLabel());
120                         }
121                         
122                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
123                                 string saveLabel = list->getLabel();
124                                 
125                                 delete list;
126                                 list = input->getListVector(lastLabel);
127                                 
128                                 process(list);
129                                                                                                         
130                                 processedLabels.insert(list->getLabel());
131                                 userLabels.erase(list->getLabel());
132                                 
133                                 //restore real lastlabel to save below
134                                 list->setLabel(saveLabel);
135                         }
136                         
137                         lastLabel = list->getLabel();                   
138                         
139                         delete list;
140                         list = input->getListVector();
141                 }
142                 
143                 
144                 //output error messages about any remaining user labels
145                 set<string>::iterator it;
146                 bool needToRun = false;
147                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
148                         mothurOut("Your file does not include the label " + *it); 
149                         if (processedLabels.count(lastLabel) != 1) {
150                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
151                                 needToRun = true;
152                         }else {
153                                 mothurOut(". Please refer to " + lastLabel + ".");  mothurOutEndLine();
154                         }
155                 }
156                 
157                 //run last label if you need to
158                 if (needToRun == true)  {
159                         if (list != NULL) {             delete list;    }
160                         list = input->getListVector(lastLabel);
161                                 
162                         process(list);                  
163                         delete list;  
164                 }
165                 
166                 delete read;
167                 globaldata->gListVector = NULL;
168                 
169                 return 0;
170         }
171         catch(exception& e) {
172                 errorOut(e, "GetListCountCommand", "execute");
173                 exit(1);
174         }
175 }
176
177 //**********************************************************************************************************************
178 //return 1 if error, 0 otherwise
179 void GetListCountCommand::process(ListVector* list) {
180         try {
181                 string binnames, name, sequence;
182                 string outputFileName = getRootName(listfile) + list->getLabel() + ".otu";
183                 openOutputFile(outputFileName, out);
184                 
185                 mothurOut(list->getLabel()); mothurOutEndLine();
186                 
187                 //for each bin in the list vector
188                 for (int i = 0; i < list->getNumBins(); i++) {
189                         binnames = list->get(i);
190                         out << i+1 << '\t' << binnames << endl;
191                 }
192                 
193                 out.close();
194         }
195         catch(exception& e) {
196                 errorOut(e, "GetListCountCommand", "process");
197                 exit(1);
198         }
199 }
200 //**********************************************************************************************************************
201
202