]> git.donarmstrong.com Git - mothur.git/blob - readdistcommand.cpp
removing chime source files from mother project.
[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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
23                 
24                 else {
25                         /*//valid paramters for this command
26                         string Array[] =  {"phylip", "column", "name", "cutoff", "precision", "group","outputdir","inputdir","sim"};
27                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28                         
29                         OptionParser parser(option);
30                         map<string, string> parameters = parser.getParameters();
31                         
32                         ValidParameters validParameter;
33                         map<string,string>::iterator it;
34                 
35                         //check to make sure all parameters are valid for command
36                         for (it = parameters.begin(); it != parameters.end(); it++) { 
37                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
38                         }
39                         
40                         globaldata->newRead();
41                         
42                         //if the user changes the input directory command factory will send this info to us in the output parameter 
43                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
44                         if (inputDir == "not found"){   inputDir = "";          }
45                         else {
46                                 string path;
47                                 it = parameters.find("phylip");
48                                 //user has given a template file
49                                 if(it != parameters.end()){ 
50                                         path = m->hasPath(it->second);
51                                         //if the user has not given a path then, add inputdir. else leave path alone.
52                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
53                                 }
54                                 
55                                 it = parameters.find("column");
56                                 //user has given a template file
57                                 if(it != parameters.end()){ 
58                                         path = m->hasPath(it->second);
59                                         //if the user has not given a path then, add inputdir. else leave path alone.
60                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
61                                 }
62                                 
63                                 it = parameters.find("name");
64                                 //user has given a template file
65                                 if(it != parameters.end()){ 
66                                         path = m->hasPath(it->second);
67                                         //if the user has not given a path then, add inputdir. else leave path alone.
68                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
69                                 }
70                                 
71                                 it = parameters.find("group");
72                                 //user has given a template file
73                                 if(it != parameters.end()){ 
74                                         path = m->hasPath(it->second);
75                                         //if the user has not given a path then, add inputdir. else leave path alone.
76                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
77                                 }
78                         }
79
80                         //if the user changes the output directory command factory will send this info to us in the output parameter 
81                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
82
83                         //check for required parameters
84                         phylipfile = validParameter.validFile(parameters, "phylip", true);
85                         if (phylipfile == "not open") { abort = true; }
86                         else if (phylipfile == "not found") { phylipfile = ""; }        
87                         else {  globaldata->setPhylipFile(phylipfile);  globaldata->setFormat("phylip");        }
88                         
89                         columnfile = validParameter.validFile(parameters, "column", true);
90                         if (columnfile == "not open") { abort = true; } 
91                         else if (columnfile == "not found") { columnfile = ""; }
92                         else {  globaldata->setColumnFile(columnfile); globaldata->setFormat("column"); }
93                         
94                         groupfile = validParameter.validFile(parameters, "group", true);
95                         if (groupfile == "not open") { abort = true; }  
96                         else if (groupfile == "not found") { groupfile = ""; }
97                         else {  
98                                 globaldata->setGroupFile(groupfile); 
99                                 //groupMap = new GroupMap(groupfile);
100                                 //groupMap->readMap();
101                         }
102
103                         namefile = validParameter.validFile(parameters, "name", true);
104                         if (namefile == "not open") { abort = true; }   
105                         else if (namefile == "not found") { namefile = ""; }
106                         else {  globaldata->setNameFile(namefile);      }
107                         
108                         //you are doing a list and group shared
109                         if ((phylipfile != "") && (groupfile != "")) { 
110                         globaldata->setFormat("matrix"); }
111                         
112                         if ((phylipfile == "") && (columnfile == "")) { m->mothurOut("When executing a read.dist command you must enter a phylip or a column."); m->mothurOutEndLine(); abort = true; }
113                         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; }
114                 
115                         if (columnfile != "") {
116                                 if (namefile == "") {  cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
117                         }
118                         
119                         //check for optional parameter and set defaults
120                         // ...at some point should added some additional type checking...
121                         //get user cutoff and precision or use defaults
122                         string temp;
123                         temp = validParameter.validFile(parameters, "precision", false);                if (temp == "not found") { temp = "100"; }
124                         m->mothurConvert(temp, precision); 
125                         
126                         temp = validParameter.validFile(parameters, "sim", false);                              if (temp == "not found") { temp = "F"; }
127                         sim = m->isTrue(temp); 
128                         globaldata->sim = sim;
129                         
130                         temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10"; }
131                         convert(temp, cutoff); 
132                         cutoff += (5 / (precision * 10.0)); 
133                         
134                         if (abort == false) {
135                                 distFileName = globaldata->inputFileName;
136                                 format = globaldata->getFormat();       
137                 
138                                 if (format == "column") { read = new ReadColumnMatrix(distFileName); }  
139                                 else if (format == "phylip") { read = new ReadPhylipMatrix(distFileName); }
140                                 else if (format == "matrix") { 
141                                         groupMap = new GroupMap(groupfile);
142                                         int error = groupMap->readMap();
143                                         if (error == 1) { delete groupMap; abort = true; }
144                                         else {
145                                                 if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
146                                                 globaldata->gGroupmap = groupMap;
147                                         }
148                                 }
149                 
150                                 if (format != "matrix" ) {
151                                         read->setCutoff(cutoff);
152         
153                                         if(namefile != ""){     
154                                                 nameMap = new NameAssignment(namefile);
155                                                 nameMap->readMap();
156                                         }else{
157                                                 nameMap = NULL;
158                                         }
159                                 }
160                         }
161 */
162                 }
163
164         }
165         catch(exception& e) {
166                 m->errorOut(e, "ReadDistCommand", "ReadDistCommand");
167                 exit(1);
168         }
169 }
170 //**********************************************************************************************************************
171 int ReadDistCommand::execute(){
172         try {
173                 
174                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
175                 m->mothurOut(getHelpString()); m->mothurOutEndLine();
176 /*
177                 time_t start = time(NULL);
178                 size_t numDists = 0;
179                 
180                 if (format == "matrix") {
181                         ifstream in;
182                         m->openInputFile(distFileName, in);
183                         matrix = new FullMatrix(in); //reads the matrix file
184                         in.close();
185                         
186                         if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
187                         
188                         //if files don't match...
189                         if (matrix->getNumSeqs() < groupMap->getNumSeqs()) {  
190                                 m->mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences.");  m->mothurOutEndLine();                         
191                                 //create new group file
192                                 if(outputDir == "") { outputDir += m->hasPath(groupfile); }
193                                 
194                                 string newGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "editted.groups";
195                                 outputNames.push_back(newGroupFile);
196                                 ofstream outGroups;
197                                 m->openOutputFile(newGroupFile, outGroups);
198                                 
199                                 for (int i = 0; i < matrix->getNumSeqs(); i++) {
200                                         if (m->control_pressed) { delete groupMap; delete matrix; outGroups.close(); m->mothurRemove(newGroupFile); return 0; }
201                                         
202                                         Names temp = matrix->getRowInfo(i);
203                                         outGroups << temp.seqName << '\t' << temp.groupName << endl;
204                                 }
205                                 outGroups.close();
206                                 
207                                 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();
208                                 
209                                 //read new groupfile
210                                 delete groupMap; groupMap = NULL;
211                                 groupfile = newGroupFile;
212                                 globaldata->setGroupFile(groupfile); 
213                                 
214                                 groupMap = new GroupMap(groupfile);
215                                 groupMap->readMap();
216                                 
217                                 if (m->control_pressed) { delete groupMap; delete matrix; m->mothurRemove(newGroupFile); return 0; }
218         
219                                 globaldata->gGroupmap = groupMap;
220                         }
221                         
222                         //memory leak prevention
223                         if (globaldata->gMatrix != NULL) { delete globaldata->gMatrix;  }
224                         globaldata->gMatrix = matrix; //save matrix for coverage commands
225                         numDists = matrix->getSizes()[1];
226                 } else {
227                         read->read(nameMap);
228                         //to prevent memory leak
229                         
230                         if (m->control_pressed) {  return 0; }
231                 
232                         if (globaldata->gListVector != NULL) {  delete globaldata->gListVector;  }
233                         globaldata->gListVector = read->getListVector();
234
235                         if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix;  }
236                         globaldata->gSparseMatrix = read->getMatrix();
237                         numDists = globaldata->gSparseMatrix->getNNodes();
238                 }
239                 
240                 if (m->control_pressed) {  return 0; }
241
242                 if (outputNames.size() != 0) {
243                         m->mothurOutEndLine();
244                         m->mothurOut("Output File Name: "); m->mothurOutEndLine();
245                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
246                         m->mothurOutEndLine();
247                 }
248                 
249                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to read "); m->mothurOutEndLine();
250  */
251                 return 0;
252                 
253         }
254         catch(exception& e) {
255                 m->errorOut(e, "ReadDistCommand", "execute");
256                 exit(1);
257         }
258 }