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 ReadDistCommand::ReadDistCommand(string option) {
17 globaldata = GlobalData::getInstance();
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"phylip", "column", "name", "cutoff","hard", "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 = 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 = 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 = 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 = 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"; }
127 globaldata->sim = sim;
129 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "F"; }
132 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; }
133 convert(temp, cutoff);
134 if (!hard) { cutoff += (5 / (precision * 10.0)); }
136 if (abort == false) {
137 distFileName = globaldata->inputFileName;
138 format = globaldata->getFormat();
140 if (format == "column") { read = new ReadColumnMatrix(distFileName); }
141 else if (format == "phylip") { read = new ReadPhylipMatrix(distFileName); }
142 else if (format == "matrix") {
143 groupMap = new GroupMap(groupfile);
144 int error = groupMap->readMap();
145 if (error == 1) { delete groupMap; abort = true; }
147 if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; }
148 globaldata->gGroupmap = groupMap;
152 if (format != "matrix" ) {
153 read->setCutoff(cutoff);
156 nameMap = new NameAssignment(namefile);
167 catch(exception& e) {
168 m->errorOut(e, "ReadDistCommand", "ReadDistCommand");
172 //**********************************************************************************************************************
174 void ReadDistCommand::help(){
176 m->mothurOut("The read.dist command parameter options are phylip or column, group, name, sim, cutoff and precision\n");
177 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");
178 m->mothurOut("For this use the read.dist command should be in the following format: \n");
179 m->mothurOut("read.dist(phylip=yourDistFile, name=yourNameFile, cutoff=yourCutoff, precision=yourPrecision) \n");
180 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");
181 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");
182 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");
183 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");
184 m->mothurOut("For this use the read.dist command should be in the following format: \n");
185 m->mothurOut("read.dist(phylip=yourPhylipfile, group=yourGroupFile). The cutoff and precision parameters are not valid with this use. \n");
186 m->mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourPhylipfile).\n\n");
188 catch(exception& e) {
189 m->errorOut(e, "ReadDistCommand", "help");
194 //**********************************************************************************************************************
196 ReadDistCommand::~ReadDistCommand(){
197 if (abort == false) {
198 if (format != "matrix") {
205 //**********************************************************************************************************************
206 int ReadDistCommand::execute(){
209 if (abort == true) { return 0; }
211 time_t start = time(NULL);
214 vector<string> outputNames;
216 if (format == "matrix") {
218 openInputFile(distFileName, in);
219 matrix = new FullMatrix(in); //reads the matrix file
222 if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
224 //if files don't match...
225 if (matrix->getNumSeqs() < groupMap->getNumSeqs()) {
226 m->mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences."); m->mothurOutEndLine();
227 //create new group file
228 if(outputDir == "") { outputDir += hasPath(groupfile); }
230 string newGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "editted.groups";
231 outputNames.push_back(newGroupFile);
233 openOutputFile(newGroupFile, outGroups);
235 for (int i = 0; i < matrix->getNumSeqs(); i++) {
236 if (m->control_pressed) { delete groupMap; delete matrix; outGroups.close(); remove(newGroupFile.c_str()); return 0; }
238 Names temp = matrix->getRowInfo(i);
239 outGroups << temp.seqName << '\t' << temp.groupName << endl;
243 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();
246 delete groupMap; groupMap = NULL;
247 groupfile = newGroupFile;
248 globaldata->setGroupFile(groupfile);
250 groupMap = new GroupMap(groupfile);
253 if (m->control_pressed) { delete groupMap; delete matrix; remove(newGroupFile.c_str()); return 0; }
255 globaldata->gGroupmap = groupMap;
258 //memory leak prevention
259 if (globaldata->gMatrix != NULL) { delete globaldata->gMatrix; }
260 globaldata->gMatrix = matrix; //save matrix for coverage commands
261 numDists = matrix->getSizes()[1];
264 //to prevent memory leak
266 if (m->control_pressed) { return 0; }
268 if (globaldata->gListVector != NULL) { delete globaldata->gListVector; }
269 globaldata->gListVector = read->getListVector();
271 if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix; }
272 globaldata->gSparseMatrix = read->getMatrix();
273 numDists = globaldata->gSparseMatrix->getNNodes();
276 if (m->control_pressed) { return 0; }
278 if (outputNames.size() != 0) {
279 m->mothurOutEndLine();
280 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
281 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
282 m->mothurOutEndLine();
285 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to read "); m->mothurOutEndLine();
289 catch(exception& e) {
290 m->errorOut(e, "ReadDistCommand", "execute");