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"};
30 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
32 OptionParser parser(option);
33 map<string, string> parameters = parser.getParameters();
35 ValidParameters validParameter("dist.seqs");
36 map<string, string>::iterator it2;
38 //check to make sure all parameters are valid for command
39 for (it2 = parameters.begin(); it2 != parameters.end(); it2++) {
40 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
43 //if the user changes the input directory command factory will send this info to us in the output parameter
44 string inputDir = validParameter.validFile(parameters, "inputdir", false);
45 if (inputDir == "not found"){ inputDir = ""; }
48 it2 = parameters.find("fasta");
49 //user has given a template file
50 if(it2 != parameters.end()){
51 path = m->hasPath(it2->second);
52 //if the user has not given a path then, add inputdir. else leave path alone.
53 if (path == "") { parameters["fasta"] = inputDir + it2->second; }
56 it2 = parameters.find("oldfasta");
57 //user has given a template file
58 if(it2 != parameters.end()){
59 path = m->hasPath(it2->second);
60 //if the user has not given a path then, add inputdir. else leave path alone.
61 if (path == "") { parameters["oldfasta"] = inputDir + it2->second; }
64 it2 = parameters.find("column");
65 //user has given a template file
66 if(it2 != parameters.end()){
67 path = m->hasPath(it2->second);
68 //if the user has not given a path then, add inputdir. else leave path alone.
69 if (path == "") { parameters["column"] = inputDir + it2->second; }
73 //check for required parameters
74 fastafile = validParameter.validFile(parameters, "fasta", true);
75 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the dist.seqs command."); m->mothurOutEndLine(); abort = true; }
76 else if (fastafile == "not open") { abort = true; }
79 m->openInputFile(fastafile, inFASTA);
80 alignDB = SequenceDB(inFASTA);
84 oldfastafile = validParameter.validFile(parameters, "oldfasta", true);
85 if (oldfastafile == "not found") { oldfastafile = ""; }
86 else if (oldfastafile == "not open") { abort = true; }
88 column = validParameter.validFile(parameters, "column", true);
89 if (column == "not found") { column = ""; }
90 else if (column == "not open") { abort = true; }
92 //if the user changes the output directory command factory will send this info to us in the output parameter
93 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
95 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
98 //check for optional parameter and set defaults
99 // ...at some point should added some additional type checking...
100 calc = validParameter.validFile(parameters, "calc", false);
101 if (calc == "not found") { calc = "onegap"; }
103 if (calc == "default") { calc = "onegap"; }
105 m->splitAtDash(calc, Estimators);
108 temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; }
109 convert(temp, countends);
111 temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; }
112 convert(temp, cutoff);
114 temp = validParameter.validFile(parameters, "processors", false); if(temp == "not found"){ temp = "1"; }
115 convert(temp, processors);
117 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "column"; }
119 if (((column != "") && (oldfastafile == "")) || ((column == "") && (oldfastafile != ""))) { m->mothurOut("If you provide column or oldfasta, you must provide both."); m->mothurOutEndLine(); abort=true; }
121 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; }
123 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"; }
125 ValidCalculators validCalculator;
127 if (m->isTrue(countends) == true) {
128 for (int i=0; i<Estimators.size(); i++) {
129 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
130 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
131 else if (Estimators[i] == "eachgap") { distCalculator = new eachGapDist(); }
132 else if (Estimators[i] == "onegap") { distCalculator = new oneGapDist(); }
136 for (int i=0; i<Estimators.size(); i++) {
137 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
138 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
139 else if (Estimators[i] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
140 else if (Estimators[i] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
148 catch(exception& e) {
149 m->errorOut(e, "DistanceCommand", "DistanceCommand");
154 //**********************************************************************************************************************
156 DistanceCommand::~DistanceCommand(){
158 for(int i=0;i<lines.size();i++){
164 //**********************************************************************************************************************
166 void DistanceCommand::help(){
168 m->mothurOut("The dist.seqs command reads a file containing sequences and creates a distance file.\n");
169 m->mothurOut("The dist.seqs command parameters are fasta, oldfasta, column, calc, countends, output, cutoff and processors. \n");
170 m->mothurOut("The fasta parameter is required.\n");
171 m->mothurOut("The oldfasta and column parameters allow you to append the distances calculated to the column file.\n");
172 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");
173 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");
174 m->mothurOut("The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0.\n");
175 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");
176 m->mothurOut("The processors parameter allows you to specify number of processors to use. The default is 1.\n");
177 m->mothurOut("The dist.seqs command should be in the following format: \n");
178 m->mothurOut("dist.seqs(fasta=yourFastaFile, calc=yourCalc, countends=yourEnds, cutoff= yourCutOff, processors=yourProcessors) \n");
179 m->mothurOut("Example dist.seqs(fasta=amazon.fasta, calc=eachgap, countends=F, cutoff= 2.0, processors=3).\n");
180 m->mothurOut("Note: No spaces between parameter labels (i.e. calc), '=' and parameters (i.e.yourCalc).\n\n");
182 catch(exception& e) {
183 m->errorOut(e, "DistanceCommand", "help");
187 //**********************************************************************************************************************
189 int DistanceCommand::execute(){
192 if (abort == true) { return 0; }
194 int startTime = time(NULL);
196 //save number of new sequence
197 numNewFasta = alignDB.getNumSeqs();
199 //sanity check the oldfasta and column file as well as add oldfasta sequences to alignDB
200 if ((oldfastafile != "") && (column != "")) { if (!(sanityCheck())) { return 0; } }
202 if (m->control_pressed) { return 0; }
204 int numSeqs = alignDB.getNumSeqs();
209 if (output == "lt") { //does the user want lower triangle phylip formatted file
210 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "phylip.dist";
211 remove(outputFile.c_str());
213 //output numSeqs to phylip formatted dist file
214 }else if (output == "column") { //user wants column format
215 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "dist";
217 //so we don't accidentally overwrite
218 if (outputFile == column) {
219 string tempcolumn = column + ".old";
220 rename(column.c_str(), tempcolumn.c_str());
223 remove(outputFile.c_str());
224 }else { //assume square
225 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "square.dist";
226 remove(outputFile.c_str());
236 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
237 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
239 //each process gets where it should start and stop in the file
240 if (output != "square") {
241 start = int (sqrt(float(pid)/float(processors)) * numSeqs);
242 end = int (sqrt(float(pid+1)/float(processors)) * numSeqs);
244 start = int ((float(pid)/float(processors)) * numSeqs);
245 end = int ((float(pid+1)/float(processors)) * numSeqs);
248 if (output == "column") {
250 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
252 //char* filename = new char[outputFile.length()];
253 //memcpy(filename, outputFile.c_str(), outputFile.length());
256 strcpy(filename, outputFile.c_str());
258 MPI_File_open(MPI_COMM_WORLD, filename, amode, MPI_INFO_NULL, &outMPI);
261 if (pid == 0) { //you are the root process
266 driverMPI(start, end, outMPI, cutoff);
268 if (m->control_pressed) { MPI_File_close(&outMPI); delete distCalculator; return 0; }
271 for(int i = 1; i < processors; i++) {
272 if (m->control_pressed) { MPI_File_close(&outMPI); delete distCalculator; return 0; }
275 MPI_Recv(buf, 4, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
277 }else { //you are a child process
279 driverMPI(start, end, outMPI, cutoff);
281 if (m->control_pressed) { MPI_File_close(&outMPI); delete distCalculator; return 0; }
285 //tell parent you are done.
286 MPI_Send(buf, 4, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
289 MPI_File_close(&outMPI);
291 }else { //lower triangle format
292 if (pid == 0) { //you are the root process
296 unsigned long int mySize;
298 if (output != "square"){ driverMPI(start, end, outputFile, mySize); }
299 else { driverMPI(start, end, outputFile, mySize, output); }
301 if (m->control_pressed) { delete distCalculator; return 0; }
303 int amode=MPI_MODE_APPEND|MPI_MODE_WRONLY|MPI_MODE_CREATE; //
307 //char* filename = new char[outputFile.length()];
308 //memcpy(filename, outputFile.c_str(), outputFile.length());
311 strcpy(filename, outputFile.c_str());
313 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
317 for(int b = 1; b < processors; b++) {
318 unsigned long int fileSize;
320 if (m->control_pressed) { MPI_File_close(&outMPI); delete distCalculator; return 0; }
322 MPI_Recv(&fileSize, 1, MPI_LONG, b, tag, MPI_COMM_WORLD, &status);
324 string outTemp = outputFile + toString(b) + ".temp";
326 char* buf = new char[outTemp.length()];
327 memcpy(buf, outTemp.c_str(), outTemp.length());
329 MPI_File_open(MPI_COMM_SELF, buf, MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
333 while (count < fileSize) {
335 MPI_File_read(inMPI, buf2, 1, MPI_CHAR, &status);
336 MPI_File_write(outMPI, buf2, 1, MPI_CHAR, &status);
340 MPI_File_close(&inMPI); //deleted on close
343 MPI_File_close(&outMPI);
344 }else { //you are a child process
346 unsigned long int size;
347 if (output != "square"){ driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size); }
348 else { driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size, output); }
350 if (m->control_pressed) { delete distCalculator; return 0; }
352 //tell parent you are done.
353 MPI_Send(&size, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
356 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
359 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
360 //if you don't need to fork anything
362 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
363 else { driver(0, numSeqs, outputFile, "square"); }
364 }else{ //you have multiple processors
366 for (int i = 0; i < processors; i++) {
367 lines.push_back(new linePair());
368 if (output != "square") {
369 lines[i]->start = int (sqrt(float(i)/float(processors)) * numSeqs);
370 lines[i]->end = int (sqrt(float(i+1)/float(processors)) * numSeqs);
372 lines[i]->start = int ((float(i)/float(processors)) * numSeqs);
373 lines[i]->end = int ((float(i+1)/float(processors)) * numSeqs);
377 createProcesses(outputFile);
379 map<int, int>::iterator it = processIDS.begin();
380 rename((outputFile + toString(it->second) + ".temp").c_str(), outputFile.c_str());
383 //append and remove temp files
384 for (; it != processIDS.end(); it++) {
385 m->appendFiles((outputFile + toString(it->second) + ".temp"), outputFile);
386 remove((outputFile + toString(it->second) + ".temp").c_str());
391 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
392 else { driver(0, numSeqs, outputFile, "square"); }
396 if (m->control_pressed) { delete distCalculator; remove(outputFile.c_str()); return 0; }
399 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
401 if (pid == 0) { //only one process should output to screen
404 //if (output == "square") { convertMatrix(outputFile); }
407 fileHandle.open(outputFile.c_str());
409 m->gobble(fileHandle);
410 if (fileHandle.eof()) { m->mothurOut(outputFile + " is blank. This can result if there are no distances below your cutoff."); m->mothurOutEndLine(); }
413 //append the old column file to the new one
414 if ((oldfastafile != "") && (column != "")) {
415 //we had to rename the column file so we didnt overwrite above, but we want to keep old name
416 if (outputFile == column) {
417 string tempcolumn = column + ".old";
418 m->appendFiles(tempcolumn, outputFile);
419 remove(tempcolumn.c_str());
421 m->appendFiles(outputFile, column);
422 remove(outputFile.c_str());
432 if (m->control_pressed) { delete distCalculator; remove(outputFile.c_str()); return 0; }
434 delete distCalculator;
436 m->mothurOutEndLine();
437 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
438 m->mothurOut(outputFile); m->mothurOutEndLine();
439 m->mothurOutEndLine();
440 m->mothurOut("It took " + toString(time(NULL) - startTime) + " to calculate the distances for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
444 catch(exception& e) {
445 m->errorOut(e, "DistanceCommand", "execute");
449 /**************************************************************************************************/
450 void DistanceCommand::createProcesses(string filename) {
452 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
456 //loop through and create all the processes you want
457 while (process != processors) {
461 processIDS[lines[process]->end] = pid; //create map from line number to pid so you can append files in correct order later
464 if (output != "square") { driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", cutoff); }
465 else { driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", "square"); }
467 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
470 //force parent to wait until all the processes are done
471 for (map<int, int>::iterator it = processIDS.begin(); it != processIDS.end(); it++) {
472 int temp = it->second;
477 catch(exception& e) {
478 m->errorOut(e, "DistanceCommand", "createProcesses");
483 /**************************************************************************************************/
484 /////// need to fix to work with calcs and sequencedb
485 int DistanceCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
488 int startTime = time(NULL);
491 ofstream outFile(dFileName.c_str(), ios::trunc);
492 outFile.setf(ios::fixed, ios::showpoint);
493 outFile << setprecision(4);
495 if((output == "lt") && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
497 for(int i=startLine;i<endLine;i++){
499 string name = alignDB.get(i).getName();
500 if (name.length() < 10) { //pad with spaces to make compatible
501 while (name.length() < 10) { name += " "; }
503 outFile << name << '\t';
505 for(int j=0;j<i;j++){
507 if (m->control_pressed) { outFile.close(); return 0; }
509 //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
510 //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
511 if ((i >= numNewFasta) && (j >= numNewFasta)) { break; }
513 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
514 double dist = distCalculator->getDist();
517 if (output == "column") { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
519 if (output == "lt") { outFile << dist << '\t'; }
522 if (output == "lt") { outFile << endl; }
525 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
529 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
535 catch(exception& e) {
536 m->errorOut(e, "DistanceCommand", "driver");
540 /**************************************************************************************************/
541 /////// need to fix to work with calcs and sequencedb
542 int DistanceCommand::driver(int startLine, int endLine, string dFileName, string square){
545 int startTime = time(NULL);
548 ofstream outFile(dFileName.c_str(), ios::trunc);
549 outFile.setf(ios::fixed, ios::showpoint);
550 outFile << setprecision(4);
552 if(startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
554 for(int i=startLine;i<endLine;i++){
556 string name = alignDB.get(i).getName();
557 //pad with spaces to make compatible
558 if (name.length() < 10) { while (name.length() < 10) { name += " "; } }
560 outFile << name << '\t';
562 for(int j=0;j<alignDB.getNumSeqs();j++){
564 if (m->control_pressed) { outFile.close(); return 0; }
566 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
567 double dist = distCalculator->getDist();
569 outFile << dist << '\t';
575 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
579 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
585 catch(exception& e) {
586 m->errorOut(e, "DistanceCommand", "driver");
591 /**************************************************************************************************/
592 /////// need to fix to work with calcs and sequencedb
593 int DistanceCommand::driverMPI(int startLine, int endLine, MPI_File& outMPI, float cutoff){
596 int startTime = time(NULL);
598 string outputString = "";
600 for(int i=startLine;i<endLine;i++){
602 for(int j=0;j<i;j++){
604 if (m->control_pressed) { return 0; }
606 //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
607 //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
608 if ((i >= numNewFasta) && (j >= numNewFasta)) { break; }
610 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
611 double dist = distCalculator->getDist();
614 outputString += (alignDB.get(i).getName() + ' ' + alignDB.get(j).getName() + ' ' + toString(dist) + '\n');
619 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
620 cout << i << '\t' << (time(NULL) - startTime) << endl;
624 //send results to parent
625 int length = outputString.length();
627 char* buf = new char[length];
628 memcpy(buf, outputString.c_str(), length);
630 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
636 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
637 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
640 catch(exception& e) {
641 m->errorOut(e, "DistanceCommand", "driverMPI");
645 /**************************************************************************************************/
646 /////// need to fix to work with calcs and sequencedb
647 int DistanceCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size){
652 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
654 //char* filename = new char[file.length()];
655 //memcpy(filename, file.c_str(), file.length());
658 strcpy(filename, file.c_str());
660 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
663 int startTime = time(NULL);
665 string outputString = "";
668 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
670 for(int i=startLine;i<endLine;i++){
672 string name = alignDB.get(i).getName();
673 if (name.length() < 10) { //pad with spaces to make compatible
674 while (name.length() < 10) { name += " "; }
676 outputString += name + "\t";
678 for(int j=0;j<i;j++){
680 if (m->control_pressed) { return 0; }
682 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
683 double dist = distCalculator->getDist();
685 outputString += toString(dist) + "\t";
688 outputString += "\n";
692 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
693 cout << i << '\t' << (time(NULL) - startTime) << endl;
697 //send results to parent
698 int length = outputString.length();
699 char* buf = new char[length];
700 memcpy(buf, outputString.c_str(), length);
702 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
703 size += outputString.length();
708 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
709 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
710 MPI_File_close(&outMPI);
714 catch(exception& e) {
715 m->errorOut(e, "DistanceCommand", "driverMPI");
719 /**************************************************************************************************/
720 /////// need to fix to work with calcs and sequencedb
721 int DistanceCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size, string square){
726 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
728 //char* filename = new char[file.length()];
729 //memcpy(filename, file.c_str(), file.length());
732 strcpy(filename, file.c_str());
734 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
737 int startTime = time(NULL);
739 string outputString = "";
742 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
744 for(int i=startLine;i<endLine;i++){
746 string name = alignDB.get(i).getName();
747 if (name.length() < 10) { //pad with spaces to make compatible
748 while (name.length() < 10) { name += " "; }
750 outputString += name + "\t";
752 for(int j=0;j<alignDB.getNumSeqs();j++){
754 if (m->control_pressed) { return 0; }
756 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
757 double dist = distCalculator->getDist();
759 outputString += toString(dist) + "\t";
762 outputString += "\n";
766 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
767 cout << i << '\t' << (time(NULL) - startTime) << endl;
771 //send results to parent
772 int length = outputString.length();
773 char* buf = new char[length];
774 memcpy(buf, outputString.c_str(), length);
776 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
777 size += outputString.length();
782 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
783 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
784 MPI_File_close(&outMPI);
788 catch(exception& e) {
789 m->errorOut(e, "DistanceCommand", "driverMPI");
794 /**************************************************************************************************
795 int DistanceCommand::convertMatrix(string outputFile) {
798 //sort file by first column so the distances for each row are together
799 string outfile = m->getRootName(outputFile) + "sorted.dist.temp";
802 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
803 string command = "sort -n " + outputFile + " -o " + outfile;
804 system(command.c_str());
805 #else //sort using windows sort
806 string command = "sort " + outputFile + " /O " + outfile;
807 system(command.c_str());
811 //output to new file distance for each row and save positions in file where new row begins
813 m->openInputFile(outfile, in);
816 m->openOutputFile(outputFile, out);
818 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
820 out << alignDB.getNumSeqs() << endl;
822 //get first currentRow
823 string first, currentRow, second;
825 map<string, float> rowDists; //take advantage of the fact that maps are already sorted by key
826 map<string, float>::iterator it;
831 rowDists[first] = 0.00; //distance to yourself is 0.0
834 //m->openInputFile(outfile, in);
837 if (m->control_pressed) { in.close(); remove(outfile.c_str()); out.close(); return 0; }
839 in >> first >> second >> dist; m->gobble(in);
841 if (first != currentRow) {
843 out << currentRow << '\t'; //print name
846 for (it = rowDists.begin(); it != rowDists.end(); it++) {
847 out << it->second << '\t';
854 rowDists[first] = 0.00;
855 rowDists[second] = dist;
857 rowDists[second] = dist;
861 out << currentRow << '\t'; //print name
864 for (it = rowDists.begin(); it != rowDists.end(); it++) {
865 out << it->second << '\t';
872 remove(outfile.c_str());
877 catch(exception& e) {
878 m->errorOut(e, "DistanceCommand", "convertMatrix");
882 /**************************************************************************************************
883 int DistanceCommand::convertToLowerTriangle(string outputFile) {
886 //sort file by first column so the distances for each row are together
887 string outfile = m->getRootName(outputFile) + "sorted.dist.temp";
890 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
891 string command = "sort -n " + outputFile + " -o " + outfile;
892 system(command.c_str());
893 #else //sort using windows sort
894 string command = "sort " + outputFile + " /O " + outfile;
895 system(command.c_str());
899 //output to new file distance for each row and save positions in file where new row begins
901 m->openInputFile(outfile, in);
904 m->openOutputFile(outputFile, out);
906 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
908 out << alignDB.getNumSeqs() << endl;
910 //get first currentRow
911 string first, currentRow, second;
915 map<string, float> rowDists; //take advantage of the fact that maps are already sorted by key
916 map<string, float>::iterator it;
921 rowDists[first] = 0.00; //distance to yourself is 0.0
924 //m->openInputFile(outfile, in);
927 if (m->control_pressed) { in.close(); remove(outfile.c_str()); out.close(); return 0; }
929 in >> first >> second >> dist; m->gobble(in);
931 if (first != currentRow) {
933 out << currentRow << '\t'; //print name
936 for (it = rowDists.begin(); it != rowDists.end(); it++) {
937 if (j >= i) { break; }
938 out << it->second << '\t';
946 rowDists[first] = 0.00;
947 rowDists[second] = dist;
951 rowDists[second] = dist;
955 out << currentRow << '\t'; //print name
958 for (it = rowDists.begin(); it != rowDists.end(); it++) {
959 out << it->second << '\t';
966 remove(outfile.c_str());
971 catch(exception& e) {
972 m->errorOut(e, "DistanceCommand", "convertToLowerTriangle");
976 /**************************************************************************************************/
977 //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,
978 //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.
979 //also check to make sure the 2 files have the same alignment length.
980 bool DistanceCommand::sanityCheck() {
984 //make sure the 2 fasta files have the same alignment length
986 m->openInputFile(fastafile, in);
987 int fastaAlignLength = 0;
990 fastaAlignLength = tempIn.getAligned().length();
995 m->openInputFile(oldfastafile, in2);
996 int oldfastaAlignLength = 0;
998 Sequence tempIn2(in2);
999 oldfastaAlignLength = tempIn2.getAligned().length();
1003 if (fastaAlignLength != oldfastaAlignLength) { m->mothurOut("fasta files do not have the same alignment length."); m->mothurOutEndLine(); return false; }
1005 //read fasta file and save names as well as adding them to the alignDB
1006 set<string> namesOldFasta;
1009 m->openInputFile(oldfastafile, inFasta);
1011 while (!inFasta.eof()) {
1012 if (m->control_pressed) { inFasta.close(); return good; }
1014 Sequence temp(inFasta);
1016 if (temp.getName() != "") {
1017 namesOldFasta.insert(temp.getName()); //save name
1018 alignDB.push_back(temp); //add to DB
1026 //read through the column file checking names and removing distances above the cutoff
1028 m->openInputFile(column, inDist);
1031 string outputFile = column + ".temp";
1032 m->openOutputFile(outputFile, outDist);
1034 string name1, name2;
1036 while (!inDist.eof()) {
1037 if (m->control_pressed) { inDist.close(); outDist.close(); remove(outputFile.c_str()); return good; }
1039 inDist >> name1 >> name2 >> dist; m->gobble(inDist);
1041 //both names are in fasta file and distance is below cutoff
1042 if ((namesOldFasta.count(name1) == 0) || (namesOldFasta.count(name2) == 0)) { good = false; break; }
1044 if (dist <= cutoff) {
1045 outDist << name1 << '\t' << name2 << '\t' << dist << endl;
1054 remove(column.c_str());
1055 rename(outputFile.c_str(), column.c_str());
1057 remove(outputFile.c_str()); //temp file is bad because file mismatch above
1061 catch(exception& e) {
1062 m->errorOut(e, "DistanceCommand", "m->appendFiles");
1066 /**************************************************************************************************/