]> git.donarmstrong.com Git - mothur.git/blob - readdistcommand.cpp
31d8308414755601166268f6088dd7142f7dfaad
[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                         parser = new OptionParser();
29                         parser->parse(option, parameters);  delete parser;
30                         
31                         ValidParameters* validParameter = new ValidParameters();
32                 
33                         //check to make sure all parameters are valid for command
34                         for (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                         
66                         //you are doing a list and group shared
67                         if ((phylipfile != "") && (groupfile != "")) { globaldata->setFormat("matrix"); }
68                         
69                         if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist command you must enter a phylip or a column." << endl; abort = true; }
70                         else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist command you must enter ONLY ONE of the following: phylip or column." << endl; 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                         delete validParameter;
88                         
89                         if (abort == false) {
90                                 filename = globaldata->inputFileName;
91                                 format = globaldata->getFormat();       
92                 
93                                 if (format == "column") { read = new ReadColumnMatrix(filename); }      
94                                 else if (format == "phylip") { read = new ReadPhylipMatrix(filename); }
95                                 else if (format == "matrix") { 
96                                         groupMap = new GroupMap(groupfile);
97                                         groupMap->readMap();
98                                         if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
99                                         globaldata->gGroupmap = groupMap;
100                                 }
101                 
102                                 if (format != "matrix" ) {
103                                         read->setCutoff(cutoff);
104         
105                                         if(namefile != ""){     
106                                                 nameMap = new NameAssignment(namefile);
107                                                 nameMap->readMap(1,2);
108                                         }else{
109                                                 nameMap = NULL;
110                                         }
111                                 }
112                         }
113
114                 }
115
116         }
117         catch(exception& e) {
118                 cout << "Standard Error: " << e.what() << " has occurred in the ReadDistCommand class Function ReadDistCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
119                 exit(1);
120         }
121         catch(...) {
122                 cout << "An unknown error has occurred in the ReadDistCommand class function ReadDistCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123                 exit(1);
124         }
125 }
126 //**********************************************************************************************************************
127
128 void ReadDistCommand::help(){
129         try {
130                 cout << "The read.dist command parameter options are phylip or column, group, name, cutoff and precision" << "\n";
131                 cout << "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";
132                 cout << "For this use the read.dist command should be in the following format: " << "\n";
133                 cout << "read.dist(phylip=yourDistFile, name=yourNameFile, cutoff=yourCutoff, precision=yourPrecision) " << "\n";
134                 cout << "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";
135                 cout << "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";
136                 cout << "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";
137                 cout << "For this use the read.dist command should be in the following format: " << "\n";
138                 cout << "read.dist(phylip=yourPhylipfile, group=yourGroupFile). The cutoff and precision parameters are not valid with this use.  " << "\n";
139                 cout << "Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourPhylipfile)." << "\n" << "\n";
140         }
141         catch(exception& e) {
142                 cout << "Standard Error: " << e.what() << " has occurred in the ReadDistCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143                 exit(1);
144         }
145         catch(...) {
146                 cout << "An unknown error has occurred in the ReadDistCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }       
149 }
150
151 //**********************************************************************************************************************
152 ReadDistCommand::~ReadDistCommand(){
153         delete read;
154         delete nameMap;
155 }
156
157 //**********************************************************************************************************************
158 int ReadDistCommand::execute(){
159         try {
160                 
161                 if (abort == true) {    return 0;       }
162                 
163                 if (format == "matrix") {
164                         ifstream in;
165                         openInputFile(filename, in);
166                         matrix = new FullMatrix(in); //reads the matrix file
167                         //memory leak prevention
168                         //if (globaldata->gMatrix != NULL) { delete globaldata->gMatrix;  }
169                         globaldata->gMatrix = matrix; //save matrix for coverage commands
170                 }else {
171                         read->read(nameMap);
172                         //to prevent memory leak
173                         if (globaldata->gListVector != NULL) { delete globaldata->gListVector;  }
174                         globaldata->gListVector = read->getListVector();
175                         if (globaldata->gSparseMatrix != NULL) { delete globaldata->gSparseMatrix;  }
176                         globaldata->gSparseMatrix = read->getMatrix();
177                 }
178                 return 0;
179         }
180         catch(exception& e) {
181                 cout << "Standard Error: " << e.what() << " has occurred in the ReadDistCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182                 exit(1);
183         }
184         catch(...) {
185                 cout << "An unknown error has occurred in the ReadDistCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186                 exit(1);
187         }
188 }