]> git.donarmstrong.com Git - mothur.git/blob - readdistcommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[mothur.git] / readdistcommand.cpp
1 /*
2  *  readdistcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/20/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "readdistcommand.h"
11 #include "readphylip.h"
12 #include "readcolumn.h"
13 #include "readmatrix.hpp"
14
15 ReadDistCommand::ReadDistCommand(string option){
16         try {
17                 globaldata = GlobalData::getInstance();
18                 abort = false;
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 Array[] =  {"phylip", "column", "name", "cutoff", "precision", "group","outputdir","inputdir"};
26                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string, string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                         map<string,string>::iterator it;
33                 
34                         //check to make sure all parameters are valid for command
35                         for (it = parameters.begin(); it != parameters.end(); it++) { 
36                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
37                         }
38                         
39                         globaldata->newRead();
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("phylip");
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["phylip"] = inputDir + it->second;           }
52                                 }
53                                 
54                                 it = parameters.find("column");
55                                 //user has given a template file
56                                 if(it != parameters.end()){ 
57                                         path = hasPath(it->second);
58                                         //if the user has not given a path then, add inputdir. else leave path alone.
59                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
60                                 }
61                                 
62                                 it = parameters.find("name");
63                                 //user has given a template file
64                                 if(it != parameters.end()){ 
65                                         path = hasPath(it->second);
66                                         //if the user has not given a path then, add inputdir. else leave path alone.
67                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
68                                 }
69                                 
70                                 it = parameters.find("group");
71                                 //user has given a template file
72                                 if(it != parameters.end()){ 
73                                         path = hasPath(it->second);
74                                         //if the user has not given a path then, add inputdir. else leave path alone.
75                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
76                                 }
77                         }
78
79                         //if the user changes the output directory command factory will send this info to us in the output parameter 
80                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
81
82                         //check for required parameters
83                         phylipfile = validParameter.validFile(parameters, "phylip", true);
84                         if (phylipfile == "not open") { abort = true; }
85                         else if (phylipfile == "not found") { phylipfile = ""; }        
86                         else {  globaldata->setPhylipFile(phylipfile);  globaldata->setFormat("phylip");        }
87                         
88                         columnfile = validParameter.validFile(parameters, "column", true);
89                         if (columnfile == "not open") { abort = true; } 
90                         else if (columnfile == "not found") { columnfile = ""; }
91                         else {  globaldata->setColumnFile(columnfile); globaldata->setFormat("column"); }
92                         
93                         groupfile = validParameter.validFile(parameters, "group", true);
94                         if (groupfile == "not open") { abort = true; }  
95                         else if (groupfile == "not found") { groupfile = ""; }
96                         else {  
97                                 globaldata->setGroupFile(groupfile); 
98                                 //groupMap = new GroupMap(groupfile);
99                                 //groupMap->readMap();
100                         }
101
102                         namefile = validParameter.validFile(parameters, "name", true);
103                         if (namefile == "not open") { abort = true; }   
104                         else if (namefile == "not found") { namefile = ""; }
105                         else {  globaldata->setNameFile(namefile);      }
106                         
107                         //you are doing a list and group shared
108                         if ((phylipfile != "") && (groupfile != "")) { 
109                         globaldata->setFormat("matrix"); }
110                         
111                         if ((phylipfile == "") && (columnfile == "")) { mothurOut("When executing a read.dist command you must enter a phylip or a column."); mothurOutEndLine(); abort = true; }
112                         else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When executing a read.dist command you must enter ONLY ONE of the following: phylip or column."); mothurOutEndLine(); abort = true; }
113                 
114                         if (columnfile != "") {
115                                 if (namefile == "") {  cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
116                         }
117                         
118                         //check for optional parameter and set defaults
119                         // ...at some point should added some additional type checking...
120                         //get user cutoff and precision or use defaults
121                         string temp;
122                         temp = validParameter.validFile(parameters, "precision", false);                        if (temp == "not found") { temp = "100"; }
123                         convert(temp, precision); 
124                         
125                         temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10"; }
126                         convert(temp, cutoff); 
127                         cutoff += (5 / (precision * 10.0));
128                         
129                         if (abort == false) {
130                                 distFileName = globaldata->inputFileName;
131                                 format = globaldata->getFormat();       
132                 
133                                 if (format == "column") { read = new ReadColumnMatrix(distFileName); }  
134                                 else if (format == "phylip") { read = new ReadPhylipMatrix(distFileName); }
135                                 else if (format == "matrix") { 
136                                         groupMap = new GroupMap(groupfile);
137                                         int error = groupMap->readMap();
138                                         if (error == 1) { delete groupMap; abort = true; }
139                                         else {
140                                                 if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
141                                                 globaldata->gGroupmap = groupMap;
142                                         }
143                                 }
144                 
145                                 if (format != "matrix" ) {
146                                         read->setCutoff(cutoff);
147         
148                                         if(namefile != ""){     
149                                                 nameMap = new NameAssignment(namefile);
150                                                 nameMap->readMap();
151                                         }else{
152                                                 nameMap = NULL;
153                                         }
154                                 }
155                         }
156
157                 }
158
159         }
160         catch(exception& e) {
161                 errorOut(e, "ReadDistCommand", "ReadDistCommand");
162                 exit(1);
163         }
164 }
165 //**********************************************************************************************************************
166
167 void ReadDistCommand::help(){
168         try {
169                 mothurOut("The read.dist command parameter options are phylip or column, group, name, cutoff and precision\n");
170                 mothurOut("The read.dist command can be used in two ways.  The first is to read a phylip or column and run the cluster command\n");
171                 mothurOut("For this use the read.dist command should be in the following format: \n");
172                 mothurOut("read.dist(phylip=yourDistFile, name=yourNameFile, cutoff=yourCutoff, precision=yourPrecision) \n");
173                 mothurOut("The phylip or column parameter is required, but only one may be used.  If you use a column file the name filename is required. \n");
174                 mothurOut("If you do not provide a cutoff value 10.00 is assumed. If you do not provide a precision value then 100 is assumed.\n");
175                 mothurOut("The second way to use the read.dist command is to read a phylip or column and a group, so you can use the libshuff command.\n");
176                 mothurOut("For this use the read.dist command should be in the following format: \n");
177                 mothurOut("read.dist(phylip=yourPhylipfile, group=yourGroupFile). The cutoff and precision parameters are not valid with this use.  \n");
178                 mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourPhylipfile).\n\n");
179         }
180         catch(exception& e) {
181                 errorOut(e, "ReadDistCommand", "help");
182                 exit(1);
183         }
184 }
185
186 //**********************************************************************************************************************
187
188 ReadDistCommand::~ReadDistCommand(){
189         if (abort == false) {
190                 if (format != "matrix") { 
191                         delete read; 
192                         delete nameMap; 
193                 }
194         }
195 }
196
197 //**********************************************************************************************************************
198 int ReadDistCommand::execute(){
199         try {
200                 
201                 if (abort == true) {    return 0;       }
202
203                 time_t start = time(NULL);
204                 size_t numDists = 0;
205                 
206                 if (format == "matrix") {
207                         ifstream in;
208                         openInputFile(distFileName, in);
209                         matrix = new FullMatrix(in); //reads the matrix file
210                         in.close();
211                         
212                         //if files don't match...
213                         if (matrix->getNumSeqs() < groupMap->getNumSeqs()) {  
214                                 mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences.");  mothurOutEndLine();                               
215                                 //create new group file
216                                 if(outputDir == "") { outputDir += hasPath(groupfile); }
217                                 
218                                 string newGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "editted.groups";
219                                 ofstream outGroups;
220                                 openOutputFile(newGroupFile, outGroups);
221                                 
222                                 for (int i = 0; i < matrix->getNumSeqs(); i++) {
223                                         Names temp = matrix->getRowInfo(i);
224                                         outGroups << temp.seqName << '\t' << temp.groupName << endl;
225                                 }
226                                 outGroups.close();
227                                 
228                                 mothurOut(newGroupFile + " is a new group file containing only the sequence that are in your distance file. I will read this file instead."); mothurOutEndLine();
229                                 
230                                 //read new groupfile
231                                 delete groupMap; groupMap = NULL;
232                                 groupfile = newGroupFile;
233                                 globaldata->setGroupFile(groupfile); 
234                                 
235                                 groupMap = new GroupMap(groupfile);
236                                 groupMap->readMap();
237                                 
238                                 globaldata->gGroupmap = groupMap;
239                         }
240                         
241                         //memory leak prevention
242                         if (globaldata->gMatrix != NULL) { delete globaldata->gMatrix;  }
243                         globaldata->gMatrix = matrix; //save matrix for coverage commands
244                         numDists = matrix->getSizes()[1];
245                 } else {
246                         read->read(nameMap);
247                         //to prevent memory leak
248
249                         if (globaldata->gListVector != NULL) {  delete globaldata->gListVector;  }
250                         globaldata->gListVector = read->getListVector();
251
252                         if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix;  }
253                         globaldata->gSparseMatrix = read->getMatrix();
254                         numDists = globaldata->gSparseMatrix->getNNodes();
255         //cout << "matrix contains " << numDists << " distances." << endl;
256                         
257     /*  int lines = cutoff / (1.0/precision);
258       vector<float> dist_cutoff(lines+1,0);
259                         for (int i = 0; i <= lines;i++) {       
260         dist_cutoff[i] = (i + 0.5) / precision; 
261       } 
262       vector<int> dist_count(lines+1,0);
263       list<PCell>::iterator currentCell;
264       SparseMatrix* smatrix = globaldata->gSparseMatrix;
265                 for (currentCell = smatrix->begin(); currentCell != smatrix->end(); currentCell++) {
266                                 for (int i = 0; i <= lines;i++) {       
267                                         if (currentCell->dist < dist_cutoff[i]) {
268                                                 dist_count[i]++;
269             break;
270           }
271         }
272                         }
273 */
274      // string dist_string = "Dist:";
275     //  string count_string = "Count: ";
276                         //for (int i = 0; i <= lines;i++) {     
277         //dist_string = dist_string.append("\t").append(toString(dist_cutoff[i]));
278       //        count_string = count_string.append("\t").append(toString(dist_count[i]));
279                 //      }
280       //mothurOut(dist_string); mothurOutEndLine(); mothurOut(count_string); mothurOutEndLine();
281                 }
282                 mothurOut("It took " + toString(time(NULL) - start) + " secs to read "); mothurOutEndLine();
283                 return 0;
284                 
285         }
286         catch(exception& e) {
287                 errorOut(e, "ReadDistCommand", "execute");
288                 exit(1);
289         }
290 }