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 string filename = globaldata->getFastaFile();
29 openInputFile(filename, in);
33 if (isTrue(countends) == true) {
34 for (i=0; i<globaldata->Estimators.size(); i++) {
35 if (validCalculator->isValidCalculator("distance", globaldata->Estimators[i]) == true) {
36 if (globaldata->Estimators[i] == "nogaps") {
37 distCalculator = new ignoreGaps();
38 }else if (globaldata->Estimators[i] == "eachgap") {
39 distCalculator = new eachGapDist();
40 }else if (globaldata->Estimators[i] == "onegap") {
41 distCalculator = new oneGapDist(); }
45 for (i=0; i<globaldata->Estimators.size(); i++) {
46 if (validCalculator->isValidCalculator("distance", globaldata->Estimators[i]) == true) {
47 if (globaldata->Estimators[i] == "nogaps") {
48 distCalculator = new ignoreGaps();
49 }else if (globaldata->Estimators[i] == "eachgap") {
50 distCalculator = new eachGapIgnoreTermGapDist();
51 }else if (globaldata->Estimators[i] == "onegap") {
52 distCalculator = new oneGapIgnoreTermGapDist();
58 //reset calc for next command
59 globaldata->setCalc("");
62 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function DistanceCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
66 cout << "An unknown error has occurred in the DistanceCommand class function DistanceCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
70 //**********************************************************************************************************************
72 int DistanceCommand::execute(){
75 //reads fasta file and fills sequenceDB
76 if(globaldata->getFastaFile() != "") { seqDB = new SequenceDB(in); }
77 else { cout << "Error no fasta file." << endl; return 0; }
79 int numSeqs = seqDB->getNumSeqs();
82 string distFile = getRootName(globaldata->getFastaFile()) + "dist";
84 remove(distFile.c_str());
86 //# if defined (_WIN32)
87 //figure out how to implement the fork and wait commands in windows
88 // driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff);
91 #if defined (__APPLE__) || (__MACH__)
93 driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff);
95 else if(processors == 2){
99 driver(distCalculator, seqDB, 0, (numSeqs/sqrt(2)), distFile + "tempa", cutoff);
100 appendFiles((distFile+"tempa"), distFile);
101 remove((distFile + "tempa").c_str());
104 driver(distCalculator, seqDB, (numSeqs/sqrt(2)), numSeqs, distFile + "tempb", cutoff);
105 appendFiles((distFile+"tempb"), distFile);
106 remove((distFile + "tempb").c_str());
111 else if(processors == 3){
116 driver(distCalculator, seqDB, 0, sqrt(3) * numSeqs / 3, distFile + "tempa", cutoff);
117 appendFiles(distFile+"tempa", distFile);
118 remove((distFile + "tempa").c_str());
121 driver(distCalculator, seqDB, sqrt(3) * numSeqs / 3, sqrt(6) * numSeqs / 3, distFile + "tempb", cutoff);
122 appendFiles(distFile+"tempb", distFile);
123 remove((distFile + "tempb").c_str());
128 driver(distCalculator, seqDB, sqrt(6) * numSeqs / 3, numSeqs, distFile + "tempc", cutoff);
129 appendFiles(distFile+"tempc", distFile);
130 remove((distFile + "tempc").c_str());
134 else if(processors == 4){
139 driver(distCalculator, seqDB, 0, numSeqs / 2, distFile + "tempa", cutoff);
140 appendFiles(distFile+"tempa", distFile);
141 remove((distFile + "tempa").c_str());
144 driver(distCalculator, seqDB, numSeqs / 2, (numSeqs/sqrt(2)), distFile + "tempb", cutoff);
145 appendFiles(distFile+"tempb", distFile);
146 remove((distFile + "tempb").c_str());
153 driver(distCalculator, seqDB, (numSeqs/sqrt(2)), (sqrt(3) * numSeqs / 2), distFile + "tempc", cutoff);
154 appendFiles(distFile+"tempc", distFile);
155 remove((distFile + "tempc").c_str());
158 driver(distCalculator, seqDB, (sqrt(3) * numSeqs / 2), numSeqs, distFile + "tempd", cutoff);
159 appendFiles(distFile+"tempd", distFile);
160 remove((distFile + "tempd").c_str());
167 #elif (linux) || (__linux)
169 driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff);
171 else if(processors == 2){
175 driver(distCalculator, seqDB, 0, (numSeqs/sqrt(2)), distFile + "tempa", cutoff);
176 appendFiles((distFile+"tempa"), distFile);
177 remove((distFile + "tempa").c_str());
180 driver(distCalculator, seqDB, (numSeqs/sqrt(2)), numSeqs, distFile + "tempb", cutoff);
181 appendFiles((distFile+"tempb"), distFile);
182 remove((distFile + "tempb").c_str());
187 else if(processors == 3){
192 driver(distCalculator, seqDB, 0, sqrt(3) * numSeqs / 3, distFile + "tempa", cutoff);
193 appendFiles(distFile+"tempa", distFile);
194 remove((distFile + "tempa").c_str());
197 driver(distCalculator, seqDB, sqrt(3) * numSeqs / 3, sqrt(6) * numSeqs / 3, distFile + "tempb", cutoff);
198 appendFiles(distFile+"tempb", distFile);
199 remove((distFile + "tempb").c_str());
204 driver(distCalculator, seqDB, sqrt(6) * numSeqs / 3, numSeqs, distFile + "tempc", cutoff);
205 appendFiles(distFile+"tempc", distFile);
206 remove((distFile + "tempc").c_str());
210 else if(processors == 4){
215 driver(distCalculator, seqDB, 0, numSeqs / 2, distFile + "tempa", cutoff);
216 appendFiles(distFile+"tempa", distFile);
217 remove((distFile + "tempa").c_str());
220 driver(distCalculator, seqDB, numSeqs / 2, (numSeqs/sqrt(2)), distFile + "tempb", cutoff);
221 appendFiles(distFile+"tempb", distFile);
222 remove((distFile + "tempb").c_str());
229 driver(distCalculator, seqDB, (numSeqs/sqrt(2)), (sqrt(3) * numSeqs / 2), distFile + "tempc", cutoff);
230 appendFiles(distFile+"tempc", distFile);
231 remove((distFile + "tempc").c_str());
234 driver(distCalculator, seqDB, (sqrt(3) * numSeqs / 2), numSeqs, distFile + "tempd", cutoff);
235 appendFiles(distFile+"tempd", distFile);
236 remove((distFile + "tempd").c_str());
245 driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff);
248 delete distCalculator;
253 catch(exception& e) {
254 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
258 cout << "An unknown error has occurred in the DistanceCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263 /**************************************************************************************************/
264 /////// need to fix to work with calcs and sequencedb
265 int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLine, int endLine, string dFileName, float cutoff){
267 int startTime = time(NULL);
269 ofstream distFile(dFileName.c_str(), ios::trunc);
270 distFile.setf(ios::fixed, ios::showpoint);
271 distFile << setprecision(4);
273 for(int i=startLine;i<endLine;i++){
275 for(int j=0;j<i;j++){
276 distCalculator->calcDist(*(align->get(i)), *(align->get(j)));
277 double dist = distCalculator->getDist();
280 distFile << align->get(i)->getName() << ' ' << align->get(j)->getName() << ' ' << dist << endl;
285 cout << i << '\t' << time(NULL) - startTime << endl;
289 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);
313 //open temp file for reading
314 openInputFile(temp, input);
317 //read input file and write to output file
318 while(input.eof() != true) {
319 getline(input, line); //getline removes the newline char
321 output << line << endl; // Appending back newline char
328 catch(exception& e) {
329 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function appendFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
333 cout << "An unknown error has occurred in the DistanceCommand class function appendFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
337 /**************************************************************************************************/