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","oldfasta","column", "output", "calc", "countends", "cutoff", "processors", "outputdir","inputdir","compress"};
31 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
33 OptionParser parser(option);
34 map<string, string> parameters = parser.getParameters();
36 ValidParameters validParameter("dist.seqs");
37 map<string, string>::iterator it2;
39 //check to make sure all parameters are valid for command
40 for (it2 = parameters.begin(); it2 != parameters.end(); it2++) {
41 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
44 //if the user changes the input directory command factory will send this info to us in the output parameter
45 string inputDir = validParameter.validFile(parameters, "inputdir", false);
46 if (inputDir == "not found"){ inputDir = ""; }
49 it2 = parameters.find("fasta");
50 //user has given a template file
51 if(it2 != parameters.end()){
52 path = m->hasPath(it2->second);
53 //if the user has not given a path then, add inputdir. else leave path alone.
54 if (path == "") { parameters["fasta"] = inputDir + it2->second; }
57 it2 = parameters.find("oldfasta");
58 //user has given a template file
59 if(it2 != parameters.end()){
60 path = m->hasPath(it2->second);
61 //if the user has not given a path then, add inputdir. else leave path alone.
62 if (path == "") { parameters["oldfasta"] = inputDir + it2->second; }
65 it2 = parameters.find("column");
66 //user has given a template file
67 if(it2 != parameters.end()){
68 path = m->hasPath(it2->second);
69 //if the user has not given a path then, add inputdir. else leave path alone.
70 if (path == "") { parameters["column"] = inputDir + it2->second; }
74 //check for required parameters
75 fastafile = validParameter.validFile(parameters, "fasta", true);
76 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the dist.seqs command."); m->mothurOutEndLine(); abort = true; }
77 else if (fastafile == "not open") { abort = true; }
80 m->openInputFile(fastafile, inFASTA);
81 alignDB = SequenceDB(inFASTA);
85 oldfastafile = validParameter.validFile(parameters, "oldfasta", true);
86 if (oldfastafile == "not found") { oldfastafile = ""; }
87 else if (oldfastafile == "not open") { abort = true; }
89 column = validParameter.validFile(parameters, "column", true);
90 if (column == "not found") { column = ""; }
91 else if (column == "not open") { abort = true; }
93 //if the user changes the output directory command factory will send this info to us in the output parameter
94 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
96 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
99 //check for optional parameter and set defaults
100 // ...at some point should added some additional type checking...
101 calc = validParameter.validFile(parameters, "calc", false);
102 if (calc == "not found") { calc = "onegap"; }
104 if (calc == "default") { calc = "onegap"; }
106 m->splitAtDash(calc, Estimators);
109 temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; }
110 convert(temp, countends);
112 temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; }
113 convert(temp, cutoff);
115 temp = validParameter.validFile(parameters, "processors", false); if(temp == "not found"){ temp = "1"; }
116 convert(temp, processors);
118 temp = validParameter.validFile(parameters, "compress", false); if(temp == "not found"){ temp = "F"; }
119 convert(temp, compress);
121 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "column"; }
123 if (((column != "") && (oldfastafile == "")) || ((column == "") && (oldfastafile != ""))) { m->mothurOut("If you provide column or oldfasta, you must provide both."); m->mothurOutEndLine(); abort=true; }
125 if ((column != "") && (oldfastafile != "") && (output != "column")) { m->mothurOut("You have provided column and oldfasta, indicating you want to append distances to your column file. Your output must be in column format to do so."); m->mothurOutEndLine(); abort=true; }
127 if ((output != "column") && (output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are column, lt and square. I will use column."); m->mothurOutEndLine(); output = "column"; }
129 ValidCalculators validCalculator;
131 if (m->isTrue(countends) == true) {
132 for (int i=0; i<Estimators.size(); i++) {
133 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
134 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
135 else if (Estimators[i] == "eachgap") { distCalculator = new eachGapDist(); }
136 else if (Estimators[i] == "onegap") { distCalculator = new oneGapDist(); }
140 for (int i=0; i<Estimators.size(); i++) {
141 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
142 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
143 else if (Estimators[i] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
144 else if (Estimators[i] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
152 catch(exception& e) {
153 m->errorOut(e, "DistanceCommand", "DistanceCommand");
158 //**********************************************************************************************************************
160 DistanceCommand::~DistanceCommand(){
162 for(int i=0;i<lines.size();i++){
168 //**********************************************************************************************************************
170 void DistanceCommand::help(){
172 m->mothurOut("The dist.seqs command reads a file containing sequences and creates a distance file.\n");
173 m->mothurOut("The dist.seqs command parameters are fasta, oldfasta, column, calc, countends, output, compress, cutoff and processors. \n");
174 m->mothurOut("The fasta parameter is required.\n");
175 m->mothurOut("The oldfasta and column parameters allow you to append the distances calculated to the column file.\n");
176 m->mothurOut("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");
177 m->mothurOut("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");
178 m->mothurOut("The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0.\n");
179 m->mothurOut("The output parameter allows you to specify format of your distance matrix. Options are column, lt, and square. The default is column.\n");
180 m->mothurOut("The processors parameter allows you to specify number of processors to use. The default is 1.\n");
181 m->mothurOut("The compress parameter allows you to indicate that you want the resulting distance file compressed. The default is false.\n");
182 m->mothurOut("The dist.seqs command should be in the following format: \n");
183 m->mothurOut("dist.seqs(fasta=yourFastaFile, calc=yourCalc, countends=yourEnds, cutoff= yourCutOff, processors=yourProcessors) \n");
184 m->mothurOut("Example dist.seqs(fasta=amazon.fasta, calc=eachgap, countends=F, cutoff= 2.0, processors=3).\n");
185 m->mothurOut("Note: No spaces between parameter labels (i.e. calc), '=' and parameters (i.e.yourCalc).\n\n");
187 catch(exception& e) {
188 m->errorOut(e, "DistanceCommand", "help");
192 //**********************************************************************************************************************
194 int DistanceCommand::execute(){
197 if (abort == true) { return 0; }
199 int startTime = time(NULL);
201 //save number of new sequence
202 numNewFasta = alignDB.getNumSeqs();
204 //sanity check the oldfasta and column file as well as add oldfasta sequences to alignDB
205 if ((oldfastafile != "") && (column != "")) { if (!(sanityCheck())) { return 0; } }
207 if (m->control_pressed) { return 0; }
209 int numSeqs = alignDB.getNumSeqs();
214 if (output == "lt") { //does the user want lower triangle phylip formatted file
215 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "phylip.dist";
216 remove(outputFile.c_str());
218 //output numSeqs to phylip formatted dist file
219 }else if (output == "column") { //user wants column format
220 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "dist";
222 //so we don't accidentally overwrite
223 if (outputFile == column) {
224 string tempcolumn = column + ".old";
225 rename(column.c_str(), tempcolumn.c_str());
228 remove(outputFile.c_str());
229 }else { //assume square
230 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "square.dist";
231 remove(outputFile.c_str());
241 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
242 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
244 //each process gets where it should start and stop in the file
245 if (output != "square") {
246 start = int (sqrt(float(pid)/float(processors)) * numSeqs);
247 end = int (sqrt(float(pid+1)/float(processors)) * numSeqs);
249 start = int ((float(pid)/float(processors)) * numSeqs);
250 end = int ((float(pid+1)/float(processors)) * numSeqs);
253 if (output == "column") {
255 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
257 //char* filename = new char[outputFile.length()];
258 //memcpy(filename, outputFile.c_str(), outputFile.length());
261 strcpy(filename, outputFile.c_str());
263 MPI_File_open(MPI_COMM_WORLD, filename, amode, MPI_INFO_NULL, &outMPI);
266 if (pid == 0) { //you are the root process
271 driverMPI(start, end, outMPI, cutoff);
273 if (m->control_pressed) { MPI_File_close(&outMPI); delete distCalculator; return 0; }
276 for(int i = 1; i < processors; i++) {
277 if (m->control_pressed) { MPI_File_close(&outMPI); delete distCalculator; return 0; }
280 MPI_Recv(buf, 4, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
282 }else { //you are a child process
284 driverMPI(start, end, outMPI, cutoff);
286 if (m->control_pressed) { MPI_File_close(&outMPI); delete distCalculator; return 0; }
290 //tell parent you are done.
291 MPI_Send(buf, 4, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
294 MPI_File_close(&outMPI);
296 }else { //lower triangle format
297 if (pid == 0) { //you are the root process
301 unsigned long int mySize;
303 if (output != "square"){ driverMPI(start, end, outputFile, mySize); }
304 else { driverMPI(start, end, outputFile, mySize, output); }
306 if (m->control_pressed) { delete distCalculator; return 0; }
308 int amode=MPI_MODE_APPEND|MPI_MODE_WRONLY|MPI_MODE_CREATE; //
312 //char* filename = new char[outputFile.length()];
313 //memcpy(filename, outputFile.c_str(), outputFile.length());
316 strcpy(filename, outputFile.c_str());
318 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
322 for(int b = 1; b < processors; b++) {
323 unsigned long int fileSize;
325 if (m->control_pressed) { MPI_File_close(&outMPI); delete distCalculator; return 0; }
327 MPI_Recv(&fileSize, 1, MPI_LONG, b, tag, MPI_COMM_WORLD, &status);
329 string outTemp = outputFile + toString(b) + ".temp";
331 char* buf = new char[outTemp.length()];
332 memcpy(buf, outTemp.c_str(), outTemp.length());
334 MPI_File_open(MPI_COMM_SELF, buf, MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
338 while (count < fileSize) {
340 MPI_File_read(inMPI, buf2, 1, MPI_CHAR, &status);
341 MPI_File_write(outMPI, buf2, 1, MPI_CHAR, &status);
345 MPI_File_close(&inMPI); //deleted on close
348 MPI_File_close(&outMPI);
349 }else { //you are a child process
351 unsigned long int size;
352 if (output != "square"){ driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size); }
353 else { driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size, output); }
355 if (m->control_pressed) { delete distCalculator; return 0; }
357 //tell parent you are done.
358 MPI_Send(&size, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
361 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
364 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
365 //if you don't need to fork anything
367 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
368 else { driver(0, numSeqs, outputFile, "square"); }
369 }else{ //you have multiple processors
371 for (int i = 0; i < processors; i++) {
372 lines.push_back(new linePair());
373 if (output != "square") {
374 lines[i]->start = int (sqrt(float(i)/float(processors)) * numSeqs);
375 lines[i]->end = int (sqrt(float(i+1)/float(processors)) * numSeqs);
377 lines[i]->start = int ((float(i)/float(processors)) * numSeqs);
378 lines[i]->end = int ((float(i+1)/float(processors)) * numSeqs);
382 createProcesses(outputFile);
384 map<int, int>::iterator it = processIDS.begin();
385 rename((outputFile + toString(it->second) + ".temp").c_str(), outputFile.c_str());
388 //append and remove temp files
389 for (; it != processIDS.end(); it++) {
390 m->appendFiles((outputFile + toString(it->second) + ".temp"), outputFile);
391 remove((outputFile + toString(it->second) + ".temp").c_str());
396 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
397 else { driver(0, numSeqs, outputFile, "square"); }
401 if (m->control_pressed) { delete distCalculator; remove(outputFile.c_str()); return 0; }
404 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
406 if (pid == 0) { //only one process should output to screen
409 //if (output == "square") { convertMatrix(outputFile); }
412 fileHandle.open(outputFile.c_str());
414 m->gobble(fileHandle);
415 if (fileHandle.eof()) { m->mothurOut(outputFile + " is blank. This can result if there are no distances below your cutoff."); m->mothurOutEndLine(); }
418 //append the old column file to the new one
419 if ((oldfastafile != "") && (column != "")) {
420 //we had to rename the column file so we didnt overwrite above, but we want to keep old name
421 if (outputFile == column) {
422 string tempcolumn = column + ".old";
423 m->appendFiles(tempcolumn, outputFile);
424 remove(tempcolumn.c_str());
426 m->appendFiles(outputFile, column);
427 remove(outputFile.c_str());
431 if (outputDir != "") {
432 string newOutputName = outputDir + m->getSimpleName(outputFile);
433 rename(outputFile.c_str(), newOutputName.c_str());
434 remove(outputFile.c_str());
435 outputFile = newOutputName;
444 if (m->control_pressed) { delete distCalculator; remove(outputFile.c_str()); return 0; }
446 delete distCalculator;
448 m->mothurOutEndLine();
449 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
450 m->mothurOut(outputFile); m->mothurOutEndLine();
451 m->mothurOutEndLine();
452 m->mothurOut("It took " + toString(time(NULL) - startTime) + " to calculate the distances for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
455 if (m->isTrue(compress)) {
456 m->mothurOut("Compressing..."); m->mothurOutEndLine();
457 m->mothurOut("(Replacing " + outputFile + " with " + outputFile + ".gz)"); m->mothurOutEndLine();
458 system(("gzip -v " + outputFile).c_str());
464 catch(exception& e) {
465 m->errorOut(e, "DistanceCommand", "execute");
469 /**************************************************************************************************/
470 void DistanceCommand::createProcesses(string filename) {
472 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
476 //loop through and create all the processes you want
477 while (process != processors) {
481 processIDS[lines[process]->end] = pid; //create map from line number to pid so you can append files in correct order later
484 if (output != "square") { driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", cutoff); }
485 else { driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", "square"); }
487 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
490 //force parent to wait until all the processes are done
491 for (map<int, int>::iterator it = processIDS.begin(); it != processIDS.end(); it++) {
492 int temp = it->second;
497 catch(exception& e) {
498 m->errorOut(e, "DistanceCommand", "createProcesses");
503 /**************************************************************************************************/
504 /////// need to fix to work with calcs and sequencedb
505 int DistanceCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
508 int startTime = time(NULL);
511 ofstream outFile(dFileName.c_str(), ios::trunc);
512 outFile.setf(ios::fixed, ios::showpoint);
513 outFile << setprecision(4);
515 if((output == "lt") && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
517 for(int i=startLine;i<endLine;i++){
519 string name = alignDB.get(i).getName();
520 if (name.length() < 10) { //pad with spaces to make compatible
521 while (name.length() < 10) { name += " "; }
523 outFile << name << '\t';
525 for(int j=0;j<i;j++){
527 if (m->control_pressed) { outFile.close(); return 0; }
529 //if there was a column file given and we are appending, we don't want to calculate the distances that are already in the column file
530 //the alignDB contains the new sequences and then the old, so if i an oldsequence and j is an old sequence then break out of this loop
531 if ((i >= numNewFasta) && (j >= numNewFasta)) { break; }
533 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
534 double dist = distCalculator->getDist();
537 if (output == "column") { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
539 if (output == "lt") { outFile << dist << '\t'; }
542 if (output == "lt") { outFile << endl; }
545 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
549 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
555 catch(exception& e) {
556 m->errorOut(e, "DistanceCommand", "driver");
560 /**************************************************************************************************/
561 /////// need to fix to work with calcs and sequencedb
562 int DistanceCommand::driver(int startLine, int endLine, string dFileName, string square){
565 int startTime = time(NULL);
568 ofstream outFile(dFileName.c_str(), ios::trunc);
569 outFile.setf(ios::fixed, ios::showpoint);
570 outFile << setprecision(4);
572 if(startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
574 for(int i=startLine;i<endLine;i++){
576 string name = alignDB.get(i).getName();
577 //pad with spaces to make compatible
578 if (name.length() < 10) { while (name.length() < 10) { name += " "; } }
580 outFile << name << '\t';
582 for(int j=0;j<alignDB.getNumSeqs();j++){
584 if (m->control_pressed) { outFile.close(); return 0; }
586 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
587 double dist = distCalculator->getDist();
589 outFile << dist << '\t';
595 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
599 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
605 catch(exception& e) {
606 m->errorOut(e, "DistanceCommand", "driver");
611 /**************************************************************************************************/
612 /////// need to fix to work with calcs and sequencedb
613 int DistanceCommand::driverMPI(int startLine, int endLine, MPI_File& outMPI, float cutoff){
616 int startTime = time(NULL);
618 string outputString = "";
620 for(int i=startLine;i<endLine;i++){
622 for(int j=0;j<i;j++){
624 if (m->control_pressed) { return 0; }
626 //if there was a column file given and we are appending, we don't want to calculate the distances that are already in the column file
627 //the alignDB contains the new sequences and then the old, so if i an oldsequence and j is an old sequence then break out of this loop
628 if ((i >= numNewFasta) && (j >= numNewFasta)) { break; }
630 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
631 double dist = distCalculator->getDist();
634 outputString += (alignDB.get(i).getName() + ' ' + alignDB.get(j).getName() + ' ' + toString(dist) + '\n');
639 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
640 cout << i << '\t' << (time(NULL) - startTime) << endl;
644 //send results to parent
645 int length = outputString.length();
647 char* buf = new char[length];
648 memcpy(buf, outputString.c_str(), length);
650 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
656 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
657 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
660 catch(exception& e) {
661 m->errorOut(e, "DistanceCommand", "driverMPI");
665 /**************************************************************************************************/
666 /////// need to fix to work with calcs and sequencedb
667 int DistanceCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size){
672 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
674 //char* filename = new char[file.length()];
675 //memcpy(filename, file.c_str(), file.length());
678 strcpy(filename, file.c_str());
680 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
683 int startTime = time(NULL);
685 string outputString = "";
688 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
690 for(int i=startLine;i<endLine;i++){
692 string name = alignDB.get(i).getName();
693 if (name.length() < 10) { //pad with spaces to make compatible
694 while (name.length() < 10) { name += " "; }
696 outputString += name + "\t";
698 for(int j=0;j<i;j++){
700 if (m->control_pressed) { return 0; }
702 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
703 double dist = distCalculator->getDist();
705 outputString += toString(dist) + "\t";
708 outputString += "\n";
712 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
713 cout << i << '\t' << (time(NULL) - startTime) << endl;
717 //send results to parent
718 int length = outputString.length();
719 char* buf = new char[length];
720 memcpy(buf, outputString.c_str(), length);
722 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
723 size += outputString.length();
728 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
729 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
730 MPI_File_close(&outMPI);
734 catch(exception& e) {
735 m->errorOut(e, "DistanceCommand", "driverMPI");
739 /**************************************************************************************************/
740 /////// need to fix to work with calcs and sequencedb
741 int DistanceCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size, string square){
746 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
748 //char* filename = new char[file.length()];
749 //memcpy(filename, file.c_str(), file.length());
752 strcpy(filename, file.c_str());
754 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
757 int startTime = time(NULL);
759 string outputString = "";
762 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
764 for(int i=startLine;i<endLine;i++){
766 string name = alignDB.get(i).getName();
767 if (name.length() < 10) { //pad with spaces to make compatible
768 while (name.length() < 10) { name += " "; }
770 outputString += name + "\t";
772 for(int j=0;j<alignDB.getNumSeqs();j++){
774 if (m->control_pressed) { return 0; }
776 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
777 double dist = distCalculator->getDist();
779 outputString += toString(dist) + "\t";
782 outputString += "\n";
786 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
787 cout << i << '\t' << (time(NULL) - startTime) << endl;
791 //send results to parent
792 int length = outputString.length();
793 char* buf = new char[length];
794 memcpy(buf, outputString.c_str(), length);
796 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
797 size += outputString.length();
802 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
803 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
804 MPI_File_close(&outMPI);
808 catch(exception& e) {
809 m->errorOut(e, "DistanceCommand", "driverMPI");
814 /**************************************************************************************************
815 int DistanceCommand::convertMatrix(string outputFile) {
818 //sort file by first column so the distances for each row are together
819 string outfile = m->getRootName(outputFile) + "sorted.dist.temp";
822 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
823 string command = "sort -n " + outputFile + " -o " + outfile;
824 system(command.c_str());
825 #else //sort using windows sort
826 string command = "sort " + outputFile + " /O " + outfile;
827 system(command.c_str());
831 //output to new file distance for each row and save positions in file where new row begins
833 m->openInputFile(outfile, in);
836 m->openOutputFile(outputFile, out);
838 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
840 out << alignDB.getNumSeqs() << endl;
842 //get first currentRow
843 string first, currentRow, second;
845 map<string, float> rowDists; //take advantage of the fact that maps are already sorted by key
846 map<string, float>::iterator it;
851 rowDists[first] = 0.00; //distance to yourself is 0.0
854 //m->openInputFile(outfile, in);
857 if (m->control_pressed) { in.close(); remove(outfile.c_str()); out.close(); return 0; }
859 in >> first >> second >> dist; m->gobble(in);
861 if (first != currentRow) {
863 out << currentRow << '\t'; //print name
866 for (it = rowDists.begin(); it != rowDists.end(); it++) {
867 out << it->second << '\t';
874 rowDists[first] = 0.00;
875 rowDists[second] = dist;
877 rowDists[second] = dist;
881 out << currentRow << '\t'; //print name
884 for (it = rowDists.begin(); it != rowDists.end(); it++) {
885 out << it->second << '\t';
892 remove(outfile.c_str());
897 catch(exception& e) {
898 m->errorOut(e, "DistanceCommand", "convertMatrix");
902 /**************************************************************************************************
903 int DistanceCommand::convertToLowerTriangle(string outputFile) {
906 //sort file by first column so the distances for each row are together
907 string outfile = m->getRootName(outputFile) + "sorted.dist.temp";
910 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
911 string command = "sort -n " + outputFile + " -o " + outfile;
912 system(command.c_str());
913 #else //sort using windows sort
914 string command = "sort " + outputFile + " /O " + outfile;
915 system(command.c_str());
919 //output to new file distance for each row and save positions in file where new row begins
921 m->openInputFile(outfile, in);
924 m->openOutputFile(outputFile, out);
926 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
928 out << alignDB.getNumSeqs() << endl;
930 //get first currentRow
931 string first, currentRow, second;
935 map<string, float> rowDists; //take advantage of the fact that maps are already sorted by key
936 map<string, float>::iterator it;
941 rowDists[first] = 0.00; //distance to yourself is 0.0
944 //m->openInputFile(outfile, in);
947 if (m->control_pressed) { in.close(); remove(outfile.c_str()); out.close(); return 0; }
949 in >> first >> second >> dist; m->gobble(in);
951 if (first != currentRow) {
953 out << currentRow << '\t'; //print name
956 for (it = rowDists.begin(); it != rowDists.end(); it++) {
957 if (j >= i) { break; }
958 out << it->second << '\t';
966 rowDists[first] = 0.00;
967 rowDists[second] = dist;
971 rowDists[second] = dist;
975 out << currentRow << '\t'; //print name
978 for (it = rowDists.begin(); it != rowDists.end(); it++) {
979 out << it->second << '\t';
986 remove(outfile.c_str());
991 catch(exception& e) {
992 m->errorOut(e, "DistanceCommand", "convertToLowerTriangle");
996 /**************************************************************************************************/
997 //its okay if the column file does not contain all the names in the fasta file, since some distance may have been above a cutoff,
998 //but no sequences can be in the column file that are not in oldfasta. also, if a distance is above the cutoff given then remove it.
999 //also check to make sure the 2 files have the same alignment length.
1000 bool DistanceCommand::sanityCheck() {
1004 //make sure the 2 fasta files have the same alignment length
1006 m->openInputFile(fastafile, in);
1007 int fastaAlignLength = 0;
1009 Sequence tempIn(in);
1010 fastaAlignLength = tempIn.getAligned().length();
1015 m->openInputFile(oldfastafile, in2);
1016 int oldfastaAlignLength = 0;
1018 Sequence tempIn2(in2);
1019 oldfastaAlignLength = tempIn2.getAligned().length();
1023 if (fastaAlignLength != oldfastaAlignLength) { m->mothurOut("fasta files do not have the same alignment length."); m->mothurOutEndLine(); return false; }
1025 //read fasta file and save names as well as adding them to the alignDB
1026 set<string> namesOldFasta;
1029 m->openInputFile(oldfastafile, inFasta);
1031 while (!inFasta.eof()) {
1032 if (m->control_pressed) { inFasta.close(); return good; }
1034 Sequence temp(inFasta);
1036 if (temp.getName() != "") {
1037 namesOldFasta.insert(temp.getName()); //save name
1038 alignDB.push_back(temp); //add to DB
1046 //read through the column file checking names and removing distances above the cutoff
1048 m->openInputFile(column, inDist);
1051 string outputFile = column + ".temp";
1052 m->openOutputFile(outputFile, outDist);
1054 string name1, name2;
1056 while (!inDist.eof()) {
1057 if (m->control_pressed) { inDist.close(); outDist.close(); remove(outputFile.c_str()); return good; }
1059 inDist >> name1 >> name2 >> dist; m->gobble(inDist);
1061 //both names are in fasta file and distance is below cutoff
1062 if ((namesOldFasta.count(name1) == 0) || (namesOldFasta.count(name2) == 0)) { good = false; break; }
1064 if (dist <= cutoff) {
1065 outDist << name1 << '\t' << name2 << '\t' << dist << endl;
1074 remove(column.c_str());
1075 rename(outputFile.c_str(), column.c_str());
1077 remove(outputFile.c_str()); //temp file is bad because file mismatch above
1081 catch(exception& e) {
1082 m->errorOut(e, "DistanceCommand", "m->appendFiles");
1086 /**************************************************************************************************/