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", "precision", "group"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
33 //check to make sure all parameters are valid for command
34 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 globaldata->newRead();
40 //check for required parameters
41 phylipfile = validParameter.validFile(parameters, "phylip", true);
42 if (phylipfile == "not open") { abort = true; }
43 else if (phylipfile == "not found") { phylipfile = ""; }
44 else { globaldata->setPhylipFile(phylipfile); globaldata->setFormat("phylip"); }
46 columnfile = validParameter.validFile(parameters, "column", true);
47 if (columnfile == "not open") { abort = true; }
48 else if (columnfile == "not found") { columnfile = ""; }
49 else { globaldata->setColumnFile(columnfile); globaldata->setFormat("column"); }
51 groupfile = validParameter.validFile(parameters, "group", true);
52 if (groupfile == "not open") { abort = true; }
53 else if (groupfile == "not found") { groupfile = ""; }
55 globaldata->setGroupFile(groupfile);
56 //groupMap = new GroupMap(groupfile);
57 //groupMap->readMap();
60 namefile = validParameter.validFile(parameters, "name", true);
61 if (namefile == "not open") { abort = true; }
62 else if (namefile == "not found") { namefile = ""; }
63 else { globaldata->setNameFile(namefile); }
65 //you are doing a list and group shared
66 if ((phylipfile != "") && (groupfile != "")) {
67 globaldata->setFormat("matrix"); }
69 if ((phylipfile == "") && (columnfile == "")) { mothurOut("When executing a read.dist command you must enter a phylip or a column."); mothurOutEndLine(); abort = true; }
70 else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When executing a read.dist command you must enter ONLY ONE of the following: phylip or column."); mothurOutEndLine(); abort = true; }
72 if (columnfile != "") {
73 if (namefile == "") { cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
76 //check for optional parameter and set defaults
77 // ...at some point should added some additional type checking...
78 //get user cutoff and precision or use defaults
80 temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; }
81 convert(temp, precision);
83 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; }
84 convert(temp, cutoff);
85 cutoff += (5 / (precision * 10.0));
88 distFileName = globaldata->inputFileName;
89 format = globaldata->getFormat();
91 if (format == "column") { read = new ReadColumnMatrix(distFileName); }
92 else if (format == "phylip") { read = new ReadPhylipMatrix(distFileName); }
93 else if (format == "matrix") {
94 groupMap = new GroupMap(groupfile);
96 if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; }
97 globaldata->gGroupmap = groupMap;
100 if (format != "matrix" ) {
101 read->setCutoff(cutoff);
104 nameMap = new NameAssignment(namefile);
115 catch(exception& e) {
116 errorOut(e, "ReadDistCommand", "ReadDistCommand");
120 //**********************************************************************************************************************
122 void ReadDistCommand::help(){
124 mothurOut("The read.dist command parameter options are phylip or column, group, name, cutoff and precision\n");
125 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");
126 mothurOut("For this use the read.dist command should be in the following format: \n");
127 mothurOut("read.dist(phylip=yourDistFile, name=yourNameFile, cutoff=yourCutoff, precision=yourPrecision) \n");
128 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");
129 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");
130 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");
131 mothurOut("For this use the read.dist command should be in the following format: \n");
132 mothurOut("read.dist(phylip=yourPhylipfile, group=yourGroupFile). The cutoff and precision parameters are not valid with this use. \n");
133 mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourPhylipfile).\n\n");
135 catch(exception& e) {
136 errorOut(e, "ReadDistCommand", "help");
141 //**********************************************************************************************************************
143 ReadDistCommand::~ReadDistCommand(){
144 if (abort == false) {
145 if (format != "matrix") { delete read; delete nameMap; }
149 //**********************************************************************************************************************
150 int ReadDistCommand::execute(){
153 if (abort == true) { return 0; }
155 time_t start = time(NULL);
158 if (format == "matrix") {
160 openInputFile(distFileName, in);
161 matrix = new FullMatrix(in); //reads the matrix file
163 //memory leak prevention
164 if (globaldata->gMatrix != NULL) { delete globaldata->gMatrix; }
165 globaldata->gMatrix = matrix; //save matrix for coverage commands
166 numDists = matrix->getSizes()[1];
169 //to prevent memory leak
171 if (globaldata->gListVector != NULL) { delete globaldata->gListVector; }
172 globaldata->gListVector = read->getListVector();
174 if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix; }
175 globaldata->gSparseMatrix = read->getMatrix();
176 numDists = globaldata->gSparseMatrix->getNNodes();
178 int lines = cutoff / (1.0/precision);
179 vector<float> dist_cutoff(lines+1,0);
180 for (int i = 0; i <= lines;i++) {
181 dist_cutoff[i] = (i + 0.5) / precision;
183 vector<int> dist_count(lines+1,0);
184 list<PCell>::iterator currentCell;
185 SparseMatrix* smatrix = globaldata->gSparseMatrix;
186 for (currentCell = smatrix->begin(); currentCell != smatrix->end(); currentCell++) {
187 for (int i = 0; i <= lines;i++) {
188 if (currentCell->dist < dist_cutoff[i]) {
195 string dist_string = "Dist:";
196 string count_string = "Count: ";
197 for (int i = 0; i <= lines;i++) {
198 dist_string = dist_string.append("\t").append(toString(dist_cutoff[i]));
199 count_string = count_string.append("\t").append(toString(dist_count[i]));
201 mothurOut(dist_string); mothurOutEndLine(); mothurOut(count_string); mothurOutEndLine();
203 mothurOut("It took " + toString(time(NULL) - start) + " secs to read " + toString(numDists) + " distances (cutoff: " + toString(cutoff) + ")"); mothurOutEndLine();
207 catch(exception& e) {
208 errorOut(e, "ReadDistCommand", "execute");