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 //**********************************************************************************************************************
18 vector<string> DistanceCommand::setParameters(){
20 CommandParameter pcolumn("column", "InputTypes", "", "", "none", "none", "OldFastaColumn",false,false); parameters.push_back(pcolumn);
21 CommandParameter poldfasta("oldfasta", "InputTypes", "", "", "none", "none", "OldFastaColumn",false,false); parameters.push_back(poldfasta);
22 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
23 CommandParameter poutput("output", "Multiple", "column-lt-square", "column", "", "", "",false,false); parameters.push_back(poutput);
24 CommandParameter pcalc("calc", "Multiple", "nogaps-eachgap-onegap", "onegap", "", "", "",false,false); parameters.push_back(pcalc);
25 CommandParameter pcountends("countends", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pcountends);
26 CommandParameter pcompress("compress", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pcompress);
27 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
28 CommandParameter pcutoff("cutoff", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pcutoff);
29 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
30 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
32 vector<string> myArray;
33 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
37 m->errorOut(e, "DistanceCommand", "setParameters");
41 //**********************************************************************************************************************
42 string DistanceCommand::getHelpString(){
44 string helpString = "";
45 helpString += "The dist.seqs command reads a file containing sequences and creates a distance file.\n";
46 helpString += "The dist.seqs command parameters are fasta, oldfasta, column, calc, countends, output, compress, cutoff and processors. \n";
47 helpString += "The fasta parameter is required, unless you have a valid current fasta file.\n";
48 helpString += "The oldfasta and column parameters allow you to append the distances calculated to the column file.\n";
49 helpString += "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";
50 helpString += "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";
51 helpString += "The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0.\n";
52 helpString += "The output parameter allows you to specify format of your distance matrix. Options are column, lt, and square. The default is column.\n";
53 helpString += "The processors parameter allows you to specify number of processors to use. The default is 1.\n";
54 helpString += "The compress parameter allows you to indicate that you want the resulting distance file compressed. The default is false.\n";
55 helpString += "The dist.seqs command should be in the following format: \n";
56 helpString += "dist.seqs(fasta=yourFastaFile, calc=yourCalc, countends=yourEnds, cutoff= yourCutOff, processors=yourProcessors) \n";
57 helpString += "Example dist.seqs(fasta=amazon.fasta, calc=eachgap, countends=F, cutoff= 2.0, processors=3).\n";
58 helpString += "Note: No spaces between parameter labels (i.e. calc), '=' and parameters (i.e.yourCalc).\n";
62 m->errorOut(e, "DistanceCommand", "getHelpString");
66 //**********************************************************************************************************************
67 DistanceCommand::DistanceCommand(){
69 abort = true; calledHelp = true;
71 vector<string> tempOutNames;
72 outputTypes["phylip"] = tempOutNames;
73 outputTypes["column"] = tempOutNames;
76 m->errorOut(e, "DistanceCommand", "DistanceCommand");
80 //**********************************************************************************************************************
81 DistanceCommand::DistanceCommand(string option) {
83 abort = false; calledHelp = false;
86 //allow user to run help
87 if(option == "help") { help(); abort = true; calledHelp = true; }
90 vector<string> myArray = setParameters();
92 OptionParser parser(option);
93 map<string, string> parameters = parser.getParameters();
95 ValidParameters validParameter("dist.seqs");
96 map<string, string>::iterator it2;
98 //check to make sure all parameters are valid for command
99 for (it2 = parameters.begin(); it2 != parameters.end(); it2++) {
100 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
103 //initialize outputTypes
104 vector<string> tempOutNames;
105 outputTypes["phylip"] = tempOutNames;
106 outputTypes["column"] = tempOutNames;
108 //if the user changes the input directory command factory will send this info to us in the output parameter
109 string inputDir = validParameter.validFile(parameters, "inputdir", false);
110 if (inputDir == "not found"){ inputDir = ""; }
113 it2 = parameters.find("fasta");
114 //user has given a template file
115 if(it2 != parameters.end()){
116 path = m->hasPath(it2->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["fasta"] = inputDir + it2->second; }
121 it2 = parameters.find("oldfasta");
122 //user has given a template file
123 if(it2 != parameters.end()){
124 path = m->hasPath(it2->second);
125 //if the user has not given a path then, add inputdir. else leave path alone.
126 if (path == "") { parameters["oldfasta"] = inputDir + it2->second; }
129 it2 = parameters.find("column");
130 //user has given a template file
131 if(it2 != parameters.end()){
132 path = m->hasPath(it2->second);
133 //if the user has not given a path then, add inputdir. else leave path alone.
134 if (path == "") { parameters["column"] = inputDir + it2->second; }
138 //check for required parameters
139 fastafile = validParameter.validFile(parameters, "fasta", true);
140 if (fastafile == "not found") {
141 fastafile = m->getFastaFile();
142 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine();
144 m->openInputFile(fastafile, inFASTA);
145 alignDB = SequenceDB(inFASTA);
147 }else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
148 }else if (fastafile == "not open") { abort = true; }
151 m->openInputFile(fastafile, inFASTA);
152 alignDB = SequenceDB(inFASTA);
156 oldfastafile = validParameter.validFile(parameters, "oldfasta", true);
157 if (oldfastafile == "not found") { oldfastafile = ""; }
158 else if (oldfastafile == "not open") { abort = true; }
160 column = validParameter.validFile(parameters, "column", true);
161 if (column == "not found") { column = ""; }
162 else if (column == "not open") { abort = true; }
164 //if the user changes the output directory command factory will send this info to us in the output parameter
165 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
167 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
170 //check for optional parameter and set defaults
171 // ...at some point should added some additional type checking...
172 calc = validParameter.validFile(parameters, "calc", false);
173 if (calc == "not found") { calc = "onegap"; }
175 if (calc == "default") { calc = "onegap"; }
177 m->splitAtDash(calc, Estimators);
180 temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; }
181 convert(temp, countends);
183 temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; }
184 convert(temp, cutoff);
186 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
187 m->setProcessors(temp);
188 convert(temp, processors);
190 temp = validParameter.validFile(parameters, "compress", false); if(temp == "not found"){ temp = "F"; }
191 convert(temp, compress);
193 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "column"; }
195 if (((column != "") && (oldfastafile == "")) || ((column == "") && (oldfastafile != ""))) { m->mothurOut("If you provide column or oldfasta, you must provide both."); m->mothurOutEndLine(); abort=true; }
197 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; }
199 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"; }
201 ValidCalculators validCalculator;
203 if (m->isTrue(countends) == true) {
204 for (int i=0; i<Estimators.size(); i++) {
205 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
206 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
207 else if (Estimators[i] == "eachgap") { distCalculator = new eachGapDist(); }
208 else if (Estimators[i] == "onegap") { distCalculator = new oneGapDist(); }
212 for (int i=0; i<Estimators.size(); i++) {
213 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
214 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
215 else if (Estimators[i] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
216 else if (Estimators[i] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
224 catch(exception& e) {
225 m->errorOut(e, "DistanceCommand", "DistanceCommand");
229 //**********************************************************************************************************************
231 int DistanceCommand::execute(){
234 if (abort == true) { if (calledHelp) { return 0; } return 2; }
236 int startTime = time(NULL);
238 //save number of new sequence
239 numNewFasta = alignDB.getNumSeqs();
241 //sanity check the oldfasta and column file as well as add oldfasta sequences to alignDB
242 if ((oldfastafile != "") && (column != "")) { if (!(sanityCheck())) { return 0; } }
244 if (m->control_pressed) { return 0; }
246 int numSeqs = alignDB.getNumSeqs();
251 if (output == "lt") { //does the user want lower triangle phylip formatted file
252 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "phylip.dist";
253 remove(outputFile.c_str()); outputTypes["phylip"].push_back(outputFile);
255 //output numSeqs to phylip formatted dist file
256 }else if (output == "column") { //user wants column format
257 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "dist";
258 outputTypes["column"].push_back(outputFile);
260 //so we don't accidentally overwrite
261 if (outputFile == column) {
262 string tempcolumn = column + ".old";
263 rename(column.c_str(), tempcolumn.c_str());
266 remove(outputFile.c_str());
267 }else { //assume square
268 outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "square.dist";
269 remove(outputFile.c_str());
270 outputTypes["phylip"].push_back(outputFile);
280 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
281 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
283 //each process gets where it should start and stop in the file
284 if (output != "square") {
285 start = int (sqrt(float(pid)/float(processors)) * numSeqs);
286 end = int (sqrt(float(pid+1)/float(processors)) * numSeqs);
288 start = int ((float(pid)/float(processors)) * numSeqs);
289 end = int ((float(pid+1)/float(processors)) * numSeqs);
292 if (output == "column") {
294 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
296 //char* filename = new char[outputFile.length()];
297 //memcpy(filename, outputFile.c_str(), outputFile.length());
300 strcpy(filename, outputFile.c_str());
302 MPI_File_open(MPI_COMM_WORLD, filename, amode, MPI_INFO_NULL, &outMPI);
305 if (pid == 0) { //you are the root process
310 driverMPI(start, end, outMPI, cutoff);
312 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); delete distCalculator; return 0; }
315 for(int i = 1; i < processors; i++) {
316 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); delete distCalculator; return 0; }
319 MPI_Recv(buf, 5, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
321 }else { //you are a child process
323 driverMPI(start, end, outMPI, cutoff);
325 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); delete distCalculator; return 0; }
329 //tell parent you are done.
330 MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
333 MPI_File_close(&outMPI);
335 }else { //lower triangle format
336 if (pid == 0) { //you are the root process
340 unsigned long int mySize;
342 if (output != "square"){ driverMPI(start, end, outputFile, mySize); }
343 else { driverMPI(start, end, outputFile, mySize, output); }
345 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; return 0; }
347 int amode=MPI_MODE_APPEND|MPI_MODE_WRONLY|MPI_MODE_CREATE; //
351 //char* filename = new char[outputFile.length()];
352 //memcpy(filename, outputFile.c_str(), outputFile.length());
355 strcpy(filename, outputFile.c_str());
357 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
361 for(int b = 1; b < processors; b++) {
362 unsigned long int fileSize;
364 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); delete distCalculator; return 0; }
366 MPI_Recv(&fileSize, 1, MPI_LONG, b, tag, MPI_COMM_WORLD, &status);
368 string outTemp = outputFile + toString(b) + ".temp";
370 char* buf = new char[outTemp.length()];
371 memcpy(buf, outTemp.c_str(), outTemp.length());
373 MPI_File_open(MPI_COMM_SELF, buf, MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
377 while (count < fileSize) {
379 MPI_File_read(inMPI, buf2, 1, MPI_CHAR, &status);
380 MPI_File_write(outMPI, buf2, 1, MPI_CHAR, &status);
384 MPI_File_close(&inMPI); //deleted on close
387 MPI_File_close(&outMPI);
388 }else { //you are a child process
390 unsigned long int size;
391 if (output != "square"){ driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size); }
392 else { driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size, output); }
394 if (m->control_pressed) { delete distCalculator; return 0; }
396 //tell parent you are done.
397 MPI_Send(&size, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
400 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
403 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
404 //if you don't need to fork anything
406 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
407 else { driver(0, numSeqs, outputFile, "square"); }
408 }else{ //you have multiple processors
410 unsigned long int numDists = 0;
412 if (output == "square") {
413 numDists = numSeqs * numSeqs;
415 for(int i=0;i<numSeqs;i++){
416 for(int j=0;j<i;j++){
418 if (numDists > processors) { break; }
423 if (numDists < processors) { processors = numDists; }
425 for (int i = 0; i < processors; i++) {
426 distlinePair tempLine;
427 lines.push_back(tempLine);
428 if (output != "square") {
429 lines[i].start = int (sqrt(float(i)/float(processors)) * numSeqs);
430 lines[i].end = int (sqrt(float(i+1)/float(processors)) * numSeqs);
432 lines[i].start = int ((float(i)/float(processors)) * numSeqs);
433 lines[i].end = int ((float(i+1)/float(processors)) * numSeqs);
438 createProcesses(outputFile);
442 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
443 else { driver(0, numSeqs, outputFile, "square"); }
447 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; remove(outputFile.c_str()); return 0; }
450 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
452 if (pid == 0) { //only one process should output to screen
455 //if (output == "square") { convertMatrix(outputFile); }
458 fileHandle.open(outputFile.c_str());
460 m->gobble(fileHandle);
461 if (fileHandle.eof()) { m->mothurOut(outputFile + " is blank. This can result if there are no distances below your cutoff."); m->mothurOutEndLine(); }
464 //append the old column file to the new one
465 if ((oldfastafile != "") && (column != "")) {
466 //we had to rename the column file so we didnt overwrite above, but we want to keep old name
467 if (outputFile == column) {
468 string tempcolumn = column + ".old";
469 m->appendFiles(tempcolumn, outputFile);
470 remove(tempcolumn.c_str());
472 m->appendFiles(outputFile, column);
473 remove(outputFile.c_str());
477 if (outputDir != "") {
478 string newOutputName = outputDir + m->getSimpleName(outputFile);
479 rename(outputFile.c_str(), newOutputName.c_str());
480 remove(outputFile.c_str());
481 outputFile = newOutputName;
490 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; remove(outputFile.c_str()); return 0; }
492 delete distCalculator;
494 //set phylip file as new current phylipfile
496 itTypes = outputTypes.find("phylip");
497 if (itTypes != outputTypes.end()) {
498 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
501 //set column file as new current columnfile
502 itTypes = outputTypes.find("column");
503 if (itTypes != outputTypes.end()) {
504 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setColumnFile(current); }
507 m->mothurOutEndLine();
508 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
509 m->mothurOut(outputFile); m->mothurOutEndLine();
510 m->mothurOutEndLine();
511 m->mothurOut("It took " + toString(time(NULL) - startTime) + " to calculate the distances for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
514 if (m->isTrue(compress)) {
515 m->mothurOut("Compressing..."); m->mothurOutEndLine();
516 m->mothurOut("(Replacing " + outputFile + " with " + outputFile + ".gz)"); m->mothurOutEndLine();
517 system(("gzip -v " + outputFile).c_str());
518 outputNames.push_back(outputFile + ".gz");
519 }else { outputNames.push_back(outputFile); }
524 catch(exception& e) {
525 m->errorOut(e, "DistanceCommand", "execute");
529 /**************************************************************************************************/
530 void DistanceCommand::createProcesses(string filename) {
532 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
536 //loop through and create all the processes you want
537 while (process != processors) {
541 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
544 if (output != "square") { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", cutoff); }
545 else { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", "square"); }
548 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
550 for (int i=0;i<processIDS.size();i++) { int temp = processIDS[i]; kill (temp, SIGINT); }
555 //parent does its part
556 if (output != "square") { driver(lines[0].start, lines[0].end, filename, cutoff); }
557 else { driver(lines[0].start, lines[0].end, filename, "square"); }
560 //force parent to wait until all the processes are done
561 for (int i=0;i<processIDS.size();i++) {
562 int temp = processIDS[i];
566 //append and remove temp files
567 for (int i=0;i<processIDS.size();i++) {
568 m->appendFiles((filename + toString(processIDS[i]) + ".temp"), filename);
569 remove((filename + toString(processIDS[i]) + ".temp").c_str());
573 catch(exception& e) {
574 m->errorOut(e, "DistanceCommand", "createProcesses");
579 /**************************************************************************************************/
580 /////// need to fix to work with calcs and sequencedb
581 int DistanceCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
584 int startTime = time(NULL);
587 ofstream outFile(dFileName.c_str(), ios::trunc);
588 outFile.setf(ios::fixed, ios::showpoint);
589 outFile << setprecision(4);
591 if((output == "lt") && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
593 for(int i=startLine;i<endLine;i++){
595 string name = alignDB.get(i).getName();
596 if (name.length() < 10) { //pad with spaces to make compatible
597 while (name.length() < 10) { name += " "; }
599 outFile << name << '\t';
601 for(int j=0;j<i;j++){
603 if (m->control_pressed) { outFile.close(); return 0; }
605 //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
606 //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
607 if ((i >= numNewFasta) && (j >= numNewFasta)) { break; }
609 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
610 double dist = distCalculator->getDist();
613 if (output == "column") { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
615 if (output == "lt") { outFile << dist << '\t'; }
618 if (output == "lt") { outFile << endl; }
621 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
625 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
631 catch(exception& e) {
632 m->errorOut(e, "DistanceCommand", "driver");
636 /**************************************************************************************************/
637 /////// need to fix to work with calcs and sequencedb
638 int DistanceCommand::driver(int startLine, int endLine, string dFileName, string square){
641 int startTime = time(NULL);
644 ofstream outFile(dFileName.c_str(), ios::trunc);
645 outFile.setf(ios::fixed, ios::showpoint);
646 outFile << setprecision(4);
648 if(startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
650 for(int i=startLine;i<endLine;i++){
652 string name = alignDB.get(i).getName();
653 //pad with spaces to make compatible
654 if (name.length() < 10) { while (name.length() < 10) { name += " "; } }
656 outFile << name << '\t';
658 for(int j=0;j<alignDB.getNumSeqs();j++){
660 if (m->control_pressed) { outFile.close(); return 0; }
662 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
663 double dist = distCalculator->getDist();
665 outFile << dist << '\t';
671 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
675 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
681 catch(exception& e) {
682 m->errorOut(e, "DistanceCommand", "driver");
687 /**************************************************************************************************/
688 /////// need to fix to work with calcs and sequencedb
689 int DistanceCommand::driverMPI(int startLine, int endLine, MPI_File& outMPI, float cutoff){
692 int startTime = time(NULL);
694 string outputString = "";
696 for(int i=startLine;i<endLine;i++){
698 for(int j=0;j<i;j++){
700 if (m->control_pressed) { return 0; }
702 //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
703 //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
704 if ((i >= numNewFasta) && (j >= numNewFasta)) { break; }
706 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
707 double dist = distCalculator->getDist();
710 outputString += (alignDB.get(i).getName() + ' ' + alignDB.get(j).getName() + ' ' + toString(dist) + '\n');
715 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
716 cout << i << '\t' << (time(NULL) - startTime) << endl;
720 //send results to parent
721 int length = outputString.length();
723 char* buf = new char[length];
724 memcpy(buf, outputString.c_str(), length);
726 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
732 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
733 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
736 catch(exception& e) {
737 m->errorOut(e, "DistanceCommand", "driverMPI");
741 /**************************************************************************************************/
742 /////// need to fix to work with calcs and sequencedb
743 int DistanceCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size){
748 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
750 //char* filename = new char[file.length()];
751 //memcpy(filename, file.c_str(), file.length());
754 strcpy(filename, file.c_str());
756 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
759 int startTime = time(NULL);
761 string outputString = "";
764 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
766 for(int i=startLine;i<endLine;i++){
768 string name = alignDB.get(i).getName();
769 if (name.length() < 10) { //pad with spaces to make compatible
770 while (name.length() < 10) { name += " "; }
772 outputString += name + "\t";
774 for(int j=0;j<i;j++){
776 if (m->control_pressed) { return 0; }
778 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
779 double dist = distCalculator->getDist();
781 outputString += toString(dist) + "\t";
784 outputString += "\n";
788 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
789 cout << i << '\t' << (time(NULL) - startTime) << endl;
793 //send results to parent
794 int length = outputString.length();
795 char* buf = new char[length];
796 memcpy(buf, outputString.c_str(), length);
798 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
799 size += outputString.length();
804 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
805 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
806 MPI_File_close(&outMPI);
810 catch(exception& e) {
811 m->errorOut(e, "DistanceCommand", "driverMPI");
815 /**************************************************************************************************/
816 /////// need to fix to work with calcs and sequencedb
817 int DistanceCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size, string square){
822 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
824 //char* filename = new char[file.length()];
825 //memcpy(filename, file.c_str(), file.length());
828 strcpy(filename, file.c_str());
830 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
833 int startTime = time(NULL);
835 string outputString = "";
838 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
840 for(int i=startLine;i<endLine;i++){
842 string name = alignDB.get(i).getName();
843 if (name.length() < 10) { //pad with spaces to make compatible
844 while (name.length() < 10) { name += " "; }
846 outputString += name + "\t";
848 for(int j=0;j<alignDB.getNumSeqs();j++){
850 if (m->control_pressed) { return 0; }
852 distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
853 double dist = distCalculator->getDist();
855 outputString += toString(dist) + "\t";
858 outputString += "\n";
862 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
863 cout << i << '\t' << (time(NULL) - startTime) << endl;
867 //send results to parent
868 int length = outputString.length();
869 char* buf = new char[length];
870 memcpy(buf, outputString.c_str(), length);
872 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
873 size += outputString.length();
878 //m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
879 cout << (endLine-1) << '\t' << (time(NULL) - startTime) << endl;
880 MPI_File_close(&outMPI);
884 catch(exception& e) {
885 m->errorOut(e, "DistanceCommand", "driverMPI");
890 /**************************************************************************************************
891 int DistanceCommand::convertMatrix(string outputFile) {
894 //sort file by first column so the distances for each row are together
895 string outfile = m->getRootName(outputFile) + "sorted.dist.temp";
898 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
899 string command = "sort -n " + outputFile + " -o " + outfile;
900 system(command.c_str());
901 #else //sort using windows sort
902 string command = "sort " + outputFile + " /O " + outfile;
903 system(command.c_str());
907 //output to new file distance for each row and save positions in file where new row begins
909 m->openInputFile(outfile, in);
912 m->openOutputFile(outputFile, out);
914 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
916 out << alignDB.getNumSeqs() << endl;
918 //get first currentRow
919 string first, currentRow, second;
921 map<string, float> rowDists; //take advantage of the fact that maps are already sorted by key
922 map<string, float>::iterator it;
927 rowDists[first] = 0.00; //distance to yourself is 0.0
930 //m->openInputFile(outfile, in);
933 if (m->control_pressed) { in.close(); remove(outfile.c_str()); out.close(); return 0; }
935 in >> first >> second >> dist; m->gobble(in);
937 if (first != currentRow) {
939 out << currentRow << '\t'; //print name
942 for (it = rowDists.begin(); it != rowDists.end(); it++) {
943 out << it->second << '\t';
950 rowDists[first] = 0.00;
951 rowDists[second] = dist;
953 rowDists[second] = dist;
957 out << currentRow << '\t'; //print name
960 for (it = rowDists.begin(); it != rowDists.end(); it++) {
961 out << it->second << '\t';
968 remove(outfile.c_str());
973 catch(exception& e) {
974 m->errorOut(e, "DistanceCommand", "convertMatrix");
978 /**************************************************************************************************
979 int DistanceCommand::convertToLowerTriangle(string outputFile) {
982 //sort file by first column so the distances for each row are together
983 string outfile = m->getRootName(outputFile) + "sorted.dist.temp";
986 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
987 string command = "sort -n " + outputFile + " -o " + outfile;
988 system(command.c_str());
989 #else //sort using windows sort
990 string command = "sort " + outputFile + " /O " + outfile;
991 system(command.c_str());
995 //output to new file distance for each row and save positions in file where new row begins
997 m->openInputFile(outfile, in);
1000 m->openOutputFile(outputFile, out);
1002 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
1004 out << alignDB.getNumSeqs() << endl;
1006 //get first currentRow
1007 string first, currentRow, second;
1011 map<string, float> rowDists; //take advantage of the fact that maps are already sorted by key
1012 map<string, float>::iterator it;
1017 rowDists[first] = 0.00; //distance to yourself is 0.0
1020 //m->openInputFile(outfile, in);
1023 if (m->control_pressed) { in.close(); remove(outfile.c_str()); out.close(); return 0; }
1025 in >> first >> second >> dist; m->gobble(in);
1027 if (first != currentRow) {
1028 //print out last row
1029 out << currentRow << '\t'; //print name
1032 for (it = rowDists.begin(); it != rowDists.end(); it++) {
1033 if (j >= i) { break; }
1034 out << it->second << '\t';
1042 rowDists[first] = 0.00;
1043 rowDists[second] = dist;
1047 rowDists[second] = dist;
1050 //print out last row
1051 out << currentRow << '\t'; //print name
1054 for (it = rowDists.begin(); it != rowDists.end(); it++) {
1055 out << it->second << '\t';
1062 remove(outfile.c_str());
1067 catch(exception& e) {
1068 m->errorOut(e, "DistanceCommand", "convertToLowerTriangle");
1072 /**************************************************************************************************/
1073 //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,
1074 //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.
1075 //also check to make sure the 2 files have the same alignment length.
1076 bool DistanceCommand::sanityCheck() {
1080 //make sure the 2 fasta files have the same alignment length
1082 m->openInputFile(fastafile, in);
1083 int fastaAlignLength = 0;
1085 Sequence tempIn(in);
1086 fastaAlignLength = tempIn.getAligned().length();
1091 m->openInputFile(oldfastafile, in2);
1092 int oldfastaAlignLength = 0;
1094 Sequence tempIn2(in2);
1095 oldfastaAlignLength = tempIn2.getAligned().length();
1099 if (fastaAlignLength != oldfastaAlignLength) { m->mothurOut("fasta files do not have the same alignment length."); m->mothurOutEndLine(); return false; }
1101 //read fasta file and save names as well as adding them to the alignDB
1102 set<string> namesOldFasta;
1105 m->openInputFile(oldfastafile, inFasta);
1107 while (!inFasta.eof()) {
1108 if (m->control_pressed) { inFasta.close(); return good; }
1110 Sequence temp(inFasta);
1112 if (temp.getName() != "") {
1113 namesOldFasta.insert(temp.getName()); //save name
1114 alignDB.push_back(temp); //add to DB
1122 //read through the column file checking names and removing distances above the cutoff
1124 m->openInputFile(column, inDist);
1127 string outputFile = column + ".temp";
1128 m->openOutputFile(outputFile, outDist);
1130 string name1, name2;
1132 while (!inDist.eof()) {
1133 if (m->control_pressed) { inDist.close(); outDist.close(); remove(outputFile.c_str()); return good; }
1135 inDist >> name1 >> name2 >> dist; m->gobble(inDist);
1137 //both names are in fasta file and distance is below cutoff
1138 if ((namesOldFasta.count(name1) == 0) || (namesOldFasta.count(name2) == 0)) { good = false; break; }
1140 if (dist <= cutoff) {
1141 outDist << name1 << '\t' << name2 << '\t' << dist << endl;
1150 remove(column.c_str());
1151 rename(outputFile.c_str(), column.c_str());
1153 remove(outputFile.c_str()); //temp file is bad because file mismatch above
1159 catch(exception& e) {
1160 m->errorOut(e, "DistanceCommand", "sanityCheck");
1164 /**************************************************************************************************/