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 ends = globaldata->getEnds();
24 seqDB = globaldata->gSequenceDB;
25 convert(globaldata->getProcessors(), processors);
26 convert(globaldata->getCutOff(), cutoff);
27 distFile = getRootName(globaldata->getFastaFile()) + "dist";
31 for (i=0; i<globaldata->Estimators.size(); i++) {
32 if (validCalculator->isValidCalculator("distance", globaldata->Estimators[i]) == true) {
33 if (globaldata->Estimators[i] == "nogaps") {
34 distCalculator = new ignoreGaps();
35 }else if (globaldata->Estimators[i] == "eachgap") {
36 distCalculator = new eachGapDist();
37 }else if (globaldata->Estimators[i] == "onegap") {
38 distCalculator = new oneGapDist(); }
42 for (i=0; i<globaldata->Estimators.size(); i++) {
43 if (validCalculator->isValidCalculator("distance", globaldata->Estimators[i]) == true) {
44 if (globaldata->Estimators[i] == "nogaps") {
45 distCalculator = new ignoreGaps();
46 }else if (globaldata->Estimators[i] == "eachgap") {
47 distCalculator = new eachGapIgnoreTermGapDist();
48 }else if (globaldata->Estimators[i] == "onegap") {
49 distCalculator = new oneGapIgnoreTermGapDist();
56 //reset calc for next command
57 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(){
74 int numSeqs = seqDB->getNumSeqs();
76 system(("rm "+distFile).c_str() );
78 driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff);
80 else if(processors == 2){
84 driver(distCalculator, seqDB, 0, (numSeqs/sqrt(2)), distFile + "tempa", cutoff);
85 // system(("cat " + distFile + "tempa" + " >> " + distFile).c_str());
86 // system(("rm " + distFile + "tempa").c_str());
89 driver(distCalculator, seqDB, (numSeqs/sqrt(2)), numSeqs, distFile + "tempb", cutoff);
90 // system(("cat " + distFile + "tempb" + " >> " + distFile).c_str());
91 // system(("rm " + distFile + "tempb").c_str());
96 else if(processors == 3){
101 driver(distCalculator, seqDB, 0, sqrt(3) * numSeqs / 3, distFile + "tempa", cutoff);
103 system(("cat " + distFile + "tempa" + " >> " + distFile).c_str());
106 //get system call from pat system(("copy " + distFile + "tempa").c_str());
108 cout << "Sorry but I can't continue because this operating system doesn't appear to support the cat() or copy() system calls." << endl;
113 system(("rm " + distFile + "tempa").c_str());
116 system(("erase " + distFile + "tempa").c_str());
118 cout << "Sorry but I can't remove the required files because this operating system doesn't appear to support the rm() or erase() system calls." << endl;
123 driver(distCalculator, seqDB, sqrt(3) * numSeqs / 3, sqrt(6) * numSeqs / 3, distFile + "tempb", cutoff);
124 system(("cat " + distFile + "tempb" + " >> " + distFile).c_str());
125 system(("rm " + distFile + "tempb").c_str());
130 driver(distCalculator, seqDB, sqrt(6) * numSeqs / 3, numSeqs, distFile + "tempc", cutoff);
131 system(("cat " + distFile + "tempc" + " >> " + distFile).c_str());
132 system(("rm " + distFile + "tempc").c_str());
136 else if(processors == 4){
141 driver(distCalculator, seqDB, 0, numSeqs / 2, distFile + "tempa", cutoff);
142 system(("cat " + distFile + "tempa" + " >> " + distFile).c_str());
143 system(("rm " + distFile + "tempa").c_str());
146 driver(distCalculator, seqDB, numSeqs / 2, (numSeqs/sqrt(2)), distFile + "tempb", cutoff);
147 system(("cat " + distFile + "tempb" + " >> " + distFile).c_str());
148 system(("rm " + distFile + "tempb").c_str());
155 driver(distCalculator, seqDB, (numSeqs/sqrt(2)), (sqrt(3) * numSeqs / 2), distFile + "tempc", cutoff);
156 system(("cat " + distFile + "tempc" + " >> " + distFile).c_str());
157 system(("rm " + distFile + "tempc").c_str());
160 driver(distCalculator, seqDB, (sqrt(3) * numSeqs / 2), numSeqs, distFile + "tempd", cutoff);
161 system(("cat " + distFile + "tempd" + " >> " + distFile).c_str());
162 system(("rm " + distFile + "tempd").c_str());
170 delete distCalculator;
175 catch(exception& e) {
176 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180 cout << "An unknown error has occurred in the DistanceCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185 /**************************************************************************************************/
186 /////// need to fix to work with calcs and sequencedb
187 int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLine, int endLine, string dFileName, float cutoff){
189 int startTime = time(NULL);
191 ofstream distFile(dFileName.c_str(), ios::trunc);
192 distFile.setf(ios::fixed, ios::showpoint);
193 distFile << setprecision(4);
195 for(int i=startLine;i<endLine;i++){
197 for(int j=0;j<i;j++){
199 distCalculator->calcDist(align->get(i), align->get(j));
200 double dist = distCalculator->getDist();
202 distFile << align->get(i).getName() << ' ' << align->get(j).getName() << ' ' << dist << endl;
207 cout << i << '\t' << time(NULL) - startTime << endl;
211 cout << endLine-1 << '\t' << time(NULL) - startTime << endl;
215 catch(exception& e) {
216 cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function driver. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220 cout << "An unknown error has occurred in the DistanceCommand class function driver. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
226 /**************************************************************************************************/