]> git.donarmstrong.com Git - mothur.git/blob - readdistcommand.cpp
4ea630f476ae99cef0b9ef8f65b86a2f44a10873
[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","sim"};
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 == "")) { m->mothurOut("When executing a read.dist command you must enter a phylip or a column."); m->mothurOutEndLine(); abort = true; }
112                         else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a read.dist command you must enter ONLY ONE of the following: phylip or column."); m->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, "sim", false);                              if (temp == "not found") { temp = "F"; }
126                         sim = isTrue(temp); 
127                         globaldata->sim = sim;
128                         
129                         temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10"; }
130                         convert(temp, cutoff); 
131                         cutoff += (5 / (precision * 10.0)); 
132                         
133                         if (abort == false) {
134                                 distFileName = globaldata->inputFileName;
135                                 format = globaldata->getFormat();       
136                 
137                                 if (format == "column") { read = new ReadColumnMatrix(distFileName); }  
138                                 else if (format == "phylip") { read = new ReadPhylipMatrix(distFileName); }
139                                 else if (format == "matrix") { 
140                                         groupMap = new GroupMap(groupfile);
141                                         int error = groupMap->readMap();
142                                         if (error == 1) { delete groupMap; abort = true; }
143                                         else {
144                                                 if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
145                                                 globaldata->gGroupmap = groupMap;
146                                         }
147                                 }
148                 
149                                 if (format != "matrix" ) {
150                                         read->setCutoff(cutoff);
151         
152                                         if(namefile != ""){     
153                                                 nameMap = new NameAssignment(namefile);
154                                                 nameMap->readMap();
155                                         }else{
156                                                 nameMap = NULL;
157                                         }
158                                 }
159                         }
160
161                 }
162
163         }
164         catch(exception& e) {
165                 m->errorOut(e, "ReadDistCommand", "ReadDistCommand");
166                 exit(1);
167         }
168 }
169 //**********************************************************************************************************************
170
171 void ReadDistCommand::help(){
172         try {
173                 m->mothurOut("The read.dist command parameter options are phylip or column, group, name, sim, cutoff and precision\n");
174                 m->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");
175                 m->mothurOut("For this use the read.dist command should be in the following format: \n");
176                 m->mothurOut("read.dist(phylip=yourDistFile, name=yourNameFile, cutoff=yourCutoff, precision=yourPrecision) \n");
177                 m->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");
178                 m->mothurOut("The sim parameter is used to indicate that your distance file contains similarity values instead of distance values. The default is false, if sim=true then mothur will convert the similarity values to distances. \n");
179                 m->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");
180                 m->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");
181                 m->mothurOut("For this use the read.dist command should be in the following format: \n");
182                 m->mothurOut("read.dist(phylip=yourPhylipfile, group=yourGroupFile). The cutoff and precision parameters are not valid with this use.  \n");
183                 m->mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourPhylipfile).\n\n");
184         }
185         catch(exception& e) {
186                 m->errorOut(e, "ReadDistCommand", "help");
187                 exit(1);
188         }
189 }
190
191 //**********************************************************************************************************************
192
193 ReadDistCommand::~ReadDistCommand(){
194         if (abort == false) {
195                 if (format != "matrix") { 
196                         delete read; 
197                         delete nameMap; 
198                 }
199         }
200 }
201
202 //**********************************************************************************************************************
203 int ReadDistCommand::execute(){
204         try {
205                 
206                 if (abort == true) {    return 0;       }
207
208                 time_t start = time(NULL);
209                 size_t numDists = 0;
210                 
211                 vector<string> outputNames;
212                 
213                 if (format == "matrix") {
214                         ifstream in;
215                         openInputFile(distFileName, in);
216                         matrix = new FullMatrix(in); //reads the matrix file
217                         in.close();
218                         
219                         if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
220                         
221                         //if files don't match...
222                         if (matrix->getNumSeqs() < groupMap->getNumSeqs()) {  
223                                 m->mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences.");  m->mothurOutEndLine();                         
224                                 //create new group file
225                                 if(outputDir == "") { outputDir += hasPath(groupfile); }
226                                 
227                                 string newGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "editted.groups";
228                                 outputNames.push_back(newGroupFile);
229                                 ofstream outGroups;
230                                 openOutputFile(newGroupFile, outGroups);
231                                 
232                                 for (int i = 0; i < matrix->getNumSeqs(); i++) {
233                                         if (m->control_pressed) { delete groupMap; delete matrix; outGroups.close(); remove(newGroupFile.c_str()); return 0; }
234                                         
235                                         Names temp = matrix->getRowInfo(i);
236                                         outGroups << temp.seqName << '\t' << temp.groupName << endl;
237                                 }
238                                 outGroups.close();
239                                 
240                                 m->mothurOut(newGroupFile + " is a new group file containing only the sequence that are in your distance file. I will read this file instead."); m->mothurOutEndLine();
241                                 
242                                 //read new groupfile
243                                 delete groupMap; groupMap = NULL;
244                                 groupfile = newGroupFile;
245                                 globaldata->setGroupFile(groupfile); 
246                                 
247                                 groupMap = new GroupMap(groupfile);
248                                 groupMap->readMap();
249                                 
250                                 if (m->control_pressed) { delete groupMap; delete matrix; remove(newGroupFile.c_str()); return 0; }
251         
252                                 globaldata->gGroupmap = groupMap;
253                         }
254                         
255                         //memory leak prevention
256                         if (globaldata->gMatrix != NULL) { delete globaldata->gMatrix;  }
257                         globaldata->gMatrix = matrix; //save matrix for coverage commands
258                         numDists = matrix->getSizes()[1];
259                 } else {
260                         read->read(nameMap);
261                         //to prevent memory leak
262                         
263                         if (m->control_pressed) {  return 0; }
264                 
265                         if (globaldata->gListVector != NULL) {  delete globaldata->gListVector;  }
266                         globaldata->gListVector = read->getListVector();
267
268                         if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix;  }
269                         globaldata->gSparseMatrix = read->getMatrix();
270                         numDists = globaldata->gSparseMatrix->getNNodes();
271                 }
272                 
273                 if (m->control_pressed) {  return 0; }
274
275                 if (outputNames.size() != 0) {
276                         m->mothurOutEndLine();
277                         m->mothurOut("Output File Name: "); m->mothurOutEndLine();
278                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
279                         m->mothurOutEndLine();
280                 }
281                 
282                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to read "); m->mothurOutEndLine();
283                 return 0;
284                 
285         }
286         catch(exception& e) {
287                 m->errorOut(e, "ReadDistCommand", "execute");
288                 exit(1);
289         }
290 }