]> git.donarmstrong.com Git - mothur.git/blob - readdistcommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[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 //**********************************************************************************************************************
16 ReadDistCommand::ReadDistCommand(string option) {
17         try {
18                 abort = false; calledHelp = false;   
19                 
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; calledHelp = 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 = m->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 = m->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 = m->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 = m->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 = m->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 int ReadDistCommand::execute(){
171         try {
172                 
173                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
174                 m->mothurOut(getHelpString()); m->mothurOutEndLine();
175 /*
176                 time_t start = time(NULL);
177                 size_t numDists = 0;
178                 
179                 if (format == "matrix") {
180                         ifstream in;
181                         m->openInputFile(distFileName, in);
182                         matrix = new FullMatrix(in); //reads the matrix file
183                         in.close();
184                         
185                         if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
186                         
187                         //if files don't match...
188                         if (matrix->getNumSeqs() < groupMap->getNumSeqs()) {  
189                                 m->mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences.");  m->mothurOutEndLine();                         
190                                 //create new group file
191                                 if(outputDir == "") { outputDir += m->hasPath(groupfile); }
192                                 
193                                 string newGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "editted.groups";
194                                 outputNames.push_back(newGroupFile);
195                                 ofstream outGroups;
196                                 m->openOutputFile(newGroupFile, outGroups);
197                                 
198                                 for (int i = 0; i < matrix->getNumSeqs(); i++) {
199                                         if (m->control_pressed) { delete groupMap; delete matrix; outGroups.close(); remove(newGroupFile.c_str()); return 0; }
200                                         
201                                         Names temp = matrix->getRowInfo(i);
202                                         outGroups << temp.seqName << '\t' << temp.groupName << endl;
203                                 }
204                                 outGroups.close();
205                                 
206                                 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();
207                                 
208                                 //read new groupfile
209                                 delete groupMap; groupMap = NULL;
210                                 groupfile = newGroupFile;
211                                 globaldata->setGroupFile(groupfile); 
212                                 
213                                 groupMap = new GroupMap(groupfile);
214                                 groupMap->readMap();
215                                 
216                                 if (m->control_pressed) { delete groupMap; delete matrix; remove(newGroupFile.c_str()); return 0; }
217         
218                                 globaldata->gGroupmap = groupMap;
219                         }
220                         
221                         //memory leak prevention
222                         if (globaldata->gMatrix != NULL) { delete globaldata->gMatrix;  }
223                         globaldata->gMatrix = matrix; //save matrix for coverage commands
224                         numDists = matrix->getSizes()[1];
225                 } else {
226                         read->read(nameMap);
227                         //to prevent memory leak
228                         
229                         if (m->control_pressed) {  return 0; }
230                 
231                         if (globaldata->gListVector != NULL) {  delete globaldata->gListVector;  }
232                         globaldata->gListVector = read->getListVector();
233
234                         if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix;  }
235                         globaldata->gSparseMatrix = read->getMatrix();
236                         numDists = globaldata->gSparseMatrix->getNNodes();
237                 }
238                 
239                 if (m->control_pressed) {  return 0; }
240
241                 if (outputNames.size() != 0) {
242                         m->mothurOutEndLine();
243                         m->mothurOut("Output File Name: "); m->mothurOutEndLine();
244                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
245                         m->mothurOutEndLine();
246                 }
247                 
248                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to read "); m->mothurOutEndLine();
249  */
250                 return 0;
251                 
252         }
253         catch(exception& e) {
254                 m->errorOut(e, "ReadDistCommand", "execute");
255                 exit(1);
256         }
257 }