]> git.donarmstrong.com Git - mothur.git/blob - readdistcommand.cpp
f6b41951962b7ce6b85ae50a9958bc0408c13835
[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 ReadDistCommand::ReadDistCommand(string option){
16         try {
17                 globaldata = GlobalData::getInstance();
18                 abort = false;
19                 
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; }
22                 
23                 else {
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)));
27                         
28                         OptionParser parser(option);
29                         map<string, string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                 
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;  }
36                         }
37                         
38                         globaldata->newRead();
39                         
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");        }
45                         
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"); }
50                         
51                         groupfile = validParameter.validFile(parameters, "group", true);
52                         if (groupfile == "not open") { abort = true; }  
53                         else if (groupfile == "not found") { groupfile = ""; }
54                         else {  
55                                 globaldata->setGroupFile(groupfile); 
56                                 //groupMap = new GroupMap(groupfile);
57                                 //groupMap->readMap();
58                         }
59
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);      }
64                         
65                         //you are doing a list and group shared
66                         if ((phylipfile != "") && (groupfile != "")) { 
67                         globaldata->setFormat("matrix"); }
68                         
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; }
71                 
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; }
74                         }
75                         
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
79                         string temp;
80                         temp = validParameter.validFile(parameters, "precision", false);                        if (temp == "not found") { temp = "100"; }
81                         convert(temp, precision); 
82                         
83                         temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10"; }
84                         convert(temp, cutoff); 
85                         cutoff += (5 / (precision * 10.0));
86                         
87                         if (abort == false) {
88                                 distFileName = globaldata->inputFileName;
89                                 format = globaldata->getFormat();       
90                 
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);
95                                         groupMap->readMap();
96         
97                                         if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
98                                         globaldata->gGroupmap = groupMap;
99                                 }
100                 
101                                 if (format != "matrix" ) {
102                                         read->setCutoff(cutoff);
103         
104                                         if(namefile != ""){     
105                                                 nameMap = new NameAssignment(namefile);
106                                                 nameMap->readMap();
107                                         }else{
108                                                 nameMap = NULL;
109                                         }
110                                 }
111                         }
112
113                 }
114
115         }
116         catch(exception& e) {
117                 errorOut(e, "ReadDistCommand", "ReadDistCommand");
118                 exit(1);
119         }
120 }
121 //**********************************************************************************************************************
122
123 void ReadDistCommand::help(){
124         try {
125                 mothurOut("The read.dist command parameter options are phylip or column, group, name, cutoff and precision\n");
126                 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");
127                 mothurOut("For this use the read.dist command should be in the following format: \n");
128                 mothurOut("read.dist(phylip=yourDistFile, name=yourNameFile, cutoff=yourCutoff, precision=yourPrecision) \n");
129                 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");
130                 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");
131                 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");
132                 mothurOut("For this use the read.dist command should be in the following format: \n");
133                 mothurOut("read.dist(phylip=yourPhylipfile, group=yourGroupFile). The cutoff and precision parameters are not valid with this use.  \n");
134                 mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourPhylipfile).\n\n");
135         }
136         catch(exception& e) {
137                 errorOut(e, "ReadDistCommand", "help");
138                 exit(1);
139         }
140 }
141
142 //**********************************************************************************************************************
143
144 ReadDistCommand::~ReadDistCommand(){
145         if (abort == false) {
146                 if (format != "matrix") { 
147                         delete read; 
148                         delete nameMap; 
149                 }
150         }
151 }
152
153 //**********************************************************************************************************************
154 int ReadDistCommand::execute(){
155         try {
156                 
157                 if (abort == true) {    return 0;       }
158
159                 time_t start = time(NULL);
160                 size_t numDists = 0;
161                 
162                 if (format == "matrix") {
163                         ifstream in;
164                         openInputFile(distFileName, in);
165                         matrix = new FullMatrix(in); //reads the matrix file
166                         in.close();
167                         //memory leak prevention
168                         if (globaldata->gMatrix != NULL) { delete globaldata->gMatrix;  }
169                         globaldata->gMatrix = matrix; //save matrix for coverage commands
170                         numDists = matrix->getSizes()[1];
171                 } else {
172                         read->read(nameMap);
173                         //to prevent memory leak
174
175                         if (globaldata->gListVector != NULL) {  delete globaldata->gListVector;  }
176                         globaldata->gListVector = read->getListVector();
177
178                         if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix;  }
179                         globaldata->gSparseMatrix = read->getMatrix();
180                         numDists = globaldata->gSparseMatrix->getNNodes();
181                         
182       int lines = cutoff / (1.0/precision);
183       vector<float> dist_cutoff(lines+1,0);
184                         for (int i = 0; i <= lines;i++) {       
185         dist_cutoff[i] = (i + 0.5) / precision; 
186       } 
187       vector<int> dist_count(lines+1,0);
188       list<PCell>::iterator currentCell;
189       SparseMatrix* smatrix = globaldata->gSparseMatrix;
190                 for (currentCell = smatrix->begin(); currentCell != smatrix->end(); currentCell++) {
191                                 for (int i = 0; i <= lines;i++) {       
192                                         if (currentCell->dist < dist_cutoff[i]) {
193                                                 dist_count[i]++;
194             break;
195           }
196         }
197                         }
198
199      // string dist_string = "Dist:";
200     //  string count_string = "Count: ";
201                         //for (int i = 0; i <= lines;i++) {     
202         //dist_string = dist_string.append("\t").append(toString(dist_cutoff[i]));
203       //        count_string = count_string.append("\t").append(toString(dist_count[i]));
204                 //      }
205       //mothurOut(dist_string); mothurOutEndLine(); mothurOut(count_string); mothurOutEndLine();
206                 }
207                 mothurOut("It took " + toString(time(NULL) - start) + " secs to read "); mothurOutEndLine();
208                 return 0;
209                 
210         }
211         catch(exception& e) {
212                 errorOut(e, "ReadDistCommand", "execute");
213                 exit(1);
214         }
215 }