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; }
22 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
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)));
29 OptionParser parser(option);
30 map<string, string> parameters = parser.getParameters();
32 ValidParameters validParameter;
33 map<string,string>::iterator it;
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; }
40 globaldata->newRead();
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 = ""; }
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; }
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; }
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; }
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; }
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 = ""; }
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"); }
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"); }
94 groupfile = validParameter.validFile(parameters, "group", true);
95 if (groupfile == "not open") { abort = true; }
96 else if (groupfile == "not found") { groupfile = ""; }
98 globaldata->setGroupFile(groupfile);
99 //groupMap = new GroupMap(groupfile);
100 //groupMap->readMap();
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); }
108 //you are doing a list and group shared
109 if ((phylipfile != "") && (groupfile != "")) {
110 globaldata->setFormat("matrix"); }
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; }
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; }
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
123 temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; }
124 m->mothurConvert(temp, precision);
126 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
127 sim = m->isTrue(temp);
128 globaldata->sim = sim;
130 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; }
131 convert(temp, cutoff);
132 cutoff += (5 / (precision * 10.0));
134 if (abort == false) {
135 distFileName = globaldata->inputFileName;
136 format = globaldata->getFormat();
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; }
145 if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; }
146 globaldata->gGroupmap = groupMap;
150 if (format != "matrix" ) {
151 read->setCutoff(cutoff);
154 nameMap = new NameAssignment(namefile);
165 catch(exception& e) {
166 m->errorOut(e, "ReadDistCommand", "ReadDistCommand");
170 //**********************************************************************************************************************
171 int ReadDistCommand::execute(){
174 if (abort == true) { if (calledHelp) { return 0; } return 2; }
175 m->mothurOut(getHelpString()); m->mothurOutEndLine();
177 time_t start = time(NULL);
180 if (format == "matrix") {
182 m->openInputFile(distFileName, in);
183 matrix = new FullMatrix(in); //reads the matrix file
186 if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
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); }
194 string newGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "editted.groups";
195 outputNames.push_back(newGroupFile);
197 m->openOutputFile(newGroupFile, outGroups);
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; }
202 Names temp = matrix->getRowInfo(i);
203 outGroups << temp.seqName << '\t' << temp.groupName << endl;
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();
210 delete groupMap; groupMap = NULL;
211 groupfile = newGroupFile;
212 globaldata->setGroupFile(groupfile);
214 groupMap = new GroupMap(groupfile);
217 if (m->control_pressed) { delete groupMap; delete matrix; m->mothurRemove(newGroupFile); return 0; }
219 globaldata->gGroupmap = groupMap;
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];
228 //to prevent memory leak
230 if (m->control_pressed) { return 0; }
232 if (globaldata->gListVector != NULL) { delete globaldata->gListVector; }
233 globaldata->gListVector = read->getListVector();
235 if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix; }
236 globaldata->gSparseMatrix = read->getMatrix();
237 numDists = globaldata->gSparseMatrix->getNNodes();
240 if (m->control_pressed) { return 0; }
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();
249 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to read "); m->mothurOutEndLine();
254 catch(exception& e) {
255 m->errorOut(e, "ReadDistCommand", "execute");