5 * Created by Sarah Westcott on 1/20/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "readdistcommand.h"
11 #include "readphylip.h"
12 #include "readcolumn.h"
13 #include "readmatrix.hpp"
15 //**********************************************************************************************************************
16 ReadDistCommand::ReadDistCommand(string option) {
18 abort = false; calledHelp = false;
20 //allow user to run help
21 if(option == "help") { help(); abort = true; calledHelp = true; }
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)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
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; }
39 globaldata->newRead();
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 = ""; }
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; }
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; }
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; }
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; }
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 = ""; }
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"); }
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"); }
93 groupfile = validParameter.validFile(parameters, "group", true);
94 if (groupfile == "not open") { abort = true; }
95 else if (groupfile == "not found") { groupfile = ""; }
97 globaldata->setGroupFile(groupfile);
98 //groupMap = new GroupMap(groupfile);
99 //groupMap->readMap();
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); }
107 //you are doing a list and group shared
108 if ((phylipfile != "") && (groupfile != "")) {
109 globaldata->setFormat("matrix"); }
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; }
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; }
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
122 temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; }
123 convert(temp, precision);
125 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
126 sim = m->isTrue(temp);
127 globaldata->sim = sim;
129 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; }
130 convert(temp, cutoff);
131 cutoff += (5 / (precision * 10.0));
133 if (abort == false) {
134 distFileName = globaldata->inputFileName;
135 format = globaldata->getFormat();
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; }
144 if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; }
145 globaldata->gGroupmap = groupMap;
149 if (format != "matrix" ) {
150 read->setCutoff(cutoff);
153 nameMap = new NameAssignment(namefile);
164 catch(exception& e) {
165 m->errorOut(e, "ReadDistCommand", "ReadDistCommand");
169 //**********************************************************************************************************************
170 int ReadDistCommand::execute(){
173 if (abort == true) { if (calledHelp) { return 0; } return 2; }
174 m->mothurOut(getHelpString()); m->mothurOutEndLine();
176 time_t start = time(NULL);
179 if (format == "matrix") {
181 m->openInputFile(distFileName, in);
182 matrix = new FullMatrix(in); //reads the matrix file
185 if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
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); }
193 string newGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "editted.groups";
194 outputNames.push_back(newGroupFile);
196 m->openOutputFile(newGroupFile, outGroups);
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; }
201 Names temp = matrix->getRowInfo(i);
202 outGroups << temp.seqName << '\t' << temp.groupName << endl;
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();
209 delete groupMap; groupMap = NULL;
210 groupfile = newGroupFile;
211 globaldata->setGroupFile(groupfile);
213 groupMap = new GroupMap(groupfile);
216 if (m->control_pressed) { delete groupMap; delete matrix; remove(newGroupFile.c_str()); return 0; }
218 globaldata->gGroupmap = groupMap;
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];
227 //to prevent memory leak
229 if (m->control_pressed) { return 0; }
231 if (globaldata->gListVector != NULL) { delete globaldata->gListVector; }
232 globaldata->gListVector = read->getListVector();
234 if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix; }
235 globaldata->gSparseMatrix = read->getMatrix();
236 numDists = globaldata->gSparseMatrix->getNNodes();
239 if (m->control_pressed) { return 0; }
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();
248 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to read "); m->mothurOutEndLine();
253 catch(exception& e) {
254 m->errorOut(e, "ReadDistCommand", "execute");