5 * Created by Sarah Westcott on 5/7/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "distancecommand.h"
11 #include "ignoregaps.h"
12 #include "eachgapdist.h"
13 #include "eachgapignore.h"
14 #include "onegapdist.h"
15 #include "onegapignore.h"
17 //**********************************************************************************************************************
19 DistanceCommand::DistanceCommand(string option){
24 //allow user to run help
25 if(option == "help") { help(); abort = true; }
28 //valid paramters for this command
29 string Array[] = {"fasta", "phylip", "calc", "countends", "cutoff", "processors"};
30 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
32 OptionParser parser(option);
33 map<string, string> parameters = parser.getParameters();
35 ValidParameters validParameter;
37 //check to make sure all parameters are valid for command
38 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
39 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
42 //check for required parameters
43 fastafile = validParameter.validFile(parameters, "fasta", true);
44 if (fastafile == "not found") { cout << "fasta is a required parameter for the dist.seqs command." << endl; abort = true; }
45 else if (fastafile == "not open") { abort = true; }
48 openInputFile(fastafile, inFASTA);
49 alignDB = SequenceDB(inFASTA);
53 //check for optional parameter and set defaults
54 // ...at some point should added some additional type checking...
55 calc = validParameter.validFile(parameters, "calc", false);
56 if (calc == "not found") { calc = "onegap"; }
58 if (calc == "default") { calc = "onegap"; }
60 splitAtDash(calc, Estimators);
63 temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; }
64 convert(temp, countends);
66 temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; }
67 convert(temp, cutoff);
69 temp = validParameter.validFile(parameters, "processors", false); if(temp == "not found"){ temp = "1"; }
70 convert(temp, processors);
72 phylip = validParameter.validFile(parameters, "phylip", false); if(phylip == "not found"){ phylip = "F"; }
75 ValidCalculators validCalculator;
77 if (isTrue(countends) == true) {
78 for (int i=0; i<Estimators.size(); i++) {
79 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
80 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
81 else if (Estimators[i] == "eachgap") { distCalculator = new eachGapDist(); }
82 else if (Estimators[i] == "onegap") { distCalculator = new oneGapDist(); }
86 for (int i=0; i<Estimators.size(); i++) {
87 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
88 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
89 else if (Estimators[i] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
90 else if (Estimators[i] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
99 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function DistanceCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103 cout << "An unknown error has occurred in the DistanceCommand class function DistanceCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108 //**********************************************************************************************************************
110 DistanceCommand::~DistanceCommand(){
112 for(int i=0;i<lines.size();i++){
118 //**********************************************************************************************************************
120 void DistanceCommand::help(){
122 cout << "The dist.seqs command reads a file containing sequences and creates a distance file." << "\n";
123 cout << "The dist.seqs command parameters are fasta, calc, countends, cutoff and processors. " << "\n";
124 cout << "The fasta parameter is required." << "\n";
125 cout << "The calc parameter allows you to specify the method of calculating the distances. Your options are: nogaps, onegap or eachgap. The default is onegap." << "\n";
126 cout << "The countends parameter allows you to specify whether to include terminal gaps in distance. Your options are: T or F. The default is T." << "\n";
127 cout << "The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0." << "\n";
128 cout << "The processors parameter allows you to specify number of processors to use. The default is 1." << "\n";
129 cout << "The dist.seqs command should be in the following format: " << "\n";
130 cout << "dist.seqs(fasta=yourFastaFile, calc=yourCalc, countends=yourEnds, cutoff= yourCutOff, processors=yourProcessors) " << "\n";
131 cout << "Example dist.seqs(fasta=amazon.fasta, calc=eachgap, countends=F, cutoff= 2.0, processors=3)." << "\n";
132 cout << "Note: No spaces between parameter labels (i.e. calc), '=' and parameters (i.e.yourCalc)." << "\n" << "\n";
134 catch(exception& e) {
135 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
139 cout << "An unknown error has occurred in the DistanceCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145 //**********************************************************************************************************************
147 int DistanceCommand::execute(){
150 if (abort == true) { return 0; }
152 int numSeqs = alignDB.getNumSeqs();
157 //doses the user want the phylip formatted file as well
158 if (isTrue(phylip) == true) {
159 outputFile = getRootName(fastafile) + "phylip.dist";
160 remove(outputFile.c_str());
162 //output numSeqs to phylip formatted dist file
163 }else { //user wants column format
164 outputFile = getRootName(fastafile) + "dist";
165 remove(outputFile.c_str());
168 //# if defined (_WIN32)
169 //figure out how to implement the fork and wait commands in windows
170 // driver(distCalculator, seqDB, 0, numSeqs, distFile, phylipFile, cutoff);
174 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
175 //if you don't need to fork anything
177 driver(0, numSeqs, outputFile, cutoff);
178 }else{ //you have multiple processors
180 for (int i = 0; i < processors; i++) {
181 lines.push_back(new linePair());
182 lines[i]->start = int (sqrt(float(i)/float(processors)) * numSeqs);
183 lines[i]->end = int (sqrt(float(i+1)/float(processors)) * numSeqs);
186 createProcesses(outputFile);
188 map<int, int>::iterator it = processIDS.begin();
189 rename((outputFile + toString(it->second) + ".temp").c_str(), outputFile.c_str());
192 //append and remove temp files
193 for (; it != processIDS.end(); it++) {
194 appendFiles((outputFile + toString(it->second) + ".temp"), outputFile);
195 remove((outputFile + toString(it->second) + ".temp").c_str());
200 driver(0, numSeqs, outputFile, cutoff);
203 delete distCalculator;
208 catch(exception& e) {
209 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213 cout << "An unknown error has occurred in the DistanceCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
217 /**************************************************************************************************/
218 void DistanceCommand::createProcesses(string filename) {
220 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
224 //loop through and create all the processes you want
225 while (process != processors) {
229 processIDS[lines[process]->end] = pid; //create map from line number to pid so you can append files in correct order later
232 driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", cutoff);
234 }else { cout << "unable to spawn the necessary processes." << endl; exit(0); }
237 //force parent to wait until all the processes are done
238 for (map<int, int>::iterator it = processIDS.begin(); it != processIDS.end(); it++) {
239 int temp = it->second;
244 catch(exception& e) {
245 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function createProcesses. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249 cout << "An unknown error has occurred in the DistanceCommand class function createProcesses. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 /**************************************************************************************************/
255 /////// need to fix to work with calcs and sequencedb
256 int DistanceCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
259 int startTime = time(NULL);
262 ofstream outFile(dFileName.c_str(), ios::trunc);
263 outFile.setf(ios::fixed, ios::showpoint);
264 outFile << setprecision(4);
266 if(isTrue(phylip) && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
267 for(int i=startLine;i<endLine;i++){
268 if(isTrue(phylip)) { outFile << alignDB.get(i).getName() << '\t'; }
269 for(int j=0;j<i;j++){
270 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
271 double dist = distCalculator->getDist();
274 if (!isTrue(phylip)) { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
276 if (isTrue(phylip)) { outFile << dist << '\t'; }
280 if (isTrue(phylip) == true) { outFile << endl; }
283 cout << i << '\t' << time(NULL) - startTime << endl;
287 cout << endLine-1 << '\t' << time(NULL) - startTime << endl;
293 catch(exception& e) {
294 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function driver. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298 cout << "An unknown error has occurred in the DistanceCommand class function driver. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304 /**************************************************************************************************/
305 void DistanceCommand::appendFiles(string temp, string filename) {
310 //open output file in append mode
311 openOutputFileAppend(filename, output);
312 openInputFile(temp, input);
314 while(char c = input.get()){
315 if(input.eof()) { break; }
316 else { output << c; }
322 catch(exception& e) {
323 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function appendFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
327 cout << "An unknown error has occurred in the DistanceCommand class function appendFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
331 /**************************************************************************************************/