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(){
21 globaldata = GlobalData::getInstance();
22 validCalculator = new ValidCalculators();
23 countends = globaldata->getCountEnds();
24 convert(globaldata->getProcessors(), processors);
25 convert(globaldata->getCutOff(), cutoff);
28 if (countends == "T") {
29 for (i=0; i<globaldata->Estimators.size(); i++) {
30 if (validCalculator->isValidCalculator("distance", globaldata->Estimators[i]) == true) {
31 if (globaldata->Estimators[i] == "nogaps") {
32 distCalculator = new ignoreGaps();
33 }else if (globaldata->Estimators[i] == "eachgap") {
34 distCalculator = new eachGapDist();
35 }else if (globaldata->Estimators[i] == "onegap") {
36 distCalculator = new oneGapDist(); }
40 for (i=0; i<globaldata->Estimators.size(); i++) {
41 if (validCalculator->isValidCalculator("distance", globaldata->Estimators[i]) == true) {
42 if (globaldata->Estimators[i] == "nogaps") {
43 distCalculator = new ignoreGaps();
44 }else if (globaldata->Estimators[i] == "eachgap") {
45 distCalculator = new eachGapIgnoreTermGapDist();
46 }else if (globaldata->Estimators[i] == "onegap") {
47 distCalculator = new oneGapIgnoreTermGapDist();
53 //reset calc for next command
54 globaldata->setCalc("");
57 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function DistanceCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
61 cout << "An unknown error has occurred in the DistanceCommand class function DistanceCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65 //**********************************************************************************************************************
67 int DistanceCommand::execute(){
71 string filename = globaldata->inputFileName;
73 if(globaldata->getFastaFile() != "") {
74 readSeqs = new ReadFasta(filename); }
75 else if(globaldata->getNexusFile() != "") {
76 readSeqs = new ReadNexus(filename); }
77 else if(globaldata->getClustalFile() != "") {
78 readSeqs = new ReadClustal(filename); }
79 else if(globaldata->getPhylipFile() != "") {
80 readSeqs = new ReadPhylip(filename); }
83 seqDB = readSeqs->getDB();
85 int numSeqs = seqDB->getNumSeqs();
88 string distFile = getRootName(globaldata->getFastaFile()) + "dist";
90 remove(distFile.c_str());
92 //# if defined (_WIN32)
93 //figure out how to implement the fork and wait commands in windows
94 // driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff);
97 # if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
99 driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff);
101 else if(processors == 2){
105 driver(distCalculator, seqDB, 0, (numSeqs/sqrt(2)), distFile + "tempa", cutoff);
106 appendFiles((distFile+"tempa"), distFile);
107 remove((distFile + "tempa").c_str());
110 driver(distCalculator, seqDB, (numSeqs/sqrt(2)), numSeqs, distFile + "tempb", cutoff);
111 appendFiles((distFile+"tempb"), distFile);
112 remove((distFile + "tempb").c_str());
117 else if(processors == 3){
122 driver(distCalculator, seqDB, 0, sqrt(3) * numSeqs / 3, distFile + "tempa", cutoff);
123 appendFiles(distFile+"tempa", distFile);
124 remove((distFile + "tempa").c_str());
127 driver(distCalculator, seqDB, sqrt(3) * numSeqs / 3, sqrt(6) * numSeqs / 3, distFile + "tempb", cutoff);
128 appendFiles(distFile+"tempb", distFile);
129 remove((distFile + "tempb").c_str());
134 driver(distCalculator, seqDB, sqrt(6) * numSeqs / 3, numSeqs, distFile + "tempc", cutoff);
135 appendFiles(distFile+"tempc", distFile);
136 remove((distFile + "tempc").c_str());
140 else if(processors == 4){
145 driver(distCalculator, seqDB, 0, numSeqs / 2, distFile + "tempa", cutoff);
146 appendFiles(distFile+"tempa", distFile);
147 remove((distFile + "tempa").c_str());
150 driver(distCalculator, seqDB, numSeqs / 2, (numSeqs/sqrt(2)), distFile + "tempb", cutoff);
151 appendFiles(distFile+"tempb", distFile);
152 remove((distFile + "tempb").c_str());
159 driver(distCalculator, seqDB, (numSeqs/sqrt(2)), (sqrt(3) * numSeqs / 2), distFile + "tempc", cutoff);
160 appendFiles(distFile+"tempc", distFile);
161 remove((distFile + "tempc").c_str());
164 driver(distCalculator, seqDB, (sqrt(3) * numSeqs / 2), numSeqs, distFile + "tempd", cutoff);
165 appendFiles(distFile+"tempd", distFile);
166 remove((distFile + "tempd").c_str());
174 driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff);
177 delete distCalculator;
182 catch(exception& e) {
183 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 cout << "An unknown error has occurred in the DistanceCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192 /**************************************************************************************************/
193 /////// need to fix to work with calcs and sequencedb
194 int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLine, int endLine, string dFileName, float cutoff){
196 int startTime = time(NULL);
198 ofstream distFile(dFileName.c_str(), ios::trunc);
199 distFile.setf(ios::fixed, ios::showpoint);
200 distFile << setprecision(4);
202 for(int i=startLine;i<endLine;i++){
204 for(int j=0;j<i;j++){
205 distCalculator->calcDist(align->get(i), align->get(j));
206 double dist = distCalculator->getDist();
209 distFile << align->get(i).getName() << ' ' << align->get(j).getName() << ' ' << dist << endl;
214 cout << i << '\t' << time(NULL) - startTime << endl;
218 cout << endLine-1 << '\t' << time(NULL) - startTime << endl;
222 catch(exception& e) {
223 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function driver. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227 cout << "An unknown error has occurred in the DistanceCommand class function driver. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
233 /**************************************************************************************************/
234 void DistanceCommand::appendFiles(string temp, string filename) {
239 //open output file in append mode
240 openOutputFileAppend(filename, output);
242 //open temp file for reading
243 openInputFile(temp, input);
246 //read input file and write to output file
247 while(input.eof() != true) {
248 getline(input, line); //getline removes the newline char
250 output << line << endl; // Appending back newline char
257 catch(exception& e) {
258 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function appendFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262 cout << "An unknown error has occurred in the DistanceCommand class function appendFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
266 /**************************************************************************************************/